livecd-tools/rosa-image-fix-x86_64.pl
2014-01-21 12:47:57 +04:00

31 lines
917 B
Perl
Executable file

#!/usr/bin/perl
use strict;
use warnings;
use Fcntl ':seek';
if (scalar(@ARGV) < 1) {
print "Usage: $0 iso-image-file\n";
exit;
}
my $filename = $ARGV[0];
my $file;
open($file, '+<', $filename) or die "Failed to open file $filename for read-write: $!";
binmode($file);
# Read the first sector (512 bytes) containing the MBR
my $mbr;
read($file, $mbr, 512) or die "Failed to read the MBR sector: $!";
# The partition table starts at offset 446 and contains 4 records 16 bytes each. What we do:
# 1) Replace the first record with the second one;
# 2) Replace the first byte of teh resulting first record with 0x80 (active partition);
# 3) Delete the second partition by zeroing its contents.
substr($mbr, 446, 32) = "\x80" . substr($mbr, 463, 15) . ("\0" x 16);
# Write the updated MBR back
seek($file, 0, SEEK_SET) or die "Failed to position at the beginning of the file: $!";
print $file $mbr;
close($file);