v2Const.c
5.59 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <netinet/in.h>
#include <sha1.h>
#include <PR/bcp.h>
#include <PR/bbvirage.h>
#include <PR/bbnand.h>
#include "../../lib/bbboot/boot.h"
#include <algorithms.h>
#define H2BE4(a) (htonl((a)))
#define BE2H4(a) (ntohl((a)))
#define MIPS_JR_T0 0x01000008
#define MIPS_NOP 0x00000000
#ifndef u32
#define u32 unsigned int
#endif
#define OPTARG "k:hne"
#define MAXERRS 128
#define VERBOSE(x)
int asciiToBin(char *str,int bytes,u8 *buf)
{
int i,j;
char t;
for(i=0;i<bytes;i++){
t = str[2*i];
if ((t >= '0') && (t <= '9'))
j = (t - '0') << 4;
else if ((t >= 'a') && (t <= 'f'))
j = (t - 'a' + 10) << 4;
else if ((t >= 'A') && (t <= 'F'))
j = (t - 'A' + 10) << 4;
else return -1;
t = str[2*i+1];
if ((t >= '0') && (t <= '9'))
j ^= (t - '0');
else if ((t >= 'a') && (t <= 'f'))
j ^= (t - 'a' + 10);
else if ((t >= 'A') && (t <= 'F'))
j ^= (t - 'A' + 10);
else return -1;
buf[i]=j;
}
return 0;
}
void usage()
{
printf("Usage:\n");
printf(" writeV2 [opts] <skBinaryIN> <v2ConstOUT>\n");
printf("where opts are:\n");
printf(" -k <16 bytes of ascii-represented hex> \n");
printf(" -n patch boot code to skip SHA compare\n");
printf(" -e put enable code bb2002fb\n");
printf(" -h This message.\n");
}
int main(int argc,char *argv[])
{
int i;
u32 fbytes,aesbytes,*viragePatch;
u8 *indata=NULL;
/*
FILE *fpOut=NULL;
*/
FILE *fpIn=NULL;
FILE *v2ConstantsPtr;
SHA1Context sha;
BbVirage2 virage2;
u8 *messageDigest;
/* for getopt usage */
extern int optind;
extern char *optarg;
int c, noSHA = 0;
int enable = 0;
messageDigest = (u8 *)(virage2.skHash);
memset(&virage2,0,sizeof(virage2));
/* default bootAppKey, changed by -k arg */
virage2.bootAppKey[0] = H2BE4(0xb8190276);
virage2.bootAppKey[1] = H2BE4(0x7e25db27);
virage2.bootAppKey[2] = H2BE4(0x0f3449c5);
virage2.bootAppKey[3] = H2BE4(0xd94b162f);
optind = 1;
while ((c = getopt(argc, argv, OPTARG)) != EOF) {
switch (c) {
case 'k':
if (argc <= 2) goto usage;
asciiToBin(optarg,sizeof(BbAesKey),(u8 *)(virage2.bootAppKey));
break;
case 'n':
noSHA = 1;
break;
case 'e':
enable = 1;
break;
case 'h': /* Help */
default:
usage:
usage();
return 1;
}
} /* while */
if( (argc-optind)!=2){
printf("incorrect number of args!\n\n");
usage();
return 1;
}
v2ConstantsPtr = fopen(argv[optind+1], "w");
if(v2ConstantsPtr == NULL){
fprintf(stderr, " couldnt create v2 constants file\n");
exit(1);
}
if(noSHA){
for(i=0;i<20;i++)
messageDigest[i]=0;
}
else{
if( (fpIn = fopen(argv[optind],"r"))==NULL ){
printf("Error: failed to open input file %s\n",argv[optind]);
return 1;
}
aesbytes = LOAD_SK_PAGES*PI_FLASH_PAGE_DATA_SIZE;
if( (indata = (u8 *)malloc(aesbytes))==NULL ){
printf("Error: failed to allocate %d bytes.\n",aesbytes);
return 1;
}
fbytes = fread(indata,1,aesbytes,fpIn);
fclose(fpIn);
if(fbytes != aesbytes){
free(indata);
printf("SK size wrong (according to LOAD_SK_PAGES define):\n");
printf(" expected %08x bytes, read %08x bytes.\n",aesbytes,fbytes);
return 1;
}
/* hash input extended to 16B multiple, prepended by length */
SHA1Reset(&sha);
SHA1Input(&sha,indata,aesbytes);
SHA1Result(&sha,messageDigest);
}
/*
* write virage2 file with hash value, bootcode patches,
* and checksum.
*/
/* patch codes */
#define PATCH_INSTRUCTION(I) \
*(viragePatch++) = H2BE4((I)); \
VERBOSE(printf("adding = %08x\n",(I)))
#define MIPS_B 0x10000000
#define MIPS_LUI_A3 0x3c070000
#define MIPS_LUI_T1 0x3c090000
#define MIPS_ORI_T1 0x35290000
#define MIPS_SW_T1_A3 0xace90000
viragePatch = (u32 *)(virage2.romPatch);
if (noSHA) {
/* Jump over patch 2 */
PATCH_INSTRUCTION(MIPS_B|0x3);
PATCH_INSTRUCTION(MIPS_NOP);
/* Patch 2 goes here
PATCH_INSTRUCTION(MIPS_JR_TO);
PATCH_INSTRUCTION(MIPS_JR_NOP);
*/
viragePatch += 2;
/* patch a j 0xfc40854 @ 0xbfc40770 */
PATCH_INSTRUCTION(MIPS_LUI_T1|0x0bf1);
PATCH_INSTRUCTION(MIPS_ORI_T1|0x0215);
PATCH_INSTRUCTION(MIPS_LUI_A3|0xbfc4);
PATCH_INSTRUCTION(MIPS_SW_T1_A3|0x0770);
PATCH_INSTRUCTION(MIPS_JR_T0);
PATCH_INSTRUCTION(MIPS_NOP);
viragePatch = (u32 *)(virage2.romPatch) + 2;
} else {
PATCH_INSTRUCTION(MIPS_JR_T0);
PATCH_INSTRUCTION(MIPS_NOP);
}
PATCH_INSTRUCTION(MIPS_JR_T0);
PATCH_INSTRUCTION(MIPS_NOP);
/*
*write constants to virage constants file
*/
if(enable == 0){
virage2.jtagEnable = htonl(0x01234567);
}
else{
virage2.jtagEnable = htonl(0xbb2002fb);
}
fwrite(virage2.skHash, 4, 5, v2ConstantsPtr);
fwrite(virage2.romPatch, 4, 16, v2ConstantsPtr);
fwrite(virage2.bootAppKey, 4, 4, v2ConstantsPtr);
fwrite(&(virage2.jtagEnable), 4, 1, v2ConstantsPtr);
/*exit_cleanup:*/
if(indata)
free(indata);
return 0;
}