/*
 * $Id: toflash.c,v 1.2 2006/02/28 22:34:31 jlivesey Exp $
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define ECC_UNIT 512
#define PAGE_SIZE 2048
#define SPARE_SIZE 64

#define bit(c, n) ((c & (1 << n)) >> n)

unsigned int
computeECC(unsigned char *indata)
{
    unsigned char data[512];
    unsigned int ecc = 0, bits[8], allBits, parity[12], parity_p[12];
    int count, bitcount, parityCount;

    /*
     * This swizzling of the input data is necessary because the NNGC
     * system is big-endian.
     */
    {
        int i;

        for (i = 0; i < ECC_UNIT; i += 4) {
            data[i] = indata[i+3];
            data[i+1] = indata[i+2];
            data[i+2] = indata[i+1];
            data[i+3] = indata[i];
        }
    }

    memset (&bits[0], 0, sizeof (bits));
    memset (&parity[0], 0, sizeof (parity));
    memset (&parity_p[0], 0, sizeof (parity_p));

    for (count = 0; count < ECC_UNIT; count++) {
        for (bitcount = 0; bitcount < 8; bitcount++) {
            bits[bitcount] = bit(data[count], bitcount);
        }
        allBits = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ bits[4] ^ bits[5] ^ bits[6] ^ bits[7];

        parity[0] = parity[0] ^ bits[1] ^ bits[3] ^ bits[5] ^ bits[7];
        parity_p[0] = parity_p[0] ^ bits[0] ^ bits[2] ^ bits[4] ^ bits[6];
        parity[1] = parity[1] ^ bits[2] ^ bits[3] ^ bits[6] ^ bits[7];
        parity_p[1] = parity_p[1] ^ bits[0] ^ bits[1] ^ bits[4] ^ bits[5];
        parity[2] = parity[2] ^ bits[4] ^ bits[5] ^ bits[6] ^ bits[7];
        parity_p[2] = parity_p[2] ^ bits[0] ^ bits[1] ^ bits[2] ^ bits[3];

        for (parityCount = 3; parityCount < 12; parityCount++) {
            if ((count >> (parityCount - 3)) & 1) {
                parity[parityCount] = parity[parityCount] ^ allBits;
            } else {
                parity_p[parityCount] = parity_p[parityCount] ^ allBits;
            }
        }
    }

    ecc = 0;
    for (count = 0; count < 12; count++) {
        ecc = ecc | ((parity[count] & 0x1) << (16 + count)) 
            | ((parity_p[count] & 0x1) << count);
    }

    /*
     * This swizzling of the output ECC is necessary because the NNGC
     * system is big-endian.
     */
    ecc = ((ecc & 0xff000000) >> 24) 
        | ((ecc & 0x00ff0000) >> 8) 
        | ((ecc & 0x0000ff00) << 8) 
        | ((ecc & 0x000000ff) << 24);

    return ecc;
}

int
main (int argc, char **argv)
{
    char *infilename = "", *outfilename = "";
    FILE *infile, *outfile;
    unsigned char data[PAGE_SIZE];
    unsigned int spare[SPARE_SIZE/sizeof(unsigned int)];
    unsigned int status;
    int i, j, k, l, errors = 0, page = 0, eof = 0;

    if (argc > 2) {
        infilename = argv[1];
        printf ("%s\n", infilename);
        if ((infile = fopen (infilename, "r")) == NULL) {
            perror ("open");
            exit (1);
        }
        outfilename = argv[2];
        printf ("%s\n", outfilename);
        if ((outfile = fopen (outfilename, "w")) == NULL) {
            perror ("open");
            exit (1);
        }
        if (argc > 3) {
            if (!strcmp(argv[3], "-s")) {
                errors = 1;
            } else {
                if (!strcmp(argv[3], "-d")) {
                    errors = 2;
                } else {
                    fprintf (stderr, "usage: %s [-s|-d] file1 file2\n", argv[0]);
                    exit (1);
                }
            }
                
        }
    } else {
        fprintf (stderr, "usage: %s [-s|-d] file1 file2\n", argv[0]);
        exit (1);
    }

    while (1) {
        memset (&data[0], 0, sizeof (data));
        status = fread (&data[0], sizeof(char), sizeof (data), infile);
        if (status != sizeof (data)) {
            if (status == 0) {
                /*
                 * Zero read.
                 */
                goto out;
            }
            if (feof(infile)) {
                /*
                 * Short read.
                 */
                eof = 1;
            }
        }
        i = 12;
        memset (&spare[0], 0, sizeof (spare));
        for (j = 0; j < PAGE_SIZE; j += ECC_UNIT) {
            spare[i] = computeECC (&data[j]);
            printf ("%s: page %d spare[%d] = 0x%08x\n",
                    __FUNCTION__, page, i, spare[i]);
            i++;
        }
        if (errors != 0) {
            for (k = 0; k < errors; k++) {
                l = random() & (PAGE_SIZE - 1);
                data[l] = data[l] ^ (1 << (random() & 0x7));
            }
        }
        fwrite (&data[0], sizeof (data), 1, outfile);
        fwrite (&spare[0], sizeof (spare), 1, outfile);
        page++;
        if (eof) {
            break;
        }
    }
    
 out:

    exit (0);
}
