storage-ext.c 17 KB

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