Permalink
Browse files

Add boilerplate code for kip patching; refactor

  • Loading branch information...
Reisyukaku committed Jul 27, 2018
1 parent 2bb0b8c commit 23ca8cf2e85fffce5dfae747da9368d623ae1c9a
Showing with 21 additions and 14 deletions.
  1. +12 −7 src/firmware.c
  2. +0 −2 src/hwinit/util.c
  3. +4 −4 src/package.c
  4. +5 −1 src/package.h
View
@@ -27,8 +27,6 @@
#define VERSION "v0.1"
static pk11_offs *pk11Offs = NULL;
static u8 customSecmon = 0;
static u8 customWarmboot = 0;
// TODO: Maybe find these with memsearch
static const pk11_offs _pk11_offs[] = {
@@ -111,7 +109,8 @@ void loadKip(link_t *info, char *path) {
list_append(info, &ki->link);
}
void patch(pk11_offs *pk11, pkg2_hdr_t *pkg2) {
void patch(pk11_offs *pk11, pkg2_hdr_t *pkg2, link_t *kips) {
//Secmon patches
if(!customSecmon){
uPtr *rlc_ptr = NULL;
uPtr *ver_ptr = NULL;
@@ -177,6 +176,14 @@ void patch(pk11_offs *pk11, pkg2_hdr_t *pkg2) {
*hdrsig_ptr = NOP;
*sha2_ptr = NOP;
}
if(!customKern) {
//TODO
}
LIST_FOREACH_ENTRY(pkg2_kip1_info_t, ki, kips, link) {
if(ki->kip1->tid == 0x0100000000000001) {
//TODO
}
}
}
int keygen(u8 *keyblob, u32 fwVer, void *tsec_fw) {
@@ -265,9 +272,7 @@ u8 loadFirm() {
u8 *pkg11 = package1 + pk11Offs->pkg11_off;
u32 pkg11_size = *(u32 *)pkg11;
se_aes_crypt_ctr(11, pkg11 + 0x20, pkg11_size, pkg11 + 0x20, pkg11_size, pkg11 + 0x10);
ret = pkg1_unpack(pk11Offs, package1);
customWarmboot = ret & 1;
customSecmon = ret & 2;
pkg1_unpack(pk11Offs, package1);
PMC(APBDEV_PMC_SCRATCH1) = pk11Offs->warmboot_base;
free(package1);
@@ -307,7 +312,7 @@ u8 loadFirm() {
// Patch firmware.
print("Patching OS...\n");
patch(pk11Offs, dec_pkg2);
patch(pk11Offs, dec_pkg2, &kip1_info);
// Load all KIPs.
char **sysmods = NULL;
View
@@ -65,14 +65,12 @@ uPtr memsearch(const u8 *startPos, u32 searchSize, const void *pattern, u32 patt
return 0;
}
typedef struct lsf_head {
u32 loc;
u32 size;
u32 filesize;
} lsf_head;
char * blz_decompress(unsigned char * compressed, u32 * isize) {
u32 size = *isize;
u32 compressed_size;
View
@@ -46,7 +46,7 @@ pkg2_hdr_t *unpackFirmwarePackage(u8 *data) {
return hdr;
}
u8 pkg1_unpack(pk11_offs *offs, u8 *pkg1) {
void pkg1_unpack(pk11_offs *offs, u8 *pkg1) {
u8 ret = 0;
u8 *extWb;
u8 *extSec;
@@ -78,13 +78,12 @@ u8 pkg1_unpack(pk11_offs *offs, u8 *pkg1) {
}
if(extWb != NULL) {
free(extWb);
ret |= 1;
customWarmboot = 1;
}
if(extSec != NULL) {
free(extSec);
ret |= 2;
customSecmon = 1;
}
return ret;
}
void buildFirmwarePackage(u8 *kernel, u32 kernel_size, link_t *kips_info) {
@@ -109,6 +108,7 @@ void buildFirmwarePackage(u8 *kernel, u32 kernel_size, link_t *kips_info) {
fread(extKern, fsize(), 1);
fclose();
}
if(extKern != NULL) customKern = 1;
memcpy(pdst, extKern == NULL ? kernel : extKern, kernel_size);
hdr->sec_size[PKG2_SEC_KERNEL] = kernel_size;
hdr->sec_off[PKG2_SEC_KERNEL] = 0x10000000;
View
@@ -8,6 +8,10 @@
#define INI1_MAGIC 0x31494E49
static u8 customSecmon = 0;
static u8 customWarmboot = 0;
static u8 customKern = 0;
typedef struct _pkg2_hdr_t
{
u8 ctr[0x10];
@@ -86,5 +90,5 @@ typedef struct {
} pk11_header;
pkg2_hdr_t *unpackFirmwarePackage(u8 *data);
u8 pkg1_unpack(pk11_offs *offs, u8 *pkg1);
void pkg1_unpack(pk11_offs *offs, u8 *pkg1);
void buildFirmwarePackage(u8 *kernel, u32 kernel_size, link_t *kips_info);

0 comments on commit 23ca8cf

Please sign in to comment.