aboutsummaryrefslogtreecommitdiff
path: root/libjava/classpath/java/beans/BeanDescriptor.java
blob: b4bc6870d24f2c71613cffd5401862012e04904e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* java.beans.BeanDescriptor
   Copyright (C) 1998, 2004 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.beans;

/**
 ** BeanDescriptor describes general information about a Bean, plus
 ** stores the Bean's Class and it's customizer's Class.<P>
 **
 ** @author John Keiser
 ** @since JDK1.1
 ** @version 1.1.0, 31 May 1998
 **/

public class BeanDescriptor extends FeatureDescriptor {
	Class<?> beanClass;
	Class<?> customizerClass;

	/** Create a new BeanDescriptor with the given beanClass and
	 ** no customizer class.
	 ** @param beanClass the class of the Bean.
	 **/
	public BeanDescriptor(Class<?> beanClass) {
		this(beanClass,null);
	}

	/** Create a new BeanDescriptor with the given bean class and
	 ** customizer class.
	 ** @param beanClass the class of the Bean.
	 ** @param customizerClass the class of the Bean's Customizer.
	 **/
	public BeanDescriptor(Class<?> beanClass, Class<?> customizerClass) {
		this.beanClass = beanClass;
		this.customizerClass = customizerClass;

		// Set the FeatureDescriptor programmatic name.
		String name = beanClass.getName();
		int lastInd = name.lastIndexOf('.');
		if (lastInd != -1)
		  name = name.substring(lastInd + 1);

		setName(name);
	}

	/** Get the Bean's class. **/
	public Class<?> getBeanClass() {
		return beanClass;
	}

	/** Get the Bean's customizer's class. **/
	public Class<?> getCustomizerClass() {
		return customizerClass;
	}
}
> 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
package Option::ROM;

# Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

=head1 NAME

Option::ROM - Option ROM manipulation

=head1 SYNOPSIS

    use Option::ROM;

    # Load a ROM image
    my $rom = new Option::ROM;
    $rom->load ( "rtl8139.rom" );

    # Modify the PCI device ID
    $rom->pci_header->{device_id} = 0x1234;
    $rom->fix_checksum();

    # Write ROM image out to a new file
    $rom->save ( "rtl8139-modified.rom" );

=head1 DESCRIPTION

C<Option::ROM> provides a mechanism for manipulating Option ROM
images.

=head1 METHODS

=cut

##############################################################################
#
# Option::ROM::Fields
#
##############################################################################

package Option::ROM::Fields;

use strict;
use warnings;
use Carp;
use bytes;

sub TIEHASH {
  my $class = shift;
  my $self = shift;

  bless $self, $class;
  return $self;
}

sub FETCH {
  my $self = shift;
  my $key = shift;

  return undef unless $self->EXISTS ( $key );
  my $raw = substr ( ${$self->{data}},
		     ( $self->{offset} + $self->{fields}->{$key}->{offset} ),
		     $self->{fields}->{$key}->{length} );
  my $unpack = ( ref $self->{fields}->{$key}->{unpack} ?
		 $self->{fields}->{$key}->{unpack} :
		 sub { unpack ( $self->{fields}->{$key}->{pack}, shift ); } );
  return &$unpack ( $raw );
}

sub STORE {
  my $self = shift;
  my $key = shift;
  my $value = shift;

  croak "Nonexistent field \"$key\"" unless $self->EXISTS ( $key );
  my $pack = ( ref $self->{fields}->{$key}->{pack} ?
	       $self->{fields}->{$key}->{pack} :
	       sub { pack ( $self->{fields}->{$key}->{pack}, shift ); } );
  my $raw = &$pack ( $value );
  substr ( ${$self->{data}},
	   ( $self->{offset} + $self->{fields}->{$key}->{offset} ),
	   $self->{fields}->{$key}->{length} ) = $raw;
}

sub DELETE {
  my $self = shift;
  my $key = shift;

  $self->STORE ( $key, 0 );
}

sub CLEAR {
  my $self = shift;

  foreach my $key ( keys %{$self->{fields}} ) {
    $self->DELETE ( $key );
  }
}

sub EXISTS {
  my $self = shift;
  my $key = shift;

  return ( exists $self->{fields}->{$key} &&
	   ( ( $self->{fields}->{$key}->{offset} +
	       $self->{fields}->{$key}->{length} ) <= $self->{length} ) &&
	   ( ! defined $self->{fields}->{$key}->{check} ||
	     &{$self->{fields}->{$key}->{check}} ( $self, $key ) ) );
}

sub FIRSTKEY {
  my $self = shift;

  keys %{$self->{fields}};
  return each %{$self->{fields}};
}

sub NEXTKEY {
  my $self = shift;
  my $lastkey = shift;

  return each %{$self->{fields}};
}

sub SCALAR {
  my $self = shift;

  return 1;
}

sub UNTIE {
  my $self = shift;
}

sub DESTROY {
  my $self = shift;
}

sub checksum {
  my $self = shift;

  my $raw = substr ( ${$self->{data}}, $self->{offset}, $self->{length} );
  return unpack ( "%8C*", $raw );
}

##############################################################################
#
# Option::ROM
#
##############################################################################

package Option::ROM;

use strict;
use warnings;
use Carp;
use bytes;
use Exporter 'import';

use constant ROM_SIGNATURE => 0xaa55;
use constant PCI_SIGNATURE => 'PCIR';
use constant PCI_LAST_IMAGE => 0x80;
use constant PNP_SIGNATURE => '$PnP';
use constant UNDI_SIGNATURE => 'UNDI';
use constant IPXE_SIGNATURE => 'iPXE';
use constant EFI_SIGNATURE => 0x00000ef1;

our @EXPORT_OK = qw ( ROM_SIGNATURE PCI_SIGNATURE PCI_LAST_IMAGE
		      PNP_SIGNATURE UNDI_SIGNATURE IPXE_SIGNATURE EFI_SIGNATURE );
our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );

use constant JMP_SHORT => 0xeb;
use constant JMP_NEAR => 0xe9;
use constant CALL_NEAR => 0xe8;

sub pack_init {
  my $dest = shift;

  # Always create a near jump; it's simpler
  if ( $dest ) {
    return pack ( "CS", JMP_NEAR, ( $dest - 6 ) );
  } else {
    return pack ( "CS", 0, 0 );
  }
}

sub unpack_init {
  my $instr = shift;

  # Accept both short and near jumps
  my $jump = unpack ( "C", $instr );
  if ( $jump == JMP_SHORT ) {
    my $offset = unpack ( "xC", $instr );
    return ( $offset + 5 );
  } elsif ( $jump == JMP_NEAR ) {
    my $offset = unpack ( "xS", $instr );
    return ( $offset + 6 );
  } elsif ( $jump == CALL_NEAR ) {
    my $offset = unpack ( "xS", $instr );
    return ( $offset + 6 );
  } elsif ( $jump == 0 ) {
    return 0;
  } else {
    carp "Unrecognised jump instruction in init vector\n";
    return 0;
  }
}

sub check_pcat_rom {
  my $self = shift;
  my $key = shift;

  my $pci = $self->{rom}->pci_header ();

  return ! defined $pci || $pci->{code_type} == 0x00;
}

=pod

=item C<< new () >>

Construct a new C<Option::ROM> object.

=cut

sub new {
  my $class = shift;

  my $hash = {};
  tie %$hash, "Option::ROM::Fields", {
    rom => $hash, # ROM object itself
    data => undef,
    offset => 0x00,
    length => 0x20,
    file_offset => 0x0,
    fields => {
      signature =>	{ offset => 0x00, length => 0x02, pack => "S" },
      length =>		{ offset => 0x02, length => 0x01, pack => "C" },
      # "init" is part of a jump instruction
      init =>		{ offset => 0x03, length => 0x03,
			  pack => \&pack_init, unpack => \&unpack_init,
			  check => \&check_pcat_rom },
      checksum =>	{ offset => 0x06, length => 0x01, pack => "C",
			  check => \&check_pcat_rom },
      ipxe_header =>	{ offset => 0x10, length => 0x02, pack => "S",
			  check => \&check_pcat_rom },
      bofm_header =>	{ offset => 0x14, length => 0x02, pack => "S",
			  check => \&check_pcat_rom },
      undi_header =>	{ offset => 0x16, length => 0x02, pack => "S",
			  check => \&check_pcat_rom },
      pci_header =>	{ offset => 0x18, length => 0x02, pack => "S" },
      pnp_header =>	{ offset => 0x1a, length => 0x02, pack => "S",
			  check => \&check_pcat_rom },
    },
  };
  bless $hash, $class;
  return $hash;
}

=pod

=item C<< set ( $data [, $file_offset ] ) >>

Set option ROM contents, optionally sets original file offset.

=cut

sub set {
  my $hash = shift;
  my $self = tied(%$hash);
  my $data = shift;
  my $file_offset = shift // 0x0;

  # Store data
  $self->{data} = \$data;
  $self->{file_offset} = $file_offset;

  # Split out any data belonging to the next image
  delete $self->{next_image};
  my $pci_header = $hash->pci_header();
  if ( ( defined $pci_header ) &&
       ( ! ( $pci_header->{last_image} & PCI_LAST_IMAGE ) ) ) {
    my $length = ( $pci_header->{image_length} * 512 );
    my $remainder = substr ( $data, $length );
    $data = substr ( $data, 0, $length );
    $self->{next_image} = new Option::ROM;
    $self->{next_image}->set ( $remainder, $self->{file_offset} + $length );
  }
}

=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>.

=cut

sub load {
  my $hash = shift;
  my $self = tied(%$hash);
  my $filename = shift;

  $self->{filename} = $filename;

  open my $fh, "<$filename"
      or croak "Cannot open $filename for reading: $!";
  binmode $fh;
  read $fh, my $data, -s $fh;
  $hash->set ( $data );
  close $fh;
}

=pod

=item C<< save ( [ $filename ] ) >>

Write the ROM data back out to the file C<$filename>.  If C<$filename>
is omitted, the file used in the call to C<load()> will be used.

=cut

sub save {
  my $hash = shift;
  my $self = tied(%$hash);
  my $filename = shift;

  $filename ||= $self->{filename};

  open my $fh, ">$filename"
      or croak "Cannot open $filename for writing: $!";
  my $data = $hash->get();
  binmode $fh;
  print $fh $data;
  close $fh;
}

=pod

=item C<< length () >>

Length of option ROM data.  This is the length of the file, not the
length from the ROM header length field.

=cut

sub length {
  my $hash = shift;
  my $self = tied(%$hash);

  return length ${$self->{data}};
}

=pod

=item C<< pci_header () >>

Return a C<Option::ROM::PCI> object representing the ROM's PCI header,
if present.

=cut

sub pci_header {
  my $hash = shift;
  my $self = tied(%$hash);

  my $offset = $hash->{pci_header};
  return undef unless $offset;

  return Option::ROM::PCI->new ( $self, $offset );
}

=pod

=item C<< pnp_header () >>

Return a C<Option::ROM::PnP> object representing the ROM's PnP header,
if present.

=cut

sub pnp_header {
  my $hash = shift;
  my $self = tied(%$hash);

  my $offset = $hash->{pnp_header};
  return undef unless $offset;

  return Option::ROM::PnP->new ( $self, $offset );
}

=pod

=item C<< undi_header () >>

Return a C<Option::ROM::UNDI> object representing the ROM's UNDI header,
if present.

=cut

sub undi_header {
  my $hash = shift;
  my $self = tied(%$hash);

  my $offset = $hash->{undi_header};
  return undef unless $offset;

  return Option::ROM::UNDI->new ( $self, $offset );
}

=pod

=item C<< ipxe_header () >>

Return a C<Option::ROM::iPXE> object representing the ROM's iPXE
header, if present.

=cut

sub ipxe_header {
  my $hash = shift;
  my $self = tied(%$hash);

  my $offset = $hash->{ipxe_header};
  return undef unless $offset;

  return Option::ROM::iPXE->new ( $self, $offset );
}

=pod

=item C<< efi_header () >>

Return a C<Option::ROM::EFI> object representing the ROM's EFI header,
if present.

=cut

sub efi_header {
  my $hash = shift;
  my $self = tied(%$hash);

  my $pci = $hash->pci_header ();
  return undef unless defined $pci;

  return Option::ROM::EFI->new ( $self, $pci );
}

=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.

=cut

sub checksum {
  my $hash = shift;
  my $self = tied(%$hash);

  my $raw = substr ( ${$self->{data}}, 0, ( $hash->{length} * 512 ) );
  return unpack ( "%8C*", $raw );
}

=pod

=item C<< fix_checksum () >>

Fix the byte checksum of the ROM.

=cut

sub fix_checksum {
  my $hash = shift;
  my $self = tied(%$hash);

  return unless ( exists $hash->{checksum} );
  $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
}

=pod

=item C<< file_offset () >>

Get file offset of image.

=cut

sub file_offset {
  my $hash = shift;
  my $self = tied(%$hash);

  return $self->{file_offset};
}

##############################################################################
#
# Option::ROM::PCI
#
##############################################################################

package Option::ROM::PCI;

use strict;
use warnings;
use Carp;
use bytes;

sub new {
  my $class = shift;
  my $rom = shift;
  my $offset = shift;

  my $hash = {};
  tie %$hash, "Option::ROM::Fields", {
    rom => $rom,
    data => $rom->{data},
    offset => $offset,
    length => 0x0c,
    fields => {
      signature =>	{ offset => 0x00, length => 0x04, pack => "a4" },
      vendor_id =>	{ offset => 0x04, length => 0x02, pack => "S" },
      device_id =>	{ offset => 0x06, length => 0x02, pack => "S" },
      device_list =>	{ offset => 0x08, length => 0x02, pack => "S" },
      struct_length =>	{ offset => 0x0a, length => 0x02, pack => "S" },
      struct_revision =>{ offset => 0x0c, length => 0x01, pack => "C" },
      prog_intf => 	{ offset => 0x0d, length => 0x01, pack => "C" },
      sub_class => 	{ offset => 0x0e, length => 0x01, pack => "C" },
      base_class => 	{ offset => 0x0f, length => 0x01, pack => "C" },
      image_length =>	{ offset => 0x10, length => 0x02, pack => "S" },
      revision =>	{ offset => 0x12, length => 0x02, pack => "S" },
      code_type => 	{ offset => 0x14, length => 0x01, pack => "C" },
      last_image => 	{ offset => 0x15, length => 0x01, pack => "C" },
      runtime_length =>	{ offset => 0x16, length => 0x02, pack => "S" },
      conf_header =>	{ offset => 0x18, length => 0x02, pack => "S" },
      clp_entry =>	{ offset => 0x1a, length => 0x02, pack => "S" },
    },
  };
  bless $hash, $class;

  my $self = tied ( %$hash );
  my $length = $rom->{rom}->length ();

  return undef unless ( $offset + $self->{length} <= $length &&
			$hash->{signature} eq Option::ROM::PCI_SIGNATURE &&
			$offset + $hash->{struct_length} <= $length );

  # Retrieve true length of structure
  $self->{length} = $hash->{struct_length};

  return $hash;
}

sub device_list {
  my $hash = shift;
  my $self = tied(%$hash);

  my $device_list = $hash->{device_list};
  return undef unless $device_list;

  my @ids;
  my $offset = ( $self->{offset} + $device_list );
  while ( 1 ) {
    my $raw = substr ( ${$self->{data}}, $offset, 2 );
    my $id = unpack ( "S", $raw );
    last unless $id;
    push @ids, $id;
    $offset += 2;
  }

  return @ids;
}

##############################################################################
#
# Option::ROM::PnP
#
##############################################################################

package Option::ROM::PnP;

use strict;
use warnings;
use Carp;
use bytes;

sub new {
  my $class = shift;
  my $rom = shift;
  my $offset = shift;

  my $hash = {};
  tie %$hash, "Option::ROM::Fields", {
    rom => $rom,
    data => $rom->{data},
    offset => $offset,
    length => 0x06,
    fields => {
      signature =>	{ offset => 0x00, length => 0x04, pack => "a4" },
      struct_revision =>{ offset => 0x04, length => 0x01, pack => "C" },
      struct_length =>	{ offset => 0x05, length => 0x01, pack => "C" },
      checksum =>	{ offset => 0x09, length => 0x01, pack => "C" },
      manufacturer =>	{ offset => 0x0e, length => 0x02, pack => "S" },
      product =>	{ offset => 0x10, length => 0x02, pack => "S" },
      bcv =>		{ offset => 0x16, length => 0x02, pack => "S" },
      bdv =>		{ offset => 0x18, length => 0x02, pack => "S" },
      bev =>		{ offset => 0x1a, length => 0x02, pack => "S" },
    },
  };
  bless $hash, $class;

  my $self = tied ( %$hash );
  my $length = $rom->{rom}->length ();

  return undef unless ( $offset + $self->{length} <= $length &&
			$hash->{signature} eq Option::ROM::PNP_SIGNATURE &&
			$offset + $hash->{struct_length} * 16 <= $length );

  # Retrieve true length of structure
  $self->{length} = ( $hash->{struct_length} * 16 );

  return $hash;
}

sub checksum {
  my $hash = shift;
  my $self = tied(%$hash);

  return $self->checksum();
}

sub fix_checksum {
  my $hash = shift;
  my $self = tied(%$hash);

  $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
}

sub manufacturer {
  my $hash = shift;
  my $self = tied(%$hash);

  my $manufacturer = $hash->{manufacturer};
  return undef unless $manufacturer;

  my $raw = substr ( ${$self->{data}}, $manufacturer );
  return unpack ( "Z*", $raw );
}

sub product {
  my $hash = shift;
  my $self = tied(%$hash);

  my $product = $hash->{product};
  return undef unless $product;

  my $raw = substr ( ${$self->{data}}, $product );
  return unpack ( "Z*", $raw );
}

##############################################################################
#
# Option::ROM::UNDI
#
##############################################################################

package Option::ROM::UNDI;

use strict;
use warnings;
use Carp;
use bytes;

sub new {
  my $class = shift;
  my $rom = shift;
  my $offset = shift;

  my $hash = {};
  tie %$hash, "Option::ROM::Fields", {
    rom => $rom,
    data => $rom->{data},
    offset => $offset,
    length => 0x16,
    fields => {
      signature =>	{ offset => 0x00, length => 0x04, pack => "a4" },
      struct_length =>	{ offset => 0x04, length => 0x01, pack => "C" },
      checksum =>	{ offset => 0x05, length => 0x01, pack => "C" },
      struct_revision =>{ offset => 0x06, length => 0x01, pack => "C" },
      version_revision =>{ offset => 0x07, length => 0x01, pack => "C" },
      version_minor =>	{ offset => 0x08, length => 0x01, pack => "C" },
      version_major =>	{ offset => 0x09, length => 0x01, pack => "C" },
      loader_entry =>	{ offset => 0x0a, length => 0x02, pack => "S" },
      stack_size =>	{ offset => 0x0c, length => 0x02, pack => "S" },
      data_size =>	{ offset => 0x0e, length => 0x02, pack => "S" },
      code_size =>	{ offset => 0x10, length => 0x02, pack => "S" },
      bus_type =>	{ offset => 0x12, length => 0x04, pack => "a4" },
    },
  };
  bless $hash, $class;

  my $self = tied ( %$hash );
  my $length = $rom->{rom}->length ();

  return undef unless ( $offset + $self->{length} <= $length &&
			$hash->{signature} eq Option::ROM::UNDI_SIGNATURE &&
			$offset + $hash->{struct_length} <= $length );

  # Retrieve true length of structure
  $self->{length} = $hash->{struct_length};

  return $hash;
}

sub checksum {
  my $hash = shift;
  my $self = tied(%$hash);

  return $self->checksum();
}

sub fix_checksum {
  my $hash = shift;
  my $self = tied(%$hash);

  $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
}

##############################################################################
#
# Option::ROM::iPXE
#
##############################################################################

package Option::ROM::iPXE;

use strict;
use warnings;
use Carp;
use bytes;

sub new {
  my $class = shift;
  my $rom = shift;
  my $offset = shift;

  my $hash = {};
  tie %$hash, "Option::ROM::Fields", {
    rom => $rom,
    data => $rom->{data},
    offset => $offset,
    length => 0x06,
    fields => {
      signature =>	{ offset => 0x00, length => 0x04, pack => "a4" },
      struct_length =>	{ offset => 0x04, length => 0x01, pack => "C" },
      checksum =>	{ offset => 0x05, length => 0x01, pack => "C" },
      shrunk_length =>	{ offset => 0x06, length => 0x01, pack => "C" },
      build_id =>	{ offset => 0x08, length => 0x04, pack => "L" },
    },
  };
  bless $hash, $class;

  my $self = tied ( %$hash );
  my $length = $rom->{rom}->length ();

  return undef unless ( $offset + $self->{length} <= $length &&
			$hash->{signature} eq Option::ROM::IPXE_SIGNATURE &&
			$offset + $hash->{struct_length} <= $length );

  # Retrieve true length of structure
  $self->{length} = $hash->{struct_length};

  return $hash;
}

sub checksum {
  my $hash = shift;
  my $self = tied(%$hash);

  return $self->checksum();
}

sub fix_checksum {
  my $hash = shift;
  my $self = tied(%$hash);

  $hash->{checksum} = ( ( $hash->{checksum} - $hash->checksum() ) & 0xff );
}

##############################################################################
#
# Option::ROM::EFI
#
##############################################################################

package Option::ROM::EFI;

use strict;
use warnings;
use Carp;
use bytes;

sub new {
  my $class = shift;
  my $rom = shift;
  my $pci = shift;

  my $hash = {};
  tie %$hash, "Option::ROM::Fields", {
    rom => $rom,
    data => $rom->{data},
    offset => 0x00,
    length => 0x18,
    fields => {
      signature =>		{ offset => 0x00, length => 0x02, pack => "S" },
      init_size =>		{ offset => 0x02, length => 0x02, pack => "S" },
      efi_signature =>		{ offset => 0x04, length => 0x04, pack => "L" },
      efi_subsystem =>		{ offset => 0x08, length => 0x02, pack => "S" },
      efi_machine_type =>	{ offset => 0x0a, length => 0x02, pack => "S" },
      compression_type =>	{ offset => 0x0c, length => 0x02, pack => "S" },
      efi_image_offset =>	{ offset => 0x16, length => 0x02, pack => "S" },
    },
  };
  bless $hash, $class;

  my $self = tied ( %$hash );

  return undef unless ( $hash->{efi_signature} == Option::ROM::EFI_SIGNATURE &&
			$pci->{code_type} == 0x03 );

  return $hash;
}

1;