| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# generate_firmware_task |
| 4 |
# |
| 5 |
# Work out which firmware packages we need |
| 6 |
# Several steps: |
| 7 |
# |
| 8 |
# 1. Look for packages which contain "firmware" or "microcode" in |
| 9 |
# their package names |
| 10 |
# 2. Check each of those packages to see if they contain files in |
| 11 |
# /lib/firmware or /usr/lib/firmware |
| 12 |
# 3. For those that do, output the package name into the firmware task |
| 13 |
# |
| 14 |
# Copyright Steve McIntyre <[email protected]> 2011 |
| 15 |
# Copyright Cyril Brulebois <[email protected]> 2023 |
| 16 |
# |
| 17 |
# GPL 2 |
| 18 |
# |
| 19 |
|
| 20 |
use strict; |
| 21 |
use File::Basename; |
| 22 |
|
| 23 |
my ($mirror, $codename, $archlist, $excludelist, $outfile, $localdebs, $backports); |
| 24 |
my $date; |
| 25 |
my $tasksdir; |
| 26 |
my (@pkgfiles, @bp_pkgfiles); |
| 27 |
my ($pkg, $filename, $arch); |
| 28 |
my %seen; |
| 29 |
my @components; |
| 30 |
my $cpp_command; |
| 31 |
my %excluded; |
| 32 |
|
| 33 |
sub contains_firmware($$) { |
| 34 |
my ($mirror, $filename) = @_; |
| 35 |
my $count = 0; |
| 36 |
open (PKGLISTING, "dpkg --contents $mirror/$filename |") or |
| 37 |
die "Can't check package file $filename: $!\n"; |
| 38 |
while ($_ = <PKGLISTING>) { |
| 39 |
$_ =~ /\.\/lib\/firmware\// and $count++; |
| 40 |
$_ =~ /\.\/usr\/lib\/firmware\// and $count++; |
| 41 |
} |
| 42 |
close PKGLISTING; |
| 43 |
if ($count) { |
| 44 |
return 1; |
| 45 |
} else { |
| 46 |
return 0; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
sub check_packages($$@) { |
| 51 |
my ($use_bp, $orig_mode, @pkgfiles) = @_; |
| 52 |
|
| 53 |
for my $pkgfile (@pkgfiles) { |
| 54 |
open (INPKG, "\$BASEDIR/tools/catz $pkgfile |") or die "Can't read input package files: $!\n"; |
| 55 |
$/ = ''; # Browse by paragraph |
| 56 |
|
| 57 |
while (defined($_ = <INPKG>)) { |
| 58 |
m/^Package: (\S+)/m and $pkg = $1; |
| 59 |
m/^Filename: (\S+)/m and $filename = $1; |
| 60 |
|
| 61 |
if (! ($pkg =~ /(microcode|firmware)/)) { |
| 62 |
next; |
| 63 |
} |
| 64 |
|
| 65 |
if (!exists $seen{$filename}) { |
| 66 |
$seen{$filename} = 1; |
| 67 |
|
| 68 |
# Don't include packages that we're told to exclude |
| 69 |
if ($excluded{$pkg}) { |
| 70 |
print STDERR " Ignoring $pkg\n"; |
| 71 |
next; |
| 72 |
} |
| 73 |
|
| 74 |
if (contains_firmware($mirror, $filename)) { |
| 75 |
printf STDERR " %-35s (%s)\n", $pkg, $filename; |
| 76 |
if ($orig_mode) { |
| 77 |
if ($use_bp) { |
| 78 |
print OUT "$pkg/$codename-backports\n"; |
| 79 |
} else { |
| 80 |
print OUT "$pkg\n"; |
| 81 |
} |
| 82 |
} else { |
| 83 |
print "$filename $pkgfile\n"; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
close INPKG; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
# Either the caller tells us explicitly which components to use, or we make this |
| 94 |
# determination on our own, based on various environment variables: |
| 95 |
sub read_env { |
| 96 |
my $env_var = shift; |
| 97 |
my $default = shift; |
| 98 |
|
| 99 |
if (exists($ENV{$env_var})) { |
| 100 |
return $ENV{$env_var}; |
| 101 |
} |
| 102 |
# else |
| 103 |
return $default; |
| 104 |
} |
| 105 |
|
| 106 |
# Either the caller tells us explicitly which components to use, or we make this |
| 107 |
# determination on our own, based on various environment variables, just like |
| 108 |
# tools/apt-selection does: |
| 109 |
@components = split /\ /, read_env('FIRMWARE_COMPONENTS', ''); |
| 110 |
if (!@components) { |
| 111 |
@components = qw(main); |
| 112 |
if (read_env('CONTRIB', 0) == 1) { |
| 113 |
push @components, qw(contrib); |
| 114 |
} |
| 115 |
if (read_env('NONFREE', 0) == 1 or read_env('EXTRANONFREE', 0) == 1 or read_env('FORCE_FIRMWARE', 0) == 1) { |
| 116 |
push @components, (split / /, read_env('NONFREE_COMPONENTS', '')); |
| 117 |
} |
| 118 |
} |
| 119 |
$codename = $ENV{'CODENAME'}; |
| 120 |
$mirror = $ENV{'MIRROR'}; |
| 121 |
$localdebs = $ENV{'LOCALDEBS'}; |
| 122 |
$backports = $ENV{'BACKPORTS'}; |
| 123 |
$archlist = shift; |
| 124 |
$excludelist = shift; |
| 125 |
$outfile = shift; |
| 126 |
|
| 127 |
if (!defined($codename) || !defined($mirror) || |
| 128 |
!defined($excludelist) || |
| 129 |
!defined($archlist) || !defined($outfile)) { |
| 130 |
die "Error in arguments\n"; |
| 131 |
} |
| 132 |
|
| 133 |
$tasksdir = dirname($outfile); |
| 134 |
$cpp_command = "cpp -traditional -undef -P -C -Wall -nostdinc -I$tasksdir"; |
| 135 |
|
| 136 |
foreach $arch (split(' ', $archlist)) { |
| 137 |
for my $component (@components) { |
| 138 |
if (defined($backports)) { |
| 139 |
push @bp_pkgfiles, "$mirror/dists/$codename-backports/$component/binary-$arch/Packages.gz"; |
| 140 |
push @bp_pkgfiles, "$mirror/dists/$codename-backports/$component/binary-$arch/Packages.xz"; |
| 141 |
} |
| 142 |
push @pkgfiles, "$mirror/dists/$codename/$component/binary-$arch/Packages.gz"; |
| 143 |
push @pkgfiles, "$mirror/dists/$codename/$component/binary-$arch/Packages.xz"; |
| 144 |
} |
| 145 |
$cpp_command .= " -DARCH$arch"; |
| 146 |
} |
| 147 |
|
| 148 |
if (defined($localdebs)) { |
| 149 |
for my $component (@components) { |
| 150 |
foreach $arch (split(' ', $archlist)) { |
| 151 |
if (defined($backports)) { |
| 152 |
push @bp_pkgfiles, "$localdebs/dists/$codename-backports/$component/binary-$arch/Packages.gz"; |
| 153 |
push @bp_pkgfiles, "$localdebs/dists/$codename-backports/$component/binary-$arch/Packages.xz"; |
| 154 |
} |
| 155 |
push @pkgfiles, "$localdebs/dists/$codename/$component/binary-$arch/Packages.gz"; |
| 156 |
push @pkgfiles, "$localdebs/dists/$codename/$component/binary-$arch/Packages.xz"; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
# Build the hash of excluded packages |
| 162 |
open (EXLIST, "$cpp_command $excludelist |") or |
| 163 |
die "Can't parse exclude list: $!\n"; |
| 164 |
while (defined (my $exc = <EXLIST>)) { |
| 165 |
chomp $exc; |
| 166 |
$excluded{$exc} = 1; |
| 167 |
} |
| 168 |
close EXLIST; |
| 169 |
|
| 170 |
# This special filename is an crude but easy way to distinguish the historical |
| 171 |
# caller (top-level Makefile) from tools/make-firmware-image which should use |
| 172 |
# the same logic, but is interested in the Filename for interesting firmware |
| 173 |
# packages: |
| 174 |
my $orig_mode = $outfile ne '--list-filenames-and-indices' ? 1 : 0; |
| 175 |
if ($orig_mode) { |
| 176 |
# Prepare the output file: |
| 177 |
open (OUT, "> $outfile") or die "Can't open outfile for writing: $!\n"; |
| 178 |
|
| 179 |
$date = `date -u`; |
| 180 |
chomp $date; |
| 181 |
|
| 182 |
print OUT "/*\n"; |
| 183 |
print OUT " * 'firmware' task file; generated automatically by generate_firmware_task\n"; |
| 184 |
print OUT " * for \"$archlist\" on $date\n"; |
| 185 |
print OUT " * Do not edit - changes will not be preserved\n"; |
| 186 |
print OUT " */\n"; |
| 187 |
} |
| 188 |
|
| 189 |
# Those function calls will either write the task to OUT (historical mode) or |
| 190 |
# filenames to stdout (for make-firmware-image): |
| 191 |
if (defined($backports)) { |
| 192 |
check_packages(1, $orig_mode, @bp_pkgfiles); |
| 193 |
} |
| 194 |
check_packages(0, $orig_mode, @pkgfiles); |
| 195 |
|
| 196 |
if ($orig_mode) { |
| 197 |
close OUT; |
| 198 |
} |