blob: 43c990c10f881c86a2a8ce56172fdb6a28b73796 [file] [log] [blame]
Thomas Heijligend1e04572023-11-27 14:28:55 +00001
2#include <stdio.h>
Thomas Heijligenb00b3162023-11-29 10:02:31 +00003#include <errno.h>
4#include <string.h>
Thomas Heijligend1e04572023-11-27 14:28:55 +00005
Thomas Heijligen62268ee2023-11-27 15:10:41 +00006#include "blockdev.h"
Thomas Heijligend1e04572023-11-27 14:28:55 +00007
Thomas Heijligen62268ee2023-11-27 15:10:41 +00008FILE* block_device = NULL;
Thomas Heijligend1e04572023-11-27 14:28:55 +00009size_t device_size = 0;
10
11
12int devopen(const char* name, int* reopen)
13{
14 block_device = fopen(name, "rwb");
15 if (!block_device) {
Thomas Heijligenb00b3162023-11-29 10:02:31 +000016 fprintf(stderr, "devopen: %s.\n", strerror(errno));
Thomas Heijligend1e04572023-11-27 14:28:55 +000017 return 0;
18 }
Thomas Heijligenb00b3162023-11-29 10:02:31 +000019 int seek_res = 0;
20 seek_res |= fseek(block_device, 0, SEEK_END);
Thomas Heijligen62268ee2023-11-27 15:10:41 +000021 device_size = ftell(block_device);
Thomas Heijligenb00b3162023-11-29 10:02:31 +000022 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 Heijligend1e04572023-11-27 14:28:55 +000029 return 1;
30}
31
32
33void devclose(void)
34{
Thomas Heijligen62268ee2023-11-27 15:10:41 +000035 if (block_device) {
Thomas Heijligend1e04572023-11-27 14:28:55 +000036 fclose(block_device);
Thomas Heijligen62268ee2023-11-27 15:10:41 +000037 }
Thomas Heijligend1e04572023-11-27 14:28:55 +000038 block_device = NULL;
39 device_size = 0;
40}
41
42int devread(unsigned long sector, unsigned long byte_offset, unsigned long byte_len, void *buf)
43{
44 if (!block_device) {
Thomas Heijligen62268ee2023-11-27 15:10:41 +000045 fprintf(stderr, "devread: Device not open.\n");
Thomas Heijligend1e04572023-11-27 14:28:55 +000046 return 0;
47 }
48
49 const unsigned long offset = sector * 512 + byte_offset;
Thomas Heijligen62268ee2023-11-27 15:10:41 +000050 if (offset + byte_len > device_size) {
51 fprintf(stderr, "devread: Attempt to read beyond device.\n");
Thomas Heijligend1e04572023-11-27 14:28:55 +000052 return 0;
53 }
54
Thomas Heijligen62268ee2023-11-27 15:10:41 +000055 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 Heijligend1e04572023-11-27 14:28:55 +000059 if (fread(buf, byte_len, 1, block_device) != byte_len) {
Thomas Heijligen62268ee2023-11-27 15:10:41 +000060 fprintf(stderr, "devread: Failed to read from device.\n");
Thomas Heijligend1e04572023-11-27 14:28:55 +000061 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);