diff --git a/src/libwhb/include/whb/sdcard.h b/src/libwhb/include/whb/sdcard.h new file mode 100644 index 00000000..bee1a8c4 --- /dev/null +++ b/src/libwhb/include/whb/sdcard.h @@ -0,0 +1,27 @@ +#pragma once +#include + +/** + * \defgroup whb_sdcard SDCard Access + * \ingroup whb + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +BOOL +WHBMountSdCard(); + +char * +WHBGetSdCardMountPath(); + +BOOL +WHBUnmountSdCard(); + +#ifdef __cplusplus +} +#endif + +/** @} */ diff --git a/src/libwhb/src/sdcard.c b/src/libwhb/src/sdcard.c new file mode 100644 index 00000000..3e2e1290 --- /dev/null +++ b/src/libwhb/src/sdcard.c @@ -0,0 +1,78 @@ +#include +#include +#include + +static BOOL +sMounted = FALSE; + +static char +sMountPath[128] = { 0 }; + +static FSClient +sClient; + +BOOL +WHBMountSdCard() +{ + FSCmdBlock cmd; + FSMountSource mountSource; + FSStatus result; + + if (sMounted) { + return TRUE; + } + + FSInit(); + + result = FSAddClient(&sClient, -1); + if (result != FS_STATUS_OK) { + WHBLogPrintf("%s: FSAddClient error %d", __FUNCTION__, result); + return FALSE; + } + + FSInitCmdBlock(&cmd); + result = FSGetMountSource(&sClient, &cmd, FS_MOUNT_SOURCE_SD, &mountSource, -1); + if (result < 0) { + WHBLogPrintf("%s: FSGetMountSource error %d", __FUNCTION__, result); + goto fail; + } + + result = FSMount(&sClient, &cmd, &mountSource, sMountPath, sizeof(sMountPath), -1); + if (result < 0) { + WHBLogPrintf("%s: FSMount error %d", __FUNCTION__, result); + goto fail; + } + + sMounted = TRUE; + return TRUE; + +fail: + FSDelClient(&sClient, -1); + return FALSE; +} + +char * +WHBGetSdCardMountPath() +{ + return sMountPath; +} + +BOOL +WHBUnmountSdCard() +{ + FSCmdBlock cmd; + FSStatus result; + + if (!sMounted) { + return TRUE; + } + + result = FSUnmount(&sClient, &cmd, sMountPath, -1); + if (result < 0) { + WHBLogPrintf("%s: FSUnmount error %d", __FUNCTION__, result); + return FALSE; + } + + sMounted = FALSE; + return TRUE; +}