Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 1 | |
| 2 | #include <stdio.h> |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 3 | #include <errno.h> |
| 4 | #include <string.h> |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 5 | |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 6 | #include "blockdev.h" |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 7 | |
Thomas Heijligen | dc1a84b | 2023-12-04 13:39:42 +0000 | [diff] [blame] | 8 | // private |
| 9 | static FILE* block_device = NULL; |
| 10 | static size_t device_size = 0; |
| 11 | |
| 12 | // from filo |
| 13 | unsigned long part_start = 0; |
| 14 | unsigned long part_length = 0; |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | int devopen(const char* name, int* reopen) |
| 18 | { |
| 19 | block_device = fopen(name, "rwb"); |
| 20 | if (!block_device) { |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 21 | fprintf(stderr, "devopen: %s.\n", strerror(errno)); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 22 | return 0; |
| 23 | } |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 24 | int seek_res = 0; |
| 25 | seek_res |= fseek(block_device, 0, SEEK_END); |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 26 | device_size = ftell(block_device); |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 27 | seek_res |= fseek(block_device, 0, SEEK_SET); |
| 28 | if (seek_res || device_size == -1L || device_size % 512) { |
| 29 | fprintf(stderr, "devopen: Bad file %s.\n", name); |
| 30 | devclose(); |
| 31 | return 0; |
| 32 | } |
| 33 | |
Thomas Heijligen | dc1a84b | 2023-12-04 13:39:42 +0000 | [diff] [blame] | 34 | part_start = 0; |
| 35 | part_length = device_size; |
| 36 | |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | void devclose(void) |
| 42 | { |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 43 | if (block_device) { |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 44 | fclose(block_device); |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 45 | } |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 46 | block_device = NULL; |
| 47 | device_size = 0; |
Thomas Heijligen | dc1a84b | 2023-12-04 13:39:42 +0000 | [diff] [blame] | 48 | part_start = 0; |
| 49 | part_length = 0; |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | int devread(unsigned long sector, unsigned long byte_offset, unsigned long byte_len, void *buf) |
| 53 | { |
| 54 | if (!block_device) { |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 55 | fprintf(stderr, "devread: Device not open.\n"); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | const unsigned long offset = sector * 512 + byte_offset; |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 60 | if (offset + byte_len > device_size) { |
| 61 | fprintf(stderr, "devread: Attempt to read beyond device.\n"); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 65 | if (fseek(block_device, sector * 512 + byte_offset, SEEK_SET) != 0) { |
| 66 | fprintf(stderr, "devread: Failed to set offset on device.\n"); |
| 67 | return 0; |
| 68 | } |
Nico Huber | a68ee40 | 2024-01-23 16:17:36 +0100 | [diff] [blame] | 69 | if (fread(buf, byte_len, 1, block_device) != 1) { |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 70 | fprintf(stderr, "devread: Failed to read from device.\n"); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
Thomas Heijligen | dc1a84b | 2023-12-04 13:39:42 +0000 | [diff] [blame] | 78 | void dev_set_partition(unsigned long start, unsigned long size) |
| 79 | { |
| 80 | if (start + size <= device_size) { |
| 81 | part_start = start; |
| 82 | part_length = size; |
| 83 | } |
| 84 | } |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 85 | |
Thomas Heijligen | dc1a84b | 2023-12-04 13:39:42 +0000 | [diff] [blame] | 86 | void dev_get_partition(unsigned long *start, unsigned long *size) |
| 87 | { |
| 88 | *start = part_start; |
| 89 | *size = part_length; |
| 90 | } |
| 91 | |