2018-06-13 14:36:15 +02:00
|
|
|
#include "devoptab_sd.h"
|
2018-05-27 13:11:45 +02:00
|
|
|
|
|
|
|
ssize_t
|
|
|
|
__wut_fs_read(struct _reent *r,
|
|
|
|
void *fd,
|
|
|
|
char *ptr,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FSStatus rc;
|
2018-06-20 11:26:10 +02:00
|
|
|
uint32_t bytes, bytesRead = 0;
|
2018-05-27 13:11:45 +02:00
|
|
|
__wut_fs_file_t *file = (__wut_fs_file_t *)fd;
|
|
|
|
|
|
|
|
// Check that the file was opened with read access
|
|
|
|
if ((file->flags & O_ACCMODE) == O_WRONLY) {
|
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up command block
|
|
|
|
FSCmdBlock fsCmd;
|
|
|
|
FSInitCmdBlock(&fsCmd);
|
|
|
|
|
|
|
|
FSStat fsstat;
|
2018-06-13 14:36:15 +02:00
|
|
|
rc = FSGetStatFile(__wut_devoptab_sd_client, &fsCmd, file->fd, &fsstat, -1);
|
2018-05-27 13:11:45 +02:00
|
|
|
|
|
|
|
if(rc < 0) {
|
|
|
|
r->_errno = __wut_fs_translate_error(rc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy to internal buffer and read in chunks.
|
2018-06-20 11:26:10 +02:00
|
|
|
uint8_t *tmp_buffer = memalign(0x40, 8192);
|
2018-05-27 13:11:45 +02:00
|
|
|
|
|
|
|
while(len > 0) {
|
|
|
|
size_t toRead = len;
|
|
|
|
|
|
|
|
if (toRead > 8192) {
|
|
|
|
toRead = 8192;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the data
|
2018-06-13 14:36:15 +02:00
|
|
|
rc = FSReadFile(__wut_devoptab_sd_client, &fsCmd, tmp_buffer, 1, toRead, file->fd, 0, -1);
|
2018-05-27 13:11:45 +02:00
|
|
|
|
|
|
|
if(rc <= 0)
|
|
|
|
{
|
|
|
|
free(tmp_buffer);
|
|
|
|
|
|
|
|
// Return partial transfer
|
|
|
|
if (bytesRead > 0) {
|
|
|
|
return bytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->_errno = __wut_fs_translate_error(rc);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
bytes = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy to internal buffer
|
|
|
|
memcpy(ptr, tmp_buffer, bytes);
|
|
|
|
|
|
|
|
file->offset += bytes;
|
|
|
|
bytesRead += bytes;
|
|
|
|
ptr += bytes;
|
|
|
|
len -= bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(tmp_buffer);
|
|
|
|
return bytesRead;
|
|
|
|
}
|