Skip to content

Commit f0f11f2

Browse files
committed
upd: boot splash, file read/write freeze; fix: lua shell commands
1 parent 3f553c7 commit f0f11f2

10 files changed

Lines changed: 449 additions & 31 deletions

File tree

ZenOS.vhd

0 Bytes
Binary file not shown.

src/drv/disk/fat.c

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "../../libk/core/mem.h"
2121
#include "../../libk/spinlock.h"
2222
#include "../../drv/rtc.h"
23+
#include "../../kernel/sched.h"
24+
25+
#define FAT_IO_CHUNK 4096U
2326

2427
DWORD get_fattime(void) {
2528
rtc_time_t t = rtc_get_time();
@@ -155,23 +158,49 @@ int fat_open(const char *path, int write) { return fat_open_vol(path, write, 0);
155158

156159
int fat_read(int fd, void *buf, uint32_t size, uint32_t *bytes_read) {
157160
if (!initialized || fd < 0 || fd >= FAT_MAX_FDS || !fd_table[fd].used) return -1;
158-
UINT br = 0;
159-
fat_lock();
160-
FRESULT fr = f_read(&fd_table[fd].fil, buf, size, &br);
161-
fat_unlock();
162-
if (bytes_read) *bytes_read = br;
163-
return (fr == FR_OK) ? 0 : -1;
161+
uint8_t *out = (uint8_t *)buf;
162+
uint32_t total = 0;
163+
while (total < size) {
164+
UINT br = 0;
165+
UINT chunk = (UINT)(size - total);
166+
if (chunk > FAT_IO_CHUNK)
167+
chunk = FAT_IO_CHUNK;
168+
fat_lock();
169+
FRESULT fr = f_read(&fd_table[fd].fil, out + total, chunk, &br);
170+
fat_unlock();
171+
total += br;
172+
if (fr != FR_OK) {
173+
if (bytes_read) *bytes_read = total;
174+
return total ? 0 : -1;
175+
}
176+
if (br == 0 || br < chunk)
177+
break;
178+
if (total < size)
179+
sched_yield();
180+
}
181+
if (bytes_read) *bytes_read = total;
182+
return 0;
164183
}
165184

166185
int fat_write(int fd, const void *buf, uint32_t size) {
167186
if (!initialized || fd < 0 || fd >= FAT_MAX_FDS || !fd_table[fd].used) return -1;
168187
if (!fd_table[fd].writable) return -1;
169-
UINT bw = 0;
170-
fat_lock();
171-
FRESULT fr = f_write(&fd_table[fd].fil, buf, size, &bw);
172-
fat_unlock();
173-
if (fr != FR_OK || bw != size) { log("Write fd=%d failed.", 2, 0, fd); return -1; }
174-
fd_table[fd].total_written += bw;
188+
const uint8_t *in = (const uint8_t *)buf;
189+
uint32_t total = 0;
190+
while (total < size) {
191+
UINT bw = 0;
192+
UINT chunk = (UINT)(size - total);
193+
if (chunk > FAT_IO_CHUNK)
194+
chunk = FAT_IO_CHUNK;
195+
fat_lock();
196+
FRESULT fr = f_write(&fd_table[fd].fil, in + total, chunk, &bw);
197+
fat_unlock();
198+
fd_table[fd].total_written += bw;
199+
total += bw;
200+
if (fr != FR_OK || bw != chunk) { log("Write fd=%d failed.", 2, 0, fd); return -1; }
201+
if (total < size)
202+
sched_yield();
203+
}
175204
return 0;
176205
}
177206

@@ -446,23 +475,50 @@ int fat_open_entry(const char *path, int write, fd_entry_t *out) { return fat_op
446475

447476
int fat_read_entry(fd_entry_t *e, void *buf, uint32_t size, uint32_t *bytes_read) {
448477
if (!e || !e->used || e->type != FD_FILE || !e->file) return -1;
449-
UINT br = 0;
450-
fat_lock();
451-
FRESULT fr = f_read(&e->file->fil, buf, size, &br);
452-
fat_unlock();
453-
if (bytes_read) *bytes_read = br;
454-
return (fr == FR_OK) ? 0 : -1;
478+
uint8_t *out = (uint8_t *)buf;
479+
uint32_t total = 0;
480+
while (total < size) {
481+
UINT br = 0;
482+
UINT chunk = (UINT)(size - total);
483+
if (chunk > FAT_IO_CHUNK)
484+
chunk = FAT_IO_CHUNK;
485+
fat_lock();
486+
FRESULT fr = f_read(&e->file->fil, out + total, chunk, &br);
487+
fat_unlock();
488+
total += br;
489+
if (fr != FR_OK) {
490+
if (bytes_read) *bytes_read = total;
491+
return total ? 0 : -1;
492+
}
493+
if (br == 0 || br < chunk)
494+
break;
495+
if (total < size)
496+
sched_yield();
497+
}
498+
if (bytes_read) *bytes_read = total;
499+
return 0;
455500
}
456501

457502
int fat_write_entry(fd_entry_t *e, const void *buf, uint32_t size) {
458503
if (!e || !e->used || e->type != FD_FILE || !e->file) return -1;
459504
if (!e->file->writable) return -1;
460-
UINT bw = 0;
461-
fat_lock();
462-
FRESULT fr = f_write(&e->file->fil, buf, size, &bw);
463-
fat_unlock();
464-
if (fr != FR_OK || bw != size) return -1;
465-
e->file->total_written += bw;
505+
const uint8_t *in = (const uint8_t *)buf;
506+
uint32_t total = 0;
507+
while (total < size) {
508+
UINT bw = 0;
509+
UINT chunk = (UINT)(size - total);
510+
if (chunk > FAT_IO_CHUNK)
511+
chunk = FAT_IO_CHUNK;
512+
fat_lock();
513+
FRESULT fr = f_write(&e->file->fil, in + total, chunk, &bw);
514+
fat_unlock();
515+
e->file->total_written += bw;
516+
total += bw;
517+
if (fr != FR_OK || bw != chunk)
518+
return -1;
519+
if (total < size)
520+
sched_yield();
521+
}
466522
return 0;
467523
}
468524

0 commit comments

Comments
 (0)