dfs_elm.c 8.5 KB

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