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