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.

ROM.pm 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. package Option::ROM;
  2. # Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License as
  6. # published by the Free Software Foundation; either version 2 of the
  7. # License, or any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. # 02110-1301, USA.
  18. =head1 NAME
  19. Option::ROM - Option ROM manipulation
  20. =head1 SYNOPSIS
  21. use Option::ROM;
  22. # Load a ROM image
  23. my $rom = new Option::ROM;
  24. $rom->load ( "rtl8139.rom" );
  25. # Modify the PCI device ID
  26. $rom->pci_header->{device_id} = 0x1234;
  27. $rom->fix_checksum();
  28. # Write ROM image out to a new file
  29. $rom->save ( "rtl8139-modified.rom" );
  30. =head1 DESCRIPTION
  31. C<Option::ROM> provides a mechanism for manipulating Option ROM
  32. images.
  33. =head1 METHODS
  34. =cut
  35. ##############################################################################
  36. #
  37. # Option::ROM::Fields
  38. #
  39. ##############################################################################
  40. package Option::ROM::Fields;
  41. use strict;
  42. use warnings;
  43. use Carp;
  44. use bytes;
  45. sub TIEHASH {
  46. my $class = shift;
  47. my $self = shift;
  48. bless $self, $class;
  49. return $self;
  50. }
  51. sub FETCH {
  52. my $self = shift;
  53. my $key = shift;
  54. return undef unless $self->EXISTS ( $key );
  55. my $raw = substr ( ${$self->{data}},
  56. ( $self->{offset} + $self->{fields}->{$key}->{offset} ),
  57. $self->{fields}->{$key}->{length} );
  58. my $unpack = ( ref $self->{fields}->{$key}->{unpack} ?
  59. $self->{fields}->{$key}->{unpack} :
  60. sub { unpack ( $self->{fields}->{$key}->{pack}, shift ); } );
  61. return &$unpack ( $raw );
  62. }
  63. sub STORE {
  64. my $self = shift;
  65. my $key = shift;
  66. my $value = shift;
  67. croak "Nonexistent field \"$key\"" unless $self->EXISTS ( $key );
  68. my $pack = ( ref $self->{fields}->{$key}->{pack} ?
  69. $self->{fields}->{$key}->{pack} :
  70. sub { pack ( $self->{fields}->{$key}->{pack}, shift ); } );
  71. my $raw = &$pack ( $value );
  72. substr ( ${$self->{data}},
  73. ( $self->{offset} + $self->{fields}->{$key}->{offset} ),
  74. $self->{fields}->{$key}->{length} ) = $raw;
  75. }
  76. sub DELETE {
  77. my $self = shift;
  78. my $key = shift;
  79. $self->STORE ( $key, 0 );
  80. }
  81. sub CLEAR {
  82. my $self = shift;
  83. foreach my $key ( keys %{$self->{fields}} ) {
  84. $self->DELETE ( $key );
  85. }
  86. }
  87. sub EXISTS {
  88. my $self = shift;
  89. my $key = shift;
  90. return ( exists $self->{fields}->{$key} &&
  91. ( ( $self->{fields}->{$key}->{offset} +
  92. $self->{fields}->{$key}->{length} ) <= $self->{length} ) );
  93. }
  94. sub FIRSTKEY {
  95. my $self = shift;
  96. keys %{$self->{fields}};
  97. return each %{$self->{fields}};
  98. }
  99. sub NEXTKEY {
  100. my $self = shift;
  101. my $lastkey = shift;
  102. return each %{$self->{fields}};
  103. }
  104. sub SCALAR {
  105. my $self = shift;
  106. return 1;
  107. }
  108. sub UNTIE {
  109. my $self = shift;
  110. }
  111. sub DESTROY {
  112. my $self = shift;
  113. }
  114. sub checksum {
  115. my $self = shift;
  116. my $raw = substr ( ${$self->{data}}, $self->{offset}, $self->{length} );
  117. return unpack ( "%8C*", $raw );
  118. }
  119. ##############################################################################
  120. #
  121. # Option::ROM
  122. #
  123. ##############################################################################
  124. package Option::ROM;
  125. use strict;
  126. use warnings;
  127. use Carp;
  128. use bytes;
  129. use Exporter 'import';
  130. use constant ROM_SIGNATURE => 0xaa55;
  131. use constant PCI_SIGNATURE => 'PCIR';
  132. use constant PCI_LAST_IMAGE => 0x80;
  133. use constant PNP_SIGNATURE => '$PnP';
  134. use constant IPXE_SIGNATURE => 'iPXE';
  135. our @EXPORT_OK = qw ( ROM_SIGNATURE PCI_SIGNATURE PCI_LAST_IMAGE
  136. PNP_SIGNATURE IPXE_SIGNATURE );
  137. our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
  138. use constant JMP_SHORT => 0xeb;
  139. use constant JMP_NEAR => 0xe9;
  140. use constant CALL_NEAR => 0xe8;
  141. sub pack_init {
  142. my $dest = shift;
  143. # Always create a near jump; it's simpler
  144. if ( $dest ) {
  145. return pack ( "CS", JMP_NEAR, ( $dest - 6 ) );
  146. } else {
  147. return pack ( "CS", 0, 0 );
  148. }
  149. }
  150. sub unpack_init {
  151. my $instr = shift;
  152. # Accept both short and near jumps
  153. my $jump = unpack ( "C", $instr );
  154. if ( $jump == JMP_SHORT ) {
  155. my $offset = unpack ( "xC", $instr );
  156. return ( $offset + 5 );
  157. } elsif ( $jump == JMP_NEAR ) {
  158. my $offset = unpack ( "xS", $instr );
  159. return ( $offset + 6 );
  160. } elsif ( $jump == CALL_NEAR ) {
  161. my $offset = unpack ( "xS", $instr );
  162. return ( $offset + 6 );
  163. } elsif ( $jump == 0 ) {
  164. return 0;
  165. } else {
  166. croak "Unrecognised jump instruction in init vector\n";
  167. }
  168. }
  169. =pod
  170. =item C<< new () >>
  171. Construct a new C<Option::ROM> object.
  172. =cut
  173. sub new {
  174. my $class = shift;
  175. my $hash = {};
  176. tie %$hash, "Option::ROM::Fields", {
  177. data => undef,
  178. offset => 0x00,
  179. length => 0x20,
  180. fields => {
  181. signature => { offset => 0x00, length => 0x02, pack => "S" },
  182. length => { offset => 0x02, length => 0x01, pack => "C" },
  183. # "init" is part of a jump instruction
  184. init => { offset => 0x03, length => 0x03,
  185. pack => \&pack_init, unpack => \&unpack_init },
  186. checksum => { offset => 0x06, length => 0x01, pack => "C" },
  187. ipxe_header => { offset => 0x10, length => 0x02, pack => "S" },
  188. bofm_header => { offset => 0x14, length => 0x02, pack => "S" },
  189. undi_header => { offset => 0x16, length => 0x02, pack => "S" },
  190. pci_header => { offset => 0x18, length => 0x02, pack => "S" },
  191. pnp_header => { offset => 0x1a, length => 0x02, pack => "S" },
  192. },
  193. };
  194. bless $hash, $class;
  195. return $hash;
  196. }
  197. =pod
  198. =item C<< set ( $data ) >>
  199. Set option ROM contents.
  200. =cut
  201. sub set {
  202. my $hash = shift;
  203. my $self = tied(%$hash);
  204. my $data = shift;
  205. # Store data
  206. $self->{data} = \$data;
  207. # Split out any data belonging to the next image
  208. delete $self->{next_image};
  209. my $pci_header = $hash->pci_header();
  210. if ( ( defined $pci_header ) &&
  211. ( ! ( $pci_header->{last_image} & PCI_LAST_IMAGE ) ) ) {
  212. my $length = ( $pci_header->{image_length} * 512 );
  213. my $remainder = substr ( $data, $length );
  214. $data = substr ( $data, 0, $length );
  215. $self->{next_image} = new Option::ROM;
  216. $self->{next_image}->set ( $remainder );
  217. }
  218. }
  219. =pod
  220. =item C<< get () >>
  221. Get option ROM contents.
  222. =cut
  223. sub get {
  224. my $hash = shift;
  225. my $self = tied(%$hash);
  226. my $data = ${$self->{data}};
  227. $data .= $self->{next_image}->get() if $self->{next_image};
  228. return $data;
  229. }
  230. =pod
  231. =item C<< load ( $filename ) >>
  232. Load option ROM contents from the file C<$filename>.
  233. =cut
  234. sub load {
  235. my $hash = shift;
  236. my $self = tied(%$hash);
  237. my $filename = shift;
  238. $self->{filename} = $filename;
  239. open my $fh, "<$filename"
  240. or croak "Cannot open $filename for reading: $!";
  241. read $fh, my $data, -s $fh;
  242. $hash->set ( $data );
  243. close $fh;
  244. }
  245. =pod
  246. =item C<< save ( [ $filename ] ) >>
  247. Write the ROM data back out to the file C<$filename>. If C<$filename>
  248. is omitted, the file used in the call to C<load()> will be used.
  249. =cut
  250. sub save {
  251. my $hash = shift;
  252. my $self = tied(%$hash);
  253. my $filename = shift;
  254. $filename ||= $self->{filename};
  255. open my $fh, ">$filename"
  256. or croak "Cannot open $filename for writing: $!";
  257. my $data = $hash->get();
  258. print $fh $data;
  259. close $fh;
  260. }
  261. =pod
  262. =item C<< length () >>
  263. Length of option ROM data. This is the length of the file, not the
  264. length from the ROM header length field.
  265. =cut
  266. sub length {
  267. my $hash = shift;
  268. my $self = tied(%$hash);
  269. return length ${$self->{data}};
  270. }
  271. =pod
  272. =item C<< pci_header () >>
  273. Return a C<Option::ROM::PCI> object representing the ROM's PCI header,
  274. if present.
  275. =cut
  276. sub pci_header {
  277. my $hash = shift;
  278. my $self = tied(%$hash);
  279. my $offset = $hash->{pci_header};
  280. return undef unless $offset != 0;
  281. return Option::ROM::PCI->new ( $self->{data}, $offset );
  282. }
  283. =pod
  284. =item C<< pnp_header () >>
  285. Return a C<Option::ROM::PnP> object representing the ROM's PnP header,
  286. if present.
  287. =cut
  288. sub pnp_header {
  289. my $hash = shift;
  290. my $self = tied(%$hash);
  291. my $offset = $hash->{pnp_header};
  292. return undef unless $offset != 0;
  293. return Option::ROM::PnP->new ( $self->{data}, $offset );
  294. }
  295. =pod
  296. =item C<< undi_header () >>
  297. Return a C<Option::ROM::UNDI> object representing the ROM's UNDI header,
  298. if present.
  299. =cut
  300. sub undi_header {
  301. my $hash = shift;
  302. my $self = tied(%$hash);
  303. my $offset = $hash->{undi_header};
  304. return undef unless $offset != 0;
  305. return Option::ROM::UNDI->new ( $self->{data}, $offset );
  306. }
  307. =pod
  308. =item C<< ipxe_header () >>
  309. Return a C<Option::ROM::iPXE> object representing the ROM's iPXE
  310. header, if present.
  311. =cut
  312. sub ipxe_header {
  313. my $hash = shift;
  314. my $self = tied(%$hash);
  315. my $offset = $hash->{ipxe_header};
  316. return undef unless $offset != 0;
  317. return Option::ROM::iPXE->new ( $self->{data}, $offset );
  318. }
  319. =pod
  320. =item C<< next_image () >>
  321. Return a C<Option::ROM> object representing the next image within the
  322. ROM, if present.
  323. =cut
  324. sub next_image {
  325. my $hash = shift;
  326. my $self = tied(%$hash);
  327. return $self->{next_image};
  328. }
  329. =pod
  330. =item C<< checksum () >>
  331. Calculate the byte checksum of the ROM.
  332. =cut
  333. sub checksum {
  334. my $hash = shift;
  335. my $self = tied(%$hash);
  336. my $raw = substr ( ${$self->{data}}, 0, ( $hash->{length} * 512 ) );
  337. return unpack ( "%8C*", $raw );
  338. }
  339. =pod
  340. =item C<< fix_checksum () >>
  341. Fix the byte checksum of the ROM.
  342. =cut
  343. sub fix_checksum {
  344. my $hash = shift;
  345. my $self = tied(%$hash);
  346. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  347. }
  348. ##############################################################################
  349. #
  350. # Option::ROM::PCI
  351. #
  352. ##############################################################################
  353. package Option::ROM::PCI;
  354. use strict;
  355. use warnings;
  356. use Carp;
  357. use bytes;
  358. sub new {
  359. my $class = shift;
  360. my $data = shift;
  361. my $offset = shift;
  362. my $hash = {};
  363. tie %$hash, "Option::ROM::Fields", {
  364. data => $data,
  365. offset => $offset,
  366. length => 0x0c,
  367. fields => {
  368. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  369. vendor_id => { offset => 0x04, length => 0x02, pack => "S" },
  370. device_id => { offset => 0x06, length => 0x02, pack => "S" },
  371. device_list => { offset => 0x08, length => 0x02, pack => "S" },
  372. struct_length => { offset => 0x0a, length => 0x02, pack => "S" },
  373. struct_revision =>{ offset => 0x0c, length => 0x01, pack => "C" },
  374. prog_intf => { offset => 0x0d, length => 0x01, pack => "C" },
  375. sub_class => { offset => 0x0e, length => 0x01, pack => "C" },
  376. base_class => { offset => 0x0f, length => 0x01, pack => "C" },
  377. image_length => { offset => 0x10, length => 0x02, pack => "S" },
  378. revision => { offset => 0x12, length => 0x02, pack => "S" },
  379. code_type => { offset => 0x14, length => 0x01, pack => "C" },
  380. last_image => { offset => 0x15, length => 0x01, pack => "C" },
  381. runtime_length => { offset => 0x16, length => 0x02, pack => "S" },
  382. conf_header => { offset => 0x18, length => 0x02, pack => "S" },
  383. clp_entry => { offset => 0x1a, length => 0x02, pack => "S" },
  384. },
  385. };
  386. bless $hash, $class;
  387. # Retrieve true length of structure
  388. my $self = tied ( %$hash );
  389. $self->{length} = $hash->{struct_length};
  390. return $hash;
  391. }
  392. sub device_list {
  393. my $hash = shift;
  394. my $self = tied(%$hash);
  395. my $device_list = $hash->{device_list};
  396. return undef unless $device_list;
  397. my @ids;
  398. my $offset = ( $self->{offset} + $device_list );
  399. while ( 1 ) {
  400. my $raw = substr ( ${$self->{data}}, $offset, 2 );
  401. my $id = unpack ( "S", $raw );
  402. last unless $id;
  403. push @ids, $id;
  404. $offset += 2;
  405. }
  406. return @ids;
  407. }
  408. ##############################################################################
  409. #
  410. # Option::ROM::PnP
  411. #
  412. ##############################################################################
  413. package Option::ROM::PnP;
  414. use strict;
  415. use warnings;
  416. use Carp;
  417. use bytes;
  418. sub new {
  419. my $class = shift;
  420. my $data = shift;
  421. my $offset = shift;
  422. my $hash = {};
  423. tie %$hash, "Option::ROM::Fields", {
  424. data => $data,
  425. offset => $offset,
  426. length => 0x06,
  427. fields => {
  428. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  429. struct_revision =>{ offset => 0x04, length => 0x01, pack => "C" },
  430. struct_length => { offset => 0x05, length => 0x01, pack => "C" },
  431. checksum => { offset => 0x09, length => 0x01, pack => "C" },
  432. manufacturer => { offset => 0x0e, length => 0x02, pack => "S" },
  433. product => { offset => 0x10, length => 0x02, pack => "S" },
  434. bcv => { offset => 0x16, length => 0x02, pack => "S" },
  435. bdv => { offset => 0x18, length => 0x02, pack => "S" },
  436. bev => { offset => 0x1a, length => 0x02, pack => "S" },
  437. },
  438. };
  439. bless $hash, $class;
  440. # Retrieve true length of structure
  441. my $self = tied ( %$hash );
  442. $self->{length} = ( $hash->{struct_length} * 16 );
  443. return $hash;
  444. }
  445. sub checksum {
  446. my $hash = shift;
  447. my $self = tied(%$hash);
  448. return $self->checksum();
  449. }
  450. sub fix_checksum {
  451. my $hash = shift;
  452. my $self = tied(%$hash);
  453. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  454. }
  455. sub manufacturer {
  456. my $hash = shift;
  457. my $self = tied(%$hash);
  458. my $manufacturer = $hash->{manufacturer};
  459. return undef unless $manufacturer;
  460. my $raw = substr ( ${$self->{data}}, $manufacturer );
  461. return unpack ( "Z*", $raw );
  462. }
  463. sub product {
  464. my $hash = shift;
  465. my $self = tied(%$hash);
  466. my $product = $hash->{product};
  467. return undef unless $product;
  468. my $raw = substr ( ${$self->{data}}, $product );
  469. return unpack ( "Z*", $raw );
  470. }
  471. ##############################################################################
  472. #
  473. # Option::ROM::UNDI
  474. #
  475. ##############################################################################
  476. package Option::ROM::UNDI;
  477. use strict;
  478. use warnings;
  479. use Carp;
  480. use bytes;
  481. sub new {
  482. my $class = shift;
  483. my $data = shift;
  484. my $offset = shift;
  485. my $hash = {};
  486. tie %$hash, "Option::ROM::Fields", {
  487. data => $data,
  488. offset => $offset,
  489. length => 0x16,
  490. fields => {
  491. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  492. struct_length => { offset => 0x04, length => 0x01, pack => "C" },
  493. checksum => { offset => 0x05, length => 0x01, pack => "C" },
  494. struct_revision =>{ offset => 0x06, length => 0x01, pack => "C" },
  495. version_revision =>{ offset => 0x07, length => 0x01, pack => "C" },
  496. version_minor => { offset => 0x08, length => 0x01, pack => "C" },
  497. version_major => { offset => 0x09, length => 0x01, pack => "C" },
  498. loader_entry => { offset => 0x0a, length => 0x02, pack => "S" },
  499. stack_size => { offset => 0x0c, length => 0x02, pack => "S" },
  500. data_size => { offset => 0x0e, length => 0x02, pack => "S" },
  501. code_size => { offset => 0x10, length => 0x02, pack => "S" },
  502. bus_type => { offset => 0x12, length => 0x04, pack => "a4" },
  503. },
  504. };
  505. bless $hash, $class;
  506. # Retrieve true length of structure
  507. my $self = tied ( %$hash );
  508. $self->{length} = $hash->{struct_length};
  509. return $hash;
  510. }
  511. sub checksum {
  512. my $hash = shift;
  513. my $self = tied(%$hash);
  514. return $self->checksum();
  515. }
  516. sub fix_checksum {
  517. my $hash = shift;
  518. my $self = tied(%$hash);
  519. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  520. }
  521. ##############################################################################
  522. #
  523. # Option::ROM::iPXE
  524. #
  525. ##############################################################################
  526. package Option::ROM::iPXE;
  527. use strict;
  528. use warnings;
  529. use Carp;
  530. use bytes;
  531. sub new {
  532. my $class = shift;
  533. my $data = shift;
  534. my $offset = shift;
  535. my $hash = {};
  536. tie %$hash, "Option::ROM::Fields", {
  537. data => $data,
  538. offset => $offset,
  539. length => 0x06,
  540. fields => {
  541. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  542. struct_length => { offset => 0x04, length => 0x01, pack => "C" },
  543. checksum => { offset => 0x05, length => 0x01, pack => "C" },
  544. shrunk_length => { offset => 0x06, length => 0x01, pack => "C" },
  545. build_id => { offset => 0x08, length => 0x04, pack => "L" },
  546. },
  547. };
  548. bless $hash, $class;
  549. # Retrieve true length of structure
  550. my $self = tied ( %$hash );
  551. $self->{length} = $hash->{struct_length};
  552. return $hash;
  553. }
  554. sub checksum {
  555. my $hash = shift;
  556. my $self = tied(%$hash);
  557. return $self->checksum();
  558. }
  559. sub fix_checksum {
  560. my $hash = shift;
  561. my $self = tied(%$hash);
  562. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  563. }
  564. 1;