2021-06-23 19:27:06 +01:00
|
|
|
/*
|
|
|
|
* nxdt_bfsar.c
|
|
|
|
*
|
2023-04-08 12:42:22 +01:00
|
|
|
* Copyright (c) 2020-2023, DarkMatterCore <pabloacurielz@gmail.com>.
|
2021-06-23 19:27:06 +01:00
|
|
|
*
|
|
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
|
|
*
|
|
|
|
* nxdumptool is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* nxdumptool is distributed in the hope that 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nxdt_utils.h"
|
|
|
|
#include "nxdt_bfsar.h"
|
|
|
|
#include "romfs.h"
|
|
|
|
#include "title.h"
|
|
|
|
|
|
|
|
#define BFSAR_FILENAME "qlaunch.bfsar"
|
|
|
|
#define BFSAR_ROMFS_PATH "/sound/" BFSAR_FILENAME
|
|
|
|
|
|
|
|
/* Global variables. */
|
|
|
|
|
|
|
|
static Mutex g_bfsarMutex = 0;
|
|
|
|
static bool g_bfsarInterfaceInit = false;
|
|
|
|
|
|
|
|
static char g_bfsarPath[FS_MAX_PATH] = {0};
|
|
|
|
|
|
|
|
bool bfsarInitialize(void)
|
|
|
|
{
|
|
|
|
bool use_root = true;
|
|
|
|
const char *launch_path = utilsGetLaunchPath();
|
|
|
|
char *ptr1 = NULL, *ptr2 = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
TitleInfo *title_info = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
NcaContext *nca_ctx = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
RomFileSystemContext romfs_ctx = {0};
|
|
|
|
RomFileSystemFileEntry *romfs_file_entry = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
FILE *bfsar_file = NULL;
|
|
|
|
u8 *bfsar_data = NULL;
|
|
|
|
size_t bfsar_size = 0, wr = 0;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
bool ret = false;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
SCOPED_LOCK(&g_bfsarMutex)
|
|
|
|
{
|
|
|
|
ret = g_bfsarInterfaceInit;
|
|
|
|
if (ret) break;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Generate BFSAR file path. */
|
|
|
|
if (launch_path)
|
|
|
|
{
|
|
|
|
ptr1 = strchr(launch_path, '/');
|
|
|
|
ptr2 = strrchr(launch_path, '/');
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
if (ptr1 && ptr2 && ptr1 != ptr2)
|
|
|
|
{
|
|
|
|
/* Create BFSAR file in the current working directory. */
|
2023-05-24 20:05:34 +01:00
|
|
|
snprintf(g_bfsarPath, sizeof(g_bfsarPath), "%.*s" BFSAR_FILENAME, (int)((ptr2 - launch_path) + 1), launch_path);
|
2021-06-23 19:27:06 +01:00
|
|
|
use_root = false;
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Create BFSAR file in the SD card root directory. */
|
2023-05-24 20:05:34 +01:00
|
|
|
if (use_root) sprintf(g_bfsarPath, DEVOPTAB_SDMC_DEVICE "/" BFSAR_FILENAME);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_DEBUG("BFSAR path: \"%s\".", g_bfsarPath);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Check if the BFSAR file is already available and not empty. */
|
|
|
|
bfsar_file = fopen(g_bfsarPath, "rb");
|
|
|
|
if (bfsar_file)
|
|
|
|
{
|
|
|
|
fseek(bfsar_file, 0, SEEK_END);
|
|
|
|
bfsar_size = ftell(bfsar_file);
|
|
|
|
if (bfsar_size)
|
|
|
|
{
|
|
|
|
ret = g_bfsarInterfaceInit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Get title info. */
|
|
|
|
if (!(title_info = titleGetInfoFromStorageByTitleId(NcmStorageId_BuiltInSystem, QLAUNCH_TID)))
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to get title info for qlaunch!");
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Allocate memory for a temporary NCA context. */
|
|
|
|
nca_ctx = calloc(1, sizeof(NcaContext));
|
|
|
|
if (!nca_ctx)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to allocate memory for temporary NCA context!");
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Initialize NCA context. */
|
2023-05-24 20:05:34 +01:00
|
|
|
/* Don't allow invalid NCA signatures. */
|
2023-05-30 00:22:12 +01:00
|
|
|
if (!ncaInitializeContext(nca_ctx, NcmStorageId_BuiltInSystem, 0, &(title_info->meta_key), titleGetContentInfoByTypeAndIdOffset(title_info, NcmContentType_Program, 0), NULL) || \
|
2023-05-24 20:05:34 +01:00
|
|
|
!nca_ctx->valid_main_signature)
|
2021-06-23 19:27:06 +01:00
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to initialize qlaunch Program NCA context!");
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Initialize RomFS context. */
|
2022-07-04 13:30:48 +01:00
|
|
|
if (!romfsInitializeContext(&romfs_ctx, &(nca_ctx->fs_ctx[1]), NULL))
|
2021-06-23 19:27:06 +01:00
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to initialize RomFS context for qlaunch Program NCA!");
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Get RomFS file entry. */
|
|
|
|
if (!(romfs_file_entry = romfsGetFileEntryByPath(&romfs_ctx, BFSAR_ROMFS_PATH)))
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to retrieve RomFS file entry for \"" BFSAR_ROMFS_PATH "\"!");
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Check file size. */
|
|
|
|
bfsar_size = romfs_file_entry->size;
|
|
|
|
if (!bfsar_size)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("File size for qlaunch's \"" BFSAR_ROMFS_PATH "\" is zero!");
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Allocate memory for BFSAR data. */
|
|
|
|
if (!(bfsar_data = malloc(bfsar_size)))
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to allocate 0x%lX bytes for qlaunch's \"" BFSAR_ROMFS_PATH "\"!", bfsar_size);
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Read BFSAR data. */
|
|
|
|
if (!romfsReadFileEntryData(&romfs_ctx, romfs_file_entry, bfsar_data, bfsar_size, 0))
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to read 0x%lX bytes long \"" BFSAR_ROMFS_PATH "\" from qlaunch!", bfsar_size);
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Create BFSAR file. */
|
|
|
|
bfsar_file = fopen(g_bfsarPath, "wb");
|
|
|
|
if (!bfsar_file)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to open \"%s\" for writing!", g_bfsarPath);
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Write BFSAR data. */
|
|
|
|
wr = fwrite(bfsar_data, 1, bfsar_size, bfsar_file);
|
|
|
|
if (wr != bfsar_size)
|
|
|
|
{
|
2022-07-12 17:34:49 +01:00
|
|
|
LOG_MSG_ERROR("Failed to write 0x%lX bytes block to \"%s\"!", bfsar_size, g_bfsarPath);
|
2021-06-23 19:27:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
/* Update flags. */
|
|
|
|
ret = g_bfsarInterfaceInit = true;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-29 17:48:32 +01:00
|
|
|
if (bfsar_file)
|
|
|
|
{
|
|
|
|
fclose(bfsar_file);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-07-29 17:48:32 +01:00
|
|
|
/* Commit SD card filesystem changes. */
|
|
|
|
utilsCommitSdCardFileSystemChanges();
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
if (bfsar_data) free(bfsar_data);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
romfsFreeContext(&romfs_ctx);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
if (nca_ctx) free(nca_ctx);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
titleFreeTitleInfo(&title_info);
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bfsarExit(void)
|
|
|
|
{
|
|
|
|
SCOPED_LOCK(&g_bfsarMutex)
|
|
|
|
{
|
|
|
|
/* Clear BFSAR file path. */
|
|
|
|
*g_bfsarPath = '\0';
|
|
|
|
g_bfsarInterfaceInit = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *bfsarGetFilePath(void)
|
|
|
|
{
|
|
|
|
const char *ret = NULL;
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
SCOPED_TRY_LOCK(&g_bfsarMutex)
|
|
|
|
{
|
|
|
|
if (g_bfsarInterfaceInit) ret = (const char*)g_bfsarPath;
|
|
|
|
}
|
2022-07-05 02:04:28 +01:00
|
|
|
|
2021-06-23 19:27:06 +01:00
|
|
|
return ret;
|
|
|
|
}
|