2023-12-20 19:32:48 +00:00
|
|
|
/*
|
|
|
|
* nxdt_devoptab.h
|
|
|
|
*
|
2024-04-12 10:47:36 +01:00
|
|
|
* Copyright (c) 2020-2024, DarkMatterCore <pabloacurielz@gmail.com>.
|
2023-12-20 19:32:48 +00: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef __NXDT_DEVOPTAB_H__
|
|
|
|
#define __NXDT_DEVOPTAB_H__
|
|
|
|
|
2024-04-30 22:01:42 +01:00
|
|
|
#include "../pfs.h"
|
|
|
|
#include "../hfs.h"
|
|
|
|
#include "../romfs.h"
|
2024-10-28 13:31:58 +00:00
|
|
|
#include "../fatfs/ff.h"
|
2023-12-20 19:32:48 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
#define DEVOPTAB_MOUNT_NAME_LENGTH 32 // Including NULL terminator.
|
2023-12-20 19:32:48 +00:00
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
#define DEVOPTAB_INIT_ERROR_STATE r->_errno = 0
|
|
|
|
#define DEVOPTAB_DECL_DEV_CTX DevoptabDeviceContext *dev_ctx = (DevoptabDeviceContext*)r->deviceData
|
|
|
|
#define DEVOPTAB_DECL_FS_CTX(type) type *fs_ctx = (type*)dev_ctx->fs_ctx
|
|
|
|
#define DEVOPTAB_DECL_FILE_STATE(type) type *file = (type*)fd
|
|
|
|
#define DEVOPTAB_DECL_DIR_STATE(type) type *dir = (type*)dirState->dirStruct
|
2023-12-20 19:32:48 +00:00
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
#define DEVOPTAB_SET_ERROR(x) r->_errno = (x)
|
|
|
|
#define DEVOPTAB_IS_ERROR_SET (r->_errno != 0)
|
2023-12-20 19:32:48 +00:00
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
#define DEVOPTAB_EXIT goto end
|
|
|
|
#define DEVOPTAB_SET_ERROR_AND_EXIT(x) \
|
2023-12-20 19:32:48 +00:00
|
|
|
do { \
|
|
|
|
DEVOPTAB_SET_ERROR(x); \
|
|
|
|
DEVOPTAB_EXIT; \
|
|
|
|
} while(0)
|
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
#define DEVOPTAB_RETURN_INT(x) return (DEVOPTAB_IS_ERROR_SET ? -1 : (x))
|
|
|
|
#define DEVOPTAB_RETURN_PTR(x) return (DEVOPTAB_IS_ERROR_SET ? NULL : (x))
|
|
|
|
#define DEVOPTAB_RETURN_BOOL return (DEVOPTAB_IS_ERROR_SET ? false : true)
|
|
|
|
#define DEVOPTAB_RETURN_UNSUPPORTED_OP r->_errno = ENOSYS; \
|
|
|
|
return -1
|
2023-12-20 19:32:48 +00:00
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
#define DEVOPTAB_INIT_VARS devoptabControlMutex(true); \
|
|
|
|
DEVOPTAB_INIT_ERROR_STATE; \
|
|
|
|
DEVOPTAB_DECL_DEV_CTX; \
|
|
|
|
if (!dev_ctx->initialized) DEVOPTAB_SET_ERROR_AND_EXIT(ENODEV)
|
2023-12-20 19:32:48 +00:00
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
#define DEVOPTAB_INIT_FILE_VARS(type) DEVOPTAB_INIT_VARS; \
|
|
|
|
DEVOPTAB_DECL_FILE_STATE(type)
|
2023-12-20 19:32:48 +00:00
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
#define DEVOPTAB_INIT_DIR_VARS(type) DEVOPTAB_INIT_VARS; \
|
|
|
|
if (!dirState) DEVOPTAB_SET_ERROR_AND_EXIT(EINVAL); \
|
|
|
|
DEVOPTAB_DECL_DIR_STATE(type)
|
2023-12-20 19:32:48 +00:00
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
#define DEVOPTAB_DEINIT_VARS devoptabControlMutex(false)
|
2023-12-20 19:32:48 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
bool initialized; ///< Device initialization flag.
|
|
|
|
char name[DEVOPTAB_MOUNT_NAME_LENGTH]; ///< Mount name string, without a trailing colon (:).
|
|
|
|
time_t mount_time; ///< Mount time.
|
|
|
|
devoptab_t device; ///< Devoptab virtual device interface. Provides a way to use libcstd I/O calls on the mounted filesystem.
|
2024-10-28 13:31:58 +00:00
|
|
|
void *fs_ctx; ///< Pointer to actual type-specific filesystem context (PartitionFileSystemContext, HashFileSystemContext, RomFileSystemContext, FATFS).
|
2023-12-20 19:32:48 +00:00
|
|
|
} DevoptabDeviceContext;
|
|
|
|
|
|
|
|
/// Mounts a virtual Partition FS device using the provided Partition FS context and a mount name.
|
|
|
|
bool devoptabMountPartitionFileSystemDevice(PartitionFileSystemContext *pfs_ctx, const char *name);
|
|
|
|
|
|
|
|
/// Mounts a virtual Hash FS device using the provided Hash FS context and a mount name.
|
|
|
|
bool devoptabMountHashFileSystemDevice(HashFileSystemContext *hfs_ctx, const char *name);
|
|
|
|
|
|
|
|
/// Mounts a virtual RomFS device using the provided RomFS context and a mount name.
|
|
|
|
bool devoptabMountRomFileSystemDevice(RomFileSystemContext *romfs_ctx, const char *name);
|
|
|
|
|
2024-10-28 13:31:58 +00:00
|
|
|
/// Mounts a virtual FatFs device using the provided FATFS object and a mount name.
|
|
|
|
bool devoptabMountFatFsDevice(FATFS *fatfs, const char *name);
|
|
|
|
|
2023-12-20 19:32:48 +00:00
|
|
|
/// Unmounts a previously mounted virtual device.
|
|
|
|
void devoptabUnmountDevice(const char *name);
|
|
|
|
|
|
|
|
/// Unmounts all previously mounted virtual devices.
|
|
|
|
void devoptabUnmountAllDevices(void);
|
|
|
|
|
|
|
|
/// (Un)locks the devoptab mutex. Used by filesystem-specific devoptab interfaces.
|
|
|
|
void devoptabControlMutex(bool lock);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __NXDT_DEVOPTAB_H__ */
|