#!/usr/bin/perl

# Copyright (c) 2008 Motorola, Inc. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# 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.

use strict;
use warnings FATAL => 'all';

# Usage: add_bootimage_header <arch> <image_type> <kernel> <kernel_reloc> <kernel_entry> <rd> OR
#        add_bootimage_header <arch> <image_type> <kernel> <kernel_reloc> <kernel_entry>

binmode STDOUT;

# Below is the type 1 and version 2 of the bootimage_header.
# Change the version if extra parameters are added at the end.
#  This is backwards compatible. Also change header_length.
# Change the type if a new structure is defined.
#
# struct bootimage_header {
#   unsigned int magic;
#   unsigned int type;
#   unsigned int version;
#   unsigned int header_length;
#   unsigned int kernel_reloc_address;
#   unsigned int kernel_entry_address;
#   unsigned int kernel_size;
#   unsigned int initrd_reloc_address;
#   unsigned int initrd_size;
#   // New in version 2
#   unsigned int architecture;
#   unsigned int image_type;
# };

print pack("V", 0xFFAA55FF);  # magic
print pack("V", 0x00000001);  # type
print pack("V", 0x00000002);  # version
print pack("V", 0x0000002C);  # header_length

my $kernel_size = -s $ARGV[2];
my $kernel_reloc = oct($ARGV[3]);
my $kernel_entry = oct($ARGV[4]);
print pack("V", $kernel_reloc); # kernel_reloc_address
print pack("V", $kernel_entry); # kernel_entry_address
print pack("V", $kernel_size);  # kernel_size

my $rd_size = -s $ARGV[5];
print pack("V", 0xB0000000);  # initrd_reloc_address
print pack("V", $rd_size);    # initrd_size

print pack("V", $ARGV[0]);    # architecture
print pack("V", $ARGV[1]);    # image_type
