Commit 3e5c1f25 authored by Ryan Jackson's avatar Ryan Jackson

Remove unneeded firmware from initramfs

Once we determine which modules we need, parse the actual .ko files for
'firmware=.*' strings.  These provide the path(s) to that module's
firmware, relative to /lib/firmware.  Remove all firmware from the
output directory then copy in only the files we need.
parent 7e1cd70e
......@@ -25,6 +25,8 @@ my %aliases;
my %symbols;
my @order;
my @firmware;
sub parse_deps
{
my ($module_dir) = @_;
......@@ -111,6 +113,21 @@ sub parse_order
close ORDER;
}
sub parse_firmware
{
my ($module_file) = @_;
my @files;
open STRINGS, "strings \"$module_file\"|" or
die "Couldn't read $module_file: $!\n";
while (<STRINGS>) {
chomp;
next unless (/^firmware=(.*)$/);
push @files, $1;
}
return @files;
}
#my @modules = @ARGV;
my @modules = qw/unix sunrpc af_packet ext3 ext2 ufs/;
if (@ARGV != 3) {
......@@ -138,7 +155,9 @@ if ($? != 0) {
$version = glob "$tmpdir/lib/modules/*";
$version = (split /\/+/, $version)[-1];
$basedir = "$tmpdir/lib/modules";
$basedir = "$tmpdir/lib";
my $module_basedir = "$basedir/modules";
my $firmware_basedir = "$basedir/firmware";
open MODLIST, $modlist || die "Couldn't read $modlist: $!\n";
while (<MODLIST>) {
......@@ -150,9 +169,9 @@ close MODLIST;
my @list = @modules;
parse_deps("$basedir/$version");
parse_symbols("$basedir/$version");
parse_aliases("$basedir/$version");
parse_deps("$module_basedir/$version");
parse_symbols("$module_basedir/$version");
parse_aliases("$module_basedir/$version");
while (@list) {
my $module = shift @list;
......@@ -167,13 +186,16 @@ while (@list) {
$seen{$module} = 1;
}
parse_order("$basedir/$version");
parse_order("$module_basedir/$version");
$outdir .= '/lib/modules/' . $version;
rmtree($outdir);
mkpath($outdir);
my $module_outdir = $outdir . '/lib/modules/' . $version;
my $firmware_outdir = $outdir . '/lib/firmware';
rmtree($module_outdir);
rmtree($firmware_outdir);
mkpath($module_outdir);
mkpath($firmware_outdir);
open DEPS, ">$outdir/modules.dep" || die "Can't write to modules.dep: $!\n";
open DEPS, ">$module_outdir/modules.dep" || die "Can't write to modules.dep: $!\n";
#print "#modules.dep\n\n";
for my $module (keys %seen) {
my @tmp = map { $files{$_} } @{$deps{$module}};
......@@ -181,7 +203,7 @@ for my $module (keys %seen) {
}
close DEPS;
open SYMS, ">$outdir/modules.symbols" || die "Can't write to modules.symbols: $!\n";
open SYMS, ">$module_outdir/modules.symbols" || die "Can't write to modules.symbols: $!\n";
#print "#modules.symbols\n\n";
for my $module (keys %seen) {
for my $sym (@{$symbols{$module}}) {
......@@ -190,7 +212,7 @@ for my $module (keys %seen) {
}
close SYMS;
open ALIAS, ">$outdir/modules.alias" || die "Can't write to modules.alias: $!\n";
open ALIAS, ">$module_outdir/modules.alias" || die "Can't write to modules.alias: $!\n";
#print "#modules.aliases\n\n";
for my $module (keys %seen) {
for my $alias (@{$aliases{$module}}) {
......@@ -199,7 +221,7 @@ for my $module (keys %seen) {
}
close ALIAS;
open ORDER, ">$outdir/modules.order" || die "Can't write to modules.order: $!\n";
open ORDER, ">$module_outdir/modules.order" || die "Can't write to modules.order: $!\n";
print ORDER "$_\n" for (@order);
close ORDER;
......@@ -207,7 +229,19 @@ for my $module (keys %seen) {
my $file = $files{$module};
my $path = $file;
$path =~ s/\/[^\/]+$//;
mkpath($outdir . '/' . $path);
copy("$basedir/$version/$file", "$outdir/$file");
mkpath($module_outdir . '/' . $path);
copy("$module_basedir/$version/$file", "$module_outdir/$file");
push @firmware, parse_firmware("$module_basedir/$version/$file");
}
for my $fw (@firmware) {
my $path = $fw;
$path =~ s/\/[^\/]+$//;
mkpath($firmware_outdir . '/' . $path);
if (! -f "$firmware_basedir/$fw") {
print STDERR "WARNING: $firmware_basedir/$fw does not exist\n";
next;
}
copy("$firmware_basedir/$fw", "$firmware_outdir/$fw");
}
rmtree($tmpdir);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment