4 use vars qw(@ISA @EXPORT);
6 @EXPORT = qw(get_module_entry search_file get_entries);
8 my @internal_searchpaths;
12 sub get_command_and_cmdline($)
14 my ($file, $args) = split /\s+/, $_[0], 2;
17 $full .= " $args" if defined $args;
20 if (length($full) > $arglen) {
21 print "$line: \"$full\" too long...\n";
28 # extract an entry with modules from a modules.list file
29 sub get_module_entry($$)
31 my ($mod_file, $entry_to_pick) = @_;
35 if ($entry_to_pick eq 'auto-build-entry') {
36 print "Automatic build entry is being built.\n";
37 print "This image is useless but it always builds.\n";
39 $mods[0] = { command => 'Makefile', cmdline => 'Makefile', type => 'bin'};
40 $mods[1] = { command => 'Makefile', cmdline => 'Makefile', type => 'bin'};
41 $mods[2] = { command => 'Makefile', cmdline => 'Makefile', type => 'bin'};
49 open(M, $mod_file) || die "Cannot open $mod_file!: $!";
51 # preseed first 3 modules
52 $mods[0] = { command => 'fiasco', cmdline => 'fiasco', type => 'bin'};
53 $mods[1] = { command => 'sigma0', cmdline => 'sigma0', type => 'bin'};
54 $mods[2] = { command => 'roottask', cmdline => 'moe', type => 'bin'};
57 my $process_mode = undef;
62 my $bootstrap_command = "bootstrap";
63 my $bootstrap_cmdline = "bootstrap";
73 if (/^modaddr\s+(\S+)/) {
74 $modaddr_global = $1 if $global;
75 $modaddr_title = $1 if !$global;
79 my ($type, $remaining) = split /\s+/, $_, 2;
82 $type = 'bin' if $type eq 'module';
84 if ($type =~ /^(entry|title)$/) {
85 if (lc($entry_to_pick) eq lc($remaining)) {
86 $process_mode = 'entry';
89 $process_mode = undef;
93 } elsif ($type eq 'searchpath') {
94 push @internal_searchpaths, $remaining;
96 } elsif ($type eq 'group') {
97 $process_mode = 'group';
98 $current_group_name = (split /\s+/, $remaining)[0];
100 } elsif ($type eq 'default-bootstrap') {
101 my ($file, $full) = get_command_and_cmdline($remaining);
102 $bootstrap_command = $file;
103 $bootstrap_cmdline = $full;
105 } elsif ($type eq 'default-kernel') {
106 my ($file, $full) = get_command_and_cmdline($remaining);
107 $mods[0]{command} = $file;
108 $mods[0]{cmdline} = $full;
110 } elsif ($type eq 'default-sigma0') {
111 my ($file, $full) = get_command_and_cmdline($remaining);
112 $mods[1]{command} = $file;
113 $mods[1]{cmdline} = $full;
115 } elsif ($type eq 'default-roottask') {
116 my ($file, $full) = get_command_and_cmdline($remaining);
117 $mods[2]{command} = $file;
118 $mods[2]{cmdline} = $full;
122 next unless $process_mode;
124 my @valid_types = ( 'bin', 'data', 'bin-nostrip', 'data-nostrip',
125 'bootstrap', 'roottask', 'kernel', 'sigma0',
126 'module-perl', 'module-shell', 'module-glob',
127 'module-group', 'moe', 'initrd', 'set');
128 die "$line: Invalid type \"$type\""
129 unless grep(/^$type$/, @valid_types);
133 if ($type eq 'set') {
134 my ($varname, $value) = split /\s+/, $remaining, 2;
135 $is_mode_linux = 1 if $varname eq 'mode' and lc($value) eq 'linux';
138 if ($type eq 'module-perl') {
139 @m = eval $remaining;
142 } elsif ($type eq 'module-shell') {
143 @m = split /\n/, `$remaining`;
144 die "Shell command on line $line failed\n" if $?;
146 } elsif ($type eq 'module-glob') {
147 @m = glob $remaining;
149 } elsif ($type eq 'module-group') {
151 foreach (split /\s+/, $remaining) {
152 die "Unknown group '$_'" unless defined $groups{$_};
153 push @m, @{$groups{$_}};
156 } elsif ($type eq 'moe') {
157 $mods[2]{command} = 'moe';
158 $mods[2]{cmdline} = "moe rom/$remaining";
163 if ($process_mode eq 'entry') {
166 my ($file, $full) = get_command_and_cmdline($m);
169 if ($type eq 'bootstrap') {
170 $bootstrap_command = $file;
171 $bootstrap_cmdline = $full;
172 } elsif ($type =~ /(rmgr|roottask)/i) {
173 $mods[2]{command} = $file;
174 $mods[2]{cmdline} = $full;
175 } elsif ($type eq 'kernel') {
176 $mods[0]{command} = $file;
177 $mods[0]{cmdline} = $full;
178 } elsif ($type eq 'sigma0') {
179 $mods[1]{command} = $file;
180 $mods[1]{cmdline} = $full;
181 } elsif ($type eq 'initrd') {
182 $linux_initrd = $file;
192 } elsif ($process_mode eq 'group') {
193 push @{$groups{$current_group_name}}, @m;
195 die "Invalid mode '$process_mode'";
201 die "Unknown entry \"$entry_to_pick\"!" unless $found_entry;
202 die "'modaddr' not set" unless $modaddr_title || $modaddr_global;
204 my $m = $modaddr_title || $modaddr_global;
205 if (defined $is_mode_linux)
207 die "No Linux kernel image defined" unless defined $mods[0]{cmdline};
208 print "Entry '$entry_to_pick' is a Linux type entry\n";
210 # @files is actually redundant but eases file selection for entry
212 push @files, $mods[0]{command};
213 push @files, $linux_initrd if defined $linux_initrd;
216 # actually bootstrap is always the kernel in this
217 # environment, for convenience we use $mods[0] because that
218 # are the contents of 'kernel xxx' which sounds more
220 bootstrap => { command => $mods[0]{command},
221 cmdline => $mods[0]{cmdline}},
225 $r{initrd} = { cmdline => $linux_initrd } if defined $linux_initrd;
229 # now some implicit stuff
230 if ($bootstrap_cmdline =~ /-modaddr\s+/) {
231 $bootstrap_cmdline =~ s/(-modaddr\s+)%modaddr%/$1$m/;
233 $bootstrap_cmdline .= " -modaddr $m";
236 my @filse; # again, this is redundant but helps generator functions
237 push @files, $bootstrap_command;
238 push @files, $_->{command} foreach @mods;
241 bootstrap => { command => $bootstrap_command,
242 cmdline => $bootstrap_cmdline },
244 modaddr => $modaddr_title || $modaddr_global,
250 sub entry_is_linux(%)
253 return defined $e{type} && $e{type} eq 'Linux';
259 return defined $e{type} and $e{type} eq 'MB';
267 open(M, $mod_file) || die "Cannot open $mod_file!: $!";
274 push @entry_list, $2 if /^(entry|title)\s+(.+)/;
282 # Search for a file by using a path list (single string, split with colons
283 # or spaces, see the split)
284 # return undef if it could not be found, the complete path otherwise
290 foreach my $p (split(/[:\s]+/, $paths), @internal_searchpaths) {
291 return "$p/$file" if -e "$p/$file" and ! -d "$p/$file";
294 return $file if $file =~ /^\// && -e $file;
299 sub search_file_or_die($$)
303 my $f = search_file($file, $paths);
304 die "Could not find '$file' with path '$paths'" unless defined $f;
308 sub get_file_uncompressed_or_die($$$)
314 my $fp = L4::ModList::search_file_or_die($command, $paths);
316 open F, $fp || die "connot open '$fp': $!";
321 if (length($buf) >= 2 && unpack("n", $buf) == 0x1f8b) {
322 (my $tf = $fp) =~ s|.*/||;
323 $tf = $tmpdir.'/'.$tf;
324 print "'$fp' is a zipped file, uncompressing to '$tf'\n";
325 system("zcat $fp >$tf");
335 sub generate_grub1_entry($$%)
337 my $entryname = shift;
339 $prefix = '' unless defined $prefix;
340 $prefix = "/$prefix" if $prefix ne '' and $prefix !~ /^\//;
342 my $s = "title $entryname\n";
343 my $c = $entry{bootstrap}{cmdline};
344 $c =~ s/^\S*\/([^\/]+\s*)/$1/;
345 $s .= "kernel $prefix/$c\n";
347 if (entry_is_linux(%entry) and defined $entry{initrd})
349 $c = $entry{initrd}{cmdline};
350 $c =~ s/^\S*\/([^\/]+\s*)/$1/;
351 $s .= "initrd $prefix/$c\n";
355 foreach my $m (@{$entry{mods}})
358 $c =~ s/^\S*\/([^\/]+\s*)/$1/;
359 $s .= "module $prefix/$c\n";
364 sub generate_grub2_entry($$%)
366 my $entryname = shift;
368 $prefix = '' unless defined $prefix;
369 $prefix = "/$prefix" if $prefix ne '' and $prefix !~ /^\//;
371 # basename of first path
372 my ($c, $args) = split(/\s+/, $entry{bootstrap}{cmdline}, 2);
373 my $bn = (reverse split(/\/+/, $c))[0];
374 my $s = "menuentry \"$entryname\" {\n";
376 if (entry_is_linux(%entry))
378 $s .= " echo Loading '$prefix/$bn $args'\n";
379 $s .= " linux $prefix/$bn $args\n";
380 if (defined $entry{initrd})
382 $bn = (reverse split(/\/+/, $entry{initrd}{cmdline}))[0];
383 my $c = $entry{initrd}{cmdline};
384 $c =~ s/^\S*(\/[^\/]+\s*)/$1/;
385 $s .= " initrd $prefix$c\n";
390 $s .= " echo Loading '$prefix/$bn $prefix/$bn $args'\n";
391 $s .= " multiboot $prefix/$bn $prefix/$bn $args\n";
392 foreach my $m (@{$entry{mods}})
395 $bn = (reverse split(/\/+/, $m->{command}))[0];
396 my $c = $m->{cmdline};
397 $c =~ s/^\S*(\/[^\/]+\s*)/$1/;
398 $s .= " echo Loading '$prefix/$bn $c'\n";
399 $s .= " module $prefix/$bn $c\n";
402 $s .= " echo Done, booting...\n";