storage_ext.c 18 KB

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