1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-10 04:31:44 +00:00
nxdumptool/source/main.c

340 lines
9 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-30 12:24:08 +01:00
#include "bktr.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-30 12:24:08 +01:00
Ticket base_tik = {0}, update_tik = {0};
NcaContext *base_nca_ctx = NULL, *update_nca_ctx = NULL;
2020-04-24 10:38:13 +01:00
NcmContentStorage ncm_storage = {0};
2020-04-30 12:24:08 +01:00
BktrContext bktr_ctx = {0};
RomFileSystemFileEntry *bktr_file_entry = NULL;
2020-04-24 10:38:13 +01:00
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
};*/
2020-04-30 12:24:08 +01:00
// Untitled Goose Game's Base Program NCA
NcmPackagedContentInfo base_program_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
}
2020-04-29 11:55:20 +01:00
};
2020-04-30 12:24:08 +01:00
// Untitled Goose Game's Update Program NCA
NcmPackagedContentInfo update_program_content_info = {
.hash = {
2020-04-30 12:24:08 +01:00
0xDB, 0xEE, 0x62, 0x0E, 0x8F, 0x64, 0x37, 0xE4, 0x8A, 0x5C, 0x63, 0x61, 0xE8, 0xD2, 0x32, 0x6A,
0x21, 0x0D, 0x79, 0x50, 0x3A, 0xAF, 0x0D, 0x66, 0x76, 0xE2, 0xBC, 0x84, 0xF7, 0x0A, 0x21, 0xE2
},
.info = {
.content_id = {
2020-04-30 12:24:08 +01:00
.c = { 0xDB, 0xEE, 0x62, 0x0E, 0x8F, 0x64, 0x37, 0xE4, 0x8A, 0x5C, 0x63, 0x61, 0xE8, 0xD2, 0x32, 0x6A }
},
.size = {
2020-04-30 12:24:08 +01:00
0x00, 0xF4, 0xA0, 0x0E, 0x00, 0x00
},
2020-04-30 12:24:08 +01:00
.content_type = NcmContentType_Program,
.id_offset = 0
}
2020-04-30 12:24:08 +01:00
};
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-30 12:24:08 +01:00
base_nca_ctx = calloc(1, sizeof(NcaContext));
if (!base_nca_ctx)
2020-04-16 01:06:41 +01:00
{
2020-04-30 12:24:08 +01:00
printf("base nca ctx buf failed\n");
2020-04-24 10:38:13 +01:00
goto out2;
2020-04-16 01:06:41 +01:00
}
2020-04-30 12:24:08 +01:00
printf("base nca ctx buf succeeded\n");
consoleUpdate(NULL);
update_nca_ctx = calloc(1, sizeof(NcaContext));
if (!update_nca_ctx)
{
printf("update nca ctx buf failed\n");
goto out2;
}
printf("update 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-30 12:24:08 +01:00
if (!ncaInitializeContext(base_nca_ctx, NcmStorageId_SdCard, &ncm_storage, 0, &base_program_content_info, &base_tik))
2020-04-15 21:50:07 +01:00
{
2020-04-30 12:24:08 +01:00
printf("base nca initialize ctx failed\n");
2020-04-24 10:38:13 +01:00
goto out2;
2020-04-15 21:50:07 +01:00
}
2020-04-30 12:24:08 +01:00
tmp_file = fopen("sdmc:/nxdt_test/base_nca_ctx.bin", "wb");
2020-04-24 10:38:13 +01:00
if (tmp_file)
2020-04-15 21:50:07 +01:00
{
2020-04-30 12:24:08 +01:00
fwrite(base_nca_ctx, 1, sizeof(NcaContext), tmp_file);
2020-04-24 10:38:13 +01:00
fclose(tmp_file);
tmp_file = NULL;
2020-04-30 12:24:08 +01:00
printf("base nca ctx saved\n");
2020-04-16 01:06:41 +01:00
} else {
2020-04-30 12:24:08 +01:00
printf("base nca ctx not saved\n");
2020-04-11 06:28:26 +01:00
}
2020-04-16 01:06:41 +01:00
consoleUpdate(NULL);
2020-04-30 12:24:08 +01:00
if (!ncaInitializeContext(update_nca_ctx, NcmStorageId_SdCard, &ncm_storage, 0, &update_program_content_info, &update_tik))
2020-04-24 10:38:13 +01:00
{
2020-04-30 12:24:08 +01:00
printf("update nca 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
2020-04-30 12:24:08 +01:00
tmp_file = fopen("sdmc:/nxdt_test/update_nca_ctx.bin", "wb");
2020-04-26 09:35:01 +01:00
if (tmp_file)
2020-04-24 10:38:13 +01:00
{
2020-04-30 12:24:08 +01:00
fwrite(update_nca_ctx, 1, sizeof(NcaContext), tmp_file);
2020-04-26 09:35:01 +01:00
fclose(tmp_file);
tmp_file = NULL;
2020-04-30 12:24:08 +01:00
printf("update nca ctx saved\n");
2020-04-26 09:35:01 +01:00
} else {
2020-04-30 12:24:08 +01:00
printf("update nca ctx not saved\n");
2020-04-24 10:38:13 +01:00
}
consoleUpdate(NULL);
2020-04-30 12:24:08 +01:00
if (!bktrInitializeContext(&bktr_ctx, &(base_nca_ctx->fs_contexts[1]), &(update_nca_ctx->fs_contexts[1])))
2020-04-20 11:39:41 +01:00
{
2020-04-30 12:24:08 +01:00
printf("bktr initialize ctx failed\n");
goto out2;
}
2020-04-30 12:24:08 +01:00
printf("bktr initialize ctx succeeded\n");
consoleUpdate(NULL);
2020-04-26 09:35:01 +01:00
2020-04-30 12:24:08 +01:00
tmp_file = fopen("sdmc:/nxdt_test/bktr_ctx.bin", "wb");
2020-04-29 10:54:40 +01:00
if (tmp_file)
2020-04-28 09:58:17 +01:00
{
2020-04-30 12:24:08 +01:00
fwrite(&bktr_ctx, 1, sizeof(BktrContext), tmp_file);
2020-04-29 10:54:40 +01:00
fclose(tmp_file);
tmp_file = NULL;
2020-04-30 12:24:08 +01:00
printf("bktr ctx saved\n");
2020-04-29 10:54:40 +01:00
} else {
2020-04-30 12:24:08 +01:00
printf("bktr ctx not saved\n");
2020-04-29 10:54:40 +01:00
}
consoleUpdate(NULL);
2020-05-01 05:34:30 +01:00
/*bktr_file_entry = bktrGetFileEntryByPath(&bktr_ctx, "/Data/resources.assets");
2020-04-30 12:24:08 +01:00
if (!bktr_file_entry)
2020-04-29 10:54:40 +01:00
{
2020-04-30 12:24:08 +01:00
printf("bktr get file entry by path failed\n");
2020-04-29 10:54:40 +01:00
goto out2;
}
2020-04-30 12:24:08 +01:00
printf("bktr get file entry by path success: %.*s | 0x%lX\n", bktr_file_entry->name_length, bktr_file_entry->name, bktr_file_entry->size);
2020-05-01 05:34:30 +01:00
consoleUpdate(NULL);*/
2020-04-29 10:54:40 +01:00
2020-04-30 12:24:08 +01:00
/*tmp_file = fopen("sdmc:/nxdt_test/resources.assets", "wb");
2020-04-29 10:54:40 +01:00
if (tmp_file)
{
2020-04-30 12:24:08 +01:00
u64 curpos = 0, blksize = 0x400000;
for(curpos = 0; curpos < bktr_file_entry->size; curpos += blksize)
{
if (blksize > (bktr_file_entry->size - curpos)) blksize = (bktr_file_entry->size - curpos);
if (!bktrReadFileEntryData(&bktr_ctx, bktr_file_entry, buf, blksize, curpos)) break;
fwrite(buf, 1, blksize, tmp_file);
}
2020-04-29 10:54:40 +01:00
fclose(tmp_file);
tmp_file = NULL;
2020-04-28 09:58:17 +01:00
2020-04-30 12:24:08 +01:00
if (curpos < bktr_file_entry->size)
2020-04-28 09:58:17 +01:00
{
2020-04-30 12:24:08 +01:00
printf("resources.assets read error\n");
2020-04-28 09:58:17 +01:00
} else {
2020-04-30 12:24:08 +01:00
printf("resources.assets saved\n");
2020-04-28 09:58:17 +01:00
}
2020-04-30 12:24:08 +01:00
} else {
printf("resources.assets not saved\n");
2020-05-01 05:34:30 +01:00
}
consoleUpdate(NULL);*/
2020-04-29 10:54:40 +01:00
2020-05-01 05:34:30 +01:00
/*tmp_file = fopen("sdmc:/nxdt_test/romfs.bin", "wb");
2020-04-29 10:54:40 +01:00
if (tmp_file)
{
2020-04-30 12:24:08 +01:00
u64 curpos = 0, blksize = 0x400000;
for(curpos = 0; curpos < bktr_ctx.size; curpos += blksize)
{
if (blksize > (bktr_ctx.size - curpos)) blksize = (bktr_ctx.size - curpos);
if (!bktrReadFileSystemData(&bktr_ctx, buf, blksize, curpos)) break;
fwrite(buf, 1, blksize, tmp_file);
}
2020-04-29 10:54:40 +01:00
fclose(tmp_file);
tmp_file = NULL;
2020-04-30 12:24:08 +01:00
if (curpos < bktr_ctx.size)
{
printf("romfs read error\n");
} else {
printf("romfs saved\n");
}
2020-04-28 09:58:17 +01:00
} else {
2020-04-30 12:24:08 +01:00
printf("romfs not saved\n");
2020-04-28 09:58:17 +01:00
}
2020-05-01 05:34:30 +01:00
consoleUpdate(NULL);*/
printf("updated file list:\n");
consoleUpdate(NULL);
u64 offset = 0;
bool updated = false;
char bktr_path[FS_MAX_PATH] = {0};
while(offset < bktr_ctx.patch_romfs_ctx.file_table_size)
{
if (!(bktr_file_entry = bktrGetFileEntryByOffset(&bktr_ctx, offset)))
{
printf("Failed to retrieve file entry!\n");
goto out2;
}
if (bktrIsFileEntryUpdated(&bktr_ctx, bktr_file_entry, &updated) && updated && bktrGeneratePathFromFileEntry(&bktr_ctx, bktr_file_entry, bktr_path, FS_MAX_PATH))
{
printf("%s\n", bktr_path);
consoleUpdate(NULL);
}
offset += ALIGN_UP(sizeof(RomFileSystemFileEntry) + bktr_file_entry->name_length, 4);
}
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-30 12:24:08 +01:00
bktrFreeContext(&bktr_ctx);
2020-04-24 10:38:13 +01:00
if (serviceIsActive(&(ncm_storage.s))) ncmContentStorageClose(&ncm_storage);
2020-04-30 12:24:08 +01:00
if (update_nca_ctx) free(update_nca_ctx);
if (base_nca_ctx) free(base_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;
}