diff options
Diffstat (limited to 'machine/htif.c')
-rw-r--r-- | machine/htif.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/machine/htif.c b/machine/htif.c index fa3db53..18457d6 100644 --- a/machine/htif.c +++ b/machine/htif.c @@ -2,6 +2,8 @@ #include "atomic.h" #include "mtrap.h" +#include <string.h> + volatile uint64_t tohost __attribute__((section(".htif"))); volatile uint64_t fromhost __attribute__((section(".htif"))); volatile int htif_console_buf; @@ -85,3 +87,58 @@ void htif_poweroff() tohost = 1; } } + +struct request { + uint64_t addr; + uint64_t offset; + uint64_t size; + uint64_t tag; +}; + +void htif_disk_read(uintptr_t addr, uintptr_t offset, size_t size) +{ + struct request req; + + req.addr = addr; + req.offset = offset; + req.size = size; + req.tag = 0; + + do_tohost_fromhost(2, 0, (uintptr_t) &req); +} + +void htif_disk_write(uintptr_t addr, uintptr_t offset, size_t size) +{ + struct request req; + + req.addr = addr; + req.offset = offset; + req.size = size; + req.tag = 0; + + do_tohost_fromhost(2, 1, (uintptr_t) &req); +} + +unsigned long htif_disk_size(void) +{ + char idbuf[128]; + uintptr_t addr = (uintptr_t) idbuf; + unsigned long payload; + char *id = idbuf, *s; + + // The buffer address needs to be aligned to 64 bytes + if (addr % 64 != 0) { + unsigned long inc = 64 - (addr % 64); + addr += inc; + id += inc; + } + + payload = (addr << 8) | 0xff; + do_tohost_fromhost(2, 255, payload); + + s = strstr(id, "size="); + if (s == NULL) + return 0; + s += 5; + return atol(s); +} |