1#!/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";
46
47my $uname = `uname -r`;
48chomp $uname;
49
50my @searchconfigs = (
51	{
52	    "file" => ".config",
53	    "exec" => "cat",
54	},
55	{
56	    "file" => "/proc/config.gz",
57	    "exec" => "zcat",
58	},
59	{
60	    "file" => "/boot/config-$uname",
61	    "exec" => "cat",
62	},
63	{
64	    "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	},
88);
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
113# Get the build source and top level Kconfig file (passed in)
114my $ksource = $ARGV[0];
115my $kconfig = $ARGV[1];
116
117my @makefiles = `find $ksource -name Makefile`;
118my %depends;
119my %selects;
120my %prompts;
121my %objects;
122my $var;
123my $cont = 0;
124
125# prevent recursion
126my %read_kconfigs;
127
128sub read_kconfig {
129    my ($kconfig) = @_;
130
131    my $state = "NONE";
132    my $config;
133    my @kconfigs;
134
135    open(KIN, "$ksource/$kconfig") || die "Can't open $kconfig";
136    while (<KIN>) {
137	chomp;
138
139	# collect any Kconfig sources
140	if (/^source\s*"(.*)"/) {
141	    $kconfigs[$#kconfigs+1] = $1;
142	}
143
144	# configs found
145	if (/^\s*config\s+(\S+)\s*$/) {
146	    $state = "NEW";
147	    $config = $1;
148
149	# collect the depends for the config
150	} elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
151	    $state = "DEP";
152	    $depends{$config} = $1;
153	} elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
154	    $depends{$config} .= " " . $1;
155
156	# Get the configs that select this config
157	} elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
158	    if (defined($selects{$1})) {
159		$selects{$1} .= " " . $config;
160	    } else {
161		$selects{$1} = $config;
162	    }
163
164	# configs without prompts must be selected
165	} elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
166	    # note if the config has a prompt
167	    $prompt{$config} = 1;
168
169	# stop on "help"
170	} elsif (/^\s*help\s*$/) {
171	    $state = "NONE";
172	}
173    }
174    close(KIN);
175
176    # read in any configs that were found.
177    foreach $kconfig (@kconfigs) {
178	if (!defined($read_kconfigs{$kconfig})) {
179	    $read_kconfigs{$kconfig} = 1;
180	    read_kconfig($kconfig);
181	}
182    }
183}
184
185if ($kconfig) {
186    read_kconfig($kconfig);
187}
188
189# Read all Makefiles to map the configs to the objects
190foreach my $makefile (@makefiles) {
191    chomp $makefile;
192
193    open(MIN,$makefile) || die "Can't open $makefile";
194    while (<MIN>) {
195	my $objs;
196
197	# is this a line after a line with a backslash?
198	if ($cont && /(\S.*)$/) {
199	    $objs = $1;
200	}
201	$cont = 0;
202
203	# collect objects after obj-$(CONFIG_FOO_BAR)
204	if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
205	    $var = $1;
206	    $objs = $2;
207	}
208	if (defined($objs)) {
209	    # test if the line ends with a backslash
210	    if ($objs =~ m,(.*)\\$,) {
211		$objs = $1;
212		$cont = 1;
213	    }
214
215	    foreach my $obj (split /\s+/,$objs) {
216		$obj =~ s/-/_/g;
217		if ($obj =~ /(.*)\.o$/) {
218		    # Objects may bes enabled by more than one config.
219		    # Store configs in an array.
220		    my @arr;
221
222		    if (defined($objects{$1})) {
223			@arr = @{$objects{$1}};
224		    }
225
226		    $arr[$#arr+1] = $var;
227
228		    # The objects have a hash mapping to a reference
229		    # of an array of configs.
230		    $objects{$1} = \@arr;
231		}
232	    }
233	}
234    }
235    close(MIN);
236}
237
238my %modules;
239
240# see what modules are loaded on this system
241open(LIN,"/sbin/lsmod|") || die "Cant lsmod";
242while (<LIN>) {
243	next if (/^Module/);  # Skip the first line.
244	if (/^(\S+)/) {
245		$modules{$1} = 1;
246	}
247}
248close (LIN);
249
250# add to the configs hash all configs that are needed to enable
251# a loaded module.
252my %configs;
253foreach my $module (keys(%modules)) {
254    if (defined($objects{$module})) {
255	@arr = @{$objects{$module}};
256	foreach my $conf (@arr) {
257	    $configs{$conf} = $module;
258	}
259    } else {
260	# Most likely, someone has a custom (binary?) module loaded.
261	print STDERR "$module config not found!!\n";
262    }
263}
264
265my $valid = "A-Za-z_0-9";
266my $repeat = 1;
267
268#
269# Note, we do not care about operands (like: &&, ||, !) we want to add any
270# config that is in the depend list of another config. This script does
271# not enable configs that are not already enabled. If we come across a
272# config A that depends on !B, we can still add B to the list of depends
273# to keep on. If A was on in the original config, B would not have been
274# and B would not be turned on by this script.
275#
276sub parse_config_dep_select
277{
278    my ($p) = @_;
279
280    while ($p =~ /[$valid]/) {
281
282	if ($p =~ /^[^$valid]*([$valid]+)/) {
283	    my $conf = "CONFIG_" . $1;
284
285	    $p =~ s/^[^$valid]*[$valid]+//;
286
287	    if (!defined($configs{$conf})) {
288		# We must make sure that this config has its
289		# dependencies met.
290		$repeat = 1; # do again
291		$configs{$conf} = 1;
292	    }
293	} else {
294	    die "this should never happen";
295	}
296    }
297}
298
299while ($repeat) {
300    $repeat = 0;
301
302    foreach my $config (keys %configs) {
303	$config =~ s/^CONFIG_//;
304
305	if (defined($depends{$config})) {
306	    # This config has dependencies. Make sure they are also included
307	    parse_config_dep_select $depends{$config};
308	}
309
310	if (defined($prompt{$config}) || !defined($selects{$config})) {
311	    next;
312	}
313
314	# config has no prompt and must be selected.
315	parse_config_dep_select $selects{$config};
316    }
317}
318
319my %setconfigs;
320
321# Finally, read the .config file and turn off any module enabled that
322# we could not find a reason to keep enabled.
323while(<CIN>) {
324
325    if (/CONFIG_IKCONFIG/) {
326	if (/# CONFIG_IKCONFIG is not set/) {
327	    # enable IKCONFIG at least as a module
328	    print "CONFIG_IKCONFIG=m\n";
329	    # don't ask about PROC
330	    print "# CONFIG_IKCONFIG_PROC is not set\n";
331	} else {
332	    print;
333	}
334	next;
335    }
336
337    if (/^(CONFIG.*)=(m|y)/) {
338	if (defined($configs{$1})) {
339	    $setconfigs{$1} = $2;
340	} elsif ($2 eq "m") {
341	    print "# $1 is not set\n";
342	    next;
343	}
344    }
345    print;
346}
347close(CIN);
348
349# Integrity check, make sure all modules that we want enabled do
350# indeed have their configs set.
351loop:
352foreach my $module (keys(%modules)) {
353    if (defined($objects{$module})) {
354	my @arr = @{$objects{$module}};
355	foreach my $conf (@arr) {
356	    if (defined($setconfigs{$conf})) {
357		next loop;
358	    }
359	}
360	print STDERR "module $module did not have configs";
361	foreach my $conf (@arr) {
362	    print STDERR " " , $conf;
363	}
364	print STDERR "\n";
365    }
366}
367