From b2ab8b94decb8b1bec6acb60c137009c41b4f538 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 10 Oct 2024 20:47:52 -0700 Subject: [PATCH 1/3] ndpmtool/elftool: support force_debug_prod --- src/elf2kip.c | 7 ++++++- src/npdmtool.c | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/elf2kip.c b/src/elf2kip.c index 005957c..237d62c 100644 --- a/src/elf2kip.c +++ b/src/elf2kip.c @@ -554,6 +554,7 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) { } int allow_debug = 0; int force_debug = 0; + int force_debug_prod = 0; if (!cJSON_GetBoolean(value, "allow_debug", &allow_debug)) { status = 0; goto PARSE_CAPS_END; @@ -562,7 +563,11 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) { status = 0; goto PARSE_CAPS_END; } - desc = (allow_debug & 1) | ((force_debug & 1) << 1); + if (!cJSON_GetBoolean(value, "force_debug_prod", &force_debug_prod)) { + status = 0; + goto PARSE_CAPS_END; + } + desc = (allow_debug & 1) | ((force_debug_prod & 1) << 1) | ((force_debug & 1) << 2); kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 17) | (0xFFFF)); } else { fprintf(stderr, "Error: unknown capability %s\n", type_str); diff --git a/src/npdmtool.c b/src/npdmtool.c index 0481b2d..eec7acf 100644 --- a/src/npdmtool.c +++ b/src/npdmtool.c @@ -819,6 +819,7 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) { } int allow_debug = 0; int force_debug = 0; + int force_debug_prod = 0; if (!cJSON_GetBoolean(value, "allow_debug", &allow_debug)) { status = 0; goto NPDM_BUILD_END; @@ -827,7 +828,11 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) { status = 0; goto NPDM_BUILD_END; } - desc = (allow_debug & 1) | ((force_debug & 1) << 1); + if (!cJSON_GetBoolean(value, "force_debug_prod", &force_debug_prod)) { + status = 0; + goto NPDM_BUILD_END; + } + desc = (allow_debug & 1) | ((force_debug_prod & 1) << 1) | ((force_debug & 1) << 2); caps[cur_cap++] = (u32)((desc << 17) | (0xFFFF)); } } From 8aae04d0b87d47951bdb2ed91f551b51bcce1bad Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 10 Oct 2024 20:50:07 -0700 Subject: [PATCH 2/3] npdmtool: add new flags from 18.0.0/19.0.0 --- src/npdmtool.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/npdmtool.c b/src/npdmtool.c index eec7acf..7b1c62b 100644 --- a/src/npdmtool.c +++ b/src/npdmtool.c @@ -373,6 +373,16 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) { header.MmuFlags |= ((disable_device_address_space_merge & 1) << 5); } + int enable_alias_region_extra_size; // optional + if (cJSON_GetBoolean(npdm_json, "enable_alias_region_extra_size", &enable_alias_region_extra_size)) { + header.MmuFlags |= ((enable_alias_region_extra_size & 1) << 6); + } + + int prevent_code_reads; // optional + if (cJSON_GetBoolean(npdm_json, "prevent_code_reads", &prevent_code_reads)) { + header.MmuFlags |= ((prevent_code_reads & 1) << 7); + } + u8 signature_key_generation; // optional if (cJSON_GetU8(npdm_json, "signature_key_generation", &signature_key_generation)) { header.SignatureKeyGeneration = signature_key_generation; From a457147e736a398ee27585496c97ae2a0e0c8cd4 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 14 Oct 2024 12:00:11 -0700 Subject: [PATCH 3/3] elf2nro: add support for aligned header flag --- src/elf2nro.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/elf2nro.c b/src/elf2nro.c index 2b6c9e8..7d5bd06 100644 --- a/src/elf2nro.c +++ b/src/elf2nro.c @@ -21,9 +21,9 @@ typedef struct { typedef struct { u8 Magic[4]; - u32 Unk1; + u32 version; u32 size; - u32 Unk2; + u32 flags; NsoSegment Segments[3]; u32 bssSize; u32 Unk3; @@ -78,9 +78,10 @@ int main(int argc, char* argv[]) { fprintf(stderr, "--nacp= Embeds control.nacp into the output file.\n"); fprintf(stderr, "--romfs= Embeds RomFS into the output file.\n"); fprintf(stderr, "--romfsdir= Builds and embeds RomFS into the output file.\n"); + fprintf(stderr, "--alignedheader Sets the \"AlignedHeader\" flag in the output file.\n"); return EXIT_FAILURE; } - + NroStart nro_start; memset(&nro_start, 0, sizeof(nro_start)); @@ -102,18 +103,20 @@ int main(int argc, char* argv[]) { int argi; char* icon_path = NULL, *nacp_path = NULL, *romfs_path = NULL, *romfs_dir_path = NULL; + u32 aligned_header = 0; for (argi=3; argip_filesz + 0xFFF) & ~0xFFF; @@ -174,13 +177,13 @@ int main(int argc, char* argv[]) { fprintf(stderr, "Out of memory!\n"); return EXIT_FAILURE; } - + memcpy(buf[i], &elf[phdr->p_offset], phdr->p_filesz); file_off += nro_hdr.Segments[i].Size; file_off = (file_off + 0xFFF) & ~0xFFF; } - + /* Iterate over sections to find build id. */ size_t cur_sect_hdr_ofs = hdr->e_shoff; for (unsigned int i = 0; i < hdr->e_shnum; i++) { @@ -206,9 +209,12 @@ int main(int argc, char* argv[]) { fprintf(stderr, "Failed to open output file!\n"); return EXIT_FAILURE; } - + nro_hdr.size = file_off; + nro_hdr.version = 0; + nro_hdr.flags = (aligned_header << 0); + // TODO check retvals for (i=0; i<3; i++) @@ -270,7 +276,7 @@ int main(int argc, char* argv[]) { asset_hdr.romfs.offset = tmp_off; asset_hdr.romfs.size = romfs_len; tmp_off+= romfs_len; - + } else if (romfs_dir_path) { asset_hdr.romfs.offset = tmp_off; asset_hdr.romfs.size = build_romfs_by_path_into_file(romfs_dir_path, out, file_off + tmp_off);