diff --git a/Makefile b/Makefile index d0e3130..ab69791 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ INCLUDES := source source/fatfs source/nand #--------------------------------------------------------------------------------- ARCH := -mthumb -mthumb-interwork -flto -CFLAGS := -g -Wall -Wextra -Wpedantic -pedantic -O2\ +CFLAGS := -g -Wall -Wextra -Wpedantic -Wcast-align -pedantic -O2\ -march=armv5te -mtune=arm946e-s -fomit-frame-pointer\ -ffast-math -std=c99\ $(ARCH) diff --git a/source/common.h b/source/common.h index 2aab15b..7563d8c 100644 --- a/source/common.h +++ b/source/common.h @@ -28,9 +28,12 @@ ((((u32) getbe16(d))<<16) | ((u32) getbe16(d+2))) #define getbe64(d) \ ((((u64) getbe32(d))<<32) | ((u64) getbe32(d+4))) -#define getle16(d) (*((u16*) (d))) -#define getle32(d) (*((u32*) (d))) -#define getle64(d) (*((u64*) (d))) +#define getle16(d) \ + (((d)[1]<<8) | (d)[0]) +#define getle32(d) \ + ((((u32) getle16(d+2))<<16) | ((u32) getle16(d))) +#define getle64(d) \ + ((((u64) getle32(d+4))<<32) | ((u64) getle32(d))) #define align(v,a) \ (((v) % (a)) ? ((v) + (a) - ((v) % (a))) : (v)) diff --git a/source/fs.c b/source/fs.c index 99160a4..28dbaed 100644 --- a/source/fs.c +++ b/source/fs.c @@ -105,7 +105,7 @@ uint64_t GetSDCardSize() { return (u64) getMMCDevice(1)->total_size * 512; } -bool FormatSDCard(u32 hidden_mb) { +bool FormatSDCard(u64 hidden_mb) { u8 mbr[0x200] = { 0 }; u8 mbrdata[0x42] = { 0x80, 0x01, 0x01, 0x00, 0x0C, 0xFE, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -115,22 +115,23 @@ bool FormatSDCard(u32 hidden_mb) { 0x55, 0xAA }; u32 sd_size = getMMCDevice(1)->total_size; - u32* fat_sector = (u32*) (mbrdata + 0x08); - u32* fat_size = (u32*) (mbrdata + 0x0C); - u32* emu_sector = (u32*) (mbrdata + 0x18); - u32* emu_size = (u32*) (mbrdata + 0x1C); + u32 emu_sector = 1; + u32 emu_size = (u32) ((hidden_mb * 1024 * 1024) / 512); + u32 fat_sector = align(emu_sector + emu_size, 0x2000); // align to 4MB + u32 fat_size = (fat_sector < sd_size) ? sd_size - fat_sector : 0; - *emu_sector = 1; - *emu_size = hidden_mb * (1024 * 1024) / 512; - *fat_sector = align(*emu_sector + *emu_size, 0x2000); // align to 4MB - if (sd_size < *fat_sector + 0x80000) { // minimum free space: 256MB + // FAT size check + if (fat_size < 0x80000) { // minimum free space: 256MB ShowPrompt(false, "ERROR: SD card is too small"); return false; } - *fat_size = sd_size - *fat_sector; - sd_size = *fat_size; + sd_size = fat_size; // build the MBR + memcpy(mbrdata + 0x08, &fat_sector, 4); + memcpy(mbrdata + 0x0C, &fat_size, 4); + memcpy(mbrdata + 0x18, &emu_sector, 4); + memcpy(mbrdata + 0x1C, &emu_size, 4); memcpy(mbr + 0x1BE, mbrdata, 0x42); if (hidden_mb) memcpy(mbr, "GATEWAYNAND", 12); else memset(mbr + 0x1CE, 0, 0x10); @@ -138,7 +139,7 @@ bool FormatSDCard(u32 hidden_mb) { // one last warning.... if (!ShowUnlockSequence(3, "!WARNING!\n \nProceeding will format this SD.\nThis will irreversibly delete\nALL data on it.\n")) return false; - ShowString("Formatting SD, please wait...", hidden_mb); + ShowString("Formatting SD, please wait..."); // write the MBR to disk // !this assumes a fully deinitialized file system! diff --git a/source/fs.h b/source/fs.h index f538900..c68072d 100644 --- a/source/fs.h +++ b/source/fs.h @@ -46,7 +46,7 @@ void SetFSSearch(const char* pattern, const char* path); uint64_t GetSDCardSize(); /** Format the SD card **/ -bool FormatSDCard(u32 hidden_mb); +bool FormatSDCard(u64 hidden_mb); /** Check if writing to this path is allowed **/ bool CheckWritePermissions(const char* path); diff --git a/source/godmode.c b/source/godmode.c index 1771a1a..71a23ac 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -205,7 +205,7 @@ u32 SdFormatMenu(void) { } while (emunand_size_mb > sdcard_size_mb); if (emunand_size_mb == (u64) -1) return 1; - if (!FormatSDCard((u32) emunand_size_mb)) { + if (!FormatSDCard(emunand_size_mb)) { ShowPrompt(false, "Format SD: failed!"); return 1; } diff --git a/source/nand/nand.c b/source/nand/nand.c index d9776e3..87b1395 100644 --- a/source/nand/nand.c +++ b/source/nand/nand.c @@ -126,13 +126,13 @@ bool InitNandCrypto(void) } // part #1: Get NAND CID, set up TWL/CTR counter - u8 NandCid[16]; + u32 NandCid[4]; u8 shasum[32]; - sdmmc_get_cid( 1, (uint32_t*) NandCid); - sha_quick(shasum, NandCid, 16, SHA256_MODE); + sdmmc_get_cid( 1, NandCid); + sha_quick(shasum, (u8*) NandCid, 16, SHA256_MODE); memcpy(CtrNandCtr, shasum, 16); - sha_quick(shasum, NandCid, 16, SHA1_MODE); + sha_quick(shasum, (u8*) NandCid, 16, SHA1_MODE); for(u32 i = 0; i < 16; i++) // little endian and reversed order TwlNandCtr[i] = shasum[15-i]; @@ -146,10 +146,11 @@ bool InitNandCrypto(void) // thanks b1l1s & Normmatt // see source from https://gbatemp.net/threads/release-twltool-dsi-downgrading-save-injection-etc-multitool.393488/ const char* nintendo = "NINTENDO"; - u32* TwlKeyXW = (u32*) TwlKeyX; - TwlKeyXW[0] = (TwlCustId[0] ^ 0xB358A6AF) | 0x80000000; - TwlKeyXW[3] = TwlCustId[1] ^ 0x08C267B7; - memcpy(TwlKeyX + 4, nintendo, 8); + u32 TwlKeyXW0 = (TwlCustId[0] ^ 0xB358A6AF) | 0x80000000; + u32 TwlKeyXW3 = TwlCustId[1] ^ 0x08C267B7; + memcpy(TwlKeyX + 4, nintendo, 8); + memcpy(TwlKeyX + 0, &TwlKeyXW0, 4); + memcpy(TwlKeyX + 12, &TwlKeyXW3, 4); // see: https://www.3dbrew.org/wiki/Memory_layout#ARM9_ITCM u32 TwlKeyYW3 = 0xE1A00005;