Load bios9i file if DS BIOS is set in SCFG when running from flashcard

This commit is contained in:
RocketRobz 2022-11-14 23:40:40 -07:00
parent 816dbb98db
commit dfcbab2c81
13 changed files with 315 additions and 20 deletions

View File

@ -137,7 +137,7 @@ int main() {
setPowerButtonCB(powerButtonCB);
// Check for 3DS
if(isDSiMode() || REG_SCFG_EXT != 0) {
if(isDSiMode() || (REG_SCFG_EXT & BIT(22))) {
u8 byteBak = my_i2cReadRegister(0x4A, 0x71);
my_i2cWriteRegister(0x4A, 0x71, 0xD2);
fifoSendValue32(FIFO_USER_05, my_i2cReadRegister(0x4A, 0x71));
@ -154,8 +154,8 @@ int main() {
u8 base[16]={0};
u8 in[16]={0};
u8 iv[16]={0};
u8 *scratch=(u8*)0x02300200;
u8 *out=(u8*)0x02300000;
u8 *scratch=(u8*)0x02F00200;
u8 *out=(u8*)0x02F00000;
u8 *key3=(u8*)0x40044D0;
aes(in, base, iv, 2);

View File

@ -4,7 +4,7 @@
obtain one at https://mozilla.org/MPL/2.0/.
--------------------------------------------------------------------------------*/
MEMORY {
ewram : ORIGIN = 0x02004000, LENGTH = 3M + 512K - 0x4000
ewram : ORIGIN = 0x0200C000, LENGTH = 3M + 512K - 0xC000
dtcm : ORIGIN = 0x0b000000, LENGTH = 16K
vectors : ORIGIN = 0x01000000, LENGTH = 256
itcm : ORIGIN = 0x01000100, LENGTH = 32K - 256

View File

@ -117,7 +117,7 @@ static void dsi_aes_set_key(uint32_t *rk, const uint32_t *console_id, key_mode_t
int dsi_sha1_verify(const void *digest_verify, const void *data, unsigned len) {
uint8_t digest[SHA1_LEN];
swiSHA1Calc(digest, data, len);
my_swiSHA1Calc(digest, data, len);
// return type of swiSHA1Verify() is declared void, so how exactly should we use it?
int ret = memcmp(digest, digest_verify, SHA1_LEN);
if (ret != 0) {
@ -153,7 +153,7 @@ void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int i
aes_set_key_enc_128_be(boot2_rk, (uint8_t*)DSi_BOOT2_KEY);
uint32_t digest[SHA1_LEN / sizeof(uint32_t)];
swiSHA1Calc(digest, emmc_cid, 16);
my_swiSHA1Calc(digest, emmc_cid, 16);
nand_ctr_iv[0] = digest[0];
nand_ctr_iv[1] = digest[1];
nand_ctr_iv[2] = digest[2];

View File

@ -18,7 +18,7 @@ typedef enum {
} key_mode_t;
// don't want to include nds.h just for this
void swiSHA1Calc(void *digest, const void *buf, size_t len);
void my_swiSHA1Calc(void *digest, const void *buf, size_t len);
int dsi_sha1_verify(const void *digest_verify, const void *data, unsigned len);

View File

@ -5,6 +5,7 @@
#include <dirent.h>
#include <vector>
#include "my_sha1.h"
#include "file_browse.h"
#include "font.h"
#include "ndsheaderbanner.h"
@ -14,7 +15,7 @@
#define copyBufSize 0x8000
#define shaChunkSize 0x10000
u32 copyBuf[copyBufSize];
u32* copyBuf = (u32*)0x02004000;
std::vector<ClipboardFile> clipboard;
bool clipboardOn = false;
@ -89,7 +90,7 @@ bool calculateSHA1(const char *fileName, u8 *sha1) {
memset(sha1, 0, 20);
swiSHA1context_t ctx;
ctx.sha_block=0; //this is weird but it has to be done
swiSHA1Init(&ctx);
my_swiSHA1Init(&ctx);
font->clear(false);
font->printf(firstCol, 0, false, alignStart, Palette::white, STR_CALCULATING_SHA1.c_str(), fileName);
@ -104,7 +105,7 @@ bool calculateSHA1(const char *fileName, u8 *sha1) {
while (true) {
size_t ret = fread(buf, 1, shaChunkSize, fp);
if (!ret) break;
swiSHA1Update(&ctx, buf, ret);
my_swiSHA1Update(&ctx, buf, ret);
scanKeys();
int keys = keysHeld();
if (keys & KEY_START) {
@ -120,7 +121,7 @@ bool calculateSHA1(const char *fileName, u8 *sha1) {
font->printf(firstCol, nameHeight + 6, false, alignStart, Palette::white, STR_N_OF_N_BYTES_PROCESSED.c_str(), ftell(fp), fsize);
font->update(false);
}
swiSHA1Final(sha1, &ctx);
my_swiSHA1Final(sha1, &ctx);
free(buf);
fclose(fp);
return true;

View File

@ -99,7 +99,7 @@ bool getDirectoryContents(std::vector<DirEntry>& dirContents) {
if (extension(pent->d_name, {"nds", "argv", "dsi", "ids", "app", "srl"})) {
isApp = (currentDrive == Drive::sdCard && sdMounted) || (currentDrive == Drive::flashcard && flashcardMounted);
} else if (extension(pent->d_name, {"firm"})) {
isApp = (isDSiMode() && is3DS && sdMounted);
isApp = (is3DS && sdMounted);
}
dirContents.emplace_back(pent->d_name, pent->d_type == DT_DIR ? 0 : -1, pent->d_type == DT_DIR, isApp);
@ -213,7 +213,7 @@ FileOperation fileBrowse_A(DirEntry* entry, char path[PATH_MAX]) {
// The bios SHA1 functions are only available on the DSi
// https://problemkaputt.de/gbatek.htm#biossha1functionsdsionly
if (isDSiMode()) {
if (bios9iEnabled) {
operations.push_back(FileOperation::calculateSHA1);
}
}

View File

@ -54,6 +54,7 @@ bool screenSwapped = false;
bool arm7SCFGLocked = false;
bool isRegularDS = true;
bool bios9iEnabled = false;
bool is3DS = false;
int ownNitroFSMounted;
std::string prevTime;
@ -147,6 +148,7 @@ int main(int argc, char **argv) {
fifoSendValue32(FIFO_USER_07, 0);
if (isDSiMode()) {
bios9iEnabled = true;
if (!arm7SCFGLocked) {
//font->print(-2, -4, false, " Held - Disable NAND access", Alignment::right);
font->print(-2, -3, false, " Held - Disable cart access", Alignment::right);
@ -204,7 +206,57 @@ int main(int argc, char **argv) {
if (ram32MB) {
is3DS = fifoGetValue32(FIFO_USER_05) != 0xD2;
}
//nandMount();
FILE* bios = fopen("sd:/_nds/bios9i.bin", "rb");
if (!bios) {
bios = fopen("sd:/_nds/bios9i_part1.bin", "rb");
}
if (bios) {
tonccpy((u32*)0x02008000, (u32*)0x02000000, 0x4000);
extern u32* copyBuf;
copyBuf = new u32[0x8000/4];
fread((u32*)0x02000000, 1, 0x8000, bios);
fclose(bios);
// Relocate addresses
*(u32*)0x020000CC -= 0xFFFF0000;
*(u32*)0x02003264 -= 0xFFFF0000;
*(u32*)0x02003268 -= 0xFFFF0000;
*(u32*)0x0200326C -= 0xFFFF0000;
*(u32*)0x020033E0 -= 0xFFFF0000;
*(u32*)0x020042C0 -= 0xFFFF0000;
*(u32*)0x02004B88 -= 0xFFFF0000;
*(u32*)0x02004B90 -= 0xFFFF0000;
*(u32*)0x02004B9C -= 0xFFFF0000;
*(u32*)0x02004BA0 -= 0xFFFF0000;
*(u32*)0x02004E1C -= 0xFFFF0000;
*(u32*)0x02004F18 -= 0xFFFF0000;
*(u32*)0x020000CC += 0x02000000;
*(u32*)0x02003264 += 0x02000000;
*(u32*)0x02003268 += 0x02000000;
*(u32*)0x0200326C += 0x02000000;
*(u32*)0x020033E0 += 0x02000000;
*(u32*)0x020042C0 += 0x02000000;
*(u32*)0x02004B88 += 0x02000000;
*(u32*)0x02004B90 += 0x02000000;
*(u32*)0x02004B9C += 0x02000000;
*(u32*)0x02004BA0 += 0x02000000;
*(u32*)0x02004E1C += 0x02000000;
*(u32*)0x02004F18 += 0x02000000;
u32* itcmAddr = (u32*)0x01000000;
for (int i = 0; i < 8; i++) {
itcmAddr[i] = 0xEA7FFFFE;
}
setVectorBase(0);
bios9iEnabled = true;
// nandMount(); // Returns corrupt data for some reason
}
} else if (isRegularDS && (io_dldi_data->ioInterface.features & FEATURE_SLOT_NDS)) {
ramdriveMount(false);
}

View File

@ -10,6 +10,7 @@ extern bool screenSwapped;
extern bool arm7SCFGLocked;
extern bool isRegularDS;
extern bool bios9iEnabled;
extern bool is3DS;
extern int ownNitroFSMounted;

View File

@ -0,0 +1,88 @@
/*---------------------------------------------------------------------------------
Copyright (C) 2017
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.
---------------------------------------------------------------------------------*/
.global my_swiRSAInitHeapTWL
.global my_swiRSADecryptRAWTWL
.global my_swiRSADecryptTWL
.global my_swiRSADecryptPGPTWL
.global my_swiSHA1InitTWL
.global my_swiSHA1UpdateTWL
.global my_swiSHA1FinalTWL
.global my_swiSHA1CalcTWL
.global my_swiSHA1VerifyTWL
.arm
@---------------------------------------------------------------------------------
my_swiRSAInitHeapTWL:
@---------------------------------------------------------------------------------
swi 0x200000
bx lr
@---------------------------------------------------------------------------------
my_swiRSADecryptRAWTWL:
@---------------------------------------------------------------------------------
swi 0x210000
bx lr
@---------------------------------------------------------------------------------
my_swiRSADecryptTWL:
@---------------------------------------------------------------------------------
swi 0x220000
bx lr
@---------------------------------------------------------------------------------
my_swiRSADecryptPGPTWL:
@---------------------------------------------------------------------------------
swi 0x230000
bx lr
@---------------------------------------------------------------------------------
my_swiSHA1InitTWL:
@---------------------------------------------------------------------------------
swi 0x240000
bx lr
@---------------------------------------------------------------------------------
my_swiSHA1UpdateTWL:
@---------------------------------------------------------------------------------
swi 0x250000
bx lr
@---------------------------------------------------------------------------------
my_swiSHA1FinalTWL:
@---------------------------------------------------------------------------------
swi 0x260000
bx lr
@---------------------------------------------------------------------------------
my_swiSHA1CalcTWL:
@---------------------------------------------------------------------------------
swi 0x270000
bx lr
@---------------------------------------------------------------------------------
my_swiSHA1VerifyTWL:
@---------------------------------------------------------------------------------
swi 0x280000
bx lr

65
arm9/source/my_sha1.c Normal file
View File

@ -0,0 +1,65 @@
/*---------------------------------------------------------------------------------
Copyright (C) 2017
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/sha1.h>
#include <nds/system.h>
extern bool bios9iEnabled;
void my_swiSHA1InitTWL(swiSHA1context_t *ctx);
void my_swiSHA1UpdateTWL(swiSHA1context_t *ctx, const void *data, size_t len);
void my_swiSHA1FinalTWL(void *digest, swiSHA1context_t *ctx);
void my_swiSHA1CalcTWL(void *digest, const void *data, size_t len);
void my_swiSHA1VerifyTWL(const void *digest1, const void *digest2);
//---------------------------------------------------------------------------------
void my_swiSHA1Init(swiSHA1context_t *ctx) {
//---------------------------------------------------------------------------------
if (bios9iEnabled) my_swiSHA1InitTWL(ctx);
}
//---------------------------------------------------------------------------------
void my_swiSHA1Update(swiSHA1context_t *ctx, const void *data, size_t len) {
//---------------------------------------------------------------------------------
if (bios9iEnabled) my_swiSHA1UpdateTWL(ctx, data, len);
}
//---------------------------------------------------------------------------------
void my_swiSHA1Final(void *digest, swiSHA1context_t *ctx) {
//---------------------------------------------------------------------------------
if (bios9iEnabled) my_swiSHA1FinalTWL(digest, ctx);
}
//---------------------------------------------------------------------------------
void my_swiSHA1Calc(void *digest, const void *data, size_t len) {
//---------------------------------------------------------------------------------
if (bios9iEnabled) my_swiSHA1CalcTWL(digest, data, len);
}
//---------------------------------------------------------------------------------
void my_swiSHA1Verify(const void *digest1, const void *digest2) {
//---------------------------------------------------------------------------------
if (bios9iEnabled) my_swiSHA1VerifyTWL(digest1, digest2);
}

87
arm9/source/my_sha1.h Normal file
View File

@ -0,0 +1,87 @@
/*---------------------------------------------------------------------------------
DSi sha1 functions
Copyright (C) 2017
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.
---------------------------------------------------------------------------------*/
/*! \file sha1.h
\brief DSi SHA1 functions.
*/
#ifndef MY_SHA1_H_INCLUDE
#define MY_SHA1_H_INCLUDE
#ifdef __cplusplus
extern "C" {
#endif
#include <nds/ndstypes.h>
#include <nds/sha1.h>
#include <stddef.h>
/**
* \brief SHA-1 context setup
*
* \param ctx context to be initialized
*/
void my_swiSHA1Init(swiSHA1context_t *ctx);
/**
* \brief SHA-1 process buffer
*
* \param ctx SHA-1 context
* \param data buffer to process
* \param len length of data
*/
void my_swiSHA1Update(swiSHA1context_t *ctx, const void *data, size_t len);
/**
* \brief SHA-1 final digest
*
* \param digest buffer to hold SHA-1 checksum result
* \param ctx SHA-1 context
*/
void my_swiSHA1Final(void *digest, swiSHA1context_t *ctx);
/**
* \brief SHA-1 checksum
*
* \param digest buffer to hold SHA-1 checksum result
* \param data buffer to process
* \param len length of data
*/
void my_swiSHA1Calc(void *digest, const void *data, size_t len);
/**
* \brief SHA-1 verify
*
* \param digest1 buffer containing hash to verify
* \param digest2 buffer containing hash to verify
*/
void my_swiSHA1Verify(const void *digest1, const void *digest2);
#ifdef __cplusplus
}
#endif
#endif // MY_SHA1_H_INCLUDE

View File

@ -25,7 +25,7 @@ void nandio_set_fat_sig_fix(u32 offset) {
}
void getConsoleID(u8 *consoleID){
u8 *fifo=(u8*)0x02300000; //shared mem address that has our computed key3 stuff
u8 *fifo=(u8*)0x02F00000; //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_xy[16]; //key3_y ^ key3_x
u8 key_x[16];////key3_x - contains a DSi console id (which just happens to be the LFCS on 3ds)

View File

@ -2,6 +2,7 @@
#include <stdio.h>
#include <sys/statvfs.h>
#include <nds.h>
#include "my_sha1.h"
#include "utils.h"
swiSHA1context_t sha1ctx;
@ -63,8 +64,8 @@ int save_file(const char *filename, const void *buffer, size_t size, int save_sh
}
if (save_sha1) {
sha1ctx.sha_block = 0;
swiSHA1Init(&sha1ctx);
swiSHA1Update(&sha1ctx, buffer, size);
my_swiSHA1Init(&sha1ctx);
my_swiSHA1Update(&sha1ctx, buffer, size);
save_sha1_file(filename);
}
return 0;
@ -146,7 +147,7 @@ int save_sha1_file(const char *filename) {
char *sha1_buf = (char *)malloc(len_buf + 1); // extra for \0
char *p = sha1_buf;
char *digest = (char *)malloc(20);
swiSHA1Final(digest, &sha1ctx);
my_swiSHA1Final(digest, &sha1ctx);
for (int i = 0; i < 20; ++i) {
p += siprintf(p, "%02X", digest[i]);
}