3 # Set PXELINUX hard-coded options
6 use Socket
; # For gethostbyname
11 6 => 'domain-name-servers',
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
);
37 my($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($s);
39 return ($addrtype == AF_INET
) ?
$addrs[0] : undef;
50 foreach $s (split(/,/, $l)) {
51 my($name,$aliases,$addrtype,$length,@addrs)
53 if ($addrtype == AF_INET
) {
54 foreach $addr (@addrs) {
60 return $d ne '' ?
$d : undef;
73 for ($i = 0; $i < length($l); $i += 4) {
74 push(@h, inet_ntoa
(substr($l, $i, 4)));
92 for ($i = 0; $i < length($s); $i++) {
93 $c = substr($s, $i, 1);
94 if ($c eq "\'" || $c eq '!') {
109 if ($s =~ /^[0-9]+$/) {
110 return pack("N", $s);
120 if (length($l) == 4) {
121 return unpack("N", $l);
131 if ($s =~ /^[0-9a-f]{1,2}(:[0-9a-f]{1,2})*$/) {
135 foreach $h (split(/\:/, $s)) {
139 return pack("C", @b);
151 for ($i = 0; $i < length($l); $i++) {
152 push(@h, sprintf("%02x", unpack("C", substr($l, $i, $1))));
155 return join(':', @h);
163 if (defined($option_format{$opt})) {
164 $v = $option_format{$opt}[1]($arg);
165 return $v if (defined($v));
168 return parse_generic
($arg);
176 if (defined($option_format{$opt})) {
177 $v = $option_format{$opt}[2]($arg);
178 return $v if (defined($v));
181 return show_generic
($arg);
188 if (defined($option_rnames{$n})) {
189 return $option_rnames{$n};
190 } elsif ($n =~ /^[0-9]+$/ && $n >= 1 && $n <= 254) {
200 my $data, $bdata, $adata;
201 my $patch_start = (stat($file))[7];
202 my $hdroffset = 0; # 0 means non-deep-embedded
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
216 my $magic = pack("VVVV", 0x2a171ead, 0x0600e65e,
217 0x4025a4e4, 0x42388fc8);
220 return undef unless (read($file, $data, 16) == 16);
221 last if ($data eq $magic);
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;
233 $hdr{'bufsize'} = $bufsize;
234 $hdr{'hdroffset'} = $hdroffset;
236 # It is a BIOS PXE file
238 return undef if ($magic != 0x2983c8ac);
239 return undef if ($len < 7*4);
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
);
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) = @_;
271 my $patch_start = $hdr->{'patch_start'};
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);
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);
314 sub delete_option
($$)
316 my ($num, $block) = @_;
320 while ($x < length($block)) {
321 ($o, $l) = unpack("CC", substr($block, $x, 2));
324 substr($block, $x, $l+2) = '';
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);
333 # Skip to the next option
343 my ($num, $data, $block) = @_;
345 $block = delete_option
($num, $block);
347 if (length($data) == 0) {
349 } elsif (length($data) > 255) {
350 die "$0: option $num has too much data (max 255 bytes)\n";
352 return $block . pack("CC", $num, length($data)) . $data;
358 my($pfx, $data) = @_;
361 while ($x < length($data)) {
362 ($o, $l) = unpack("CC", substr($data, $x, 2));
366 } elsif ($o == 255) {
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);
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";
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];
404 foreach $opt (keys(%option_names)) {
405 $option_rnames{$option_names{$opt}} = $opt;
417 while (defined($opt = shift(@ARGV))) {
419 if (defined($file)) {
424 } elsif ($opt eq '-b' || $opt eq '--before') {
425 $oname = shift(@ARGV);
426 $odata = shift(@ARGV);
428 if (!defined($odata)) {
433 $onum = option_number
($oname);
434 if (!defined($onum)) {
435 print STDERR
"$0: unknown option name: $oname\n";
440 $odata = parse_option
($onum, $odata);
441 if (!defined($odata)) {
442 print STDERR
"$0: unable to parse data for option $oname\n";
447 delete $after{$onum};
448 $before{$onum} = $odata;
450 } elsif ($opt eq '-a' || $opt eq '--after') {
451 $oname = shift(@ARGV);
452 $odata = shift(@ARGV);
454 if (!defined($odata)) {
459 $onum = option_number
($oname);
460 if (!defined($onum)) {
461 print STDERR
"$0: unknown option name: $oname\n";
466 $odata = parse_option
($onum, $odata);
467 if (!defined($odata)) {
468 print STDERR
"$0: unable to parse data for option $oname\n";
473 delete $before{$onum};
474 $after{$onum} = $odata;
476 } elsif ($opt eq '-d' || $opt eq '--delete') {
477 $oname = shift(@ARGV);
479 if (!defined($oname)) {
484 $onum = option_number
($oname);
485 if (!defined($onum)) {
486 print STDERR
"$0: unknown option name: $oname\n";
492 delete $before{$onum};
493 delete $after{$onum};
494 } elsif ($opt eq '-n' || $opt eq '--no-write' || $opt eq '--dry-run') {
496 } elsif ($opt eq '-l' || $opt eq '--list') {
498 } elsif ($opt eq '-h' || $opt eq '--help') {
501 print STDERR
"Invalid option: $opt\n";
506 if (!defined($file) && !$usage) {
512 if ($err || $usage) {
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]);
541 list_options
('-b ', $data[0]);
542 list_options
('-a ', $data[1]);
546 if (!write_optsets
(\
*FILE
, $hdrinfo, @data)) {
547 die "$0: $file: failed to write options: $!\n";