summaryrefslogtreecommitdiffstats
path: root/src/util/padimg.pl
diff options
context:
space:
mode:
authorMichael Brown2009-04-16 04:15:08 +0200
committerMichael Brown2009-04-16 04:15:08 +0200
commit7741546a406217827c3d4a8d72aaa322b2565c35 (patch)
treea871a7879a035e76ae17cc096dc5317e4d8f400c /src/util/padimg.pl
parent[dhcp] Accept filename via DHCP option 67 as well as BOOTP filename field (diff)
downloadipxe-7741546a406217827c3d4a8d72aaa322b2565c35.tar.gz
ipxe-7741546a406217827c3d4a8d72aaa322b2565c35.tar.xz
ipxe-7741546a406217827c3d4a8d72aaa322b2565c35.zip
[build] Pad .rom, .dsk, and .hd images to 512-byte boundaries
QEMU will silently round down a disk or ROM image file to the nearest 512 bytes. Fix by always padding .rom, .dsk and .hd images to the nearest 512-byte boundary. Originally-fixed-by: Stefan Hajnoczi <stefanha@gmail.com>
Diffstat (limited to 'src/util/padimg.pl')
-rwxr-xr-xsrc/util/padimg.pl44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/util/padimg.pl b/src/util/padimg.pl
new file mode 100755
index 00000000..f3dcbbc3
--- /dev/null
+++ b/src/util/padimg.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+use Getopt::Long;
+use Fcntl;
+
+my $verbosity = 0;
+my $blksize = 512;
+my $byte = 0;
+
+my $opts = {
+ 'verbose|v+' => sub { $verbosity++; },
+ 'quiet|q+' => sub { $verbosity--; },
+ 'blksize|s=o' => sub { $blksize = $_[1]; },
+ 'byte|b=o' => sub { $byte = $_[1]; },
+};
+
+Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
+GetOptions ( { map { /^(\w+)/; $1 => $opts->{$_} } keys %$opts }, keys %$opts )
+ or die "Could not parse command-line options\n";
+
+while ( my $filename = shift ) {
+ die "$filename is not a file\n" unless -f $filename;
+ my $oldsize = -s $filename;
+ my $newsize = ( ( $oldsize + $blksize - 1 ) & ~( $blksize - 1 ) );
+ my $padsize = ( $newsize - $oldsize );
+ next unless $padsize;
+ if ( $verbosity >= 1 ) {
+ printf "Padding %s from %d to %d bytes with %d x 0x%02x\n",
+ $filename, $oldsize, $newsize, $padsize, $byte;
+ }
+ if ( $byte ) {
+ sysopen ( my $fh, $filename, ( O_WRONLY | O_APPEND ) )
+ or die "Could not open $filename for appending: $!\n";
+ syswrite $fh, ( chr ( $byte ) x $padsize )
+ or die "Could not append to $filename: $!\n";
+ close ( $fh );
+ } else {
+ truncate $filename, $newsize
+ or die "Could not resize $filename: $!\n";
+ }
+ die "Failed to pad $filename\n" unless -s $filename == $newsize;
+}