From cae3d272d346e4eef1a449ab17811e4e7e1407b8 Mon Sep 17 00:00:00 2001 From: Pk11 Date: Sun, 9 Apr 2023 13:38:35 -0500 Subject: [PATCH] Only reserve space in trimmed NDS files for RSA key if it exists (#804) --- arm9/source/gamecart/gamecart.c | 11 ++++++++++- arm9/source/utils/gameutil.c | 17 +++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/arm9/source/gamecart/gamecart.c b/arm9/source/gamecart/gamecart.c index df83bbe..0ca8077 100644 --- a/arm9/source/gamecart/gamecart.c +++ b/arm9/source/gamecart/gamecart.c @@ -254,7 +254,7 @@ u32 InitCartRead(CartData* cdata) { if (nds_header->device_capacity >= 15) return 1; // too big, not valid if (cdata->cart_size == 0) cdata->cart_size = (128 * 1024) << nds_header->device_capacity; - cdata->data_size = nds_header->ntr_rom_size + 0x88; + cdata->data_size = nds_header->ntr_rom_size; cdata->arm9i_rom_offset = 0; // TWL header @@ -273,6 +273,15 @@ u32 InitCartRead(CartData* cdata) { NTR_CmdReadHeader(cdata->storage); if (!NTR_Secure_Init(cdata->storage, NULL, Cart_GetID(), 1)) return 1; } + } else { + // Check if immediately after the reported cart size + // is the magic number string 'ac' (auth code). + // If found, add 0x88 bytes for the download play RSA key. + u16 rsaMagic; + ReadCartBytes(&rsaMagic, cdata->data_size, 2, cdata, false); + if(rsaMagic == 0x6361) { + cdata->data_size += 0x88; + } } // store encrypted secure area diff --git a/arm9/source/utils/gameutil.c b/arm9/source/utils/gameutil.c index 72b7010..3c9da9b 100644 --- a/arm9/source/utils/gameutil.c +++ b/arm9/source/utils/gameutil.c @@ -3296,12 +3296,21 @@ u64 GetGameFileTrimmedSize(const char* path) { trimsize = GetAnyFileTrimmedSize(path); } else if (filetype & GAME_NDS) { TwlHeader hdr; - if (fvx_qread(path, &hdr, 0, sizeof(TwlHeader), NULL) != FR_OK) + if (fvx_qread(path, &hdr, 0, sizeof(TwlHeader), NULL) != FR_OK) { return 0; - if (hdr.unit_code != 0x00) // DSi or NDS+DSi + } if (hdr.unit_code != 0x00) { // DSi or NDS+DSi trimsize = hdr.ntr_twl_rom_size; - else if (hdr.ntr_rom_size) // regular NDS - trimsize = hdr.ntr_rom_size + 0x88; + } else if (hdr.ntr_rom_size) { // regular NDS + trimsize = hdr.ntr_rom_size; + + // Check if immediately after the reported cart size + // is the magic number string 'ac' (auth code). + // If found, add 0x88 bytes for the download play RSA key. + u16 rsaMagic; + if(fvx_qread(path, &rsaMagic, trimsize, 2, NULL) == FR_OK && rsaMagic == 0x6361) { + trimsize += 0x88; + } + } } else { u8 hdr[0x200]; if (fvx_qread(path, &hdr, 0, 0x200, NULL) != FR_OK)