diff options
author | leleraffa97@hotmail.it <leleraffa97@hotmail.it> | 2017-08-23 21:18:02 +0200 |
---|---|---|
committer | leleraffa97@hotmail.it <leleraffa97@hotmail.it> | 2017-08-23 21:18:02 +0200 |
commit | 3f92d0e29700fc2da45354a4cf3e3afe5c5245c2 (patch) | |
tree | cb6def68635ff2b4e4ab30c7e8fdbdf3e730bc05 /sw/programmer/linux/src/flash.c | |
parent | Filesystem struct order (diff) | |
parent | new programmer interface for linux (diff) | |
download | z80uPC-3f92d0e29700fc2da45354a4cf3e3afe5c5245c2.tar.gz z80uPC-3f92d0e29700fc2da45354a4cf3e3afe5c5245c2.zip |
Merge remote-tracking branch 'origin/naopross' into atlas
Diffstat (limited to 'sw/programmer/linux/src/flash.c')
-rw-r--r-- | sw/programmer/linux/src/flash.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/sw/programmer/linux/src/flash.c b/sw/programmer/linux/src/flash.c new file mode 100644 index 0000000..48189da --- /dev/null +++ b/sw/programmer/linux/src/flash.c @@ -0,0 +1,59 @@ +#include "flash.h" + +static int flash_serial_fd = -1; + +int flash_open(const char *path, unsigned long baudrate) +{ + flash_serial_fd = serial_open(path, baudrate); + + if (flash_serial_fd < 0) + return -1; + + return 0; +} + +int flash_write(const char *romfile, void (*log)(const char *)) +{ + int romfd; + ssize_t written; + + struct stat romst; + struct flash_blk head; + + uint8_t *buffer = malloc(FLASH_BLOCK_SIZE); + + romfd = open(romfile, O_RDONLY); + + if (fstat(romfd, &romst) != 0) + goto exit_romfd; + + while ((head.size = read(romfd, buffer, FLASH_BLOCK_SIZE))) { + head.addr = (uint16_t) lseek(romfd, 0, SEEK_CUR); + + written = write(flash_serial_fd, &head, sizeof(struct flash_blk)); + if (written < 0) { + // error + } + + written = write(flash_serial_fd, buffer, head.size); + if (written < 0) { + // error + } + + char logbuf[64]; + sprintf(logbuf, "[@] Written %d bytes at address %d\n", head.size, head.addr); + log(logbuf); + } + +exit_romfd: + close(romfd); + + free(buffer); + return 0; +} + +void flash_close(void) +{ + if (flash_serial_fd >= 0) + close(flash_serial_fd); +}
\ No newline at end of file |