|  | @@ -1,226 +0,0 @@
 | 
		
	
		
			
			| 1 |  | -#!/usr/bin/perl -w
 | 
		
	
		
			
			| 2 |  | -
 | 
		
	
		
			
			| 3 |  | -use Getopt::Std;
 | 
		
	
		
			
			| 4 |  | -
 | 
		
	
		
			
			| 5 |  | -use constant MINROMSIZE => 8192;
 | 
		
	
		
			
			| 6 |  | -use constant MAXROMSIZE => 262144;
 | 
		
	
		
			
			| 7 |  | -
 | 
		
	
		
			
			| 8 |  | -use constant PCI_PTR_LOC => 0x18;	# from beginning of ROM
 | 
		
	
		
			
			| 9 |  | -use constant PCI_HDR_SIZE => 0x18;
 | 
		
	
		
			
			| 10 |  | -use constant PNP_PTR_LOC => 0x1a;	# from beginning of ROM
 | 
		
	
		
			
			| 11 |  | -use constant PNP_HDR_SIZE => 0x20;
 | 
		
	
		
			
			| 12 |  | -use constant PNP_CHKSUM_OFF => 0x9;	# bytes from beginning of PnP header
 | 
		
	
		
			
			| 13 |  | -use constant PNP_DEVICE_OFF => 0x10;	# bytes from beginning of PnP header
 | 
		
	
		
			
			| 14 |  | -use constant PCI_VEND_ID_OFF => 0x4;	# bytes from beginning of PCI header
 | 
		
	
		
			
			| 15 |  | -use constant PCI_DEV_ID_OFF => 0x6;	# bytes from beginning of PCI header
 | 
		
	
		
			
			| 16 |  | -use constant PCI_SIZE_OFF => 0x10;	# bytes from beginning of PCI header
 | 
		
	
		
			
			| 17 |  | -
 | 
		
	
		
			
			| 18 |  | -use constant UNDI_PTR_LOC => 0x16;	# from beginning of ROM
 | 
		
	
		
			
			| 19 |  | -use constant UNDI_HDR_SIZE => 0x16;
 | 
		
	
		
			
			| 20 |  | -use constant UNDI_CHKSUM_OFF => 0x05;
 | 
		
	
		
			
			| 21 |  | -
 | 
		
	
		
			
			| 22 |  | -use strict;
 | 
		
	
		
			
			| 23 |  | -
 | 
		
	
		
			
			| 24 |  | -use vars qw(%opts);
 | 
		
	
		
			
			| 25 |  | -
 | 
		
	
		
			
			| 26 |  | -use bytes;
 | 
		
	
		
			
			| 27 |  | -
 | 
		
	
		
			
			| 28 |  | -sub getromsize ($) {
 | 
		
	
		
			
			| 29 |  | -	my ($romref) = @_;
 | 
		
	
		
			
			| 30 |  | -	my $i;
 | 
		
	
		
			
			| 31 |  | -
 | 
		
	
		
			
			| 32 |  | -	print STDERR "BIOS extension ROM Image did not start with 0x55 0xAA\n"
 | 
		
	
		
			
			| 33 |  | -		if (substr($$romref, 0, 2) ne "\x55\xaa");
 | 
		
	
		
			
			| 34 |  | -	my $size = ord(substr($$romref, 2, 1)) * 512;
 | 
		
	
		
			
			| 35 |  | -	for ($i = MINROMSIZE; $i < MAXROMSIZE and $i < $size; $i *= 2) { }
 | 
		
	
		
			
			| 36 |  | -	print STDERR "$size is a strange size for a boot ROM\n"
 | 
		
	
		
			
			| 37 |  | -		if ($size > 0 and $i > $size);
 | 
		
	
		
			
			| 38 |  | -	return ($size);
 | 
		
	
		
			
			| 39 |  | -}
 | 
		
	
		
			
			| 40 |  | -
 | 
		
	
		
			
			| 41 |  | -sub addident ($) {
 | 
		
	
		
			
			| 42 |  | -	my ($romref) = @_;
 | 
		
	
		
			
			| 43 |  | -
 | 
		
	
		
			
			| 44 |  | -	return (0) unless (my $s = $opts{'i'});
 | 
		
	
		
			
			| 45 |  | -	# include the terminating NUL byte too
 | 
		
	
		
			
			| 46 |  | -	$s .= "\x00";
 | 
		
	
		
			
			| 47 |  | -	my $len = length($s);
 | 
		
	
		
			
			| 48 |  | -	# Put the identifier in only if the space is blank
 | 
		
	
		
			
			| 49 |  | -	my $pos = length($$romref) - $len - 2;
 | 
		
	
		
			
			| 50 |  | -	return (0) if (substr($$romref, $pos, $len) ne ("\xFF" x $len));
 | 
		
	
		
			
			| 51 |  | -	substr($$romref, $pos, $len) = $s;
 | 
		
	
		
			
			| 52 |  | -	return ($pos);
 | 
		
	
		
			
			| 53 |  | -}
 | 
		
	
		
			
			| 54 |  | -
 | 
		
	
		
			
			| 55 |  | -sub pcipnpheaders ($$) {
 | 
		
	
		
			
			| 56 |  | -	my ($romref, $identoffset) = @_;
 | 
		
	
		
			
			| 57 |  | -	my ($pci_hdr_offset, $pnp_hdr_offset);
 | 
		
	
		
			
			| 58 |  | -
 | 
		
	
		
			
			| 59 |  | -	$pci_hdr_offset = unpack('v', substr($$romref, PCI_PTR_LOC, 2));
 | 
		
	
		
			
			| 60 |  | -	$pnp_hdr_offset = unpack('v', substr($$romref, PNP_PTR_LOC, 2));
 | 
		
	
		
			
			| 61 |  | -	# Sanity checks
 | 
		
	
		
			
			| 62 |  | -	if ($pci_hdr_offset < PCI_PTR_LOC + 2
 | 
		
	
		
			
			| 63 |  | -		or $pci_hdr_offset > length($$romref) - PCI_HDR_SIZE
 | 
		
	
		
			
			| 64 |  | -		or $pnp_hdr_offset < PNP_PTR_LOC + 2
 | 
		
	
		
			
			| 65 |  | -		or $pnp_hdr_offset > length($$romref) - PNP_HDR_SIZE
 | 
		
	
		
			
			| 66 |  | -		or substr($$romref, $pci_hdr_offset, 4) ne 'PCIR'
 | 
		
	
		
			
			| 67 |  | -		or substr($$romref, $pnp_hdr_offset, 4) ne '$PnP') {
 | 
		
	
		
			
			| 68 |  | -		$pci_hdr_offset = $pnp_hdr_offset = 0;
 | 
		
	
		
			
			| 69 |  | -	} else {
 | 
		
	
		
			
			| 70 |  | -		printf "PCI header at %#x and PnP header at %#x\n",
 | 
		
	
		
			
			| 71 |  | -			$pci_hdr_offset, $pnp_hdr_offset if $opts{'v'};
 | 
		
	
		
			
			| 72 |  | -	}
 | 
		
	
		
			
			| 73 |  | -	if ($pci_hdr_offset > 0) {
 | 
		
	
		
			
			| 74 |  | -		my ($pci_vendor_id, $pci_device_id);
 | 
		
	
		
			
			| 75 |  | -		# if no -p option, just report what's there
 | 
		
	
		
			
			| 76 |  | -		if (!defined($opts{'p'})) {
 | 
		
	
		
			
			| 77 |  | -			$pci_vendor_id = unpack('v', substr($$romref, $pci_hdr_offset+PCI_VEND_ID_OFF, 2));
 | 
		
	
		
			
			| 78 |  | -			$pci_device_id = unpack('v', substr($$romref, $pci_hdr_offset+PCI_DEV_ID_OFF, 2));
 | 
		
	
		
			
			| 79 |  | -			printf "PCI Vendor ID %#x Device ID %#x\n",
 | 
		
	
		
			
			| 80 |  | -				$pci_vendor_id, $pci_device_id;
 | 
		
	
		
			
			| 81 |  | -		} else {
 | 
		
	
		
			
			| 82 |  | -			substr($$romref, $pci_hdr_offset + PCI_SIZE_OFF, 2)
 | 
		
	
		
			
			| 83 |  | -				= pack('v', length($$romref) / 512);
 | 
		
	
		
			
			| 84 |  | -			($pci_vendor_id, $pci_device_id) = split(/,/, $opts{'p'});
 | 
		
	
		
			
			| 85 |  | -			substr($$romref, $pci_hdr_offset+PCI_VEND_ID_OFF, 2)
 | 
		
	
		
			
			| 86 |  | -				= pack('v', oct($pci_vendor_id)) if ($pci_vendor_id);
 | 
		
	
		
			
			| 87 |  | -			substr($$romref, $pci_hdr_offset+PCI_DEV_ID_OFF, 2)
 | 
		
	
		
			
			| 88 |  | -				= pack('v', oct($pci_device_id)) if ($pci_device_id);
 | 
		
	
		
			
			| 89 |  | -		}
 | 
		
	
		
			
			| 90 |  | -	}
 | 
		
	
		
			
			| 91 |  | -	if ($pnp_hdr_offset > 0) {
 | 
		
	
		
			
			| 92 |  | -		if (defined($identoffset)) {
 | 
		
	
		
			
			| 93 |  | -			# Point to device id string at end of ROM image
 | 
		
	
		
			
			| 94 |  | -			substr($$romref, $pnp_hdr_offset+PNP_DEVICE_OFF, 2)
 | 
		
	
		
			
			| 95 |  | -				= pack('v', $identoffset);
 | 
		
	
		
			
			| 96 |  | -		}
 | 
		
	
		
			
			| 97 |  | -		substr($$romref, $pnp_hdr_offset+PNP_CHKSUM_OFF, 1) = "\x00";
 | 
		
	
		
			
			| 98 |  | -		my $sum = unpack('%8C*', substr($$romref, $pnp_hdr_offset,
 | 
		
	
		
			
			| 99 |  | -			PNP_HDR_SIZE));
 | 
		
	
		
			
			| 100 |  | -		substr($$romref, $pnp_hdr_offset+PNP_CHKSUM_OFF, 1) = chr(256 - $sum);
 | 
		
	
		
			
			| 101 |  | -	}
 | 
		
	
		
			
			| 102 |  | -}
 | 
		
	
		
			
			| 103 |  | -
 | 
		
	
		
			
			| 104 |  | -sub undiheaders ($) {
 | 
		
	
		
			
			| 105 |  | -	my ($romref) = @_;
 | 
		
	
		
			
			| 106 |  | -	my ($undi_hdr_offset);
 | 
		
	
		
			
			| 107 |  | -
 | 
		
	
		
			
			| 108 |  | -	$undi_hdr_offset = unpack('v', substr($$romref, UNDI_PTR_LOC, 2));
 | 
		
	
		
			
			| 109 |  | -	# Sanity checks
 | 
		
	
		
			
			| 110 |  | -	if ($undi_hdr_offset < UNDI_PTR_LOC + 2
 | 
		
	
		
			
			| 111 |  | -		or $undi_hdr_offset > length($$romref) - UNDI_HDR_SIZE
 | 
		
	
		
			
			| 112 |  | -		or substr($$romref, $undi_hdr_offset, 4) ne 'UNDI') {
 | 
		
	
		
			
			| 113 |  | -		$undi_hdr_offset = 0;
 | 
		
	
		
			
			| 114 |  | -	} else {
 | 
		
	
		
			
			| 115 |  | -		printf "UNDI header at %#x\n", $undi_hdr_offset if $opts{'v'};
 | 
		
	
		
			
			| 116 |  | -	}
 | 
		
	
		
			
			| 117 |  | -	if ($undi_hdr_offset > 0) {
 | 
		
	
		
			
			| 118 |  | -		substr($$romref, $undi_hdr_offset+UNDI_CHKSUM_OFF, 1) = "\x00";
 | 
		
	
		
			
			| 119 |  | -		my $sum = unpack('%8C*', substr($$romref, $undi_hdr_offset,
 | 
		
	
		
			
			| 120 |  | -			UNDI_HDR_SIZE));
 | 
		
	
		
			
			| 121 |  | -		substr($$romref, $undi_hdr_offset+UNDI_CHKSUM_OFF, 1) = chr(256 - $sum);
 | 
		
	
		
			
			| 122 |  | -	}
 | 
		
	
		
			
			| 123 |  | -}
 | 
		
	
		
			
			| 124 |  | -
 | 
		
	
		
			
			| 125 |  | -sub writerom ($$) {
 | 
		
	
		
			
			| 126 |  | -	my ($filename, $romref) = @_;
 | 
		
	
		
			
			| 127 |  | -
 | 
		
	
		
			
			| 128 |  | -	open(R, ">$filename") or die "$filename: $!\n";
 | 
		
	
		
			
			| 129 |  | -	print R $$romref;
 | 
		
	
		
			
			| 130 |  | -	close(R);
 | 
		
	
		
			
			| 131 |  | -}
 | 
		
	
		
			
			| 132 |  | -
 | 
		
	
		
			
			| 133 |  | -sub checksum ($) {
 | 
		
	
		
			
			| 134 |  | -	my ($romref) = @_;
 | 
		
	
		
			
			| 135 |  | -
 | 
		
	
		
			
			| 136 |  | -	substr($$romref, 6, 1) = "\x00";
 | 
		
	
		
			
			| 137 |  | -	my $sum = unpack('%8C*', $$romref);
 | 
		
	
		
			
			| 138 |  | -	substr($$romref, 6, 1) = chr(256 - $sum);
 | 
		
	
		
			
			| 139 |  | -	# Double check
 | 
		
	
		
			
			| 140 |  | -	$sum = unpack('%8C*', $$romref);
 | 
		
	
		
			
			| 141 |  | -	if ($sum != 0) {
 | 
		
	
		
			
			| 142 |  | -		print "Checksum fails\n"
 | 
		
	
		
			
			| 143 |  | -	} elsif ($opts{'v'}) {
 | 
		
	
		
			
			| 144 |  | -		print "Checksum ok\n";
 | 
		
	
		
			
			| 145 |  | -	}
 | 
		
	
		
			
			| 146 |  | -}
 | 
		
	
		
			
			| 147 |  | -
 | 
		
	
		
			
			| 148 |  | -sub makerom () {
 | 
		
	
		
			
			| 149 |  | -	my ($rom, $romsize);
 | 
		
	
		
			
			| 150 |  | -
 | 
		
	
		
			
			| 151 |  | -	getopts('3xi:p:s:v', \%opts);
 | 
		
	
		
			
			| 152 |  | -	$ARGV[0] or die "Usage: $0 [-s romsize] [-i ident] [-p vendorid,deviceid] [-x] [-3] rom-file\n";
 | 
		
	
		
			
			| 153 |  | -	open(R, $ARGV[0]) or die "$ARGV[0]: $!\n";
 | 
		
	
		
			
			| 154 |  | -	# Read in the whole ROM in one gulp
 | 
		
	
		
			
			| 155 |  | -	my $filesize = read(R, $rom, MAXROMSIZE+1);
 | 
		
	
		
			
			| 156 |  | -	close(R);
 | 
		
	
		
			
			| 157 |  | -	defined($filesize) and $filesize >= 3 or die "Cannot get first 3 bytes of file\n";
 | 
		
	
		
			
			| 158 |  | -	print "$filesize bytes read\n" if $opts{'v'};
 | 
		
	
		
			
			| 159 |  | -	# If PXE image, just fill the length field and write it out
 | 
		
	
		
			
			| 160 |  | -	if ($opts{'x'}) {
 | 
		
	
		
			
			| 161 |  | -		substr($rom, 2, 1) = chr((length($rom) + 511) / 512);
 | 
		
	
		
			
			| 162 |  | -		writerom($ARGV[0], \$rom);
 | 
		
	
		
			
			| 163 |  | -		return;
 | 
		
	
		
			
			| 164 |  | -	}
 | 
		
	
		
			
			| 165 |  | -	# Size specified with -s overrides value in 3rd byte in image
 | 
		
	
		
			
			| 166 |  | -	# -s 0 means round up to next 512 byte block
 | 
		
	
		
			
			| 167 |  | -	if (defined($opts{'s'})) {
 | 
		
	
		
			
			| 168 |  | -		if (($romsize = oct($opts{'s'})) <= 0) {
 | 
		
	
		
			
			| 169 |  | -			# NB: This roundup trick only works on powers of 2
 | 
		
	
		
			
			| 170 |  | -			$romsize = ($filesize + 511) & ~511
 | 
		
	
		
			
			| 171 |  | -		}
 | 
		
	
		
			
			| 172 |  | -	} else {
 | 
		
	
		
			
			| 173 |  | -		# Shrink romsize down to the smallest power of two that will do
 | 
		
	
		
			
			| 174 |  | -		for ($romsize = MAXROMSIZE;
 | 
		
	
		
			
			| 175 |  | -		     $romsize > MINROMSIZE and $romsize >= 2*$filesize;
 | 
		
	
		
			
			| 176 |  | -		     $romsize /= 2) { }
 | 
		
	
		
			
			| 177 |  | -	}
 | 
		
	
		
			
			| 178 |  | -	if ($filesize > $romsize) {
 | 
		
	
		
			
			| 179 |  | -		print STDERR "ROM size of $romsize not big enough for data, ";
 | 
		
	
		
			
			| 180 |  | -		# NB: This roundup trick only works on powers of 2
 | 
		
	
		
			
			| 181 |  | -		$romsize = ($filesize + 511) & ~511;
 | 
		
	
		
			
			| 182 |  | -		print "will use $romsize instead\n"
 | 
		
	
		
			
			| 183 |  | -	}
 | 
		
	
		
			
			| 184 |  | -	# Pad with 0xFF to $romsize
 | 
		
	
		
			
			| 185 |  | -	$rom .= "\xFF" x ($romsize - length($rom));
 | 
		
	
		
			
			| 186 |  | -	if ($romsize >= 128 * 1024) {
 | 
		
	
		
			
			| 187 |  | -		print "Warning: ROM size exceeds extension BIOS limit\n";
 | 
		
	
		
			
			| 188 |  | -	}
 | 
		
	
		
			
			| 189 |  | -	substr($rom, 2, 1) = chr(($romsize / 512) % 256);
 | 
		
	
		
			
			| 190 |  | -	print "ROM size is $romsize\n" if $opts{'v'};
 | 
		
	
		
			
			| 191 |  | -	# set the product string only if we don't have one yet
 | 
		
	
		
			
			| 192 |  | -	my $pnp_hdr_offset = unpack('v', substr($rom, PNP_PTR_LOC, 2));
 | 
		
	
		
			
			| 193 |  | -	my $identoffset = substr($rom, $pnp_hdr_offset+PNP_DEVICE_OFF, 2) eq "\0\0" ? addident(\$rom) : undef;
 | 
		
	
		
			
			| 194 |  | -	pcipnpheaders(\$rom, $identoffset);
 | 
		
	
		
			
			| 195 |  | -	undiheaders(\$rom);
 | 
		
	
		
			
			| 196 |  | -	# 3c503 requires last two bytes to be 0x80
 | 
		
	
		
			
			| 197 |  | -	substr($rom, MINROMSIZE-2, 2) = "\x80\x80"
 | 
		
	
		
			
			| 198 |  | -		if ($opts{'3'} and $romsize == MINROMSIZE);
 | 
		
	
		
			
			| 199 |  | -	checksum(\$rom);
 | 
		
	
		
			
			| 200 |  | -	writerom($ARGV[0], \$rom);
 | 
		
	
		
			
			| 201 |  | -}
 | 
		
	
		
			
			| 202 |  | -
 | 
		
	
		
			
			| 203 |  | -sub modrom () {
 | 
		
	
		
			
			| 204 |  | -	my ($rom);
 | 
		
	
		
			
			| 205 |  | -
 | 
		
	
		
			
			| 206 |  | -	getopts('p:v', \%opts);
 | 
		
	
		
			
			| 207 |  | -	$ARGV[0] or die "Usage: $0 [-p vendorid,deviceid] rom-file\n";
 | 
		
	
		
			
			| 208 |  | -	open(R, $ARGV[0]) or die "$ARGV[0]: $!\n";
 | 
		
	
		
			
			| 209 |  | -	# Read in the whole ROM in one gulp
 | 
		
	
		
			
			| 210 |  | -	my $filesize = read(R, $rom, MAXROMSIZE+1);
 | 
		
	
		
			
			| 211 |  | -	close(R);
 | 
		
	
		
			
			| 212 |  | -	defined($filesize) and $filesize >= 3 or die "Cannot get first 3 bytes of file\n";
 | 
		
	
		
			
			| 213 |  | -	print "$filesize bytes read\n" if $opts{'v'};
 | 
		
	
		
			
			| 214 |  | -	pcipnpheaders(\$rom, undef);
 | 
		
	
		
			
			| 215 |  | -	undiheaders(\$rom);
 | 
		
	
		
			
			| 216 |  | -	checksum(\$rom);
 | 
		
	
		
			
			| 217 |  | -	writerom($ARGV[0], \$rom);
 | 
		
	
		
			
			| 218 |  | -}
 | 
		
	
		
			
			| 219 |  | -
 | 
		
	
		
			
			| 220 |  | -# Main routine. See how we were called and behave accordingly
 | 
		
	
		
			
			| 221 |  | -if ($0 =~ m:modrom(\.pl)?$:) {
 | 
		
	
		
			
			| 222 |  | -	modrom();
 | 
		
	
		
			
			| 223 |  | -} else {
 | 
		
	
		
			
			| 224 |  | -	makerom();
 | 
		
	
		
			
			| 225 |  | -}
 | 
		
	
		
			
			| 226 |  | -exit(0);
 |