Clean up formatting

No code changes outside of a very minor change to arm7/main.c (removes unused functions)
This commit is contained in:
Pk11 2022-01-14 22:42:47 -06:00
parent c9267de164
commit 68fc9a6586
28 changed files with 937 additions and 959 deletions

View File

@ -15,10 +15,10 @@ include $(DEVKITARM)/ds_rules
# all directories are relative to this makefile # all directories are relative to this makefile
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
BUILD := build BUILD := build
SOURCES := src SOURCES := src
INCLUDES := include build INCLUDES := include build
DATA := DATA :=
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# options for code generation # options for code generation
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
@ -44,32 +44,32 @@ LIBS := -lnds7
# include and lib # include and lib
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
LIBDIRS := $(LIBNDS) LIBDIRS := $(LIBNDS)
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional # no real need to edit anything past this point unless you need to add additional
# rules for different file extensions # rules for different file extensions
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR))) ifneq ($(BUILD),$(notdir $(CURDIR)))
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
export ARM7ELF := $(CURDIR)/$(TARGET).elf export ARM7ELF := $(CURDIR)/$(TARGET).elf
export DEPSDIR := $(CURDIR)/$(BUILD) export DEPSDIR := $(CURDIR)/$(BUILD)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
export OFILES := $(addsuffix .o,$(BINFILES)) \ export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD) -I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
@ -87,33 +87,33 @@ endif
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
.PHONY: $(BUILD) clean .PHONY: $(BUILD) clean
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
$(BUILD): $(BUILD):
@[ -d $@ ] || mkdir -p $@ @[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
clean: clean:
@echo clean ... @echo clean ...
@rm -fr $(BUILD) *.elf @rm -fr $(BUILD) *.elf
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
else else
DEPENDS := $(OFILES:.o=.d) DEPENDS := $(OFILES:.o=.d)
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# main targets # main targets
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
$(ARM7ELF) : $(OFILES) $(ARM7ELF) : $(OFILES)
@echo linking $(notdir $@) @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 %.bin.o : %.bin
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
@ -121,7 +121,7 @@ $(ARM7ELF) : $(OFILES)
@$(bin2o) @$(bin2o)
-include $(DEPENDS) -include $(DEPENDS)
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------
endif endif
#--------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------

View File

@ -29,34 +29,29 @@
---------------------------------------------------------------------------------*/ ---------------------------------------------------------------------------------*/
#include "my_sdmmc.h" #include "my_sdmmc.h"
//#include <dswifi7.h>
//#include <maxmod7.h>
#include <nds.h> #include <nds.h>
#include <string.h> #include <string.h>
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
void VblankHandler(void) { void VcountHandler()
//---------------------------------------------------------------------------------
// Wifi_Update();
}
//---------------------------------------------------------------------------------
void VcountHandler() {
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
{
inputGetAndSend(); inputGetAndSend();
} }
volatile bool exitflag = false; volatile bool exitflag = false;
volatile bool reboot = 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 // 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); int cause = (i2cReadRegister(I2C_PM, I2CREGPM_PWRIF) & 0x3) | (i2cReadRegister(I2C_GPIO, 0x02)<<2);
switch (cause & 3) { switch (cause & 3)
{
case 1: case 1:
reboot = true; reboot = true;
exitflag = 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]; for (int i = 0; i < 4; i++) REG_AES_IV[i] = ctr[3-i];
} }
// 10 11 22 23 24 25 // 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 //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 //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. //as long as the output changes when keyslot values change, it's good enough.
AES_RDFIFO_FLUSH | void aes(void* in, void* out, void* iv, u32 method)
AES_CNT_KEY_APPLY | {
REG_AES_CNT = ( AES_CNT_MODE(method) |
AES_WRFIFO_FLUSH |
AES_RDFIFO_FLUSH |
AES_CNT_KEY_APPLY |
AES_CNT_KEYSLOT(3) | AES_CNT_KEYSLOT(3) |
AES_CNT_DMA_WRITE_SIZE(2) | AES_CNT_DMA_WRITE_SIZE(2) |
AES_CNT_DMA_READ_SIZE(1) 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_BLKCNT = (1 << 16);
REG_AES_CNT |= 0x80000000; REG_AES_CNT |= 0x80000000;
for (int j = 0; j < 0x10; j+=4) REG_AES_WRFIFO = *((u32*)(in+j)); 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; for (int j = 0; j < 0x10; j+=4) *((u32*)(out+j)) = REG_AES_RDFIFO;
//REG_AES_CNT &= ~0x80000000; //REG_AES_CNT &= ~0x80000000;
//if (method & (AES_CTR_DECRYPT | AES_CTR_ENCRYPT)) add_ctr((u8*)iv); //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 my_sdmmc_nand_startup();
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
int main() { int main()
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
{
// clear sound registers // clear sound registers
dmaFillWords(0, (void*)0x04000400, 0x100); dmaFillWords(0, (void*)0x04000400, 0x100);
@ -115,8 +116,9 @@ int main() {
initClockIRQ(); initClockIRQ();
fifoInit(); fifoInit();
touchInit(); 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; u8 *out=(u8*)0x02300000;
#if USENATIVECONSOLEID #if USENATIVECONSOLEID
@ -132,19 +134,21 @@ int main() {
u8 base[16]={0}; u8 base[16]={0};
u8 in[16]={0}; u8 in[16]={0};
u8 iv[16]={0}; u8 iv[16]={0};
u8 *scratch=(u8*)0x02300200; u8 *scratch=(u8*)0x02300200;
u8 *key3=(u8*)0x40044D0; u8 *key3=(u8*)0x40044D0;
aes(in, base, iv, 2); 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. //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) //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 i=0;i<16;i++)
for(int j=0;j<256;j++){ {
for (int j=0;j<256;j++)
{
*(key3+i)=j & 0xFF; *(key3+i)=j & 0xFF;
aes(in, scratch, iv, 2); aes(in, scratch, iv, 2);
if(!memcmp(scratch, base, 16)){ if (!memcmp(scratch, base, 16))
{
out[i]=j; out[i]=j;
//hit++; //hit++;
break; 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 my_sdmmc_get_cid(true, (u32*)0x2FFD7BC); // Get eMMC CID
//sdmmc_nand_cid((u32*)0x2FFD7BC); //sdmmc_nand_cid((u32*)0x2FFD7BC);
} }
// mmInstall(FIFO_MAXMOD);
SetYtrigger(80); SetYtrigger(80);
// installWifiFIFO();
installSoundFIFO(); installSoundFIFO();
installSystemFIFO(); installSystemFIFO();
irqSet(IRQ_VCOUNT, VcountHandler); irqSet(IRQ_VCOUNT, VcountHandler);
irqSet(IRQ_VBLANK, VblankHandler);
irqEnable( IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK); irqEnable( IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK);
// Keep the ARM7 mostly idle // Keep the ARM7 mostly idle
while (!exitflag) { while (!exitflag)
if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R))) { {
if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R)))
{
exitflag = true; exitflag = true;
} }
@ -195,10 +195,13 @@ int main() {
fifoWaitValue32(FIFO_USER_02); fifoWaitValue32(FIFO_USER_02);
fifoCheckValue32(FIFO_USER_02); fifoCheckValue32(FIFO_USER_02);
if (reboot) { if (reboot)
{
i2cWriteRegister(I2C_PM, I2CREGPM_RESETFLAG, 1); i2cWriteRegister(I2C_PM, I2CREGPM_RESETFLAG, 1);
i2cWriteRegister(I2C_PM, I2CREGPM_PWRCNT, 1); i2cWriteRegister(I2C_PM, I2CREGPM_PWRCNT, 1);
} else { }
else
{
writePowerManagement(PM_CONTROL_REG,PM_SYSTEM_PWR); writePowerManagement(PM_CONTROL_REG,PM_SYSTEM_PWR);
} }

View File

@ -10,50 +10,57 @@
static struct mmcdevice deviceSD; static struct mmcdevice deviceSD;
static struct mmcdevice deviceNAND; static struct mmcdevice deviceNAND;
/*mmcdevice *getMMCDevice(int drive) { /*mmcdevice *getMMCDevice(int drive)
if(drive==0) return &deviceNAND; {
return &deviceSD; 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; //if (ctx->error == 0x4) return -1;
return (ctx->error << 29) >> 31; //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); sdmmc_mask16(REG_SDPORTSEL,0x3,(u16)ctx->devicenumber);
if (ctx->SDOPT == 0) { setckl(ctx->clk);
sdmmc_mask16(REG_SDOPT, 0, 0x8000); if (ctx->SDOPT == 0)
} else { {
sdmmc_mask16(REG_SDOPT, 0x8000, 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; const bool getSDRESP = (cmd << 15) >> 31;
u16 flags = (cmd << 15) >> 31; u16 flags = (cmd << 15) >> 31;
const bool readdata = cmd & 0x20000; const bool readdata = cmd & 0x20000;
const bool writedata = cmd & 0x40000; const bool writedata = cmd & 0x40000;
if(readdata || writedata) if (readdata || writedata)
{ {
flags |= TMIO_STAT0_DATAEND; flags |= TMIO_STAT0_DATAEND;
} }
ctx->error = 0; 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_SDIRMASK0,0);
sdmmc_write16(REG_SDIRMASK1,0); sdmmc_write16(REG_SDIRMASK1,0);
sdmmc_write16(REG_SDSTATUS0,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 #endif
u16 status0 = 0; u16 status0 = 0;
while(1) while (1)
{ {
volatile u16 status1 = sdmmc_read16(REG_SDSTATUS1); volatile u16 status1 = sdmmc_read16(REG_SDSTATUS1);
#ifdef DATA32_SUPPORT #ifdef DATA32_SUPPORT
volatile u16 ctl32 = sdmmc_read16(REG_SDDATACTL32); volatile u16 ctl32 = sdmmc_read16(REG_SDDATACTL32);
if((ctl32 & 0x100)) if (ctl32 & 0x100)
#else #else
if((status1 & TMIO_STAT1_RXRDY)) if (status1 & TMIO_STAT1_RXRDY)
#endif #endif
{ {
if(readdata) if (readdata)
{ {
if(rUseBuf) if (rUseBuf)
{ {
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0); sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0);
if(size >= blkSize) if (size >= blkSize)
{ {
#ifdef DATA32_SUPPORT #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); *rDataPtr32++ = sdmmc_read32(REG_SDFIFO32);
} }
} }
else else
{ {
for(u32 i = 0; i < blkSize; i += 4) for (u32 i = 0; i < blkSize; i += 4)
{ {
u32 data = sdmmc_read32(REG_SDFIFO32); u32 data = sdmmc_read32(REG_SDFIFO32);
*rDataPtr8++ = data; *rDataPtr8++ = data;
@ -124,16 +131,16 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
} }
} }
#else #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); *rDataPtr16++ = sdmmc_read16(REG_SDFIFO);
} }
} }
else else
{ {
for(u16 i = 0; i < blkSize; i += 2) for (u16 i = 0; i < blkSize; i += 2)
{ {
u16 data = sdmmc_read16(REG_SDFIFO); u16 data = sdmmc_read16(REG_SDFIFO);
*rDataPtr8++ = data; *rDataPtr8++ = data;
@ -149,29 +156,29 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
} }
} }
#ifdef DATA32_SUPPORT #ifdef DATA32_SUPPORT
if(!(ctl32 & 0x200)) if (!(ctl32 & 0x200))
#else #else
if((status1 & TMIO_STAT1_TXRQ)) if ((status1 & TMIO_STAT1_TXRQ))
#endif #endif
{ {
if(writedata) if (writedata)
{ {
if(tUseBuf) if (tUseBuf)
{ {
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0); sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0);
if(size >= blkSize) if (size >= blkSize)
{ {
#ifdef DATA32_SUPPORT #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++); sdmmc_write32(REG_SDFIFO32, *tDataPtr32++);
} }
} }
else else
{ {
for(u32 i = 0; i < blkSize; i += 4) for (u32 i = 0; i < blkSize; i += 4)
{ {
u32 data = *tDataPtr8++; u32 data = *tDataPtr8++;
data |= (u32)*tDataPtr8++ << 8; data |= (u32)*tDataPtr8++ << 8;
@ -181,16 +188,16 @@ void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
} }
} }
#else #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++); sdmmc_write16(REG_SDFIFO, *tDataPtr16++);
} }
} }
else else
{ {
for(u16 i = 0; i < blkSize; i += 2) for (u16 i = 0; i < blkSize; i += 2)
{ {
u16 data = *tDataPtr8++; u16 data = *tDataPtr8++;
data |= (u16)(*tDataPtr8++ << 8); 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); sdmmc_mask16(REG_SDDATACTL32, 0x1000, 0);
} }
} }
if(status1 & TMIO_MASK_GW) if (status1 & TMIO_MASK_GW)
{ {
ctx->error |= 4; ctx->error |= 4;
break; break;
} }
if(!(status1 & TMIO_STAT1_CMD_BUSY)) if (!(status1 & TMIO_STAT1_CMD_BUSY))
{ {
status0 = sdmmc_read16(REG_SDSTATUS0); status0 = sdmmc_read16(REG_SDSTATUS0);
if(sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND) if (sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND)
{ {
ctx->error |= 0x1; ctx->error |= 0x1;
} }
if(status0 & TMIO_STAT0_DATAEND) if (status0 & TMIO_STAT0_DATAEND)
{ {
ctx->error |= 0x2; ctx->error |= 0x2;
} }
if((status0 & flags) == flags) if ((status0 & flags) == flags)
break; 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_SDSTATUS0,0);
sdmmc_write16(REG_SDSTATUS1,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[0] = (u32)(sdmmc_read16(REG_SDRESP0) | (sdmmc_read16(REG_SDRESP1) << 16));
ctx->ret[1] = (u32)(sdmmc_read16(REG_SDRESP2) | (sdmmc_read16(REG_SDRESP3) << 16)); ctx->ret[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; return 1; //my_sdmmc_cardready;
} }
@ -252,162 +260,168 @@ int my_sdmmc_cardinserted() {
static bool my_sdmmc_controller_initialised = false; 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.isSDHC = 0;
deviceSD.SDOPT = 0; deviceSD.SDOPT = 0;
deviceSD.res = 0; deviceSD.res = 0;
deviceSD.initarg = 0; deviceSD.initarg = 0;
deviceSD.clk = 0x80; deviceSD.clk = 0x80;
deviceSD.devicenumber = 0; deviceSD.devicenumber = 0;
deviceNAND.isSDHC = 0; deviceNAND.isSDHC = 0;
deviceNAND.SDOPT = 0; deviceNAND.SDOPT = 0;
deviceNAND.res = 0; deviceNAND.res = 0;
deviceNAND.initarg = 1; deviceNAND.initarg = 1;
deviceNAND.clk = 0x80; deviceNAND.clk = 0x80;
deviceNAND.devicenumber = 1; deviceNAND.devicenumber = 1;
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xF7FFu; *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xF7FFu;
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xEFFFu; *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xEFFFu;
#ifdef DATA32_SUPPORT #ifdef DATA32_SUPPORT
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u; *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u;
#else #else
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u; *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u;
#endif #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 #ifdef DATA32_SUPPORT
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFFu; *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFFu;
*(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDFu; *(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDFu;
*(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 512; *(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 512;
#else #else
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFDu; *(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFDu;
*(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDDu; *(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDDu;
*(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 0; *(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 0;
#endif #endif
*(vu16*)(SDMMC_BASE + REG_SDBLKCOUNT32) = 1; *(vu16*)(SDMMC_BASE + REG_SDBLKCOUNT32) = 1;
*(vu16*)(SDMMC_BASE + REG_SDRESET) &= 0xFFFEu; *(vu16*)(SDMMC_BASE + REG_SDRESET) &= 0xFFFEu;
*(vu16*)(SDMMC_BASE + REG_SDRESET) |= 1u; *(vu16*)(SDMMC_BASE + REG_SDRESET) |= 1u;
*(vu16*)(SDMMC_BASE + REG_SDIRMASK0) |= TMIO_MASK_ALL; *(vu16*)(SDMMC_BASE + REG_SDIRMASK0) |= TMIO_MASK_ALL;
*(vu16*)(SDMMC_BASE + REG_SDIRMASK1) |= TMIO_MASK_ALL>>16; *(vu16*)(SDMMC_BASE + REG_SDIRMASK1) |= TMIO_MASK_ALL>>16;
*(vu16*)(SDMMC_BASE + 0x0fc) |= 0xDBu; //SDCTL_RESERVED7 *(vu16*)(SDMMC_BASE + 0x0fc) |= 0xDBu; //SDCTL_RESERVED7
*(vu16*)(SDMMC_BASE + 0x0fe) |= 0xDBu; //SDCTL_RESERVED8 *(vu16*)(SDMMC_BASE + 0x0fe) |= 0xDBu; //SDCTL_RESERVED8
*(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu; *(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu;
#ifdef DATA32_SUPPORT #ifdef DATA32_SUPPORT
*(vu16*)(SDMMC_BASE + REG_SDCLKCTL) = 0x20; *(vu16*)(SDMMC_BASE + REG_SDCLKCTL) = 0x20;
*(vu16*)(SDMMC_BASE + REG_SDOPT) = 0x40EE; *(vu16*)(SDMMC_BASE + REG_SDOPT) = 0x40EE;
#else #else
*(vu16*)(SDMMC_BASE + REG_SDCLKCTL) = 0x40; //Nintendo sets this to 0x20 *(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_SDOPT) = 0x40EB; //Nintendo sets this to 0x40EE
#endif #endif
*(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu; *(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu;
*(vu16*)(SDMMC_BASE + REG_SDBLKLEN) = 512; *(vu16*)(SDMMC_BASE + REG_SDBLKLEN) = 512;
*(vu16*)(SDMMC_BASE + REG_SDSTOP) = 0; *(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; u32 result = 0;
switch (type) { if (type == -1) type = csd[14] >> 6;
case 0: switch (type)
{ {
u32 block_len = csd[9] & 0xf; case 0:
block_len = 1 << block_len; {
u32 mult = (csd[4] >> 7) | ((csd[5] & 3) << 1); u32 block_len = csd[9] & 0xf;
mult = 1 << (mult + 2); block_len = 1 << block_len;
result = csd[8] & 3; u32 mult = (csd[4] >> 7) | ((csd[5] & 3) << 1);
result = (result << 8) | csd[7]; mult = 1 << (mult + 2);
result = (result << 2) | (csd[6] >> 6); result = csd[8] & 3;
result = (result + 1) * mult * block_len / 512; result = (result << 8) | csd[7];
} result = (result << 2) | (csd[6] >> 6);
break; result = (result + 1) * mult * block_len / 512;
case 1: }
result = csd[7] & 0x3f; break;
result = (result << 8) | csd[6]; case 1:
result = (result << 8) | csd[5]; result = csd[7] & 0x3f;
result = (result + 1) * 1024; result = (result << 8) | csd[6];
break; result = (result << 8) | csd[5];
} result = (result + 1) * 1024;
return result; break;
}
return result;
} }
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
int my_sdmmc_sdcard_init() { int my_sdmmc_sdcard_init()
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
{
// We need to send at least 74 clock pulses. // We need to send at least 74 clock pulses.
my_setTarget(&deviceSD); my_setTarget(&deviceSD);
swiDelay(0x1980); // ~75-76 clocks swiDelay(0x1980); // ~75-76 clocks
// card reset // card reset
my_sdmmc_send_command(&deviceSD,0,0); my_sdmmc_send_command(&deviceSD,0,0);
// CMD8 0x1AA // CMD8 0x1AA
my_sdmmc_send_command(&deviceSD,0x10408,0x1AA); my_sdmmc_send_command(&deviceSD,0x10408,0x1AA);
u32 temp = (deviceSD.error & 0x1) << 0x1E; u32 temp = (deviceSD.error & 0x1) << 0x1E;
u32 temp2 = 0; u32 temp2 = 0;
do { do {
do { do {
// CMD55 // CMD55
my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10); my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10);
// ACMD41 // ACMD41
my_sdmmc_send_command(&deviceSD,0x10769,0x00FF8000 | temp); my_sdmmc_send_command(&deviceSD,0x10769,0x00FF8000 | temp);
temp2 = 1; temp2 = 1;
} while ( !(deviceSD.error & 1) ); }
while ( !(deviceSD.error & 1) );
} while((deviceSD.ret[0] & 0x80000000) == 0); }
while ((deviceSD.ret[0] & 0x80000000) == 0);
if(!((deviceSD.ret[0] >> 30) & 1) || !temp) if (!((deviceSD.ret[0] >> 30) & 1) || !temp)
temp2 = 0; temp2 = 0;
deviceSD.isSDHC = temp2; deviceSD.isSDHC = temp2;
my_sdmmc_send_command(&deviceSD,0x10602,0); my_sdmmc_send_command(&deviceSD,0x10602,0);
if (deviceSD.error & 0x4) return -1; if (deviceSD.error & 0x4) return -1;
my_sdmmc_send_command(&deviceSD,0x10403,0); my_sdmmc_send_command(&deviceSD,0x10403,0);
if (deviceSD.error & 0x4) return -1; if (deviceSD.error & 0x4) return -1;
deviceSD.initarg = deviceSD.ret[0] >> 0x10; deviceSD.initarg = deviceSD.ret[0] >> 0x10;
my_sdmmc_send_command(&deviceSD,0x10609,deviceSD.initarg << 0x10); my_sdmmc_send_command(&deviceSD,0x10609,deviceSD.initarg << 0x10);
if (deviceSD.error & 0x4) return -1; if (deviceSD.error & 0x4) return -1;
// Command Class 10 support // Command Class 10 support
const bool cmd6Supported = ((u8*)deviceSD.ret)[10] & 0x40; const bool cmd6Supported = ((u8*)deviceSD.ret)[10] & 0x40;
deviceSD.total_size = calcSDSize((u8*)&deviceSD.ret[0],-1); deviceSD.total_size = calcSDSize((u8*)&deviceSD.ret[0],-1);
setckl(0x201); // 16.756991 MHz setckl(0x201); // 16.756991 MHz
my_sdmmc_send_command(&deviceSD,0x10507,deviceSD.initarg << 0x10); my_sdmmc_send_command(&deviceSD,0x10507,deviceSD.initarg << 0x10);
if (deviceSD.error & 0x4) return -1; if (deviceSD.error & 0x4) return -1;
// CMD55 // CMD55
my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10); my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10);
if (deviceSD.error & 0x4) return -1; if (deviceSD.error & 0x4) return -1;
// ACMD42 // ACMD42
my_sdmmc_send_command(&deviceSD,0x1076A,0x0); my_sdmmc_send_command(&deviceSD,0x1076A,0x0);
if (deviceSD.error & 0x4) return -1; if (deviceSD.error & 0x4) return -1;
// CMD55 // CMD55
my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10); my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10);
if (deviceSD.error & 0x4) return -7; if (deviceSD.error & 0x4) return -7;
deviceSD.SDOPT = 1; deviceSD.SDOPT = 1;
my_sdmmc_send_command(&deviceSD,0x10446,0x2); my_sdmmc_send_command(&deviceSD,0x10446,0x2);
if (deviceSD.error & 0x4) return -8; if (deviceSD.error & 0x4) return -8;
sdmmc_mask16(REG_SDOPT, 0x8000, 0); // Switch to 4 bit mode. sdmmc_mask16(REG_SDOPT, 0x8000, 0); // Switch to 4 bit mode.
// TODO: CMD6 to switch to high speed mode. // TODO: CMD6 to switch to high speed mode.
if(cmd6Supported) if (cmd6Supported)
{ {
sdmmc_write16(REG_SDSTOP,0); sdmmc_write16(REG_SDSTOP,0);
sdmmc_write16(REG_SDBLKLEN32,64); sdmmc_write16(REG_SDBLKLEN32,64);
@ -416,251 +430,267 @@ int my_sdmmc_sdcard_init() {
deviceSD.size = 64; deviceSD.size = 64;
my_sdmmc_send_command(&deviceSD,0x31C06,0x80FFFFF1); my_sdmmc_send_command(&deviceSD,0x31C06,0x80FFFFF1);
sdmmc_write16(REG_SDBLKLEN,512); sdmmc_write16(REG_SDBLKLEN,512);
if(deviceSD.error & 0x4) return -9; if (deviceSD.error & 0x4) return -9;
deviceSD.clk = 0x200; // 33.513982 MHz deviceSD.clk = 0x200; // 33.513982 MHz
setckl(0x200); setckl(0x200);
} }
else deviceSD.clk = 0x201; // 16.756991 MHz else deviceSD.clk = 0x201; // 16.756991 MHz
my_sdmmc_send_command(&deviceSD,0x1040D,deviceSD.initarg << 0x10); my_sdmmc_send_command(&deviceSD,0x1040D,deviceSD.initarg << 0x10);
if (deviceSD.error & 0x4) return -9; if (deviceSD.error & 0x4) return -9;
my_sdmmc_send_command(&deviceSD,0x10410,0x200); my_sdmmc_send_command(&deviceSD,0x10410,0x200);
if (deviceSD.error & 0x4) return -10; 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 {
do { do {
my_sdmmc_send_command(&deviceNAND,0x10701,0x100000); my_sdmmc_send_command(&deviceNAND,0x10701,0x100000);
} while ( !(deviceNAND.error & 1) ); }
} while ( !(deviceNAND.error & 1) );
while((deviceNAND.ret[0] & 0x80000000) == 0); }
while ((deviceNAND.ret[0] & 0x80000000) == 0);
my_sdmmc_send_command(&deviceNAND,0x10602,0x0); my_sdmmc_send_command(&deviceNAND,0x10602,0x0);
if((deviceNAND.error & 0x4))return -1; if ((deviceNAND.error & 0x4))return -1;
my_sdmmc_send_command(&deviceNAND,0x10403,deviceNAND.initarg << 0x10); my_sdmmc_send_command(&deviceNAND,0x10403,deviceNAND.initarg << 0x10);
if((deviceNAND.error & 0x4))return -1; if ((deviceNAND.error & 0x4))return -1;
my_sdmmc_send_command(&deviceNAND,0x10609,deviceNAND.initarg << 0x10); my_sdmmc_send_command(&deviceNAND,0x10609,deviceNAND.initarg << 0x10);
if((deviceNAND.error & 0x4))return -1; if ((deviceNAND.error & 0x4))return -1;
deviceNAND.total_size = calcSDSize((uint8_t*)&deviceNAND.ret[0],0); deviceNAND.total_size = calcSDSize((uint8_t*)&deviceNAND.ret[0],0);
deviceNAND.clk = 1; deviceNAND.clk = 1;
setckl(1); setckl(1);
my_sdmmc_send_command(&deviceNAND,0x10407,deviceNAND.initarg << 0x10); my_sdmmc_send_command(&deviceNAND,0x10407,deviceNAND.initarg << 0x10);
if((deviceNAND.error & 0x4))return -1; if ((deviceNAND.error & 0x4))return -1;
deviceNAND.SDOPT = 1; deviceNAND.SDOPT = 1;
my_sdmmc_send_command(&deviceNAND,0x10506,0x3B70100); my_sdmmc_send_command(&deviceNAND,0x10506,0x3B70100);
if((deviceNAND.error & 0x4))return -1; if ((deviceNAND.error & 0x4))return -1;
my_sdmmc_send_command(&deviceNAND,0x10506,0x3B90100); my_sdmmc_send_command(&deviceNAND,0x10506,0x3B90100);
if((deviceNAND.error & 0x4))return -1; if ((deviceNAND.error & 0x4))return -1;
my_sdmmc_send_command(&deviceNAND,0x1040D,deviceNAND.initarg << 0x10); my_sdmmc_send_command(&deviceNAND,0x1040D,deviceNAND.initarg << 0x10);
if((deviceNAND.error & 0x4))return -1; if ((deviceNAND.error & 0x4))return -1;
my_sdmmc_send_command(&deviceNAND,0x10410,0x200); my_sdmmc_send_command(&deviceNAND,0x10410,0x200);
if((deviceNAND.error & 0x4))return -1; 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); if (device->isSDHC == 0) sector_no <<= 9;
sdmmc_write16(REG_SDSTOP,0x100); my_setTarget(device);
sdmmc_write16(REG_SDSTOP,0x100);
#ifdef DATA32_SUPPORT #ifdef DATA32_SUPPORT
sdmmc_write16(REG_SDBLKCOUNT32,numsectors); sdmmc_write16(REG_SDBLKCOUNT32,numsectors);
sdmmc_write16(REG_SDBLKLEN32,0x200); sdmmc_write16(REG_SDBLKLEN32,0x200);
#endif #endif
sdmmc_write16(REG_SDBLKCOUNT,numsectors); sdmmc_write16(REG_SDBLKCOUNT,numsectors);
device->rData = out; device->rData = out;
device->size = numsectors << 9; device->size = numsectors << 9;
my_sdmmc_send_command(device,0x33C12,sector_no); my_sdmmc_send_command(device,0x33C12,sector_no);
my_setTarget(&deviceSD); my_setTarget(&deviceSD);
return my_geterror(device); 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; if (device->isSDHC == 0)
my_setTarget(device); sector_no <<= 9;
sdmmc_write16(REG_SDSTOP,0x100); my_setTarget(device);
sdmmc_write16(REG_SDSTOP,0x100);
#ifdef DATA32_SUPPORT #ifdef DATA32_SUPPORT
sdmmc_write16(REG_SDBLKCOUNT32,numsectors); sdmmc_write16(REG_SDBLKCOUNT32,numsectors);
sdmmc_write16(REG_SDBLKLEN32,0x200); sdmmc_write16(REG_SDBLKLEN32,0x200);
#endif #endif
sdmmc_write16(REG_SDBLKCOUNT,numsectors); sdmmc_write16(REG_SDBLKCOUNT,numsectors);
device->tData = in; device->tData = in;
device->size = numsectors << 9; device->size = numsectors << 9;
my_sdmmc_send_command(device,0x52C19,sector_no); my_sdmmc_send_command(device,0x52C19,sector_no);
my_setTarget(&deviceSD); my_setTarget(&deviceSD);
return my_geterror(device); 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 // use cmd7 to put sd card in standby mode
// CMD7 // CMD7
my_sdmmc_send_command(device, 0x10507, 0); my_sdmmc_send_command(device, 0x10507, 0);
// get sd card info // get sd card info
// use cmd10 to read CID // use cmd10 to read CID
my_sdmmc_send_command(device, 0x1060A, device->initarg << 0x10); my_sdmmc_send_command(device, 0x1060A, device->initarg << 0x10);
for(int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
cid[i] = device->ret[i]; cid[i] = device->ret[i];
// put sd card back to transfer mode // put sd card back to transfer mode
// CMD7 // CMD7
my_sdmmc_send_command(device, 0x10507, device->initarg << 0x10); 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(); int oldIME = enterCriticalSection();
switch (msg.type) { 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: leaveCriticalSection(oldIME);
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); 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 result = 0;
int oldIME = enterCriticalSection(); int sdflag = 0;
int oldIME = enterCriticalSection();
switch(value) { switch (value)
{
case SDMMC_HAVE_SD:
result = sdmmc_read16(REG_SDSTATUS0);
break;
case SDMMC_HAVE_SD: case SDMMC_SD_START:
result = sdmmc_read16(REG_SDSTATUS0); sdflag = 1;
break; /* 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: case SDMMC_SD_IS_INSERTED:
sdflag = 1; result = my_sdmmc_cardinserted();
/* Falls through. */ break;
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: case SDMMC_SD_STOP:
result = my_sdmmc_cardinserted(); break;
break;
case SDMMC_SD_STOP: case SDMMC_NAND_SIZE:
break; result = deviceNAND.total_size;
break;
}
case SDMMC_NAND_SIZE: leaveCriticalSection(oldIME);
result = deviceNAND.total_size;
break;
}
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);
} }

View File

@ -12,7 +12,7 @@
#define REG_SDPORTSEL 0x02 #define REG_SDPORTSEL 0x02
#define REG_SDCMDARG 0x04 #define REG_SDCMDARG 0x04
#define REG_SDCMDARG0 0x04 #define REG_SDCMDARG0 0x04
#define REG_SDCMDARG1 0x06 #define REG_SDCMDARG1 0x06
#define REG_SDSTOP 0x08 #define REG_SDSTOP 0x08
#define REG_SDRESP 0x0c #define REG_SDRESP 0x0c
#define REG_SDBLKCOUNT 0x0a #define REG_SDBLKCOUNT 0x0a
@ -98,8 +98,8 @@
#define TMIO_MASK_ALL 0x837f031d #define TMIO_MASK_ALL 0x837f031d
#define TMIO_MASK_GW (TMIO_STAT1_ILL_ACCESS | TMIO_STAT1_CMDTIMEOUT | TMIO_STAT1_TXUNDERRUN | TMIO_STAT1_RXOVERFLOW | \ #define TMIO_MASK_GW (TMIO_STAT1_ILL_ACCESS | TMIO_STAT1_CMDTIMEOUT | TMIO_STAT1_TXUNDERRUN | TMIO_STAT1_RXOVERFLOW | \
TMIO_STAT1_DATATIMEOUT | TMIO_STAT1_STOPBIT_ERR | TMIO_STAT1_CRCFAIL | TMIO_STAT1_CMD_IDX_ERR) TMIO_STAT1_DATATIMEOUT | TMIO_STAT1_STOPBIT_ERR | TMIO_STAT1_CRCFAIL | TMIO_STAT1_CMD_IDX_ERR)
#define TMIO_MASK_READOP (TMIO_STAT1_RXRDY | TMIO_STAT1_DATAEND) #define TMIO_MASK_READOP (TMIO_STAT1_RXRDY | TMIO_STAT1_DATAEND)
#define TMIO_MASK_WRITEOP (TMIO_STAT1_TXRQ | TMIO_STAT1_DATAEND) #define TMIO_MASK_WRITEOP (TMIO_STAT1_TXRQ | TMIO_STAT1_DATAEND)
@ -107,25 +107,25 @@
typedef struct mmcdevice { typedef struct mmcdevice {
u8* rData; u8* rData;
const u8* tData; const u8* tData;
u32 size; u32 size;
u32 startOffset; u32 startOffset;
u32 endOffset; u32 endOffset;
u32 error; u32 error;
u16 stat0; u16 stat0;
u16 stat1; u16 stat1;
u32 ret[4]; u32 ret[4];
u32 initarg; u32 initarg;
u32 isSDHC; u32 isSDHC;
u32 clk; u32 clk;
u32 SDOPT; u32 SDOPT;
u32 devicenumber; u32 devicenumber;
u32 total_size; //size in sectors of the device u32 total_size; //size in sectors of the device
u32 res; u32 res;
} mmcdevice; } mmcdevice;
enum { enum {
MMC_DEVICE_SDCARD, MMC_DEVICE_SDCARD,
MMC_DEVICE_NAND, MMC_DEVICE_NAND,
}; };
void my_sdmmc_controller_init(bool force_init); void my_sdmmc_controller_init(bool force_init);
@ -136,12 +136,14 @@ int my_sdmmc_sdcard_init();
int my_sdmmc_nand_init(); int my_sdmmc_nand_init();
void my_sdmmc_get_cid(int devicenumber, u32 *cid); void my_sdmmc_get_cid(int devicenumber, u32 *cid);
static inline void sdmmc_nand_cid( u32 *cid) { static inline void sdmmc_nand_cid( u32 *cid)
my_sdmmc_get_cid(MMC_DEVICE_NAND, cid); {
my_sdmmc_get_cid(MMC_DEVICE_NAND, cid);
} }
static inline void sdmmc_sdcard_cid( u32 *cid) { static inline void sdmmc_sdcard_cid( u32 *cid)
my_sdmmc_get_cid(MMC_DEVICE_SDCARD, cid); {
my_sdmmc_get_cid(MMC_DEVICE_SDCARD, cid);
} }
int my_sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out); int my_sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out);
@ -153,32 +155,37 @@ extern u32 sdmmc_cid[];
extern int sdmmc_curdevice; extern int sdmmc_curdevice;
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
static inline u16 sdmmc_read16(u16 reg) { static inline u16 sdmmc_read16(u16 reg)
//--------------------------------------------------------------------------------- //---------------------------------------------------------------------------------
{
return *(vu16*)(SDMMC_BASE + 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; *(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); u16 val = sdmmc_read16(reg);
val &= ~clear; val &= ~clear;
val |= set; 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, 0x100, 0);
sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100); sdmmc_mask16(REG_SDCLKCTL, 0x2FF, data & 0x2FF);
sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100);
} }
#endif #endif

View File

@ -140,12 +140,12 @@ static void generateList(Menu* m)
{ {
if (count < m->page * ITEMS_PER_PAGE) if (count < m->page * ITEMS_PER_PAGE)
count += 1; count += 1;
else else
{ {
if (m->itemCount >= ITEMS_PER_PAGE) if (m->itemCount >= ITEMS_PER_PAGE)
done = true; done = true;
else else
{ {
char* fpath = (char*)malloc(strlen(BACKUP_PATH) + strlen(ent->d_name) + 8); 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) if (count < m->page * ITEMS_PER_PAGE)
count += 1; count += 1;
else else
{ {
if (m->itemCount >= ITEMS_PER_PAGE) if (m->itemCount >= ITEMS_PER_PAGE)
done = true; done = true;
else else
{ {
char* fpath = (char*)malloc(strlen(BACKUP_PATH) + strlen(ent->d_name) + 8); char* fpath = (char*)malloc(strlen(BACKUP_PATH) + strlen(ent->d_name) + 8);

View File

@ -34,7 +34,7 @@ static bool _patchGameCode(tDSiHeader* h)
//set as standard app //set as standard app
h->tid_high = 0x00030004; h->tid_high = 0x00030004;
do { do {
do { do {
//generate a random game code //generate a random game code
@ -42,7 +42,7 @@ static bool _patchGameCode(tDSiHeader* h)
h->ndshdr.gameCode[i] = 'A' + (rand() % 26); h->ndshdr.gameCode[i] = 'A' + (rand() % 26);
} }
while (h->ndshdr.gameCode[0] == 'A'); //first letter shouldn't be A while (h->ndshdr.gameCode[0] == 'A'); //first letter shouldn't be A
//correct title id //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] ); 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) if (h->ndshdr.reserved1[8] == 0x80)
{ {
iprintf("iQue Hack..."); iprintf("iQue Hack...");
h->ndshdr.reserved1[8] = 0x00; h->ndshdr.reserved1[8] = 0x00;
iprintf("\x1B[42m"); //green iprintf("\x1B[42m"); //green
@ -191,7 +191,7 @@ static void _createPublicSav(tDSiHeader* h, char* dataPath)
fclose(f); fclose(f);
free(publicPath); free(publicPath);
} }
} }
} }
@ -236,7 +236,7 @@ static void _createPrivateSav(tDSiHeader* h, char* dataPath)
fclose(f); fclose(f);
free(privatePath); free(privatePath);
} }
} }
} }
@ -280,7 +280,7 @@ static void _createBannerSav(tDSiHeader* h, char* dataPath)
fclose(f); fclose(f);
free(bannerPath); free(bannerPath);
} }
} }
} }
@ -364,7 +364,7 @@ bool install(char* fpath, bool systemTitle)
//start installation //start installation
clearScreen(&bottomScreen); clearScreen(&bottomScreen);
tDSiHeader* h = getRomHeader(fpath); tDSiHeader* h = getRomHeader(fpath);
if (!h) 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 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"; const char areYouSure[] = "Are you sure you want to install\n";
char* msg = (char*)malloc(strlen(system) + strlen(areYouSure) + strlen(fpath) + 2); 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); sprintf(msg, "%s%s?\n", areYouSure, fpath);
else else
sprintf(msg, "%s%s%s?\n", system, areYouSure, fpath); sprintf(msg, "%s%s%s?\n", system, areYouSure, fpath);
bool choice = choiceBox(msg); bool choice = choiceBox(msg);
free(msg); free(msg);
if (choice == NO) if (choice == NO)
return false; return false;
} }
@ -556,11 +556,11 @@ bool install(char* fpath, bool systemTitle)
//create title directory /title/XXXXXXXX/XXXXXXXX //create title directory /title/XXXXXXXX/XXXXXXXX
char dirPath[32]; char dirPath[32];
mkdir(sdnandMode ? "sd:/title" : "nand:/title", 0777); mkdir(sdnandMode ? "sd:/title" : "nand:/title", 0777);
sprintf(dirPath, "%s:/title/%08x", sdnandMode ? "sd" : "nand", (unsigned int)h->tid_high); sprintf(dirPath, "%s:/title/%08x", sdnandMode ? "sd" : "nand", (unsigned int)h->tid_high);
mkdir(dirPath, 0777); 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 //check if title is free
if (_titleIsUsed(h)) if (_titleIsUsed(h))
@ -779,7 +779,7 @@ bool install(char* fpath, bool systemTitle)
keyWait(KEY_A | KEY_B); keyWait(KEY_A | KEY_B);
goto complete; goto complete;
} }
error: error:
messagePrint("\x1B[31m\nInstallation failed.\n\x1B[47m"); messagePrint("\x1B[31m\nInstallation failed.\n\x1B[47m");

View File

@ -98,7 +98,7 @@ void installMenu()
case INSTALL_MENU_INSTALL: case INSTALL_MENU_INSTALL:
install(m->items[m->cursor].value, false); install(m->items[m->cursor].value, false);
break; break;
case INSTALL_MENU_SYSTEM_TITLE: case INSTALL_MENU_SYSTEM_TITLE:
if (sdnandMode) if (sdnandMode)
install(m->items[m->cursor].value, true); install(m->items[m->cursor].value, true);
@ -114,7 +114,7 @@ void installMenu()
} }
break; break;
case INSTALL_MENU_BACK: case INSTALL_MENU_BACK:
break; break;
} }
} }
@ -154,7 +154,7 @@ static void generateList(Menu* m)
if (currentDir[0] == '\0') if (currentDir[0] == '\0')
dir = opendir("sd:/"); dir = opendir("sd:/");
else else
dir = opendir(currentDir); dir = opendir(currentDir);
if (dir) if (dir)
{ {
@ -169,12 +169,12 @@ static void generateList(Menu* m)
{ {
if (count < m->page * ITEMS_PER_PAGE) if (count < m->page * ITEMS_PER_PAGE)
count += 1; count += 1;
else else
{ {
if (m->itemCount >= ITEMS_PER_PAGE) if (m->itemCount >= ITEMS_PER_PAGE)
done = true; done = true;
else else
{ {
char* fpath = (char*)malloc(strlen(currentDir) + strlen(ent->d_name) + 8); 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) if (count < m->page * ITEMS_PER_PAGE)
count += 1; count += 1;
else else
{ {
if (m->itemCount >= ITEMS_PER_PAGE) if (m->itemCount >= ITEMS_PER_PAGE)
done = true; done = true;
else else
{ {
char* fpath = (char*)malloc(strlen(currentDir) + strlen(ent->d_name) + 8); char* fpath = (char*)malloc(strlen(currentDir) + strlen(ent->d_name) + 8);
@ -281,7 +281,7 @@ static int subMenu()
static bool delete(Menu* m) static bool delete(Menu* m)
{ {
if (!m) return false; if (!m) return false;
char* fpath = m->items[m->cursor].value; char* fpath = m->items[m->cursor].value;
bool result = false; bool result = false;
@ -313,7 +313,7 @@ static bool delete(Menu* m)
{ {
messageBox("\x1B[31mCould not delete file.\x1B[47m"); messageBox("\x1B[31mCould not delete file.\x1B[47m");
} }
} }
} }
return result; return result;

View File

@ -43,8 +43,8 @@ distribution.
//#define TMD_CREATOR_VER "0.2" //#define TMD_CREATOR_VER "0.2"
#define TMD_SIZE 0x208 #define TMD_SIZE 0x208
#define SHA_BUFFER_SIZE 0x200 #define SHA_BUFFER_SIZE 0x200
#define SHA_DIGEST_LENGTH 0x14 #define SHA_DIGEST_LENGTH 0x14
void tmd_create(uint8_t* tmd, FILE* app) 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) // 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; tmd[0x1AA + i] = 0x80;
} }
} }
@ -123,7 +124,7 @@ void tmd_create(uint8_t* tmd, FILE* app)
printProgressBar((float)fileread / (float)filesize); printProgressBar((float)fileread / (float)filesize);
} }
while(buffer_read == SHA_BUFFER_SIZE); while (buffer_read == SHA_BUFFER_SIZE);
clearProgressBar(); clearProgressBar();
consoleSelect(&bottomScreen); consoleSelect(&bottomScreen);
@ -140,7 +141,8 @@ int maketmd(char* input, char* tmdPath)
iprintf("MakeTMD for DSiWare Homebrew\n"); iprintf("MakeTMD for DSiWare Homebrew\n");
iprintf("by Przemyslaw Skryjomski\n\t(Tuxality)\n"); iprintf("by Przemyslaw Skryjomski\n\t(Tuxality)\n");
if(input == NULL || tmdPath == NULL) { if (input == NULL || tmdPath == NULL)
{
iprintf("\x1B[33m"); //yellow iprintf("\x1B[33m"); //yellow
iprintf("\nUsage: %s file.app <file.tmd>\n", "maketmd"); iprintf("\nUsage: %s file.app <file.tmd>\n", "maketmd");
iprintf("\x1B[47m"); //white iprintf("\x1B[47m"); //white
@ -150,7 +152,8 @@ int maketmd(char* input, char* tmdPath)
// APP file (input) // APP file (input)
FILE* app = fopen(input, "rb"); FILE* app = fopen(input, "rb");
if(!app) { if (!app)
{
iprintf("\x1B[31m"); //red iprintf("\x1B[31m"); //red
iprintf("Error at opening %s for reading.\n", input); iprintf("Error at opening %s for reading.\n", input);
iprintf("\x1B[47m"); //white iprintf("\x1B[47m"); //white

View File

@ -4,7 +4,7 @@
Menu* newMenu() Menu* newMenu()
{ {
Menu* m = (Menu*)malloc(sizeof(Menu)); Menu* m = (Menu*)malloc(sizeof(Menu));
m->cursor = 0; m->cursor = 0;
m->page = 0; m->page = 0;
m->itemCount = 0; m->itemCount = 0;
@ -27,7 +27,7 @@ void freeMenu(Menu* m)
if (!m) return; if (!m) return;
clearMenu(m); clearMenu(m);
free(m); free(m);
m = NULL; 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); m->items[i].value = (char*)malloc(strlen(value)+1);
sprintf(m->items[i].value, "%s", value); sprintf(m->items[i].value, "%s", value);
} }
m->itemCount += 1; m->itemCount += 1;
} }
@ -77,7 +77,7 @@ void sortMenuItems(Menu* m)
void setMenuHeader(Menu* m, char* str) void setMenuHeader(Menu* m, char* str)
{ {
if (!m) return; if (!m) return;
if (!str) if (!str)
{ {
m->header[0] = '\0'; m->header[0] = '\0';
@ -153,7 +153,7 @@ void printMenu(Menu* m)
iprintf(" \n"); iprintf(" \n");
} }
//cursor //cursor
iprintf("\x1b[%d;0H>", 2 + m->cursor); iprintf("\x1b[%d;0H>", 2 + m->cursor);
//scroll arrows //scroll arrows
@ -193,7 +193,7 @@ static void _moveCursor(Menu* m, int dir)
{ {
m->cursor = m->itemCount-1; m->cursor = m->itemCount-1;
} }
} }
} }
bool moveCursor(Menu* m) bool moveCursor(Menu* m)

View File

@ -14,7 +14,7 @@ void keyWait(u32 key)
} }
bool choiceBox(char* message) bool choiceBox(char* message)
{ {
const int choiceRow = 10; const int choiceRow = 10;
int cursor = 0; int cursor = 0;

View File

@ -9,9 +9,9 @@
// https://github.com/Jimmy-Z/bfCL/blob/master/dsi.h // https://github.com/Jimmy-Z/bfCL/blob/master/dsi.h
// ported back to 32 bit for ARM9 // ported back to 32 bit for ARM9
static dsi_context nand_ctx ; static dsi_context nand_ctx;
static dsi_context boot2_ctx ; static dsi_context boot2_ctx;
static dsi_es_context es_ctx ; static dsi_es_context es_ctx;
static uint8_t nand_ctr_iv[16]; static uint8_t nand_ctr_iv[16];
static uint8_t boot2_ctr[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) static void generate_key(uint8_t *generated_key, const uint32_t *console_id, const key_mode_t mode)
{ {
uint32_t key[4]; uint32_t key[4];
switch (mode) switch (mode)
{ {
case NAND: case NAND:
key[0] = console_id[0]; key[0] = console_id[0];
key[1] = console_id[0] ^ KEYSEED_DSI_NAND_0; key[1] = console_id[0] ^ KEYSEED_DSI_NAND_0;
key[2] = console_id[1] ^ KEYSEED_DSI_NAND_1; key[2] = console_id[1] ^ KEYSEED_DSI_NAND_1;
key[3] = console_id[1]; key[3] = console_id[1];
break; break;
case NAND_3DS: case NAND_3DS:
key[0] = (console_id[0] ^ KEYSEED_3DS_NAND_0) | 0x80000000; key[0] = (console_id[0] ^ KEYSEED_3DS_NAND_0) | 0x80000000;
key[1] = KEYSEED_3DS_NAND_1; key[1] = KEYSEED_3DS_NAND_1;
key[2] = KEYSEED_3DS_NAND_2; key[2] = KEYSEED_3DS_NAND_2;
key[3] = console_id[1] ^ KEYSEED_3DS_NAND_3; key[3] = console_id[1] ^ KEYSEED_3DS_NAND_3;
break; break;
case ES: case ES:
key[0] = KEYSEED_ES_0; key[0] = KEYSEED_ES_0;
key[1] = KEYSEED_ES_1; key[1] = KEYSEED_ES_1;
key[2] = console_id[1] ^ KEYSEED_ES_2; key[2] = console_id[1] ^ KEYSEED_ES_2;
key[3] = console_id[0]; key[3] = console_id[0];
break; break;
default: default:
break; break;
} }
u128_xor((uint8_t *)key, mode == ES ? DSi_ES_KEY_Y : DSi_NAND_KEY_Y); u128_xor((uint8_t *)key, mode == ES ? DSi_ES_KEY_Y : DSi_NAND_KEY_Y);
u128_add((uint8_t *)key, DSi_KEY_MAGIC); u128_add((uint8_t *)key, DSi_KEY_MAGIC);
u128_lrot((uint8_t *)key, 42) ; u128_lrot((uint8_t *)key, 42);
memcpy(generated_key, key, 16) ; 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]; uint8_t digest[SHA1_LEN];
swiSHA1Calc(digest, data, len); swiSHA1Calc(digest, data, len);
return memcmp(digest, digest_verify, SHA1_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]; uint32_t console_id[2];
GET_UINT32_BE(console_id[0], console_id_be, 4); GET_UINT32_BE(console_id[0], console_id_be, 4);
GET_UINT32_BE(console_id[1], console_id_be, 0); GET_UINT32_BE(console_id[1], console_id_be, 0);
uint8_t key[16]; uint8_t key[16];
generate_key(key, console_id, is3DS ? NAND_3DS : NAND); 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 normalkey[4];
u32 tadsrl_keyX[4] = {0x4E00004A, 0x4A00004E, 0, 0}; 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); F_XY((u8 *)normalkey, (u8 *)tadsrl_keyX, DSi_ES_KEY_Y);
dsi_es_init(&es_ctx, (u8*)normalkey); 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); 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) // crypt one block, in/out must be aligned to 32 bit(restriction induced by xor_128)
// offset as block offset, block as AES block // 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] ; uint8_t ctr[16];
memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)) ; memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv));
u128_add32(ctr, offset); u128_add32(ctr, offset);
dsi_set_ctr(&nand_ctx, ctr) ; dsi_set_ctr(&nand_ctx, ctr);
dsi_crypt_ctr(&nand_ctx, in, out, 16) ; 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] ; uint8_t ctr[16];
memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)) ; memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv));
u128_add32(ctr, offset); u128_add32(ctr, offset);
for (unsigned i = 0; i < count; ++i) for (unsigned i = 0; i < count; ++i)
{ {
dsi_set_ctr(&nand_ctx, ctr) ; dsi_set_ctr(&nand_ctx, ctr);
dsi_crypt_ctr(&nand_ctx, in, out, 16) ; dsi_crypt_ctr(&nand_ctx, in, out, 16);
out += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE;
in += 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) 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); return dsi_es_decrypt(&es_ctx, buf, buf + buf_len - 0x20, buf_len - 0x20);
else else
dsi_es_encrypt(&es_ctx, buf, buf + buf_len - 0x20, buf_len - 0x20); 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; 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++) for (int i=0;i<4;i++)
{ {
boot2_ctr[i] = (size_r) >> (8*i) ; boot2_ctr[i] = (size_r) >> (8*i);
boot2_ctr[i+4] = (-size_r) >> (8*i) ; boot2_ctr[i+4] = (-size_r) >> (8*i);
boot2_ctr[i+8] = (~size_r) >> (8*i) ; boot2_ctr[i+8] = (~size_r) >> (8*i);
boot2_ctr[i+12] = 0 ; boot2_ctr[i+12] = 0;
} }
} }
void dsi_boot2_crypt(uint8_t* out, const uint8_t* in, unsigned count) void dsi_boot2_crypt(uint8_t* out, const uint8_t* in, unsigned count)
{ {
for (unsigned i = 0; i < count; ++i) for (unsigned i = 0; i < count; ++i)
{ {
dsi_set_ctr(&boot2_ctx, boot2_ctr) ; dsi_set_ctr(&boot2_ctx, boot2_ctr);
dsi_crypt_ctr(&boot2_ctx, in, out, 16) ; dsi_crypt_ctr(&boot2_ctx, in, out, 16);
out += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE; in += AES_BLOCK_SIZE;
u128_add32(boot2_ctr, 1); u128_add32(boot2_ctr, 1);
} }
} }

View File

@ -28,7 +28,7 @@ extern "C" {
((uint8_t*)&(n))[1] = (b)[i + 2]; \ ((uint8_t*)&(n))[1] = (b)[i + 2]; \
((uint8_t*)&(n))[2] = (b)[i + 1]; \ ((uint8_t*)&(n))[2] = (b)[i + 1]; \
((uint8_t*)&(n))[3] = (b)[i + 0] ((uint8_t*)&(n))[3] = (b)[i + 0]
#define PUT_UINT32_BE(n, b, i) \ #define PUT_UINT32_BE(n, b, i) \
(b)[i + 0] = ((uint8_t*)&(n))[3]; \ (b)[i + 0] = ((uint8_t*)&(n))[3]; \
(b)[i + 1] = ((uint8_t*)&(n))[2]; \ (b)[i + 1] = ((uint8_t*)&(n))[2]; \

View File

@ -5,7 +5,7 @@
* *
* If you happen to know whom to credit I'd love to add the name * 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. * from tonccpy.
*/ */
@ -16,14 +16,22 @@
/************************ Constants / Defines *********************************/ /************************ Constants / Defines *********************************/
const uint8_t DSi_KEY_MAGIC[16] = { 0x79, 0x3e, 0x4f, 0x1a, 0x5f, 0x0f, 0x68, 0x2a, const uint8_t DSi_KEY_MAGIC[16] = {
0x58, 0x02, 0x59, 0x29, 0x4e, 0xfb, 0xfe, 0xff }; 0x79, 0x3e, 0x4f, 0x1a, 0x5f, 0x0f, 0x68, 0x2a,
const uint8_t DSi_NAND_KEY_Y[16] = { 0x76, 0xdc, 0xb9, 0x0a, 0xd3, 0xc4, 0x4d, 0xbd, 0x58, 0x02, 0x59, 0x29, 0x4e, 0xfb, 0xfe, 0xff
0x1d, 0xdd, 0x2d, 0x20, 0x05, 0x00, 0xa0, 0xe1 }; };
const uint8_t DSi_ES_KEY_Y[16] = { 0xe5, 0xcc, 0x5a, 0x8b, 0x56, 0xd0, 0xc9,0x72, const uint8_t DSi_NAND_KEY_Y[16] = {
0x9c, 0x17, 0xe8, 0xdc, 0x39, 0x12, 0x36, 0xa9 }; 0x76, 0xdc, 0xb9, 0x0a, 0xd3, 0xc4, 0x4d, 0xbd,
const uint8_t DSi_BOOT2_KEY[16] = { 0x98, 0xee, 0x80, 0x80, 0x00, 0x6c, 0xb4, 0xf6, 0x1d, 0xdd, 0x2d, 0x20, 0x05, 0x00, 0xa0, 0xe1
0x3a, 0xc2, 0x6e, 0x62, 0xf9, 0xec, 0x34, 0xad }; };
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 *******************************************/ /************************ 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]; uint8_t key_xy[16];
for(int i=0; i<16; i++) for (int i=0; i<16; i++)
key_xy[i] = key_x[i] ^ key_y[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_add(key, key_xy);
u128_lrot(key, 42); 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_rrot(key_xy, 42);
u128_sub(key_xy, DSi_KEY_MAGIC); u128_sub(key_xy, DSi_KEY_MAGIC);
} }

View File

@ -5,10 +5,10 @@
* *
* If you happen to know whom to credit I'd love to add the name * 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. * from tonccpy.
*/ */
#ifndef _H_F_XY #ifndef _H_F_XY
#define _H_F_XY #define _H_F_XY
@ -18,10 +18,10 @@ extern "C" {
/************************ Constants / Defines *********************************/ /************************ Constants / Defines *********************************/
extern const uint8_t DSi_KEY_MAGIC[16] ; extern const uint8_t DSi_KEY_MAGIC[16];
extern const uint8_t DSi_NAND_KEY_Y[16] ; extern const uint8_t DSi_NAND_KEY_Y[16];
extern const uint8_t DSi_ES_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_BOOT2_KEY[16];
/************************ Function Protoypes **********************************/ /************************ Function Protoypes **********************************/
@ -33,4 +33,3 @@ void F_XY_reverse(const uint8_t *key, uint8_t *key_xy);
#endif #endif
#endif #endif

View File

@ -12,12 +12,12 @@
/************************ Function Protoypes **********************************/ /************************ Function Protoypes **********************************/
bool nandio_startup() ; bool nandio_startup();
bool nandio_is_inserted() ; bool nandio_is_inserted();
bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer) ; 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_write_sectors(sec_t offset, sec_t len, const void *buffer);
bool nandio_clear_status() ; bool nandio_clear_status();
bool nandio_shutdown() ; bool nandio_shutdown();
/************************ Constants / Defines *********************************/ /************************ Constants / Defines *********************************/
@ -46,7 +46,7 @@ static u32 fat_sig_fix_offset = 0;
static u32 sector_buf32[SECTOR_SIZE/sizeof(u32)]; static u32 sector_buf32[SECTOR_SIZE/sizeof(u32)];
static u8 *sector_buf = (u8*)sector_buf32; 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; 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 *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[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) 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 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 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); memcpy(&consoleID[4], &key_x[0xC], 4);
} }
bool nandio_startup() bool nandio_startup()
{ {
if (!nand_Startup()) if (!nand_Startup())
{ {
return false; return false;
} }
nand_ReadSectors(0, 1, sector_buf); nand_ReadSectors(0, 1, sector_buf);
is3DS = parse_ncsd(sector_buf) == 0; is3DS = parse_ncsd(sector_buf) == 0;
@ -83,8 +83,8 @@ bool nandio_startup()
// Get ConsoleID // Get ConsoleID
getConsoleID(consoleID); getConsoleID(consoleID);
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
{ {
consoleIDfixed[i] = consoleID[7-i]; consoleIDfixed[i] = consoleID[7-i];
} }
// iprintf("sector 0 is %s\n", is3DS ? "3DS" : "DSi"); // iprintf("sector 0 is %s\n", is3DS ? "3DS" : "DSi");
@ -94,27 +94,27 @@ bool nandio_startup()
parse_mbr(sector_buf, is3DS); parse_mbr(sector_buf, is3DS);
mbr_t *mbr = (mbr_t*)sector_buf; mbr_t *mbr = (mbr_t*)sector_buf;
nandio_set_fat_sig_fix(is3DS ? 0 : mbr->partitions[0].offset); 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); crypt_buf = (u8*)memalign(32, SECTOR_SIZE * CRYPT_BUF_LEN);
} }
return crypt_buf != 0; return crypt_buf != 0;
} }
bool nandio_is_inserted() bool nandio_is_inserted()
{ {
return true; return true;
} }
// len is guaranteed <= CRYPT_BUF_LEN // 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); dsi_nand_crypt(buffer, crypt_buf, start * SECTOR_SIZE / AES_BLOCK_SIZE, len * SECTOR_SIZE / AES_BLOCK_SIZE);
if (fat_sig_fix_offset && if (fat_sig_fix_offset &&
start == 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'; ((u8*)buffer)[0x38] = 'T';
} }
return true; return true;
} else { }
else
{
return false; return false;
} }
} }
// len is guaranteed <= CRYPT_BUF_LEN // 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] ; static u8 writeCopy[SECTOR_SIZE*16];
memcpy(writeCopy, buffer, len * SECTOR_SIZE) ; memcpy(writeCopy, buffer, len * SECTOR_SIZE);
dsi_nand_crypt(crypt_buf, writeCopy, start * SECTOR_SIZE / AES_BLOCK_SIZE, len * SECTOR_SIZE / AES_BLOCK_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)) if (nand_WriteSectors(start, len, crypt_buf))
{ {
return true; return true;
} else { }
else
{
return false; 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) while (len >= CRYPT_BUF_LEN)
{ {
if (!read_sectors(offset, CRYPT_BUF_LEN, buffer)) if (!read_sectors(offset, CRYPT_BUF_LEN, buffer))
{ {
return false; return false;
} }
offset += CRYPT_BUF_LEN; offset += CRYPT_BUF_LEN;
len -= CRYPT_BUF_LEN; len -= CRYPT_BUF_LEN;
buffer = ((u8*)buffer) + SECTOR_SIZE * CRYPT_BUF_LEN; buffer = ((u8*)buffer) + SECTOR_SIZE * CRYPT_BUF_LEN;
} }
if (len > 0) if (len > 0)
{ {
return read_sectors(offset, len, buffer); return read_sectors(offset, len, buffer);
} else } else
{ {
return true; 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) if (writingLocked)
return false; return false;
nandWritten = true; nandWritten = true;
while (len >= CRYPT_BUF_LEN) while (len >= CRYPT_BUF_LEN)
{ {
if (!write_sectors(offset, CRYPT_BUF_LEN, buffer)) if (!write_sectors(offset, CRYPT_BUF_LEN, buffer))
{ {
return false; return false;
} }
offset += CRYPT_BUF_LEN; offset += CRYPT_BUF_LEN;
len -= CRYPT_BUF_LEN; len -= CRYPT_BUF_LEN;
buffer = ((u8*)buffer) + SECTOR_SIZE * CRYPT_BUF_LEN; buffer = ((u8*)buffer) + SECTOR_SIZE * CRYPT_BUF_LEN;
} }
if (len > 0) if (len > 0)
{ {
return write_sectors(offset, len, buffer); return write_sectors(offset, len, buffer);
} else } else
{ {
return true; return true;
} }
} }
bool nandio_clear_status() bool nandio_clear_status()
{ {
return true; return true;
} }
bool nandio_shutdown() bool nandio_shutdown()
{ {
if (nandWritten) if (nandWritten)
{ {
// at cleanup we synchronize the FAT statgings // 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 // 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 // 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 // and did not push the changes to the other copies
// to do this we read the first partition sector // to do this we read the first partition sector
nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf) ; nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf);
u8 stagingLevels = sector_buf[0x10] ; u8 stagingLevels = sector_buf[0x10];
u8 reservedSectors = sector_buf[0x0E] ; u8 reservedSectors = sector_buf[0x0E];
u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8) ; u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8);
/* /*
iprintf("[i] Staging for %i FAT copies\n",stagingLevels); iprintf("[i] Staging for %i FAT copies\n",stagingLevels);
iprintf("[i] Stages starting at %i\n",reservedSectors); iprintf("[i] Stages starting at %i\n",reservedSectors);
@ -224,12 +228,12 @@ bool nandio_shutdown()
for (u32 sector = 0;sector < sectorsPerFatCopy; sector++) for (u32 sector = 0;sector < sectorsPerFatCopy; sector++)
{ {
// read fat 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 // write to each copy, except the source copy
writingLocked = false; writingLocked = false;
for (int stage = 1;stage < stagingLevels;stage++) 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; writingLocked = true;
} }

View File

@ -18,13 +18,13 @@ extern const DISC_INTERFACE io_dsi_nand;
void nandio_set_fat_sig_fix(uint32_t offset); 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_lock_writing();
extern bool nandio_unlock_writing() ; extern bool nandio_unlock_writing();
extern bool nandio_force_fat_fix() ; extern bool nandio_force_fat_fix();
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -332,10 +332,10 @@ static const unsigned long RCON[10] =
* Forward S-box & tables * Forward S-box & tables
*/ */
static unsigned char FSb[256]; static unsigned char FSb[256];
static unsigned long FT0[256]; static unsigned long FT0[256];
static unsigned long FT1[256]; static unsigned long FT1[256];
static unsigned long FT2[256]; static unsigned long FT2[256];
static unsigned long FT3[256]; static unsigned long FT3[256];
/* /*
* Reverse S-box & tables * 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 ) if( padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 )
return( 0 ); return( 0 );
// If padlock data misaligned, we just fall back to // If padlock data misaligned, we just fall back to
// unaccelerated mode // unaccelerated mode
// //

View File

@ -19,17 +19,17 @@
/************************ Constants / Defines *********************************/ /************************ Constants / Defines *********************************/
static const mbr_partition_t ptable_DSi[MBR_PARTITIONS] = { static const mbr_partition_t ptable_DSi[MBR_PARTITIONS] = {
{0u, {3u, 24u, 4u}, 6u, {15u, 224u, 59u}, 0x00000877u, 0x00066f89u}, {0u, {3u, 24u, 4u}, 6u, {15u, 224u, 59u}, 0x00000877u, 0x00066f89u},
{0u, {2u, 206u, 60u}, 6u, {15u, 224u, 190u}, 0x0006784u, 0x000105b3u}, {0u, {2u, 206u, 60u}, 6u, {15u, 224u, 190u}, 0x0006784u, 0x000105b3u},
{0u, {2u, 222u, 191u}, 1u, {15u, 224u, 191u}, 0x00077e5u, 0x000001a3u}, {0u, {2u, 222u, 191u}, 1u, {15u, 224u, 191u}, 0x00077e5u, 0x000001a3u},
{0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u} {0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u}
}; };
static const mbr_partition_t ptable_3DS[MBR_PARTITIONS] = { static const mbr_partition_t ptable_3DS[MBR_PARTITIONS] = {
{0u, {4u, 24u, 0u}, 6u, {1u, 160u, 63u}, 0x0000009u, 0x00047da9u}, {0u, {4u, 24u, 0u}, 6u, {1u, 160u, 63u}, 0x0000009u, 0x00047da9u},
{0u, {4u, 142u, 64u}, 6u, {1u, 160u, 195u}, 0x0004808u, 0x000105b3u}, {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, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u} {0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u}
}; };
/************************ Functions *******************************************/ /************************ Functions *******************************************/
@ -40,37 +40,38 @@ static const mbr_partition_t ptable_3DS[MBR_PARTITIONS] = {
* - the signature magic * - the signature magic
* - the partition types * - the partition types
* to ensure a valid 3DS ncsd is present * to ensure a valid 3DS ncsd is present
* *
* Return values: * Return values:
* 0: NCSD is a valid * 0: NCSD is a valid
* -1: the signature/magic is invalid * -1: the signature/magic is invalid
* -2: at least one unknown partition type was found * -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; const ncsd_header_t * h = (ncsd_header_t *)sector0;
if (NCSD_MAGIC != h->magic) if (NCSD_MAGIC != h->magic)
{ {
return -1; return -1;
} }
for (unsigned i = 0; i < NCSD_PARTITIONS; ++i) for (unsigned i = 0; i < NCSD_PARTITIONS; ++i)
{ {
unsigned fs_type = h->fs_types[i]; unsigned fs_type = h->fs_types[i];
if (fs_type == 0) if (fs_type == 0)
{ {
break; break;
} }
switch (fs_type) { switch (fs_type)
case 1: {
case 3: case 1:
case 4: case 3:
break; case 4:
default: break;
return -2; default:
} return -2;
} }
return 0; }
return 0;
} }
/*! \brief Sanity check of the MBR /*! \brief Sanity check of the MBR
@ -79,28 +80,28 @@ int parse_ncsd(const uint8_t sector0[SECTOR_SIZE])
* - the signature * - the signature
* - the partition0 values * - the partition0 values
* to ensure a valid DSi main partition can be found * to ensure a valid DSi main partition can be found
* *
* Return values: * Return values:
* 0: MBR is a valid DSi partition * 0: MBR is a valid DSi partition
* -1: the signature is invalid * -1: the signature is invalid
* -2: the first partition does not match expected values * -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_t *m = (mbr_t*)sector0;
const mbr_partition_t *ref_ptable; // reference partition table 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 ((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! // if the signature is invalid, the bootsector shall not be used!
return -1; return -1;
} }
ref_ptable = is3DS?ptable_3DS:ptable_DSi; ref_ptable = is3DS?ptable_3DS:ptable_DSi;
// only test the 1st partition now, we've seen variations on the 3rd partition // only test the 1st partition now, we've seen variations on the 3rd partition
// and after all we only care about the 1st partition // and after all we only care about the 1st partition
if (memcmp(ref_ptable, m->partitions, sizeof(mbr_partition_t))) if (memcmp(ref_ptable, m->partitions, sizeof(mbr_partition_t)))
{ {
return -2; return -2;
} }
return 0; return 0;
} }

View File

@ -35,29 +35,26 @@ extern "C" {
/************************ Structures / Datatypes ******************************/ /************************ Structures / Datatypes ******************************/
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma pack(push, 1) #pragma pack(push, 1)
#define __PACKED #define __PACKED
#elif defined __GNUC__ #elif defined __GNUC__
#define __PACKED __attribute__ ((__packed__)) #define __PACKED __attribute__ ((__packed__))
#endif #endif
typedef struct { typedef struct {
uint32_t offset, uint32_t offset, length;
length;
} __PACKED ncsd_partition_t; } __PACKED ncsd_partition_t;
typedef struct { typedef struct {
uint8_t digest[NCSD_SIGNATURESIZE] ; uint8_t digest[NCSD_SIGNATURESIZE];
} __PACKED ncsd_sginature ; } __PACKED ncsd_sginature;
typedef struct { typedef struct {
ncsd_sginature signature; ncsd_sginature signature;
uint32_t magic, uint32_t magic, size;
size; uint64_t media_id;
uint64_t media_id; uint8_t fs_types[NCSD_PARTITIONS], crypt_types[NCSD_PARTITIONS];
uint8_t fs_types[NCSD_PARTITIONS], ncsd_partition_t partitions[NCSD_PARTITIONS];
crypt_types[NCSD_PARTITIONS];
ncsd_partition_t partitions[NCSD_PARTITIONS];
} __PACKED ncsd_header_t; } __PACKED ncsd_header_t;
/* /*
@ -73,11 +70,11 @@ typedef struct {
* | | | Bit 14..23: Cylinder | * | | | Bit 14..23: Cylinder |
* ---------------------------------------------------------- * ----------------------------------------------------------
*/ */
typedef struct { typedef struct {
uint8_t head; uint8_t head;
uint8_t sectorAndCylHigh; uint8_t sectorAndCylHigh;
uint8_t cylinderLow; uint8_t cylinderLow;
} __PACKED chs_t; } __PACKED chs_t;
/* /*
@ -110,12 +107,12 @@ typedef struct {
*/ */
typedef struct { typedef struct {
uint8_t status; uint8_t status;
chs_t chs_first; chs_t chs_first;
uint8_t type; uint8_t type;
chs_t chs_last; chs_t chs_last;
uint32_t offset; uint32_t offset;
uint32_t length; uint32_t length;
} __PACKED mbr_partition_t; } __PACKED mbr_partition_t;
/* /*
@ -140,13 +137,13 @@ typedef struct {
*/ */
typedef struct { typedef struct {
uint8_t bootstrap[MBR_BOOTSTRAP_SIZE]; uint8_t bootstrap[MBR_BOOTSTRAP_SIZE];
mbr_partition_t partitions[MBR_PARTITIONS]; mbr_partition_t partitions[MBR_PARTITIONS];
uint8_t boot_signature[2]; uint8_t boot_signature[2];
} __PACKED mbr_t; } __PACKED mbr_t;
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma pack(pop) #pragma pack(pop)
#endif #endif
#undef __PACKED #undef __PACKED
@ -159,11 +156,9 @@ int parse_mbr(const uint8_t sector0[SECTOR_SIZE], const int is3DS);
/************************ static code verification ****************************/ /************************ static code verification ****************************/
static_assert(sizeof(ncsd_header_t) == NCSD_HEADERSIZE, static_assert(sizeof(ncsd_header_t) == NCSD_HEADERSIZE, "sizeof(ncsd_header_t) should equal 0x160");
"sizeof(ncsd_header_t) should equal 0x160"); static_assert(sizeof(mbr_t) == SECTOR_SIZE, "sizeof(mbr_t) should equal 0x200");
static_assert(sizeof(mbr_t) == SECTOR_SIZE,
"sizeof(mbr_t) should equal 0x200");
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -5,80 +5,70 @@
#include <time.h> #include <time.h>
#include "u128_math.h" #include "u128_math.h"
void dsi_set_key( dsi_context* ctx, void dsi_set_key(dsi_context* ctx, const unsigned char key[16])
const unsigned char key[16] )
{ {
unsigned char keyswap[16]; unsigned char keyswap[16];
u128_swap(keyswap, key) ; u128_swap(keyswap, key);
aes_setkey_enc(&ctx->aes, keyswap, 128); aes_setkey_enc(&ctx->aes, keyswap, 128);
} }
void dsi_add_ctr( dsi_context* ctx, void dsi_add_ctr(dsi_context* ctx, unsigned int carry)
unsigned int carry) {
{ unsigned int counter[4];
unsigned int counter[4]; unsigned char *outctr = (unsigned char*)ctx->ctr;
unsigned char *outctr = (unsigned char*)ctx->ctr; int sum;
int sum; signed int i;
signed int i;
for(i = 0; i < 4; i++) for (i = 0; i < 4; i++)
counter[i] = (outctr[i * 4 + 0] << 24) | (outctr[i * 4 + 1] << 16) | counter[i] = (outctr[i * 4 + 0] << 24) | (outctr[i * 4 + 1] << 16) |
(outctr[i * 4 + 2] << 8) | (outctr[i * 4 + 3] << 0); (outctr[i * 4 + 2] << 8) | (outctr[i * 4 + 3] << 0);
for(i = 3; i >= 0; i--) for (i = 3; i >= 0; i--)
{ {
sum = counter[i] + carry; sum = counter[i] + carry;
if (sum < counter[i]) if (sum < counter[i])
carry = 1; carry = 1;
else else
carry = 0; carry = 0;
counter[i] = sum; counter[i] = sum;
} }
for(i = 0; i < 4; i++) for (i = 0; i < 4; i++)
{ {
outctr[i * 4 + 0] = counter[i] >> 24; outctr[i * 4 + 0] = counter[i] >> 24;
outctr[i * 4 + 1] = counter[i] >> 16; outctr[i * 4 + 1] = counter[i] >> 16;
outctr[i * 4 + 2] = counter[i] >> 8; outctr[i * 4 + 2] = counter[i] >> 8;
outctr[i * 4 + 3] = counter[i] >> 0; outctr[i * 4 + 3] = counter[i] >> 0;
} }
} }
void dsi_set_ctr( dsi_context* ctx, void dsi_set_ctr(dsi_context* ctx, const unsigned char ctr[16])
const unsigned char ctr[16] )
{ {
int i; int i;
for(i=0; i<16; i++) for (i=0; i<16; i++)
ctx->ctr[i] = ctr[15-i]; ctx->ctr[i] = ctr[15-i];
} }
void dsi_init_ctr( dsi_context* ctx, void dsi_init_ctr(dsi_context* ctx, const unsigned char key[16], const unsigned char ctr[12])
const unsigned char key[16],
const unsigned char ctr[12] )
{ {
dsi_set_key(ctx, key); dsi_set_key(ctx, key);
dsi_set_ctr(ctx, ctr); dsi_set_ctr(ctx, ctr);
} }
void dsi_crypt_ctr( dsi_context* ctx, void dsi_crypt_ctr(dsi_context* ctx, const void* in, void* out, unsigned int len)
const void* in,
void* out,
unsigned int len)
{ {
unsigned int i; unsigned int i;
for(i = 0; i < len; i += 0x10) 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, void dsi_crypt_ctr_block(dsi_context* ctx, const unsigned char input[16], unsigned char output[16])
const unsigned char input[16],
unsigned char output[16] )
{ {
int i; int i;
unsigned char stream[16]; unsigned char stream[16];
@ -89,14 +79,14 @@ void dsi_crypt_ctr_block( dsi_context* ctx,
if (input) if (input)
{ {
for(i=0; i<16; i++) for (i=0; i<16; i++)
{ {
output[i] = stream[15-i] ^ input[i]; output[i] = stream[15-i] ^ input[i];
} }
} }
else else
{ {
for(i=0; i<16; i++) for (i=0; i<16; i++)
output[i] = stream[15-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, void dsi_init_ccm(dsi_context* ctx, unsigned char key[16], unsigned int maclength,
unsigned char key[16], unsigned int payloadlength, unsigned int assoclength, unsigned char nonce[12])
unsigned int maclength,
unsigned int payloadlength,
unsigned int assoclength,
unsigned char nonce[12] )
{ {
int i; int i;
@ -128,7 +114,7 @@ void dsi_init_ccm( dsi_context* ctx,
ctx->mac[0] = (maclength<<3) | 2; ctx->mac[0] = (maclength<<3) | 2;
if (assoclength) if (assoclength)
ctx->mac[0] |= (1<<6); 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[1+i] = nonce[11-i];
ctx->mac[13] = payloadlength>>16; ctx->mac[13] = payloadlength>>16;
ctx->mac[14] = payloadlength>>8; ctx->mac[14] = payloadlength>>8;
@ -139,7 +125,7 @@ void dsi_init_ccm( dsi_context* ctx,
// CCM CTR: // CCM CTR:
// [1-byte flags] [12-byte nonce] [3-byte ctr] // [1-byte flags] [12-byte nonce] [3-byte ctr]
ctx->ctr[0] = 2; 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[1+i] = nonce[11-i];
ctx->ctr[13] = 0; ctx->ctr[13] = 0;
ctx->ctr[14] = 0; ctx->ctr[14] = 0;
@ -148,21 +134,18 @@ void dsi_init_ccm( dsi_context* ctx,
dsi_crypt_ctr_block(ctx, 0, ctx->S0); dsi_crypt_ctr_block(ctx, 0, ctx->S0);
} }
void dsi_encrypt_ccm_block( dsi_context* ctx, void dsi_encrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac)
unsigned char input[16],
unsigned char output[16],
unsigned char* mac )
{ {
int i; int i;
for(i=0; i<16; i++) for (i=0; i<16; i++)
ctx->mac[i] ^= input[15-i]; ctx->mac[i] ^= input[15-i];
aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->mac, ctx->mac); aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->mac, ctx->mac);
if (mac) if (mac)
{ {
for(i=0; i<16; i++) for (i=0; i<16; i++)
mac[i] = ctx->mac[15-i] ^ ctx->S0[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, void dsi_decrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac)
unsigned char input[16],
unsigned char output[16],
unsigned char* mac )
{ {
int i; int i;
@ -184,12 +164,12 @@ void dsi_decrypt_ccm_block( dsi_context* ctx,
dsi_crypt_ctr_block(ctx, input, output); 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]; ctx->mac[i] ^= output[15-i];
} }
else else
{ {
for(i=0; i<16; i++) for (i=0; i<16; i++)
ctx->mac[i] ^= input[15-i]; ctx->mac[i] ^= input[15-i];
} }
@ -198,22 +178,18 @@ void dsi_decrypt_ccm_block( dsi_context* ctx,
if (mac) if (mac)
{ {
for(i=0; i<16; i++) for (i=0; i<16; i++)
mac[i] = ctx->mac[15-i] ^ ctx->S0[i]; mac[i] = ctx->mac[15-i] ^ ctx->S0[i];
} }
} }
void dsi_decrypt_ccm( dsi_context* ctx, void dsi_decrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac)
unsigned char* input,
unsigned char* output,
unsigned int size,
unsigned char* mac )
{ {
unsigned char block[16]; unsigned char block[16];
unsigned char ctr[16]; unsigned char ctr[16];
while(size > 16) while (size > 16)
{ {
dsi_decrypt_ccm_block(ctx, input, output, mac); dsi_decrypt_ccm_block(ctx, input, output, mac);
@ -226,7 +202,7 @@ void dsi_decrypt_ccm( dsi_context* ctx,
} }
memcpy(ctr, ctx->ctr, 16); memcpy(ctr, ctx->ctr, 16);
memset(block, 0, 16); memset(block, 0, 16);
dsi_crypt_ctr_block(ctx, block, block); dsi_crypt_ctr_block(ctx, block, block);
memcpy(ctx->ctr, ctr, 16); memcpy(ctx->ctr, ctr, 16);
memcpy(block, input, size); memcpy(block, input, size);
@ -237,15 +213,11 @@ void dsi_decrypt_ccm( dsi_context* ctx,
} }
void dsi_encrypt_ccm( dsi_context* ctx, void dsi_encrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac)
unsigned char* input,
unsigned char* output,
unsigned int size,
unsigned char* mac )
{ {
unsigned char block[16]; unsigned char block[16];
while(size > 16) while (size > 16)
{ {
dsi_encrypt_ccm_block(ctx, input, output, mac); dsi_encrypt_ccm_block(ctx, input, output, mac);
@ -263,30 +235,25 @@ void dsi_encrypt_ccm( dsi_context* ctx,
memcpy(output, block, size); memcpy(output, block, size);
} }
void dsi_es_init( dsi_es_context* ctx, void dsi_es_init(dsi_es_context* ctx, unsigned char key[16])
unsigned char key[16] )
{ {
memcpy(ctx->key, key, 16); memcpy(ctx->key, key, 16);
ctx->randomnonce = 1; ctx->randomnonce = 1;
} }
void dsi_es_set_nonce( dsi_es_context* ctx, void dsi_es_set_nonce(dsi_es_context* ctx, unsigned char nonce[12])
unsigned char nonce[12] )
{ {
memcpy(ctx->nonce, nonce, 12); memcpy(ctx->nonce, nonce, 12);
ctx->randomnonce = 0; 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; ctx->randomnonce = 1;
} }
int dsi_es_decrypt( dsi_es_context* ctx,
unsigned char* buffer, int dsi_es_decrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size)
unsigned char metablock[32],
unsigned int size )
{ {
unsigned char ctr[16]; unsigned char ctr[16];
unsigned char nonce[12]; 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); chksize = (scratchpad[13]<<16) | (scratchpad[14]<<8) | (scratchpad[15]<<0);
if (scratchpad[0] != 0x3A) if (scratchpad[0] != 0x3A)
{ {
return -1; return -1;
} }
if (chksize != size) if (chksize != size)
{ {
return -2; return -2;
} }
memcpy(nonce, metablock + 17, 12); 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); dsi_decrypt_ccm(&cryptoctx, buffer, buffer, size, genmac);
if (memcmp(genmac, chkmac, 16) != 0) if (memcmp(genmac, chkmac, 16) != 0)
{ {
return -3; return -3;
} }
return 0; return 0;
} }
void dsi_es_encrypt( dsi_es_context* ctx, void dsi_es_encrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size)
unsigned char* buffer,
unsigned char metablock[32],
unsigned int size )
{ {
int i; int i;
unsigned char nonce[12]; unsigned char nonce[12];
@ -348,9 +312,9 @@ void dsi_es_encrypt( dsi_es_context* ctx,
if (ctx->randomnonce) 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(); nonce[i] = rand();
} }
else else

View File

@ -10,7 +10,7 @@ typedef struct
unsigned char S0[16]; unsigned char S0[16];
unsigned int maclen; unsigned int maclen;
aes_context aes; aes_context aes;
} }
dsi_context; dsi_context;
@ -27,75 +27,39 @@ typedef struct
extern "C" { extern "C" {
#endif #endif
void dsi_set_key( dsi_context* ctx, void dsi_set_key(dsi_context* ctx, const unsigned char key[16]);
const unsigned char key[16] );
void dsi_add_ctr( dsi_context* ctx, void dsi_add_ctr(dsi_context* ctx, unsigned int carry);
unsigned int carry );
void dsi_set_ctr( dsi_context* ctx, void dsi_set_ctr(dsi_context* ctx, const unsigned char ctr[16]);
const unsigned char ctr[16] );
void dsi_init_ctr( dsi_context* ctx, void dsi_init_ctr(dsi_context* ctx, const unsigned char key[16], const unsigned char ctr[12]);
const unsigned char key[16],
const unsigned char ctr[12] );
void dsi_crypt_ctr( dsi_context* ctx, void dsi_crypt_ctr(dsi_context* ctx, const void* in, void* out, unsigned int len);
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_init_ccm( dsi_context* ctx, void dsi_crypt_ctr_block(dsi_context* ctx, const unsigned char input[16], unsigned char output[16]);
unsigned char key[16],
unsigned int maclength,
unsigned int payloadlength,
unsigned int assoclength,
unsigned char nonce[12] );
void dsi_encrypt_ccm_block( dsi_context* ctx, void dsi_init_ccm(dsi_context* ctx, unsigned char key[16], unsigned int maclength,
unsigned char input[16], unsigned int payloadlength, unsigned int assoclength, unsigned char nonce[12]);
unsigned char output[16],
unsigned char* mac );
void dsi_decrypt_ccm_block( dsi_context* ctx, void dsi_encrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac);
unsigned char input[16],
unsigned char output[16], void dsi_decrypt_ccm_block(dsi_context* ctx, unsigned char input[16], unsigned char output[16], unsigned char* mac);
unsigned char* mac );
void dsi_decrypt_ccm( dsi_context* ctx, void dsi_decrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac);
unsigned char* input,
unsigned char* output,
unsigned int size,
unsigned char* mac );
void dsi_encrypt_ccm( dsi_context* ctx, void dsi_encrypt_ccm(dsi_context* ctx, unsigned char* input, unsigned char* output, unsigned int size, unsigned char* mac);
unsigned char* input,
unsigned char* output,
unsigned int size,
unsigned char* mac );
void dsi_es_init( dsi_es_context* ctx, void dsi_es_init(dsi_es_context* ctx, unsigned char key[16]);
unsigned char key[16] );
void dsi_es_set_nonce( dsi_es_context* ctx, void dsi_es_set_nonce(dsi_es_context* ctx, unsigned char nonce[12]);
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, int dsi_es_decrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size);
unsigned char* buffer,
unsigned char metablock[32],
unsigned int size );
void dsi_es_encrypt( dsi_es_context* ctx, void dsi_es_encrypt(dsi_es_context* ctx, unsigned char* buffer, unsigned char metablock[32], unsigned int size);
unsigned char* buffer,
unsigned char metablock[32],
unsigned int size );
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -6,101 +6,101 @@
// rotate a 128bit, little endian by shift bits in direction of increasing significance. // rotate a 128bit, little endian by shift bits in direction of increasing significance.
void u128_lrot(uint8_t *num, uint32_t shift) void u128_lrot(uint8_t *num, uint32_t shift)
{ {
uint8_t tmp[16]; uint8_t tmp[16];
for (int i=0;i<16;i++) for (int i=0;i<16;i++)
{ {
// rot: rotate to more significant. // rot: rotate to more significant.
// LSB is tmp[0], MSB is tmp[15] // LSB is tmp[0], MSB is tmp[15]
const uint32_t byteshift = shift / 8 ; const uint32_t byteshift = shift / 8;
const uint32_t bitshift = shift % 8; const uint32_t bitshift = shift % 8;
tmp[(i+byteshift) % 16] = (num[i] << bitshift) tmp[(i+byteshift) % 16] = (num[i] << bitshift)
| ((num[(i+16-1) % 16] >> (8-bitshift)) & 0xff); | ((num[(i+16-1) % 16] >> (8-bitshift)) & 0xff);
} }
memcpy(num, tmp, 16) ; memcpy(num, tmp, 16);
} }
// rotate a 128bit, little endian by shift bits in direction of decreasing significance. // rotate a 128bit, little endian by shift bits in direction of decreasing significance.
void u128_rrot(uint8_t *num, uint32_t shift) void u128_rrot(uint8_t *num, uint32_t shift)
{ {
uint8_t tmp[16]; uint8_t tmp[16];
for (int i=0;i<16;i++) for (int i=0;i<16;i++)
{ {
// rot: rotate to less significant. // rot: rotate to less significant.
// LSB is tmp[0], MSB is tmp[15] // LSB is tmp[0], MSB is tmp[15]
const uint32_t byteshift = shift / 8 ; const uint32_t byteshift = shift / 8;
const uint32_t bitshift = shift % 8; const uint32_t bitshift = shift % 8;
tmp[i] = (num[(i+byteshift) % 16] >> bitshift) tmp[i] = (num[(i+byteshift) % 16] >> bitshift)
| ((num[(i+byteshift+1) % 16] << (8-bitshift)) & 0xff); | ((num[(i+byteshift+1) % 16] << (8-bitshift)) & 0xff);
} }
memcpy(num, tmp, 16) ; memcpy(num, tmp, 16);
} }
// xor two 128bit, little endian values and store the result into the first // xor two 128bit, little endian values and store the result into the first
void u128_xor(uint8_t *a, const uint8_t *b) void u128_xor(uint8_t *a, const uint8_t *b)
{ {
for (int i=0;i<16;i++) for (int i=0;i<16;i++)
{ {
a[i] = a[i] ^ b[i] ; a[i] = a[i] ^ b[i];
} }
} }
// or two 128bit, little endian values and store the result into the first // or two 128bit, little endian values and store the result into the first
void u128_or(uint8_t *a, const uint8_t *b) void u128_or(uint8_t *a, const uint8_t *b)
{ {
for (int i=0;i<16;i++) for (int i=0;i<16;i++)
{ {
a[i] = a[i] | b[i] ; a[i] = a[i] | b[i];
} }
} }
// and two 128bit, little endian values and store the result into the first // and two 128bit, little endian values and store the result into the first
void u128_and(uint8_t *a, const uint8_t *b) void u128_and(uint8_t *a, const uint8_t *b)
{ {
for (int i=0;i<16;i++) for (int i=0;i<16;i++)
{ {
a[i] = a[i] & b[i] ; a[i] = a[i] & b[i];
} }
} }
// add two 128bit, little endian values and store the result into the first // add two 128bit, little endian values and store the result into the first
void u128_add(uint8_t *a, const uint8_t *b) void u128_add(uint8_t *a, const uint8_t *b)
{ {
uint8_t carry = 0 ; uint8_t carry = 0;
for (int i=0;i<16;i++) for (int i=0;i<16;i++)
{ {
uint16_t sum = a[i] + b[i] + carry ; uint16_t sum = a[i] + b[i] + carry;
a[i] = sum & 0xff ; a[i] = sum & 0xff;
carry = sum >> 8 ; carry = sum >> 8;
} }
} }
// add two 128bit, little endian values and store the result into the first // add two 128bit, little endian values and store the result into the first
void u128_add32(uint8_t *a, const uint32_t b) void u128_add32(uint8_t *a, const uint32_t b)
{ {
uint8_t _b[16]; uint8_t _b[16];
memset(_b, 0, sizeof(_b)) ; memset(_b, 0, sizeof(_b));
for (int i=0;i<4;i++) for (int i=0;i<4;i++)
_b[i] = b >> (i*8) ; _b[i] = b >> (i*8);
u128_add(a, _b); u128_add(a, _b);
} }
// sub two 128bit, little endian values and store the result into the first // sub two 128bit, little endian values and store the result into the first
void u128_sub(uint8_t *a, const uint8_t *b) void u128_sub(uint8_t *a, const uint8_t *b)
{ {
uint8_t carry = 0 ; uint8_t carry = 0;
for (int i=0;i<16;i++) for (int i=0;i<16;i++)
{ {
uint16_t sub = a[i] - b[i] - (carry & 1); uint16_t sub = a[i] - b[i] - (carry & 1);
a[i] = sub & 0xff ; a[i] = sub & 0xff;
carry = sub >> 8 ; 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++) for (int i=0;i<16;i++)
{ {
out[15-i] = in[i]; out[15-i] = in[i];
} }
} }

View File

@ -7,32 +7,32 @@ extern "C" {
#endif #endif
// left rotate by (shift % 128) bits // 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 // 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 // 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 // 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 // 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 // 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 // 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 // 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 // swap byte order
void u128_swap(uint8_t *out, const uint8_t *in) ; void u128_swap(uint8_t *out, const uint8_t *in);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -61,7 +61,7 @@ tNDSBanner* getRomBanner(char const* fpath)
} }
free(h); free(h);
fclose(f); fclose(f);
} }
return b; return b;
@ -112,7 +112,7 @@ bool getGameTitlePath(char const* fpath, char* out, bool full)
tNDSBanner* b = getRomBanner(fpath); tNDSBanner* b = getRomBanner(fpath);
bool result = getGameTitle(b, out, full); bool result = getGameTitle(b, out, full);
free(b); free(b);
return result; return result;
} }
@ -134,7 +134,7 @@ bool getRomCode(tDSiHeader* h, char* out)
sprintf(out, "%.4s", h->ndshdr.gameCode); sprintf(out, "%.4s", h->ndshdr.gameCode);
return true; return true;
} }
void printRomInfo(char const* fpath) void printRomInfo(char const* fpath)
@ -184,9 +184,9 @@ void printRomInfo(char const* fpath)
switch (h->ndshdr.unitCode) switch (h->ndshdr.unitCode)
{ {
case 0: iprintf("NDS"); break; case 0: iprintf("NDS"); break;
case 2: iprintf("NDS+DSi"); break; case 2: iprintf("NDS+DSi"); break;
case 3: iprintf("DSi"); break; case 3: iprintf("DSi"); break;
default: iprintf("unknown"); default: iprintf("unknown");
} }
@ -199,8 +199,8 @@ void printRomInfo(char const* fpath)
switch (h->ndshdr.reserved1[7]) switch (h->ndshdr.reserved1[7])
{ {
case 0x3: iprintf("Normal"); break; case 0x3: iprintf("Normal"); break;
case 0xB: iprintf("Sys"); break; case 0xB: iprintf("Sys"); break;
case 0xF: iprintf("Debug/Sys"); break; case 0xF: iprintf("Debug/Sys"); break;
default: iprintf("unknown"); default: iprintf("unknown");
} }
@ -216,7 +216,7 @@ void printRomInfo(char const* fpath)
h->tid_high == 0x00030017 || h->tid_high == 0x00030017 ||
h->tid_high == 0x00030000) 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);
} }
} }

View File

@ -6,13 +6,13 @@ static u32 _getClusterSize(u32 sizebytes)
{ {
if (sizebytes < 573440) if (sizebytes < 573440)
return 512; return 512;
else if (sizebytes < 5472256) else if (sizebytes < 5472256)
return 2048; return 2048;
else if (sizebytes < 17301504) else if (sizebytes < 17301504)
return 4096; return 4096;
else else
return 2048; return 2048;
} }
@ -21,7 +21,7 @@ static u16 _getMaxFiles(u32 sizebytes)
{ {
if (sizebytes < 573440) if (sizebytes < 573440)
return 16; return 16;
else else
return 256; return 256;
} }
@ -52,17 +52,17 @@ bool initFatHeader(FILE* f)
{ {
if (!f) if (!f)
return false; return false;
//get size //get size
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
u32 size = ftell(f); u32 size = ftell(f);
FATHeader* h = (FATHeader*)malloc(sizeof(FATHeader)); FATHeader* h = (FATHeader*)malloc(sizeof(FATHeader));
h->BS_JmpBoot[0] = 0xE9; h->BS_JmpBoot[0] = 0xE9;
h->BS_JmpBoot[1] = 0; h->BS_JmpBoot[1] = 0;
h->BS_JmpBoot[2] = 0; h->BS_JmpBoot[2] = 0;
h->BS_OEMName[0] = 'M'; h->BS_OEMName[0] = 'M';
h->BS_OEMName[1] = 'S'; h->BS_OEMName[1] = 'S';
h->BS_OEMName[2] = 'W'; h->BS_OEMName[2] = 'W';
@ -71,14 +71,14 @@ bool initFatHeader(FILE* f)
h->BS_OEMName[5] = '4'; h->BS_OEMName[5] = '4';
h->BS_OEMName[6] = '.'; h->BS_OEMName[6] = '.';
h->BS_OEMName[7] = '1'; h->BS_OEMName[7] = '1';
h->BPB_BytesPerSec = 512; h->BPB_BytesPerSec = 512;
h->BPB_SecPerClus = _getClusterSize(size) / h->BPB_BytesPerSec; h->BPB_SecPerClus = _getClusterSize(size) / h->BPB_BytesPerSec;
h->BPB_RsvdSecCnt = 1; h->BPB_RsvdSecCnt = 1;
h->BPB_NumFATs = 2; h->BPB_NumFATs = 2;
h->BPB_RootEntCnt = _getMaxFiles(size) * 2; h->BPB_RootEntCnt = _getMaxFiles(size) * 2;
h->BPB_TotSec16 = _getTotSec16(size, h->BPB_BytesPerSec); // h->BPB_TotSec16 = _getTotSec16(size, h->BPB_BytesPerSec); //
h->BPB_Media = 0xF8; h->BPB_Media = 0xF8;
h->BPB_FATSz16 = _getFatz(size); // h->BPB_FATSz16 = _getFatz(size); //
h->BPB_SecPerTrk = 0; h->BPB_SecPerTrk = 0;
h->BPB_NumHeads = 0; h->BPB_NumHeads = 0;
@ -88,7 +88,7 @@ bool initFatHeader(FILE* f)
h->BS_Reserved1 = 0; h->BS_Reserved1 = 0;
h->BS_BootSig = 0x29; h->BS_BootSig = 0x29;
h->BS_VolID = 305419896; h->BS_VolID = 305419896;
h->BS_VolLab[0] = 'V'; h->BS_VolLab[0] = 'V';
h->BS_VolLab[1] = 'O'; h->BS_VolLab[1] = 'O';
h->BS_VolLab[2] = 'L'; h->BS_VolLab[2] = 'L';
@ -100,7 +100,7 @@ bool initFatHeader(FILE* f)
h->BS_VolLab[8] = 'B'; h->BS_VolLab[8] = 'B';
h->BS_VolLab[9] = 'E'; h->BS_VolLab[9] = 'E';
h->BS_VolLab[10] = 'L'; h->BS_VolLab[10] = 'L';
h->BS_FilSysType[0] = 'F'; h->BS_FilSysType[0] = 'F';
h->BS_FilSysType[1] = 'A'; h->BS_FilSysType[1] = 'A';
h->BS_FilSysType[2] = 'T'; h->BS_FilSysType[2] = 'T';
@ -109,15 +109,15 @@ bool initFatHeader(FILE* f)
h->BS_FilSysType[5] = ' '; h->BS_FilSysType[5] = ' ';
h->BS_FilSysType[6] = ' '; h->BS_FilSysType[6] = ' ';
h->BS_FilSysType[7] = ' '; h->BS_FilSysType[7] = ' ';
memset(h->BS_BootCode, 0, 448); memset(h->BS_BootCode, 0, 448);
h->BS_BootSign = 0xAA55; h->BS_BootSign = 0xAA55;
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
fwrite(h, sizeof(FATHeader), 1, f); fwrite(h, sizeof(FATHeader), 1, f);
free(h); free(h);
return true; return true;
} }

View File

@ -48,15 +48,15 @@ void printProgressBar(float percent)
//Print bars //Print bars
if (bars > 0) if (bars > 0)
{ {
for (int i = 0; i < bars; i++) for (int i = 0; i < bars; i++)
iprintf("\x1b[23;%dH|", 1 + i); iprintf("\x1b[23;%dH|", 1 + i);
} }
lastBars = bars; lastBars = bars;
iprintf("\x1B[47m"); //white iprintf("\x1B[47m"); //white
} }
} }
void clearProgressBar() void clearProgressBar()
@ -150,7 +150,7 @@ int copyFilePart(char const* src, u32 offset, u32 size, char const* dst)
} }
fclose(fin); fclose(fin);
return 0; return 0;
} }
unsigned long long getFileSize(FILE* f) unsigned long long getFileSize(FILE* f)
@ -257,7 +257,7 @@ bool copyDir(char const* src, char const* dst)
iprintf("%s -> \n%s...", fsrc, fdst); iprintf("%s -> \n%s...", fsrc, fdst);
int ret = copyFile(fsrc, fdst); int ret = copyFile(fsrc, fdst);
if (ret != 0) if (ret != 0)
{ {
iprintf("\x1B[31m"); //red iprintf("\x1B[31m"); //red
@ -407,9 +407,9 @@ unsigned long long getDirSize(const char* path, u32 blockSize)
size += getDirSize(fullpath, blockSize); size += getDirSize(fullpath, blockSize);
} }
else else
{ {
char fullpath[260]; char fullpath[260];
sprintf(fullpath, "%s/%s", path, ent->d_name); sprintf(fullpath, "%s/%s", path, ent->d_name);
size += getFileSizePath(fullpath); size += getFileSizePath(fullpath);
@ -444,32 +444,32 @@ int getMenuSlotsFree()
}; };
int freeSlots = getMenuSlots(); int freeSlots = getMenuSlots();
DIR* dir; DIR* dir;
struct dirent* ent; struct dirent* ent;
for (int i = 0; i < NUM_OF_DIRS; i++) for (int i = 0; i < NUM_OF_DIRS; i++)
{ {
char path[256]; char path[256];
sprintf(path, "%s:/title/%s", sdnandMode ? "sd" : "nand", dirs[i]); sprintf(path, "%s:/title/%s", sdnandMode ? "sd" : "nand", dirs[i]);
dir = opendir(path); dir = opendir(path);
if (dir) if (dir)
{ {
while ( (ent = readdir(dir)) != NULL ) while ( (ent = readdir(dir)) != NULL )
{ {
if (strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0) if (strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0)
continue; continue;
if (ent->d_type == DT_DIR) if (ent->d_type == DT_DIR)
freeSlots -= 1; freeSlots -= 1;
} }
} }
closedir(dir); closedir(dir);
} }
return freeSlots; return freeSlots;
} }

View File

@ -39,7 +39,7 @@ void testMenu()
swiWaitForVBlank(); swiWaitForVBlank();
unsigned long long sdsize = getSDCardSize(); unsigned long long sdsize = getSDCardSize();
printBytes(sdsize); printBytes(sdsize);
iprintf("\n"); iprintf("\n");
swiWaitForVBlank(); swiWaitForVBlank();

View File

@ -62,11 +62,11 @@ void titleMenu()
{ {
resetMenu(m); resetMenu(m);
generateList(m); generateList(m);
} }
} }
break; break;
} }
printMenu(m); printMenu(m);
} }
} }
@ -150,12 +150,12 @@ static void generateList(Menu* m)
//current item is not on page //current item is not on page
if (count < m->page * ITEMS_PER_PAGE) if (count < m->page * ITEMS_PER_PAGE)
count += 1; count += 1;
else else
{ {
if (m->itemCount >= ITEMS_PER_PAGE) if (m->itemCount >= ITEMS_PER_PAGE)
done = true; done = true;
else else
{ {
//found requested title //found requested title
@ -168,7 +168,7 @@ static void generateList(Menu* m)
addMenuItem(m, title, path, 0); addMenuItem(m, title, path, 0);
free(path); free(path);
} }
} }
} }
} }
@ -176,7 +176,7 @@ static void generateList(Menu* m)
} }
closedir(subdir); closedir(subdir);
free(contentPath); free(contentPath);
} }
} }
} }
@ -379,7 +379,7 @@ static bool delete(Menu* m)
if (!m) return false; if (!m) return false;
char* fpath = m->items[m->cursor].value; char* fpath = m->items[m->cursor].value;
bool result = false; bool result = false;
bool choice = NO; bool choice = NO;
{ {
@ -390,7 +390,7 @@ static bool delete(Menu* m)
char str[] = "Are you sure you want to delete\n"; char str[] = "Are you sure you want to delete\n";
char* msg = (char*)malloc(strlen(str) + strlen(title) + 8); char* msg = (char*)malloc(strlen(str) + strlen(title) + 8);
sprintf(msg, "%s%s", str, title); sprintf(msg, "%s%s", str, title);
choice = choiceBox(msg); choice = choiceBox(msg);
free(msg); free(msg);