2015-08-21 20:11:23 +02:00
|
|
|
/*
|
|
|
|
* patches.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "patches.h"
|
2016-02-08 03:37:03 +01:00
|
|
|
#include "memory.h"
|
2015-08-21 20:11:23 +02:00
|
|
|
|
|
|
|
/**************************************************
|
|
|
|
* Patches
|
|
|
|
**************************************************/
|
|
|
|
|
2016-03-26 17:24:16 +01:00
|
|
|
const u32 mpuPatch[3] = {0x00360003, 0x00200603, 0x001C0603};
|
2016-02-08 03:37:03 +01:00
|
|
|
|
2016-04-26 20:06:31 +02:00
|
|
|
const u16 nandRedir[2] = {0x4C00, 0x47A0},
|
|
|
|
sigPatch[2] = {0x2000, 0x4770},
|
2016-05-03 01:17:22 +02:00
|
|
|
writeBlock[2] = {0x2000, 0x46C0},
|
|
|
|
writeBlockSafe[2] = {0x2400, 0xE01D};
|
2016-03-06 16:24:42 +01:00
|
|
|
|
2016-05-12 02:59:21 +02:00
|
|
|
//Official implementation of svcBackdoor
|
|
|
|
const u8 svcBackdoor[40] = {0xFF, 0x10, 0xCD, 0xE3, //bic r1, sp, #0xff
|
|
|
|
0x0F, 0x1C, 0x81, 0xE3, //orr r1, r1, #0xf00
|
|
|
|
0x28, 0x10, 0x81, 0xE2, //add r1, r1, #0x28
|
|
|
|
0x00, 0x20, 0x91, 0xE5, //ldr r2, [r1]
|
|
|
|
0x00, 0x60, 0x22, 0xE9, //stmdb r2!, {sp, lr}
|
|
|
|
0x02, 0xD0, 0xA0, 0xE1, //mov sp, r2
|
|
|
|
0x30, 0xFF, 0x2F, 0xE1, //blx r0
|
|
|
|
0x03, 0x00, 0xBD, 0xE8, //pop {r0, r1}
|
|
|
|
0x00, 0xD0, 0xA0, 0xE1, //mov sp, r0
|
|
|
|
0x11, 0xFF, 0x2F, 0xE1}; //bx r1
|
|
|
|
|
2015-08-21 20:11:23 +02:00
|
|
|
/**************************************************
|
|
|
|
* Functions
|
|
|
|
**************************************************/
|
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
u8 *getProcess9(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-05-11 19:28:28 +02:00
|
|
|
u8 *off = memsearch(pos, "ess9", size, 4);
|
|
|
|
|
2016-05-12 02:59:21 +02:00
|
|
|
*process9Size = *(u32 *)(off - 0x60) * 0x200;
|
2016-05-11 19:28:28 +02:00
|
|
|
*process9MemAddr = *(u32 *)(off + 0xC);
|
|
|
|
|
|
|
|
//Process9 code offset (start of NCCH + ExeFS offset + ExeFS header size)
|
|
|
|
return off - 0x204 + (*(u32 *)(off - 0x64) * 0x200) + 0x200;
|
2016-03-21 18:56:41 +01:00
|
|
|
}
|
|
|
|
|
2016-05-12 13:35:39 +02:00
|
|
|
void getSigChecks(u8 *pos, u32 size, u16 **off, u16 **off2)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-02-08 03:37:03 +01:00
|
|
|
//Look for signature checks
|
2016-03-29 17:43:53 +02:00
|
|
|
const u8 pattern[] = {0xC0, 0x1C, 0x76, 0xE7},
|
|
|
|
pattern2[] = {0xB5, 0x22, 0x4D, 0x0C};
|
2015-08-21 20:11:23 +02:00
|
|
|
|
2016-05-12 13:35:39 +02:00
|
|
|
*off = (u16 *)memsearch(pos, pattern, size, 4);
|
|
|
|
*off2 = (u16 *)(memsearch(pos, pattern2, size, 4) - 1);
|
2015-08-21 20:11:23 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
void *getReboot(u8 *pos, u32 size, u32 process9MemAddr, u32 *fOpenOffset)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-02-08 03:37:03 +01:00
|
|
|
//Look for FIRM reboot code
|
2016-03-26 19:21:17 +01:00
|
|
|
const u8 pattern[] = {0xDE, 0x1F, 0x8D, 0xE2};
|
2015-08-21 20:11:23 +02:00
|
|
|
|
2016-05-11 19:28:28 +02:00
|
|
|
u8 *off = memsearch(pos, pattern, size, 4) - 0x10;
|
2016-03-21 20:24:12 +01:00
|
|
|
|
2016-03-26 17:24:16 +01:00
|
|
|
//Firmlaunch function offset - offset in BLX opcode (A4-16 - ARM DDI 0100E) + 1
|
2016-05-12 13:35:39 +02:00
|
|
|
*fOpenOffset = (u32)(off + 9 - (-((*(u32 *)off & 0x00FFFFFF) << 2) & (0xFFFFFF << 2)) - pos + process9MemAddr);
|
2016-05-11 19:28:28 +02:00
|
|
|
|
|
|
|
return off;
|
2016-02-19 21:32:07 +01:00
|
|
|
}
|
|
|
|
|
2016-04-02 17:58:06 +02:00
|
|
|
u16 *getFirmWrite(u8 *pos, u32 size)
|
|
|
|
{
|
2016-02-19 21:32:07 +01:00
|
|
|
//Look for FIRM writing code
|
2016-03-29 17:43:53 +02:00
|
|
|
u8 *const off = memsearch(pos, "exe:", size, 4);
|
2016-03-26 19:21:17 +01:00
|
|
|
const u8 pattern[] = {0x00, 0x28, 0x01, 0xDA};
|
2016-02-19 21:32:07 +01:00
|
|
|
|
2016-03-26 17:24:16 +01:00
|
|
|
return (u16 *)memsearch(off - 0x100, pattern, 0x100, 4);
|
2016-03-29 17:43:53 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:17:22 +02:00
|
|
|
u16 *getFirmWriteSafe(u8 *pos, u32 size)
|
|
|
|
{
|
|
|
|
//Look for FIRM writing code
|
|
|
|
const u8 pattern[] = {0x04, 0x1E, 0x1D, 0xDB};
|
|
|
|
|
|
|
|
return (u16 *)memsearch(pos, pattern, size, 4);
|
|
|
|
}
|
|
|
|
|
2016-05-09 03:41:00 +02:00
|
|
|
u32 getLoader(u8 *pos, u32 *loaderSize)
|
2016-04-11 21:14:51 +02:00
|
|
|
{
|
2016-05-07 19:16:53 +02:00
|
|
|
u8 *off = pos;
|
2016-05-09 03:41:00 +02:00
|
|
|
u32 size;
|
|
|
|
|
|
|
|
while(1)
|
2016-05-07 19:16:53 +02:00
|
|
|
{
|
2016-05-09 03:41:00 +02:00
|
|
|
size = *(u32 *)(off + 0x104) * 0x200;
|
|
|
|
if(*(u32 *)(off + 0x200) == 0x64616F6C) break;
|
|
|
|
off += size;
|
2016-05-07 19:16:53 +02:00
|
|
|
}
|
2016-05-09 03:41:00 +02:00
|
|
|
|
|
|
|
*loaderSize = size;
|
|
|
|
|
|
|
|
return (u32)(off - pos);
|
2016-05-10 23:06:32 +02:00
|
|
|
}
|
|
|
|
|
2016-05-12 12:24:14 +02:00
|
|
|
u32 *getSvcAndExceptions(u8 *pos, u32 size, u32 **exceptionsPage)
|
2016-05-10 23:06:32 +02:00
|
|
|
{
|
2016-05-12 12:24:14 +02:00
|
|
|
const u8 pattern[] = {0x00, 0xB0, 0x9C, 0xE5}; //cpsid aif
|
2016-05-10 23:06:32 +02:00
|
|
|
|
2016-05-12 14:48:52 +02:00
|
|
|
*exceptionsPage = (u32 *)memsearch(pos, pattern, size, 4) - 0xB;
|
2016-05-12 02:59:21 +02:00
|
|
|
|
2016-05-12 12:24:14 +02:00
|
|
|
u32 svcOffset = (-(((*exceptionsPage)[2] & 0xFFFFFF) << 2) & (0xFFFFFF << 2)) - 8; //Branch offset + 8 for prefetch
|
2016-05-12 02:59:21 +02:00
|
|
|
u32 *svcTable = (u32 *)(pos + *(u32 *)(pos + 0xFFFF0008 - svcOffset - 0xFFF00000 + 8) - 0xFFF00000); //SVC handler address
|
|
|
|
while(*svcTable) svcTable++; //Look for SVC0 (NULL)
|
|
|
|
|
|
|
|
return svcTable;
|
2016-05-11 19:28:28 +02:00
|
|
|
}
|