Implement a proper block layer with partition support.
[pintos-anon] / src / utils / pintos-set-cmdline
1 #! /usr/bin/perl -w
2
3 use strict;
4 use Fcntl 'SEEK_SET';
5
6 # Read Pintos.pm from the same directory as this program.
7 BEGIN { my $self = $0; $self =~ s%/+[^/]*$%%; require "$self/Pintos.pm"; }
8
9 # Get command-line arguments.
10 usage (0) if @ARGV == 1 && $ARGV[0] eq '--help';
11 usage (1) if @ARGV < 2 || $ARGV[1] ne '--';
12 my ($disk, undef, @kernel_args) = @ARGV;
13
14 # Open disk.
15 my ($handle);
16 open ($handle, '+<', $disk) or die "$disk: open: $!\n";
17
18 # Check that it's a partitioned disk with a Pintos loader.
19 my ($buffer) = read_fully ($handle, $disk, 512);
20 unpack ("x510 v", $buffer) == 0xaa55 or die "$disk: not a partitioned disk\n";
21 $buffer =~ /Pintos/ or die "$disk: does not contain Pintos loader\n";
22
23 # Write the command line.
24 our ($LOADER_SIZE);
25 sysseek ($handle, $LOADER_SIZE, SEEK_SET) == $LOADER_SIZE
26   or die "$disk: seek: $!\n";
27 write_fully ($handle, $disk, make_kernel_command_line (@kernel_args));
28
29 # Close disk.
30 close ($handle) or die "$disk: close: $!\n";
31
32 exit 0;
33
34 sub usage {
35     print <<'EOF';
36 pintos-set-cmdline, a utility for changing the command line in Pintos disks
37 Usage: pintos-set-cmdline DISK -- [ARGUMENT...]
38 where DISK is a bootable disk containing a Pintos loader
39   and each ARGUMENT is inserted into the command line written to DISK.
40 EOF
41     exit ($_[0]);
42 }