You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

makerom.pl 7.5KB

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