2016-04-04 22:45:49 +02:00
|
|
|
#include "image.h"
|
2016-11-29 16:33:56 +01:00
|
|
|
#include "sddata.h"
|
2016-07-21 00:30:26 +02:00
|
|
|
#include "ff.h"
|
2016-04-04 22:45:49 +02:00
|
|
|
|
2016-05-02 11:22:49 +02:00
|
|
|
static FIL mount_file;
|
|
|
|
static u32 mount_state = 0;
|
2016-04-04 22:45:49 +02:00
|
|
|
|
2016-11-29 16:33:56 +01:00
|
|
|
static char mount_path[256] = { 0 };
|
|
|
|
|
2016-11-26 14:25:10 +01:00
|
|
|
|
2016-11-30 23:45:36 +01:00
|
|
|
int ReadImageBytes(u8* buffer, u64 offset, u64 count) {
|
2016-04-04 22:45:49 +02:00
|
|
|
UINT bytes_read;
|
|
|
|
UINT ret;
|
2016-04-10 16:05:09 +02:00
|
|
|
if (!count) return -1;
|
2016-04-04 22:45:49 +02:00
|
|
|
if (!mount_state) return FR_INVALID_OBJECT;
|
2016-11-26 14:25:10 +01:00
|
|
|
if (f_tell(&mount_file) != offset) {
|
|
|
|
if (f_size(&mount_file) < offset) return -1;
|
|
|
|
f_lseek(&mount_file, offset);
|
2016-04-10 16:05:09 +02:00
|
|
|
}
|
2016-11-29 16:33:56 +01:00
|
|
|
ret = fx_read(&mount_file, buffer, count, &bytes_read);
|
2016-11-26 14:25:10 +01:00
|
|
|
return (ret != 0) ? (int) ret : (bytes_read != count) ? -1 : 0;
|
2016-04-04 22:45:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 23:45:36 +01:00
|
|
|
int WriteImageBytes(const u8* buffer, u64 offset, u64 count) {
|
2016-04-04 22:45:49 +02:00
|
|
|
UINT bytes_written;
|
|
|
|
UINT ret;
|
2016-04-10 16:05:09 +02:00
|
|
|
if (!count) return -1;
|
2016-04-04 22:45:49 +02:00
|
|
|
if (!mount_state) return FR_INVALID_OBJECT;
|
2016-11-26 14:25:10 +01:00
|
|
|
if (f_tell(&mount_file) != offset)
|
|
|
|
f_lseek(&mount_file, offset);
|
2016-11-29 16:33:56 +01:00
|
|
|
ret = fx_write(&mount_file, buffer, count, &bytes_written);
|
2016-11-26 14:25:10 +01:00
|
|
|
return (ret != 0) ? (int) ret : (bytes_written != count) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ReadImageSectors(u8* buffer, u32 sector, u32 count) {
|
|
|
|
return ReadImageBytes(buffer, sector * 0x200, count * 0x200);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WriteImageSectors(const u8* buffer, u32 sector, u32 count) {
|
|
|
|
return WriteImageBytes(buffer, sector * 0x200, count * 0x200);
|
2016-04-04 22:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int SyncImage(void) {
|
2016-12-13 16:00:14 +01:00
|
|
|
return mount_state ? f_sync(&mount_file) : FR_INVALID_OBJECT;
|
2016-04-04 22:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetMountSize(void) {
|
2016-12-13 16:00:14 +01:00
|
|
|
return mount_state ? f_size(&mount_file) : 0;
|
2016-04-04 22:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 GetMountState(void) {
|
|
|
|
return mount_state;
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:33:56 +01:00
|
|
|
const char* GetMountPath(void) {
|
|
|
|
return mount_path;
|
|
|
|
}
|
|
|
|
|
2016-04-04 22:45:49 +02:00
|
|
|
u32 MountImage(const char* path) {
|
2016-11-29 16:33:56 +01:00
|
|
|
u32 type = (path) ? IdentifyFileType(path) : 0;
|
2016-04-04 22:45:49 +02:00
|
|
|
if (mount_state) {
|
2016-12-13 16:00:14 +01:00
|
|
|
fx_close(&mount_file);
|
2016-04-06 12:52:23 +02:00
|
|
|
mount_state = 0;
|
2016-11-29 16:33:56 +01:00
|
|
|
*mount_path = 0;
|
2016-04-04 22:45:49 +02:00
|
|
|
}
|
2016-11-29 16:33:56 +01:00
|
|
|
if (!type) return 0;
|
2016-12-08 23:36:18 +01:00
|
|
|
if ((fx_open(&mount_file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK) &&
|
|
|
|
(fx_open(&mount_file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK))
|
2016-04-06 12:52:23 +02:00
|
|
|
return 0;
|
2016-04-04 22:45:49 +02:00
|
|
|
f_lseek(&mount_file, 0);
|
|
|
|
f_sync(&mount_file);
|
2016-11-29 16:33:56 +01:00
|
|
|
strncpy(mount_path, path, 255);
|
2016-11-25 13:34:01 +01:00
|
|
|
return (mount_state = type);
|
2016-04-04 22:45:49 +02:00
|
|
|
}
|