-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibsecr.c
More file actions
executable file
·332 lines (266 loc) · 10.7 KB
/
libsecr.c
File metadata and controls
executable file
·332 lines (266 loc) · 10.7 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <stdio.h>
#include <string.h>
#include <kernel.h>
#include <sifrpc.h>
#include "libsecr.h"
#include "secrsif.h"
static SifRpcClientData_t SifRpcClient01;
static SifRpcClientData_t SifRpcClient02;
static SifRpcClientData_t SifRpcClient03;
static SifRpcClientData_t SifRpcClient04;
static SifRpcClientData_t SifRpcClient05;
static SifRpcClientData_t SifRpcClient06;
static SifRpcClientData_t SifRpcClient07;
static unsigned char RpcBuffer[0x1000] ALIGNED(64);
#define _printf(args...) //printf(args)
int SecrInit(void){
SifInitRpc(0);
nopdelay();
while(SifBindRpc(&SifRpcClient01, 0x80000A01, 0)<0 || SifRpcClient01.server==NULL){
_printf("libsecr: bind failed\n");
}
nopdelay();
while(SifBindRpc(&SifRpcClient02, 0x80000A02, 0)<0 || SifRpcClient02.server==NULL){
_printf("libsecr: bind failed\n");
}
nopdelay();
while(SifBindRpc(&SifRpcClient03, 0x80000A03, 0)<0 || SifRpcClient03.server==NULL){
_printf("libsecr: bind failed\n");
}
nopdelay();
while(SifBindRpc(&SifRpcClient04, 0x80000A04, 0)<0 || SifRpcClient04.server==NULL){
_printf("libsecr: bind failed\n");
}
nopdelay();
while(SifBindRpc(&SifRpcClient05, 0x80000A05, 0)<0 || SifRpcClient05.server==NULL){
_printf("libsecr: bind failed\n");
}
nopdelay();
while(SifBindRpc(&SifRpcClient06, 0x80000A06, 0)<0 || SifRpcClient06.server==NULL){
_printf("libsecr: bind failed\n");
}
nopdelay();
while(SifBindRpc(&SifRpcClient07, 0x80000A07, 0)<0 || SifRpcClient07.server==NULL){
_printf("libsecr: bind failed\n");
}
return 1;
}
void SecrDeinit(void){
memset(&SifRpcClient01, 0, sizeof(SifRpcClientData_t));
memset(&SifRpcClient02, 0, sizeof(SifRpcClientData_t));
memset(&SifRpcClient03, 0, sizeof(SifRpcClientData_t));
memset(&SifRpcClient04, 0, sizeof(SifRpcClientData_t));
memset(&SifRpcClient05, 0, sizeof(SifRpcClientData_t));
memset(&SifRpcClient06, 0, sizeof(SifRpcClientData_t));
memset(&SifRpcClient07, 0, sizeof(SifRpcClientData_t));
}
int SecrDownloadHeader(int port, int slot, void *buffer, SecrBitTable_t *BitTable, s32 *pSize){
int result;
((struct SecrSifDownloadHeaderParams*)RpcBuffer)->port=port;
((struct SecrSifDownloadHeaderParams*)RpcBuffer)->slot=slot;
memcpy(((struct SecrSifDownloadHeaderParams*)RpcBuffer)->buffer, buffer, sizeof(((struct SecrSifDownloadHeaderParams*)RpcBuffer)->buffer));
if(SifCallRpc(&SifRpcClient01, 1, 0, RpcBuffer, sizeof(RpcBuffer), RpcBuffer, sizeof(RpcBuffer), NULL, NULL)<0){
_printf("sceSecrDownloadHeader: rpc error\n");
result=0;
}
else{
memcpy(BitTable, &((struct SecrSifDownloadHeaderParams*)RpcBuffer)->BitTable, ((struct SecrSifDownloadHeaderParams*)RpcBuffer)->size);
//BUG: pSize doesn't seem to be filled in within the Sony original.
if(pSize!=NULL) *pSize=((struct SecrSifDownloadHeaderParams*)RpcBuffer)->size;
result=((struct SecrSifDownloadHeaderParams*)RpcBuffer)->result;
}
return result;
}
int SecrDownloadBlock(void *src, unsigned int size){
int result;
memcpy(((struct SecrSifDownloadBlockParams*)RpcBuffer)->buffer, src, sizeof(((struct SecrSifDownloadBlockParams*)RpcBuffer)->buffer));
((struct SecrSifDownloadBlockParams*)RpcBuffer)->size=size;
if(SifCallRpc(&SifRpcClient02, 1, 0, RpcBuffer, sizeof(RpcBuffer), RpcBuffer, sizeof(RpcBuffer), NULL, NULL)<0){
_printf("sceSecrDownloadBlock: rpc error\n");
result=0;
}
else{
result=((struct SecrSifDownloadBlockParams*)RpcBuffer)->result;
}
return result;
}
int SecrDownloadGetKbit(int port, int slot, void *kbit){
int result;
((struct SecrSifDownloadGetKbitParams*)RpcBuffer)->port=port;
((struct SecrSifDownloadGetKbitParams*)RpcBuffer)->slot=slot;
if(SifCallRpc(&SifRpcClient03, 1, 0, RpcBuffer, sizeof(RpcBuffer), RpcBuffer, sizeof(RpcBuffer), NULL, NULL)<0){
_printf("sceSecrDownloadGetKbit: rpc error\n");
result=0;
}
else{
memcpy(kbit, ((struct SecrSifDownloadGetKbitParams*)RpcBuffer)->kbit, sizeof(((struct SecrSifDownloadGetKbitParams*)RpcBuffer)->kbit));
result=((struct SecrSifDownloadGetKbitParams*)RpcBuffer)->result;
}
return result;
}
int SecrDownloadGetKc(int port, int slot, void *kc){
int result;
((struct SecrSifDownloadGetKcParams*)RpcBuffer)->port=port;
((struct SecrSifDownloadGetKcParams*)RpcBuffer)->slot=slot;
if(SifCallRpc(&SifRpcClient04, 1, 0, RpcBuffer, sizeof(RpcBuffer), RpcBuffer, sizeof(RpcBuffer), NULL, NULL)<0){
_printf("sceSecrDownloadGetKc: rpc error\n");
result=0;
}
else{
memcpy(kc, ((struct SecrSifDownloadGetKcParams*)RpcBuffer)->kc, sizeof(((struct SecrSifDownloadGetKcParams*)RpcBuffer)->kc));
result=((struct SecrSifDownloadGetKcParams*)RpcBuffer)->result;
}
return result;
}
int SecrDownloadGetICVPS2(void *icvps2){
int result;
if(SifCallRpc(&SifRpcClient05, 1, 0, RpcBuffer, sizeof(RpcBuffer), RpcBuffer, sizeof(RpcBuffer), NULL, NULL)<0){
_printf("sceSecrDownloadGetICVPS2: rpc error\n");
result=0;
}
else{
memcpy(icvps2, ((struct SecrSifDownloadGetIcvps2Params*)RpcBuffer)->icvps2, sizeof(((struct SecrSifDownloadGetIcvps2Params*)RpcBuffer)->icvps2));
result=((struct SecrSifDownloadGetIcvps2Params*)RpcBuffer)->result;
}
return result;
}
int SecrDiskBootHeader(void *buffer, SecrBitTable_t *BitTable, s32 *pSize){
int result;
memcpy(((struct SecrSifDiskBootHeaderParams*)RpcBuffer)->buffer, buffer, sizeof(((struct SecrSifDiskBootHeaderParams*)RpcBuffer)->buffer));
if(SifCallRpc(&SifRpcClient06, 1, 0, RpcBuffer, sizeof(RpcBuffer), RpcBuffer, sizeof(RpcBuffer), NULL, NULL)<0){
_printf("sceSecrDiskBootHeader: rpc error\n");
result=0;
}
else{
memcpy(BitTable, &((struct SecrSifDiskBootHeaderParams*)RpcBuffer)->BitTable, ((struct SecrSifDiskBootHeaderParams*)RpcBuffer)->size);
//BUG: pSize doesn't seem to be filled in within the Sony original.
if(pSize!=NULL) *pSize=((struct SecrSifDiskBootHeaderParams*)RpcBuffer)->size;
result=((struct SecrSifDiskBootHeaderParams*)RpcBuffer)->result;
}
return result;
}
int SecrDiskBootBlock(void *src, void *dst, unsigned int size){
int result;
memcpy(((struct SecrSifDiskBootBlockParams*)RpcBuffer)->source, src, size);
((struct SecrSifDiskBootBlockParams*)RpcBuffer)->size=size;
if(SifCallRpc(&SifRpcClient07, 1, 0, RpcBuffer, sizeof(RpcBuffer), RpcBuffer, sizeof(RpcBuffer), NULL, NULL)<0){
_printf("sceSecrDiskBootBlock: rpc error\n");
result=0;
}
else{
result=((struct SecrSifDiskBootBlockParams*)RpcBuffer)->result;
memcpy(dst, ((struct SecrSifDiskBootBlockParams*)RpcBuffer)->destination, size);
}
return result;
}
static unsigned short int GetHeaderLength(const void *buffer){
return ((const SecrKELFHeader_t*)buffer)->KELF_header_size;
}
static void store_kbit(void *buffer, const void *kbit){
const SecrKELFHeader_t *header=buffer;
int offset=sizeof(SecrKELFHeader_t), kbit_offset;
if(header->BIT_count>0) offset+=header->BIT_count*sizeof(SecrBitBlockData_t);
if((*(unsigned int*)&header->flags)&1) offset+=((unsigned char*)buffer)[offset]+1;
if(((*(unsigned int*)&header->flags)&0xF000)==0) offset+=8;
kbit_offset=(unsigned int)buffer+offset;
memcpy((void*)kbit_offset, kbit, 16);
_printf("kbit_offset: %d\n", kbit_offset);
}
static void store_kc(void *buffer, const void *kc){
const SecrKELFHeader_t *header=buffer;
int offset=sizeof(SecrKELFHeader_t), kc_offset;
if(header->BIT_count>0) offset+=header->BIT_count*sizeof(SecrBitBlockData_t);
if((*(unsigned int*)&header->flags)&1) offset+=((unsigned char*)buffer)[offset]+1;
if(((*(unsigned int*)&header->flags)&0xF000)==0) offset+=8;
kc_offset=(unsigned int)buffer+offset+0x10; //Goes after Kbit.
memcpy((void*)kc_offset, kc, 16);
_printf("kc_offset: %d\n", kc_offset);
}
static int Uses_ICVPS2(const void *buffer){
return(((const SecrKELFHeader_t*)buffer)->flags>>1&1);
}
static void store_icvps2(void *buffer, const void *icvps2){
unsigned int pICVPS2;
pICVPS2=(unsigned int)buffer+((SecrKELFHeader_t*)buffer)->KELF_header_size-8;
memcpy((void*)pICVPS2, icvps2, 8);
_printf("icvps2_offset %d\n", pICVPS2);
}
static unsigned int get_BitTableOffset(const void *buffer){
const SecrKELFHeader_t *header=buffer;
int offset=sizeof(SecrKELFHeader_t);
if(header->BIT_count>0) offset+=header->BIT_count*sizeof(SecrBitBlockData_t); //They used a loop for this. D:
if((*(unsigned int*)&header->flags)&1) offset+=((const unsigned char*)buffer)[offset] + 1;
if(((*(unsigned int *)&header->flags)&0xF000)==0) offset+=8;
return(offset+0x20); //Goes after Kbit and Kc.
}
void *SecrDownloadFile(int port, int slot, void *buffer){
SecrBitTable_t BitTableData;
unsigned int offset, i;
void *result;
unsigned char kbit[16], kcontent[16], icvps2[8];
_printf("SecrDownloadFile start\n");
if(SecrDownloadHeader(port, slot, buffer, &BitTableData, NULL)!=0){
if(BitTableData.header.block_count>0) {
offset=BitTableData.header.headersize;
for(i=0; i<BitTableData.header.block_count; i++){
if(BitTableData.blocks[i].flags&2){
if(!SecrDownloadBlock((void*)((unsigned int)buffer+offset), BitTableData.blocks[i].size)) {
_printf("SecrDownloadFile: failed\n");
return NULL;
}
}
offset+=BitTableData.blocks[i].size;
}
}
if(SecrDownloadGetKbit(port, slot, kbit)==0){
_printf("SecrDownloadFile: Cannot get kbit\n");
return NULL;
}
if(SecrDownloadGetKc(port, slot, kcontent)==0){
_printf("SecrDownloadFile: Cannot get kc\n");
return NULL;
}
store_kbit(buffer, kbit);
store_kc(buffer, kcontent);
if(Uses_ICVPS2(buffer)==1){
if(SecrDownloadGetICVPS2(icvps2)==0) {
_printf("SecrDownloadFile: Cannot get icvps2\n");
return NULL;
}
store_icvps2(buffer, icvps2);
}
result=buffer;
}
else{
_printf("SecrDownloadFile: Cannot encrypt header\n");
return NULL;
}
_printf("SecrDownloadFile complete\n");
return result;
}
void *SecrDiskBootFile(void *buffer){
void *result;
SecrBitTable_t *BitTableData;
unsigned int offset, i;
BitTableData=(SecrBitTable_t *)((unsigned int)buffer+get_BitTableOffset(buffer));
if(SecrDiskBootHeader(buffer, BitTableData, NULL)){
if(BitTableData->header.block_count>0){
offset=BitTableData->header.headersize;
for(i=0; i<BitTableData->header.block_count; i++){
if(BitTableData->blocks[i].flags&3){
if(!SecrDiskBootBlock((void*)((unsigned int)buffer+offset), (void*)((unsigned int)buffer+offset), BitTableData->blocks[i].size)) {
_printf("SecrDiskBootFile: failed\n");
return NULL;
}
}
offset+=BitTableData->blocks[i].size;
}
}
result=(void*)((unsigned int)buffer+GetHeaderLength(buffer));
}
else{
_printf("sceSecrDiskBootFile: Cannot decrypt header\n");
result=NULL;
}
return result;
}