From a0d2642bb197e282f0930415f142ad677347e072 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 11 Apr 2018 18:19:01 -0600 Subject: [PATCH] Skeleton out framework for patching package2. --- fusee/fusee-secondary/src/nxboot.c | 4 +++ fusee/fusee-secondary/src/package2.c | 47 ++++++++++++++++++++++++++++ fusee/fusee-secondary/src/package2.h | 37 ++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 fusee/fusee-secondary/src/package2.c create mode 100644 fusee/fusee-secondary/src/package2.h diff --git a/fusee/fusee-secondary/src/nxboot.c b/fusee/fusee-secondary/src/nxboot.c index 9799d9140..eb97c5fe9 100644 --- a/fusee/fusee-secondary/src/nxboot.c +++ b/fusee/fusee-secondary/src/nxboot.c @@ -1,6 +1,7 @@ #include "utils.h" #include "nxboot.h" #include "key_derivation.h" +#include "package2.h" #include "loader.h" #include "splash_screen.h" #include "exocfg.h" @@ -58,6 +59,9 @@ void nxboot_main(void) { /* Derive keydata. */ derive_nx_keydata(MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware); + /* Patch package2, adding thermosphere + custom KIPs. */ + package2_patch((void *)loader_ctx->package2_loadfile.load_address); + /* Boot up Exosphere. */ MAILBOX_NX_BOOTLOADER_IS_SECMON_AWAKE = 0; if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware <= EXOSPHERE_TARGET_FIRMWARE_400) { diff --git a/fusee/fusee-secondary/src/package2.c b/fusee/fusee-secondary/src/package2.c new file mode 100644 index 000000000..15a84fa65 --- /dev/null +++ b/fusee/fusee-secondary/src/package2.c @@ -0,0 +1,47 @@ +#include "utils.h" +#include "package2.h" +#include "se.h" + +void package2_decrypt(void *package2_address); +void package2_add_thermosphere_section(void *package2_address); +void package2_patch_kernel(void *package2_address); +void package2_patch_ini1(void *package2_address); +void package2_fixup_header_and_section_hashes(void *package2_address); + +void package2_patch(void *package2_address) { + /* First things first: Decrypt (TODO: Relocate?) Package2. */ + package2_decrypt(package2_address); + + /* Modify Package2 to add an additional thermosphere section. */ + package2_add_thermosphere_section(package2_address); + + /* Perform any patches we want to the NX kernel. */ + package2_patch_kernel(package2_address); + + /* Perform any patches we want to the INI1 (This is where our built-in sysmodules will be added.) */ + package2_patch_ini1(package2_address); + + /* Fix all necessary data in the header to accomodate for the new patches. */ + package2_fixup_header_and_section_hashes(package2_address); +} + + +void package2_decrypt(void *package2_address) { + /* TODO */ +} + +void package2_add_thermosphere_section(void *package2_address) { + /* TODO */ +} + +void package2_patch_kernel(void *package2_address) { + /* TODO */ +} + +void package2_patch_ini1(void *package2_address) { + /* TODO */ +} + +void package2_fixup_header_and_section_hashes(void *package2_address) { + /* TODO */ +} \ No newline at end of file diff --git a/fusee/fusee-secondary/src/package2.h b/fusee/fusee-secondary/src/package2.h new file mode 100644 index 000000000..11c0c367b --- /dev/null +++ b/fusee/fusee-secondary/src/package2.h @@ -0,0 +1,37 @@ +#ifndef FUSEE_PACKAGE2_H +#define FUSEE_PACKAGE2_H + +/* This is a library for patching Package2 prior to handoff to Exosphere. */ + +#define MAGIC_PK21 (0x31324B50) +#define PACKAGE2_SIZE_MAX 0x7FC000 +#define PACKAGE2_SECTION_MAX 0x3 + +typedef struct { + union { + uint8_t ctr[0x10]; + uint32_t ctr_dwords[0x4]; + }; + uint8_t section_ctrs[4][0x10]; + uint32_t magic; + uint32_t entrypoint; + uint32_t _0x58; + uint8_t version_max; /* Must be > TZ value. */ + uint8_t version_min; /* Must be < TZ value. */ + uint16_t _0x5E; + uint32_t section_sizes[4]; + uint32_t section_offsets[4]; + uint8_t section_hashes[4][0x20]; +} package2_meta_t; + +typedef struct { + uint8_t signature[0x100]; + union { + package2_meta_t metadata; + uint8_t encrypted_header[0x100]; + }; +} package2_header_t; + +void package2_patch(void *package2_address); + +#endif \ No newline at end of file