From 836530d4e33cf704731a4a86ac7b51bf5f9d6afb Mon Sep 17 00:00:00 2001 From: CTCaer Date: Thu, 20 Jan 2022 13:32:48 +0200 Subject: [PATCH] pkg2: refactor bitflags and remove debugging code --- bootloader/hos/pkg2.c | 57 +++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/bootloader/hos/pkg2.c b/bootloader/hos/pkg2.c index 9c946ae..2954ec7 100644 --- a/bootloader/hos/pkg2.c +++ b/bootloader/hos/pkg2.c @@ -28,6 +28,9 @@ #include #include "../storage/emummc.h" +//#define DPRINTF(...) gfx_printf(__VA_ARGS__) +#define DPRINTF(...) + extern hekate_config h_cfg; extern const u8 package2_keyseed[]; @@ -35,13 +38,6 @@ u32 pkg2_newkern_ini1_val; u32 pkg2_newkern_ini1_start; u32 pkg2_newkern_ini1_end; -#ifdef KIP1_PATCH_DEBUG - #define DPRINTF(...) gfx_printf(__VA_ARGS__) - #define DEBUG_PRINTING -#else - #define DPRINTF(...) -#endif - enum kip_offset_section { KIP_TEXT = 0, @@ -57,7 +53,7 @@ enum kip_offset_section #define KIP_PATCH_OFFSET_MASK (~KIP_PATCH_SECTION_MASK) #define GET_KIP_PATCH_SECTION(x) (((x) >> KIP_PATCH_SECTION_SHIFT) & 7) #define GET_KIP_PATCH_OFFSET(x) ((x) & KIP_PATCH_OFFSET_MASK) -#define KPS(x) ((u32)(x) << KIP_PATCH_SECTION_SHIFT) +#define KPS(x) ((u32)(x) << KIP_PATCH_SECTION_SHIFT) #include "pkg2_patches.inl" @@ -306,7 +302,7 @@ int pkg2_decompress_kip(pkg2_kip1_info_t* ki, u32 sectsToDecomp) unsigned int newKipSize = sizeof(hdr); for (u32 sectIdx = 0; sectIdx < KIP1_NUM_SECTIONS; sectIdx++) { - u32 sectCompBit = 1u << sectIdx; + u32 sectCompBit = BIT(sectIdx); // For compressed, cant get actual decompressed size without doing it, so use safe "output size". if (sectIdx < 3 && (sectsToDecomp & sectCompBit) && (hdr.flags & sectCompBit)) newKipSize += hdr.sections[sectIdx].size_decomp; @@ -319,7 +315,7 @@ int pkg2_decompress_kip(pkg2_kip1_info_t* ki, u32 sectsToDecomp) const unsigned char* srcDataPtr = ki->kip1->data; for (u32 sectIdx = 0; sectIdx < KIP1_NUM_SECTIONS; sectIdx++) { - u32 sectCompBit = 1u << sectIdx; + u32 sectCompBit = BIT(sectIdx); // Easy copy path for uncompressed or ones we dont want to uncompress. if (sectIdx >= 3 || !(sectsToDecomp & sectCompBit) || !(hdr.flags & sectCompBit)) { @@ -335,11 +331,11 @@ int pkg2_decompress_kip(pkg2_kip1_info_t* ki, u32 sectsToDecomp) unsigned int compSize = hdr.sections[sectIdx].size_comp; unsigned int outputSize = hdr.sections[sectIdx].size_decomp; - gfx_printf("Decomping %s KIP1 sect %d of size %d...\n", (const char*)hdr.name, sectIdx, compSize); + gfx_printf("Decomping '%s', sect %d, size %d..\n", (const char*)hdr.name, sectIdx, compSize); if (blz_uncompress_srcdest(srcDataPtr, compSize, dstDataPtr, outputSize) == 0) { gfx_con.mute = false; - gfx_printf("%kERROR decomping sect %d of %s KIP!%k\n", 0xFFFF0000, sectIdx, (char*)hdr.name, 0xFFCCCCCC); + gfx_printf("%kERROR decomping sect %d of '%s'!%k\n", 0xFFFF0000, sectIdx, (char*)hdr.name, 0xFFCCCCCC); free(newKip); return 1; @@ -366,7 +362,7 @@ int pkg2_decompress_kip(pkg2_kip1_info_t* ki, u32 sectsToDecomp) static int _kipm_inject(const char *kipm_path, char *target_name, pkg2_kip1_info_t* ki) { - if (!strcmp((const char *)ki->kip1->name, target_name)) + if (!strncmp((const char *)ki->kip1->name, target_name, sizeof(ki->kip1->name))) { u32 size = 0; u8 *kipm_data = (u8 *)sd_file_read(kipm_path, &size); @@ -491,7 +487,7 @@ const char* pkg2_patch_kips(link_t *info, char* patchNames) } } - u32 shaBuf[32 / sizeof(u32)]; + u32 shaBuf[SE_SHA_256_SIZE / sizeof(u32)]; LIST_FOREACH_ENTRY(pkg2_kip1_info_t, ki, info, link) { shaBuf[0] = 0; // sha256 for this kip not yet calculated. @@ -542,30 +538,19 @@ const char* pkg2_patch_kips(link_t *info, char* patchNames) continue; if (!strcmp(currPatchset->name, "emummc")) - bitsAffected |= 1u << GET_KIP_PATCH_SECTION(currPatchset->patches->offset); + bitsAffected |= BIT(GET_KIP_PATCH_SECTION(currPatchset->patches->offset)); for (const kip1_patch_t* currPatch=currPatchset->patches; currPatch != NULL && (currPatch->length != 0); currPatch++) - bitsAffected |= 1u << GET_KIP_PATCH_SECTION(currPatch->offset); + bitsAffected |= BIT(GET_KIP_PATCH_SECTION(currPatch->offset)); } } currPatchset++; } // Got patches to apply to this kip, have to decompress it. -#ifdef DEBUG_PRINTING - u32 preDecompTime = get_tmr_us(); -#endif if (pkg2_decompress_kip(ki, bitsAffected)) return (const char*)ki->kip1->name; // Failed to decompress. -#ifdef DEBUG_PRINTING - u32 postDecompTime = get_tmr_us(); - if (!se_calc_sha256_oneshot(shaBuf, ki->kip1, ki->size)) - memset(shaBuf, 0, sizeof(shaBuf)); - - DPRINTF("%dms %s KIP1 size %d hash %08X\n", (postDecompTime-preDecompTime) / 1000, ki->kip1->name, (int)ki->size, __builtin_bswap32(shaBuf[0])); -#endif - currPatchset = _kip_id_sets[currKipIdx].patchset; bool emummc_patch_selected = false; while (currPatchset != NULL && currPatchset->name != NULL) @@ -575,7 +560,7 @@ const char* pkg2_patch_kips(link_t *info, char* patchNames) if (strcmp(currPatchset->name, patches[currEnabIdx])) continue; - u32 appliedMask = 1u << currEnabIdx; + u32 appliedMask = BIT(currEnabIdx); if (!strcmp(currPatchset->name, "emummc")) { @@ -587,7 +572,7 @@ const char* pkg2_patch_kips(link_t *info, char* patchNames) if (currPatchset->patches == NULL) { - DPRINTF("Patch '%s' not necessary for %s KIP1\n", currPatchset->name, (const char*)ki->kip1->name); + DPRINTF("Patch '%s' not necessary for %s\n", currPatchset->name, (const char*)ki->kip1->name); patchesApplied |= appliedMask; continue; // Continue in case it's double defined. @@ -596,10 +581,10 @@ const char* pkg2_patch_kips(link_t *info, char* patchNames) unsigned char* kipSectData = ki->kip1->data; for (u32 currSectIdx = 0; currSectIdx < KIP1_NUM_SECTIONS; currSectIdx++) { - if (bitsAffected & (1u << currSectIdx)) + if (bitsAffected & BIT(currSectIdx)) { - gfx_printf("Applying patch '%s' on %s KIP1 sect %d\n", currPatchset->name, (const char*)ki->kip1->name, currSectIdx); - for (const kip1_patch_t* currPatch = currPatchset->patches; currPatch != NULL && currPatch->srcData != 0; currPatch++) + gfx_printf("Applying '%s' on %s, sect %d\n", currPatchset->name, (const char*)ki->kip1->name, currSectIdx); + for (const kip1_patch_t* currPatch = currPatchset->patches; currPatch != NULL && currPatch->srcData != NULL; currPatch++) { if (GET_KIP_PATCH_SECTION(currPatch->offset) != currSectIdx) continue; @@ -607,7 +592,7 @@ const char* pkg2_patch_kips(link_t *info, char* patchNames) if (!currPatch->length) { gfx_con.mute = false; - gfx_printf("%kPatch is empty!%k\n", 0xFFFF0000, 0xFFCCCCCC); + gfx_printf("%kPatch empty!%k\n", 0xFFFF0000, 0xFFCCCCCC); return currPatchset->name; // MUST stop here as it's not probably intended. } @@ -617,7 +602,7 @@ const char* pkg2_patch_kips(link_t *info, char* patchNames) (memcmp(&kipSectData[currOffset], currPatch->dstData, currPatch->length) != 0)) { gfx_con.mute = false; - gfx_printf("%kPatch data mismatch at 0x%x!%k\n", 0xFFFF0000, currOffset, 0xFFCCCCCC); + gfx_printf("%kPatch mismatch at 0x%x!%k\n", 0xFFFF0000, currOffset, 0xFFCCCCCC); return currPatchset->name; // MUST stop here as kip is likely corrupt. } else @@ -654,7 +639,7 @@ const char* pkg2_patch_kips(link_t *info, char* patchNames) for (u32 i = 0; i < numPatches; i++) { - if ((patchesApplied & (1u << i)) == 0) + if ((patchesApplied & BIT(i)) == 0) return patches[i]; } @@ -698,7 +683,6 @@ pkg2_hdr_t *pkg2_decrypt(void *data, u8 kb, bool is_exo) // Decrypt header. se_aes_crypt_ctr(pkg2_keyslot, hdr, sizeof(pkg2_hdr_t), hdr, sizeof(pkg2_hdr_t), hdr); - //gfx_hexdump((u32)hdr, hdr, 0x100); if (hdr->magic != PKG2_MAGIC) return NULL; @@ -711,7 +695,6 @@ DPRINTF("sec %d has size %08X\n", i, hdr->sec_size[i]); continue; se_aes_crypt_ctr(pkg2_keyslot, pdata, hdr->sec_size[i], pdata, hdr->sec_size[i], &hdr->sec_ctr[i * SE_AES_IV_SIZE]); - //gfx_hexdump((u32)pdata, pdata, 0x100); pdata += hdr->sec_size[i]; }