pxe: Add support for embedded options in EFI
[syslinux.git] / utils / pxelinux-options
blobdfd98cf12b3f2979d042d39f2353b50e6e27749f
1 #!/usr/bin/perl
3 # Set PXELINUX hard-coded options
6 use Socket; # For gethostbyname
7 use Fcntl;
8 use bytes;
10 %option_names = (
11 6 => 'domain-name-servers',
12 15 => 'domain-name',
13 54 => 'next-server',
14 209 => 'config-file',
15 210 => 'path-prefix',
16 211 => 'reboottime'
19 @fmt_oneip = ("ip-address", \&parse_oneip, \&show_ip);
20 @fmt_multiip = ("ip-address-list", \&parse_multiip, \&show_ip);
21 @fmt_string = ("string", \&parse_string, \&show_string);
22 @fmt_uint32 = ("uint32", \&parse_uint32, \&show_uint32);
24 %option_format = (
25 6 => \@fmt_multiip,
26 15 => \@fmt_string,
27 54 => \@fmt_oneip,
28 67 => \@fmt_string,
29 209 => \@fmt_string,
30 210 => \@fmt_string,
31 211 => \@fmt_uint32
34 sub parse_oneip($)
36 my($s) = @_;
37 my($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($s);
39 return ($addrtype == AF_INET) ? $addrs[0] : undef;
42 sub parse_multiip($)
44 my($l) = @_;
45 my $s;
46 my @a = ();
47 my $addr;
48 my $d = '';
50 foreach $s (split(/,/, $l)) {
51 my($name,$aliases,$addrtype,$length,@addrs)
52 = gethostbyname($s);
53 if ($addrtype == AF_INET) {
54 foreach $addr (@addrs) {
55 $d .= $addr;
60 return $d ne '' ? $d : undef;
63 sub show_ip($)
65 my($l) = @_;
67 if (length($l) & 3) {
68 return undef;
69 } else {
70 my @h = ();
71 my $i;
73 for ($i = 0; $i < length($l); $i += 4) {
74 push(@h, inet_ntoa(substr($l, $i, 4)));
77 return join(',', @h);
81 sub parse_string($)
83 return $_[0];
86 sub show_string($)
88 my($s) = @_;
89 my $o, $i, $c;
91 $o = "\'";
92 for ($i = 0; $i < length($s); $i++) {
93 $c = substr($s, $i, 1);
94 if ($c eq "\'" || $c eq '!') {
95 $o .= "\'\\$c\'";
96 } else {
97 $o .= $c;
100 $o .= "\'";
102 return $o;
105 sub parse_uint32($)
107 my($s) = @_;
109 if ($s =~ /^[0-9]+$/) {
110 return pack("N", $s);
111 } else {
112 return undef;
116 sub show_uint32($)
118 my($l) = @_;
120 if (length($l) == 4) {
121 return unpack("N", $l);
122 } else {
123 return undef;
127 sub parse_generic($)
129 my($s) = @_;
131 if ($s =~ /^[0-9a-f]{1,2}(:[0-9a-f]{1,2})*$/) {
132 my $h;
133 my @b = ();
135 foreach $h (split(/\:/, $s)) {
136 push(@b, hex $h);
139 return pack("C", @b);
140 } else {
141 return undef;
145 sub show_generic($)
147 my($l) = @_;
148 my $i;
149 my @h;
151 for ($i = 0; $i < length($l); $i++) {
152 push(@h, sprintf("%02x", unpack("C", substr($l, $i, $1))));
155 return join(':', @h);
158 sub parse_option($$)
160 my($opt, $arg) = @_;
161 my $v;
163 if (defined($option_format{$opt})) {
164 $v = $option_format{$opt}[1]($arg);
165 return $v if (defined($v));
168 return parse_generic($arg);
171 sub show_option($$)
173 my($opt, $arg) = @_;
174 my $v;
176 if (defined($option_format{$opt})) {
177 $v = $option_format{$opt}[2]($arg);
178 return $v if (defined($v));
181 return show_generic($arg);
184 sub option_number($)
186 my($n) = @_;
188 if (defined($option_rnames{$n})) {
189 return $option_rnames{$n};
190 } elsif ($n =~ /^[0-9]+$/ && $n >= 1 && $n <= 254) {
191 return $n+0;
192 } else {
193 return undef;
197 sub read_optsets($)
199 my($file) = @_;
200 my $data, $bdata, $adata;
201 my $patch_start = (stat($file))[7];
202 my $hdroffset = 0; # 0 means non-deep-embedded
203 my $bufsize = 0;
204 my $junk;
205 my %hdr;
207 return undef unless (seek($file, 0, SEEK_SET));
208 return undef unless (read($file, $data, 48) == 48);
210 my($mzmagic, $junk, $magic, $len, $flags, $boff, $blen, $aoff, $alen)
211 = unpack("va[6]VVVVVVV", $data);
213 if ($mzmagic == 0x5a4d) {
214 # It is an EFI file... search for the magic number
215 $hdroffset = 48;
216 my $magic = pack("VVVV", 0x2a171ead, 0x0600e65e,
217 0x4025a4e4, 0x42388fc8);
219 while (1) {
220 return undef unless (read($file, $data, 16) == 16);
221 last if ($data eq $magic);
223 $hdroffset += 16;
226 return undef unless (read($file, $data, 16) == 16);
227 ($blen, $alen, $bufsize, $junk) = unpack("VVVV", $data);
229 printf STDERR "EFI: offset = 0x%x, blen = %d, alen = %d, bufsize = %d\n", $hdroffset, $blen, $alen, $bufsize;
231 $patch_start = $boff = $hdroffset + 32;
232 $aoff = $boff + $blen;
234 $hdr{'deep'} = 1;
235 $hdr{'bufsize'} = $bufsize;
236 $hdr{'hdroffset'} = $hdroffset;
237 } else {
238 # It is a BIOS PXE file
240 return undef if ($magic != 0x2983c8ac);
241 return undef if ($len < 7*4);
243 $hdr{'deep'} = 0;
246 if ($blen == 0) {
247 $bdata = '';
248 } else {
249 return undef unless (seek($file, $boff, SEEK_SET));
250 return undef unless (read($file, $bdata, $blen) == $blen);
251 $patch_start = $boff if ($boff < patch_start);
254 if ($alen == 0) {
255 $adata = '';
256 } else {
257 return undef unless (seek($file, $aoff, SEEK_SET));
258 return undef unless (read($file, $adata, $alen) == $alen);
259 $patch_start = $aoff if ($aoff < $patch_start);
262 $hdr{'patch_start'} = $patch_start;
264 return (\%hdr, $bdata, $adata);
267 sub write_optsets($$$@)
269 my($file, $hdr, $bdata, $adata) = @_;
270 my $boff = 0;
271 my $aoff = 0;
272 my $bufsize = 0;
273 my $patch_start = $hdr->{'patch_start'};
274 my $len;
276 $bdata .= "\xff" unless ($bdata eq '');
277 $adata .= "\xff" unless ($adata eq '');
279 $len = length($bdata) + length($adata);
281 if (defined($hdr->{'bufsize'})) {
282 return undef unless ($len <= $hdr->{'bufsize'});
285 return undef unless (seek($file, $patch_start, SEEK_SET));
287 if (length($bdata)) {
288 $boff = $patch_start;
289 return undef unless (print $file $bdata);
290 $patch_start += length($bdata);
293 if (length($adata)) {
294 $aoff = $patch_start;
295 return undef unless (print $file $adata);
296 $patch_start += length($adata);
299 if ($hdr->{'deep'}) {
300 return undef unless (print $file "\0" x ($hdr->{'bufsize'} - $len));
301 return undef unless (seek($file, $hdr->{'hdroffset'} + 16, SEEK_SET));
302 my $hdr = pack("VV", length($bdata), length($adata));
303 return undef unless (print $file $hdr);
304 } else {
305 my $hdr = pack("VVVV", $boff, length($bdata), $aoff, length($adata));
307 return undef unless (seek($file, 8+3*4, SEEK_SET));
308 return undef unless (print $file $hdr);
310 truncate($file, $patch_start);
313 return 1;
316 sub delete_option($$)
318 my ($num, $block) = @_;
319 my $o, $l, $c, $x;
321 $x = 0;
322 while ($x < length($block)) {
323 ($o, $l) = unpack("CC", substr($block, $x, 2));
324 if ($o == $num) {
325 # Delete this option
326 substr($block, $x, $l+2) = '';
327 } elsif ($o == 0) {
328 # Delete a null option
329 substr($block, $x, 1) = '';
330 } elsif ($o == 255) {
331 # End marker - truncate block
332 $block = substr($block, 0, $x);
333 last;
334 } else {
335 # Skip to the next option
336 $x += $l+2;
340 return $block;
343 sub add_option($$$)
345 my ($num, $data, $block) = @_;
347 $block = delete_option($num, $block);
349 if (length($data) == 0) {
350 return $block;
351 } elsif (length($data) > 255) {
352 die "$0: option $num has too much data (max 255 bytes)\n";
353 } else {
354 return $block . pack("CC", $num, length($data)) . $data;
358 sub list_options($$)
360 my($pfx, $data) = @_;
361 my $x, $o, $l;
363 while ($x < length($data)) {
364 ($o, $l) = unpack("CC", substr($data, $x, 2));
366 if ($o == 0) {
367 $x++;
368 } elsif ($o == 255) {
369 last;
370 } else {
371 my $odata = substr($data, $x+2, $l);
372 last if (length($odata) != $l); # Incomplete option
374 printf "%s%-20s %s\n", $pfx,
375 $option_names{$o} || sprintf("%d", $o),
376 show_option($o, $odata);
378 $x += $l+2;
383 sub usage()
385 my $i;
387 print STDERR "Usage: $0 options pxelinux.0\n";
388 print STDERR "Options:\n";
389 print STDERR "--before option value -b Add an option before DHCP data\n";
390 print STDERR "--after option value -a Add an option after DHCP data\n";
391 print STDERR "--delete option -d Delete an option\n";
392 print STDERR "--list -l List set options\n";
393 print STDERR "--dry-run -n Don't modify the target file\n";
394 print STDERR "--help -h Display this help text\n";
395 print STDERR "\n";
396 print STDERR "The following DHCP options are currently recognized:\n";
397 printf STDERR "%-23s %-3s %s\n", 'Name', 'Num', 'Value Format';
399 foreach $i (sort { $a <=> $b } keys(%option_names)) {
400 printf STDERR "%-23s %3d %s\n",
401 $option_names{$i}, $i, $option_format{$i}[0];
405 %option_rnames = ();
406 foreach $opt (keys(%option_names)) {
407 $option_rnames{$option_names{$opt}} = $opt;
410 %before = ();
411 %after = ();
412 @clear = ();
413 $usage = 0;
414 $err = 0;
415 $list = 0;
416 $no_write = 0;
417 undef $file;
419 while (defined($opt = shift(@ARGV))) {
420 if ($opt !~ /^-/) {
421 if (defined($file)) {
422 $err = $usage = 1;
423 last;
425 $file = $opt;
426 } elsif ($opt eq '-b' || $opt eq '--before') {
427 $oname = shift(@ARGV);
428 $odata = shift(@ARGV);
430 if (!defined($odata)) {
431 $err = $usage = 1;
432 last;
435 $onum = option_number($oname);
436 if (!defined($onum)) {
437 print STDERR "$0: unknown option name: $oname\n";
438 $err = 1;
439 next;
442 $odata = parse_option($onum, $odata);
443 if (!defined($odata)) {
444 print STDERR "$0: unable to parse data for option $oname\n";
445 $err = 1;
446 next;
449 delete $after{$onum};
450 $before{$onum} = $odata;
451 push(@clear, $onum);
452 } elsif ($opt eq '-a' || $opt eq '--after') {
453 $oname = shift(@ARGV);
454 $odata = shift(@ARGV);
456 if (!defined($odata)) {
457 $err = $usage = 1;
458 last;
461 $onum = option_number($oname);
462 if (!defined($onum)) {
463 print STDERR "$0: unknown option name: $oname\n";
464 $err = 1;
465 next;
468 $odata = parse_option($onum, $odata);
469 if (!defined($odata)) {
470 print STDERR "$0: unable to parse data for option $oname\n";
471 $err = 1;
472 next;
475 delete $before{$onum};
476 $after{$onum} = $odata;
477 push(@clear, $onum);
478 } elsif ($opt eq '-d' || $opt eq '--delete') {
479 $oname = shift(@ARGV);
481 if (!defined($oname)) {
482 $err = $usage = 1;
483 last;
486 $onum = option_number($oname);
487 if (!defined($onum)) {
488 print STDERR "$0: unknown option name: $oname\n";
489 $err = 1;
490 next;
493 push(@clear, $onum);
494 delete $before{$onum};
495 delete $after{$onum};
496 } elsif ($opt eq '-n' || $opt eq '--no-write' || $opt eq '--dry-run') {
497 $no_write = 1;
498 } elsif ($opt eq '-l' || $opt eq '--list') {
499 $list = 1;
500 } elsif ($opt eq '-h' || $opt eq '--help') {
501 $usage = 1;
502 } else {
503 print STDERR "Invalid option: $opt\n";
504 $err = $usage = 1;
508 if (!defined($file) && !$usage) {
509 $err = $usage = 1;
511 if ($usage) {
512 usage();
514 if ($err || $usage) {
515 exit($err);
518 if (!scalar(@clear)) {
519 $no_write = 1; # No modifications requested
522 $mode = $no_write ? '<' : '+<';
524 open(FILE, $mode, $file)
525 or die "$0: cannot open: $file: $!\n";
526 ($hdrinfo, @data) = read_optsets(\*FILE);
527 if (!defined($hdrinfo)) {
528 die "$0: $file: patch block not found or file corrupt\n";
531 foreach $o (@clear) {
532 $data[0] = delete_option($o, $data[0]);
533 $data[1] = delete_option($o, $data[1]);
535 foreach $o (keys(%before)) {
536 $data[0] = add_option($o, $before{$o}, $data[0]);
538 foreach $o (keys(%after)) {
539 $data[1] = add_option($o, $after{$o}, $data[1]);
542 if ($list) {
543 list_options('-b ', $data[0]);
544 list_options('-a ', $data[1]);
547 if (!$no_write) {
548 if (!write_optsets(\*FILE, $hdrinfo, @data)) {
549 die "$0: $file: failed to write options: $!\n";
553 close(FILE);
554 exit 0;