Skip to content

Commit

Permalink
Custom devoptab wrappers (part 5).
Browse files Browse the repository at this point in the history
This commit implements a devoptab wrapper for RomFS sections within NCAs, using code from romfs.c/h.

Other changes include:

* devoptab: add additional safety checks and some minor tweaks to both hfs_dev and pfs_dev calls.
  • Loading branch information
DarkMatterCore committed Dec 21, 2023
1 parent 091747d commit ef03aa4
Show file tree
Hide file tree
Showing 4 changed files with 557 additions and 20 deletions.
11 changes: 6 additions & 5 deletions source/devoptab/hfs_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static int hfsdev_open(struct _reent *r, void *fd, const char *path, int flags,
HFS_DEV_INIT_FS_ACCESS;

/* Validate input. */
if (!file || (flags & (O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC | O_EXCL))) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL);
if (!file || (flags & (O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC | O_EXCL))) DEVOPTAB_SET_ERROR_AND_EXIT(EROFS);

/* Get truncated path. */
if (!(path = hfsdev_get_truncated_path(r, path))) DEVOPTAB_EXIT;
Expand All @@ -124,7 +124,7 @@ static int hfsdev_open(struct _reent *r, void *fd, const char *path, int flags,
/* Reset file descriptor. */
memset(file, 0, sizeof(HashFileSystemFileState));

/* Get information about the requested Partition FS entry. */
/* Get information about the requested Hash FS entry. */
if (!hfsGetEntryIndexByName(fs_ctx, path, &(file->index)) || !(file->hfs_entry = hfsGetEntryByIndex(fs_ctx, file->index)) || \
!(file->name = hfsGetEntryNameByIndex(fs_ctx, file->index))) DEVOPTAB_SET_ERROR(ENOENT);

Expand Down Expand Up @@ -202,7 +202,7 @@ static off_t hfsdev_seek(struct _reent *r, void *fd, off_t pos, int dir)
offset += pos;

/* Don't allow positive seeks beyond the end of file. */
if (offset > (off_t)file->hfs_entry->size) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL);
if (offset > (off_t)file->hfs_entry->size) DEVOPTAB_SET_ERROR_AND_EXIT(EOVERFLOW);

LOG_MSG_DEBUG("Seeking to offset 0x%lX from \"%s:/%s\".", offset, dev_ctx->name, file->name);

Expand Down Expand Up @@ -247,7 +247,7 @@ static int hfsdev_stat(struct _reent *r, const char *file, struct stat *st)

LOG_MSG_DEBUG("Getting file stats for \"%s:/%s\".", dev_ctx->name, file);

/* Get information about the requested Partition FS entry. */
/* Get information about the requested Hash FS entry. */
if (!hfsGetEntryIndexByName(fs_ctx, file, &index) || !(hfs_entry = hfsGetEntryByIndex(fs_ctx, index))) DEVOPTAB_SET_ERROR(ENOENT);

/* Fill stat info. */
Expand Down Expand Up @@ -315,7 +315,7 @@ static int hfsdev_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename,
/* Fill bogus directory entry. */
memset(filestat, 0, sizeof(struct stat));

filestat->st_nlink = 1;
filestat->st_nlink = (2 + hfsGetEntryCount(fs_ctx)); // One for self, one for parent.
filestat->st_mode = (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH);
filestat->st_atime = filestat->st_mtime = filestat->st_ctime = dev_ctx->mount_time;

Expand All @@ -332,6 +332,7 @@ static int hfsdev_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename,

/* Get Hash FS entry. */
if (!(hfs_entry = hfsGetEntryByIndex(fs_ctx, dir->index)) || !(fname = hfsGetEntryName(fs_ctx, hfs_entry))) DEVOPTAB_SET_ERROR_AND_EXIT(EIO);
if (strlen(fname) > NAME_MAX) DEVOPTAB_SET_ERROR_AND_EXIT(ENAMETOOLONG);

/* Copy filename. */
strcpy(filename, fname);
Expand Down
17 changes: 5 additions & 12 deletions source/devoptab/nxdt_devoptab.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static const u32 g_devoptabDeviceCount = MAX_ELEMENTS(g_devoptabDevices);

const devoptab_t *pfsdev_get_devoptab();
const devoptab_t *hfsdev_get_devoptab();
const devoptab_t *romfsdev_get_devoptab();

static bool devoptabMountDevice(void *fs_ctx, const char *name, u8 type);
static DevoptabDeviceContext *devoptabFindDevice(const char *name);
Expand All @@ -58,10 +59,7 @@ bool devoptabMountPartitionFileSystemDevice(PartitionFileSystemContext *pfs_ctx,

bool ret = false;

SCOPED_LOCK(&g_devoptabMutex)
{
ret = devoptabMountDevice(pfs_ctx, name, DevoptabDeviceType_PartitionFileSystem);
}
SCOPED_LOCK(&g_devoptabMutex) ret = devoptabMountDevice(pfs_ctx, name, DevoptabDeviceType_PartitionFileSystem);

return ret;
}
Expand All @@ -76,10 +74,7 @@ bool devoptabMountHashFileSystemDevice(HashFileSystemContext *hfs_ctx, const cha

bool ret = false;

SCOPED_LOCK(&g_devoptabMutex)
{
ret = devoptabMountDevice(hfs_ctx, name, DevoptabDeviceType_HashFileSystem);
}
SCOPED_LOCK(&g_devoptabMutex) ret = devoptabMountDevice(hfs_ctx, name, DevoptabDeviceType_HashFileSystem);

return ret;
}
Expand All @@ -94,10 +89,7 @@ bool devoptabMountRomFileSystemDevice(RomFileSystemContext *romfs_ctx, const cha

bool ret = false;

SCOPED_LOCK(&g_devoptabMutex)
{
ret = devoptabMountDevice(romfs_ctx, name, DevoptabDeviceType_RomFileSystem);
}
SCOPED_LOCK(&g_devoptabMutex) ret = devoptabMountDevice(romfs_ctx, name, DevoptabDeviceType_RomFileSystem);

return ret;
}
Expand Down Expand Up @@ -176,6 +168,7 @@ static bool devoptabMountDevice(void *fs_ctx, const char *name, u8 type)
device = hfsdev_get_devoptab();
break;
case DevoptabDeviceType_RomFileSystem:
device = romfsdev_get_devoptab();
break;
default:
break;
Expand Down
Loading

0 comments on commit ef03aa4

Please sign in to comment.