storage_int.c 23 KB

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