#!/usr/bin/perl
#
# fixupimage : take an ELF kernel and add a.out headers to suit SYS_UBOOT

$image = $ARGV[0];
$SIZEBIN='m68k-linux-size';
$CAT='cat';
$GENAOUT='genaout';

sub sys(@)
{
  # works like system() but dies with a diagnostic if the return code
  # is non-zero. It's really just to avoid cluttering code with error
  # handling when you don't really expect errors anyway :->
  system(@_) == 0 or die "sys @_ failed: $?";
}

if ($#ARGV != 0)
{
   print "fixupimage imagefile : convert the ELF kernel image imagefile\n"
       . "to a format acceptable to the NetBSD bootloader.\n";
   exit(1);
}

open(SIZE,"$SIZEBIN $image |") or die "$SIZEBIN $image failed: $!";
$_ = <SIZE>; # first line is headers and irrelevant
$_ = <SIZE>; # second line is text data bss dec hex fname

($txt, $data, $bss, $dec, $hex, $fname) = split;

sys("$GENAOUT $dec 0 0 > $image.hdrs");
sys("m68k-linux-objcopy -O binary $image $image.bin");
sys("$CAT $image.hdrs $image.bin > $image.aout");
unlink("$image.bin","$image.hdrs");
