wutdevoptab: Implement devoptab using FSA functions

This commit is contained in:
Maschell
2022-08-09 12:53:58 +02:00
committed by Dave Murphy
parent e492c31d06
commit 4083224b51
28 changed files with 928 additions and 541 deletions

View File

@@ -1,13 +1,18 @@
#include "devoptab_fsa.h"
#include <mutex>
ssize_t __wut_fsa_write(struct _reent *r, void *fd, const char *ptr, size_t len) {
FSError status;
__wut_fsa_file_t *file;
__wut_fsa_device_t *deviceData;
ssize_t __wut_fs_write(struct _reent *r, void *fd, const char *ptr, size_t len) {
if (!fd || !ptr) {
r->_errno = EINVAL;
return -1;
}
// Check that the file was opened with write access
__wut_fs_file_t *file = (__wut_fs_file_t *) fd;
file = (__wut_fsa_file_t *) fd;
if ((file->flags & O_ACCMODE) == O_RDONLY) {
r->_errno = EBADF;
return -1;
@@ -16,12 +21,13 @@ ssize_t __wut_fs_write(struct _reent *r, void *fd, const char *ptr, size_t len)
// cache-aligned, cache-line-sized
__attribute__((aligned(0x40))) uint8_t alignedBuffer[0x40];
FSCmdBlock cmd;
FSInitCmdBlock(&cmd);
deviceData = (__wut_fsa_device_t *) r->deviceData;
std::scoped_lock lock(file->mutex);
// If O_APPEND is set, we always write to the end of the file.
// When writing we file->offset to the file size to keep in sync.
if(file->flags & O_APPEND) {
if (file->flags & O_APPEND) {
file->offset = file->appendOffset;
}
@@ -44,18 +50,24 @@ ssize_t __wut_fs_write(struct _reent *r, void *fd, const char *ptr, size_t len)
size &= ~0x3F;
}
// Limit each request to 256 KiB
if (size > 0x40000) {
size = 0x40000;
}
if (tmp == alignedBuffer) {
memcpy(tmp, ptr, size);
}
FSStatus status = FSWriteFile(__wut_devoptab_fs_client, &cmd, tmp, 1, size,
file->fd, 0, FS_ERROR_FLAG_ALL);
status = FSAWriteFile(deviceData->clientHandle, tmp, 1, size, file->fd, 0);
if (status < 0) {
WUT_DEBUG_REPORT("FSAWriteFile(0x%08X, 0x%08X, 1, 0x%08X, 0x%08X, 0) (%s) failed: %s\n",
deviceData->clientHandle, tmp, size, file->fd, file->fullPath, FSAGetStatusStr(status));
if (bytesWritten != 0) {
return bytesWritten; // error after partial write
}
r->_errno = __wut_fs_translate_error(status);
r->_errno = __wut_fsa_translate_error(status);
return -1;
}
@@ -64,7 +76,7 @@ ssize_t __wut_fs_write(struct _reent *r, void *fd, const char *ptr, size_t len)
bytesWritten += status;
ptr += status;
if (status != size) {
if ((size_t) status != size) {
return bytesWritten; // partial write
}
}