storage-int.c 21 KB

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