storage_ext.c 17 KB

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