util.h 5.78 KB
#ifndef __SK_UTIL_HDR__
#define __SK_UTIL_HDR__

#include <PR/bbtypes.h>
#include <PR/bbvirage.h>
#include <PR/bbmetadata.h>
#include <PR/bbskapi.h>
#include <sha1.h>
#include "skerror.h"
#include "entry.h"

extern BbVirage2 *v2;
extern BbVirage01 v01;

typedef struct {
    u8 *data;
    u32 size;
} SkDataChain;

/* The bundle of content metadata, certs, and crls is loaded
 * to iram at the address hard-coded below.
 */
#define CMDBUNDLE_STORAGE_START   PHYS_TO_K0(INTERNAL_RAM_START)

#define PI_FLASH_DEV0_BUF0_READ_PAGE 0x9f008a10
#define PI_FLASH_DEV0_BUF1_READ_PAGE \
          (PI_FLASH_DEV0_BUF0_READ_PAGE | PI_FLASH_CTRL_BUF1) 

#define FLASH_MODULE_PRESENT \
             (!((IO_READ(MI_EINTR_REG)&MI_EINTR_MODULE_REMOVED)))

#define SK_DMA_BUF_TO_DRAM 0
#define SK_DMA_DRAM_TO_BUF 1

#define POLL_DMA_STATUS \
         do{}while((IO_READ(PI_STATUS_REG))&PI_STATUS_DMA_BUSY)

#define KSEG_TO_PHYS(a) (((u32)(a))&0x1fffffff)

/* lifted from cpusim/cpusim.h. maybe should be central? */
#define __REG(x) "$" #x
#define __REG1(x) #x
#define getcp0reg(source)                                       \
({ int __res;                                                   \
        __asm__ __volatile__(                                   \
        ".set\tpush\n\t"                                        \
        ".set\treorder\n\t"                                     \
        "mfc0\t%0,"__REG(source)"\n\t"                          \
        ".set\tpop"                                             \
        : "=r" (__res));                                        \
        __res;})

#define setcp0reg(register,value)                               \
        __asm__ __volatile__(                                   \
        "mtc0\t%0,"__REG(register)"\n\t"                        \
        "nop"                                                   \
        : : "r" (value));

#define delay_msec(_t) \
{ \
  u32 cur = getcp0reg(C0_COUNT); \
  while((cur+((_t)*75000))>(getcp0reg(C0_COUNT)) ){} \
}    

/* XXX: need to wrap this and other debug output functions
 *      with _DEBUG define 
 */
#if defined(SK_LOG_IDE) || defined(SK_LOG_MEM) || defined(SK_LOG_RDB)
void message(const char* s);

/* output to debug port as "0xXXXXXXXX" */
void output_int32_hex(u32 val);
void output_int32_array(u32 *d, int num);
#else
#define message(x)
#define output_int32_hex(x)
#endif

#define PRINT_LONG(_l)       \
    message( #_l ": ");          \
    output_int32_hex((_l));  \
    message("\n")

#ifdef DEBUG
#define PROFILE_OUT(_msg)                    \
    message("PROF: " #_msg ": ");            \
    output_int32_hex(getcp0reg(C0_COUNT));   \
    message("\n")
#else
#define PROFILE_OUT(_msg)
#endif

/* util to set hw and sw access rights. */
void setAccessRights(BbContentMetaDataHead *cmdh);

/* dramAddr is physical.
 * dir is SK_DMA_*_TO_*.
 * dma full flash page from indicated pi buffer.
 */
int piBufDma(u32 piBuf,u32 dramAddr,u32 bytes,u32 dir);


/* 
 * AES drivers
 */

#define PI_AES_CTRL_BASE    0x80000000
#define PI_AES_DATA_SHIFT(data)  ((data)<<PI_AES_CTRL_DATA_SHIFT)
#define PI_AES_IV_SHIFT(iv)      ((iv)<<PI_AES_CTRL_IV_SHIFT)
#define PI_AES_SIZE_SHIFT(size)  ((size)<<PI_AES_CTRL_SIZE_SHIFT)
#define POLL_AES_BUSY  do{}while((IO_READ(PI_AES_CTRL_REG))&PI_AES_CTRL_BUSY)

extern void aesHwInit(u32 *key,u32 *iv);

/* buffer can be 0 or 1 */
void aesStart(u32 buffer, u32 hardwareChaining);


int verifyRsaSigDataChain(
    SkDataChain *data,
    int number,  /* number in above chain */
    u32 *rsaPubkey,         /* size determined by sigType arg */
    BbRsaExponent rsaExp,
    u32 sigType,           /* signature type, BB_SIG_TYPE_* */
    BbGenericSig *signature /* compare against this */
    );
extern int verifyRsaSigFromHash(
     BbShaHash hash,
     u32 *rsaPubkey,         /* size determined by sigType arg */
     BbRsaExponent rsaExp,
     u32 sigType,           /* signature type, BB_SIG_TYPE_* */
     BbGenericSig *signature /* compare against this */
     );

int getRandoms(u32 *randoms, int numwords);
void computeEccSig(u8 *data, u32 datasize, BbEccPrivateKey private_key, BbEccSig sign, u32 identity);
int verifyEccSig(u8 *data, u32 datasize, BbEccPublicKey public_key, BbEccSig sign, u32 identity);
int readFlashPage(u32 pageNum,int piBuf);

extern char* strchr(const char *s, int c);
extern size_t strlen(const char *s);
extern int strcmp(const char *s,const char *t);
extern int strncmp(const char *s,const char *t, int bytes);
extern char *strstr(const char * s1,const char * s2);
extern void* memcpy(void *s1, const void *s2, size_t n);
extern void* wmemcpy(void* s1, const void* s2, size_t n);
extern int memcmp(const void *s1, const void *s2, size_t n);
extern void* memset(void *s, int c, size_t n);
extern void bzero(void *s, size_t n);


extern int v01Update(BbVirage01* v);
extern int v01Load(BbVirage01* v);

extern void getBbPublicKey(BbEccPublicKey pKey);

/* limited rights management */

extern BbTicketId gSavedTid;
extern u16 gSavedCc;
extern u16 gSavedCode;
extern s32 gTime;
extern s32 gLimit;
#define SK_TIMER_PRESCALE	(30000<<16)
#define	SK_TIMER_BASE		51200		/* 16 seconds using PRESCALE */
#define SK_TIMER_SCALE		1		/* scaling divisor to get
						   less than BASE seconds */
#define SK_TIMER_TICS		(SK_TIMER_BASE/SK_TIMER_SCALE)
#define SK_TIMER_TIC_TO_SEC(x)	(((x)<<4)/SK_TIMER_SCALE)
#define SK_TIMER_SEC_TO_TIC(x)	(((x)*SK_TIMER_SCALE)>>4)

extern u16* getCcSlot(BbTicketId tid);

extern int ptrValid(void* start, u32 len, int align);
extern int bundlePtrValid(void* start, u32 len, int align);

extern int
certChainPtrsValid(BbCertBase *chain[BB_CERT_CHAIN_MAXLEN]);

extern void skInvalDCache(void *addr, int nbytes); /* in invaldcache.s */

extern void skReboot(void);	/* in entry.s */

#ifdef SK_STACK_CHECK
#define SK_STACK_RESV_INIT_C 24
#define SK_CONTEXT_SIZE  (0x118 + 8 + SK_STACK_RESV_INIT_C) 
int stackCheckUsage();
#endif

#endif