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