Move includes into source/ directory, some small cleanups

This commit is contained in:
archshift 2015-05-10 00:14:42 -07:00
parent 2c131f4c63
commit bc5aa24d4f
12 changed files with 83 additions and 114 deletions

View File

@ -20,7 +20,7 @@ export TARGET := $(shell basename $(CURDIR))
BUILD := build
SOURCES := source source/fatfs
DATA := data
INCLUDES := include source
INCLUDES := source
#---------------------------------------------------------------------------------
# Setup some defines

View File

@ -8,19 +8,21 @@
//#include "DrawCharacter.h"
#include "misc.h"
void ioDelay( u32 us );
void ioDelay(u32 us);
extern u8* bottomScreen;
int CartInited=0;
int CartID=-1;
u32 CartType=0;
int A0_Response=-1;
int CartID = -1;
u32 CartType = 0;
int A0_Response = -1;
u32 rand1 = 0;
u32 rand2 = 0;
u32 BSWAP32(u32 val) {
return (((val >> 24) & 0xFF)) | (((val >> 16) & 0xFF) << 8) | (((val >> 8) & 0xFF) << 16) | (((val) & 0xFF)<<24);
return (((val >> 24) & 0xFF)) |
(((val >> 16) & 0xFF) << 8) |
(((val >> 8) & 0xFF) << 16) |
((val & 0xFF) << 24);
}
//Reset the cart slot?
@ -29,22 +31,22 @@ u32 BSWAP32(u32 val) {
void GatewayCartInit()
{
REG_CARDCONF2 = 0x0C;
REG_CARDCONF &= ~3;
if(REG_CARDCONF2 == 0xC)
{
while(REG_CARDCONF2 != 0);
}
if(REG_CARDCONF2 != 0)
{
return;
}
REG_CARDCONF2 = 0x4;
while(REG_CARDCONF2 != 0x4);
REG_CARDCONF2 = 0x8;
while(REG_CARDCONF2 != 0x8);
}
@ -66,7 +68,7 @@ void SwitchToCTRCARD()
void NTR_SendCommand( u32 command[2], u32 pageSize, u32 latency, void * buffer )
{
REG_NTRCARDMCNT = NTRCARD_CR1_ENABLE;
for( u32 i=0; i<2; ++i )
{
REG_NTRCARDCMD[i*4+0] = command[i]>>24;
@ -104,10 +106,10 @@ void NTR_SendCommand( u32 command[2], u32 pageSize, u32 latency, void * buffer )
u32 count = 0;
u32 cardCtrl = REG_NTRCARDROMCNT;
if(useBuf32)
{
while( (cardCtrl & NTRCARD_BUSY) && count < pageSize)
while( (cardCtrl & NTRCARD_BUSY) && count < pageSize)
{
cardCtrl = REG_NTRCARDROMCNT;
if( cardCtrl & NTRCARD_DATA_READY ) {
@ -119,7 +121,7 @@ void NTR_SendCommand( u32 command[2], u32 pageSize, u32 latency, void * buffer )
}
else if(useBuf)
{
while( (cardCtrl & NTRCARD_BUSY) && count < pageSize)
while( (cardCtrl & NTRCARD_BUSY) && count < pageSize)
{
cardCtrl = REG_NTRCARDROMCNT;
if( cardCtrl & NTRCARD_DATA_READY ) {
@ -135,7 +137,7 @@ void NTR_SendCommand( u32 command[2], u32 pageSize, u32 latency, void * buffer )
}
else
{
while( (cardCtrl & NTRCARD_BUSY) && count < pageSize)
while( (cardCtrl & NTRCARD_BUSY) && count < pageSize)
{
cardCtrl = REG_NTRCARDROMCNT;
if( cardCtrl & NTRCARD_DATA_READY ) {
@ -148,7 +150,7 @@ void NTR_SendCommand( u32 command[2], u32 pageSize, u32 latency, void * buffer )
// if read is not finished, ds will not pull ROM CS to high, we pull it high manually
if( count != transferLength ) {
// MUST wait for next data ready,
// MUST wait for next data ready,
// if ds pull ROM CS to high during 4 byte data transfer, something will mess up
// so we have to wait next data ready
do { cardCtrl = REG_NTRCARDROMCNT; } while(!(cardCtrl & NTRCARD_DATA_READY));
@ -169,7 +171,7 @@ void CTR_SendCommand( u32 command[4], u32 pageSize, u32 blocks, u32 latency, voi
REG_CTRCARDCMD[1] = command[2];
REG_CTRCARDCMD[2] = command[1];
REG_CTRCARDCMD[3] = command[0];
//Make sure this never happens
if(blocks == 0) blocks = 1;
@ -207,7 +209,7 @@ void CTR_SendCommand( u32 command[4], u32 pageSize, u32 blocks, u32 latency, voi
pageParam = CTRCARD_PAGESIZE_4K;
break;
}
REG_CTRCARDBLKCNT = blocks - 1;
transferLength *= blocks;
@ -222,10 +224,10 @@ void CTR_SendCommand( u32 command[4], u32 pageSize, u32 blocks, u32 latency, voi
u32 count = 0;
u32 cardCtrl = REG_CTRCARDCNT;
if(useBuf32)
{
while( (cardCtrl & CTRCARD_BUSY) && count < transferLength)
while( (cardCtrl & CTRCARD_BUSY) && count < transferLength)
{
cardCtrl = REG_CTRCARDCNT;
if( cardCtrl & CTRCARD_DATA_READY ) {
@ -237,7 +239,7 @@ void CTR_SendCommand( u32 command[4], u32 pageSize, u32 blocks, u32 latency, voi
}
else if(useBuf)
{
while( (cardCtrl & CTRCARD_BUSY) && count < transferLength)
while( (cardCtrl & CTRCARD_BUSY) && count < transferLength)
{
cardCtrl = REG_CTRCARDCNT;
if( cardCtrl & CTRCARD_DATA_READY ) {
@ -253,7 +255,7 @@ void CTR_SendCommand( u32 command[4], u32 pageSize, u32 blocks, u32 latency, voi
}
else
{
while( (cardCtrl & CTRCARD_BUSY) && count < transferLength)
while( (cardCtrl & CTRCARD_BUSY) && count < transferLength)
{
cardCtrl = REG_CTRCARDCNT;
if( cardCtrl & CTRCARD_DATA_READY ) {
@ -266,7 +268,7 @@ void CTR_SendCommand( u32 command[4], u32 pageSize, u32 blocks, u32 latency, voi
// if read is not finished, ds will not pull ROM CS to high, we pull it high manually
if( count != transferLength ) {
// MUST wait for next data ready,
// MUST wait for next data ready,
// if ds pull ROM CS to high during 4 byte data transfer, something will mess up
// so we have to wait next data ready
do { cardCtrl = REG_CTRCARDCNT; } while(!(cardCtrl & CTRCARD_DATA_READY));
@ -285,7 +287,7 @@ u32 Cart_GetSecureID()
{
u32 id = 0;
u32 getid_cmd[4] = { 0xA2000000, 0x00000000, rand1, rand2 };
CTR_SendCommand( getid_cmd, 0x4, 1, 0x100802C, &id );
CTR_SendCommand(getid_cmd, 0x4, 1, 0x100802C, &id);
return id;
}
@ -297,7 +299,11 @@ int Cart_IsInserted()
void Cart_ReadSectorSD(u8* aBuffer,u32 aSector)
{
u64 adr = ((u64)0xBF << 56) | (aSector * 0x200);
u32 readheader_cmd[4] = { (u32)(adr>>32), (u32)(adr&0xFFFFFFFF), 0x00000000, 0x00000000 };
u32 readheader_cmd[4] = {
(u32)(adr >> 32),
(u32)(adr&0xFFFFFFFF),
0x00000000, 0x00000000
};
CTR_SendCommand( readheader_cmd, 0x200, 1, 0x100802C, aBuffer );
}
@ -308,82 +314,60 @@ u32 Cart_GetID()
void Cart_Init()
{
//Skip init if its already been done
//if(CartInited) return;
GatewayCartInit(); //Seems to reset the cart slot?
REG_CTRCARDSECCNT &= 0xFFFFFFFB;
ioDelay(0xF000);
SwitchToNTRCARD();
ioDelay(0xF000);
REG_NTRCARDROMCNT = 0;
REG_NTRCARDMCNT = REG_NTRCARDMCNT&0xFF;
ioDelay(167550);
REG_NTRCARDMCNT |= (NTRCARD_CR1_ENABLE | NTRCARD_CR1_IRQ);
REG_NTRCARDROMCNT = NTRCARD_nRESET | NTRCARD_SEC_SEED;
while(REG_NTRCARDROMCNT & NTRCARD_BUSY);
//Reset
while (REG_NTRCARDROMCNT & NTRCARD_BUSY);
// Reset
u32 reset_cmd[2] = { 0x9F000000, 0x00000000 };
NTR_SendCommand( reset_cmd, 0x2000, NTRCARD_CLK_SLOW|NTRCARD_DELAY1(0x1FFF)|NTRCARD_DELAY2(0x18), NULL );
NTR_SendCommand(reset_cmd, 0x2000, NTRCARD_CLK_SLOW | NTRCARD_DELAY1(0x1FFF) | NTRCARD_DELAY2(0x18), NULL);
u32 getid_cmd[2] = { 0x90000000, 0x00000000 };
NTR_SendCommand( getid_cmd, 0x4, NTRCARD_CLK_SLOW|NTRCARD_DELAY1(0x1FFF)|NTRCARD_DELAY2(0x18), &CartID );
if((CartID & 0x10000000)) //3ds
NTR_SendCommand(getid_cmd, 0x4, NTRCARD_CLK_SLOW | NTRCARD_DELAY1(0x1FFF) | NTRCARD_DELAY2(0x18), &CartID);
if ((CartID & 0x10000000)) // 3ds
{
u32 unknowna0_cmd[2] = { 0xA0000000, 0x00000000 };
NTR_SendCommand( unknowna0_cmd, 0x4, 0, &A0_Response );
NTR_SendCommand(unknowna0_cmd, 0x4, 0, &A0_Response);
u32 enter16bytemode_cmd[2] = { 0x3E000000, 0x00000000 };
NTR_SendCommand( enter16bytemode_cmd, 0x0, 0, NULL );
NTR_SendCommand(enter16bytemode_cmd, 0x0, 0, NULL);
SwitchToCTRCARD();
ioDelay(0xF000);
REG_CTRCARDBLKCNT = 0;
}
/*u8 data[0x200];
u32 readheader_cmd[4] = { 0x82000000, 0x00000000, 0x00000000, 0x00000000 };
CTR_SendCommand( readheader_cmd, 0x200, 1, 0x802C, &data );*/
/*char test[200];
for(int i=0; i<16*16; i+=16)
{
int j = 16*16 + i;
sprintf(test,"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
data[j+0],data[j+1],data[j+2],data[j+3],data[j+4],data[j+5],data[j+6],data[j+7],
data[j+8],data[j+9],data[j+10],data[j+11],data[j+12],data[j+13],data[j+14],data[j+15]);
DrawString((unsigned char *)0x20184E60, test, 4, 70+i/2, RGB(40, 40, 40), RGB(208, 208, 208));
sprintf(test,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",
data[j+0],data[j+1],data[j+2],data[j+3],data[j+4],data[j+5],data[j+6],data[j+7],
data[j+8],data[j+9],data[j+10],data[j+11],data[j+12],data[j+13],data[j+14],data[j+15]);
DrawString((unsigned char *)0x20184E60, test, 266, 70+i/2, RGB(40, 40, 40), RGB(208, 208, 208));
}*/
//sprintf(test,"0x100 = %02x%02x%02x%02x (NCSD)",data[0x100],data[0x101],data[0x102],data[0x103]);
//DrawString((unsigned char *)0x20184E60, test, 10, 80, RGB(40, 40, 40), RGB(208, 208, 208));
CartInited = 1;
}
void SendReadCommand( u32 sector, u32 length, u32 blocks, void * buffer )
{
u32 read_cmd[4] = { (0xBF000000 | (u32)(sector>>23)), (u32)((sector<<9) & 0xFFFFFFFF), 0x00000000, 0x00000000 };
CTR_SendCommand( read_cmd, length, blocks, 0x100822C, buffer );
u32 read_cmd[4] = {
(0xBF000000 | (u32)(sector>>23)),
(u32)((sector<<9) & 0xFFFFFFFF),
0x00000000, 0x00000000
};
CTR_SendCommand(read_cmd, length, blocks, 0x100822C, buffer);
}
void GetHeader( void * buffer )
void GetHeader(void * buffer)
{
u32 readheader_cmd[4] = { 0x82000000, 0x00000000, 0x00000000, 0x00000000 };
CTR_SendCommand( readheader_cmd, 0x200, 1, 0x4802C, buffer );
CTR_SendCommand(readheader_cmd, 0x200, 1, 0x4802C, buffer);
}
//returns 1 if MAC valid otherwise 0
@ -445,47 +429,47 @@ void AES_SetKeyControl(u32 a) {
void Cart_Secure_Init(u32 *buf,u32 *out)
{
AES_SetKeyControl(0x3B);
u8 mac_valid = card_aes(out, buf, 0x200);
// if(!mac_valid)
// if(!mac_valid)
// ClearScreen(bottomScreen, RGB(255, 0, 0));
ioDelay(0xF0000);
ctr_set_sec_key(A0_Response);
ctr_set_sec_seed(out, true);
rand1 = 0x42434445;//*((vu32*)0x10011000);
rand2 = 0x46474849;//*((vu32*)0x10011010);
u32 seed_cmd[4] = { 0x83000000, 0x00000000, rand1, rand2 };
CTR_SendCommand( seed_cmd, 0, 1, 0x100822C, NULL );
out[3] = BSWAP32(rand2);
out[2] = BSWAP32(rand1);
ctr_set_sec_seed(out, false);
//ClearScreen(bottomScreen, RGB(255, 0, 255));
u32 test = 0;
u32 A2_cmd[4] = { 0xA2000000, 0x00000000, rand1, rand2 };
CTR_SendCommand( A2_cmd, 4, 1, 0x100822C, &test );
//ClearScreen(bottomScreen, RGB(0, 255, 0));
u32 test2 = 0;
u32 A3_cmd[4] = { 0xA3000000, 0x00000000, rand1, rand2 };
CTR_SendCommand( A3_cmd, 4, 1, 0x100822C, &test2 );
//ClearScreen(bottomScreen, RGB(255, 0, 0));
u32 C5_cmd[4] = { 0xC5000000, 0x00000000, rand1, rand2 };
CTR_SendCommand( C5_cmd, 0, 1, 0x100822C, NULL );
//ClearScreen(bottomScreen, RGB(0, 0, 255));
CTR_SendCommand( A2_cmd, 4, 1, 0x100822C, &test );
CTR_SendCommand( A2_cmd, 4, 1, 0x100822C, &test );
CTR_SendCommand( A2_cmd, 4, 1, 0x100822C, &test );

View File

@ -67,8 +67,6 @@ int main() {
Debug("Successfully f_mounted");
Debug("cleared stuff! Initializing game card...", 1, 1, 0xFF);
// ROM DUMPING CODE STARTS HERE
Cart_Init();
@ -82,18 +80,6 @@ int main() {
Debug("Done reading header: %08x :)...", *(u32*)&header[0x100]);
// TODO: Check first header bytes for "NCSD" or other magic words which should be there for valid NCCHs
/* if(*ptr != 0x4453434E) // Check for "NCSD" magic
{
Debug("Bad header read! Got %08x, expected %08x", *ptr, 0x4453434E);
svcSleepThread(150000000LL);
wait_key();
return 0;
}
Debug("Done checking NCSD magic:)...", 1, 1, 0xFF);
*/
u32 sec_keys[4];
Cart_Secure_Init((u32*)header,sec_keys);
@ -111,13 +97,14 @@ int main() {
wait_key();
Debug("File name: \"%s\"", String2);
unsigned flags = FA_READ | FA_WRITE | FA_CREATE_ALWAYS;
ret = (f_open(&file, String2, flags) == FR_OK);
if (!ret) {
if (f_open(&file, String2, flags) != FR_OK) {
Debug("Failed to create file...");
wait_key();
return 0;
}
f_lseek(&file, 0);
f_sync(&file);
Debug("Successfully created file");
@ -131,26 +118,24 @@ int main() {
wait_key();
u32 cartSize = (*((u32*)&target[0x104]));
u32 cartSize = *(u32*)(&target[0x104]);
Debug("Cart size: %d MB", cartSize * 0x200 / 1024/1024);
wait_key();
// Dump remaining data
for(u32 adr=0x20; adr<cartSize; adr+=ramCache)
{
for(u32 adr=0x20; adr<cartSize; adr+=ramCache) {
ClearTop();
Debug("Wrote 0x%x bytes, e.g. %08x",bytes_written, *(u32*)target);
u32 dumped = cartSize - adr;
if(dumped > ramCache) dumped = ramCache;
for(u32 adr2=0; (adr2 < dumped); adr2+=blocks)
{
for(u32 adr2=0; (adr2 < dumped); adr2+=blocks) {
u32 currentSector = (adr+adr2);
u32 percent = ((currentSector*100)/cartSize);
if ((adr2 % (blocks*3)) == blocks*2)
Debug("Dumping %08X / %08X - %03d%%",currentSector,cartSize,percent);
SendReadCommand( currentSector, mediaUnit, blocks, (void*)(target+(adr2*mediaUnit)) );
SendReadCommand(currentSector, mediaUnit, blocks, (void*)(target + (adr2 * mediaUnit)));
}
f_write(&file, target, dumped * mediaUnit, &bytes_written);
f_sync(&file);