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