Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Reisyukaku committed Oct 18, 2018
2 parents ed2d5eb + d18c962 commit e350bb5
Showing 1 changed file with 58 additions and 46 deletions.
104 changes: 58 additions & 46 deletions src/firmware.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,59 @@ pk11_offs *pkg11_offsentify(u8 *pkg1) {
return NULL;
}

void patchFS(pkg2_kip1_info_t* ki) {
u8 kipHash[0x20];

print("Patching FS\n");

se_calc_sha256(&kipHash, ki->kip1, ki->size);
se_calc_sha256(&kipHash, ki->kip1, ki->size);

//Create header
size_t sizeDiff = ki->kip1->sections[0].size_decomp - ki->kip1->sections[0].size_comp;

size_t newSize = ki->size + sizeDiff;
pkg2_kip1_t *moddedKip = malloc(newSize);
memcpy(moddedKip, ki->kip1, newSize);

u32 pos = 0;
//Get decomp .text segment
u8 *kipDecompText = blz_decompress(moddedKip->data, moddedKip->sections[0].size_comp);

kippatchset_t *pset = kippatch_find_set(kipHash, kip_patches);
if (!pset) {
print(" could not find patchset with matching hash\n");
} else {
int res = kippatch_apply_set(kipDecompText, moddedKip->sections[0].size_decomp, pset);
if (res) error("kippatch_apply_set() failed\n");
}

moddedKip->flags &= ~1;
memcpy((void*)moddedKip->data, kipDecompText, moddedKip->sections[0].size_decomp);
free(kipDecompText);
pos += moddedKip->sections[0].size_comp;
moddedKip->sections[0].size_comp = moddedKip->sections[0].size_decomp;

for(int i = 1; i < KIP1_NUM_SECTIONS; i++) {
if(moddedKip->sections[i].offset != 0) {
memcpy((void*)moddedKip->data + pos + sizeDiff, (void*)ki->kip1->data + pos, moddedKip->sections[i].size_comp);
pos += moddedKip->sections[i].size_comp;
}
}

free(ki->kip1);
ki->size = newSize;
ki->kip1 = moddedKip;
}

pkg2_kip1_info_t* find_by_tid(link_t* kip_list, u64 tid) {
LIST_FOREACH_ENTRY(pkg2_kip1_info_t, ki, kip_list, link) {
if(ki->kip1->tid == 0x0100000000000000)
return ki;
}
return NULL;
}

void patch(pk11_offs *pk11, pkg2_hdr_t *pkg2, link_t *kips) {
//Patch Secmon
if(!customSecmon){
Expand Down Expand Up @@ -224,53 +277,12 @@ void patch(pk11_offs *pk11, pkg2_hdr_t *pkg2, link_t *kips) {

end:;
}

u8 kipHash[0x20];

//Patch FS module (truly not my proudest code TODO cleanup)
LIST_FOREACH_ENTRY(pkg2_kip1_info_t, ki, kips, link) {
//Patch FS
if(ki->kip1->tid == 0x0100000000000000) {
print("Patching FS\n");

se_calc_sha256(&kipHash, ki->kip1, ki->size);
se_calc_sha256(&kipHash, ki->kip1, ki->size);

//Create header
size_t sizeDiff = ki->kip1->sections[0].size_decomp - ki->kip1->sections[0].size_comp;
size_t newSize = ki->size + sizeDiff;
pkg2_kip1_t *moddedKip = malloc(newSize);
memcpy(moddedKip, ki->kip1, newSize);
u32 pos = 0;
for(int i = 0; i < KIP1_NUM_SECTIONS; i++) {
if(!i) {
//Get decomp .text segment
u8 *kipDecompText = blz_decompress(moddedKip->data, moddedKip->sections[i].size_comp);

kippatchset_t *pset = kippatch_find_set(kipHash, kip_patches);
if (!pset) {
print(" could not find patchset with matching hash\n");
} else {
int res = kippatch_apply_set(kipDecompText, moddedKip->sections[i].size_decomp, pset);
if (res) error("kippatch_apply_set() failed\n");
}

moddedKip->flags &= ~1;
memcpy((void*)moddedKip->data, kipDecompText, moddedKip->sections[i].size_decomp);
free(kipDecompText);
pos += moddedKip->sections[i].size_comp;
moddedKip->sections[i].size_comp = moddedKip->sections[i].size_decomp;
} else {
if(moddedKip->sections[i].offset == 0) continue;
memcpy((void*)moddedKip->data + pos + sizeDiff, (void*)ki->kip1->data + pos, moddedKip->sections[i].size_comp);
pos += moddedKip->sections[i].size_comp;
}
}

free(ki->kip1);
ki->size = newSize;
ki->kip1 = moddedKip;
}
pkg2_kip1_info_t* FS_module = find_by_tid(kips, 0x0100000000000000);
if(FS_module == NULL) {
error("Could not find FS Module.\n");
} else {
patchFS(FS_module);
}
}

Expand Down

0 comments on commit e350bb5

Please # to comment.