Skip to content

Commit

Permalink
Add NDS dumping features
Browse files Browse the repository at this point in the history
Mostly osilloscorpion's (deleted account) work with a couple touches
from @idgrepthat.
  • Loading branch information
d0k3 committed Aug 8, 2016
1 parent fdeef83 commit 34e96d0
Show file tree
Hide file tree
Showing 13 changed files with 710 additions and 21 deletions.
129 changes: 116 additions & 13 deletions source/decryptor/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "platform.h"
#include "gamecart/protocol.h"
#include "gamecart/command_ctr.h"
#include "gamecart/command_ntr.h"
#include "decryptor/aes.h"
#include "decryptor/sha.h"
#include "decryptor/decryptor.h"
Expand Down Expand Up @@ -1479,7 +1480,7 @@ static u32 DecryptCartNcchToFile(u32 offset_cart, u32 offset_file, u32 size, u32
return 0;
}

u32 DumpGameCart(u32 param)
u32 DumpCtrGameCart(u32 param)
{
NcsdHeader* ncsd = (NcsdHeader*) 0x20316000;
NcchHeader* ncch = (NcchHeader*) 0x20317000;
Expand All @@ -1490,18 +1491,7 @@ u32 DumpGameCart(u32 param)
u64 data_size = 0;
u64 dump_size = 0;
u32 result = 0;


// check if cartridge inserted
if (REG_CARDCONF2 & 0x1) {
Debug("Cartridge was not detected");
return 1;
}

// initialize cartridge
Cart_Init();
Debug("Cartridge ID: %08X", Cart_GetID());


// read cartridge NCCH header
CTR_CmdReadHeader(ncch);
if (memcmp(ncch->magic, "NCCH", 4) != 0) {
Expand Down Expand Up @@ -1683,6 +1673,113 @@ u32 DumpGameCart(u32 param)
return result;
}

u32 DumpNtrGameCart(u32 param)
{
char filename[64];
u64 cart_size = 0;
u64 data_size = 0;
u64 dump_size = 0;
u8* buff = BUFFER_ADDRESS;
u64 offset = 0x8000;
char name[16];

memset (buff, 0x00, 0x4000);


NTR_CmdReadHeader (buff);
if (buff[0] == 0x00) {
Debug("Error reading cart header");
return 1;
}

memset (name, 0x00, sizeof (name));
memcpy (name, &buff[0x00], 12);
Debug("Product name: %s", name);

memset (name, 0x00, sizeof (name));
memcpy (name, &buff[0x0C], 4 + 2);
Debug("Product ID: %s", name);

cart_size = (128 * 1024) << buff[0x14];
data_size = *((u32*)&buff[0x80]);;
dump_size = (param & CD_TRIM) ? data_size : cart_size;
Debug("Cartridge data size: %lluMB", cart_size / 0x100000);
Debug("Cartridge used size: %lluMB", data_size / 0x100000);
Debug("Cartridge dump size: %lluMB", dump_size / 0x100000);

//Unitcode (00h=NDS, 02h=NDS+DSi, 03h=DSi) (bit1=DSi)
if (buff[0x12] == 0x02) {
Debug ("Hybrid(DS+DSi) cartridge was detected");
Debug ("This dumper supports only DS mode");
}
else if (buff[0x12] != 0x00) {
Debug ("DSi Cartridge is not supported");
return 1;
}

if (!NTR_Secure_Init (buff, Cart_GetID())) {
Debug("Error reading secure data");
return 1;
}

Debug("");
snprintf(filename, 64, "/%s%s%s.nds", GetGameDir() ? GetGameDir() : "", GetGameDir() ? "/" : "", name);

if (!DebugFileCreate(filename, true))
return 1;
if (!DebugFileWrite(buff, 0x8000, 0)) {
FileClose();
return 1;
}

u32 stop = 0;
for (offset=0x8000;offset < dump_size;offset+=CART_CHUNK_SIZE) {
if( (offset + CART_CHUNK_SIZE) > dump_size)
stop = (offset + CART_CHUNK_SIZE)-dump_size; // correct over-sized writes with "stop" variable

for(u32 i=0;i < CART_CHUNK_SIZE;i+=0x200) {
NTR_CmdReadData (offset+i, buff+i);
}
if (!DebugFileWrite((void*) buff, CART_CHUNK_SIZE - stop, offset)) {
FileClose();
return 1;
}
ShowProgress(offset, dump_size);
}

FileClose ();
ShowProgress(0, 0);
return 0;
}

u32 DumpGameCart(u32 param)
{
u32 cartId;

// check if cartridge inserted
if (REG_CARDCONF2 & 0x1) {
Debug("Cartridge was not detected");
return 1;
}

// initialize cartridge
Cart_Init();
cartId = Cart_GetID();
Debug("Cartridge ID: %08X", Cart_GetID());
Debug("Cartridge Type: %s", (cartId & 0x10000000) ? "CTR" : "NTR");

// check cartridge type
if ((cartId & 0x10000000) && (param & CD_NTRCART)) {
Debug("NTR cart dump selected but CTR detected");
return 1;
} else if (!(cartId & 0x10000000) && !(param & CD_NTRCART)) {
Debug("CTR cart dump selected but NTR detected");
return 1;
}

return (cartId & 0x10000000) ? DumpCtrGameCart(param) : DumpNtrGameCart(param);
}

u32 DumpPrivateHeader(u32 param)
{
(void) param;
Expand All @@ -1707,6 +1804,12 @@ u32 DumpPrivateHeader(u32 param)
*(u32*) (privateHeader + 0x48) = 0xFFFFFFFF;
*(u32*) (privateHeader + 0x4C) = 0xFFFFFFFF;

// check for NTR cartridge
if (!(cartId & 0x10000000)) {
Debug("Error: NTR carts have no private headers");
return 1;
}

// read cartridge NCCH header
CTR_CmdReadHeader(ncch);
if (memcmp(ncch->magic, "NCCH", 4) != 0) {
Expand Down
1 change: 1 addition & 0 deletions source/decryptor/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define CD_TRIM (1<<0)
#define CD_DECRYPT (1<<1)
#define CD_MAKECIA (1<<2)
#define CD_NTRCART (1<<3)

#define MAX_ENTRIES 1024
#define CIA_CERT_SIZE 0xA00
Expand Down
150 changes: 150 additions & 0 deletions source/gamecart/card_ntr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*---------------------------------------------------------------------------------
Copyright (C) 2005
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/card.h"
#include "nds/dma.h"
#include "nds/memory.h"
#include "nds/bios.h"


//---------------------------------------------------------------------------------
void cardWriteCommand(const u8 *command) {
//---------------------------------------------------------------------------------
int index;

REG_AUXSPICNTH = CARD_CR1_ENABLE | CARD_CR1_IRQ;

for (index = 0; index < 8; index++) {
CARD_COMMAND[7-index] = command[index];
}
}


//---------------------------------------------------------------------------------
void cardPolledTransfer(u32 flags, u32 *destination, u32 length, const u8 *command) {
//---------------------------------------------------------------------------------
u32 data;
cardWriteCommand(command);
REG_ROMCTRL = flags;
u32 * target = destination + length;
do {
// Read data if available
if (REG_ROMCTRL & CARD_DATA_READY) {
data=CARD_DATA_RD;
if (destination < target)
*destination = data;
destination++;
}
} while (REG_ROMCTRL & CARD_BUSY);
}


//---------------------------------------------------------------------------------
void cardStartTransfer(const u8 *command, u32 *destination, int channel, u32 flags) {
//---------------------------------------------------------------------------------
cardWriteCommand(command);

// Set up a DMA channel to transfer a word every time the card makes one
DMA_SRC(channel) = (u32)&CARD_DATA_RD;
DMA_DEST(channel) = (u32)destination;
DMA_CR(channel) = DMA_ENABLE | DMA_START_CARD | DMA_32_BIT | DMA_REPEAT | DMA_SRC_FIX | 0x0001;

REG_ROMCTRL = flags;
}


//---------------------------------------------------------------------------------
u32 cardWriteAndRead(const u8 *command, u32 flags) {
//---------------------------------------------------------------------------------
cardWriteCommand(command);
REG_ROMCTRL = flags | CARD_ACTIVATE | CARD_nRESET | CARD_BLK_SIZE(7);
while (!(REG_ROMCTRL & CARD_DATA_READY)) ;
return CARD_DATA_RD;
}

//---------------------------------------------------------------------------------
void cardParamCommand (u8 command, u32 parameter, u32 flags, u32 *destination, u32 length) {
//---------------------------------------------------------------------------------
u8 cmdData[8];

cmdData[7] = (u8) command;
cmdData[6] = (u8) (parameter >> 24);
cmdData[5] = (u8) (parameter >> 16);
cmdData[4] = (u8) (parameter >> 8);
cmdData[3] = (u8) (parameter >> 0);
cmdData[2] = 0;
cmdData[1] = 0;
cmdData[0] = 0;

cardPolledTransfer(flags, destination, length, cmdData);
}

//---------------------------------------------------------------------------------
void cardReadHeader(u8 *header) {
//---------------------------------------------------------------------------------
REG_ROMCTRL=0;
REG_AUXSPICNTH=0;
swiDelay(167550);
REG_AUXSPICNTH=CARD_CR1_ENABLE|CARD_CR1_IRQ;
REG_ROMCTRL=CARD_nRESET|CARD_SEC_SEED;
while(REG_ROMCTRL&CARD_BUSY) ;
cardReset();
while(REG_ROMCTRL&CARD_BUSY) ;

cardParamCommand(CARD_CMD_HEADER_READ,0,CARD_ACTIVATE|CARD_nRESET|CARD_CLK_SLOW|CARD_BLK_SIZE(1)|CARD_DELAY1(0x1FFF)|CARD_DELAY2(0x3F),(u32*)header,512/4);
}


//---------------------------------------------------------------------------------
u32 cardReadID(u32 flags) {
//---------------------------------------------------------------------------------
const u8 command[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, CARD_CMD_HEADER_CHIPID};
return cardWriteAndRead(command, flags);
}


//---------------------------------------------------------------------------------
void cardReset() {
//---------------------------------------------------------------------------------
const u8 cmdData[8]={0,0,0,0,0,0,0,CARD_CMD_DUMMY};
cardWriteCommand(cmdData);
REG_ROMCTRL=CARD_ACTIVATE|CARD_nRESET|CARD_CLK_SLOW|CARD_BLK_SIZE(5)|CARD_DELAY2(0x18);
u32 read=0;

do {
if(REG_ROMCTRL&CARD_DATA_READY) {
if(read<0x2000) {
u32 data=CARD_DATA_RD;
(void)data;
read+=4;
}
}
} while(REG_ROMCTRL&CARD_BUSY);
}




11 changes: 11 additions & 0 deletions source/gamecart/card_ntr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#pragma once

void cardWriteCommand(const u8 *command);
void cardPolledTransfer(u32 flags, u32 *destination, u32 length, const u8 *command);
void cardStartTransfer(const u8 *command, u32 *destination, int channel, u32 flags);
u32 cardWriteAndRead(const u8 *command, u32 flags);
void cardParamCommand (u8 command, u32 parameter, u32 flags, u32 *destination, u32 length);
void cardReadHeader(u8 *header);
u32 cardReadID(u32 flags);
void cardReset();
30 changes: 23 additions & 7 deletions source/gamecart/command_ntr.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
// Copyright 2014 Normmatt
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
//
// modifyed by osilloscopion (2 Jul 2016)
//

#include "command_ntr.h"

#include "protocol_ntr.h"
#include "card_ntr.h"
#include "delay.h"


u32 ReadDataFlags = 0;

void NTR_CmdReset(void)
{
static const u32 reset_cmd[2] = { 0x9F000000, 0x00000000 };
NTR_SendCommand(reset_cmd, 0x2000, NTRCARD_CLK_SLOW | NTRCARD_DELAY1(0x1FFF) | NTRCARD_DELAY2(0x18), NULL);
cardReset ();
ioDelay(0xF000);
}

u32 NTR_CmdGetCartId(void)
{
u32 id;
static const u32 getid_cmd[2] = { 0x90000000, 0x00000000 };
NTR_SendCommand(getid_cmd, 0x4, NTRCARD_CLK_SLOW | NTRCARD_DELAY1(0x1FFF) | NTRCARD_DELAY2(0x18), &id);
return id;
return cardReadID (0);
}

void NTR_CmdEnter16ByteMode(void)
{
static const u32 enter16bytemode_cmd[2] = { 0x3E000000, 0x00000000 };
NTR_SendCommand(enter16bytemode_cmd, 0x0, 0, NULL);
}

void NTR_CmdReadHeader (void* buffer)
{
cardReadHeader (buffer);
}

void NTR_CmdReadData (u32 offset, void* buffer)
{
cardParamCommand (NTRCARD_CMD_DATA_READ, offset, ReadDataFlags | NTRCARD_ACTIVATE | NTRCARD_nRESET | NTRCARD_BLK_SIZE(1), (u32*)buffer, 0x200 / 4);
}


Loading

0 comments on commit 34e96d0

Please sign in to comment.