rdram2new.c 3.19 KB

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "verify.h"

char *InputFileName=NULL, *OutputFileName=NULL;
FILE *InputFile, *OutputFile;

void usage(void)
{
    fprintf(stderr, "usage: rdram2new -i in.rdram -o out.rdram \n");
    exit(1);
}

int fsize(char *name)
{
    struct stat stbuf;

    if (stat(name, &stbuf) == -1)
	{
	    fprintf(stderr, "fsize: cannot access %s\n", name);
	    return;
	}

    return(stbuf.st_size);
}

void ReadAndProcessInputFile(void)
{
    int InputFileSize;
    unsigned char *MainMemory, *HiddenMemory, OldByte;
    int MainMemorySize, HiddenMemorySize;
    VerifyInfo *VerifyStuff;
    int i;

    if ((InputFile = fopen(InputFileName, "r")) == NULL)
	{
	    fprintf(stderr, "Error, could not open input file %s \n", InputFileName);
	    exit(1);
	}

    InputFileSize = fsize(InputFileName);

    MainMemorySize   = 0;
    HiddenMemorySize = 0;

    if (InputFileSize == 0x400000)
	{
	    MainMemorySize   = 0x400000;
	    HiddenMemorySize = 0x000000;
	}
    else if (InputFileSize == 0x440000)
	{
	    MainMemorySize   = 0x400000;
	    HiddenMemorySize = 0x040000;
	}
    else if (InputFileSize == 0x200000)
	{
	    MainMemorySize   = 0x200000;
	    HiddenMemorySize = 0;
	}
    else if (InputFileSize == 0x240000)
	{
	    MainMemorySize   = 0x200000;
	    HiddenMemorySize = 0x040000;
	}
    else 
	{
	    fprintf(stderr, "ERROR: file size not correct \n");
	    exit(1);
	}

    MainMemory   = (unsigned char *) malloc(MainMemorySize);
    HiddenMemory = (unsigned char *) malloc(HiddenMemorySize);    
    
    fread(MainMemory, MainMemorySize, 1, InputFile);

    VerifyStuff = (VerifyInfo *) (MainMemory + VERIFY_INFO_PHYSADDR);

    if ((VerifyStuff->magicNumber == MAGICNUMBER) &&
	(VerifyStuff->version     == VERSION))
	{
	    fprintf(stderr, "ERROR: rdram file is already in new format \n");
	    exit(1);
	}

    VerifyStuff->magicNumber = MAGICNUMBER;
    VerifyStuff->version     = VERSION;

    if ((OutputFile = fopen(OutputFileName, "w")) == NULL)
	{
	    fprintf(stderr, "Error, could not open output file %s \n", OutputFileName);
	    exit(1);
	}

    fwrite(MainMemory, MainMemorySize, 1, OutputFile);

    /* Now read in hidden bits and do the right thing */

    fread(HiddenMemory, HiddenMemorySize, 1, InputFile);

    /* Not being efficient here, just getting the job done.
       Were this an actual Microsoft interview I'd be much
       more obtuse.. */
    for (i=0; i<HiddenMemorySize; i++)
	{
	    OldByte = HiddenMemory[i];
	    HiddenMemory[i] = 
		((OldByte &   3) << 6) |
		((OldByte &  12) << 2) |
		((OldByte &  48) >> 2) |
		((OldByte & 192) >> 6);
	}
    
    fwrite(HiddenMemory, HiddenMemorySize, 1, OutputFile);

    fclose(InputFile);
    fclose(OutputFile);
}

int main(int argc, char **argv)
{
    int c;
    extern char *optarg;
    extern int optind;
    char *ofile = NULL;

    while ((c = getopt(argc, argv, "i:o:")) != EOF)
	switch (c)
	    {
	    case 'i':
		InputFileName = strdup(optarg);
		break;
	    case 'o':
		OutputFileName = strdup(optarg);
		break;
	    }    

    if (InputFileName == NULL)
	usage();
	    
    if (OutputFileName == NULL)
	usage();

    ReadAndProcessInputFile();

    exit(0);    
}