util.h
5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#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