blob: 42ef5ea5ebdc7d6842dcc2417c5fb067014d039b [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;
Arnaud Lacombe22d550a2011-07-20 00:40:09 -040046use Getopt::Long;
hiromucf5a1892010-08-17 19:49:18 +090047
Steven Rostedtdcc60242009-04-29 22:52:21 -040048my $config = ".config";
Steven Rostedtdcc60242009-04-29 22:52:21 -040049
Steven Rostedtcdfc4792009-04-30 14:39:48 -040050my $uname = `uname -r`;
51chomp $uname;
52
53my @searchconfigs = (
54 {
Steven Rostedta9024832009-05-07 11:01:34 -040055 "file" => ".config",
56 "exec" => "cat",
57 },
58 {
Steven Rostedtcdfc4792009-04-30 14:39:48 -040059 "file" => "/proc/config.gz",
60 "exec" => "zcat",
61 },
62 {
Steven Rostedt810b2be2009-04-30 19:30:04 -040063 "file" => "/boot/config-$uname",
64 "exec" => "cat",
65 },
66 {
Steven Rostedtcdfc4792009-04-30 14:39:48 -040067 "file" => "/boot/vmlinuz-$uname",
68 "exec" => "scripts/extract-ikconfig",
69 "test" => "scripts/extract-ikconfig",
70 },
71 {
72 "file" => "vmlinux",
73 "exec" => "scripts/extract-ikconfig",
74 "test" => "scripts/extract-ikconfig",
75 },
76 {
77 "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
78 "exec" => "scripts/extract-ikconfig",
79 "test" => "scripts/extract-ikconfig",
80 },
81 {
82 "file" => "kernel/configs.ko",
83 "exec" => "scripts/extract-ikconfig",
84 "test" => "scripts/extract-ikconfig",
85 },
86 {
87 "file" => "kernel/configs.o",
88 "exec" => "scripts/extract-ikconfig",
89 "test" => "scripts/extract-ikconfig",
90 },
Steven Rostedtcdfc4792009-04-30 14:39:48 -040091);
92
93sub find_config {
94 foreach my $conf (@searchconfigs) {
95 my $file = $conf->{"file"};
96
97 next if ( ! -f "$file");
98
99 if (defined($conf->{"test"})) {
100 `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
101 next if ($?);
102 }
103
104 my $exec = $conf->{"exec"};
105
106 print STDERR "using config: '$file'\n";
107
108 open(CIN, "$exec $file |") || die "Failed to run $exec $file";
109 return;
110 }
111 die "No config file found";
112}
113
114find_config;
115
Arnaud Lacombe22d550a2011-07-20 00:40:09 -0400116# Parse options
117my $localmodconfig = 0;
118my $localyesconfig = 0;
119
120GetOptions("localmodconfig" => \$localmodconfig,
121 "localyesconfig" => \$localyesconfig);
122
Steven Rostedt463bf902009-11-20 09:21:12 -0500123# Get the build source and top level Kconfig file (passed in)
124my $ksource = $ARGV[0];
125my $kconfig = $ARGV[1];
Arnaud Lacombef597a712011-07-01 17:45:31 -0400126my $lsmod_file = $ENV{'LSMOD'};
Steven Rostedt463bf902009-11-20 09:21:12 -0500127
Toralf Förster17431922010-05-26 20:22:02 +0200128my @makefiles = `find $ksource -name Makefile 2>/dev/null`;
129chomp @makefiles;
130
Steven Rostedtdcc60242009-04-29 22:52:21 -0400131my %depends;
132my %selects;
133my %prompts;
134my %objects;
135my $var;
Steven Rostedt13d7e932010-01-06 17:56:12 -0500136my $iflevel = 0;
137my @ifdeps;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400138
Steven Rostedtdcc60242009-04-29 22:52:21 -0400139# prevent recursion
140my %read_kconfigs;
141
142sub read_kconfig {
143 my ($kconfig) = @_;
144
145 my $state = "NONE";
146 my $config;
147 my @kconfigs;
148
Steven Rostedt20d19042010-10-28 22:13:51 -0400149 my $cont = 0;
150 my $line;
151
Steven Rostedt49089802010-10-29 00:56:46 -0400152 my $source = "$ksource/$kconfig";
153 my $last_source = "";
154
155 # Check for any environment variables used
156 while ($source =~ /\$(\w+)/ && $last_source ne $source) {
157 my $env = $1;
158 $last_source = $source;
159 $source =~ s/\$$env/$ENV{$env}/;
160 }
161
162 open(KIN, "$source") || die "Can't open $kconfig";
Steven Rostedtdcc60242009-04-29 22:52:21 -0400163 while (<KIN>) {
164 chomp;
165
Steven Rostedt20d19042010-10-28 22:13:51 -0400166 # Make sure that lines ending with \ continue
167 if ($cont) {
168 $_ = $line . " " . $_;
169 }
170
171 if (s/\\$//) {
172 $cont = 1;
173 $line = $_;
174 next;
175 }
176
177 $cont = 0;
178
Steven Rostedtdcc60242009-04-29 22:52:21 -0400179 # collect any Kconfig sources
180 if (/^source\s*"(.*)"/) {
181 $kconfigs[$#kconfigs+1] = $1;
182 }
183
184 # configs found
Steven Rostedt8ef17fa2010-10-28 22:21:57 -0400185 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
Steven Rostedtdcc60242009-04-29 22:52:21 -0400186 $state = "NEW";
Steven Rostedt8ef17fa2010-10-28 22:21:57 -0400187 $config = $2;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400188
Steven Rostedt13d7e932010-01-06 17:56:12 -0500189 for (my $i = 0; $i < $iflevel; $i++) {
190 if ($i) {
191 $depends{$config} .= " " . $ifdeps[$i];
192 } else {
193 $depends{$config} = $ifdeps[$i];
194 }
195 $state = "DEP";
196 }
197
Steven Rostedtdcc60242009-04-29 22:52:21 -0400198 # collect the depends for the config
199 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
200 $state = "DEP";
201 $depends{$config} = $1;
202 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
203 $depends{$config} .= " " . $1;
204
205 # Get the configs that select this config
206 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
207 if (defined($selects{$1})) {
208 $selects{$1} .= " " . $config;
209 } else {
210 $selects{$1} = $config;
211 }
212
213 # configs without prompts must be selected
214 } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
215 # note if the config has a prompt
hiromu yaguraccece602010-08-15 13:13:17 +0900216 $prompts{$config} = 1;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400217
Steven Rostedt13d7e932010-01-06 17:56:12 -0500218 # Check for if statements
219 } elsif (/^if\s+(.*\S)\s*$/) {
220 my $deps = $1;
221 # remove beginning and ending non text
222 $deps =~ s/^[^a-zA-Z0-9_]*//;
223 $deps =~ s/[^a-zA-Z0-9_]*$//;
224
225 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
226
227 $ifdeps[$iflevel++] = join ':', @deps;
228
229 } elsif (/^endif/) {
230
231 $iflevel-- if ($iflevel);
232
Steven Rostedtdcc60242009-04-29 22:52:21 -0400233 # stop on "help"
234 } elsif (/^\s*help\s*$/) {
235 $state = "NONE";
236 }
237 }
238 close(KIN);
239
240 # read in any configs that were found.
241 foreach $kconfig (@kconfigs) {
242 if (!defined($read_kconfigs{$kconfig})) {
243 $read_kconfigs{$kconfig} = 1;
244 read_kconfig($kconfig);
245 }
246 }
247}
248
249if ($kconfig) {
250 read_kconfig($kconfig);
251}
252
253# Read all Makefiles to map the configs to the objects
254foreach my $makefile (@makefiles) {
Steven Rostedtdcc60242009-04-29 22:52:21 -0400255
Steven Rostedtd060d962012-01-13 17:50:39 -0500256 my $line = "";
Steven Rostedt20d19042010-10-28 22:13:51 -0400257
Steven Rostedtdcc60242009-04-29 22:52:21 -0400258 open(MIN,$makefile) || die "Can't open $makefile";
259 while (<MIN>) {
Steven Rostedtd060d962012-01-13 17:50:39 -0500260 # if this line ends with a backslash, continue
261 chomp;
262 if (/^(.*)\\$/) {
263 $line .= $1;
264 next;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400265 }
Steven Rostedtd060d962012-01-13 17:50:39 -0500266
267 $line .= $_;
268 $_ = $line;
269 $line = "";
270
271 my $objs;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400272
273 # collect objects after obj-$(CONFIG_FOO_BAR)
274 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
275 $var = $1;
276 $objs = $2;
277 }
278 if (defined($objs)) {
Steven Rostedtdcc60242009-04-29 22:52:21 -0400279 foreach my $obj (split /\s+/,$objs) {
280 $obj =~ s/-/_/g;
281 if ($obj =~ /(.*)\.o$/) {
Toralf Foerster1d1d1fe2010-05-28 10:25:52 +0200282 # Objects may be enabled by more than one config.
Steven Rostedtdcc60242009-04-29 22:52:21 -0400283 # Store configs in an array.
284 my @arr;
285
286 if (defined($objects{$1})) {
287 @arr = @{$objects{$1}};
288 }
289
290 $arr[$#arr+1] = $var;
291
292 # The objects have a hash mapping to a reference
293 # of an array of configs.
294 $objects{$1} = \@arr;
295 }
296 }
297 }
298 }
299 close(MIN);
300}
301
302my %modules;
303
Steven Rostedt615f0832010-02-02 21:51:27 -0500304if (defined($lsmod_file)) {
305 if ( ! -f $lsmod_file) {
Arnaud Lacombef597a712011-07-01 17:45:31 -0400306 if ( -f $ENV{'objtree'}."/".$lsmod_file) {
307 $lsmod_file = $ENV{'objtree'}."/".$lsmod_file;
308 } else {
309 die "$lsmod_file not found";
310 }
Steven Rostedt88f66ea2010-01-06 18:49:44 -0500311 }
Steven Rostedt615f0832010-02-02 21:51:27 -0500312 if ( -x $lsmod_file) {
313 # the file is executable, run it
314 open(LIN, "$lsmod_file|");
315 } else {
316 # Just read the contents
317 open(LIN, "$lsmod_file");
318 }
319} else {
320
321 # see what modules are loaded on this system
322 my $lsmod;
323
hiromucf5a1892010-08-17 19:49:18 +0900324 foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) {
Steven Rostedt615f0832010-02-02 21:51:27 -0500325 if ( -x "$dir/lsmod" ) {
326 $lsmod = "$dir/lsmod";
327 last;
328 }
Steven Rostedt88f66ea2010-01-06 18:49:44 -0500329}
Steven Rostedt615f0832010-02-02 21:51:27 -0500330 if (!defined($lsmod)) {
331 # try just the path
332 $lsmod = "lsmod";
333 }
334
335 open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod";
Steven Rostedt88f66ea2010-01-06 18:49:44 -0500336}
337
Steven Rostedtdcc60242009-04-29 22:52:21 -0400338while (<LIN>) {
339 next if (/^Module/); # Skip the first line.
340 if (/^(\S+)/) {
341 $modules{$1} = 1;
342 }
343}
344close (LIN);
345
346# add to the configs hash all configs that are needed to enable
347# a loaded module.
348my %configs;
349foreach my $module (keys(%modules)) {
350 if (defined($objects{$module})) {
Toralf Foerstere5199ed2010-05-28 10:24:59 +0200351 my @arr = @{$objects{$module}};
Steven Rostedtdcc60242009-04-29 22:52:21 -0400352 foreach my $conf (@arr) {
353 $configs{$conf} = $module;
354 }
355 } else {
356 # Most likely, someone has a custom (binary?) module loaded.
357 print STDERR "$module config not found!!\n";
358 }
359}
360
361my $valid = "A-Za-z_0-9";
362my $repeat = 1;
363
364#
365# Note, we do not care about operands (like: &&, ||, !) we want to add any
366# config that is in the depend list of another config. This script does
367# not enable configs that are not already enabled. If we come across a
368# config A that depends on !B, we can still add B to the list of depends
369# to keep on. If A was on in the original config, B would not have been
370# and B would not be turned on by this script.
371#
372sub parse_config_dep_select
373{
374 my ($p) = @_;
375
376 while ($p =~ /[$valid]/) {
377
378 if ($p =~ /^[^$valid]*([$valid]+)/) {
379 my $conf = "CONFIG_" . $1;
380
381 $p =~ s/^[^$valid]*[$valid]+//;
382
383 if (!defined($configs{$conf})) {
384 # We must make sure that this config has its
385 # dependencies met.
386 $repeat = 1; # do again
387 $configs{$conf} = 1;
388 }
389 } else {
390 die "this should never happen";
391 }
392 }
393}
394
395while ($repeat) {
396 $repeat = 0;
397
398 foreach my $config (keys %configs) {
399 $config =~ s/^CONFIG_//;
400
Steven Rostedt74398d32009-04-30 10:17:51 -0400401 if (defined($depends{$config})) {
402 # This config has dependencies. Make sure they are also included
403 parse_config_dep_select $depends{$config};
Steven Rostedtdcc60242009-04-29 22:52:21 -0400404 }
405
hiromu yaguraccece602010-08-15 13:13:17 +0900406 if (defined($prompts{$config}) || !defined($selects{$config})) {
Steven Rostedtdcc60242009-04-29 22:52:21 -0400407 next;
408 }
409
410 # config has no prompt and must be selected.
411 parse_config_dep_select $selects{$config};
412 }
413}
414
415my %setconfigs;
416
417# Finally, read the .config file and turn off any module enabled that
418# we could not find a reason to keep enabled.
419while(<CIN>) {
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400420
421 if (/CONFIG_IKCONFIG/) {
422 if (/# CONFIG_IKCONFIG is not set/) {
423 # enable IKCONFIG at least as a module
424 print "CONFIG_IKCONFIG=m\n";
425 # don't ask about PROC
Steven Rostedtd08ca272009-04-30 19:24:00 -0400426 print "# CONFIG_IKCONFIG_PROC is not set\n";
Steven Rostedtdcc60242009-04-29 22:52:21 -0400427 } else {
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400428 print;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400429 }
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400430 next;
431 }
432
433 if (/^(CONFIG.*)=(m|y)/) {
434 if (defined($configs{$1})) {
Arnaud Lacombe22d550a2011-07-20 00:40:09 -0400435 if ($localyesconfig) {
436 $setconfigs{$1} = 'y';
437 } else {
438 $setconfigs{$1} = $2;
439 }
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400440 } elsif ($2 eq "m") {
441 print "# $1 is not set\n";
Steven Rostedtd08ca272009-04-30 19:24:00 -0400442 next;
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400443 }
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400444 }
Steven Rostedtd08ca272009-04-30 19:24:00 -0400445 print;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400446}
447close(CIN);
448
449# Integrity check, make sure all modules that we want enabled do
450# indeed have their configs set.
451loop:
452foreach my $module (keys(%modules)) {
453 if (defined($objects{$module})) {
454 my @arr = @{$objects{$module}};
455 foreach my $conf (@arr) {
456 if (defined($setconfigs{$conf})) {
457 next loop;
458 }
459 }
460 print STDERR "module $module did not have configs";
461 foreach my $conf (@arr) {
462 print STDERR " " , $conf;
463 }
464 print STDERR "\n";
465 }
466}