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-17 22:59:05 +01:00
|
|
|
|
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 "pfs0.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
NcmPackagedContentInfo content_info = {
|
|
|
|
.hash = {
|
|
|
|
0x8E, 0xF9, 0x20, 0xD4, 0x5E, 0xE1, 0x9E, 0xD1, 0xD2, 0x04, 0xC4, 0xC8, 0x22, 0x50, 0x79, 0xE8,
|
|
|
|
0x8E, 0xF9, 0x20, 0xD4, 0x5E, 0xE1, 0x9E, 0xD1, 0xD2, 0x04, 0xC4, 0xC8, 0x22, 0x50, 0x79, 0xE8
|
|
|
|
},
|
|
|
|
.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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PartitionFileSystemContext pfs0_ctx = {0};
|
|
|
|
PartitionFileSystemEntry *pfs0_entry = NULL;
|
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);
|
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
tmp_file = fopen("sdmc:/nxdt_test/section0.bin", "wb");
|
|
|
|
if (tmp_file)
|
2020-04-16 01:06:41 +01:00
|
|
|
{
|
2020-04-24 10:38:13 +01:00
|
|
|
u64 blksize = 0x400000;
|
|
|
|
u64 total = nca_ctx->fs_contexts[0].section_size;
|
|
|
|
|
|
|
|
printf("nca section0 created: 0x%lX\n", total);
|
2020-04-16 01:06:41 +01:00
|
|
|
consoleUpdate(NULL);
|
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
for(u64 curpos = 0; curpos < total; curpos += blksize)
|
2020-04-16 01:06:41 +01:00
|
|
|
{
|
2020-04-24 10:38:13 +01:00
|
|
|
if (blksize > (total - curpos)) blksize = (total - curpos);
|
2020-04-16 01:06:41 +01:00
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
if (!ncaReadFsSection(&(nca_ctx->fs_contexts[0]), buf, blksize, curpos))
|
2020-04-16 01:06:41 +01:00
|
|
|
{
|
2020-04-24 10:38:13 +01:00
|
|
|
printf("nca read section failed\n");
|
|
|
|
goto out2;
|
2020-04-16 01:06:41 +01:00
|
|
|
}
|
2020-04-24 10:38:13 +01:00
|
|
|
|
|
|
|
fwrite(buf, 1, blksize, tmp_file);
|
2020-04-16 01:06:41 +01:00
|
|
|
}
|
2020-04-24 10:38:13 +01:00
|
|
|
|
|
|
|
fclose(tmp_file);
|
|
|
|
tmp_file = NULL;
|
|
|
|
|
|
|
|
printf("nca read section0 success\n");
|
2020-04-16 01:06:41 +01:00
|
|
|
} else {
|
2020-04-24 10:38:13 +01:00
|
|
|
printf("nca section0 not created\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
consoleUpdate(NULL);
|
|
|
|
|
|
|
|
if (!pfs0InitializeContext(&pfs0_ctx, &(nca_ctx->fs_contexts[0])))
|
|
|
|
{
|
|
|
|
printf("pfs0 initialize ctx failed\n");
|
|
|
|
goto out2;
|
2020-04-16 01:06:41 +01:00
|
|
|
}
|
2020-04-15 21:50:07 +01:00
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
printf("pfs0 initialize ctx succeeded\n");
|
2020-04-15 21:50:07 +01:00
|
|
|
consoleUpdate(NULL);
|
2020-04-16 01:06:41 +01:00
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
tmp_file = fopen("sdmc:/nxdt_test/pfs0_ctx.bin", "wb");
|
|
|
|
if (tmp_file)
|
2020-04-16 11:13:11 +01:00
|
|
|
{
|
2020-04-24 10:38:13 +01:00
|
|
|
fwrite(&pfs0_ctx, 1, sizeof(PartitionFileSystemContext), tmp_file);
|
|
|
|
fclose(tmp_file);
|
|
|
|
tmp_file = NULL;
|
|
|
|
printf("pfs0 ctx saved\n");
|
2020-04-16 11:13:11 +01:00
|
|
|
} else {
|
2020-04-24 10:38:13 +01:00
|
|
|
printf("pfs0 ctx not saved\n");
|
2020-04-16 11:13:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
consoleUpdate(NULL);
|
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
tmp_file = fopen("sdmc:/nxdt_test/pfs0_header.bin", "wb");
|
|
|
|
if (tmp_file)
|
|
|
|
{
|
|
|
|
fwrite(pfs0_ctx.header, 1, pfs0_ctx.header_size, tmp_file);
|
|
|
|
fclose(tmp_file);
|
|
|
|
tmp_file = NULL;
|
|
|
|
printf("pfs0 header saved\n");
|
|
|
|
} else {
|
|
|
|
printf("pfs0 header not saved\n");
|
|
|
|
}
|
2020-04-17 22:59:05 +01:00
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
consoleUpdate(NULL);
|
2020-04-17 22:59:05 +01:00
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
pfs0_entry = pfs0GetEntryByName(&pfs0_ctx, "main.npdm");
|
|
|
|
if (!pfs0_entry)
|
|
|
|
{
|
|
|
|
printf("pfs0 get entry by name failed\n");
|
|
|
|
goto out2;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("pfs0 get entry by name succeeded\n");
|
|
|
|
consoleUpdate(NULL);
|
2020-04-17 22:59:05 +01:00
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
tmp_file = fopen("sdmc:/nxdt_test/main.npdm", "wb");
|
|
|
|
if (tmp_file)
|
2020-04-20 11:39:41 +01:00
|
|
|
{
|
2020-04-24 10:38:13 +01:00
|
|
|
u64 blksize = 0x400000;
|
|
|
|
u64 total = pfs0_entry->size;
|
|
|
|
|
2020-04-24 19:42:16 +01:00
|
|
|
printf("main.npdm created. Target size -> 0x%lX\n", total);
|
2020-04-20 11:39:41 +01:00
|
|
|
consoleUpdate(NULL);
|
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
for(u64 curpos = 0; curpos < total; curpos += blksize)
|
2020-04-20 11:39:41 +01:00
|
|
|
{
|
2020-04-24 10:38:13 +01:00
|
|
|
if (blksize > (total - curpos)) blksize = (total - curpos);
|
2020-04-20 11:39:41 +01:00
|
|
|
|
2020-04-24 19:42:16 +01:00
|
|
|
if (!pfs0ReadEntryData(&pfs0_ctx, pfs0_entry, buf, blksize, 0))
|
2020-04-20 11:39:41 +01:00
|
|
|
{
|
2020-04-24 19:42:16 +01:00
|
|
|
printf("pfs0 read entry data failed\n");
|
2020-04-24 10:38:13 +01:00
|
|
|
goto out2;
|
2020-04-20 11:39:41 +01:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
fwrite(buf, 1, blksize, tmp_file);
|
2020-04-20 11:39:41 +01:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:38:13 +01:00
|
|
|
fclose(tmp_file);
|
|
|
|
tmp_file = NULL;
|
|
|
|
|
2020-04-24 19:42:16 +01:00
|
|
|
printf("pfs0 read main.npdm success\n");
|
2020-04-20 11:39:41 +01:00
|
|
|
} else {
|
2020-04-24 10:38:13 +01:00
|
|
|
printf("main.npdm not created\n");
|
2020-04-20 11:39:41 +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);
|
|
|
|
|
|
|
|
pfs0FreeContext(&pfs0_ctx);
|
|
|
|
|
|
|
|
if (serviceIsActive(&(ncm_storage.s))) ncmContentStorageClose(&ncm_storage);
|
|
|
|
|
|
|
|
if (nca_ctx) free(nca_ctx);
|
2020-04-17 22:59:05 +01:00
|
|
|
|
2020-04-21 11:23:33 +01:00
|
|
|
if (buf) free(buf);
|
2020-04-17 22:59:05 +01:00
|
|
|
|
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;
|
|
|
|
}
|