/*
 *               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.
 *
 * $Id: eeprom.c,v 1.1 2006/11/01 00:25:33 pramila Exp $
 */


#include <types.h>
#include <ioslibc.h>
#include <iop.h>
#include <sys.h>
#include <mmu.h>
#include <diag/proto.h>
#include <diag/aes_sw.h>
#include <diag/sha1.h>
#include <diag/sha_sw.h>
#include <addr_map.h>
#include <bsl.h>
#include <csl.h>
#include <iosctypes.h>
#include <iosccert.h>
#include "nvram.c"
#include "root.c"
#include "ms_dpki_cert.c"
#include "hw_crypto.c"


#define DATA_START          IOP_GDDR3_START
#define DATA_SIZE           (64 * 1024)

#define SHA_BLOCK_SZ_SHIFT  6
#define HASH_RESULT_SIZE    20

/* Device control */
#define WAIT(dev)           while (IO_READ(dev##_CTRL_REG) & dev##_CTRL_EXEC)
#define FLUSH(dev)          ahbMemFlush(IOS_WB_##dev)
#define INVAL(dev)          ahbMemRBInvalidate(IOS_RB_##dev)


#define STACK_SIZE          (2 * 1024)
const u8    _initStack[STACK_SIZE];
const u32   _initStackSize = sizeof(_initStack);
const u32   _initPriority = 10;

void
busDelay (unsigned int cycles)
{
    int i;

    for (i = 0; i < cycles; i++)
        ;

    return;
}

void
fillBufFromRandom(u32 *data, u32 size)
{
    u32 count, value;

    value = 0;
    for (count = 0; count < (size / 4); count++) {
        value = (value * 1187) - 903;
        data[count] = value;
    }
}


#define PROD_DEF_ID (0x4a39) 
#define DEV_DEF_ID  (0x1f5)

#define ALIGN_SIZE 64
//static IOSCRsa2048EccCert msCert __attribute__((aligned(ALIGN_SIZE)));
static IOSCEccSignedCert returnedCert __attribute__((aligned(ALIGN_SIZE)));
static IOSCEccSignedCert appCert __attribute__((aligned(ALIGN_SIZE)));
#ifdef RVL_DEVEL_PKI
#define msCert ms_dpki
#else
#define msCert ms_ppki
#endif

extern u8 msCert[];

s32
main(void)
{
    BSL_KeyStoreInit();
    IOSCError err;
    u32 id;
    IOSCPublicKeyHandle msPublickeyh;
    IOSCPublicKeyHandle devPublickeyh;
    IOSCPublicKeyHandle appPublickeyh;
    IOSCSecretKeyHandle inputPrivkeyh;
    CSLOSEccPrivateKey inputPrivate;
    IOSCCertName appName;
    err = BSL_GetData(IOSC_DEV_ID_HANDLE, &id);
    if (err != IOSC_ERROR_OK) {
        printf("Failed to read device ID, err=%d\n", err);
        goto main_out;
    }

    if (id == PROD_DEF_ID || id == DEV_DEF_ID) {
        printf("Default ID found 0x%x\n", id);
        goto main_out;
    }
    printf("Dev ID = 0x%x\n", id);
    
    /* Check hash and common key directly from eeprom */
        
    /*
     * Check EEPROM contents: device cert signature and private key
     */
     
    err = BSL_CreateObject(&msPublickeyh, IOSC_PUBLICKEY_TYPE, IOSC_ECC233_SUBTYPE);
    if (err != IOSC_ERROR_OK) {
        printf("Failed to create object for MS public key=%d\n", err);
        goto main_out;
    }
    /* Import MS public key */
    err = BSL_ImportPublicKey((((IOSCRsa2048EccCert *)msCert)->pubKey), 0, msPublickeyh);
    if (err != IOSC_ERROR_OK) {
        printf("Failed to import MS public key=%d\n", err);
        goto main_out;
    }
    /* Get device certificate */
    err = BSL_GetDeviceCertificate((IOSCEccSignedCert *)&returnedCert);
    if (err != IOSC_ERROR_OK) {
        printf("Failed to get Device Cert=%d\n", err);
        goto main_out;
    }
    /* Import it to check if it was a good cert */
    err = BSL_CreateObject(&devPublickeyh, IOSC_PUBLICKEY_TYPE, IOSC_ECC233_SUBTYPE);
    if (err != IOSC_ERROR_OK) {
        printf("Failed to create object for Dev public key=%d\n", err);
        goto main_out;
    }
    err = BSL_ImportCertificate((u8 *)returnedCert, msPublickeyh, devPublickeyh);
    if(err == IOS_ERROR_OK) {
        printf("Device Certificate import Pass\n");
    } else {
        printf("Device Certificate import Fail, err = %d\n", err);
        goto main_out;
    }
    /* Use the device private key to sign an application cert */
    memset(inputPrivate, 0x5, sizeof(CSLOSEccPrivateKey));
    err = BSL_CreateObject(&inputPrivkeyh, IOSC_SECRETKEY_TYPE, IOSC_ECC233_SUBTYPE);
    if (err != IOSC_ERROR_OK) {
        printf("Failed to create object for input private key=%d\n", err);
        goto main_out;
    }
    err = BSL_ImportSecretKey(inputPrivkeyh, 0, 0, IOSC_NOSIGN_NOENC, 0, 0, inputPrivate);
    if (err != IOSC_ERROR_OK) {
        printf("Failed to import input private key=%d\n", err);
        goto main_out;
    }
    memset(appName, 0x0, sizeof(IOSCCertName));
    memcpy(appName, "APP00000001", sizeof("APP00000001"));
    err = BSL_GenerateCertificate(inputPrivkeyh, appName, &appCert);
    if (err != IOSC_ERROR_OK) {
        printf("Failed to generate application cert err = %d\n", err);
        goto main_out;
    }
    err = BSL_CreateObject(&appPublickeyh, IOSC_PUBLICKEY_TYPE, IOSC_ECC233_SUBTYPE);
    if (err != IOSC_ERROR_OK) {
        printf("Failed to create public key=%d\n", err);
        goto main_out;
    }
    err = BSL_ImportCertificate(appCert, devPublickeyh, appPublickeyh);
    if (err == IOS_ERROR_OK) {
        printf("App Cert Import Pass\n");
    } else {
        printf("Failed to import App cert err = \n", err);
        goto main_out;
    }
    /* Delete all the crap */
    BSL_DeleteObject(devPublickeyh);
    BSL_DeleteObject(msPublickeyh);
    BSL_DeleteObject(inputPrivkeyh);
    BSL_DeleteObject(appPublickeyh);
main_out:
    return (err);
}
