/*
 *               Copyright (C) 2006, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */


#include <iostypes.h>
#include <ios.h>
#include <ioslibc.h>
#include <apputils.h>
#include <sys.h>
#include <ee.h>


#define STACK_SIZE      4096              
const u8                _initStack[STACK_SIZE];
const u32               _initStackSize = sizeof(_initStack);
const u32               _initPriority = 11;


#define LINE_SIZE       256


static IOSError readEE(char *args, char *d);
static IOSError writeEE(char *args, char *d);
static IOSError eraseEE(char *args, char *d);
static IOSError writeEEAll(char *args, char *d);
static IOSError eraseEEAll(char *args, char *d);
static IOSError getVersion(char *args, char *d);
static IOSError incVersion(char *args, char *d);


static Command commands[] = {
    {"help",    help,
        "help                   - Print this message", commands},
    {"readEE",  readEE,
        "readEE addr            - Read half-word at addr", 0},
    {"writeEE", writeEE,
        "writeEE addr value     - Write value to half-word at addr", 0},
    {"eraseEE", eraseEE,
        "eraseEE addr           - Erase half-word at addr", 0},
    {"writeEEAll", writeEEAll,
        "writeEEAll value       - Write value too all of EEPROM", 0},
    {"eraseEEAll", eraseEEAll,
        "eraseEEAll             - Erase all of EEPROM", 0},
    {"getVersion", getVersion,
        "getVersion <-o> <-c> <-s> <-f> - Get the specified version", 0},
    {"incVersion", incVersion,
        "incVersion <-o> <-c> <-s> <-f> - Increment the specified version", 0},
    {0},
};


static IOSError
readEE(char *args, char *d)
{
    IOSError rc = IOS_ERROR_OK;
    u32 addr, value;
    char *ptr;

    /* Get half-word address */
    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto readEE_out;
    }
    addr = asciiToInt(ptr);

    /* Read from EEPROM */
    rc = EE_RdCmd(addr, &value);
    if (rc != IOS_ERROR_OK) {
        goto readEE_out;
    }
    value &= 0xffff;

    printf("EEPROM value at half-word %d is 0x%x\n", addr, value);

readEE_out:

    if (rc != IOS_ERROR_OK) {
        printf("readEE failed, rc=%d\n", rc);
    }

    return (rc);
}

static IOSError
writeEE(char *args, char *d)
{
    IOSError rc = IOS_ERROR_OK;
    u32 addr, value;
    char *ptr;

    /* Get half-word address */
    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto writeEE_out;
    }
    addr = asciiToInt(ptr);

    /* Get half-word value */
    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto writeEE_out;
    }
    value = asciiToInt(ptr);
    value &= 0xffff;

    /* Enable write */
    rc = EE_WenCmd();
    if (rc != IOS_ERROR_OK) {
        goto writeEE_out;
    }

    /* Write to EEPROM */
    rc = EE_WrCmd(addr, value);
    if (rc != IOS_ERROR_OK) {
        goto writeEE_out;
    }

    /* Disable write */
    rc = EE_WdsCmd();
    if (rc != IOS_ERROR_OK) {
        goto writeEE_out;
    }

    printf("Written 0x%x to EEPROM at half-word %d\n", value, addr);

writeEE_out:

    if (rc != IOS_ERROR_OK) {
        printf("writeEE failed, rc=%d\n", rc);
    }

    return (rc);
}

static IOSError
eraseEE(char *args, char *d)
{
    IOSError rc = IOS_ERROR_OK;
    u32 addr;
    char *ptr;

    /* Get half-word address */
    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto eraseEE_out;
    }
    addr = asciiToInt(ptr);

    /* Enable write */
    rc = EE_WenCmd();
    if (rc != IOS_ERROR_OK) {
        goto eraseEE_out;
    }

    /* Erase value in EEPROM */
    rc = EE_EraseCmd(addr);
    if (rc != IOS_ERROR_OK) {
        goto eraseEE_out;
    }

    /* Disable write */
    rc = EE_WdsCmd();
    if (rc != IOS_ERROR_OK) {
        goto eraseEE_out;
    }

    printf("Erase addr %d in EEPROM\n", addr);

eraseEE_out:

    if (rc != IOS_ERROR_OK) {
        printf("eraseEE failed, rc=%d\n", rc);
    }

    return (rc);
}

static IOSError
writeEEAll(char *args, char *d)
{
    IOSError rc = IOS_ERROR_OK;
    u32 value;
    char *ptr;

    /* Get half-word value */
    if (getNextToken(&ptr, &args) == 0) {
        rc = IOS_ERROR_INVALID;
        goto writeEEAll_out;
    }
    value = asciiToInt(ptr);
    value &= 0xffff;

    /* Enable write */
    rc = EE_WenCmd();
    if (rc != IOS_ERROR_OK) {
        goto writeEEAll_out;
    }

    /* Write to all of EEPROM */
    rc = EE_WralCmd(value);
    if (rc != IOS_ERROR_OK) {
        goto writeEEAll_out;
    }

    /* Disable write */
    rc = EE_WdsCmd();
    if (rc != IOS_ERROR_OK) {
        goto writeEEAll_out;
    }

    printf("Written 0x%x to all of EEPROM\n", value);

writeEEAll_out:

    if (rc != IOS_ERROR_OK) {
        printf("writeEEAll failed, rc=%d\n", rc);
    }

    return (rc);
}

static IOSError
eraseEEAll(char *args, char *d)
{
    IOSError rc = IOS_ERROR_OK;

    /* Enable write */
    rc = EE_WenCmd();
    if (rc != IOS_ERROR_OK) {
        goto eraseEEAll_out;
    }

    /* Erase value in EEPROM */
    rc = EE_EralCmd();
    if (rc != IOS_ERROR_OK) {
        goto eraseEEAll_out;
    }

    /* Disable write */
    rc = EE_WdsCmd();
    if (rc != IOS_ERROR_OK) {
        goto eraseEEAll_out;
    }

    printf("Erase all of EEPROM\n");

eraseEEAll_out:

    if (rc != IOS_ERROR_OK) {
        printf("eraseEEAll failed, rc=%d\n", rc);
    }

    return (rc);
}


static IOSError
getVersion(char *args, char *d)
{
    IOSError rc = IOS_ERROR_OK;
    char *ptr;
    u32 os = 0, ca = 0, signer = 0, sfs = 0;

    while (getNextToken(&ptr, &args) != 0) {
        if (memcmp(ptr, "-o", 2) == 0) {
            os = 1;
        } else if (memcmp(ptr, "-c", 2) == 0) {
            ca = 1;
        } else if (memcmp(ptr, "-s", 2) == 0) {
            signer = 1;
        } else if (memcmp(ptr, "-f", 2) == 0) {
            sfs = 1;
        } else {
            rc = IOS_ERROR_INVALID;
            goto getVersion_out;
        }
    }

    if (os) {
        rc = EE_GetOsVersion();
        if (rc < 0) {
            goto getVersion_out;
        }
        printf("OS version: %d\n", rc);
    }

    if (ca) {
        rc = EE_GetCaCrlVersion();
        if (rc < 0) {
            goto getVersion_out;
        }
        printf("CA CRL version: %d\n", rc);
    }

    if (signer) {
        rc = EE_GetSignerCrlVersion();
        if (rc < 0) {
            goto getVersion_out;
        }
        printf("Signer CRL version: %d\n", rc);
    }

    if (sfs) {
        rc = EE_GetSfsVersion();
        if (rc < 0) {
            goto getVersion_out;
        }
        printf("SFS version: %d\n", rc);
    }

    rc = IOS_ERROR_OK;

getVersion_out:

    if (rc != IOS_ERROR_OK) {
        printf("getVersion failed, rc=%d\n", rc);
    }

    return (rc);
}


static IOSError
incVersion(char *args, char *d)
{
    IOSError rc = IOS_ERROR_OK;
    char *ptr;
    u32 os = 0, ca = 0, signer = 0, sfs = 0;

    while (getNextToken(&ptr, &args) != 0) {
        if (memcmp(ptr, "-o", 2) == 0) {
            os = 1;
        } else if (memcmp(ptr, "-c", 2) == 0) {
            ca = 1;
        } else if (memcmp(ptr, "-s", 2) == 0) {
            signer = 1;
        } else if (memcmp(ptr, "-f", 2) == 0) {
            sfs = 1;
        } else {
            rc = IOS_ERROR_INVALID;
            goto incVersion_out;
        }
    }

    if (os) {
        rc = EE_IncOsVersion();
        if (rc < 0) {
            goto incVersion_out;
        }
        printf("Incremented OS version\n");
    }

    if (ca) {
        rc = EE_IncCaCrlVersion();
        if (rc < 0) {
            goto incVersion_out;
        }
        printf("Incremented CA CRL version\n");
    }

    if (signer) {
        rc = EE_IncSignerCrlVersion();
        if (rc < 0) {
            goto incVersion_out;
        }
        printf("Incremented Signer CRL version\n");
    }

    if (sfs) {
        rc = EE_IncSfsVersion();
        if (rc < 0) {
            goto incVersion_out;
        }
        printf("Incremented SFS version\n");
    }

incVersion_out:

    if (rc != IOS_ERROR_OK) {
        printf("incVersion failed, rc=%d\n", rc);
    }

    return (rc);
}


void
main(void)
{
    char line[LINE_SIZE];

    printf("eemon [");
    printf("built: " BUILD_TIME "]\n\n");
    initChip();
    initSWWorkArounds(0,0);

    /* Initialize the GPIO setup */
    EE_GPIOSetup();

    while (1) {
        printf("Enter command: ");
        gets(line);
        if (parseCommand(line, commands) < 0) {
            help(line, (void *)commands);
        }
    }
}
