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 | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 8 | FILE* block_device = NULL; |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 9 | size_t device_size = 0; |
| 10 | |
| 11 | |
| 12 | int devopen(const char* name, int* reopen) |
| 13 | { |
| 14 | block_device = fopen(name, "rwb"); |
| 15 | if (!block_device) { |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame^] | 16 | fprintf(stderr, "devopen: %s.\n", strerror(errno)); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 17 | return 0; |
| 18 | } |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame^] | 19 | int seek_res = 0; |
| 20 | seek_res |= fseek(block_device, 0, SEEK_END); |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 21 | device_size = ftell(block_device); |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame^] | 22 | seek_res |= fseek(block_device, 0, SEEK_SET); |
| 23 | if (seek_res || device_size == -1L || device_size % 512) { |
| 24 | fprintf(stderr, "devopen: Bad file %s.\n", name); |
| 25 | devclose(); |
| 26 | return 0; |
| 27 | } |
| 28 | |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 29 | return 1; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | void devclose(void) |
| 34 | { |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 35 | if (block_device) { |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 36 | fclose(block_device); |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 37 | } |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 38 | block_device = NULL; |
| 39 | device_size = 0; |
| 40 | } |
| 41 | |
| 42 | int devread(unsigned long sector, unsigned long byte_offset, unsigned long byte_len, void *buf) |
| 43 | { |
| 44 | if (!block_device) { |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 45 | fprintf(stderr, "devread: Device not open.\n"); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | const unsigned long offset = sector * 512 + byte_offset; |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 50 | if (offset + byte_len > device_size) { |
| 51 | fprintf(stderr, "devread: Attempt to read beyond device.\n"); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 55 | if (fseek(block_device, sector * 512 + byte_offset, SEEK_SET) != 0) { |
| 56 | fprintf(stderr, "devread: Failed to set offset on device.\n"); |
| 57 | return 0; |
| 58 | } |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 59 | if (fread(buf, byte_len, 1, block_device) != byte_len) { |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 60 | fprintf(stderr, "devread: Failed to read from device.\n"); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | //void dev_set_partition(unsigned long start, unsigned long size); |
| 69 | //void dev_get_partition(unsigned long *start, unsigned long *size); |
| 70 | |
| 71 | //int file_open(const char *filename); |
| 72 | //int file_read(void *buf, unsigned long len); |
| 73 | //unsigned long file_seek(unsigned long offset); |
| 74 | //unsigned long file_size(void); |
| 75 | //void file_set_size(unsigned long size); |
| 76 | //void file_close(void); |