mirror of
https://github.com/rvtr/TDT.git
synced 2025-10-31 13:51:07 -04:00
SysNAND title manager
WARNING: This is *barely* tested and should be used with *extreme caution*
This commit is contained in:
parent
e74700db9e
commit
b6fa1e97c9
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
*.o
|
||||
*.d
|
||||
*.nds
|
||||
*.elf
|
||||
*.map
|
||||
*build/
|
||||
*.vscode
|
||||
*.DS_Store
|
||||
*._*
|
||||
10
Makefile
10
Makefile
@ -13,12 +13,12 @@ export TOPDIR := $(CURDIR)
|
||||
NITRO_FILES :=
|
||||
|
||||
# These set the information text in the nds file
|
||||
GAME_TITLE := TMFH
|
||||
GAME_SUBTITLE1 := Title Manager for HiyaCFW
|
||||
GAME_SUBTITLE2 := JeffRuLz
|
||||
GAME_TITLE := TMFN
|
||||
GAME_SUBTITLE1 := Title Manager for NAND
|
||||
GAME_SUBTITLE2 := JeffRuLz, Pk11
|
||||
|
||||
GAME_CODE := TMFH
|
||||
GAME_LABEL := TITLEMANFH
|
||||
GAME_CODE := TMFN
|
||||
GAME_LABEL := TITLEMANFN
|
||||
|
||||
include $(DEVKITARM)/ds_rules
|
||||
|
||||
|
||||
17
README.md
17
README.md
@ -1,8 +1,13 @@
|
||||
# Title Manager for HiyaCFW
|
||||
A basic title manager for DSi emunand.
|
||||
# Title Manager for NAND
|
||||
A basic title manager for DSi NAND, modified from JeffRuLz's Title Manager for HiyaCFW.
|
||||
|
||||
## WARNING
|
||||
This modifies your internal system NAND! There is *always* a risk of **bricking**, albeit small, when you modify NAND. Please proceed with caution.
|
||||
|
||||
This has *barely* been tested and should be used with *extreme caution*. Please always use with Unlaunch installed, homebrew installed to the DSi Menu won't work without it and I'm not even sure if DSiWare installed by this will.
|
||||
|
||||
## Features
|
||||
- Install DSiWare and homebrew onto your emunand home menu.
|
||||
- Install DSiWare and homebrew onto your SysNAND home menu.
|
||||
|
||||
- Delete system titles and others hidden from Data Management.
|
||||
|
||||
@ -11,7 +16,7 @@ A basic title manager for DSi emunand.
|
||||
- View basic title header info.
|
||||
|
||||
## Notes
|
||||
- Backup your SD card! Nothing bad should happen, but this is an early release so who knows.
|
||||
- Backup your SD card and your NAND! Nothing bad should happen, but this is an early release so who knows.
|
||||
|
||||
- This cannot be used to install cartridge games or older DS homebrew.
|
||||
|
||||
@ -23,3 +28,7 @@ A basic title manager for DSi emunand.
|
||||
[MakeTMD by Tuxality](https://github.com/Tuxality/maketmd)
|
||||
|
||||
[GBATEK](https://problemkaputt.de/gbatek.htm)
|
||||
|
||||
[TMFH by JeffRuLz](https://github.com/JeffRuLz/TMFH)
|
||||
|
||||
[DSi Language Patcher by DesperateProgrammer](https://github.com/DesperateProgrammer/DSiLanguagePacher)
|
||||
|
||||
276
arm7/src/main.c
276
arm7/src/main.c
@ -1,98 +1,178 @@
|
||||
/*---------------------------------------------------------------------------------
|
||||
|
||||
default ARM7 core
|
||||
|
||||
Copyright (C) 2005 - 2010
|
||||
Michael Noland (joat)
|
||||
Jason Rogers (dovoto)
|
||||
Dave Murphy (WinterMute)
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any
|
||||
damages arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any
|
||||
purpose, including commercial applications, and to alter it and
|
||||
redistribute it freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you
|
||||
must not claim that you wrote the original software. If you use
|
||||
this software in a product, an acknowledgment in the product
|
||||
documentation would be appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and
|
||||
must not be misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
|
||||
---------------------------------------------------------------------------------*/
|
||||
#include <nds.h>
|
||||
//#include <dswifi7.h>
|
||||
//#include <maxmod7.h>
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void VblankHandler(void) {
|
||||
//---------------------------------------------------------------------------------
|
||||
// Wifi_Update();
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void VcountHandler() {
|
||||
//---------------------------------------------------------------------------------
|
||||
inputGetAndSend();
|
||||
}
|
||||
|
||||
volatile bool exitflag = false;
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void powerButtonCB() {
|
||||
//---------------------------------------------------------------------------------
|
||||
exitflag = true;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int main() {
|
||||
//---------------------------------------------------------------------------------
|
||||
// clear sound registers
|
||||
dmaFillWords(0, (void*)0x04000400, 0x100);
|
||||
|
||||
REG_SOUNDCNT |= SOUND_ENABLE;
|
||||
writePowerManagement(PM_CONTROL_REG, ( readPowerManagement(PM_CONTROL_REG) & ~PM_SOUND_MUTE ) | PM_SOUND_AMP );
|
||||
powerOn(POWER_SOUND);
|
||||
|
||||
readUserSettings();
|
||||
ledBlink(0);
|
||||
|
||||
irqInit();
|
||||
// Start the RTC tracking IRQ
|
||||
initClockIRQ();
|
||||
fifoInit();
|
||||
touchInit();
|
||||
|
||||
// mmInstall(FIFO_MAXMOD);
|
||||
|
||||
SetYtrigger(80);
|
||||
|
||||
// installWifiFIFO();
|
||||
installSoundFIFO();
|
||||
|
||||
installSystemFIFO();
|
||||
|
||||
irqSet(IRQ_VCOUNT, VcountHandler);
|
||||
irqSet(IRQ_VBLANK, VblankHandler);
|
||||
|
||||
irqEnable( IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK);
|
||||
|
||||
setPowerButtonCB(powerButtonCB);
|
||||
|
||||
// Keep the ARM7 mostly idle
|
||||
while (!exitflag) {
|
||||
if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R))) {
|
||||
exitflag = true;
|
||||
}
|
||||
swiWaitForVBlank();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------------
|
||||
|
||||
default ARM7 core
|
||||
|
||||
Copyright (C) 2005 - 2010
|
||||
Michael Noland (joat)
|
||||
Jason Rogers (dovoto)
|
||||
Dave Murphy (WinterMute)
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any
|
||||
damages arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any
|
||||
purpose, including commercial applications, and to alter it and
|
||||
redistribute it freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you
|
||||
must not claim that you wrote the original software. If you use
|
||||
this software in a product, an acknowledgment in the product
|
||||
documentation would be appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and
|
||||
must not be misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
|
||||
---------------------------------------------------------------------------------*/
|
||||
#include <nds.h>
|
||||
//#include <dswifi7.h>
|
||||
//#include <maxmod7.h>
|
||||
#include "string.h"
|
||||
#include "my_sdmmc.h"
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void VblankHandler(void) {
|
||||
//---------------------------------------------------------------------------------
|
||||
// Wifi_Update();
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void VcountHandler() {
|
||||
//---------------------------------------------------------------------------------
|
||||
inputGetAndSend();
|
||||
}
|
||||
|
||||
volatile bool exitflag = false;
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void powerButtonCB() {
|
||||
//---------------------------------------------------------------------------------
|
||||
exitflag = true;
|
||||
}
|
||||
|
||||
void set_ctr(u32* ctr){
|
||||
for (int i = 0; i < 4; i++) REG_AES_IV[i] = ctr[3-i];
|
||||
}
|
||||
|
||||
// 10 11 22 23 24 25
|
||||
void aes(void* in, void* out, void* iv, u32 method){ //this is sort of a bodged together dsi aes function adapted from this 3ds function
|
||||
REG_AES_CNT = ( AES_CNT_MODE(method) | //https://github.com/TiniVi/AHPCFW/blob/master/source/aes.c#L42
|
||||
AES_WRFIFO_FLUSH | //as long as the output changes when keyslot values change, it's good enough.
|
||||
AES_RDFIFO_FLUSH |
|
||||
AES_CNT_KEY_APPLY |
|
||||
AES_CNT_KEYSLOT(3) |
|
||||
AES_CNT_DMA_WRITE_SIZE(2) |
|
||||
AES_CNT_DMA_READ_SIZE(1)
|
||||
);
|
||||
|
||||
if (iv != NULL) set_ctr((u32*)iv);
|
||||
REG_AES_BLKCNT = (1 << 16);
|
||||
REG_AES_CNT |= 0x80000000;
|
||||
|
||||
for (int j = 0; j < 0x10; j+=4) REG_AES_WRFIFO = *((u32*)(in+j));
|
||||
while(((REG_AES_CNT >> 0x5) & 0x1F) < 0x4); //wait for every word to get processed
|
||||
for (int j = 0; j < 0x10; j+=4) *((u32*)(out+j)) = REG_AES_RDFIFO;
|
||||
//REG_AES_CNT &= ~0x80000000;
|
||||
//if (method & (AES_CTR_DECRYPT | AES_CTR_ENCRYPT)) add_ctr((u8*)iv);
|
||||
}
|
||||
|
||||
int my_sdmmc_nand_startup();
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int main() {
|
||||
//---------------------------------------------------------------------------------
|
||||
// clear sound registers
|
||||
dmaFillWords(0, (void*)0x04000400, 0x100);
|
||||
|
||||
REG_SOUNDCNT |= SOUND_ENABLE;
|
||||
writePowerManagement(PM_CONTROL_REG, ( readPowerManagement(PM_CONTROL_REG) & ~PM_SOUND_MUTE ) | PM_SOUND_AMP );
|
||||
powerOn(POWER_SOUND);
|
||||
|
||||
readUserSettings();
|
||||
ledBlink(0);
|
||||
|
||||
irqInit();
|
||||
// Start the RTC tracking IRQ
|
||||
initClockIRQ();
|
||||
fifoInit();
|
||||
touchInit();
|
||||
|
||||
if (isDSiMode() /*|| ((REG_SCFG_EXT & BIT(17)) && (REG_SCFG_EXT & BIT(18)))*/) {
|
||||
u8 *out=(u8*)0x02300000;
|
||||
|
||||
#if USENATIVECONSOLEID
|
||||
// first check whether we can read the console ID directly and it was not hidden by SCFG
|
||||
if (((*(volatile uint16_t *)0x04004000) & (1u << 10)) == 0)
|
||||
{
|
||||
// The console id registers are readable, so use them!
|
||||
memcpy(out, (uint8_t *)0x04004D00, 8);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
// For getting ConsoleID without reading from 0x4004D00...
|
||||
u8 base[16]={0};
|
||||
u8 in[16]={0};
|
||||
u8 iv[16]={0};
|
||||
u8 *scratch=(u8*)0x02300200;
|
||||
u8 *key3=(u8*)0x40044D0;
|
||||
|
||||
|
||||
aes(in, base, iv, 2);
|
||||
|
||||
//write consecutive 0-255 values to any byte in key3 until we get the same aes output as "base" above - this reveals the hidden byte. this way we can uncover all 16 bytes of the key3 normalkey pretty easily.
|
||||
//greets to Martin Korth for this trick https://problemkaputt.de/gbatek.htm#dsiaesioports (Reading Write-Only Values)
|
||||
for(int i=0;i<16;i++){
|
||||
for(int j=0;j<256;j++){
|
||||
*(key3+i)=j & 0xFF;
|
||||
aes(in, scratch, iv, 2);
|
||||
if(!memcmp(scratch, base, 16)){
|
||||
out[i]=j;
|
||||
//hit++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
my_sdmmc_nand_startup() ;
|
||||
my_sdmmc_get_cid(true, (u32*)0x2FFD7BC); // Get eMMC CID
|
||||
//sdmmc_nand_cid((u32*)0x2FFD7BC);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// mmInstall(FIFO_MAXMOD);
|
||||
|
||||
SetYtrigger(80);
|
||||
|
||||
// installWifiFIFO();
|
||||
installSoundFIFO();
|
||||
|
||||
installSystemFIFO();
|
||||
|
||||
irqSet(IRQ_VCOUNT, VcountHandler);
|
||||
irqSet(IRQ_VBLANK, VblankHandler);
|
||||
|
||||
irqEnable( IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK);
|
||||
|
||||
setPowerButtonCB(powerButtonCB);
|
||||
|
||||
// Keep the ARM7 mostly idle
|
||||
while (!exitflag) {
|
||||
if ( 0 == (REG_KEYINPUT & (KEY_SELECT | KEY_START | KEY_L | KEY_R))) {
|
||||
exitflag = true;
|
||||
}
|
||||
swiWaitForVBlank();
|
||||
}
|
||||
|
||||
// Tell ARM9 to safely exit
|
||||
fifoSendValue32(FIFO_USER_01, 0x54495845); // 'EXIT'
|
||||
fifoWaitValue32(FIFO_USER_02);
|
||||
fifoCheckValue32(FIFO_USER_02);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
666
arm7/src/my_sdmmc.c
Normal file
666
arm7/src/my_sdmmc.c
Normal file
@ -0,0 +1,666 @@
|
||||
#include <nds/system.h>
|
||||
#include <nds/bios.h>
|
||||
#include "my_sdmmc.h"
|
||||
#include <nds/interrupts.h>
|
||||
#include <nds/fifocommon.h>
|
||||
#include <nds/fifomessages.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
static struct mmcdevice deviceSD;
|
||||
static struct mmcdevice deviceNAND;
|
||||
|
||||
/*mmcdevice *getMMCDevice(int drive) {
|
||||
if(drive==0) return &deviceNAND;
|
||||
return &deviceSD;
|
||||
}
|
||||
*/
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_geterror(struct mmcdevice *ctx) {
|
||||
//---------------------------------------------------------------------------------
|
||||
//if(ctx->error == 0x4) return -1;
|
||||
//else return 0;
|
||||
return (ctx->error << 29) >> 31;
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_setTarget(struct mmcdevice *ctx) {
|
||||
//---------------------------------------------------------------------------------
|
||||
sdmmc_mask16(REG_SDPORTSEL,0x3,(u16)ctx->devicenumber);
|
||||
setckl(ctx->clk);
|
||||
if (ctx->SDOPT == 0) {
|
||||
sdmmc_mask16(REG_SDOPT, 0, 0x8000);
|
||||
} else {
|
||||
sdmmc_mask16(REG_SDOPT, 0x8000, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmc_send_command(struct mmcdevice *ctx, uint32_t cmd, uint32_t args) {
|
||||
//---------------------------------------------------------------------------------
|
||||
const bool getSDRESP = (cmd << 15) >> 31;
|
||||
u16 flags = (cmd << 15) >> 31;
|
||||
const bool readdata = cmd & 0x20000;
|
||||
const bool writedata = cmd & 0x40000;
|
||||
|
||||
if(readdata || writedata)
|
||||
{
|
||||
flags |= TMIO_STAT0_DATAEND;
|
||||
}
|
||||
|
||||
ctx->error = 0;
|
||||
while((sdmmc_read16(REG_SDSTATUS1) & TMIO_STAT1_CMD_BUSY)); //mmc working?
|
||||
sdmmc_write16(REG_SDIRMASK0,0);
|
||||
sdmmc_write16(REG_SDIRMASK1,0);
|
||||
sdmmc_write16(REG_SDSTATUS0,0);
|
||||
sdmmc_write16(REG_SDSTATUS1,0);
|
||||
sdmmc_mask16(REG_SDDATACTL32,0x1800,0x400); // Disable TX32RQ and RX32RDY IRQ. Clear fifo.
|
||||
sdmmc_write16(REG_SDCMDARG0,args &0xFFFF);
|
||||
sdmmc_write16(REG_SDCMDARG1,args >> 16);
|
||||
sdmmc_write16(REG_SDCMD,cmd &0xFFFF);
|
||||
|
||||
u32 size = ctx->size;
|
||||
const u16 blkSize = sdmmc_read16(REG_SDBLKLEN32);
|
||||
#ifdef DATA32_SUPPORT
|
||||
u32 *rDataPtr32 = (u32*)ctx->rData;
|
||||
#else
|
||||
u16 *rDataPtr16 = (u16*)ctx->rData;
|
||||
#endif
|
||||
u8 *rDataPtr8 = ctx->rData;
|
||||
#ifdef DATA32_SUPPORT
|
||||
const u32 *tDataPtr32 = (u32*)ctx->tData;
|
||||
#else
|
||||
const u16 *tDataPtr16 = (u16*)ctx->tData;
|
||||
#endif
|
||||
const u8 *tDataPtr8 = ctx->tData;
|
||||
|
||||
#ifdef DATA32_SUPPORT
|
||||
bool rUseBuf = ( NULL != rDataPtr32 );
|
||||
bool tUseBuf = ( NULL != tDataPtr32 );
|
||||
#else
|
||||
bool rUseBuf = ( NULL != rDataPtr16 );
|
||||
bool tUseBuf = ( NULL != tDataPtr16 );
|
||||
#endif
|
||||
|
||||
u16 status0 = 0;
|
||||
while(1)
|
||||
{
|
||||
volatile u16 status1 = sdmmc_read16(REG_SDSTATUS1);
|
||||
#ifdef DATA32_SUPPORT
|
||||
volatile u16 ctl32 = sdmmc_read16(REG_SDDATACTL32);
|
||||
if((ctl32 & 0x100))
|
||||
#else
|
||||
if((status1 & TMIO_STAT1_RXRDY))
|
||||
#endif
|
||||
{
|
||||
if(readdata)
|
||||
{
|
||||
if(rUseBuf)
|
||||
{
|
||||
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0);
|
||||
if(size >= blkSize)
|
||||
{
|
||||
#ifdef DATA32_SUPPORT
|
||||
if(!((u32)rDataPtr32 & 3))
|
||||
{
|
||||
for(u32 i = 0; i < blkSize; i += 4)
|
||||
{
|
||||
*rDataPtr32++ = sdmmc_read32(REG_SDFIFO32);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(u32 i = 0; i < blkSize; i += 4)
|
||||
{
|
||||
u32 data = sdmmc_read32(REG_SDFIFO32);
|
||||
*rDataPtr8++ = data;
|
||||
*rDataPtr8++ = data >> 8;
|
||||
*rDataPtr8++ = data >> 16;
|
||||
*rDataPtr8++ = data >> 24;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if(!((u16)rDataPtr16 & 1))
|
||||
{
|
||||
for(u16 i = 0; i < blkSize; i += 2)
|
||||
{
|
||||
*rDataPtr16++ = sdmmc_read16(REG_SDFIFO);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(u16 i = 0; i < blkSize; i += 2)
|
||||
{
|
||||
u16 data = sdmmc_read16(REG_SDFIFO);
|
||||
*rDataPtr8++ = data;
|
||||
*rDataPtr8++ = data >> 8;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
size -= blkSize;
|
||||
}
|
||||
}
|
||||
|
||||
sdmmc_mask16(REG_SDDATACTL32, 0x800, 0);
|
||||
}
|
||||
}
|
||||
#ifdef DATA32_SUPPORT
|
||||
if(!(ctl32 & 0x200))
|
||||
#else
|
||||
if((status1 & TMIO_STAT1_TXRQ))
|
||||
#endif
|
||||
{
|
||||
if(writedata)
|
||||
{
|
||||
if(tUseBuf)
|
||||
{
|
||||
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0);
|
||||
if(size >= blkSize)
|
||||
{
|
||||
#ifdef DATA32_SUPPORT
|
||||
if(!((u32)tDataPtr32 & 3))
|
||||
{
|
||||
for(u32 i = 0; i < blkSize; i += 4)
|
||||
{
|
||||
sdmmc_write32(REG_SDFIFO32, *tDataPtr32++);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(u32 i = 0; i < blkSize; i += 4)
|
||||
{
|
||||
u32 data = *tDataPtr8++;
|
||||
data |= (u32)*tDataPtr8++ << 8;
|
||||
data |= (u32)*tDataPtr8++ << 16;
|
||||
data |= (u32)*tDataPtr8++ << 24;
|
||||
sdmmc_write32(REG_SDFIFO32, data);
|
||||
}
|
||||
}
|
||||
#else
|
||||
if(!((u16)tDataPtr16 & 1))
|
||||
{
|
||||
for(u16 i = 0; i < blkSize; i += 2)
|
||||
{
|
||||
sdmmc_write16(REG_SDFIFO, *tDataPtr16++);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(u16 i = 0; i < blkSize; i += 2)
|
||||
{
|
||||
u16 data = *tDataPtr8++;
|
||||
data |= (u16)(*tDataPtr8++ << 8);
|
||||
sdmmc_write16(REG_SDFIFO, data);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
size -= blkSize;
|
||||
}
|
||||
}
|
||||
|
||||
sdmmc_mask16(REG_SDDATACTL32, 0x1000, 0);
|
||||
}
|
||||
}
|
||||
if(status1 & TMIO_MASK_GW)
|
||||
{
|
||||
ctx->error |= 4;
|
||||
break;
|
||||
}
|
||||
|
||||
if(!(status1 & TMIO_STAT1_CMD_BUSY))
|
||||
{
|
||||
status0 = sdmmc_read16(REG_SDSTATUS0);
|
||||
if(sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND)
|
||||
{
|
||||
ctx->error |= 0x1;
|
||||
}
|
||||
if(status0 & TMIO_STAT0_DATAEND)
|
||||
{
|
||||
ctx->error |= 0x2;
|
||||
}
|
||||
|
||||
if((status0 & flags) == flags)
|
||||
break;
|
||||
}
|
||||
}
|
||||
ctx->stat0 = sdmmc_read16(REG_SDSTATUS0);
|
||||
ctx->stat1 = sdmmc_read16(REG_SDSTATUS1);
|
||||
sdmmc_write16(REG_SDSTATUS0,0);
|
||||
sdmmc_write16(REG_SDSTATUS1,0);
|
||||
|
||||
if(getSDRESP != 0)
|
||||
{
|
||||
ctx->ret[0] = (u32)(sdmmc_read16(REG_SDRESP0) | (sdmmc_read16(REG_SDRESP1) << 16));
|
||||
ctx->ret[1] = (u32)(sdmmc_read16(REG_SDRESP2) | (sdmmc_read16(REG_SDRESP3) << 16));
|
||||
ctx->ret[2] = (u32)(sdmmc_read16(REG_SDRESP4) | (sdmmc_read16(REG_SDRESP5) << 16));
|
||||
ctx->ret[3] = (u32)(sdmmc_read16(REG_SDRESP6) | (sdmmc_read16(REG_SDRESP7) << 16));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_cardinserted() {
|
||||
//---------------------------------------------------------------------------------
|
||||
return 1; //my_sdmmc_cardready;
|
||||
}
|
||||
|
||||
|
||||
static bool my_sdmmc_controller_initialised = false;
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmc_controller_init( bool force_init ) {
|
||||
//---------------------------------------------------------------------------------
|
||||
|
||||
if (!force_init && my_sdmmc_controller_initialised) return;
|
||||
|
||||
deviceSD.isSDHC = 0;
|
||||
deviceSD.SDOPT = 0;
|
||||
deviceSD.res = 0;
|
||||
deviceSD.initarg = 0;
|
||||
deviceSD.clk = 0x80;
|
||||
deviceSD.devicenumber = 0;
|
||||
|
||||
deviceNAND.isSDHC = 0;
|
||||
deviceNAND.SDOPT = 0;
|
||||
deviceNAND.res = 0;
|
||||
deviceNAND.initarg = 1;
|
||||
deviceNAND.clk = 0x80;
|
||||
deviceNAND.devicenumber = 1;
|
||||
|
||||
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xF7FFu;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xEFFFu;
|
||||
#ifdef DATA32_SUPPORT
|
||||
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u;
|
||||
#else
|
||||
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) |= 0x402u;
|
||||
#endif
|
||||
*(vu16*)(SDMMC_BASE + REG_SDDATACTL) = (*(vu16*)(SDMMC_BASE + REG_SDDATACTL) & 0xFFDD) | 2;
|
||||
#ifdef DATA32_SUPPORT
|
||||
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFFu;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDFu;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 512;
|
||||
#else
|
||||
*(vu16*)(SDMMC_BASE + REG_SDDATACTL32) &= 0xFFFDu;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDDATACTL) &= 0xFFDDu;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDBLKLEN32) = 0;
|
||||
#endif
|
||||
*(vu16*)(SDMMC_BASE + REG_SDBLKCOUNT32) = 1;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDRESET) &= 0xFFFEu;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDRESET) |= 1u;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDIRMASK0) |= TMIO_MASK_ALL;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDIRMASK1) |= TMIO_MASK_ALL>>16;
|
||||
*(vu16*)(SDMMC_BASE + 0x0fc) |= 0xDBu; //SDCTL_RESERVED7
|
||||
*(vu16*)(SDMMC_BASE + 0x0fe) |= 0xDBu; //SDCTL_RESERVED8
|
||||
*(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu;
|
||||
#ifdef DATA32_SUPPORT
|
||||
*(vu16*)(SDMMC_BASE + REG_SDCLKCTL) = 0x20;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDOPT) = 0x40EE;
|
||||
#else
|
||||
*(vu16*)(SDMMC_BASE + REG_SDCLKCTL) = 0x40; //Nintendo sets this to 0x20
|
||||
*(vu16*)(SDMMC_BASE + REG_SDOPT) = 0x40EB; //Nintendo sets this to 0x40EE
|
||||
#endif
|
||||
*(vu16*)(SDMMC_BASE + REG_SDPORTSEL) &= 0xFFFCu;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDBLKLEN) = 512;
|
||||
*(vu16*)(SDMMC_BASE + REG_SDSTOP) = 0;
|
||||
|
||||
my_sdmmc_controller_initialised = true;
|
||||
|
||||
my_setTarget(&deviceSD);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static u32 calcSDSize(u8* csd, int type) {
|
||||
//---------------------------------------------------------------------------------
|
||||
u32 result = 0;
|
||||
if (type == -1) type = csd[14] >> 6;
|
||||
switch (type) {
|
||||
case 0:
|
||||
{
|
||||
u32 block_len = csd[9] & 0xf;
|
||||
block_len = 1 << block_len;
|
||||
u32 mult = (csd[4] >> 7) | ((csd[5] & 3) << 1);
|
||||
mult = 1 << (mult + 2);
|
||||
result = csd[8] & 3;
|
||||
result = (result << 8) | csd[7];
|
||||
result = (result << 2) | (csd[6] >> 6);
|
||||
result = (result + 1) * mult * block_len / 512;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
result = csd[7] & 0x3f;
|
||||
result = (result << 8) | csd[6];
|
||||
result = (result << 8) | csd[5];
|
||||
result = (result + 1) * 1024;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_sdcard_init() {
|
||||
//---------------------------------------------------------------------------------
|
||||
// We need to send at least 74 clock pulses.
|
||||
my_setTarget(&deviceSD);
|
||||
swiDelay(0x1980); // ~75-76 clocks
|
||||
|
||||
// card reset
|
||||
my_sdmmc_send_command(&deviceSD,0,0);
|
||||
|
||||
// CMD8 0x1AA
|
||||
my_sdmmc_send_command(&deviceSD,0x10408,0x1AA);
|
||||
u32 temp = (deviceSD.error & 0x1) << 0x1E;
|
||||
|
||||
u32 temp2 = 0;
|
||||
do {
|
||||
do {
|
||||
// CMD55
|
||||
my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10);
|
||||
// ACMD41
|
||||
my_sdmmc_send_command(&deviceSD,0x10769,0x00FF8000 | temp);
|
||||
temp2 = 1;
|
||||
} while ( !(deviceSD.error & 1) );
|
||||
|
||||
} while((deviceSD.ret[0] & 0x80000000) == 0);
|
||||
|
||||
if(!((deviceSD.ret[0] >> 30) & 1) || !temp)
|
||||
temp2 = 0;
|
||||
|
||||
deviceSD.isSDHC = temp2;
|
||||
|
||||
my_sdmmc_send_command(&deviceSD,0x10602,0);
|
||||
if (deviceSD.error & 0x4) return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceSD,0x10403,0);
|
||||
if (deviceSD.error & 0x4) return -1;
|
||||
deviceSD.initarg = deviceSD.ret[0] >> 0x10;
|
||||
|
||||
my_sdmmc_send_command(&deviceSD,0x10609,deviceSD.initarg << 0x10);
|
||||
if (deviceSD.error & 0x4) return -1;
|
||||
|
||||
// Command Class 10 support
|
||||
const bool cmd6Supported = ((u8*)deviceSD.ret)[10] & 0x40;
|
||||
deviceSD.total_size = calcSDSize((u8*)&deviceSD.ret[0],-1);
|
||||
setckl(0x201); // 16.756991 MHz
|
||||
|
||||
my_sdmmc_send_command(&deviceSD,0x10507,deviceSD.initarg << 0x10);
|
||||
if (deviceSD.error & 0x4) return -1;
|
||||
|
||||
// CMD55
|
||||
my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10);
|
||||
if (deviceSD.error & 0x4) return -1;
|
||||
|
||||
// ACMD42
|
||||
my_sdmmc_send_command(&deviceSD,0x1076A,0x0);
|
||||
if (deviceSD.error & 0x4) return -1;
|
||||
|
||||
// CMD55
|
||||
my_sdmmc_send_command(&deviceSD,0x10437,deviceSD.initarg << 0x10);
|
||||
if (deviceSD.error & 0x4) return -7;
|
||||
|
||||
deviceSD.SDOPT = 1;
|
||||
my_sdmmc_send_command(&deviceSD,0x10446,0x2);
|
||||
if (deviceSD.error & 0x4) return -8;
|
||||
sdmmc_mask16(REG_SDOPT, 0x8000, 0); // Switch to 4 bit mode.
|
||||
|
||||
// TODO: CMD6 to switch to high speed mode.
|
||||
if(cmd6Supported)
|
||||
{
|
||||
sdmmc_write16(REG_SDSTOP,0);
|
||||
sdmmc_write16(REG_SDBLKLEN32,64);
|
||||
sdmmc_write16(REG_SDBLKLEN,64);
|
||||
deviceSD.rData = NULL;
|
||||
deviceSD.size = 64;
|
||||
my_sdmmc_send_command(&deviceSD,0x31C06,0x80FFFFF1);
|
||||
sdmmc_write16(REG_SDBLKLEN,512);
|
||||
if(deviceSD.error & 0x4) return -9;
|
||||
|
||||
deviceSD.clk = 0x200; // 33.513982 MHz
|
||||
setckl(0x200);
|
||||
}
|
||||
else deviceSD.clk = 0x201; // 16.756991 MHz
|
||||
|
||||
my_sdmmc_send_command(&deviceSD,0x1040D,deviceSD.initarg << 0x10);
|
||||
if (deviceSD.error & 0x4) return -9;
|
||||
|
||||
my_sdmmc_send_command(&deviceSD,0x10410,0x200);
|
||||
if (deviceSD.error & 0x4) return -10;
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_nand_init() {
|
||||
//---------------------------------------------------------------------------------
|
||||
my_setTarget(&deviceNAND);
|
||||
swiDelay(0xF000);
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0,0);
|
||||
|
||||
do {
|
||||
do {
|
||||
my_sdmmc_send_command(&deviceNAND,0x10701,0x100000);
|
||||
} while ( !(deviceNAND.error & 1) );
|
||||
}
|
||||
while((deviceNAND.ret[0] & 0x80000000) == 0);
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10602,0x0);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10403,deviceNAND.initarg << 0x10);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10609,deviceNAND.initarg << 0x10);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
deviceNAND.total_size = calcSDSize((uint8_t*)&deviceNAND.ret[0],0);
|
||||
deviceNAND.clk = 1;
|
||||
setckl(1);
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10407,deviceNAND.initarg << 0x10);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
deviceNAND.SDOPT = 1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10506,0x3B70100);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10506,0x3B90100);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x1040D,deviceNAND.initarg << 0x10);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
my_sdmmc_send_command(&deviceNAND,0x10410,0x200);
|
||||
if((deviceNAND.error & 0x4))return -1;
|
||||
|
||||
deviceNAND.clk |= 0x200;
|
||||
|
||||
my_setTarget(&deviceSD);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_readsectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *out) {
|
||||
//---------------------------------------------------------------------------------
|
||||
if (device->isSDHC == 0) sector_no <<= 9;
|
||||
my_setTarget(device);
|
||||
sdmmc_write16(REG_SDSTOP,0x100);
|
||||
|
||||
#ifdef DATA32_SUPPORT
|
||||
sdmmc_write16(REG_SDBLKCOUNT32,numsectors);
|
||||
sdmmc_write16(REG_SDBLKLEN32,0x200);
|
||||
#endif
|
||||
|
||||
sdmmc_write16(REG_SDBLKCOUNT,numsectors);
|
||||
device->rData = out;
|
||||
device->size = numsectors << 9;
|
||||
my_sdmmc_send_command(device,0x33C12,sector_no);
|
||||
my_setTarget(&deviceSD);
|
||||
return my_geterror(device);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_writesectors(struct mmcdevice *device, u32 sector_no, u32 numsectors, void *in) {
|
||||
//---------------------------------------------------------------------------------
|
||||
if (device->isSDHC == 0)
|
||||
sector_no <<= 9;
|
||||
my_setTarget(device);
|
||||
sdmmc_write16(REG_SDSTOP,0x100);
|
||||
|
||||
#ifdef DATA32_SUPPORT
|
||||
sdmmc_write16(REG_SDBLKCOUNT32,numsectors);
|
||||
sdmmc_write16(REG_SDBLKLEN32,0x200);
|
||||
#endif
|
||||
|
||||
sdmmc_write16(REG_SDBLKCOUNT,numsectors);
|
||||
device->tData = in;
|
||||
device->size = numsectors << 9;
|
||||
my_sdmmc_send_command(device,0x52C19,sector_no);
|
||||
my_setTarget(&deviceSD);
|
||||
return my_geterror(device);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmc_get_cid(int devicenumber, u32 *cid) {
|
||||
//---------------------------------------------------------------------------------
|
||||
|
||||
struct mmcdevice *device = (devicenumber == 1 ? &deviceNAND : &deviceSD);
|
||||
|
||||
int oldIME = enterCriticalSection();
|
||||
|
||||
my_setTarget(device);
|
||||
|
||||
// use cmd7 to put sd card in standby mode
|
||||
// CMD7
|
||||
my_sdmmc_send_command(device, 0x10507, 0);
|
||||
|
||||
// get sd card info
|
||||
// use cmd10 to read CID
|
||||
my_sdmmc_send_command(device, 0x1060A, device->initarg << 0x10);
|
||||
|
||||
for(int i = 0; i < 4; ++i)
|
||||
cid[i] = device->ret[i];
|
||||
|
||||
// put sd card back to transfer mode
|
||||
// CMD7
|
||||
my_sdmmc_send_command(device, 0x10507, device->initarg << 0x10);
|
||||
|
||||
leaveCriticalSection(oldIME);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmcMsgHandler(int bytes, void *user_data) {
|
||||
//---------------------------------------------------------------------------------
|
||||
FifoMessage msg;
|
||||
int retval = 0;
|
||||
|
||||
fifoGetDatamsg(FIFO_SDMMC, bytes, (u8*)&msg);
|
||||
|
||||
int oldIME = enterCriticalSection();
|
||||
switch (msg.type) {
|
||||
|
||||
case SDMMC_SD_READ_SECTORS:
|
||||
retval = my_sdmmc_readsectors(&deviceSD, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer);
|
||||
break;
|
||||
case SDMMC_SD_WRITE_SECTORS:
|
||||
retval = my_sdmmc_writesectors(&deviceSD, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer);
|
||||
break;
|
||||
case SDMMC_NAND_READ_SECTORS:
|
||||
retval = my_sdmmc_readsectors(&deviceNAND, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer);
|
||||
break;
|
||||
case SDMMC_NAND_WRITE_SECTORS:
|
||||
retval = my_sdmmc_writesectors(&deviceNAND, msg.sdParams.startsector, msg.sdParams.numsectors, msg.sdParams.buffer);
|
||||
break;
|
||||
}
|
||||
|
||||
leaveCriticalSection(oldIME);
|
||||
|
||||
fifoSendValue32(FIFO_SDMMC, retval);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_nand_startup() {
|
||||
//---------------------------------------------------------------------------------
|
||||
my_sdmmc_controller_init(false);
|
||||
return my_sdmmc_nand_init();
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_sd_startup() {
|
||||
//---------------------------------------------------------------------------------
|
||||
my_sdmmc_controller_init(false);
|
||||
return my_sdmmc_sdcard_init();
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
void my_sdmmcValueHandler(u32 value, void* user_data) {
|
||||
//---------------------------------------------------------------------------------
|
||||
int result = 0;
|
||||
int sdflag = 0;
|
||||
int oldIME = enterCriticalSection();
|
||||
|
||||
switch(value) {
|
||||
|
||||
case SDMMC_HAVE_SD:
|
||||
result = sdmmc_read16(REG_SDSTATUS0);
|
||||
break;
|
||||
|
||||
case SDMMC_SD_START:
|
||||
sdflag = 1;
|
||||
/* Falls through. */
|
||||
case SDMMC_NAND_START:
|
||||
if (sdmmc_read16(REG_SDSTATUS0) == 0) {
|
||||
result = 1;
|
||||
} else {
|
||||
result = (sdflag == 1 ) ? my_sdmmc_sd_startup() : my_sdmmc_nand_startup();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDMMC_SD_IS_INSERTED:
|
||||
result = my_sdmmc_cardinserted();
|
||||
break;
|
||||
|
||||
case SDMMC_SD_STOP:
|
||||
break;
|
||||
|
||||
case SDMMC_NAND_SIZE:
|
||||
result = deviceNAND.total_size;
|
||||
break;
|
||||
}
|
||||
|
||||
leaveCriticalSection(oldIME);
|
||||
|
||||
fifoSendValue32(FIFO_SDMMC, result);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, void *out) {
|
||||
//---------------------------------------------------------------------------------
|
||||
return my_sdmmc_readsectors(&deviceSD, sector_no, numsectors, out);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, void *in) {
|
||||
//---------------------------------------------------------------------------------
|
||||
return my_sdmmc_writesectors(&deviceSD, sector_no, numsectors, in);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, void *out) {
|
||||
//---------------------------------------------------------------------------------
|
||||
return my_sdmmc_readsectors(&deviceNAND, sector_no, numsectors, out);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
int my_sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, void *in) {
|
||||
//---------------------------------------------------------------------------------
|
||||
return my_sdmmc_writesectors(&deviceNAND, sector_no, numsectors, in);
|
||||
}
|
||||
|
||||
|
||||
197
arm7/src/my_sdmmc.h
Normal file
197
arm7/src/my_sdmmc.h
Normal file
@ -0,0 +1,197 @@
|
||||
#ifndef __SDMMC_H__
|
||||
#define __SDMMC_H__
|
||||
|
||||
#include <nds/ndstypes.h>
|
||||
|
||||
#define DATA32_SUPPORT
|
||||
|
||||
#define SDMMC_BASE 0x04004800
|
||||
|
||||
|
||||
#define REG_SDCMD 0x00
|
||||
#define REG_SDPORTSEL 0x02
|
||||
#define REG_SDCMDARG 0x04
|
||||
#define REG_SDCMDARG0 0x04
|
||||
#define REG_SDCMDARG1 0x06
|
||||
#define REG_SDSTOP 0x08
|
||||
#define REG_SDRESP 0x0c
|
||||
#define REG_SDBLKCOUNT 0x0a
|
||||
|
||||
#define REG_SDRESP0 0x0c
|
||||
#define REG_SDRESP1 0x0e
|
||||
#define REG_SDRESP2 0x10
|
||||
#define REG_SDRESP3 0x12
|
||||
#define REG_SDRESP4 0x14
|
||||
#define REG_SDRESP5 0x16
|
||||
#define REG_SDRESP6 0x18
|
||||
#define REG_SDRESP7 0x1a
|
||||
|
||||
#define REG_SDSTATUS0 0x1c
|
||||
#define REG_SDSTATUS1 0x1e
|
||||
|
||||
#define REG_SDIRMASK0 0x20
|
||||
#define REG_SDIRMASK1 0x22
|
||||
#define REG_SDCLKCTL 0x24
|
||||
|
||||
#define REG_SDBLKLEN 0x26
|
||||
#define REG_SDOPT 0x28
|
||||
#define REG_SDFIFO 0x30
|
||||
|
||||
#define REG_SDDATACTL 0xd8
|
||||
#define REG_SDRESET 0xe0
|
||||
#define REG_SDPROTECTED 0xf6 //bit 0 determines if sd is protected or not?
|
||||
|
||||
#define REG_SDDATACTL32 0x100
|
||||
#define REG_SDBLKLEN32 0x104
|
||||
#define REG_SDBLKCOUNT32 0x108
|
||||
#define REG_SDFIFO32 0x10C
|
||||
|
||||
#define REG_CLK_AND_WAIT_CTL 0x138
|
||||
#define REG_RESET_SDIO 0x1e0
|
||||
//The below defines are from linux kernel drivers/mmc tmio_mmc.h.
|
||||
/* Definitions for values the CTRL_STATUS register can take. */
|
||||
#define TMIO_STAT0_CMDRESPEND 0x0001
|
||||
#define TMIO_STAT0_DATAEND 0x0004
|
||||
#define TMIO_STAT0_CARD_REMOVE 0x0008
|
||||
#define TMIO_STAT0_CARD_INSERT 0x0010
|
||||
#define TMIO_STAT0_SIGSTATE 0x0020
|
||||
#define TMIO_STAT0_WRPROTECT 0x0080
|
||||
#define TMIO_STAT0_CARD_REMOVE_A 0x0100
|
||||
#define TMIO_STAT0_CARD_INSERT_A 0x0200
|
||||
#define TMIO_STAT0_SIGSTATE_A 0x0400
|
||||
|
||||
#define TMIO_STAT1_CMD_IDX_ERR 0x0001
|
||||
#define TMIO_STAT1_CRCFAIL 0x0002
|
||||
#define TMIO_STAT1_STOPBIT_ERR 0x0004
|
||||
#define TMIO_STAT1_DATATIMEOUT 0x0008
|
||||
#define TMIO_STAT1_RXOVERFLOW 0x0010
|
||||
#define TMIO_STAT1_TXUNDERRUN 0x0020
|
||||
#define TMIO_STAT1_CMDTIMEOUT 0x0040
|
||||
#define TMIO_STAT1_RXRDY 0x0100
|
||||
#define TMIO_STAT1_TXRQ 0x0200
|
||||
#define TMIO_STAT1_ILL_FUNC 0x2000
|
||||
#define TMIO_STAT1_CMD_BUSY 0x4000
|
||||
#define TMIO_STAT1_ILL_ACCESS 0x8000
|
||||
|
||||
#define SDMC_NORMAL 0x00000000
|
||||
#define SDMC_ERR_COMMAND 0x00000001
|
||||
#define SDMC_ERR_CRC 0x00000002
|
||||
#define SDMC_ERR_END 0x00000004
|
||||
#define SDMC_ERR_TIMEOUT 0x00000008
|
||||
#define SDMC_ERR_FIFO_OVF 0x00000010
|
||||
#define SDMC_ERR_FIFO_UDF 0x00000020
|
||||
#define SDMC_ERR_WP 0x00000040
|
||||
#define SDMC_ERR_ABORT 0x00000080
|
||||
#define SDMC_ERR_FPGA_TIMEOUT 0x00000100
|
||||
#define SDMC_ERR_PARAM 0x00000200
|
||||
#define SDMC_ERR_R1_STATUS 0x00000800
|
||||
#define SDMC_ERR_NUM_WR_SECTORS 0x00001000
|
||||
#define SDMC_ERR_RESET 0x00002000
|
||||
#define SDMC_ERR_ILA 0x00004000
|
||||
#define SDMC_ERR_INFO_DETECT 0x00008000
|
||||
|
||||
#define SDMC_STAT_ERR_UNKNOWN 0x00080000
|
||||
#define SDMC_STAT_ERR_CC 0x00100000
|
||||
#define SDMC_STAT_ERR_ECC_FAILED 0x00200000
|
||||
#define SDMC_STAT_ERR_CRC 0x00800000
|
||||
#define SDMC_STAT_ERR_OTHER 0xf9c70008
|
||||
|
||||
#define TMIO_MASK_ALL 0x837f031d
|
||||
|
||||
#define TMIO_MASK_GW (TMIO_STAT1_ILL_ACCESS | TMIO_STAT1_CMDTIMEOUT | TMIO_STAT1_TXUNDERRUN | TMIO_STAT1_RXOVERFLOW | \
|
||||
TMIO_STAT1_DATATIMEOUT | TMIO_STAT1_STOPBIT_ERR | TMIO_STAT1_CRCFAIL | TMIO_STAT1_CMD_IDX_ERR)
|
||||
|
||||
#define TMIO_MASK_READOP (TMIO_STAT1_RXRDY | TMIO_STAT1_DATAEND)
|
||||
#define TMIO_MASK_WRITEOP (TMIO_STAT1_TXRQ | TMIO_STAT1_DATAEND)
|
||||
|
||||
typedef struct mmcdevice {
|
||||
u8* rData;
|
||||
const u8* tData;
|
||||
u32 size;
|
||||
u32 startOffset;
|
||||
u32 endOffset;
|
||||
u32 error;
|
||||
u16 stat0;
|
||||
u16 stat1;
|
||||
u32 ret[4];
|
||||
u32 initarg;
|
||||
u32 isSDHC;
|
||||
u32 clk;
|
||||
u32 SDOPT;
|
||||
u32 devicenumber;
|
||||
u32 total_size; //size in sectors of the device
|
||||
u32 res;
|
||||
} mmcdevice;
|
||||
|
||||
enum {
|
||||
MMC_DEVICE_SDCARD,
|
||||
MMC_DEVICE_NAND,
|
||||
};
|
||||
|
||||
void my_sdmmc_controller_init(bool force_init);
|
||||
void my_sdmmc_initirq();
|
||||
int my_sdmmc_cardinserted();
|
||||
|
||||
int my_sdmmc_sdcard_init();
|
||||
int my_sdmmc_nand_init();
|
||||
void my_sdmmc_get_cid(int devicenumber, u32 *cid);
|
||||
|
||||
static inline void sdmmc_nand_cid( u32 *cid) {
|
||||
my_sdmmc_get_cid(MMC_DEVICE_NAND, cid);
|
||||
}
|
||||
|
||||
static inline void sdmmc_sdcard_cid( u32 *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_writesectors(u32 sector_no, u32 numsectors, void *in);
|
||||
int my_sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, void *out);
|
||||
int my_sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, void *in);
|
||||
|
||||
extern u32 sdmmc_cid[];
|
||||
extern int sdmmc_curdevice;
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline u16 sdmmc_read16(u16 reg) {
|
||||
//---------------------------------------------------------------------------------
|
||||
return *(vu16*)(SDMMC_BASE + reg);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline void sdmmc_write16(u16 reg, u16 val) {
|
||||
//---------------------------------------------------------------------------------
|
||||
*(vu16*)(SDMMC_BASE + reg) = val;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline u32 sdmmc_read32(u16 reg) {
|
||||
//---------------------------------------------------------------------------------
|
||||
return *(vu32*)(SDMMC_BASE + reg);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline void sdmmc_write32(u16 reg, u32 val) {
|
||||
//---------------------------------------------------------------------------------
|
||||
*(vu32*)(SDMMC_BASE + reg) = val;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline void sdmmc_mask16(u16 reg, u16 clear, u16 set) {
|
||||
//---------------------------------------------------------------------------------
|
||||
u16 val = sdmmc_read16(reg);
|
||||
val &= ~clear;
|
||||
val |= set;
|
||||
sdmmc_write16(reg, val);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------
|
||||
static inline void setckl(u32 data) {
|
||||
//---------------------------------------------------------------------------------
|
||||
sdmmc_mask16(REG_SDCLKCTL, 0x100, 0);
|
||||
sdmmc_mask16(REG_SDCLKCTL, 0x2FF, data & 0x2FF);
|
||||
sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100);
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -15,8 +15,8 @@ include $(DEVKITARM)/ds_rules
|
||||
# all directories are relative to this makefile
|
||||
#---------------------------------------------------------------------------------
|
||||
BUILD := build
|
||||
SOURCES := src
|
||||
INCLUDES := include
|
||||
SOURCES := src src/nand src/nand/polarssl src/nand/twltool
|
||||
INCLUDES := include src/nand
|
||||
DATA :=
|
||||
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ void backupMenu()
|
||||
}
|
||||
else
|
||||
{
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
@ -86,7 +86,7 @@ static int subMenu()
|
||||
|
||||
printMenu(m);
|
||||
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
@ -192,7 +192,7 @@ static void restore(Menu* m)
|
||||
{
|
||||
clearScreen(&bottomScreen);
|
||||
|
||||
if (!copyDir(fpath, "/title"))
|
||||
if (!copyDir(fpath, "nand:/title"))
|
||||
{
|
||||
messagePrint("\x1B[31m\nFailed to restore backup.\n\x1B[47m");
|
||||
}
|
||||
|
||||
@ -13,7 +13,7 @@ static bool _titleIsUsed(tDSiHeader* h)
|
||||
if (!h) return false;
|
||||
|
||||
char path[64];
|
||||
sprintf(path, "/title/%08x/%08x/", (unsigned int)h->tid_high, (unsigned int)h->tid_low);
|
||||
sprintf(path, "nand:/title/%08x/%08x/", (unsigned int)h->tid_high, (unsigned int)h->tid_low);
|
||||
|
||||
return dirExists(path);
|
||||
}
|
||||
@ -345,8 +345,8 @@ bool install(char* fpath, bool systemTitle)
|
||||
printBytes(installSize);
|
||||
iprintf("\n");
|
||||
|
||||
if (!_checkSdSpace(installSize))
|
||||
goto error;
|
||||
// if (!_checkSdSpace(installSize))
|
||||
// goto error;
|
||||
|
||||
//system title patch
|
||||
if (systemTitle)
|
||||
@ -382,14 +382,16 @@ bool install(char* fpath, bool systemTitle)
|
||||
if (_iqueHack(h))
|
||||
fixHeader = true;
|
||||
|
||||
//create title directory /title/XXXXXXXX/XXXXXXXX
|
||||
//create title directory nand:/title/XXXXXXXX/XXXXXXXX
|
||||
char dirPath[32];
|
||||
mkdir("/title", 0777);
|
||||
mkdir("nand:/title", 0777);
|
||||
|
||||
sprintf(dirPath, "/title/%08x", (unsigned int)h->tid_high);
|
||||
sprintf(dirPath, "nand:/title/%08x", (unsigned int)h->tid_high);
|
||||
mkdir(dirPath, 0777);
|
||||
|
||||
sprintf(dirPath, "/title/%08x/%08x", (unsigned int)h->tid_high, (unsigned int)h->tid_low);
|
||||
sprintf(dirPath, "nand:/title/%08x/%08x", (unsigned int)h->tid_high, (unsigned int)h->tid_low);
|
||||
|
||||
nandWritten = true;
|
||||
|
||||
//check if title is free
|
||||
if (_titleIsUsed(h))
|
||||
@ -413,7 +415,7 @@ bool install(char* fpath, bool systemTitle)
|
||||
|
||||
mkdir(dirPath, 0777);
|
||||
|
||||
//content folder /title/XXXXXXXX/XXXXXXXXX/content
|
||||
//content folder nand:/title/XXXXXXXX/XXXXXXXXX/content
|
||||
{
|
||||
char contentPath[64];
|
||||
sprintf(contentPath, "%s/content", dirPath);
|
||||
|
||||
@ -24,7 +24,7 @@ static void _setHeader(Menu* m)
|
||||
{
|
||||
if (!m) return;
|
||||
if (currentDir[0] == '\0')
|
||||
setMenuHeader(m, "/");
|
||||
setMenuHeader(m, "sd:/");
|
||||
else
|
||||
setMenuHeader(m, currentDir);
|
||||
}
|
||||
@ -49,7 +49,7 @@ void installMenu()
|
||||
}
|
||||
else*/
|
||||
{
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
@ -151,7 +151,7 @@ static void generateList(Menu* m)
|
||||
DIR* dir = NULL;
|
||||
|
||||
if (currentDir[0] == '\0')
|
||||
dir = opendir("/");
|
||||
dir = opendir("sd:/");
|
||||
else
|
||||
dir = opendir(currentDir);
|
||||
|
||||
@ -253,7 +253,7 @@ static int subMenu()
|
||||
|
||||
printMenu(m);
|
||||
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
|
||||
@ -1,10 +1,14 @@
|
||||
#include "main.h"
|
||||
#include "menu.h"
|
||||
#include "message.h"
|
||||
#include "nand/nandio.h"
|
||||
#include <time.h>
|
||||
|
||||
#define VERSION "0.7.1"
|
||||
|
||||
bool programEnd = false;
|
||||
bool nandWritten = false;
|
||||
|
||||
PrintConsole topScreen;
|
||||
PrintConsole bottomScreen;
|
||||
|
||||
@ -40,9 +44,14 @@ static int _mainMenu(int cursor)
|
||||
//top screen
|
||||
clearScreen(&topScreen);
|
||||
|
||||
iprintf("\t Title Manager for NAND\n");
|
||||
iprintf("\t\t\tmodified from\n");
|
||||
iprintf("\tTitle Manager for HiyaCFW\n");
|
||||
iprintf("\nversion %s\n", VERSION);
|
||||
iprintf("\x1b[23;0HJeff - 2018-2019");
|
||||
iprintf("\n\n\x1B[41mWARNING:\x1B[47m This tool writes to\nyour internal NAND!\n\nThis always has a risk, albeit\nlow, of \x1B[41mbricking\x1B[47m your system\nand should be done with caution!\n");
|
||||
iprintf("\nDo not exit by holding POWER,\ntap it or choose \"Shut Down\".\n");
|
||||
iprintf("\x1b[22;0HJeff - 2018-2019");
|
||||
iprintf("\x1b[23;0HPk11 - 2022");
|
||||
|
||||
//menu
|
||||
Menu* m = newMenu();
|
||||
@ -59,7 +68,7 @@ static int _mainMenu(int cursor)
|
||||
//bottom screen
|
||||
printMenu(m);
|
||||
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
@ -77,6 +86,11 @@ static int _mainMenu(int cursor)
|
||||
return result;
|
||||
}
|
||||
|
||||
void fifoHandler(u32 value32, void* userdata) {
|
||||
if(value32 == 0x54495845) // 'EXIT'
|
||||
programEnd = true;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
srand(time(0));
|
||||
@ -96,10 +110,21 @@ int main(int argc, char **argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
//setup nand access
|
||||
if (!fatMountSimple("nand", &io_dsi_nand))
|
||||
{
|
||||
messageBox("nand init \x1B[31mfailed\n\x1B[47m");
|
||||
return 0;
|
||||
}
|
||||
|
||||
messageBox("\x1B[41mWARNING:\x1B[47m This tool writes to\nyour internal NAND!\n\nThis always has a risk, albeit\nlow, of \x1B[41mbricking\x1B[47m your system\nand should be done with caution!\n");
|
||||
messageBox("Do not exit by holding POWER,\ntap it or choose \"Shut Down\".\n");
|
||||
|
||||
//main menu
|
||||
bool programEnd = false;
|
||||
int cursor = 0;
|
||||
|
||||
fifoSetValue32Handler(FIFO_USER_01, fifoHandler, NULL);
|
||||
|
||||
while (!programEnd)
|
||||
{
|
||||
cursor = _mainMenu(cursor);
|
||||
@ -128,6 +153,16 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
clearScreen(&bottomScreen);
|
||||
printf("Unmounting NAND...\n");
|
||||
fatUnmount("nand:");
|
||||
if(nandWritten) {
|
||||
printf("Merging stages...\n");
|
||||
nandio_shutdown();
|
||||
}
|
||||
|
||||
fifoSendValue32(FIFO_USER_02, 0x54495845); // 'EXIT'
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -5,6 +5,9 @@
|
||||
#include <fat.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern bool programEnd;
|
||||
extern bool nandWritten;
|
||||
|
||||
void installMenu();
|
||||
void titleMenu();
|
||||
void backupMenu();
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
|
||||
void keyWait(u32 key)
|
||||
{
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
@ -25,7 +25,7 @@ bool choiceBox(char* message)
|
||||
iprintf("\x1B[47m"); //white
|
||||
iprintf("\x1b[%d;0H\tYes\n\tNo\n", choiceRow);
|
||||
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
@ -62,7 +62,7 @@ bool choicePrint(char* message)
|
||||
iprintf("\x1B[47m"); //white
|
||||
iprintf("Yes - [A]\nNo - [B]\n");
|
||||
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
@ -95,7 +95,7 @@ void messagePrint(char* message)
|
||||
iprintf("%s\n", message);
|
||||
iprintf("\nOkay - [A]\n");
|
||||
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
|
||||
124
arm9/src/nand/crypto.c
Normal file
124
arm9/src/nand/crypto.c
Normal file
@ -0,0 +1,124 @@
|
||||
#include <stdint.h>
|
||||
#include "crypto.h"
|
||||
#include "u128_math.h"
|
||||
#include "f_xy.h"
|
||||
#include "twltool/dsi.h"
|
||||
|
||||
// more info:
|
||||
// https://github.com/Jimmy-Z/TWLbf/blob/master/dsi.c
|
||||
// https://github.com/Jimmy-Z/bfCL/blob/master/dsi.h
|
||||
// ported back to 32 bit for ARM9
|
||||
|
||||
static dsi_context nand_ctx ;
|
||||
static dsi_context boot2_ctx ;
|
||||
static dsi_context es_ctx ;
|
||||
|
||||
static uint8_t nand_ctr_iv[16];
|
||||
static uint8_t boot2_ctr[16];
|
||||
|
||||
static void generate_key(uint8_t *generated_key, const uint32_t *console_id, const key_mode_t mode)
|
||||
{
|
||||
uint32_t key[4];
|
||||
switch (mode)
|
||||
{
|
||||
case NAND:
|
||||
key[0] = console_id[0];
|
||||
key[1] = console_id[0] ^ KEYSEED_DSI_NAND_0;
|
||||
key[2] = console_id[1] ^ KEYSEED_DSI_NAND_1;
|
||||
key[3] = console_id[1];
|
||||
break;
|
||||
case NAND_3DS:
|
||||
key[0] = (console_id[0] ^ KEYSEED_3DS_NAND_0) | 0x80000000;
|
||||
key[1] = KEYSEED_3DS_NAND_1;
|
||||
key[2] = KEYSEED_3DS_NAND_2;
|
||||
key[3] = console_id[1] ^ KEYSEED_3DS_NAND_3;
|
||||
break;
|
||||
case ES:
|
||||
key[0] = KEYSEED_ES_0;
|
||||
key[1] = KEYSEED_ES_1;
|
||||
key[2] = console_id[1] ^ KEYSEED_ES_2;
|
||||
key[3] = console_id[0];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
u128_xor((uint8_t *)key, mode == ES ? DSi_ES_KEY_Y : DSi_NAND_KEY_Y);
|
||||
u128_add((uint8_t *)key, DSi_KEY_MAGIC);
|
||||
u128_lrot((uint8_t *)key, 42) ;
|
||||
memcpy(generated_key, key, 16) ;
|
||||
}
|
||||
|
||||
int dsi_sha1_verify(const void *digest_verify, const void *data, unsigned len)
|
||||
{
|
||||
uint8_t digest[SHA1_LEN];
|
||||
swiSHA1Calc(digest, data, len);
|
||||
return memcmp(digest, digest_verify, SHA1_LEN);
|
||||
}
|
||||
|
||||
void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int is3DS)
|
||||
{
|
||||
uint32_t console_id[2];
|
||||
GET_UINT32_BE(console_id[0], console_id_be, 4);
|
||||
GET_UINT32_BE(console_id[1], console_id_be, 0);
|
||||
|
||||
uint8_t key[16];
|
||||
generate_key(key, console_id, is3DS ? NAND_3DS : NAND);
|
||||
dsi_set_key(&nand_ctx, key) ;
|
||||
|
||||
generate_key(key, console_id, ES);
|
||||
dsi_set_key(&es_ctx, key) ;
|
||||
|
||||
dsi_set_key(&boot2_ctx, DSi_BOOT2_KEY) ;
|
||||
|
||||
swiSHA1Calc(nand_ctr_iv, emmc_cid, 16);
|
||||
|
||||
}
|
||||
|
||||
// crypt one block, in/out must be aligned to 32 bit(restriction induced by xor_128)
|
||||
// offset as block offset, block as AES block
|
||||
void dsi_nand_crypt_1(uint8_t* out, const uint8_t* in, uint32_t offset)
|
||||
{
|
||||
uint8_t ctr[16] ;
|
||||
memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)) ;
|
||||
u128_add32(ctr, offset);
|
||||
dsi_set_ctr(&nand_ctx, ctr) ;
|
||||
dsi_crypt_ctr(&nand_ctx, in, out, 16) ;
|
||||
}
|
||||
|
||||
void dsi_nand_crypt(uint8_t* out, const uint8_t* in, uint32_t offset, unsigned count)
|
||||
{
|
||||
uint8_t ctr[16] ;
|
||||
memcpy(ctr, nand_ctr_iv, sizeof(nand_ctr_iv)) ;
|
||||
u128_add32(ctr, offset);
|
||||
for (unsigned i = 0; i < count; ++i)
|
||||
{
|
||||
dsi_set_ctr(&nand_ctx, ctr) ;
|
||||
dsi_crypt_ctr(&nand_ctx, in, out, 16) ;
|
||||
out += AES_BLOCK_SIZE;
|
||||
in += AES_BLOCK_SIZE;
|
||||
u128_add32(ctr, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void dsi_boot2_crypt_set_ctr(uint32_t size_r)
|
||||
{
|
||||
for (int i=0;i<4;i++)
|
||||
{
|
||||
boot2_ctr[i] = (size_r) >> (8*i) ;
|
||||
boot2_ctr[i+4] = (-size_r) >> (8*i) ;
|
||||
boot2_ctr[i+8] = (~size_r) >> (8*i) ;
|
||||
boot2_ctr[i+12] = 0 ;
|
||||
}
|
||||
}
|
||||
|
||||
void dsi_boot2_crypt(uint8_t* out, const uint8_t* in, unsigned count)
|
||||
{
|
||||
for (unsigned i = 0; i < count; ++i)
|
||||
{
|
||||
dsi_set_ctr(&boot2_ctx, boot2_ctr) ;
|
||||
dsi_crypt_ctr(&boot2_ctx, in, out, 16) ;
|
||||
out += AES_BLOCK_SIZE;
|
||||
in += AES_BLOCK_SIZE;
|
||||
u128_add32(boot2_ctr, 1);
|
||||
}
|
||||
}
|
||||
70
arm9/src/nand/crypto.h
Normal file
70
arm9/src/nand/crypto.h
Normal file
@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include <nds.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
#define AES_BLOCK_SIZE 16
|
||||
#define SHA1_LEN 20
|
||||
|
||||
#define KEYSEED_DSI_NAND_0 0x24ee6906
|
||||
#define KEYSEED_DSI_NAND_1 0xe65b601d
|
||||
|
||||
#define KEYSEED_3DS_NAND_0 0xb358a6af
|
||||
#define KEYSEED_3DS_NAND_1 0x544e494e
|
||||
#define KEYSEED_3DS_NAND_2 0x4f444e45
|
||||
#define KEYSEED_3DS_NAND_3 0x08c267b7
|
||||
|
||||
#define KEYSEED_ES_0 0x4e00004a
|
||||
#define KEYSEED_ES_1 0x4a00004e
|
||||
#define KEYSEED_ES_2 0xc80c4b72
|
||||
|
||||
#define GET_UINT32_BE(n, b, i) \
|
||||
((uint8_t*)&(n))[0] = (b)[i + 3]; \
|
||||
((uint8_t*)&(n))[1] = (b)[i + 2]; \
|
||||
((uint8_t*)&(n))[2] = (b)[i + 1]; \
|
||||
((uint8_t*)&(n))[3] = (b)[i + 0]
|
||||
|
||||
#define PUT_UINT32_BE(n, b, i) \
|
||||
(b)[i + 0] = ((uint8_t*)&(n))[3]; \
|
||||
(b)[i + 1] = ((uint8_t*)&(n))[2]; \
|
||||
(b)[i + 2] = ((uint8_t*)&(n))[1]; \
|
||||
(b)[i + 3] = ((uint8_t*)&(n))[0]
|
||||
|
||||
|
||||
typedef enum {
|
||||
ENCRYPT,
|
||||
DECRYPT
|
||||
} crypt_mode_t;
|
||||
|
||||
typedef enum {
|
||||
NAND,
|
||||
NAND_3DS,
|
||||
ES
|
||||
} key_mode_t;
|
||||
|
||||
|
||||
// don't want to include nds.h just for this
|
||||
void swiSHA1Calc(void *digest, const void *buf, size_t len);
|
||||
|
||||
int dsi_sha1_verify(const void *digest_verify, const void *data, unsigned len);
|
||||
|
||||
void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int is3DS);
|
||||
|
||||
void dsi_nand_crypt_1(uint8_t *out, const uint8_t* in, u32 offset);
|
||||
|
||||
void dsi_nand_crypt(uint8_t *out, const uint8_t* in, u32 offset, unsigned count);
|
||||
|
||||
int dsi_es_block_crypt(uint8_t *buf, unsigned buf_len, crypt_mode_t mode);
|
||||
|
||||
void dsi_boot2_crypt_set_ctr(uint32_t size_r);
|
||||
|
||||
void dsi_boot2_crypt(uint8_t* out, const uint8_t* in, unsigned count);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
50
arm9/src/nand/f_xy.c
Normal file
50
arm9/src/nand/f_xy.c
Normal file
@ -0,0 +1,50 @@
|
||||
/* f_xy.c
|
||||
*
|
||||
* This file was imported from godmode9i, but it is liely not the
|
||||
* original source. twltool uses the same file.
|
||||
*
|
||||
* 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
|
||||
* from tonccpy.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include "u128_math.h"
|
||||
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
const uint8_t DSi_KEY_MAGIC[16] = { 0x79, 0x3e, 0x4f, 0x1a, 0x5f, 0x0f, 0x68, 0x2a,
|
||||
0x58, 0x02, 0x59, 0x29, 0x4e, 0xfb, 0xfe, 0xff };
|
||||
const uint8_t DSi_NAND_KEY_Y[16] = { 0x76, 0xdc, 0xb9, 0x0a, 0xd3, 0xc4, 0x4d, 0xbd,
|
||||
0x1d, 0xdd, 0x2d, 0x20, 0x05, 0x00, 0xa0, 0xe1 };
|
||||
const uint8_t DSi_ES_KEY_Y[16] = { 0xe5, 0xcc, 0x5a, 0x8b, 0x56, 0xd0, 0xc9,0x72,
|
||||
0x9c, 0x17, 0xe8, 0xdc, 0x39, 0x12, 0x36, 0xa9 };
|
||||
const uint8_t DSi_BOOT2_KEY[16] = { 0x98, 0xee, 0x80, 0x80, 0x00, 0x6c, 0xb4, 0xf6,
|
||||
0x3a, 0xc2, 0x6e, 0x62, 0xf9, 0xec, 0x34, 0xad };
|
||||
|
||||
/************************ Functions *******************************************/
|
||||
|
||||
void F_XY(uint8_t *key, const uint8_t *key_x, const uint8_t *key_y)
|
||||
{
|
||||
uint8_t key_xy[16];
|
||||
|
||||
for(int i=0; i<16; i++)
|
||||
key_xy[i] = key_x[i] ^ key_y[i];
|
||||
|
||||
memcpy(key, DSi_KEY_MAGIC, sizeof(DSi_KEY_MAGIC));
|
||||
|
||||
u128_add(key, key_xy);
|
||||
u128_lrot(key, 42);
|
||||
}
|
||||
|
||||
//F_XY_reverse does the reverse of F(X^Y): takes (normal)key, and does F in reverse to generate the original X^Y key_xy.
|
||||
void F_XY_reverse(const uint8_t *key, uint8_t *key_xy)
|
||||
{
|
||||
memcpy(key_xy, key, 16);
|
||||
u128_rrot(key_xy, 42);
|
||||
u128_sub(key_xy, DSi_KEY_MAGIC);
|
||||
}
|
||||
|
||||
36
arm9/src/nand/f_xy.h
Normal file
36
arm9/src/nand/f_xy.h
Normal file
@ -0,0 +1,36 @@
|
||||
/* f_xy.h
|
||||
*
|
||||
* This file was imported from godmode9i, but it is liely not the
|
||||
* original source. twltool uses the same file.
|
||||
*
|
||||
* 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
|
||||
* from tonccpy.
|
||||
*/
|
||||
|
||||
#ifndef _H_F_XY
|
||||
#define _H_F_XY
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
extern const uint8_t DSi_KEY_MAGIC[16] ;
|
||||
extern const uint8_t DSi_NAND_KEY_Y[16] ;
|
||||
extern const uint8_t DSi_ES_KEY_Y[16] ;
|
||||
extern const uint8_t DSi_BOOT2_KEY[16] ;
|
||||
|
||||
/************************ Function Protoypes **********************************/
|
||||
|
||||
void F_XY(uint8_t *key, const uint8_t *key_x, const uint8_t *key_y);
|
||||
void F_XY_reverse(const uint8_t *key, uint8_t *key_xy);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
227
arm9/src/nand/nandio.c
Normal file
227
arm9/src/nand/nandio.c
Normal file
@ -0,0 +1,227 @@
|
||||
|
||||
#include <nds.h>
|
||||
#include <nds/disc_io.h>
|
||||
#include <malloc.h>
|
||||
#include <stdio.h>
|
||||
#include "crypto.h"
|
||||
#include "sector0.h"
|
||||
#include "f_xy.h"
|
||||
#include "nandio.h"
|
||||
#include "u128_math.h"
|
||||
|
||||
/************************ Function Protoypes **********************************/
|
||||
|
||||
bool nandio_startup() ;
|
||||
bool nandio_is_inserted() ;
|
||||
bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer) ;
|
||||
bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer) ;
|
||||
bool nandio_clear_status() ;
|
||||
bool nandio_shutdown() ;
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
const DISC_INTERFACE io_dsi_nand = {
|
||||
NAND_DEVICENAME,
|
||||
FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE,
|
||||
nandio_startup,
|
||||
nandio_is_inserted,
|
||||
nandio_read_sectors,
|
||||
nandio_write_sectors,
|
||||
nandio_clear_status,
|
||||
nandio_shutdown
|
||||
};
|
||||
|
||||
bool is3DS;
|
||||
|
||||
extern bool nand_Startup();
|
||||
|
||||
static u8* crypt_buf = 0;
|
||||
|
||||
static u32 fat_sig_fix_offset = 0;
|
||||
|
||||
static u32 sector_buf32[SECTOR_SIZE/sizeof(u32)];
|
||||
static u8 *sector_buf = (u8*)sector_buf32;
|
||||
|
||||
void nandio_set_fat_sig_fix(u32 offset)
|
||||
{
|
||||
fat_sig_fix_offset = offset;
|
||||
}
|
||||
|
||||
void getConsoleID(u8 *consoleID)
|
||||
{
|
||||
u8 *fifo=(u8*)0x02300000; //shared mem address that has our computed key3 stuff
|
||||
u8 key[16]; //key3 normalkey - keyslot 3 is used for DSi/twln NAND crypto
|
||||
u8 key_x[16];////key3_x - contains a DSi console id (which just happens to be the LFCS on 3ds)
|
||||
|
||||
memcpy(key, fifo, 16); //receive the goods from arm7
|
||||
|
||||
F_XY_reverse(key, key_x); //work backwards from the normalkey to get key_x that has the consoleID
|
||||
|
||||
u128_xor(key_x, DSi_NAND_KEY_Y) ;
|
||||
|
||||
memcpy(&consoleID[0], &key_x[0], 4);
|
||||
memcpy(&consoleID[4], &key_x[0xC], 4);
|
||||
}
|
||||
|
||||
bool nandio_startup()
|
||||
{
|
||||
if (!nand_Startup())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
nand_ReadSectors(0, 1, sector_buf);
|
||||
is3DS = parse_ncsd(sector_buf) == 0;
|
||||
//if (is3DS) return false;
|
||||
|
||||
u8 consoleID[8];
|
||||
u8 consoleIDfixed[8];
|
||||
|
||||
// Get ConsoleID
|
||||
getConsoleID(consoleID);
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
consoleIDfixed[i] = consoleID[7-i];
|
||||
}
|
||||
// iprintf("sector 0 is %s\n", is3DS ? "3DS" : "DSi");
|
||||
dsi_crypt_init((const u8*)consoleIDfixed, (const u8*)0x2FFD7BC, is3DS);
|
||||
dsi_nand_crypt(sector_buf, sector_buf, 0, SECTOR_SIZE / AES_BLOCK_SIZE);
|
||||
|
||||
parse_mbr(sector_buf, is3DS);
|
||||
|
||||
mbr_t *mbr = (mbr_t*)sector_buf;
|
||||
|
||||
nandio_set_fat_sig_fix(is3DS ? 0 : mbr->partitions[0].offset);
|
||||
|
||||
if (crypt_buf == 0)
|
||||
{
|
||||
crypt_buf = (u8*)memalign(32, SECTOR_SIZE * CRYPT_BUF_LEN);
|
||||
}
|
||||
|
||||
return crypt_buf != 0;
|
||||
}
|
||||
|
||||
bool nandio_is_inserted()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// len is guaranteed <= CRYPT_BUF_LEN
|
||||
static bool read_sectors(sec_t start, sec_t len, void *buffer)
|
||||
{
|
||||
if (nand_ReadSectors(start, len, crypt_buf))
|
||||
{
|
||||
dsi_nand_crypt(buffer, crypt_buf, start * SECTOR_SIZE / AES_BLOCK_SIZE, len * SECTOR_SIZE / AES_BLOCK_SIZE);
|
||||
if (fat_sig_fix_offset &&
|
||||
start == fat_sig_fix_offset
|
||||
&& ((u8*)buffer)[0x36] == 0
|
||||
&& ((u8*)buffer)[0x37] == 0
|
||||
&& ((u8*)buffer)[0x38] == 0)
|
||||
{
|
||||
((u8*)buffer)[0x36] = 'F';
|
||||
((u8*)buffer)[0x37] = 'A';
|
||||
((u8*)buffer)[0x38] = 'T';
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// len is guaranteed <= CRYPT_BUF_LEN
|
||||
static bool write_sectors(sec_t start, sec_t len, const void *buffer)
|
||||
{
|
||||
static u8 writeCopy[SECTOR_SIZE*16] ;
|
||||
memcpy(writeCopy, buffer, len * SECTOR_SIZE) ;
|
||||
|
||||
dsi_nand_crypt(crypt_buf, writeCopy, start * SECTOR_SIZE / AES_BLOCK_SIZE, len * SECTOR_SIZE / AES_BLOCK_SIZE);
|
||||
if (nand_WriteSectors(start, len, crypt_buf))
|
||||
{
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer)
|
||||
{
|
||||
while (len >= CRYPT_BUF_LEN)
|
||||
{
|
||||
if (!read_sectors(offset, CRYPT_BUF_LEN, buffer))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
offset += CRYPT_BUF_LEN;
|
||||
len -= CRYPT_BUF_LEN;
|
||||
buffer = ((u8*)buffer) + SECTOR_SIZE * CRYPT_BUF_LEN;
|
||||
}
|
||||
if (len > 0)
|
||||
{
|
||||
return read_sectors(offset, len, buffer);
|
||||
} else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer)
|
||||
{
|
||||
while (len >= CRYPT_BUF_LEN)
|
||||
{
|
||||
if (!write_sectors(offset, CRYPT_BUF_LEN, buffer))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
offset += CRYPT_BUF_LEN;
|
||||
len -= CRYPT_BUF_LEN;
|
||||
buffer = ((u8*)buffer) + SECTOR_SIZE * CRYPT_BUF_LEN;
|
||||
}
|
||||
if (len > 0)
|
||||
{
|
||||
return write_sectors(offset, len, buffer);
|
||||
} else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool nandio_clear_status()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool nandio_shutdown()
|
||||
{
|
||||
// at cleanup we synchronize the FAT statgings
|
||||
// A FatFS might have multiple copies of the FAT.
|
||||
// we will get them back synchonized as we just worked on the first copy
|
||||
// this allows us to revert changes in the FAT if we did not properly finish
|
||||
// and did not push the changes to the other copies
|
||||
// to do this we read the first partition sector
|
||||
nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf) ;
|
||||
u8 stagingLevels = sector_buf[0x10] ;
|
||||
u8 reservedSectors = sector_buf[0x0E] ;
|
||||
u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8) ;
|
||||
/*
|
||||
iprintf("[i] Staging for %i FAT copies\n",stagingLevels);
|
||||
iprintf("[i] Stages starting at %i\n",reservedSectors);
|
||||
iprintf("[i] %i sectors per stage\n",sectorsPerFatCopy);
|
||||
*/
|
||||
if (stagingLevels > 1)
|
||||
{
|
||||
for (u32 sector = 0;sector < sectorsPerFatCopy; sector++)
|
||||
{
|
||||
// read fat sector
|
||||
nandio_read_sectors(fat_sig_fix_offset + reservedSectors + sector, 1, sector_buf) ;
|
||||
// write to each copy, except the source copy
|
||||
for (int stage = 1;stage < stagingLevels;stage++)
|
||||
{
|
||||
nandio_write_sectors(fat_sig_fix_offset + reservedSectors + sector + (stage *sectorsPerFatCopy), 1, sector_buf) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
free(crypt_buf);
|
||||
crypt_buf = 0;
|
||||
return true;
|
||||
}
|
||||
27
arm9/src/nand/nandio.h
Normal file
27
arm9/src/nand/nandio.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <nds/disc_io.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
#define CRYPT_BUF_LEN 64
|
||||
#define NAND_DEVICENAME (('N' << 24) | ('A' << 16) | ('N' << 8) | 'D')
|
||||
|
||||
extern const DISC_INTERFACE io_dsi_nand;
|
||||
|
||||
/************************ Function Protoypes **********************************/
|
||||
|
||||
void nandio_set_fat_sig_fix(uint32_t offset);
|
||||
|
||||
void getConsoleID(uint8_t *consoleID) ;
|
||||
|
||||
extern bool nandio_shutdown() ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
1164
arm9/src/nand/polarssl/aes.c
Normal file
1164
arm9/src/nand/polarssl/aes.c
Normal file
File diff suppressed because it is too large
Load Diff
139
arm9/src/nand/polarssl/aes.h
Normal file
139
arm9/src/nand/polarssl/aes.h
Normal file
@ -0,0 +1,139 @@
|
||||
/**
|
||||
* \file aes.h
|
||||
*
|
||||
* Copyright (C) 2006-2010, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_AES_H
|
||||
#define POLARSSL_AES_H
|
||||
|
||||
#define AES_ENCRYPT 1
|
||||
#define AES_DECRYPT 0
|
||||
|
||||
#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0800
|
||||
#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0810
|
||||
|
||||
/**
|
||||
* \brief AES context structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int nr; /*!< number of rounds */
|
||||
unsigned long *rk; /*!< AES round keys */
|
||||
unsigned long buf[68]; /*!< unaligned data */
|
||||
}
|
||||
aes_context;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief AES key schedule (encryption)
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
* \param key encryption key
|
||||
* \param keysize must be 128, 192 or 256
|
||||
*
|
||||
* \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int aes_setkey_enc( aes_context *ctx, const unsigned char *key, int keysize );
|
||||
|
||||
/**
|
||||
* \brief AES key schedule (decryption)
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
* \param key decryption key
|
||||
* \param keysize must be 128, 192 or 256
|
||||
*
|
||||
* \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int aes_setkey_dec( aes_context *ctx, const unsigned char *key, int keysize );
|
||||
|
||||
/**
|
||||
* \brief AES-ECB block encryption/decryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param input 16-byte input block
|
||||
* \param output 16-byte output block
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int aes_crypt_ecb( aes_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] );
|
||||
|
||||
/**
|
||||
* \brief AES-CBC buffer encryption/decryption
|
||||
* Length should be a multiple of the block
|
||||
* size (16 bytes)
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
|
||||
*/
|
||||
int aes_crypt_cbc( aes_context *ctx,
|
||||
int mode,
|
||||
int length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
/**
|
||||
* \brief AES-CFB128 buffer encryption/decryption.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv_off offset in IV (updated after use)
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int aes_crypt_cfb128( aes_context *ctx,
|
||||
int mode,
|
||||
int length,
|
||||
int *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int aes_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* aes.h */
|
||||
2452
arm9/src/nand/polarssl/bignum.c
Normal file
2452
arm9/src/nand/polarssl/bignum.c
Normal file
File diff suppressed because it is too large
Load Diff
761
arm9/src/nand/polarssl/bignum.h
Normal file
761
arm9/src/nand/polarssl/bignum.h
Normal file
@ -0,0 +1,761 @@
|
||||
/**
|
||||
* \file bignum.h
|
||||
*
|
||||
* \brief Multi-precision integer library
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
#ifndef MBEDTLS_BIGNUM_H
|
||||
#define MBEDTLS_BIGNUM_H
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
|
||||
#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
|
||||
#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
|
||||
#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
|
||||
#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
|
||||
#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
|
||||
#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
|
||||
#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
|
||||
|
||||
#define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
|
||||
|
||||
/*
|
||||
* Maximum size MPIs are allowed to grow to in number of limbs.
|
||||
*/
|
||||
#define MBEDTLS_MPI_MAX_LIMBS 10000
|
||||
|
||||
#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
|
||||
/*
|
||||
* Maximum window size used for modular exponentiation. Default: 6
|
||||
* Minimum value: 1. Maximum value: 6.
|
||||
*
|
||||
* Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
|
||||
* for the sliding window calculation. (So 64 by default)
|
||||
*
|
||||
* Reduction in size, reduces speed.
|
||||
*/
|
||||
#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
|
||||
#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
|
||||
|
||||
#if !defined(MBEDTLS_MPI_MAX_SIZE)
|
||||
/*
|
||||
* Maximum size of MPIs allowed in bits and bytes for user-MPIs.
|
||||
* ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
|
||||
*
|
||||
* Note: Calculations can results temporarily in larger MPIs. So the number
|
||||
* of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
|
||||
*/
|
||||
#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
|
||||
#endif /* !MBEDTLS_MPI_MAX_SIZE */
|
||||
|
||||
#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
|
||||
|
||||
/*
|
||||
* When reading from files with mbedtls_mpi_read_file() and writing to files with
|
||||
* mbedtls_mpi_write_file() the buffer should have space
|
||||
* for a (short) label, the MPI (in the provided radix), the newline
|
||||
* characters and the '\0'.
|
||||
*
|
||||
* By default we assume at least a 10 char label, a minimum radix of 10
|
||||
* (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
|
||||
* Autosized at compile time for at least a 10 char label, a minimum radix
|
||||
* of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
|
||||
*
|
||||
* This used to be statically sized to 1250 for a maximum of 4096 bit
|
||||
* numbers (1234 decimal chars).
|
||||
*
|
||||
* Calculate using the formula:
|
||||
* MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
|
||||
* LabelSize + 6
|
||||
*/
|
||||
#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
|
||||
#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
|
||||
#define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
|
||||
|
||||
/*
|
||||
* Define the base integer type, architecture-wise.
|
||||
*
|
||||
* 32 or 64-bit integer types can be forced regardless of the underlying
|
||||
* architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
|
||||
* respectively and undefining MBEDTLS_HAVE_ASM.
|
||||
*
|
||||
* Double-width integers (e.g. 128-bit in 64-bit architectures) can be
|
||||
* disabled by defining MBEDTLS_NO_UDBL_DIVISION.
|
||||
*/
|
||||
#if !defined(MBEDTLS_HAVE_INT32)
|
||||
#if defined(_MSC_VER) && defined(_M_AMD64)
|
||||
/* Always choose 64-bit when using MSC */
|
||||
#if !defined(MBEDTLS_HAVE_INT64)
|
||||
#define MBEDTLS_HAVE_INT64
|
||||
#endif /* !MBEDTLS_HAVE_INT64 */
|
||||
typedef int64_t mbedtls_mpi_sint;
|
||||
typedef uint64_t mbedtls_mpi_uint;
|
||||
#elif defined(__GNUC__) && ( \
|
||||
defined(__amd64__) || defined(__x86_64__) || \
|
||||
defined(__ppc64__) || defined(__powerpc64__) || \
|
||||
defined(__ia64__) || defined(__alpha__) || \
|
||||
( defined(__sparc__) && defined(__arch64__) ) || \
|
||||
defined(__s390x__) || defined(__mips64) )
|
||||
#if !defined(MBEDTLS_HAVE_INT64)
|
||||
#define MBEDTLS_HAVE_INT64
|
||||
#endif /* MBEDTLS_HAVE_INT64 */
|
||||
typedef int64_t mbedtls_mpi_sint;
|
||||
typedef uint64_t mbedtls_mpi_uint;
|
||||
#if !defined(MBEDTLS_NO_UDBL_DIVISION)
|
||||
/* mbedtls_t_udbl defined as 128-bit unsigned int */
|
||||
typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
|
||||
#define MBEDTLS_HAVE_UDBL
|
||||
#endif /* !MBEDTLS_NO_UDBL_DIVISION */
|
||||
#elif defined(__ARMCC_VERSION) && defined(__aarch64__)
|
||||
/*
|
||||
* __ARMCC_VERSION is defined for both armcc and armclang and
|
||||
* __aarch64__ is only defined by armclang when compiling 64-bit code
|
||||
*/
|
||||
#if !defined(MBEDTLS_HAVE_INT64)
|
||||
#define MBEDTLS_HAVE_INT64
|
||||
#endif /* !MBEDTLS_HAVE_INT64 */
|
||||
typedef int64_t mbedtls_mpi_sint;
|
||||
typedef uint64_t mbedtls_mpi_uint;
|
||||
#if !defined(MBEDTLS_NO_UDBL_DIVISION)
|
||||
/* mbedtls_t_udbl defined as 128-bit unsigned int */
|
||||
typedef __uint128_t mbedtls_t_udbl;
|
||||
#define MBEDTLS_HAVE_UDBL
|
||||
#endif /* !MBEDTLS_NO_UDBL_DIVISION */
|
||||
#elif defined(MBEDTLS_HAVE_INT64)
|
||||
/* Force 64-bit integers with unknown compiler */
|
||||
typedef int64_t mbedtls_mpi_sint;
|
||||
typedef uint64_t mbedtls_mpi_uint;
|
||||
#endif
|
||||
#endif /* !MBEDTLS_HAVE_INT32 */
|
||||
|
||||
#if !defined(MBEDTLS_HAVE_INT64)
|
||||
/* Default to 32-bit compilation */
|
||||
#if !defined(MBEDTLS_HAVE_INT32)
|
||||
#define MBEDTLS_HAVE_INT32
|
||||
#endif /* !MBEDTLS_HAVE_INT32 */
|
||||
typedef int32_t mbedtls_mpi_sint;
|
||||
typedef uint32_t mbedtls_mpi_uint;
|
||||
#if !defined(MBEDTLS_NO_UDBL_DIVISION)
|
||||
typedef uint64_t mbedtls_t_udbl;
|
||||
#define MBEDTLS_HAVE_UDBL
|
||||
#endif /* !MBEDTLS_NO_UDBL_DIVISION */
|
||||
#endif /* !MBEDTLS_HAVE_INT64 */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief MPI structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int s; /*!< integer sign */
|
||||
size_t n; /*!< total # of limbs */
|
||||
mbedtls_mpi_uint *p; /*!< pointer to limbs */
|
||||
}
|
||||
mbedtls_mpi;
|
||||
|
||||
/**
|
||||
* \brief Initialize one MPI (make internal references valid)
|
||||
* This just makes it ready to be set or freed,
|
||||
* but does not define a value for the MPI.
|
||||
*
|
||||
* \param X One MPI to initialize.
|
||||
*/
|
||||
void mbedtls_mpi_init( mbedtls_mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Unallocate one MPI
|
||||
*
|
||||
* \param X One MPI to unallocate.
|
||||
*/
|
||||
void mbedtls_mpi_free( mbedtls_mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Enlarge to the specified number of limbs
|
||||
*
|
||||
* \param X MPI to grow
|
||||
* \param nblimbs The target number of limbs
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
|
||||
|
||||
/**
|
||||
* \brief Resize down, keeping at least the specified number of limbs
|
||||
*
|
||||
* \param X MPI to shrink
|
||||
* \param nblimbs The minimum number of limbs to keep
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
|
||||
|
||||
/**
|
||||
* \brief Copy the contents of Y into X
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param Y Source MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Swap the contents of X and Y
|
||||
*
|
||||
* \param X First MPI value
|
||||
* \param Y Second MPI value
|
||||
*/
|
||||
void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Safe conditional assignement X = Y if assign is 1
|
||||
*
|
||||
* \param X MPI to conditionally assign to
|
||||
* \param Y Value to be assigned
|
||||
* \param assign 1: perform the assignment, 0: keep X's original value
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
*
|
||||
* \note This function is equivalent to
|
||||
* if( assign ) mbedtls_mpi_copy( X, Y );
|
||||
* except that it avoids leaking any information about whether
|
||||
* the assignment was done or not (the above code may leak
|
||||
* information through branch prediction and/or memory access
|
||||
* patterns analysis).
|
||||
*/
|
||||
int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
|
||||
|
||||
/**
|
||||
* \brief Safe conditional swap X <-> Y if swap is 1
|
||||
*
|
||||
* \param X First mbedtls_mpi value
|
||||
* \param Y Second mbedtls_mpi value
|
||||
* \param assign 1: perform the swap, 0: keep X and Y's original values
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
*
|
||||
* \note This function is equivalent to
|
||||
* if( assign ) mbedtls_mpi_swap( X, Y );
|
||||
* except that it avoids leaking any information about whether
|
||||
* the assignment was done or not (the above code may leak
|
||||
* information through branch prediction and/or memory access
|
||||
* patterns analysis).
|
||||
*/
|
||||
int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
|
||||
|
||||
/**
|
||||
* \brief Set value from integer
|
||||
*
|
||||
* \param X MPI to set
|
||||
* \param z Value to use
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
|
||||
|
||||
/**
|
||||
* \brief Get a specific bit from X
|
||||
*
|
||||
* \param X MPI to use
|
||||
* \param pos Zero-based index of the bit in X
|
||||
*
|
||||
* \return Either a 0 or a 1
|
||||
*/
|
||||
int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
|
||||
|
||||
/**
|
||||
* \brief Set a bit of X to a specific value of 0 or 1
|
||||
*
|
||||
* \note Will grow X if necessary to set a bit to 1 in a not yet
|
||||
* existing limb. Will not grow if bit should be set to 0
|
||||
*
|
||||
* \param X MPI to use
|
||||
* \param pos Zero-based index of the bit in X
|
||||
* \param val The value to set the bit to (0 or 1)
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
* MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
|
||||
*/
|
||||
int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
|
||||
|
||||
/**
|
||||
* \brief Return the number of zero-bits before the least significant
|
||||
* '1' bit
|
||||
*
|
||||
* Note: Thus also the zero-based index of the least significant '1' bit
|
||||
*
|
||||
* \param X MPI to use
|
||||
*/
|
||||
size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Return the number of bits up to and including the most
|
||||
* significant '1' bit'
|
||||
*
|
||||
* Note: Thus also the one-based index of the most significant '1' bit
|
||||
*
|
||||
* \param X MPI to use
|
||||
*/
|
||||
size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Return the total size in bytes
|
||||
*
|
||||
* \param X MPI to use
|
||||
*/
|
||||
size_t mbedtls_mpi_size( const mbedtls_mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Import from an ASCII string
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param radix Input numeric base
|
||||
* \param s Null-terminated string buffer
|
||||
*
|
||||
* \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
|
||||
*/
|
||||
int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
|
||||
|
||||
/**
|
||||
* \brief Export into an ASCII string
|
||||
*
|
||||
* \param X Source MPI
|
||||
* \param radix Output numeric base
|
||||
* \param buf Buffer to write the string to
|
||||
* \param buflen Length of buf
|
||||
* \param olen Length of the string written, including final NUL byte
|
||||
*
|
||||
* \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
|
||||
* *olen is always updated to reflect the amount
|
||||
* of data that has (or would have) been written.
|
||||
*
|
||||
* \note Call this function with buflen = 0 to obtain the
|
||||
* minimum required buffer size in *olen.
|
||||
*/
|
||||
int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
|
||||
char *buf, size_t buflen, size_t *olen );
|
||||
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
/**
|
||||
* \brief Read MPI from a line in an opened file
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param radix Input numeric base
|
||||
* \param fin Input file handle
|
||||
*
|
||||
* \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
|
||||
* the file read buffer is too small or a
|
||||
* MBEDTLS_ERR_MPI_XXX error code
|
||||
*
|
||||
* \note On success, this function advances the file stream
|
||||
* to the end of the current line or to EOF.
|
||||
*
|
||||
* The function returns 0 on an empty line.
|
||||
*
|
||||
* Leading whitespaces are ignored, as is a
|
||||
* '0x' prefix for radix 16.
|
||||
*
|
||||
*/
|
||||
int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
|
||||
|
||||
/**
|
||||
* \brief Write X into an opened file, or stdout if fout is NULL
|
||||
*
|
||||
* \param p Prefix, can be NULL
|
||||
* \param X Source MPI
|
||||
* \param radix Output numeric base
|
||||
* \param fout Output file handle (can be NULL)
|
||||
*
|
||||
* \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
|
||||
*
|
||||
* \note Set fout == NULL to print X on the console.
|
||||
*/
|
||||
int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
|
||||
#endif /* MBEDTLS_FS_IO */
|
||||
|
||||
/**
|
||||
* \brief Import X from unsigned binary data, big endian
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param buf Input buffer
|
||||
* \param buflen Input buffer size
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
|
||||
|
||||
/**
|
||||
* \brief Export X into unsigned binary data, big endian.
|
||||
* Always fills the whole buffer, which will start with zeros
|
||||
* if the number is smaller.
|
||||
*
|
||||
* \param X Source MPI
|
||||
* \param buf Output buffer
|
||||
* \param buflen Output buffer size
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
|
||||
*/
|
||||
int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
|
||||
|
||||
/**
|
||||
* \brief Left-shift: X <<= count
|
||||
*
|
||||
* \param X MPI to shift
|
||||
* \param count Amount to shift
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
|
||||
|
||||
/**
|
||||
* \brief Right-shift: X >>= count
|
||||
*
|
||||
* \param X MPI to shift
|
||||
* \param count Amount to shift
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
|
||||
|
||||
/**
|
||||
* \brief Compare unsigned values
|
||||
*
|
||||
* \param X Left-hand MPI
|
||||
* \param Y Right-hand MPI
|
||||
*
|
||||
* \return 1 if |X| is greater than |Y|,
|
||||
* -1 if |X| is lesser than |Y| or
|
||||
* 0 if |X| is equal to |Y|
|
||||
*/
|
||||
int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Compare signed values
|
||||
*
|
||||
* \param X Left-hand MPI
|
||||
* \param Y Right-hand MPI
|
||||
*
|
||||
* \return 1 if X is greater than Y,
|
||||
* -1 if X is lesser than Y or
|
||||
* 0 if X is equal to Y
|
||||
*/
|
||||
int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Compare signed values
|
||||
*
|
||||
* \param X Left-hand MPI
|
||||
* \param z The integer value to compare to
|
||||
*
|
||||
* \return 1 if X is greater than z,
|
||||
* -1 if X is lesser than z or
|
||||
* 0 if X is equal to z
|
||||
*/
|
||||
int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
|
||||
|
||||
/**
|
||||
* \brief Unsigned addition: X = |A| + |B|
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Unsigned subtraction: X = |A| - |B|
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
|
||||
*/
|
||||
int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Signed addition: X = A + B
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Signed subtraction: X = A - B
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Signed addition: X = A + b
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param b The integer value to add
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
|
||||
|
||||
/**
|
||||
* \brief Signed subtraction: X = A - b
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param b The integer value to subtract
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
|
||||
|
||||
/**
|
||||
* \brief Baseline multiplication: X = A * B
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Baseline multiplication: X = A * b
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param b The unsigned integer value to multiply with
|
||||
*
|
||||
* \note b is unsigned
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
|
||||
|
||||
/**
|
||||
* \brief Division by mbedtls_mpi: A = Q * B + R
|
||||
*
|
||||
* \param Q Destination MPI for the quotient
|
||||
* \param R Destination MPI for the rest value
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
* MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
|
||||
*
|
||||
* \note Either Q or R can be NULL.
|
||||
*/
|
||||
int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Division by int: A = Q * b + R
|
||||
*
|
||||
* \param Q Destination MPI for the quotient
|
||||
* \param R Destination MPI for the rest value
|
||||
* \param A Left-hand MPI
|
||||
* \param b Integer to divide by
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
* MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
|
||||
*
|
||||
* \note Either Q or R can be NULL.
|
||||
*/
|
||||
int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
|
||||
|
||||
/**
|
||||
* \brief Modulo: R = A mod B
|
||||
*
|
||||
* \param R Destination MPI for the rest value
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
* MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
|
||||
* MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
|
||||
*/
|
||||
int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Modulo: r = A mod b
|
||||
*
|
||||
* \param r Destination mbedtls_mpi_uint
|
||||
* \param A Left-hand MPI
|
||||
* \param b Integer to divide by
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
* MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
|
||||
* MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
|
||||
*/
|
||||
int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
|
||||
|
||||
/**
|
||||
* \brief Sliding-window exponentiation: X = A^E mod N
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param E Exponent MPI
|
||||
* \param N Modular MPI
|
||||
* \param _RR Speed-up MPI used for recalculations
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
* MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
|
||||
* if E is negative
|
||||
*
|
||||
* \note _RR is used to avoid re-computing R*R mod N across
|
||||
* multiple calls, which speeds up things a bit. It can
|
||||
* be set to NULL if the extra performance is unneeded.
|
||||
*/
|
||||
int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
|
||||
|
||||
/**
|
||||
* \brief Fill an MPI X with size bytes of random
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param size Size in bytes
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/**
|
||||
* \brief Greatest common divisor: G = gcd(A, B)
|
||||
*
|
||||
* \param G Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Modular inverse: X = A^-1 mod N
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param N Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
* MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
|
||||
MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
|
||||
*/
|
||||
int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
|
||||
|
||||
/**
|
||||
* \brief Miller-Rabin primality test
|
||||
*
|
||||
* \param X MPI to check
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
*
|
||||
* \return 0 if successful (probably prime),
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
* MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
|
||||
*/
|
||||
int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/**
|
||||
* \brief Prime number generation
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param nbits Required size of X in bits
|
||||
* ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
|
||||
* \param dh_flag If 1, then (X-1)/2 will be prime too
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
*
|
||||
* \return 0 if successful (probably prime),
|
||||
* MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
|
||||
* MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
|
||||
*/
|
||||
int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int mbedtls_mpi_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* bignum.h */
|
||||
887
arm9/src/nand/polarssl/bn_mul.h
Normal file
887
arm9/src/nand/polarssl/bn_mul.h
Normal file
@ -0,0 +1,887 @@
|
||||
/**
|
||||
* \file bn_mul.h
|
||||
*
|
||||
* \brief Multi-precision integer library
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
/*
|
||||
* Multiply source vector [s] with b, add result
|
||||
* to destination vector [d] and set carry c.
|
||||
*
|
||||
* Currently supports:
|
||||
*
|
||||
* . IA-32 (386+) . AMD64 / EM64T
|
||||
* . IA-32 (SSE2) . Motorola 68000
|
||||
* . PowerPC, 32-bit . MicroBlaze
|
||||
* . PowerPC, 64-bit . TriCore
|
||||
* . SPARC v8 . ARM v3+
|
||||
* . Alpha . MIPS32
|
||||
* . C, longlong . C, generic
|
||||
*/
|
||||
#ifndef MBEDTLS_BN_MUL_H
|
||||
#define MBEDTLS_BN_MUL_H
|
||||
|
||||
#include "bignum.h"
|
||||
|
||||
#if defined(MBEDTLS_HAVE_ASM)
|
||||
|
||||
#ifndef asm
|
||||
#define asm __asm
|
||||
#endif
|
||||
|
||||
/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
|
||||
#if defined(__GNUC__) && \
|
||||
( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 )
|
||||
#if defined(__i386__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"movl %%ebx, %0 \n\t" \
|
||||
"movl %5, %%esi \n\t" \
|
||||
"movl %6, %%edi \n\t" \
|
||||
"movl %7, %%ecx \n\t" \
|
||||
"movl %8, %%ebx \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lodsl \n\t" \
|
||||
"mull %%ebx \n\t" \
|
||||
"addl %%ecx, %%eax \n\t" \
|
||||
"adcl $0, %%edx \n\t" \
|
||||
"addl (%%edi), %%eax \n\t" \
|
||||
"adcl $0, %%edx \n\t" \
|
||||
"movl %%edx, %%ecx \n\t" \
|
||||
"stosl \n\t"
|
||||
|
||||
#if defined(MBEDTLS_HAVE_SSE2)
|
||||
|
||||
#define MULADDC_HUIT \
|
||||
"movd %%ecx, %%mm1 \n\t" \
|
||||
"movd %%ebx, %%mm0 \n\t" \
|
||||
"movd (%%edi), %%mm3 \n\t" \
|
||||
"paddq %%mm3, %%mm1 \n\t" \
|
||||
"movd (%%esi), %%mm2 \n\t" \
|
||||
"pmuludq %%mm0, %%mm2 \n\t" \
|
||||
"movd 4(%%esi), %%mm4 \n\t" \
|
||||
"pmuludq %%mm0, %%mm4 \n\t" \
|
||||
"movd 8(%%esi), %%mm6 \n\t" \
|
||||
"pmuludq %%mm0, %%mm6 \n\t" \
|
||||
"movd 12(%%esi), %%mm7 \n\t" \
|
||||
"pmuludq %%mm0, %%mm7 \n\t" \
|
||||
"paddq %%mm2, %%mm1 \n\t" \
|
||||
"movd 4(%%edi), %%mm3 \n\t" \
|
||||
"paddq %%mm4, %%mm3 \n\t" \
|
||||
"movd 8(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm6, %%mm5 \n\t" \
|
||||
"movd 12(%%edi), %%mm4 \n\t" \
|
||||
"paddq %%mm4, %%mm7 \n\t" \
|
||||
"movd %%mm1, (%%edi) \n\t" \
|
||||
"movd 16(%%esi), %%mm2 \n\t" \
|
||||
"pmuludq %%mm0, %%mm2 \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"movd 20(%%esi), %%mm4 \n\t" \
|
||||
"pmuludq %%mm0, %%mm4 \n\t" \
|
||||
"paddq %%mm3, %%mm1 \n\t" \
|
||||
"movd 24(%%esi), %%mm6 \n\t" \
|
||||
"pmuludq %%mm0, %%mm6 \n\t" \
|
||||
"movd %%mm1, 4(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"movd 28(%%esi), %%mm3 \n\t" \
|
||||
"pmuludq %%mm0, %%mm3 \n\t" \
|
||||
"paddq %%mm5, %%mm1 \n\t" \
|
||||
"movd 16(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm2 \n\t" \
|
||||
"movd %%mm1, 8(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm7, %%mm1 \n\t" \
|
||||
"movd 20(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm4 \n\t" \
|
||||
"movd %%mm1, 12(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm2, %%mm1 \n\t" \
|
||||
"movd 24(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm6 \n\t" \
|
||||
"movd %%mm1, 16(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm4, %%mm1 \n\t" \
|
||||
"movd 28(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm3 \n\t" \
|
||||
"movd %%mm1, 20(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm6, %%mm1 \n\t" \
|
||||
"movd %%mm1, 24(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm3, %%mm1 \n\t" \
|
||||
"movd %%mm1, 28(%%edi) \n\t" \
|
||||
"addl $32, %%edi \n\t" \
|
||||
"addl $32, %%esi \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"movd %%mm1, %%ecx \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"emms \n\t" \
|
||||
"movl %4, %%ebx \n\t" \
|
||||
"movl %%ecx, %1 \n\t" \
|
||||
"movl %%edi, %2 \n\t" \
|
||||
"movl %%esi, %3 \n\t" \
|
||||
: "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "eax", "ecx", "edx", "esi", "edi" \
|
||||
);
|
||||
|
||||
#else
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"movl %4, %%ebx \n\t" \
|
||||
"movl %%ecx, %1 \n\t" \
|
||||
"movl %%edi, %2 \n\t" \
|
||||
"movl %%esi, %3 \n\t" \
|
||||
: "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "eax", "ecx", "edx", "esi", "edi" \
|
||||
);
|
||||
#endif /* SSE2 */
|
||||
#endif /* i386 */
|
||||
|
||||
#if defined(__amd64__) || defined (__x86_64__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"xorq %%r8, %%r8 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"movq (%%rsi), %%rax \n\t" \
|
||||
"mulq %%rbx \n\t" \
|
||||
"addq $8, %%rsi \n\t" \
|
||||
"addq %%rcx, %%rax \n\t" \
|
||||
"movq %%r8, %%rcx \n\t" \
|
||||
"adcq $0, %%rdx \n\t" \
|
||||
"nop \n\t" \
|
||||
"addq %%rax, (%%rdi) \n\t" \
|
||||
"adcq %%rdx, %%rcx \n\t" \
|
||||
"addq $8, %%rdi \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
: "+c" (c), "+D" (d), "+S" (s) \
|
||||
: "b" (b) \
|
||||
: "rax", "rdx", "r8" \
|
||||
);
|
||||
|
||||
#endif /* AMD64 */
|
||||
|
||||
#if defined(__mc68020__) || defined(__mcpu32__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"movl %3, %%a2 \n\t" \
|
||||
"movl %4, %%a3 \n\t" \
|
||||
"movl %5, %%d3 \n\t" \
|
||||
"movl %6, %%d2 \n\t" \
|
||||
"moveq #0, %%d0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"moveq #0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"addxl %%d4, %%d3 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"movl %%d3, %0 \n\t" \
|
||||
"movl %%a3, %1 \n\t" \
|
||||
"movl %%a2, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "d0", "d1", "d2", "d3", "d4", "a2", "a3" \
|
||||
);
|
||||
|
||||
#define MULADDC_HUIT \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t"
|
||||
|
||||
#endif /* MC68000 */
|
||||
|
||||
#if defined(__powerpc64__) || defined(__ppc64__)
|
||||
|
||||
#if defined(__MACH__) && defined(__APPLE__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld r3, %3 \n\t" \
|
||||
"ld r4, %4 \n\t" \
|
||||
"ld r5, %5 \n\t" \
|
||||
"ld r6, %6 \n\t" \
|
||||
"addi r3, r3, -8 \n\t" \
|
||||
"addi r4, r4, -8 \n\t" \
|
||||
"addic r5, r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldu r7, 8(r3) \n\t" \
|
||||
"mulld r8, r7, r6 \n\t" \
|
||||
"mulhdu r9, r7, r6 \n\t" \
|
||||
"adde r8, r8, r5 \n\t" \
|
||||
"ld r7, 8(r4) \n\t" \
|
||||
"addze r5, r9 \n\t" \
|
||||
"addc r8, r8, r7 \n\t" \
|
||||
"stdu r8, 8(r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze r5, r5 \n\t" \
|
||||
"addi r4, r4, 8 \n\t" \
|
||||
"addi r3, r3, 8 \n\t" \
|
||||
"std r5, %0 \n\t" \
|
||||
"std r4, %1 \n\t" \
|
||||
"std r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
|
||||
#else /* __MACH__ && __APPLE__ */
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld %%r3, %3 \n\t" \
|
||||
"ld %%r4, %4 \n\t" \
|
||||
"ld %%r5, %5 \n\t" \
|
||||
"ld %%r6, %6 \n\t" \
|
||||
"addi %%r3, %%r3, -8 \n\t" \
|
||||
"addi %%r4, %%r4, -8 \n\t" \
|
||||
"addic %%r5, %%r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldu %%r7, 8(%%r3) \n\t" \
|
||||
"mulld %%r8, %%r7, %%r6 \n\t" \
|
||||
"mulhdu %%r9, %%r7, %%r6 \n\t" \
|
||||
"adde %%r8, %%r8, %%r5 \n\t" \
|
||||
"ld %%r7, 8(%%r4) \n\t" \
|
||||
"addze %%r5, %%r9 \n\t" \
|
||||
"addc %%r8, %%r8, %%r7 \n\t" \
|
||||
"stdu %%r8, 8(%%r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze %%r5, %%r5 \n\t" \
|
||||
"addi %%r4, %%r4, 8 \n\t" \
|
||||
"addi %%r3, %%r3, 8 \n\t" \
|
||||
"std %%r5, %0 \n\t" \
|
||||
"std %%r4, %1 \n\t" \
|
||||
"std %%r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
#endif /* __MACH__ && __APPLE__ */
|
||||
|
||||
#elif defined(__powerpc__) || defined(__ppc__) /* end PPC64/begin PPC32 */
|
||||
|
||||
#if defined(__MACH__) && defined(__APPLE__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lwz r3, %3 \n\t" \
|
||||
"lwz r4, %4 \n\t" \
|
||||
"lwz r5, %5 \n\t" \
|
||||
"lwz r6, %6 \n\t" \
|
||||
"addi r3, r3, -4 \n\t" \
|
||||
"addi r4, r4, -4 \n\t" \
|
||||
"addic r5, r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lwzu r7, 4(r3) \n\t" \
|
||||
"mullw r8, r7, r6 \n\t" \
|
||||
"mulhwu r9, r7, r6 \n\t" \
|
||||
"adde r8, r8, r5 \n\t" \
|
||||
"lwz r7, 4(r4) \n\t" \
|
||||
"addze r5, r9 \n\t" \
|
||||
"addc r8, r8, r7 \n\t" \
|
||||
"stwu r8, 4(r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze r5, r5 \n\t" \
|
||||
"addi r4, r4, 4 \n\t" \
|
||||
"addi r3, r3, 4 \n\t" \
|
||||
"stw r5, %0 \n\t" \
|
||||
"stw r4, %1 \n\t" \
|
||||
"stw r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
#else /* __MACH__ && __APPLE__ */
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lwz %%r3, %3 \n\t" \
|
||||
"lwz %%r4, %4 \n\t" \
|
||||
"lwz %%r5, %5 \n\t" \
|
||||
"lwz %%r6, %6 \n\t" \
|
||||
"addi %%r3, %%r3, -4 \n\t" \
|
||||
"addi %%r4, %%r4, -4 \n\t" \
|
||||
"addic %%r5, %%r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lwzu %%r7, 4(%%r3) \n\t" \
|
||||
"mullw %%r8, %%r7, %%r6 \n\t" \
|
||||
"mulhwu %%r9, %%r7, %%r6 \n\t" \
|
||||
"adde %%r8, %%r8, %%r5 \n\t" \
|
||||
"lwz %%r7, 4(%%r4) \n\t" \
|
||||
"addze %%r5, %%r9 \n\t" \
|
||||
"addc %%r8, %%r8, %%r7 \n\t" \
|
||||
"stwu %%r8, 4(%%r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze %%r5, %%r5 \n\t" \
|
||||
"addi %%r4, %%r4, 4 \n\t" \
|
||||
"addi %%r3, %%r3, 4 \n\t" \
|
||||
"stw %%r5, %0 \n\t" \
|
||||
"stw %%r4, %1 \n\t" \
|
||||
"stw %%r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
#endif /* __MACH__ && __APPLE__ */
|
||||
|
||||
#endif /* PPC32 */
|
||||
|
||||
/*
|
||||
* The Sparc(64) assembly is reported to be broken.
|
||||
* Disable it for now, until we're able to fix it.
|
||||
*/
|
||||
#if 0 && defined(__sparc__)
|
||||
#if defined(__sparc64__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldx %3, %%o0 \n\t" \
|
||||
"ldx %4, %%o1 \n\t" \
|
||||
"ld %5, %%o2 \n\t" \
|
||||
"ld %6, %%o3 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ld [%%o0], %%o4 \n\t" \
|
||||
"inc 4, %%o0 \n\t" \
|
||||
"ld [%%o1], %%o5 \n\t" \
|
||||
"umul %%o3, %%o4, %%o4 \n\t" \
|
||||
"addcc %%o4, %%o2, %%o4 \n\t" \
|
||||
"rd %%y, %%g1 \n\t" \
|
||||
"addx %%g1, 0, %%g1 \n\t" \
|
||||
"addcc %%o4, %%o5, %%o4 \n\t" \
|
||||
"st %%o4, [%%o1] \n\t" \
|
||||
"addx %%g1, 0, %%o2 \n\t" \
|
||||
"inc 4, %%o1 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"st %%o2, %0 \n\t" \
|
||||
"stx %%o1, %1 \n\t" \
|
||||
"stx %%o0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "g1", "o0", "o1", "o2", "o3", "o4", \
|
||||
"o5" \
|
||||
);
|
||||
|
||||
#else /* __sparc64__ */
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld %3, %%o0 \n\t" \
|
||||
"ld %4, %%o1 \n\t" \
|
||||
"ld %5, %%o2 \n\t" \
|
||||
"ld %6, %%o3 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ld [%%o0], %%o4 \n\t" \
|
||||
"inc 4, %%o0 \n\t" \
|
||||
"ld [%%o1], %%o5 \n\t" \
|
||||
"umul %%o3, %%o4, %%o4 \n\t" \
|
||||
"addcc %%o4, %%o2, %%o4 \n\t" \
|
||||
"rd %%y, %%g1 \n\t" \
|
||||
"addx %%g1, 0, %%g1 \n\t" \
|
||||
"addcc %%o4, %%o5, %%o4 \n\t" \
|
||||
"st %%o4, [%%o1] \n\t" \
|
||||
"addx %%g1, 0, %%o2 \n\t" \
|
||||
"inc 4, %%o1 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"st %%o2, %0 \n\t" \
|
||||
"st %%o1, %1 \n\t" \
|
||||
"st %%o0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "g1", "o0", "o1", "o2", "o3", "o4", \
|
||||
"o5" \
|
||||
);
|
||||
|
||||
#endif /* __sparc64__ */
|
||||
#endif /* __sparc__ */
|
||||
|
||||
#if defined(__microblaze__) || defined(microblaze)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lwi r3, %3 \n\t" \
|
||||
"lwi r4, %4 \n\t" \
|
||||
"lwi r5, %5 \n\t" \
|
||||
"lwi r6, %6 \n\t" \
|
||||
"andi r7, r6, 0xffff \n\t" \
|
||||
"bsrli r6, r6, 16 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lhui r8, r3, 0 \n\t" \
|
||||
"addi r3, r3, 2 \n\t" \
|
||||
"lhui r9, r3, 0 \n\t" \
|
||||
"addi r3, r3, 2 \n\t" \
|
||||
"mul r10, r9, r6 \n\t" \
|
||||
"mul r11, r8, r7 \n\t" \
|
||||
"mul r12, r9, r7 \n\t" \
|
||||
"mul r13, r8, r6 \n\t" \
|
||||
"bsrli r8, r10, 16 \n\t" \
|
||||
"bsrli r9, r11, 16 \n\t" \
|
||||
"add r13, r13, r8 \n\t" \
|
||||
"add r13, r13, r9 \n\t" \
|
||||
"bslli r10, r10, 16 \n\t" \
|
||||
"bslli r11, r11, 16 \n\t" \
|
||||
"add r12, r12, r10 \n\t" \
|
||||
"addc r13, r13, r0 \n\t" \
|
||||
"add r12, r12, r11 \n\t" \
|
||||
"addc r13, r13, r0 \n\t" \
|
||||
"lwi r10, r4, 0 \n\t" \
|
||||
"add r12, r12, r10 \n\t" \
|
||||
"addc r13, r13, r0 \n\t" \
|
||||
"add r12, r12, r5 \n\t" \
|
||||
"addc r5, r13, r0 \n\t" \
|
||||
"swi r12, r4, 0 \n\t" \
|
||||
"addi r4, r4, 4 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"swi r5, %0 \n\t" \
|
||||
"swi r4, %1 \n\t" \
|
||||
"swi r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4" "r5", "r6", "r7", "r8", \
|
||||
"r9", "r10", "r11", "r12", "r13" \
|
||||
);
|
||||
|
||||
#endif /* MicroBlaze */
|
||||
|
||||
#if defined(__tricore__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld.a %%a2, %3 \n\t" \
|
||||
"ld.a %%a3, %4 \n\t" \
|
||||
"ld.w %%d4, %5 \n\t" \
|
||||
"ld.w %%d1, %6 \n\t" \
|
||||
"xor %%d5, %%d5 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ld.w %%d0, [%%a2+] \n\t" \
|
||||
"madd.u %%e2, %%e4, %%d0, %%d1 \n\t" \
|
||||
"ld.w %%d0, [%%a3] \n\t" \
|
||||
"addx %%d2, %%d2, %%d0 \n\t" \
|
||||
"addc %%d3, %%d3, 0 \n\t" \
|
||||
"mov %%d4, %%d3 \n\t" \
|
||||
"st.w [%%a3+], %%d2 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"st.w %0, %%d4 \n\t" \
|
||||
"st.a %1, %%a3 \n\t" \
|
||||
"st.a %2, %%a2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "d0", "d1", "e2", "d4", "a2", "a3" \
|
||||
);
|
||||
|
||||
#endif /* TriCore */
|
||||
|
||||
/*
|
||||
* gcc -O0 by default uses r7 for the frame pointer, so it complains about our
|
||||
* use of r7 below, unless -fomit-frame-pointer is passed. Unfortunately,
|
||||
* passing that option is not easy when building with yotta.
|
||||
*
|
||||
* On the other hand, -fomit-frame-pointer is implied by any -Ox options with
|
||||
* x !=0, which we can detect using __OPTIMIZE__ (which is also defined by
|
||||
* clang and armcc5 under the same conditions).
|
||||
*
|
||||
* So, only use the optimized assembly below for optimized build, which avoids
|
||||
* the build error and is pretty reasonable anyway.
|
||||
*/
|
||||
#if defined(__GNUC__) && !defined(__OPTIMIZE__)
|
||||
#define MULADDC_CANNOT_USE_R7
|
||||
#endif
|
||||
|
||||
#if defined(__arm__) && !defined(MULADDC_CANNOT_USE_R7)
|
||||
|
||||
#if defined(__thumb__) && !defined(__thumb2__)
|
||||
|
||||
#pragma message "using ARM THUMB MULADDC"
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldr r0, %3 \n\t" \
|
||||
"ldr r1, %4 \n\t" \
|
||||
"ldr r2, %5 \n\t" \
|
||||
"ldr r3, %6 \n\t" \
|
||||
"lsr r7, r3, #16 \n\t" \
|
||||
"mov r9, r7 \n\t" \
|
||||
"lsl r7, r3, #16 \n\t" \
|
||||
"lsr r7, r7, #16 \n\t" \
|
||||
"mov r8, r7 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldmia r0!, {r6} \n\t" \
|
||||
"lsr r7, r6, #16 \n\t" \
|
||||
"lsl r6, r6, #16 \n\t" \
|
||||
"lsr r6, r6, #16 \n\t" \
|
||||
"mov r4, r8 \n\t" \
|
||||
"mul r4, r6 \n\t" \
|
||||
"mov r3, r9 \n\t" \
|
||||
"mul r6, r3 \n\t" \
|
||||
"mov r5, r9 \n\t" \
|
||||
"mul r5, r7 \n\t" \
|
||||
"mov r3, r8 \n\t" \
|
||||
"mul r7, r3 \n\t" \
|
||||
"lsr r3, r6, #16 \n\t" \
|
||||
"add r5, r5, r3 \n\t" \
|
||||
"lsr r3, r7, #16 \n\t" \
|
||||
"add r5, r5, r3 \n\t" \
|
||||
"add r4, r4, r2 \n\t" \
|
||||
"mov r2, #0 \n\t" \
|
||||
"adc r5, r2 \n\t" \
|
||||
"lsl r3, r6, #16 \n\t" \
|
||||
"add r4, r4, r3 \n\t" \
|
||||
"adc r5, r2 \n\t" \
|
||||
"lsl r3, r7, #16 \n\t" \
|
||||
"add r4, r4, r3 \n\t" \
|
||||
"adc r5, r2 \n\t" \
|
||||
"ldr r3, [r1] \n\t" \
|
||||
"add r4, r4, r3 \n\t" \
|
||||
"adc r2, r5 \n\t" \
|
||||
"stmia r1!, {r4} \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"str r2, %0 \n\t" \
|
||||
"str r1, %1 \n\t" \
|
||||
"str r0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r0", "r1", "r2", "r3", "r4", "r5", \
|
||||
"r6", "r7", "r8", "r9", "cc" \
|
||||
);
|
||||
|
||||
#else
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldr r0, %3 \n\t" \
|
||||
"ldr r1, %4 \n\t" \
|
||||
"ldr r2, %5 \n\t" \
|
||||
"ldr r3, %6 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldr r4, [r0], #4 \n\t" \
|
||||
"mov r5, #0 \n\t" \
|
||||
"ldr r6, [r1] \n\t" \
|
||||
"umlal r2, r5, r3, r4 \n\t" \
|
||||
"adds r7, r6, r2 \n\t" \
|
||||
"adc r2, r5, #0 \n\t" \
|
||||
"str r7, [r1], #4 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"str r2, %0 \n\t" \
|
||||
"str r1, %1 \n\t" \
|
||||
"str r0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r0", "r1", "r2", "r3", "r4", "r5", \
|
||||
"r6", "r7", "cc" \
|
||||
);
|
||||
|
||||
#endif /* Thumb */
|
||||
|
||||
#endif /* ARMv3 */
|
||||
|
||||
#if defined(__alpha__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldq $1, %3 \n\t" \
|
||||
"ldq $2, %4 \n\t" \
|
||||
"ldq $3, %5 \n\t" \
|
||||
"ldq $4, %6 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldq $6, 0($1) \n\t" \
|
||||
"addq $1, 8, $1 \n\t" \
|
||||
"mulq $6, $4, $7 \n\t" \
|
||||
"umulh $6, $4, $6 \n\t" \
|
||||
"addq $7, $3, $7 \n\t" \
|
||||
"cmpult $7, $3, $3 \n\t" \
|
||||
"ldq $5, 0($2) \n\t" \
|
||||
"addq $7, $5, $7 \n\t" \
|
||||
"cmpult $7, $5, $5 \n\t" \
|
||||
"stq $7, 0($2) \n\t" \
|
||||
"addq $2, 8, $2 \n\t" \
|
||||
"addq $6, $3, $3 \n\t" \
|
||||
"addq $5, $3, $3 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"stq $3, %0 \n\t" \
|
||||
"stq $2, %1 \n\t" \
|
||||
"stq $1, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "$1", "$2", "$3", "$4", "$5", "$6", "$7" \
|
||||
);
|
||||
#endif /* Alpha */
|
||||
|
||||
#if defined(__mips__) && !defined(__mips64)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lw $10, %3 \n\t" \
|
||||
"lw $11, %4 \n\t" \
|
||||
"lw $12, %5 \n\t" \
|
||||
"lw $13, %6 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lw $14, 0($10) \n\t" \
|
||||
"multu $13, $14 \n\t" \
|
||||
"addi $10, $10, 4 \n\t" \
|
||||
"mflo $14 \n\t" \
|
||||
"mfhi $9 \n\t" \
|
||||
"addu $14, $12, $14 \n\t" \
|
||||
"lw $15, 0($11) \n\t" \
|
||||
"sltu $12, $14, $12 \n\t" \
|
||||
"addu $15, $14, $15 \n\t" \
|
||||
"sltu $14, $15, $14 \n\t" \
|
||||
"addu $12, $12, $9 \n\t" \
|
||||
"sw $15, 0($11) \n\t" \
|
||||
"addu $12, $12, $14 \n\t" \
|
||||
"addi $11, $11, 4 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"sw $12, %0 \n\t" \
|
||||
"sw $11, %1 \n\t" \
|
||||
"sw $10, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "$9", "$10", "$11", "$12", "$13", "$14", "$15" \
|
||||
);
|
||||
|
||||
#endif /* MIPS */
|
||||
#endif /* GNUC */
|
||||
|
||||
#if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
__asm mov esi, s \
|
||||
__asm mov edi, d \
|
||||
__asm mov ecx, c \
|
||||
__asm mov ebx, b
|
||||
|
||||
#define MULADDC_CORE \
|
||||
__asm lodsd \
|
||||
__asm mul ebx \
|
||||
__asm add eax, ecx \
|
||||
__asm adc edx, 0 \
|
||||
__asm add eax, [edi] \
|
||||
__asm adc edx, 0 \
|
||||
__asm mov ecx, edx \
|
||||
__asm stosd
|
||||
|
||||
#if defined(MBEDTLS_HAVE_SSE2)
|
||||
|
||||
#define EMIT __asm _emit
|
||||
|
||||
#define MULADDC_HUIT \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0xC9 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0xC3 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x1F \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x16 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xF8 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xDC \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xEE \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xFC \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x0F \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xD8 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCD \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xD5 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCF \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xE5 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xF5 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCC \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xDD \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCE \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C \
|
||||
EMIT 0x83 EMIT 0xC7 EMIT 0x20 \
|
||||
EMIT 0x83 EMIT 0xC6 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0xC9
|
||||
|
||||
#define MULADDC_STOP \
|
||||
EMIT 0x0F EMIT 0x77 \
|
||||
__asm mov c, ecx \
|
||||
__asm mov d, edi \
|
||||
__asm mov s, esi \
|
||||
|
||||
#else
|
||||
|
||||
#define MULADDC_STOP \
|
||||
__asm mov c, ecx \
|
||||
__asm mov d, edi \
|
||||
__asm mov s, esi \
|
||||
|
||||
#endif /* SSE2 */
|
||||
#endif /* MSVC */
|
||||
|
||||
#endif /* MBEDTLS_HAVE_ASM */
|
||||
|
||||
#if !defined(MULADDC_CORE)
|
||||
#if defined(MBEDTLS_HAVE_UDBL)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
{ \
|
||||
mbedtls_t_udbl r; \
|
||||
mbedtls_mpi_uint r0, r1;
|
||||
|
||||
#define MULADDC_CORE \
|
||||
r = *(s++) * (mbedtls_t_udbl) b; \
|
||||
r0 = (mbedtls_mpi_uint) r; \
|
||||
r1 = (mbedtls_mpi_uint)( r >> biL ); \
|
||||
r0 += c; r1 += (r0 < c); \
|
||||
r0 += *d; r1 += (r0 < *d); \
|
||||
c = r1; *(d++) = r0;
|
||||
|
||||
#define MULADDC_STOP \
|
||||
}
|
||||
|
||||
#else
|
||||
#define MULADDC_INIT \
|
||||
{ \
|
||||
mbedtls_mpi_uint s0, s1, b0, b1; \
|
||||
mbedtls_mpi_uint r0, r1, rx, ry; \
|
||||
b0 = ( b << biH ) >> biH; \
|
||||
b1 = ( b >> biH );
|
||||
|
||||
#define MULADDC_CORE \
|
||||
s0 = ( *s << biH ) >> biH; \
|
||||
s1 = ( *s >> biH ); s++; \
|
||||
rx = s0 * b1; r0 = s0 * b0; \
|
||||
ry = s1 * b0; r1 = s1 * b1; \
|
||||
r1 += ( rx >> biH ); \
|
||||
r1 += ( ry >> biH ); \
|
||||
rx <<= biH; ry <<= biH; \
|
||||
r0 += rx; r1 += (r0 < rx); \
|
||||
r0 += ry; r1 += (r0 < ry); \
|
||||
r0 += c; r1 += (r0 < c); \
|
||||
r0 += *d; r1 += (r0 < *d); \
|
||||
c = r1; *(d++) = r0;
|
||||
|
||||
#define MULADDC_STOP \
|
||||
}
|
||||
|
||||
#endif /* C (generic) */
|
||||
#endif /* C (longlong) */
|
||||
|
||||
#endif /* bn_mul.h */
|
||||
5
arm9/src/nand/polarssl/config.h
Normal file
5
arm9/src/nand/polarssl/config.h
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
#define MBEDTLS_BIGNUM_C
|
||||
#define POLARSSL_AES_C
|
||||
|
||||
#define MBEDTLS_HAVE_ASM
|
||||
98
arm9/src/nand/polarssl/padlock.h
Normal file
98
arm9/src/nand/polarssl/padlock.h
Normal file
@ -0,0 +1,98 @@
|
||||
/**
|
||||
* \file padlock.h
|
||||
*
|
||||
* Copyright (C) 2006-2010, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_PADLOCK_H
|
||||
#define POLARSSL_PADLOCK_H
|
||||
|
||||
#include "aes.h"
|
||||
|
||||
#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__)
|
||||
|
||||
#ifndef POLARSSL_HAVE_X86
|
||||
#define POLARSSL_HAVE_X86
|
||||
#endif
|
||||
|
||||
#define PADLOCK_RNG 0x000C
|
||||
#define PADLOCK_ACE 0x00C0
|
||||
#define PADLOCK_PHE 0x0C00
|
||||
#define PADLOCK_PMM 0x3000
|
||||
|
||||
#define PADLOCK_ALIGN16(x) (unsigned long *) (16 + ((long) x & ~15))
|
||||
|
||||
#define POLARSSL_ERR_PADLOCK_DATA_MISALIGNED -0x08E0
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief PadLock detection routine
|
||||
*
|
||||
* \param The feature to detect
|
||||
*
|
||||
* \return 1 if CPU has support for the feature, 0 otherwise
|
||||
*/
|
||||
int padlock_supports( int feature );
|
||||
|
||||
/**
|
||||
* \brief PadLock AES-ECB block en(de)cryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param input 16-byte input block
|
||||
* \param output 16-byte output block
|
||||
*
|
||||
* \return 0 if success, 1 if operation failed
|
||||
*/
|
||||
int padlock_xcryptecb( aes_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] );
|
||||
|
||||
/**
|
||||
* \brief PadLock AES-CBC buffer en(de)cryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if success, 1 if operation failed
|
||||
*/
|
||||
int padlock_xcryptcbc( aes_context *ctx,
|
||||
int mode,
|
||||
int length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HAVE_X86 */
|
||||
|
||||
#endif /* padlock.h */
|
||||
106
arm9/src/nand/sector0.c
Normal file
106
arm9/src/nand/sector0.c
Normal file
@ -0,0 +1,106 @@
|
||||
/* sector0.c
|
||||
* This code was imported from https://github.com/DS-Homebrew/GodMode9i
|
||||
*
|
||||
* Changes against the source:
|
||||
* - Documentation added
|
||||
* - clean up formatting
|
||||
* - moved magic numbers to defines from sector0.h
|
||||
* - fixed parse_mbr to return valid even when signature was invalid but
|
||||
* the bootstrap region was not all zero. (? Why was this code there ?)
|
||||
* - removed verbose output (must be handled on application level, not
|
||||
* within the helpers
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include "sector0.h"
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
static const mbr_partition_t ptable_DSi[MBR_PARTITIONS] = {
|
||||
{0u, {3u, 24u, 4u}, 6u, {15u, 224u, 59u}, 0x00000877u, 0x00066f89u},
|
||||
{0u, {2u, 206u, 60u}, 6u, {15u, 224u, 190u}, 0x0006784u, 0x000105b3u},
|
||||
{0u, {2u, 222u, 191u}, 1u, {15u, 224u, 191u}, 0x00077e5u, 0x000001a3u},
|
||||
{0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u}
|
||||
};
|
||||
|
||||
static const mbr_partition_t ptable_3DS[MBR_PARTITIONS] = {
|
||||
{0u, {4u, 24u, 0u}, 6u, {1u, 160u, 63u}, 0x0000009u, 0x00047da9u},
|
||||
{0u, {4u, 142u, 64u}, 6u, {1u, 160u, 195u}, 0x0004808u, 0x000105b3u},
|
||||
{0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u},
|
||||
{0u, {0u, 0u, 0u}, 0u, {0u, 0u, 0u}, 0u, 0u}
|
||||
};
|
||||
|
||||
/************************ Functions *******************************************/
|
||||
|
||||
/*! \brief Sanity check of the NCSD
|
||||
*
|
||||
* The ncsd is checked for
|
||||
* - the signature magic
|
||||
* - the partition types
|
||||
* to ensure a valid 3DS ncsd is present
|
||||
*
|
||||
* Return values:
|
||||
* 0: NCSD is a valid
|
||||
* -1: the signature/magic is invalid
|
||||
* -2: at least one unknown partition type was found
|
||||
*/
|
||||
int parse_ncsd(const uint8_t sector0[SECTOR_SIZE])
|
||||
{
|
||||
const ncsd_header_t * h = (ncsd_header_t *)sector0;
|
||||
if (NCSD_MAGIC != h->magic)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < NCSD_PARTITIONS; ++i)
|
||||
{
|
||||
unsigned fs_type = h->fs_types[i];
|
||||
if (fs_type == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch (fs_type) {
|
||||
case 1:
|
||||
case 3:
|
||||
case 4:
|
||||
break;
|
||||
default:
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief Sanity check of the MBR
|
||||
*
|
||||
* The master boot record is checked for
|
||||
* - the signature
|
||||
* - the partition0 values
|
||||
* to ensure a valid DSi main partition can be found
|
||||
*
|
||||
* Return values:
|
||||
* 0: MBR is a valid DSi partition
|
||||
* -1: the signature is invalid
|
||||
* -2: the first partition does not match expected values
|
||||
*/
|
||||
int parse_mbr(const uint8_t sector0[SECTOR_SIZE], const int is3DS)
|
||||
{
|
||||
const mbr_t *m = (mbr_t*)sector0;
|
||||
const mbr_partition_t *ref_ptable; // reference partition table
|
||||
|
||||
if ((MBR_SIGNATURE_0 != m->boot_signature[0]) || (MBR_SIGNATURE_1 != m->boot_signature[1]))
|
||||
{
|
||||
// if the signature is invalid, the bootsector shall not be used!
|
||||
return -1;
|
||||
}
|
||||
ref_ptable = is3DS?ptable_3DS:ptable_DSi;
|
||||
// only test the 1st partition now, we've seen variations on the 3rd partition
|
||||
// and after all we only care about the 1st partition
|
||||
if (memcmp(ref_ptable, m->partitions, sizeof(mbr_partition_t)))
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
169
arm9/src/nand/sector0.h
Normal file
169
arm9/src/nand/sector0.h
Normal file
@ -0,0 +1,169 @@
|
||||
/* sector0.h
|
||||
* This code was imported from https://github.com/DS-Homebrew/GodMode9i
|
||||
*
|
||||
* Changes against the source:
|
||||
* - Documentation added
|
||||
* - clean up / reorder
|
||||
* - removed verbose output (must be handled on application level, not
|
||||
* within the helpers */
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
|
||||
/************************ Constants / Defines *********************************/
|
||||
|
||||
#define SECTOR_SIZE 0x200
|
||||
|
||||
#define MBR_PARTITIONS 4
|
||||
#define MBR_BOOTSTRAP_SIZE (SECTOR_SIZE - (2 + MBR_PARTITIONS * 16))
|
||||
#define MBR_SIGNATURE_0 0x55
|
||||
#define MBR_SIGNATURE_1 0xAA
|
||||
|
||||
// https://3dbrew.org/wiki/NCSD#NCSD_header
|
||||
#define NCSD_PARTITIONS 8
|
||||
#define NCSD_SIGNATURESIZE 0x100
|
||||
#define NCSD_HEADERSIZE (NCSD_SIGNATURESIZE + 0x060)
|
||||
|
||||
#define NCSD_MAGIC 0x4453434e
|
||||
|
||||
/************************ Structures / Datatypes ******************************/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma pack(push, 1)
|
||||
#define __PACKED
|
||||
#elif defined __GNUC__
|
||||
#define __PACKED __attribute__ ((__packed__))
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint32_t offset,
|
||||
length;
|
||||
} __PACKED ncsd_partition_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t digest[NCSD_SIGNATURESIZE] ;
|
||||
} __PACKED ncsd_sginature ;
|
||||
|
||||
typedef struct {
|
||||
ncsd_sginature signature;
|
||||
uint32_t magic,
|
||||
size;
|
||||
uint64_t media_id;
|
||||
uint8_t fs_types[NCSD_PARTITIONS],
|
||||
crypt_types[NCSD_PARTITIONS];
|
||||
ncsd_partition_t partitions[NCSD_PARTITIONS];
|
||||
} __PACKED ncsd_header_t;
|
||||
|
||||
/*
|
||||
* CHS Sector Address Format
|
||||
* See https://en.wikipedia.org/wiki/Master_boot_record#PTE
|
||||
*
|
||||
* ==========================================================
|
||||
* | Offset | Field Length | Description |
|
||||
* ==========================================================
|
||||
* | 1 | 3 | CHS Address of first sector |
|
||||
* | | | Bit 0..7: Head |
|
||||
* | | | Bit 8..13: Sector |
|
||||
* | | | Bit 14..23: Cylinder |
|
||||
* ----------------------------------------------------------
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
uint8_t head;
|
||||
uint8_t sectorAndCylHigh;
|
||||
uint8_t cylinderLow;
|
||||
} __PACKED chs_t;
|
||||
|
||||
/*
|
||||
* Partition table entries
|
||||
* See https://en.wikipedia.org/wiki/Master_boot_record#PTE
|
||||
*
|
||||
*
|
||||
* ==========================================================
|
||||
* | Offset | Field Length | Description |
|
||||
* ==========================================================
|
||||
* | 0 | 1 | Bit 7: Bootable |
|
||||
* | | | Bit 0..6: Reserved (0) |
|
||||
* ----------------------------------------------------------
|
||||
* | 1 | 3 | CHS Address of first sector |
|
||||
* | | | Bit 0..7: Head |
|
||||
* | | | Bit 8..13: Sector |
|
||||
* | | | Bit 14..23: Cylinder |
|
||||
* ----------------------------------------------------------
|
||||
* | 4 | 1 | Partition Type |
|
||||
* ----------------------------------------------------------
|
||||
* | 5 | 3 | CHS Address of last sector |
|
||||
* | | | Bit 0..7: Head |
|
||||
* | | | Bit 8..13: Sector |
|
||||
* | | | Bit 14..23: Cylinder |
|
||||
* ----------------------------------------------------------
|
||||
* | 8 | 4 | LBA Address of first sector |
|
||||
* ----------------------------------------------------------
|
||||
* | 12 | 4 | Number of sectors |
|
||||
* ----------------------------------------------------------
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
uint8_t status;
|
||||
chs_t chs_first;
|
||||
uint8_t type;
|
||||
chs_t chs_last;
|
||||
uint32_t offset;
|
||||
uint32_t length;
|
||||
} __PACKED mbr_partition_t;
|
||||
|
||||
/*
|
||||
* Master Boot Record
|
||||
* https://en.wikipedia.org/wiki/Master_boot_record#Sector_layout
|
||||
*
|
||||
* ==========================================================
|
||||
* | Offset | Field Length | Description |
|
||||
* ==========================================================
|
||||
* | 0 | 446 | Boot code |
|
||||
* ----------------------------------------------------------
|
||||
* | 446 | 16 | Partition 0 |
|
||||
* ----------------------------------------------------------
|
||||
* | 462 | 16 | Partition 1 |
|
||||
* ----------------------------------------------------------
|
||||
* | 478 | 16 | Partition 2 |
|
||||
* ----------------------------------------------------------
|
||||
* | 494 | 16 | Partition 3 |
|
||||
* ----------------------------------------------------------
|
||||
* | 510 | 2 | Signature ( 55 aa ) |
|
||||
* ----------------------------------------------------------
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
uint8_t bootstrap[MBR_BOOTSTRAP_SIZE];
|
||||
mbr_partition_t partitions[MBR_PARTITIONS];
|
||||
uint8_t boot_signature[2];
|
||||
} __PACKED mbr_t;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma pack(pop)
|
||||
#endif
|
||||
#undef __PACKED
|
||||
|
||||
/************************ Function Protoypes **********************************/
|
||||
|
||||
int parse_ncsd(const uint8_t sector0[SECTOR_SIZE]);
|
||||
|
||||
int parse_mbr(const uint8_t sector0[SECTOR_SIZE], const int is3DS);
|
||||
|
||||
|
||||
/************************ static code verification ****************************/
|
||||
|
||||
static_assert(sizeof(ncsd_header_t) == NCSD_HEADERSIZE,
|
||||
"sizeof(ncsd_header_t) should equal 0x160");
|
||||
static_assert(sizeof(mbr_t) == SECTOR_SIZE,
|
||||
"sizeof(mbr_t) should equal 0x200");
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
379
arm9/src/nand/twltool/dsi.c
Normal file
379
arm9/src/nand/twltool/dsi.c
Normal file
@ -0,0 +1,379 @@
|
||||
#include "dsi.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "u128_math.h"
|
||||
|
||||
void dsi_set_key( dsi_context* ctx,
|
||||
const unsigned char key[16] )
|
||||
{
|
||||
unsigned char keyswap[16];
|
||||
u128_swap(keyswap, key) ;
|
||||
|
||||
aes_setkey_enc(&ctx->aes, keyswap, 128);
|
||||
}
|
||||
|
||||
void dsi_add_ctr( dsi_context* ctx,
|
||||
unsigned int carry)
|
||||
{
|
||||
unsigned int counter[4];
|
||||
unsigned char *outctr = (unsigned char*)ctx->ctr;
|
||||
int sum;
|
||||
signed int i;
|
||||
|
||||
for(i = 0; i < 4; i++)
|
||||
counter[i] = (outctr[i * 4 + 0] << 24) | (outctr[i * 4 + 1] << 16) |
|
||||
(outctr[i * 4 + 2] << 8) | (outctr[i * 4 + 3] << 0);
|
||||
|
||||
for(i = 3; i >= 0; i--)
|
||||
{
|
||||
sum = counter[i] + carry;
|
||||
|
||||
if (sum < counter[i])
|
||||
carry = 1;
|
||||
else
|
||||
carry = 0;
|
||||
|
||||
counter[i] = sum;
|
||||
}
|
||||
|
||||
for(i = 0; i < 4; i++)
|
||||
{
|
||||
outctr[i * 4 + 0] = counter[i] >> 24;
|
||||
outctr[i * 4 + 1] = counter[i] >> 16;
|
||||
outctr[i * 4 + 2] = counter[i] >> 8;
|
||||
outctr[i * 4 + 3] = counter[i] >> 0;
|
||||
}
|
||||
}
|
||||
|
||||
void dsi_set_ctr( dsi_context* ctx,
|
||||
const unsigned char ctr[16] )
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0; i<16; i++)
|
||||
ctx->ctr[i] = ctr[15-i];
|
||||
}
|
||||
|
||||
void dsi_init_ctr( dsi_context* ctx,
|
||||
const unsigned char key[16],
|
||||
const unsigned char ctr[12] )
|
||||
{
|
||||
dsi_set_key(ctx, key);
|
||||
dsi_set_ctr(ctx, ctr);
|
||||
}
|
||||
|
||||
void dsi_crypt_ctr( dsi_context* ctx,
|
||||
const void* in,
|
||||
void* out,
|
||||
unsigned int len)
|
||||
{
|
||||
unsigned int i;
|
||||
for(i = 0; i < len; i += 0x10)
|
||||
{
|
||||
dsi_crypt_ctr_block(ctx, in+i, out+i);
|
||||
}
|
||||
}
|
||||
|
||||
void dsi_crypt_ctr_block( dsi_context* ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
{
|
||||
int i;
|
||||
unsigned char stream[16];
|
||||
|
||||
|
||||
aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->ctr, stream);
|
||||
|
||||
|
||||
if (input)
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
{
|
||||
output[i] = stream[15-i] ^ input[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
output[i] = stream[15-i];
|
||||
}
|
||||
|
||||
dsi_add_ctr(ctx, 1);
|
||||
}
|
||||
|
||||
|
||||
void dsi_init_ccm( dsi_context* ctx,
|
||||
unsigned char key[16],
|
||||
unsigned int maclength,
|
||||
unsigned int payloadlength,
|
||||
unsigned int assoclength,
|
||||
unsigned char nonce[12] )
|
||||
{
|
||||
int i;
|
||||
|
||||
|
||||
|
||||
dsi_set_key(ctx, key);
|
||||
|
||||
ctx->maclen = maclength;
|
||||
|
||||
maclength = (maclength-2)/2;
|
||||
|
||||
payloadlength = (payloadlength+15) & ~15;
|
||||
|
||||
// CCM B0 block:
|
||||
// [1-byte flags] [12-byte nonce] [3-byte size]
|
||||
ctx->mac[0] = (maclength<<3) | 2;
|
||||
if (assoclength)
|
||||
ctx->mac[0] |= (1<<6);
|
||||
for(i=0; i<12; i++)
|
||||
ctx->mac[1+i] = nonce[11-i];
|
||||
ctx->mac[13] = payloadlength>>16;
|
||||
ctx->mac[14] = payloadlength>>8;
|
||||
ctx->mac[15] = payloadlength>>0;
|
||||
|
||||
aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->mac, ctx->mac);
|
||||
|
||||
// CCM CTR:
|
||||
// [1-byte flags] [12-byte nonce] [3-byte ctr]
|
||||
ctx->ctr[0] = 2;
|
||||
for(i=0; i<12; i++)
|
||||
ctx->ctr[1+i] = nonce[11-i];
|
||||
ctx->ctr[13] = 0;
|
||||
ctx->ctr[14] = 0;
|
||||
ctx->ctr[15] = 0;
|
||||
|
||||
dsi_crypt_ctr_block(ctx, 0, ctx->S0);
|
||||
}
|
||||
|
||||
void dsi_encrypt_ccm_block( dsi_context* ctx,
|
||||
unsigned char input[16],
|
||||
unsigned char output[16],
|
||||
unsigned char* mac )
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i=0; i<16; i++)
|
||||
ctx->mac[i] ^= input[15-i];
|
||||
|
||||
aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->mac, ctx->mac);
|
||||
|
||||
if (mac)
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
mac[i] = ctx->mac[15-i] ^ ctx->S0[i];
|
||||
}
|
||||
|
||||
if (output)
|
||||
dsi_crypt_ctr_block(ctx, input, output);
|
||||
}
|
||||
|
||||
|
||||
void dsi_decrypt_ccm_block( dsi_context* ctx,
|
||||
unsigned char input[16],
|
||||
unsigned char output[16],
|
||||
unsigned char* mac )
|
||||
{
|
||||
int i;
|
||||
|
||||
|
||||
if (output)
|
||||
{
|
||||
dsi_crypt_ctr_block(ctx, input, output);
|
||||
|
||||
|
||||
for(i=0; i<16; i++)
|
||||
ctx->mac[i] ^= output[15-i];
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
ctx->mac[i] ^= input[15-i];
|
||||
}
|
||||
|
||||
aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->mac, ctx->mac);
|
||||
|
||||
|
||||
if (mac)
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
mac[i] = ctx->mac[15-i] ^ ctx->S0[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void dsi_decrypt_ccm( dsi_context* ctx,
|
||||
unsigned char* input,
|
||||
unsigned char* output,
|
||||
unsigned int size,
|
||||
unsigned char* mac )
|
||||
{
|
||||
unsigned char block[16];
|
||||
unsigned char ctr[16];
|
||||
|
||||
while(size > 16)
|
||||
{
|
||||
dsi_decrypt_ccm_block(ctx, input, output, mac);
|
||||
|
||||
if (input)
|
||||
input += 16;
|
||||
if (output)
|
||||
output += 16;
|
||||
|
||||
size -= 16;
|
||||
}
|
||||
|
||||
memcpy(ctr, ctx->ctr, 16);
|
||||
memset(block, 0, 16);
|
||||
dsi_crypt_ctr_block(ctx, block, block);
|
||||
memcpy(ctx->ctr, ctr, 16);
|
||||
memcpy(block, input, size);
|
||||
|
||||
|
||||
dsi_decrypt_ccm_block(ctx, block, block, mac);
|
||||
memcpy(output, block, size);
|
||||
}
|
||||
|
||||
|
||||
void dsi_encrypt_ccm( dsi_context* ctx,
|
||||
unsigned char* input,
|
||||
unsigned char* output,
|
||||
unsigned int size,
|
||||
unsigned char* mac )
|
||||
{
|
||||
unsigned char block[16];
|
||||
|
||||
while(size > 16)
|
||||
{
|
||||
dsi_encrypt_ccm_block(ctx, input, output, mac);
|
||||
|
||||
if (input)
|
||||
input += 16;
|
||||
if (output)
|
||||
output += 16;
|
||||
|
||||
size -= 16;
|
||||
}
|
||||
|
||||
memset(block, 0, 16);
|
||||
memcpy(block, input, size);
|
||||
dsi_encrypt_ccm_block(ctx, block, block, mac);
|
||||
memcpy(output, block, size);
|
||||
}
|
||||
|
||||
void dsi_es_init( dsi_es_context* ctx,
|
||||
unsigned char key[16] )
|
||||
{
|
||||
memcpy(ctx->key, key, 16);
|
||||
ctx->randomnonce = 1;
|
||||
}
|
||||
|
||||
void dsi_es_set_nonce( dsi_es_context* ctx,
|
||||
unsigned char nonce[12] )
|
||||
{
|
||||
memcpy(ctx->nonce, nonce, 12);
|
||||
ctx->randomnonce = 0;
|
||||
}
|
||||
|
||||
void dsi_es_set_random_nonce( dsi_es_context* ctx )
|
||||
{
|
||||
ctx->randomnonce = 1;
|
||||
}
|
||||
|
||||
|
||||
int dsi_es_decrypt( dsi_es_context* ctx,
|
||||
unsigned char* buffer,
|
||||
unsigned char metablock[32],
|
||||
unsigned int size )
|
||||
{
|
||||
unsigned char ctr[16];
|
||||
unsigned char nonce[12];
|
||||
unsigned char scratchpad[16];
|
||||
unsigned char chkmac[16];
|
||||
unsigned char genmac[16];
|
||||
dsi_context cryptoctx;
|
||||
unsigned int chksize;
|
||||
|
||||
|
||||
memcpy(chkmac, metablock, 16);
|
||||
|
||||
memcpy(ctr, metablock + 16, 16);
|
||||
ctr[0] = 0;
|
||||
ctr[13] = 0;
|
||||
ctr[14] = 0;
|
||||
ctr[15] = 0;
|
||||
|
||||
dsi_init_ctr(&cryptoctx, ctx->key, ctr);
|
||||
dsi_crypt_ctr_block(&cryptoctx, metablock+16, scratchpad);
|
||||
|
||||
chksize = (scratchpad[13]<<16) | (scratchpad[14]<<8) | (scratchpad[15]<<0);
|
||||
|
||||
if (scratchpad[0] != 0x3A)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (chksize != size)
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
|
||||
memcpy(nonce, metablock + 17, 12);
|
||||
|
||||
dsi_init_ccm(&cryptoctx, ctx->key, 16, size, 0, nonce);
|
||||
dsi_decrypt_ccm(&cryptoctx, buffer, buffer, size, genmac);
|
||||
|
||||
if (memcmp(genmac, chkmac, 16) != 0)
|
||||
{
|
||||
return -3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void dsi_es_encrypt( dsi_es_context* ctx,
|
||||
unsigned char* buffer,
|
||||
unsigned char metablock[32],
|
||||
unsigned int size )
|
||||
{
|
||||
int i;
|
||||
unsigned char nonce[12];
|
||||
unsigned char mac[16];
|
||||
unsigned char ctr[16];
|
||||
unsigned char scratchpad[16];
|
||||
dsi_context cryptoctx;
|
||||
|
||||
if (ctx->randomnonce)
|
||||
{
|
||||
srand( (unsigned int)time(0) );
|
||||
|
||||
for(i=0; i<12; i++)
|
||||
nonce[i] = rand();
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(nonce, ctx->nonce, 12);
|
||||
}
|
||||
|
||||
dsi_init_ccm(&cryptoctx, ctx->key, 16, size, 0, nonce);
|
||||
dsi_encrypt_ccm(&cryptoctx, buffer, buffer, size, mac);
|
||||
|
||||
memset(scratchpad, 0, 16);
|
||||
scratchpad[0] = 0x3A;
|
||||
scratchpad[13] = size >> 16;
|
||||
scratchpad[14] = size >> 8;
|
||||
scratchpad[15] = size >> 0;
|
||||
|
||||
memset(ctr, 0, 16);
|
||||
memcpy(ctr+1, nonce, 12);
|
||||
|
||||
dsi_init_ctr(&cryptoctx, ctx->key, ctr);
|
||||
dsi_crypt_ctr_block(&cryptoctx, scratchpad, metablock+16);
|
||||
memcpy(metablock+17, nonce, 12);
|
||||
|
||||
memcpy(metablock, mac, 16);
|
||||
|
||||
}
|
||||
104
arm9/src/nand/twltool/dsi.h
Normal file
104
arm9/src/nand/twltool/dsi.h
Normal file
@ -0,0 +1,104 @@
|
||||
#ifndef _DSI_H_
|
||||
#define _DSI_H_
|
||||
|
||||
#include "polarssl/aes.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char ctr[16];
|
||||
unsigned char mac[16];
|
||||
unsigned char S0[16];
|
||||
unsigned int maclen;
|
||||
|
||||
aes_context aes;
|
||||
}
|
||||
dsi_context;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char key[16];
|
||||
unsigned char nonce[12];
|
||||
int randomnonce;
|
||||
} dsi_es_context;
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void dsi_set_key( dsi_context* ctx,
|
||||
const unsigned char key[16] );
|
||||
|
||||
void dsi_add_ctr( dsi_context* ctx,
|
||||
unsigned int carry );
|
||||
|
||||
void dsi_set_ctr( dsi_context* ctx,
|
||||
const unsigned char ctr[16] );
|
||||
|
||||
void dsi_init_ctr( dsi_context* ctx,
|
||||
const unsigned char key[16],
|
||||
const unsigned char ctr[12] );
|
||||
|
||||
void dsi_crypt_ctr( dsi_context* ctx,
|
||||
const void* in,
|
||||
void* out,
|
||||
unsigned int len);
|
||||
|
||||
void dsi_crypt_ctr_block( dsi_context* ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] );
|
||||
|
||||
void dsi_init_ccm( dsi_context* ctx,
|
||||
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,
|
||||
unsigned char input[16],
|
||||
unsigned char output[16],
|
||||
unsigned char* mac );
|
||||
|
||||
void dsi_decrypt_ccm_block( dsi_context* ctx,
|
||||
unsigned char input[16],
|
||||
unsigned char output[16],
|
||||
unsigned char* mac );
|
||||
|
||||
|
||||
void dsi_decrypt_ccm( dsi_context* ctx,
|
||||
unsigned char* input,
|
||||
unsigned char* output,
|
||||
unsigned int size,
|
||||
unsigned char* mac );
|
||||
|
||||
void dsi_encrypt_ccm( dsi_context* ctx,
|
||||
unsigned char* input,
|
||||
unsigned char* output,
|
||||
unsigned int size,
|
||||
unsigned char* mac );
|
||||
|
||||
void dsi_es_init( dsi_es_context* ctx,
|
||||
unsigned char key[16] );
|
||||
|
||||
void dsi_es_set_nonce( dsi_es_context* ctx,
|
||||
unsigned char nonce[12] );
|
||||
|
||||
void dsi_es_set_random_nonce( dsi_es_context* ctx );
|
||||
|
||||
int dsi_es_decrypt( dsi_es_context* ctx,
|
||||
unsigned char* buffer,
|
||||
unsigned char metablock[32],
|
||||
unsigned int size );
|
||||
|
||||
void dsi_es_encrypt( dsi_es_context* ctx,
|
||||
unsigned char* buffer,
|
||||
unsigned char metablock[32],
|
||||
unsigned int size );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _DSI_H_
|
||||
106
arm9/src/nand/u128_math.c
Normal file
106
arm9/src/nand/u128_math.c
Normal file
@ -0,0 +1,106 @@
|
||||
// u128_math
|
||||
|
||||
#include "u128_math.h"
|
||||
#include <string.h>
|
||||
|
||||
// rotate a 128bit, little endian by shift bits in direction of increasing significance.
|
||||
void u128_lrot(uint8_t *num, uint32_t shift)
|
||||
{
|
||||
uint8_t tmp[16];
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
// rot: rotate to more significant.
|
||||
// LSB is tmp[0], MSB is tmp[15]
|
||||
const uint32_t byteshift = shift / 8 ;
|
||||
const uint32_t bitshift = shift % 8;
|
||||
tmp[(i+byteshift) % 16] = (num[i] << bitshift)
|
||||
| ((num[(i+16-1) % 16] >> (8-bitshift)) & 0xff);
|
||||
}
|
||||
memcpy(num, tmp, 16) ;
|
||||
}
|
||||
|
||||
// rotate a 128bit, little endian by shift bits in direction of decreasing significance.
|
||||
void u128_rrot(uint8_t *num, uint32_t shift)
|
||||
{
|
||||
uint8_t tmp[16];
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
// rot: rotate to less significant.
|
||||
// LSB is tmp[0], MSB is tmp[15]
|
||||
const uint32_t byteshift = shift / 8 ;
|
||||
const uint32_t bitshift = shift % 8;
|
||||
tmp[i] = (num[(i+byteshift) % 16] >> bitshift)
|
||||
| ((num[(i+byteshift+1) % 16] << (8-bitshift)) & 0xff);
|
||||
}
|
||||
memcpy(num, tmp, 16) ;
|
||||
}
|
||||
|
||||
// xor two 128bit, little endian values and store the result into the first
|
||||
void u128_xor(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
a[i] = a[i] ^ b[i] ;
|
||||
}
|
||||
}
|
||||
|
||||
// or two 128bit, little endian values and store the result into the first
|
||||
void u128_or(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
a[i] = a[i] | b[i] ;
|
||||
}
|
||||
}
|
||||
|
||||
// and two 128bit, little endian values and store the result into the first
|
||||
void u128_and(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
a[i] = a[i] & b[i] ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// add two 128bit, little endian values and store the result into the first
|
||||
void u128_add(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
uint8_t carry = 0 ;
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
uint16_t sum = a[i] + b[i] + carry ;
|
||||
a[i] = sum & 0xff ;
|
||||
carry = sum >> 8 ;
|
||||
}
|
||||
}
|
||||
|
||||
// add two 128bit, little endian values and store the result into the first
|
||||
void u128_add32(uint8_t *a, const uint32_t b)
|
||||
{
|
||||
uint8_t _b[16];
|
||||
memset(_b, 0, sizeof(_b)) ;
|
||||
for (int i=0;i<4;i++)
|
||||
_b[i] = b >> (i*8) ;
|
||||
u128_add(a, _b);
|
||||
}
|
||||
|
||||
// sub two 128bit, little endian values and store the result into the first
|
||||
void u128_sub(uint8_t *a, const uint8_t *b)
|
||||
{
|
||||
uint8_t carry = 0 ;
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
uint16_t sub = a[i] - b[i] - (carry & 1);
|
||||
a[i] = sub & 0xff ;
|
||||
carry = sub >> 8 ;
|
||||
}
|
||||
}
|
||||
|
||||
void u128_swap(uint8_t *out, const uint8_t *in)
|
||||
{
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
out[15-i] = in[i];
|
||||
}
|
||||
}
|
||||
38
arm9/src/nand/u128_math.h
Normal file
38
arm9/src/nand/u128_math.h
Normal file
@ -0,0 +1,38 @@
|
||||
// u128_math
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// left rotate by (shift % 128) bits
|
||||
void u128_lrot(uint8_t *num, uint32_t shift) ;
|
||||
|
||||
// right rotate by (shift % 128) bits
|
||||
void u128_rrot(uint8_t *num, uint32_t shift) ;
|
||||
|
||||
// bitwise xor strored to a
|
||||
void u128_xor(uint8_t *a, const uint8_t *b) ;
|
||||
|
||||
// bitwise or strored to a
|
||||
void u128_or(uint8_t *a, const uint8_t *b) ;
|
||||
|
||||
// bitwise and strored to a
|
||||
void u128_and(uint8_t *a, const uint8_t *b) ;
|
||||
|
||||
// add b to a and store in a
|
||||
void u128_add(uint8_t *a, const uint8_t *b) ;
|
||||
|
||||
// add 32 bit value b to a and store in a
|
||||
void u128_add32(uint8_t *a, const uint32_t b) ;
|
||||
|
||||
// substract b from a and store in a
|
||||
void u128_sub(uint8_t *a, const uint8_t *b) ;
|
||||
|
||||
// swap byte order
|
||||
void u128_swap(uint8_t *out, const uint8_t *in) ;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -83,6 +83,9 @@ int copyFile(char const* src, char const* dst)
|
||||
{
|
||||
if (!src) return 1;
|
||||
|
||||
if(strncmp(dst, "nand:", 5) == 0)
|
||||
nandWritten = true;
|
||||
|
||||
unsigned long long size = getFileSizePath(src);
|
||||
return copyFilePart(src, 0, size, dst);
|
||||
}
|
||||
@ -92,6 +95,9 @@ int copyFilePart(char const* src, u32 offset, u32 size, char const* dst)
|
||||
if (!src) return 1;
|
||||
if (!dst) return 2;
|
||||
|
||||
if(strncmp(dst, "nand:", 5) == 0)
|
||||
nandWritten = true;
|
||||
|
||||
FILE* fin = fopen(src, "rb");
|
||||
|
||||
if (!fin)
|
||||
@ -124,7 +130,7 @@ int copyFilePart(char const* src, u32 offset, u32 size, char const* dst)
|
||||
#define BUFF_SIZE 128 //Arbitrary. A value too large freezes the ds.
|
||||
char* buffer = (char*)malloc(BUFF_SIZE);
|
||||
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
unsigned int toRead = BUFF_SIZE;
|
||||
if (size - totalBytesRead < BUFF_SIZE)
|
||||
@ -179,6 +185,9 @@ bool padFile(char const* path, int size)
|
||||
{
|
||||
if (!path) return false;
|
||||
|
||||
if(strncmp(path, "nand:", 5) == 0)
|
||||
nandWritten = true;
|
||||
|
||||
FILE* f = fopen(path, "ab");
|
||||
if (!f)
|
||||
{
|
||||
@ -212,6 +221,9 @@ bool copyDir(char const* src, char const* dst)
|
||||
{
|
||||
if (!src || !dst) return false;
|
||||
|
||||
if(strncmp(dst, "nand:", 5) == 0)
|
||||
nandWritten = true;
|
||||
|
||||
// iprintf("copyDir\n%s\n%s\n\n", src, dst);
|
||||
|
||||
bool result = true;
|
||||
@ -447,7 +459,7 @@ int getMenuSlotsFree()
|
||||
for (int i = 0; i < NUM_OF_DIRS; i++)
|
||||
{
|
||||
char path[256];
|
||||
sprintf(path, "/title/%s", dirs[i]);
|
||||
sprintf(path, "nand:/title/%s", dirs[i]);
|
||||
|
||||
dir = opendir(path);
|
||||
|
||||
@ -481,7 +493,7 @@ unsigned long long getSDCardSize()
|
||||
if (sdIsInserted())
|
||||
{
|
||||
struct statvfs st;
|
||||
if (statvfs("/", &st) == 0)
|
||||
if (statvfs("sd:/", &st) == 0)
|
||||
return st.f_bsize * st.f_blocks;
|
||||
}
|
||||
|
||||
@ -503,30 +515,18 @@ unsigned long long getSDCardFree()
|
||||
//internal storage
|
||||
unsigned long long getDsiSize()
|
||||
{
|
||||
//The DSi has 256MB of internal storage. Some is unavailable and used by other things.
|
||||
//An empty DSi reads 1024 open blocks
|
||||
return 1024 * BYTES_PER_BLOCK;
|
||||
struct statvfs st;
|
||||
if (statvfs("nand:/", &st) == 0)
|
||||
return st.f_bsize * st.f_blocks;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long long getDsiFree()
|
||||
{
|
||||
//Get free space by subtracting file sizes in emulated nand folders
|
||||
unsigned long long size = getDsiSize();
|
||||
unsigned long long appSize = getDirSize("/title/00030004");
|
||||
struct statvfs st;
|
||||
if (statvfs("nand:/", &st) == 0)
|
||||
return st.f_bsize * st.f_bavail;
|
||||
|
||||
//round up to a full block
|
||||
if (appSize % BYTES_PER_BLOCK != 0)
|
||||
appSize = ((int)(appSize / BYTES_PER_BLOCK) * BYTES_PER_BLOCK) + BYTES_PER_BLOCK;
|
||||
|
||||
//subtract, but don't go under 0
|
||||
if (appSize > size)
|
||||
{
|
||||
size = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
size -= appSize;
|
||||
}
|
||||
|
||||
return size;
|
||||
return 0;
|
||||
}
|
||||
@ -4,7 +4,7 @@
|
||||
#include <nds/ndstypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define BACKUP_PATH "/titlebackup"
|
||||
#define BACKUP_PATH "sd:/titlebackup"
|
||||
#define BYTES_PER_BLOCK (1024*128)
|
||||
|
||||
//printing
|
||||
|
||||
@ -30,7 +30,7 @@ void titleMenu()
|
||||
}
|
||||
else
|
||||
{
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
@ -94,11 +94,11 @@ static void generateList(Menu* m)
|
||||
bool done = false;
|
||||
int count = 0; //used to skip to the right page
|
||||
|
||||
//search each category directory /title/XXXXXXXX
|
||||
//search each category directory nand:/title/XXXXXXXX
|
||||
for (int i = 0; i < NUM_OF_DIRS && done == false; i++)
|
||||
{
|
||||
char* dirPath = (char*)malloc(strlen(dirs[i])+10);
|
||||
sprintf(dirPath, "/title/%s", dirs[i]);
|
||||
char* dirPath = (char*)malloc(strlen(dirs[i])+15);
|
||||
sprintf(dirPath, "nand:/title/%s", dirs[i]);
|
||||
|
||||
struct dirent* ent;
|
||||
DIR* dir = opendir(dirPath);
|
||||
@ -112,7 +112,7 @@ static void generateList(Menu* m)
|
||||
|
||||
if (ent->d_type == DT_DIR)
|
||||
{
|
||||
//scan content folder /title/XXXXXXXX/content
|
||||
//scan content folder nand:/title/XXXXXXXX/content
|
||||
char* contentPath = (char*)malloc(strlen(dirPath) + strlen(ent->d_name) + 20);
|
||||
sprintf(contentPath, "%s/%s/content", dirPath, ent->d_name);
|
||||
|
||||
@ -196,7 +196,7 @@ static int subMenu()
|
||||
|
||||
printMenu(m);
|
||||
|
||||
while (1)
|
||||
while (!programEnd)
|
||||
{
|
||||
swiWaitForVBlank();
|
||||
scanKeys();
|
||||
@ -268,12 +268,12 @@ static void backup(Menu* m)
|
||||
u32 tid_high = 1;
|
||||
getTitleId(h, &tid_low, &tid_high);
|
||||
|
||||
char* srcpath = (char*)malloc(strlen("/title/") + 32);
|
||||
sprintf(srcpath, "/title/%08x/%08x", (unsigned int)tid_high, (unsigned int)tid_low);
|
||||
char* srcpath = (char*)malloc(strlen("nand:/title/") + 32);
|
||||
sprintf(srcpath, "nand:/title/%08x/%08x", (unsigned int)tid_high, (unsigned int)tid_low);
|
||||
|
||||
if (getSDCardFree() < getDirSize(srcpath))
|
||||
{
|
||||
messageBox("Not enough space on SD card.");
|
||||
messageBox("Not enough space on NAND.");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -281,20 +281,20 @@ static void backup(Menu* m)
|
||||
sprintf(dstpath, "%s/%s", BACKUP_PATH, backname);
|
||||
|
||||
//create dirs
|
||||
mkdir(BACKUP_PATH, 0777); // /titlebackup
|
||||
mkdir(dstpath, 0777); // /titlebackup/App Name - XXXX
|
||||
mkdir(BACKUP_PATH, 0777); // sd:/titlebackup
|
||||
mkdir(dstpath, 0777); // sd:/titlebackup/App Name - XXXX
|
||||
free(dstpath);
|
||||
|
||||
dstpath = (char*)malloc(strlen(BACKUP_PATH) + strlen(backname) + 16);
|
||||
sprintf(dstpath, "%s/%s/%08x", BACKUP_PATH, backname, (unsigned int)tid_high);
|
||||
|
||||
mkdir(dstpath, 0777); // /titlebackup/App Name - XXXX/tid_high
|
||||
mkdir(dstpath, 0777); // sd:/titlebackup/App Name - XXXX/tid_high
|
||||
free(dstpath);
|
||||
|
||||
dstpath = (char*)malloc(strlen(BACKUP_PATH) + strlen(backname) + 32);
|
||||
sprintf(dstpath, "%s/%s/%08x/%08x", BACKUP_PATH, backname, (unsigned int)tid_high, (unsigned int)tid_low);
|
||||
|
||||
mkdir(dstpath, 0777); // /titlebackup/App Name - XXXX/tid_high/tid_low
|
||||
mkdir(dstpath, 0777); // sd:/titlebackup/App Name - XXXX/tid_high/tid_low
|
||||
|
||||
// iprintf("dst %s\nsrc %s", dstpath, srcpath);
|
||||
// keyWait(KEY_A);
|
||||
@ -347,7 +347,7 @@ static bool delete(Menu* m)
|
||||
else
|
||||
{
|
||||
char dirPath[64];
|
||||
sprintf(dirPath, "%.24s", fpath);
|
||||
sprintf(dirPath, "%.29s", fpath);
|
||||
|
||||
if (!dirExists(dirPath))
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user