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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 $length = ( $hash->{length} * 512 );
  210. my $pci_header = $hash->pci_header();
  211. if ( ( $length < length $data ) &&
  212. ( defined $pci_header ) &&
  213. ( ! ( $pci_header->{last_image} & PCI_LAST_IMAGE ) ) ) {
  214. my $remainder = substr ( $data, $length );
  215. $data = substr ( $data, 0, $length );
  216. $self->{next_image} = new Option::ROM;
  217. $self->{next_image}->set ( $remainder );
  218. }
  219. }
  220. =pod
  221. =item C<< get () >>
  222. Get option ROM contents.
  223. =cut
  224. sub get {
  225. my $hash = shift;
  226. my $self = tied(%$hash);
  227. my $data = ${$self->{data}};
  228. $data .= $self->{next_image}->get() if $self->{next_image};
  229. return $data;
  230. }
  231. =pod
  232. =item C<< load ( $filename ) >>
  233. Load option ROM contents from the file C<$filename>.
  234. =cut
  235. sub load {
  236. my $hash = shift;
  237. my $self = tied(%$hash);
  238. my $filename = shift;
  239. $self->{filename} = $filename;
  240. open my $fh, "<$filename"
  241. or croak "Cannot open $filename for reading: $!";
  242. read $fh, my $data, -s $fh;
  243. $hash->set ( $data );
  244. close $fh;
  245. }
  246. =pod
  247. =item C<< save ( [ $filename ] ) >>
  248. Write the ROM data back out to the file C<$filename>. If C<$filename>
  249. is omitted, the file used in the call to C<load()> will be used.
  250. =cut
  251. sub save {
  252. my $hash = shift;
  253. my $self = tied(%$hash);
  254. my $filename = shift;
  255. $filename ||= $self->{filename};
  256. open my $fh, ">$filename"
  257. or croak "Cannot open $filename for writing: $!";
  258. my $data = $hash->get();
  259. print $fh $data;
  260. close $fh;
  261. }
  262. =pod
  263. =item C<< length () >>
  264. Length of option ROM data. This is the length of the file, not the
  265. length from the ROM header length field.
  266. =cut
  267. sub length {
  268. my $hash = shift;
  269. my $self = tied(%$hash);
  270. return length ${$self->{data}};
  271. }
  272. =pod
  273. =item C<< pci_header () >>
  274. Return a C<Option::ROM::PCI> object representing the ROM's PCI header,
  275. if present.
  276. =cut
  277. sub pci_header {
  278. my $hash = shift;
  279. my $self = tied(%$hash);
  280. my $offset = $hash->{pci_header};
  281. return undef unless $offset != 0;
  282. return Option::ROM::PCI->new ( $self->{data}, $offset );
  283. }
  284. =pod
  285. =item C<< pnp_header () >>
  286. Return a C<Option::ROM::PnP> object representing the ROM's PnP header,
  287. if present.
  288. =cut
  289. sub pnp_header {
  290. my $hash = shift;
  291. my $self = tied(%$hash);
  292. my $offset = $hash->{pnp_header};
  293. return undef unless $offset != 0;
  294. return Option::ROM::PnP->new ( $self->{data}, $offset );
  295. }
  296. =pod
  297. =item C<< undi_header () >>
  298. Return a C<Option::ROM::UNDI> object representing the ROM's UNDI header,
  299. if present.
  300. =cut
  301. sub undi_header {
  302. my $hash = shift;
  303. my $self = tied(%$hash);
  304. my $offset = $hash->{undi_header};
  305. return undef unless $offset != 0;
  306. return Option::ROM::UNDI->new ( $self->{data}, $offset );
  307. }
  308. =pod
  309. =item C<< ipxe_header () >>
  310. Return a C<Option::ROM::iPXE> object representing the ROM's iPXE
  311. header, if present.
  312. =cut
  313. sub ipxe_header {
  314. my $hash = shift;
  315. my $self = tied(%$hash);
  316. my $offset = $hash->{ipxe_header};
  317. return undef unless $offset != 0;
  318. return Option::ROM::iPXE->new ( $self->{data}, $offset );
  319. }
  320. =pod
  321. =item C<< next_image () >>
  322. Return a C<Option::ROM> object representing the next image within the
  323. ROM, if present.
  324. =cut
  325. sub next_image {
  326. my $hash = shift;
  327. my $self = tied(%$hash);
  328. return $self->{next_image};
  329. }
  330. =pod
  331. =item C<< checksum () >>
  332. Calculate the byte checksum of the ROM.
  333. =cut
  334. sub checksum {
  335. my $hash = shift;
  336. my $self = tied(%$hash);
  337. my $raw = substr ( ${$self->{data}}, 0, ( $hash->{length} * 512 ) );
  338. return unpack ( "%8C*", $raw );
  339. }
  340. =pod
  341. =item C<< fix_checksum () >>
  342. Fix the byte checksum of the ROM.
  343. =cut
  344. sub fix_checksum {
  345. my $hash = shift;
  346. my $self = tied(%$hash);
  347. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  348. }
  349. ##############################################################################
  350. #
  351. # Option::ROM::PCI
  352. #
  353. ##############################################################################
  354. package Option::ROM::PCI;
  355. use strict;
  356. use warnings;
  357. use Carp;
  358. use bytes;
  359. sub new {
  360. my $class = shift;
  361. my $data = shift;
  362. my $offset = shift;
  363. my $hash = {};
  364. tie %$hash, "Option::ROM::Fields", {
  365. data => $data,
  366. offset => $offset,
  367. length => 0x0c,
  368. fields => {
  369. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  370. vendor_id => { offset => 0x04, length => 0x02, pack => "S" },
  371. device_id => { offset => 0x06, length => 0x02, pack => "S" },
  372. device_list => { offset => 0x08, length => 0x02, pack => "S" },
  373. struct_length => { offset => 0x0a, length => 0x02, pack => "S" },
  374. struct_revision =>{ offset => 0x0c, length => 0x01, pack => "C" },
  375. base_class => { offset => 0x0d, length => 0x01, pack => "C" },
  376. sub_class => { offset => 0x0e, length => 0x01, pack => "C" },
  377. prog_intf => { offset => 0x0f, length => 0x01, pack => "C" },
  378. image_length => { offset => 0x10, length => 0x02, pack => "S" },
  379. revision => { offset => 0x12, length => 0x02, pack => "S" },
  380. code_type => { offset => 0x14, length => 0x01, pack => "C" },
  381. last_image => { offset => 0x15, length => 0x01, pack => "C" },
  382. runtime_length => { offset => 0x16, length => 0x02, pack => "S" },
  383. conf_header => { offset => 0x18, length => 0x02, pack => "S" },
  384. clp_entry => { offset => 0x1a, length => 0x02, pack => "S" },
  385. },
  386. };
  387. bless $hash, $class;
  388. # Retrieve true length of structure
  389. my $self = tied ( %$hash );
  390. $self->{length} = $hash->{struct_length};
  391. return $hash;
  392. }
  393. ##############################################################################
  394. #
  395. # Option::ROM::PnP
  396. #
  397. ##############################################################################
  398. package Option::ROM::PnP;
  399. use strict;
  400. use warnings;
  401. use Carp;
  402. use bytes;
  403. sub new {
  404. my $class = shift;
  405. my $data = shift;
  406. my $offset = shift;
  407. my $hash = {};
  408. tie %$hash, "Option::ROM::Fields", {
  409. data => $data,
  410. offset => $offset,
  411. length => 0x06,
  412. fields => {
  413. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  414. struct_revision =>{ offset => 0x04, length => 0x01, pack => "C" },
  415. struct_length => { offset => 0x05, length => 0x01, pack => "C" },
  416. checksum => { offset => 0x09, length => 0x01, pack => "C" },
  417. manufacturer => { offset => 0x0e, length => 0x02, pack => "S" },
  418. product => { offset => 0x10, length => 0x02, pack => "S" },
  419. bcv => { offset => 0x16, length => 0x02, pack => "S" },
  420. bdv => { offset => 0x18, length => 0x02, pack => "S" },
  421. bev => { offset => 0x1a, length => 0x02, pack => "S" },
  422. },
  423. };
  424. bless $hash, $class;
  425. # Retrieve true length of structure
  426. my $self = tied ( %$hash );
  427. $self->{length} = ( $hash->{struct_length} * 16 );
  428. return $hash;
  429. }
  430. sub checksum {
  431. my $hash = shift;
  432. my $self = tied(%$hash);
  433. return $self->checksum();
  434. }
  435. sub fix_checksum {
  436. my $hash = shift;
  437. my $self = tied(%$hash);
  438. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  439. }
  440. sub manufacturer {
  441. my $hash = shift;
  442. my $self = tied(%$hash);
  443. my $manufacturer = $hash->{manufacturer};
  444. return undef unless $manufacturer;
  445. my $raw = substr ( ${$self->{data}}, $manufacturer );
  446. return unpack ( "Z*", $raw );
  447. }
  448. sub product {
  449. my $hash = shift;
  450. my $self = tied(%$hash);
  451. my $product = $hash->{product};
  452. return undef unless $product;
  453. my $raw = substr ( ${$self->{data}}, $product );
  454. return unpack ( "Z*", $raw );
  455. }
  456. ##############################################################################
  457. #
  458. # Option::ROM::UNDI
  459. #
  460. ##############################################################################
  461. package Option::ROM::UNDI;
  462. use strict;
  463. use warnings;
  464. use Carp;
  465. use bytes;
  466. sub new {
  467. my $class = shift;
  468. my $data = shift;
  469. my $offset = shift;
  470. my $hash = {};
  471. tie %$hash, "Option::ROM::Fields", {
  472. data => $data,
  473. offset => $offset,
  474. length => 0x16,
  475. fields => {
  476. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  477. struct_length => { offset => 0x04, length => 0x01, pack => "C" },
  478. checksum => { offset => 0x05, length => 0x01, pack => "C" },
  479. struct_revision =>{ offset => 0x06, length => 0x01, pack => "C" },
  480. version_revision =>{ offset => 0x07, length => 0x01, pack => "C" },
  481. version_minor => { offset => 0x08, length => 0x01, pack => "C" },
  482. version_major => { offset => 0x09, length => 0x01, pack => "C" },
  483. loader_entry => { offset => 0x0a, length => 0x02, pack => "S" },
  484. stack_size => { offset => 0x0c, length => 0x02, pack => "S" },
  485. data_size => { offset => 0x0e, length => 0x02, pack => "S" },
  486. code_size => { offset => 0x10, length => 0x02, pack => "S" },
  487. bus_type => { offset => 0x12, length => 0x04, pack => "a4" },
  488. },
  489. };
  490. bless $hash, $class;
  491. # Retrieve true length of structure
  492. my $self = tied ( %$hash );
  493. $self->{length} = $hash->{struct_length};
  494. return $hash;
  495. }
  496. sub checksum {
  497. my $hash = shift;
  498. my $self = tied(%$hash);
  499. return $self->checksum();
  500. }
  501. sub fix_checksum {
  502. my $hash = shift;
  503. my $self = tied(%$hash);
  504. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  505. }
  506. ##############################################################################
  507. #
  508. # Option::ROM::iPXE
  509. #
  510. ##############################################################################
  511. package Option::ROM::iPXE;
  512. use strict;
  513. use warnings;
  514. use Carp;
  515. use bytes;
  516. sub new {
  517. my $class = shift;
  518. my $data = shift;
  519. my $offset = shift;
  520. my $hash = {};
  521. tie %$hash, "Option::ROM::Fields", {
  522. data => $data,
  523. offset => $offset,
  524. length => 0x06,
  525. fields => {
  526. signature => { offset => 0x00, length => 0x04, pack => "a4" },
  527. struct_length => { offset => 0x04, length => 0x01, pack => "C" },
  528. checksum => { offset => 0x05, length => 0x01, pack => "C" },
  529. shrunk_length => { offset => 0x06, length => 0x01, pack => "C" },
  530. build_id => { offset => 0x08, length => 0x04, pack => "L" },
  531. },
  532. };
  533. bless $hash, $class;
  534. # Retrieve true length of structure
  535. my $self = tied ( %$hash );
  536. $self->{length} = $hash->{struct_length};
  537. return $hash;
  538. }
  539. sub checksum {
  540. my $hash = shift;
  541. my $self = tied(%$hash);
  542. return $self->checksum();
  543. }
  544. sub fix_checksum {
  545. my $hash = shift;
  546. my $self = tied(%$hash);
  547. $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
  548. }
  549. 1;