From 280b1da282b88b89a8b9a1d61fe9506b505c5443 Mon Sep 17 00:00:00 2001 From: RocketRobz Date: Sun, 16 Feb 2020 15:06:17 -0700 Subject: [PATCH] Improve NAND init code --- arm9/source/main.cpp | 5 ++++- arm9/source/nandio.c | 27 ++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/arm9/source/main.cpp b/arm9/source/main.cpp index 3f7c89f..f36f4ed 100644 --- a/arm9/source/main.cpp +++ b/arm9/source/main.cpp @@ -206,10 +206,13 @@ int main(int argc, char **argv) { ramdrive2Mount(); } nandMounted = nandMount(); - is3DS = ((access("sd:/Nintendo 3DS", F_OK) == 0) && (*(vu32*)(0x0DFFFE0C) == 0x474D3969)); + //is3DS = ((access("sd:/Nintendo 3DS", F_OK) == 0) && (*(vu32*)(0x0DFFFE0C) == 0x474D3969)); /*FILE* cidFile = fopen("sd:/gm9i/CID.bin", "wb"); fwrite((void*)0x2FFD7BC, 1, 16, cidFile); fclose(cidFile);*/ + /*FILE* cidFile = fopen("sd:/gm9i/ConsoleID.bin", "wb"); + fwrite((void*)0x2FFFD00, 1, 8, cidFile); + fclose(cidFile);*/ } /*else if (isRegularDS) { *(vu32*)(0x08240000) = 1; expansionPakFound = ((*(vu32*)(0x08240000) == 1) && (io_dldi_data->ioInterface.features & FEATURE_SLOT_NDS)); diff --git a/arm9/source/nandio.c b/arm9/source/nandio.c index d1795c9..6bc750a 100644 --- a/arm9/source/nandio.c +++ b/arm9/source/nandio.c @@ -2,12 +2,15 @@ #include #include #include +#include #include "crypto.h" #include "sector0.h" //#define SECTOR_SIZE 512 #define CRYPT_BUF_LEN 64 +extern bool is3DS; + extern bool nand_Startup(); static u8* crypt_buf = 0; @@ -25,19 +28,29 @@ bool nandio_startup() { if (!nand_Startup()) return false; nand_ReadSectors(0, 1, sector_buf); - int is3DS = parse_ncsd(sector_buf, 0) == 0; - if (is3DS) return false; + is3DS = parse_ncsd(sector_buf, 0) == 0; + //if (is3DS) return false; if (*(u32*)(0x2FFD7BC) == 0) { - // Get eMMC CID - *(u32*)(0x2FFFD0C) = 0x454D4D43; - while (*(u32*)(0x2FFFD0C) != 0); + if (is3DS) { + FILE* cidFile = fopen("sd:/gm9/out/nand_cid.mem", "rb"); + if (!cidFile) return false; + fread((void*)0x2FFD7BC, 1, 16, cidFile); + fclose(cidFile); + } else { + // Get eMMC CID + *(u32*)(0x2FFFD0C) = 0x454D4D43; + while (*(u32*)(0x2FFFD0C) != 0); + } } // iprintf("sector 0 is %s\n", is3DS ? "3DS" : "DSi"); dsi_crypt_init((const u8*)0x2FFFD00, (const u8*)0x2FFD7BC, is3DS); - //dsi_nand_crypt(sector_buf, sector_buf, 0, SECTOR_SIZE / AES_BLOCK_SIZE); - //parse_mbr(sector_buf, is3DS, 0); + dsi_nand_crypt(sector_buf, sector_buf, 0, SECTOR_SIZE / AES_BLOCK_SIZE); + parse_mbr(sector_buf, is3DS, 0); + + mbr_t *mbr = (mbr_t*)sector_buf; + nandio_set_fat_sig_fix(is3DS ? 0 : mbr->partitions[0].offset); if (crypt_buf == 0) { crypt_buf = (u8*)memalign(32, SECTOR_SIZE * CRYPT_BUF_LEN);