storage_ext.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. 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("notification");
  188. sd_notify_error(notification);
  189. furi_record_close("notification");
  190. }
  191. } else {
  192. FURI_LOG_I(TAG, "card mounted");
  193. if(notify) {
  194. NotificationApp* notification = furi_record_open("notification");
  195. sd_notify_success(notification);
  196. furi_record_close("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("notification");
  213. sd_notify_eject(notification);
  214. furi_record_close("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. return FSE_NOT_READY;
  299. #else
  300. StorageData* storage = ctx;
  301. SDFile* file_data = storage_get_storage_file_data(file, storage);
  302. uint16_t bytes_written = 0;
  303. file->internal_error_id = f_write(file_data, buff, bytes_to_write, &bytes_written);
  304. file->error_id = storage_ext_parse_error(file->internal_error_id);
  305. return bytes_written;
  306. #endif
  307. }
  308. static bool
  309. storage_ext_file_seek(void* ctx, File* file, const uint32_t offset, const bool from_start) {
  310. StorageData* storage = ctx;
  311. SDFile* file_data = storage_get_storage_file_data(file, storage);
  312. if(from_start) {
  313. file->internal_error_id = f_lseek(file_data, offset);
  314. } else {
  315. uint64_t position = f_tell(file_data);
  316. position += offset;
  317. file->internal_error_id = f_lseek(file_data, position);
  318. }
  319. file->error_id = storage_ext_parse_error(file->internal_error_id);
  320. return (file->error_id == FSE_OK);
  321. }
  322. static uint64_t storage_ext_file_tell(void* ctx, File* file) {
  323. StorageData* storage = ctx;
  324. SDFile* file_data = storage_get_storage_file_data(file, storage);
  325. uint64_t position = 0;
  326. position = f_tell(file_data);
  327. file->error_id = FSE_OK;
  328. return position;
  329. }
  330. static bool storage_ext_file_truncate(void* ctx, File* file) {
  331. #ifdef FURI_RAM_EXEC
  332. return FSE_NOT_READY;
  333. #else
  334. StorageData* storage = ctx;
  335. SDFile* file_data = storage_get_storage_file_data(file, storage);
  336. file->internal_error_id = f_truncate(file_data);
  337. file->error_id = storage_ext_parse_error(file->internal_error_id);
  338. return (file->error_id == FSE_OK);
  339. #endif
  340. }
  341. static bool storage_ext_file_sync(void* ctx, File* file) {
  342. #ifdef FURI_RAM_EXEC
  343. return FSE_NOT_READY;
  344. #else
  345. StorageData* storage = ctx;
  346. SDFile* file_data = storage_get_storage_file_data(file, storage);
  347. file->internal_error_id = f_sync(file_data);
  348. file->error_id = storage_ext_parse_error(file->internal_error_id);
  349. return (file->error_id == FSE_OK);
  350. #endif
  351. }
  352. static uint64_t storage_ext_file_size(void* ctx, File* file) {
  353. StorageData* storage = ctx;
  354. SDFile* file_data = storage_get_storage_file_data(file, storage);
  355. uint64_t size = 0;
  356. size = f_size(file_data);
  357. file->error_id = FSE_OK;
  358. return size;
  359. }
  360. static bool storage_ext_file_eof(void* ctx, File* file) {
  361. StorageData* storage = ctx;
  362. SDFile* file_data = storage_get_storage_file_data(file, storage);
  363. bool eof = f_eof(file_data);
  364. file->internal_error_id = 0;
  365. file->error_id = FSE_OK;
  366. return eof;
  367. }
  368. /******************* Dir Functions *******************/
  369. static bool storage_ext_dir_open(void* ctx, File* file, const char* path) {
  370. StorageData* storage = ctx;
  371. SDDir* file_data = malloc(sizeof(SDDir));
  372. storage_set_storage_file_data(file, file_data, storage);
  373. file->internal_error_id = f_opendir(file_data, path);
  374. file->error_id = storage_ext_parse_error(file->internal_error_id);
  375. return (file->error_id == FSE_OK);
  376. }
  377. static bool storage_ext_dir_close(void* ctx, File* file) {
  378. StorageData* storage = ctx;
  379. SDDir* file_data = storage_get_storage_file_data(file, storage);
  380. file->internal_error_id = f_closedir(file_data);
  381. file->error_id = storage_ext_parse_error(file->internal_error_id);
  382. free(file_data);
  383. return (file->error_id == FSE_OK);
  384. }
  385. static bool storage_ext_dir_read(
  386. void* ctx,
  387. File* file,
  388. FileInfo* fileinfo,
  389. char* name,
  390. const uint16_t name_length) {
  391. StorageData* storage = ctx;
  392. SDDir* file_data = storage_get_storage_file_data(file, storage);
  393. SDFileInfo _fileinfo;
  394. file->internal_error_id = f_readdir(file_data, &_fileinfo);
  395. file->error_id = storage_ext_parse_error(file->internal_error_id);
  396. if(fileinfo != NULL) {
  397. fileinfo->size = _fileinfo.fsize;
  398. fileinfo->flags = 0;
  399. if(_fileinfo.fattrib & AM_DIR) fileinfo->flags |= FSF_DIRECTORY;
  400. }
  401. if(name != NULL) {
  402. snprintf(name, name_length, "%s", _fileinfo.fname);
  403. }
  404. if(_fileinfo.fname[0] == 0) {
  405. file->error_id = FSE_NOT_EXIST;
  406. }
  407. return (file->error_id == FSE_OK);
  408. }
  409. static bool storage_ext_dir_rewind(void* ctx, File* file) {
  410. StorageData* storage = ctx;
  411. SDDir* file_data = storage_get_storage_file_data(file, storage);
  412. file->internal_error_id = f_readdir(file_data, NULL);
  413. file->error_id = storage_ext_parse_error(file->internal_error_id);
  414. return (file->error_id == FSE_OK);
  415. }
  416. /******************* Common FS Functions *******************/
  417. static FS_Error storage_ext_common_stat(void* ctx, const char* path, FileInfo* fileinfo) {
  418. SDFileInfo _fileinfo;
  419. SDError result = f_stat(path, &_fileinfo);
  420. if(fileinfo != NULL) {
  421. fileinfo->size = _fileinfo.fsize;
  422. fileinfo->flags = 0;
  423. if(_fileinfo.fattrib & AM_DIR) fileinfo->flags |= FSF_DIRECTORY;
  424. }
  425. return storage_ext_parse_error(result);
  426. }
  427. static FS_Error storage_ext_common_remove(void* ctx, const char* path) {
  428. #ifdef FURI_RAM_EXEC
  429. return FSE_NOT_READY;
  430. #else
  431. SDError result = f_unlink(path);
  432. return storage_ext_parse_error(result);
  433. #endif
  434. }
  435. static FS_Error storage_ext_common_mkdir(void* ctx, const char* path) {
  436. #ifdef FURI_RAM_EXEC
  437. return FSE_NOT_READY;
  438. #else
  439. SDError result = f_mkdir(path);
  440. return storage_ext_parse_error(result);
  441. #endif
  442. }
  443. static FS_Error storage_ext_common_fs_info(
  444. void* ctx,
  445. const char* fs_path,
  446. uint64_t* total_space,
  447. uint64_t* free_space) {
  448. #ifdef FURI_RAM_EXEC
  449. return FSE_NOT_READY;
  450. #else
  451. StorageData* storage = ctx;
  452. SDData* sd_data = storage->data;
  453. DWORD free_clusters;
  454. FATFS* fs;
  455. SDError fresult = f_getfree(sd_data->path, &free_clusters, &fs);
  456. if((FRESULT)fresult == FR_OK) {
  457. uint32_t total_sectors = (fs->n_fatent - 2) * fs->csize;
  458. uint32_t free_sectors = free_clusters * fs->csize;
  459. uint16_t sector_size = _MAX_SS;
  460. #if _MAX_SS != _MIN_SS
  461. sector_size = fs->ssize;
  462. #endif
  463. if(total_space != NULL) {
  464. *total_space = (uint64_t)total_sectors * (uint64_t)sector_size;
  465. }
  466. if(free_space != NULL) {
  467. *free_space = (uint64_t)free_sectors * (uint64_t)sector_size;
  468. }
  469. }
  470. return storage_ext_parse_error(fresult);
  471. #endif
  472. }
  473. /******************* Init Storage *******************/
  474. static const FS_Api fs_api = {
  475. .file =
  476. {
  477. .open = storage_ext_file_open,
  478. .close = storage_ext_file_close,
  479. .read = storage_ext_file_read,
  480. .write = storage_ext_file_write,
  481. .seek = storage_ext_file_seek,
  482. .tell = storage_ext_file_tell,
  483. .truncate = storage_ext_file_truncate,
  484. .size = storage_ext_file_size,
  485. .sync = storage_ext_file_sync,
  486. .eof = storage_ext_file_eof,
  487. },
  488. .dir =
  489. {
  490. .open = storage_ext_dir_open,
  491. .close = storage_ext_dir_close,
  492. .read = storage_ext_dir_read,
  493. .rewind = storage_ext_dir_rewind,
  494. },
  495. .common =
  496. {
  497. .stat = storage_ext_common_stat,
  498. .mkdir = storage_ext_common_mkdir,
  499. .remove = storage_ext_common_remove,
  500. .fs_info = storage_ext_common_fs_info,
  501. },
  502. };
  503. void storage_ext_init(StorageData* storage) {
  504. SDData* sd_data = malloc(sizeof(SDData));
  505. sd_data->fs = &USERFatFS;
  506. sd_data->path = "0:/";
  507. sd_data->sd_was_present = true;
  508. storage->data = sd_data;
  509. storage->api.tick = storage_ext_tick;
  510. storage->fs_api = &fs_api;
  511. hal_sd_detect_init();
  512. // do not notify on first launch, notifications app is waiting for our thread to read settings
  513. storage_ext_tick_internal(storage, false);
  514. }