dfs_elm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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_CREAT) mode |= FA_CREATE_NEW;
  116. if (file->flags & DFS_O_WRONLY) mode |= FA_WRITE;
  117. /* allocate a fd */
  118. fd = (FIL*)rt_malloc(sizeof(FIL));
  119. if (fd == RT_NULL)
  120. {
  121. return -DFS_STATUS_ENOMEM;
  122. }
  123. result = f_open(fd, file->path, mode);
  124. if (result == FR_OK)
  125. {
  126. file->pos = fd->fptr;
  127. file->size = fd->fsize;
  128. file->data = fd;
  129. }
  130. else
  131. {
  132. /* open failed, return */
  133. rt_free(fd);
  134. return elm_result_to_dfs(result);
  135. }
  136. }
  137. return DFS_STATUS_OK;
  138. }
  139. int dfs_elm_close(struct dfs_fd* file)
  140. {
  141. FRESULT result;
  142. result = FR_OK;
  143. if (file->type == FT_DIRECTORY)
  144. {
  145. DIR* dir;
  146. dir = (DIR*)(file->data);
  147. RT_ASSERT(dir != RT_NULL);
  148. /* release memory */
  149. rt_free(dir);
  150. }
  151. else if (file->type == FT_REGULAR)
  152. {
  153. FIL* fd;
  154. fd = (FIL*)(file->data);
  155. RT_ASSERT(fd != RT_NULL);
  156. result = f_close(fd);
  157. if (result == FR_OK)
  158. {
  159. /* release memory */
  160. rt_free(fd);
  161. }
  162. }
  163. return elm_result_to_dfs(result);
  164. }
  165. int dfs_elm_ioctl(struct dfs_fd* file, int cmd, void* args)
  166. {
  167. return -DFS_STATUS_ENOSYS;
  168. }
  169. int dfs_elm_read(struct dfs_fd* file, void* buf, rt_size_t len)
  170. {
  171. FIL* fd;
  172. FRESULT result;
  173. UINT byte_read;
  174. if (file->type == FT_DIRECTORY)
  175. {
  176. return -DFS_STATUS_EISDIR;
  177. }
  178. fd = (FIL*)(file->data);
  179. RT_ASSERT(fd != RT_NULL);
  180. result = f_read(fd, buf, len, &byte_read);
  181. /* update position */
  182. file->pos = fd->fptr;
  183. if (result == FR_OK) return byte_read;
  184. return elm_result_to_dfs(result);
  185. }
  186. int dfs_elm_write(struct dfs_fd* file, const void* buf, rt_size_t len)
  187. {
  188. FIL* fd;
  189. FRESULT result;
  190. UINT byte_write;
  191. if (file->type == FT_DIRECTORY)
  192. {
  193. return -DFS_STATUS_EISDIR;
  194. }
  195. fd = (FIL*)(file->data);
  196. RT_ASSERT(fd != RT_NULL);
  197. result = f_write(fd, buf, len, &byte_write);
  198. /* update position */
  199. file->pos = fd->fptr;
  200. if (result == FR_OK) return byte_write;
  201. return elm_result_to_dfs(result);
  202. }
  203. int dfs_elm_lseek(struct dfs_fd* file, rt_off_t offset)
  204. {
  205. FIL* fd;
  206. FRESULT result;
  207. fd = (FIL*)(file->data);
  208. RT_ASSERT(fd != RT_NULL);
  209. result = f_lseek(fd, offset);
  210. return elm_result_to_dfs(result);
  211. }
  212. int dfs_elm_getdents(struct dfs_fd* file, struct dfs_dirent* dirp, rt_uint32_t count)
  213. {
  214. DIR* dir;
  215. FILINFO fno;
  216. FRESULT result;
  217. rt_uint32_t index;
  218. struct dfs_dirent* d;
  219. dir = (DIR*)(file->data);
  220. RT_ASSERT(dir != RT_NULL);
  221. /* make integer count */
  222. count = (count / sizeof(struct dfs_dirent)) * sizeof(struct dfs_dirent);
  223. if ( count == 0 ) return -DFS_STATUS_EINVAL;
  224. #if _USE_LFN
  225. /* allocate long file name */
  226. fno.lfname = rt_malloc(256);
  227. fno.lfsize = 256;
  228. #endif
  229. index = 0;
  230. while (1)
  231. {
  232. char *fn;
  233. d = dirp + index;
  234. result = f_readdir(dir, &fno);
  235. if (result != FR_OK || fno.fname[0] == 0) break;
  236. #if _USE_LFN
  237. fn = *fno.lfname? fno.lfname : fno.fname;
  238. #else
  239. fn = fno.fname;
  240. #endif
  241. d->d_type = DFS_DT_UNKNOWN;
  242. if (fno.fattrib & AM_DIR) d->d_type &= DFS_DT_DIR;
  243. else d->d_type &= DFS_DT_REG;
  244. d->d_namlen = rt_strlen(fn);
  245. d->d_reclen = (rt_uint16_t)sizeof(struct dfs_dirent);
  246. rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);
  247. index ++;
  248. if ( index * sizeof(struct dfs_dirent) >= count )
  249. break;
  250. }
  251. #if _USE_LFN
  252. rt_free(fno.lfname);
  253. #endif
  254. if (index == 0)
  255. return elm_result_to_dfs(result);
  256. return index * sizeof(struct dfs_dirent);
  257. }
  258. int dfs_elm_unlink(struct dfs_filesystem* fs, const char* path)
  259. {
  260. FRESULT result;
  261. result = f_unlink(path);
  262. return elm_result_to_dfs(result);
  263. }
  264. int dfs_elm_rename(struct dfs_filesystem* fs, const char* oldpath, const char* newpath)
  265. {
  266. FRESULT result;
  267. result = f_rename(oldpath, newpath);
  268. return elm_result_to_dfs(result);
  269. }
  270. int dfs_elm_stat(struct dfs_filesystem* fs, const char *path, struct dfs_stat *st)
  271. {
  272. FILINFO file_info;
  273. FRESULT result;
  274. #if _USE_LFN
  275. /* allocate long file name */
  276. file_info.lfname = rt_malloc(256);
  277. file_info.lfsize = 256;
  278. #endif
  279. result = f_stat(path, &file_info);
  280. if (result == FR_OK)
  281. {
  282. /* convert to dfs stat structure */
  283. st->st_dev = 0;
  284. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  285. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  286. if (file_info.fattrib & AM_DIR)
  287. {
  288. st->st_mode &= ~DFS_S_IFREG;
  289. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  290. }
  291. if (file_info.fattrib & AM_RDO)
  292. st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH);
  293. st->st_size = file_info.fsize;
  294. st->st_mtime = file_info.ftime;
  295. st->st_blksize = 512;
  296. }
  297. #if _USE_LFN
  298. rt_free(file_info.lfname);
  299. #endif
  300. return elm_result_to_dfs(result);
  301. }
  302. static struct dfs_filesystem_operation dfs_elm;
  303. int elm_init(void)
  304. {
  305. rt_strncpy(dfs_elm.name, "elm", DFS_FS_NAME_MAX);
  306. dfs_elm.mount = dfs_elm_mount;
  307. dfs_elm.unmount = dfs_elm_unmount;
  308. dfs_elm.open = dfs_elm_open;
  309. dfs_elm.close = dfs_elm_close;
  310. dfs_elm.ioctl = dfs_elm_ioctl;
  311. dfs_elm.read = dfs_elm_read;
  312. dfs_elm.write = dfs_elm_write;
  313. dfs_elm.lseek = dfs_elm_lseek;
  314. dfs_elm.getdents= dfs_elm_getdents;
  315. dfs_elm.unlink = dfs_elm_unlink;
  316. dfs_elm.stat = dfs_elm_stat;
  317. dfs_elm.rename = dfs_elm_rename;
  318. /* register fatfs file system */
  319. dfs_register(&dfs_elm);
  320. return 0;
  321. }
  322. /*
  323. * RT-Thread Device Interface for ELM FatFs
  324. */
  325. #include "diskio.h"
  326. /* Inidialize a Drive */
  327. DSTATUS disk_initialize (BYTE drv)
  328. {
  329. return 0;
  330. }
  331. /* Return Disk Status */
  332. DSTATUS disk_status (BYTE drv)
  333. {
  334. return 0;
  335. }
  336. /* Read Sector(s) */
  337. DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, BYTE count)
  338. {
  339. rt_size_t result;
  340. rt_device_t device = disk[drv];
  341. result = rt_device_read(device, sector * 512, buff, count * 512);
  342. if (result == count * 512)
  343. {
  344. return RES_OK;
  345. }
  346. return RES_ERROR;
  347. }
  348. /* Write Sector(s) */
  349. DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
  350. {
  351. rt_size_t result;
  352. rt_device_t device = disk[drv];
  353. result = rt_device_write(device, sector * 512, buff, count * 512);
  354. if (result == count * 512)
  355. {
  356. return RES_OK;
  357. }
  358. return RES_ERROR;
  359. }
  360. /* Miscellaneous Functions */
  361. DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff)
  362. {
  363. return RES_OK;
  364. }
  365. rt_time_t get_fattime()
  366. {
  367. return 0;
  368. }