1#!/usr/bin/perl -w
2#
3# Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License as
7# published by the Free Software Foundation; either version 2 of the
8# License, or any later version.
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19use strict;
20use warnings;
21
22use FindBin;
23use lib "$FindBin::Bin";
24use Option::ROM qw ( :all );
25
26sub merge_entry_points {
27  my $baserom_entry = \shift;
28  my $rom_entry = \shift;
29  my $offset = shift;
30
31  if ( $$rom_entry ) {
32    my $old_entry = $$baserom_entry;
33    $$baserom_entry = ( $offset + $$rom_entry );
34    $$rom_entry = $old_entry;
35  }
36}
37
38my @romfiles = @ARGV;
39my @roms = map { my $rom = new Option::ROM; $rom->load($_); $rom } @romfiles;
40
41my $baserom = shift @roms;
42my $offset = $baserom->length;
43
44foreach my $rom ( @roms ) {
45
46  # Update base length
47  $baserom->{length} += $rom->{length};
48
49  # Merge initialisation entry point
50  merge_entry_points ( $baserom->{init}, $rom->{init}, $offset );
51
52  # Merge BOFM header
53  merge_entry_points ( $baserom->{bofm_header}, $rom->{bofm_header}, $offset );
54
55  # Update PCI header, if present in both
56  my $baserom_pci = $baserom->pci_header;
57  my $rom_pci = $rom->pci_header;
58  if ( $baserom_pci && $rom_pci ) {
59
60    # Update PCI lengths
61    $baserom_pci->{image_length} += $rom_pci->{image_length};
62    if ( exists $baserom_pci->{runtime_length} ) {
63      if ( exists $rom_pci->{runtime_length} ) {
64	$baserom_pci->{runtime_length} += $rom_pci->{runtime_length};
65      } else {
66	$baserom_pci->{runtime_length} += $rom_pci->{image_length};
67      }
68    }
69
70    # Merge CLP entry point
71    if ( exists ( $baserom_pci->{clp_entry} ) &&
72	 exists ( $rom_pci->{clp_entry} ) ) {
73      merge_entry_points ( $baserom_pci->{clp_entry}, $rom_pci->{clp_entry},
74			   $offset );
75    }
76  }
77
78  # Update PnP header, if present in both
79  my $baserom_pnp = $baserom->pnp_header;
80  my $rom_pnp = $rom->pnp_header;
81  if ( $baserom_pnp && $rom_pnp ) {
82    merge_entry_points ( $baserom_pnp->{bcv}, $rom_pnp->{bcv}, $offset );
83    merge_entry_points ( $baserom_pnp->{bdv}, $rom_pnp->{bdv}, $offset );
84    merge_entry_points ( $baserom_pnp->{bev}, $rom_pnp->{bev}, $offset );
85  }
86
87  # Fix checksum for this ROM segment
88  $rom->fix_checksum();
89
90  $offset += $rom->length;
91}
92
93$baserom->pnp_header->fix_checksum() if $baserom->pnp_header;
94$baserom->fix_checksum();
95$baserom->save ( "-" );
96foreach my $rom ( @roms ) {
97  $rom->save ( "-" );
98}
99