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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. base_class => { offset => 0x0d, length => 0x01, pack => "C" },
  375. sub_class => { offset => 0x0e, length => 0x01, pack => "C" },
  376. prog_intf => { 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. ##############################################################################
  393. #
  394. # Option::ROM::PnP
  395. #
  396. ##############################################################################
  397. package Option::ROM::PnP;
  398. use strict;
  399. use warnings;
  400. use Carp;
  401. use bytes;
  402. sub new {
  403. my $class = shift;
  404. my $data = shift;
  405. my $offset = shift;
  406. my $hash = {};
  407. tie %$hash, "Option::ROM::Fields", {
  408. data => $data,
  409. offset => $offset,
  410. length => 0x06,
  411. fields => {
  412. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  413. struct_revision =>{ offset => 0x04, length => 0x01, pack => "C" },
  414. struct_length => { offset => 0x05, length => 0x01, pack => "C" },
  415. checksum => { offset => 0x09, length => 0x01, pack => "C" },
  416. manufacturer => { offset => 0x0e, length => 0x02, pack => "S" },
  417. product => { offset => 0x10, length => 0x02, pack => "S" },
  418. bcv => { offset => 0x16, length => 0x02, pack => "S" },
  419. bdv => { offset => 0x18, length => 0x02, pack => "S" },
  420. bev => { offset => 0x1a, length => 0x02, pack => "S" },
  421. },
  422. };
  423. bless $hash, $class;
  424. # Retrieve true length of structure
  425. my $self = tied ( %$hash );
  426. $self->{length} = ( $hash->{struct_length} * 16 );
  427. return $hash;
  428. }
  429. sub checksum {
  430. my $hash = shift;
  431. my $self = tied(%$hash);
  432. return $self->checksum();
  433. }
  434. sub fix_checksum {
  435. my $hash = shift;
  436. my $self = tied(%$hash);
  437. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  438. }
  439. sub manufacturer {
  440. my $hash = shift;
  441. my $self = tied(%$hash);
  442. my $manufacturer = $hash->{manufacturer};
  443. return undef unless $manufacturer;
  444. my $raw = substr ( ${$self->{data}}, $manufacturer );
  445. return unpack ( "Z*", $raw );
  446. }
  447. sub product {
  448. my $hash = shift;
  449. my $self = tied(%$hash);
  450. my $product = $hash->{product};
  451. return undef unless $product;
  452. my $raw = substr ( ${$self->{data}}, $product );
  453. return unpack ( "Z*", $raw );
  454. }
  455. ##############################################################################
  456. #
  457. # Option::ROM::UNDI
  458. #
  459. ##############################################################################
  460. package Option::ROM::UNDI;
  461. use strict;
  462. use warnings;
  463. use Carp;
  464. use bytes;
  465. sub new {
  466. my $class = shift;
  467. my $data = shift;
  468. my $offset = shift;
  469. my $hash = {};
  470. tie %$hash, "Option::ROM::Fields", {
  471. data => $data,
  472. offset => $offset,
  473. length => 0x16,
  474. fields => {
  475. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  476. struct_length => { offset => 0x04, length => 0x01, pack => "C" },
  477. checksum => { offset => 0x05, length => 0x01, pack => "C" },
  478. struct_revision =>{ offset => 0x06, length => 0x01, pack => "C" },
  479. version_revision =>{ offset => 0x07, length => 0x01, pack => "C" },
  480. version_minor => { offset => 0x08, length => 0x01, pack => "C" },
  481. version_major => { offset => 0x09, length => 0x01, pack => "C" },
  482. loader_entry => { offset => 0x0a, length => 0x02, pack => "S" },
  483. stack_size => { offset => 0x0c, length => 0x02, pack => "S" },
  484. data_size => { offset => 0x0e, length => 0x02, pack => "S" },
  485. code_size => { offset => 0x10, length => 0x02, pack => "S" },
  486. bus_type => { offset => 0x12, length => 0x04, pack => "a4" },
  487. },
  488. };
  489. bless $hash, $class;
  490. # Retrieve true length of structure
  491. my $self = tied ( %$hash );
  492. $self->{length} = $hash->{struct_length};
  493. return $hash;
  494. }
  495. sub checksum {
  496. my $hash = shift;
  497. my $self = tied(%$hash);
  498. return $self->checksum();
  499. }
  500. sub fix_checksum {
  501. my $hash = shift;
  502. my $self = tied(%$hash);
  503. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  504. }
  505. ##############################################################################
  506. #
  507. # Option::ROM::iPXE
  508. #
  509. ##############################################################################
  510. package Option::ROM::iPXE;
  511. use strict;
  512. use warnings;
  513. use Carp;
  514. use bytes;
  515. sub new {
  516. my $class = shift;
  517. my $data = shift;
  518. my $offset = shift;
  519. my $hash = {};
  520. tie %$hash, "Option::ROM::Fields", {
  521. data => $data,
  522. offset => $offset,
  523. length => 0x06,
  524. fields => {
  525. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  526. struct_length => { offset => 0x04, length => 0x01, pack => "C" },
  527. checksum => { offset => 0x05, length => 0x01, pack => "C" },
  528. shrunk_length => { offset => 0x06, length => 0x01, pack => "C" },
  529. build_id => { offset => 0x08, length => 0x04, pack => "L" },
  530. },
  531. };
  532. bless $hash, $class;
  533. # Retrieve true length of structure
  534. my $self = tied ( %$hash );
  535. $self->{length} = $hash->{struct_length};
  536. return $hash;
  537. }
  538. sub checksum {
  539. my $hash = shift;
  540. my $self = tied(%$hash);
  541. return $self->checksum();
  542. }
  543. sub fix_checksum {
  544. my $hash = shift;
  545. my $self = tied(%$hash);
  546. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  547. }
  548. 1;