| |
| #include <stdio.h> |
| #include <errno.h> |
| #include <string.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) { |
| fprintf(stderr, "devopen: %s.\n", strerror(errno)); |
| return 0; |
| } |
| int seek_res = 0; |
| seek_res |= fseek(block_device, 0, SEEK_END); |
| device_size = ftell(block_device); |
| seek_res |= fseek(block_device, 0, SEEK_SET); |
| if (seek_res || device_size == -1L || device_size % 512) { |
| fprintf(stderr, "devopen: Bad file %s.\n", name); |
| devclose(); |
| return 0; |
| } |
| |
| 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); |