From 643b58533ea62a9591e37b40f1d197ed0fd0b64b Mon Sep 17 00:00:00 2001 From: d0k3 Date: Fri, 3 Nov 2017 16:11:29 +0100 Subject: [PATCH] Cleanup: combine several .h files into one --- source/filesys/filetype.c | 3 +++ source/filesys/filetype.h | 3 ++- source/filesys/fs.h | 11 ++++++++ source/{fatfs => filesys}/image.c | 0 source/{fatfs => filesys}/image.h | 0 source/game/gba.h | 1 - source/game/ncch.h | 1 + source/godmode.c | 22 +++------------- source/utils/ctrtransfer.c | 6 +---- source/utils/gameutil.c | 4 +-- source/utils/keydbutil.c | 6 ++--- source/{nand => utils}/nandcmac.c | 0 source/{nand => utils}/nandcmac.h | 0 source/utils/nandutil.c | 16 +++++------ source/utils/scripting.c | 13 +++------ source/utils/sysinfo.c | 2 +- source/utils/utils.h | 9 +++++++ source/virtual/vgame.c | 44 ++++++++++++++++++++----------- 18 files changed, 73 insertions(+), 68 deletions(-) create mode 100644 source/filesys/fs.h rename source/{fatfs => filesys}/image.c (100%) rename source/{fatfs => filesys}/image.h (100%) rename source/{nand => utils}/nandcmac.c (100%) rename source/{nand => utils}/nandcmac.h (100%) create mode 100644 source/utils/utils.h diff --git a/source/filesys/filetype.c b/source/filesys/filetype.c index 3d1a384..217fc8c 100644 --- a/source/filesys/filetype.c +++ b/source/filesys/filetype.c @@ -58,11 +58,14 @@ u64 IdentifyFileType(const char* path) { u64 type = GAME_NCCH; if (NCCH_IS_CXI(ncch)) { type |= FLAG_CXI; + /* the below is unused for now + type |= NCCH_IS_FIRM(ncch) ? FLAG_FIRM : 0; NcchExtHeader exhdr; if ((FileGetData(path, &exhdr, 0x400, 0x200) == 0x400) && // read only what we need (DecryptNcch(&exhdr, 0x200, 0x400, ncch, NULL) == 0) && NCCH_IS_GBAVC(&exhdr)) type |= FLAG_GBAVC; + */ } if (fsize >= (ncch->size * NCCH_MEDIA_UNIT)) return type; // NCCH (".APP") file diff --git a/source/filesys/filetype.h b/source/filesys/filetype.h index 7eaf96e..7e392cf 100644 --- a/source/filesys/filetype.h +++ b/source/filesys/filetype.h @@ -30,7 +30,8 @@ #define HDR_NAND (1ULL<<25) #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_CTR (1ULL<<61) #define FLAG_NUSCDN (1ULL<<62) diff --git a/source/filesys/fs.h b/source/filesys/fs.h new file mode 100644 index 0000000..28800fe --- /dev/null +++ b/source/filesys/fs.h @@ -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" diff --git a/source/fatfs/image.c b/source/filesys/image.c similarity index 100% rename from source/fatfs/image.c rename to source/filesys/image.c diff --git a/source/fatfs/image.h b/source/filesys/image.h similarity index 100% rename from source/fatfs/image.h rename to source/filesys/image.h diff --git a/source/game/gba.h b/source/game/gba.h index d96229b..31d0b5c 100644 --- a/source/game/gba.h +++ b/source/game/gba.h @@ -1,7 +1,6 @@ #pragma once #include "common.h" -#include "gba.h" #define GBAVC_MAGIC '.', 'C', 'A', 'A' #define AGBSAVE_MAGIC '.', 'S', 'A', 'V' diff --git a/source/game/ncch.h b/source/game/ncch.h index 3b0eb71..6fcb06f 100644 --- a/source/game/ncch.h +++ b/source/game/ncch.h @@ -10,6 +10,7 @@ #define NCCH_ENCRYPTED(ncch) (!((ncch)->flags[7] & 0x04)) #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_NOCRYPTO 0x0004 diff --git a/source/godmode.c b/source/godmode.c index 7a4cbb4..a5bde6e 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -1,33 +1,19 @@ #include "godmode.h" #include "ui.h" #include "hid.h" -#include "fsinit.h" -#include "fsdrive.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 "fs.h" +#include "utils.h" #include "nand.h" #include "virtual.h" #include "vcart.h" #include "game.h" -#include "nandcmac.h" -#include "ctrtransfer.h" -#include "ncchinfo.h" -#include "sysinfo.h" -#include "image.h" +#include "unittype.h" +#include "entrypoints.h" #include "bootfirm.h" #include "qlzcomp.h" #include "timer.h" #include "rtc.h" #include "power.h" -#include "vff.h" #include "vram0.h" diff --git a/source/utils/ctrtransfer.c b/source/utils/ctrtransfer.c index 021d76b..1a0d8c0 100644 --- a/source/utils/ctrtransfer.c +++ b/source/utils/ctrtransfer.c @@ -1,10 +1,6 @@ #include "ctrtransfer.h" #include "nandcmac.h" -#include "fsutil.h" -#include "fsinit.h" -#include "fsperm.h" -#include "image.h" -#include "gameutil.h" +#include "fs.h" #include "essentials.h" #include "ui.h" diff --git a/source/utils/gameutil.c b/source/utils/gameutil.c index 63d2faf..4f60117 100644 --- a/source/utils/gameutil.c +++ b/source/utils/gameutil.c @@ -2,12 +2,10 @@ #include "game.h" #include "hid.h" #include "ui.h" -#include "fsperm.h" -#include "filetype.h" +#include "fs.h" #include "unittype.h" #include "aes.h" #include "sha.h" -#include "vff.h" // use NCCH crypto defines for everything #define CRYPTO_DECRYPT NCCH_NOCRYPTO diff --git a/source/utils/keydbutil.c b/source/utils/keydbutil.c index 6d34ee2..baad89c 100644 --- a/source/utils/keydbutil.c +++ b/source/utils/keydbutil.c @@ -1,9 +1,7 @@ #include "keydbutil.h" -#include "fsperm.h" -#include "filetype.h" -#include "unittype.h" -#include "vff.h" +#include "fs.h" #include "ui.h" +#include "unittype.h" #define MAX_KEYDB_SIZE (TEMP_BUFFER_SIZE) diff --git a/source/nand/nandcmac.c b/source/utils/nandcmac.c similarity index 100% rename from source/nand/nandcmac.c rename to source/utils/nandcmac.c diff --git a/source/nand/nandcmac.h b/source/utils/nandcmac.h similarity index 100% rename from source/nand/nandcmac.h rename to source/utils/nandcmac.h diff --git a/source/utils/nandutil.c b/source/utils/nandutil.c index 53c0ca6..1c8c561 100644 --- a/source/utils/nandutil.c +++ b/source/utils/nandutil.c @@ -1,20 +1,16 @@ #include "nandutil.h" -#include "gameutil.h" +#include "nandcmac.h" #include "nand.h" #include "firm.h" #include "fatmbr.h" -#include "essentials.h" // for essential backup struct -#include "nandcmac.h" #include "gba.h" -#include "image.h" -#include "fsinit.h" -#include "fsperm.h" -#include "sighax.h" -#include "unittype.h" +#include "fs.h" +#include "ui.h" #include "sdmmc.h" #include "sha.h" -#include "ui.h" -#include "vff.h" +#include "sighax.h" +#include "essentials.h" // for essential backup struct +#include "unittype.h" static const u8 twl_mbr_std[0x42] = { diff --git a/source/utils/scripting.c b/source/utils/scripting.c index 5f147ce..2a099c2 100644 --- a/source/utils/scripting.c +++ b/source/utils/scripting.c @@ -1,24 +1,17 @@ #include "scripting.h" -#include "fsutil.h" -#include "fsinit.h" -#include "fsperm.h" +#include "fs.h" +#include "utils.h" #include "nand.h" -#include "nandcmac.h" -#include "nandutil.h" -#include "gameutil.h" -#include "keydbutil.h" -#include "filetype.h" #include "bootfirm.h" #include "qrcodegen.h" #include "firm.h" #include "power.h" -#include "vff.h" #include "unittype.h" +#include "region.h" #include "rtc.h" #include "sha.h" #include "hid.h" #include "ui.h" -#include "region.h" #define _MAX_ARGS 3 #define _ARG_MAX_LEN 512 diff --git a/source/utils/sysinfo.c b/source/utils/sysinfo.c index 616620f..fc66f12 100644 --- a/source/utils/sysinfo.c +++ b/source/utils/sysinfo.c @@ -3,8 +3,8 @@ #include "itcm.h" #include "region.h" #include "unittype.h" +#include "essentials.h" // for SecureInfo #include "vff.h" -#include "nand/essentials.h" // For SecureInfo #include #include #include diff --git a/source/utils/utils.h b/source/utils/utils.h new file mode 100644 index 0000000..ccc4125 --- /dev/null +++ b/source/utils/utils.h @@ -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" diff --git a/source/virtual/vgame.c b/source/virtual/vgame.c index 4a7e60e..35051a6 100644 --- a/source/virtual/vgame.c +++ b/source/virtual/vgame.c @@ -104,24 +104,21 @@ static RomFsLv3Index lv3idx; 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); - if ((ret == 0) && (cia_cnt_idx <= 0xFFFF)) { + if ((ret == 0) && iv0) { u8 ctr[AES_BLOCK_SIZE] = { 0 }; - if (block == block0) { - ctr[0] = (cia_cnt_idx >> 8) & 0xFF; - ctr[1] = (cia_cnt_idx >> 0) & 0xFF; - } else { - if ((ret = ReadImageBytes(ctr, (block-1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE)) != 0) - return ret; - } - if (DecryptCiaContentSequential(buffer, count * AES_BLOCK_SIZE, ctr, cia_titlekey) != 0) - return -1; + if (block == block0) memcpy(ctr, iv0, AES_BLOCK_SIZE); + else if ((ret = ReadImageBytes(ctr, (block-1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE)) != 0) + return ret; + + u32 mode = AES_CNT_TITLEKEY_DECRYPT_MODE; + cbc_decrypt(buffer, buffer, count, mode, ctr); } 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; u64 block0 = offset0 / 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) 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; memcpy(buffer8, temp + off_fix, 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) { 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; buffer8 += 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 ((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; memcpy(buffer8, temp, count); count = 0; @@ -157,6 +154,23 @@ int ReadCiaContentImageBytes(void* buffer, u64 offset, u64 count, u32 cia_cnt_id 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 ret = ((offset_ccnt != (u64) -1) && (index_ccnt <= 0xFFFF)) ? ReadCiaContentImageBytes(buffer, offset, count, index_ccnt, offset_ccnt) :