storage_ext.c 17 KB

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