From 68fc9a658698e4a4f1c4401efa1ca8f8a99fb6b8 Mon Sep 17 00:00:00 2001 From: Pk11 Date: Fri, 14 Jan 2022 22:42:47 -0600 Subject: [PATCH] Clean up formatting No code changes outside of a very minor change to arm7/main.c (removes unused functions) --- arm7/Makefile | 36 +- arm7/src/main.c | 91 ++--- arm7/src/my_sdmmc.c | 638 ++++++++++++++++++----------------- arm7/src/my_sdmmc.h | 76 +++-- arm9/src/backupmenu.c | 8 +- arm9/src/install.c | 26 +- arm9/src/installmenu.c | 18 +- arm9/src/maketmd.c | 15 +- arm9/src/menu.c | 12 +- arm9/src/message.c | 2 +- arm9/src/nand/crypto.c | 130 +++---- arm9/src/nand/crypto.h | 2 +- arm9/src/nand/f_xy.c | 33 +- arm9/src/nand/f_xy.h | 13 +- arm9/src/nand/nandio.c | 126 +++---- arm9/src/nand/nandio.h | 10 +- arm9/src/nand/polarssl/aes.c | 10 +- arm9/src/nand/sector0.c | 99 +++--- arm9/src/nand/sector0.h | 61 ++-- arm9/src/nand/twltool/dsi.c | 180 ++++------ arm9/src/nand/twltool/dsi.h | 74 ++-- arm9/src/nand/u128_math.c | 118 +++---- arm9/src/nand/u128_math.h | 22 +- arm9/src/rom.c | 16 +- arm9/src/sav.c | 32 +- arm9/src/storage.c | 30 +- arm9/src/testmenu.c | 2 +- arm9/src/titlemenu.c | 16 +- 28 files changed, 937 insertions(+), 959 deletions(-) diff --git a/arm7/Makefile b/arm7/Makefile index 7295837..a425346 100644 --- a/arm7/Makefile +++ b/arm7/Makefile @@ -15,10 +15,10 @@ include $(DEVKITARM)/ds_rules # all directories are relative to this makefile #--------------------------------------------------------------------------------- BUILD := build -SOURCES := src +SOURCES := src INCLUDES := include build DATA := - + #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- @@ -44,32 +44,32 @@ LIBS := -lnds7 # include and lib #--------------------------------------------------------------------------------- LIBDIRS := $(LIBNDS) - - + + #--------------------------------------------------------------------------------- # no real need to edit anything past this point unless you need to add additional # rules for different file extensions #--------------------------------------------------------------------------------- ifneq ($(BUILD),$(notdir $(CURDIR))) #--------------------------------------------------------------------------------- - + export ARM7ELF := $(CURDIR)/$(TARGET).elf export DEPSDIR := $(CURDIR)/$(BUILD) export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) - + CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) - + export OFILES := $(addsuffix .o,$(BINFILES)) \ $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) - + export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ -I$(CURDIR)/$(BUILD) - + export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) #--------------------------------------------------------------------------------- @@ -87,33 +87,33 @@ endif #--------------------------------------------------------------------------------- .PHONY: $(BUILD) clean - + #--------------------------------------------------------------------------------- $(BUILD): @[ -d $@ ] || mkdir -p $@ @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile - + #--------------------------------------------------------------------------------- clean: @echo clean ... @rm -fr $(BUILD) *.elf - - + + #--------------------------------------------------------------------------------- else - + DEPENDS := $(OFILES:.o=.d) - + #--------------------------------------------------------------------------------- # main targets #--------------------------------------------------------------------------------- $(ARM7ELF) : $(OFILES) @echo linking $(notdir $@) - @$(LD) $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@ + @$(LD) $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@ #--------------------------------------------------------------------------------- -# you need a rule like this for each extension you use as binary data +# you need a rule like this for each extension you use as binary data #--------------------------------------------------------------------------------- %.bin.o : %.bin #--------------------------------------------------------------------------------- @@ -121,7 +121,7 @@ $(ARM7ELF) : $(OFILES) @$(bin2o) -include $(DEPENDS) - + #--------------------------------------------------------------------------------------- endif #--------------------------------------------------------------------------------------- diff --git a/arm7/src/main.c b/arm7/src/main.c index 140317d..0d47176 100644 --- a/arm7/src/main.c +++ b/arm7/src/main.c @@ -29,34 +29,29 @@ ---------------------------------------------------------------------------------*/ #include "my_sdmmc.h" -//#include -//#include #include #include //--------------------------------------------------------------------------------- -void VblankHandler(void) { -//--------------------------------------------------------------------------------- -// Wifi_Update(); -} - - -//--------------------------------------------------------------------------------- -void VcountHandler() { +void VcountHandler() //--------------------------------------------------------------------------------- +{ inputGetAndSend(); } volatile bool exitflag = false; volatile bool reboot = false; +// Custom POWER button handling, based on the default function: // https://github.com/devkitPro/libnds/blob/154a21cc3d57716f773ff2b10f815511c1b8ba9f/source/common/interrupts.c#L51-L69 //--------------------------------------------------------------------------------- -TWL_CODE void i2cIRQHandlerCustom() { +TWL_CODE void i2cIRQHandlerCustom() //--------------------------------------------------------------------------------- +{ int cause = (i2cReadRegister(I2C_PM, I2CREGPM_PWRIF) & 0x3) | (i2cReadRegister(I2C_GPIO, 0x02)<<2); - switch (cause & 3) { + switch (cause & 3) + { case 1: reboot = true; exitflag = true; @@ -68,27 +63,32 @@ TWL_CODE void i2cIRQHandlerCustom() { } } -void set_ctr(u32* ctr){ +void set_ctr(u32* ctr) +{ for (int i = 0; i < 4; i++) REG_AES_IV[i] = ctr[3-i]; } // 10 11 22 23 24 25 -void aes(void* in, void* out, void* iv, u32 method){ //this is sort of a bodged together dsi aes function adapted from this 3ds function - REG_AES_CNT = ( AES_CNT_MODE(method) | //https://github.com/TiniVi/AHPCFW/blob/master/source/aes.c#L42 - AES_WRFIFO_FLUSH | //as long as the output changes when keyslot values change, it's good enough. - AES_RDFIFO_FLUSH | - AES_CNT_KEY_APPLY | +//this is sort of a bodged together dsi aes function adapted from this 3ds function +//https://github.com/TiniVi/AHPCFW/blob/master/source/aes.c#L42 +//as long as the output changes when keyslot values change, it's good enough. +void aes(void* in, void* out, void* iv, u32 method) +{ + REG_AES_CNT = ( AES_CNT_MODE(method) | + AES_WRFIFO_FLUSH | + AES_RDFIFO_FLUSH | + AES_CNT_KEY_APPLY | AES_CNT_KEYSLOT(3) | AES_CNT_DMA_WRITE_SIZE(2) | AES_CNT_DMA_READ_SIZE(1) ); - - if (iv != NULL) set_ctr((u32*)iv); + + if (iv != NULL) set_ctr((u32*)iv); REG_AES_BLKCNT = (1 << 16); REG_AES_CNT |= 0x80000000; - + for (int j = 0; j < 0x10; j+=4) REG_AES_WRFIFO = *((u32*)(in+j)); - while(((REG_AES_CNT >> 0x5) & 0x1F) < 0x4); //wait for every word to get processed + while (((REG_AES_CNT >> 0x5) & 0x1F) < 0x4); //wait for every word to get processed for (int j = 0; j < 0x10; j+=4) *((u32*)(out+j)) = REG_AES_RDFIFO; //REG_AES_CNT &= ~0x80000000; //if (method & (AES_CTR_DECRYPT | AES_CTR_ENCRYPT)) add_ctr((u8*)iv); @@ -97,8 +97,9 @@ void aes(void* in, void* out, void* iv, u32 method){ //this is sort of a bodged int my_sdmmc_nand_startup(); //--------------------------------------------------------------------------------- -int main() { +int main() //--------------------------------------------------------------------------------- +{ // clear sound registers dmaFillWords(0, (void*)0x04000400, 0x100); @@ -115,8 +116,9 @@ int main() { initClockIRQ(); fifoInit(); touchInit(); - - if (isDSiMode() /*|| ((REG_SCFG_EXT & BIT(17)) && (REG_SCFG_EXT & BIT(18)))*/) { + + if (isDSiMode() /*|| ((REG_SCFG_EXT & BIT(17)) && (REG_SCFG_EXT & BIT(18)))*/) + { u8 *out=(u8*)0x02300000; #if USENATIVECONSOLEID @@ -132,19 +134,21 @@ int main() { u8 base[16]={0}; u8 in[16]={0}; u8 iv[16]={0}; - u8 *scratch=(u8*)0x02300200; + u8 *scratch=(u8*)0x02300200; u8 *key3=(u8*)0x40044D0; - - + aes(in, base, iv, 2); //write consecutive 0-255 values to any byte in key3 until we get the same aes output as "base" above - this reveals the hidden byte. this way we can uncover all 16 bytes of the key3 normalkey pretty easily. //greets to Martin Korth for this trick https://problemkaputt.de/gbatek.htm#dsiaesioports (Reading Write-Only Values) - for(int i=0;i<16;i++){ - for(int j=0;j<256;j++){ + for (int i=0;i<16;i++) + { + for (int j=0;j<256;j++) + { *(key3+i)=j & 0xFF; aes(in, scratch, iv, 2); - if(!memcmp(scratch, base, 16)){ + if (!memcmp(scratch, base, 16)) + { out[i]=j; //hit++; break; @@ -152,31 +156,27 @@ int main() { } } } - - my_sdmmc_nand_startup() ; + + my_sdmmc_nand_startup(); my_sdmmc_get_cid(true, (u32*)0x2FFD7BC); // Get eMMC CID //sdmmc_nand_cid((u32*)0x2FFD7BC); - } - - - -// mmInstall(FIFO_MAXMOD); + } SetYtrigger(80); -// installWifiFIFO(); installSoundFIFO(); installSystemFIFO(); irqSet(IRQ_VCOUNT, VcountHandler); - irqSet(IRQ_VBLANK, VblankHandler); irqEnable( IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK); // Keep the ARM7 mostly idle - while (!exitflag) { - if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R))) { + while (!exitflag) + { + if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R))) + { exitflag = true; } @@ -195,10 +195,13 @@ int main() { fifoWaitValue32(FIFO_USER_02); fifoCheckValue32(FIFO_USER_02); - if (reboot) { + if (reboot) + { i2cWriteRegister(I2C_PM, I2CREGPM_RESETFLAG, 1); i2cWriteRegister(I2C_PM, I2CREGPM_PWRCNT, 1); - } else { + } + else + { writePowerManagement(PM_CONTROL_REG,PM_SYSTEM_PWR); } diff --git a/arm7/src/my_sdmmc.c b/arm7/src/my_sdmmc.c index c257bb9..c8f9a2c 100644 --- a/arm7/src/my_sdmmc.c +++ b/arm7/src/my_sdmmc.c @@ -10,50 +10,57 @@ static struct mmcdevice deviceSD; static struct mmcdevice deviceNAND; -/*mmcdevice *getMMCDevice(int drive) { - if(drive==0) return &deviceNAND; - return &deviceSD; +/*mmcdevice *getMMCDevice(int drive) +{ + if (drive==0) return &deviceNAND; + return &deviceSD; } */ //--------------------------------------------------------------------------------- -int my_geterror(struct mmcdevice *ctx) { +int my_geterror(struct mmcdevice *ctx) //--------------------------------------------------------------------------------- - //if(ctx->error == 0x4) return -1; - //else return 0; - return (ctx->error << 29) >> 31; +{ + //if (ctx->error == 0x4) return -1; + //else return 0; + return (ctx->error << 29) >> 31; } //--------------------------------------------------------------------------------- -void my_setTarget(struct mmcdevice *ctx) { +void my_setTarget(struct mmcdevice *ctx) //--------------------------------------------------------------------------------- - 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); - } +{ + 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); + } } //--------------------------------------------------------------------------------- -void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) { +void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) //--------------------------------------------------------------------------------- +{ const bool getSDRESP = (cmd << 15) >> 31; u16 flags = (cmd << 15) >> 31; const bool readdata = cmd & 0x20000; const bool 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? + 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); @@ -87,34 +94,34 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) { #endif u16 status0 = 0; - while(1) + while (1) { volatile u16 status1 = sdmmc_read16(REG_SDSTATUS1); #ifdef DATA32_SUPPORT volatile u16 ctl32 = sdmmc_read16(REG_SDDATACTL32); - if((ctl32 & 0x100)) + if (ctl32 & 0x100) #else - if((status1 & TMIO_STAT1_RXRDY)) + if (status1 & TMIO_STAT1_RXRDY) #endif { - if(readdata) + if (readdata) { - if(rUseBuf) + if (rUseBuf) { sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0); - if(size >= blkSize) + if (size >= blkSize) { #ifdef DATA32_SUPPORT - if(!((u32)rDataPtr32 & 3)) + if (!((u32)rDataPtr32 & 3)) { - for(u32 i = 0; i < blkSize; i += 4) + for (u32 i = 0; i < blkSize; i += 4) { *rDataPtr32++ = sdmmc_read32(REG_SDFIFO32); } } else { - for(u32 i = 0; i < blkSize; i += 4) + for (u32 i = 0; i < blkSize; i += 4) { u32 data = sdmmc_read32(REG_SDFIFO32); *rDataPtr8++ = data; @@ -124,16 +131,16 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) { } } #else - if(!((u16)rDataPtr16 & 1)) + if (!((u16)rDataPtr16 & 1)) { - for(u16 i = 0; i < blkSize; i += 2) + for (u16 i = 0; i < blkSize; i += 2) { *rDataPtr16++ = sdmmc_read16(REG_SDFIFO); } } else { - for(u16 i = 0; i < blkSize; i += 2) + for (u16 i = 0; i < blkSize; i += 2) { u16 data = sdmmc_read16(REG_SDFIFO); *rDataPtr8++ = data; @@ -149,29 +156,29 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) { } } #ifdef DATA32_SUPPORT - if(!(ctl32 & 0x200)) + if (!(ctl32 & 0x200)) #else - if((status1 & TMIO_STAT1_TXRQ)) + if ((status1 & TMIO_STAT1_TXRQ)) #endif { - if(writedata) + if (writedata) { - if(tUseBuf) + if (tUseBuf) { sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0); - if(size >= blkSize) + if (size >= blkSize) { #ifdef DATA32_SUPPORT - if(!((u32)tDataPtr32 & 3)) + if (!((u32)tDataPtr32 & 3)) { - for(u32 i = 0; i < blkSize; i += 4) + for (u32 i = 0; i < blkSize; i += 4) { sdmmc_write32(REG_SDFIFO32, *tDataPtr32++); } } else { - for(u32 i = 0; i < blkSize; i += 4) + for (u32 i = 0; i < blkSize; i += 4) { u32 data = *tDataPtr8++; data |= (u32)*tDataPtr8++ << 8; @@ -181,16 +188,16 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) { } } #else - if(!((u16)tDataPtr16 & 1)) + if (!((u16)tDataPtr16 & 1)) { - for(u16 i = 0; i < blkSize; i += 2) + for (u16 i = 0; i < blkSize; i += 2) { sdmmc_write16(REG_SDFIFO, *tDataPtr16++); } } else { - for(u16 i = 0; i < blkSize; i += 2) + for (u16 i = 0; i < blkSize; i += 2) { u16 data = *tDataPtr8++; data |= (u16)(*tDataPtr8++ << 8); @@ -205,25 +212,25 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) { sdmmc_mask16(REG_SDDATACTL32, 0x1000, 0); } } - if(status1 & TMIO_MASK_GW) + 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; } } @@ -232,7 +239,7 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) { sdmmc_write16(REG_SDSTATUS0,0); sdmmc_write16(REG_SDSTATUS1,0); - if(getSDRESP != 0) + 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)); @@ -243,8 +250,9 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) { //--------------------------------------------------------------------------------- -int my_sdmmc_cardinserted() { +int my_sdmmc_cardinserted() //--------------------------------------------------------------------------------- +{ return 1; //my_sdmmc_cardready; } @@ -252,162 +260,168 @@ int my_sdmmc_cardinserted() { static bool my_sdmmc_controller_initialised = false; //--------------------------------------------------------------------------------- -void my_sdmmc_controller_init( bool force_init ) { +void my_sdmmc_controller_init( bool force_init ) //--------------------------------------------------------------------------------- +{ - if (!force_init && my_sdmmc_controller_initialised) return; + if (!force_init && my_sdmmc_controller_initialised) return; - deviceSD.isSDHC = 0; - deviceSD.SDOPT = 0; - deviceSD.res = 0; - deviceSD.initarg = 0; - deviceSD.clk = 0x80; - deviceSD.devicenumber = 0; + deviceSD.isSDHC = 0; + deviceSD.SDOPT = 0; + deviceSD.res = 0; + deviceSD.initarg = 0; + deviceSD.clk = 0x80; + deviceSD.devicenumber = 0; - deviceNAND.isSDHC = 0; - deviceNAND.SDOPT = 0; - deviceNAND.res = 0; - deviceNAND.initarg = 1; - deviceNAND.clk = 0x80; - deviceNAND.devicenumber = 1; + deviceNAND.isSDHC = 0; + deviceNAND.SDOPT = 0; + deviceNAND.res = 0; + deviceNAND.initarg = 1; + deviceNAND.clk = 0x80; + deviceNAND.devicenumber = 1; - *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xF7FFu; - *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xEFFFu; + *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xF7FFu; + *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xEFFFu; #ifdef DATA32_SUPPORT - *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u; + *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u; #else - *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u; + *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u; #endif - *(vu16*)(SDMMC_BASE + REG_SDDATACTL) = (*(vu16*)(SDMMC_BASE + REG_SDDATACTL) & 0xFFDD) | 2; + *(vu16*)(SDMMC_BASE + REG_SDDATACTL) = (*(vu16*)(SDMMC_BASE + REG_SDDATACTL) & 0xFFDD) | 2; #ifdef DATA32_SUPPORT - *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFFu; - *(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDFu; - *(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 512; + *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFFu; + *(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDFu; + *(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 512; #else - *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFDu; - *(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDDu; - *(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 0; + *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFDu; + *(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDDu; + *(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 0; #endif - *(vu16*)(SDMMC_BASE + REG_SDBLKCOUNT32) = 1; - *(vu16*)(SDMMC_BASE + REG_SDRESET) &= 0xFFFEu; - *(vu16*)(SDMMC_BASE + REG_SDRESET) |= 1u; - *(vu16*)(SDMMC_BASE + REG_SDIRMASK0) |= TMIO_MASK_ALL; - *(vu16*)(SDMMC_BASE + REG_SDIRMASK1) |= TMIO_MASK_ALL>>16; - *(vu16*)(SDMMC_BASE + 0x0fc) |= 0xDBu; //SDCTL_RESERVED7 - *(vu16*)(SDMMC_BASE + 0x0fe) |= 0xDBu; //SDCTL_RESERVED8 - *(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu; + *(vu16*)(SDMMC_BASE + REG_SDBLKCOUNT32) = 1; + *(vu16*)(SDMMC_BASE + REG_SDRESET) &= 0xFFFEu; + *(vu16*)(SDMMC_BASE + REG_SDRESET) |= 1u; + *(vu16*)(SDMMC_BASE + REG_SDIRMASK0) |= TMIO_MASK_ALL; + *(vu16*)(SDMMC_BASE + REG_SDIRMASK1) |= TMIO_MASK_ALL>>16; + *(vu16*)(SDMMC_BASE + 0x0fc) |= 0xDBu; //SDCTL_RESERVED7 + *(vu16*)(SDMMC_BASE + 0x0fe) |= 0xDBu; //SDCTL_RESERVED8 + *(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu; #ifdef DATA32_SUPPORT - *(vu16*)(SDMMC_BASE + REG_SDCLKCTL) = 0x20; - *(vu16*)(SDMMC_BASE + REG_SDOPT) = 0x40EE; + *(vu16*)(SDMMC_BASE + REG_SDCLKCTL) = 0x20; + *(vu16*)(SDMMC_BASE + REG_SDOPT) = 0x40EE; #else - *(vu16*)(SDMMC_BASE + REG_SDCLKCTL) = 0x40; //Nintendo sets this to 0x20 - *(vu16*)(SDMMC_BASE + REG_SDOPT) = 0x40EB; //Nintendo sets this to 0x40EE + *(vu16*)(SDMMC_BASE + REG_SDCLKCTL) = 0x40; //Nintendo sets this to 0x20 + *(vu16*)(SDMMC_BASE + REG_SDOPT) = 0x40EB; //Nintendo sets this to 0x40EE #endif - *(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu; - *(vu16*)(SDMMC_BASE + REG_SDBLKLEN) = 512; - *(vu16*)(SDMMC_BASE + REG_SDSTOP) = 0; + *(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu; + *(vu16*)(SDMMC_BASE + REG_SDBLKLEN) = 512; + *(vu16*)(SDMMC_BASE + REG_SDSTOP) = 0; - my_sdmmc_controller_initialised = true; + my_sdmmc_controller_initialised = true; - my_setTarget(&deviceSD); + my_setTarget(&deviceSD); } //--------------------------------------------------------------------------------- -static u32 calcSDSize(u8* csd, int type) { +static u32 calcSDSize(u8* csd, int type) //--------------------------------------------------------------------------------- - u32 result = 0; - if (type == -1) type = csd[14] >> 6; - switch (type) { - case 0: - { - u32 block_len = csd[9] & 0xf; - block_len = 1 << block_len; - u32 mult = (csd[4] >> 7) | ((csd[5] & 3) << 1); - mult = 1 << (mult + 2); - result = csd[8] & 3; - result = (result << 8) | csd[7]; - result = (result << 2) | (csd[6] >> 6); - result = (result + 1) * mult * block_len / 512; - } - break; - case 1: - result = csd[7] & 0x3f; - result = (result << 8) | csd[6]; - result = (result << 8) | csd[5]; - result = (result + 1) * 1024; - break; - } - return result; +{ + u32 result = 0; + if (type == -1) type = csd[14] >> 6; + switch (type) + { + case 0: + { + u32 block_len = csd[9] & 0xf; + block_len = 1 << block_len; + u32 mult = (csd[4] >> 7) | ((csd[5] & 3) << 1); + mult = 1 << (mult + 2); + result = csd[8] & 3; + result = (result << 8) | csd[7]; + result = (result << 2) | (csd[6] >> 6); + result = (result + 1) * mult * block_len / 512; + } + break; + case 1: + result = csd[7] & 0x3f; + result = (result << 8) | csd[6]; + result = (result << 8) | csd[5]; + result = (result + 1) * 1024; + break; + } + return result; } //--------------------------------------------------------------------------------- -int my_sdmmc_sdcard_init() { +int my_sdmmc_sdcard_init() //--------------------------------------------------------------------------------- +{ // We need to send at least 74 clock pulses. - my_setTarget(&deviceSD); + my_setTarget(&deviceSD); swiDelay(0x1980); // ~75-76 clocks - // card reset - my_sdmmc_send_command(&deviceSD,0,0); + // card reset + my_sdmmc_send_command(&deviceSD,0,0); - // CMD8 0x1AA - my_sdmmc_send_command(&deviceSD,0x10408,0x1AA); - u32 temp = (deviceSD.error & 0x1) << 0x1E; + // CMD8 0x1AA + my_sdmmc_send_command(&deviceSD,0x10408,0x1AA); + u32 temp = (deviceSD.error & 0x1) << 0x1E; - u32 temp2 = 0; - do { - do { - // CMD55 - my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10); - // ACMD41 - my_sdmmc_send_command(&deviceSD,0x10769,0x00FF8000 | temp); - temp2 = 1; - } while ( !(deviceSD.error & 1) ); + u32 temp2 = 0; + do { + do { + // CMD55 + my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10); + // ACMD41 + my_sdmmc_send_command(&deviceSD,0x10769,0x00FF8000 | temp); + temp2 = 1; + } + while ( !(deviceSD.error & 1) ); - } while((deviceSD.ret[0] & 0x80000000) == 0); + } + while ((deviceSD.ret[0] & 0x80000000) == 0); - if(!((deviceSD.ret[0] >> 30) & 1) || !temp) - temp2 = 0; + if (!((deviceSD.ret[0] >> 30) & 1) || !temp) + temp2 = 0; - deviceSD.isSDHC = temp2; + deviceSD.isSDHC = temp2; - my_sdmmc_send_command(&deviceSD,0x10602,0); - if (deviceSD.error & 0x4) return -1; + my_sdmmc_send_command(&deviceSD,0x10602,0); + if (deviceSD.error & 0x4) return -1; - my_sdmmc_send_command(&deviceSD,0x10403,0); - if (deviceSD.error & 0x4) return -1; - deviceSD.initarg = deviceSD.ret[0] >> 0x10; + my_sdmmc_send_command(&deviceSD,0x10403,0); + if (deviceSD.error & 0x4) return -1; + deviceSD.initarg = deviceSD.ret[0] >> 0x10; - my_sdmmc_send_command(&deviceSD,0x10609,deviceSD.initarg << 0x10); - if (deviceSD.error & 0x4) return -1; + my_sdmmc_send_command(&deviceSD,0x10609,deviceSD.initarg << 0x10); + if (deviceSD.error & 0x4) return -1; // Command Class 10 support const bool cmd6Supported = ((u8*)deviceSD.ret)[10] & 0x40; - deviceSD.total_size = calcSDSize((u8*)&deviceSD.ret[0],-1); - setckl(0x201); // 16.756991 MHz + deviceSD.total_size = calcSDSize((u8*)&deviceSD.ret[0],-1); + setckl(0x201); // 16.756991 MHz - my_sdmmc_send_command(&deviceSD,0x10507,deviceSD.initarg << 0x10); - if (deviceSD.error & 0x4) return -1; + my_sdmmc_send_command(&deviceSD,0x10507,deviceSD.initarg << 0x10); + if (deviceSD.error & 0x4) return -1; - // CMD55 - my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10); - if (deviceSD.error & 0x4) return -1; + // CMD55 + my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10); + if (deviceSD.error & 0x4) return -1; - // ACMD42 - my_sdmmc_send_command(&deviceSD,0x1076A,0x0); - if (deviceSD.error & 0x4) return -1; + // ACMD42 + my_sdmmc_send_command(&deviceSD,0x1076A,0x0); + if (deviceSD.error & 0x4) return -1; - // CMD55 - my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10); - if (deviceSD.error & 0x4) return -7; + // CMD55 + my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10); + if (deviceSD.error & 0x4) return -7; - deviceSD.SDOPT = 1; - my_sdmmc_send_command(&deviceSD,0x10446,0x2); - if (deviceSD.error & 0x4) return -8; + deviceSD.SDOPT = 1; + my_sdmmc_send_command(&deviceSD,0x10446,0x2); + if (deviceSD.error & 0x4) return -8; sdmmc_mask16(REG_SDOPT, 0x8000, 0); // Switch to 4 bit mode. // TODO: CMD6 to switch to high speed mode. - if(cmd6Supported) + if (cmd6Supported) { sdmmc_write16(REG_SDSTOP,0); sdmmc_write16(REG_SDBLKLEN32,64); @@ -416,251 +430,267 @@ int my_sdmmc_sdcard_init() { deviceSD.size = 64; my_sdmmc_send_command(&deviceSD,0x31C06,0x80FFFFF1); sdmmc_write16(REG_SDBLKLEN,512); - if(deviceSD.error & 0x4) return -9; + if (deviceSD.error & 0x4) return -9; deviceSD.clk = 0x200; // 33.513982 MHz setckl(0x200); } else deviceSD.clk = 0x201; // 16.756991 MHz - my_sdmmc_send_command(&deviceSD,0x1040D,deviceSD.initarg << 0x10); - if (deviceSD.error & 0x4) return -9; + my_sdmmc_send_command(&deviceSD,0x1040D,deviceSD.initarg << 0x10); + if (deviceSD.error & 0x4) return -9; - my_sdmmc_send_command(&deviceSD,0x10410,0x200); - if (deviceSD.error & 0x4) return -10; + my_sdmmc_send_command(&deviceSD,0x10410,0x200); + if (deviceSD.error & 0x4) return -10; - return 0; + return 0; } //--------------------------------------------------------------------------------- -int my_sdmmc_nand_init() { +int my_sdmmc_nand_init() //--------------------------------------------------------------------------------- - my_setTarget(&deviceNAND); - swiDelay(0xF000); +{ + my_setTarget(&deviceNAND); + swiDelay(0xF000); - my_sdmmc_send_command(&deviceNAND,0,0); + my_sdmmc_send_command(&deviceNAND,0,0); - do { - do { - my_sdmmc_send_command(&deviceNAND,0x10701,0x100000); - } while ( !(deviceNAND.error & 1) ); - } - while((deviceNAND.ret[0] & 0x80000000) == 0); + do { + do { + my_sdmmc_send_command(&deviceNAND,0x10701,0x100000); + } + while ( !(deviceNAND.error & 1) ); + } + while ((deviceNAND.ret[0] & 0x80000000) == 0); - my_sdmmc_send_command(&deviceNAND,0x10602,0x0); - if((deviceNAND.error & 0x4))return -1; + my_sdmmc_send_command(&deviceNAND,0x10602,0x0); + if ((deviceNAND.error & 0x4))return -1; - my_sdmmc_send_command(&deviceNAND,0x10403,deviceNAND.initarg << 0x10); - if((deviceNAND.error & 0x4))return -1; + my_sdmmc_send_command(&deviceNAND,0x10403,deviceNAND.initarg << 0x10); + if ((deviceNAND.error & 0x4))return -1; - my_sdmmc_send_command(&deviceNAND,0x10609,deviceNAND.initarg << 0x10); - if((deviceNAND.error & 0x4))return -1; + my_sdmmc_send_command(&deviceNAND,0x10609,deviceNAND.initarg << 0x10); + if ((deviceNAND.error & 0x4))return -1; - deviceNAND.total_size = calcSDSize((uint8_t*)&deviceNAND.ret[0],0); - deviceNAND.clk = 1; - setckl(1); + deviceNAND.total_size = calcSDSize((uint8_t*)&deviceNAND.ret[0],0); + deviceNAND.clk = 1; + setckl(1); - my_sdmmc_send_command(&deviceNAND,0x10407,deviceNAND.initarg << 0x10); - if((deviceNAND.error & 0x4))return -1; + my_sdmmc_send_command(&deviceNAND,0x10407,deviceNAND.initarg << 0x10); + if ((deviceNAND.error & 0x4))return -1; - deviceNAND.SDOPT = 1; + deviceNAND.SDOPT = 1; - my_sdmmc_send_command(&deviceNAND,0x10506,0x3B70100); - if((deviceNAND.error & 0x4))return -1; + my_sdmmc_send_command(&deviceNAND,0x10506,0x3B70100); + if ((deviceNAND.error & 0x4))return -1; - my_sdmmc_send_command(&deviceNAND,0x10506,0x3B90100); - if((deviceNAND.error & 0x4))return -1; + my_sdmmc_send_command(&deviceNAND,0x10506,0x3B90100); + if ((deviceNAND.error & 0x4))return -1; - my_sdmmc_send_command(&deviceNAND,0x1040D,deviceNAND.initarg << 0x10); - if((deviceNAND.error & 0x4))return -1; + my_sdmmc_send_command(&deviceNAND,0x1040D,deviceNAND.initarg << 0x10); + if ((deviceNAND.error & 0x4))return -1; - my_sdmmc_send_command(&deviceNAND,0x10410,0x200); - if((deviceNAND.error & 0x4))return -1; + my_sdmmc_send_command(&deviceNAND,0x10410,0x200); + if ((deviceNAND.error & 0x4))return -1; - deviceNAND.clk |= 0x200; + deviceNAND.clk |= 0x200; - my_setTarget(&deviceSD); + my_setTarget(&deviceSD); - return 0; + return 0; } //--------------------------------------------------------------------------------- -int my_sdmmc_readsectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *out) { +int my_sdmmc_readsectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *out) //--------------------------------------------------------------------------------- - if (device->isSDHC == 0) sector_no <<= 9; - my_setTarget(device); - sdmmc_write16(REG_SDSTOP,0x100); +{ + if (device->isSDHC == 0) sector_no <<= 9; + my_setTarget(device); + sdmmc_write16(REG_SDSTOP,0x100); #ifdef DATA32_SUPPORT - sdmmc_write16(REG_SDBLKCOUNT32,numsectors); - sdmmc_write16(REG_SDBLKLEN32,0x200); + sdmmc_write16(REG_SDBLKCOUNT32,numsectors); + sdmmc_write16(REG_SDBLKLEN32,0x200); #endif - sdmmc_write16(REG_SDBLKCOUNT,numsectors); - device->rData = out; - device->size = numsectors << 9; - my_sdmmc_send_command(device,0x33C12,sector_no); - my_setTarget(&deviceSD); - return my_geterror(device); + sdmmc_write16(REG_SDBLKCOUNT,numsectors); + device->rData = out; + device->size = numsectors << 9; + my_sdmmc_send_command(device,0x33C12,sector_no); + my_setTarget(&deviceSD); + return my_geterror(device); } //--------------------------------------------------------------------------------- -int my_sdmmc_writesectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *in) { +int my_sdmmc_writesectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *in) //--------------------------------------------------------------------------------- - if (device->isSDHC == 0) - sector_no <<= 9; - my_setTarget(device); - sdmmc_write16(REG_SDSTOP,0x100); +{ + if (device->isSDHC == 0) + sector_no <<= 9; + my_setTarget(device); + sdmmc_write16(REG_SDSTOP,0x100); #ifdef DATA32_SUPPORT - sdmmc_write16(REG_SDBLKCOUNT32,numsectors); - sdmmc_write16(REG_SDBLKLEN32,0x200); + sdmmc_write16(REG_SDBLKCOUNT32,numsectors); + sdmmc_write16(REG_SDBLKLEN32,0x200); #endif - sdmmc_write16(REG_SDBLKCOUNT,numsectors); - device->tData = in; - device->size = numsectors << 9; - my_sdmmc_send_command(device,0x52C19,sector_no); - my_setTarget(&deviceSD); - return my_geterror(device); + sdmmc_write16(REG_SDBLKCOUNT,numsectors); + device->tData = in; + device->size = numsectors << 9; + my_sdmmc_send_command(device,0x52C19,sector_no); + my_setTarget(&deviceSD); + return my_geterror(device); } //--------------------------------------------------------------------------------- -void my_sdmmc_get_cid(int devicenumber, u32 *cid) { +void my_sdmmc_get_cid(int devicenumber, u32 *cid) //--------------------------------------------------------------------------------- +{ - struct mmcdevice *device = (devicenumber == 1 ? &deviceNAND : &deviceSD); + struct mmcdevice *device = (devicenumber == 1 ? &deviceNAND : &deviceSD); - int oldIME = enterCriticalSection(); + int oldIME = enterCriticalSection(); - my_setTarget(device); + my_setTarget(device); - // use cmd7 to put sd card in standby mode - // CMD7 - my_sdmmc_send_command(device, 0x10507, 0); + // use cmd7 to put sd card in standby mode + // CMD7 + my_sdmmc_send_command(device, 0x10507, 0); - // get sd card info - // use cmd10 to read CID - my_sdmmc_send_command(device, 0x1060A, device->initarg << 0x10); + // get sd card info + // use cmd10 to read CID + my_sdmmc_send_command(device, 0x1060A, device->initarg << 0x10); - for(int i = 0; i < 4; ++i) - cid[i] = device->ret[i]; + for (int i = 0; i < 4; ++i) + cid[i] = device->ret[i]; - // put sd card back to transfer mode - // CMD7 - my_sdmmc_send_command(device, 0x10507, device->initarg << 0x10); + // put sd card back to transfer mode + // CMD7 + my_sdmmc_send_command(device, 0x10507, device->initarg << 0x10); - leaveCriticalSection(oldIME); + leaveCriticalSection(oldIME); } //--------------------------------------------------------------------------------- -void my_sdmmcMsgHandler(int bytes, void *user_data) { +void my_sdmmcMsgHandler(int bytes, void *user_data) //--------------------------------------------------------------------------------- - FifoMessage msg; - int retval = 0; +{ + FifoMessage msg; + int retval = 0; - fifoGetDatamsg(FIFO_SDMMC, bytes, (u8*)&msg); + fifoGetDatamsg(FIFO_SDMMC, bytes, (u8*)&msg); - int oldIME = enterCriticalSection(); - switch (msg.type) { + int oldIME = enterCriticalSection(); + switch (msg.type) + { + case SDMMC_SD_READ_SECTORS: + retval = my_sdmmc_readsectors(&deviceSD, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer); + break; + case SDMMC_SD_WRITE_SECTORS: + retval = my_sdmmc_writesectors(&deviceSD, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer); + break; + case SDMMC_NAND_READ_SECTORS: + retval = my_sdmmc_readsectors(&deviceNAND, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer); + break; + case SDMMC_NAND_WRITE_SECTORS: + retval = my_sdmmc_writesectors(&deviceNAND, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer); + break; + } - case SDMMC_SD_READ_SECTORS: - retval = my_sdmmc_readsectors(&deviceSD, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer); - break; - case SDMMC_SD_WRITE_SECTORS: - retval = my_sdmmc_writesectors(&deviceSD, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer); - break; - case SDMMC_NAND_READ_SECTORS: - retval = my_sdmmc_readsectors(&deviceNAND, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer); - break; - case SDMMC_NAND_WRITE_SECTORS: - retval = my_sdmmc_writesectors(&deviceNAND, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer); - break; - } + leaveCriticalSection(oldIME); - leaveCriticalSection(oldIME); - - fifoSendValue32(FIFO_SDMMC, retval); + fifoSendValue32(FIFO_SDMMC, retval); } //--------------------------------------------------------------------------------- -int my_sdmmc_nand_startup() { +int my_sdmmc_nand_startup() //--------------------------------------------------------------------------------- - my_sdmmc_controller_init(false); - return my_sdmmc_nand_init(); +{ + my_sdmmc_controller_init(false); + return my_sdmmc_nand_init(); } //--------------------------------------------------------------------------------- -int my_sdmmc_sd_startup() { +int my_sdmmc_sd_startup() //--------------------------------------------------------------------------------- - my_sdmmc_controller_init(false); - return my_sdmmc_sdcard_init(); +{ + my_sdmmc_controller_init(false); + return my_sdmmc_sdcard_init(); } //--------------------------------------------------------------------------------- -void my_sdmmcValueHandler(u32 value, void* user_data) { +void my_sdmmcValueHandler(u32 value, void* user_data) //--------------------------------------------------------------------------------- - int result = 0; - int sdflag = 0; - int oldIME = enterCriticalSection(); +{ + int result = 0; + int sdflag = 0; + int oldIME = enterCriticalSection(); - switch(value) { + switch (value) + { + case SDMMC_HAVE_SD: + result = sdmmc_read16(REG_SDSTATUS0); + break; - case SDMMC_HAVE_SD: - result = sdmmc_read16(REG_SDSTATUS0); - break; + case SDMMC_SD_START: + sdflag = 1; + /* Falls through. */ + case SDMMC_NAND_START: + if (sdmmc_read16(REG_SDSTATUS0) == 0) + { + result = 1; + } + else + { + result = (sdflag == 1 ) ? my_sdmmc_sd_startup() : my_sdmmc_nand_startup(); + } + break; - case SDMMC_SD_START: - sdflag = 1; - /* Falls through. */ - case SDMMC_NAND_START: - if (sdmmc_read16(REG_SDSTATUS0) == 0) { - result = 1; - } else { - result = (sdflag == 1 ) ? my_sdmmc_sd_startup() : my_sdmmc_nand_startup(); - } - break; + case SDMMC_SD_IS_INSERTED: + result = my_sdmmc_cardinserted(); + break; - case SDMMC_SD_IS_INSERTED: - result = my_sdmmc_cardinserted(); - break; + case SDMMC_SD_STOP: + break; - case SDMMC_SD_STOP: - break; + case SDMMC_NAND_SIZE: + result = deviceNAND.total_size; + break; + } - case SDMMC_NAND_SIZE: - result = deviceNAND.total_size; - break; - } + leaveCriticalSection(oldIME); - leaveCriticalSection(oldIME); - - fifoSendValue32(FIFO_SDMMC, result); + fifoSendValue32(FIFO_SDMMC, result); } //--------------------------------------------------------------------------------- -int my_sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out) { +int my_sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out) //--------------------------------------------------------------------------------- - return my_sdmmc_readsectors(&deviceSD, sector_no, numsectors, out); +{ + return my_sdmmc_readsectors(&deviceSD, sector_no, numsectors, out); } //--------------------------------------------------------------------------------- -int my_sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, void *in) { +int my_sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, void *in) //--------------------------------------------------------------------------------- - return my_sdmmc_writesectors(&deviceSD, sector_no, numsectors, in); +{ + return my_sdmmc_writesectors(&deviceSD, sector_no, numsectors, in); } //--------------------------------------------------------------------------------- -int my_sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, void *out) { +int my_sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, void *out) //--------------------------------------------------------------------------------- - return my_sdmmc_readsectors(&deviceNAND, sector_no, numsectors, out); +{ + return my_sdmmc_readsectors(&deviceNAND, sector_no, numsectors, out); } //--------------------------------------------------------------------------------- -int my_sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, void *in) { +int my_sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, void *in) //--------------------------------------------------------------------------------- - return my_sdmmc_writesectors(&deviceNAND, sector_no, numsectors, in); +{ + return my_sdmmc_writesectors(&deviceNAND, sector_no, numsectors, in); } diff --git a/arm7/src/my_sdmmc.h b/arm7/src/my_sdmmc.h index 144c576..1fbea51 100644 --- a/arm7/src/my_sdmmc.h +++ b/arm7/src/my_sdmmc.h @@ -12,7 +12,7 @@ #define REG_SDPORTSEL 0x02 #define REG_SDCMDARG 0x04 #define REG_SDCMDARG0 0x04 -#define REG_SDCMDARG1 0x06 +#define REG_SDCMDARG1 0x06 #define REG_SDSTOP 0x08 #define REG_SDRESP 0x0c #define REG_SDBLKCOUNT 0x0a @@ -98,8 +98,8 @@ #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) +#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) #define TMIO_MASK_READOP (TMIO_STAT1_RXRDY | TMIO_STAT1_DATAEND) #define TMIO_MASK_WRITEOP (TMIO_STAT1_TXRQ | TMIO_STAT1_DATAEND) @@ -107,25 +107,25 @@ typedef struct mmcdevice { u8* rData; const u8* tData; - u32 size; - u32 startOffset; - u32 endOffset; - u32 error; - u16 stat0; - u16 stat1; - u32 ret[4]; - u32 initarg; - u32 isSDHC; - u32 clk; - u32 SDOPT; - u32 devicenumber; - u32 total_size; //size in sectors of the device - u32 res; + u32 size; + u32 startOffset; + u32 endOffset; + u32 error; + u16 stat0; + u16 stat1; + u32 ret[4]; + u32 initarg; + u32 isSDHC; + u32 clk; + u32 SDOPT; + u32 devicenumber; + u32 total_size; //size in sectors of the device + u32 res; } mmcdevice; enum { - MMC_DEVICE_SDCARD, - MMC_DEVICE_NAND, + MMC_DEVICE_SDCARD, + MMC_DEVICE_NAND, }; void my_sdmmc_controller_init(bool force_init); @@ -136,12 +136,14 @@ int my_sdmmc_sdcard_init(); int my_sdmmc_nand_init(); void my_sdmmc_get_cid(int devicenumber, u32 *cid); -static inline void sdmmc_nand_cid( u32 *cid) { - my_sdmmc_get_cid(MMC_DEVICE_NAND, cid); +static inline void sdmmc_nand_cid( u32 *cid) +{ + my_sdmmc_get_cid(MMC_DEVICE_NAND, cid); } -static inline void sdmmc_sdcard_cid( u32 *cid) { - my_sdmmc_get_cid(MMC_DEVICE_SDCARD, cid); +static inline void sdmmc_sdcard_cid( u32 *cid) +{ + my_sdmmc_get_cid(MMC_DEVICE_SDCARD, cid); } int my_sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out); @@ -153,32 +155,37 @@ extern u32 sdmmc_cid[]; extern int sdmmc_curdevice; //--------------------------------------------------------------------------------- -static inline u16 sdmmc_read16(u16 reg) { +static inline u16 sdmmc_read16(u16 reg) //--------------------------------------------------------------------------------- +{ return *(vu16*)(SDMMC_BASE + reg); } //--------------------------------------------------------------------------------- -static inline void sdmmc_write16(u16 reg, u16 val) { +static inline void sdmmc_write16(u16 reg, u16 val) //--------------------------------------------------------------------------------- +{ *(vu16*)(SDMMC_BASE + reg) = val; } //--------------------------------------------------------------------------------- -static inline u32 sdmmc_read32(u16 reg) { +static inline u32 sdmmc_read32(u16 reg) //--------------------------------------------------------------------------------- - return *(vu32*)(SDMMC_BASE + reg); +{ + return *(vu32*)(SDMMC_BASE + reg); } //--------------------------------------------------------------------------------- -static inline void sdmmc_write32(u16 reg, u32 val) { +static inline void sdmmc_write32(u16 reg, u32 val) //--------------------------------------------------------------------------------- - *(vu32*)(SDMMC_BASE + reg) = val; +{ + *(vu32*)(SDMMC_BASE + reg) = val; } //--------------------------------------------------------------------------------- -static inline void sdmmc_mask16(u16 reg, u16 clear, u16 set) { +static inline void sdmmc_mask16(u16 reg, u16 clear, u16 set) //--------------------------------------------------------------------------------- +{ u16 val = sdmmc_read16(reg); val &= ~clear; val |= set; @@ -187,11 +194,12 @@ static inline void sdmmc_mask16(u16 reg, u16 clear, u16 set) { //--------------------------------------------------------------------------------- -static inline void setckl(u32 data) { +static inline void setckl(u32 data) //--------------------------------------------------------------------------------- - sdmmc_mask16(REG_SDCLKCTL, 0x100, 0); - sdmmc_mask16(REG_SDCLKCTL, 0x2FF, data & 0x2FF); - sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100); +{ + sdmmc_mask16(REG_SDCLKCTL, 0x100, 0); + sdmmc_mask16(REG_SDCLKCTL, 0x2FF, data & 0x2FF); + sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100); } #endif diff --git a/arm9/src/backupmenu.c b/arm9/src/backupmenu.c index c4afa8a..d968f5a 100644 --- a/arm9/src/backupmenu.c +++ b/arm9/src/backupmenu.c @@ -140,12 +140,12 @@ static void generateList(Menu* m) { if (count < m->page * ITEMS_PER_PAGE) count += 1; - + else { if (m->itemCount >= ITEMS_PER_PAGE) done = true; - + else { char* fpath = (char*)malloc(strlen(BACKUP_PATH) + strlen(ent->d_name) + 8); @@ -166,12 +166,12 @@ static void generateList(Menu* m) { if (count < m->page * ITEMS_PER_PAGE) count += 1; - + else { if (m->itemCount >= ITEMS_PER_PAGE) done = true; - + else { char* fpath = (char*)malloc(strlen(BACKUP_PATH) + strlen(ent->d_name) + 8); diff --git a/arm9/src/install.c b/arm9/src/install.c index 61506c9..ddce36c 100644 --- a/arm9/src/install.c +++ b/arm9/src/install.c @@ -34,7 +34,7 @@ static bool _patchGameCode(tDSiHeader* h) //set as standard app h->tid_high = 0x00030004; - + do { do { //generate a random game code @@ -42,7 +42,7 @@ static bool _patchGameCode(tDSiHeader* h) h->ndshdr.gameCode[i] = 'A' + (rand() % 26); } while (h->ndshdr.gameCode[0] == 'A'); //first letter shouldn't be A - + //correct title id h->tid_low = ( (h->ndshdr.gameCode[0] << 24) | (h->ndshdr.gameCode[1] << 16) | (h->ndshdr.gameCode[2] << 8) | h->ndshdr.gameCode[3] ); } @@ -63,8 +63,8 @@ static bool _iqueHack(tDSiHeader* h) if (h->ndshdr.reserved1[8] == 0x80) { - iprintf("iQue Hack..."); - + iprintf("iQue Hack..."); + h->ndshdr.reserved1[8] = 0x00; iprintf("\x1B[42m"); //green @@ -191,7 +191,7 @@ static void _createPublicSav(tDSiHeader* h, char* dataPath) fclose(f); free(publicPath); - } + } } } @@ -236,7 +236,7 @@ static void _createPrivateSav(tDSiHeader* h, char* dataPath) fclose(f); free(privatePath); - } + } } } @@ -280,7 +280,7 @@ static void _createBannerSav(tDSiHeader* h, char* dataPath) fclose(f); free(bannerPath); - } + } } } @@ -364,7 +364,7 @@ bool install(char* fpath, bool systemTitle) //start installation clearScreen(&bottomScreen); - tDSiHeader* h = getRomHeader(fpath); + tDSiHeader* h = getRomHeader(fpath); if (!h) { @@ -426,14 +426,14 @@ bool install(char* fpath, bool systemTitle) const char system[] = "\x1B[41mWARNING:\x1B[47m This is a system app,\ninstalling it is potentially\nmore risky than regular DSiWare.\n\x1B[33m"; const char areYouSure[] = "Are you sure you want to install\n"; char* msg = (char*)malloc(strlen(system) + strlen(areYouSure) + strlen(fpath) + 2); - if(sdnandMode || h->tid_high == 0x00030004) + if (sdnandMode || h->tid_high == 0x00030004) sprintf(msg, "%s%s?\n", areYouSure, fpath); else sprintf(msg, "%s%s%s?\n", system, areYouSure, fpath); bool choice = choiceBox(msg); free(msg); - + if (choice == NO) return false; } @@ -556,11 +556,11 @@ bool install(char* fpath, bool systemTitle) //create title directory /title/XXXXXXXX/XXXXXXXX char dirPath[32]; mkdir(sdnandMode ? "sd:/title" : "nand:/title", 0777); - + sprintf(dirPath, "%s:/title/%08x", sdnandMode ? "sd" : "nand", (unsigned int)h->tid_high); mkdir(dirPath, 0777); - sprintf(dirPath, "%s:/title/%08x/%08x", sdnandMode ? "sd" : "nand", (unsigned int)h->tid_high, (unsigned int)h->tid_low); + sprintf(dirPath, "%s:/title/%08x/%08x", sdnandMode ? "sd" : "nand", (unsigned int)h->tid_high, (unsigned int)h->tid_low); //check if title is free if (_titleIsUsed(h)) @@ -779,7 +779,7 @@ bool install(char* fpath, bool systemTitle) keyWait(KEY_A | KEY_B); goto complete; - } + } error: messagePrint("\x1B[31m\nInstallation failed.\n\x1B[47m"); diff --git a/arm9/src/installmenu.c b/arm9/src/installmenu.c index 8521494..380a12f 100644 --- a/arm9/src/installmenu.c +++ b/arm9/src/installmenu.c @@ -98,7 +98,7 @@ void installMenu() case INSTALL_MENU_INSTALL: install(m->items[m->cursor].value, false); break; - + case INSTALL_MENU_SYSTEM_TITLE: if (sdnandMode) install(m->items[m->cursor].value, true); @@ -114,7 +114,7 @@ void installMenu() } break; - case INSTALL_MENU_BACK: + case INSTALL_MENU_BACK: break; } } @@ -154,7 +154,7 @@ static void generateList(Menu* m) if (currentDir[0] == '\0') dir = opendir("sd:/"); else - dir = opendir(currentDir); + dir = opendir(currentDir); if (dir) { @@ -169,12 +169,12 @@ static void generateList(Menu* m) { if (count < m->page * ITEMS_PER_PAGE) count += 1; - + else { if (m->itemCount >= ITEMS_PER_PAGE) done = true; - + else { char* fpath = (char*)malloc(strlen(currentDir) + strlen(ent->d_name) + 8); @@ -195,12 +195,12 @@ static void generateList(Menu* m) { if (count < m->page * ITEMS_PER_PAGE) count += 1; - + else { if (m->itemCount >= ITEMS_PER_PAGE) done = true; - + else { char* fpath = (char*)malloc(strlen(currentDir) + strlen(ent->d_name) + 8); @@ -281,7 +281,7 @@ static int subMenu() static bool delete(Menu* m) { if (!m) return false; - + char* fpath = m->items[m->cursor].value; bool result = false; @@ -313,7 +313,7 @@ static bool delete(Menu* m) { messageBox("\x1B[31mCould not delete file.\x1B[47m"); } - } + } } return result; diff --git a/arm9/src/maketmd.c b/arm9/src/maketmd.c index b9b7e87..6467995 100644 --- a/arm9/src/maketmd.c +++ b/arm9/src/maketmd.c @@ -43,8 +43,8 @@ distribution. //#define TMD_CREATOR_VER "0.2" -#define TMD_SIZE 0x208 -#define SHA_BUFFER_SIZE 0x200 +#define TMD_SIZE 0x208 +#define SHA_BUFFER_SIZE 0x200 #define SHA_DIGEST_LENGTH 0x14 void tmd_create(uint8_t* tmd, FILE* app) @@ -75,7 +75,8 @@ void tmd_create(uint8_t* tmd, FILE* app) // Phase 4 - offset 0x1AA (fill-in 0x80 value, 0x10 times) { - for(size_t i = 0; i<0x10; i++) { + for (size_t i = 0; i<0x10; i++) + { tmd[0x1AA + i] = 0x80; } } @@ -123,7 +124,7 @@ void tmd_create(uint8_t* tmd, FILE* app) printProgressBar((float)fileread / (float)filesize); } - while(buffer_read == SHA_BUFFER_SIZE); + while (buffer_read == SHA_BUFFER_SIZE); clearProgressBar(); consoleSelect(&bottomScreen); @@ -140,7 +141,8 @@ int maketmd(char* input, char* tmdPath) iprintf("MakeTMD for DSiWare Homebrew\n"); iprintf("by Przemyslaw Skryjomski\n\t(Tuxality)\n"); - if(input == NULL || tmdPath == NULL) { + if (input == NULL || tmdPath == NULL) + { iprintf("\x1B[33m"); //yellow iprintf("\nUsage: %s file.app \n", "maketmd"); iprintf("\x1B[47m"); //white @@ -150,7 +152,8 @@ int maketmd(char* input, char* tmdPath) // APP file (input) FILE* app = fopen(input, "rb"); - if(!app) { + if (!app) + { iprintf("\x1B[31m"); //red iprintf("Error at opening %s for reading.\n", input); iprintf("\x1B[47m"); //white diff --git a/arm9/src/menu.c b/arm9/src/menu.c index cf7c548..1c6ec19 100644 --- a/arm9/src/menu.c +++ b/arm9/src/menu.c @@ -4,7 +4,7 @@ Menu* newMenu() { Menu* m = (Menu*)malloc(sizeof(Menu)); - + m->cursor = 0; m->page = 0; m->itemCount = 0; @@ -27,7 +27,7 @@ void freeMenu(Menu* m) if (!m) return; clearMenu(m); - + free(m); m = NULL; } @@ -52,7 +52,7 @@ void addMenuItem(Menu* m, char const* label, char const* value, bool directory) m->items[i].value = (char*)malloc(strlen(value)+1); sprintf(m->items[i].value, "%s", value); } - + m->itemCount += 1; } @@ -77,7 +77,7 @@ void sortMenuItems(Menu* m) void setMenuHeader(Menu* m, char* str) { if (!m) return; - + if (!str) { m->header[0] = '\0'; @@ -153,7 +153,7 @@ void printMenu(Menu* m) iprintf(" \n"); } - //cursor + //cursor iprintf("\x1b[%d;0H>", 2 + m->cursor); //scroll arrows @@ -193,7 +193,7 @@ static void _moveCursor(Menu* m, int dir) { m->cursor = m->itemCount-1; } - } + } } bool moveCursor(Menu* m) diff --git a/arm9/src/message.c b/arm9/src/message.c index 34a14a0..83fec38 100644 --- a/arm9/src/message.c +++ b/arm9/src/message.c @@ -14,7 +14,7 @@ void keyWait(u32 key) } bool choiceBox(char* message) -{ +{ const int choiceRow = 10; int cursor = 0; diff --git a/arm9/src/nand/crypto.c b/arm9/src/nand/crypto.c index 173cd46..c774c4e 100644 --- a/arm9/src/nand/crypto.c +++ b/arm9/src/nand/crypto.c @@ -9,9 +9,9 @@ // https://github.com/Jimmy-Z/bfCL/blob/master/dsi.h // ported back to 32 bit for ARM9 -static dsi_context nand_ctx ; -static dsi_context boot2_ctx ; -static dsi_es_context es_ctx ; +static dsi_context nand_ctx; +static dsi_context boot2_ctx; +static dsi_es_context es_ctx; static uint8_t nand_ctr_iv[16]; static uint8_t boot2_ctr[16]; @@ -19,51 +19,51 @@ static uint8_t boot2_ctr[16]; static void generate_key(uint8_t *generated_key, const uint32_t *console_id, const key_mode_t mode) { uint32_t key[4]; - switch (mode) - { - case NAND: - key[0] = console_id[0]; - key[1] = console_id[0] ^ KEYSEED_DSI_NAND_0; - key[2] = console_id[1] ^ KEYSEED_DSI_NAND_1; - key[3] = console_id[1]; - break; - case NAND_3DS: - key[0] = (console_id[0] ^ KEYSEED_3DS_NAND_0) | 0x80000000; - key[1] = KEYSEED_3DS_NAND_1; - key[2] = KEYSEED_3DS_NAND_2; - key[3] = console_id[1] ^ KEYSEED_3DS_NAND_3; - break; - case ES: - key[0] = KEYSEED_ES_0; - key[1] = KEYSEED_ES_1; - key[2] = console_id[1] ^ KEYSEED_ES_2; - key[3] = console_id[0]; - break; - default: - break; + switch (mode) + { + case NAND: + key[0] = console_id[0]; + key[1] = console_id[0] ^ KEYSEED_DSI_NAND_0; + key[2] = console_id[1] ^ KEYSEED_DSI_NAND_1; + key[3] = console_id[1]; + break; + case NAND_3DS: + key[0] = (console_id[0] ^ KEYSEED_3DS_NAND_0) | 0x80000000; + key[1] = KEYSEED_3DS_NAND_1; + key[2] = KEYSEED_3DS_NAND_2; + key[3] = console_id[1] ^ KEYSEED_3DS_NAND_3; + break; + case ES: + key[0] = KEYSEED_ES_0; + key[1] = KEYSEED_ES_1; + key[2] = console_id[1] ^ KEYSEED_ES_2; + key[3] = console_id[0]; + break; + default: + break; } u128_xor((uint8_t *)key, mode == ES ? DSi_ES_KEY_Y : DSi_NAND_KEY_Y); u128_add((uint8_t *)key, DSi_KEY_MAGIC); - u128_lrot((uint8_t *)key, 42) ; - memcpy(generated_key, key, 16) ; + u128_lrot((uint8_t *)key, 42); + memcpy(generated_key, key, 16); } -int dsi_sha1_verify(const void *digest_verify, const void *data, unsigned len) +int dsi_sha1_verify(const void *digest_verify, const void *data, unsigned len) { uint8_t digest[SHA1_LEN]; swiSHA1Calc(digest, data, len); return memcmp(digest, digest_verify, SHA1_LEN); } -void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int is3DS) -{ +void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int is3DS) +{ uint32_t console_id[2]; GET_UINT32_BE(console_id[0], console_id_be, 4); GET_UINT32_BE(console_id[1], console_id_be, 0); uint8_t key[16]; generate_key(key, console_id, is3DS ? NAND_3DS : NAND); - dsi_set_key(&nand_ctx, key) ; + dsi_set_key(&nand_ctx, key); u32 normalkey[4]; u32 tadsrl_keyX[4] = {0x4E00004A, 0x4A00004E, 0, 0}; @@ -72,7 +72,7 @@ void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int i F_XY((u8 *)normalkey, (u8 *)tadsrl_keyX, DSi_ES_KEY_Y); dsi_es_init(&es_ctx, (u8*)normalkey); - dsi_set_key(&boot2_ctx, DSi_BOOT2_KEY) ; + dsi_set_key(&boot2_ctx, DSi_BOOT2_KEY); swiSHA1Calc(nand_ctr_iv, emmc_cid, 16); @@ -80,33 +80,33 @@ void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int i // crypt one block, in/out must be aligned to 32 bit(restriction induced by xor_128) // offset as block offset, block as AES block -void dsi_nand_crypt_1(uint8_t* out, const uint8_t* in, uint32_t offset) +void dsi_nand_crypt_1(uint8_t* out, const uint8_t* in, uint32_t offset) { - uint8_t ctr[16] ; - memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)) ; + uint8_t ctr[16]; + memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)); u128_add32(ctr, offset); - dsi_set_ctr(&nand_ctx, ctr) ; - dsi_crypt_ctr(&nand_ctx, in, out, 16) ; + dsi_set_ctr(&nand_ctx, ctr); + dsi_crypt_ctr(&nand_ctx, in, out, 16); } -void dsi_nand_crypt(uint8_t* out, const uint8_t* in, uint32_t offset, unsigned count) +void dsi_nand_crypt(uint8_t* out, const uint8_t* in, uint32_t offset, unsigned count) { - uint8_t ctr[16] ; - memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)) ; + uint8_t ctr[16]; + memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)); u128_add32(ctr, offset); - for (unsigned i = 0; i < count; ++i) - { - dsi_set_ctr(&nand_ctx, ctr) ; - dsi_crypt_ctr(&nand_ctx, in, out, 16) ; + for (unsigned i = 0; i < count; ++i) + { + dsi_set_ctr(&nand_ctx, ctr); + dsi_crypt_ctr(&nand_ctx, in, out, 16); out += AES_BLOCK_SIZE; in += AES_BLOCK_SIZE; - u128_add32(ctr, 1); + u128_add32(ctr, 1); } } int dsi_es_block_crypt(uint8_t *buf, unsigned buf_len, crypt_mode_t mode) { - if(mode == DECRYPT) + if (mode == DECRYPT) return dsi_es_decrypt(&es_ctx, buf, buf + buf_len - 0x20, buf_len - 0x20); else dsi_es_encrypt(&es_ctx, buf, buf + buf_len - 0x20, buf_len - 0x20); @@ -114,25 +114,25 @@ int dsi_es_block_crypt(uint8_t *buf, unsigned buf_len, crypt_mode_t mode) return 0; } -void dsi_boot2_crypt_set_ctr(uint32_t size_r) +void dsi_boot2_crypt_set_ctr(uint32_t size_r) { - for (int i=0;i<4;i++) - { - boot2_ctr[i] = (size_r) >> (8*i) ; - boot2_ctr[i+4] = (-size_r) >> (8*i) ; - boot2_ctr[i+8] = (~size_r) >> (8*i) ; - boot2_ctr[i+12] = 0 ; - } -} - -void dsi_boot2_crypt(uint8_t* out, const uint8_t* in, unsigned count) -{ - for (unsigned i = 0; i < count; ++i) - { - dsi_set_ctr(&boot2_ctx, boot2_ctr) ; - dsi_crypt_ctr(&boot2_ctx, in, out, 16) ; - out += AES_BLOCK_SIZE; - in += AES_BLOCK_SIZE; - u128_add32(boot2_ctr, 1); + for (int i=0;i<4;i++) + { + boot2_ctr[i] = (size_r) >> (8*i); + boot2_ctr[i+4] = (-size_r) >> (8*i); + boot2_ctr[i+8] = (~size_r) >> (8*i); + boot2_ctr[i+12] = 0; + } +} + +void dsi_boot2_crypt(uint8_t* out, const uint8_t* in, unsigned count) +{ + for (unsigned i = 0; i < count; ++i) + { + dsi_set_ctr(&boot2_ctx, boot2_ctr); + dsi_crypt_ctr(&boot2_ctx, in, out, 16); + out += AES_BLOCK_SIZE; + in += AES_BLOCK_SIZE; + u128_add32(boot2_ctr, 1); } } diff --git a/arm9/src/nand/crypto.h b/arm9/src/nand/crypto.h index aafd9e2..55c0d71 100644 --- a/arm9/src/nand/crypto.h +++ b/arm9/src/nand/crypto.h @@ -28,7 +28,7 @@ extern "C" { ((uint8_t*)&(n))[1] = (b)[i + 2]; \ ((uint8_t*)&(n))[2] = (b)[i + 1]; \ ((uint8_t*)&(n))[3] = (b)[i + 0] - + #define PUT_UINT32_BE(n, b, i) \ (b)[i + 0] = ((uint8_t*)&(n))[3]; \ (b)[i + 1] = ((uint8_t*)&(n))[2]; \ diff --git a/arm9/src/nand/f_xy.c b/arm9/src/nand/f_xy.c index e07c796..5ff606c 100644 --- a/arm9/src/nand/f_xy.c +++ b/arm9/src/nand/f_xy.c @@ -5,7 +5,7 @@ * * If you happen to know whom to credit I'd love to add the name * - * Refactored to reduce the pointer casts and remove the dependency + * Refactored to reduce the pointer casts and remove the dependency * from tonccpy. */ @@ -16,14 +16,22 @@ /************************ Constants / Defines *********************************/ -const uint8_t DSi_KEY_MAGIC[16] = { 0x79, 0x3e, 0x4f, 0x1a, 0x5f, 0x0f, 0x68, 0x2a, - 0x58, 0x02, 0x59, 0x29, 0x4e, 0xfb, 0xfe, 0xff }; -const uint8_t DSi_NAND_KEY_Y[16] = { 0x76, 0xdc, 0xb9, 0x0a, 0xd3, 0xc4, 0x4d, 0xbd, - 0x1d, 0xdd, 0x2d, 0x20, 0x05, 0x00, 0xa0, 0xe1 }; -const uint8_t DSi_ES_KEY_Y[16] = { 0xe5, 0xcc, 0x5a, 0x8b, 0x56, 0xd0, 0xc9,0x72, - 0x9c, 0x17, 0xe8, 0xdc, 0x39, 0x12, 0x36, 0xa9 }; -const uint8_t DSi_BOOT2_KEY[16] = { 0x98, 0xee, 0x80, 0x80, 0x00, 0x6c, 0xb4, 0xf6, - 0x3a, 0xc2, 0x6e, 0x62, 0xf9, 0xec, 0x34, 0xad }; +const uint8_t DSi_KEY_MAGIC[16] = { + 0x79, 0x3e, 0x4f, 0x1a, 0x5f, 0x0f, 0x68, 0x2a, + 0x58, 0x02, 0x59, 0x29, 0x4e, 0xfb, 0xfe, 0xff +}; +const uint8_t DSi_NAND_KEY_Y[16] = { + 0x76, 0xdc, 0xb9, 0x0a, 0xd3, 0xc4, 0x4d, 0xbd, + 0x1d, 0xdd, 0x2d, 0x20, 0x05, 0x00, 0xa0, 0xe1 +}; +const uint8_t DSi_ES_KEY_Y[16] = { + 0xe5, 0xcc, 0x5a, 0x8b, 0x56, 0xd0, 0xc9,0x72, + 0x9c, 0x17, 0xe8, 0xdc, 0x39, 0x12, 0x36, 0xa9 +}; +const uint8_t DSi_BOOT2_KEY[16] = { + 0x98, 0xee, 0x80, 0x80, 0x00, 0x6c, 0xb4, 0xf6, + 0x3a, 0xc2, 0x6e, 0x62, 0xf9, 0xec, 0x34, 0xad +}; /************************ Functions *******************************************/ @@ -31,10 +39,10 @@ void F_XY(uint8_t *key, const uint8_t *key_x, const uint8_t *key_y) { uint8_t key_xy[16]; - for(int i=0; i<16; i++) - key_xy[i] = key_x[i] ^ key_y[i]; + for (int i=0; i<16; i++) + key_xy[i] = key_x[i] ^ key_y[i]; - memcpy(key, DSi_KEY_MAGIC, sizeof(DSi_KEY_MAGIC)); + memcpy(key, DSi_KEY_MAGIC, sizeof(DSi_KEY_MAGIC)); u128_add(key, key_xy); u128_lrot(key, 42); @@ -47,4 +55,3 @@ void F_XY_reverse(const uint8_t *key, uint8_t *key_xy) u128_rrot(key_xy, 42); u128_sub(key_xy, DSi_KEY_MAGIC); } - diff --git a/arm9/src/nand/f_xy.h b/arm9/src/nand/f_xy.h index 383868e..a25661e 100644 --- a/arm9/src/nand/f_xy.h +++ b/arm9/src/nand/f_xy.h @@ -5,10 +5,10 @@ * * If you happen to know whom to credit I'd love to add the name * - * Refactored to reduce the pointer casts and remove the dependency + * Refactored to reduce the pointer casts and remove the dependency * from tonccpy. */ - + #ifndef _H_F_XY #define _H_F_XY @@ -18,10 +18,10 @@ extern "C" { /************************ Constants / Defines *********************************/ -extern const uint8_t DSi_KEY_MAGIC[16] ; -extern const uint8_t DSi_NAND_KEY_Y[16] ; -extern const uint8_t DSi_ES_KEY_Y[16] ; -extern const uint8_t DSi_BOOT2_KEY[16] ; +extern const uint8_t DSi_KEY_MAGIC[16]; +extern const uint8_t DSi_NAND_KEY_Y[16]; +extern const uint8_t DSi_ES_KEY_Y[16]; +extern const uint8_t DSi_BOOT2_KEY[16]; /************************ Function Protoypes **********************************/ @@ -33,4 +33,3 @@ void F_XY_reverse(const uint8_t *key, uint8_t *key_xy); #endif #endif - diff --git a/arm9/src/nand/nandio.c b/arm9/src/nand/nandio.c index a9d997a..8a9f2a0 100644 --- a/arm9/src/nand/nandio.c +++ b/arm9/src/nand/nandio.c @@ -12,12 +12,12 @@ /************************ Function Protoypes **********************************/ -bool nandio_startup() ; -bool nandio_is_inserted() ; -bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer) ; -bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer) ; -bool nandio_clear_status() ; -bool nandio_shutdown() ; +bool nandio_startup(); +bool nandio_is_inserted(); +bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer); +bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer); +bool nandio_clear_status(); +bool nandio_shutdown(); /************************ Constants / Defines *********************************/ @@ -46,7 +46,7 @@ static u32 fat_sig_fix_offset = 0; static u32 sector_buf32[SECTOR_SIZE/sizeof(u32)]; static u8 *sector_buf = (u8*)sector_buf32; -void nandio_set_fat_sig_fix(u32 offset) +void nandio_set_fat_sig_fix(u32 offset) { fat_sig_fix_offset = offset; } @@ -56,23 +56,23 @@ void getConsoleID(u8 *consoleID) u8 *fifo=(u8*)0x02300000; //shared mem address that has our computed key3 stuff u8 key[16]; //key3 normalkey - keyslot 3 is used for DSi/twln NAND crypto u8 key_x[16];////key3_x - contains a DSi console id (which just happens to be the LFCS on 3ds) - + memcpy(key, fifo, 16); //receive the goods from arm7 F_XY_reverse(key, key_x); //work backwards from the normalkey to get key_x that has the consoleID - u128_xor(key_x, DSi_NAND_KEY_Y) ; + u128_xor(key_x, DSi_NAND_KEY_Y); - memcpy(&consoleID[0], &key_x[0], 4); + memcpy(&consoleID[0], &key_x[0], 4); memcpy(&consoleID[4], &key_x[0xC], 4); } -bool nandio_startup() +bool nandio_startup() { - if (!nand_Startup()) - { - return false; - } + if (!nand_Startup()) + { + return false; + } nand_ReadSectors(0, 1, sector_buf); is3DS = parse_ncsd(sector_buf) == 0; @@ -83,8 +83,8 @@ bool nandio_startup() // Get ConsoleID getConsoleID(consoleID); - for (int i = 0; i < 8; i++) - { + for (int i = 0; i < 8; i++) + { consoleIDfixed[i] = consoleID[7-i]; } // iprintf("sector 0 is %s\n", is3DS ? "3DS" : "DSi"); @@ -94,27 +94,27 @@ bool nandio_startup() parse_mbr(sector_buf, is3DS); mbr_t *mbr = (mbr_t*)sector_buf; - + nandio_set_fat_sig_fix(is3DS ? 0 : mbr->partitions[0].offset); - if (crypt_buf == 0) - { + if (crypt_buf == 0) + { crypt_buf = (u8*)memalign(32, SECTOR_SIZE * CRYPT_BUF_LEN); } - + return crypt_buf != 0; } -bool nandio_is_inserted() +bool nandio_is_inserted() { return true; } // len is guaranteed <= CRYPT_BUF_LEN -static bool read_sectors(sec_t start, sec_t len, void *buffer) +static bool read_sectors(sec_t start, sec_t len, void *buffer) { - if (nand_ReadSectors(start, len, crypt_buf)) - { + if (nand_ReadSectors(start, len, crypt_buf)) + { dsi_nand_crypt(buffer, crypt_buf, start * SECTOR_SIZE / AES_BLOCK_SIZE, len * SECTOR_SIZE / AES_BLOCK_SIZE); if (fat_sig_fix_offset && start == fat_sig_fix_offset @@ -127,93 +127,97 @@ static bool read_sectors(sec_t start, sec_t len, void *buffer) ((u8*)buffer)[0x38] = 'T'; } return true; - } else { + } + else + { return false; } } // len is guaranteed <= CRYPT_BUF_LEN -static bool write_sectors(sec_t start, sec_t len, const void *buffer) +static bool write_sectors(sec_t start, sec_t len, const void *buffer) { - static u8 writeCopy[SECTOR_SIZE*16] ; - memcpy(writeCopy, buffer, len * SECTOR_SIZE) ; - - dsi_nand_crypt(crypt_buf, writeCopy, start * SECTOR_SIZE / AES_BLOCK_SIZE, len * SECTOR_SIZE / AES_BLOCK_SIZE); - if (nand_WriteSectors(start, len, crypt_buf)) - { + static u8 writeCopy[SECTOR_SIZE*16]; + memcpy(writeCopy, buffer, len * SECTOR_SIZE); + + dsi_nand_crypt(crypt_buf, writeCopy, start * SECTOR_SIZE / AES_BLOCK_SIZE, len * SECTOR_SIZE / AES_BLOCK_SIZE); + if (nand_WriteSectors(start, len, crypt_buf)) + { return true; - } else { + } + else + { return false; } } -bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer) +bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer) { - while (len >= CRYPT_BUF_LEN) - { - if (!read_sectors(offset, CRYPT_BUF_LEN, buffer)) - { + while (len >= CRYPT_BUF_LEN) + { + if (!read_sectors(offset, CRYPT_BUF_LEN, buffer)) + { return false; } offset += CRYPT_BUF_LEN; len -= CRYPT_BUF_LEN; buffer = ((u8*)buffer) + SECTOR_SIZE * CRYPT_BUF_LEN; } - if (len > 0) - { + if (len > 0) + { return read_sectors(offset, len, buffer); - } else - { + } else + { return true; } } -bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer) +bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer) { if (writingLocked) return false; nandWritten = true; - while (len >= CRYPT_BUF_LEN) - { - if (!write_sectors(offset, CRYPT_BUF_LEN, buffer)) - { + while (len >= CRYPT_BUF_LEN) + { + if (!write_sectors(offset, CRYPT_BUF_LEN, buffer)) + { return false; } offset += CRYPT_BUF_LEN; len -= CRYPT_BUF_LEN; buffer = ((u8*)buffer) + SECTOR_SIZE * CRYPT_BUF_LEN; } - if (len > 0) - { + if (len > 0) + { return write_sectors(offset, len, buffer); - } else - { + } else + { return true; } } -bool nandio_clear_status() +bool nandio_clear_status() { return true; } -bool nandio_shutdown() +bool nandio_shutdown() { if (nandWritten) { // at cleanup we synchronize the FAT statgings - // A FatFS might have multiple copies of the FAT. + // A FatFS might have multiple copies of the FAT. // we will get them back synchonized as we just worked on the first copy // this allows us to revert changes in the FAT if we did not properly finish // and did not push the changes to the other copies // to do this we read the first partition sector - nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf) ; - u8 stagingLevels = sector_buf[0x10] ; - u8 reservedSectors = sector_buf[0x0E] ; - u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8) ; + nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf); + u8 stagingLevels = sector_buf[0x10]; + u8 reservedSectors = sector_buf[0x0E]; + u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8); /* iprintf("[i] Staging for %i FAT copies\n",stagingLevels); iprintf("[i] Stages starting at %i\n",reservedSectors); @@ -224,12 +228,12 @@ bool nandio_shutdown() for (u32 sector = 0;sector < sectorsPerFatCopy; sector++) { // read fat sector - nandio_read_sectors(fat_sig_fix_offset + reservedSectors + sector, 1, sector_buf) ; + nandio_read_sectors(fat_sig_fix_offset + reservedSectors + sector, 1, sector_buf); // write to each copy, except the source copy writingLocked = false; for (int stage = 1;stage < stagingLevels;stage++) { - nandio_write_sectors(fat_sig_fix_offset + reservedSectors + sector + (stage *sectorsPerFatCopy), 1, sector_buf) ; + nandio_write_sectors(fat_sig_fix_offset + reservedSectors + sector + (stage *sectorsPerFatCopy), 1, sector_buf); } writingLocked = true; } diff --git a/arm9/src/nand/nandio.h b/arm9/src/nand/nandio.h index bc11f0c..95424c6 100644 --- a/arm9/src/nand/nandio.h +++ b/arm9/src/nand/nandio.h @@ -18,13 +18,13 @@ extern const DISC_INTERFACE io_dsi_nand; void nandio_set_fat_sig_fix(uint32_t offset); -void getConsoleID(uint8_t *consoleID) ; +void getConsoleID(uint8_t *consoleID); -extern bool nandio_shutdown() ; +extern bool nandio_shutdown(); -extern bool nandio_lock_writing() ; -extern bool nandio_unlock_writing() ; -extern bool nandio_force_fat_fix() ; +extern bool nandio_lock_writing(); +extern bool nandio_unlock_writing(); +extern bool nandio_force_fat_fix(); #ifdef __cplusplus } diff --git a/arm9/src/nand/polarssl/aes.c b/arm9/src/nand/polarssl/aes.c index da37339..8e80a9b 100644 --- a/arm9/src/nand/polarssl/aes.c +++ b/arm9/src/nand/polarssl/aes.c @@ -332,10 +332,10 @@ static const unsigned long RCON[10] = * Forward S-box & tables */ static unsigned char FSb[256]; -static unsigned long FT0[256]; -static unsigned long FT1[256]; -static unsigned long FT2[256]; -static unsigned long FT3[256]; +static unsigned long FT0[256]; +static unsigned long FT1[256]; +static unsigned long FT2[256]; +static unsigned long FT3[256]; /* * Reverse S-box & tables @@ -774,7 +774,7 @@ int aes_crypt_cbc( aes_context *ctx, { if( padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 ) return( 0 ); - + // If padlock data misaligned, we just fall back to // unaccelerated mode // diff --git a/arm9/src/nand/sector0.c b/arm9/src/nand/sector0.c index 12d1b9f..0e2c147 100644 --- a/arm9/src/nand/sector0.c +++ b/arm9/src/nand/sector0.c @@ -19,17 +19,17 @@ /************************ Constants / Defines *********************************/ static const mbr_partition_t ptable_DSi[MBR_PARTITIONS] = { - {0u, {3u, 24u, 4u}, 6u, {15u, 224u, 59u}, 0x00000877u, 0x00066f89u}, - {0u, {2u, 206u, 60u}, 6u, {15u, 224u, 190u}, 0x0006784u, 0x000105b3u}, - {0u, {2u, 222u, 191u}, 1u, {15u, 224u, 191u}, 0x00077e5u, 0x000001a3u}, - {0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u} + {0u, {3u, 24u, 4u}, 6u, {15u, 224u, 59u}, 0x00000877u, 0x00066f89u}, + {0u, {2u, 206u, 60u}, 6u, {15u, 224u, 190u}, 0x0006784u, 0x000105b3u}, + {0u, {2u, 222u, 191u}, 1u, {15u, 224u, 191u}, 0x00077e5u, 0x000001a3u}, + {0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u} }; static const mbr_partition_t ptable_3DS[MBR_PARTITIONS] = { - {0u, {4u, 24u, 0u}, 6u, {1u, 160u, 63u}, 0x0000009u, 0x00047da9u}, - {0u, {4u, 142u, 64u}, 6u, {1u, 160u, 195u}, 0x0004808u, 0x000105b3u}, - {0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u}, - {0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u} + {0u, {4u, 24u, 0u}, 6u, {1u, 160u, 63u}, 0x0000009u, 0x00047da9u}, + {0u, {4u, 142u, 64u}, 6u, {1u, 160u, 195u}, 0x0004808u, 0x000105b3u}, + {0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u}, + {0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u} }; /************************ Functions *******************************************/ @@ -40,37 +40,38 @@ static const mbr_partition_t ptable_3DS[MBR_PARTITIONS] = { * - the signature magic * - the partition types * to ensure a valid 3DS ncsd is present - * + * * Return values: * 0: NCSD is a valid * -1: the signature/magic is invalid * -2: at least one unknown partition type was found */ -int parse_ncsd(const uint8_t sector0[SECTOR_SIZE]) +int parse_ncsd(const uint8_t sector0[SECTOR_SIZE]) { - const ncsd_header_t * h = (ncsd_header_t *)sector0; - if (NCSD_MAGIC != h->magic) - { - return -1; - } + const ncsd_header_t * h = (ncsd_header_t *)sector0; + if (NCSD_MAGIC != h->magic) + { + return -1; + } - for (unsigned i = 0; i < NCSD_PARTITIONS; ++i) - { - unsigned fs_type = h->fs_types[i]; - if (fs_type == 0) - { - break; - } - switch (fs_type) { - case 1: - case 3: - case 4: - break; - default: - return -2; - } - } - return 0; + for (unsigned i = 0; i < NCSD_PARTITIONS; ++i) + { + unsigned fs_type = h->fs_types[i]; + if (fs_type == 0) + { + break; + } + switch (fs_type) + { + case 1: + case 3: + case 4: + break; + default: + return -2; + } + } + return 0; } /*! \brief Sanity check of the MBR @@ -79,28 +80,28 @@ int parse_ncsd(const uint8_t sector0[SECTOR_SIZE]) * - the signature * - the partition0 values * to ensure a valid DSi main partition can be found - * + * * Return values: * 0: MBR is a valid DSi partition * -1: the signature is invalid * -2: the first partition does not match expected values */ -int parse_mbr(const uint8_t sector0[SECTOR_SIZE], const int is3DS) +int parse_mbr(const uint8_t sector0[SECTOR_SIZE], const int is3DS) { - const mbr_t *m = (mbr_t*)sector0; - const mbr_partition_t *ref_ptable; // reference partition table + const mbr_t *m = (mbr_t*)sector0; + const mbr_partition_t *ref_ptable; // reference partition table - if ((MBR_SIGNATURE_0 != m->boot_signature[0]) || (MBR_SIGNATURE_1 != m->boot_signature[1])) - { - // if the signature is invalid, the bootsector shall not be used! - return -1; - } - ref_ptable = is3DS?ptable_3DS:ptable_DSi; - // only test the 1st partition now, we've seen variations on the 3rd partition - // and after all we only care about the 1st partition - if (memcmp(ref_ptable, m->partitions, sizeof(mbr_partition_t))) - { - return -2; - } - return 0; + if ((MBR_SIGNATURE_0 != m->boot_signature[0]) || (MBR_SIGNATURE_1 != m->boot_signature[1])) + { + // if the signature is invalid, the bootsector shall not be used! + return -1; + } + ref_ptable = is3DS?ptable_3DS:ptable_DSi; + // only test the 1st partition now, we've seen variations on the 3rd partition + // and after all we only care about the 1st partition + if (memcmp(ref_ptable, m->partitions, sizeof(mbr_partition_t))) + { + return -2; + } + return 0; } diff --git a/arm9/src/nand/sector0.h b/arm9/src/nand/sector0.h index e4ed0a6..783cbae 100644 --- a/arm9/src/nand/sector0.h +++ b/arm9/src/nand/sector0.h @@ -35,29 +35,26 @@ extern "C" { /************************ Structures / Datatypes ******************************/ #ifdef _MSC_VER - #pragma pack(push, 1) - #define __PACKED + #pragma pack(push, 1) + #define __PACKED #elif defined __GNUC__ - #define __PACKED __attribute__ ((__packed__)) + #define __PACKED __attribute__ ((__packed__)) #endif typedef struct { - uint32_t offset, - length; + uint32_t offset, length; } __PACKED ncsd_partition_t; typedef struct { - uint8_t digest[NCSD_SIGNATURESIZE] ; -} __PACKED ncsd_sginature ; + uint8_t digest[NCSD_SIGNATURESIZE]; +} __PACKED ncsd_sginature; typedef struct { - ncsd_sginature signature; - uint32_t magic, - size; - uint64_t media_id; - uint8_t fs_types[NCSD_PARTITIONS], - crypt_types[NCSD_PARTITIONS]; - ncsd_partition_t partitions[NCSD_PARTITIONS]; + ncsd_sginature signature; + uint32_t magic, size; + uint64_t media_id; + uint8_t fs_types[NCSD_PARTITIONS], crypt_types[NCSD_PARTITIONS]; + ncsd_partition_t partitions[NCSD_PARTITIONS]; } __PACKED ncsd_header_t; /* @@ -73,11 +70,11 @@ typedef struct { * | | | Bit 14..23: Cylinder | * ---------------------------------------------------------- */ - + typedef struct { - uint8_t head; - uint8_t sectorAndCylHigh; - uint8_t cylinderLow; + uint8_t head; + uint8_t sectorAndCylHigh; + uint8_t cylinderLow; } __PACKED chs_t; /* @@ -110,12 +107,12 @@ typedef struct { */ typedef struct { - uint8_t status; - chs_t chs_first; - uint8_t type; - chs_t chs_last; - uint32_t offset; - uint32_t length; + uint8_t status; + chs_t chs_first; + uint8_t type; + chs_t chs_last; + uint32_t offset; + uint32_t length; } __PACKED mbr_partition_t; /* @@ -140,13 +137,13 @@ typedef struct { */ typedef struct { - uint8_t bootstrap[MBR_BOOTSTRAP_SIZE]; - mbr_partition_t partitions[MBR_PARTITIONS]; - uint8_t boot_signature[2]; + uint8_t bootstrap[MBR_BOOTSTRAP_SIZE]; + mbr_partition_t partitions[MBR_PARTITIONS]; + uint8_t boot_signature[2]; } __PACKED mbr_t; #ifdef _MSC_VER - #pragma pack(pop) + #pragma pack(pop) #endif #undef __PACKED @@ -159,11 +156,9 @@ int parse_mbr(const uint8_t sector0[SECTOR_SIZE], const int is3DS); /************************ static code verification ****************************/ -static_assert(sizeof(ncsd_header_t) == NCSD_HEADERSIZE, - "sizeof(ncsd_header_t) should equal 0x160"); -static_assert(sizeof(mbr_t) == SECTOR_SIZE, - "sizeof(mbr_t) should equal 0x200"); +static_assert(sizeof(ncsd_header_t) == NCSD_HEADERSIZE, "sizeof(ncsd_header_t) should equal 0x160"); +static_assert(sizeof(mbr_t) == SECTOR_SIZE, "sizeof(mbr_t) should equal 0x200"); #ifdef __cplusplus } -#endif \ No newline at end of file +#endif diff --git a/arm9/src/nand/twltool/dsi.c b/arm9/src/nand/twltool/dsi.c index 1b81e81..1f1091c 100644 --- a/arm9/src/nand/twltool/dsi.c +++ b/arm9/src/nand/twltool/dsi.c @@ -5,80 +5,70 @@ #include #include "u128_math.h" -void dsi_set_key( dsi_context* ctx, - const unsigned char key[16] ) +void dsi_set_key(dsi_context* ctx, const unsigned char key[16]) { unsigned char keyswap[16]; - u128_swap(keyswap, key) ; + u128_swap(keyswap, key); aes_setkey_enc(&ctx->aes, keyswap, 128); } -void dsi_add_ctr( dsi_context* ctx, - unsigned int carry) -{ - unsigned int counter[4]; - unsigned char *outctr = (unsigned char*)ctx->ctr; - int sum; - signed int i; +void dsi_add_ctr(dsi_context* ctx, unsigned int carry) +{ + unsigned int counter[4]; + unsigned char *outctr = (unsigned char*)ctx->ctr; + int sum; + signed int i; - for(i = 0; i < 4; i++) - counter[i] = (outctr[i * 4 + 0] << 24) | (outctr[i * 4 + 1] << 16) | - (outctr[i * 4 + 2] << 8) | (outctr[i * 4 + 3] << 0); + for (i = 0; i < 4; i++) + counter[i] = (outctr[i * 4 + 0] << 24) | (outctr[i * 4 + 1] << 16) | + (outctr[i * 4 + 2] << 8) | (outctr[i * 4 + 3] << 0); - for(i = 3; i >= 0; i--) - { - sum = counter[i] + carry; + for (i = 3; i >= 0; i--) + { + sum = counter[i] + carry; - if (sum < counter[i]) - carry = 1; - else - carry = 0; + if (sum < counter[i]) + carry = 1; + else + carry = 0; - counter[i] = sum; - } + counter[i] = sum; + } - for(i = 0; i < 4; i++) - { - outctr[i * 4 + 0] = counter[i] >> 24; - outctr[i * 4 + 1] = counter[i] >> 16; - outctr[i * 4 + 2] = counter[i] >> 8; - outctr[i * 4 + 3] = counter[i] >> 0; - } + for (i = 0; i < 4; i++) + { + outctr[i * 4 + 0] = counter[i] >> 24; + outctr[i * 4 + 1] = counter[i] >> 16; + outctr[i * 4 + 2] = counter[i] >> 8; + outctr[i * 4 + 3] = counter[i] >> 0; + } } - -void dsi_set_ctr( dsi_context* ctx, - const unsigned char ctr[16] ) + +void dsi_set_ctr(dsi_context* ctx, const unsigned char ctr[16]) { int i; - for(i=0; i<16; i++) + for (i=0; i<16; i++) ctx->ctr[i] = ctr[15-i]; } -void dsi_init_ctr( dsi_context* ctx, - const unsigned char key[16], - const unsigned char ctr[12] ) +void dsi_init_ctr(dsi_context* ctx, const unsigned char key[16], const unsigned char ctr[12]) { dsi_set_key(ctx, key); dsi_set_ctr(ctx, ctr); } -void dsi_crypt_ctr( dsi_context* ctx, - const void* in, - void* out, - unsigned int len) +void dsi_crypt_ctr(dsi_context* ctx, const void* in, void* out, unsigned int len) { - unsigned int i; - for(i = 0; i < len; i += 0x10) + unsigned int i; + for (i = 0; i < len; i += 0x10) { - dsi_crypt_ctr_block(ctx, in+i, out+i); + dsi_crypt_ctr_block(ctx, in+i, out+i); } } -void dsi_crypt_ctr_block( dsi_context* ctx, - const unsigned char input[16], - unsigned char output[16] ) +void dsi_crypt_ctr_block(dsi_context* ctx, const unsigned char input[16], unsigned char output[16]) { int i; unsigned char stream[16]; @@ -89,14 +79,14 @@ void dsi_crypt_ctr_block( dsi_context* ctx, if (input) { - for(i=0; i<16; i++) + for (i=0; i<16; i++) { output[i] = stream[15-i] ^ input[i]; } } else { - for(i=0; i<16; i++) + for (i=0; i<16; i++) output[i] = stream[15-i]; } @@ -104,12 +94,8 @@ void dsi_crypt_ctr_block( dsi_context* ctx, } -void dsi_init_ccm( dsi_context* ctx, - unsigned char key[16], - unsigned int maclength, - unsigned int payloadlength, - unsigned int assoclength, - unsigned char nonce[12] ) +void dsi_init_ccm(dsi_context* ctx, unsigned char key[16], unsigned int maclength, + unsigned int payloadlength, unsigned int assoclength, unsigned char nonce[12]) { int i; @@ -128,7 +114,7 @@ void dsi_init_ccm( dsi_context* ctx, ctx->mac[0] = (maclength<<3) | 2; if (assoclength) ctx->mac[0] |= (1<<6); - for(i=0; i<12; i++) + for (i=0; i<12; i++) ctx->mac[1+i] = nonce[11-i]; ctx->mac[13] = payloadlength>>16; ctx->mac[14] = payloadlength>>8; @@ -139,7 +125,7 @@ void dsi_init_ccm( dsi_context* ctx, // CCM CTR: // [1-byte flags] [12-byte nonce] [3-byte ctr] ctx->ctr[0] = 2; - for(i=0; i<12; i++) + for (i=0; i<12; i++) ctx->ctr[1+i] = nonce[11-i]; ctx->ctr[13] = 0; ctx->ctr[14] = 0; @@ -148,21 +134,18 @@ void dsi_init_ccm( dsi_context* ctx, dsi_crypt_ctr_block(ctx, 0, ctx->S0); } -void dsi_encrypt_ccm_block( dsi_context* ctx, - unsigned char input[16], - unsigned char output[16], - unsigned char* mac ) +void dsi_encrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac) { int i; - for(i=0; i<16; i++) + for (i=0; i<16; i++) ctx->mac[i] ^= input[15-i]; aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->mac, ctx->mac); if (mac) { - for(i=0; i<16; i++) + for (i=0; i<16; i++) mac[i] = ctx->mac[15-i] ^ ctx->S0[i]; } @@ -171,10 +154,7 @@ void dsi_encrypt_ccm_block( dsi_context* ctx, } -void dsi_decrypt_ccm_block( dsi_context* ctx, - unsigned char input[16], - unsigned char output[16], - unsigned char* mac ) +void dsi_decrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac) { int i; @@ -184,12 +164,12 @@ void dsi_decrypt_ccm_block( dsi_context* ctx, dsi_crypt_ctr_block(ctx, input, output); - for(i=0; i<16; i++) + for (i=0; i<16; i++) ctx->mac[i] ^= output[15-i]; } else { - for(i=0; i<16; i++) + for (i=0; i<16; i++) ctx->mac[i] ^= input[15-i]; } @@ -198,22 +178,18 @@ void dsi_decrypt_ccm_block( dsi_context* ctx, if (mac) { - for(i=0; i<16; i++) + for (i=0; i<16; i++) mac[i] = ctx->mac[15-i] ^ ctx->S0[i]; } } -void dsi_decrypt_ccm( dsi_context* ctx, - unsigned char* input, - unsigned char* output, - unsigned int size, - unsigned char* mac ) +void dsi_decrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac) { unsigned char block[16]; unsigned char ctr[16]; - while(size > 16) + while (size > 16) { dsi_decrypt_ccm_block(ctx, input, output, mac); @@ -226,7 +202,7 @@ void dsi_decrypt_ccm( dsi_context* ctx, } memcpy(ctr, ctx->ctr, 16); - memset(block, 0, 16); + memset(block, 0, 16); dsi_crypt_ctr_block(ctx, block, block); memcpy(ctx->ctr, ctr, 16); memcpy(block, input, size); @@ -237,15 +213,11 @@ void dsi_decrypt_ccm( dsi_context* ctx, } -void dsi_encrypt_ccm( dsi_context* ctx, - unsigned char* input, - unsigned char* output, - unsigned int size, - unsigned char* mac ) +void dsi_encrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac) { unsigned char block[16]; - while(size > 16) + while (size > 16) { dsi_encrypt_ccm_block(ctx, input, output, mac); @@ -263,30 +235,25 @@ void dsi_encrypt_ccm( dsi_context* ctx, memcpy(output, block, size); } -void dsi_es_init( dsi_es_context* ctx, - unsigned char key[16] ) +void dsi_es_init(dsi_es_context* ctx, unsigned char key[16]) { memcpy(ctx->key, key, 16); ctx->randomnonce = 1; } -void dsi_es_set_nonce( dsi_es_context* ctx, - unsigned char nonce[12] ) +void dsi_es_set_nonce(dsi_es_context* ctx, unsigned char nonce[12]) { memcpy(ctx->nonce, nonce, 12); ctx->randomnonce = 0; } -void dsi_es_set_random_nonce( dsi_es_context* ctx ) +void dsi_es_set_random_nonce(dsi_es_context* ctx) { ctx->randomnonce = 1; } - -int dsi_es_decrypt( dsi_es_context* ctx, - unsigned char* buffer, - unsigned char metablock[32], - unsigned int size ) + +int dsi_es_decrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size) { unsigned char ctr[16]; unsigned char nonce[12]; @@ -311,14 +278,14 @@ int dsi_es_decrypt( dsi_es_context* ctx, chksize = (scratchpad[13]<<16) | (scratchpad[14]<<8) | (scratchpad[15]<<0); if (scratchpad[0] != 0x3A) - { + { return -1; - } - - if (chksize != size) - { - return -2; - } + } + + if (chksize != size) + { + return -2; + } memcpy(nonce, metablock + 17, 12); @@ -326,18 +293,15 @@ int dsi_es_decrypt( dsi_es_context* ctx, dsi_decrypt_ccm(&cryptoctx, buffer, buffer, size, genmac); if (memcmp(genmac, chkmac, 16) != 0) - { + { return -3; - } - + } + return 0; } -void dsi_es_encrypt( dsi_es_context* ctx, - unsigned char* buffer, - unsigned char metablock[32], - unsigned int size ) +void dsi_es_encrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size) { int i; unsigned char nonce[12]; @@ -348,9 +312,9 @@ void dsi_es_encrypt( dsi_es_context* ctx, if (ctx->randomnonce) { - srand( (unsigned int)time(0) ); + srand((unsigned int)time(0)); - for(i=0; i<12; i++) + for (i=0; i<12; i++) nonce[i] = rand(); } else diff --git a/arm9/src/nand/twltool/dsi.h b/arm9/src/nand/twltool/dsi.h index f29958c..f170509 100644 --- a/arm9/src/nand/twltool/dsi.h +++ b/arm9/src/nand/twltool/dsi.h @@ -10,7 +10,7 @@ typedef struct unsigned char S0[16]; unsigned int maclen; - aes_context aes; + aes_context aes; } dsi_context; @@ -27,75 +27,39 @@ typedef struct extern "C" { #endif -void dsi_set_key( dsi_context* ctx, - const unsigned char key[16] ); +void dsi_set_key(dsi_context* ctx, const unsigned char key[16]); -void dsi_add_ctr( dsi_context* ctx, - unsigned int carry ); +void dsi_add_ctr(dsi_context* ctx, unsigned int carry); -void dsi_set_ctr( dsi_context* ctx, - const unsigned char ctr[16] ); +void dsi_set_ctr(dsi_context* ctx, const unsigned char ctr[16]); -void dsi_init_ctr( dsi_context* ctx, - const unsigned char key[16], - const unsigned char ctr[12] ); +void dsi_init_ctr(dsi_context* ctx, const unsigned char key[16], const unsigned char ctr[12]); -void dsi_crypt_ctr( dsi_context* ctx, - const void* in, - void* out, - unsigned int len); - -void dsi_crypt_ctr_block( dsi_context* ctx, - const unsigned char input[16], - unsigned char output[16] ); +void dsi_crypt_ctr(dsi_context* ctx, const void* in, void* out, unsigned int len); -void dsi_init_ccm( dsi_context* ctx, - unsigned char key[16], - unsigned int maclength, - unsigned int payloadlength, - unsigned int assoclength, - unsigned char nonce[12] ); +void dsi_crypt_ctr_block(dsi_context* ctx, const unsigned char input[16], unsigned char output[16]); -void dsi_encrypt_ccm_block( dsi_context* ctx, - unsigned char input[16], - unsigned char output[16], - unsigned char* mac ); +void dsi_init_ccm(dsi_context* ctx, unsigned char key[16], unsigned int maclength, + unsigned int payloadlength, unsigned int assoclength, unsigned char nonce[12]); -void dsi_decrypt_ccm_block( dsi_context* ctx, - unsigned char input[16], - unsigned char output[16], - unsigned char* mac ); +void dsi_encrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac); + +void dsi_decrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac); -void dsi_decrypt_ccm( dsi_context* ctx, - unsigned char* input, - unsigned char* output, - unsigned int size, - unsigned char* mac ); +void dsi_decrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac); -void dsi_encrypt_ccm( dsi_context* ctx, - unsigned char* input, - unsigned char* output, - unsigned int size, - unsigned char* mac ); +void dsi_encrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac); -void dsi_es_init( dsi_es_context* ctx, - unsigned char key[16] ); +void dsi_es_init(dsi_es_context* ctx, unsigned char key[16]); -void dsi_es_set_nonce( dsi_es_context* ctx, - unsigned char nonce[12] ); +void dsi_es_set_nonce(dsi_es_context* ctx, unsigned char nonce[12]); -void dsi_es_set_random_nonce( dsi_es_context* ctx ); +void dsi_es_set_random_nonce(dsi_es_context* ctx); -int dsi_es_decrypt( dsi_es_context* ctx, - unsigned char* buffer, - unsigned char metablock[32], - unsigned int size ); +int dsi_es_decrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size); -void dsi_es_encrypt( dsi_es_context* ctx, - unsigned char* buffer, - unsigned char metablock[32], - unsigned int size ); +void dsi_es_encrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size); #ifdef __cplusplus } diff --git a/arm9/src/nand/u128_math.c b/arm9/src/nand/u128_math.c index e6b2abe..9bf247b 100644 --- a/arm9/src/nand/u128_math.c +++ b/arm9/src/nand/u128_math.c @@ -6,101 +6,101 @@ // rotate a 128bit, little endian by shift bits in direction of increasing significance. void u128_lrot(uint8_t *num, uint32_t shift) { - uint8_t tmp[16]; - for (int i=0;i<16;i++) - { - // rot: rotate to more significant. - // LSB is tmp[0], MSB is tmp[15] - const uint32_t byteshift = shift / 8 ; - const uint32_t bitshift = shift % 8; - tmp[(i+byteshift) % 16] = (num[i] << bitshift) - | ((num[(i+16-1) % 16] >> (8-bitshift)) & 0xff); - } - memcpy(num, tmp, 16) ; + uint8_t tmp[16]; + for (int i=0;i<16;i++) + { + // rot: rotate to more significant. + // LSB is tmp[0], MSB is tmp[15] + const uint32_t byteshift = shift / 8; + const uint32_t bitshift = shift % 8; + tmp[(i+byteshift) % 16] = (num[i] << bitshift) + | ((num[(i+16-1) % 16] >> (8-bitshift)) & 0xff); + } + memcpy(num, tmp, 16); } // rotate a 128bit, little endian by shift bits in direction of decreasing significance. void u128_rrot(uint8_t *num, uint32_t shift) { - uint8_t tmp[16]; - for (int i=0;i<16;i++) - { - // rot: rotate to less significant. - // LSB is tmp[0], MSB is tmp[15] - const uint32_t byteshift = shift / 8 ; - const uint32_t bitshift = shift % 8; - tmp[i] = (num[(i+byteshift) % 16] >> bitshift) - | ((num[(i+byteshift+1) % 16] << (8-bitshift)) & 0xff); - } - memcpy(num, tmp, 16) ; + uint8_t tmp[16]; + for (int i=0;i<16;i++) + { + // rot: rotate to less significant. + // LSB is tmp[0], MSB is tmp[15] + const uint32_t byteshift = shift / 8; + const uint32_t bitshift = shift % 8; + tmp[i] = (num[(i+byteshift) % 16] >> bitshift) + | ((num[(i+byteshift+1) % 16] << (8-bitshift)) & 0xff); + } + memcpy(num, tmp, 16); } // xor two 128bit, little endian values and store the result into the first void u128_xor(uint8_t *a, const uint8_t *b) { - for (int i=0;i<16;i++) - { - a[i] = a[i] ^ b[i] ; - } + for (int i=0;i<16;i++) + { + a[i] = a[i] ^ b[i]; + } } // or two 128bit, little endian values and store the result into the first void u128_or(uint8_t *a, const uint8_t *b) { - for (int i=0;i<16;i++) - { - a[i] = a[i] | b[i] ; - } + for (int i=0;i<16;i++) + { + a[i] = a[i] | b[i]; + } } // and two 128bit, little endian values and store the result into the first void u128_and(uint8_t *a, const uint8_t *b) { - for (int i=0;i<16;i++) - { - a[i] = a[i] & b[i] ; - } + for (int i=0;i<16;i++) + { + a[i] = a[i] & b[i]; + } } // add two 128bit, little endian values and store the result into the first void u128_add(uint8_t *a, const uint8_t *b) { - uint8_t carry = 0 ; - for (int i=0;i<16;i++) - { - uint16_t sum = a[i] + b[i] + carry ; - a[i] = sum & 0xff ; - carry = sum >> 8 ; - } + uint8_t carry = 0; + for (int i=0;i<16;i++) + { + uint16_t sum = a[i] + b[i] + carry; + a[i] = sum & 0xff; + carry = sum >> 8; + } } // add two 128bit, little endian values and store the result into the first void u128_add32(uint8_t *a, const uint32_t b) { - uint8_t _b[16]; - memset(_b, 0, sizeof(_b)) ; - for (int i=0;i<4;i++) - _b[i] = b >> (i*8) ; - u128_add(a, _b); + uint8_t _b[16]; + memset(_b, 0, sizeof(_b)); + for (int i=0;i<4;i++) + _b[i] = b >> (i*8); + u128_add(a, _b); } // sub two 128bit, little endian values and store the result into the first void u128_sub(uint8_t *a, const uint8_t *b) { - uint8_t carry = 0 ; - for (int i=0;i<16;i++) - { - uint16_t sub = a[i] - b[i] - (carry & 1); - a[i] = sub & 0xff ; - carry = sub >> 8 ; - } + uint8_t carry = 0; + for (int i=0;i<16;i++) + { + uint16_t sub = a[i] - b[i] - (carry & 1); + a[i] = sub & 0xff; + carry = sub >> 8; + } } -void u128_swap(uint8_t *out, const uint8_t *in) +void u128_swap(uint8_t *out, const uint8_t *in) { - for (int i=0;i<16;i++) - { - out[15-i] = in[i]; - } -} \ No newline at end of file + for (int i=0;i<16;i++) + { + out[15-i] = in[i]; + } +} diff --git a/arm9/src/nand/u128_math.h b/arm9/src/nand/u128_math.h index 114b500..bc2f64a 100644 --- a/arm9/src/nand/u128_math.h +++ b/arm9/src/nand/u128_math.h @@ -7,32 +7,32 @@ extern "C" { #endif // left rotate by (shift % 128) bits -void u128_lrot(uint8_t *num, uint32_t shift) ; +void u128_lrot(uint8_t *num, uint32_t shift); // right rotate by (shift % 128) bits -void u128_rrot(uint8_t *num, uint32_t shift) ; +void u128_rrot(uint8_t *num, uint32_t shift); // bitwise xor strored to a -void u128_xor(uint8_t *a, const uint8_t *b) ; +void u128_xor(uint8_t *a, const uint8_t *b); // bitwise or strored to a -void u128_or(uint8_t *a, const uint8_t *b) ; +void u128_or(uint8_t *a, const uint8_t *b); // bitwise and strored to a -void u128_and(uint8_t *a, const uint8_t *b) ; +void u128_and(uint8_t *a, const uint8_t *b); // add b to a and store in a -void u128_add(uint8_t *a, const uint8_t *b) ; +void u128_add(uint8_t *a, const uint8_t *b); // add 32 bit value b to a and store in a -void u128_add32(uint8_t *a, const uint32_t b) ; +void u128_add32(uint8_t *a, const uint32_t b); // substract b from a and store in a -void u128_sub(uint8_t *a, const uint8_t *b) ; +void u128_sub(uint8_t *a, const uint8_t *b); -// swap byte order -void u128_swap(uint8_t *out, const uint8_t *in) ; +// swap byte order +void u128_swap(uint8_t *out, const uint8_t *in); #ifdef __cplusplus } -#endif \ No newline at end of file +#endif diff --git a/arm9/src/rom.c b/arm9/src/rom.c index e44a5c7..2d20728 100644 --- a/arm9/src/rom.c +++ b/arm9/src/rom.c @@ -61,7 +61,7 @@ tNDSBanner* getRomBanner(char const* fpath) } free(h); - fclose(f); + fclose(f); } return b; @@ -112,7 +112,7 @@ bool getGameTitlePath(char const* fpath, char* out, bool full) tNDSBanner* b = getRomBanner(fpath); bool result = getGameTitle(b, out, full); - + free(b); return result; } @@ -134,7 +134,7 @@ bool getRomCode(tDSiHeader* h, char* out) sprintf(out, "%.4s", h->ndshdr.gameCode); - return true; + return true; } void printRomInfo(char const* fpath) @@ -184,9 +184,9 @@ void printRomInfo(char const* fpath) switch (h->ndshdr.unitCode) { - case 0: iprintf("NDS"); break; + case 0: iprintf("NDS"); break; case 2: iprintf("NDS+DSi"); break; - case 3: iprintf("DSi"); break; + case 3: iprintf("DSi"); break; default: iprintf("unknown"); } @@ -199,8 +199,8 @@ void printRomInfo(char const* fpath) switch (h->ndshdr.reserved1[7]) { - case 0x3: iprintf("Normal"); break; - case 0xB: iprintf("Sys"); break; + case 0x3: iprintf("Normal"); break; + case 0xB: iprintf("Sys"); break; case 0xF: iprintf("Debug/Sys"); break; default: iprintf("unknown"); } @@ -216,7 +216,7 @@ void printRomInfo(char const* fpath) h->tid_high == 0x00030017 || h->tid_high == 0x00030000) { - iprintf("Title ID: %08x %08x\n", (unsigned int)h->tid_high, (unsigned int)h->tid_low); + iprintf("Title ID: %08x %08x\n", (unsigned int)h->tid_high, (unsigned int)h->tid_low); } } diff --git a/arm9/src/sav.c b/arm9/src/sav.c index 90cdbde..9eaba01 100644 --- a/arm9/src/sav.c +++ b/arm9/src/sav.c @@ -6,13 +6,13 @@ static u32 _getClusterSize(u32 sizebytes) { if (sizebytes < 573440) return 512; - + else if (sizebytes < 5472256) return 2048; - + else if (sizebytes < 17301504) return 4096; - + else return 2048; } @@ -21,7 +21,7 @@ static u16 _getMaxFiles(u32 sizebytes) { if (sizebytes < 573440) return 16; - + else return 256; } @@ -52,17 +52,17 @@ bool initFatHeader(FILE* f) { if (!f) return false; - + //get size fseek(f, 0, SEEK_END); u32 size = ftell(f); - + FATHeader* h = (FATHeader*)malloc(sizeof(FATHeader)); - + h->BS_JmpBoot[0] = 0xE9; h->BS_JmpBoot[1] = 0; h->BS_JmpBoot[2] = 0; - + h->BS_OEMName[0] = 'M'; h->BS_OEMName[1] = 'S'; h->BS_OEMName[2] = 'W'; @@ -71,14 +71,14 @@ bool initFatHeader(FILE* f) h->BS_OEMName[5] = '4'; h->BS_OEMName[6] = '.'; h->BS_OEMName[7] = '1'; - + h->BPB_BytesPerSec = 512; h->BPB_SecPerClus = _getClusterSize(size) / h->BPB_BytesPerSec; h->BPB_RsvdSecCnt = 1; h->BPB_NumFATs = 2; h->BPB_RootEntCnt = _getMaxFiles(size) * 2; h->BPB_TotSec16 = _getTotSec16(size, h->BPB_BytesPerSec); // - h->BPB_Media = 0xF8; + h->BPB_Media = 0xF8; h->BPB_FATSz16 = _getFatz(size); // h->BPB_SecPerTrk = 0; h->BPB_NumHeads = 0; @@ -88,7 +88,7 @@ bool initFatHeader(FILE* f) h->BS_Reserved1 = 0; h->BS_BootSig = 0x29; h->BS_VolID = 305419896; - + h->BS_VolLab[0] = 'V'; h->BS_VolLab[1] = 'O'; h->BS_VolLab[2] = 'L'; @@ -100,7 +100,7 @@ bool initFatHeader(FILE* f) h->BS_VolLab[8] = 'B'; h->BS_VolLab[9] = 'E'; h->BS_VolLab[10] = 'L'; - + h->BS_FilSysType[0] = 'F'; h->BS_FilSysType[1] = 'A'; h->BS_FilSysType[2] = 'T'; @@ -109,15 +109,15 @@ bool initFatHeader(FILE* f) h->BS_FilSysType[5] = ' '; h->BS_FilSysType[6] = ' '; h->BS_FilSysType[7] = ' '; - + memset(h->BS_BootCode, 0, 448); - + h->BS_BootSign = 0xAA55; - + fseek(f, 0, SEEK_SET); fwrite(h, sizeof(FATHeader), 1, f); free(h); - + return true; } \ No newline at end of file diff --git a/arm9/src/storage.c b/arm9/src/storage.c index 7ca8467..cfe03f5 100644 --- a/arm9/src/storage.c +++ b/arm9/src/storage.c @@ -48,15 +48,15 @@ void printProgressBar(float percent) //Print bars if (bars > 0) - { + { for (int i = 0; i < bars; i++) - iprintf("\x1b[23;%dH|", 1 + i); + iprintf("\x1b[23;%dH|", 1 + i); } lastBars = bars; iprintf("\x1B[47m"); //white - } + } } void clearProgressBar() @@ -150,7 +150,7 @@ int copyFilePart(char const* src, u32 offset, u32 size, char const* dst) } fclose(fin); - return 0; + return 0; } unsigned long long getFileSize(FILE* f) @@ -257,7 +257,7 @@ bool copyDir(char const* src, char const* dst) iprintf("%s -> \n%s...", fsrc, fdst); int ret = copyFile(fsrc, fdst); - + if (ret != 0) { iprintf("\x1B[31m"); //red @@ -407,9 +407,9 @@ unsigned long long getDirSize(const char* path, u32 blockSize) size += getDirSize(fullpath, blockSize); } else - { + { char fullpath[260]; - sprintf(fullpath, "%s/%s", path, ent->d_name); + sprintf(fullpath, "%s/%s", path, ent->d_name); size += getFileSizePath(fullpath); @@ -444,32 +444,32 @@ int getMenuSlotsFree() }; int freeSlots = getMenuSlots(); - + DIR* dir; - struct dirent* ent; - + struct dirent* ent; + for (int i = 0; i < NUM_OF_DIRS; i++) { char path[256]; sprintf(path, "%s:/title/%s", sdnandMode ? "sd" : "nand", dirs[i]); - + dir = opendir(path); - + if (dir) { while ( (ent = readdir(dir)) != NULL ) { if (strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0) continue; - + if (ent->d_type == DT_DIR) freeSlots -= 1; - } + } } closedir(dir); } - + return freeSlots; } diff --git a/arm9/src/testmenu.c b/arm9/src/testmenu.c index 4e16114..091f667 100644 --- a/arm9/src/testmenu.c +++ b/arm9/src/testmenu.c @@ -39,7 +39,7 @@ void testMenu() swiWaitForVBlank(); unsigned long long sdsize = getSDCardSize(); - printBytes(sdsize); + printBytes(sdsize); iprintf("\n"); swiWaitForVBlank(); diff --git a/arm9/src/titlemenu.c b/arm9/src/titlemenu.c index 63667bb..b491d14 100644 --- a/arm9/src/titlemenu.c +++ b/arm9/src/titlemenu.c @@ -62,11 +62,11 @@ void titleMenu() { resetMenu(m); generateList(m); - } + } } break; } - + printMenu(m); } } @@ -150,12 +150,12 @@ static void generateList(Menu* m) //current item is not on page if (count < m->page * ITEMS_PER_PAGE) count += 1; - + else { if (m->itemCount >= ITEMS_PER_PAGE) done = true; - + else { //found requested title @@ -168,7 +168,7 @@ static void generateList(Menu* m) addMenuItem(m, title, path, 0); free(path); - } + } } } } @@ -176,7 +176,7 @@ static void generateList(Menu* m) } closedir(subdir); - free(contentPath); + free(contentPath); } } } @@ -379,7 +379,7 @@ static bool delete(Menu* m) if (!m) return false; char* fpath = m->items[m->cursor].value; - + bool result = false; bool choice = NO; { @@ -390,7 +390,7 @@ static bool delete(Menu* m) char str[] = "Are you sure you want to delete\n"; char* msg = (char*)malloc(strlen(str) + strlen(title) + 8); sprintf(msg, "%s%s", str, title); - + choice = choiceBox(msg); free(msg);