storage_ext.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #include "fatfs.h"
  2. #include "../filesystem_api_internal.h"
  3. #include "storage_ext.h"
  4. #include <furi_hal.h>
  5. #include "sd_notify.h"
  6. #include <furi_hal_sd.h>
  7. typedef FIL SDFile;
  8. typedef DIR SDDir;
  9. typedef FILINFO SDFileInfo;
  10. typedef FRESULT SDError;
  11. #define TAG "StorageExt"
  12. #define STORAGE_PATH "/ext"
  13. /********************* Definitions ********************/
  14. typedef struct {
  15. FATFS* fs;
  16. const char* path;
  17. bool sd_was_present;
  18. } SDData;
  19. static FS_Error storage_ext_parse_error(SDError error);
  20. /******************* Core Functions *******************/
  21. static bool sd_mount_card(StorageData* storage, bool notify) {
  22. bool result = false;
  23. const uint8_t max_init_counts = 10;
  24. uint8_t counter = max_init_counts;
  25. uint8_t bsp_result;
  26. SDData* sd_data = storage->data;
  27. storage_data_lock(storage);
  28. while(result == false && counter > 0 && hal_sd_detect()) {
  29. if(notify) {
  30. NotificationApp* notification = furi_record_open("notification");
  31. sd_notify_wait(notification);
  32. furi_record_close("notification");
  33. }
  34. if((counter % 2) == 0) {
  35. // power reset sd card
  36. bsp_result = BSP_SD_Init(true);
  37. } else {
  38. bsp_result = BSP_SD_Init(false);
  39. }
  40. if(bsp_result) {
  41. // bsp error
  42. storage->status = StorageStatusErrorInternal;
  43. } else {
  44. SDError status = f_mount(sd_data->fs, sd_data->path, 1);
  45. if(status == FR_OK || status == FR_NO_FILESYSTEM) {
  46. #ifndef FURI_RAM_EXEC
  47. FATFS* fs;
  48. uint32_t free_clusters;
  49. status = f_getfree(sd_data->path, &free_clusters, &fs);
  50. #endif
  51. if(status == FR_OK || status == FR_NO_FILESYSTEM) {
  52. result = true;
  53. }
  54. if(status == FR_OK) {
  55. storage->status = StorageStatusOK;
  56. } else if(status == FR_NO_FILESYSTEM) {
  57. storage->status = StorageStatusNoFS;
  58. } else {
  59. storage->status = StorageStatusNotAccessible;
  60. }
  61. } else {
  62. storage->status = StorageStatusNotMounted;
  63. }
  64. }
  65. if(notify) {
  66. NotificationApp* notification = furi_record_open("notification");
  67. sd_notify_wait_off(notification);
  68. furi_record_close("notification");
  69. }
  70. if(!result) {
  71. furi_hal_delay_ms(1000);
  72. FURI_LOG_E(
  73. TAG, "init cycle %d, error: %s", counter, storage_data_status_text(storage));
  74. counter--;
  75. }
  76. }
  77. storage_data_unlock(storage);
  78. return result;
  79. }
  80. FS_Error sd_unmount_card(StorageData* storage) {
  81. SDData* sd_data = storage->data;
  82. SDError error;
  83. storage_data_lock(storage);
  84. storage->status = StorageStatusNotReady;
  85. error = FR_DISK_ERR;
  86. // TODO do i need to close the files?
  87. f_mount(0, sd_data->path, 0);
  88. storage_data_unlock(storage);
  89. return storage_ext_parse_error(error);
  90. }
  91. FS_Error sd_format_card(StorageData* storage) {
  92. #ifdef FURI_RAM_EXEC
  93. UNUSED(storage);
  94. return FSE_NOT_READY;
  95. #else
  96. uint8_t* work_area;
  97. SDData* sd_data = storage->data;
  98. SDError error;
  99. storage_data_lock(storage);
  100. work_area = malloc(_MAX_SS);
  101. error = f_mkfs(sd_data->path, FM_ANY, 0, work_area, _MAX_SS);
  102. free(work_area);
  103. do {
  104. storage->status = StorageStatusNotAccessible;
  105. if(error != FR_OK) break;
  106. storage->status = StorageStatusNoFS;
  107. error = f_setlabel("Flipper SD");
  108. if(error != FR_OK) break;
  109. storage->status = StorageStatusNotMounted;
  110. error = f_mount(sd_data->fs, sd_data->path, 1);
  111. if(error != FR_OK) break;
  112. storage->status = StorageStatusOK;
  113. } while(false);
  114. storage_data_unlock(storage);
  115. return storage_ext_parse_error(error);
  116. #endif
  117. }
  118. FS_Error sd_card_info(StorageData* storage, SDInfo* sd_info) {
  119. #ifndef FURI_RAM_EXEC
  120. uint32_t free_clusters, free_sectors, total_sectors;
  121. FATFS* fs;
  122. #endif
  123. SDData* sd_data = storage->data;
  124. SDError error;
  125. // clean data
  126. memset(sd_info, 0, sizeof(SDInfo));
  127. // get fs info
  128. storage_data_lock(storage);
  129. error = f_getlabel(sd_data->path, sd_info->label, NULL);
  130. if(error == FR_OK) {
  131. #ifndef FURI_RAM_EXEC
  132. error = f_getfree(sd_data->path, &free_clusters, &fs);
  133. #endif
  134. }
  135. storage_data_unlock(storage);
  136. if(error == FR_OK) {
  137. // calculate size
  138. #ifndef FURI_RAM_EXEC
  139. total_sectors = (fs->n_fatent - 2) * fs->csize;
  140. free_sectors = free_clusters * fs->csize;
  141. #endif
  142. uint16_t sector_size = _MAX_SS;
  143. #if _MAX_SS != _MIN_SS
  144. sector_size = fs->ssize;
  145. #endif
  146. #ifdef FURI_RAM_EXEC
  147. sd_info->fs_type = 0;
  148. sd_info->kb_total = 0;
  149. sd_info->kb_free = 0;
  150. sd_info->cluster_size = 512;
  151. sd_info->sector_size = sector_size;
  152. #else
  153. sd_info->fs_type = fs->fs_type;
  154. switch(fs->fs_type) {
  155. case FS_FAT12:
  156. sd_info->fs_type = FST_FAT12;
  157. break;
  158. case FS_FAT16:
  159. sd_info->fs_type = FST_FAT16;
  160. break;
  161. case FS_FAT32:
  162. sd_info->fs_type = FST_FAT32;
  163. break;
  164. case FS_EXFAT:
  165. sd_info->fs_type = FST_EXFAT;
  166. break;
  167. default:
  168. sd_info->fs_type = FST_UNKNOWN;
  169. break;
  170. }
  171. sd_info->kb_total = total_sectors / 1024 * sector_size;
  172. sd_info->kb_free = free_sectors / 1024 * sector_size;
  173. sd_info->cluster_size = fs->csize;
  174. sd_info->sector_size = sector_size;
  175. #endif
  176. }
  177. return storage_ext_parse_error(error);
  178. }
  179. static void storage_ext_tick_internal(StorageData* storage, bool notify) {
  180. SDData* sd_data = storage->data;
  181. if(sd_data->sd_was_present) {
  182. if(hal_sd_detect()) {
  183. FURI_LOG_I(TAG, "card detected");
  184. sd_mount_card(storage, notify);
  185. if(storage->status != StorageStatusOK) {
  186. FURI_LOG_E(TAG, "sd init error: %s", storage_data_status_text(storage));
  187. if(notify) {
  188. NotificationApp* notification = furi_record_open("notification");
  189. sd_notify_error(notification);
  190. furi_record_close("notification");
  191. }
  192. } else {
  193. FURI_LOG_I(TAG, "card mounted");
  194. if(notify) {
  195. NotificationApp* notification = furi_record_open("notification");
  196. sd_notify_success(notification);
  197. furi_record_close("notification");
  198. }
  199. }
  200. sd_data->sd_was_present = false;
  201. if(!hal_sd_detect()) {
  202. FURI_LOG_I(TAG, "card removed while mounting");
  203. sd_unmount_card(storage);
  204. sd_data->sd_was_present = true;
  205. }
  206. }
  207. } else {
  208. if(!hal_sd_detect()) {
  209. FURI_LOG_I(TAG, "card removed");
  210. sd_data->sd_was_present = true;
  211. sd_unmount_card(storage);
  212. if(notify) {
  213. NotificationApp* notification = furi_record_open("notification");
  214. sd_notify_eject(notification);
  215. furi_record_close("notification");
  216. }
  217. }
  218. }
  219. }
  220. static void storage_ext_tick(StorageData* storage) {
  221. storage_ext_tick_internal(storage, true);
  222. }
  223. /****************** Common Functions ******************/
  224. static FS_Error storage_ext_parse_error(SDError error) {
  225. FS_Error result;
  226. switch(error) {
  227. case FR_OK:
  228. result = FSE_OK;
  229. break;
  230. case FR_NOT_READY:
  231. result = FSE_NOT_READY;
  232. break;
  233. case FR_NO_FILE:
  234. case FR_NO_PATH:
  235. case FR_NO_FILESYSTEM:
  236. result = FSE_NOT_EXIST;
  237. break;
  238. case FR_EXIST:
  239. result = FSE_EXIST;
  240. break;
  241. case FR_INVALID_NAME:
  242. result = FSE_INVALID_NAME;
  243. break;
  244. case FR_INVALID_OBJECT:
  245. case FR_INVALID_PARAMETER:
  246. result = FSE_INVALID_PARAMETER;
  247. break;
  248. case FR_DENIED:
  249. result = FSE_DENIED;
  250. break;
  251. default:
  252. result = FSE_INTERNAL;
  253. break;
  254. }
  255. return result;
  256. }
  257. /******************* File Functions *******************/
  258. static bool storage_ext_file_open(
  259. void* ctx,
  260. File* file,
  261. const char* path,
  262. FS_AccessMode access_mode,
  263. FS_OpenMode open_mode) {
  264. StorageData* storage = ctx;
  265. uint8_t _mode = 0;
  266. if(access_mode & FSAM_READ) _mode |= FA_READ;
  267. if(access_mode & FSAM_WRITE) _mode |= FA_WRITE;
  268. if(open_mode & FSOM_OPEN_EXISTING) _mode |= FA_OPEN_EXISTING;
  269. if(open_mode & FSOM_OPEN_ALWAYS) _mode |= FA_OPEN_ALWAYS;
  270. if(open_mode & FSOM_OPEN_APPEND) _mode |= FA_OPEN_APPEND;
  271. if(open_mode & FSOM_CREATE_NEW) _mode |= FA_CREATE_NEW;
  272. if(open_mode & FSOM_CREATE_ALWAYS) _mode |= FA_CREATE_ALWAYS;
  273. SDFile* file_data = malloc(sizeof(SDFile));
  274. storage_set_storage_file_data(file, file_data, storage);
  275. file->internal_error_id = f_open(file_data, path, _mode);
  276. file->error_id = storage_ext_parse_error(file->internal_error_id);
  277. return (file->error_id == FSE_OK);
  278. }
  279. static bool storage_ext_file_close(void* ctx, File* file) {
  280. StorageData* storage = ctx;
  281. SDFile* file_data = storage_get_storage_file_data(file, storage);
  282. file->internal_error_id = f_close(file_data);
  283. file->error_id = storage_ext_parse_error(file->internal_error_id);
  284. free(file_data);
  285. return (file->error_id == FSE_OK);
  286. }
  287. static uint16_t
  288. storage_ext_file_read(void* ctx, File* file, void* buff, uint16_t const bytes_to_read) {
  289. StorageData* storage = ctx;
  290. SDFile* file_data = storage_get_storage_file_data(file, storage);
  291. uint16_t bytes_read = 0;
  292. file->internal_error_id = f_read(file_data, buff, bytes_to_read, &bytes_read);
  293. file->error_id = storage_ext_parse_error(file->internal_error_id);
  294. return bytes_read;
  295. }
  296. static uint16_t
  297. storage_ext_file_write(void* ctx, File* file, const void* buff, uint16_t const bytes_to_write) {
  298. #ifdef FURI_RAM_EXEC
  299. UNUSED(ctx);
  300. UNUSED(file);
  301. UNUSED(buff);
  302. UNUSED(bytes_to_write);
  303. return FSE_NOT_READY;
  304. #else
  305. StorageData* storage = ctx;
  306. SDFile* file_data = storage_get_storage_file_data(file, storage);
  307. uint16_t bytes_written = 0;
  308. file->internal_error_id = f_write(file_data, buff, bytes_to_write, &bytes_written);
  309. file->error_id = storage_ext_parse_error(file->internal_error_id);
  310. return bytes_written;
  311. #endif
  312. }
  313. static bool
  314. storage_ext_file_seek(void* ctx, File* file, const uint32_t offset, const bool from_start) {
  315. StorageData* storage = ctx;
  316. SDFile* file_data = storage_get_storage_file_data(file, storage);
  317. if(from_start) {
  318. file->internal_error_id = f_lseek(file_data, offset);
  319. } else {
  320. uint64_t position = f_tell(file_data);
  321. position += offset;
  322. file->internal_error_id = f_lseek(file_data, position);
  323. }
  324. file->error_id = storage_ext_parse_error(file->internal_error_id);
  325. return (file->error_id == FSE_OK);
  326. }
  327. static uint64_t storage_ext_file_tell(void* ctx, File* file) {
  328. StorageData* storage = ctx;
  329. SDFile* file_data = storage_get_storage_file_data(file, storage);
  330. uint64_t position = 0;
  331. position = f_tell(file_data);
  332. file->error_id = FSE_OK;
  333. return position;
  334. }
  335. static bool storage_ext_file_truncate(void* ctx, File* file) {
  336. #ifdef FURI_RAM_EXEC
  337. UNUSED(ctx);
  338. UNUSED(file);
  339. return FSE_NOT_READY;
  340. #else
  341. StorageData* storage = ctx;
  342. SDFile* file_data = storage_get_storage_file_data(file, storage);
  343. file->internal_error_id = f_truncate(file_data);
  344. file->error_id = storage_ext_parse_error(file->internal_error_id);
  345. return (file->error_id == FSE_OK);
  346. #endif
  347. }
  348. static bool storage_ext_file_sync(void* ctx, File* file) {
  349. #ifdef FURI_RAM_EXEC
  350. UNUSED(ctx);
  351. UNUSED(file);
  352. return FSE_NOT_READY;
  353. #else
  354. StorageData* storage = ctx;
  355. SDFile* file_data = storage_get_storage_file_data(file, storage);
  356. file->internal_error_id = f_sync(file_data);
  357. file->error_id = storage_ext_parse_error(file->internal_error_id);
  358. return (file->error_id == FSE_OK);
  359. #endif
  360. }
  361. static uint64_t storage_ext_file_size(void* ctx, File* file) {
  362. StorageData* storage = ctx;
  363. SDFile* file_data = storage_get_storage_file_data(file, storage);
  364. uint64_t size = 0;
  365. size = f_size(file_data);
  366. file->error_id = FSE_OK;
  367. return size;
  368. }
  369. static bool storage_ext_file_eof(void* ctx, File* file) {
  370. StorageData* storage = ctx;
  371. SDFile* file_data = storage_get_storage_file_data(file, storage);
  372. bool eof = f_eof(file_data);
  373. file->internal_error_id = 0;
  374. file->error_id = FSE_OK;
  375. return eof;
  376. }
  377. /******************* Dir Functions *******************/
  378. static bool storage_ext_dir_open(void* ctx, File* file, const char* path) {
  379. StorageData* storage = ctx;
  380. SDDir* file_data = malloc(sizeof(SDDir));
  381. storage_set_storage_file_data(file, file_data, storage);
  382. file->internal_error_id = f_opendir(file_data, path);
  383. file->error_id = storage_ext_parse_error(file->internal_error_id);
  384. return (file->error_id == FSE_OK);
  385. }
  386. static bool storage_ext_dir_close(void* ctx, File* file) {
  387. StorageData* storage = ctx;
  388. SDDir* file_data = storage_get_storage_file_data(file, storage);
  389. file->internal_error_id = f_closedir(file_data);
  390. file->error_id = storage_ext_parse_error(file->internal_error_id);
  391. free(file_data);
  392. return (file->error_id == FSE_OK);
  393. }
  394. static bool storage_ext_dir_read(
  395. void* ctx,
  396. File* file,
  397. FileInfo* fileinfo,
  398. char* name,
  399. const uint16_t name_length) {
  400. StorageData* storage = ctx;
  401. SDDir* file_data = storage_get_storage_file_data(file, storage);
  402. SDFileInfo _fileinfo;
  403. file->internal_error_id = f_readdir(file_data, &_fileinfo);
  404. file->error_id = storage_ext_parse_error(file->internal_error_id);
  405. if(fileinfo != NULL) {
  406. fileinfo->size = _fileinfo.fsize;
  407. fileinfo->flags = 0;
  408. if(_fileinfo.fattrib & AM_DIR) fileinfo->flags |= FSF_DIRECTORY;
  409. }
  410. if(name != NULL) {
  411. snprintf(name, name_length, "%s", _fileinfo.fname);
  412. }
  413. if(_fileinfo.fname[0] == 0) {
  414. file->error_id = FSE_NOT_EXIST;
  415. }
  416. return (file->error_id == FSE_OK);
  417. }
  418. static bool storage_ext_dir_rewind(void* ctx, File* file) {
  419. StorageData* storage = ctx;
  420. SDDir* file_data = storage_get_storage_file_data(file, storage);
  421. file->internal_error_id = f_readdir(file_data, NULL);
  422. file->error_id = storage_ext_parse_error(file->internal_error_id);
  423. return (file->error_id == FSE_OK);
  424. }
  425. /******************* Common FS Functions *******************/
  426. static FS_Error storage_ext_common_stat(void* ctx, const char* path, FileInfo* fileinfo) {
  427. UNUSED(ctx);
  428. SDFileInfo _fileinfo;
  429. SDError result = f_stat(path, &_fileinfo);
  430. if(fileinfo != NULL) {
  431. fileinfo->size = _fileinfo.fsize;
  432. fileinfo->flags = 0;
  433. if(_fileinfo.fattrib & AM_DIR) fileinfo->flags |= FSF_DIRECTORY;
  434. }
  435. return storage_ext_parse_error(result);
  436. }
  437. static FS_Error storage_ext_common_remove(void* ctx, const char* path) {
  438. UNUSED(ctx);
  439. #ifdef FURI_RAM_EXEC
  440. UNUSED(path);
  441. return FSE_NOT_READY;
  442. #else
  443. SDError result = f_unlink(path);
  444. return storage_ext_parse_error(result);
  445. #endif
  446. }
  447. static FS_Error storage_ext_common_mkdir(void* ctx, const char* path) {
  448. UNUSED(ctx);
  449. #ifdef FURI_RAM_EXEC
  450. UNUSED(path);
  451. return FSE_NOT_READY;
  452. #else
  453. SDError result = f_mkdir(path);
  454. return storage_ext_parse_error(result);
  455. #endif
  456. }
  457. static FS_Error storage_ext_common_fs_info(
  458. void* ctx,
  459. const char* fs_path,
  460. uint64_t* total_space,
  461. uint64_t* free_space) {
  462. UNUSED(fs_path);
  463. #ifdef FURI_RAM_EXEC
  464. UNUSED(ctx);
  465. UNUSED(total_space);
  466. UNUSED(free_space);
  467. return FSE_NOT_READY;
  468. #else
  469. StorageData* storage = ctx;
  470. SDData* sd_data = storage->data;
  471. DWORD free_clusters;
  472. FATFS* fs;
  473. SDError fresult = f_getfree(sd_data->path, &free_clusters, &fs);
  474. if((FRESULT)fresult == FR_OK) {
  475. uint32_t total_sectors = (fs->n_fatent - 2) * fs->csize;
  476. uint32_t free_sectors = free_clusters * fs->csize;
  477. uint16_t sector_size = _MAX_SS;
  478. #if _MAX_SS != _MIN_SS
  479. sector_size = fs->ssize;
  480. #endif
  481. if(total_space != NULL) {
  482. *total_space = (uint64_t)total_sectors * (uint64_t)sector_size;
  483. }
  484. if(free_space != NULL) {
  485. *free_space = (uint64_t)free_sectors * (uint64_t)sector_size;
  486. }
  487. }
  488. return storage_ext_parse_error(fresult);
  489. #endif
  490. }
  491. /******************* Init Storage *******************/
  492. static const FS_Api fs_api = {
  493. .file =
  494. {
  495. .open = storage_ext_file_open,
  496. .close = storage_ext_file_close,
  497. .read = storage_ext_file_read,
  498. .write = storage_ext_file_write,
  499. .seek = storage_ext_file_seek,
  500. .tell = storage_ext_file_tell,
  501. .truncate = storage_ext_file_truncate,
  502. .size = storage_ext_file_size,
  503. .sync = storage_ext_file_sync,
  504. .eof = storage_ext_file_eof,
  505. },
  506. .dir =
  507. {
  508. .open = storage_ext_dir_open,
  509. .close = storage_ext_dir_close,
  510. .read = storage_ext_dir_read,
  511. .rewind = storage_ext_dir_rewind,
  512. },
  513. .common =
  514. {
  515. .stat = storage_ext_common_stat,
  516. .mkdir = storage_ext_common_mkdir,
  517. .remove = storage_ext_common_remove,
  518. .fs_info = storage_ext_common_fs_info,
  519. },
  520. };
  521. void storage_ext_init(StorageData* storage) {
  522. SDData* sd_data = malloc(sizeof(SDData));
  523. sd_data->fs = &USERFatFS;
  524. sd_data->path = "0:/";
  525. sd_data->sd_was_present = true;
  526. storage->data = sd_data;
  527. storage->api.tick = storage_ext_tick;
  528. storage->fs_api = &fs_api;
  529. hal_sd_detect_init();
  530. // do not notify on first launch, notifications app is waiting for our thread to read settings
  531. storage_ext_tick_internal(storage, false);
  532. }