Cleanup: combine several .h files into one

This commit is contained in:
d0k3 2017-11-03 16:11:29 +01:00
parent 0f3fc2a5d1
commit 643b58533e
18 changed files with 73 additions and 68 deletions

View File

@ -58,11 +58,14 @@ u64 IdentifyFileType(const char* path) {
u64 type = GAME_NCCH; u64 type = GAME_NCCH;
if (NCCH_IS_CXI(ncch)) { if (NCCH_IS_CXI(ncch)) {
type |= FLAG_CXI; type |= FLAG_CXI;
/* the below is unused for now
type |= NCCH_IS_FIRM(ncch) ? FLAG_FIRM : 0;
NcchExtHeader exhdr; NcchExtHeader exhdr;
if ((FileGetData(path, &exhdr, 0x400, 0x200) == 0x400) && // read only what we need if ((FileGetData(path, &exhdr, 0x400, 0x200) == 0x400) && // read only what we need
(DecryptNcch(&exhdr, 0x200, 0x400, ncch, NULL) == 0) && (DecryptNcch(&exhdr, 0x200, 0x400, ncch, NULL) == 0) &&
NCCH_IS_GBAVC(&exhdr)) NCCH_IS_GBAVC(&exhdr))
type |= FLAG_GBAVC; type |= FLAG_GBAVC;
*/
} }
if (fsize >= (ncch->size * NCCH_MEDIA_UNIT)) if (fsize >= (ncch->size * NCCH_MEDIA_UNIT))
return type; // NCCH (".APP") file return type; // NCCH (".APP") file

View File

@ -30,7 +30,8 @@
#define HDR_NAND (1ULL<<25) #define HDR_NAND (1ULL<<25)
#define TYPE_BASE 0xFFFFFFFFULL // 32 bit reserved for base types #define TYPE_BASE 0xFFFFFFFFULL // 32 bit reserved for base types
#define FLAG_GBAVC (1ULL<<59) // #define FLAG_FIRM (1ULL<<58) // <--- for CXIs containing FIRMs
// #define FLAG_GBAVC (1ULL<<59) // <--- for GBAVC CXIs
#define FLAG_ENC (1ULL<<60) #define FLAG_ENC (1ULL<<60)
#define FLAG_CTR (1ULL<<61) #define FLAG_CTR (1ULL<<61)
#define FLAG_NUSCDN (1ULL<<62) #define FLAG_NUSCDN (1ULL<<62)

11
source/filesys/fs.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "filetype.h"
#include "fsdir.h"
#include "fsdrive.h"
#include "fsgame.h"
#include "fsinit.h"
#include "fsperm.h"
#include "fsutil.h"
#include "image.h"
#include "vff.h"

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "common.h" #include "common.h"
#include "gba.h"
#define GBAVC_MAGIC '.', 'C', 'A', 'A' #define GBAVC_MAGIC '.', 'C', 'A', 'A'
#define AGBSAVE_MAGIC '.', 'S', 'A', 'V' #define AGBSAVE_MAGIC '.', 'S', 'A', 'V'

View File

@ -10,6 +10,7 @@
#define NCCH_ENCRYPTED(ncch) (!((ncch)->flags[7] & 0x04)) #define NCCH_ENCRYPTED(ncch) (!((ncch)->flags[7] & 0x04))
#define NCCH_IS_CXI(ncch) ((ncch)->flags[5] & 0x02) #define NCCH_IS_CXI(ncch) ((ncch)->flags[5] & 0x02)
#define NCCH_IS_FIRM(ncch) (((ncch)->programId >> 32) == 0x00040138)
#define NCCH_IS_GBAVC(exhdr) (getle32((exhdr)->aci_data + 8) == 0x00000202) #define NCCH_IS_GBAVC(exhdr) (getle32((exhdr)->aci_data + 8) == 0x00000202)
#define NCCH_NOCRYPTO 0x0004 #define NCCH_NOCRYPTO 0x0004

View File

@ -1,33 +1,19 @@
#include "godmode.h" #include "godmode.h"
#include "ui.h" #include "ui.h"
#include "hid.h" #include "hid.h"
#include "fsinit.h" #include "fs.h"
#include "fsdrive.h" #include "utils.h"
#include "fsutil.h"
#include "fsperm.h"
#include "fsgame.h"
#include "scripting.h"
#include "gameutil.h"
#include "keydbutil.h"
#include "nandutil.h"
#include "filetype.h"
#include "unittype.h"
#include "entrypoints.h"
#include "nand.h" #include "nand.h"
#include "virtual.h" #include "virtual.h"
#include "vcart.h" #include "vcart.h"
#include "game.h" #include "game.h"
#include "nandcmac.h" #include "unittype.h"
#include "ctrtransfer.h" #include "entrypoints.h"
#include "ncchinfo.h"
#include "sysinfo.h"
#include "image.h"
#include "bootfirm.h" #include "bootfirm.h"
#include "qlzcomp.h" #include "qlzcomp.h"
#include "timer.h" #include "timer.h"
#include "rtc.h" #include "rtc.h"
#include "power.h" #include "power.h"
#include "vff.h"
#include "vram0.h" #include "vram0.h"

View File

@ -1,10 +1,6 @@
#include "ctrtransfer.h" #include "ctrtransfer.h"
#include "nandcmac.h" #include "nandcmac.h"
#include "fsutil.h" #include "fs.h"
#include "fsinit.h"
#include "fsperm.h"
#include "image.h"
#include "gameutil.h"
#include "essentials.h" #include "essentials.h"
#include "ui.h" #include "ui.h"

View File

@ -2,12 +2,10 @@
#include "game.h" #include "game.h"
#include "hid.h" #include "hid.h"
#include "ui.h" #include "ui.h"
#include "fsperm.h" #include "fs.h"
#include "filetype.h"
#include "unittype.h" #include "unittype.h"
#include "aes.h" #include "aes.h"
#include "sha.h" #include "sha.h"
#include "vff.h"
// use NCCH crypto defines for everything // use NCCH crypto defines for everything
#define CRYPTO_DECRYPT NCCH_NOCRYPTO #define CRYPTO_DECRYPT NCCH_NOCRYPTO

View File

@ -1,9 +1,7 @@
#include "keydbutil.h" #include "keydbutil.h"
#include "fsperm.h" #include "fs.h"
#include "filetype.h"
#include "unittype.h"
#include "vff.h"
#include "ui.h" #include "ui.h"
#include "unittype.h"
#define MAX_KEYDB_SIZE (TEMP_BUFFER_SIZE) #define MAX_KEYDB_SIZE (TEMP_BUFFER_SIZE)

View File

@ -1,20 +1,16 @@
#include "nandutil.h" #include "nandutil.h"
#include "gameutil.h" #include "nandcmac.h"
#include "nand.h" #include "nand.h"
#include "firm.h" #include "firm.h"
#include "fatmbr.h" #include "fatmbr.h"
#include "essentials.h" // for essential backup struct
#include "nandcmac.h"
#include "gba.h" #include "gba.h"
#include "image.h" #include "fs.h"
#include "fsinit.h" #include "ui.h"
#include "fsperm.h"
#include "sighax.h"
#include "unittype.h"
#include "sdmmc.h" #include "sdmmc.h"
#include "sha.h" #include "sha.h"
#include "ui.h" #include "sighax.h"
#include "vff.h" #include "essentials.h" // for essential backup struct
#include "unittype.h"
static const u8 twl_mbr_std[0x42] = { static const u8 twl_mbr_std[0x42] = {

View File

@ -1,24 +1,17 @@
#include "scripting.h" #include "scripting.h"
#include "fsutil.h" #include "fs.h"
#include "fsinit.h" #include "utils.h"
#include "fsperm.h"
#include "nand.h" #include "nand.h"
#include "nandcmac.h"
#include "nandutil.h"
#include "gameutil.h"
#include "keydbutil.h"
#include "filetype.h"
#include "bootfirm.h" #include "bootfirm.h"
#include "qrcodegen.h" #include "qrcodegen.h"
#include "firm.h" #include "firm.h"
#include "power.h" #include "power.h"
#include "vff.h"
#include "unittype.h" #include "unittype.h"
#include "region.h"
#include "rtc.h" #include "rtc.h"
#include "sha.h" #include "sha.h"
#include "hid.h" #include "hid.h"
#include "ui.h" #include "ui.h"
#include "region.h"
#define _MAX_ARGS 3 #define _MAX_ARGS 3
#define _ARG_MAX_LEN 512 #define _ARG_MAX_LEN 512

View File

@ -3,8 +3,8 @@
#include "itcm.h" #include "itcm.h"
#include "region.h" #include "region.h"
#include "unittype.h" #include "unittype.h"
#include "essentials.h" // for SecureInfo
#include "vff.h" #include "vff.h"
#include "nand/essentials.h" // For SecureInfo
#include <ctype.h> #include <ctype.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>

9
source/utils/utils.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "ctrtransfer.h"
#include "gameutil.h"
#include "keydbutil.h"
#include "nandcmac.h"
#include "nandutil.h"
#include "scripting.h"
#include "sysinfo.h"

View File

@ -104,24 +104,21 @@ static RomFsLv3Index lv3idx;
static u8 cia_titlekey[16]; static u8 cia_titlekey[16];
int ReadCiaContentImageBlocks(void* buffer, u64 block, u64 count, u32 cia_cnt_idx, u64 block0) { int ReadCbcImageBlocks(void* buffer, u64 block, u64 count, u8* iv0, u64 block0) {
int ret = ReadImageBytes(buffer, block * AES_BLOCK_SIZE, count * AES_BLOCK_SIZE); int ret = ReadImageBytes(buffer, block * AES_BLOCK_SIZE, count * AES_BLOCK_SIZE);
if ((ret == 0) && (cia_cnt_idx <= 0xFFFF)) { if ((ret == 0) && iv0) {
u8 ctr[AES_BLOCK_SIZE] = { 0 }; u8 ctr[AES_BLOCK_SIZE] = { 0 };
if (block == block0) { if (block == block0) memcpy(ctr, iv0, AES_BLOCK_SIZE);
ctr[0] = (cia_cnt_idx >> 8) & 0xFF; else if ((ret = ReadImageBytes(ctr, (block-1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE)) != 0)
ctr[1] = (cia_cnt_idx >> 0) & 0xFF;
} else {
if ((ret = ReadImageBytes(ctr, (block-1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE)) != 0)
return ret; return ret;
}
if (DecryptCiaContentSequential(buffer, count * AES_BLOCK_SIZE, ctr, cia_titlekey) != 0) u32 mode = AES_CNT_TITLEKEY_DECRYPT_MODE;
return -1; cbc_decrypt(buffer, buffer, count, mode, ctr);
} }
return ret; return ret;
} }
int ReadCiaContentImageBytes(void* buffer, u64 offset, u64 count, u32 cia_cnt_idx, u64 offset0) { int ReadCbcImageBytes(void* buffer, u64 offset, u64 count, u8* iv0, u64 offset0) {
u32 off_fix = offset % AES_BLOCK_SIZE; u32 off_fix = offset % AES_BLOCK_SIZE;
u64 block0 = offset0 / AES_BLOCK_SIZE; u64 block0 = offset0 / AES_BLOCK_SIZE;
u8 __attribute__((aligned(32))) temp[AES_BLOCK_SIZE]; u8 __attribute__((aligned(32))) temp[AES_BLOCK_SIZE];
@ -130,7 +127,7 @@ int ReadCiaContentImageBytes(void* buffer, u64 offset, u64 count, u32 cia_cnt_id
if (off_fix) { // misaligned offset (at beginning) if (off_fix) { // misaligned offset (at beginning)
u32 fix_byte = ((off_fix + count) >= AES_BLOCK_SIZE) ? AES_BLOCK_SIZE - off_fix : count; u32 fix_byte = ((off_fix + count) >= AES_BLOCK_SIZE) ? AES_BLOCK_SIZE - off_fix : count;
if ((ret = ReadCiaContentImageBlocks(temp, offset / AES_BLOCK_SIZE, 1, cia_cnt_idx, block0)) != 0) if ((ret = ReadCbcImageBlocks(temp, offset / AES_BLOCK_SIZE, 1, iv0, block0)) != 0)
return ret; return ret;
memcpy(buffer8, temp + off_fix, fix_byte); memcpy(buffer8, temp + off_fix, fix_byte);
buffer8 += fix_byte; buffer8 += fix_byte;
@ -140,7 +137,7 @@ int ReadCiaContentImageBytes(void* buffer, u64 offset, u64 count, u32 cia_cnt_id
if (count >= AES_BLOCK_SIZE) { if (count >= AES_BLOCK_SIZE) {
u64 blocks = count / AES_BLOCK_SIZE; u64 blocks = count / AES_BLOCK_SIZE;
if ((ret = ReadCiaContentImageBlocks(buffer8, offset / AES_BLOCK_SIZE, blocks, cia_cnt_idx, block0)) != 0) if ((ret = ReadCbcImageBlocks(buffer8, offset / AES_BLOCK_SIZE, blocks, iv0, block0)) != 0)
return ret; return ret;
buffer8 += AES_BLOCK_SIZE * blocks; buffer8 += AES_BLOCK_SIZE * blocks;
offset += AES_BLOCK_SIZE * blocks; offset += AES_BLOCK_SIZE * blocks;
@ -148,7 +145,7 @@ int ReadCiaContentImageBytes(void* buffer, u64 offset, u64 count, u32 cia_cnt_id
} }
if (count) { // misaligned offset (at end) if (count) { // misaligned offset (at end)
if ((ret = ReadCiaContentImageBlocks(temp, offset / AES_BLOCK_SIZE, 1, cia_cnt_idx, block0)) != 0) if ((ret = ReadCbcImageBlocks(temp, offset / AES_BLOCK_SIZE, 1, iv0, block0)) != 0)
return ret; return ret;
memcpy(buffer8, temp, count); memcpy(buffer8, temp, count);
count = 0; count = 0;
@ -157,6 +154,23 @@ int ReadCiaContentImageBytes(void* buffer, u64 offset, u64 count, u32 cia_cnt_id
return ret; return ret;
} }
int ReadCiaContentImageBytes(void* buffer, u64 offset, u64 count, u32 cia_cnt_idx, u64 offset0) {
// setup key for CIA
u8 tik[16] __attribute__((aligned(32)));
memcpy(tik, cia_titlekey, 16);
setup_aeskey(0x11, tik);
use_aeskey(0x11);
// setup IV0
u8 iv0[AES_BLOCK_SIZE] = { 0 };
iv0[0] = (cia_cnt_idx >> 8) & 0xFF;
iv0[1] = (cia_cnt_idx >> 0) & 0xFF;
// continue in next function
u8* iv0_ptr = (cia_cnt_idx <= 0xFFFF) ? iv0 : NULL;
return ReadCbcImageBytes(buffer, offset, count, iv0_ptr, offset0);
}
int ReadGameImageBytes(void* buffer, u64 offset, u64 count) { int ReadGameImageBytes(void* buffer, u64 offset, u64 count) {
int ret = ((offset_ccnt != (u64) -1) && (index_ccnt <= 0xFFFF)) ? int ret = ((offset_ccnt != (u64) -1) && (index_ccnt <= 0xFFFF)) ?
ReadCiaContentImageBytes(buffer, offset, count, index_ccnt, offset_ccnt) : ReadCiaContentImageBytes(buffer, offset, count, index_ccnt, offset_ccnt) :