dfs_elm.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #include <dfs_fs.h>
  2. #include <dfs_def.h>
  3. #include "ff.h"
  4. #define ELM_MAX_DISK 4
  5. static rt_device_t disk[ELM_MAX_DISK] = {0};
  6. static int elm_result_to_dfs(FRESULT result)
  7. {
  8. int status = DFS_STATUS_OK;
  9. switch (result)
  10. {
  11. case FR_OK:
  12. break;
  13. case FR_NO_FILE:
  14. case FR_NO_PATH:
  15. case FR_NO_FILESYSTEM:
  16. status = -DFS_STATUS_ENOENT;
  17. break;
  18. case FR_INVALID_NAME:
  19. status = -DFS_STATUS_EINVAL;
  20. break;
  21. case FR_EXIST:
  22. case FR_INVALID_OBJECT:
  23. status = -DFS_STATUS_EEXIST;
  24. break;
  25. case FR_DISK_ERR:
  26. case FR_NOT_READY:
  27. case FR_INT_ERR:
  28. status = -DFS_STATUS_EIO;
  29. break;
  30. case FR_WRITE_PROTECTED:
  31. case FR_DENIED:
  32. status = -DFS_STATUS_EROFS;
  33. break;
  34. default:
  35. status = -1;
  36. break;
  37. }
  38. return status;
  39. }
  40. int dfs_elm_mount(struct dfs_filesystem* fs)
  41. {
  42. FATFS *fat;
  43. FRESULT result;
  44. rt_uint32_t index;
  45. /* handle RT-Thread device routine */
  46. for (index = 0; index < ELM_MAX_DISK; index ++)
  47. {
  48. if (disk[index] == RT_NULL)
  49. {
  50. break;
  51. }
  52. }
  53. if (index == ELM_MAX_DISK) return -DFS_STATUS_EMMOUNT;
  54. /* get device */
  55. disk[index] = fs->dev_id;
  56. fat = (FATFS *) rt_malloc(sizeof(FATFS));
  57. if (fat == RT_NULL)
  58. {
  59. return -1;
  60. }
  61. /* mount fatfs, always 0 logic driver */
  62. result = f_mount(index, fat);
  63. if (result == FR_OK)
  64. fs->data = fat;
  65. else
  66. {
  67. rt_free(fat);
  68. return elm_result_to_dfs(result);
  69. }
  70. return 0;
  71. }
  72. int dfs_elm_unmount(struct dfs_filesystem* fs)
  73. {
  74. FATFS *fat;
  75. fat = (FATFS*) fs->data;
  76. RT_ASSERT(fat != RT_NULL);
  77. /* elm not support unmount */
  78. rt_kprintf("elm fatfs not support unmount\n");
  79. return 0;
  80. }
  81. int dfs_elm_open(struct dfs_fd* file)
  82. {
  83. FIL* fd;
  84. BYTE mode;
  85. FRESULT result;
  86. if (file->flags & DFS_O_DIRECTORY)
  87. {
  88. DIR *dir;
  89. if (file->flags & DFS_O_CREAT)
  90. {
  91. result = f_mkdir(file->path);
  92. if (result != FR_OK)
  93. {
  94. return elm_result_to_dfs(result);
  95. }
  96. }
  97. /* open directory */
  98. dir = (DIR *)rt_malloc(sizeof(DIR));
  99. if (dir == RT_NULL)
  100. {
  101. return -DFS_STATUS_ENOMEM;
  102. }
  103. result = f_opendir(dir, file->path);
  104. if (result != FR_OK)
  105. {
  106. rt_free(dir);
  107. return elm_result_to_dfs(result);
  108. }
  109. file->data = dir;
  110. return DFS_STATUS_OK;
  111. }
  112. else
  113. {
  114. mode = FA_READ;
  115. if (file->flags & DFS_O_WRONLY) mode |= FA_WRITE;
  116. /* Opens the file, if it is existing. If not, a new file is created. */
  117. if (file->flags & DFS_O_CREAT) mode |= FA_OPEN_ALWAYS;
  118. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  119. if (file->flags & DFS_O_TRUNC) mode |= FA_CREATE_ALWAYS;
  120. /* Creates a new file. The function fails if the file is already existing. */
  121. if (file->flags & DFS_O_EXCL) mode |= FA_CREATE_NEW;
  122. /* allocate a fd */
  123. fd = (FIL*)rt_malloc(sizeof(FIL));
  124. if (fd == RT_NULL)
  125. {
  126. return -DFS_STATUS_ENOMEM;
  127. }
  128. result = f_open(fd, file->path, mode);
  129. if (result == FR_OK)
  130. {
  131. file->pos = fd->fptr;
  132. file->size = fd->fsize;
  133. file->data = fd;
  134. if (file->flags & DFS_O_APPEND)
  135. {
  136. file->pos = f_lseek(fd, fd->fsize);
  137. }
  138. }
  139. else
  140. {
  141. /* open failed, return */
  142. rt_free(fd);
  143. return elm_result_to_dfs(result);
  144. }
  145. }
  146. return DFS_STATUS_OK;
  147. }
  148. int dfs_elm_close(struct dfs_fd* file)
  149. {
  150. FRESULT result;
  151. result = FR_OK;
  152. if (file->type == FT_DIRECTORY)
  153. {
  154. DIR* dir;
  155. dir = (DIR*)(file->data);
  156. RT_ASSERT(dir != RT_NULL);
  157. /* release memory */
  158. rt_free(dir);
  159. }
  160. else if (file->type == FT_REGULAR)
  161. {
  162. FIL* fd;
  163. fd = (FIL*)(file->data);
  164. RT_ASSERT(fd != RT_NULL);
  165. result = f_close(fd);
  166. if (result == FR_OK)
  167. {
  168. /* release memory */
  169. rt_free(fd);
  170. }
  171. }
  172. return elm_result_to_dfs(result);
  173. }
  174. int dfs_elm_ioctl(struct dfs_fd* file, int cmd, void* args)
  175. {
  176. return -DFS_STATUS_ENOSYS;
  177. }
  178. int dfs_elm_read(struct dfs_fd* file, void* buf, rt_size_t len)
  179. {
  180. FIL* fd;
  181. FRESULT result;
  182. UINT byte_read;
  183. if (file->type == FT_DIRECTORY)
  184. {
  185. return -DFS_STATUS_EISDIR;
  186. }
  187. fd = (FIL*)(file->data);
  188. RT_ASSERT(fd != RT_NULL);
  189. result = f_read(fd, buf, len, &byte_read);
  190. /* update position */
  191. file->pos = fd->fptr;
  192. if (result == FR_OK) return byte_read;
  193. return elm_result_to_dfs(result);
  194. }
  195. int dfs_elm_write(struct dfs_fd* file, const void* buf, rt_size_t len)
  196. {
  197. FIL* fd;
  198. FRESULT result;
  199. UINT byte_write;
  200. if (file->type == FT_DIRECTORY)
  201. {
  202. return -DFS_STATUS_EISDIR;
  203. }
  204. fd = (FIL*)(file->data);
  205. RT_ASSERT(fd != RT_NULL);
  206. result = f_write(fd, buf, len, &byte_write);
  207. /* update position */
  208. file->pos = fd->fptr;
  209. if (result == FR_OK) return byte_write;
  210. return elm_result_to_dfs(result);
  211. }
  212. int dfs_elm_lseek(struct dfs_fd* file, rt_off_t offset)
  213. {
  214. FIL* fd;
  215. FRESULT result;
  216. fd = (FIL*)(file->data);
  217. RT_ASSERT(fd != RT_NULL);
  218. result = f_lseek(fd, offset);
  219. return elm_result_to_dfs(result);
  220. }
  221. int dfs_elm_getdents(struct dfs_fd* file, struct dfs_dirent* dirp, rt_uint32_t count)
  222. {
  223. DIR* dir;
  224. FILINFO fno;
  225. FRESULT result;
  226. rt_uint32_t index;
  227. struct dfs_dirent* d;
  228. dir = (DIR*)(file->data);
  229. RT_ASSERT(dir != RT_NULL);
  230. /* make integer count */
  231. count = (count / sizeof(struct dfs_dirent)) * sizeof(struct dfs_dirent);
  232. if ( count == 0 ) return -DFS_STATUS_EINVAL;
  233. #if _USE_LFN
  234. /* allocate long file name */
  235. fno.lfname = rt_malloc(256);
  236. fno.lfsize = 256;
  237. #endif
  238. index = 0;
  239. while (1)
  240. {
  241. char *fn;
  242. d = dirp + index;
  243. result = f_readdir(dir, &fno);
  244. if (result != FR_OK || fno.fname[0] == 0) break;
  245. #if _USE_LFN
  246. fn = *fno.lfname? fno.lfname : fno.fname;
  247. #else
  248. fn = fno.fname;
  249. #endif
  250. d->d_type = DFS_DT_UNKNOWN;
  251. if (fno.fattrib & AM_DIR) d->d_type &= DFS_DT_DIR;
  252. else d->d_type &= DFS_DT_REG;
  253. d->d_namlen = rt_strlen(fn);
  254. d->d_reclen = (rt_uint16_t)sizeof(struct dfs_dirent);
  255. rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);
  256. index ++;
  257. if ( index * sizeof(struct dfs_dirent) >= count )
  258. break;
  259. }
  260. #if _USE_LFN
  261. rt_free(fno.lfname);
  262. #endif
  263. if (index == 0)
  264. return elm_result_to_dfs(result);
  265. return index * sizeof(struct dfs_dirent);
  266. }
  267. int dfs_elm_unlink(struct dfs_filesystem* fs, const char* path)
  268. {
  269. FRESULT result;
  270. result = f_unlink(path);
  271. return elm_result_to_dfs(result);
  272. }
  273. int dfs_elm_rename(struct dfs_filesystem* fs, const char* oldpath, const char* newpath)
  274. {
  275. FRESULT result;
  276. result = f_rename(oldpath, newpath);
  277. return elm_result_to_dfs(result);
  278. }
  279. int dfs_elm_stat(struct dfs_filesystem* fs, const char *path, struct dfs_stat *st)
  280. {
  281. FILINFO file_info;
  282. FRESULT result;
  283. #if _USE_LFN
  284. /* allocate long file name */
  285. file_info.lfname = rt_malloc(256);
  286. file_info.lfsize = 256;
  287. #endif
  288. result = f_stat(path, &file_info);
  289. if (result == FR_OK)
  290. {
  291. /* convert to dfs stat structure */
  292. st->st_dev = 0;
  293. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  294. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  295. if (file_info.fattrib & AM_DIR)
  296. {
  297. st->st_mode &= ~DFS_S_IFREG;
  298. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  299. }
  300. if (file_info.fattrib & AM_RDO)
  301. st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH);
  302. st->st_size = file_info.fsize;
  303. st->st_mtime = file_info.ftime;
  304. st->st_blksize = 512;
  305. }
  306. #if _USE_LFN
  307. rt_free(file_info.lfname);
  308. #endif
  309. return elm_result_to_dfs(result);
  310. }
  311. static struct dfs_filesystem_operation dfs_elm;
  312. int elm_init(void)
  313. {
  314. rt_strncpy(dfs_elm.name, "elm", DFS_FS_NAME_MAX);
  315. dfs_elm.mount = dfs_elm_mount;
  316. dfs_elm.unmount = dfs_elm_unmount;
  317. dfs_elm.open = dfs_elm_open;
  318. dfs_elm.close = dfs_elm_close;
  319. dfs_elm.ioctl = dfs_elm_ioctl;
  320. dfs_elm.read = dfs_elm_read;
  321. dfs_elm.write = dfs_elm_write;
  322. dfs_elm.lseek = dfs_elm_lseek;
  323. dfs_elm.getdents= dfs_elm_getdents;
  324. dfs_elm.unlink = dfs_elm_unlink;
  325. dfs_elm.stat = dfs_elm_stat;
  326. dfs_elm.rename = dfs_elm_rename;
  327. /* register fatfs file system */
  328. dfs_register(&dfs_elm);
  329. return 0;
  330. }
  331. /*
  332. * RT-Thread Device Interface for ELM FatFs
  333. */
  334. #include "diskio.h"
  335. /* Inidialize a Drive */
  336. DSTATUS disk_initialize (BYTE drv)
  337. {
  338. return 0;
  339. }
  340. /* Return Disk Status */
  341. DSTATUS disk_status (BYTE drv)
  342. {
  343. return 0;
  344. }
  345. /* Read Sector(s) */
  346. DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, BYTE count)
  347. {
  348. rt_size_t result;
  349. rt_device_t device = disk[drv];
  350. result = rt_device_read(device, sector * 512, buff, count * 512);
  351. if (result == count * 512)
  352. {
  353. return RES_OK;
  354. }
  355. return RES_ERROR;
  356. }
  357. /* Write Sector(s) */
  358. DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
  359. {
  360. rt_size_t result;
  361. rt_device_t device = disk[drv];
  362. result = rt_device_write(device, sector * 512, buff, count * 512);
  363. if (result == count * 512)
  364. {
  365. return RES_OK;
  366. }
  367. return RES_ERROR;
  368. }
  369. /* Miscellaneous Functions */
  370. DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff)
  371. {
  372. return RES_OK;
  373. }
  374. rt_time_t get_fattime()
  375. {
  376. return 0;
  377. }
  378. #if _FS_REENTRANT
  379. BOOL ff_cre_syncobj(BYTE drv, _SYNC_t* m)
  380. {
  381. char name[8];
  382. rt_mutex_t mutex;
  383. rt_snprintf(name, sizeof(name), "fat%d", drv);
  384. mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  385. if (mutex != RT_NULL)
  386. {
  387. *m = mutex;
  388. return TRUE;
  389. }
  390. return FALSE;
  391. }
  392. BOOL ff_del_syncobj(_SYNC_t m)
  393. {
  394. rt_mutex_delete(m);
  395. return TRUE;
  396. }
  397. BOOL ff_req_grant(_SYNC_t m)
  398. {
  399. if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK) return TRUE;
  400. return FALSE;
  401. }
  402. void ff_rel_grant(_SYNC_t m)
  403. {
  404. rt_mutex_release(m);
  405. }
  406. #endif