2022-09-03 23:29:22 +02:00
|
|
|
#include "logger.h"
|
2022-03-03 23:50:19 +01:00
|
|
|
#include "sdutils/sdutils.h"
|
|
|
|
#include <coreinit/debug.h>
|
|
|
|
#include <coreinit/dynload.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
static OSDynLoad_Module sModuleHandle = nullptr;
|
|
|
|
|
2022-09-03 21:51:38 +02:00
|
|
|
static SDUtilsStatus (*sSDUtilsGetVersion)(SDUtilsVersion *) = nullptr;
|
2022-04-28 19:47:16 +02:00
|
|
|
|
2022-03-03 23:50:19 +01:00
|
|
|
static bool (*sSDUtilsAddAttachHandler)(SDAttachHandlerFn) = nullptr;
|
|
|
|
static bool (*sSDUtilsRemoveAttachHandler)(SDAttachHandlerFn) = nullptr;
|
|
|
|
|
2022-04-28 19:47:43 +02:00
|
|
|
static bool (*sSDUtilsAddCleanUpHandlesHandler)(SDCleanUpHandlesHandlerFn) = nullptr;
|
|
|
|
static bool (*sSDUtilsRemoveCleanUpHandlesHandler)(SDCleanUpHandlesHandlerFn) = nullptr;
|
|
|
|
|
2022-09-03 21:51:38 +02:00
|
|
|
static SDUtilsVersion sSDUtilsVersion = SD_UTILS_MODULE_VERSION_ERROR;
|
|
|
|
|
2022-09-03 19:08:41 +02:00
|
|
|
SDUtilsStatus SDUtils_InitLibrary() {
|
2022-03-03 23:50:19 +01:00
|
|
|
if (OSDynLoad_Acquire("homebrew_sdhotswap", &sModuleHandle) != OS_DYNLOAD_OK) {
|
2022-09-03 23:30:46 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("OSDynLoad_Acquire homebrew_sdhotswap failed.");
|
2022-03-03 23:50:19 +01:00
|
|
|
return SDUTILS_RESULT_MODULE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:47:16 +02:00
|
|
|
if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsGetVersion", (void **) &sSDUtilsGetVersion) != OS_DYNLOAD_OK) {
|
2022-09-03 23:30:46 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsGetVersion failed.");
|
2022-04-28 19:47:16 +02:00
|
|
|
return SDUTILS_RESULT_MODULE_MISSING_EXPORT;
|
|
|
|
}
|
2022-09-03 21:51:38 +02:00
|
|
|
|
|
|
|
auto res = SDUtils_GetVersion(&sSDUtilsVersion);
|
|
|
|
if (res != SDUTILS_RESULT_SUCCESS) {
|
2022-04-28 19:47:16 +02:00
|
|
|
return SDUTILS_RESULT_UNSUPPORTED_VERSION;
|
|
|
|
}
|
|
|
|
|
2022-03-03 23:50:19 +01:00
|
|
|
if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddAttachHandler", (void **) &sSDUtilsAddAttachHandler) != OS_DYNLOAD_OK) {
|
2022-09-03 23:30:46 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsAddAttachHandler failed.");
|
2022-09-03 23:38:42 +02:00
|
|
|
sSDUtilsAddAttachHandler = nullptr;
|
2022-03-03 23:50:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveAttachHandler", (void **) &sSDUtilsRemoveAttachHandler) != OS_DYNLOAD_OK) {
|
2022-09-03 23:30:46 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsRemoveAttachHandler failed.");
|
2022-09-03 23:38:42 +02:00
|
|
|
sSDUtilsRemoveAttachHandler = nullptr;
|
2022-03-03 23:50:19 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:47:43 +02:00
|
|
|
if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsAddCleanUpHandlesHandler", (void **) &sSDUtilsAddCleanUpHandlesHandler) != OS_DYNLOAD_OK) {
|
2022-09-03 23:30:46 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsAddCleanUpHandlesHandler failed.");
|
2022-09-03 23:38:42 +02:00
|
|
|
sSDUtilsAddCleanUpHandlesHandler = nullptr;
|
2022-04-28 19:47:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsRemoveCleanUpHandlesHandler", (void **) &sSDUtilsRemoveCleanUpHandlesHandler) != OS_DYNLOAD_OK) {
|
2022-09-03 23:30:46 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("FindExport SDUtilsRemoveCleanUpHandlesHandler failed.");
|
2022-09-03 23:38:42 +02:00
|
|
|
sSDUtilsRemoveCleanUpHandlesHandler = nullptr;
|
2022-04-28 19:47:43 +02:00
|
|
|
}
|
|
|
|
|
2022-03-03 23:50:19 +01:00
|
|
|
return SDUTILS_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-09-03 19:08:41 +02:00
|
|
|
SDUtilsStatus SDUtils_DeInitLibrary() {
|
|
|
|
// We don't need to release the OSDynLoad handle for modules.
|
|
|
|
return SDUTILS_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:51:38 +02:00
|
|
|
SDUtilsStatus GetVersion(SDUtilsVersion *);
|
2022-09-03 23:32:16 +02:00
|
|
|
SDUtilsStatus SDUtils_GetVersion(SDUtilsVersion *outVersion) {
|
|
|
|
if (sSDUtilsGetVersion == nullptr) {
|
|
|
|
if (OSDynLoad_Acquire("homebrew_sdhotswap", &sModuleHandle) != OS_DYNLOAD_OK) {
|
|
|
|
DEBUG_FUNCTION_LINE_WARN("OSDynLoad_Acquire failed.");
|
|
|
|
return SDUTILS_RESULT_MODULE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OSDynLoad_FindExport(sModuleHandle, FALSE, "SDUtilsGetVersion", (void **) &sSDUtilsGetVersion) != OS_DYNLOAD_OK) {
|
|
|
|
DEBUG_FUNCTION_LINE_WARN("FindExport SDUtilsGetVersion failed.");
|
|
|
|
return SDUTILS_RESULT_MODULE_MISSING_EXPORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (outVersion == nullptr) {
|
2022-09-03 21:51:38 +02:00
|
|
|
return SDUTILS_RESULT_INVALID_ARGUMENT;
|
2022-04-28 19:47:16 +02:00
|
|
|
}
|
2022-09-03 23:32:16 +02:00
|
|
|
return reinterpret_cast<decltype(&GetVersion)>(sSDUtilsGetVersion)(outVersion);
|
2022-04-28 19:47:16 +02:00
|
|
|
}
|
|
|
|
|
2022-03-03 23:50:19 +01:00
|
|
|
SDUtilsStatus SDUtils_IsSdCardMounted(bool *status) {
|
|
|
|
if (status == nullptr) {
|
|
|
|
return SDUTILS_RESULT_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
auto dir = opendir("fs:/vol/external01/");
|
|
|
|
if (dir != nullptr) {
|
|
|
|
closedir(dir);
|
|
|
|
*status = true;
|
|
|
|
} else {
|
|
|
|
*status = false;
|
|
|
|
}
|
|
|
|
return SDUTILS_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AddAttachHandler(SDAttachHandlerFn);
|
|
|
|
|
|
|
|
SDUtilsStatus SDUtils_AddAttachHandler(SDAttachHandlerFn fn) {
|
2022-09-03 23:38:42 +02:00
|
|
|
if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) {
|
2022-03-03 23:50:19 +01:00
|
|
|
return SDUTILS_RESULT_LIB_UNINITIALIZED;
|
|
|
|
}
|
2022-09-03 23:38:42 +02:00
|
|
|
if (sSDUtilsAddAttachHandler == nullptr || sSDUtilsVersion < 1) {
|
|
|
|
return SDUTILS_RESULT_UNSUPPORTED_COMMAND;
|
|
|
|
}
|
2022-03-03 23:50:19 +01:00
|
|
|
auto res = reinterpret_cast<decltype(&AddAttachHandler)>(sSDUtilsAddAttachHandler)(fn);
|
|
|
|
return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoveAttachHandler(SDAttachHandlerFn);
|
|
|
|
|
|
|
|
SDUtilsStatus SDUtils_RemoveAttachHandler(SDAttachHandlerFn fn) {
|
2022-09-03 23:38:42 +02:00
|
|
|
if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) {
|
2022-03-03 23:50:19 +01:00
|
|
|
return SDUTILS_RESULT_LIB_UNINITIALIZED;
|
|
|
|
}
|
2022-09-03 23:38:42 +02:00
|
|
|
if (sSDUtilsRemoveAttachHandler == nullptr || sSDUtilsVersion < 1) {
|
|
|
|
return SDUTILS_RESULT_UNSUPPORTED_COMMAND;
|
|
|
|
}
|
2022-03-03 23:50:19 +01:00
|
|
|
auto res = reinterpret_cast<decltype(&RemoveAttachHandler)>(sSDUtilsRemoveAttachHandler)(fn);
|
|
|
|
return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND;
|
2022-04-28 19:47:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn);
|
|
|
|
|
|
|
|
SDUtilsStatus SDUtils_AddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) {
|
2022-09-03 23:38:42 +02:00
|
|
|
if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) {
|
2022-04-28 19:47:43 +02:00
|
|
|
return SDUTILS_RESULT_LIB_UNINITIALIZED;
|
|
|
|
}
|
2022-09-03 23:38:42 +02:00
|
|
|
if (sSDUtilsAddCleanUpHandlesHandler == nullptr || sSDUtilsVersion < 1) {
|
|
|
|
return SDUTILS_RESULT_UNSUPPORTED_COMMAND;
|
|
|
|
}
|
2022-04-28 19:47:43 +02:00
|
|
|
auto res = reinterpret_cast<decltype(&AddCleanUpHandlesHandler)>(sSDUtilsAddCleanUpHandlesHandler)(fn);
|
|
|
|
return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_MAX_CALLBACKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn);
|
|
|
|
|
|
|
|
SDUtilsStatus SDUtils_RemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) {
|
2022-09-03 23:38:42 +02:00
|
|
|
if (sSDUtilsVersion == SD_UTILS_MODULE_VERSION_ERROR) {
|
2022-04-28 19:47:43 +02:00
|
|
|
return SDUTILS_RESULT_LIB_UNINITIALIZED;
|
|
|
|
}
|
2022-09-03 23:38:42 +02:00
|
|
|
if (sSDUtilsRemoveCleanUpHandlesHandler == nullptr || sSDUtilsVersion < 1) {
|
|
|
|
return SDUTILS_RESULT_UNSUPPORTED_COMMAND;
|
|
|
|
}
|
2022-04-28 19:47:43 +02:00
|
|
|
auto res = reinterpret_cast<decltype(&RemoveCleanUpHandlesHandler)>(sSDUtilsRemoveCleanUpHandlesHandler)(fn);
|
|
|
|
return res ? SDUTILS_RESULT_SUCCESS : SDUTILS_RESULT_NOT_FOUND;
|
2022-03-03 23:50:19 +01:00
|
|
|
}
|