18 static unsigned char RpcBuffer[0x1000] ALIGNED(64);
20 #define _printf(args...) // printf(args)
30 while (sceSifBindRpc(&SifRpcClient01, 0x80000A01, 0) < 0 || SifRpcClient01.server == NULL) {
31 _printf(
"libsecr: bind failed\n");
35 while (sceSifBindRpc(&SifRpcClient02, 0x80000A02, 0) < 0 || SifRpcClient02.server == NULL) {
36 _printf(
"libsecr: bind failed\n");
40 while (sceSifBindRpc(&SifRpcClient03, 0x80000A03, 0) < 0 || SifRpcClient03.server == NULL) {
41 _printf(
"libsecr: bind failed\n");
45 while (sceSifBindRpc(&SifRpcClient04, 0x80000A04, 0) < 0 || SifRpcClient04.server == NULL) {
46 _printf(
"libsecr: bind failed\n");
50 while (sceSifBindRpc(&SifRpcClient05, 0x80000A05, 0) < 0 || SifRpcClient05.server == NULL) {
51 _printf(
"libsecr: bind failed\n");
55 while (sceSifBindRpc(&SifRpcClient06, 0x80000A06, 0) < 0 || SifRpcClient06.server == NULL) {
56 _printf(
"libsecr: bind failed\n");
60 while (sceSifBindRpc(&SifRpcClient07, 0x80000A07, 0) < 0 || SifRpcClient07.server == NULL) {
61 _printf(
"libsecr: bind failed\n");
78 int SecrDownloadHeader(
int port,
int slot,
void *buffer,
SecrBitTable_t *BitTable, s32 *pSize)
86 if (sceSifCallRpc(&SifRpcClient01, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
87 _printf(
"sceSecrDownloadHeader: rpc error\n");
100 int SecrDownloadBlock(
void *src,
unsigned int size)
107 if (sceSifCallRpc(&SifRpcClient02, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
108 _printf(
"sceSecrDownloadBlock: rpc error\n");
117 int SecrDownloadGetKbit(
int port,
int slot,
void *kbit)
124 if (sceSifCallRpc(&SifRpcClient03, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
125 _printf(
"sceSecrDownloadGetKbit: rpc error\n");
135 int SecrDownloadGetKc(
int port,
int slot,
void *kc)
142 if (sceSifCallRpc(&SifRpcClient04, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
143 _printf(
"sceSecrDownloadGetKc: rpc error\n");
153 int SecrDownloadGetICVPS2(
void *icvps2)
157 if (sceSifCallRpc(&SifRpcClient05, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
158 _printf(
"sceSecrDownloadGetICVPS2: rpc error\n");
168 int SecrDiskBootHeader(
void *buffer,
SecrBitTable_t *BitTable, s32 *pSize)
174 if (sceSifCallRpc(&SifRpcClient06, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
175 _printf(
"sceSecrDiskBootHeader: rpc error\n");
188 int SecrDiskBootBlock(
void *src,
void *dst,
unsigned int size)
195 if (sceSifCallRpc(&SifRpcClient07, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
196 _printf(
"sceSecrDiskBootBlock: rpc error\n");
206 static unsigned short int GetHeaderLength(
const void *buffer)
211 static void store_kbit(
void *buffer,
const void *kbit)
214 int offset = 0x20, kbit_offset;
218 if (((header->
flags) & 1) != 0)
219 offset += ((
unsigned char *)buffer)[offset] + 1;
220 if (((header->
flags) & 0xF000) == 0)
223 kbit_offset = (
unsigned int)buffer + offset;
224 memcpy((
void *)kbit_offset, kbit, 16);
225 _printf(
"kbit_offset: %d\n", kbit_offset);
228 static void store_kc(
void *buffer,
const void *kc)
231 int offset = 0x20, kc_offset;
235 if (((header->
flags) & 1) != 0)
236 offset += ((
unsigned char *)buffer)[offset] + 1;
237 if (((header->
flags) & 0xF000) == 0)
240 kc_offset = (
unsigned int)buffer + offset + 0x10;
241 memcpy((
void *)kc_offset, kc, 16);
242 _printf(
"kc_offset: %d\n", kc_offset);
245 static int Uses_ICVPS2(
const void *buffer)
250 static void store_icvps2(
void *buffer,
const void *icvps2)
252 unsigned int pICVPS2;
254 pICVPS2 = (
unsigned int)buffer + ((
SecrKELFHeader_t *)buffer)->KELF_header_size - 8;
255 memcpy((
void *)pICVPS2, icvps2, 8);
256 _printf(
"icvps2_offset %d\n", pICVPS2);
259 static unsigned int get_BitTableOffset(
const void *buffer)
266 if ((header->
flags & 1) != 0)
267 offset += ((
const unsigned char *)buffer)[offset] + 1;
268 if ((header->
flags & 0xF000) == 0)
270 return (offset + 0x20);
273 void *SecrDownloadFile(
int port,
int slot,
void *buffer)
278 _printf(
"SecrDownloadFile start\n");
279 if (SecrDownloadHeader(port, slot, buffer, &BitTableData, NULL) != 0) {
280 unsigned char kbit[16], kcontent[16];
283 unsigned int offset, i;
286 for (i = 0; i < BitTableData.header.
block_count; i++) {
288 if (!SecrDownloadBlock((
void *)((
unsigned int)buffer + offset), BitTableData.
blocks[i].
size)) {
289 _printf(
"SecrDownloadFile: failed\n");
297 if (SecrDownloadGetKbit(port, slot, kbit) == 0) {
298 _printf(
"SecrDownloadFile: Cannot get kbit\n");
301 if (SecrDownloadGetKc(port, slot, kcontent) == 0) {
302 _printf(
"SecrDownloadFile: Cannot get kc\n");
306 store_kbit(buffer, kbit);
307 store_kc(buffer, kcontent);
309 if (Uses_ICVPS2(buffer) == 1) {
310 unsigned char icvps2[8];
312 if (SecrDownloadGetICVPS2(icvps2) == 0) {
313 _printf(
"SecrDownloadFile: Cannot get icvps2\n");
317 store_icvps2(buffer, icvps2);
322 _printf(
"SecrDownloadFile: Cannot encrypt header\n");
326 _printf(
"SecrDownloadFile complete\n");
331 void *SecrDiskBootFile(
void *buffer)
336 BitTableData = (
SecrBitTable_t *)((
unsigned int)buffer + get_BitTableOffset(buffer));
337 if (SecrDiskBootHeader(buffer, BitTableData, NULL)) {
339 unsigned int offset, i;
342 for (i = 0; i < BitTableData->header.
block_count; i++) {
344 if (!SecrDiskBootBlock((
void *)((
unsigned int)buffer + offset), (
void *)((
unsigned int)buffer + offset), BitTableData->
blocks[i].
size)) {
345 _printf(
"SecrDiskBootFile: failed\n");
353 result = (
void *)((
unsigned int)buffer + GetHeaderLength(buffer));
355 _printf(
"sceSecrDiskBootFile: Cannot decrypt header\n");