1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-12-02 07:02:18 +00:00
nxdumptool/source/main.c

362 lines
20 KiB
C
Raw Permalink Normal View History

2018-05-15 13:57:40 +01:00
#include <stdio.h>
2019-04-21 17:27:33 +01:00
#include <stdlib.h>
#include <string.h>
2018-05-15 13:57:40 +01:00
#include <switch.h>
2018-05-16 17:06:57 +01:00
#include <memory.h>
2018-05-15 17:00:19 +01:00
#include "dumper.h"
#include "ui.h"
#include "util.h"
2019-06-05 23:44:18 +01:00
#include "fs_ext.h"
#include "keys.h"
2018-05-15 13:57:40 +01:00
2019-04-21 17:27:33 +01:00
/* Extern variables */
2018-05-15 13:57:40 +01:00
2019-04-21 17:27:33 +01:00
extern FsDeviceOperator fsOperatorInstance;
2019-04-21 17:27:33 +01:00
extern FsEventNotifier fsGameCardEventNotifier;
extern Handle fsGameCardEventHandle;
extern Event fsGameCardKernelEvent;
extern UEvent exitEvent;
2019-04-21 17:27:33 +01:00
extern bool gameCardInserted;
2018-06-30 07:37:42 +01:00
2019-05-01 21:24:13 +01:00
extern char strbuf[NAME_BUF_LEN * 4];
2019-06-05 23:44:18 +01:00
extern char appLaunchPath[NAME_BUF_LEN];
2019-05-01 21:24:13 +01:00
extern nca_keyset_t nca_keyset;
2019-04-21 17:27:33 +01:00
int main(int argc, char *argv[])
{
2019-06-05 23:44:18 +01:00
/* Copy launch path */
if (!envIsNso() && argc > 0)
{
int i;
for(i = 0; i < argc; i++)
{
if (strlen(argv[i]) > 10 && !strncasecmp(argv[i], "sdmc:/", 6) && !strncasecmp(argv[i] + strlen(argv[i]) - 4, ".nro", 4))
{
snprintf(appLaunchPath, sizeof(appLaunchPath) / sizeof(appLaunchPath[0]), argv[i]);
break;
}
}
}
2019-04-21 17:27:33 +01:00
/* Initialize UI */
if (!uiInit()) return -1;
int ret = 0;
Result result;
2019-06-05 23:44:18 +01:00
/* Initialize the ncm service */
result = ncmInitialize();
2019-04-21 17:27:33 +01:00
if (R_SUCCEEDED(result))
{
2019-06-05 23:44:18 +01:00
/* Initialize the ns service */
result = nsInitialize();
2019-04-21 17:27:33 +01:00
if (R_SUCCEEDED(result))
{
2019-06-05 23:44:18 +01:00
/* Initialize the csrng service */
result = csrngInitialize();
2019-04-21 17:27:33 +01:00
if (R_SUCCEEDED(result))
{
2019-06-05 23:44:18 +01:00
/* Initialize the spl service */
result = splInitialize();
2019-04-21 17:27:33 +01:00
if (R_SUCCEEDED(result))
{
2019-06-05 23:44:18 +01:00
/* Initialize the pm:dmnt service */
result = pmdmntInitialize();
2019-04-21 17:27:33 +01:00
if (R_SUCCEEDED(result))
{
2019-06-05 23:44:18 +01:00
/* Open device operator */
result = fsOpenDeviceOperator(&fsOperatorInstance);
2019-04-21 17:27:33 +01:00
if (R_SUCCEEDED(result))
{
2019-06-05 23:44:18 +01:00
/* Open gamecard detection event notifier */
result = fsOpenGameCardDetectionEventNotifier(&fsGameCardEventNotifier);
2019-04-21 17:27:33 +01:00
if (R_SUCCEEDED(result))
{
2019-06-05 23:44:18 +01:00
/* Retrieve gamecard detection event handle */
result = fsEventNotifierGetEventHandle(&fsGameCardEventNotifier, &fsGameCardEventHandle);
2019-04-21 17:27:33 +01:00
if (R_SUCCEEDED(result))
{
2019-06-05 23:44:18 +01:00
/* Retrieve initial gamecard status */
gameCardInserted = isGameCardInserted();
/* Load gamecard detection kernel event */
eventLoadRemote(&fsGameCardKernelEvent, fsGameCardEventHandle, false);
/* Create usermode exit event */
ueventCreate(&exitEvent, false);
/* Create gamecard detection thread */
Thread thread;
result = threadCreate(&thread, fsGameCardDetectionThreadFunc, NULL, 0x10000, 0x2C, -2);
2019-04-21 17:27:33 +01:00
if (R_SUCCEEDED(result))
{
2019-06-05 23:44:18 +01:00
/* Start gamecard detection thread */
result = threadStart(&thread);
if (R_SUCCEEDED(result))
2019-04-21 17:27:33 +01:00
{
2019-06-05 23:44:18 +01:00
/* Zero out NCA keyset */
memset(&nca_keyset, 0, sizeof(nca_keyset_t));
2019-06-09 02:36:21 +01:00
/* Init ExeFS context */
initExeFsContext();
2019-06-05 23:44:18 +01:00
/* Init RomFS context */
initRomFsContext();
2019-06-09 02:36:21 +01:00
/* Make sure output directories exist */
createOutputDirectories();
2019-06-05 23:44:18 +01:00
/* Main application loop */
bool exitLoop = false;
while(appletMainLoop())
2019-04-21 17:27:33 +01:00
{
2019-06-05 23:44:18 +01:00
UIResult result = uiProcess();
switch(result)
{
case resultShowMainMenu:
uiSetState(stateMainMenu);
break;
2019-06-09 02:36:21 +01:00
case resultShowGameCardMenu:
uiSetState(stateGameCardMenu);
break;
2019-06-05 23:44:18 +01:00
case resultShowXciDumpMenu:
uiSetState(stateXciDumpMenu);
break;
case resultDumpXci:
uiSetState(stateDumpXci);
break;
case resultShowNspDumpMenu:
uiSetState(stateNspDumpMenu);
break;
case resultShowNspAppDumpMenu:
uiSetState(stateNspAppDumpMenu);
break;
case resultShowNspPatchDumpMenu:
uiSetState(stateNspPatchDumpMenu);
break;
case resultShowNspAddOnDumpMenu:
uiSetState(stateNspAddOnDumpMenu);
break;
case resultDumpNsp:
uiSetState(stateDumpNsp);
break;
case resultShowHfs0Menu:
uiSetState(stateHfs0Menu);
break;
case resultShowRawHfs0PartitionDumpMenu:
uiSetState(stateRawHfs0PartitionDumpMenu);
break;
case resultDumpRawHfs0Partition:
uiSetState(stateDumpRawHfs0Partition);
break;
case resultShowHfs0PartitionDataDumpMenu:
uiSetState(stateHfs0PartitionDataDumpMenu);
break;
case resultDumpHfs0PartitionData:
uiSetState(stateDumpHfs0PartitionData);
break;
case resultShowHfs0BrowserMenu:
uiSetState(stateHfs0BrowserMenu);
break;
case resultHfs0BrowserGetList:
uiSetState(stateHfs0BrowserGetList);
break;
case resultShowHfs0Browser:
uiSetState(stateHfs0Browser);
break;
case resultHfs0BrowserCopyFile:
uiSetState(stateHfs0BrowserCopyFile);
break;
2019-06-09 02:36:21 +01:00
case resultShowExeFsMenu:
uiSetState(stateExeFsMenu);
break;
case resultShowExeFsSectionDataDumpMenu:
uiSetState(stateExeFsSectionDataDumpMenu);
break;
case resultDumpExeFsSectionData:
uiSetState(stateDumpExeFsSectionData);
break;
case resultShowExeFsSectionBrowserMenu:
uiSetState(stateExeFsSectionBrowserMenu);
break;
case resultExeFsSectionBrowserGetList:
uiSetState(stateExeFsSectionBrowserGetList);
break;
case resultShowExeFsSectionBrowser:
uiSetState(stateExeFsSectionBrowser);
break;
case resultExeFsSectionBrowserCopyFile:
uiSetState(stateExeFsSectionBrowserCopyFile);
break;
2019-06-05 23:44:18 +01:00
case resultShowRomFsMenu:
uiSetState(stateRomFsMenu);
break;
case resultShowRomFsSectionDataDumpMenu:
uiSetState(stateRomFsSectionDataDumpMenu);
break;
case resultDumpRomFsSectionData:
uiSetState(stateDumpRomFsSectionData);
break;
case resultShowRomFsSectionBrowserMenu:
uiSetState(stateRomFsSectionBrowserMenu);
break;
case resultRomFsSectionBrowserGetEntries:
uiSetState(stateRomFsSectionBrowserGetEntries);
break;
case resultShowRomFsSectionBrowser:
uiSetState(stateRomFsSectionBrowser);
break;
case resultRomFsSectionBrowserChangeDir:
uiSetState(stateRomFsSectionBrowserChangeDir);
break;
case resultRomFsSectionBrowserCopyFile:
uiSetState(stateRomFsSectionBrowserCopyFile);
break;
case resultDumpGameCardCertificate:
uiSetState(stateDumpGameCardCertificate);
break;
2019-06-09 02:36:21 +01:00
case resultShowSdCardEmmcMenu:
uiSetState(stateSdCardEmmcMenu);
break;
case resultShowSdCardEmmcTitleMenu:
uiSetState(stateSdCardEmmcTitleMenu);
break;
case resultShowSdCardEmmcOrphanPatchAddOnMenu:
uiSetState(stateSdCardEmmcOrphanPatchAddOnMenu);
break;
2019-06-05 23:44:18 +01:00
case resultShowUpdateMenu:
uiSetState(stateUpdateMenu);
break;
case resultUpdateNSWDBXml:
uiSetState(stateUpdateNSWDBXml);
break;
case resultUpdateApplication:
uiSetState(stateUpdateApplication);
break;
case resultExit:
exitLoop = true;
break;
default:
break;
}
if (exitLoop) break;
2019-04-21 17:27:33 +01:00
}
2019-06-05 23:44:18 +01:00
/* Signal the exit event to terminate the gamecard detection thread */
ueventSignal(&exitEvent);
/* Wait for the gamecard detection thread to exit */
threadWaitForExit(&thread);
} else {
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to start gamecard detection thread! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
uiRefreshDisplay();
delay(5);
ret = -11;
2019-04-21 17:27:33 +01:00
}
2019-06-05 23:44:18 +01:00
/* Close gamecard detection thread */
threadClose(&thread);
2019-04-21 17:27:33 +01:00
} else {
2019-06-05 23:44:18 +01:00
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to create gamecard detection thread! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
2019-04-21 17:27:33 +01:00
uiRefreshDisplay();
delay(5);
ret = -10;
}
2019-06-05 23:44:18 +01:00
/* Close gamecard detection kernel event */
eventClose(&fsGameCardKernelEvent);
2019-04-21 17:27:33 +01:00
} else {
2019-06-05 23:44:18 +01:00
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to retrieve gamecard detection event handle! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
2019-04-21 17:27:33 +01:00
uiRefreshDisplay();
delay(5);
ret = -9;
}
2019-06-05 23:44:18 +01:00
/* Close gamecard detection event notifier */
fsEventNotifierClose(&fsGameCardEventNotifier);
2019-04-21 17:27:33 +01:00
} else {
2019-06-05 23:44:18 +01:00
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to open gamecard detection event notifier! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
2019-04-21 17:27:33 +01:00
uiRefreshDisplay();
delay(5);
ret = -8;
}
2019-06-05 23:44:18 +01:00
/* Close device operator */
fsDeviceOperatorClose(&fsOperatorInstance);
2019-04-21 17:27:33 +01:00
} else {
2019-06-05 23:44:18 +01:00
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to open device operator! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
2019-04-21 17:27:33 +01:00
uiRefreshDisplay();
delay(5);
ret = -7;
}
2019-06-05 23:44:18 +01:00
/* Denitialize the pm:dmnt service */
pmdmntExit();
2019-04-21 17:27:33 +01:00
} else {
2019-06-05 23:44:18 +01:00
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to initialize the pm:dmnt service! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
2019-04-21 17:27:33 +01:00
uiRefreshDisplay();
delay(5);
ret = -6;
}
2019-06-05 23:44:18 +01:00
/* Denitialize the spl service */
splExit();
2019-04-21 17:27:33 +01:00
} else {
2019-06-05 23:44:18 +01:00
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to initialize the spl service! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
2019-04-21 17:27:33 +01:00
uiRefreshDisplay();
delay(5);
ret = -5;
}
2019-06-05 23:44:18 +01:00
/* Denitialize the csrng service */
csrngExit();
2019-04-21 17:27:33 +01:00
} else {
2019-06-05 23:44:18 +01:00
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to initialize the csrng service! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
2019-04-21 17:27:33 +01:00
uiRefreshDisplay();
delay(5);
ret = -4;
}
2019-06-05 23:44:18 +01:00
/* Denitialize the ns service */
nsExit();
2019-04-21 17:27:33 +01:00
} else {
2019-06-05 23:44:18 +01:00
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to initialize the ns service! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
2019-04-21 17:27:33 +01:00
uiRefreshDisplay();
delay(5);
ret = -3;
}
2019-06-05 23:44:18 +01:00
/* Denitialize the ncm service */
ncmExit();
2019-04-21 17:27:33 +01:00
} else {
2019-06-05 23:44:18 +01:00
snprintf(strbuf, sizeof(strbuf) / sizeof(strbuf[0]), "Failed to initialize the ncm service! (0x%08X)", result);
uiDrawString(strbuf, 8, 8, 255, 255, 255);
2019-04-21 17:27:33 +01:00
uiRefreshDisplay();
delay(5);
ret = -2;
}
2019-06-09 02:36:21 +01:00
/* Free global resources */
freeGlobalData();
2019-04-21 17:27:33 +01:00
/* Deinitialize UI */
uiDeinit();
return ret;
2018-05-15 13:57:40 +01:00
}