| |
| #include <stdio.h> |
| |
| #include "blockdev.h" |
| |
| FILE* block_device = NULL; |
| size_t device_size = 0; |
| |
| |
| int devopen(const char* name, int* reopen) |
| { |
| block_device = fopen(name, "rwb"); |
| if (!block_device) { |
| return 0; |
| } |
| fseek(block_device, 0, SEEK_END); |
| device_size = ftell(block_device); |
| return 1; |
| } |
| |
| |
| void devclose(void) |
| { |
| if (block_device) { |
| fclose(block_device); |
| } |
| block_device = NULL; |
| device_size = 0; |
| } |
| |
| int devread(unsigned long sector, unsigned long byte_offset, unsigned long byte_len, void *buf) |
| { |
| if (!block_device) { |
| fprintf(stderr, "devread: Device not open.\n"); |
| return 0; |
| } |
| |
| const unsigned long offset = sector * 512 + byte_offset; |
| if (offset + byte_len > device_size) { |
| fprintf(stderr, "devread: Attempt to read beyond device.\n"); |
| return 0; |
| } |
| |
| if (fseek(block_device, sector * 512 + byte_offset, SEEK_SET) != 0) { |
| fprintf(stderr, "devread: Failed to set offset on device.\n"); |
| return 0; |
| } |
| if (fread(buf, byte_len, 1, block_device) != byte_len) { |
| fprintf(stderr, "devread: Failed to read from device.\n"); |
| return 0; |
| } |
| return 1; |
| } |
| |
| |
| |
| //void dev_set_partition(unsigned long start, unsigned long size); |
| //void dev_get_partition(unsigned long *start, unsigned long *size); |
| |
| //int file_open(const char *filename); |
| //int file_read(void *buf, unsigned long len); |
| //unsigned long file_seek(unsigned long offset); |
| //unsigned long file_size(void); |
| //void file_set_size(unsigned long size); |
| //void file_close(void); |