From 98cdbe0784fee2dbb395843aa137edbcd0bb8b86 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 6 Sep 2016 22:27:23 +0200 Subject: [PATCH 1/8] Minor stuff --- source/config.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/config.c b/source/config.c index 6a702a4..9dd0e32 100644 --- a/source/config.c +++ b/source/config.c @@ -201,8 +201,7 @@ void configMenu(bool oldPinStatus) drawCharacter(selected, 10 + multiOptions[selectedOption].posXs[oldEnabled] * SPACING_X, multiOptions[selectedOption].posY, COLOR_BLACK); multiOptions[selectedOption].enabled = oldEnabled == 3 ? 0 : oldEnabled + 1; - if(!selectedOption) - updateBrightness(multiOptions[selectedOption].enabled); + if(!selectedOption) updateBrightness(multiOptions[0].enabled); } else { From 5b6318ee3a2f17c2c5ac3815ffdbf421eb666785 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Tue, 6 Sep 2016 22:26:48 -0400 Subject: [PATCH 2/8] Support additional EmuNAND layouts. Fixes compatibility with a second EmuNAND placed after an EmuNAND created with either the 'default' or 'minimum' setup sizes with EmuNAND9 / 3DS Multi EmuNAND Creator. --- source/emunand.c | 68 ++++++++++++++++++++++++++++++++---------------- source/emunand.h | 1 + 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/source/emunand.c b/source/emunand.c index 9d31e40..ace1d29 100644 --- a/source/emunand.c +++ b/source/emunand.c @@ -25,37 +25,59 @@ #include "fatfs/sdmmc/sdmmc.h" #include "../build/emunandpatch.h" +#define O3DS_LEGACY_FAT 0x200000 +#define O3DS_DEFAULT_FAT 0x1DE000 +#define O3DS_MINIMUM_FAT 0x1D8000 + +#define N3DS_LEGACY_FAT 0x400000 +#define N3DS_DEFAULT_FAT 0x3B2000 +#define N3DS_MINIMUM_FAT 0x26E000 + void locateEmuNand(u32 *off, u32 *head, FirmwareSource *emuNand) { static u8 temp[0x200]; - + const u32 nandSize = getMMCDevice(0)->total_size; - u32 nandOffset = *emuNand == FIRMWARE_EMUNAND ? 0 : - (nandSize > 0x200000 ? 0x400000 : 0x200000); - - //Check for RedNAND - if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && - *(u32 *)(temp + 0x100) == NCSD_MAGIC) - { - *off = nandOffset + 1; - *head = nandOffset + 1; - } - - //Check for Gateway emuNAND - else if(!sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) && - *(u32 *)(temp + 0x100) == NCSD_MAGIC) - { - *off = nandOffset; - *head = nandOffset + nandSize; - } - + const u32 nandLayoutO3DS[3] = { O3DS_LEGACY_FAT, O3DS_DEFAULT_FAT, O3DS_MINIMUM_FAT }; // Legacy, Default, Minimum + const u32 nandLayoutN3DS[3] = { N3DS_LEGACY_FAT, N3DS_DEFAULT_FAT, N3DS_MINIMUM_FAT }; // Legacy, Default, Minimum + + u8 i; + u32 nandOffset; + bool found = false; + + for (i = 0; i < 3; i++) + { + if (i > 0 && *emuNand != FIRMWARE_EMUNAND2) break; + + // Check for 'Legacy', 'Default' and 'Minimum' partition layouts when checking for the 2nd EmuNAND + nandOffset = (*emuNand == FIRMWARE_EMUNAND ? 0 : (isN3DS ? nandLayoutN3DS[i] : nandLayoutO3DS[i])); + + //Check for RedNAND + if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) + { + *off = nandOffset + 1; + *head = nandOffset + 1; + found = true; + break; + } + + //Check for Gateway emuNAND + else if(!sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) + { + *off = nandOffset; + *head = nandOffset + nandSize; + found = true; + break; + } + } + /* Fallback to the first emuNAND if there's no second one, or to SysNAND if there isn't any */ - else - { + if (!found) + { *emuNand = (*emuNand == FIRMWARE_EMUNAND2) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND; if(*emuNand) locateEmuNand(off, head, emuNand); - } + } } static inline u8 *getFreeK9Space(u8 *pos, u32 size) diff --git a/source/emunand.h b/source/emunand.h index 543fbe0..8c8fd89 100644 --- a/source/emunand.h +++ b/source/emunand.h @@ -27,6 +27,7 @@ #define NCSD_MAGIC 0x4453434E extern u32 emuOffset; +extern bool isN3DS; void locateEmuNand(u32 *off, u32 *head, FirmwareSource *emuNand); void patchEmuNand(u8 *arm9Section, u32 arm9SectionSize, u8 *process9Offset, u32 process9Size, u32 emuHeader, u32 branchAdditive); \ No newline at end of file From f10427287f6d0afa99862663a71117880ab0b917 Mon Sep 17 00:00:00 2001 From: Aurora Date: Wed, 7 Sep 2016 14:26:01 +0200 Subject: [PATCH 3/8] Update sdmmc (thanks @gemarcano) --- source/fatfs/sdmmc/common.h | 3 - source/fatfs/sdmmc/delay.h | 8 +- source/fatfs/sdmmc/delay.s | 28 +- source/fatfs/sdmmc/sdmmc.c | 539 ++++++++++++++++++++---------------- source/fatfs/sdmmc/sdmmc.h | 115 +++----- 5 files changed, 353 insertions(+), 340 deletions(-) delete mode 100644 source/fatfs/sdmmc/common.h diff --git a/source/fatfs/sdmmc/common.h b/source/fatfs/sdmmc/common.h deleted file mode 100644 index 3b77ae6..0000000 --- a/source/fatfs/sdmmc/common.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#include "../../types.h" \ No newline at end of file diff --git a/source/fatfs/sdmmc/delay.h b/source/fatfs/sdmmc/delay.h index afad438..543794a 100644 --- a/source/fatfs/sdmmc/delay.h +++ b/source/fatfs/sdmmc/delay.h @@ -1,9 +1,5 @@ -// Copyright 2014 Normmatt -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - #pragma once -#include "common.h" +#include "../../types.h" -void ioDelay(u32 us); +void waitcycles(u32 us); diff --git a/source/fatfs/sdmmc/delay.s b/source/fatfs/sdmmc/delay.s index b3baccd..3a2cfdf 100644 --- a/source/fatfs/sdmmc/delay.s +++ b/source/fatfs/sdmmc/delay.s @@ -1,17 +1,15 @@ -// Copyright 2014 Normmatt -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - .arm -.global ioDelay -.type ioDelay STT_FUNC +.global waitcycles +.type waitcycles STT_FUNC -@ioDelay ( u32 us ) -ioDelay: - ldr r1, =0x18000000 @ VRAM -1: - @ Loop doing uncached reads from VRAM to make loop timing more reliable - ldr r2, [r1] - subs r0, #1 - bgt 1b - bx lr +@waitcycles ( u32 us ) +waitcycles: + PUSH {R0-R2,LR} + STR R0, [SP,#4] + waitcycles_loop: + LDR R3, [SP,#4] + SUBS R2, R3, #1 + STR R2, [SP,#4] + CMP R3, #0 + BNE waitcycles_loop + POP {R0-R2,PC} diff --git a/source/fatfs/sdmmc/sdmmc.c b/source/fatfs/sdmmc/sdmmc.c index 9eb7f50..27e2031 100644 --- a/source/fatfs/sdmmc/sdmmc.c +++ b/source/fatfs/sdmmc/sdmmc.c @@ -1,6 +1,26 @@ -// Copyright 2014 Normmatt -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (c) 2014-2015, Normmatt + * + * Alternatively, the contents of this file may be used under the terms + * of the GNU General Public License Version 2, as described below: + * + * This file is free software: you may copy, redistribute and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 2 of the License, or (at your + * option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ #include "sdmmc.h" #include "delay.h" @@ -8,23 +28,28 @@ struct mmcdevice handleNAND; struct mmcdevice handleSD; -static inline u16 sdmmc_read16(u16 reg) { - return *(vu16*)(SDMMC_BASE + reg); +static inline u16 sdmmc_read16(u16 reg) +{ + return *(vu16 *)(SDMMC_BASE + reg); } -static inline void sdmmc_write16(u16 reg, u16 val) { - *(vu16*)(SDMMC_BASE + reg) = val; +static inline void sdmmc_write16(u16 reg, u16 val) +{ + *(vu16 *)(SDMMC_BASE + reg) = val; } -static inline u32 sdmmc_read32(u16 reg) { - return *(vu32*)(SDMMC_BASE + reg); +static inline u32 sdmmc_read32(u16 reg) +{ + return *(vu32 *)(SDMMC_BASE + reg); } -static inline void sdmmc_write32(u16 reg, u32 val) { - *(vu32*)(SDMMC_BASE + reg) = val; +static inline void sdmmc_write32(u16 reg, u32 val) +{ + *(vu32 *)(SDMMC_BASE + reg) = val; } -static inline void sdmmc_mask16(u16 reg, const u16 clear, const u16 set) { +static inline void sdmmc_mask16(u16 reg, const u16 clear, const u16 set) +{ u16 val = sdmmc_read16(reg); val &= ~clear; val |= set; @@ -38,172 +63,215 @@ static inline void setckl(u32 data) sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100); } - mmcdevice *getMMCDevice(int drive) { - if(drive==0) return &handleNAND; + if(drive == 0) return &handleNAND; return &handleSD; } -static u32 __attribute__((noinline)) geterror(struct mmcdevice *ctx) +static int geterror(struct mmcdevice *ctx) { - return (ctx->error << 29) >> 31; + return (int)((ctx->error << 29) >> 31); } -static void __attribute__((noinline)) inittarget(struct mmcdevice *ctx) +static void inittarget(struct mmcdevice *ctx) { - sdmmc_mask16(REG_SDPORTSEL,0x3,(u16)ctx->devicenumber); + sdmmc_mask16(REG_SDPORTSEL, 0x3, (u16)ctx->devicenumber); setckl(ctx->clk); - if (ctx->SDOPT == 0) { - sdmmc_mask16(REG_SDOPT, 0, 0x8000); - } else { - sdmmc_mask16(REG_SDOPT, 0x8000, 0); - } - + if(ctx->SDOPT == 0) sdmmc_mask16(REG_SDOPT, 0, 0x8000); + else sdmmc_mask16(REG_SDOPT, 0x8000, 0); } -static void __attribute__((noinline)) sdmmc_send_command(struct mmcdevice *ctx, u32 cmd, u32 args) +static void __attribute__ ((noinline)) sdmmc_send_command(struct mmcdevice *ctx, u32 cmd, u32 args) { - bool getSDRESP = (cmd << 15) >> 31; + u32 getSDRESP = (cmd << 15) >> 31; u16 flags = (cmd << 15) >> 31; - const bool readdata = cmd & 0x20000; - const bool writedata = cmd & 0x40000; + const int readdata = cmd & 0x20000; + const int writedata = cmd & 0x40000; - if (readdata || writedata) + if(readdata || writedata) flags |= TMIO_STAT0_DATAEND; ctx->error = 0; - while (sdmmc_read16(REG_SDSTATUS1) & TMIO_STAT1_CMD_BUSY); //mmc working? - sdmmc_write16(REG_SDIRMASK0,0); - sdmmc_write16(REG_SDIRMASK1,0); - sdmmc_write16(REG_SDSTATUS0,0); - sdmmc_write16(REG_SDSTATUS1,0); - sdmmc_mask16(REG_SDDATACTL32,0x1800,0); - - sdmmc_write16(REG_SDCMDARG0,args &0xFFFF); - sdmmc_write16(REG_SDCMDARG1,args >> 16); - sdmmc_write16(REG_SDCMD,cmd &0xFFFF); + while((sdmmc_read16(REG_SDSTATUS1) & TMIO_STAT1_CMD_BUSY)); //mmc working? + sdmmc_write16(REG_SDIRMASK0, 0); + sdmmc_write16(REG_SDIRMASK1, 0); + sdmmc_write16(REG_SDSTATUS0, 0); + sdmmc_write16(REG_SDSTATUS1, 0); + sdmmc_mask16(REG_DATACTL32, 0x1800, 0); + sdmmc_write16(REG_SDCMDARG0, args & 0xFFFF); + sdmmc_write16(REG_SDCMDARG1, args >> 16); + sdmmc_write16(REG_SDCMD, cmd & 0xFFFF); u32 size = ctx->size; - vu8 *dataPtr = ctx->data; + u8 *rDataPtr = ctx->rData; + const u8 *tDataPtr = ctx->tData; - bool useBuf = ( NULL != dataPtr ); + int rUseBuf = NULL != rDataPtr; + int tUseBuf = NULL != tDataPtr; u16 status0 = 0; - while(true) { - u16 status1 = sdmmc_read16(REG_SDSTATUS1); - if (status1 & TMIO_STAT1_RXRDY) { - if (readdata && useBuf) { - sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0); - //sdmmc_write16(REG_SDSTATUS1,~TMIO_STAT1_RXRDY); - if (size > 0x1FF) { - for(int i = 0; i<0x200; i+=2) { - u16 data = sdmmc_read16(REG_SDFIFO); - *dataPtr++ = data & 0xFF; - *dataPtr++ = data >> 8; + while(true) + { + vu16 status1 = sdmmc_read16(REG_SDSTATUS1); + vu16 ctl32 = sdmmc_read16(REG_DATACTL32); + if((ctl32 & 0x100)) + { + if(readdata) + { + if(rUseBuf) + { + sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0); + if(size > 0x1FF) + { + //Gabriel Marcano: This implementation doesn't assume alignment. + //I've removed the alignment check doen with former rUseBuf32 as a result + for(int i = 0; i < 0x200; i += 4) + { + u32 data = sdmmc_read32(REG_SDFIFO32); + *rDataPtr++ = data; + *rDataPtr++ = data >> 8; + *rDataPtr++ = data >> 16; + *rDataPtr++ = data >> 24; + } + size -= 0x200; } - size -= 0x200; } - } - } - if (status1 & TMIO_STAT1_TXRQ) { - if (writedata && useBuf) { - sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0); - //sdmmc_write16(REG_SDSTATUS1,~TMIO_STAT1_TXRQ); - if (size > 0x1FF) { - for (int i = 0; i<0x200; i+=2) { - u16 data = *dataPtr++; - data |= *dataPtr++ << 8; - sdmmc_write16(REG_SDFIFO, data); - } - size -= 0x200; - } + sdmmc_mask16(REG_DATACTL32, 0x800, 0); } } - if (status1 & TMIO_MASK_GW) { + if(!(ctl32 & 0x200)) + { + if(writedata) + { + if(tUseBuf) + { + sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0); + if(size > 0x1FF) + { + for(int i = 0; i < 0x200; i += 4) + { + u32 data = *tDataPtr++; + data |= (u32)*tDataPtr++ << 8; + data |= (u32)*tDataPtr++ << 16; + data |= (u32)*tDataPtr++ << 24; + sdmmc_write32(REG_SDFIFO32, data); + } + size -= 0x200; + } + } + + sdmmc_mask16(REG_DATACTL32, 0x1000, 0); + } + } + if(status1 & TMIO_MASK_GW) + { ctx->error |= 4; break; } - if (!(status1 & TMIO_STAT1_CMD_BUSY)) { + if(!(status1 & TMIO_STAT1_CMD_BUSY)) + { status0 = sdmmc_read16(REG_SDSTATUS0); - if (sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND) + if(sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND) + { ctx->error |= 0x1; - if (status0 & TMIO_STAT0_DATAEND) + } + if(status0 & TMIO_STAT0_DATAEND) + { ctx->error |= 0x2; + } - if ((status0 & flags) == flags) + if((status0 & flags) == flags) break; } } ctx->stat0 = sdmmc_read16(REG_SDSTATUS0); ctx->stat1 = sdmmc_read16(REG_SDSTATUS1); - sdmmc_write16(REG_SDSTATUS0,0); - sdmmc_write16(REG_SDSTATUS1,0); + sdmmc_write16(REG_SDSTATUS0, 0); + sdmmc_write16(REG_SDSTATUS1, 0); - if (getSDRESP != 0) { - ctx->ret[0] = (u32)sdmmc_read16(REG_SDRESP0) | (u32)(sdmmc_read16(REG_SDRESP1) << 16); - ctx->ret[1] = (u32)sdmmc_read16(REG_SDRESP2) | (u32)(sdmmc_read16(REG_SDRESP3) << 16); - ctx->ret[2] = (u32)sdmmc_read16(REG_SDRESP4) | (u32)(sdmmc_read16(REG_SDRESP5) << 16); - ctx->ret[3] = (u32)sdmmc_read16(REG_SDRESP6) | (u32)(sdmmc_read16(REG_SDRESP7) << 16); + if(getSDRESP != 0) + { + ctx->ret[0] = (u32)(sdmmc_read16(REG_SDRESP0) | (sdmmc_read16(REG_SDRESP1) << 16)); + ctx->ret[1] = (u32)(sdmmc_read16(REG_SDRESP2) | (sdmmc_read16(REG_SDRESP3) << 16)); + ctx->ret[2] = (u32)(sdmmc_read16(REG_SDRESP4) | (sdmmc_read16(REG_SDRESP5) << 16)); + ctx->ret[3] = (u32)(sdmmc_read16(REG_SDRESP6) | (sdmmc_read16(REG_SDRESP7) << 16)); } } -u32 __attribute__((noinline)) sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, vu8 *in) +int __attribute__ ((noinline)) sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, const u8 *in) { - if (handleSD.isSDHC == 0) - sector_no <<= 9; + if(handleSD.isSDHC == 0) sector_no <<= 9; inittarget(&handleSD); - sdmmc_write16(REG_SDSTOP,0x100); - - sdmmc_write16(REG_SDBLKCOUNT,numsectors); - handleSD.data = in; + sdmmc_write16(REG_SDSTOP, 0x100); + sdmmc_write16(REG_SDBLKCOUNT32, numsectors); + sdmmc_write16(REG_SDBLKLEN32, 0x200); + sdmmc_write16(REG_SDBLKCOUNT, numsectors); + handleSD.tData = in; handleSD.size = numsectors << 9; - sdmmc_send_command(&handleSD,0x52C19,sector_no); + sdmmc_send_command(&handleSD, 0x52C19, sector_no); return geterror(&handleSD); } -u32 __attribute__((noinline)) sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, vu8 *out) +int __attribute__ ((noinline)) sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, u8 *out) { - if (handleSD.isSDHC == 0) - sector_no <<= 9; + if(handleSD.isSDHC == 0) sector_no <<= 9; inittarget(&handleSD); - sdmmc_write16(REG_SDSTOP,0x100); - - sdmmc_write16(REG_SDBLKCOUNT,numsectors); - handleSD.data = out; + sdmmc_write16(REG_SDSTOP, 0x100); + sdmmc_write16(REG_SDBLKCOUNT32, numsectors); + sdmmc_write16(REG_SDBLKLEN32, 0x200); + sdmmc_write16(REG_SDBLKCOUNT, numsectors); + handleSD.rData = out; handleSD.size = numsectors << 9; - sdmmc_send_command(&handleSD,0x33C12,sector_no); + sdmmc_send_command(&handleSD, 0x33C12, sector_no); return geterror(&handleSD); } -u32 __attribute__((noinline)) sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, vu8 *out) +int __attribute__ ((noinline)) sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, u8 *out) { - if (handleNAND.isSDHC == 0) - sector_no <<= 9; + if(handleNAND.isSDHC == 0) sector_no <<= 9; inittarget(&handleNAND); - sdmmc_write16(REG_SDSTOP,0x100); - - sdmmc_write16(REG_SDBLKCOUNT,numsectors); - - handleNAND.data = out; + sdmmc_write16(REG_SDSTOP, 0x100); + sdmmc_write16(REG_SDBLKCOUNT32, numsectors); + sdmmc_write16(REG_SDBLKLEN32, 0x200); + sdmmc_write16(REG_SDBLKCOUNT, numsectors); + handleNAND.rData = out; handleNAND.size = numsectors << 9; - sdmmc_send_command(&handleNAND,0x33C12,sector_no); + sdmmc_send_command(&handleNAND, 0x33C12, sector_no); inittarget(&handleSD); return geterror(&handleNAND); } -static u32 calcSDSize(u8* csd, int type) +/* +int __attribute__ ((noinline)) sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, const u8 *in) //experimental +{ + if(handleNAND.isSDHC == 0) sector_no <<= 9; + inittarget(&handleNAND); + sdmmc_write16(REG_SDSTOP, 0x100); + sdmmc_write16(REG_SDBLKCOUNT32, numsectors); + sdmmc_write16(REG_SDBLKLEN32, 0x200); + sdmmc_write16(REG_SDBLKCOUNT, numsectors); + handleNAND.tData = in; + handleNAND.size = numsectors << 9; + sdmmc_send_command(&handleNAND, 0x52C19, sector_no); + inittarget(&handleSD); + return geterror(&handleNAND); +} +*/ + +static u32 calcSDSize(u8 *csd, int type) { u32 result = 0; - if (type == -1) type = csd[14] >> 6; - switch (type) { + if(type == -1) type = csd[14] >> 6; + switch(type) + { case 0: { - u32 block_len = csd[9] & 0xf; + u32 block_len = csd[9] & 0xF; block_len = 1u << block_len; - u32 mult = (u32)(csd[4] >> 7) | (u32)((csd[5] & 3) << 1); + u32 mult = (u32)((csd[4] >> 7) | ((csd[5] & 3) << 1)); mult = 1u << (mult + 2); result = csd[8] & 3; result = (result << 8) | csd[7]; @@ -212,18 +280,42 @@ static u32 calcSDSize(u8* csd, int type) } break; case 1: - result = csd[7] & 0x3f; + result = csd[7] & 0x3F; result = (result << 8) | csd[6]; result = (result << 8) | csd[5]; result = (result + 1) * 1024; break; - default: - break; //Do nothing otherwise + default: + break; //Do nothing otherwise FIXME perhaps return some error? } return result; } static void InitSD() +{ + *(vu16 *)0x10006100 &= 0xF7FFu; //SDDATACTL32 + *(vu16 *)0x10006100 &= 0xEFFFu; //SDDATACTL32 + *(vu16 *)0x10006100 |= 0x402u; //SDDATACTL32 + *(vu16 *)0x100060D8 = (*(vu16 *)0x100060D8 & 0xFFDD) | 2; + *(vu16 *)0x10006100 &= 0xFFFFu; //SDDATACTL32 + *(vu16 *)0x100060D8 &= 0xFFDFu; //SDDATACTL + *(vu16 *)0x10006104 = 512; //SDBLKLEN32 + *(vu16 *)0x10006108 = 1; //SDBLKCOUNT32 + *(vu16 *)0x100060E0 &= 0xFFFEu; //SDRESET + *(vu16 *)0x100060E0 |= 1u; //SDRESET + *(vu16 *)0x10006020 |= TMIO_MASK_ALL; //SDIR_MASK0 + *(vu16 *)0x10006022 |= TMIO_MASK_ALL>>16; //SDIR_MASK1 + *(vu16 *)0x100060FC |= 0xDBu; //SDCTL_RESERVED7 + *(vu16 *)0x100060FE |= 0xDBu; //SDCTL_RESERVED8 + *(vu16 *)0x10006002 &= 0xFFFCu; //SDPORTSEL + *(vu16 *)0x10006024 = 0x20; + *(vu16 *)0x10006028 = 0x40EE; + *(vu16 *)0x10006002 &= 0xFFFCu; ////SDPORTSEL + *(vu16 *)0x10006026 = 512; //SDBLKLEN + *(vu16 *)0x10006008 = 0; //SDSTOP +} + +static int Nand_Init() { //NAND handleNAND.isSDHC = 0; @@ -233,80 +325,50 @@ static void InitSD() handleNAND.clk = 0x80; handleNAND.devicenumber = 1; - //SD - handleSD.isSDHC = 0; - handleSD.SDOPT = 0; - handleSD.res = 0; - handleSD.initarg = 0; - handleSD.clk = 0x80; - handleSD.devicenumber = 0; - - *(vu16*)0x10006100 &= 0xF7FFu; //SDDATACTL32 - *(vu16*)0x10006100 &= 0xEFFFu; //SDDATACTL32 - *(vu16*)0x10006100 |= 0x402u; //SDDATACTL32 - *(vu16*)0x100060D8 = (*(vu16*)0x100060D8 & 0xFFDD) | 2; - *(vu16*)0x10006100 &= 0xFFFDu; //SDDATACTL32 - *(vu16*)0x100060D8 &= 0xFFDDu; //SDDATACTL - *(vu16*)0x10006104 = 0; //SDBLKLEN32 - *(vu16*)0x10006108 = 1; //SDBLKCOUNT32 - *(vu16*)0x100060E0 &= 0xFFFEu; //SDRESET - *(vu16*)0x100060E0 |= 1u; //SDRESET - *(vu16*)0x10006020 |= TMIO_MASK_ALL; //SDIR_MASK0 - *(vu16*)0x10006022 |= TMIO_MASK_ALL>>16; //SDIR_MASK1 - *(vu16*)0x100060FC |= 0xDBu; //SDCTL_RESERVED7 - *(vu16*)0x100060FE |= 0xDBu; //SDCTL_RESERVED8 - *(vu16*)0x10006002 &= 0xFFFCu; //SDPORTSEL - *(vu16*)0x10006024 = 0x40; //Nintendo sets this to 0x20 - *(vu16*)0x10006028 = 0x40EB; //Nintendo sets this to 0x40EE - *(vu16*)0x10006002 &= 0xFFFCu; ////SDPORTSEL - *(vu16*)0x10006026 = 512; //SDBLKLEN - *(vu16*)0x10006008 = 0; //SDSTOP - - inittarget(&handleSD); -} - -static int Nand_Init() -{ inittarget(&handleNAND); - ioDelay(0xF000); + waitcycles(0xF000); - sdmmc_send_command(&handleNAND,0,0); + sdmmc_send_command(&handleNAND, 0, 0); - do { - do { - sdmmc_send_command(&handleNAND,0x10701,0x100000); - } while ( !(handleNAND.error & 1) ); - } while((handleNAND.ret[0] & 0x80000000) == 0); + do + { + do + { + sdmmc_send_command(&handleNAND, 0x10701, 0x100000); + } + while(!(handleNAND.error & 1)); + } + while((handleNAND.ret[0] & 0x80000000) == 0); - sdmmc_send_command(&handleNAND,0x10602,0x0); - if (handleNAND.error & 0x4) return -1; + sdmmc_send_command(&handleNAND, 0x10602, 0x0); + if((handleNAND.error & 0x4))return -1; - sdmmc_send_command(&handleNAND,0x10403,handleNAND.initarg << 0x10); - if (handleNAND.error & 0x4) return -1; + sdmmc_send_command(&handleNAND, 0x10403, handleNAND.initarg << 0x10); + if((handleNAND.error & 0x4))return -1; - sdmmc_send_command(&handleNAND,0x10609,handleNAND.initarg << 0x10); - if (handleNAND.error & 0x4) return -1; + sdmmc_send_command(&handleNAND, 0x10609, handleNAND.initarg << 0x10); + if((handleNAND.error & 0x4))return -1; - handleNAND.total_size = calcSDSize((u8*)&handleNAND.ret[0],0); + handleNAND.total_size = calcSDSize((u8*)&handleNAND.ret[0], 0); handleNAND.clk = 1; setckl(1); - sdmmc_send_command(&handleNAND,0x10407,handleNAND.initarg << 0x10); - if (handleNAND.error & 0x4) return -1; + sdmmc_send_command(&handleNAND, 0x10407, handleNAND.initarg << 0x10); + if((handleNAND.error & 0x4))return -1; handleNAND.SDOPT = 1; - sdmmc_send_command(&handleNAND,0x10506,0x3B70100); - if (handleNAND.error & 0x4) return -1; + sdmmc_send_command(&handleNAND, 0x10506, 0x3B70100); + if((handleNAND.error & 0x4))return -1; - sdmmc_send_command(&handleNAND,0x10506,0x3B90100); - if (handleNAND.error & 0x4) return -1; + sdmmc_send_command(&handleNAND, 0x10506, 0x3B90100); + if((handleNAND.error & 0x4))return -1; - sdmmc_send_command(&handleNAND,0x1040D,handleNAND.initarg << 0x10); - if (handleNAND.error & 0x4) return -1; + sdmmc_send_command(&handleNAND, 0x1040D, handleNAND.initarg << 0x10); + if((handleNAND.error & 0x4))return -1; - sdmmc_send_command(&handleNAND,0x10410,0x200); - if (handleNAND.error & 0x4) return -1; + sdmmc_send_command(&handleNAND, 0x10410, 0x200); + if((handleNAND.error & 0x4))return -1; handleNAND.clk |= 0x200; @@ -317,113 +379,102 @@ static int Nand_Init() static int SD_Init() { + //SD + handleSD.isSDHC = 0; + handleSD.SDOPT = 0; + handleSD.res = 0; + handleSD.initarg = 0; + handleSD.clk = 0x80; + handleSD.devicenumber = 0; + inittarget(&handleSD); - ioDelay(1u << 18); //Card needs a little bit of time to be detected, it seems - + waitcycles(1u << 22); //Card needs a little bit of time to be detected, it seems FIXME test again to see what a good number is for the delay + //If not inserted - if (!(*((vu16*)0x1000601c) & TMIO_STAT0_SIGSTATE)) return -1; - - sdmmc_send_command(&handleSD,0,0); - sdmmc_send_command(&handleSD,0x10408,0x1AA); - //u32 temp = (handleSD.ret[0] == 0x1AA) << 0x1E; + if(!(*((vu16 *)(SDMMC_BASE + REG_SDSTATUS0)) & TMIO_STAT0_SIGSTATE)) return 5; + + sdmmc_send_command(&handleSD, 0, 0); + sdmmc_send_command(&handleSD, 0x10408, 0x1AA); u32 temp = (handleSD.error & 0x1) << 0x1E; - //int count = 0; u32 temp2 = 0; - do { - do { - sdmmc_send_command(&handleSD,0x10437,handleSD.initarg << 0x10); - sdmmc_send_command(&handleSD,0x10769,0x00FF8000 | temp); + do + { + do + { + sdmmc_send_command(&handleSD, 0x10437, handleSD.initarg << 0x10); + sdmmc_send_command(&handleSD, 0x10769, 0x00FF8000 | temp); temp2 = 1; - } while ( !(handleSD.error & 1) ); - - } while((handleSD.ret[0] & 0x80000000) == 0); + } + while(!(handleSD.error & 1)); + } + while((handleSD.ret[0] & 0x80000000) == 0); if(!((handleSD.ret[0] >> 30) & 1) || !temp) temp2 = 0; handleSD.isSDHC = temp2; - sdmmc_send_command(&handleSD,0x10602,0); - if (handleSD.error & 0x4) return -1; + sdmmc_send_command(&handleSD, 0x10602, 0); + if((handleSD.error & 0x4)) return -1; - sdmmc_send_command(&handleSD,0x10403,0); - if (handleSD.error & 0x4) return -1; + sdmmc_send_command(&handleSD, 0x10403, 0); + if((handleSD.error & 0x4)) return -2; handleSD.initarg = handleSD.ret[0] >> 0x10; - sdmmc_send_command(&handleSD,0x10609,handleSD.initarg << 0x10); - if (handleSD.error & 0x4) return -1; + sdmmc_send_command(&handleSD, 0x10609, handleSD.initarg << 0x10); + if((handleSD.error & 0x4)) return -3; - handleSD.total_size = calcSDSize((u8*)&handleSD.ret[0],-1); + handleSD.total_size = calcSDSize((u8*)&handleSD.ret[0], -1); handleSD.clk = 1; setckl(1); - sdmmc_send_command(&handleSD,0x10507,handleSD.initarg << 0x10); - if (handleSD.error & 0x4) return -1; + sdmmc_send_command(&handleSD, 0x10507, handleSD.initarg << 0x10); + if((handleSD.error & 0x4)) return -4; - sdmmc_send_command(&handleSD,0x10437,handleSD.initarg << 0x10); - if (handleSD.error & 0x4) return -1; + sdmmc_send_command(&handleSD, 0x10437, handleSD.initarg << 0x10); + if((handleSD.error & 0x4)) return -5; handleSD.SDOPT = 1; - sdmmc_send_command(&handleSD,0x10446,0x2); - if (handleSD.error & 0x4) return -1; + sdmmc_send_command(&handleSD, 0x10446, 0x2); + if((handleSD.error & 0x4)) return -6; - sdmmc_send_command(&handleSD,0x1040D,handleSD.initarg << 0x10); - if (handleSD.error & 0x4) return -1; + sdmmc_send_command(&handleSD, 0x1040D, handleSD.initarg << 0x10); + if((handleSD.error & 0x4)) return -7; - sdmmc_send_command(&handleSD,0x10410,0x200); - if (handleSD.error & 0x4) return -1; + sdmmc_send_command(&handleSD, 0x10410, 0x200); + if((handleSD.error & 0x4)) return -8; handleSD.clk |= 0x200; return 0; } +void sdmmc_get_cid(bool isNand, u32 *info) +{ + struct mmcdevice *device = isNand ? &handleNAND : &handleSD; + + inittarget(device); + + // use cmd7 to put sd card in standby mode + // CMD7 + sdmmc_send_command(device, 0x10507, 0); + + // get sd card info + // use cmd10 to read CID + sdmmc_send_command(device, 0x1060A, device->initarg << 0x10); + + for(int i = 0; i < 4; ++i) + info[i] = device->ret[i]; + + // put sd card back to transfer mode + // CMD7 + sdmmc_send_command(device, 0x10507, device->initarg << 0x10); +} + void sdmmc_sdcard_init() { InitSD(); Nand_Init(); SD_Init(); -} - -int sdmmc_get_cid(int isNand, uint32_t *info) -{ - struct mmcdevice *device; - if(isNand) - device = &handleNAND; - else - device = &handleSD; - - inittarget(device); - // use cmd7 to put sd card in standby mode - // CMD7 - { - sdmmc_send_command(device,0x10507,0); - //if((device->error & 0x4)) return -1; - } - - // get sd card info - // use cmd10 to read CID - { - sdmmc_send_command(device,0x1060A,device->initarg << 0x10); - //if((device->error & 0x4)) return -2; - - for( int i = 0; i < 4; ++i ) { - info[i] = device->ret[i]; - } - } - - // put sd card back to transfer mode - // CMD7 - { - sdmmc_send_command(device,0x10507,device->initarg << 0x10); - //if((device->error & 0x4)) return -3; - } - - if(isNand) - { - inittarget(&handleSD); - } - - return 0; } \ No newline at end of file diff --git a/source/fatfs/sdmmc/sdmmc.h b/source/fatfs/sdmmc/sdmmc.h index f666a1c..44449b7 100644 --- a/source/fatfs/sdmmc/sdmmc.h +++ b/source/fatfs/sdmmc/sdmmc.h @@ -1,52 +1,48 @@ -// Copyright 2014 Normmatt -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - #pragma once -#include "common.h" +#include "../../types.h" -#define SDMMC_BASE 0x10006000u +#define SDMMC_BASE 0x10006000 -#define REG_SDCMD 0x00 -#define REG_SDPORTSEL 0x02 -#define REG_SDCMDARG 0x04 -#define REG_SDCMDARG0 0x04 -#define REG_SDCMDARG1 0x06 -#define REG_SDSTOP 0x08 -#define REG_SDBLKCOUNT 0x0a +#define REG_SDCMD 0x00 +#define REG_SDPORTSEL 0x02 +#define REG_SDCMDARG 0x04 +#define REG_SDCMDARG0 0x04 +#define REG_SDCMDARG1 0x06 +#define REG_SDSTOP 0x08 +#define REG_SDBLKCOUNT 0x0a -#define REG_SDRESP0 0x0c -#define REG_SDRESP1 0x0e -#define REG_SDRESP2 0x10 -#define REG_SDRESP3 0x12 -#define REG_SDRESP4 0x14 -#define REG_SDRESP5 0x16 -#define REG_SDRESP6 0x18 -#define REG_SDRESP7 0x1a +#define REG_SDRESP0 0x0c +#define REG_SDRESP1 0x0e +#define REG_SDRESP2 0x10 +#define REG_SDRESP3 0x12 +#define REG_SDRESP4 0x14 +#define REG_SDRESP5 0x16 +#define REG_SDRESP6 0x18 +#define REG_SDRESP7 0x1a -#define REG_SDSTATUS0 0x1c -#define REG_SDSTATUS1 0x1e +#define REG_SDSTATUS0 0x1c +#define REG_SDSTATUS1 0x1e -#define REG_SDIRMASK0 0x20 -#define REG_SDIRMASK1 0x22 -#define REG_SDCLKCTL 0x24 +#define REG_SDIRMASK0 0x20 +#define REG_SDIRMASK1 0x22 +#define REG_SDCLKCTL 0x24 -#define REG_SDBLKLEN 0x26 -#define REG_SDOPT 0x28 -#define REG_SDFIFO 0x30 +#define REG_SDBLKLEN 0x26 +#define REG_SDOPT 0x28 +#define REG_SDFIFO 0x30 -#define REG_SDDATACTL 0xd8 -#define REG_SDRESET 0xe0 -#define REG_SDPROTECTED 0xf6 //bit 0 determines if sd is protected or not? +#define REG_DATACTL 0xd8 +#define REG_SDRESET 0xe0 +#define REG_SDPROTECTED 0xf6 //bit 0 determines if sd is protected or not? -#define REG_SDDATACTL32 0x100 -#define REG_SDBLKLEN32 0x104 -#define REG_SDBLKCOUNT32 0x108 -#define REG_SDFIFO32 0x10C +#define REG_DATACTL32 0x100 +#define REG_SDBLKLEN32 0x104 +#define REG_SDBLKCOUNT32 0x108 +#define REG_SDFIFO32 0x10C -#define REG_CLK_AND_WAIT_CTL 0x138 -#define REG_RESET_SDIO 0x1e0 +#define REG_CLK_AND_WAIT_CTL 0x138 +#define REG_RESET_SDIO 0x1e0 #define TMIO_STAT0_CMDRESPEND 0x0001 #define TMIO_STAT0_DATAEND 0x0004 @@ -70,40 +66,17 @@ #define TMIO_STAT1_CMD_BUSY 0x4000 #define TMIO_STAT1_ILL_ACCESS 0x8000 -//Comes from TWLSDK mongoose.tef DWARF info -#define SDMC_NORMAL 0x00000000 -#define SDMC_ERR_COMMAND 0x00000001 -#define SDMC_ERR_CRC 0x00000002 -#define SDMC_ERR_END 0x00000004 -#define SDMC_ERR_TIMEOUT 0x00000008 -#define SDMC_ERR_FIFO_OVF 0x00000010 -#define SDMC_ERR_FIFO_UDF 0x00000020 -#define SDMC_ERR_WP 0x00000040 -#define SDMC_ERR_ABORT 0x00000080 -#define SDMC_ERR_FPGA_TIMEOUT 0x00000100 -#define SDMC_ERR_PARAM 0x00000200 -#define SDMC_ERR_R1_STATUS 0x00000800 -#define SDMC_ERR_NUM_WR_SECTORS 0x00001000 -#define SDMC_ERR_RESET 0x00002000 -#define SDMC_ERR_ILA 0x00004000 -#define SDMC_ERR_INFO_DETECT 0x00008000 - -#define SDMC_STAT_ERR_UNKNOWN 0x00080000 -#define SDMC_STAT_ERR_CC 0x00100000 -#define SDMC_STAT_ERR_ECC_FAILED 0x00200000 -#define SDMC_STAT_ERR_CRC 0x00800000 -#define SDMC_STAT_ERR_OTHER 0xf9c70008 - #define TMIO_MASK_ALL 0x837f031d #define TMIO_MASK_GW (TMIO_STAT1_ILL_ACCESS | TMIO_STAT1_CMDTIMEOUT | TMIO_STAT1_TXUNDERRUN | TMIO_STAT1_RXOVERFLOW | \ - TMIO_STAT1_DATATIMEOUT | TMIO_STAT1_STOPBIT_ERR | TMIO_STAT1_CRCFAIL | TMIO_STAT1_CMD_IDX_ERR) + TMIO_STAT1_DATATIMEOUT | TMIO_STAT1_STOPBIT_ERR | TMIO_STAT1_CRCFAIL | TMIO_STAT1_CMD_IDX_ERR) #define TMIO_MASK_READOP (TMIO_STAT1_RXRDY | TMIO_STAT1_DATAEND) #define TMIO_MASK_WRITEOP (TMIO_STAT1_TXRQ | TMIO_STAT1_DATAEND) typedef struct mmcdevice { - vu8* data; + u8* rData; + const u8* tData; u32 size; u32 error; u16 stat0; @@ -118,12 +91,10 @@ typedef struct mmcdevice { u32 res; } mmcdevice; -mmcdevice *getMMCDevice(int drive); - void sdmmc_sdcard_init(); -u32 sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, vu8 *out); -u32 sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, vu8 *in); - -u32 sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, vu8 *out); - -int sdmmc_get_cid( int isNand, uint32_t *info); \ No newline at end of file +int sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, u8 *out); +int sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, const u8 *in); +int sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, u8 *out); +//int sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, const u8 *in); +void sdmmc_get_cid(bool isNand, u32 *info); +mmcdevice *getMMCDevice(int drive); \ No newline at end of file From 99654bd5b24adaf4d30057b7687038ded87aab65 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Wed, 7 Sep 2016 11:25:20 -0400 Subject: [PATCH 4/8] Fix compatibility with 2DS. Adds a check to determine if the NAND size is greater than the size of an Old 3DS Toshiba NAND. --- source/emunand.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/emunand.c b/source/emunand.c index ace1d29..c2db014 100644 --- a/source/emunand.c +++ b/source/emunand.c @@ -25,6 +25,8 @@ #include "fatfs/sdmmc/sdmmc.h" #include "../build/emunandpatch.h" +#define O3DS_TOSHIBA_NAND 0x1DD000 + #define O3DS_LEGACY_FAT 0x200000 #define O3DS_DEFAULT_FAT 0x1DE000 #define O3DS_MINIMUM_FAT 0x1D8000 @@ -50,7 +52,7 @@ void locateEmuNand(u32 *off, u32 *head, FirmwareSource *emuNand) if (i > 0 && *emuNand != FIRMWARE_EMUNAND2) break; // Check for 'Legacy', 'Default' and 'Minimum' partition layouts when checking for the 2nd EmuNAND - nandOffset = (*emuNand == FIRMWARE_EMUNAND ? 0 : (isN3DS ? nandLayoutN3DS[i] : nandLayoutO3DS[i])); + nandOffset = (*emuNand == FIRMWARE_EMUNAND ? 0 : ((isN3DS || nandSize > O3DS_TOSHIBA_NAND) ? nandLayoutN3DS[i] : nandLayoutO3DS[i])); //Check for RedNAND if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) From a331fcd8737abf5178bbd790f8f0229ae4473442 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Wed, 7 Sep 2016 11:33:05 -0400 Subject: [PATCH 5/8] Add minimum NAND size exception for 2DS. --- source/emunand.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/emunand.c b/source/emunand.c index c2db014..5fe88f9 100644 --- a/source/emunand.c +++ b/source/emunand.c @@ -54,6 +54,9 @@ void locateEmuNand(u32 *off, u32 *head, FirmwareSource *emuNand) // Check for 'Legacy', 'Default' and 'Minimum' partition layouts when checking for the 2nd EmuNAND nandOffset = (*emuNand == FIRMWARE_EMUNAND ? 0 : ((isN3DS || nandSize > O3DS_TOSHIBA_NAND) ? nandLayoutN3DS[i] : nandLayoutO3DS[i])); + // Exception for 2DS + if (i == 2 && !isN3DS && nandOffset == N3DS_MINIMUM_FAT) nandOffset = O3DS_MINIMUM_FAT; + //Check for RedNAND if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) { From deb91d1d022e20c6fbe1ee93e5a0529042b29487 Mon Sep 17 00:00:00 2001 From: Aurora Date: Wed, 7 Sep 2016 18:04:31 +0200 Subject: [PATCH 6/8] Refactor the emuNAND code --- source/emunand.c | 101 ++++++++++++++++++------------------- source/emunand.h | 2 +- source/fatfs/sdmmc/sdmmc.c | 16 +++--- source/firm.c | 4 +- 4 files changed, 59 insertions(+), 64 deletions(-) diff --git a/source/emunand.c b/source/emunand.c index 5fe88f9..3ff5486 100644 --- a/source/emunand.c +++ b/source/emunand.c @@ -25,64 +25,59 @@ #include "fatfs/sdmmc/sdmmc.h" #include "../build/emunandpatch.h" -#define O3DS_TOSHIBA_NAND 0x1DD000 - -#define O3DS_LEGACY_FAT 0x200000 -#define O3DS_DEFAULT_FAT 0x1DE000 -#define O3DS_MINIMUM_FAT 0x1D8000 - -#define N3DS_LEGACY_FAT 0x400000 -#define N3DS_DEFAULT_FAT 0x3B2000 -#define N3DS_MINIMUM_FAT 0x26E000 - -void locateEmuNand(u32 *off, u32 *head, FirmwareSource *emuNand) +void locateEmuNand(u32 *emuHeader, FirmwareSource *emuNand) { static u8 temp[0x200]; - const u32 nandSize = getMMCDevice(0)->total_size; - const u32 nandLayoutO3DS[3] = { O3DS_LEGACY_FAT, O3DS_DEFAULT_FAT, O3DS_MINIMUM_FAT }; // Legacy, Default, Minimum - const u32 nandLayoutN3DS[3] = { N3DS_LEGACY_FAT, N3DS_DEFAULT_FAT, N3DS_MINIMUM_FAT }; // Legacy, Default, Minimum - - u8 i; - u32 nandOffset; - bool found = false; - - for (i = 0; i < 3; i++) - { - if (i > 0 && *emuNand != FIRMWARE_EMUNAND2) break; - - // Check for 'Legacy', 'Default' and 'Minimum' partition layouts when checking for the 2nd EmuNAND - nandOffset = (*emuNand == FIRMWARE_EMUNAND ? 0 : ((isN3DS || nandSize > O3DS_TOSHIBA_NAND) ? nandLayoutN3DS[i] : nandLayoutO3DS[i])); - - // Exception for 2DS - if (i == 2 && !isN3DS && nandOffset == N3DS_MINIMUM_FAT) nandOffset = O3DS_MINIMUM_FAT; - - //Check for RedNAND - if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) - { - *off = nandOffset + 1; - *head = nandOffset + 1; - found = true; - break; - } - - //Check for Gateway emuNAND - else if(!sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) - { - *off = nandOffset; - *head = nandOffset + nandSize; - found = true; - break; - } - } - + bool found = false; + + for (u32 i = 0; i < 3 && !found; i++) + { + u32 nandOffset; + + switch(i) + { + case 1: + nandOffset = nandSize + 1; //"Default" layout + break; + case 2: + nandOffset = isN3DS ? 0x26E000 : 0x1D8000; //"Minsize" layout + break; + default: + nandOffset = *emuNand == FIRMWARE_EMUNAND ? 0 : (nandSize > 0x200000 ? 0x400000 : 0x200000); //"Legacy" layout + break; + } + + //Check for RedNAND + if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) + { + emuOffset = nandOffset + 1; + *emuHeader = nandOffset + 1; + found = true; + } + + //Check for Gateway emuNAND + else if(!sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) + { + emuOffset = nandOffset; + *emuHeader = nandOffset + nandSize; + found = true; + } + + if(*emuNand == FIRMWARE_EMUNAND) break; + } + /* Fallback to the first emuNAND if there's no second one, or to SysNAND if there isn't any */ - if (!found) - { - *emuNand = (*emuNand == FIRMWARE_EMUNAND2) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND; - if(*emuNand) locateEmuNand(off, head, emuNand); - } + if(!found) + { + if(*emuNand != FIRMWARE_EMUNAND) + { + *emuNand = FIRMWARE_EMUNAND; + locateEmuNand(emuHeader, emuNand); + } + else *emuNand = FIRMWARE_SYSNAND; + } } static inline u8 *getFreeK9Space(u8 *pos, u32 size) diff --git a/source/emunand.h b/source/emunand.h index 8c8fd89..9226179 100644 --- a/source/emunand.h +++ b/source/emunand.h @@ -29,5 +29,5 @@ extern u32 emuOffset; extern bool isN3DS; -void locateEmuNand(u32 *off, u32 *head, FirmwareSource *emuNand); +void locateEmuNand(u32 *emuHeader, FirmwareSource *emuNand); void patchEmuNand(u8 *arm9Section, u32 arm9SectionSize, u8 *process9Offset, u32 process9Size, u32 emuHeader, u32 branchAdditive); \ No newline at end of file diff --git a/source/fatfs/sdmmc/sdmmc.c b/source/fatfs/sdmmc/sdmmc.c index 27e2031..74901e0 100644 --- a/source/fatfs/sdmmc/sdmmc.c +++ b/source/fatfs/sdmmc/sdmmc.c @@ -341,34 +341,34 @@ static int Nand_Init() while((handleNAND.ret[0] & 0x80000000) == 0); sdmmc_send_command(&handleNAND, 0x10602, 0x0); - if((handleNAND.error & 0x4))return -1; + if((handleNAND.error & 0x4)) return -1; sdmmc_send_command(&handleNAND, 0x10403, handleNAND.initarg << 0x10); - if((handleNAND.error & 0x4))return -1; + if((handleNAND.error & 0x4)) return -1; sdmmc_send_command(&handleNAND, 0x10609, handleNAND.initarg << 0x10); - if((handleNAND.error & 0x4))return -1; + if((handleNAND.error & 0x4)) return -1; handleNAND.total_size = calcSDSize((u8*)&handleNAND.ret[0], 0); handleNAND.clk = 1; setckl(1); sdmmc_send_command(&handleNAND, 0x10407, handleNAND.initarg << 0x10); - if((handleNAND.error & 0x4))return -1; + if((handleNAND.error & 0x4)) return -1; handleNAND.SDOPT = 1; sdmmc_send_command(&handleNAND, 0x10506, 0x3B70100); - if((handleNAND.error & 0x4))return -1; + if((handleNAND.error & 0x4)) return -1; sdmmc_send_command(&handleNAND, 0x10506, 0x3B90100); - if((handleNAND.error & 0x4))return -1; + if((handleNAND.error & 0x4)) return -1; sdmmc_send_command(&handleNAND, 0x1040D, handleNAND.initarg << 0x10); - if((handleNAND.error & 0x4))return -1; + if((handleNAND.error & 0x4)) return -1; sdmmc_send_command(&handleNAND, 0x10410, 0x200); - if((handleNAND.error & 0x4))return -1; + if((handleNAND.error & 0x4)) return -1; handleNAND.clk |= 0x200; diff --git a/source/firm.c b/source/firm.c index 667e1b0..eeb2df2 100755 --- a/source/firm.c +++ b/source/firm.c @@ -195,13 +195,13 @@ void main(void) //If we need to boot emuNAND, make sure it exists if(nandType != FIRMWARE_SYSNAND) { - locateEmuNand(&emuOffset, &emuHeader, &nandType); + locateEmuNand(&emuHeader, &nandType); if(nandType == FIRMWARE_SYSNAND) firmSource = FIRMWARE_SYSNAND; } //Same if we're using emuNAND as the FIRM source else if(firmSource != FIRMWARE_SYSNAND) - locateEmuNand(&emuOffset, &emuHeader, &firmSource); + locateEmuNand(&emuHeader, &firmSource); if(!isFirmlaunch) { From ddbe5fd27ba1e62d70274835eaeff0230201a90b Mon Sep 17 00:00:00 2001 From: Aurora Date: Wed, 7 Sep 2016 22:22:31 +0200 Subject: [PATCH 7/8] Round NAND size to 4MB for the default layout --- source/emunand.c | 3 +-- source/emunand.h | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/emunand.c b/source/emunand.c index 3ff5486..93aff58 100644 --- a/source/emunand.c +++ b/source/emunand.c @@ -34,11 +34,10 @@ void locateEmuNand(u32 *emuHeader, FirmwareSource *emuNand) for (u32 i = 0; i < 3 && !found; i++) { u32 nandOffset; - switch(i) { case 1: - nandOffset = nandSize + 1; //"Default" layout + nandOffset = ROUND_TO_4MB(nandSize + 1); //"Default" layout break; case 2: nandOffset = isN3DS ? 0x26E000 : 0x1D8000; //"Minsize" layout diff --git a/source/emunand.h b/source/emunand.h index 9226179..81f8603 100644 --- a/source/emunand.h +++ b/source/emunand.h @@ -24,7 +24,8 @@ #include "types.h" -#define NCSD_MAGIC 0x4453434E +#define NCSD_MAGIC 0x4453434E +#define ROUND_TO_4MB(x) (((x) + 0x2000 - 1) & (~(0x2000 - 1))) extern u32 emuOffset; extern bool isN3DS; From 5d39242b8350ec52906c05e2bc48a481b468388b Mon Sep 17 00:00:00 2001 From: Aurora Date: Thu, 8 Sep 2016 00:49:55 +0200 Subject: [PATCH 8/8] Added support for up to 4 emuNANDs (the "second emuNAND as default" toggle is now a multi option, and you can choose the emuNAND on startup by holding Up (1)/Right (2)/Down (3)/Left (4) when EmuNAND is being booted), added a B payload as the B button was freed --- injector/source/patcher.c | 27 +++++++++++++++++++++++---- injector/source/patcher.h | 16 ++++++++-------- source/buttons.h | 3 ++- source/config.c | 11 ++++++----- source/config.h | 26 +++++++++++++------------- source/emunand.c | 2 ++ source/firm.c | 30 ++++++++++++++++++++++++------ source/fs.c | 7 ++++--- source/types.h | 4 +++- 9 files changed, 85 insertions(+), 41 deletions(-) diff --git a/injector/source/patcher.c b/injector/source/patcher.c index 9c69eb4..188a524 100644 --- a/injector/source/patcher.c +++ b/injector/source/patcher.c @@ -345,15 +345,34 @@ void patchCode(u64 progId, u8 *code, u32 size) if(CONFIG_SHOWNAND) { static const u16 verPattern[] = u"Ver."; - const u32 currentNand = BOOTCONFIG(0, 3); - const u32 matchingFirm = BOOTCONFIG(2, 1) == (currentNand != 0); + const u32 currentNand = BOOTCFG_NAND; + const u32 matchingFirm = BOOTCFG_FIRM == (currentNand != 0); + + u16 *verString; + switch(currentNand) + { + case 1: + verString = matchingFirm ? u" Emu" : u"EmuS"; + break; + case 2: + verString = matchingFirm ? u"Emu2" : u"Em2S"; + break; + case 3: + verString = matchingFirm ? u"Emu3" : u"Em3S"; + break; + case 4: + verString = matchingFirm ? u"Emu4" : u"Em4S"; + break; + default: + verString = matchingFirm ? u" Sys" : u"SysE"; + break; + } //Patch Ver. string patchMemory(code, size, verPattern, sizeof(verPattern) - sizeof(u16), 0, - !currentNand ? ((matchingFirm) ? u" Sys" : u"SysE") : - ((currentNand == 1) ? (matchingFirm ? u" Emu" : u"EmuS") : ((matchingFirm) ? u"Emu2" : u"Em2S")), + verString, sizeof(verPattern) - sizeof(u16), 1 ); } diff --git a/injector/source/patcher.h b/injector/source/patcher.h index dc48b1a..4f7b503 100644 --- a/injector/source/patcher.h +++ b/injector/source/patcher.h @@ -4,16 +4,16 @@ #define PATH_MAX 255 -#define CONFIG(a) (((info.config >> (a + 20)) & 1) != 0) -#define MULTICONFIG(a) ((info.config >> (a * 2 + 6)) & 3) +#define CONFIG(a) (((info.config >> (a + 21)) & 1) != 0) +#define MULTICONFIG(a) ((info.config >> (a * 2 + 7)) & 3) #define BOOTCONFIG(a, b) ((info.config >> a) & b) -#define BOOTCFG_NAND BOOTCONFIG(0, 3) -#define BOOTCFG_FIRM BOOTCONFIG(2, 1) -#define BOOTCFG_SAFEMODE BOOTCONFIG(5, 1) -#define CONFIG_NEWCPU MULTICONFIG(2) +#define BOOTCFG_NAND BOOTCONFIG(0, 7) +#define BOOTCFG_FIRM BOOTCONFIG(3, 1) +#define BOOTCFG_SAFEMODE BOOTCONFIG(6, 1) +#define CONFIG_NEWCPU MULTICONFIG(3) #define CONFIG_USESYSFIRM CONFIG(1) -#define CONFIG_USELANGEMUANDCODE CONFIG(3) -#define CONFIG_SHOWNAND CONFIG(4) +#define CONFIG_USELANGEMUANDCODE CONFIG(2) +#define CONFIG_SHOWNAND CONFIG(3) void patchCode(u64 progId, u8 *code, u32 size); \ No newline at end of file diff --git a/source/buttons.h b/source/buttons.h index 7c85fff..24c2adc 100644 --- a/source/buttons.h +++ b/source/buttons.h @@ -40,7 +40,8 @@ #define BUTTON_DOWN (1 << 7) #define SAFE_MODE (BUTTON_R1 | BUTTON_L1 | BUTTON_A | BUTTON_UP) -#define SINGLE_PAYLOAD_BUTTONS (BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN | BUTTON_START | BUTTON_X | BUTTON_Y) +#define SINGLE_PAYLOAD_BUTTONS (BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN | BUTTON_START | BUTTON_B | BUTTON_X | BUTTON_Y) #define L_PAYLOAD_BUTTONS (BUTTON_R1 | BUTTON_A | BUTTON_SELECT) +#define EMUNAND_BUTTONS (BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN) #define MENU_BUTTONS (BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN | BUTTON_A | BUTTON_START) #define PIN_BUTTONS (BUTTON_A | BUTTON_B | BUTTON_X | BUTTON_Y | BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN | BUTTON_START) \ No newline at end of file diff --git a/source/config.c b/source/config.c index 9dd0e32..006e253 100644 --- a/source/config.c +++ b/source/config.c @@ -71,13 +71,13 @@ void configMenu(bool oldPinStatus) drawString(CONFIG_TITLE, 10, 10, COLOR_TITLE); drawString("Press A to select, START to save", 10, 30, COLOR_WHITE); - const char *multiOptionsText[] = { "Screen brightness: 4( ) 3( ) 2( ) 1( )", + const char *multiOptionsText[] = { "Default EmuNAND: 1( ) 2( ) 3( ) 4( )", + "Screen brightness: 4( ) 3( ) 2( ) 1( )", "PIN lock: Off( ) 4( ) 6( ) 8( ) digits", "New 3DS CPU: Off( ) Clock( ) L2( ) Clock+L2( )" }; const char *singleOptionsText[] = { "( ) Autoboot SysNAND", "( ) Use SysNAND FIRM if booting with R (A9LH)", - "( ) Use second EmuNAND as default", "( ) Enable region/language emu. and ext. .code", "( ) Show current NAND in System Settings", "( ) Show GBA boot screen in patched AGB_FIRM", @@ -88,6 +88,7 @@ void configMenu(bool oldPinStatus) int posY; u32 enabled; } multiOptions[] = { + { .posXs = {19, 24, 29, 34} }, { .posXs = {21, 26, 31, 36} }, { .posXs = {14, 19, 24, 29} }, { .posXs = {17, 26, 32, 44} } @@ -201,7 +202,7 @@ void configMenu(bool oldPinStatus) drawCharacter(selected, 10 + multiOptions[selectedOption].posXs[oldEnabled] * SPACING_X, multiOptions[selectedOption].posY, COLOR_BLACK); multiOptions[selectedOption].enabled = oldEnabled == 3 ? 0 : oldEnabled + 1; - if(!selectedOption) updateBrightness(multiOptions[0].enabled); + if(selectedOption == 1) updateBrightness(multiOptions[1].enabled); } else { @@ -228,9 +229,9 @@ void configMenu(bool oldPinStatus) //Parse and write the new configuration for(u32 i = 0; i < multiOptionsAmount; i++) - configData.config |= multiOptions[i].enabled << (i * 2 + 6); + configData.config |= multiOptions[i].enabled << (i * 2 + 7); for(u32 i = 0; i < singleOptionsAmount; i++) - configData.config |= (singleOptions[i].enabled ? 1 : 0) << (i + 20); + configData.config |= (singleOptions[i].enabled ? 1 : 0) << (i + 21); if(CONFIG_PIN != 0) newPin(oldPinStatus && CONFIG_PIN == oldPinLength); else if(oldPinStatus) fileDelete(PIN_PATH); diff --git a/source/config.h b/source/config.h index 2209697..425ca76 100644 --- a/source/config.h +++ b/source/config.h @@ -24,26 +24,26 @@ #include "types.h" -#define CONFIG(a) (((configData.config >> (a + 20)) & 1) != 0) -#define MULTICONFIG(a) ((configData.config >> (a * 2 + 6)) & 3) +#define CONFIG(a) (((configData.config >> (a + 21)) & 1) != 0) +#define MULTICONFIG(a) ((configData.config >> (a * 2 + 7)) & 3) #define BOOTCONFIG(a, b) ((configData.config >> a) & b) #define CONFIG_PATH "/luma/config.bin" #define CONFIG_VERSIONMAJOR 1 -#define CONFIG_VERSIONMINOR 2 +#define CONFIG_VERSIONMINOR 3 -#define BOOTCFG_NAND BOOTCONFIG(0, 3) -#define BOOTCFG_FIRM BOOTCONFIG(2, 1) -#define BOOTCFG_A9LH BOOTCONFIG(3, 1) -#define BOOTCFG_NOFORCEFLAG BOOTCONFIG(4, 1) -#define BOOTCFG_SAFEMODE BOOTCONFIG(5, 1) -#define CONFIG_BRIGHTNESS MULTICONFIG(0) -#define CONFIG_PIN MULTICONFIG(1) +#define BOOTCFG_NAND BOOTCONFIG(0, 7) +#define BOOTCFG_FIRM BOOTCONFIG(3, 1) +#define BOOTCFG_A9LH BOOTCONFIG(4, 1) +#define BOOTCFG_NOFORCEFLAG BOOTCONFIG(5, 1) +#define BOOTCFG_SAFEMODE BOOTCONFIG(6, 1) +#define CONFIG_DEFAULTEMU MULTICONFIG(0) +#define CONFIG_BRIGHTNESS MULTICONFIG(1) +#define CONFIG_PIN MULTICONFIG(2) #define CONFIG_AUTOBOOTSYS CONFIG(0) #define CONFIG_USESYSFIRM CONFIG(1) -#define CONFIG_USESECONDEMU CONFIG(2) -#define CONFIG_SHOWGBABOOT CONFIG(5) -#define CONFIG_PAYLOADSPLASH CONFIG(6) +#define CONFIG_SHOWGBABOOT CONFIG(4) +#define CONFIG_PAYLOADSPLASH CONFIG(5) typedef struct __attribute__((packed)) { diff --git a/source/emunand.c b/source/emunand.c index 93aff58..80915af 100644 --- a/source/emunand.c +++ b/source/emunand.c @@ -47,6 +47,8 @@ void locateEmuNand(u32 *emuHeader, FirmwareSource *emuNand) break; } + if(*emuNand != FIRMWARE_EMUNAND) nandOffset *= ((u32)*emuNand - 1); + //Check for RedNAND if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) { diff --git a/source/firm.c b/source/firm.c index eeb2df2..56719e3 100755 --- a/source/firm.c +++ b/source/firm.c @@ -98,7 +98,7 @@ void main(void) u32 pressed = HID_PAD; //Save old options and begin saving the new boot configuration - configTemp = (configData.config & 0xFFFFFFC0) | ((u32)isA9lh << 3); + configTemp = (configData.config & 0xFFFFFFC0) | ((u32)isA9lh << 4); //If it's a MCU reboot, try to force boot options if(isA9lh && CFG_BOOTENV) @@ -111,7 +111,7 @@ void main(void) needConfig = DONT_CONFIGURE; //Flag to prevent multiple boot options-forcing - configTemp |= 1 << 4; + configTemp |= 1 << 5; } /* Else, force the last used boot options unless a button is pressed @@ -146,7 +146,7 @@ void main(void) firmSource = FIRMWARE_SYSNAND; //Flag to tell loader to init SD - configTemp |= 1 << 5; + configTemp |= 1 << 6; //If the PIN has been verified, wait to make it easier to press the SAFE_MODE combo if(pinExists && !shouldLoadConfigMenu) @@ -161,7 +161,8 @@ void main(void) /* If L and R/A/Select or one of the single payload buttons are pressed, chainload an external payload */ - bool shouldLoadPayload = (pressed & SINGLE_PAYLOAD_BUTTONS) || ((pressed & BUTTON_L1) && (pressed & L_PAYLOAD_BUTTONS)); + bool shouldLoadPayload = ((pressed & SINGLE_PAYLOAD_BUTTONS) && !(pressed & (BUTTON_L1 | BUTTON_R1))) || + ((pressed & L_PAYLOAD_BUTTONS) && (pressed & BUTTON_L1)); if(shouldLoadPayload) loadPayload(pressed); @@ -187,7 +188,24 @@ void main(void) /* If we're booting emuNAND the second emuNAND is set as default and B isn't pressed, or vice-versa, boot the second emuNAND */ - if(nandType != FIRMWARE_SYSNAND && (CONFIG_USESECONDEMU == !(pressed & BUTTON_B))) nandType = FIRMWARE_EMUNAND2; + if(nandType == FIRMWARE_EMUNAND) + switch(pressed & EMUNAND_BUTTONS) + { + case BUTTON_UP: + break; + case BUTTON_RIGHT: + nandType = FIRMWARE_EMUNAND2; + break; + case BUTTON_DOWN: + nandType = FIRMWARE_EMUNAND3; + break; + case BUTTON_LEFT: + nandType = FIRMWARE_EMUNAND4; + break; + default: + nandType = (FirmwareSource)(1 + CONFIG_DEFAULTEMU); + break; + } } } } @@ -205,7 +223,7 @@ void main(void) if(!isFirmlaunch) { - configTemp |= (u32)nandType | ((u32)firmSource << 2); + configTemp |= (u32)nandType | ((u32)firmSource << 3); writeConfig(needConfig, configTemp); } diff --git a/source/fs.c b/source/fs.c index abd2bde..79d68fe 100644 --- a/source/fs.c +++ b/source/fs.c @@ -103,15 +103,16 @@ void loadPayload(u32 pressed) { const char *pattern; - if(pressed & BUTTON_RIGHT) pattern = PATTERN("right"); - else if(pressed & BUTTON_LEFT) pattern = PATTERN("left"); + if(pressed & BUTTON_LEFT) pattern = PATTERN("left"); + else if(pressed & BUTTON_RIGHT) pattern = PATTERN("right"); else if(pressed & BUTTON_UP) pattern = PATTERN("up"); else if(pressed & BUTTON_DOWN) pattern = PATTERN("down"); + else if(pressed & BUTTON_START) pattern = PATTERN("start"); + else if(pressed & BUTTON_B) pattern = PATTERN("b"); else if(pressed & BUTTON_X) pattern = PATTERN("x"); else if(pressed & BUTTON_Y) pattern = PATTERN("y"); else if(pressed & BUTTON_R1) pattern = PATTERN("r"); else if(pressed & BUTTON_A) pattern = PATTERN("a"); - else if(pressed & BUTTON_START) pattern = PATTERN("start"); else pattern = PATTERN("select"); DIR dir; diff --git a/source/types.h b/source/types.h index d5fd269..6b0e9ee 100644 --- a/source/types.h +++ b/source/types.h @@ -41,7 +41,9 @@ typedef enum FirmwareSource { FIRMWARE_SYSNAND = 0, FIRMWARE_EMUNAND = 1, - FIRMWARE_EMUNAND2 = 2 + FIRMWARE_EMUNAND2 = 2, + FIRMWARE_EMUNAND3 = 3, + FIRMWARE_EMUNAND4 = 4 } FirmwareSource; typedef enum FirmwareType