lfs_emubd.c 7.8 KB

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