17static unsigned char RpcBuffer[0x1000] ALIGNED(64);
19#define _printf(args...)
26 while (SifBindRpc(&SifRpcClient01, 0x80000A01, 0) < 0 || SifRpcClient01.server == NULL) {
27 _printf(
"libsecr: bind failed\n");
31 while (SifBindRpc(&SifRpcClient02, 0x80000A02, 0) < 0 || SifRpcClient02.server == NULL) {
32 _printf(
"libsecr: bind failed\n");
36 while (SifBindRpc(&SifRpcClient03, 0x80000A03, 0) < 0 || SifRpcClient03.server == NULL) {
37 _printf(
"libsecr: bind failed\n");
41 while (SifBindRpc(&SifRpcClient04, 0x80000A04, 0) < 0 || SifRpcClient04.server == NULL) {
42 _printf(
"libsecr: bind failed\n");
46 while (SifBindRpc(&SifRpcClient05, 0x80000A05, 0) < 0 || SifRpcClient05.server == NULL) {
47 _printf(
"libsecr: bind failed\n");
51 while (SifBindRpc(&SifRpcClient06, 0x80000A06, 0) < 0 || SifRpcClient06.server == NULL) {
52 _printf(
"libsecr: bind failed\n");
56 while (SifBindRpc(&SifRpcClient07, 0x80000A07, 0) < 0 || SifRpcClient07.server == NULL) {
57 _printf(
"libsecr: bind failed\n");
74int SecrDownloadHeader(
int port,
int slot,
void *buffer,
SecrBitTable_t *BitTable, s32 *pSize)
82 if (SifCallRpc(&SifRpcClient01, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
83 _printf(
"sceSecrDownloadHeader: rpc error\n");
96int SecrDownloadBlock(
void *src,
unsigned int size)
103 if (SifCallRpc(&SifRpcClient02, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
104 _printf(
"sceSecrDownloadBlock: rpc error\n");
113int SecrDownloadGetKbit(
int port,
int slot,
void *kbit)
120 if (SifCallRpc(&SifRpcClient03, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
121 _printf(
"sceSecrDownloadGetKbit: rpc error\n");
131int SecrDownloadGetKc(
int port,
int slot,
void *kc)
138 if (SifCallRpc(&SifRpcClient04, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
139 _printf(
"sceSecrDownloadGetKc: rpc error\n");
149int SecrDownloadGetICVPS2(
void *icvps2)
153 if (SifCallRpc(&SifRpcClient05, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
154 _printf(
"sceSecrDownloadGetICVPS2: rpc error\n");
164int SecrDiskBootHeader(
void *buffer,
SecrBitTable_t *BitTable, s32 *pSize)
170 if (SifCallRpc(&SifRpcClient06, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
171 _printf(
"sceSecrDiskBootHeader: rpc error\n");
184int SecrDiskBootBlock(
void *src,
void *dst,
unsigned int size)
191 if (SifCallRpc(&SifRpcClient07, 1, 0, RpcBuffer,
sizeof(RpcBuffer), RpcBuffer,
sizeof(RpcBuffer), NULL, NULL) < 0) {
192 _printf(
"sceSecrDiskBootBlock: rpc error\n");
202static unsigned short int GetHeaderLength(
const void *buffer)
207static void store_kbit(
void *buffer,
const void *kbit)
210 int offset = 0x20, kbit_offset;
214 if (((header->
flags) & 1) != 0)
215 offset += ((
unsigned char *)buffer)[offset] + 1;
216 if (((header->
flags) & 0xF000) == 0)
219 kbit_offset = (
unsigned int)buffer + offset;
220 memcpy((
void *)kbit_offset, kbit, 16);
221 _printf(
"kbit_offset: %d\n", kbit_offset);
224static void store_kc(
void *buffer,
const void *kc)
227 int offset = 0x20, kc_offset;
231 if (((header->
flags) & 1) != 0)
232 offset += ((
unsigned char *)buffer)[offset] + 1;
233 if (((header->
flags) & 0xF000) == 0)
236 kc_offset = (
unsigned int)buffer + offset + 0x10;
237 memcpy((
void *)kc_offset, kc, 16);
238 _printf(
"kc_offset: %d\n", kc_offset);
241static int Uses_ICVPS2(
const void *buffer)
246static void store_icvps2(
void *buffer,
const void *icvps2)
248 unsigned int pICVPS2;
250 pICVPS2 = (
unsigned int)buffer + ((
SecrKELFHeader_t *)buffer)->KELF_header_size - 8;
251 memcpy((
void *)pICVPS2, icvps2, 8);
252 _printf(
"icvps2_offset %d\n", pICVPS2);
255static unsigned int get_BitTableOffset(
const void *buffer)
262 if ((header->
flags & 1) != 0)
263 offset += ((
const unsigned char *)buffer)[offset] + 1;
264 if ((header->
flags & 0xF000) == 0)
266 return (offset + 0x20);
269void *SecrDownloadFile(
int port,
int slot,
void *buffer)
274 _printf(
"SecrDownloadFile start\n");
275 if (SecrDownloadHeader(port, slot, buffer, &BitTableData, NULL) != 0) {
276 unsigned char kbit[16], kcontent[16];
279 unsigned int offset, i;
282 for (i = 0; i < BitTableData.header.
block_count; i++) {
284 if (!SecrDownloadBlock((
void *)((
unsigned int)buffer + offset), BitTableData.
blocks[i].
size)) {
285 _printf(
"SecrDownloadFile: failed\n");
293 if (SecrDownloadGetKbit(port, slot, kbit) == 0) {
294 _printf(
"SecrDownloadFile: Cannot get kbit\n");
297 if (SecrDownloadGetKc(port, slot, kcontent) == 0) {
298 _printf(
"SecrDownloadFile: Cannot get kc\n");
302 store_kbit(buffer, kbit);
303 store_kc(buffer, kcontent);
305 if (Uses_ICVPS2(buffer) == 1) {
306 unsigned char icvps2[8];
308 if (SecrDownloadGetICVPS2(icvps2) == 0) {
309 _printf(
"SecrDownloadFile: Cannot get icvps2\n");
313 store_icvps2(buffer, icvps2);
318 _printf(
"SecrDownloadFile: Cannot encrypt header\n");
322 _printf(
"SecrDownloadFile complete\n");
327void *SecrDiskBootFile(
void *buffer)
332 BitTableData = (
SecrBitTable_t *)((
unsigned int)buffer + get_BitTableOffset(buffer));
333 if (SecrDiskBootHeader(buffer, BitTableData, NULL)) {
335 unsigned int offset, i;
338 for (i = 0; i < BitTableData->header.
block_count; i++) {
340 if (!SecrDiskBootBlock((
void *)((
unsigned int)buffer + offset), (
void *)((
unsigned int)buffer + offset), BitTableData->
blocks[i].
size)) {
341 _printf(
"SecrDiskBootFile: failed\n");
349 result = (
void *)((
unsigned int)buffer + GetHeaderLength(buffer));
351 _printf(
"sceSecrDiskBootFile: Cannot decrypt header\n");
struct KELF_Header SecrKELFHeader_t
SecrBitBlockData_t blocks[63]
struct SecrBitBlockData SecrBitBlockData_t