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

Add a menu, improvements

This commit is contained in:
MCMrARM 2018-05-15 18:00:19 +02:00
parent e3f450ba7b
commit f4b900d0f6
8 changed files with 220 additions and 68 deletions

9
source/ccolor.h Normal file
View file

@ -0,0 +1,9 @@
#pragma once
#define C_RESET "\x1b[0m"
#define C_DIM "\x1b[2m"
#define C_RED "\x1b[31m"
#define C_GREEN "\x1b[32m"
#define C_CYAN "\x1b[36m"
#define C_INVERT "\x1b[7m"
#define C_CLEAR_LINE "\x1b[2K"

65
source/dumper.c Normal file
View file

@ -0,0 +1,65 @@
#include "dumper.h"
#include "fsext.h"
#include <stdio.h>
#include <malloc.h>
#include "ccolor.h"
#include "util.h"
bool dumpPartitionRaw(FsDeviceOperator* fsOperator, u32 partition) {
u32 handle;
if (R_FAILED(fsDeviceOperatorGetGameCardHandle(fsOperator, &handle))) {
printf("GetGameCardHandle failed\n");
return false;
}
FsStorage gameCardStorage;
Result result;
if (R_FAILED(result = fsOpenGameCard(&gameCardStorage, handle, partition))) {
printf("MountGameCard failed %x\n", result);
return false;
}
printf("Opened card\n");
u64 size;
fsStorageGetSize(&gameCardStorage, &size);
printf("Total size = %li\n", size);
FILE* outFile = fopen("out.bin", "wb");
if (!outFile) {
printf("Failed to open output file!\n");
fsStorageClose(&gameCardStorage);
return false;
}
printf("Starting...");
syncDisplay();
const size_t bufs = 1024 * 1024;
char* buf = (char*) malloc(bufs);
for (u64 off = 0; off < size; off += bufs) {
u64 n = bufs;
if (size - off < n)
n = size - off;
if (R_FAILED(result = fsStorageRead(&gameCardStorage, off, buf, n))) {
printf("fsStorageRead error\n");
fsStorageClose(&gameCardStorage);
return false;
}
if (fwrite(buf, 1, n, outFile) != n) {
printf("fwrite error\n");
fsStorageClose(&gameCardStorage);
return false;
}
if (((off / bufs) % 10) == 0) {
printf(C_CLEAR_LINE "\rDumping %i%% [%li / %li bytes]", (int) (off * 100 / size), off, size);
syncDisplay();
}
}
free(buf);
printf(C_CLEAR_LINE "\rDone!\n");
syncDisplay();
fclose(outFile);
fsStorageClose(&gameCardStorage);
return true;
}

5
source/dumper.h Normal file
View file

@ -0,0 +1,5 @@
#pragma once
#include <switch.h>
bool dumpPartitionRaw(FsDeviceOperator* fsOperator, u32 partition);

View file

@ -1,102 +1,70 @@
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <switch.h>
#include "fsext.h"
#include "menu.h"
#include "dumper.h"
#include "ccolor.h"
bool isGameCardInserted(FsDeviceOperator* o) {
bool inserted;
if (R_FAILED(fsDeviceOperatorIsGameCardInserted(o, &inserted)))
return false;
return inserted;
FsDeviceOperator fsOperatorInstance;
bool shouldWaitForAnyButton = false;
void menuWaitForAnyButton() {
printf(C_DIM "Press any button to return to menu\n");
shouldWaitForAnyButton = true;
}
bool doLogic(FsDeviceOperator* fsOperator) {
bool cardInserted = isGameCardInserted(fsOperator);
printf("IsGameCardInserted = %i\n", cardInserted);
if (!cardInserted)
return false;
u32 handle;
if (R_FAILED(fsDeviceOperatorGetGameCardHandle(fsOperator, &handle))) {
printf("GetGameCardHandle failed\n");
return false;
}
FsStorage gameCardStorage;
Result result;
if (R_FAILED(result = fsOpenGameCard(&gameCardStorage, handle, 0))) {
printf("MountGameCard failed %i\n", result);
return false;
}
printf("Opened card\n");
u64 size;
fsStorageGetSize(&gameCardStorage, &size);
printf("Size = %li\n", size);
FILE* outFile = fopen("out.bin", "wb");
printf("Opened output file\n");
const size_t bufs = 1024 * 1024;
char* buf = (char*) malloc(bufs);
for (u64 off = 0; off < size; off += bufs) {
u64 n = bufs;
if (size - off < n)
n = size - off;
if (R_FAILED(result = fsStorageRead(&gameCardStorage, off, buf, n))) {
printf("fsStorageRead error\n");
fsStorageClose(&gameCardStorage);
return false;
}
if (fwrite(buf, 1, n, outFile) != n) {
printf("fwrite error\n");
fsStorageClose(&gameCardStorage);
return false;
}
printf("Dumping %i%% [%li / %li]\n", (int) (off * 100 / size), off, size);
}
free(buf);
fclose(outFile);
fsStorageClose(&gameCardStorage);
printf("Done!\n");
return true;
void startOperation(const char* title) {
consoleClear();
printf(C_DIM "%s\n\n" C_RESET, title);
}
void dumpPartitionZero() {
startOperation("Raw Dump Partition 0 (SysUpdate)");
dumpPartitionRaw(&fsOperatorInstance, 0);
menuWaitForAnyButton();
}
MenuItem mainMenu[] = {
{ .text = "Raw Dump Partition 0 (SysUpdate)", .callback = dumpPartitionZero },
{ .text = NULL }
};
int main(int argc, char **argv)
{
gfxInitDefault();
consoleInit(NULL);
FsDeviceOperator fsOperator;
if (R_FAILED(fsOpenDeviceOperator(&fsOperator))) {
if (R_FAILED(fsOpenDeviceOperator(&fsOperatorInstance))) {
printf("Failed to open device operator\n");
return -1;
}
doLogic(&fsOperator);
fsDeviceOperatorClose(&fsOperator);
menuSetCurrent(mainMenu);
while(appletMainLoop())
{
bool btnWait = shouldWaitForAnyButton;
hidScanInput();
//
if (!btnWait)
menuUpdate(&fsOperatorInstance);
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break;
if (btnWait && kDown) {
shouldWaitForAnyButton = false;
menuPrint();
}
gfxFlushBuffers();
gfxSwapBuffers();
gfxWaitForVsync();
}
fsDeviceOperatorClose(&fsOperatorInstance);
gfxExit();
return 0;
}

73
source/menu.c Normal file
View file

@ -0,0 +1,73 @@
#include "menu.h"
#include <stdio.h>
#include "ccolor.h"
#include "util.h"
MenuItem* menuCurrent;
int menuCurrentCount;
bool menuCardIsInserted;
int menuSelIndex = 0;
void menuPrint() {
consoleClear();
printf(C_CYAN "Game Card dump tool" C_RESET "\n");
printf(menuCardIsInserted ? C_GREEN "Game Card is inserted\n" : C_RED "Game Card is NOT inserted\n");
printf(C_RESET "\n");
int index = 0;
MenuItem* menuItems = menuCurrent;
while (menuItems->text) {
if (index == menuSelIndex)
printf(C_INVERT "%s" C_RESET "\n", menuItems->text);
else
printf("%s\n", menuItems->text);
menuItems++;
index++;
}
}
bool menuHandleGameCardStatus(FsDeviceOperator* fsOperator) {
bool cardInserted = isGameCardInserted(fsOperator);
if (menuCardIsInserted != cardInserted) {
menuCardIsInserted = cardInserted;
return true;
}
return false;
}
int menuHandleInput() {
bool needsRefresh = false;
u64 keys = hidKeysDown(CONTROLLER_P1_AUTO);
if ((keys & KEY_A) && menuCurrent[menuSelIndex].callback != NULL) {
menuCurrent[menuSelIndex].callback();
return -1;
}
if (((keys & KEY_RSTICK_UP) | (keys & KEY_LSTICK_UP)) && menuSelIndex > 0) {
menuSelIndex--;
needsRefresh = true;
}
if (((keys & KEY_RSTICK_DOWN) | (keys & KEY_LSTICK_DOWN)) && menuSelIndex + 1 < menuCurrentCount) {
menuSelIndex++;
needsRefresh = true;
}
return needsRefresh ? 1 : 0;
}
void menuSetCurrent(MenuItem* menuItems) {
menuCurrent = menuItems;
menuCurrentCount = 0;
while ((menuItems++)->text != NULL)
menuCurrentCount++;
menuSelIndex = 0;
menuPrint();
}
void menuUpdate(FsDeviceOperator* fsOperator) {
int inputStatus = menuHandleInput();
if (inputStatus == -1)
return;
if (inputStatus || menuHandleGameCardStatus(fsOperator))
menuPrint();
}

12
source/menu.h Normal file
View file

@ -0,0 +1,12 @@
#pragma once
#include <switch.h>
typedef struct {
const char* text;
void (*callback)();
} MenuItem;
void menuPrint();
void menuSetCurrent(MenuItem* menuItems);
void menuUpdate(FsDeviceOperator* fsOperator);

14
source/util.c Normal file
View file

@ -0,0 +1,14 @@
#include "util.h"
#include "fsext.h"
bool isGameCardInserted(FsDeviceOperator* o) {
bool inserted;
if (R_FAILED(fsDeviceOperatorIsGameCardInserted(o, &inserted)))
return false;
return inserted;
}
void syncDisplay() {
gfxFlushBuffers();
gfxSwapBuffers();
gfxWaitForVsync();
}

6
source/util.h Normal file
View file

@ -0,0 +1,6 @@
#pragma once
#include <switch.h>
bool isGameCardInserted(FsDeviceOperator* o);
void syncDisplay();