Skip to content

Commit

Permalink
utils: cleanup resource initialization code
Browse files Browse the repository at this point in the history
Other changes include:

* codebase: fix building with logging disabled.
* codebase: remove superfluous log level guards.

* defines: add SYSMMC_SUBDIR, EMUMMC_SUBDIR and SYSTEM_UPDATE_SUBDIR macros.

* poc: use subdirectory macros in output path generation code.
* poc: fix output directories for the eMMC FAT partition browser.
  • Loading branch information
DarkMatterCore committed Nov 2, 2024
1 parent f817ec5 commit ca61151
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 178 deletions.
52 changes: 29 additions & 23 deletions code_templates/nxdt_rw_poc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2615,7 +2615,7 @@ static bool saveGameCardImage(void *userdata)
}

snprintf(path, MAX_ELEMENTS(path), " [%s][%s][%s].xci", prepend_key_area ? "KA" : "NKA", keep_certificate ? "C" : "NC", trim_dump ? "T" : "NT");
filename = generateOutputGameCardFileName("Gamecard", path, true);
filename = generateOutputGameCardFileName(GAMECARD_SUBDIR, path, true);
if (!filename) goto end;

if (dev_idx == 1)
Expand Down Expand Up @@ -2740,7 +2740,7 @@ static bool saveGameCardHeader(void *userdata)
crc = crc32Calculate(&gc_header, sizeof(GameCardHeader));
snprintf(path, MAX_ELEMENTS(path), " (Header) (%08X).bin", crc);

filename = generateOutputGameCardFileName("Gamecard", path, true);
filename = generateOutputGameCardFileName(GAMECARD_SUBDIR, path, true);
if (!filename) goto end;

if (!saveFileData(filename, &gc_header, sizeof(GameCardHeader))) goto end;
Expand Down Expand Up @@ -2774,7 +2774,7 @@ static bool saveGameCardCardInfo(void *userdata)
crc = crc32Calculate(&gc_cardinfo, sizeof(GameCardInfo));
snprintf(path, MAX_ELEMENTS(path), " (CardInfo) (%08X).bin", crc);

filename = generateOutputGameCardFileName("Gamecard", path, true);
filename = generateOutputGameCardFileName(GAMECARD_SUBDIR, path, true);
if (!filename) goto end;

if (!saveFileData(filename, &gc_cardinfo, sizeof(GameCardInfo))) goto end;
Expand Down Expand Up @@ -2808,7 +2808,7 @@ static bool saveGameCardCertificate(void *userdata)
crc = crc32Calculate(&gc_cert, sizeof(FsGameCardCertificate));
snprintf(path, MAX_ELEMENTS(path), " (Certificate) (%08X).bin", crc);

filename = generateOutputGameCardFileName("Gamecard", path, true);
filename = generateOutputGameCardFileName(GAMECARD_SUBDIR, path, true);
if (!filename) goto end;

if (!saveFileData(filename, &gc_cert, sizeof(FsGameCardCertificate))) goto end;
Expand Down Expand Up @@ -2836,7 +2836,7 @@ static bool saveGameCardInitialData(void *userdata)
crc = crc32Calculate(&(gc_security_information.initial_data), sizeof(GameCardInitialData));
snprintf(path, MAX_ELEMENTS(path), " (Initial Data) (%08X).bin", crc);

filename = generateOutputGameCardFileName("Gamecard", path, true);
filename = generateOutputGameCardFileName(GAMECARD_SUBDIR, path, true);
if (!filename) goto end;

if (!saveFileData(filename, &(gc_security_information.initial_data), sizeof(GameCardInitialData))) goto end;
Expand Down Expand Up @@ -2869,7 +2869,7 @@ static bool saveGameCardSpecificData(void *userdata)
crc = crc32Calculate(&(gc_security_information.specific_data), sizeof(GameCardSpecificData));
snprintf(path, MAX_ELEMENTS(path), " (Specific Data) (%08X).bin", crc);

filename = generateOutputGameCardFileName("Gamecard", path, true);
filename = generateOutputGameCardFileName(GAMECARD_SUBDIR, path, true);
if (!filename) goto end;

if (!saveFileData(filename, &(gc_security_information.specific_data), sizeof(GameCardSpecificData))) goto end;
Expand Down Expand Up @@ -2901,7 +2901,7 @@ static bool saveGameCardIdSet(void *userdata)
crc = crc32Calculate(&id_set, sizeof(FsGameCardIdSet));
snprintf(path, MAX_ELEMENTS(path), " (Card ID Set) (%08X).bin", crc);

filename = generateOutputGameCardFileName("Gamecard", path, true);
filename = generateOutputGameCardFileName(GAMECARD_SUBDIR, path, true);
if (!filename) goto end;

if (!saveFileData(filename, &id_set, sizeof(FsGameCardIdSet))) goto end;
Expand Down Expand Up @@ -2934,7 +2934,7 @@ static bool saveGameCardUid(void *userdata)
crc = crc32Calculate(&(gc_security_information.specific_data.card_uid), sizeof(gc_security_information.specific_data.card_uid));
snprintf(path, MAX_ELEMENTS(path), " (Card UID) (%08X).bin", crc);

filename = generateOutputGameCardFileName("Gamecard", path, true);
filename = generateOutputGameCardFileName(GAMECARD_SUBDIR, path, true);
if (!filename) goto end;

if (!saveFileData(filename, &(gc_security_information.specific_data.card_uid), sizeof(gc_security_information.specific_data.card_uid))) goto end;
Expand Down Expand Up @@ -2996,7 +2996,7 @@ static bool saveGameCardRawHfsPartition(HashFileSystemContext *hfs_ctx)
consolePrint("raw %s hfs partition size: 0x%lX (%s)\n", hfs_ctx->name, hfs_ctx->size, size_str);

snprintf(path, MAX_ELEMENTS(path), "/%s.hfs0", hfs_ctx->name);
filename = generateOutputGameCardFileName("HFS/Raw", path, true);
filename = generateOutputGameCardFileName(HFS_SUBDIR "/Raw", path, true);
if (!filename) goto end;

if (dev_idx == 1)
Expand Down Expand Up @@ -3146,7 +3146,7 @@ static bool browseGameCardHfsPartition(void *userdata)

/* Generate output base path. */
snprintf(subdir, MAX_ELEMENTS(subdir), "/%s", hfs_ctx.name);
base_out_path = generateOutputGameCardFileName("HFS/Extracted", subdir, true);
base_out_path = generateOutputGameCardFileName(HFS_SUBDIR "/Extracted", subdir, true);
if (!base_out_path) goto end;

/* Display file browser. */
Expand Down Expand Up @@ -3413,7 +3413,7 @@ static bool saveTicket(void *userdata)
crc = crc32Calculate(tik.data, tik.size);
snprintf(path, MAX_ELEMENTS(path), " (%08X).tik", crc);

filename = generateOutputTitleFileName(title_info, "Ticket", path);
filename = generateOutputTitleFileName(title_info, TICKET_SUBDIR, path);
if (!filename) goto end;

if (!saveFileData(filename, tik.data, tik.size)) goto end;
Expand Down Expand Up @@ -3471,7 +3471,7 @@ static bool saveNintendoContentArchive(void *userdata)
utilsGenerateFormattedSizeString((double)shared_thread_data->total_size, size_str, sizeof(size_str));
consolePrint("nca size: 0x%lX (%s)\n", shared_thread_data->total_size, size_str);

snprintf(subdir, MAX_ELEMENTS(subdir), "NCA/%s", nca_thread_data.nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(subdir, MAX_ELEMENTS(subdir), NCA_SUBDIR "/%s", nca_thread_data.nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(path, MAX_ELEMENTS(path), "/%s.%s", nca_thread_data.nca_ctx->content_id_str, content_info->content_type == NcmContentType_Meta ? "cnmt.nca" : "nca");

filename = generateOutputTitleFileName(title_info, subdir, path);
Expand Down Expand Up @@ -3655,7 +3655,7 @@ static bool browseNintendoContentArchiveFsSection(void *userdata)

base_out_path = generateOutputLayeredFsFileName(title_id + nca_ctx->id_offset, NULL, section_type == NcaFsSectionType_PartitionFs ? "exefs" : "romfs");
} else {
snprintf(subdir, MAX_ELEMENTS(subdir), "NCA FS/%s/Extracted", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(subdir, MAX_ELEMENTS(subdir), NCA_FS_SUBDIR "/%s/Extracted", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(extension, MAX_ELEMENTS(extension), "/%s #%u/%u", titleGetNcmContentTypeName(nca_ctx->content_type), nca_ctx->id_offset, nca_fs_ctx->section_idx);

TitleInfo *title_info = (title_id == g_ncaUserTitleInfo->meta_key.id ? g_ncaUserTitleInfo : g_ncaBasePatchTitleInfo);
Expand Down Expand Up @@ -3725,19 +3725,25 @@ static bool saveSystemUpdateDump(void *userdata)
static bool browseEmmcPartition(void *userdata)
{
u8 bis_partition_id = (userdata ? *((u8*)userdata) : 0);
const char *mount_name = NULL;
const char *gpt_name = NULL, *mount_name = NULL;
char *base_out_path = NULL;

bool success = false;

if (bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System || !(mount_name = bisStorageGetMountNameByBisPartitionId(bis_partition_id)))
if (bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System)
{
consolePrint("invalid bis partition id! (%u)\n", bis_partition_id);
goto end;
}

if (!(gpt_name = bisStorageGetGptPartitionNameByBisPartitionId(bis_partition_id)) || !(mount_name = bisStorageGetMountNameByBisPartitionId(bis_partition_id)))
{
consolePrint("failed to get names for bis partition id! (%u)\n", bis_partition_id);
goto end;
}

/* Generate output base path. */
base_out_path = generateOutputGameCardFileName(utilsGetAtmosphereEmummcStatus() ? "emuMMC" : "sysMMC", mount_name, false);
base_out_path = generateOutputGameCardFileName(utilsGetAtmosphereEmummcStatus() ? EMUMMC_SUBDIR : SYSMMC_SUBDIR, gpt_name, false);
if (!base_out_path) goto end;

/* Display file browser. */
Expand Down Expand Up @@ -4437,7 +4443,7 @@ static bool saveRawPartitionFsSection(PartitionFileSystemContext *pfs_ctx, bool

filename = generateOutputLayeredFsFileName(title_id + nca_ctx->id_offset, NULL, "exefs.nsp");
} else {
snprintf(subdir, MAX_ELEMENTS(subdir), "NCA FS/%s/Raw", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(subdir, MAX_ELEMENTS(subdir), NCA_FS_SUBDIR "/%s/Raw", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(path, MAX_ELEMENTS(path), "/%s #%u/%u.nsp", titleGetNcmContentTypeName(nca_ctx->content_type), nca_ctx->id_offset, nca_fs_ctx->section_idx);

TitleInfo *title_info = (title_id == g_ncaUserTitleInfo->meta_key.id ? g_ncaUserTitleInfo : g_ncaBasePatchTitleInfo);
Expand Down Expand Up @@ -4597,7 +4603,7 @@ static bool saveRawRomFsSection(RomFileSystemContext *romfs_ctx, bool use_layere

filename = generateOutputLayeredFsFileName(title_id + nca_ctx->id_offset, NULL, "romfs.bin");
} else {
snprintf(subdir, MAX_ELEMENTS(subdir), "NCA FS/%s/Raw", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(subdir, MAX_ELEMENTS(subdir), NCA_FS_SUBDIR "/%s/Raw", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(path, MAX_ELEMENTS(path), "/%s #%u/%u.bin", titleGetNcmContentTypeName(nca_ctx->content_type), nca_ctx->id_offset, nca_fs_ctx->section_idx);

TitleInfo *title_info = (title_id == g_ncaUserTitleInfo->meta_key.id ? g_ncaUserTitleInfo : g_ncaBasePatchTitleInfo);
Expand Down Expand Up @@ -4896,7 +4902,7 @@ static void extractedHfsReadThreadFunc(void *arg)
buf2 = usbAllocatePageAlignedBuffer(BLOCK_SIZE);

snprintf(hfs_path, MAX_ELEMENTS(hfs_path), "/%s", hfs_ctx->name);
filename = generateOutputGameCardFileName("HFS/Extracted", hfs_path, true);
filename = generateOutputGameCardFileName(HFS_SUBDIR "/Extracted", hfs_path, true);
filename_len = (filename ? strlen(filename) : 0);

if (!shared_thread_data->total_size || !hfs_entry_count || !buf1 || !buf2 || !filename)
Expand Down Expand Up @@ -5278,7 +5284,7 @@ static void extractedPartitionFsReadThreadFunc(void *arg)

filename = generateOutputLayeredFsFileName(title_id + nca_ctx->id_offset, NULL, "exefs");
} else {
snprintf(subdir, MAX_ELEMENTS(subdir), "NCA FS/%s/Extracted", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(subdir, MAX_ELEMENTS(subdir), NCA_FS_SUBDIR "/%s/Extracted", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(pfs_path, MAX_ELEMENTS(pfs_path), "/%s #%u/%u", titleGetNcmContentTypeName(nca_ctx->content_type), nca_ctx->id_offset, nca_fs_ctx->section_idx);

TitleInfo *title_info = (title_id == g_ncaUserTitleInfo->meta_key.id ? g_ncaUserTitleInfo : g_ncaBasePatchTitleInfo);
Expand Down Expand Up @@ -5596,7 +5602,7 @@ static void extractedRomFsReadThreadFunc(void *arg)

filename = generateOutputLayeredFsFileName(title_id + nca_ctx->id_offset, NULL, "romfs");
} else {
snprintf(subdir, MAX_ELEMENTS(subdir), "NCA FS/%s/Extracted", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(subdir, MAX_ELEMENTS(subdir), NCA_FS_SUBDIR "/%s/Extracted", nca_ctx->storage_id == NcmStorageId_BuiltInSystem ? "System" : "User");
snprintf(romfs_path, MAX_ELEMENTS(romfs_path), "/%s #%u/%u", titleGetNcmContentTypeName(nca_ctx->content_type), nca_ctx->id_offset, nca_fs_ctx->section_idx);

TitleInfo *title_info = (title_id == g_ncaUserTitleInfo->meta_key.id ? g_ncaUserTitleInfo : g_ncaBasePatchTitleInfo);
Expand Down Expand Up @@ -6193,7 +6199,7 @@ static void systemUpdateReadThreadFunc(void *arg)

snprintf(sys_upd_path, MAX_ELEMENTS(sys_upd_path), "/%.*s (%s)", (int)sizeof(sys_upd_dump_ctx->version_file.display_title),
sys_upd_dump_ctx->version_file.display_title, utilsIsDevelopmentUnit() ? "Dev" : "Prod");
filename = generateOutputGameCardFileName("System Update", sys_upd_path, false);
filename = generateOutputGameCardFileName(SYSTEM_UPDATE_SUBDIR, sys_upd_path, false);
filename_len = (filename ? strlen(filename) : 0);

if (!shared_thread_data->total_size || !buf1 || !buf2 || !filename)
Expand Down Expand Up @@ -6627,7 +6633,7 @@ static void nspThreadFunc(void *arg)
}

/* Generate output path. */
filename = generateOutputTitleFileName(title_info, "NSP", ".nsp");
filename = generateOutputTitleFileName(title_info, NSP_SUBDIR, ".nsp");
if (!filename) goto end;

/* Get free space on output storage. */
Expand Down
9 changes: 9 additions & 0 deletions include/core/nxdt_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ void logControlMutex(bool lock);
#define LOG_MSG_BUF_ERROR(dst, dst_size, fmt, ...) do {} while(0)
#define LOG_DATA_ERROR(data, data_size, fmt, ...) do {} while(0)

#define logWriteStringToLogFile(...) do {} while(0)
#define logWriteFormattedStringToLogFile(...) do {} while(0)
#define logWriteFormattedStringToBuffer(...) do {} while(0)
#define logWriteBinaryDataToLogFile(...) do {} while(0)
#define logFlushLogFile(...) do {} while(0)
#define logCloseLogFile(...) do {} while(0)
#define logGetLastMessage(...) NULL
#define logControlMutex(...) do {} while(0)

#endif /* (LOG_LEVEL >= LOG_LEVEL_DEBUG) && (LOG_LEVEL < LOG_LEVEL_NONE) */

#ifdef __cplusplus
Expand Down
6 changes: 3 additions & 3 deletions include/core/nxdt_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ void utilsCloseResources(void);
/// Returns a pointer to the application launch path.
const char *utilsGetLaunchPath(void);

/// Returns the nxlink socket descriptor, or -1 if an nxlink connection couldn't be established.
int utilsGetNxLinkFileDescriptor(void);

/// Returns a pointer to the FsFileSystem object for the SD card.
FsFileSystem *utilsGetSdCardFileSystemObject(void);

/// Returns the nxlink socket descriptor, or -1 if an nxlink connection couldn't be established.
int utilsGetNxLinkFileDescriptor(void);

/// Commits SD card filesystem changes.
/// Must be used after closing a file handle from the SD card.
bool utilsCommitSdCardFileSystemChanges(void);
Expand Down
3 changes: 3 additions & 0 deletions include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
#define TICKET_SUBDIR "Ticket"
#define NCA_SUBDIR "NCA"
#define NCA_FS_SUBDIR "NCA FS"
#define SYSMMC_SUBDIR "sysMMC"
#define EMUMMC_SUBDIR "emuMMC"
#define SYSTEM_UPDATE_SUBDIR "System Update"

#define CONFIG_FILE_NAME APP_TITLE "_config.json"
#define DEFAULT_CONFIG_PATH "romfs:/default_config.json"
Expand Down
8 changes: 0 additions & 8 deletions source/core/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ static bool memRetrieveProgramMemory(MemoryLocation *location, bool is_segment)
/* Clear output MemoryLocation element. */
memFreeMemoryLocation(location);

#if LOG_LEVEL < LOG_LEVEL_NONE
/* LOG_*() macros will be useless if the target program is the FS sysmodule. */
/* This is because any FS I/O operation *will* lock up the console while FS itself is being debugged. */
/* So we'll just log data to a temporary buffer using LOG_MSG_BUF_*() macros, then write it all out to the logfile after calling svcCloseHandle(). */
/* However, we must prevent other threads from logging data as well in order to avoid a lock up, so we'll temporarily lock the logfile mutex. */
/* We don't need to take care of manually (re)allocating memory for our buffer -- the log handler ABI takes care of that for us. */
logControlMutex(true);
#endif

/* Retrieve debug handle by program ID. */
if (!memRetrieveDebugHandleFromProgramById(&debug_handle, location->program_id))
Expand Down Expand Up @@ -139,7 +137,6 @@ static bool memRetrieveProgramMemory(MemoryLocation *location, bool is_segment)
/* Filter out unwanted memory pages. */
if (MEM_INVALID_SEGMENT_PAGE_TYPE(mem_type) || mem_info.attr || (mem_info.perm & Perm_Rx) != Perm_Rx) continue;

#if LOG_LEVEL == LOG_LEVEL_DEBUG
MEMLOG_DEBUG("svcQueryDebugProcessMemory info (FS .text segment lookup) (program %016lX, page 0x%X, debug handle 0x%X):\r\n" \
"- addr: 0x%lX\r\n" \
"- size: 0x%lX\r\n" \
Expand All @@ -150,7 +147,6 @@ static bool memRetrieveProgramMemory(MemoryLocation *location, bool is_segment)
"- device_refcount: 0x%X", \
location->program_id, page_info, debug_handle, mem_info.addr, mem_info.size, mem_info.type, mem_info.attr, mem_info.perm, \
mem_info.ipc_refcount, mem_info.device_refcount);
#endif

/* Update .text segment address. */
last_text_addr = mem_info.addr;
Expand Down Expand Up @@ -182,7 +178,6 @@ static bool memRetrieveProgramMemory(MemoryLocation *location, bool is_segment)
(is_segment && (MEM_INVALID_SEGMENT_PAGE_TYPE(mem_type) || !(((segment <<= 1) >> 1) & location->mask))) || \
(!is_segment && location->program_id == FS_SYSMODULE_TID && MEM_INVALID_FS_PAGE_TYPE(mem_type))) continue;

#if LOG_LEVEL == LOG_LEVEL_DEBUG
MEMLOG_DEBUG("svcQueryDebugProcessMemory info (program %016lX, page 0x%X, debug handle 0x%X):\r\n" \
"- addr: 0x%lX\r\n" \
"- size: 0x%lX\r\n" \
Expand All @@ -193,7 +188,6 @@ static bool memRetrieveProgramMemory(MemoryLocation *location, bool is_segment)
"- device_refcount: 0x%X", \
location->program_id, page_info, debug_handle, mem_info.addr, mem_info.size, mem_info.type, mem_info.attr, mem_info.perm, \
mem_info.ipc_refcount, mem_info.device_refcount);
#endif

/* Reallocate data buffer. */
tmp = realloc(location->data, location->data_size + mem_info.size);
Expand Down Expand Up @@ -224,10 +218,8 @@ static bool memRetrieveProgramMemory(MemoryLocation *location, bool is_segment)
/* Close debug handle. */
if (debug_handle != INVALID_HANDLE) svcCloseHandle(debug_handle);

#if LOG_LEVEL < LOG_LEVEL_NONE
/* Unlock logfile mutex. */
logControlMutex(false);
#endif

if (success && (!location->data || !location->data_size))
{
Expand Down
Loading

0 comments on commit ca61151

Please sign in to comment.