summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakaRikka <38417346+TakaRikka@users.noreply.github.com>2021-09-16 20:51:11 -0700
committerGitHub <noreply@github.com>2021-09-16 20:51:11 -0700
commitd1622d3cae13860823206322b63ea8841471b1d4 (patch)
treeb6d0fa88276562c7529dfad68366cf5c9952f407
parent65dae72e1ee0ad38bdef3186e3e5991f91664360 (diff)
parenta47b14ad44beaa47a4e90d4b5a1707ebbc893a4c (diff)
Merge pull request #5 from zsrtp/nand
added NAND functions
-rw-r--r--include/card.h27
-rw-r--r--include/nand.h37
-rw-r--r--include/storage.h78
-rw-r--r--src/card.c1
4 files changed, 116 insertions, 27 deletions
diff --git a/include/card.h b/include/card.h
index 2ce2329..6e1117d 100644
--- a/include/card.h
+++ b/include/card.h
@@ -10,25 +10,6 @@
#define CARD_ICON_MAX 8
#define SECTOR_SIZE INT32_MAX
-enum CardError {
- Ready = 0,
- Busy = -1,
- WrongDevice = -2,
- NoCard = -3,
- NoFile = -4,
- IoError = -5,
- Broken = -6,
- Exist = -7,
- NoEnt = -8,
- InsSpace = -9,
- NoPerm = -10,
- Limit = -11,
- NameTooLong = -12,
- Encoding = -13,
- Canceled = -14,
- FatalError = -128
-};
-
typedef void (*CARDCallback)(int32_t chn, int32_t result);
typedef struct CardInfo {
@@ -82,12 +63,4 @@ int32_t CARDDeleteAsync(int32_t chn, const char* fileName, CARDCallback callback
}
#endif // __cplusplus
-typedef struct Card {
- CardInfo card_info;
- int32_t sector_size;
- const char* file_name;
- int32_t card_result;
- char file_name_buffer[CARD_FILENAME_MAX * 2];
-} Card;
-
#endif // __CARD_H__ \ No newline at end of file
diff --git a/include/nand.h b/include/nand.h
new file mode 100644
index 0000000..789ddca
--- /dev/null
+++ b/include/nand.h
@@ -0,0 +1,37 @@
+#ifndef __NAND_H__
+#define __NAND_H__
+
+#include <stdint.h>
+
+#define NAND_FILENAME_MAX 32
+#define SECTOR_SIZE 0x400
+
+#define NAND_OPEN_READ 0x01
+#define NAND_OPEN_WRITE 0x02
+#define NAND_OPEN_RW (NAND_OPEN_READ | NAND_OPEN_WRITE)
+
+typedef struct NANDInfo {
+ uint8_t unk[0x90];
+} NANDInfo;
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+#ifdef WII_PLATFORM
+int32_t NANDCreate(const char *fileName, uint32_t unk2, int32_t unk3);
+int32_t NANDDelete(const char *fileName);
+int32_t NANDOpen(const char *fileName, NANDInfo *fileInfo, uint32_t mode);
+int32_t NANDSafeOpen(const char *fileName, NANDInfo *fileInfo, uint32_t mode, void* safeCopyBuf, uint32_t size);
+int32_t NANDClose(NANDInfo *fileInfo);
+int32_t NANDSafeClose(NANDInfo *fileInfo);
+int32_t NANDRead(NANDInfo *fileInfo, void *dest, uint32_t length);
+int32_t NANDWrite(NANDInfo *fileInfo, void *src, uint32_t length);
+int32_t NANDSeek(NANDInfo *fileInfo, uint32_t offset, int32_t unk1);
+#endif // WII_PLATFORM
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+extern uint8_t l_safeCopyBuf[0x4000];
+
+#endif // __NAND_H__ \ No newline at end of file
diff --git a/include/storage.h b/include/storage.h
new file mode 100644
index 0000000..9789a33
--- /dev/null
+++ b/include/storage.h
@@ -0,0 +1,78 @@
+#ifndef __STORAGE_H__
+#define __STORAGE_H__
+
+#include <stdint.h>
+
+#ifndef WII_PLATFORM
+#include "card.h"
+#define STORAGE_FILENAME_MAX CARD_FILENAME_MAX
+#define FileInfo CardInfo
+#define OPEN_MODE_READ
+#define OPEN_MODE_WRITE
+#define OPEN_MODE_RW
+#else // WII_PLATFORM
+#include "nand.h"
+#define STORAGE_FILENAME_MAX NAND_FILENAME_MAX
+#define FileInfo NANDInfo
+#define OPEN_MODE_READ NAND_OPEN_READ
+#define OPEN_MODE_WRITE NAND_OPEN_WRITE
+#define OPEN_MODE_RW NAND_OPEN_RW
+#endif // WII_PLATFORM
+
+enum StorageError {
+ Ready = 0,
+ Busy = -1,
+ WrongDevice = -2,
+ NoCard = -3,
+ NoFile = -4,
+ IoError = -5,
+ Broken = -6,
+ Exist = -7,
+ NoEnt = -8,
+ InsSpace = -9,
+ NoPerm = -10,
+ Limit = -11,
+ NameTooLong = -12,
+ Encoding = -13,
+ Canceled = -14,
+ FatalError = -128
+};
+
+typedef struct Storage {
+ FileInfo info;
+ int32_t sector_size;
+ const char* file_name;
+ int32_t result;
+ char file_name_buffer[STORAGE_FILENAME_MAX * 2];
+ uint32_t result_size;
+} Storage;
+
+
+#ifndef WII_PLATFORM
+#define StorageCreate(ch, fileName, size, fileBuffer) CARDCreate(ch, fileName, size, fileBuffer)
+#define StorageDelete(ch, fileName) CARDDelete(ch, fileName)
+#define StorageOpen(ch, fileName, fileInfo, mode) CARDOpen(ch, fileName, fileInfo)
+#define StorageClose(fileInfo) CARDClose(fileInfo)
+#define StorageRead(storage, data, length, offset) ({(storage).result = CARDRead(&(storage).info, data, length, offset);})
+#define StorageWrite(storage, data, length, offset) ({(storage).result = CARDWrite(&(storage).info, data, length, offset);})
+#else // WII_PLATFORM
+#define StorageCreate(ch, fileName, size, fileBuffer) NANDCreate(fileName, 0x34, 0)
+#define StorageDelete(ch, fileName) NANDDelete(fileName)
+#define StorageOpen(ch, fileName, fileInfo, mode) NANDOpen(fileName, fileInfo, mode)
+#define StorageClose(fileInfo) NANDClose(fileInfo)
+#define StorageRead(storage, data, length, offset) ({ \
+ (storage).result = NANDSeek(&(storage).info, offset, 0); \
+ if ((storage).result == Ready) { \
+ (storage).result_size = NANDRead(&(storage).info, data, length); \
+ } \
+})
+#define StorageWrite(storage, data, length, offset) ({ \
+ (storage).result = NANDSeek(&(storage).info, offset, 0); \
+ if ((storage).result == Ready) { \
+ (storage).result_size = NANDWrite(&(storage).info, data, length); \
+ } \
+})
+#endif // WII_PLATFORM
+
+
+#endif // __STORAGE_H__ \ No newline at end of file
diff --git a/src/card.c b/src/card.c
index ff85643..e704e1d 100644
--- a/src/card.c
+++ b/src/card.c
@@ -1,5 +1,6 @@
#include "card.h"
#include "card_internal.h"
+#include "storage.h"
#include <stddef.h>
// We declare those instead of using the standard headers to let