| |
| #include <stdio.h> |
| #include <errno.h> |
| #include <string.h> |
| |
| #include "blockdev.h" |
| |
| // private |
| static FILE* block_device = NULL; |
| static size_t device_size = 0; |
| |
| // from filo |
| unsigned long part_start = 0; |
| unsigned long part_length = 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; |
| } |
| |
| part_start = 0; |
| part_length = device_size; |
| |
| return 1; |
| } |
| |
| |
| void devclose(void) |
| { |
| if (block_device) { |
| fclose(block_device); |
| } |
| block_device = NULL; |
| device_size = 0; |
| part_start = 0; |
| part_length = 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) != 1) { |
| fprintf(stderr, "devread: Failed to read from device.\n"); |
| return 0; |
| } |
| return 1; |
| } |
| |
| |
| |
| void dev_set_partition(unsigned long start, unsigned long size) |
| { |
| if (start + size <= device_size) { |
| part_start = start; |
| part_length = size; |
| } |
| } |
| |
| void dev_get_partition(unsigned long *start, unsigned long *size) |
| { |
| *start = part_start; |
| *size = part_length; |
| } |
| |