storage_int.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 %lu, off %lu, buffer: %p, size %lu, 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 %lu, off %lu, buffer: %p, size %lu, 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 %lu, translated page: %zx", 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;
  204. if(error >= LFS_ERR_OK) {
  205. result = FSE_OK;
  206. } else {
  207. switch(error) {
  208. case LFS_ERR_NOENT:
  209. result = FSE_NOT_EXIST;
  210. break;
  211. case LFS_ERR_EXIST:
  212. result = FSE_EXIST;
  213. break;
  214. case LFS_ERR_NOTEMPTY:
  215. result = FSE_DENIED;
  216. break;
  217. case LFS_ERR_INVAL:
  218. case LFS_ERR_NOATTR:
  219. result = FSE_INVALID_PARAMETER;
  220. break;
  221. case LFS_ERR_BADF:
  222. case LFS_ERR_ISDIR:
  223. case LFS_ERR_NOTDIR:
  224. case LFS_ERR_NAMETOOLONG:
  225. result = FSE_INVALID_NAME;
  226. break;
  227. case LFS_ERR_IO:
  228. case LFS_ERR_FBIG:
  229. case LFS_ERR_NOSPC:
  230. case LFS_ERR_NOMEM:
  231. case LFS_ERR_CORRUPT:
  232. default:
  233. result = FSE_INTERNAL;
  234. }
  235. }
  236. return result;
  237. }
  238. /* Returns false if less than reserved space is left free */
  239. static bool storage_int_check_for_free_space(StorageData* storage) {
  240. LFSData* lfs_data = lfs_data_get_from_storage(storage);
  241. lfs_ssize_t result = lfs_fs_size(lfs_get_from_storage(storage));
  242. if(result >= 0) {
  243. lfs_size_t free_space =
  244. (lfs_data->config.block_count - result) * lfs_data->config.block_size;
  245. return (free_space > LFS_RESERVED_PAGES_COUNT * furi_hal_flash_get_page_size());
  246. }
  247. return false;
  248. }
  249. /******************* File Functions *******************/
  250. static bool storage_int_file_open(
  251. void* ctx,
  252. File* file,
  253. const char* path,
  254. FS_AccessMode access_mode,
  255. FS_OpenMode open_mode) {
  256. StorageData* storage = ctx;
  257. lfs_t* lfs = lfs_get_from_storage(storage);
  258. bool enough_free_space = storage_int_check_for_free_space(storage);
  259. int flags = 0;
  260. if(access_mode & FSAM_READ) flags |= LFS_O_RDONLY;
  261. if(access_mode & FSAM_WRITE) flags |= LFS_O_WRONLY;
  262. if(open_mode & FSOM_OPEN_EXISTING) flags |= 0;
  263. if(open_mode & FSOM_OPEN_ALWAYS) flags |= LFS_O_CREAT;
  264. if(open_mode & FSOM_OPEN_APPEND) flags |= LFS_O_CREAT | LFS_O_APPEND;
  265. if(open_mode & FSOM_CREATE_NEW) flags |= LFS_O_CREAT | LFS_O_EXCL;
  266. if(open_mode & FSOM_CREATE_ALWAYS) flags |= LFS_O_CREAT | LFS_O_TRUNC;
  267. LFSHandle* handle = lfs_handle_alloc_file();
  268. storage_set_storage_file_data(file, handle, storage);
  269. if(!enough_free_space) {
  270. FuriString* filename;
  271. filename = furi_string_alloc();
  272. path_extract_basename(path, filename);
  273. bool is_dot_file =
  274. (!furi_string_empty(filename) && (furi_string_get_char(filename, 0) == '.'));
  275. furi_string_free(filename);
  276. /* Restrict write & creation access to all non-dot files */
  277. if(!is_dot_file && (flags & (LFS_O_CREAT | LFS_O_WRONLY))) {
  278. file->internal_error_id = LFS_ERR_NOSPC;
  279. file->error_id = FSE_DENIED;
  280. FURI_LOG_W(TAG, "Denied access to '%s': no free space", path);
  281. return false;
  282. }
  283. }
  284. file->internal_error_id = lfs_file_open(lfs, lfs_handle_get_file(handle), path, flags);
  285. if(file->internal_error_id >= LFS_ERR_OK) {
  286. lfs_handle_set_open(handle);
  287. }
  288. file->error_id = storage_int_parse_error(file->internal_error_id);
  289. return (file->error_id == FSE_OK);
  290. }
  291. static bool storage_int_file_close(void* ctx, File* file) {
  292. StorageData* storage = ctx;
  293. lfs_t* lfs = lfs_get_from_storage(storage);
  294. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  295. if(lfs_handle_is_open(handle)) {
  296. file->internal_error_id = lfs_file_close(lfs, lfs_handle_get_file(handle));
  297. } else {
  298. file->internal_error_id = LFS_ERR_BADF;
  299. }
  300. file->error_id = storage_int_parse_error(file->internal_error_id);
  301. lfs_handle_free(handle);
  302. return (file->error_id == FSE_OK);
  303. }
  304. static uint16_t
  305. storage_int_file_read(void* ctx, File* file, void* buff, uint16_t const bytes_to_read) {
  306. StorageData* storage = ctx;
  307. lfs_t* lfs = lfs_get_from_storage(storage);
  308. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  309. uint16_t bytes_read = 0;
  310. if(lfs_handle_is_open(handle)) {
  311. file->internal_error_id =
  312. lfs_file_read(lfs, lfs_handle_get_file(handle), buff, bytes_to_read);
  313. } else {
  314. file->internal_error_id = LFS_ERR_BADF;
  315. }
  316. file->error_id = storage_int_parse_error(file->internal_error_id);
  317. if(file->error_id == FSE_OK) {
  318. bytes_read = file->internal_error_id;
  319. file->internal_error_id = 0;
  320. }
  321. return bytes_read;
  322. }
  323. static uint16_t
  324. storage_int_file_write(void* ctx, File* file, const void* buff, uint16_t const bytes_to_write) {
  325. StorageData* storage = ctx;
  326. lfs_t* lfs = lfs_get_from_storage(storage);
  327. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  328. uint16_t bytes_written = 0;
  329. if(lfs_handle_is_open(handle)) {
  330. file->internal_error_id =
  331. lfs_file_write(lfs, lfs_handle_get_file(handle), buff, bytes_to_write);
  332. } else {
  333. file->internal_error_id = LFS_ERR_BADF;
  334. }
  335. file->error_id = storage_int_parse_error(file->internal_error_id);
  336. if(file->error_id == FSE_OK) {
  337. bytes_written = file->internal_error_id;
  338. file->internal_error_id = 0;
  339. }
  340. return bytes_written;
  341. }
  342. static bool
  343. storage_int_file_seek(void* ctx, File* file, const uint32_t offset, const bool from_start) {
  344. StorageData* storage = ctx;
  345. lfs_t* lfs = lfs_get_from_storage(storage);
  346. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  347. if(lfs_handle_is_open(handle)) {
  348. if(from_start) {
  349. file->internal_error_id =
  350. lfs_file_seek(lfs, lfs_handle_get_file(handle), offset, LFS_SEEK_SET);
  351. } else {
  352. file->internal_error_id =
  353. lfs_file_seek(lfs, lfs_handle_get_file(handle), offset, LFS_SEEK_CUR);
  354. }
  355. } else {
  356. file->internal_error_id = LFS_ERR_BADF;
  357. }
  358. file->error_id = storage_int_parse_error(file->internal_error_id);
  359. return (file->error_id == FSE_OK);
  360. }
  361. static uint64_t storage_int_file_tell(void* ctx, File* file) {
  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. file->internal_error_id = lfs_file_tell(lfs, lfs_handle_get_file(handle));
  367. } else {
  368. file->internal_error_id = LFS_ERR_BADF;
  369. }
  370. file->error_id = storage_int_parse_error(file->internal_error_id);
  371. int32_t position = 0;
  372. if(file->error_id == FSE_OK) {
  373. position = file->internal_error_id;
  374. file->internal_error_id = 0;
  375. }
  376. return position;
  377. }
  378. static bool storage_int_file_truncate(void* ctx, File* file) {
  379. StorageData* storage = ctx;
  380. lfs_t* lfs = lfs_get_from_storage(storage);
  381. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  382. if(lfs_handle_is_open(handle)) {
  383. file->internal_error_id = lfs_file_tell(lfs, lfs_handle_get_file(handle));
  384. file->error_id = storage_int_parse_error(file->internal_error_id);
  385. if(file->error_id == FSE_OK) {
  386. uint32_t position = file->internal_error_id;
  387. file->internal_error_id =
  388. lfs_file_truncate(lfs, lfs_handle_get_file(handle), position);
  389. file->error_id = storage_int_parse_error(file->internal_error_id);
  390. }
  391. } else {
  392. file->internal_error_id = LFS_ERR_BADF;
  393. file->error_id = storage_int_parse_error(file->internal_error_id);
  394. }
  395. return (file->error_id == FSE_OK);
  396. }
  397. static bool storage_int_file_sync(void* ctx, File* file) {
  398. StorageData* storage = ctx;
  399. lfs_t* lfs = lfs_get_from_storage(storage);
  400. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  401. if(lfs_handle_is_open(handle)) {
  402. file->internal_error_id = lfs_file_sync(lfs, lfs_handle_get_file(handle));
  403. } else {
  404. file->internal_error_id = LFS_ERR_BADF;
  405. }
  406. file->error_id = storage_int_parse_error(file->internal_error_id);
  407. return (file->error_id == FSE_OK);
  408. }
  409. static uint64_t storage_int_file_size(void* ctx, File* file) {
  410. StorageData* storage = ctx;
  411. lfs_t* lfs = lfs_get_from_storage(storage);
  412. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  413. if(lfs_handle_is_open(handle)) {
  414. file->internal_error_id = lfs_file_size(lfs, lfs_handle_get_file(handle));
  415. } else {
  416. file->internal_error_id = LFS_ERR_BADF;
  417. }
  418. file->error_id = storage_int_parse_error(file->internal_error_id);
  419. uint32_t size = 0;
  420. if(file->error_id == FSE_OK) {
  421. size = file->internal_error_id;
  422. file->internal_error_id = 0;
  423. }
  424. return size;
  425. }
  426. static bool storage_int_file_eof(void* ctx, File* file) {
  427. StorageData* storage = ctx;
  428. lfs_t* lfs = lfs_get_from_storage(storage);
  429. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  430. bool eof = true;
  431. if(lfs_handle_is_open(handle)) {
  432. int32_t position = lfs_file_tell(lfs, lfs_handle_get_file(handle));
  433. int32_t size = lfs_file_size(lfs, lfs_handle_get_file(handle));
  434. if(position < 0) {
  435. file->internal_error_id = position;
  436. } else if(size < 0) {
  437. file->internal_error_id = size;
  438. } else {
  439. file->internal_error_id = LFS_ERR_OK;
  440. eof = (position >= size);
  441. }
  442. } else {
  443. file->internal_error_id = LFS_ERR_BADF;
  444. }
  445. file->error_id = storage_int_parse_error(file->internal_error_id);
  446. return eof;
  447. }
  448. /******************* Dir Functions *******************/
  449. static bool storage_int_dir_open(void* ctx, File* file, const char* path) {
  450. StorageData* storage = ctx;
  451. lfs_t* lfs = lfs_get_from_storage(storage);
  452. LFSHandle* handle = lfs_handle_alloc_dir();
  453. storage_set_storage_file_data(file, handle, storage);
  454. file->internal_error_id = lfs_dir_open(lfs, lfs_handle_get_dir(handle), path);
  455. if(file->internal_error_id >= LFS_ERR_OK) {
  456. lfs_handle_set_open(handle);
  457. }
  458. file->error_id = storage_int_parse_error(file->internal_error_id);
  459. return (file->error_id == FSE_OK);
  460. }
  461. static bool storage_int_dir_close(void* ctx, File* file) {
  462. StorageData* storage = ctx;
  463. lfs_t* lfs = lfs_get_from_storage(storage);
  464. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  465. if(lfs_handle_is_open(handle)) {
  466. file->internal_error_id = lfs_dir_close(lfs, lfs_handle_get_dir(handle));
  467. } else {
  468. file->internal_error_id = LFS_ERR_BADF;
  469. }
  470. file->error_id = storage_int_parse_error(file->internal_error_id);
  471. lfs_handle_free(handle);
  472. return (file->error_id == FSE_OK);
  473. }
  474. static bool storage_int_dir_read(
  475. void* ctx,
  476. File* file,
  477. FileInfo* fileinfo,
  478. char* name,
  479. const uint16_t name_length) {
  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. struct lfs_info _fileinfo;
  485. // LFS returns virtual directories "." and "..", so we read until we get something meaningful or an empty string
  486. do {
  487. file->internal_error_id = lfs_dir_read(lfs, lfs_handle_get_dir(handle), &_fileinfo);
  488. file->error_id = storage_int_parse_error(file->internal_error_id);
  489. } while(strcmp(_fileinfo.name, ".") == 0 || strcmp(_fileinfo.name, "..") == 0);
  490. if(fileinfo != NULL) {
  491. fileinfo->size = _fileinfo.size;
  492. fileinfo->flags = 0;
  493. if(_fileinfo.type & LFS_TYPE_DIR) fileinfo->flags |= FSF_DIRECTORY;
  494. }
  495. if(name != NULL) {
  496. snprintf(name, name_length, "%s", _fileinfo.name);
  497. }
  498. // set FSE_NOT_EXIST error on end of directory
  499. if(file->internal_error_id == 0) {
  500. file->error_id = FSE_NOT_EXIST;
  501. }
  502. } else {
  503. file->internal_error_id = LFS_ERR_BADF;
  504. file->error_id = storage_int_parse_error(file->internal_error_id);
  505. }
  506. return (file->error_id == FSE_OK);
  507. }
  508. static bool storage_int_dir_rewind(void* ctx, File* file) {
  509. StorageData* storage = ctx;
  510. lfs_t* lfs = lfs_get_from_storage(storage);
  511. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  512. if(lfs_handle_is_open(handle)) {
  513. file->internal_error_id = lfs_dir_rewind(lfs, lfs_handle_get_dir(handle));
  514. } else {
  515. file->internal_error_id = LFS_ERR_BADF;
  516. }
  517. file->error_id = storage_int_parse_error(file->internal_error_id);
  518. return (file->error_id == FSE_OK);
  519. }
  520. /******************* Common FS Functions *******************/
  521. static FS_Error storage_int_common_stat(void* ctx, const char* path, FileInfo* fileinfo) {
  522. StorageData* storage = ctx;
  523. lfs_t* lfs = lfs_get_from_storage(storage);
  524. struct lfs_info _fileinfo;
  525. int result = lfs_stat(lfs, path, &_fileinfo);
  526. if(fileinfo != NULL) {
  527. fileinfo->size = _fileinfo.size;
  528. fileinfo->flags = 0;
  529. if(_fileinfo.type & LFS_TYPE_DIR) fileinfo->flags |= FSF_DIRECTORY;
  530. }
  531. return storage_int_parse_error(result);
  532. }
  533. static FS_Error storage_int_common_remove(void* ctx, const char* path) {
  534. StorageData* storage = ctx;
  535. lfs_t* lfs = lfs_get_from_storage(storage);
  536. int result = lfs_remove(lfs, path);
  537. return storage_int_parse_error(result);
  538. }
  539. static FS_Error storage_int_common_mkdir(void* ctx, const char* path) {
  540. StorageData* storage = ctx;
  541. lfs_t* lfs = lfs_get_from_storage(storage);
  542. int result = lfs_mkdir(lfs, path);
  543. return storage_int_parse_error(result);
  544. }
  545. static FS_Error storage_int_common_fs_info(
  546. void* ctx,
  547. const char* fs_path,
  548. uint64_t* total_space,
  549. uint64_t* free_space) {
  550. UNUSED(fs_path);
  551. StorageData* storage = ctx;
  552. lfs_t* lfs = lfs_get_from_storage(storage);
  553. LFSData* lfs_data = lfs_data_get_from_storage(storage);
  554. if(total_space) {
  555. *total_space = lfs_data->config.block_size * lfs_data->config.block_count;
  556. }
  557. lfs_ssize_t result = lfs_fs_size(lfs);
  558. if(free_space && (result >= 0)) {
  559. *free_space = (lfs_data->config.block_count - result) * lfs_data->config.block_size;
  560. }
  561. return storage_int_parse_error(result);
  562. }
  563. /******************* Init Storage *******************/
  564. static const FS_Api fs_api = {
  565. .file =
  566. {
  567. .open = storage_int_file_open,
  568. .close = storage_int_file_close,
  569. .read = storage_int_file_read,
  570. .write = storage_int_file_write,
  571. .seek = storage_int_file_seek,
  572. .tell = storage_int_file_tell,
  573. .truncate = storage_int_file_truncate,
  574. .size = storage_int_file_size,
  575. .sync = storage_int_file_sync,
  576. .eof = storage_int_file_eof,
  577. },
  578. .dir =
  579. {
  580. .open = storage_int_dir_open,
  581. .close = storage_int_dir_close,
  582. .read = storage_int_dir_read,
  583. .rewind = storage_int_dir_rewind,
  584. },
  585. .common =
  586. {
  587. .stat = storage_int_common_stat,
  588. .mkdir = storage_int_common_mkdir,
  589. .remove = storage_int_common_remove,
  590. .fs_info = storage_int_common_fs_info,
  591. },
  592. };
  593. void storage_int_init(StorageData* storage) {
  594. FURI_LOG_I(TAG, "Starting");
  595. LFSData* lfs_data = storage_int_lfs_data_alloc();
  596. FURI_LOG_I(
  597. TAG,
  598. "Config: start %p, read %lu, write %lu, page size: %lu, page count: %lu, cycles: %ld",
  599. (void*)lfs_data->start_address,
  600. lfs_data->config.read_size,
  601. lfs_data->config.prog_size,
  602. lfs_data->config.block_size,
  603. lfs_data->config.block_count,
  604. lfs_data->config.block_cycles);
  605. storage_int_lfs_mount(lfs_data, storage);
  606. storage->data = lfs_data;
  607. storage->api.tick = NULL;
  608. storage->fs_api = &fs_api;
  609. }