storage_int.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #include "storage_int.h"
  2. #include <lfs.h>
  3. #include <furi_hal.h>
  4. #include <toolbox/path.h>
  5. #define TAG "StorageInt"
  6. #define STORAGE_PATH STORAGE_INT_PATH_PREFIX
  7. #define LFS_CLEAN_FINGERPRINT 0
  8. /* When less than LFS_RESERVED_PAGES_COUNT are left free, creation &
  9. * modification of non-dot files is restricted */
  10. #define LFS_RESERVED_PAGES_COUNT 3
  11. typedef struct {
  12. const size_t start_address;
  13. const size_t start_page;
  14. struct lfs_config config;
  15. lfs_t lfs;
  16. } LFSData;
  17. typedef struct {
  18. void* data;
  19. bool open;
  20. } LFSHandle;
  21. static LFSHandle* lfs_handle_alloc_file() {
  22. LFSHandle* handle = malloc(sizeof(LFSHandle));
  23. handle->data = malloc(sizeof(lfs_file_t));
  24. return handle;
  25. }
  26. static LFSHandle* lfs_handle_alloc_dir() {
  27. LFSHandle* handle = malloc(sizeof(LFSHandle));
  28. handle->data = malloc(sizeof(lfs_dir_t));
  29. return handle;
  30. }
  31. /* INTERNALS */
  32. static lfs_dir_t* lfs_handle_get_dir(LFSHandle* handle) {
  33. return handle->data;
  34. }
  35. static lfs_file_t* lfs_handle_get_file(LFSHandle* handle) {
  36. return handle->data;
  37. }
  38. static void lfs_handle_free(LFSHandle* handle) {
  39. free(handle->data);
  40. free(handle);
  41. }
  42. static void lfs_handle_set_open(LFSHandle* handle) {
  43. handle->open = true;
  44. }
  45. static bool lfs_handle_is_open(LFSHandle* handle) {
  46. return handle->open;
  47. }
  48. static lfs_t* lfs_get_from_storage(StorageData* storage) {
  49. return &((LFSData*)storage->data)->lfs;
  50. }
  51. static LFSData* lfs_data_get_from_storage(StorageData* storage) {
  52. return (LFSData*)storage->data;
  53. }
  54. static int storage_int_device_read(
  55. const struct lfs_config* c,
  56. lfs_block_t block,
  57. lfs_off_t off,
  58. void* buffer,
  59. lfs_size_t size) {
  60. LFSData* lfs_data = c->context;
  61. size_t address = lfs_data->start_address + block * c->block_size + off;
  62. FURI_LOG_T(
  63. TAG,
  64. "Device read: block %ld, off %ld, buffer: %p, size %ld, translated address: %p",
  65. block,
  66. off,
  67. buffer,
  68. size,
  69. (void*)address);
  70. memcpy(buffer, (void*)address, size);
  71. return 0;
  72. }
  73. static int storage_int_device_prog(
  74. const struct lfs_config* c,
  75. lfs_block_t block,
  76. lfs_off_t off,
  77. const void* buffer,
  78. lfs_size_t size) {
  79. LFSData* lfs_data = c->context;
  80. size_t address = lfs_data->start_address + block * c->block_size + off;
  81. FURI_LOG_T(
  82. TAG,
  83. "Device prog: block %ld, off %ld, buffer: %p, size %ld, translated address: %p",
  84. block,
  85. off,
  86. buffer,
  87. size,
  88. (void*)address);
  89. int ret = 0;
  90. while(size > 0) {
  91. furi_hal_flash_write_dword(address, *(uint64_t*)buffer);
  92. address += c->prog_size;
  93. buffer += c->prog_size;
  94. size -= c->prog_size;
  95. }
  96. return ret;
  97. }
  98. static int storage_int_device_erase(const struct lfs_config* c, lfs_block_t block) {
  99. LFSData* lfs_data = c->context;
  100. size_t page = lfs_data->start_page + block;
  101. FURI_LOG_D(TAG, "Device erase: page %ld, translated page: %x", block, page);
  102. furi_hal_flash_erase(page);
  103. return 0;
  104. }
  105. static int storage_int_device_sync(const struct lfs_config* c) {
  106. UNUSED(c);
  107. FURI_LOG_D(TAG, "Device sync: skipping");
  108. return 0;
  109. }
  110. static LFSData* storage_int_lfs_data_alloc() {
  111. LFSData* lfs_data = malloc(sizeof(LFSData));
  112. // Internal storage start address
  113. *(size_t*)(&lfs_data->start_address) = furi_hal_flash_get_free_page_start_address();
  114. *(size_t*)(&lfs_data->start_page) =
  115. (lfs_data->start_address - furi_hal_flash_get_base()) / furi_hal_flash_get_page_size();
  116. // LFS configuration
  117. // Glue and context
  118. lfs_data->config.context = lfs_data;
  119. lfs_data->config.read = storage_int_device_read;
  120. lfs_data->config.prog = storage_int_device_prog;
  121. lfs_data->config.erase = storage_int_device_erase;
  122. lfs_data->config.sync = storage_int_device_sync;
  123. // Block device description
  124. lfs_data->config.read_size = furi_hal_flash_get_read_block_size();
  125. lfs_data->config.prog_size = furi_hal_flash_get_write_block_size();
  126. lfs_data->config.block_size = furi_hal_flash_get_page_size();
  127. lfs_data->config.block_count = furi_hal_flash_get_free_page_count();
  128. lfs_data->config.block_cycles = furi_hal_flash_get_cycles_count();
  129. lfs_data->config.cache_size = 16;
  130. lfs_data->config.lookahead_size = 16;
  131. return lfs_data;
  132. };
  133. // Returns true if fingerprint was invalid and LFS reformatting is needed
  134. static bool storage_int_check_and_set_fingerprint(LFSData* lfs_data) {
  135. bool value = false;
  136. uint32_t os_fingerprint = 0;
  137. os_fingerprint |= ((lfs_data->start_page & 0xFF) << 0);
  138. os_fingerprint |= ((lfs_data->config.block_count & 0xFF) << 8);
  139. os_fingerprint |= ((LFS_DISK_VERSION_MAJOR & 0xFFFF) << 16);
  140. uint32_t rtc_fingerprint = furi_hal_rtc_get_register(FuriHalRtcRegisterLfsFingerprint);
  141. if(rtc_fingerprint == LFS_CLEAN_FINGERPRINT) {
  142. FURI_LOG_I(TAG, "Storing LFS fingerprint in RTC");
  143. furi_hal_rtc_set_register(FuriHalRtcRegisterLfsFingerprint, os_fingerprint);
  144. } else if(rtc_fingerprint != os_fingerprint) {
  145. FURI_LOG_E(TAG, "LFS fingerprint mismatch");
  146. furi_hal_rtc_set_register(FuriHalRtcRegisterLfsFingerprint, os_fingerprint);
  147. value = true;
  148. }
  149. return value;
  150. }
  151. static void storage_int_lfs_mount(LFSData* lfs_data, StorageData* storage) {
  152. int err;
  153. lfs_t* lfs = &lfs_data->lfs;
  154. bool was_fingerprint_outdated = storage_int_check_and_set_fingerprint(lfs_data);
  155. bool need_format = furi_hal_rtc_is_flag_set(FuriHalRtcFlagFactoryReset) ||
  156. was_fingerprint_outdated;
  157. if(need_format) {
  158. // Format storage
  159. err = lfs_format(lfs, &lfs_data->config);
  160. if(err == 0) {
  161. FURI_LOG_I(TAG, "Factory reset: Format successful, trying to mount");
  162. furi_hal_rtc_reset_flag(FuriHalRtcFlagFactoryReset);
  163. err = lfs_mount(lfs, &lfs_data->config);
  164. if(err == 0) {
  165. FURI_LOG_I(TAG, "Factory reset: Mounted");
  166. storage->status = StorageStatusOK;
  167. } else {
  168. FURI_LOG_E(TAG, "Factory reset: Mount after format failed");
  169. storage->status = StorageStatusNotMounted;
  170. }
  171. } else {
  172. FURI_LOG_E(TAG, "Factory reset: Format failed");
  173. storage->status = StorageStatusNoFS;
  174. }
  175. } else {
  176. // Normal
  177. err = lfs_mount(lfs, &lfs_data->config);
  178. if(err == 0) {
  179. FURI_LOG_I(TAG, "Mounted");
  180. storage->status = StorageStatusOK;
  181. } else {
  182. FURI_LOG_E(TAG, "Mount failed, formatting");
  183. err = lfs_format(lfs, &lfs_data->config);
  184. if(err == 0) {
  185. FURI_LOG_I(TAG, "Format successful, trying to mount");
  186. err = lfs_mount(lfs, &lfs_data->config);
  187. if(err == 0) {
  188. FURI_LOG_I(TAG, "Mounted");
  189. storage->status = StorageStatusOK;
  190. } else {
  191. FURI_LOG_E(TAG, "Mount after format failed");
  192. storage->status = StorageStatusNotMounted;
  193. }
  194. } else {
  195. FURI_LOG_E(TAG, "Format failed");
  196. storage->status = StorageStatusNoFS;
  197. }
  198. }
  199. }
  200. }
  201. /****************** Common Functions ******************/
  202. static FS_Error storage_int_parse_error(int error) {
  203. FS_Error result = FSE_INTERNAL;
  204. if(error >= LFS_ERR_OK) {
  205. result = FSE_OK;
  206. } else {
  207. switch(error) {
  208. case LFS_ERR_IO:
  209. result = FSE_INTERNAL;
  210. break;
  211. case LFS_ERR_CORRUPT:
  212. result = FSE_INTERNAL;
  213. break;
  214. case LFS_ERR_NOENT:
  215. result = FSE_NOT_EXIST;
  216. break;
  217. case LFS_ERR_EXIST:
  218. result = FSE_EXIST;
  219. break;
  220. case LFS_ERR_NOTDIR:
  221. result = FSE_INVALID_NAME;
  222. break;
  223. case LFS_ERR_ISDIR:
  224. result = FSE_INVALID_NAME;
  225. break;
  226. case LFS_ERR_NOTEMPTY:
  227. result = FSE_DENIED;
  228. break;
  229. case LFS_ERR_BADF:
  230. result = FSE_INVALID_NAME;
  231. break;
  232. case LFS_ERR_FBIG:
  233. result = FSE_INTERNAL;
  234. break;
  235. case LFS_ERR_INVAL:
  236. result = FSE_INVALID_PARAMETER;
  237. break;
  238. case LFS_ERR_NOSPC:
  239. result = FSE_INTERNAL;
  240. break;
  241. case LFS_ERR_NOMEM:
  242. result = FSE_INTERNAL;
  243. break;
  244. case LFS_ERR_NOATTR:
  245. result = FSE_INVALID_PARAMETER;
  246. break;
  247. case LFS_ERR_NAMETOOLONG:
  248. result = FSE_INVALID_NAME;
  249. break;
  250. default:
  251. break;
  252. }
  253. }
  254. return result;
  255. }
  256. /* Returns false if less than reserved space is left free */
  257. static bool storage_int_check_for_free_space(StorageData* storage) {
  258. LFSData* lfs_data = lfs_data_get_from_storage(storage);
  259. lfs_ssize_t result = lfs_fs_size(lfs_get_from_storage(storage));
  260. if(result >= 0) {
  261. lfs_size_t free_space =
  262. (lfs_data->config.block_count - result) * lfs_data->config.block_size;
  263. return (free_space > LFS_RESERVED_PAGES_COUNT * furi_hal_flash_get_page_size());
  264. }
  265. return false;
  266. }
  267. /******************* File Functions *******************/
  268. static bool storage_int_file_open(
  269. void* ctx,
  270. File* file,
  271. const char* path,
  272. FS_AccessMode access_mode,
  273. FS_OpenMode open_mode) {
  274. StorageData* storage = ctx;
  275. lfs_t* lfs = lfs_get_from_storage(storage);
  276. bool enough_free_space = storage_int_check_for_free_space(storage);
  277. int flags = 0;
  278. if(access_mode & FSAM_READ) flags |= LFS_O_RDONLY;
  279. if(access_mode & FSAM_WRITE) flags |= LFS_O_WRONLY;
  280. if(open_mode & FSOM_OPEN_EXISTING) flags |= 0;
  281. if(open_mode & FSOM_OPEN_ALWAYS) flags |= LFS_O_CREAT;
  282. if(open_mode & FSOM_OPEN_APPEND) flags |= LFS_O_CREAT | LFS_O_APPEND;
  283. if(open_mode & FSOM_CREATE_NEW) flags |= LFS_O_CREAT | LFS_O_EXCL;
  284. if(open_mode & FSOM_CREATE_ALWAYS) flags |= LFS_O_CREAT | LFS_O_TRUNC;
  285. LFSHandle* handle = lfs_handle_alloc_file();
  286. storage_set_storage_file_data(file, handle, storage);
  287. if(!enough_free_space) {
  288. FuriString* filename;
  289. filename = furi_string_alloc();
  290. path_extract_basename(path, filename);
  291. bool is_dot_file =
  292. (!furi_string_empty(filename) && (furi_string_get_char(filename, 0) == '.'));
  293. furi_string_free(filename);
  294. /* Restrict write & creation access to all non-dot files */
  295. if(!is_dot_file && (flags & (LFS_O_CREAT | LFS_O_WRONLY))) {
  296. file->internal_error_id = LFS_ERR_NOSPC;
  297. file->error_id = FSE_DENIED;
  298. FURI_LOG_W(TAG, "Denied access to '%s': no free space", path);
  299. return false;
  300. }
  301. }
  302. file->internal_error_id = lfs_file_open(lfs, lfs_handle_get_file(handle), path, flags);
  303. if(file->internal_error_id >= LFS_ERR_OK) {
  304. lfs_handle_set_open(handle);
  305. }
  306. file->error_id = storage_int_parse_error(file->internal_error_id);
  307. return (file->error_id == FSE_OK);
  308. }
  309. static bool storage_int_file_close(void* ctx, File* file) {
  310. StorageData* storage = ctx;
  311. lfs_t* lfs = lfs_get_from_storage(storage);
  312. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  313. if(lfs_handle_is_open(handle)) {
  314. file->internal_error_id = lfs_file_close(lfs, lfs_handle_get_file(handle));
  315. } else {
  316. file->internal_error_id = LFS_ERR_BADF;
  317. }
  318. file->error_id = storage_int_parse_error(file->internal_error_id);
  319. lfs_handle_free(handle);
  320. return (file->error_id == FSE_OK);
  321. }
  322. static uint16_t
  323. storage_int_file_read(void* ctx, File* file, void* buff, uint16_t const bytes_to_read) {
  324. StorageData* storage = ctx;
  325. lfs_t* lfs = lfs_get_from_storage(storage);
  326. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  327. uint16_t bytes_read = 0;
  328. if(lfs_handle_is_open(handle)) {
  329. file->internal_error_id =
  330. lfs_file_read(lfs, lfs_handle_get_file(handle), buff, bytes_to_read);
  331. } else {
  332. file->internal_error_id = LFS_ERR_BADF;
  333. }
  334. file->error_id = storage_int_parse_error(file->internal_error_id);
  335. if(file->error_id == FSE_OK) {
  336. bytes_read = file->internal_error_id;
  337. file->internal_error_id = 0;
  338. }
  339. return bytes_read;
  340. }
  341. static uint16_t
  342. storage_int_file_write(void* ctx, File* file, const void* buff, uint16_t const bytes_to_write) {
  343. StorageData* storage = ctx;
  344. lfs_t* lfs = lfs_get_from_storage(storage);
  345. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  346. uint16_t bytes_written = 0;
  347. if(lfs_handle_is_open(handle)) {
  348. file->internal_error_id =
  349. lfs_file_write(lfs, lfs_handle_get_file(handle), buff, bytes_to_write);
  350. } else {
  351. file->internal_error_id = LFS_ERR_BADF;
  352. }
  353. file->error_id = storage_int_parse_error(file->internal_error_id);
  354. if(file->error_id == FSE_OK) {
  355. bytes_written = file->internal_error_id;
  356. file->internal_error_id = 0;
  357. }
  358. return bytes_written;
  359. }
  360. static bool
  361. storage_int_file_seek(void* ctx, File* file, const uint32_t offset, const bool from_start) {
  362. StorageData* storage = ctx;
  363. lfs_t* lfs = lfs_get_from_storage(storage);
  364. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  365. if(lfs_handle_is_open(handle)) {
  366. if(from_start) {
  367. file->internal_error_id =
  368. lfs_file_seek(lfs, lfs_handle_get_file(handle), offset, LFS_SEEK_SET);
  369. } else {
  370. file->internal_error_id =
  371. lfs_file_seek(lfs, lfs_handle_get_file(handle), offset, LFS_SEEK_CUR);
  372. }
  373. } else {
  374. file->internal_error_id = LFS_ERR_BADF;
  375. }
  376. file->error_id = storage_int_parse_error(file->internal_error_id);
  377. return (file->error_id == FSE_OK);
  378. }
  379. static uint64_t storage_int_file_tell(void* ctx, File* file) {
  380. StorageData* storage = ctx;
  381. lfs_t* lfs = lfs_get_from_storage(storage);
  382. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  383. if(lfs_handle_is_open(handle)) {
  384. file->internal_error_id = lfs_file_tell(lfs, lfs_handle_get_file(handle));
  385. } else {
  386. file->internal_error_id = LFS_ERR_BADF;
  387. }
  388. file->error_id = storage_int_parse_error(file->internal_error_id);
  389. int32_t position = 0;
  390. if(file->error_id == FSE_OK) {
  391. position = file->internal_error_id;
  392. file->internal_error_id = 0;
  393. }
  394. return position;
  395. }
  396. static bool storage_int_file_truncate(void* ctx, File* file) {
  397. StorageData* storage = ctx;
  398. lfs_t* lfs = lfs_get_from_storage(storage);
  399. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  400. if(lfs_handle_is_open(handle)) {
  401. file->internal_error_id = lfs_file_tell(lfs, lfs_handle_get_file(handle));
  402. file->error_id = storage_int_parse_error(file->internal_error_id);
  403. if(file->error_id == FSE_OK) {
  404. uint32_t position = file->internal_error_id;
  405. file->internal_error_id =
  406. lfs_file_truncate(lfs, lfs_handle_get_file(handle), position);
  407. file->error_id = storage_int_parse_error(file->internal_error_id);
  408. }
  409. } else {
  410. file->internal_error_id = LFS_ERR_BADF;
  411. file->error_id = storage_int_parse_error(file->internal_error_id);
  412. }
  413. return (file->error_id == FSE_OK);
  414. }
  415. static bool storage_int_file_sync(void* ctx, File* file) {
  416. StorageData* storage = ctx;
  417. lfs_t* lfs = lfs_get_from_storage(storage);
  418. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  419. if(lfs_handle_is_open(handle)) {
  420. file->internal_error_id = lfs_file_sync(lfs, lfs_handle_get_file(handle));
  421. } else {
  422. file->internal_error_id = LFS_ERR_BADF;
  423. }
  424. file->error_id = storage_int_parse_error(file->internal_error_id);
  425. return (file->error_id == FSE_OK);
  426. }
  427. static uint64_t storage_int_file_size(void* ctx, File* file) {
  428. StorageData* storage = ctx;
  429. lfs_t* lfs = lfs_get_from_storage(storage);
  430. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  431. if(lfs_handle_is_open(handle)) {
  432. file->internal_error_id = lfs_file_size(lfs, lfs_handle_get_file(handle));
  433. } else {
  434. file->internal_error_id = LFS_ERR_BADF;
  435. }
  436. file->error_id = storage_int_parse_error(file->internal_error_id);
  437. uint32_t size = 0;
  438. if(file->error_id == FSE_OK) {
  439. size = file->internal_error_id;
  440. file->internal_error_id = 0;
  441. }
  442. return size;
  443. }
  444. static bool storage_int_file_eof(void* ctx, File* file) {
  445. StorageData* storage = ctx;
  446. lfs_t* lfs = lfs_get_from_storage(storage);
  447. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  448. bool eof = true;
  449. if(lfs_handle_is_open(handle)) {
  450. int32_t position = lfs_file_tell(lfs, lfs_handle_get_file(handle));
  451. int32_t size = lfs_file_size(lfs, lfs_handle_get_file(handle));
  452. if(position < 0) {
  453. file->internal_error_id = position;
  454. } else if(size < 0) {
  455. file->internal_error_id = size;
  456. } else {
  457. file->internal_error_id = LFS_ERR_OK;
  458. eof = (position >= size);
  459. }
  460. } else {
  461. file->internal_error_id = LFS_ERR_BADF;
  462. }
  463. file->error_id = storage_int_parse_error(file->internal_error_id);
  464. return eof;
  465. }
  466. /******************* Dir Functions *******************/
  467. static bool storage_int_dir_open(void* ctx, File* file, const char* path) {
  468. StorageData* storage = ctx;
  469. lfs_t* lfs = lfs_get_from_storage(storage);
  470. LFSHandle* handle = lfs_handle_alloc_dir();
  471. storage_set_storage_file_data(file, handle, storage);
  472. file->internal_error_id = lfs_dir_open(lfs, lfs_handle_get_dir(handle), path);
  473. if(file->internal_error_id >= LFS_ERR_OK) {
  474. lfs_handle_set_open(handle);
  475. }
  476. file->error_id = storage_int_parse_error(file->internal_error_id);
  477. return (file->error_id == FSE_OK);
  478. }
  479. static bool storage_int_dir_close(void* ctx, File* file) {
  480. StorageData* storage = ctx;
  481. lfs_t* lfs = lfs_get_from_storage(storage);
  482. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  483. if(lfs_handle_is_open(handle)) {
  484. file->internal_error_id = lfs_dir_close(lfs, lfs_handle_get_dir(handle));
  485. } else {
  486. file->internal_error_id = LFS_ERR_BADF;
  487. }
  488. file->error_id = storage_int_parse_error(file->internal_error_id);
  489. lfs_handle_free(handle);
  490. return (file->error_id == FSE_OK);
  491. }
  492. static bool storage_int_dir_read(
  493. void* ctx,
  494. File* file,
  495. FileInfo* fileinfo,
  496. char* name,
  497. const uint16_t name_length) {
  498. StorageData* storage = ctx;
  499. lfs_t* lfs = lfs_get_from_storage(storage);
  500. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  501. if(lfs_handle_is_open(handle)) {
  502. struct lfs_info _fileinfo;
  503. // LFS returns virtual directories "." and "..", so we read until we get something meaningful or an empty string
  504. do {
  505. file->internal_error_id = lfs_dir_read(lfs, lfs_handle_get_dir(handle), &_fileinfo);
  506. file->error_id = storage_int_parse_error(file->internal_error_id);
  507. } while(strcmp(_fileinfo.name, ".") == 0 || strcmp(_fileinfo.name, "..") == 0);
  508. if(fileinfo != NULL) {
  509. fileinfo->size = _fileinfo.size;
  510. fileinfo->flags = 0;
  511. if(_fileinfo.type & LFS_TYPE_DIR) fileinfo->flags |= FSF_DIRECTORY;
  512. }
  513. if(name != NULL) {
  514. snprintf(name, name_length, "%s", _fileinfo.name);
  515. }
  516. // set FSE_NOT_EXIST error on end of directory
  517. if(file->internal_error_id == 0) {
  518. file->error_id = FSE_NOT_EXIST;
  519. }
  520. } else {
  521. file->internal_error_id = LFS_ERR_BADF;
  522. file->error_id = storage_int_parse_error(file->internal_error_id);
  523. }
  524. return (file->error_id == FSE_OK);
  525. }
  526. static bool storage_int_dir_rewind(void* ctx, File* file) {
  527. StorageData* storage = ctx;
  528. lfs_t* lfs = lfs_get_from_storage(storage);
  529. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  530. if(lfs_handle_is_open(handle)) {
  531. file->internal_error_id = lfs_dir_rewind(lfs, lfs_handle_get_dir(handle));
  532. } else {
  533. file->internal_error_id = LFS_ERR_BADF;
  534. }
  535. file->error_id = storage_int_parse_error(file->internal_error_id);
  536. return (file->error_id == FSE_OK);
  537. }
  538. /******************* Common FS Functions *******************/
  539. static FS_Error storage_int_common_stat(void* ctx, const char* path, FileInfo* fileinfo) {
  540. StorageData* storage = ctx;
  541. lfs_t* lfs = lfs_get_from_storage(storage);
  542. struct lfs_info _fileinfo;
  543. int result = lfs_stat(lfs, path, &_fileinfo);
  544. if(fileinfo != NULL) {
  545. fileinfo->size = _fileinfo.size;
  546. fileinfo->flags = 0;
  547. if(_fileinfo.type & LFS_TYPE_DIR) fileinfo->flags |= FSF_DIRECTORY;
  548. }
  549. return storage_int_parse_error(result);
  550. }
  551. static FS_Error storage_int_common_remove(void* ctx, const char* path) {
  552. StorageData* storage = ctx;
  553. lfs_t* lfs = lfs_get_from_storage(storage);
  554. int result = lfs_remove(lfs, path);
  555. return storage_int_parse_error(result);
  556. }
  557. static FS_Error storage_int_common_mkdir(void* ctx, const char* path) {
  558. StorageData* storage = ctx;
  559. lfs_t* lfs = lfs_get_from_storage(storage);
  560. int result = lfs_mkdir(lfs, path);
  561. return storage_int_parse_error(result);
  562. }
  563. static FS_Error storage_int_common_fs_info(
  564. void* ctx,
  565. const char* fs_path,
  566. uint64_t* total_space,
  567. uint64_t* free_space) {
  568. UNUSED(fs_path);
  569. StorageData* storage = ctx;
  570. lfs_t* lfs = lfs_get_from_storage(storage);
  571. LFSData* lfs_data = lfs_data_get_from_storage(storage);
  572. if(total_space) {
  573. *total_space = lfs_data->config.block_size * lfs_data->config.block_count;
  574. }
  575. lfs_ssize_t result = lfs_fs_size(lfs);
  576. if(free_space && (result >= 0)) {
  577. *free_space = (lfs_data->config.block_count - result) * lfs_data->config.block_size;
  578. }
  579. return storage_int_parse_error(result);
  580. }
  581. /******************* Init Storage *******************/
  582. static const FS_Api fs_api = {
  583. .file =
  584. {
  585. .open = storage_int_file_open,
  586. .close = storage_int_file_close,
  587. .read = storage_int_file_read,
  588. .write = storage_int_file_write,
  589. .seek = storage_int_file_seek,
  590. .tell = storage_int_file_tell,
  591. .truncate = storage_int_file_truncate,
  592. .size = storage_int_file_size,
  593. .sync = storage_int_file_sync,
  594. .eof = storage_int_file_eof,
  595. },
  596. .dir =
  597. {
  598. .open = storage_int_dir_open,
  599. .close = storage_int_dir_close,
  600. .read = storage_int_dir_read,
  601. .rewind = storage_int_dir_rewind,
  602. },
  603. .common =
  604. {
  605. .stat = storage_int_common_stat,
  606. .mkdir = storage_int_common_mkdir,
  607. .remove = storage_int_common_remove,
  608. .fs_info = storage_int_common_fs_info,
  609. },
  610. };
  611. void storage_int_init(StorageData* storage) {
  612. FURI_LOG_I(TAG, "Starting");
  613. LFSData* lfs_data = storage_int_lfs_data_alloc();
  614. FURI_LOG_I(
  615. TAG,
  616. "Config: start %p, read %ld, write %ld, page size: %ld, page count: %ld, cycles: %ld",
  617. (void*)lfs_data->start_address,
  618. lfs_data->config.read_size,
  619. lfs_data->config.prog_size,
  620. lfs_data->config.block_size,
  621. lfs_data->config.block_count,
  622. lfs_data->config.block_cycles);
  623. storage_int_lfs_mount(lfs_data, storage);
  624. storage->data = lfs_data;
  625. storage->api.tick = NULL;
  626. storage->fs_api = &fs_api;
  627. }