streamline_config.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Copyright 2005-2009 - Steven Rostedt
  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 configuration 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. #
  45. use warnings;
  46. use strict;
  47. use Getopt::Long;
  48. # set the environment variable LOCALMODCONFIG_DEBUG to get
  49. # debug output.
  50. my $debugprint = 0;
  51. $debugprint = 1 if (defined($ENV{LOCALMODCONFIG_DEBUG}));
  52. sub dprint {
  53. return if (!$debugprint);
  54. print STDERR @_;
  55. }
  56. my $uname = `uname -r`;
  57. chomp $uname;
  58. my @searchconfigs = (
  59. {
  60. "file" => ".config",
  61. "exec" => "cat",
  62. },
  63. {
  64. "file" => "/proc/config.gz",
  65. "exec" => "zcat",
  66. },
  67. {
  68. "file" => "/boot/config-$uname",
  69. "exec" => "cat",
  70. },
  71. {
  72. "file" => "/boot/vmlinuz-$uname",
  73. "exec" => "scripts/extract-ikconfig",
  74. "test" => "scripts/extract-ikconfig",
  75. },
  76. {
  77. "file" => "vmlinux",
  78. "exec" => "scripts/extract-ikconfig",
  79. "test" => "scripts/extract-ikconfig",
  80. },
  81. {
  82. "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
  83. "exec" => "scripts/extract-ikconfig",
  84. "test" => "scripts/extract-ikconfig",
  85. },
  86. {
  87. "file" => "kernel/configs.ko",
  88. "exec" => "scripts/extract-ikconfig",
  89. "test" => "scripts/extract-ikconfig",
  90. },
  91. {
  92. "file" => "kernel/configs.o",
  93. "exec" => "scripts/extract-ikconfig",
  94. "test" => "scripts/extract-ikconfig",
  95. },
  96. );
  97. sub read_config {
  98. foreach my $conf (@searchconfigs) {
  99. my $file = $conf->{"file"};
  100. next if ( ! -f "$file");
  101. if (defined($conf->{"test"})) {
  102. `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
  103. next if ($?);
  104. }
  105. my $exec = $conf->{"exec"};
  106. print STDERR "using config: '$file'\n";
  107. open(my $infile, '-|', "$exec $file") || die "Failed to run $exec $file";
  108. my @x = <$infile>;
  109. close $infile;
  110. return @x;
  111. }
  112. die "No config file found";
  113. }
  114. my @config_file = read_config;
  115. # Parse options
  116. my $localmodconfig = 0;
  117. my $localyesconfig = 0;
  118. GetOptions("localmodconfig" => \$localmodconfig,
  119. "localyesconfig" => \$localyesconfig);
  120. # Get the build source and top level Kconfig file (passed in)
  121. my $ksource = ($ARGV[0] ? $ARGV[0] : '.');
  122. my $kconfig = $ARGV[1];
  123. my $lsmod_file = $ENV{'LSMOD'};
  124. my @makefiles = `find $ksource -name Makefile -or -name Kbuild 2>/dev/null`;
  125. chomp @makefiles;
  126. my %depends;
  127. my %selects;
  128. my %prompts;
  129. my %objects;
  130. my %config2kfile;
  131. my %defaults;
  132. my $var;
  133. my $iflevel = 0;
  134. my @ifdeps;
  135. # prevent recursion
  136. my %read_kconfigs;
  137. sub read_kconfig {
  138. my ($kconfig) = @_;
  139. my $state = "NONE";
  140. my $config;
  141. my $cont = 0;
  142. my $line;
  143. my $source = "$ksource/$kconfig";
  144. my $last_source = "";
  145. # Check for any environment variables used
  146. while ($source =~ /\$\((\w+)\)/ && $last_source ne $source) {
  147. my $env = $1;
  148. $last_source = $source;
  149. $source =~ s/\$\($env\)/$ENV{$env}/;
  150. }
  151. open(my $kinfile, '<', $source) || die "Can't open $source";
  152. while (<$kinfile>) {
  153. chomp;
  154. # Make sure that lines ending with \ continue
  155. if ($cont) {
  156. $_ = $line . " " . $_;
  157. }
  158. if (s/\\$//) {
  159. $cont = 1;
  160. $line = $_;
  161. next;
  162. }
  163. $cont = 0;
  164. # collect any Kconfig sources
  165. if (/^source\s+"?([^"]+)/) {
  166. my $kconfig = $1;
  167. # prevent reading twice.
  168. if (!defined($read_kconfigs{$kconfig})) {
  169. $read_kconfigs{$kconfig} = 1;
  170. read_kconfig($kconfig);
  171. }
  172. next;
  173. }
  174. # configs found
  175. if (/^\s*(menu)?config\s+(\S+)\s*$/) {
  176. $state = "NEW";
  177. $config = $2;
  178. $config2kfile{"CONFIG_$config"} = $kconfig;
  179. # Add depends for 'if' nesting
  180. for (my $i = 0; $i < $iflevel; $i++) {
  181. if ($i) {
  182. $depends{$config} .= " " . $ifdeps[$i];
  183. } else {
  184. $depends{$config} = $ifdeps[$i];
  185. }
  186. $state = "DEP";
  187. }
  188. # collect the depends for the config
  189. } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
  190. $state = "DEP";
  191. $depends{$config} = $1;
  192. } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
  193. $depends{$config} .= " " . $1;
  194. } elsif ($state ne "NONE" && /^\s*def(_(bool|tristate)|ault)\s+(\S.*)$/) {
  195. my $dep = $3;
  196. $defaults{$config} = 1;
  197. if ($dep !~ /^\s*(y|m|n)\s*$/) {
  198. $dep =~ s/.*\sif\s+//;
  199. $depends{$config} .= " " . $dep;
  200. dprint "Added default depends $dep to $config\n";
  201. }
  202. # Get the configs that select this config
  203. } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
  204. my $conf = $1;
  205. if (defined($selects{$conf})) {
  206. $selects{$conf} .= " " . $config;
  207. } else {
  208. $selects{$conf} = $config;
  209. }
  210. # configs without prompts must be selected
  211. } elsif ($state ne "NONE" && /^\s*(tristate\s+\S|prompt\b)/) {
  212. # note if the config has a prompt
  213. $prompts{$config} = 1;
  214. # Check for if statements
  215. } elsif (/^if\s+(.*\S)\s*$/) {
  216. my $deps = $1;
  217. # remove beginning and ending non text
  218. $deps =~ s/^[^a-zA-Z0-9_]*//;
  219. $deps =~ s/[^a-zA-Z0-9_]*$//;
  220. my @deps = split /[^a-zA-Z0-9_]+/, $deps;
  221. $ifdeps[$iflevel++] = join ':', @deps;
  222. } elsif (/^endif/) {
  223. $iflevel-- if ($iflevel);
  224. # stop on "help" and keywords that end a menu entry
  225. } elsif (/^\s*(---)?help(---)?\s*$/ || /^(comment|choice|menu)\b/) {
  226. $state = "NONE";
  227. }
  228. }
  229. close($kinfile);
  230. }
  231. if ($kconfig) {
  232. read_kconfig($kconfig);
  233. }
  234. # Makefiles can use variables to define their dependencies
  235. sub convert_vars {
  236. my ($line, %vars) = @_;
  237. my $process = "";
  238. while ($line =~ s/^(.*?)(\$\((.*?)\))//) {
  239. my $start = $1;
  240. my $variable = $2;
  241. my $var = $3;
  242. if (defined($vars{$var})) {
  243. $process .= $start . $vars{$var};
  244. } else {
  245. $process .= $start . $variable;
  246. }
  247. }
  248. $process .= $line;
  249. return $process;
  250. }
  251. # Read all Makefiles to map the configs to the objects
  252. foreach my $makefile (@makefiles) {
  253. my $line = "";
  254. my %make_vars;
  255. open(my $infile, '<', $makefile) || die "Can't open $makefile";
  256. while (<$infile>) {
  257. # if this line ends with a backslash, continue
  258. chomp;
  259. if (/^(.*)\\$/) {
  260. $line .= $1;
  261. next;
  262. }
  263. $line .= $_;
  264. $_ = $line;
  265. $line = "";
  266. my $objs;
  267. # Convert variables in a line (could define configs)
  268. $_ = convert_vars($_, %make_vars);
  269. # collect objects after obj-$(CONFIG_FOO_BAR)
  270. if (/obj-\$[({](CONFIG_[^})]*)[)}]\s*[+:]?=\s*(.*)/) {
  271. $var = $1;
  272. $objs = $2;
  273. # check if variables are set
  274. } elsif (/^\s*(\S+)\s*[:]?=\s*(.*\S)/) {
  275. $make_vars{$1} = $2;
  276. }
  277. if (defined($objs)) {
  278. foreach my $obj (split /\s+/,$objs) {
  279. $obj =~ s/-/_/g;
  280. if ($obj =~ /(.*)\.o$/) {
  281. # Objects may be enabled by more than one config.
  282. # Store configs in an array.
  283. my @arr;
  284. if (defined($objects{$1})) {
  285. @arr = @{$objects{$1}};
  286. }
  287. $arr[$#arr+1] = $var;
  288. # The objects have a hash mapping to a reference
  289. # of an array of configs.
  290. $objects{$1} = \@arr;
  291. }
  292. }
  293. }
  294. }
  295. close($infile);
  296. }
  297. my %modules;
  298. my $linfile;
  299. if (defined($lsmod_file)) {
  300. if ( ! -f $lsmod_file) {
  301. if ( -f $ENV{'objtree'}."/".$lsmod_file) {
  302. $lsmod_file = $ENV{'objtree'}."/".$lsmod_file;
  303. } else {
  304. die "$lsmod_file not found";
  305. }
  306. }
  307. my $otype = ( -x $lsmod_file) ? '-|' : '<';
  308. open($linfile, $otype, $lsmod_file);
  309. } else {
  310. # see what modules are loaded on this system
  311. my $lsmod;
  312. foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) {
  313. if ( -x "$dir/lsmod" ) {
  314. $lsmod = "$dir/lsmod";
  315. last;
  316. }
  317. }
  318. if (!defined($lsmod)) {
  319. # try just the path
  320. $lsmod = "lsmod";
  321. }
  322. open($linfile, '-|', $lsmod) || die "Can not call lsmod with $lsmod";
  323. }
  324. while (<$linfile>) {
  325. next if (/^Module/); # Skip the first line.
  326. if (/^(\S+)/) {
  327. $modules{$1} = 1;
  328. }
  329. }
  330. close ($linfile);
  331. # add to the configs hash all configs that are needed to enable
  332. # a loaded module. This is a direct obj-${CONFIG_FOO} += bar.o
  333. # where we know we need bar.o so we add FOO to the list.
  334. my %configs;
  335. foreach my $module (keys(%modules)) {
  336. if (defined($objects{$module})) {
  337. my @arr = @{$objects{$module}};
  338. foreach my $conf (@arr) {
  339. $configs{$conf} = $module;
  340. dprint "$conf added by direct ($module)\n";
  341. if ($debugprint) {
  342. my $c=$conf;
  343. $c =~ s/^CONFIG_//;
  344. if (defined($depends{$c})) {
  345. dprint " deps = $depends{$c}\n";
  346. } else {
  347. dprint " no deps\n";
  348. }
  349. }
  350. }
  351. } else {
  352. # Most likely, someone has a custom (binary?) module loaded.
  353. print STDERR "$module config not found!\n";
  354. }
  355. }
  356. # Read the current config, and see what is enabled. We want to
  357. # ignore configs that we would not enable anyway.
  358. my %orig_configs;
  359. my $valid = "A-Za-z_0-9";
  360. foreach my $line (@config_file) {
  361. $_ = $line;
  362. if (/(CONFIG_[$valid]*)=(m|y)/) {
  363. $orig_configs{$1} = $2;
  364. }
  365. }
  366. my $repeat = 1;
  367. my $depconfig;
  368. #
  369. # Note, we do not care about operands (like: &&, ||, !) we want to add any
  370. # config that is in the depend list of another config. This script does
  371. # not enable configs that are not already enabled. If we come across a
  372. # config A that depends on !B, we can still add B to the list of depends
  373. # to keep on. If A was on in the original config, B would not have been
  374. # and B would not be turned on by this script.
  375. #
  376. sub parse_config_depends
  377. {
  378. my ($p) = @_;
  379. while ($p =~ /[$valid]/) {
  380. if ($p =~ /^[^$valid]*([$valid]+)/) {
  381. my $conf = "CONFIG_" . $1;
  382. $p =~ s/^[^$valid]*[$valid]+//;
  383. # We only need to process if the depend config is a module
  384. if (!defined($orig_configs{$conf}) || $orig_configs{$conf} eq "y") {
  385. next;
  386. }
  387. if (!defined($configs{$conf})) {
  388. # We must make sure that this config has its
  389. # dependencies met.
  390. $repeat = 1; # do again
  391. dprint "$conf selected by depend $depconfig\n";
  392. $configs{$conf} = 1;
  393. }
  394. } else {
  395. die "this should never happen";
  396. }
  397. }
  398. }
  399. # Select is treated a bit differently than depends. We call this
  400. # when a config has no prompt and requires another config to be
  401. # selected. We use to just select all configs that selected this
  402. # config, but found that that can balloon into enabling hundreds
  403. # of configs that we do not care about.
  404. #
  405. # The idea is we look at all the configs that select it. If one
  406. # is already in our list of configs to enable, then there's nothing
  407. # else to do. If there isn't, we pick the first config that was
  408. # enabled in the original config and use that.
  409. sub parse_config_selects
  410. {
  411. my ($config, $p) = @_;
  412. my $next_config;
  413. while ($p =~ /[$valid]/) {
  414. if ($p =~ /^[^$valid]*([$valid]+)/) {
  415. my $conf = "CONFIG_" . $1;
  416. $p =~ s/^[^$valid]*[$valid]+//;
  417. # Make sure that this config exists in the current .config file
  418. if (!defined($orig_configs{$conf})) {
  419. dprint "$conf not set for $config select\n";
  420. next;
  421. }
  422. # Check if something other than a module selects this config
  423. if (defined($orig_configs{$conf}) && $orig_configs{$conf} ne "m") {
  424. dprint "$conf (non module) selects $config, we are good\n";
  425. # we are good with this
  426. return;
  427. }
  428. if (defined($configs{$conf})) {
  429. dprint "$conf selects $config so we are good\n";
  430. # A set config selects this config, we are good
  431. return;
  432. }
  433. # Set this config to be selected
  434. if (!defined($next_config)) {
  435. $next_config = $conf;
  436. }
  437. } else {
  438. die "this should never happen";
  439. }
  440. }
  441. # If no possible config selected this, then something happened.
  442. if (!defined($next_config)) {
  443. # Some config options have no prompt, and nothing selects them, but
  444. # they stay turned on once the final checks for the configs
  445. # are done. These configs have a default option, so turn off the
  446. # warnings for configs with default options.
  447. if (!defined($defaults{$config})) {
  448. print STDERR "WARNING: $config is required, but nothing in the\n";
  449. print STDERR " current config selects it.\n";
  450. }
  451. return;
  452. }
  453. # If we are here, then we found no config that is set and
  454. # selects this config. Repeat.
  455. $repeat = 1;
  456. # Make this config need to be selected
  457. $configs{$next_config} = 1;
  458. dprint "$next_config selected by select $config\n";
  459. }
  460. my %process_selects;
  461. # loop through all configs, select their dependencies.
  462. sub loop_depend {
  463. $repeat = 1;
  464. while ($repeat) {
  465. $repeat = 0;
  466. forloop:
  467. foreach my $config (keys %configs) {
  468. # If this config is not a module, we do not need to process it
  469. if (defined($orig_configs{$config}) && $orig_configs{$config} ne "m") {
  470. next forloop;
  471. }
  472. $config =~ s/^CONFIG_//;
  473. $depconfig = $config;
  474. if (defined($depends{$config})) {
  475. # This config has dependencies. Make sure they are also included
  476. parse_config_depends $depends{$config};
  477. }
  478. # If the config has no prompt, then we need to check if a config
  479. # that is enabled selected it. Or if we need to enable one.
  480. if (!defined($prompts{$config}) && defined($selects{$config})) {
  481. $process_selects{$config} = 1;
  482. }
  483. }
  484. }
  485. }
  486. sub loop_select {
  487. foreach my $config (keys %process_selects) {
  488. $config =~ s/^CONFIG_//;
  489. dprint "Process select $config\n";
  490. # config has no prompt and must be selected.
  491. parse_config_selects $config, $selects{$config};
  492. }
  493. }
  494. while ($repeat) {
  495. # Get the first set of configs and their dependencies.
  496. loop_depend;
  497. $repeat = 0;
  498. # Now we need to see if we have to check selects;
  499. loop_select;
  500. }
  501. my %setconfigs;
  502. my @preserved_kconfigs;
  503. if (defined($ENV{'LMC_KEEP'})) {
  504. @preserved_kconfigs = split(/:/,$ENV{LMC_KEEP});
  505. }
  506. sub in_preserved_kconfigs {
  507. my $kconfig = $config2kfile{$_[0]};
  508. if (!defined($kconfig)) {
  509. return 0;
  510. }
  511. foreach my $excl (@preserved_kconfigs) {
  512. if($kconfig =~ /^$excl/) {
  513. return 1;
  514. }
  515. }
  516. return 0;
  517. }
  518. # Finally, read the .config file and turn off any module enabled that
  519. # we could not find a reason to keep enabled.
  520. foreach my $line (@config_file) {
  521. $_ = $line;
  522. if (/CONFIG_IKCONFIG/) {
  523. if (/# CONFIG_IKCONFIG is not set/) {
  524. # enable IKCONFIG at least as a module
  525. print "CONFIG_IKCONFIG=m\n";
  526. # don't ask about PROC
  527. print "# CONFIG_IKCONFIG_PROC is not set\n";
  528. } else {
  529. print;
  530. }
  531. next;
  532. }
  533. if (/CONFIG_MODULE_SIG_KEY="(.+)"/) {
  534. my $orig_cert = $1;
  535. my $default_cert = "certs/signing_key.pem";
  536. # Check that the logic in this script still matches the one in Kconfig
  537. if (!defined($depends{"MODULE_SIG_KEY"}) ||
  538. $depends{"MODULE_SIG_KEY"} !~ /"\Q$default_cert\E"/) {
  539. print STDERR "WARNING: MODULE_SIG_KEY assertion failure, ",
  540. "update needed to ", __FILE__, " line ", __LINE__, "\n";
  541. print;
  542. } elsif ($orig_cert ne $default_cert && ! -f $orig_cert) {
  543. print STDERR "Module signature verification enabled but ",
  544. "module signing key \"$orig_cert\" not found. Resetting ",
  545. "signing key to default value.\n";
  546. print "CONFIG_MODULE_SIG_KEY=\"$default_cert\"\n";
  547. } else {
  548. print;
  549. }
  550. next;
  551. }
  552. if (/CONFIG_SYSTEM_TRUSTED_KEYS="(.+)"/) {
  553. my $orig_keys = $1;
  554. if (! -f $orig_keys) {
  555. print STDERR "System keyring enabled but keys \"$orig_keys\" ",
  556. "not found. Resetting keys to default value.\n";
  557. print "CONFIG_SYSTEM_TRUSTED_KEYS=\"\"\n";
  558. } else {
  559. print;
  560. }
  561. next;
  562. }
  563. if (/^(CONFIG.*)=(m|y)/) {
  564. if (in_preserved_kconfigs($1)) {
  565. dprint "Preserve config $1";
  566. print;
  567. next;
  568. }
  569. if (defined($configs{$1})) {
  570. if ($localyesconfig) {
  571. $setconfigs{$1} = 'y';
  572. print "$1=y\n";
  573. next;
  574. } else {
  575. $setconfigs{$1} = $2;
  576. }
  577. } elsif ($2 eq "m") {
  578. print "# $1 is not set\n";
  579. next;
  580. }
  581. }
  582. print;
  583. }
  584. # Integrity check, make sure all modules that we want enabled do
  585. # indeed have their configs set.
  586. loop:
  587. foreach my $module (keys(%modules)) {
  588. if (defined($objects{$module})) {
  589. my @arr = @{$objects{$module}};
  590. foreach my $conf (@arr) {
  591. if (defined($setconfigs{$conf})) {
  592. next loop;
  593. }
  594. }
  595. print STDERR "module $module did not have configs";
  596. foreach my $conf (@arr) {
  597. print STDERR " " , $conf;
  598. }
  599. print STDERR "\n";
  600. }
  601. }
  602. # vim: softtabstop=4