Fix all occurences of NAND_BUFFER & SDCRYPT_BUFFER

This commit is contained in:
d0k3 2018-02-05 23:16:58 +01:00
parent d731a5960f
commit 7927c742c6
4 changed files with 36 additions and 33 deletions

View File

@ -49,15 +49,6 @@
// used in several places // used in several places
#define STD_BUFFER_SIZE 0x100000 // must be a multiple of 0x200 #define STD_BUFFER_SIZE 0x100000 // must be a multiple of 0x200
// buffer area defines (in use by godmode.c)
#define DIR_BUFFER (0x20000000)
#define DIR_BUFFER_SIZE (0x100000)
// buffer area defines (in use by nand.c)
#define NAND_BUFFER ((u8*)0x20100000)
#define NAND_BUFFER_SIZE (0x100000) // must be multiple of 0x200
// buffer area defines (in use by sddata.c)
#define SDCRYPT_BUFFER ((u8*)0x20200000)
#define SDCRYPT_BUFFER_SIZE (0x100000)
// buffer area defines (in use by scripting.c) // buffer area defines (in use by scripting.c)
#define SCRIPT_BUFFER ((u8*)0x20300000) #define SCRIPT_BUFFER ((u8*)0x20300000)
#define SCRIPT_BUFFER_SIZE (0x100000) #define SCRIPT_BUFFER_SIZE (0x100000)

View File

@ -644,7 +644,10 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// setup buffer // setup buffer
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) return false; if (!buffer) {
ShowPrompt(false, "Out of memory.");
return false;
}
// actual move / copy operation // actual move / copy operation
bool same_drv = (strncasecmp(lorig, ldest, 2) == 0); bool same_drv = (strncasecmp(lorig, ldest, 2) == 0);
@ -680,7 +683,10 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// setup buffer // setup buffer
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) return false; if (!buffer) {
ShowPrompt(false, "Out of memory.");
return false;
}
// actual virtual copy operation // actual virtual copy operation
if (force_unmount) DismountDriveType(DriveType(ldest)&(DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE)); if (force_unmount) DismountDriveType(DriveType(ldest)&(DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE));

View File

@ -192,19 +192,25 @@ FRESULT fx_write (FIL* fp, const void* buff, UINT btw, UINT* bw) {
FilCryptInfo* info = fx_find_cryptinfo(fp); FilCryptInfo* info = fx_find_cryptinfo(fp);
FSIZE_t off = f_tell(fp); FSIZE_t off = f_tell(fp);
FRESULT res = FR_OK; FRESULT res = FR_OK;
if (info && info->fptr) { if (info && info->fptr) {
if (memcmp(info->ctr, DSIWARE_MAGIC, 16) == 0) return FR_DENIED; if (memcmp(info->ctr, DSIWARE_MAGIC, 16) == 0) return FR_DENIED;
void* crypt_buff = (void*) malloc(min(btw, STD_BUFFER_SIZE));
if (!crypt_buff) return FR_DENIED;
setup_aeskeyY(0x34, info->keyy); setup_aeskeyY(0x34, info->keyy);
use_aeskey(0x34); use_aeskey(0x34);
*bw = 0; *bw = 0;
for (UINT p = 0; (p < btw) && (res == FR_OK); p += SDCRYPT_BUFFER_SIZE) { for (UINT p = 0; (p < btw) && (res == FR_OK); p += STD_BUFFER_SIZE) {
UINT pcount = min(SDCRYPT_BUFFER_SIZE, (btw - p)); UINT pcount = min(STD_BUFFER_SIZE, (btw - p));
UINT bwl = 0; UINT bwl = 0;
memcpy(SDCRYPT_BUFFER, (u8*) buff + p, pcount); memcpy(crypt_buff, (u8*) buff + p, pcount);
ctr_decrypt_byte(SDCRYPT_BUFFER, SDCRYPT_BUFFER, pcount, off + p, AES_CNT_CTRNAND_MODE, info->ctr); ctr_decrypt_byte(crypt_buff, crypt_buff, pcount, off + p, AES_CNT_CTRNAND_MODE, info->ctr);
res = f_write(fp, (const void*) SDCRYPT_BUFFER, pcount, &bwl); res = f_write(fp, (const void*) crypt_buff, pcount, &bwl);
*bw += bwl; *bw += bwl;
} }
free(crypt_buff);
} else res = f_write(fp, buff, btw, bw); } else res = f_write(fp, buff, btw, bw);
return res; return res;
} }

View File

@ -352,32 +352,32 @@ int ReadNandSectors(void* buffer, u32 sector, u32 count, u32 keyslot, u32 nand_s
int WriteNandSectors(const void* buffer, u32 sector, u32 count, u32 keyslot, u32 nand_dst) int WriteNandSectors(const void* buffer, u32 sector, u32 count, u32 keyslot, u32 nand_dst)
{ {
u8* buffer8 = (u8*) buffer;
// buffer must not be changed, so this is a little complicated // buffer must not be changed, so this is a little complicated
for (u32 s = 0; s < count; s += (NAND_BUFFER_SIZE / 0x200)) { void* nand_buffer = (void*) malloc(min(STD_BUFFER_SIZE, count * 0x200));
u32 pcount = min((NAND_BUFFER_SIZE/0x200), (count - s)); if (!nand_buffer) return -1;
memcpy(NAND_BUFFER, buffer8 + (s*0x200), pcount * 0x200); int errorcode = 0;
if ((keyslot == 0x11) && (sector == SECTOR_SECRET)) CryptSector0x96(NAND_BUFFER, true);
else if (keyslot < 0x40) CryptNand(NAND_BUFFER, sector + s, pcount, keyslot); for (u32 s = 0; s < count; s += (STD_BUFFER_SIZE / 0x200)) {
u32 pcount = min((STD_BUFFER_SIZE/0x200), (count - s));
memcpy(nand_buffer, ((u8*) buffer) + (s*0x200), pcount * 0x200);
if ((keyslot == 0x11) && (sector == SECTOR_SECRET)) CryptSector0x96(nand_buffer, true);
else if (keyslot < 0x40) CryptNand(nand_buffer, sector + s, pcount, keyslot);
if (nand_dst == NAND_EMUNAND) { if (nand_dst == NAND_EMUNAND) {
int errorcode = 0;
if ((sector + s == 0) && (emunand_base_sector % 0x200000 == 0)) { // GW EmuNAND header handling if ((sector + s == 0) && (emunand_base_sector % 0x200000 == 0)) { // GW EmuNAND header handling
errorcode = sdmmc_sdcard_writesectors(emunand_base_sector + getMMCDevice(0)->total_size, 1, NAND_BUFFER); errorcode = sdmmc_sdcard_writesectors(emunand_base_sector + getMMCDevice(0)->total_size, 1, nand_buffer);
errorcode = (!errorcode && (pcount > 1)) ? sdmmc_sdcard_writesectors(emunand_base_sector + 1, pcount - 1, NAND_BUFFER + 0x200) : errorcode; if (!errorcode && (pcount > 1)) errorcode = sdmmc_sdcard_writesectors(emunand_base_sector + 1, pcount - 1, ((u8*) nand_buffer) + 0x200);
} else errorcode = sdmmc_sdcard_writesectors(emunand_base_sector + sector + s, pcount, NAND_BUFFER); } else errorcode = sdmmc_sdcard_writesectors(emunand_base_sector + sector + s, pcount, nand_buffer);
if (errorcode) return errorcode;
} else if (nand_dst == NAND_IMGNAND) { } else if (nand_dst == NAND_IMGNAND) {
int errorcode = WriteImageSectors(NAND_BUFFER, sector + s, pcount); errorcode = WriteImageSectors(nand_buffer, sector + s, pcount);
if (errorcode) return errorcode;
} else if (nand_dst == NAND_SYSNAND) { } else if (nand_dst == NAND_SYSNAND) {
int errorcode = sdmmc_nand_writesectors(sector + s, pcount, NAND_BUFFER); errorcode = sdmmc_nand_writesectors(sector + s, pcount, nand_buffer);
if (errorcode) return errorcode;
} else { } else {
return -1; errorcode = -1;
} }
} }
return 0; free(nand_buffer);
return errorcode;
} }
u32 ValidateSecretSector(u8* sector) u32 ValidateSecretSector(u8* sector)