Makefile.private: correct the names of prerelease tarballs
[syslinux/sherbszt.git] / utils / pxelinux-options
blob073eee572bd317de73ec4d8554f035d100030839
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 $patch_start = $boff = $hdroffset + 32;
230 $aoff = $boff + $blen;
232 $hdr{'deep'} = 1;
233 $hdr{'bufsize'} = $bufsize;
234 $hdr{'hdroffset'} = $hdroffset;
235 } else {
236 # It is a BIOS PXE file
238 return undef if ($magic != 0x2983c8ac);
239 return undef if ($len < 7*4);
241 $hdr{'deep'} = 0;
244 if ($blen == 0) {
245 $bdata = '';
246 } else {
247 return undef unless (seek($file, $boff, SEEK_SET));
248 return undef unless (read($file, $bdata, $blen) == $blen);
249 $patch_start = $boff if ($boff < patch_start);
252 if ($alen == 0) {
253 $adata = '';
254 } else {
255 return undef unless (seek($file, $aoff, SEEK_SET));
256 return undef unless (read($file, $adata, $alen) == $alen);
257 $patch_start = $aoff if ($aoff < $patch_start);
260 $hdr{'patch_start'} = $patch_start;
262 return (\%hdr, $bdata, $adata);
265 sub write_optsets($$@)
267 my($file, $hdr, $bdata, $adata) = @_;
268 my $boff = 0;
269 my $aoff = 0;
270 my $bufsize = 0;
271 my $patch_start = $hdr->{'patch_start'};
272 my $len;
274 $bdata .= "\xff" unless ($bdata eq '');
275 $adata .= "\xff" unless ($adata eq '');
277 $len = length($bdata) + length($adata);
279 if (defined($hdr->{'bufsize'})) {
280 return undef unless ($len <= $hdr->{'bufsize'});
283 return undef unless (seek($file, $patch_start, SEEK_SET));
285 if (length($bdata)) {
286 $boff = $patch_start;
287 return undef unless (print $file $bdata);
288 $patch_start += length($bdata);
291 if (length($adata)) {
292 $aoff = $patch_start;
293 return undef unless (print $file $adata);
294 $patch_start += length($adata);
297 if ($hdr->{'deep'}) {
298 return undef unless (print $file "\0" x ($hdr->{'bufsize'} - $len));
299 return undef unless (seek($file, $hdr->{'hdroffset'} + 16, SEEK_SET));
300 my $hdr = pack("VV", length($bdata), length($adata));
301 return undef unless (print $file $hdr);
302 } else {
303 my $hdr = pack("VVVV", $boff, length($bdata), $aoff, length($adata));
305 return undef unless (seek($file, 8+3*4, SEEK_SET));
306 return undef unless (print $file $hdr);
308 truncate($file, $patch_start);
311 return 1;
314 sub delete_option($$)
316 my ($num, $block) = @_;
317 my $o, $l, $c, $x;
319 $x = 0;
320 while ($x < length($block)) {
321 ($o, $l) = unpack("CC", substr($block, $x, 2));
322 if ($o == $num) {
323 # Delete this option
324 substr($block, $x, $l+2) = '';
325 } elsif ($o == 0) {
326 # Delete a null option
327 substr($block, $x, 1) = '';
328 } elsif ($o == 255) {
329 # End marker - truncate block
330 $block = substr($block, 0, $x);
331 last;
332 } else {
333 # Skip to the next option
334 $x += $l+2;
338 return $block;
341 sub add_option($$$)
343 my ($num, $data, $block) = @_;
345 $block = delete_option($num, $block);
347 if (length($data) == 0) {
348 return $block;
349 } elsif (length($data) > 255) {
350 die "$0: option $num has too much data (max 255 bytes)\n";
351 } else {
352 return $block . pack("CC", $num, length($data)) . $data;
356 sub list_options($$)
358 my($pfx, $data) = @_;
359 my $x, $o, $l;
361 while ($x < length($data)) {
362 ($o, $l) = unpack("CC", substr($data, $x, 2));
364 if ($o == 0) {
365 $x++;
366 } elsif ($o == 255) {
367 last;
368 } else {
369 my $odata = substr($data, $x+2, $l);
370 last if (length($odata) != $l); # Incomplete option
372 printf "%s%-20s %s\n", $pfx,
373 $option_names{$o} || sprintf("%d", $o),
374 show_option($o, $odata);
376 $x += $l+2;
381 sub usage()
383 my $i;
385 print STDERR "Usage: $0 options pxelinux.0\n";
386 print STDERR "Options:\n";
387 print STDERR "--before option value -b Add an option before DHCP data\n";
388 print STDERR "--after option value -a Add an option after DHCP data\n";
389 print STDERR "--delete option -d Delete an option\n";
390 print STDERR "--list -l List set options\n";
391 print STDERR "--dry-run -n Don't modify the target file\n";
392 print STDERR "--help -h Display this help text\n";
393 print STDERR "\n";
394 print STDERR "The following DHCP options are currently recognized:\n";
395 printf STDERR "%-23s %-3s %s\n", 'Name', 'Num', 'Value Format';
397 foreach $i (sort { $a <=> $b } keys(%option_names)) {
398 printf STDERR "%-23s %3d %s\n",
399 $option_names{$i}, $i, $option_format{$i}[0];
403 %option_rnames = ();
404 foreach $opt (keys(%option_names)) {
405 $option_rnames{$option_names{$opt}} = $opt;
408 %before = ();
409 %after = ();
410 @clear = ();
411 $usage = 0;
412 $err = 0;
413 $list = 0;
414 $no_write = 0;
415 undef $file;
417 while (defined($opt = shift(@ARGV))) {
418 if ($opt !~ /^-/) {
419 if (defined($file)) {
420 $err = $usage = 1;
421 last;
423 $file = $opt;
424 } elsif ($opt eq '-b' || $opt eq '--before') {
425 $oname = shift(@ARGV);
426 $odata = shift(@ARGV);
428 if (!defined($odata)) {
429 $err = $usage = 1;
430 last;
433 $onum = option_number($oname);
434 if (!defined($onum)) {
435 print STDERR "$0: unknown option name: $oname\n";
436 $err = 1;
437 next;
440 $odata = parse_option($onum, $odata);
441 if (!defined($odata)) {
442 print STDERR "$0: unable to parse data for option $oname\n";
443 $err = 1;
444 next;
447 delete $after{$onum};
448 $before{$onum} = $odata;
449 push(@clear, $onum);
450 } elsif ($opt eq '-a' || $opt eq '--after') {
451 $oname = shift(@ARGV);
452 $odata = shift(@ARGV);
454 if (!defined($odata)) {
455 $err = $usage = 1;
456 last;
459 $onum = option_number($oname);
460 if (!defined($onum)) {
461 print STDERR "$0: unknown option name: $oname\n";
462 $err = 1;
463 next;
466 $odata = parse_option($onum, $odata);
467 if (!defined($odata)) {
468 print STDERR "$0: unable to parse data for option $oname\n";
469 $err = 1;
470 next;
473 delete $before{$onum};
474 $after{$onum} = $odata;
475 push(@clear, $onum);
476 } elsif ($opt eq '-d' || $opt eq '--delete') {
477 $oname = shift(@ARGV);
479 if (!defined($oname)) {
480 $err = $usage = 1;
481 last;
484 $onum = option_number($oname);
485 if (!defined($onum)) {
486 print STDERR "$0: unknown option name: $oname\n";
487 $err = 1;
488 next;
491 push(@clear, $onum);
492 delete $before{$onum};
493 delete $after{$onum};
494 } elsif ($opt eq '-n' || $opt eq '--no-write' || $opt eq '--dry-run') {
495 $no_write = 1;
496 } elsif ($opt eq '-l' || $opt eq '--list') {
497 $list = 1;
498 } elsif ($opt eq '-h' || $opt eq '--help') {
499 $usage = 1;
500 } else {
501 print STDERR "Invalid option: $opt\n";
502 $err = $usage = 1;
506 if (!defined($file) && !$usage) {
507 $err = $usage = 1;
509 if ($usage) {
510 usage();
512 if ($err || $usage) {
513 exit($err);
516 if (!scalar(@clear)) {
517 $no_write = 1; # No modifications requested
520 $mode = $no_write ? '<' : '+<';
522 open(FILE, $mode, $file)
523 or die "$0: cannot open: $file: $!\n";
524 ($hdrinfo, @data) = read_optsets(\*FILE);
525 if (!defined($hdrinfo)) {
526 die "$0: $file: patch block not found or file corrupt\n";
529 foreach $o (@clear) {
530 $data[0] = delete_option($o, $data[0]);
531 $data[1] = delete_option($o, $data[1]);
533 foreach $o (keys(%before)) {
534 $data[0] = add_option($o, $before{$o}, $data[0]);
536 foreach $o (keys(%after)) {
537 $data[1] = add_option($o, $after{$o}, $data[1]);
540 if ($list) {
541 list_options('-b ', $data[0]);
542 list_options('-a ', $data[1]);
545 if (!$no_write) {
546 if (!write_optsets(\*FILE, $hdrinfo, @data)) {
547 die "$0: $file: failed to write options: $!\n";
551 close(FILE);
552 exit 0;