lfs_emubd.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Block device emulated on standard files
  3. *
  4. * Copyright (c) 2017, Arm Limited. All rights reserved.
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include "emubd/lfs_emubd.h"
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <limits.h>
  13. #include <dirent.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. #include <assert.h>
  17. #include <stdbool.h>
  18. #include <inttypes.h>
  19. // Emulated block device utils
  20. static inline void lfs_emubd_tole32(lfs_emubd_t *emu) {
  21. emu->cfg.read_size = lfs_tole32(emu->cfg.read_size);
  22. emu->cfg.prog_size = lfs_tole32(emu->cfg.prog_size);
  23. emu->cfg.block_size = lfs_tole32(emu->cfg.block_size);
  24. emu->cfg.block_count = lfs_tole32(emu->cfg.block_count);
  25. emu->stats.read_count = lfs_tole32(emu->stats.read_count);
  26. emu->stats.prog_count = lfs_tole32(emu->stats.prog_count);
  27. emu->stats.erase_count = lfs_tole32(emu->stats.erase_count);
  28. for (unsigned i = 0; i < sizeof(emu->history.blocks) /
  29. sizeof(emu->history.blocks[0]); i++) {
  30. emu->history.blocks[i] = lfs_tole32(emu->history.blocks[i]);
  31. }
  32. }
  33. static inline void lfs_emubd_fromle32(lfs_emubd_t *emu) {
  34. emu->cfg.read_size = lfs_fromle32(emu->cfg.read_size);
  35. emu->cfg.prog_size = lfs_fromle32(emu->cfg.prog_size);
  36. emu->cfg.block_size = lfs_fromle32(emu->cfg.block_size);
  37. emu->cfg.block_count = lfs_fromle32(emu->cfg.block_count);
  38. emu->stats.read_count = lfs_fromle32(emu->stats.read_count);
  39. emu->stats.prog_count = lfs_fromle32(emu->stats.prog_count);
  40. emu->stats.erase_count = lfs_fromle32(emu->stats.erase_count);
  41. for (unsigned i = 0; i < sizeof(emu->history.blocks) /
  42. sizeof(emu->history.blocks[0]); i++) {
  43. emu->history.blocks[i] = lfs_fromle32(emu->history.blocks[i]);
  44. }
  45. }
  46. // Block device emulated on existing filesystem
  47. int lfs_emubd_create(const struct lfs_config *cfg, const char *path) {
  48. lfs_emubd_t *emu = cfg->context;
  49. emu->cfg.read_size = cfg->read_size;
  50. emu->cfg.prog_size = cfg->prog_size;
  51. emu->cfg.block_size = cfg->block_size;
  52. emu->cfg.block_count = cfg->block_count;
  53. // Allocate buffer for creating children files
  54. size_t pathlen = strlen(path);
  55. emu->path = malloc(pathlen + 1 + LFS_NAME_MAX + 1);
  56. if (!emu->path) {
  57. return -ENOMEM;
  58. }
  59. strcpy(emu->path, path);
  60. emu->path[pathlen] = '/';
  61. emu->child = &emu->path[pathlen+1];
  62. memset(emu->child, '\0', LFS_NAME_MAX+1);
  63. // Create directory if it doesn't exist
  64. int err = mkdir(path, 0777);
  65. if (err && errno != EEXIST) {
  66. return -errno;
  67. }
  68. // Load stats to continue incrementing
  69. snprintf(emu->child, LFS_NAME_MAX, ".stats");
  70. FILE *f = fopen(emu->path, "r");
  71. if (!f) {
  72. memset(&emu->stats, 0, sizeof(emu->stats));
  73. } else {
  74. size_t res = fread(&emu->stats, sizeof(emu->stats), 1, f);
  75. lfs_emubd_fromle32(emu);
  76. if (res < 1) {
  77. return -errno;
  78. }
  79. err = fclose(f);
  80. if (err) {
  81. return -errno;
  82. }
  83. }
  84. // Load history
  85. snprintf(emu->child, LFS_NAME_MAX, ".history");
  86. f = fopen(emu->path, "r");
  87. if (!f) {
  88. memset(&emu->history, 0, sizeof(emu->history));
  89. } else {
  90. size_t res = fread(&emu->history, sizeof(emu->history), 1, f);
  91. lfs_emubd_fromle32(emu);
  92. if (res < 1) {
  93. return -errno;
  94. }
  95. err = fclose(f);
  96. if (err) {
  97. return -errno;
  98. }
  99. }
  100. return 0;
  101. }
  102. void lfs_emubd_destroy(const struct lfs_config *cfg) {
  103. lfs_emubd_sync(cfg);
  104. lfs_emubd_t *emu = cfg->context;
  105. free(emu->path);
  106. }
  107. int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
  108. lfs_off_t off, void *buffer, lfs_size_t size) {
  109. lfs_emubd_t *emu = cfg->context;
  110. uint8_t *data = buffer;
  111. // Check if read is valid
  112. assert(off % cfg->read_size == 0);
  113. assert(size % cfg->read_size == 0);
  114. assert(block < cfg->block_count);
  115. // Zero out buffer for debugging
  116. memset(data, 0, size);
  117. // Read data
  118. snprintf(emu->child, LFS_NAME_MAX, "%" PRIx32, block);
  119. FILE *f = fopen(emu->path, "rb");
  120. if (!f && errno != ENOENT) {
  121. return -errno;
  122. }
  123. if (f) {
  124. int err = fseek(f, off, SEEK_SET);
  125. if (err) {
  126. return -errno;
  127. }
  128. size_t res = fread(data, 1, size, f);
  129. if (res < size && !feof(f)) {
  130. return -errno;
  131. }
  132. err = fclose(f);
  133. if (err) {
  134. return -errno;
  135. }
  136. }
  137. emu->stats.read_count += 1;
  138. return 0;
  139. }
  140. int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
  141. lfs_off_t off, const void *buffer, lfs_size_t size) {
  142. lfs_emubd_t *emu = cfg->context;
  143. const uint8_t *data = buffer;
  144. // Check if write is valid
  145. assert(off % cfg->prog_size == 0);
  146. assert(size % cfg->prog_size == 0);
  147. assert(block < cfg->block_count);
  148. // Program data
  149. snprintf(emu->child, LFS_NAME_MAX, "%" PRIx32, block);
  150. FILE *f = fopen(emu->path, "r+b");
  151. if (!f) {
  152. return (errno == EACCES) ? 0 : -errno;
  153. }
  154. // Check that file was erased
  155. assert(f);
  156. int err = fseek(f, off, SEEK_SET);
  157. if (err) {
  158. return -errno;
  159. }
  160. size_t res = fwrite(data, 1, size, f);
  161. if (res < size) {
  162. return -errno;
  163. }
  164. err = fseek(f, off, SEEK_SET);
  165. if (err) {
  166. return -errno;
  167. }
  168. uint8_t dat;
  169. res = fread(&dat, 1, 1, f);
  170. if (res < 1) {
  171. return -errno;
  172. }
  173. err = fclose(f);
  174. if (err) {
  175. return -errno;
  176. }
  177. // update history and stats
  178. if (block != emu->history.blocks[0]) {
  179. memcpy(&emu->history.blocks[1], &emu->history.blocks[0],
  180. sizeof(emu->history) - sizeof(emu->history.blocks[0]));
  181. emu->history.blocks[0] = block;
  182. }
  183. emu->stats.prog_count += 1;
  184. return 0;
  185. }
  186. int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
  187. lfs_emubd_t *emu = cfg->context;
  188. // Check if erase is valid
  189. assert(block < cfg->block_count);
  190. // Erase the block
  191. snprintf(emu->child, LFS_NAME_MAX, "%" PRIx32, block);
  192. struct stat st;
  193. int err = stat(emu->path, &st);
  194. if (err && errno != ENOENT) {
  195. return -errno;
  196. }
  197. if (!err && S_ISREG(st.st_mode) && (S_IWUSR & st.st_mode)) {
  198. err = unlink(emu->path);
  199. if (err) {
  200. return -errno;
  201. }
  202. }
  203. if (err || (S_ISREG(st.st_mode) && (S_IWUSR & st.st_mode))) {
  204. FILE *f = fopen(emu->path, "w");
  205. if (!f) {
  206. return -errno;
  207. }
  208. err = fclose(f);
  209. if (err) {
  210. return -errno;
  211. }
  212. }
  213. emu->stats.erase_count += 1;
  214. return 0;
  215. }
  216. int lfs_emubd_sync(const struct lfs_config *cfg) {
  217. lfs_emubd_t *emu = cfg->context;
  218. // Just write out info/stats for later lookup
  219. snprintf(emu->child, LFS_NAME_MAX, ".config");
  220. FILE *f = fopen(emu->path, "w");
  221. if (!f) {
  222. return -errno;
  223. }
  224. lfs_emubd_tole32(emu);
  225. size_t res = fwrite(&emu->cfg, sizeof(emu->cfg), 1, f);
  226. lfs_emubd_fromle32(emu);
  227. if (res < 1) {
  228. return -errno;
  229. }
  230. int err = fclose(f);
  231. if (err) {
  232. return -errno;
  233. }
  234. snprintf(emu->child, LFS_NAME_MAX, ".stats");
  235. f = fopen(emu->path, "w");
  236. if (!f) {
  237. return -errno;
  238. }
  239. lfs_emubd_tole32(emu);
  240. res = fwrite(&emu->stats, sizeof(emu->stats), 1, f);
  241. lfs_emubd_fromle32(emu);
  242. if (res < 1) {
  243. return -errno;
  244. }
  245. err = fclose(f);
  246. if (err) {
  247. return -errno;
  248. }
  249. snprintf(emu->child, LFS_NAME_MAX, ".history");
  250. f = fopen(emu->path, "w");
  251. if (!f) {
  252. return -errno;
  253. }
  254. lfs_emubd_tole32(emu);
  255. res = fwrite(&emu->history, sizeof(emu->history), 1, f);
  256. lfs_emubd_fromle32(emu);
  257. if (res < 1) {
  258. return -errno;
  259. }
  260. err = fclose(f);
  261. if (err) {
  262. return -errno;
  263. }
  264. return 0;
  265. }