
/****************************************************************************
 * 
 *   (C) 1999 ARTX INC..  ALL RIGHTS RESERVED.  UNPUBLISHED -- RIGHTS
 *   RESERVED UNDER THE COPYRIGHT LAWS OF THE UNITED STATES.  USE OF A
 *   COPYRIGHT NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
 *   OR DISCLOSURE.
 * 
 *   THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE SECRETS OF
 *   ARTX INC..  USE, DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT
 *   THE PRIOR EXPRESS WRITTEN PERMISSION OF ARTX INC..
 * 
 *                   RESTRICTED RIGHTS LEGEND
 * 
 *   Use, duplication, or disclosure by the Government is subject to
 *   restrictions as set forth in subparagraph (c)(1)(ii) of the Rights
 *   in Technical Data and Computer Software clause at DFARS 252.227-7013
 *   or subparagraphs (c)(1) and (2) of Commercial Computer Software --
 *   Restricted Rights at 48 CFR 52.227-19, as applicable. 
 * 
 *   ArtX Inc.
 *   3400 Hillview Ave, Bldg 5
 *   Palo Alto, CA 94304
 * 
 *
 * $Id: main.c,v 1.4 2006/03/04 23:30:03 jlivesey Exp $
 *
 ****************************************************************************/

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "types.h"
#include "image.h"

char *ProgName;

char  *inFile;
#if 0
u32 asmOut;
#endif
u32 romOut;
u32 ROMOut;
u32 ROMLen = 0x100000;
#if 0
u32 danOut;
#endif
u32 dumpLen;
u32 dumpEntry;
u32 dumpDest;
#if 0
u32 memOut;
u32 statDump;
#endif
u32 hexOut = 0;
u32 hexMax = 0;
u32 verbose = 0;

#if 0
#define DANFILE         "dan.out"
#define ASMHDRFILE      "boothdr.inc"
#define ASMDATAFILE     "bootdata.inc"
#endif

#define ROMHDRFILE      "rom.h"
#define ROMDATAFILE     "rom.c"

#define MINNOWROM       "minnow.rom"

#define ASMROWLEN          13

void
usage(int err)
{
    fprintf(stderr, "usage: %s [args]\n", ProgName);
    fprintf(stderr, "  -f file       # input file\n");
    fprintf(stderr, "  -r            # rom output\n");
    fprintf(stderr, "  -d            # Dan's output\n");
    fprintf(stderr, "  -a            # asm output\n");
    fprintf(stderr, "  -m            # rundiag mem file\n");
    fprintf(stderr, "  -R            # Minnow ROM output\n");
    fprintf(stderr, "  -L <len>      # Minnow ROM length [0x100000]\n");
    fprintf(stderr, "  -l            # dump text+data length\n");
    fprintf(stderr, "  -e            # dump binary entry\n");
    fprintf(stderr, "  -t            # dump text location\n");
    fprintf(stderr, "  -H            # dump hex of program\n");
    fprintf(stderr, "  -M NN         # extend image to this length\n");
    exit(err);
}

void
boot_warning (u32 text, u32 entry)
{
    fprintf (stderr, 
             "This elf file has a text address of 0x%08x, entry 0x%08x\n",
             text, entry);
    fprintf (stderr, 
             "If the file is intended to be a bootable image, this can\n");
    fprintf (stderr, 
             "cause problems, because elftohex will convert the entire\n");
    fprintf (stderr, 
             "text segment to hex, and the entry point will not be at\n");
    fprintf (stderr, 
             "the start of the booted file.   You can correct this by\n");
    fprintf (stderr, 
             "using a linker script to force the text segment and the\n");
    fprintf (stderr,
             "entry point to the same address.\n");
}

static void
dumpCData(FILE *fp, const char *nm, u8 *data, u32 tot)
{
    u32   i, j, row;

    fprintf(fp, "#include \"rom.h\"\n\n\n");
    fprintf(fp, "u8 %sData[ROM_SIZE] = {\n", nm);
    for( i = 0 ; i < tot ; i += j ) {
        fprintf(fp, "   ");
        row = ((tot - i) > ASMROWLEN) ? ASMROWLEN : tot - i;
        for( j = 0 ; j < row ; j++ ) {
            fprintf(fp, "0x%02x", data[i+j]);
            if ( (i + j + 1) < tot ) {
                fprintf(fp, ",");
            }
        }
        fprintf(fp, "\n");
    }
    fprintf(fp, "};\n");
}

static void
dumpHData(FILE *fp, const char *nm, u8 *data, u32 tot, u32 max)
{
    u32   i, j, word, words = 0;

    for( i = 0 ; i < tot ; i += 4, words++ ) {
        word = 0;
        for (j = 0; j < 4; j++) {
            word |= (data[i+j] << ((3 - j) * 8));
        }
        fprintf (fp, "%08x\n", word);
    }
    
    if (words < max) {
        for ( i = words; i < max; i++ ) {
            fprintf (fp, "%08x\n", 0);
        }
    } else {
        if (max == 0) {
	    for (i = words; (i & 0x1ff) != 0; i++) {
	        fprintf (fp, "%08x\n", 0);
	    }
        }
    }


    return;
}

void
dumpHex(Image *ip, u32 max)
{
    FILE *fout = stdout;

    if ( ip->textLen == 0 ) {
        fprintf(stderr, "No text segment!\n");
        exit(1);
    }
    if ( ip->dataLen ) {
        fprintf(stderr, "Has a data segment!\n");
        exit(1);
    }

    if (verbose)
      fprintf (stderr, "max 0x%x text 0x%x\n", max, ip->text);

    /*
     * If specified, max is realtive to start of text segment
     */
    if (max > ip->text) {
      max = (u32)max - (u32)ip->text;
      /*
       * Max is in bytes
       */
      max = max / 4;
    }

    if (verbose)
      fprintf (stderr, "max 0x%x\n", max);

    dumpHData(fout, "rom", ip->textData, ip->textLen, max);

    fclose(fout);

    return;
}

void
dumpRom(Image *ip)
{
    FILE *fout;

    if ( ip->textLen == 0 ) {
        fprintf(stderr, "No text segment!\n");
        exit(1);
    }
    if ( ip->dataLen ) {
        fprintf(stderr, "Has a data segment!\n");
        exit(1);
    }

    /* dump the header */
    fout = fopen(ROMHDRFILE, "wb");
    if ( fout == NULL ) {
        fprintf(stderr, "Unable to open ROM hdr file\n");
        exit(1);
    }

    fprintf(fout, "/* Generated File, do not edit */\n\n");

    fprintf(fout, "#include <types.h>\n\n");
    fprintf(fout, "#define ROM_SIZE     0x%08x\n", ip->textLen);
    fprintf(fout, "extern u8 romData[];\n");

    fclose(fout);

    /* dump the data */
    fout = fopen(ROMDATAFILE, "wb");
    if ( fout == NULL ) {
        fprintf(stderr, "Unable to open ROM data file\n");
        exit(1);
    }

    fprintf(fout, "/* Generated File, do not edit */\n\n");
    
    dumpCData(fout, "rom", ip->textData, ip->textLen);

    fclose(fout);
}

void
dumpROM(Image *ip)
{
    FILE    *fout;
    unsigned val;
    unsigned i;

    if ( ip->textLen == 0 ) {
        fprintf(stderr, "No text segment!\n");
        exit(1);
    }
    if ( ip->dataLen ) {
        fprintf(stderr, "Has a data segment!\n");
        exit(1);
    }

    /* dump the data */
    fout = fopen(MINNOWROM, "wb");
    if ( fout == NULL ) {
        fprintf(stderr, "Unable to open ROM data file\n");
        exit(1);
    }

    if ( fwrite(ip->textData, ip->textLen, 1, fout) != 1 ) {
        fprintf(stderr, "Unable to write ROM text data\n");
        exit(1);
    }

    val = 0;
    if ( ip->textLen & 3 ) {
        fwrite(&val, 4 - (ip->textLen & 3), 1, fout);
    }
    i = (ip->textLen + 3) & ~3;
    for( ; i < ROMLen ; i += 4 ) {
        val = i | 0xfff00000;
        fwrite(&val, 4, 1, fout);
    }

    fclose(fout);
}

int
main(int ac, char *av[])
{
    int    errFlg = 0;
    int    c;
    Image *ip;
    u32 imgLen;

    ProgName = av[0];

    while ( (c = getopt(ac, av, "hHf:ardmM:DRL:letv")) != EOF ) {
        switch (c) {
#if 0
        case 'L':
            ROMLen = ntoi(optarg);
            break;

        case 'D':
            statDump++;
	    break;
#endif

        case 'f':
            inFile = optarg;
            break;

#if 0
        case 'd':
            danOut++;
            break;
#endif

        case 'r':
            romOut++;
            break;

#if 0
        case 'a':
            asmOut++;
            break;
#endif

        case 'R':
            ROMOut++;
            break;

#if 0
        case 'm':
            memOut++;
            break;
#endif

        case 'e':
            dumpEntry++;
            break;

        case 'l':
            dumpLen++;
            break;

        case 't':
            dumpDest++;
            break;

        case 'H':
            hexOut++;
            break;

        case 'M':
            hexMax = strtoul(optarg, NULL, 16);
            break;

	case 'v':
	    verbose++;
	    break;

        case 'h':
            usage(0);
            break;

        default:
            fprintf(stderr, "unknown option %c\n", c);
            errFlg++;
            break;
        }
    }

    if ( (inFile == NULL) || errFlg ) {
        usage(1);
    }

    /* read file */
    ip = elfRead(inFile);
    if ( ip == NULL ) {
        fprintf(stderr, "unable to load elf file %s\n", inFile);
        exit(1);
    }

    if ( ip->dataLen ) {
        imgLen = (ip->data + ip->dataLen) - ip->text;
    }
    else {
        imgLen = ip->textLen;
    }

#if 0
    if (statDump) {
        dumpImage(ip);
	return 0;
    }
    
    if ( asmOut ) {
        dumpAsm(ip);
    }
#endif

    if ( romOut ) {
        dumpRom(ip);
    }
    
    if ( ROMOut ) {
        dumpROM(ip);
    }

    if ( hexOut ) {
        if (ip->text != ip->entry) {
            boot_warning(ip->text, ip->entry);
        }
        dumpHex (ip, hexMax);
    }

#if 0
    if ( memOut ) {
        dumpMem(ip);
    }

    if ( danOut ) {
        dumpDan(ip);
    }
#endif

    if ( dumpLen ) {
        printf("len %d\n", imgLen);
    }

    if ( dumpDest ) {
        printf("text %d 0x%08x\n", ip->text, ip->text);
    }

    if ( dumpEntry ) {
        printf("entry %d 0x%08x\n", ip->entry, ip->entry);
    }

    return 0;
}

/* eof */
