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

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.
This commit is contained in:
Pablo Curiel 2024-11-01 13:54:37 +01:00
parent 4589614183
commit 94f0312566
10 changed files with 224 additions and 193 deletions

View file

@ -3730,19 +3730,12 @@ static bool browseEmmcPartition(void *userdata)
bool success = false; 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); consolePrint("invalid bis partition id! (%u)\n", bis_partition_id);
goto end; 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. */ /* Generate output base path. */
base_out_path = generateOutputGameCardFileName(utilsGetAtmosphereEmummcStatus() ? "emuMMC" : "sysMMC", mount_name, false); base_out_path = generateOutputGameCardFileName(utilsGetAtmosphereEmummcStatus() ? "emuMMC" : "sysMMC", mount_name, false);
if (!base_out_path) goto end; if (!base_out_path) goto end;
@ -3754,8 +3747,6 @@ end:
/* Free data. */ /* Free data. */
if (base_out_path) free(base_out_path); if (base_out_path) free(base_out_path);
if (mount_name) bisStorageUnmountPartition(bis_partition_id);
if (!success && g_appletStatus) if (!success && g_appletStatus)
{ {
consolePrint("press any button to continue\n"); consolePrint("press any button to continue\n");

View file

@ -28,16 +28,28 @@
extern "C" { extern "C" {
#endif #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. /// Mounts the eMMC partitions with IDs `CalibrationFile` (28) through `System` (31) and makes it possible to perform read-only FS operations with them.
/// Only eMMC BIS partition IDs `CalibrationFile` (28) through `System` (31) are supported. /// The mount name for each partition can be retrieved via bisStorageGetMountNameByBisPartitionId().
bool bisStorageMountPartition(u8 bis_partition_id, const char **out_mount_name); bool bisStorageInitialize(void);
/// Unmounts a previously mounted eMMC BIS partition. /// Unmounts all previously mounted eMMC partitions.
/// Only eMMC BIS partition IDs `CalibrationFile` (28) through `System` (31) are supported. void bisStorageExit(void);
void bisStorageUnmountPartition(u8 bis_partition_id);
/// Unmounts all previously mounted eMMC BIS partitions. /// Returns a pointer to a string that holds the GPT partition name for the provided eMMC BIS partition ID (e.g. FsBisPartitionId_CalibrationFile -> "PRODINFOF").
void bisStorageUnmountAllPartitions(void); /// 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. /// 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. /// Only used by FatFs's diskio operations.

View file

@ -1,3 +1,5 @@
#include "../../defines.h"
/*---------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ Configurations of FatFs Module / Configurations of FatFs Module
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
@ -166,11 +168,11 @@
/ Drive/Volume Configurations / Drive/Volume Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define FF_VOLUMES 4 #define FF_VOLUMES BIS_FAT_PARTITION_COUNT
/* Number of volumes (logical drives) to be used. (1-10) */ /* 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. /* 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 / 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 / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each

View file

@ -32,10 +32,10 @@ extern "C" {
#endif #endif
/* Scoped lock macro. */ /* 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. */ /* 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. /// Used by scoped locks.
typedef struct { 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' 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. /// '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'. /// '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); 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'. /// Formats the provided 'size' value to a human-readable size string and stores it in 'dst'.

View file

@ -104,6 +104,8 @@
#define LOG_BUF_SIZE 0x400000 /* 4 MiB. */ #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 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://docs.microsoft.com/en-us/windows/win32/fileio/filesystem-functionality-comparison#limits.
/// Reference: https://en.wikipedia.org/wiki/Comparison_of_file_systems#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. /// Most modern filesystems use a 255-byte limit instead of 255-character/codepoint limit, so that's what we're gonna use.

View file

@ -24,13 +24,33 @@
#include <core/devoptab/nxdt_devoptab.h> #include <core/devoptab/nxdt_devoptab.h>
#define BIS_STORAGE_INDEX(type) ((type) - FsBisPartitionId_CalibrationFile) #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_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. */ /* Type definitions. */
typedef struct { typedef struct {
u8 bis_partition_id; ///< FsBisPartitionId. 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; FsStorage bis_storage;
FATFS fatfs; FATFS fatfs;
} BisStorageFatFsContext; } BisStorageFatFsContext;
@ -38,90 +58,81 @@ typedef struct {
/* Global variables. */ /* Global variables. */
static Mutex g_bisStorageMutex = 0; 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. static const char *g_bisStorageSystemInitializerPartitionNames[BIS_FAT_PARTITION_COUNT] = {
const char *VolumeStr[FF_VOLUMES] = { [BIS_STORAGE_INDEX(FsBisPartitionId_CalibrationFile)] = "CalibrationFile",
"PRODINFOF", [BIS_STORAGE_INDEX(FsBisPartitionId_SafeMode)] = "SafeMode",
"SAFE", [BIS_STORAGE_INDEX(FsBisPartitionId_User)] = "User",
"USER", [BIS_STORAGE_INDEX(FsBisPartitionId_System)] = "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. */ /* 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); 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; bool ret = false;
SCOPED_LOCK(&g_bisStorageMutex) SCOPED_LOCK(&g_bisStorageMutex)
{ {
BisStorageFatFsContext *bis_fatfs_ctx = NULL; ret = g_bisStorageInterfaceInit;
if (ret) break;
/* Check if we have already mounted this eMMC partition. */ /* Mount all eMMC BIS FAT partitions. */
bis_fatfs_ctx = BIS_STORAGE_FATFS_CTX(bis_partition_id); ret = g_bisStorageInterfaceInit = bisStorageMountAllPartitions();
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;
} }
return ret; return ret;
} }
void bisStorageUnmountPartition(u8 bis_partition_id) void bisStorageExit(void)
{
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)
{ {
SCOPED_LOCK(&g_bisStorageMutex) 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 *bisStorageGetFsStorageByFatFsDriveNumber(u8 drive_number)
{ {
FsStorage *bis_storage = NULL; FsStorage *bis_storage = NULL;
SCOPED_LOCK(&g_bisStorageMutex) SCOPED_LOCK(&g_bisStorageMutex)
{ {
for(u8 i = 0; i < FF_VOLUMES; i++) if (drive_number >= BIS_FAT_PARTITION_COUNT || !g_bisStorageContexts[drive_number]) break;
{ bis_storage = &(g_bisStorageContexts[drive_number]->bis_storage);
if (!g_bisStorageContexts[i] || g_bisStorageContexts[i]->fatfs.pdrv != drive_number) continue;
bis_storage = &(g_bisStorageContexts[i]->bis_storage);
break;
}
} }
return 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. */ /* Check if we have already mounted this eMMC partition. */
BisStorageFatFsContext *bis_fatfs_ctx = BIS_STORAGE_FATFS_CTX(bis_partition_id); BisStorageFatFsContext *bis_fatfs_ctx = BIS_STORAGE_FATFS_CTX(bis_partition_id);
if (!bis_fatfs_ctx) return; if (!bis_fatfs_ctx) return;
@ -154,87 +264,20 @@ static void _bisStorageUnmountPartition(u8 bis_partition_id)
BIS_STORAGE_FATFS_CTX(bis_partition_id) = NULL; 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) static void bisStorageFreeFatFsContext(BisStorageFatFsContext **bis_fatfs_ctx)
{ {
if (!bis_fatfs_ctx || !*bis_fatfs_ctx) return; 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(ctx->devoptab_mount_name);
devoptabUnmountDevice(name); f_unmount(ctx->fatfs_mount_name);
f_unmount(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); free(ctx);
*bis_fatfs_ctx = NULL; ctx = *bis_fatfs_ctx = NULL;
} }

View file

@ -61,17 +61,11 @@ bool certRetrieveCertificateByName(Certificate *dst, const char *name)
SCOPED_LOCK(&g_esCertSaveMutex) SCOPED_LOCK(&g_esCertSaveMutex)
{ {
bisStorageControlMutex(true); if (!certOpenEsCertSaveFile()) break;
if (certOpenEsCertSaveFile())
{
ret = _certRetrieveCertificateByName(dst, name); ret = _certRetrieveCertificateByName(dst, name);
certCloseEsCertSaveFile(); certCloseEsCertSaveFile();
} }
bisStorageControlMutex(false);
}
return ret; return ret;
} }
@ -87,17 +81,11 @@ bool certRetrieveCertificateChainBySignatureIssuer(CertificateChain *dst, const
SCOPED_LOCK(&g_esCertSaveMutex) SCOPED_LOCK(&g_esCertSaveMutex)
{ {
bisStorageControlMutex(true); if (!certOpenEsCertSaveFile()) break;
if (certOpenEsCertSaveFile())
{
ret = _certRetrieveCertificateChainBySignatureIssuer(dst, issuer); ret = _certRetrieveCertificateChainBySignatureIssuer(dst, issuer);
certCloseEsCertSaveFile(); certCloseEsCertSaveFile();
} }
bisStorageControlMutex(false);
}
return ret; return ret;
} }
@ -207,8 +195,8 @@ static bool certOpenEsCertSaveFile(void)
char savefile_path[64] = {0}; char savefile_path[64] = {0};
bool success = false; bool success = false;
/* Mount eMMC BIS System partition. */ /* Retrieve mount name for the eMMC BIS System partition. */
if (!bisStorageMountPartition(FsBisPartitionId_System, &mount_name)) if (!(mount_name = bisStorageGetMountNameByBisPartitionId(FsBisPartitionId_System)))
{ {
LOG_MSG_ERROR("Failed to mount eMMC BIS System partition!"); LOG_MSG_ERROR("Failed to mount eMMC BIS System partition!");
goto end; goto end;
@ -229,8 +217,6 @@ static bool certOpenEsCertSaveFile(void)
success = true; success = true;
end: end:
if (!success && mount_name) bisStorageUnmountPartition(FsBisPartitionId_System);
return success; return success;
} }
@ -239,8 +225,6 @@ static void certCloseEsCertSaveFile(void)
if (!g_esCertSaveCtx) return; if (!g_esCertSaveCtx) return;
save_close_savefile(&g_esCertSaveCtx); save_close_savefile(&g_esCertSaveCtx);
bisStorageUnmountPartition(FsBisPartitionId_System);
} }
static bool _certRetrieveCertificateByName(Certificate *dst, const char *name) static bool _certRetrieveCertificateByName(Certificate *dst, const char *name)

View file

@ -22,7 +22,7 @@
#include <core/nxdt_utils.h> #include <core/nxdt_utils.h>
#include <core/devoptab/nxdt_devoptab.h> #include <core/devoptab/nxdt_devoptab.h>
#define DEVOPTAB_DEVICE_COUNT 4 #define DEVOPTAB_DEVICE_COUNT 8
/* Type definitions. */ /* Type definitions. */
@ -37,9 +37,7 @@ typedef enum {
/* Global variables. */ /* Global variables. */
static Mutex g_devoptabMutex = 0; static Mutex g_devoptabMutex = 0;
static DevoptabDeviceContext g_devoptabDevices[DEVOPTAB_DEVICE_COUNT] = {0}; static DevoptabDeviceContext g_devoptabDevices[DEVOPTAB_DEVICE_COUNT] = {0};
static const u32 g_devoptabDeviceCount = MAX_ELEMENTS(g_devoptabDevices);
/* Function prototypes. */ /* Function prototypes. */
@ -139,7 +137,7 @@ void devoptabUnmountAllDevices(void)
SCOPED_LOCK(&g_devoptabMutex) SCOPED_LOCK(&g_devoptabMutex)
{ {
/* Loop through all of our device entries and reset them all. */ /* 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; goto end;
} }
LOG_MSG_DEBUG("Successfully mounted device \"%s:\".", dev_ctx->name);
/* Update flags. */ /* Update flags. */
ret = dev_ctx->initialized = true; ret = dev_ctx->initialized = true;
@ -233,7 +233,7 @@ static DevoptabDeviceContext *devoptabFindDevice(const char *name)
{ {
DevoptabDeviceContext *dev_ctx = NULL; 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]); dev_ctx = &(g_devoptabDevices[i]);

View file

@ -283,6 +283,9 @@ bool utilsInitializeResources(void)
/* Setup an applet hook to change the hardware clocks after a system mode change (docked <-> undocked). */ /* Setup an applet hook to change the hardware clocks after a system mode change (docked <-> undocked). */
appletHook(&g_systemOverclockCookie, utilsOverclockSystemAppletHook, NULL); appletHook(&g_systemOverclockCookie, utilsOverclockSystemAppletHook, NULL);
/* Initialize eMMC BIS storage interface. */
if (!bisStorageInitialize()) break;
/* Enable video recording if we're running under title override mode. */ /* Enable video recording if we're running under title override mode. */
if (!utilsIsAppletMode()) if (!utilsIsAppletMode())
{ {
@ -335,12 +338,12 @@ void utilsCloseResources(void)
{ {
LOG_MSG_INFO("Shutting down..."); LOG_MSG_INFO("Shutting down...");
/* Close eMMC BIS storage interface. */
bisStorageExit();
/* Unmount all custom devoptab devices. */ /* Unmount all custom devoptab devices. */
devoptabUnmountAllDevices(); devoptabUnmountAllDevices();
/* Unmount all eMMC BIS partitions. */
bisStorageUnmountAllPartitions();
/* Unset long running process state. */ /* Unset long running process state. */
utilsSetLongRunningProcessState(false); utilsSetLongRunningProcessState(false);

View file

@ -331,8 +331,6 @@ static bool tikRetrieveTicketFromEsSaveDataByRightsId(Ticket *dst, const FsRight
bool success = false; bool success = false;
bisStorageControlMutex(true);
/* Allocate memory to retrieve the ticket. */ /* Allocate memory to retrieve the ticket. */
if (!(buf = malloc(buf_size))) if (!(buf = malloc(buf_size)))
{ {
@ -347,8 +345,8 @@ static bool tikRetrieveTicketFromEsSaveDataByRightsId(Ticket *dst, const FsRight
goto end; goto end;
} }
/* Mount eMMC BIS System partition. */ /* Retrieve mount name for the eMMC BIS System partition. */
if (!bisStorageMountPartition(FsBisPartitionId_System, &mount_name)) if (!(mount_name = bisStorageGetMountNameByBisPartitionId(FsBisPartitionId_System)))
{ {
LOG_MSG_ERROR("Failed to mount eMMC BIS System partition!"); LOG_MSG_ERROR("Failed to mount eMMC BIS System partition!");
goto end; goto end;
@ -391,12 +389,8 @@ static bool tikRetrieveTicketFromEsSaveDataByRightsId(Ticket *dst, const FsRight
end: end:
if (save_ctx) save_close_savefile(&save_ctx); if (save_ctx) save_close_savefile(&save_ctx);
if (mount_name) bisStorageUnmountPartition(FsBisPartitionId_System);
if (buf) free(buf); if (buf) free(buf);
bisStorageControlMutex(false);
return success; return success;
} }
@ -560,8 +554,8 @@ static bool tikGetDecryptedTitleKey(void *dst, const void *src, u8 key_generatio
return true; return true;
} }
static bool tikGetTitleKeyTypeFromRightsId(const FsRightsId *id, u8 *out)
static bool tikGetTitleKeyTypeFromRightsId(const FsRightsId *id, u8 *out)
{ {
if (!id || !out) if (!id || !out)
{ {