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.

modrom.pl 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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;
  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 and defined($identoffset)) {
  79. # Point to device id string at end of ROM image
  80. substr($$romref, $pnp_hdr_offset+PNP_DEVICE_OFF, 2)
  81. = pack('v', $identoffset);
  82. substr($$romref, $pnp_hdr_offset+PNP_CHKSUM_OFF, 1) = "\x00";
  83. my $sum = unpack('%8C*', substr($$romref, $pnp_hdr_offset,
  84. PNP_HDR_SIZE));
  85. substr($$romref, $pnp_hdr_offset+PNP_CHKSUM_OFF, 1) = chr(256 - $sum);
  86. }
  87. }
  88. sub undiheaders ($) {
  89. my ($romref) = @_;
  90. my ($undi_hdr_offset);
  91. $undi_hdr_offset = unpack('v', substr($$romref, UNDI_PTR_LOC, 2));
  92. # Sanity checks
  93. if ($undi_hdr_offset < UNDI_PTR_LOC + 2
  94. or $undi_hdr_offset > length($$romref) - UNDI_HDR_SIZE
  95. or substr($$romref, $undi_hdr_offset, 4) ne 'UNDI') {
  96. $undi_hdr_offset = 0;
  97. } else {
  98. printf "UNDI header at %#x\n", $undi_hdr_offset;
  99. }
  100. if ($undi_hdr_offset > 0) {
  101. substr($$romref, $undi_hdr_offset+UNDI_CHKSUM_OFF, 1) = "\x00";
  102. my $sum = unpack('%8C*', substr($$romref, $undi_hdr_offset,
  103. UNDI_HDR_SIZE));
  104. substr($$romref, $undi_hdr_offset+UNDI_CHKSUM_OFF, 1) = chr(256 - $sum);
  105. }
  106. }
  107. sub writerom ($$) {
  108. my ($filename, $romref) = @_;
  109. open(R, ">$filename") or die "$filename: $!\n";
  110. print R $$romref;
  111. close(R);
  112. }
  113. sub checksum ($) {
  114. my ($romref) = @_;
  115. substr($$romref, 6, 1) = "\x00";
  116. my $sum = unpack('%8C*', $$romref);
  117. substr($$romref, 6, 1) = chr(256 - $sum);
  118. # Double check
  119. $sum = unpack('%8C*', $$romref);
  120. if ($sum != 0) {
  121. print "Checksum fails\n"
  122. } elsif ($opts{'v'}) {
  123. print "Checksum ok\n";
  124. }
  125. }
  126. sub makerom () {
  127. my ($rom, $romsize);
  128. getopts('3xi:p:s:v', \%opts);
  129. $ARGV[0] or die "Usage: $0 [-s romsize] [-i ident] [-p vendorid,deviceid] [-x] [-3] rom-file\n";
  130. open(R, $ARGV[0]) or die "$ARGV[0]: $!\n";
  131. # Read in the whole ROM in one gulp
  132. my $filesize = read(R, $rom, MAXROMSIZE+1);
  133. close(R);
  134. defined($filesize) and $filesize >= 3 or die "Cannot get first 3 bytes of file\n";
  135. print "$filesize bytes read\n" if $opts{'v'};
  136. # If PXE image, just fill the length field and write it out
  137. if ($opts{'x'}) {
  138. substr($rom, 2, 1) = chr((length($rom) + 511) / 512);
  139. &writerom($ARGV[0], \$rom);
  140. return;
  141. }
  142. # Size specified with -s overrides value in 3rd byte in image
  143. # -s 0 means round up to next 512 byte block
  144. if (defined($opts{'s'})) {
  145. if (($romsize = oct($opts{'s'})) <= 0) {
  146. # NB: This roundup trick only works on powers of 2
  147. $romsize = ($filesize + 511) & ~511
  148. }
  149. } else {
  150. $romsize = &getromsize(\$rom);
  151. # 0 put there by *loader.S means makerom should pick the size
  152. if ($romsize == 0) {
  153. # Shrink romsize down to the smallest power of two that will do
  154. for ($romsize = MAXROMSIZE;
  155. $romsize > MINROMSIZE and $romsize >= 2*$filesize;
  156. $romsize /= 2) { }
  157. }
  158. }
  159. if ($filesize > $romsize) {
  160. print STDERR "ROM size of $romsize not big enough for data, ";
  161. # NB: This roundup trick only works on powers of 2
  162. $romsize = ($filesize + 511) & ~511;
  163. print "will use $romsize instead\n"
  164. }
  165. # Pad with 0xFF to $romsize
  166. $rom .= "\xFF" x ($romsize - length($rom));
  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. print "ROM size is $romsize\n" if $opts{'v'};
  172. my $identoffset = &addident(\$rom);
  173. &pcipnpheaders(\$rom, $identoffset);
  174. &undiheaders(\$rom);
  175. # 3c503 requires last two bytes to be 0x80
  176. substr($rom, MINROMSIZE-2, 2) = "\x80\x80"
  177. if ($opts{'3'} and $romsize == MINROMSIZE);
  178. &checksum(\$rom);
  179. &writerom($ARGV[0], \$rom);
  180. }
  181. sub modrom () {
  182. my ($rom);
  183. getopts('p:v', \%opts);
  184. $ARGV[0] or die "Usage: $0 [-p vendorid,deviceid] rom-file\n";
  185. open(R, $ARGV[0]) or die "$ARGV[0]: $!\n";
  186. # Read in the whole ROM in one gulp
  187. my $filesize = read(R, $rom, MAXROMSIZE+1);
  188. close(R);
  189. defined($filesize) and $filesize >= 3 or die "Cannot get first 3 bytes of file\n";
  190. print "$filesize bytes read\n" if $opts{'v'};
  191. &pcipnpheaders(\$rom);
  192. &undiheaders(\$rom);
  193. &checksum(\$rom);
  194. &writerom($ARGV[0], \$rom);
  195. }
  196. # Main routine. See how we were called and behave accordingly
  197. if ($0 =~ m:modrom(\.pl)?$:) {
  198. &modrom();
  199. } else {
  200. &makerom();
  201. }
  202. exit(0);