]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[util] Allow Option::ROM to access multiple ROM images
authorMichael Brown <mcb30@ipxe.org>
Sun, 10 Jun 2012 17:23:24 +0000 (18:23 +0100)
committerMichael Brown <mcb30@ipxe.org>
Tue, 12 Jun 2012 10:36:20 +0000 (11:36 +0100)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/util/Option/ROM.pm
src/util/disrom.pl
src/util/fixrom.pl

index 9fea4d344eab786d31985045985a53c8817f9643..b2b94c3e23b52b3a3420da13babb324ac9dd311a 100644 (file)
@@ -169,9 +169,11 @@ use Exporter 'import';
 
 use constant ROM_SIGNATURE => 0xaa55;
 use constant PCI_SIGNATURE => 'PCIR';
+use constant PCI_LAST_IMAGE => 0x80;
 use constant PNP_SIGNATURE => '$PnP';
 
-our @EXPORT_OK = qw ( ROM_SIGNATURE PCI_SIGNATURE PNP_SIGNATURE );
+our @EXPORT_OK = qw ( ROM_SIGNATURE PCI_SIGNATURE PCI_LAST_IMAGE
+                     PNP_SIGNATURE );
 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
 
 use constant JMP_SHORT => 0xeb;
@@ -241,6 +243,53 @@ sub new {
 
 =pod
 
+=item C<< set ( $data ) >>
+
+Set option ROM contents.
+
+=cut
+
+sub set {
+  my $hash = shift;
+  my $self = tied(%$hash);
+  my $data = shift;
+
+  # Store data
+  $self->{data} = \$data;
+
+  # Split out any data belonging to the next image
+  delete $self->{next_image};
+  my $length = ( $hash->{length} * 512 );
+  my $pci_header = $hash->pci_header();
+  if ( ( $length < length $data ) &&
+       ( defined $pci_header ) &&
+       ( ! ( $pci_header->{last_image} & PCI_LAST_IMAGE ) ) ) {
+    my $remainder = substr ( $data, $length );
+    $data = substr ( $data, 0, $length );
+    $self->{next_image} = new Option::ROM;
+    $self->{next_image}->set ( $remainder );
+  }
+}
+
+=pod
+
+=item C<< get () >>
+
+Get option ROM contents.
+
+=cut
+
+sub get {
+  my $hash = shift;
+  my $self = tied(%$hash);
+
+  my $data = ${$self->{data}};
+  $data .= $self->{next_image}->get() if $self->{next_image};
+  return $data;
+}
+
+=pod
+
 =item C<< load ( $filename ) >>
 
 Load option ROM contents from the file C<$filename>.
@@ -256,8 +305,8 @@ sub load {
 
   open my $fh, "<$filename"
       or croak "Cannot open $filename for reading: $!";
-  read $fh, my $data, ( 128 * 1024 ); # 128kB is theoretical max size
-  $self->{data} = \$data;
+  read $fh, my $data, -s $fh;
+  $hash->set ( $data );
   close $fh;
 }
 
@@ -279,7 +328,8 @@ sub save {
 
   open my $fh, ">$filename"
       or croak "Cannot open $filename for writing: $!";
-  print $fh ${$self->{data}};
+  my $data = $hash->get();
+  print $fh $data;
   close $fh;
 }
 
@@ -339,6 +389,22 @@ sub pnp_header {
 
 =pod
 
+=item C<< next_image () >>
+
+Return a C<Option::ROM> object representing the next image within the
+ROM, if present.
+
+=cut
+
+sub next_image {
+  my $hash = shift;
+  my $self = tied(%$hash);
+
+  return $self->{next_image};
+}
+
+=pod
+
 =item C<< checksum () >>
 
 Calculate the byte checksum of the ROM.
index 1fb4cc3cb80daeb382c13e098c1856b2901d9d01..aff972eaa2e46268c7adae64296a15bc623d99e8 100755 (executable)
@@ -27,55 +27,61 @@ my $romfile = shift || "-";
 my $rom = new Option::ROM;
 $rom->load ( $romfile );
 
-die "Not an option ROM image\n"
-    unless $rom->{signature} == ROM_SIGNATURE;
+do {
 
-my $romlength = ( $rom->{length} * 512 );
-my $filelength = $rom->length;
-die "ROM image truncated (is $filelength, should be $romlength)\n"
-    if $filelength < $romlength;
+  die "Not an option ROM image\n"
+      unless $rom->{signature} == ROM_SIGNATURE;
 
-printf "ROM header:\n\n";
-printf "  %-16s 0x%02x (%d)\n", "Length:", $rom->{length}, ( $rom->{length} * 512 );
-printf "  %-16s 0x%02x (%s0x%02x)\n", "Checksum:", $rom->{checksum},
-       ( ( $rom->checksum == 0 ) ? "" : "INCORRECT: " ), $rom->checksum;
-printf "  %-16s 0x%04x\n", "Init:", $rom->{init};
-printf "  %-16s 0x%04x\n", "UNDI header:", $rom->{undi_header};
-printf "  %-16s 0x%04x\n", "PCI header:", $rom->{pci_header};
-printf "  %-16s 0x%04x\n", "PnP header:", $rom->{pnp_header};
-printf "\n";
+  my $romlength = ( $rom->{length} * 512 );
+  my $filelength = $rom->length;
+  die "ROM image truncated (is $filelength, should be $romlength)\n"
+      if $filelength < $romlength;
 
-my $pci = $rom->pci_header();
-if ( $pci ) {
-  printf "PCI header:\n\n";
-  printf "  %-16s %s\n", "Signature:", $pci->{signature};
-  printf "  %-16s 0x%04x\n", "Vendor ID:", $pci->{vendor_id};
-  printf "  %-16s 0x%04x\n", "Device ID:", $pci->{device_id};
-  printf "  %-16s 0x%02x%02x%02x\n", "Device class:",
-        $pci->{base_class}, $pci->{sub_class}, $pci->{prog_intf};
-  printf "  %-16s 0x%04x (%d)\n", "Image length:",
-        $pci->{image_length}, ( $pci->{image_length} * 512 );
-  printf "  %-16s 0x%04x (%d)\n", "Runtime length:",
-        $pci->{runtime_length}, ( $pci->{runtime_length} * 512 );
-  if ( exists $pci->{conf_header} ) {
-    printf "  %-16s 0x%04x\n", "Config header:", $pci->{conf_header};
-    printf "  %-16s 0x%04x\n", "CLP entry:", $pci->{clp_entry};
-  }
+  printf "ROM header:\n\n";
+  printf "  %-16s 0x%02x (%d)\n", "Length:",
+        $rom->{length}, ( $rom->{length} * 512 );
+  printf "  %-16s 0x%02x (%s0x%02x)\n", "Checksum:", $rom->{checksum},
+        ( ( $rom->checksum == 0 ) ? "" : "INCORRECT: " ), $rom->checksum;
+  printf "  %-16s 0x%04x\n", "Init:", $rom->{init};
+  printf "  %-16s 0x%04x\n", "UNDI header:", $rom->{undi_header};
+  printf "  %-16s 0x%04x\n", "PCI header:", $rom->{pci_header};
+  printf "  %-16s 0x%04x\n", "PnP header:", $rom->{pnp_header};
   printf "\n";
-}
 
-my $pnp = $rom->pnp_header();
-if ( $pnp ) {
-  printf "PnP header:\n\n";
-  printf "  %-16s %s\n", "Signature:", $pnp->{signature};
-  printf "  %-16s 0x%02x (%s0x%02x)\n", "Checksum:", $pnp->{checksum},
-        ( ( $pnp->checksum == 0 ) ? "" : "INCORRECT: " ), $pnp->checksum;
-  printf "  %-16s 0x%04x \"%s\"\n", "Manufacturer:",
-        $pnp->{manufacturer}, $pnp->manufacturer;
-  printf "  %-16s 0x%04x \"%s\"\n", "Product:",
-        $pnp->{product}, $pnp->product;
-  printf "  %-16s 0x%04x\n", "BCV:", $pnp->{bcv};
-  printf "  %-16s 0x%04x\n", "BDV:", $pnp->{bdv};
-  printf "  %-16s 0x%04x\n", "BEV:", $pnp->{bev};
-  printf "\n";
-}
+  my $pci = $rom->pci_header();
+  if ( $pci ) {
+    printf "PCI header:\n\n";
+    printf "  %-16s %s\n", "Signature:", $pci->{signature};
+    printf "  %-16s 0x%04x\n", "Vendor ID:", $pci->{vendor_id};
+    printf "  %-16s 0x%04x\n", "Device ID:", $pci->{device_id};
+    printf "  %-16s 0x%02x%02x%02x\n", "Device class:",
+          $pci->{base_class}, $pci->{sub_class}, $pci->{prog_intf};
+    printf "  %-16s 0x%04x (%d)\n", "Image length:",
+          $pci->{image_length}, ( $pci->{image_length} * 512 );
+    printf "  %-16s 0x%04x (%d)\n", "Runtime length:",
+          $pci->{runtime_length}, ( $pci->{runtime_length} * 512 );
+    printf "  %-16s 0x%02x\n", "Code type:", $pci->{code_type};
+    if ( exists $pci->{conf_header} ) {
+      printf "  %-16s 0x%04x\n", "Config header:", $pci->{conf_header};
+      printf "  %-16s 0x%04x\n", "CLP entry:", $pci->{clp_entry};
+    }
+    printf "\n";
+  }
+
+  my $pnp = $rom->pnp_header();
+  if ( $pnp ) {
+    printf "PnP header:\n\n";
+    printf "  %-16s %s\n", "Signature:", $pnp->{signature};
+    printf "  %-16s 0x%02x (%s0x%02x)\n", "Checksum:", $pnp->{checksum},
+          ( ( $pnp->checksum == 0 ) ? "" : "INCORRECT: " ), $pnp->checksum;
+    printf "  %-16s 0x%04x \"%s\"\n", "Manufacturer:",
+          $pnp->{manufacturer}, $pnp->manufacturer;
+    printf "  %-16s 0x%04x \"%s\"\n", "Product:",
+          $pnp->{product}, $pnp->product;
+    printf "  %-16s 0x%04x\n", "BCV:", $pnp->{bcv};
+    printf "  %-16s 0x%04x\n", "BDV:", $pnp->{bdv};
+    printf "  %-16s 0x%04x\n", "BEV:", $pnp->{bev};
+    printf "\n";
+  }
+
+} while ( $rom = $rom->next_image );
index c3a31f41e37cc304f5a84548bd49c984bd399543..88f8cb83292289dab984eb1d95a36dce9fd24ccd 100755 (executable)
@@ -28,7 +28,11 @@ my @romfiles = @ARGV;
 foreach my $romfile ( @romfiles ) {
   my $rom = new Option::ROM;
   $rom->load ( $romfile );
-  $rom->pnp_header->fix_checksum() if $rom->pnp_header;
-  $rom->fix_checksum();
+  my $image = $rom;
+  while ( $image ) {
+    $image->pnp_header->fix_checksum() if $image->pnp_header;
+    $image->fix_checksum();
+    $image = $image->next_image();
+  }
   $rom->save ( $romfile );
 }