From 94f0312566e7c910c7ed4331fef60310fb02ccf5 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Fri, 1 Nov 2024 13:54:37 +0100 Subject: [PATCH] bis_storage: add QoL improvements The BIS storage interface is now initialized by utilsInitializeResources() via bisStorageInitialize(). It takes care of mounting all eMMC FAT partitions in read-only mode, making it possible for the rest of the code to interact with them at any time without having to manually mount them beforehand. Other changes include: * bis_storage: add bisStorageExit(). * bis_storage: add bisStorageGetGptPartitionNameByBisPartitionId(), bisStorageGetSystemInitializerPartitionNameByBisPartitionId() and bisStorageGetMountNameByBisPartitionId() helpers. * bis_storage: change bisStorageMountPartition(), bisStorageUnmountPartition() and bisStorageUnmountAllPartitions() to static functions. * bis_storage: mount FAT partitions with FatFs by using drive numbers instead of arbitrary strings. * cert: update code to reflect BIS storage interface changes. * defines: add BIS_FAT_PARTITION_COUNT macro. * fatfs/ffconf: use BIS_FAT_PARTITION_COUNT macro as the value for the FF_VOLUMES macro. * fatfs/ffconf: disable arbitrary string support for volume IDs. * nxdt_devoptab: increase concurrent mounted device count to 8. * nxdt_utils: add missing underscores to anonymous variables in SCOPED_LOCK and SCOPED_TRY_LOCK macros. * nxdt_utils: take care of BIS storage interface (de)initialization. * poc: update code to reflect BIS storage interface changes. * tik: update code to reflect BIS storage interface changes. --- code_templates/nxdt_rw_poc.c | 11 +- include/core/bis_storage.h | 28 ++- include/core/fatfs/ffconf.h | 6 +- include/core/nxdt_utils.h | 6 +- include/defines.h | 2 + source/core/bis_storage.c | 301 +++++++++++++++------------ source/core/cert.c | 32 +-- source/core/devoptab/nxdt_devoptab.c | 10 +- source/core/nxdt_utils.c | 9 +- source/core/tik.c | 12 +- 10 files changed, 224 insertions(+), 193 deletions(-) diff --git a/code_templates/nxdt_rw_poc.c b/code_templates/nxdt_rw_poc.c index 351099a..fc755df 100644 --- a/code_templates/nxdt_rw_poc.c +++ b/code_templates/nxdt_rw_poc.c @@ -3730,19 +3730,12 @@ static bool browseEmmcPartition(void *userdata) bool success = false; - if (bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System) + if (bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System || !(mount_name = bisStorageGetMountNameByBisPartitionId(bis_partition_id))) { consolePrint("invalid bis partition id! (%u)\n", bis_partition_id); goto end; } - /* Mount BIS partition. */ - if (!bisStorageMountPartition(bis_partition_id, &mount_name)) - { - consolePrint("failed to mount bis partition %u!\n", bis_partition_id); - goto end; - } - /* Generate output base path. */ base_out_path = generateOutputGameCardFileName(utilsGetAtmosphereEmummcStatus() ? "emuMMC" : "sysMMC", mount_name, false); if (!base_out_path) goto end; @@ -3754,8 +3747,6 @@ end: /* Free data. */ if (base_out_path) free(base_out_path); - if (mount_name) bisStorageUnmountPartition(bis_partition_id); - if (!success && g_appletStatus) { consolePrint("press any button to continue\n"); diff --git a/include/core/bis_storage.h b/include/core/bis_storage.h index b4c2c84..ee0b2af 100644 --- a/include/core/bis_storage.h +++ b/include/core/bis_storage.h @@ -28,16 +28,28 @@ extern "C" { #endif -/// Mounts an eMMC BIS partition using its ID and provides a pointer to a string that holds its mount name, which can be used to carry out FS operations. -/// Only eMMC BIS partition IDs `CalibrationFile` (28) through `System` (31) are supported. -bool bisStorageMountPartition(u8 bis_partition_id, const char **out_mount_name); +/// Mounts the eMMC partitions with IDs `CalibrationFile` (28) through `System` (31) and makes it possible to perform read-only FS operations with them. +/// The mount name for each partition can be retrieved via bisStorageGetMountNameByBisPartitionId(). +bool bisStorageInitialize(void); -/// Unmounts a previously mounted eMMC BIS partition. -/// Only eMMC BIS partition IDs `CalibrationFile` (28) through `System` (31) are supported. -void bisStorageUnmountPartition(u8 bis_partition_id); +/// Unmounts all previously mounted eMMC partitions. +void bisStorageExit(void); -/// Unmounts all previously mounted eMMC BIS partitions. -void bisStorageUnmountAllPartitions(void); +/// Returns a pointer to a string that holds the GPT partition name for the provided eMMC BIS partition ID (e.g. FsBisPartitionId_CalibrationFile -> "PRODINFOF"). +/// Only eMMC BIS partition IDs `CalibrationFile` (28) through `System` (31) are supported. +/// Returns NULL if the eMMC BIS storage interface hasn't been initialized yet, or if an unsupported eMMC BIS partition ID is provided. +const char *bisStorageGetGptPartitionNameByBisPartitionId(u8 bis_partition_id); + +/// Returns a pointer to a string that holds the SystemInitializer partition name for the provided eMMC BIS partition ID (e.g. FsBisPartitionId_CalibrationFile -> "CalibrationFile"). +/// Only eMMC BIS partition IDs `CalibrationFile` (28) through `System` (31) are supported. +/// Returns NULL if the eMMC BIS storage interface hasn't been initialized yet, or if an unsupported eMMC BIS partition ID is provided. +const char *bisStorageGetSystemInitializerPartitionNameByBisPartitionId(u8 bis_partition_id); + +/// Returns a pointer to a string that holds the mount name for the provided eMMC BIS partition ID (e.g. FsBisPartitionId_CalibrationFile -> "bisprodinfof"). +/// This can be used to perform read-only FS operations on a specific partition. +/// Only eMMC BIS partition IDs `CalibrationFile` (28) through `System` (31) are supported. +/// Returns NULL if the eMMC BIS storage interface hasn't been initialized yet, or if an unsupported eMMC BIS partition ID is provided. +const char *bisStorageGetMountNameByBisPartitionId(u8 bis_partition_id); /// Returns a pointer to a FsStorage object that matches the provided FatFs drive number, or NULL if it hasn't been mounted. /// Only used by FatFs's diskio operations. diff --git a/include/core/fatfs/ffconf.h b/include/core/fatfs/ffconf.h index 312b064..4b7210a 100644 --- a/include/core/fatfs/ffconf.h +++ b/include/core/fatfs/ffconf.h @@ -1,3 +1,5 @@ +#include "../../defines.h" + /*---------------------------------------------------------------------------/ / Configurations of FatFs Module /---------------------------------------------------------------------------*/ @@ -166,11 +168,11 @@ / Drive/Volume Configurations /---------------------------------------------------------------------------*/ -#define FF_VOLUMES 4 +#define FF_VOLUMES BIS_FAT_PARTITION_COUNT /* Number of volumes (logical drives) to be used. (1-10) */ -#define FF_STR_VOLUME_ID 1 +#define FF_STR_VOLUME_ID 0 /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each diff --git a/include/core/nxdt_utils.h b/include/core/nxdt_utils.h index 62cef6b..99adabe 100644 --- a/include/core/nxdt_utils.h +++ b/include/core/nxdt_utils.h @@ -32,10 +32,10 @@ extern "C" { #endif /* Scoped lock macro. */ -#define SCOPED_LOCK(mtx) for(UtilsScopedLock ANONYMOUS_VARIABLE(scoped_lock) CLEANUP(utilsUnlockScope) = utilsLockScope(mtx); ANONYMOUS_VARIABLE(scoped_lock).cond; ANONYMOUS_VARIABLE(scoped_lock).cond = 0) +#define SCOPED_LOCK(mtx) for(UtilsScopedLock ANONYMOUS_VARIABLE(scoped_lock_) CLEANUP(utilsUnlockScope) = utilsLockScope(mtx); ANONYMOUS_VARIABLE(scoped_lock_).cond; ANONYMOUS_VARIABLE(scoped_lock_).cond = 0) /* Scoped try lock macro. */ -#define SCOPED_TRY_LOCK(mtx) for(UtilsScopedLock ANONYMOUS_VARIABLE(scoped_lock) CLEANUP(utilsUnlockScope) = utilsTryLockScope(mtx); ANONYMOUS_VARIABLE(scoped_lock).cond; ANONYMOUS_VARIABLE(scoped_lock).cond = 0) +#define SCOPED_TRY_LOCK(mtx) for(UtilsScopedLock ANONYMOUS_VARIABLE(scoped_lock_) CLEANUP(utilsUnlockScope) = utilsTryLockScope(mtx); ANONYMOUS_VARIABLE(scoped_lock_).cond; ANONYMOUS_VARIABLE(scoped_lock_).cond = 0) /// Used by scoped locks. typedef struct { @@ -150,7 +150,7 @@ void utilsGenerateHexString(char *dst, size_t dst_size, const void *src, size_t /// 'src' must match the regex /^(?:[A-Fa-f0-9]{2})+$/. /// 'src_size' may be zero, in which case strlen() will be used to determine the length of 'src'. Furthermore, 'src_size' must always be a multiple of 2. /// 'dst_size' must be at least 'src_size / 2'. -/// Returns false if there's an error validating input arguments. +/// Returns false if there's an error. bool utilsParseHexString(void *dst, size_t dst_size, const char *src, size_t src_size); /// Formats the provided 'size' value to a human-readable size string and stores it in 'dst'. diff --git a/include/defines.h b/include/defines.h index e264866..8b90378 100644 --- a/include/defines.h +++ b/include/defines.h @@ -104,6 +104,8 @@ #define LOG_BUF_SIZE 0x400000 /* 4 MiB. */ #define LOG_FORCE_FLUSH 0 /* Forces a log buffer flush each time the logfile is written to. */ +#define BIS_FAT_PARTITION_COUNT 4 + /// Reference: https://docs.microsoft.com/en-us/windows/win32/fileio/filesystem-functionality-comparison#limits. /// Reference: https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits. /// Most modern filesystems use a 255-byte limit instead of 255-character/codepoint limit, so that's what we're gonna use. diff --git a/source/core/bis_storage.c b/source/core/bis_storage.c index a8ed9bc..355160d 100644 --- a/source/core/bis_storage.c +++ b/source/core/bis_storage.c @@ -24,13 +24,33 @@ #include #define BIS_STORAGE_INDEX(type) ((type) - FsBisPartitionId_CalibrationFile) + +#define BIS_STORAGE_GPT_NAME(type) g_bisStorageGptPartitionNames[BIS_STORAGE_INDEX(type)] +#define BIS_STORAGE_SYSINIT_NAME(type) g_bisStorageSystemInitializerPartitionNames[BIS_STORAGE_INDEX(type)] +#define BIS_STORAGE_MOUNT_NAME(type) g_bisStorageDevoptabMountNames[BIS_STORAGE_INDEX(type)] + #define BIS_STORAGE_FATFS_CTX(type) g_bisStorageContexts[BIS_STORAGE_INDEX(type)] -#define BIS_STORAGE_MOUNT_NAME(type) VolumeStr[BIS_STORAGE_INDEX(type)] + +#define BIS_GET_NAME_FUNC(property, field) \ +const char *bisStorageGet##property##ByBisPartitionId(u8 bis_partition_id) { \ + const char *ret = NULL; \ + SCOPED_LOCK(&g_bisStorageMutex) { \ + BisStorageFatFsContext *bis_fatfs_ctx = NULL; \ + if (!g_bisStorageInterfaceInit || bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System || \ + !(bis_fatfs_ctx = BIS_STORAGE_FATFS_CTX(bis_partition_id))) break; \ + ret = bis_fatfs_ctx->field; \ + } \ + return ret; \ +} /* Type definitions. */ typedef struct { u8 bis_partition_id; ///< FsBisPartitionId. + const char *gpt_name; + const char *sysinit_name; + const char *devoptab_mount_name; + char fatfs_mount_name[4]; FsStorage bis_storage; FATFS fatfs; } BisStorageFatFsContext; @@ -38,90 +58,81 @@ typedef struct { /* Global variables. */ static Mutex g_bisStorageMutex = 0; +static BisStorageFatFsContext *g_bisStorageContexts[BIS_FAT_PARTITION_COUNT] = {NULL}; +static bool g_bisStorageInterfaceInit = false; -static BisStorageFatFsContext *g_bisStorageContexts[FF_VOLUMES] = {0}; +static const char *g_bisStorageGptPartitionNames[BIS_FAT_PARTITION_COUNT] = { + [BIS_STORAGE_INDEX(FsBisPartitionId_CalibrationFile)] = "PRODINFOF", + [BIS_STORAGE_INDEX(FsBisPartitionId_SafeMode)] = "SAFE", + [BIS_STORAGE_INDEX(FsBisPartitionId_User)] = "USER", + [BIS_STORAGE_INDEX(FsBisPartitionId_System)] = "SYSTEM" +}; -/// Required by FatFs. -const char *VolumeStr[FF_VOLUMES] = { - "PRODINFOF", - "SAFE", - "USER", - "SYSTEM", +static const char *g_bisStorageSystemInitializerPartitionNames[BIS_FAT_PARTITION_COUNT] = { + [BIS_STORAGE_INDEX(FsBisPartitionId_CalibrationFile)] = "CalibrationFile", + [BIS_STORAGE_INDEX(FsBisPartitionId_SafeMode)] = "SafeMode", + [BIS_STORAGE_INDEX(FsBisPartitionId_User)] = "User", + [BIS_STORAGE_INDEX(FsBisPartitionId_System)] = "System" +}; + +static const char *g_bisStorageDevoptabMountNames[BIS_FAT_PARTITION_COUNT] = { + [BIS_STORAGE_INDEX(FsBisPartitionId_CalibrationFile)] = "bisprodinfof", + [BIS_STORAGE_INDEX(FsBisPartitionId_SafeMode)] = "bissafe", + [BIS_STORAGE_INDEX(FsBisPartitionId_User)] = "bisuser", + [BIS_STORAGE_INDEX(FsBisPartitionId_System)] = "bissystem" }; /* Function prototypes. */ -static void _bisStorageUnmountPartition(u8 bis_partition_id); +NX_INLINE bool bisStorageMountAllPartitions(void); +NX_INLINE void bisStorageUnmountAllPartitions(void); + +static bool bisStorageMountPartition(u8 bis_partition_id); +static void bisStorageUnmountPartition(u8 bis_partition_id); -static BisStorageFatFsContext *bisStorageInitializeFatFsContext(u8 bis_partition_id); static void bisStorageFreeFatFsContext(BisStorageFatFsContext **bis_fatfs_ctx); -bool bisStorageMountPartition(u8 bis_partition_id, const char **out_mount_name) +bool bisStorageInitialize(void) { - if (bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System || !out_mount_name) - { - LOG_MSG_ERROR("Invalid parameters!"); - return false; - } - bool ret = false; SCOPED_LOCK(&g_bisStorageMutex) { - BisStorageFatFsContext *bis_fatfs_ctx = NULL; + ret = g_bisStorageInterfaceInit; + if (ret) break; - /* Check if we have already mounted this eMMC partition. */ - bis_fatfs_ctx = BIS_STORAGE_FATFS_CTX(bis_partition_id); - if (bis_fatfs_ctx) - { - *out_mount_name = BIS_STORAGE_MOUNT_NAME(bis_partition_id); - ret = true; - break; - } - - /* Initialize BIS FatFs context. */ - bis_fatfs_ctx = bisStorageInitializeFatFsContext(bis_partition_id); - if (!bis_fatfs_ctx) break; - - /* Update output. */ - *out_mount_name = BIS_STORAGE_MOUNT_NAME(bis_partition_id); - ret = true; + /* Mount all eMMC BIS FAT partitions. */ + ret = g_bisStorageInterfaceInit = bisStorageMountAllPartitions(); } return ret; } -void bisStorageUnmountPartition(u8 bis_partition_id) -{ - if (bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System) - { - LOG_MSG_ERROR("Invalid parameters!"); - return; - } - - SCOPED_LOCK(&g_bisStorageMutex) _bisStorageUnmountPartition(bis_partition_id); -} - -void bisStorageUnmountAllPartitions(void) +void bisStorageExit(void) { SCOPED_LOCK(&g_bisStorageMutex) { - for(u8 i = 0; i < FF_VOLUMES; i++) _bisStorageUnmountPartition(i + FsBisPartitionId_CalibrationFile); + /* Unmount all eMMC BIS FAT partitions. */ + bisStorageUnmountAllPartitions(); + + g_bisStorageInterfaceInit = false; } } +BIS_GET_NAME_FUNC(GptPartitionName, gpt_name); + +BIS_GET_NAME_FUNC(SystemInitializerPartitionName, sysinit_name); + +BIS_GET_NAME_FUNC(MountName, devoptab_mount_name); + FsStorage *bisStorageGetFsStorageByFatFsDriveNumber(u8 drive_number) { FsStorage *bis_storage = NULL; SCOPED_LOCK(&g_bisStorageMutex) { - for(u8 i = 0; i < FF_VOLUMES; i++) - { - if (!g_bisStorageContexts[i] || g_bisStorageContexts[i]->fatfs.pdrv != drive_number) continue; - bis_storage = &(g_bisStorageContexts[i]->bis_storage); - break; - } + if (drive_number >= BIS_FAT_PARTITION_COUNT || !g_bisStorageContexts[drive_number]) break; + bis_storage = &(g_bisStorageContexts[drive_number]->bis_storage); } return bis_storage; @@ -141,8 +152,107 @@ void bisStorageControlMutex(bool lock) } } -static void _bisStorageUnmountPartition(u8 bis_partition_id) +NX_INLINE bool bisStorageMountAllPartitions(void) { + bool ret = false; + + for(u8 i = FsBisPartitionId_CalibrationFile; i <= FsBisPartitionId_System; i++) + { + ret = bisStorageMountPartition(i); + if (!ret) break; + } + + return ret; +} + +NX_INLINE void bisStorageUnmountAllPartitions(void) +{ + for(u8 i = FsBisPartitionId_CalibrationFile; i <= FsBisPartitionId_System; i++) bisStorageUnmountPartition(i); +} + +static bool bisStorageMountPartition(u8 bis_partition_id) +{ + if (bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System) + { + LOG_MSG_ERROR("Invalid parameters!"); + return false; + } + + BisStorageFatFsContext *bis_fatfs_ctx = NULL; + Result rc = 0; + FRESULT fr = FR_OK; + bool success = false; + + /* Check if we have already mounted this eMMC partition. */ + if (BIS_STORAGE_FATFS_CTX(bis_partition_id)) + { + success = true; + goto end; + } + + /* Allocate memory for our BIS FatFs context. */ + bis_fatfs_ctx = calloc(1, sizeof(BisStorageFatFsContext)); + if (!bis_fatfs_ctx) + { + LOG_MSG_ERROR("Failed to allocate memory for BIS FatFs context! (partition ID %u).", bis_partition_id); + goto end; + } + + /* Update context. */ + bis_fatfs_ctx->bis_partition_id = bis_partition_id; + bis_fatfs_ctx->gpt_name = BIS_STORAGE_GPT_NAME(bis_partition_id); + bis_fatfs_ctx->sysinit_name = BIS_STORAGE_SYSINIT_NAME(bis_partition_id); + bis_fatfs_ctx->devoptab_mount_name = BIS_STORAGE_MOUNT_NAME(bis_partition_id); + snprintf(bis_fatfs_ctx->fatfs_mount_name, sizeof(bis_fatfs_ctx->fatfs_mount_name), "%u:", BIS_STORAGE_INDEX(bis_partition_id)); + + /* Open BIS storage. */ + rc = fsOpenBisStorage(&(bis_fatfs_ctx->bis_storage), bis_fatfs_ctx->bis_partition_id); + if (R_FAILED(rc)) + { + LOG_MSG_ERROR("Failed to open BIS storage for %s partition! (0x%X).", bis_fatfs_ctx->gpt_name, rc); + goto end; + } + + /* Update context array. */ + /* FatFs diskio demands we do this here. */ + BIS_STORAGE_FATFS_CTX(bis_partition_id) = bis_fatfs_ctx; + + /* Mount BIS partition using FatFs. */ + fr = f_mount(&(bis_fatfs_ctx->fatfs), bis_fatfs_ctx->fatfs_mount_name, 1); + if (fr != FR_OK) + { + LOG_MSG_ERROR("Failed to mount %s partition via FatFs! (%u).", bis_fatfs_ctx->gpt_name, fr); + goto end; + } + + /* Mount devoptab device. */ + if (!devoptabMountFatFsDevice(&(bis_fatfs_ctx->fatfs), bis_fatfs_ctx->devoptab_mount_name)) + { + LOG_MSG_ERROR("Failed to mount devoptab device for %s partition!", bis_fatfs_ctx->gpt_name); + goto end; + } + + /* Update flag. */ + success = true; + +end: + if (!success && bis_fatfs_ctx) + { + bisStorageFreeFatFsContext(&bis_fatfs_ctx); + BIS_STORAGE_FATFS_CTX(bis_partition_id) = NULL; + } + + return success; +} + +static void bisStorageUnmountPartition(u8 bis_partition_id) +{ + if (bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System) + { + LOG_MSG_ERROR("Invalid parameters!"); + return; + } + /* Check if we have already mounted this eMMC partition. */ BisStorageFatFsContext *bis_fatfs_ctx = BIS_STORAGE_FATFS_CTX(bis_partition_id); if (!bis_fatfs_ctx) return; @@ -154,87 +264,20 @@ static void _bisStorageUnmountPartition(u8 bis_partition_id) BIS_STORAGE_FATFS_CTX(bis_partition_id) = NULL; } -static BisStorageFatFsContext *bisStorageInitializeFatFsContext(u8 bis_partition_id) -{ - if (bis_partition_id < FsBisPartitionId_CalibrationFile || bis_partition_id > FsBisPartitionId_System) - { - LOG_MSG_ERROR("Invalid parameters!"); - return NULL; - } - - BisStorageFatFsContext *bis_fatfs_ctx = NULL; - - Result rc = 0; - - FRESULT fr = FR_OK; - const char *name = BIS_STORAGE_MOUNT_NAME(bis_partition_id); - - bool success = false; - - /* Allocate memory for our output context. */ - bis_fatfs_ctx = calloc(1, sizeof(BisStorageFatFsContext)); - if (!bis_fatfs_ctx) - { - LOG_MSG_ERROR("Failed to allocate memory for BIS FatFs context! (partition ID %u).", bis_partition_id); - goto end; - } - - /* Set BIS partition ID. */ - bis_fatfs_ctx->bis_partition_id = bis_partition_id; - - /* Open BIS storage. */ - rc = fsOpenBisStorage(&(bis_fatfs_ctx->bis_storage), bis_fatfs_ctx->bis_partition_id); - if (R_FAILED(rc)) - { - LOG_MSG_ERROR("Failed to open eMMC BIS partition storage! (0x%X, partition ID %u).", rc, bis_partition_id); - goto end; - } - - /* Update context array. */ - /* FatFs diskio demands we do this here. */ - BIS_STORAGE_FATFS_CTX(bis_partition_id) = bis_fatfs_ctx; - - /* Mount BIS partition using FatFs. */ - fr = f_mount(&(bis_fatfs_ctx->fatfs), name, 1); - if (fr != FR_OK) - { - LOG_MSG_ERROR("Failed to mount eMMC BIS partition via FatFs! (%u, partition ID %u).", fr, bis_partition_id); - goto end; - } - - /* Mount devoptab device. */ - if (!devoptabMountFatFsDevice(&(bis_fatfs_ctx->fatfs), name)) - { - LOG_MSG_ERROR("Failed to mount devoptab device for eMMC BIS partition %u!", bis_partition_id); - goto end; - } - - /* Update flag. */ - success = true; - -end: - if (!success) - { - bisStorageFreeFatFsContext(&bis_fatfs_ctx); - BIS_STORAGE_FATFS_CTX(bis_partition_id) = NULL; - } - - return bis_fatfs_ctx; -} - static void bisStorageFreeFatFsContext(BisStorageFatFsContext **bis_fatfs_ctx) { if (!bis_fatfs_ctx || !*bis_fatfs_ctx) return; - if ((*bis_fatfs_ctx)->fatfs.fs_type) + BisStorageFatFsContext *ctx = *bis_fatfs_ctx; + + if (ctx->fatfs.fs_type) { - const char *name = BIS_STORAGE_MOUNT_NAME((*bis_fatfs_ctx)->bis_partition_id); - devoptabUnmountDevice(name); - f_unmount(name); + devoptabUnmountDevice(ctx->devoptab_mount_name); + f_unmount(ctx->fatfs_mount_name); } - if (serviceIsActive(&((*bis_fatfs_ctx)->bis_storage.s))) fsStorageClose(&((*bis_fatfs_ctx)->bis_storage)); + if (serviceIsActive(&(ctx->bis_storage.s))) fsStorageClose(&(ctx->bis_storage)); - free(*bis_fatfs_ctx); - *bis_fatfs_ctx = NULL; + free(ctx); + ctx = *bis_fatfs_ctx = NULL; } diff --git a/source/core/cert.c b/source/core/cert.c index 2f15ef0..3ee2a8e 100644 --- a/source/core/cert.c +++ b/source/core/cert.c @@ -61,15 +61,9 @@ bool certRetrieveCertificateByName(Certificate *dst, const char *name) SCOPED_LOCK(&g_esCertSaveMutex) { - bisStorageControlMutex(true); - - if (certOpenEsCertSaveFile()) - { - ret = _certRetrieveCertificateByName(dst, name); - certCloseEsCertSaveFile(); - } - - bisStorageControlMutex(false); + if (!certOpenEsCertSaveFile()) break; + ret = _certRetrieveCertificateByName(dst, name); + certCloseEsCertSaveFile(); } return ret; @@ -87,15 +81,9 @@ bool certRetrieveCertificateChainBySignatureIssuer(CertificateChain *dst, const SCOPED_LOCK(&g_esCertSaveMutex) { - bisStorageControlMutex(true); - - if (certOpenEsCertSaveFile()) - { - ret = _certRetrieveCertificateChainBySignatureIssuer(dst, issuer); - certCloseEsCertSaveFile(); - } - - bisStorageControlMutex(false); + if (!certOpenEsCertSaveFile()) break; + ret = _certRetrieveCertificateChainBySignatureIssuer(dst, issuer); + certCloseEsCertSaveFile(); } return ret; @@ -207,8 +195,8 @@ static bool certOpenEsCertSaveFile(void) char savefile_path[64] = {0}; bool success = false; - /* Mount eMMC BIS System partition. */ - if (!bisStorageMountPartition(FsBisPartitionId_System, &mount_name)) + /* Retrieve mount name for the eMMC BIS System partition. */ + if (!(mount_name = bisStorageGetMountNameByBisPartitionId(FsBisPartitionId_System))) { LOG_MSG_ERROR("Failed to mount eMMC BIS System partition!"); goto end; @@ -229,8 +217,6 @@ static bool certOpenEsCertSaveFile(void) success = true; end: - if (!success && mount_name) bisStorageUnmountPartition(FsBisPartitionId_System); - return success; } @@ -239,8 +225,6 @@ static void certCloseEsCertSaveFile(void) if (!g_esCertSaveCtx) return; save_close_savefile(&g_esCertSaveCtx); - - bisStorageUnmountPartition(FsBisPartitionId_System); } static bool _certRetrieveCertificateByName(Certificate *dst, const char *name) diff --git a/source/core/devoptab/nxdt_devoptab.c b/source/core/devoptab/nxdt_devoptab.c index 00c2e86..5ead505 100644 --- a/source/core/devoptab/nxdt_devoptab.c +++ b/source/core/devoptab/nxdt_devoptab.c @@ -22,7 +22,7 @@ #include #include -#define DEVOPTAB_DEVICE_COUNT 4 +#define DEVOPTAB_DEVICE_COUNT 8 /* Type definitions. */ @@ -37,9 +37,7 @@ typedef enum { /* Global variables. */ static Mutex g_devoptabMutex = 0; - static DevoptabDeviceContext g_devoptabDevices[DEVOPTAB_DEVICE_COUNT] = {0}; -static const u32 g_devoptabDeviceCount = MAX_ELEMENTS(g_devoptabDevices); /* Function prototypes. */ @@ -139,7 +137,7 @@ void devoptabUnmountAllDevices(void) SCOPED_LOCK(&g_devoptabMutex) { /* Loop through all of our device entries and reset them all. */ - for(u32 i = 0; i < g_devoptabDeviceCount; i++) devoptabResetDevice(&(g_devoptabDevices[i])); + for(u32 i = 0; i < DEVOPTAB_DEVICE_COUNT; i++) devoptabResetDevice(&(g_devoptabDevices[i])); } } @@ -220,6 +218,8 @@ static bool devoptabMountDevice(void *fs_ctx, const char *name, u8 type) goto end; } + LOG_MSG_DEBUG("Successfully mounted device \"%s:\".", dev_ctx->name); + /* Update flags. */ ret = dev_ctx->initialized = true; @@ -233,7 +233,7 @@ static DevoptabDeviceContext *devoptabFindDevice(const char *name) { DevoptabDeviceContext *dev_ctx = NULL; - for(u32 i = 0; i < g_devoptabDeviceCount; i++) + for(u32 i = 0; i < DEVOPTAB_DEVICE_COUNT; i++) { dev_ctx = &(g_devoptabDevices[i]); diff --git a/source/core/nxdt_utils.c b/source/core/nxdt_utils.c index 163ed4e..3bc903c 100644 --- a/source/core/nxdt_utils.c +++ b/source/core/nxdt_utils.c @@ -283,6 +283,9 @@ bool utilsInitializeResources(void) /* Setup an applet hook to change the hardware clocks after a system mode change (docked <-> undocked). */ appletHook(&g_systemOverclockCookie, utilsOverclockSystemAppletHook, NULL); + /* Initialize eMMC BIS storage interface. */ + if (!bisStorageInitialize()) break; + /* Enable video recording if we're running under title override mode. */ if (!utilsIsAppletMode()) { @@ -335,12 +338,12 @@ void utilsCloseResources(void) { LOG_MSG_INFO("Shutting down..."); + /* Close eMMC BIS storage interface. */ + bisStorageExit(); + /* Unmount all custom devoptab devices. */ devoptabUnmountAllDevices(); - /* Unmount all eMMC BIS partitions. */ - bisStorageUnmountAllPartitions(); - /* Unset long running process state. */ utilsSetLongRunningProcessState(false); diff --git a/source/core/tik.c b/source/core/tik.c index fdf340e..81c8231 100644 --- a/source/core/tik.c +++ b/source/core/tik.c @@ -331,8 +331,6 @@ static bool tikRetrieveTicketFromEsSaveDataByRightsId(Ticket *dst, const FsRight bool success = false; - bisStorageControlMutex(true); - /* Allocate memory to retrieve the ticket. */ if (!(buf = malloc(buf_size))) { @@ -347,8 +345,8 @@ static bool tikRetrieveTicketFromEsSaveDataByRightsId(Ticket *dst, const FsRight goto end; } - /* Mount eMMC BIS System partition. */ - if (!bisStorageMountPartition(FsBisPartitionId_System, &mount_name)) + /* Retrieve mount name for the eMMC BIS System partition. */ + if (!(mount_name = bisStorageGetMountNameByBisPartitionId(FsBisPartitionId_System))) { LOG_MSG_ERROR("Failed to mount eMMC BIS System partition!"); goto end; @@ -391,12 +389,8 @@ static bool tikRetrieveTicketFromEsSaveDataByRightsId(Ticket *dst, const FsRight end: if (save_ctx) save_close_savefile(&save_ctx); - if (mount_name) bisStorageUnmountPartition(FsBisPartitionId_System); - if (buf) free(buf); - bisStorageControlMutex(false); - return success; } @@ -560,8 +554,8 @@ static bool tikGetDecryptedTitleKey(void *dst, const void *src, u8 key_generatio return true; } -static bool tikGetTitleKeyTypeFromRightsId(const FsRightsId *id, u8 *out) +static bool tikGetTitleKeyTypeFromRightsId(const FsRightsId *id, u8 *out) { if (!id || !out) {