blob: 3c63aa943ee3db7abca36092369f6aa2ce681197 [file] [log] [blame]
Steven Rostedtdcc60242009-04-29 22:52:21 -04001#!/usr/bin/perl -w
2#
3# Copywrite 2005-2009 - Steven Rostedt
4# 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#
45my $config = ".config";
Steven Rostedtdcc60242009-04-29 22:52:21 -040046
Steven Rostedtcdfc4792009-04-30 14:39:48 -040047my $uname = `uname -r`;
48chomp $uname;
49
50my @searchconfigs = (
51 {
Steven Rostedta9024832009-05-07 11:01:34 -040052 "file" => ".config",
53 "exec" => "cat",
54 },
55 {
Steven Rostedtcdfc4792009-04-30 14:39:48 -040056 "file" => "/proc/config.gz",
57 "exec" => "zcat",
58 },
59 {
Steven Rostedt810b2be2009-04-30 19:30:04 -040060 "file" => "/boot/config-$uname",
61 "exec" => "cat",
62 },
63 {
Steven Rostedtcdfc4792009-04-30 14:39:48 -040064 "file" => "/boot/vmlinuz-$uname",
65 "exec" => "scripts/extract-ikconfig",
66 "test" => "scripts/extract-ikconfig",
67 },
68 {
69 "file" => "vmlinux",
70 "exec" => "scripts/extract-ikconfig",
71 "test" => "scripts/extract-ikconfig",
72 },
73 {
74 "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
75 "exec" => "scripts/extract-ikconfig",
76 "test" => "scripts/extract-ikconfig",
77 },
78 {
79 "file" => "kernel/configs.ko",
80 "exec" => "scripts/extract-ikconfig",
81 "test" => "scripts/extract-ikconfig",
82 },
83 {
84 "file" => "kernel/configs.o",
85 "exec" => "scripts/extract-ikconfig",
86 "test" => "scripts/extract-ikconfig",
87 },
Steven Rostedtcdfc4792009-04-30 14:39:48 -040088);
89
90sub find_config {
91 foreach my $conf (@searchconfigs) {
92 my $file = $conf->{"file"};
93
94 next if ( ! -f "$file");
95
96 if (defined($conf->{"test"})) {
97 `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
98 next if ($?);
99 }
100
101 my $exec = $conf->{"exec"};
102
103 print STDERR "using config: '$file'\n";
104
105 open(CIN, "$exec $file |") || die "Failed to run $exec $file";
106 return;
107 }
108 die "No config file found";
109}
110
111find_config;
112
Steven Rostedt463bf902009-11-20 09:21:12 -0500113# Get the build source and top level Kconfig file (passed in)
114my $ksource = $ARGV[0];
115my $kconfig = $ARGV[1];
Steven Rostedt615f0832010-02-02 21:51:27 -0500116my $lsmod_file = $ARGV[2];
Steven Rostedt463bf902009-11-20 09:21:12 -0500117
Toralf Förster17431922010-05-26 20:22:02 +0200118my @makefiles = `find $ksource -name Makefile 2>/dev/null`;
119chomp @makefiles;
120
Steven Rostedtdcc60242009-04-29 22:52:21 -0400121my %depends;
122my %selects;
123my %prompts;
124my %objects;
125my $var;
126my $cont = 0;
Steven Rostedt13d7e932010-01-06 17:56:12 -0500127my $iflevel = 0;
128my @ifdeps;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400129
Steven Rostedtdcc60242009-04-29 22:52:21 -0400130# prevent recursion
131my %read_kconfigs;
132
133sub read_kconfig {
134 my ($kconfig) = @_;
135
136 my $state = "NONE";
137 my $config;
138 my @kconfigs;
139
Steven Rostedt49089802010-10-29 00:56:46 -0400140 my $source = "$ksource/$kconfig";
141 my $last_source = "";
142
143 # Check for any environment variables used
144 while ($source =~ /\$(\w+)/ && $last_source ne $source) {
145 my $env = $1;
146 $last_source = $source;
147 $source =~ s/\$$env/$ENV{$env}/;
148 }
149
150 open(KIN, "$source") || die "Can't open $kconfig";
Steven Rostedtdcc60242009-04-29 22:52:21 -0400151 while (<KIN>) {
152 chomp;
153
154 # collect any Kconfig sources
155 if (/^source\s*"(.*)"/) {
156 $kconfigs[$#kconfigs+1] = $1;
157 }
158
159 # configs found
160 if (/^\s*config\s+(\S+)\s*$/) {
161 $state = "NEW";
162 $config = $1;
163
Steven Rostedt13d7e932010-01-06 17:56:12 -0500164 for (my $i = 0; $i < $iflevel; $i++) {
165 if ($i) {
166 $depends{$config} .= " " . $ifdeps[$i];
167 } else {
168 $depends{$config} = $ifdeps[$i];
169 }
170 $state = "DEP";
171 }
172
Steven Rostedtdcc60242009-04-29 22:52:21 -0400173 # collect the depends for the config
174 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
175 $state = "DEP";
176 $depends{$config} = $1;
177 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
178 $depends{$config} .= " " . $1;
179
180 # Get the configs that select this config
181 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
182 if (defined($selects{$1})) {
183 $selects{$1} .= " " . $config;
184 } else {
185 $selects{$1} = $config;
186 }
187
188 # configs without prompts must be selected
189 } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
190 # note if the config has a prompt
hiromu yaguraccece602010-08-15 13:13:17 +0900191 $prompts{$config} = 1;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400192
Steven Rostedt13d7e932010-01-06 17:56:12 -0500193 # Check for if statements
194 } elsif (/^if\s+(.*\S)\s*$/) {
195 my $deps = $1;
196 # remove beginning and ending non text
197 $deps =~ s/^[^a-zA-Z0-9_]*//;
198 $deps =~ s/[^a-zA-Z0-9_]*$//;
199
200 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
201
202 $ifdeps[$iflevel++] = join ':', @deps;
203
204 } elsif (/^endif/) {
205
206 $iflevel-- if ($iflevel);
207
Steven Rostedtdcc60242009-04-29 22:52:21 -0400208 # stop on "help"
209 } elsif (/^\s*help\s*$/) {
210 $state = "NONE";
211 }
212 }
213 close(KIN);
214
215 # read in any configs that were found.
216 foreach $kconfig (@kconfigs) {
217 if (!defined($read_kconfigs{$kconfig})) {
218 $read_kconfigs{$kconfig} = 1;
219 read_kconfig($kconfig);
220 }
221 }
222}
223
224if ($kconfig) {
225 read_kconfig($kconfig);
226}
227
228# Read all Makefiles to map the configs to the objects
229foreach my $makefile (@makefiles) {
Steven Rostedtdcc60242009-04-29 22:52:21 -0400230
231 open(MIN,$makefile) || die "Can't open $makefile";
232 while (<MIN>) {
233 my $objs;
234
235 # is this a line after a line with a backslash?
236 if ($cont && /(\S.*)$/) {
237 $objs = $1;
238 }
239 $cont = 0;
240
241 # collect objects after obj-$(CONFIG_FOO_BAR)
242 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
243 $var = $1;
244 $objs = $2;
245 }
246 if (defined($objs)) {
247 # test if the line ends with a backslash
248 if ($objs =~ m,(.*)\\$,) {
249 $objs = $1;
250 $cont = 1;
251 }
252
253 foreach my $obj (split /\s+/,$objs) {
254 $obj =~ s/-/_/g;
255 if ($obj =~ /(.*)\.o$/) {
Toralf Foerster1d1d1fe2010-05-28 10:25:52 +0200256 # Objects may be enabled by more than one config.
Steven Rostedtdcc60242009-04-29 22:52:21 -0400257 # Store configs in an array.
258 my @arr;
259
260 if (defined($objects{$1})) {
261 @arr = @{$objects{$1}};
262 }
263
264 $arr[$#arr+1] = $var;
265
266 # The objects have a hash mapping to a reference
267 # of an array of configs.
268 $objects{$1} = \@arr;
269 }
270 }
271 }
272 }
273 close(MIN);
274}
275
276my %modules;
277
Steven Rostedt615f0832010-02-02 21:51:27 -0500278if (defined($lsmod_file)) {
279 if ( ! -f $lsmod_file) {
280 die "$lsmod_file not found";
Steven Rostedt88f66ea2010-01-06 18:49:44 -0500281 }
Steven Rostedt615f0832010-02-02 21:51:27 -0500282 if ( -x $lsmod_file) {
283 # the file is executable, run it
284 open(LIN, "$lsmod_file|");
285 } else {
286 # Just read the contents
287 open(LIN, "$lsmod_file");
288 }
289} else {
290
291 # see what modules are loaded on this system
292 my $lsmod;
293
294 foreach $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) {
295 if ( -x "$dir/lsmod" ) {
296 $lsmod = "$dir/lsmod";
297 last;
298 }
Steven Rostedt88f66ea2010-01-06 18:49:44 -0500299}
Steven Rostedt615f0832010-02-02 21:51:27 -0500300 if (!defined($lsmod)) {
301 # try just the path
302 $lsmod = "lsmod";
303 }
304
305 open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod";
Steven Rostedt88f66ea2010-01-06 18:49:44 -0500306}
307
Steven Rostedtdcc60242009-04-29 22:52:21 -0400308while (<LIN>) {
309 next if (/^Module/); # Skip the first line.
310 if (/^(\S+)/) {
311 $modules{$1} = 1;
312 }
313}
314close (LIN);
315
316# add to the configs hash all configs that are needed to enable
317# a loaded module.
318my %configs;
319foreach my $module (keys(%modules)) {
320 if (defined($objects{$module})) {
Toralf Foerstere5199ed2010-05-28 10:24:59 +0200321 my @arr = @{$objects{$module}};
Steven Rostedtdcc60242009-04-29 22:52:21 -0400322 foreach my $conf (@arr) {
323 $configs{$conf} = $module;
324 }
325 } else {
326 # Most likely, someone has a custom (binary?) module loaded.
327 print STDERR "$module config not found!!\n";
328 }
329}
330
331my $valid = "A-Za-z_0-9";
332my $repeat = 1;
333
334#
335# Note, we do not care about operands (like: &&, ||, !) we want to add any
336# config that is in the depend list of another config. This script does
337# not enable configs that are not already enabled. If we come across a
338# config A that depends on !B, we can still add B to the list of depends
339# to keep on. If A was on in the original config, B would not have been
340# and B would not be turned on by this script.
341#
342sub parse_config_dep_select
343{
344 my ($p) = @_;
345
346 while ($p =~ /[$valid]/) {
347
348 if ($p =~ /^[^$valid]*([$valid]+)/) {
349 my $conf = "CONFIG_" . $1;
350
351 $p =~ s/^[^$valid]*[$valid]+//;
352
353 if (!defined($configs{$conf})) {
354 # We must make sure that this config has its
355 # dependencies met.
356 $repeat = 1; # do again
357 $configs{$conf} = 1;
358 }
359 } else {
360 die "this should never happen";
361 }
362 }
363}
364
365while ($repeat) {
366 $repeat = 0;
367
368 foreach my $config (keys %configs) {
369 $config =~ s/^CONFIG_//;
370
Steven Rostedt74398d32009-04-30 10:17:51 -0400371 if (defined($depends{$config})) {
372 # This config has dependencies. Make sure they are also included
373 parse_config_dep_select $depends{$config};
Steven Rostedtdcc60242009-04-29 22:52:21 -0400374 }
375
hiromu yaguraccece602010-08-15 13:13:17 +0900376 if (defined($prompts{$config}) || !defined($selects{$config})) {
Steven Rostedtdcc60242009-04-29 22:52:21 -0400377 next;
378 }
379
380 # config has no prompt and must be selected.
381 parse_config_dep_select $selects{$config};
382 }
383}
384
385my %setconfigs;
386
387# Finally, read the .config file and turn off any module enabled that
388# we could not find a reason to keep enabled.
389while(<CIN>) {
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400390
391 if (/CONFIG_IKCONFIG/) {
392 if (/# CONFIG_IKCONFIG is not set/) {
393 # enable IKCONFIG at least as a module
394 print "CONFIG_IKCONFIG=m\n";
395 # don't ask about PROC
Steven Rostedtd08ca272009-04-30 19:24:00 -0400396 print "# CONFIG_IKCONFIG_PROC is not set\n";
Steven Rostedtdcc60242009-04-29 22:52:21 -0400397 } else {
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400398 print;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400399 }
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400400 next;
401 }
402
403 if (/^(CONFIG.*)=(m|y)/) {
404 if (defined($configs{$1})) {
405 $setconfigs{$1} = $2;
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400406 } elsif ($2 eq "m") {
407 print "# $1 is not set\n";
Steven Rostedtd08ca272009-04-30 19:24:00 -0400408 next;
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400409 }
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400410 }
Steven Rostedtd08ca272009-04-30 19:24:00 -0400411 print;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400412}
413close(CIN);
414
415# Integrity check, make sure all modules that we want enabled do
416# indeed have their configs set.
417loop:
418foreach my $module (keys(%modules)) {
419 if (defined($objects{$module})) {
420 my @arr = @{$objects{$module}};
421 foreach my $conf (@arr) {
422 if (defined($setconfigs{$conf})) {
423 next loop;
424 }
425 }
426 print STDERR "module $module did not have configs";
427 foreach my $conf (@arr) {
428 print STDERR " " , $conf;
429 }
430 print STDERR "\n";
431 }
432}