1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-10-30 15:31:46 +00:00
nxdumptool/source/utils.c

305 lines
8 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-15 21:50:07 +01:00
#include <stdio.h>
#include <stdlib.h>
2020-04-15 21:50:07 +01:00
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <switch.h>
//#include "freetype_helper.h"
//#include "lvgl_helper.h"
2020-04-16 11:13:11 +01:00
#include "keys.h"
2020-04-15 21:50:07 +01:00
#include "gamecard.h"
2020-04-16 11:13:11 +01:00
#include "services.h"
2020-04-15 21:50:07 +01:00
#include "utils.h"
#include "nca.h"
#include "fatfs/ff.h"
2020-04-15 21:50:07 +01:00
/* Global variables. */
2020-04-16 11:13:11 +01:00
static u8 g_customFirmwareType = UtilsCustomFirmwareType_Unknown;
2020-04-15 21:50:07 +01:00
static AppletHookCookie g_systemOverclockCookie = {0};
static Mutex g_logfileMutex = 0;
static FsStorage g_emmcBisSystemPartitionStorage = {0};
2020-04-19 23:44:22 +01:00
static FATFS *g_emmcBisSystemPartitionFatFsObj = NULL;
2020-04-15 21:50:07 +01:00
/* Function prototypes. */
2020-04-16 11:13:11 +01:00
static void _utilsGetCustomFirmwareType(void);
2020-04-15 21:50:07 +01:00
static void utilsOverclockSystemAppletHook(AppletHookType hook, void *param);
static bool utilsMountEmmcBisSystemPartitionStorage(void);
static void utilsUnmountEmmcBisSystemPartitionStorage(void);
2020-04-15 21:50:07 +01:00
u64 utilsHidKeysAllDown(void)
{
u8 controller;
u64 keys_down = 0;
for(controller = 0; controller < (u8)CONTROLLER_P1_AUTO; controller++) keys_down |= hidKeysDown((HidControllerID)controller);
return keys_down;
}
u64 utilsHidKeysAllHeld(void)
{
u8 controller;
u64 keys_held = 0;
for(controller = 0; controller < (u8)CONTROLLER_P1_AUTO; controller++) keys_held |= hidKeysHeld((HidControllerID)controller);
return keys_held;
}
void utilsWaitForButtonPress(void)
{
u64 flag, keys_down;
/* Don't consider touch screen presses nor stick movement as button inputs */
flag = ~(KEY_TOUCH | KEY_LSTICK_LEFT | KEY_LSTICK_RIGHT | KEY_LSTICK_UP | KEY_LSTICK_DOWN | KEY_RSTICK_LEFT | KEY_RSTICK_RIGHT | KEY_RSTICK_UP | KEY_RSTICK_DOWN);
while(appletMainLoop())
{
hidScanInput();
keys_down = utilsHidKeysAllDown();
if (keys_down & flag) break;
}
}
void utilsWriteLogMessage(const char *func_name, const char *fmt, ...)
{
mutexLock(&g_logfileMutex);
va_list args;
FILE *logfile = NULL;
logfile = fopen(APP_BASE_PATH "nxdumptool.log", "a+");
if (!logfile) goto out;
time_t now = time(NULL);
struct tm *ts = localtime(&now);
2020-04-16 01:06:41 +01:00
fprintf(logfile, "%d/%d/%d %d:%02d:%02d -> %s: ", ts->tm_year + 1900, ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, func_name);
2020-04-15 21:50:07 +01:00
va_start(args, fmt);
vfprintf(logfile, fmt, args);
va_end(args);
fprintf(logfile, "\r\n");
fclose(logfile);
out:
mutexUnlock(&g_logfileMutex);
}
void utilsOverclockSystem(bool restore)
{
u32 cpuClkRate = ((restore ? CPU_CLKRT_NORMAL : CPU_CLKRT_OVERCLOCKED) * 1000000);
u32 memClkRate = ((restore ? MEM_CLKRT_NORMAL : MEM_CLKRT_OVERCLOCKED) * 1000000);
servicesChangeHardwareClockRates(cpuClkRate, memClkRate);
}
bool utilsInitializeResources(void)
{
Result rc = 0;
2020-04-16 11:13:11 +01:00
/* Initialize needed services */
if (!servicesInitialize())
{
LOGFILE("Failed to initialize needed services!");
return false;
}
/* Load NCA keyset */
if (!keysLoadNcaKeyset())
{
LOGFILE("Failed to load NCA keyset!");
return false;
}
2020-04-15 21:50:07 +01:00
/* Allocate NCA crypto buffer */
if (!ncaAllocateCryptoBuffer())
{
LOGFILE("Unable to allocate memory for NCA crypto buffer!");
return false;
}
/* Initialize gamecard interface */
rc = gamecardInitialize();
if (R_FAILED(rc))
{
LOGFILE("Failed to initialize gamecard interface!");
return false;
}
/* Mount eMMC BIS System partition */
if (!utilsMountEmmcBisSystemPartitionStorage()) return false;
2020-04-15 21:50:07 +01:00
/* Initialize FreeType */
//if (!freeTypeHelperInitialize()) return false;
/* Initialize LVGL */
//if (!lvglHelperInitialize()) return false;
2020-04-16 11:13:11 +01:00
/* Retrieve custom firmware type */
_utilsGetCustomFirmwareType();
2020-04-15 21:50:07 +01:00
/* Overclock system */
utilsOverclockSystem(false);
/* Setup an applet hook to change the hardware clocks after a system mode change (docked <-> undocked) */
appletHook(&g_systemOverclockCookie, utilsOverclockSystemAppletHook, NULL);
return true;
}
void utilsCloseResources(void)
{
/* Unset our overclock applet hook */
appletUnhook(&g_systemOverclockCookie);
/* Restore hardware clocks */
utilsOverclockSystem(true);
/* Free LVGL resources */
//lvglHelperExit();
/* Free FreeType resouces */
//freeTypeHelperExit();
/* Unmount eMMC BIS System partition */
utilsUnmountEmmcBisSystemPartitionStorage();
/* Deinitialize gamecard interface */
gamecardExit();
/* Free NCA crypto buffer */
ncaFreeCryptoBuffer();
2020-04-15 21:50:07 +01:00
/* Close initialized services */
servicesClose();
}
2020-04-16 11:13:11 +01:00
u8 utilsGetCustomFirmwareType(void)
{
return g_customFirmwareType;
}
void utilsGenerateHexStringFromData(char *dst, size_t dst_size, const void *src, size_t src_size)
{
if (!src || !src_size || !dst || dst_size < ((src_size * 2) + 1)) return;
size_t i, j;
const u8 *src_u8 = (const u8*)src;
for(i = 0, j = 0; i < src_size; i++)
{
2020-04-19 23:44:22 +01:00
char h_nib = ((src_u8[i] >> 4) & 0xF);
char l_nib = (src_u8[i] & 0xF);
2020-04-19 23:44:22 +01:00
dst[j++] = (h_nib + (h_nib < 0xA ? 0x30 : 0x57));
dst[j++] = (l_nib + (l_nib < 0xA ? 0x30 : 0x57));
}
dst[j] = '\0';
}
FsStorage *utilsGetEmmcBisSystemPartitionStorage(void)
{
return &g_emmcBisSystemPartitionStorage;
}
2020-04-16 11:13:11 +01:00
static void _utilsGetCustomFirmwareType(void)
{
bool tx_srv = servicesCheckRunningServiceByName("tx");
bool rnx_srv = servicesCheckRunningServiceByName("rnx");
if (!tx_srv && !rnx_srv)
{
/* Atmosphere */
g_customFirmwareType = UtilsCustomFirmwareType_Atmosphere;
} else
if (tx_srv && !rnx_srv)
{
/* SX OS */
g_customFirmwareType = UtilsCustomFirmwareType_SXOS;
} else {
/* ReiNX */
g_customFirmwareType = UtilsCustomFirmwareType_ReiNX;
}
}
2020-04-15 21:50:07 +01:00
static void utilsOverclockSystemAppletHook(AppletHookType hook, void *param)
{
(void)param;
if (hook != AppletHookType_OnOperationMode && hook != AppletHookType_OnPerformanceMode) return;
/* To do: read config here to actually know the value to use with utilsOverclockSystem */
utilsOverclockSystem(true);
}
static bool utilsMountEmmcBisSystemPartitionStorage(void)
{
Result rc = 0;
FRESULT fr = FR_OK;
rc = fsOpenBisStorage(&g_emmcBisSystemPartitionStorage, FsBisPartitionId_System);
if (R_FAILED(rc))
{
LOGFILE("Failed to open eMMC BIS System partition storage! (0x%08X)", rc);
return false;
}
2020-04-19 23:44:22 +01:00
g_emmcBisSystemPartitionFatFsObj = calloc(1, sizeof(FATFS));
if (!g_emmcBisSystemPartitionFatFsObj)
{
LOGFILE("Unable to allocate memory for FatFs object!");
return false;
}
2020-04-19 23:44:22 +01:00
fr = f_mount(g_emmcBisSystemPartitionFatFsObj, BIS_SYSTEM_PARTITION_MOUNT_NAME, 1);
if (fr != FR_OK)
{
LOGFILE("Failed to mount eMMC BIS System partition! (%u)", fr);
return false;
}
return true;
}
static void utilsUnmountEmmcBisSystemPartitionStorage(void)
{
2020-04-19 23:44:22 +01:00
if (g_emmcBisSystemPartitionFatFsObj)
{
f_unmount(BIS_SYSTEM_PARTITION_MOUNT_NAME);
2020-04-19 23:44:22 +01:00
free(g_emmcBisSystemPartitionFatFsObj);
g_emmcBisSystemPartitionFatFsObj = NULL;
}
if (serviceIsActive(&(g_emmcBisSystemPartitionStorage.s)))
{
fsStorageClose(&g_emmcBisSystemPartitionStorage);
memset(&g_emmcBisSystemPartitionStorage, 0, sizeof(FsStorage));
}
}