#! /usr/bin/perl
use strict;
use warnings;
use bytes;
use Getopt::Long;
use Fcntl qw(SEEK_SET);


sub signature($$$$;$)
{
my($hay,$hay_filename,$offset,$needle,$msg)=@_;

	die if length($needle)%1;
	my $len_bytes=length($needle)/2;
	substr($hay,$offset,$len_bytes) eq pack("H*",$needle)
			or die "\"$hay_filename\" error reading signature 0x$needle @".sprintf("0x%X",$offset)
					.(!$msg ? "" : "; $msg")."\n";
}

my $BACKUP_SECTOR_START=63;
my $MAX_BOOT_SIZE=0x1b6;	# LILO: leave some space for NT's and DR DOS' dirty


my $bin_filename="./biosautoraid.bin";
my $opt_install;
my $opt_uninstall;
my $options_error=!GetOptions(
		"bin=s"      ,\$bin_filename,
		"i|install"  ,\$opt_install,
		"u|uninstall",\$opt_uninstall,
		);
my $BIN;
open $BIN,$bin_filename or die "open \"$bin_filename\": $!";
my $bin=do { local $/; <$BIN>; } or die "read \"$bin_filename\": $!";
close $BIN or die "close \"$bin_filename\": $!";

my $bin_len=length($bin);
$bin_len>0 && !($bin_len%0x200) or die "Invalid length of \"$bin_filename\": $bin_len";
my $bin_short=($bin_len==0x200);	# => no 0xBEEF

signature $bin,$bin_filename,0x1FE     ,"55AA";
substr($bin,$MAX_BOOT_SIZE,0x1FE-$MAX_BOOT_SIZE)=~/^\x00*$/s or die "No empty magic+partitiontable: $bin_filename";

if (substr($bin,3,8) eq "BIOSRAID") {
	signature $bin,$bin_filename,0x03      ,unpack("H*","BIOSRAID");
	signature $bin,$bin_filename,0x43      ,"5E81A2".sprintf("%X",$BACKUP_SECTOR_START);
	signature $bin,$bin_filename,$bin_len-2,"BEEF" if !$bin_short;
	}
else {
	signature $bin,$bin_filename,0x03      ,unpack("H*","BIOSRAI2")
			.sprintf("%X%X",$BACKUP_SECTOR_START,$MAX_BOOT_SIZE-0x100);
	}

my $all_ok;
END {
	warn "Syntax: $0 [--bin=<biosautoraid.bin>] [{-i|--install|-u|--uninstall} <device>]" if !$all_ok;
	}

if (!@ARGV && !$options_error) {
	print "OK, \"$bin_filename\" checked.\n";
	$all_ok=1;
	exit 0;
	}

die "Type -i|--install or -u|--uninstall"                     if (!$opt_install && !$opt_uninstall) || $options_error;
die "Both -i|--install and -u|--uninstall forbidden together" if   $opt_install &&  $opt_uninstall;

@ARGV<=1 or die;
my $master_filename=$ARGV[0];

my $master_is_b=(-b $master_filename);
my $master_is_f=(-f $master_filename);
die "Invalid device file type: $master_filename" if !$master_is_b && !$master_is_f;
my $backup_filename;
my $backup_offset=$BACKUP_SECTOR_START*0x200;
if ($master_is_f) {
	$backup_filename=$master_filename;
	}
if ($master_is_b) {
	die "Filename is block device but it is unsupported: $master_filename"
			if $master_filename!~m{^/dev/[hs]d[a-z]};
	$backup_filename=$master_filename."1";

	sub hdparm_g_start_check($$)
	{
	my($filename,$start_expected)=@_;
	
		local *HDPARM;
		my $cmdname="hdparm -g '$filename'|";
		open HDPARM,$cmdname or die "open \"$cmdname\": $!";
		my $HDPARM=do { local $/; <HDPARM>; } or die "read \"$cmdname\": $!";
		close HDPARM or die "close \"$cmdname\": $!";
		my($start)=($HDPARM=~/^\s*\Q$filename\E:\s*geometry\s*=.*,\s*start\s*=\s*(\d+)\s*$/s)
				or die "Unparsable output of \"$cmdname\":\n$HDPARM";
		die "Unexpected 'start' parameter $start (expected $start_expected) of: $filename"
				if $start!=$start_expected;
	}

	hdparm_g_start_check($master_filename,0);
	hdparm_g_start_check($backup_filename,$backup_offset/0x200);
	$backup_offset=0;
	}

my $MASTER;
open $MASTER,"+<".$master_filename or die "open \"$master_filename\": $!";

# Do not write to the '/dev/hda' areas being mapped by: /dev/hda1
# as reboot will rewrite '/dev/hda' with the contents of: /dev/hda1
my $BACKUP;
if ($master_filename ne $backup_filename) {
	open $BACKUP,"+<".$backup_filename or die "open \"$backup_filename\": $!";
	die if $backup_offset;
	}
else {
	$BACKUP=$MASTER;
	die if !$backup_offset;
	}

sysseek $MASTER,0,SEEK_SET or die "Error seeking $master_filename";
my $master;
$bin_len==sysread $MASTER,$master,$bin_len or die "read \"$master_filename\": $!";
length($master)==$bin_len or die "read \"$master_filename\": ".length($master)."!=$bin_len";
signature $master,$master_filename,0x1FE,"55AA";

sysseek $BACKUP,$backup_offset,SEEK_SET or die "Error seeking $backup_filename";
my $backup;
$bin_len==sysread $BACKUP,$backup,$bin_len or die "read \"$backup_filename\": $!";
length($backup)==$bin_len or die "read \"$backup_filename\": ".length($backup)."!=$bin_len";

my $zeroes="\x00"x$bin_len;
die if length($zeroes)!=$bin_len;

$bin=substr($bin,0,$MAX_BOOT_SIZE).substr($master,$MAX_BOOT_SIZE,0x1FE-$MAX_BOOT_SIZE).substr($bin,0x1FE);

my $installed=substr($master,0x03,7) eq "BIOSRAI";
print "Already instaled? ".($installed ? "YES" : "NO")."\n";

die "Nothing to uninstall!\n" if $opt_uninstall && !$installed;

if ($installed) {
	signature $master,$master_filename,$bin_len-2,"BEEF","Different installed SECTOR_LEN?" if !$bin_short;
	signature $master,$master_filename,0x03      ,unpack("H*","BIOSRAI");
	signature $master,$master_filename,0x43      ,"5E81A2".sprintf("%X",$BACKUP_SECTOR_START) if !$bin_short;
	signature $backup,$backup_filename,0x1FE,"55AA";
	}
if ($opt_install) {
	if (!$installed) {
		if ($backup ne $zeroes) {
			# Permit different DOS-volume area and 0x1FC..0x1FD bytes.
			die "Sensitive bytes at backup sectors" if !(1
				&& (0
					|| substr($backup,0x200) eq substr($zeroes,0x200)
					|| (substr($backup,$bin_len-2) eq pack("H*","BEEF") && !$bin_short)
					)
				&& substr($backup,0,0x03) eq substr($master,0,0x03)
				&& substr($backup,0x5A,$MAX_BOOT_SIZE-0x5A) eq substr($master,0x5A,$MAX_BOOT_SIZE-0x5A)
				);
			warn "WARNING: Dropping obsolete (but similiar) backup! Use --uninstall next time.\n";
			}
		sysseek $BACKUP,$backup_offset,SEEK_SET or die "Error seeking $backup_filename";
		$bin_len==syswrite $BACKUP,$master or die "Error backing up the master";
		}
	sysseek $MASTER,0,SEEK_SET or die "Error seeking $master_filename";
	$bin_len==syswrite $MASTER,$bin or die "Error writing new master of BIOSautoRAID";
	}
if ($opt_uninstall) {
	sysseek $MASTER,0,SEEK_SET or die "Error seeking $master_filename";
	$bin_len==syswrite $MASTER,$backup or die "Error writing backup back to the master";
	sysseek $BACKUP,$backup_offset,SEEK_SET or die "Error seeking $backup_filename";
	$bin_len==syswrite $BACKUP,$zeroes or die "Error clearing the backup area";
	}

close $MASTER or die "close \"$master_filename\": $!";
if ($master_filename ne $backup_filename) {
	close $BACKUP or die "close \"$backup_filename\": $!";
	}
system("sync") and die "Error running: sync: $!";

print "OK; ".($opt_install ? ($installed ? "re" : "")."installed" : "uninstalled")."\n";
$all_ok=1;
