/*
 * $Id: checkflash.c,v 1.1.72.1 2009/03/24 22:59:54 cnguyen 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)

#define swapped(x) (((x & 0xff000000) >> 24) | ((x & 0x00ff0000) >> 8) \
| ((x & 0x0000ff00) << 8) | ((x & 0x00000000ff) << 24))

/*
 * computeECC
 *
 * Compute the ECC for a 512 byte block of data.
 */
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;

    /*
     * Copy to an internal buffer.
     */
    {
        int i;

        for (i = 0; i < ECC_UNIT; i += 4) {
            data[i] = indata[i];
            data[i+1] = indata[i+1];
            data[i+2] = indata[i+2];
            data[i+3] = indata[i+3];
        }
    }

    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;
}

void
check_ecc(int page, unsigned char *data, unsigned int *spare)
{
    unsigned int ecc[4];
    int i, j;

    for (i = 0, j = 0; i < 4; i++, j += 512) {
      ecc[i] = swapped(computeECC((unsigned char*) &data[j]));
    }
    if ((ecc[0] == spare[12]) && (ecc[1] == spare[13]) 
        && (ecc[2] == spare[14]) && (ecc[3] == spare[15])) {
        fprintf (stderr, "Page %2d, good ecc\n", page);
    } else {
        fprintf (stderr, "Page %2d, bad ecc\n", page);
        fprintf (stderr, "computed ecc 0x%08x 0x%08x 0x%08x 0x%08x\n",
                 ecc[0], ecc[1], ecc[2], ecc[3]);
        fprintf (stderr, "stored ecc 0x%08x 0x%08x 0x%08x 0x%08x\n\n",
                 spare[12], spare[13], 
                 spare[14], spare[15]);
    }

    return;
}

int
main (int argc, char **argv)
{
    char *infilename = "";
    FILE *infile;
    unsigned char data[PAGE_SIZE];
    unsigned int spare[SPARE_SIZE/sizeof(unsigned int)];
    unsigned int status;
    int page = 0, eof = 0;

    if (argc > 1) {
        infilename = argv[1];
        printf ("%s\n", infilename);
        if ((infile = fopen (infilename, "r")) == NULL) {
            perror ("open");
            exit (1);
        }
    } else {
        fprintf (stderr, "usage: %s file1\n", argv[0]);
        exit (1);
    }

    /*
     * Repeatedly read a page and its spare, and then just
     * write out the spare.
     */
    while (1) {
        status = fread (&data[0], sizeof(char), sizeof (data), infile);
        if (status != sizeof(data)) {
            if (status == 0) {
                break;
            }
            if (feof(infile)) {
                eof = 1;
            }
        }
        status = fread(&spare[0], sizeof(char), sizeof (spare), infile);
        if (status != sizeof(spare)) {
            if (feof(infile)) {
                printf ("Found EOF\n");
            }
        }
        check_ecc(page, &data[0], &spare[0]);
        page++;
    }
    
    exit (0);
}
