| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Helper script for debian-cd EFI CD creation |
| 4 |
# |
| 5 |
# Parse the Isolinux boot menus and create matching grub menus and submenus |
| 6 |
# |
| 7 |
# Complicated by the way grub theming works - we need to create a |
| 8 |
# separate grub theme per submenu simply so that we can describe the |
| 9 |
# current (sub)menu appropriately. |
| 10 |
|
| 11 |
use strict; |
| 12 |
use File::Path qw(make_path); |
| 13 |
|
| 14 |
my $isolinuxdir = shift or die "Need to know where the isolinux directory is!\n"; |
| 15 |
my $outdir = shift or die "Need to know where to write output!\n"; |
| 16 |
my $grub_theme = shift or die "Need input file location for base grub theme!\n"; |
| 17 |
my $tl_distro= shift or die "Need a top-level distro name (e.g. Debian)\n"; |
| 18 |
my $tl_kernel= shift or die "Need a top-level kernel (e.g. GNU/Linux)\n"; |
| 19 |
my $tl_version= shift or die "Need a top-level version (e.g. 7.0)\n"; |
| 20 |
my $theme_dir = "$outdir/boot/grub/theme"; |
| 21 |
my @cpp_and_opts = ('cpp', |
| 22 |
'-traditional', |
| 23 |
'-undef', |
| 24 |
'-P', |
| 25 |
'-C', |
| 26 |
'-Wall', |
| 27 |
'-nostdinc'); |
| 28 |
|
| 29 |
my @lines; |
| 30 |
my @menus; |
| 31 |
my $incdepth = 0; |
| 32 |
my @menu_number = (1,0,0,0,0); |
| 33 |
my @menu_title = ('', '', '', '', ''); |
| 34 |
my $menudepth = 0; |
| 35 |
my $pre = ""; |
| 36 |
my $in_ifcpu = 0; |
| 37 |
my $amd64_label = ""; |
| 38 |
my %menu; |
| 39 |
|
| 40 |
sub parse_file { |
| 41 |
my $file = shift; |
| 42 |
|
| 43 |
$incdepth++; |
| 44 |
# for(my $i = 0; $i < $incdepth ; $i++) { |
| 45 |
# print STDERR " "; |
| 46 |
# } |
| 47 |
# print STDERR "parsing $isolinuxdir/$file\n"; |
| 48 |
open(my $fh, "< $isolinuxdir/$file") or return; |
| 49 |
while (my $line = <$fh>) { |
| 50 |
chomp $line; |
| 51 |
if ($line =~ /^\s*include\ (.*\.cfg)/) { |
| 52 |
parse_file($1); |
| 53 |
} elsif ($line =~ /label archdetect/) { |
| 54 |
# We don't have support for arch detection in Grub, so only |
| 55 |
# show the amd64 options |
| 56 |
$in_ifcpu = 1; |
| 57 |
} elsif ($line =~ /kernel (.*c32)/) { |
| 58 |
# This tells us that the matching "append" data will have |
| 59 |
# the label we need to look for. Ugh! |
| 60 |
if ($1 =~ /ifcpu64/) { |
| 61 |
$in_ifcpu = 2; |
| 62 |
} |
| 63 |
} elsif ($line =~ /^\s*label (.*)/ && $1 =~ /$amd64_label/) { |
| 64 |
$in_ifcpu = 3; |
| 65 |
} elsif ($line =~ /^\s*append\ (.*\.cfg)/) { |
| 66 |
if ($in_ifcpu == 3) { |
| 67 |
parse_file($1); |
| 68 |
$in_ifcpu = 1; |
| 69 |
} else { |
| 70 |
push(@lines, $line); |
| 71 |
} |
| 72 |
} elsif ($line =~ /append (.*)$/ && $in_ifcpu == 2) { |
| 73 |
# Parse out the first entry - that's what we want to use |
| 74 |
# as a label elsewhere. Ugh! |
| 75 |
my @list = split(/ /, $1); |
| 76 |
$amd64_label = $list[0]; |
| 77 |
$in_ifcpu = 1; |
| 78 |
} elsif ($line =~ /default archdetect/) { |
| 79 |
$in_ifcpu = 0; |
| 80 |
} else { |
| 81 |
push(@lines, $line); |
| 82 |
} |
| 83 |
} |
| 84 |
close $fh; |
| 85 |
$incdepth--; |
| 86 |
} |
| 87 |
|
| 88 |
sub print_indent { |
| 89 |
my $text = shift; |
| 90 |
my $i = 1; |
| 91 |
while ($i++ < $menudepth) { |
| 92 |
print " "; |
| 93 |
} |
| 94 |
print $text; |
| 95 |
} |
| 96 |
|
| 97 |
sub print_kernel { |
| 98 |
my $t = shift; |
| 99 |
my %k = %{$t}; |
| 100 |
my $initrd; |
| 101 |
my $hotkey = ""; |
| 102 |
|
| 103 |
if ($k{"label"} =~ m,\^(\S),) { |
| 104 |
$hotkey = lc "--hotkey=$1 "; |
| 105 |
$k{"label"} =~ s/\^//; |
| 106 |
} |
| 107 |
if ($k{"append"} =~ s? (initrd=\S+)??) { |
| 108 |
$initrd = $1; |
| 109 |
$initrd =~ s?^.*initrd=??; |
| 110 |
} |
| 111 |
print_indent "menuentry $hotkey'$pre" . $k{"label"} . "' {\n"; |
| 112 |
print_indent " set background_color=black\n"; |
| 113 |
print_indent " linux " . $k{"kernel"} . " " . $k{"append"} . "\n"; |
| 114 |
print_indent " initrd $initrd\n"; |
| 115 |
print_indent "}\n"; |
| 116 |
} |
| 117 |
|
| 118 |
sub debug { |
| 119 |
# print_indent $menudepth . " " . $menu{"number"} . ": " . $menu{"label"} . |
| 120 |
# " '" . $menu{"title"} . "'\n"; |
| 121 |
} |
| 122 |
|
| 123 |
sub create_theme_file { |
| 124 |
my $filename = shift; |
| 125 |
my @args; |
| 126 |
push(@args, @cpp_and_opts); |
| 127 |
push(@args, "-DTITLE=\"$tl_distro $tl_kernel $tl_version\""); |
| 128 |
for (my $i = 0; $i < $menudepth; $i++) { |
| 129 |
push(@args, "-DMENU$i=\"" . $menu_title[$i] . "\""); |
| 130 |
} |
| 131 |
push(@args, "$grub_theme"); |
| 132 |
open(IN, "-|", @args) or die "Can't open cpp input: $!\n"; |
| 133 |
open(OUT, ">", "$theme_dir/$filename") |
| 134 |
or die "Can't create theme file $theme_dir/$filename: $!\n"; |
| 135 |
while (<IN>) { |
| 136 |
print OUT "$_"; |
| 137 |
} |
| 138 |
close(IN); |
| 139 |
close(OUT); |
| 140 |
open(IN, "<", "$theme_dir/$filename"); |
| 141 |
open(OUT, ">", "$theme_dir/dark-$filename"); |
| 142 |
while (<IN>) { |
| 143 |
next if ($_ =~ /^desktop-image:/); |
| 144 |
print OUT "$_"; |
| 145 |
} |
| 146 |
close(IN); |
| 147 |
close(OUT); |
| 148 |
} |
| 149 |
|
| 150 |
make_path($theme_dir); |
| 151 |
if (! -d $theme_dir) { |
| 152 |
die "Can't make theme dir $theme_dir: $!\n"; |
| 153 |
} |
| 154 |
parse_file("isolinux.cfg"); |
| 155 |
|
| 156 |
$menu{"number"} = "1"; |
| 157 |
$menu{"label"} = "top"; |
| 158 |
$menu{"title"} = "$tl_distro $tl_kernel UEFI Installer menu"; |
| 159 |
$menu_title[$menudepth] = $menu{"title"}; |
| 160 |
|
| 161 |
my %kernel; |
| 162 |
my $in_kernel = 0; |
| 163 |
my $in_dark = 0; |
| 164 |
my $new_menu = 0; |
| 165 |
|
| 166 |
$menudepth++; |
| 167 |
debug(); |
| 168 |
print_indent "set theme=/boot/grub/theme/" . $menu{"number"} . "\n"; |
| 169 |
create_theme_file($menu{"number"}); |
| 170 |
|
| 171 |
foreach my $line(@lines) { |
| 172 |
if ($line =~ /^\s*menu begin\ (\S+)/) { |
| 173 |
$menu_number[$menudepth]++; |
| 174 |
$new_menu = 1; |
| 175 |
my $mn_string = ""; |
| 176 |
for(my $i = 0; $i <= $menudepth ; $i++) { |
| 177 |
$mn_string .= "$menu_number[$i]"; |
| 178 |
if ($i < $menudepth) { |
| 179 |
$mn_string .= "-"; |
| 180 |
} |
| 181 |
} |
| 182 |
$menu{"number"} = $mn_string; |
| 183 |
$menu{"label"} = $1; |
| 184 |
if ($menu{"label"} eq "dark") { |
| 185 |
$in_dark = 1; |
| 186 |
} |
| 187 |
if ($in_kernel) { |
| 188 |
print_kernel(\%kernel); |
| 189 |
undef %kernel; |
| 190 |
$in_kernel = 0; |
| 191 |
} |
| 192 |
} elsif ($line =~ /^\s*menu end/) { |
| 193 |
if ($in_kernel) { |
| 194 |
print_kernel(\%kernel); |
| 195 |
undef %kernel; |
| 196 |
$in_kernel = 0; |
| 197 |
} |
| 198 |
$menu_number[$menudepth] = 0; |
| 199 |
$menudepth--; |
| 200 |
if ($menudepth) { |
| 201 |
print_indent "}\n"; |
| 202 |
} |
| 203 |
if ($menudepth > 1) { |
| 204 |
$pre = "... "; |
| 205 |
} else { |
| 206 |
$pre = ""; |
| 207 |
$in_dark = 0; |
| 208 |
} |
| 209 |
} elsif ($line =~ /^\s*menu title (.*$)/) { |
| 210 |
if ($in_kernel) { |
| 211 |
print_kernel(\%kernel); |
| 212 |
undef %kernel; |
| 213 |
$in_kernel = 0; |
| 214 |
} |
| 215 |
$menu{"title"} = $1; |
| 216 |
if ($new_menu) { |
| 217 |
my $hotkey = ""; |
| 218 |
if ($menu{"label"} =~ m,\^(\S),) { |
| 219 |
$hotkey = lc "--hotkey=$1 "; |
| 220 |
$menu{"label"} =~ s/\^//; |
| 221 |
} |
| 222 |
print_indent "submenu $hotkey'$pre" . $menu{"label"} . " ...' {\n"; |
| 223 |
$menu_title[$menudepth] = $menu{"title"}; |
| 224 |
$menudepth++; |
| 225 |
if ($menudepth > 1) { |
| 226 |
$pre = "... "; |
| 227 |
} else { |
| 228 |
$pre = ""; |
| 229 |
} |
| 230 |
debug(); |
| 231 |
if ($in_dark) { |
| 232 |
print_indent "set menu_color_normal=white/black\n"; |
| 233 |
print_indent "set menu_color_highlight=yellow/black\n"; |
| 234 |
print_indent "set color_normal=white/black\n"; |
| 235 |
print_indent "set color_highlight=yellow/black\n"; |
| 236 |
print_indent "background_image\n"; |
| 237 |
print_indent "set theme=/boot/grub/theme/dark-" . $menu{"number"} . "\n"; |
| 238 |
} else { |
| 239 |
print_indent "set menu_color_normal=cyan/blue\n"; |
| 240 |
print_indent "set menu_color_highlight=white/blue\n"; |
| 241 |
print_indent "set theme=/boot/grub/theme/" . $menu{"number"} . "\n"; |
| 242 |
} |
| 243 |
print_indent "set gfxpayload=keep\n"; |
| 244 |
create_theme_file($menu{"number"}); |
| 245 |
$new_menu = 0; |
| 246 |
} |
| 247 |
} elsif ($line =~ /^[ \t]*label (\S*(rescue|install|auto|expert)\S*)/) { |
| 248 |
if ($in_kernel) { |
| 249 |
print_kernel(\%kernel); |
| 250 |
undef %kernel; |
| 251 |
} |
| 252 |
$kernel{"ref"} = $1; |
| 253 |
$in_kernel = 1; |
| 254 |
} elsif ($line =~ /menu label (.*)$/ && $in_kernel) { |
| 255 |
$kernel{"label"} = $1; |
| 256 |
} elsif ($line =~ /menu label (.*)$/) { |
| 257 |
$menu{"label"} = $1; |
| 258 |
} elsif ($line =~ /menu default/ && $in_kernel) { |
| 259 |
$kernel{"default"} = 1; |
| 260 |
} elsif ($line =~ /kernel (.*)$/ && $in_kernel) { |
| 261 |
$kernel{"kernel"} = $1; |
| 262 |
} elsif ($line =~ /append (.*)$/ && $in_kernel) { |
| 263 |
$kernel{"append"} = $1; |
| 264 |
} else { |
| 265 |
#print "$line\n"; |
| 266 |
} |
| 267 |
} |
| 268 |
if ($in_kernel) { |
| 269 |
print_kernel(\%kernel); |
| 270 |
undef %kernel; |
| 271 |
} |