File: generate_firmware_task

package info (click to toggle)
debian-cd 3.2.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,848 kB
  • sloc: sh: 6,129; perl: 4,129; makefile: 413
file content (198 lines) | stat: -rwxr-xr-x 5,707 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/perl -w
#
# generate_firmware_task
#
# Work out which firmware packages we need
# Several steps:
#
# 1. Look for packages which contain "firmware" or "microcode" in
# their package names
# 2. Check each of those packages to see if they contain files in
# /lib/firmware or /usr/lib/firmware
# 3. For those that do, output the package name into the firmware task
#
# Copyright Steve McIntyre <93sam@debian.org> 2011
# Copyright Cyril Brulebois <kibi@debian.org> 2023
#
# GPL 2
#

use strict;
use File::Basename;

my ($mirror, $codename, $archlist, $excludelist, $outfile, $localdebs, $backports);
my $date;
my $tasksdir;
my (@pkgfiles, @bp_pkgfiles);
my ($pkg, $filename, $arch);
my %seen;
my @components;
my $cpp_command;
my %excluded;

sub contains_firmware($$) {
    my ($mirror, $filename) = @_;
    my $count = 0;
    open (PKGLISTING, "dpkg --contents $mirror/$filename |") or
	die "Can't check package file $filename: $!\n";
    while ($_ = <PKGLISTING>) {
	$_ =~ /\.\/lib\/firmware\// and $count++;
	$_ =~ /\.\/usr\/lib\/firmware\// and $count++;
    }
    close PKGLISTING;
    if ($count) {
	return 1;
    } else {
	return 0;
    }
}

sub check_packages($$@) {
    my ($use_bp, $orig_mode, @pkgfiles) = @_;

    for my $pkgfile (@pkgfiles) {
	open (INPKG, "\$BASEDIR/tools/catz $pkgfile |") or die "Can't read input package files: $!\n";
	$/ = ''; # Browse by paragraph

	while (defined($_ = <INPKG>)) {
	    m/^Package: (\S+)/m and $pkg = $1;
	    m/^Filename: (\S+)/m and $filename = $1;

	    if (! ($pkg =~ /(microcode|firmware)/)) {
		next;
	    }

	    if (!exists $seen{$filename}) {
		$seen{$filename} = 1;

		# Don't include packages that we're told to exclude
		if ($excluded{$pkg}) {
		    print STDERR "  Ignoring $pkg\n";
		    next;
		}

		if (contains_firmware($mirror, $filename)) {
		    printf STDERR "  %-35s (%s)\n", $pkg, $filename;
		    if ($orig_mode) {
			if ($use_bp) {
			    print OUT "$pkg/$codename-backports\n";
			} else {
			    print OUT "$pkg\n";
			}
		    } else {
			print "$filename $pkgfile\n";
		    }
		}
	    }
	}
	close INPKG;
    }
}


# Either the caller tells us explicitly which components to use, or we make this
# determination on our own, based on various environment variables:
sub read_env {
    my $env_var = shift;
    my $default = shift;

    if (exists($ENV{$env_var})) {
	return $ENV{$env_var};
    }
    # else
    return $default;
}

# Either the caller tells us explicitly which components to use, or we make this
# determination on our own, based on various environment variables, just like
# tools/apt-selection does:
@components = split /\ /, read_env('FIRMWARE_COMPONENTS', '');
if (!@components) {
    @components = qw(main);
    if (read_env('CONTRIB', 0) == 1) {
	push @components, qw(contrib);
    }
    if (read_env('NONFREE', 0) == 1 or read_env('EXTRANONFREE', 0) == 1 or read_env('FORCE_FIRMWARE', 0) == 1) {
	push @components, (split / /, read_env('NONFREE_COMPONENTS', ''));
    }
}
$codename = $ENV{'CODENAME'};
$mirror = $ENV{'MIRROR'};
$localdebs = $ENV{'LOCALDEBS'};
$backports = $ENV{'BACKPORTS'};
$archlist = shift;
$excludelist = shift;
$outfile = shift;

if (!defined($codename) || !defined($mirror) ||
    !defined($excludelist) ||
    !defined($archlist) || !defined($outfile)) {
    die "Error in arguments\n";
}

$tasksdir = dirname($outfile);
$cpp_command = "cpp -traditional -undef -P -C -Wall -nostdinc -I$tasksdir";

foreach $arch (split(' ', $archlist)) {
    for my $component (@components) {
	if (defined($backports)) {
	    push @bp_pkgfiles, "$mirror/dists/$codename-backports/$component/binary-$arch/Packages.gz";
	    push @bp_pkgfiles, "$mirror/dists/$codename-backports/$component/binary-$arch/Packages.xz";
	}
	push @pkgfiles, "$mirror/dists/$codename/$component/binary-$arch/Packages.gz";
	push @pkgfiles, "$mirror/dists/$codename/$component/binary-$arch/Packages.xz";
    }
    $cpp_command .= " -DARCH$arch";
}

if (defined($localdebs)) {
    for my $component (@components) {
	foreach $arch (split(' ', $archlist)) {
	    if (defined($backports)) {
		push @bp_pkgfiles, "$localdebs/dists/$codename-backports/$component/binary-$arch/Packages.gz";
		push @bp_pkgfiles, "$localdebs/dists/$codename-backports/$component/binary-$arch/Packages.xz";
	    }
	    push @pkgfiles, "$localdebs/dists/$codename/$component/binary-$arch/Packages.gz";
	    push @pkgfiles, "$localdebs/dists/$codename/$component/binary-$arch/Packages.xz";
	}
    }
}

# Build the hash of excluded packages
open (EXLIST, "$cpp_command $excludelist |") or
    die "Can't parse exclude list: $!\n";
while (defined (my $exc = <EXLIST>)) {
    chomp $exc;
    $excluded{$exc} = 1;
}
close EXLIST;

# This special filename is an crude but easy way to distinguish the historical
# caller (top-level Makefile) from tools/make-firmware-image which should use
# the same logic, but is interested in the Filename for interesting firmware
# packages:
my $orig_mode = $outfile ne '--list-filenames-and-indices' ? 1 : 0;
if ($orig_mode) {
    # Prepare the output file:
    open (OUT, "> $outfile") or die "Can't open outfile for writing: $!\n";

    $date = `date -u`;
    chomp $date;

    print OUT "/*\n";
    print OUT " * 'firmware' task file; generated automatically by generate_firmware_task\n";
    print OUT " * for \"$archlist\" on $date\n";
    print OUT " * Do not edit - changes will not be preserved\n";
    print OUT " */\n";
}

# Those function calls will either write the task to OUT (historical mode) or
# filenames to stdout (for make-firmware-image):
if (defined($backports)) {
    check_packages(1, $orig_mode, @bp_pkgfiles);
}
check_packages(0, $orig_mode, @pkgfiles);

if ($orig_mode) {
    close OUT;
}