mirror of
https://github.com/SciresM/hactool
synced 2024-11-10 06:34:14 +00:00
Initial XCI Support. (WIP, #4).
-Not all pertitent information is printed out. -Extraction is pretty much supported, though. -TODO: CERT stuff, decryption, title ID fetching...
This commit is contained in:
parent
910918bbb1
commit
2567d516d5
10 changed files with 415 additions and 10 deletions
7
Makefile
7
Makefile
|
@ -17,7 +17,7 @@ all:
|
|||
.c.o:
|
||||
$(CC) $(INCLUDE) -c $(CFLAGS) -o $@ $<
|
||||
|
||||
hactool: sha.o aes.o rsa.o npdm.o bktr.o pki.o pfs0.o hfs0.o romfs.o utils.o nca.o main.o filepath.o
|
||||
hactool: sha.o aes.o rsa.o npdm.o bktr.o pki.o pfs0.o hfs0.o romfs.o utils.o nca.o xci.o main.o filepath.o
|
||||
$(CC) -o $@ $^ $(LDFLAGS) -L $(LIBDIR)
|
||||
|
||||
aes.o: aes.h types.h
|
||||
|
@ -46,8 +46,13 @@ sha.o: sha.h types.h
|
|||
|
||||
utils.o: utils.h types.h
|
||||
|
||||
xci.o: xci.h types.h hfs0.h
|
||||
|
||||
clean:
|
||||
rm -f *.o hactool hactool.exe
|
||||
|
||||
clean_full:
|
||||
rm -f *.o hactool hactool.exe
|
||||
cd mbedtls && $(MAKE) clean
|
||||
|
||||
dist:
|
||||
|
|
|
@ -51,6 +51,12 @@ HFS0 options:
|
|||
--hfs0dir=dir Specify HFS0 directory path.
|
||||
--outdir=dir Specify HFS0 directory path. Overrides previous path, if present.
|
||||
--exefsdir=dir Specify HFS0 directory path. Overrides previous paths, if present.
|
||||
XCI options:
|
||||
--rootdir=dir Specify XCI root HFS0 directory path.
|
||||
--updatedir=dir Specify XCI update HFS0 directory path.
|
||||
--normaldir=dir Specify XCI update HFS0 directory path.
|
||||
--securedir=dir Specify XCI update HFS0 directory path.
|
||||
--outdir=dir Specify XCI directory path. Overrides previous paths, if present.
|
||||
```
|
||||
|
||||
## Building
|
||||
|
|
3
hfs0.c
3
hfs0.c
|
@ -11,6 +11,7 @@ void hfs0_process(hfs0_ctx_t *ctx) {
|
|||
}
|
||||
|
||||
if (raw_header.magic != MAGIC_HFS0) {
|
||||
memdump(stdout, "Sanity: ", &raw_header, sizeof(raw_header));
|
||||
printf("Error: HFS0 is corrupt!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
@ -34,7 +35,7 @@ void hfs0_process(hfs0_ctx_t *ctx) {
|
|||
uint64_t cur_ofs = 0;
|
||||
for (unsigned int i = 0; i < ctx->header->num_files; i++) {
|
||||
hfs0_file_entry_t *cur_file = hfs0_get_file_entry(ctx->header, i);
|
||||
if (cur_file->offset != cur_ofs) {
|
||||
if (cur_file->offset < cur_ofs) {
|
||||
printf("Error: HFS0 is corrupt!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
|
1
hfs0.h
1
hfs0.h
|
@ -53,5 +53,6 @@ void hfs0_process(hfs0_ctx_t *ctx);
|
|||
void hfs0_save(hfs0_ctx_t *ctx);
|
||||
void hfs0_print(hfs0_ctx_t *ctx);
|
||||
|
||||
void hfs0_save_file(hfs0_ctx_t *ctx, uint32_t i, filepath_t *dirpath);
|
||||
|
||||
#endif
|
39
main.c
39
main.c
|
@ -8,7 +8,7 @@
|
|||
#include "settings.h"
|
||||
#include "pki.h"
|
||||
#include "nca.h"
|
||||
#include "hfs0.h"
|
||||
#include "xci.h"
|
||||
|
||||
static char *prog_name = "hactool";
|
||||
|
||||
|
@ -60,6 +60,12 @@ static void usage(void) {
|
|||
" --hfs0dir=dir Specify HFS0 directory path.\n"
|
||||
" --outdir=dir Specify HFS0 directory path. Overrides previous path, if present.\n"
|
||||
" --exefsdir=dir Specify HFS0 directory path. Overrides previous paths, if present.\n"
|
||||
"XCI options:\n"
|
||||
" --rootdir=dir Specify XCI root HFS0 directory path.\n"
|
||||
" --updatedir=dir Specify XCI update HFS0 directory path.\n"
|
||||
" --normaldir=dir Specify XCI update HFS0 directory path.\n"
|
||||
" --securedir=dir Specify XCI update HFS0 directory path.\n"
|
||||
" --outdir=dir Specify XCI directory path. Overrides previous paths, if present.\n"
|
||||
"\n", __TIME__, __DATE__, prog_name);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
@ -155,6 +161,10 @@ int main(int argc, char **argv) {
|
|||
{"header", 1, NULL, 19},
|
||||
{"pfs0dir", 1, NULL, 20},
|
||||
{"hfs0dir", 1, NULL, 21},
|
||||
{"rootdir", 1, NULL, 22},
|
||||
{"updatedir", 1, NULL, 23},
|
||||
{"normaldir", 1, NULL, 24},
|
||||
{"securedir", 1, NULL, 25},
|
||||
{NULL, 0, NULL, 0},
|
||||
};
|
||||
|
||||
|
@ -188,11 +198,10 @@ int main(int argc, char **argv) {
|
|||
nca_ctx.tool_ctx->file_type = FILETYPE_ROMFS;
|
||||
} else if (!strcmp(optarg, "hfs0")) {
|
||||
nca_ctx.tool_ctx->file_type = FILETYPE_HFS0;
|
||||
} else if (!strcmp(optarg, "xci") || !strcmp(optarg, "gamecard") || !strcmp(optarg, "gc")) {
|
||||
nca_ctx.tool_ctx->file_type = FILETYPE_XCI;
|
||||
}
|
||||
/* } else if (!strcmp(optarg, "xci") || !strcmp(optarg, "gamecard") || !strcmp(optarg, "gc")) {
|
||||
* nca_ctx.tool_ctx->file_type = FILETYPE_XCI;
|
||||
* }
|
||||
* } else if (!strcmp(optarg, "package2") || !strcmp(optarg, "pk21")) {
|
||||
/* } else if (!strcmp(optarg, "package2") || !strcmp(optarg, "pk21")) {
|
||||
* nca_ctx.tool_ctx->file_type = FILETYPE_PACKAGE2;
|
||||
* }
|
||||
* } else if (!strcmp(optarg, "package1") || !strcmp(optarg, "pk11")) {
|
||||
|
@ -282,6 +291,18 @@ int main(int argc, char **argv) {
|
|||
case 21:
|
||||
filepath_set(&tool_ctx.settings.hfs0_dir_path, optarg);
|
||||
break;
|
||||
case 22:
|
||||
filepath_set(&tool_ctx.settings.rootpt_dir_path, optarg);
|
||||
break;
|
||||
case 23:
|
||||
filepath_set(&tool_ctx.settings.update_dir_path, optarg);
|
||||
break;
|
||||
case 24:
|
||||
filepath_set(&tool_ctx.settings.normal_dir_path, optarg);
|
||||
break;
|
||||
case 25:
|
||||
filepath_set(&tool_ctx.settings.secure_dir_path, optarg);
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
|
@ -371,6 +392,14 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
break;
|
||||
}
|
||||
case FILETYPE_XCI: {
|
||||
xci_ctx_t xci_ctx;
|
||||
memset(&xci_ctx, 0, sizeof(xci_ctx));
|
||||
xci_ctx.file = tool_ctx.file;
|
||||
xci_ctx.tool_ctx = &tool_ctx;
|
||||
xci_process(&xci_ctx);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
fprintf(stderr, "Unknown File Type!\n\n");
|
||||
usage();
|
||||
|
|
57
rsa.c
57
rsa.c
|
@ -39,8 +39,7 @@ int rsa2048_pss_verify(const void *data, size_t len, const unsigned char *signat
|
|||
mbedtls_mpi_free(&e_mpi);
|
||||
mbedtls_mpi_free(&message_mpi);
|
||||
|
||||
/* libgcrypt requires knowledge of the salt to do automated PSS verification as far as I can tell. */
|
||||
/* This is not an option in our case... */
|
||||
/* There's no automated PSS verification as far as I can tell. */
|
||||
if (m_buf[RSA_2048_BYTES-1] != 0xBC) {
|
||||
return false;
|
||||
}
|
||||
|
@ -79,4 +78,58 @@ int rsa2048_pss_verify(const void *data, size_t len, const unsigned char *signat
|
|||
memcpy(&validate_buf[0x28], &m_buf[RSA_2048_BYTES - 0x20 - 0x20 - 1], 0x20);
|
||||
sha256_hash_buffer(validate_hash, validate_buf, 0x48);
|
||||
return memcmp(h_buf, validate_hash, 0x20) == 0;
|
||||
}
|
||||
|
||||
/* Perform an RSA-PKCS1 verify operation on data, with signature and N. */
|
||||
int rsa2048_pkcs1_verify(const void *data, size_t len, const unsigned char *signature, const unsigned char *modulus) {
|
||||
mbedtls_mpi signature_mpi;
|
||||
mbedtls_mpi modulus_mpi;
|
||||
mbedtls_mpi e_mpi;
|
||||
mbedtls_mpi message_mpi;
|
||||
|
||||
mbedtls_mpi_init(&signature_mpi);
|
||||
mbedtls_mpi_init(&modulus_mpi);
|
||||
mbedtls_mpi_init(&e_mpi);
|
||||
mbedtls_mpi_init(&message_mpi);
|
||||
mbedtls_mpi_lset(&message_mpi, RSA_2048_BITS);
|
||||
|
||||
unsigned char m_buf[RSA_2048_BYTES];
|
||||
unsigned char h_buf[0x20];
|
||||
const unsigned char E[3] = {1, 0, 1};
|
||||
|
||||
mbedtls_mpi_read_binary(&e_mpi, E, 3);
|
||||
mbedtls_mpi_read_binary(&signature_mpi, signature, RSA_2048_BYTES);
|
||||
mbedtls_mpi_read_binary(&modulus_mpi, modulus, RSA_2048_BYTES);
|
||||
mbedtls_mpi_exp_mod(&message_mpi, &signature_mpi, &e_mpi, &modulus_mpi, NULL);
|
||||
|
||||
if (mbedtls_mpi_write_binary(&message_mpi, m_buf, RSA_2048_BYTES) != 0) {
|
||||
FATAL_ERROR("Failed to export exponentiated RSA message!");
|
||||
}
|
||||
|
||||
mbedtls_mpi_free(&signature_mpi);
|
||||
mbedtls_mpi_free(&modulus_mpi);
|
||||
mbedtls_mpi_free(&e_mpi);
|
||||
mbedtls_mpi_free(&message_mpi);
|
||||
|
||||
/* For RSA-2048, this prefix is just a constant. */
|
||||
const unsigned char pkcs1_hash_prefix[0xE0] = {
|
||||
0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF,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, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
|
||||
};
|
||||
|
||||
sha256_hash_buffer(h_buf, data, len);
|
||||
|
||||
return memcmp(pkcs1_hash_prefix, m_buf, 0xE0) == 0 && memcmp(&m_buf[0xE0], h_buf, 0x20) == 0;
|
||||
}
|
1
rsa.h
1
rsa.h
|
@ -4,5 +4,6 @@
|
|||
#include "mbedtls/rsa.h"
|
||||
|
||||
int rsa2048_pss_verify(const void *data, size_t len, const unsigned char *signature, const unsigned char *modulus);
|
||||
int rsa2048_pkcs1_verify(const void *data, size_t len, const unsigned char *signature, const unsigned char *modulus);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -53,6 +53,10 @@ typedef struct {
|
|||
filepath_t pfs0_dir_path;
|
||||
filepath_t hfs0_dir_path;
|
||||
filepath_t dec_nca_path;
|
||||
filepath_t rootpt_dir_path;
|
||||
filepath_t update_dir_path;
|
||||
filepath_t normal_dir_path;
|
||||
filepath_t secure_dir_path;
|
||||
filepath_t header_path;
|
||||
} hactool_settings_t;
|
||||
|
||||
|
@ -62,7 +66,7 @@ enum hactool_file_type
|
|||
FILETYPE_PFS0,
|
||||
FILETYPE_ROMFS,
|
||||
FILETYPE_HFS0,
|
||||
/* FILETYPE_XCI, */
|
||||
FILETYPE_XCI,
|
||||
/* FILETYPE_PACKAGE2, */
|
||||
/* FILETYPE_PACKAGE1, */
|
||||
};
|
||||
|
|
241
xci.c
Normal file
241
xci.c
Normal file
|
@ -0,0 +1,241 @@
|
|||
#include <string.h>
|
||||
#include "rsa.h"
|
||||
#include "xci.h"
|
||||
|
||||
/* This RSA-PKCS1 public key is only accessible to the gamecard controller. */
|
||||
/* However, it (and other XCI keys) can be dumped with a GCD attack on two signatures. */
|
||||
/* Contact SciresM for details, if curious. */
|
||||
const unsigned char xci_header_pubk[0x100] = {
|
||||
0x98, 0xC7, 0x26, 0xB6, 0x0D, 0x0A, 0x50, 0xA7, 0x39, 0x21, 0x0A, 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B, 0x9A, 0xD7, 0xAF, 0x42, 0x14, 0x52, 0xCD, 0xE3, 0xC5, 0xDD, 0xCE,
|
||||
0x08, 0x12, 0x17, 0xD0, 0x7F, 0x1A, 0xAA, 0x1F, 0x7D, 0xE0, 0x93, 0x54, 0xC8, 0xBC, 0x73, 0x8A,
|
||||
0xCB, 0xAD, 0x6E, 0x93, 0xE2, 0x19, 0x72, 0x6B, 0xD3, 0x45, 0xF8, 0x73, 0x3D, 0x2B, 0x6A, 0x55,
|
||||
0xD2, 0x3A, 0x8B, 0xB0, 0x8A, 0x42, 0xE3, 0x3D, 0xF1, 0x92, 0x23, 0x42, 0x2E, 0xBA, 0xCC, 0x9C,
|
||||
0x9A, 0xC1, 0xDD, 0x62, 0x86, 0x9C, 0x2E, 0xE1, 0x2D, 0x6F, 0x62, 0x67, 0x51, 0x08, 0x0E, 0xCF
|
||||
};
|
||||
|
||||
void xci_process(xci_ctx_t *ctx) {
|
||||
fseeko64(ctx->file, 0, SEEK_SET);
|
||||
if (fread(&ctx->header, 1, 0x200, ctx->file) != 0x200) {
|
||||
fprintf(stderr, "Failed to read XCI header!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->header.magic != MAGIC_HEAD) {
|
||||
fprintf(stderr, "Error: XCI header is corrupt!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (ctx->tool_ctx->action & ACTION_VERIFY) {
|
||||
if (rsa2048_pkcs1_verify(&ctx->header.magic, 0x100, ctx->header.header_sig, xci_header_pubk)) {
|
||||
ctx->header_sig_validity = VALIDITY_VALID;
|
||||
} else {
|
||||
ctx->header_sig_validity = VALIDITY_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
ctx->hfs0_hash_validity = check_memory_hash_table(ctx->file, ctx->header.hfs0_header_hash, ctx->header.hfs0_offset, ctx->header.hfs0_header_size, ctx->header.hfs0_header_size, 0);
|
||||
if (ctx->hfs0_hash_validity != VALIDITY_VALID) {
|
||||
fprintf(stderr, "Error: XCI partition is corrupt!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
hactool_ctx_t blank_ctx;
|
||||
memset(&blank_ctx, 0, sizeof(blank_ctx));
|
||||
blank_ctx.action = ctx->tool_ctx->action & ~(ACTION_EXTRACT | ACTION_INFO);
|
||||
|
||||
ctx->partition_ctx.file = ctx->file;
|
||||
ctx->partition_ctx.offset = ctx->header.hfs0_offset;
|
||||
ctx->partition_ctx.tool_ctx = &blank_ctx;
|
||||
ctx->partition_ctx.name = "rootpt";
|
||||
hfs0_process(&ctx->partition_ctx);
|
||||
|
||||
if (ctx->partition_ctx.header->num_files != 3) {
|
||||
fprintf(stderr, "Error: Invalid XCI partition!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < 3; i++) {
|
||||
hfs0_ctx_t *cur_ctx = NULL;
|
||||
|
||||
hfs0_file_entry_t *cur_file = hfs0_get_file_entry(ctx->partition_ctx.header, i);
|
||||
char *cur_name = hfs0_get_file_name(ctx->partition_ctx.header, i);
|
||||
if (!strcmp(cur_name, "update") && ctx->update_ctx.file == NULL) {
|
||||
cur_ctx = &ctx->update_ctx;
|
||||
} else if (!strcmp(cur_name, "normal") && ctx->normal_ctx.file == NULL) {
|
||||
cur_ctx = &ctx->normal_ctx;
|
||||
} else if (!strcmp(cur_name, "secure") && ctx->secure_ctx.file == NULL) {
|
||||
cur_ctx = &ctx->secure_ctx;
|
||||
}
|
||||
|
||||
if (cur_ctx == NULL) {
|
||||
fprintf(stderr, "Unknown XCI partition: %s\n", cur_name);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
cur_ctx->name = cur_name;
|
||||
cur_ctx->offset = ctx->partition_ctx.offset + hfs0_get_header_size(ctx->partition_ctx.header) + cur_file->offset;
|
||||
cur_ctx->tool_ctx = &blank_ctx;
|
||||
cur_ctx->file = ctx->file;
|
||||
hfs0_process(cur_ctx);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < 0x10; i++) {
|
||||
ctx->iv[i] = ctx->header.reversed_iv[0xF-i];
|
||||
}
|
||||
|
||||
if (ctx->tool_ctx->action & ACTION_INFO) {
|
||||
xci_print(ctx);
|
||||
}
|
||||
|
||||
if (ctx->tool_ctx->action & ACTION_EXTRACT) {
|
||||
xci_save(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
void xci_save(xci_ctx_t *ctx) {
|
||||
/* Extract to directory. */
|
||||
if (ctx->tool_ctx->settings.out_dir_path.enabled && ctx->tool_ctx->settings.out_dir_path.path.valid == VALIDITY_VALID) {
|
||||
printf("Extracting XCI...\n");
|
||||
filepath_t *dirpath = &ctx->tool_ctx->settings.out_dir_path.path;
|
||||
os_makedir(dirpath->os_path);
|
||||
for (unsigned int i = 0; i < 3; i++) {
|
||||
hfs0_ctx_t *cur_ctx = NULL;
|
||||
|
||||
char *cur_name = hfs0_get_file_name(ctx->partition_ctx.header, i);
|
||||
if (!strcmp(cur_name, "update")) {
|
||||
cur_ctx = &ctx->update_ctx;
|
||||
} else if (!strcmp(cur_name, "normal")) {
|
||||
cur_ctx = &ctx->normal_ctx;
|
||||
} else if (!strcmp(cur_name, "secure")) {
|
||||
cur_ctx = &ctx->secure_ctx;
|
||||
}
|
||||
if (cur_ctx == NULL) {
|
||||
fprintf(stderr, "Unkown XCI partition found in extraction: %s\n", cur_name);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
filepath_t partition_dirpath;
|
||||
filepath_copy(&partition_dirpath, dirpath);
|
||||
|
||||
filepath_append(&partition_dirpath, "%s", cur_name);
|
||||
os_makedir(partition_dirpath.os_path);
|
||||
for (uint32_t i = 0; i < cur_ctx->header->num_files; i++) {
|
||||
hfs0_save_file(cur_ctx, i, &partition_dirpath);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* Save Root Partition. */
|
||||
if (ctx->tool_ctx->settings.rootpt_dir_path.valid == VALIDITY_VALID) {
|
||||
printf("Saving Root Partition...\n");
|
||||
os_makedir(ctx->tool_ctx->settings.rootpt_dir_path.os_path);
|
||||
for (uint32_t i = 0; i < ctx->partition_ctx.header->num_files; i++) {
|
||||
hfs0_save_file(&ctx->partition_ctx, i, &ctx->tool_ctx->settings.rootpt_dir_path);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
/* Save Update Partition. */
|
||||
if (ctx->tool_ctx->settings.update_dir_path.valid == VALIDITY_VALID) {
|
||||
printf("Saving Update Partition...\n");
|
||||
os_makedir(ctx->tool_ctx->settings.update_dir_path.os_path);
|
||||
for (uint32_t i = 0; i < ctx->update_ctx.header->num_files; i++) {
|
||||
hfs0_save_file(&ctx->update_ctx, i, &ctx->tool_ctx->settings.update_dir_path);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
/* Save Normal Partition. */
|
||||
if (ctx->tool_ctx->settings.normal_dir_path.valid == VALIDITY_VALID) {
|
||||
printf("Saving Normal Partition...\n");
|
||||
os_makedir(ctx->tool_ctx->settings.normal_dir_path.os_path);
|
||||
for (uint32_t i = 0; i < ctx->normal_ctx.header->num_files; i++) {
|
||||
hfs0_save_file(&ctx->normal_ctx, i, &ctx->tool_ctx->settings.normal_dir_path);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
/* Save Secure Partition. */
|
||||
if (ctx->tool_ctx->settings.secure_dir_path.valid == VALIDITY_VALID) {
|
||||
printf("Saving Secure Partition...\n");
|
||||
os_makedir(ctx->tool_ctx->settings.secure_dir_path.os_path);
|
||||
for (uint32_t i = 0; i < ctx->secure_ctx.header->num_files; i++) {
|
||||
hfs0_save_file(&ctx->secure_ctx, i, &ctx->tool_ctx->settings.secure_dir_path);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char *xci_get_cartridge_type(xci_ctx_t *ctx) {
|
||||
cartridge_type_t cart_type = (cartridge_type_t)ctx->header.cart_type;
|
||||
switch (cart_type) {
|
||||
case CARTSIZE_2GB: return "2GB";
|
||||
case CARTSIZE_4GB: return "4GB";
|
||||
case CARTSIZE_8GB: return "8GB";
|
||||
case CARTSIZE_16GB: return "16GB";
|
||||
default:
|
||||
return "Unknown/Invalid";
|
||||
}
|
||||
}
|
||||
|
||||
void xci_print_hfs0(hfs0_ctx_t *ctx) {
|
||||
print_magic(" Magic: ", ctx->header->magic);
|
||||
printf(" Offset: %012"PRIx64"\n", ctx->offset);
|
||||
printf(" Number of files: %"PRId32"\n", ctx->header->num_files);
|
||||
|
||||
if (ctx->header->num_files > 0 && (ctx->header->num_files < 100 || ctx->tool_ctx->action & ACTION_VERIFY)) {
|
||||
printf(" Files:");
|
||||
for (unsigned int i = 0; i < ctx->header->num_files; i++) {
|
||||
hfs0_file_entry_t *cur_file = hfs0_get_file_entry(ctx->header, i);
|
||||
if (ctx->tool_ctx->action & ACTION_VERIFY) {
|
||||
validity_t hash_validity = check_memory_hash_table(ctx->file, cur_file->hash, ctx->offset + hfs0_get_header_size(ctx->header) + cur_file->offset, cur_file->hashed_size, cur_file->hashed_size, 0);
|
||||
printf("%s%s:/%-48s %012"PRIx64"-%012"PRIx64" (%s)\n", i == 0 ? " " : " ", ctx->name == NULL ? "hfs0" : ctx->name, hfs0_get_file_name(ctx->header, i), cur_file->offset, cur_file->offset + cur_file->size, GET_VALIDITY_STR(hash_validity));
|
||||
} else {
|
||||
printf("%s%s:/%-48s %012"PRIx64"-%012"PRIx64"\n", i == 0 ? " " : " ", ctx->name == NULL ? "hfs0" : ctx->name, hfs0_get_file_name(ctx->header, i), cur_file->offset, cur_file->offset + cur_file->size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void xci_print(xci_ctx_t *ctx) {
|
||||
printf("\nXCI:\n");
|
||||
print_magic("Magic: ", ctx->header.magic);
|
||||
|
||||
if (ctx->tool_ctx->action & ACTION_VERIFY) {
|
||||
if (ctx->header_sig_validity == VALIDITY_VALID) {
|
||||
memdump(stdout, "Header Signature (GOOD): ", &ctx->header.header_sig, 0x100);
|
||||
} else {
|
||||
memdump(stdout, "Header Signature (FAIL): ", &ctx->header.header_sig, 0x100);
|
||||
}
|
||||
} else {
|
||||
memdump(stdout, "Header Signature: ", &ctx->header.header_sig, 0x100);
|
||||
}
|
||||
|
||||
printf("Cartridge Type: %s\n", xci_get_cartridge_type(ctx));
|
||||
printf("Cartridge Size: %012"PRIx64"\n", media_to_real(ctx->header.cart_size + 1));
|
||||
memdump(stdout, "Header IV: ", ctx->iv, 0x10);
|
||||
memdump(stdout, "Encrypted Header: ", ctx->header.encrypted_data, 0x70);
|
||||
|
||||
if (ctx->tool_ctx->action & ACTION_VERIFY) {
|
||||
printf("Root Partition (%s):\n", GET_VALIDITY_STR(ctx->hfs0_hash_validity));
|
||||
} else {
|
||||
printf("Root Partition:\n");
|
||||
}
|
||||
xci_print_hfs0(&ctx->partition_ctx);
|
||||
|
||||
printf("Update Partition:\n");
|
||||
xci_print_hfs0(&ctx->update_ctx);
|
||||
|
||||
printf("Normal Partition:\n");
|
||||
xci_print_hfs0(&ctx->normal_ctx);
|
||||
|
||||
printf("Secure Partition:\n");
|
||||
xci_print_hfs0(&ctx->secure_ctx);
|
||||
}
|
64
xci.h
Normal file
64
xci.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
#ifndef HACTOOL_XCI_H
|
||||
#define HACTOOL_XCI_H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "types.h"
|
||||
#include "settings.h"
|
||||
#include "hfs0.h"
|
||||
|
||||
#define MAGIC_HEAD 0x44414548 /* "HEAD" */
|
||||
|
||||
typedef enum {
|
||||
CARTSIZE_2GB = 0xF8,
|
||||
CARTSIZE_4GB = 0xF0,
|
||||
CARTSIZE_8GB = 0xE0,
|
||||
CARTSIZE_16GB = 0xE1,
|
||||
/* CARTSIZE_32GB = 0xE2? */
|
||||
} cartridge_type_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t header_sig[0x100];
|
||||
uint32_t magic;
|
||||
uint32_t secure_offset;
|
||||
uint32_t _0x108;
|
||||
uint8_t _0x10C;
|
||||
uint8_t cart_type;
|
||||
uint8_t _0x10E;
|
||||
uint8_t _0x10F;
|
||||
uint64_t _0x110;
|
||||
uint64_t cart_size;
|
||||
unsigned char reversed_iv[0x10];
|
||||
uint64_t hfs0_offset;
|
||||
uint64_t hfs0_header_size;
|
||||
unsigned char hfs0_header_hash[0x20];
|
||||
unsigned char crypto_header_hash[0x20];
|
||||
uint32_t _0x180;
|
||||
uint32_t _0x184;
|
||||
uint32_t _0x188;
|
||||
uint32_t _0x18C;
|
||||
unsigned char encrypted_data[0x70];
|
||||
} xci_header_t;
|
||||
|
||||
typedef struct {
|
||||
FILE *file; /* File for this NCA. */
|
||||
validity_t header_sig_validity;
|
||||
validity_t cert_sig_validity;
|
||||
validity_t hfs0_hash_validity;
|
||||
hfs0_ctx_t partition_ctx;
|
||||
hfs0_ctx_t normal_ctx;
|
||||
hfs0_ctx_t update_ctx;
|
||||
hfs0_ctx_t secure_ctx;
|
||||
hactool_ctx_t *tool_ctx;
|
||||
unsigned char iv[0x10];
|
||||
/* TODO: Header decryption. */
|
||||
/* unsigned char decrypted_header[0x70]; */
|
||||
xci_header_t header;
|
||||
} xci_ctx_t;
|
||||
|
||||
void xci_process(xci_ctx_t *ctx);
|
||||
void xci_save(xci_ctx_t *ctx);
|
||||
void xci_print(xci_ctx_t *ctx);
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue