/*
 *               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>
#include "device.h"

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

static u32 efChecked = 0;
static u32 efInit = 0;

#define LOCK_EEPROM  __iosDisableInt
#define UNLOCK_EEPROM(mask)  __iosRestoreInt((mask))

u32 __iosDisableInt(void);
void __iosRestoreInt(u32 mask);

/*
 * NOTE: Address is in words
 */
static 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
 */
static int
checkInit()
{
    if (efChecked == 0) {
	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;
    }
	efChecked = 1;
    }
    return efInit;
}

s32 nvGetOsVersion()
{
    s32 rv;

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

s32 nvIncOsVersion()
{
    s32 rv = IOS_ERROR_OK;

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

s32 nvGetCaCrlVersion()
{
    s32 rv;

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

s32 nvIncCaCrlVersion()
{
    s32 rv = IOS_ERROR_OK;

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

s32 nvGetCrlVersion()
{
    s32 rv;

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

s32 nvIncCrlVersion()
{
    s32 rv = IOS_ERROR_OK;

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

s32 nvGetSfsVersion()
{
    s32 rv;

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

s32 nvIncSfsVersion()
{
    s32 rv = IOS_ERROR_OK;

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

void
nvGetDeviceId(u32 *deviceId)
{
    if (checkInit()) {
	readEfuse(NV_RVL_DEVICE_ID_OFF / sizeof (u32), (u8 *) deviceId,
		  NV_RVL_DEVICE_ID_SIZE);
    } else {	
	memcpy(deviceId, &(device[NV_RVL_DEVICE_ID_OFF]), sizeof(u32));
    }
}

void
nvGetDeviceKeys(u8 *deviceEcdhKey, u8 *commonAesKey, u8 *sfsHmacKey, 
                u8 *sfsAesKey)
{
    if (checkInit()) {
	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);
    } else {
	memcpy(commonAesKey, &device[NV_RVL_COMMON_AES_KEY_OFF], NV_RVL_COMMON_AES_KEY_SIZE);
	memcpy(deviceEcdhKey, &device[NV_RVL_DEVICE_ECDH_KEY_OFF], NV_RVL_DEVICE_ECDH_KEY_SIZE);
	memcpy(sfsAesKey, &device[NV_RVL_SFS_AES_KEY_OFF], NV_RVL_SFS_AES_KEY_SIZE);
	memcpy(sfsHmacKey, &device[NV_RVL_SFS_HMAC_KEY_OFF], NV_RVL_SFS_HMAC_KEY_SIZE);
    }
}

void
nvGetBackupKey(u8 *bkpAesKey)
{
    if (checkInit()) {
        readEfuse(NV_RVL_BACKUP_AES_KEY_OFF / sizeof (u32), bkpAesKey,
                  NV_RVL_BACKUP_AES_KEY_SIZE);
    } else {
        memcpy(bkpAesKey, &device[NV_RVL_BACKUP_AES_KEY_OFF], NV_RVL_BACKUP_AES_KEY_SIZE);
    }
}

/*
 * 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++) {
	u32 mask = LOCK_EEPROM();
        rv = EE_RdCmd(offset + i, &value);
	UNLOCK_EEPROM(mask);
	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;

    if (checkInit()) {
	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));

    } else {
	memcpy(&mfrId, &device[NV_RVL_MFR_ID_OFF], sizeof(u32));
	memcpy(&caId, &device[NV_RVL_CA_ID_OFF], sizeof(u32));
	memcpy(mfrDate, &device[NV_RVL_MFR_DATE_OFF], sizeof(u32));
	memcpy(mfrSign, &device[NV_RVL_MFR_SIGN_OFF], NV_RVL_MFR_SIGN_SIZE);
    }
    nvGetDeviceId(&deviceId);

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

}
