blob: ae34d2007f570dcc7cb3d6f571ec1e58b7a0195d [file] [log] [blame]
Steven Rostedtdcc60242009-04-29 22:52:21 -04001#!/usr/bin/perl -w
2#
Uwe Kleine-Königcce1dac2011-01-24 21:12:01 +01003# Copyright 2005-2009 - Steven Rostedt
Steven Rostedtdcc60242009-04-29 22:52:21 -04004# Licensed under the terms of the GNU GPL License version 2
5#
6# It's simple enough to figure out how this works.
7# If not, then you can ask me at stripconfig@goodmis.org
8#
9# What it does?
10#
11# If you have installed a Linux kernel from a distribution
12# that turns on way too many modules than you need, and
13# you only want the modules you use, then this program
14# is perfect for you.
15#
16# It gives you the ability to turn off all the modules that are
17# not loaded on your system.
18#
19# Howto:
20#
21# 1. Boot up the kernel that you want to stream line the config on.
22# 2. Change directory to the directory holding the source of the
23# kernel that you just booted.
24# 3. Copy the configuraton file to this directory as .config
25# 4. Have all your devices that you need modules for connected and
26# operational (make sure that their corresponding modules are loaded)
27# 5. Run this script redirecting the output to some other file
28# like config_strip.
29# 6. Back up your old config (if you want too).
30# 7. copy the config_strip file to .config
31# 8. Run "make oldconfig"
32#
33# Now your kernel is ready to be built with only the modules that
34# are loaded.
35#
36# Here's what I did with my Debian distribution.
37#
38# cd /usr/src/linux-2.6.10
39# cp /boot/config-2.6.10-1-686-smp .config
40# ~/bin/streamline_config > config_strip
41# mv .config config_sav
42# mv config_strip .config
43# make oldconfig
44#
hiromucf5a1892010-08-17 19:49:18 +090045use strict;
46
Steven Rostedtdcc60242009-04-29 22:52:21 -040047my $config = ".config";
Steven Rostedtdcc60242009-04-29 22:52:21 -040048
Steven Rostedtcdfc4792009-04-30 14:39:48 -040049my $uname = `uname -r`;
50chomp $uname;
51
52my @searchconfigs = (
53 {
Steven Rostedta9024832009-05-07 11:01:34 -040054 "file" => ".config",
55 "exec" => "cat",
56 },
57 {
Steven Rostedtcdfc4792009-04-30 14:39:48 -040058 "file" => "/proc/config.gz",
59 "exec" => "zcat",
60 },
61 {
Steven Rostedt810b2be2009-04-30 19:30:04 -040062 "file" => "/boot/config-$uname",
63 "exec" => "cat",
64 },
65 {
Steven Rostedtcdfc4792009-04-30 14:39:48 -040066 "file" => "/boot/vmlinuz-$uname",
67 "exec" => "scripts/extract-ikconfig",
68 "test" => "scripts/extract-ikconfig",
69 },
70 {
71 "file" => "vmlinux",
72 "exec" => "scripts/extract-ikconfig",
73 "test" => "scripts/extract-ikconfig",
74 },
75 {
76 "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
77 "exec" => "scripts/extract-ikconfig",
78 "test" => "scripts/extract-ikconfig",
79 },
80 {
81 "file" => "kernel/configs.ko",
82 "exec" => "scripts/extract-ikconfig",
83 "test" => "scripts/extract-ikconfig",
84 },
85 {
86 "file" => "kernel/configs.o",
87 "exec" => "scripts/extract-ikconfig",
88 "test" => "scripts/extract-ikconfig",
89 },
Steven Rostedtcdfc4792009-04-30 14:39:48 -040090);
91
92sub find_config {
93 foreach my $conf (@searchconfigs) {
94 my $file = $conf->{"file"};
95
96 next if ( ! -f "$file");
97
98 if (defined($conf->{"test"})) {
99 `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
100 next if ($?);
101 }
102
103 my $exec = $conf->{"exec"};
104
105 print STDERR "using config: '$file'\n";
106
107 open(CIN, "$exec $file |") || die "Failed to run $exec $file";
108 return;
109 }
110 die "No config file found";
111}
112
113find_config;
114
Steven Rostedt463bf902009-11-20 09:21:12 -0500115# Get the build source and top level Kconfig file (passed in)
116my $ksource = $ARGV[0];
117my $kconfig = $ARGV[1];
Arnaud Lacombef597a712011-07-01 17:45:31 -0400118my $lsmod_file = $ENV{'LSMOD'};
Steven Rostedt463bf902009-11-20 09:21:12 -0500119
Toralf Förster17431922010-05-26 20:22:02 +0200120my @makefiles = `find $ksource -name Makefile 2>/dev/null`;
121chomp @makefiles;
122
Steven Rostedtdcc60242009-04-29 22:52:21 -0400123my %depends;
124my %selects;
125my %prompts;
126my %objects;
127my $var;
Steven Rostedt13d7e932010-01-06 17:56:12 -0500128my $iflevel = 0;
129my @ifdeps;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400130
Steven Rostedtdcc60242009-04-29 22:52:21 -0400131# prevent recursion
132my %read_kconfigs;
133
134sub read_kconfig {
135 my ($kconfig) = @_;
136
137 my $state = "NONE";
138 my $config;
139 my @kconfigs;
140
Steven Rostedt20d19042010-10-28 22:13:51 -0400141 my $cont = 0;
142 my $line;
143
Steven Rostedt49089802010-10-29 00:56:46 -0400144 my $source = "$ksource/$kconfig";
145 my $last_source = "";
146
147 # Check for any environment variables used
148 while ($source =~ /\$(\w+)/ && $last_source ne $source) {
149 my $env = $1;
150 $last_source = $source;
151 $source =~ s/\$$env/$ENV{$env}/;
152 }
153
154 open(KIN, "$source") || die "Can't open $kconfig";
Steven Rostedtdcc60242009-04-29 22:52:21 -0400155 while (<KIN>) {
156 chomp;
157
Steven Rostedt20d19042010-10-28 22:13:51 -0400158 # Make sure that lines ending with \ continue
159 if ($cont) {
160 $_ = $line . " " . $_;
161 }
162
163 if (s/\\$//) {
164 $cont = 1;
165 $line = $_;
166 next;
167 }
168
169 $cont = 0;
170
Steven Rostedtdcc60242009-04-29 22:52:21 -0400171 # collect any Kconfig sources
172 if (/^source\s*"(.*)"/) {
173 $kconfigs[$#kconfigs+1] = $1;
174 }
175
176 # configs found
Steven Rostedt8ef17fa2010-10-28 22:21:57 -0400177 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
Steven Rostedtdcc60242009-04-29 22:52:21 -0400178 $state = "NEW";
Steven Rostedt8ef17fa2010-10-28 22:21:57 -0400179 $config = $2;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400180
Steven Rostedt13d7e932010-01-06 17:56:12 -0500181 for (my $i = 0; $i < $iflevel; $i++) {
182 if ($i) {
183 $depends{$config} .= " " . $ifdeps[$i];
184 } else {
185 $depends{$config} = $ifdeps[$i];
186 }
187 $state = "DEP";
188 }
189
Steven Rostedtdcc60242009-04-29 22:52:21 -0400190 # collect the depends for the config
191 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
192 $state = "DEP";
193 $depends{$config} = $1;
194 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
195 $depends{$config} .= " " . $1;
196
197 # Get the configs that select this config
198 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
199 if (defined($selects{$1})) {
200 $selects{$1} .= " " . $config;
201 } else {
202 $selects{$1} = $config;
203 }
204
205 # configs without prompts must be selected
206 } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
207 # note if the config has a prompt
hiromu yaguraccece602010-08-15 13:13:17 +0900208 $prompts{$config} = 1;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400209
Steven Rostedt13d7e932010-01-06 17:56:12 -0500210 # Check for if statements
211 } elsif (/^if\s+(.*\S)\s*$/) {
212 my $deps = $1;
213 # remove beginning and ending non text
214 $deps =~ s/^[^a-zA-Z0-9_]*//;
215 $deps =~ s/[^a-zA-Z0-9_]*$//;
216
217 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
218
219 $ifdeps[$iflevel++] = join ':', @deps;
220
221 } elsif (/^endif/) {
222
223 $iflevel-- if ($iflevel);
224
Steven Rostedtdcc60242009-04-29 22:52:21 -0400225 # stop on "help"
226 } elsif (/^\s*help\s*$/) {
227 $state = "NONE";
228 }
229 }
230 close(KIN);
231
232 # read in any configs that were found.
233 foreach $kconfig (@kconfigs) {
234 if (!defined($read_kconfigs{$kconfig})) {
235 $read_kconfigs{$kconfig} = 1;
236 read_kconfig($kconfig);
237 }
238 }
239}
240
241if ($kconfig) {
242 read_kconfig($kconfig);
243}
244
245# Read all Makefiles to map the configs to the objects
246foreach my $makefile (@makefiles) {
Steven Rostedtdcc60242009-04-29 22:52:21 -0400247
Steven Rostedt20d19042010-10-28 22:13:51 -0400248 my $cont = 0;
249
Steven Rostedtdcc60242009-04-29 22:52:21 -0400250 open(MIN,$makefile) || die "Can't open $makefile";
251 while (<MIN>) {
252 my $objs;
253
254 # is this a line after a line with a backslash?
255 if ($cont && /(\S.*)$/) {
256 $objs = $1;
257 }
258 $cont = 0;
259
260 # collect objects after obj-$(CONFIG_FOO_BAR)
261 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
262 $var = $1;
263 $objs = $2;
264 }
265 if (defined($objs)) {
266 # test if the line ends with a backslash
267 if ($objs =~ m,(.*)\\$,) {
268 $objs = $1;
269 $cont = 1;
270 }
271
272 foreach my $obj (split /\s+/,$objs) {
273 $obj =~ s/-/_/g;
274 if ($obj =~ /(.*)\.o$/) {
Toralf Foerster1d1d1fe2010-05-28 10:25:52 +0200275 # Objects may be enabled by more than one config.
Steven Rostedtdcc60242009-04-29 22:52:21 -0400276 # Store configs in an array.
277 my @arr;
278
279 if (defined($objects{$1})) {
280 @arr = @{$objects{$1}};
281 }
282
283 $arr[$#arr+1] = $var;
284
285 # The objects have a hash mapping to a reference
286 # of an array of configs.
287 $objects{$1} = \@arr;
288 }
289 }
290 }
291 }
292 close(MIN);
293}
294
295my %modules;
296
Steven Rostedt615f0832010-02-02 21:51:27 -0500297if (defined($lsmod_file)) {
298 if ( ! -f $lsmod_file) {
Arnaud Lacombef597a712011-07-01 17:45:31 -0400299 if ( -f $ENV{'objtree'}."/".$lsmod_file) {
300 $lsmod_file = $ENV{'objtree'}."/".$lsmod_file;
301 } else {
302 die "$lsmod_file not found";
303 }
Steven Rostedt88f66ea2010-01-06 18:49:44 -0500304 }
Steven Rostedt615f0832010-02-02 21:51:27 -0500305 if ( -x $lsmod_file) {
306 # the file is executable, run it
307 open(LIN, "$lsmod_file|");
308 } else {
309 # Just read the contents
310 open(LIN, "$lsmod_file");
311 }
312} else {
313
314 # see what modules are loaded on this system
315 my $lsmod;
316
hiromucf5a1892010-08-17 19:49:18 +0900317 foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) {
Steven Rostedt615f0832010-02-02 21:51:27 -0500318 if ( -x "$dir/lsmod" ) {
319 $lsmod = "$dir/lsmod";
320 last;
321 }
Steven Rostedt88f66ea2010-01-06 18:49:44 -0500322}
Steven Rostedt615f0832010-02-02 21:51:27 -0500323 if (!defined($lsmod)) {
324 # try just the path
325 $lsmod = "lsmod";
326 }
327
328 open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod";
Steven Rostedt88f66ea2010-01-06 18:49:44 -0500329}
330
Steven Rostedtdcc60242009-04-29 22:52:21 -0400331while (<LIN>) {
332 next if (/^Module/); # Skip the first line.
333 if (/^(\S+)/) {
334 $modules{$1} = 1;
335 }
336}
337close (LIN);
338
339# add to the configs hash all configs that are needed to enable
340# a loaded module.
341my %configs;
342foreach my $module (keys(%modules)) {
343 if (defined($objects{$module})) {
Toralf Foerstere5199ed2010-05-28 10:24:59 +0200344 my @arr = @{$objects{$module}};
Steven Rostedtdcc60242009-04-29 22:52:21 -0400345 foreach my $conf (@arr) {
346 $configs{$conf} = $module;
347 }
348 } else {
349 # Most likely, someone has a custom (binary?) module loaded.
350 print STDERR "$module config not found!!\n";
351 }
352}
353
354my $valid = "A-Za-z_0-9";
355my $repeat = 1;
356
357#
358# Note, we do not care about operands (like: &&, ||, !) we want to add any
359# config that is in the depend list of another config. This script does
360# not enable configs that are not already enabled. If we come across a
361# config A that depends on !B, we can still add B to the list of depends
362# to keep on. If A was on in the original config, B would not have been
363# and B would not be turned on by this script.
364#
365sub parse_config_dep_select
366{
367 my ($p) = @_;
368
369 while ($p =~ /[$valid]/) {
370
371 if ($p =~ /^[^$valid]*([$valid]+)/) {
372 my $conf = "CONFIG_" . $1;
373
374 $p =~ s/^[^$valid]*[$valid]+//;
375
376 if (!defined($configs{$conf})) {
377 # We must make sure that this config has its
378 # dependencies met.
379 $repeat = 1; # do again
380 $configs{$conf} = 1;
381 }
382 } else {
383 die "this should never happen";
384 }
385 }
386}
387
388while ($repeat) {
389 $repeat = 0;
390
391 foreach my $config (keys %configs) {
392 $config =~ s/^CONFIG_//;
393
Steven Rostedt74398d32009-04-30 10:17:51 -0400394 if (defined($depends{$config})) {
395 # This config has dependencies. Make sure they are also included
396 parse_config_dep_select $depends{$config};
Steven Rostedtdcc60242009-04-29 22:52:21 -0400397 }
398
hiromu yaguraccece602010-08-15 13:13:17 +0900399 if (defined($prompts{$config}) || !defined($selects{$config})) {
Steven Rostedtdcc60242009-04-29 22:52:21 -0400400 next;
401 }
402
403 # config has no prompt and must be selected.
404 parse_config_dep_select $selects{$config};
405 }
406}
407
408my %setconfigs;
409
410# Finally, read the .config file and turn off any module enabled that
411# we could not find a reason to keep enabled.
412while(<CIN>) {
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400413
414 if (/CONFIG_IKCONFIG/) {
415 if (/# CONFIG_IKCONFIG is not set/) {
416 # enable IKCONFIG at least as a module
417 print "CONFIG_IKCONFIG=m\n";
418 # don't ask about PROC
Steven Rostedtd08ca272009-04-30 19:24:00 -0400419 print "# CONFIG_IKCONFIG_PROC is not set\n";
Steven Rostedtdcc60242009-04-29 22:52:21 -0400420 } else {
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400421 print;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400422 }
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400423 next;
424 }
425
426 if (/^(CONFIG.*)=(m|y)/) {
427 if (defined($configs{$1})) {
428 $setconfigs{$1} = $2;
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400429 } elsif ($2 eq "m") {
430 print "# $1 is not set\n";
Steven Rostedtd08ca272009-04-30 19:24:00 -0400431 next;
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400432 }
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400433 }
Steven Rostedtd08ca272009-04-30 19:24:00 -0400434 print;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400435}
436close(CIN);
437
438# Integrity check, make sure all modules that we want enabled do
439# indeed have their configs set.
440loop:
441foreach my $module (keys(%modules)) {
442 if (defined($objects{$module})) {
443 my @arr = @{$objects{$module}};
444 foreach my $conf (@arr) {
445 if (defined($setconfigs{$conf})) {
446 next loop;
447 }
448 }
449 print STDERR "module $module did not have configs";
450 foreach my $conf (@arr) {
451 print STDERR " " , $conf;
452 }
453 print STDERR "\n";
454 }
455}