module: Append '/' to PATH strings if missing
[syslinux.git] / utils / pxelinux-options
blobab7075b57c38cda282133ac2402447bbf56e971f
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];
203 return undef unless (seek($file, 8, SEEK_SET));
204 return undef unless (read($file, $data, 7*4) == 7*4);
206 my($magic, $len, $flags, $boff, $blen, $aoff, $alen)
207 = unpack("VVVVVVV", $data);
208 return undef if ($magic != 0x2983c8ac);
209 return undef if ($len < 7*4);
211 if ($blen == 0) {
212 $bdata = '';
213 } else {
214 return undef unless (seek($file, $boff, SEEK_SET));
215 return undef unless (read($file, $bdata, $blen) == $blen);
216 $patch_start = $boff if ($boff < $patch_start);
219 if ($alen == 0) {
220 $adata = '';
221 } else {
222 return undef unless (seek($file, $aoff, SEEK_SET));
223 return undef unless (read($file, $adata, $alen) == $alen);
224 $patch_start = $aoff if ($aoff < $patch_start);
227 return ($patch_start, $bdata, $adata);
230 sub write_optsets($$@)
232 my($file, $patch_start, $bdata, $adata) = @_;
233 my $boff = 0;
234 my $aoff = 0;
236 if (length($bdata) > 0) {
237 $bdata .= "\xff";
238 $boff = $patch_start;
239 return undef unless (seek($file, $boff, SEEK_SET));
240 return undef unless (print $file $bdata);
241 $patch_start += length($bdata);
244 if (length($adata) > 0) {
245 $adata .= "\xff";
246 $aoff = $patch_start;
247 return undef unless (seek($file, $aoff, SEEK_SET));
248 return undef unless (print $file $adata);
249 $patch_start += length($adata);
252 my $hdr = pack("VVVV", $boff, length($bdata), $aoff, length($adata));
254 return undef unless (seek($file, 8+3*4, SEEK_SET));
255 return undef unless (print $file $hdr);
257 truncate($file, $patch_start);
258 return 1;
261 sub delete_option($$)
263 my ($num, $block) = @_;
264 my $o, $l, $c, $x;
266 $x = 0;
267 while ($x < length($block)) {
268 ($o, $l) = unpack("CC", substr($block, $x, 2));
269 if ($o == $num) {
270 # Delete this option
271 substr($block, $x, $l+2) = '';
272 } elsif ($o == 0) {
273 # Delete a null option
274 substr($block, $x, 1) = '';
275 } elsif ($o == 255) {
276 # End marker - truncate block
277 $block = substr($block, 0, $x);
278 last;
279 } else {
280 # Skip to the next option
281 $x += $l+2;
285 return $block;
288 sub add_option($$$)
290 my ($num, $data, $block) = @_;
292 $block = delete_option($num, $block);
294 if (length($data) == 0) {
295 return $block;
296 } elsif (length($data) > 255) {
297 die "$0: option $num has too much data (max 255 bytes)\n";
298 } else {
299 return $block . pack("CC", $num, length($data)) . $data;
303 sub list_options($$)
305 my($pfx, $data) = @_;
306 my $x, $o, $l;
308 while ($x < length($data)) {
309 ($o, $l) = unpack("CC", substr($data, $x, 2));
311 if ($o == 0) {
312 $x++;
313 } elsif ($o == 255) {
314 last;
315 } else {
316 my $odata = substr($data, $x+2, $l);
317 last if (length($odata) != $l); # Incomplete option
319 printf "%s%-20s %s\n", $pfx,
320 $option_names{$o} || sprintf("%d", $o),
321 show_option($o, $odata);
323 $x += $l+2;
328 sub usage()
330 my $i;
332 print STDERR "Usage: $0 options pxelinux.0\n";
333 print STDERR "Options:\n";
334 print STDERR "--before option value -b Add an option before DHCP data\n";
335 print STDERR "--after option value -a Add an option after DHCP data\n";
336 print STDERR "--delete option -d Delete an option\n";
337 print STDERR "--list -l List set options\n";
338 print STDERR "--dry-run -n Don't modify the target file\n";
339 print STDERR "--help -h Display this help text\n";
340 print STDERR "\n";
341 print STDERR "The following DHCP options are currently recognized:\n";
342 printf STDERR "%-23s %-3s %s\n", 'Name', 'Num', 'Value Format';
344 foreach $i (sort { $a <=> $b } keys(%option_names)) {
345 printf STDERR "%-23s %3d %s\n",
346 $option_names{$i}, $i, $option_format{$i}[0];
350 %option_rnames = ();
351 foreach $opt (keys(%option_names)) {
352 $option_rnames{$option_names{$opt}} = $opt;
355 %before = ();
356 %after = ();
357 @clear = ();
358 $usage = 0;
359 $err = 0;
360 $list = 0;
361 $no_write = 0;
362 undef $file;
364 while (defined($opt = shift(@ARGV))) {
365 if ($opt !~ /^-/) {
366 if (defined($file)) {
367 $err = $usage = 1;
368 last;
370 $file = $opt;
371 } elsif ($opt eq '-b' || $opt eq '--before') {
372 $oname = shift(@ARGV);
373 $odata = shift(@ARGV);
375 if (!defined($odata)) {
376 $err = $usage = 1;
377 last;
380 $onum = option_number($oname);
381 if (!defined($onum)) {
382 print STDERR "$0: unknown option name: $oname\n";
383 $err = 1;
384 next;
387 $odata = parse_option($onum, $odata);
388 if (!defined($odata)) {
389 print STDERR "$0: unable to parse data for option $oname\n";
390 $err = 1;
391 next;
394 delete $after{$onum};
395 $before{$onum} = $odata;
396 push(@clear, $onum);
397 } elsif ($opt eq '-a' || $opt eq '--after') {
398 $oname = shift(@ARGV);
399 $odata = shift(@ARGV);
401 if (!defined($odata)) {
402 $err = $usage = 1;
403 last;
406 $onum = option_number($oname);
407 if (!defined($onum)) {
408 print STDERR "$0: unknown option name: $oname\n";
409 $err = 1;
410 next;
413 $odata = parse_option($onum, $odata);
414 if (!defined($odata)) {
415 print STDERR "$0: unable to parse data for option $oname\n";
416 $err = 1;
417 next;
420 delete $before{$onum};
421 $after{$onum} = $odata;
422 push(@clear, $onum);
423 } elsif ($opt eq '-d' || $opt eq '--delete') {
424 $oname = shift(@ARGV);
426 if (!defined($oname)) {
427 $err = $usage = 1;
428 last;
431 $onum = option_number($oname);
432 if (!defined($onum)) {
433 print STDERR "$0: unknown option name: $oname\n";
434 $err = 1;
435 next;
438 push(@clear, $onum);
439 delete $before{$onum};
440 delete $after{$onum};
441 } elsif ($opt eq '-n' || $opt eq '--no-write' || $opt eq '--dry-run') {
442 $no_write = 1;
443 } elsif ($opt eq '-l' || $opt eq '--list') {
444 $list = 1;
445 } elsif ($opt eq '-h' || $opt eq '--help') {
446 $usage = 1;
447 } else {
448 print STDERR "Invalid option: $opt\n";
449 $err = $usage = 1;
453 if (!defined($file) && !$usage) {
454 $err = $usage = 1;
456 if ($usage) {
457 usage();
459 if ($err || $usage) {
460 exit($err);
463 if (!scalar(@clear)) {
464 $no_write = 1; # No modifications requested
467 $mode = $no_write ? '<' : '+<';
469 open(FILE, $mode, $file)
470 or die "$0: cannot open: $file: $!\n";
471 ($patch_start, @data) = read_optsets(\*FILE);
472 if (!defined($patch_start)) {
473 die "$0: $file: patch block not found or file corrupt\n";
476 foreach $o (@clear) {
477 $data[0] = delete_option($o, $data[0]);
478 $data[1] = delete_option($o, $data[1]);
480 foreach $o (keys(%before)) {
481 $data[0] = add_option($o, $before{$o}, $data[0]);
483 foreach $o (keys(%after)) {
484 $data[1] = add_option($o, $after{$o}, $data[1]);
487 if ($list) {
488 list_options('-b ', $data[0]);
489 list_options('-a ', $data[1]);
492 if (!$no_write) {
493 if (!write_optsets(\*FILE, $patch_start, @data)) {
494 die "$0: $file: failed to write options: $!\n";
498 close(FILE);
499 exit 0;