4 use vars qw(@ISA @EXPORT);
6 @EXPORT = qw(get_module_entry search_file get_entries);
8 my @internal_searchpaths;
10 # extract an entry with modules from a modules.list file
11 sub get_module_entry($$)
13 my ($mod_file, $entry_to_pick) = @_;
17 if ($entry_to_pick eq 'auto-build-entry') {
18 print "Automatic build entry is being built.\n";
19 print "This image is useless but it always builds.\n";
21 $mods[0] = { command => 'Makefile', cmdline => 'Makefile', type => 'bin'};
22 $mods[1] = { command => 'Makefile', cmdline => 'Makefile', type => 'bin'};
23 $mods[2] = { command => 'Makefile', cmdline => 'Makefile', type => 'bin'};
31 open(M, $mod_file) || die "Cannot open $mod_file!: $!";
33 # preseed first 3 modules
34 $mods[0] = { command => 'fiasco', cmdline => 'fiasco', type => 'bin'};
35 $mods[1] = { command => 'sigma0', cmdline => 'sigma0', type => 'bin'};
36 $mods[2] = { command => 'roottask', cmdline => 'moe', type => 'bin'};
40 my $process_mode = undef;
45 my $bootstrap_command = "bootstrap";
46 my $bootstrap_cmdline = "bootstrap";
56 if (/^modaddr\s+(\S+)/) {
57 $modaddr_global = $1 if $global;
58 $modaddr_title = $1 if !$global;
62 my ($type, $remaining) = split /\s+/, $_, 2;
65 $type = 'bin' if $type eq 'module';
67 if ($type =~ /^(entry|title)$/) {
68 if (lc($entry_to_pick) eq lc($remaining)) {
69 $process_mode = 'entry';
72 $process_mode = undef;
76 } elsif ($type eq 'searchpath') {
77 push @internal_searchpaths, $remaining;
79 } elsif ($type eq 'group') {
80 $process_mode = 'group';
81 $current_group_name = (split /\s+/, $remaining)[0];
85 next unless $process_mode;
87 my @valid_types = ( 'bin', 'data', 'bin-nostrip', 'data-nostrip',
88 'bootstrap', 'roottask', 'kernel', 'sigma0',
89 'module-perl', 'module-shell', 'module-glob',
90 'module-group', 'initrd', 'set');
91 die "$line: Invalid type \"$type\""
92 unless grep(/^$type$/, @valid_types);
97 my ($varname, $value) = split /\s+/, $remaining, 2;
98 $is_mode_linux = 1 if $varname eq 'mode' and lc($value) eq 'linux';
101 if ($type eq 'module-perl') {
102 @m = eval $remaining;
105 } elsif ($type eq 'module-shell') {
106 @m = split /\n/, `$remaining`;
107 die "Shell command on line $line failed\n" if $?;
109 } elsif ($type eq 'module-glob') {
110 @m = glob $remaining;
112 } elsif ($type eq 'module-group') {
114 foreach (split /\s+/, $remaining) {
115 die "Unknown group '$_'" unless defined $groups{$_};
116 push @m, @{$groups{$_}};
121 if ($process_mode eq 'entry') {
124 my ($file, $args) = split /\s+/, $m, 2;
127 $full .= " $args" if defined $args;
130 if (length($full) > $arglen) {
131 print "$line: \"$full\" too long...\n";
136 if ($type eq 'bootstrap') {
137 $bootstrap_command = $file;
138 $bootstrap_cmdline = $full;
139 } elsif ($type =~ /(rmgr|roottask)/i) {
140 $mods[2]{command} = $file;
141 $mods[2]{cmdline} = $full;
142 } elsif ($type eq 'kernel') {
143 $mods[0]{command} = $file;
144 $mods[0]{cmdline} = $full;
145 } elsif ($type eq 'sigma0') {
146 $mods[1]{command} = $file;
147 $mods[1]{cmdline} = $full;
148 } elsif ($type eq 'initrd') {
149 $linux_initrd = $full;
159 } elsif ($process_mode eq 'group') {
160 push @{$groups{$current_group_name}}, @m;
162 die "Invalid mode '$process_mode'";
168 die "Unknown entry \"$entry_to_pick\"!" unless $found_entry;
169 die "'modaddr' not set" unless $modaddr_title || $modaddr_global;
171 my $m = $modaddr_title || $modaddr_global;
172 if (defined $is_mode_linux)
174 die "No Linux kernel image defined" unless defined $mods[0]{cmdline};
175 print "Entry '$entry_to_pick' is a Linux type entry\n";
177 # @files is actually redundant but eases file selection for entry
179 push @files, $mods[0]{command};
180 push @files, $linux_initrd if defined $linux_initrd;
183 # actually bootstrap is always the kernel in this
184 # environment, for convenience we use $mods[0] because that
185 # are the contents of 'kernel xxx' which sounds more
187 bootstrap => { command => $mods[0]{command},
188 cmdline => $mods[0]{cmdline}},
192 $r{initrd} = { cmdline => $linux_initrd } if defined $linux_initrd;
196 # now some implicit stuff
197 if ($bootstrap_cmdline =~ /-modaddr\s+/) {
198 $bootstrap_cmdline =~ s/(-modaddr\s+)%modaddr%/$1$m/;
200 $bootstrap_cmdline .= " -modaddr $m";
203 my @filse; # again, this is redundant but helps generator functions
204 push @files, $bootstrap_command;
205 push @files, $_->{command} foreach @mods;
208 bootstrap => { command => $bootstrap_command,
209 cmdline => $bootstrap_cmdline },
211 modaddr => $modaddr_title || $modaddr_global,
217 sub entry_is_linux(%)
220 return defined $e{type} && $e{type} eq 'Linux';
226 return defined $e{type} and $e{type} eq 'MB';
234 open(M, $mod_file) || die "Cannot open $mod_file!: $!";
241 push @entry_list, $2 if /^(entry|title)\s+(.+)/;
249 # Search for a file by using a path list (single string, split with colons
250 # or spaces, see the split)
251 # return undef if it could not be found, the complete path otherwise
257 foreach my $p (split(/[:\s]+/, $paths), @internal_searchpaths) {
258 return "$p/$file" if -e "$p/$file" and ! -d "$p/$file";
261 return $file if $file =~ /^\// && -e $file;
266 sub search_file_or_die($$)
270 my $f = search_file($file, $paths);
271 die "Could not find '$file' with path '$paths'" unless defined $f;
275 sub get_file_uncompressed_or_die($$$)
281 my $fp = L4::ModList::search_file_or_die($command, $paths);
283 open F, $fp || die "connot open '$fp': $!";
288 if (unpack("n", $buf) == 0x1f8b) {
289 (my $tf = $fp) =~ s|.*/||;
290 $tf = $tmpdir.'/'.$tf;
291 print "'$fp' is a zipped file, uncompressing to '$tf'\n";
292 system("zcat $fp >$tf");
302 sub generate_grub1_entry($$%)
304 my $entryname = shift;
306 $prefix = '' unless defined $prefix;
307 $prefix = "/$prefix" if $prefix ne '' and $prefix !~ /^\//;
309 my $s = "title $entryname\n";
310 my $c = $entry{bootstrap}{cmdline};
311 $c =~ s/^\S*\/([^\/]+\s*)/$1/;
312 $s .= "kernel $prefix/$c\n";
314 if (entry_is_linux(%entry) and defined $entry{initrd})
316 $c = $entry{initrd}{cmdline};
317 $c =~ s/^\S*\/([^\/]+\s*)/$1/;
318 $s .= "initrd $prefix/$c\n";
322 foreach my $m (@{$entry{mods}})
325 $c =~ s/^\S*\/([^\/]+\s*)/$1/;
326 $s .= "module $prefix/$c\n";
331 sub generate_grub2_entry($$%)
333 my $entryname = shift;
335 $prefix = '' unless defined $prefix;
336 $prefix = "/$prefix" if $prefix ne '' and $prefix !~ /^\//;
338 # basename of first path
339 my ($c, $args) = split(/\s+/, $entry{bootstrap}{cmdline}, 2);
340 my $bn = (reverse split(/\/+/, $c))[0];
341 my $s = "menuentry \"$entryname\" {\n";
343 if (entry_is_linux(%entry))
345 $s .= " echo Loading '$prefix/$bn $args'\n";
346 $s .= " linux $prefix/$bn $args\n";
347 if (defined $entry{initrd})
349 $bn = (reverse split(/\/+/, $entry{initrd}{cmdline}))[0];
350 my $c = $entry{initrd}{cmdline};
351 $c =~ s/^\S*(\/[^\/]+\s*)/$1/;
352 $s .= " initrd $prefix$c\n";
357 $s .= " echo Loading '$prefix/$bn $prefix/$bn $args'\n";
358 $s .= " multiboot $prefix/$bn $prefix/$bn $args\n";
359 foreach my $m (@{$entry{mods}})
362 $bn = (reverse split(/\/+/, $m->{command}))[0];
363 my $c = $m->{cmdline};
364 $c =~ s/^\S*(\/[^\/]+\s*)/$1/;
365 $s .= " echo Loading '$prefix/$bn $c'\n";
366 $s .= " module $prefix/$bn $c\n";
369 $s .= " echo Done, booting...\n";