storage-int.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. #include "storage-int.h"
  2. #include <lfs.h>
  3. #include <api-hal.h>
  4. #define TAG "storage-int"
  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_D(
  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_D(
  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(!api_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: %d", block, page);
  100. if(api_hal_flash_erase(page, 1)) {
  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) = api_hal_flash_get_free_page_start_address();
  114. *(size_t*)(&lfs_data->start_page) =
  115. (lfs_data->start_address - api_hal_flash_get_base()) / api_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 = api_hal_flash_get_read_block_size();
  125. lfs_data->config.prog_size = api_hal_flash_get_write_block_size();
  126. lfs_data->config.block_size = api_hal_flash_get_page_size();
  127. lfs_data->config.block_count = api_hal_flash_get_free_page_count();
  128. lfs_data->config.block_cycles = api_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. ApiHalBootFlag boot_flags = api_hal_boot_get_flags();
  136. lfs_t* lfs = &lfs_data->lfs;
  137. if(boot_flags & ApiHalBootFlagFactoryReset) {
  138. // Factory reset
  139. err = lfs_format(lfs, &lfs_data->config);
  140. if(err == 0) {
  141. FURI_LOG_I(TAG, "Factory reset: Format successful, trying to mount");
  142. api_hal_boot_set_flags(boot_flags & ~ApiHalBootFlagFactoryReset);
  143. err = lfs_mount(lfs, &lfs_data->config);
  144. if(err == 0) {
  145. FURI_LOG_I(TAG, "Factory reset: Mounted");
  146. storage->status = StorageStatusOK;
  147. } else {
  148. FURI_LOG_E(TAG, "Factory reset: Mount after format failed");
  149. storage->status = StorageStatusNotMounted;
  150. }
  151. } else {
  152. FURI_LOG_E(TAG, "Factory reset: Format failed");
  153. storage->status = StorageStatusNoFS;
  154. }
  155. } else {
  156. // Normal
  157. err = lfs_mount(lfs, &lfs_data->config);
  158. if(err == 0) {
  159. FURI_LOG_I(TAG, "Mounted");
  160. storage->status = StorageStatusOK;
  161. } else {
  162. FURI_LOG_E(TAG, "Mount failed, formatting");
  163. err = lfs_format(lfs, &lfs_data->config);
  164. if(err == 0) {
  165. FURI_LOG_I(TAG, "Format successful, trying to mount");
  166. err = lfs_mount(lfs, &lfs_data->config);
  167. if(err == 0) {
  168. FURI_LOG_I(TAG, "Mounted");
  169. storage->status = StorageStatusOK;
  170. } else {
  171. FURI_LOG_E(TAG, "Mount after format failed");
  172. storage->status = StorageStatusNotMounted;
  173. }
  174. } else {
  175. FURI_LOG_E(TAG, "Format failed");
  176. storage->status = StorageStatusNoFS;
  177. }
  178. }
  179. }
  180. }
  181. /****************** Common Functions ******************/
  182. static FS_Error storage_int_parse_error(int error) {
  183. FS_Error result = FSE_INTERNAL;
  184. if(error >= LFS_ERR_OK) {
  185. result = FSE_OK;
  186. } else {
  187. switch(error) {
  188. case LFS_ERR_IO:
  189. result = FSE_INTERNAL;
  190. break;
  191. case LFS_ERR_CORRUPT:
  192. result = FSE_INTERNAL;
  193. break;
  194. case LFS_ERR_NOENT:
  195. result = FSE_NOT_EXIST;
  196. break;
  197. case LFS_ERR_EXIST:
  198. result = FSE_EXIST;
  199. break;
  200. case LFS_ERR_NOTDIR:
  201. result = FSE_INVALID_NAME;
  202. break;
  203. case LFS_ERR_ISDIR:
  204. result = FSE_INVALID_NAME;
  205. break;
  206. case LFS_ERR_NOTEMPTY:
  207. result = FSE_DENIED;
  208. break;
  209. case LFS_ERR_BADF:
  210. result = FSE_INVALID_NAME;
  211. break;
  212. case LFS_ERR_FBIG:
  213. result = FSE_INTERNAL;
  214. break;
  215. case LFS_ERR_INVAL:
  216. result = FSE_INVALID_PARAMETER;
  217. break;
  218. case LFS_ERR_NOSPC:
  219. result = FSE_INTERNAL;
  220. break;
  221. case LFS_ERR_NOMEM:
  222. result = FSE_INTERNAL;
  223. break;
  224. case LFS_ERR_NOATTR:
  225. result = FSE_INVALID_PARAMETER;
  226. break;
  227. case LFS_ERR_NAMETOOLONG:
  228. result = FSE_INVALID_NAME;
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. return result;
  235. }
  236. /******************* File Functions *******************/
  237. static bool storage_int_file_open(
  238. void* ctx,
  239. File* file,
  240. const char* path,
  241. FS_AccessMode access_mode,
  242. FS_OpenMode open_mode) {
  243. StorageData* storage = ctx;
  244. lfs_t* lfs = lfs_get_from_storage(storage);
  245. int flags = 0;
  246. if(access_mode & FSAM_READ) flags |= LFS_O_RDONLY;
  247. if(access_mode & FSAM_WRITE) flags |= LFS_O_WRONLY;
  248. if(open_mode & FSOM_OPEN_EXISTING) flags = flags;
  249. if(open_mode & FSOM_OPEN_ALWAYS) flags |= LFS_O_CREAT;
  250. if(open_mode & FSOM_OPEN_APPEND) flags |= LFS_O_CREAT | LFS_O_APPEND;
  251. if(open_mode & FSOM_CREATE_NEW) flags |= LFS_O_CREAT | LFS_O_EXCL;
  252. if(open_mode & FSOM_CREATE_ALWAYS) flags |= LFS_O_CREAT | LFS_O_TRUNC;
  253. LFSHandle* handle = lfs_handle_alloc_file();
  254. storage_set_storage_file_data(file, handle, storage);
  255. file->internal_error_id = lfs_file_open(lfs, lfs_handle_get_file(handle), path, flags);
  256. if(file->internal_error_id >= LFS_ERR_OK) {
  257. lfs_handle_set_open(handle);
  258. }
  259. file->error_id = storage_int_parse_error(file->internal_error_id);
  260. return (file->error_id == FSE_OK);
  261. }
  262. static bool storage_int_file_close(void* ctx, File* file) {
  263. StorageData* storage = ctx;
  264. lfs_t* lfs = lfs_get_from_storage(storage);
  265. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  266. if(lfs_handle_is_open(handle)) {
  267. file->internal_error_id = lfs_file_close(lfs, lfs_handle_get_file(handle));
  268. } else {
  269. file->internal_error_id = LFS_ERR_BADF;
  270. }
  271. file->error_id = storage_int_parse_error(file->internal_error_id);
  272. lfs_handle_free(handle);
  273. return (file->error_id == FSE_OK);
  274. }
  275. static uint16_t
  276. storage_int_file_read(void* ctx, File* file, void* buff, uint16_t const bytes_to_read) {
  277. StorageData* storage = ctx;
  278. lfs_t* lfs = lfs_get_from_storage(storage);
  279. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  280. uint16_t bytes_readed = 0;
  281. if(lfs_handle_is_open(handle)) {
  282. file->internal_error_id =
  283. lfs_file_read(lfs, lfs_handle_get_file(handle), buff, bytes_to_read);
  284. } else {
  285. file->internal_error_id = LFS_ERR_BADF;
  286. }
  287. file->error_id = storage_int_parse_error(file->internal_error_id);
  288. if(file->error_id == FSE_OK) {
  289. bytes_readed = file->internal_error_id;
  290. file->internal_error_id = 0;
  291. }
  292. return bytes_readed;
  293. }
  294. static uint16_t
  295. storage_int_file_write(void* ctx, File* file, const void* buff, uint16_t const bytes_to_write) {
  296. StorageData* storage = ctx;
  297. lfs_t* lfs = lfs_get_from_storage(storage);
  298. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  299. uint16_t bytes_written = 0;
  300. if(lfs_handle_is_open(handle)) {
  301. file->internal_error_id =
  302. lfs_file_write(lfs, lfs_handle_get_file(handle), buff, bytes_to_write);
  303. } else {
  304. file->internal_error_id = LFS_ERR_BADF;
  305. }
  306. file->error_id = storage_int_parse_error(file->internal_error_id);
  307. if(file->error_id == FSE_OK) {
  308. bytes_written = file->internal_error_id;
  309. file->internal_error_id = 0;
  310. }
  311. return bytes_written;
  312. }
  313. static bool
  314. storage_int_file_seek(void* ctx, File* file, const uint32_t offset, const bool from_start) {
  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. if(from_start) {
  320. file->internal_error_id =
  321. lfs_file_seek(lfs, lfs_handle_get_file(handle), offset, LFS_SEEK_SET);
  322. } else {
  323. file->internal_error_id =
  324. lfs_file_seek(lfs, lfs_handle_get_file(handle), offset, LFS_SEEK_CUR);
  325. }
  326. } else {
  327. file->internal_error_id = LFS_ERR_BADF;
  328. }
  329. file->error_id = storage_int_parse_error(file->internal_error_id);
  330. return (file->error_id == FSE_OK);
  331. }
  332. static uint64_t storage_int_file_tell(void* ctx, File* file) {
  333. StorageData* storage = ctx;
  334. lfs_t* lfs = lfs_get_from_storage(storage);
  335. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  336. if(lfs_handle_is_open(handle)) {
  337. file->internal_error_id = lfs_file_tell(lfs, lfs_handle_get_file(handle));
  338. } else {
  339. file->internal_error_id = LFS_ERR_BADF;
  340. }
  341. file->error_id = storage_int_parse_error(file->internal_error_id);
  342. int32_t position = 0;
  343. if(file->error_id == FSE_OK) {
  344. position = file->internal_error_id;
  345. file->internal_error_id = 0;
  346. }
  347. return position;
  348. }
  349. static bool storage_int_file_truncate(void* ctx, File* file) {
  350. StorageData* storage = ctx;
  351. lfs_t* lfs = lfs_get_from_storage(storage);
  352. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  353. if(lfs_handle_is_open(handle)) {
  354. file->internal_error_id = lfs_file_tell(lfs, lfs_handle_get_file(handle));
  355. file->error_id = storage_int_parse_error(file->internal_error_id);
  356. if(file->error_id == FSE_OK) {
  357. uint32_t position = file->internal_error_id;
  358. file->internal_error_id =
  359. lfs_file_truncate(lfs, lfs_handle_get_file(handle), position);
  360. file->error_id = storage_int_parse_error(file->internal_error_id);
  361. }
  362. } else {
  363. file->internal_error_id = LFS_ERR_BADF;
  364. file->error_id = storage_int_parse_error(file->internal_error_id);
  365. }
  366. return (file->error_id == FSE_OK);
  367. }
  368. static bool storage_int_file_sync(void* ctx, File* file) {
  369. StorageData* storage = ctx;
  370. lfs_t* lfs = lfs_get_from_storage(storage);
  371. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  372. if(lfs_handle_is_open(handle)) {
  373. file->internal_error_id = lfs_file_sync(lfs, lfs_handle_get_file(handle));
  374. } else {
  375. file->internal_error_id = LFS_ERR_BADF;
  376. }
  377. file->error_id = storage_int_parse_error(file->internal_error_id);
  378. return (file->error_id == FSE_OK);
  379. }
  380. static uint64_t storage_int_file_size(void* ctx, File* file) {
  381. StorageData* storage = ctx;
  382. lfs_t* lfs = lfs_get_from_storage(storage);
  383. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  384. if(lfs_handle_is_open(handle)) {
  385. file->internal_error_id = lfs_file_size(lfs, lfs_handle_get_file(handle));
  386. } else {
  387. file->internal_error_id = LFS_ERR_BADF;
  388. }
  389. file->error_id = storage_int_parse_error(file->internal_error_id);
  390. uint32_t size = 0;
  391. if(file->error_id == FSE_OK) {
  392. size = file->internal_error_id;
  393. file->internal_error_id = 0;
  394. }
  395. return size;
  396. }
  397. static bool storage_int_file_eof(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. bool eof = true;
  402. if(lfs_handle_is_open(handle)) {
  403. int32_t position = lfs_file_tell(lfs, lfs_handle_get_file(handle));
  404. int32_t size = lfs_file_size(lfs, lfs_handle_get_file(handle));
  405. if(position < 0) {
  406. file->internal_error_id = position;
  407. } else if(size < 0) {
  408. file->internal_error_id = size;
  409. } else {
  410. file->internal_error_id = LFS_ERR_OK;
  411. eof = (position >= size);
  412. }
  413. } else {
  414. file->internal_error_id = LFS_ERR_BADF;
  415. }
  416. file->error_id = storage_int_parse_error(file->internal_error_id);
  417. return eof;
  418. }
  419. /******************* Dir Functions *******************/
  420. static bool storage_int_dir_open(void* ctx, File* file, const char* path) {
  421. StorageData* storage = ctx;
  422. lfs_t* lfs = lfs_get_from_storage(storage);
  423. LFSHandle* handle = lfs_handle_alloc_dir();
  424. storage_set_storage_file_data(file, handle, storage);
  425. file->internal_error_id = lfs_dir_open(lfs, lfs_handle_get_dir(handle), path);
  426. if(file->internal_error_id >= LFS_ERR_OK) {
  427. lfs_handle_set_open(handle);
  428. }
  429. file->error_id = storage_int_parse_error(file->internal_error_id);
  430. return (file->error_id == FSE_OK);
  431. }
  432. static bool storage_int_dir_close(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_dir_close(lfs, lfs_handle_get_dir(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. lfs_handle_free(handle);
  443. return (file->error_id == FSE_OK);
  444. }
  445. static bool storage_int_dir_read(
  446. void* ctx,
  447. File* file,
  448. FileInfo* fileinfo,
  449. char* name,
  450. const uint16_t name_length) {
  451. StorageData* storage = ctx;
  452. lfs_t* lfs = lfs_get_from_storage(storage);
  453. LFSHandle* handle = storage_get_storage_file_data(file, storage);
  454. if(lfs_handle_is_open(handle)) {
  455. struct lfs_info _fileinfo;
  456. // LFS returns virtual directories "." and "..", so we read until we get something meaningful or an empty string
  457. do {
  458. file->internal_error_id = lfs_dir_read(lfs, lfs_handle_get_dir(handle), &_fileinfo);
  459. file->error_id = storage_int_parse_error(file->internal_error_id);
  460. } while(strcmp(_fileinfo.name, ".") == 0 || strcmp(_fileinfo.name, "..") == 0);
  461. if(fileinfo != NULL) {
  462. fileinfo->size = _fileinfo.size;
  463. fileinfo->flags = 0;
  464. if(_fileinfo.type & LFS_TYPE_DIR) fileinfo->flags |= FSF_DIRECTORY;
  465. }
  466. if(name != NULL) {
  467. snprintf(name, name_length, "%s", _fileinfo.name);
  468. }
  469. // set FSE_NOT_EXIST error on end of directory
  470. if(file->internal_error_id == 0) {
  471. file->error_id = FSE_NOT_EXIST;
  472. }
  473. } else {
  474. file->internal_error_id = LFS_ERR_BADF;
  475. file->error_id = storage_int_parse_error(file->internal_error_id);
  476. }
  477. return (file->error_id == FSE_OK);
  478. }
  479. static bool storage_int_dir_rewind(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_rewind(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. return (file->error_id == FSE_OK);
  490. }
  491. /******************* Common FS Functions *******************/
  492. static FS_Error storage_int_common_stat(void* ctx, const char* path, FileInfo* fileinfo) {
  493. StorageData* storage = ctx;
  494. lfs_t* lfs = lfs_get_from_storage(storage);
  495. struct lfs_info _fileinfo;
  496. int result = lfs_stat(lfs, path, &_fileinfo);
  497. if(fileinfo != NULL) {
  498. fileinfo->size = _fileinfo.size;
  499. fileinfo->flags = 0;
  500. if(_fileinfo.type & LFS_TYPE_DIR) fileinfo->flags |= FSF_DIRECTORY;
  501. }
  502. return storage_int_parse_error(result);
  503. }
  504. static FS_Error storage_int_common_remove(void* ctx, const char* path) {
  505. StorageData* storage = ctx;
  506. lfs_t* lfs = lfs_get_from_storage(storage);
  507. int result = lfs_remove(lfs, path);
  508. return storage_int_parse_error(result);
  509. }
  510. static FS_Error storage_int_common_rename(void* ctx, const char* old_path, const char* new_path) {
  511. StorageData* storage = ctx;
  512. lfs_t* lfs = lfs_get_from_storage(storage);
  513. int result = lfs_rename(lfs, old_path, new_path);
  514. return storage_int_parse_error(result);
  515. }
  516. static FS_Error storage_int_common_mkdir(void* ctx, const char* path) {
  517. StorageData* storage = ctx;
  518. lfs_t* lfs = lfs_get_from_storage(storage);
  519. int result = lfs_mkdir(lfs, path);
  520. return storage_int_parse_error(result);
  521. }
  522. static FS_Error storage_int_common_fs_info(
  523. void* ctx,
  524. const char* fs_path,
  525. uint64_t* total_space,
  526. uint64_t* free_space) {
  527. StorageData* storage = ctx;
  528. lfs_t* lfs = lfs_get_from_storage(storage);
  529. LFSData* lfs_data = lfs_data_get_from_storage(storage);
  530. *total_space = lfs_data->config.block_size * lfs_data->config.block_count;
  531. lfs_ssize_t result = lfs_fs_size(lfs);
  532. if(result >= 0) {
  533. *free_space = *total_space - (result * lfs_data->config.block_size);
  534. }
  535. return storage_int_parse_error(result);
  536. }
  537. /******************* Init Storage *******************/
  538. void storage_int_init(StorageData* storage) {
  539. FURI_LOG_I(TAG, "Starting");
  540. LFSData* lfs_data = storage_int_lfs_data_alloc();
  541. FURI_LOG_I(
  542. TAG,
  543. "Config: start %p, read %d, write %d, page size: %d, page count: %d, cycles: %d",
  544. lfs_data->start_address,
  545. lfs_data->config.read_size,
  546. lfs_data->config.prog_size,
  547. lfs_data->config.block_size,
  548. lfs_data->config.block_count,
  549. lfs_data->config.block_cycles);
  550. storage_int_lfs_mount(lfs_data, storage);
  551. storage->data = lfs_data;
  552. storage->api.tick = NULL;
  553. storage->fs_api.file.open = storage_int_file_open;
  554. storage->fs_api.file.close = storage_int_file_close;
  555. storage->fs_api.file.read = storage_int_file_read;
  556. storage->fs_api.file.write = storage_int_file_write;
  557. storage->fs_api.file.seek = storage_int_file_seek;
  558. storage->fs_api.file.tell = storage_int_file_tell;
  559. storage->fs_api.file.truncate = storage_int_file_truncate;
  560. storage->fs_api.file.size = storage_int_file_size;
  561. storage->fs_api.file.sync = storage_int_file_sync;
  562. storage->fs_api.file.eof = storage_int_file_eof;
  563. storage->fs_api.dir.open = storage_int_dir_open;
  564. storage->fs_api.dir.close = storage_int_dir_close;
  565. storage->fs_api.dir.read = storage_int_dir_read;
  566. storage->fs_api.dir.rewind = storage_int_dir_rewind;
  567. storage->fs_api.common.stat = storage_int_common_stat;
  568. storage->fs_api.common.mkdir = storage_int_common_mkdir;
  569. storage->fs_api.common.rename = storage_int_common_rename;
  570. storage->fs_api.common.remove = storage_int_common_remove;
  571. storage->fs_api.common.fs_info = storage_int_common_fs_info;
  572. }