lfs_emubd.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Block device emulated on standard files
  3. *
  4. * Copyright (c) 2017 Christopher Haster
  5. * Distributed under the MIT license
  6. */
  7. #include "emubd/lfs_emubd.h"
  8. #include "emubd/lfs_cfg.h"
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <limits.h>
  14. #include <dirent.h>
  15. #include <sys/stat.h>
  16. // Block device emulated on existing filesystem
  17. int lfs_emubd_create(lfs_emubd_t *emu, const char *path) {
  18. memset(&emu->info, 0, sizeof(emu->info));
  19. memset(&emu->stats, 0, sizeof(emu->stats));
  20. // Allocate buffer for creating children files
  21. size_t pathlen = strlen(path);
  22. emu->path = malloc(pathlen + 1 + LFS_NAME_MAX + 1);
  23. if (!emu->path) {
  24. return -ENOMEM;
  25. }
  26. strcpy(emu->path, path);
  27. emu->path[pathlen] = '/';
  28. emu->path[pathlen + 1 + LFS_NAME_MAX] = '\0';
  29. emu->child = &emu->path[pathlen+1];
  30. strncpy(emu->child, "config", LFS_NAME_MAX);
  31. // Load config, erroring if it doesn't exist
  32. lfs_cfg_t cfg;
  33. int err = lfs_cfg_create(&cfg, emu->path);
  34. if (err) {
  35. return err;
  36. }
  37. emu->info.read_size = lfs_cfg_getu(&cfg, "read_size", 0);
  38. emu->info.prog_size = lfs_cfg_getu(&cfg, "prog_size", 0);
  39. emu->info.erase_size = lfs_cfg_getu(&cfg, "erase_size", 0);
  40. emu->info.total_size = lfs_cfg_getu(&cfg, "total_size", 0);
  41. lfs_cfg_destroy(&cfg);
  42. return 0;
  43. }
  44. void lfs_emubd_destroy(lfs_emubd_t *emu) {
  45. free(emu->path);
  46. }
  47. int lfs_emubd_read(lfs_emubd_t *emu, lfs_block_t block,
  48. lfs_off_t off, lfs_size_t size, void *buffer) {
  49. uint8_t *data = buffer;
  50. // Check if read is valid
  51. if (!(off % emu->info.read_size == 0 &&
  52. size % emu->info.read_size == 0 &&
  53. ((uint64_t)block*emu->info.erase_size + off + size
  54. < emu->info.total_size))) {
  55. return -EINVAL;
  56. }
  57. // Zero out buffer for debugging
  58. memset(data, 0, size);
  59. // Iterate over blocks until enough data is read
  60. while (size > 0) {
  61. snprintf(emu->child, LFS_NAME_MAX, "%d", block);
  62. size_t count = lfs_min(emu->info.erase_size - off, size);
  63. FILE *f = fopen(emu->path, "rb");
  64. if (!f && errno != ENOENT) {
  65. return -errno;
  66. }
  67. if (f) {
  68. int err = fseek(f, off, SEEK_SET);
  69. if (err) {
  70. return -errno;
  71. }
  72. size_t res = fread(data, 1, count, f);
  73. if (res < count && !feof(f)) {
  74. return -errno;
  75. }
  76. err = fclose(f);
  77. if (err) {
  78. return -errno;
  79. }
  80. }
  81. size -= count;
  82. data += count;
  83. block += 1;
  84. off = 0;
  85. }
  86. emu->stats.read_count += 1;
  87. return 0;
  88. }
  89. int lfs_emubd_prog(lfs_emubd_t *emu, lfs_block_t block,
  90. lfs_off_t off, lfs_size_t size, const void *buffer) {
  91. const uint8_t *data = buffer;
  92. // Check if write is valid
  93. if (!(off % emu->info.prog_size == 0 &&
  94. size % emu->info.prog_size == 0 &&
  95. ((uint64_t)block*emu->info.erase_size + off + size
  96. < emu->info.total_size))) {
  97. return -EINVAL;
  98. }
  99. // Iterate over blocks until enough data is read
  100. while (size > 0) {
  101. snprintf(emu->child, LFS_NAME_MAX, "%d", block);
  102. size_t count = lfs_min(emu->info.erase_size - off, size);
  103. FILE *f = fopen(emu->path, "r+b");
  104. if (!f && errno == ENOENT) {
  105. f = fopen(emu->path, "w+b");
  106. if (!f) {
  107. return -errno;
  108. }
  109. }
  110. int err = fseek(f, off, SEEK_SET);
  111. if (err) {
  112. return -errno;
  113. }
  114. size_t res = fwrite(data, 1, count, f);
  115. if (res < count) {
  116. return -errno;
  117. }
  118. err = fclose(f);
  119. if (err) {
  120. return -errno;
  121. }
  122. size -= count;
  123. data += count;
  124. block += 1;
  125. off = 0;
  126. }
  127. emu->stats.prog_count += 1;
  128. return 0;
  129. }
  130. int lfs_emubd_erase(lfs_emubd_t *emu, lfs_block_t block,
  131. lfs_off_t off, lfs_size_t size) {
  132. // Check if erase is valid
  133. if (!(off % emu->info.erase_size == 0 &&
  134. size % emu->info.erase_size == 0 &&
  135. ((uint64_t)block*emu->info.erase_size + off + size
  136. < emu->info.total_size))) {
  137. return -EINVAL;
  138. }
  139. // Iterate and erase blocks
  140. while (size > 0) {
  141. snprintf(emu->child, LFS_NAME_MAX, "%d", block);
  142. struct stat st;
  143. int err = stat(emu->path, &st);
  144. if (err && errno != ENOENT) {
  145. return -errno;
  146. }
  147. if (!err && S_ISREG(st.st_mode)) {
  148. int err = unlink(emu->path);
  149. if (err) {
  150. return -errno;
  151. }
  152. }
  153. size -= emu->info.erase_size;
  154. block += 1;
  155. off = 0;
  156. }
  157. emu->stats.erase_count += 1;
  158. return 0;
  159. }
  160. int lfs_emubd_sync(lfs_emubd_t *emu) {
  161. // Always in sync
  162. return 0;
  163. }
  164. int lfs_emubd_info(lfs_emubd_t *emu, struct lfs_bd_info *info) {
  165. *info = emu->info;
  166. return 0;
  167. }
  168. int lfs_emubd_stats(lfs_emubd_t *emu, struct lfs_bd_stats *stats) {
  169. *stats = emu->stats;
  170. return 0;
  171. }
  172. // Wrappers for void*s
  173. static int lfs_emubd_bd_read(void *bd, lfs_block_t block,
  174. lfs_off_t off, lfs_size_t size, void *buffer) {
  175. return lfs_emubd_read((lfs_emubd_t*)bd, block, off, size, buffer);
  176. }
  177. static int lfs_emubd_bd_prog(void *bd, lfs_block_t block,
  178. lfs_off_t off, lfs_size_t size, const void *buffer) {
  179. return lfs_emubd_prog((lfs_emubd_t*)bd, block, off, size, buffer);
  180. }
  181. static int lfs_emubd_bd_erase(void *bd, lfs_block_t block,
  182. lfs_off_t off, lfs_size_t size) {
  183. return lfs_emubd_erase((lfs_emubd_t*)bd, block, off, size);
  184. }
  185. static int lfs_emubd_bd_sync(void *bd) {
  186. return lfs_emubd_sync((lfs_emubd_t*)bd);
  187. }
  188. static int lfs_emubd_bd_info(void *bd, struct lfs_bd_info *info) {
  189. return lfs_emubd_info((lfs_emubd_t*)bd, info);
  190. }
  191. const struct lfs_bd_ops lfs_emubd_ops = {
  192. .read = lfs_emubd_bd_read,
  193. .prog = lfs_emubd_bd_prog,
  194. .erase = lfs_emubd_bd_erase,
  195. .sync = lfs_emubd_bd_sync,
  196. .info = lfs_emubd_bd_info,
  197. };