/* $NetBSD: ncdcs.c,v 1.1 2009/08/18 20:22:20 skrll Exp $ */
/*
** Program to prepare an ELF file for download to an IBM Network
** Station 300 or 1000 running the NCD firmware.
**
** This may also work for other loaders based on NCD code.
**
** The program expects a signature and some marker fields a couple
** bytes into the image itself, where the checksum gets patched in.
** The checksum itself is a 16bit CRC over the ELF header, the ELF
** program header, and the image portion of the ELF file. The start
** value for the CRC is 0xffff, and no final xor is performed.
**
** Usage: ncdcs <infile> [outfile]
** e.g. ncdcs zImage zImage.ncd
**
** Copyright 2002 Jochen Roth
** NetBSD changes copyright 2003 John Gordon
**
** 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.
**
*/
unsigned short get_be_16(unsigned char *p)
{
unsigned short x = 0;
x = (unsigned short)p[0];
x = (x<<8) + (unsigned short)p[1];
return(x);
}
unsigned long get_be_32(unsigned char *p)
{
unsigned long x = 0;
x = (unsigned long)p[0];
x = (x<<8) + (unsigned long)p[1];
x = (x<<8) + (unsigned long)p[2];
x = (x<<8) + (unsigned long)p[3];
return(x);
}
if(memcmp(buf, "\177ELF", 4)) {
fprintf(stderr, "%s: not an ELF file\n", infile);
return(-1);
}
if(buf[0x04] != 1 || buf[0x05] != 2 || get_be_16(buf+0x12) != 20) {
fprintf(stderr, "%s: not a 32bit big-endian PPC ELF file\n", infile);
return(-1);
}
if(get_be_32(buf+0x1c) != 0x34) {
fprintf(stderr, "%s: ELF PPC program header not at expected offset\n", infile);
return(-1);
}
if((img_offset = get_be_32(buf+0x38)) != 0x10000) {
/*
* Doesn't seem to bother the device when the header is less
* than 64K, and the default builds for NetBSD always generate
* images that are not padded to a 64K boundary...
*/
#ifndef __NetBSD__
fprintf(stderr, "warning: %s: size of ELF header is not 64k\n", infile);
#endif
}