/*
 *               Copyright (C) 2005, 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 <iosccert.h>
#include <nvram.h>
#include <ee.h>
#include <iop.h>
#include <ioslibc.h>

static u32 osVersion = 0;
static u32 caCrlVersion = 0;
static u32 crlVersion = 0;
static u32 sfsVersion = 0;

u32 rngState = 0;

int nop(){
    return 0; 
}

#define LOCK_EEPROM nop
#define UNLOCK_EEPROM nop

/*
 * NOTE: Address is in words
 */
void
readEfuse(u32 address, u8 *buffer, u32 size)
{
    u32 data, i;

    memset(buffer, 0, size);
    for (i = 0; i < size; i += sizeof (u32)) {
        busWrt32(EFUSE_ADDR_REG,
             ACREFUSEADDR_EFEN_MASK | (address & ACREFUSEADDR_EFADDR_MASK));
        data = busRd32(EFUSE_DATA_REG);
        memcpy(buffer, &data, sizeof(u32));
        buffer += sizeof (u32);
        address++;
    }
}

/*
 * If common key is initialized use the efuse and EEPROM for data
 */
int
checkInit()
{
    static u32 efChecked = 0;
    static u32 efInit = 0;

    if (efChecked == 0) {
#ifdef ENABLE_EFINIT
        u32 key[4];
        readEfuse(NV_RVL_COMMON_AES_KEY_OFF / sizeof (u32), (u8 *) &key[0],
                  NV_RVL_COMMON_AES_KEY_SIZE);
        if ((key[0] | key[1] | key[2] | key[3]) != 0) {
            efInit = 1;
    }
#endif /* ENABLE_EFINIT */
        efChecked = 1;
    }
    return efInit;
}

s32 nvGetOsVersion()
{
    s32 rv;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_GetOsVersion();
    } else {
        rv = osVersion; 
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

s32 nvIncOsVersion()
{
    s32 rv = IOS_ERROR_OK;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_GetOsVersion();
        if((rv + 1) >=  NV_RVL_EEPROM_SIZE) {
            rv = IOS_ERROR_ACCESS;
        } else {
            rv = EE_IncOsVersion();
        }
    } else {
        osVersion++;
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

s32 nvGetCaCrlVersion()
{
    s32 rv;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_GetCaCrlVersion();
    } else {
        rv = caCrlVersion; 
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

s32 nvIncCaCrlVersion()
{
    s32 rv = IOS_ERROR_OK;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_GetCaCrlVersion();
        if((rv + 1) >=  NV_RVL_EEPROM_SIZE) {
            rv = IOS_ERROR_ACCESS;
        } else {
            rv = EE_IncCaCrlVersion();
        }
    } else {
        caCrlVersion++;
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

s32 nvGetCrlVersion()
{
    s32 rv;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_GetSignerCrlVersion();
    } else {
        rv = caCrlVersion; 
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

s32 nvIncCrlVersion()
{
    s32 rv = IOS_ERROR_OK;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_GetSignerCrlVersion();
        if ((rv + 1) >= NV_RVL_EEPROM_SIZE) {
            rv = IOS_ERROR_ACCESS;
        } else {
            rv = EE_IncSignerCrlVersion();
        }
    } else {
        crlVersion++;
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

s32 nvIncRngState()
{
    s32 rv = IOS_ERROR_OK;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_IncRngState();
    } else {
        rngState++;
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

s32 nvGetRngState()
{
    s32 rv;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_GetRngState();
    } else {
        rv = rngState;
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

s32 nvGetSfsVersion()
{
    s32 rv;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_GetSfsVersion();
    } else {
        rv = sfsVersion; 
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

s32 nvIncSfsVersion()
{
    s32 rv = IOS_ERROR_OK;

    u32 mask = LOCK_EEPROM();
    if (checkInit()) {
        rv = EE_GetSfsVersion();
        if ((rv + 1) >= NV_RVL_SFS_DATA_MAX) {
            rv = IOS_ERROR_ACCESS;
        } else {
            rv = EE_IncSfsVersion();
        }
    } else {
        sfsVersion++;
    }
    UNLOCK_EEPROM(mask);
    return rv;
}

void
nvGetDeviceId(u32 *deviceId)
{
    u32 mask = LOCK_EEPROM();

    readEfuse(NV_RVL_DEVICE_ID_OFF / sizeof (u32), (u8 *) deviceId,
                  NV_RVL_DEVICE_ID_SIZE);
    
    UNLOCK_EEPROM(mask);
}

void
nvGetDeviceKeys(u8 *deviceEcdhKey, u8 *commonAesKey, u8 *sfsHmacKey, 
                u8 *sfsAesKey)
{
    u32 mask = LOCK_EEPROM();
    
    readEfuse(NV_RVL_COMMON_AES_KEY_OFF / sizeof (u32), commonAesKey,
                  NV_RVL_COMMON_AES_KEY_SIZE);
    readEfuse(NV_RVL_DEVICE_ECDH_KEY_OFF / sizeof (u32), deviceEcdhKey,
                  NV_RVL_DEVICE_ECDH_KEY_SIZE);
    readEfuse(NV_RVL_SFS_HMAC_KEY_OFF / sizeof (u32), sfsHmacKey,
                  NV_RVL_SFS_HMAC_KEY_SIZE);
    readEfuse(NV_RVL_SFS_AES_KEY_OFF / sizeof (u32), sfsAesKey,
                  NV_RVL_SFS_AES_KEY_SIZE);
    
    UNLOCK_EEPROM(mask);
}

void
nvGetBackupKey(u8 *bkpAesKey)
{
    u32 mask = LOCK_EEPROM();

    readEfuse(NV_RVL_BACKUP_AES_KEY_OFF / sizeof (u32), bkpAesKey,
              NV_RVL_BACKUP_AES_KEY_SIZE);

    UNLOCK_EEPROM(mask);
}

/*
 * NOTE:
 * offset is in u16
 * size is in u16
 */
static s32
eeReadArray(u32 offset, u8 *buffer, int size)
{
    IOSError rv = IOS_ERROR_OK;
    int i;
    u32 value;

    for (i=0; i<size; i++) {
        rv = EE_RdCmd(offset + i, &value);
        if (rv != IOS_ERROR_OK) {
            break;
        }
        buffer[i<<1] = (value >> 8) & 0xff;
        buffer[(i<<1)+1] = value & 0xff;
    }
    return rv;
}


void
nvGetDeviceCert(IOSCName mfrName, IOSCName caName, u32 *mfrDate, IOSCName name, u8 *mfrSign)
{
    u32 deviceId;
    u32 mfrId;
    u32 caId;

    u32 mask = LOCK_EEPROM();
    
    eeReadArray((NV_RVL_MFR_ID_OFF-NV_RVL_EEPROM_OFF)>>1, (u8 *) & mfrId, 
                    sizeof(mfrId)/sizeof(u16));
    eeReadArray((NV_RVL_CA_ID_OFF-NV_RVL_EEPROM_OFF)>>1, (u8 *) & caId, 
                    sizeof(caId)/sizeof(u16));
    eeReadArray((NV_RVL_MFR_DATE_OFF-NV_RVL_EEPROM_OFF)>>1, (u8 *) mfrDate, 
                    sizeof(u32)/sizeof(u16));
    eeReadArray((NV_RVL_MFR_SIGN_OFF-NV_RVL_EEPROM_OFF)>>1, mfrSign, 
                    NV_RVL_MFR_SIGN_SIZE/sizeof(u16));
    
    UNLOCK_EEPROM(mask);
    nvGetDeviceId(&deviceId);

    snprintf(mfrName, sizeof(IOSCName), "MS%08x", mfrId); 
    snprintf(caName, sizeof(IOSCName), "CA%08x", caId);
    snprintf(name, sizeof(IOSCName), "NG%08x", deviceId);
}
