1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-29 21:52:22 +00:00
nxdumptool/source/main.c

396 lines
10 KiB
C
Raw Normal View History

2020-04-16 01:06:41 +01:00
/*
* Copyright (c) 2020 DarkMatterCore
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-04-11 06:28:26 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <switch.h>
2020-04-15 21:50:07 +01:00
//#include "lvgl_helper.h"
#include "utils.h"
2020-04-11 06:28:26 +01:00
2020-04-22 21:53:20 +01:00
#include <dirent.h>
2020-04-24 10:38:13 +01:00
#include "nca.h"
#include "romfs.h"
2020-04-26 09:35:01 +01:00
#include "rsa.h"
2020-04-24 10:38:13 +01:00
2020-04-11 06:28:26 +01:00
int main(int argc, char *argv[])
{
2020-04-15 21:50:07 +01:00
(void)argc;
(void)argv;
2020-04-11 06:28:26 +01:00
int ret = 0;
2020-04-15 21:50:07 +01:00
LOGFILE("nxdumptool starting.");
if (!utilsInitializeResources())
2020-04-11 06:28:26 +01:00
{
ret = -1;
goto out;
}
2020-04-15 21:50:07 +01:00
/*lv_test();
2020-04-11 06:28:26 +01:00
while(appletMainLoop())
{
2020-04-15 21:50:07 +01:00
lv_task_handler();
if (lvglHelperGetExitFlag()) break;
}*/
consoleInit(NULL);
2020-04-24 10:38:13 +01:00
printf("initializing...\n");
2020-04-15 21:50:07 +01:00
consoleUpdate(NULL);
2020-04-24 10:38:13 +01:00
u8 *buf = NULL;
2020-04-16 01:06:41 +01:00
FILE *tmp_file = NULL;
2020-04-24 10:38:13 +01:00
Ticket tik = {0};
NcaContext *nca_ctx = NULL;
NcmContentStorage ncm_storage = {0};
Result rc = 0;
2020-04-22 21:53:20 +01:00
mkdir("sdmc:/nxdt_test", 0744);
2020-04-24 10:38:13 +01:00
/*FsRightsId rights_id = {
.c = { 0x01, 0x00, 0x82, 0x40, 0x0B, 0xCC, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 } // Untitled Goose Game
};*/
// Untitled Goose Game's Program NCA
/*NcmPackagedContentInfo content_info = {
2020-04-24 10:38:13 +01:00
.hash = {
0x8E, 0xF9, 0x20, 0xD4, 0x5E, 0xE1, 0x9E, 0xD1, 0xD2, 0x04, 0xC4, 0xC8, 0x22, 0x50, 0x79, 0xE8,
0xD3, 0xE2, 0xE2, 0xA0, 0x66, 0xFD, 0x2B, 0xB6, 0x5C, 0x73, 0xF6, 0x89, 0xE2, 0x25, 0x0A, 0x82
2020-04-24 10:38:13 +01:00
},
.info = {
.content_id = {
.c = { 0x8E, 0xF9, 0x20, 0xD4, 0x5E, 0xE1, 0x9E, 0xD1, 0xD2, 0x04, 0xC4, 0xC8, 0x22, 0x50, 0x79, 0xE8 }
},
.size = {
0x00, 0x40, 0xAD, 0x31, 0x00, 0x00
},
.content_type = NcmContentType_Program,
.id_offset = 0
}
};*/
// Untitled Goose Game's Control NCA
NcmPackagedContentInfo content_info = {
.hash = {
0xCE, 0x6E, 0x17, 0x1F, 0x93, 0x2D, 0x29, 0x28, 0xC1, 0x62, 0x94, 0x5B, 0x86, 0x2C, 0x42, 0x93,
0xAC, 0x2C, 0x0D, 0x3E, 0xD7, 0xCE, 0x07, 0xA2, 0x34, 0x33, 0x43, 0xD9, 0x21, 0x8A, 0xA3, 0xFE
},
.info = {
.content_id = {
.c = { 0xCE, 0x6E, 0x17, 0x1F, 0x93, 0x2D, 0x29, 0x28, 0xC1, 0x62, 0x94, 0x5B, 0x86, 0x2C, 0x42, 0x93 }
},
.size = {
0x00, 0x74, 0x0A, 0x00, 0x00, 0x00
},
.content_type = NcmContentType_Control,
.id_offset = 0
}
2020-04-24 10:38:13 +01:00
};
u64 romfs_size = 0;
RomFileSystemFileEntry *romfs_file_entry = NULL;
RomFileSystemContext romfs_ctx = {0};
2020-04-29 10:54:40 +01:00
RomFileSystemFileEntryPatch romfs_patch = {0};
2020-04-22 21:53:20 +01:00
2020-04-24 10:38:13 +01:00
buf = malloc(0x400000);
if (!buf)
2020-04-16 01:06:41 +01:00
{
2020-04-24 10:38:13 +01:00
printf("read buf failed\n");
goto out2;
2020-04-16 01:06:41 +01:00
}
2020-04-24 10:38:13 +01:00
printf("read buf succeeded\n");
2020-04-16 01:06:41 +01:00
consoleUpdate(NULL);
2020-04-24 10:38:13 +01:00
nca_ctx = calloc(1, sizeof(NcaContext));
if (!nca_ctx)
2020-04-16 01:06:41 +01:00
{
2020-04-24 10:38:13 +01:00
printf("nca ctx buf failed\n");
goto out2;
2020-04-16 01:06:41 +01:00
}
2020-04-24 10:38:13 +01:00
printf("nca ctx buf succeeded\n");
2020-04-16 01:06:41 +01:00
consoleUpdate(NULL);
2020-04-24 10:38:13 +01:00
rc = ncmOpenContentStorage(&ncm_storage, NcmStorageId_SdCard);
if (R_FAILED(rc))
2020-04-16 01:06:41 +01:00
{
2020-04-24 10:38:13 +01:00
printf("ncm open storage failed\n");
goto out2;
2020-04-16 01:06:41 +01:00
}
2020-04-24 10:38:13 +01:00
printf("ncm open storage succeeded\n");
2020-04-16 01:06:41 +01:00
consoleUpdate(NULL);
2020-04-24 10:38:13 +01:00
if (!ncaInitializeContext(nca_ctx, NcmStorageId_SdCard, &ncm_storage, 0, &content_info, &tik))
2020-04-15 21:50:07 +01:00
{
2020-04-24 10:38:13 +01:00
printf("nca initialize ctx failed\n");
goto out2;
2020-04-15 21:50:07 +01:00
}
2020-04-24 10:38:13 +01:00
tmp_file = fopen("sdmc:/nxdt_test/nca_ctx.bin", "wb");
if (tmp_file)
2020-04-15 21:50:07 +01:00
{
2020-04-24 10:38:13 +01:00
fwrite(nca_ctx, 1, sizeof(NcaContext), tmp_file);
fclose(tmp_file);
tmp_file = NULL;
printf("nca ctx saved\n");
2020-04-16 01:06:41 +01:00
} else {
2020-04-24 10:38:13 +01:00
printf("nca ctx not saved\n");
2020-04-11 06:28:26 +01:00
}
2020-04-16 01:06:41 +01:00
consoleUpdate(NULL);
if (!romfsInitializeContext(&romfs_ctx, &(nca_ctx->fs_contexts[0])))
2020-04-24 10:38:13 +01:00
{
printf("romfs initialize ctx failed\n");
2020-04-24 10:38:13 +01:00
goto out2;
2020-04-16 01:06:41 +01:00
}
2020-04-15 21:50:07 +01:00
printf("romfs initialize ctx succeeded\n");
2020-04-15 21:50:07 +01:00
consoleUpdate(NULL);
2020-04-16 01:06:41 +01:00
tmp_file = fopen("sdmc:/nxdt_test/romfs_ctx.bin", "wb");
2020-04-24 10:38:13 +01:00
if (tmp_file)
{
fwrite(&romfs_ctx, 1, sizeof(RomFileSystemContext), tmp_file);
2020-04-24 10:38:13 +01:00
fclose(tmp_file);
tmp_file = NULL;
printf("romfs ctx saved\n");
2020-04-24 10:38:13 +01:00
} else {
printf("romfs ctx not saved\n");
2020-04-24 10:38:13 +01:00
}
2020-04-24 10:38:13 +01:00
consoleUpdate(NULL);
tmp_file = fopen("sdmc:/nxdt_test/dir_table.bin", "wb");
2020-04-26 09:35:01 +01:00
if (tmp_file)
2020-04-24 10:38:13 +01:00
{
fwrite(romfs_ctx.dir_table, 1, romfs_ctx.dir_table_size, tmp_file);
2020-04-26 09:35:01 +01:00
fclose(tmp_file);
tmp_file = NULL;
2020-04-29 10:54:40 +01:00
printf("romfs dir table saved\n");
2020-04-26 09:35:01 +01:00
} else {
2020-04-29 10:54:40 +01:00
printf("romfs dir table not saved\n");
2020-04-24 10:38:13 +01:00
}
consoleUpdate(NULL);
tmp_file = fopen("sdmc:/nxdt_test/file_table.bin", "wb");
2020-04-24 10:38:13 +01:00
if (tmp_file)
2020-04-20 11:39:41 +01:00
{
fwrite(romfs_ctx.file_table, 1, romfs_ctx.file_table_size, tmp_file);
2020-04-24 10:38:13 +01:00
fclose(tmp_file);
tmp_file = NULL;
2020-04-29 10:54:40 +01:00
printf("romfs file table saved\n");
2020-04-26 09:35:01 +01:00
} else {
2020-04-29 10:54:40 +01:00
printf("romfs file table not saved\n");
}
consoleUpdate(NULL);
if (romfsReadFileSystemData(&romfs_ctx, buf, romfs_ctx.size, 0))
{
printf("romfs read fs data success\n");
consoleUpdate(NULL);
tmp_file = fopen("sdmc:/nxdt_test/romfs.bin", "wb");
if (tmp_file)
{
fwrite(buf, 1, romfs_ctx.size, tmp_file);
fclose(tmp_file);
tmp_file = NULL;
printf("romfs data saved\n");
} else {
printf("romfs data not saved\n");
}
} else {
printf("romfs read fs data failed\n");
}
if (romfsGetTotalDataSize(&romfs_ctx, &romfs_size))
{
printf("romfs size succeeded: 0x%lX\n", romfs_size);
} else {
printf("romfs size failed\n");
2020-04-26 09:35:01 +01:00
}
consoleUpdate(NULL);
romfs_file_entry = romfsGetFileEntryByPath(&romfs_ctx, "/control.nacp");
if (!romfs_file_entry)
{
printf("romfs get file entry by path failed\n");
goto out2;
}
printf("romfs get file entry by path success: %s | %p\n", romfs_file_entry->name, romfs_file_entry);
consoleUpdate(NULL);
2020-04-26 09:35:01 +01:00
2020-04-29 10:54:40 +01:00
if (!romfsReadFileEntryData(&romfs_ctx, romfs_file_entry, buf, romfs_file_entry->size, 0))
{
printf("romfs read file entry failed\n");
2020-04-29 10:54:40 +01:00
goto out2;
}
2020-04-29 10:54:40 +01:00
printf("romfs read file entry success\n");
2020-04-28 09:58:17 +01:00
consoleUpdate(NULL);
2020-04-29 10:54:40 +01:00
tmp_file = fopen("sdmc:/nxdt_test/control.nacp", "wb");
if (tmp_file)
2020-04-28 09:58:17 +01:00
{
2020-04-29 10:54:40 +01:00
fwrite(buf, 1, romfs_file_entry->size, tmp_file);
fclose(tmp_file);
tmp_file = NULL;
printf("romfs file entry data saved\n");
} else {
printf("romfs file entry data not saved\n");
}
consoleUpdate(NULL);
NacpStruct *nacp_data = (NacpStruct*)buf;
memset(nacp_data->lang, 0, MEMBER_SIZE(NacpStruct, lang));
for(u8 i = 0; i < 16; i++)
{
sprintf(nacp_data->lang[i].name, "nxdumptool");
sprintf(nacp_data->lang[i].author, "DarkMatterCore");
}
tmp_file = fopen("sdmc:/nxdt_test/control_mod.nacp", "wb");
if (tmp_file)
{
fwrite(buf, 1, romfs_file_entry->size, tmp_file);
fclose(tmp_file);
tmp_file = NULL;
printf("romfs file entry mod data saved\n");
} else {
printf("romfs file entry mod data not saved\n");
}
consoleUpdate(NULL);
if (!romfsGenerateFileEntryPatch(&romfs_ctx, romfs_file_entry, buf, MEMBER_SIZE(NacpStruct, lang), 0, &romfs_patch))
{
printf("romfs file entry patch failed\n");
goto out2;
}
printf("romfs file entry patch success\n");
consoleUpdate(NULL);
tmp_file = fopen("sdmc:/nxdt_test/romfs_patch.bin", "wb");
if (tmp_file)
{
fwrite(&romfs_patch, 1, sizeof(RomFileSystemFileEntryPatch), tmp_file);
fclose(tmp_file);
tmp_file = NULL;
printf("romfs patch saved\n");
} else {
printf("romfs patch not saved\n");
}
for(u8 i = 0; i < (NCA_IVFC_HASH_DATA_LAYER_COUNT + 1); i++)
{
NcaHashInfoLayerPatch *layer_patch = (i < NCA_IVFC_HASH_DATA_LAYER_COUNT ? &(romfs_patch.cur_format_patch.hash_data_layer_patch[i]) : &(romfs_patch.cur_format_patch.hash_target_layer_patch));
if (!layer_patch->size || !layer_patch->data) continue;
2020-04-28 09:58:17 +01:00
2020-04-29 10:54:40 +01:00
char path[64];
sprintf(path, "sdmc:/nxdt_test/romfs_patch_l%u.bin", i);
tmp_file = fopen(path, "wb");
2020-04-28 09:58:17 +01:00
if (tmp_file)
{
2020-04-29 10:54:40 +01:00
fwrite(layer_patch->data, 1, layer_patch->size, tmp_file);
2020-04-28 09:58:17 +01:00
fclose(tmp_file);
tmp_file = NULL;
2020-04-29 10:54:40 +01:00
printf("romfs patch #%u saved\n", i);
2020-04-28 09:58:17 +01:00
} else {
2020-04-29 10:54:40 +01:00
printf("romfs patch #%u not saved\n", i);
2020-04-28 09:58:17 +01:00
}
2020-04-29 10:54:40 +01:00
consoleUpdate(NULL);
}
if (!ncaEncryptHeader(nca_ctx))
{
printf("nca header mod not encrypted\n");
goto out2;
}
printf("nca header mod encrypted\n");
consoleUpdate(NULL);
tmp_file = fopen("sdmc:/nxdt_test/nca_header_mod.bin", "wb");
if (tmp_file)
{
fwrite(&(nca_ctx->header), 1, sizeof(NcaHeader), tmp_file);
fclose(tmp_file);
tmp_file = NULL;
printf("nca header mod saved\n");
2020-04-28 09:58:17 +01:00
} else {
2020-04-29 10:54:40 +01:00
printf("nca header mod not saved\n");
2020-04-28 09:58:17 +01:00
}
2020-04-26 09:35:01 +01:00
2020-04-20 11:39:41 +01:00
2020-04-26 09:35:01 +01:00
2020-04-26 09:35:01 +01:00
2020-04-29 10:54:40 +01:00
2020-04-24 10:38:13 +01:00
out2:
while(appletMainLoop())
2020-04-20 11:39:41 +01:00
{
2020-04-24 10:38:13 +01:00
consoleUpdate(NULL);
2020-04-20 11:39:41 +01:00
hidScanInput();
if (utilsHidKeysAllDown() & KEY_A) break;
}
2020-04-24 10:38:13 +01:00
if (tmp_file) fclose(tmp_file);
2020-04-29 10:54:40 +01:00
romfsFreeFileEntryPatch(&romfs_patch);
romfsFreeContext(&romfs_ctx);
2020-04-24 10:38:13 +01:00
if (serviceIsActive(&(ncm_storage.s))) ncmContentStorageClose(&ncm_storage);
if (nca_ctx) free(nca_ctx);
if (buf) free(buf);
2020-04-15 21:50:07 +01:00
consoleExit(NULL);
2020-04-11 06:28:26 +01:00
out:
2020-04-15 21:50:07 +01:00
utilsCloseResources();
2020-04-11 06:28:26 +01:00
return ret;
}