dfs_elm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * File : dfs_elm.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2008-2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE.
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2008-02-22 QiuYi The first version.
  13. * 2011-10-08 Bernard fixed the block size in statfs.
  14. * 2011-11-23 Bernard fixed the rename issue.
  15. * 2012-07-26 aozima implement ff_memalloc and ff_memfree.
  16. * 2012-12-19 Bernard fixed the O_APPEND and lseek issue.
  17. */
  18. #include <rtthread.h>
  19. #include "ffconf.h"
  20. #include "ff.h"
  21. /* ELM FatFs provide a DIR struct */
  22. #define HAVE_DIR_STRUCTURE
  23. #include <dfs_fs.h>
  24. #include <dfs_def.h>
  25. static rt_device_t disk[_VOLUMES] = {0};
  26. static int elm_result_to_dfs(FRESULT result)
  27. {
  28. int status = DFS_STATUS_OK;
  29. switch (result)
  30. {
  31. case FR_OK:
  32. break;
  33. case FR_NO_FILE:
  34. case FR_NO_PATH:
  35. case FR_NO_FILESYSTEM:
  36. status = -DFS_STATUS_ENOENT;
  37. break;
  38. case FR_INVALID_NAME:
  39. status = -DFS_STATUS_EINVAL;
  40. break;
  41. case FR_EXIST:
  42. case FR_INVALID_OBJECT:
  43. status = -DFS_STATUS_EEXIST;
  44. break;
  45. case FR_DISK_ERR:
  46. case FR_NOT_READY:
  47. case FR_INT_ERR:
  48. status = -DFS_STATUS_EIO;
  49. break;
  50. case FR_WRITE_PROTECTED:
  51. case FR_DENIED:
  52. status = -DFS_STATUS_EROFS;
  53. break;
  54. case FR_MKFS_ABORTED:
  55. status = -DFS_STATUS_EINVAL;
  56. break;
  57. default:
  58. status = -1;
  59. break;
  60. }
  61. return status;
  62. }
  63. /* results:
  64. * -1, no space to install fatfs driver
  65. * >= 0, there is an space to install fatfs driver
  66. */
  67. static int get_disk(rt_device_t id)
  68. {
  69. int index;
  70. for (index = 0; index < _VOLUMES; index ++)
  71. {
  72. if (disk[index] == id)
  73. return index;
  74. }
  75. return -1;
  76. }
  77. int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  78. {
  79. FATFS *fat;
  80. FRESULT result;
  81. int index;
  82. /* get an empty position */
  83. index = get_disk(RT_NULL);
  84. if (index == -1)
  85. return -DFS_STATUS_ENOSPC;
  86. /* save device */
  87. disk[index] = fs->dev_id;
  88. fat = (FATFS *)rt_malloc(sizeof(FATFS));
  89. if (fat == RT_NULL)
  90. {
  91. disk[index] = RT_NULL;
  92. return -1;
  93. }
  94. /* mount fatfs, always 0 logic driver */
  95. result = f_mount((BYTE)index, fat);
  96. if (result == FR_OK)
  97. {
  98. char drive[8];
  99. DIR *dir;
  100. rt_snprintf(drive, sizeof(drive), "%d:/", index);
  101. dir = (DIR *)rt_malloc(sizeof(DIR));
  102. if (dir == RT_NULL)
  103. {
  104. f_mount((BYTE)index, RT_NULL);
  105. disk[index] = RT_NULL;
  106. rt_free(fat);
  107. return -DFS_STATUS_ENOMEM;
  108. }
  109. /* open the root directory to test whether the fatfs is valid */
  110. result = f_opendir(dir, drive);
  111. if (result != FR_OK)
  112. goto __err;
  113. /* mount succeed! */
  114. fs->data = fat;
  115. rt_free(dir);
  116. return 0;
  117. }
  118. __err:
  119. f_mount((BYTE)index, RT_NULL);
  120. disk[index] = RT_NULL;
  121. rt_free(fat);
  122. return elm_result_to_dfs(result);
  123. }
  124. int dfs_elm_unmount(struct dfs_filesystem *fs)
  125. {
  126. FATFS *fat;
  127. FRESULT result;
  128. int index;
  129. fat = (FATFS *)fs->data;
  130. RT_ASSERT(fat != RT_NULL);
  131. /* find the device index and then umount it */
  132. index = get_disk(fs->dev_id);
  133. if (index == -1) /* not found */
  134. return -DFS_STATUS_ENOENT;
  135. result = f_mount((BYTE)index, RT_NULL);
  136. if (result != FR_OK)
  137. return elm_result_to_dfs(result);
  138. fs->data = RT_NULL;
  139. disk[index] = RT_NULL;
  140. rt_free(fat);
  141. return DFS_STATUS_OK;
  142. }
  143. int dfs_elm_mkfs(rt_device_t dev_id)
  144. {
  145. #define FSM_STATUS_INIT 0
  146. #define FSM_STATUS_USE_TEMP_DRIVER 1
  147. FATFS *fat;
  148. int flag;
  149. FRESULT result;
  150. int index;
  151. if (dev_id == RT_NULL)
  152. return -DFS_STATUS_EINVAL;
  153. /* if the device is already mounted, then just do mkfs to the drv,
  154. * while if it is not mounted yet, then find an empty drive to do mkfs
  155. */
  156. flag = FSM_STATUS_INIT;
  157. index = get_disk(dev_id);
  158. if (index == -1)
  159. {
  160. /* not found the device id */
  161. index = get_disk(RT_NULL);
  162. if (index == -1)
  163. {
  164. /* no space to store an temp driver */
  165. rt_kprintf("sorry, there is no space to do mkfs! \n");
  166. return -DFS_STATUS_ENOSPC;
  167. }
  168. else
  169. {
  170. fat = rt_malloc(sizeof(FATFS));
  171. if (fat == RT_NULL)
  172. return -DFS_STATUS_ENOMEM;
  173. flag = FSM_STATUS_USE_TEMP_DRIVER;
  174. disk[index] = dev_id;
  175. /* just fill the FatFs[vol] in ff.c, or mkfs will failded!
  176. * consider this condition: you just umount the elm fat,
  177. * then the space in FatFs[index] is released, and now do mkfs
  178. * on the disk, you will get a failure. so we need f_mount here,
  179. * just fill the FatFS[index] in elm fatfs to make mkfs work.
  180. */
  181. f_mount((BYTE)index, fat);
  182. }
  183. }
  184. /* 1: no partition table */
  185. /* 0: auto selection of cluster size */
  186. result = f_mkfs((BYTE)index, 1, 0);
  187. /* check flag status, we need clear the temp driver stored in disk[] */
  188. if (flag == FSM_STATUS_USE_TEMP_DRIVER)
  189. {
  190. rt_free(fat);
  191. f_mount((BYTE)index, RT_NULL);
  192. disk[index] = RT_NULL;
  193. }
  194. if (result != FR_OK)
  195. {
  196. rt_kprintf("format error\n");
  197. return elm_result_to_dfs(result);
  198. }
  199. return DFS_STATUS_OK;
  200. }
  201. int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf)
  202. {
  203. FATFS *f;
  204. FRESULT res;
  205. char driver[4];
  206. DWORD fre_clust, fre_sect, tot_sect;
  207. RT_ASSERT(fs != RT_NULL);
  208. RT_ASSERT(buf != RT_NULL);
  209. f = (FATFS *)fs->data;
  210. rt_snprintf(driver, sizeof(driver), "%d:", f->drv);
  211. res = f_getfree(driver, &fre_clust, &f);
  212. if (res)
  213. return elm_result_to_dfs(res);
  214. /* Get total sectors and free sectors */
  215. tot_sect = (f->n_fatent - 2) * f->csize;
  216. fre_sect = fre_clust * f->csize;
  217. buf->f_bfree = fre_sect;
  218. buf->f_blocks = tot_sect;
  219. #if _MAX_SS != 512
  220. buf->f_bsize = f->ssize;
  221. #else
  222. buf->f_bsize = 512;
  223. #endif
  224. return 0;
  225. }
  226. int dfs_elm_open(struct dfs_fd *file)
  227. {
  228. FIL *fd;
  229. BYTE mode;
  230. FRESULT result;
  231. char *drivers_fn;
  232. #if (_VOLUMES > 1)
  233. int vol;
  234. extern int elm_get_vol(FATFS * fat);
  235. /* add path for ELM FatFS driver support */
  236. vol = elm_get_vol((FATFS *)file->fs->data);
  237. if (vol < 0)
  238. return -DFS_STATUS_ENOENT;
  239. drivers_fn = rt_malloc(256);
  240. if (drivers_fn == RT_NULL)
  241. return -DFS_STATUS_ENOMEM;
  242. rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path);
  243. #else
  244. drivers_fn = file->path;
  245. #endif
  246. if (file->flags & DFS_O_DIRECTORY)
  247. {
  248. DIR *dir;
  249. if (file->flags & DFS_O_CREAT)
  250. {
  251. result = f_mkdir(drivers_fn);
  252. if (result != FR_OK)
  253. {
  254. #if _VOLUMES > 1
  255. rt_free(drivers_fn);
  256. #endif
  257. return elm_result_to_dfs(result);
  258. }
  259. }
  260. /* open directory */
  261. dir = (DIR *)rt_malloc(sizeof(DIR));
  262. if (dir == RT_NULL)
  263. {
  264. #if _VOLUMES > 1
  265. rt_free(drivers_fn);
  266. #endif
  267. return -DFS_STATUS_ENOMEM;
  268. }
  269. result = f_opendir(dir, drivers_fn);
  270. #if _VOLUMES > 1
  271. rt_free(drivers_fn);
  272. #endif
  273. if (result != FR_OK)
  274. {
  275. rt_free(dir);
  276. return elm_result_to_dfs(result);
  277. }
  278. file->data = dir;
  279. return DFS_STATUS_OK;
  280. }
  281. else
  282. {
  283. mode = FA_READ;
  284. if (file->flags & DFS_O_WRONLY)
  285. mode |= FA_WRITE;
  286. if ((file->flags & DFS_O_ACCMODE) & DFS_O_RDWR)
  287. mode |= FA_WRITE;
  288. /* Opens the file, if it is existing. If not, a new file is created. */
  289. if (file->flags & DFS_O_CREAT)
  290. mode |= FA_OPEN_ALWAYS;
  291. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  292. if (file->flags & DFS_O_TRUNC)
  293. mode |= FA_CREATE_ALWAYS;
  294. /* Creates a new file. The function fails if the file is already existing. */
  295. if (file->flags & DFS_O_EXCL)
  296. mode |= FA_CREATE_NEW;
  297. /* allocate a fd */
  298. fd = (FIL *)rt_malloc(sizeof(FIL));
  299. if (fd == RT_NULL)
  300. {
  301. #if _VOLUMES > 1
  302. rt_free(drivers_fn);
  303. #endif
  304. return -DFS_STATUS_ENOMEM;
  305. }
  306. result = f_open(fd, drivers_fn, mode);
  307. #if _VOLUMES > 1
  308. rt_free(drivers_fn);
  309. #endif
  310. if (result == FR_OK)
  311. {
  312. file->pos = fd->fptr;
  313. file->size = fd->fsize;
  314. file->data = fd;
  315. if (file->flags & DFS_O_APPEND)
  316. {
  317. /* seek to the end of file */
  318. f_lseek(fd, fd->fsize);
  319. file->pos = fd->fptr;
  320. }
  321. }
  322. else
  323. {
  324. /* open failed, return */
  325. rt_free(fd);
  326. return elm_result_to_dfs(result);
  327. }
  328. }
  329. return DFS_STATUS_OK;
  330. }
  331. int dfs_elm_close(struct dfs_fd *file)
  332. {
  333. FRESULT result;
  334. result = FR_OK;
  335. if (file->type == FT_DIRECTORY)
  336. {
  337. DIR *dir;
  338. dir = (DIR *)(file->data);
  339. RT_ASSERT(dir != RT_NULL);
  340. /* release memory */
  341. rt_free(dir);
  342. }
  343. else if (file->type == FT_REGULAR)
  344. {
  345. FIL *fd;
  346. fd = (FIL *)(file->data);
  347. RT_ASSERT(fd != RT_NULL);
  348. result = f_close(fd);
  349. if (result == FR_OK)
  350. {
  351. /* release memory */
  352. rt_free(fd);
  353. }
  354. }
  355. return elm_result_to_dfs(result);
  356. }
  357. int dfs_elm_ioctl(struct dfs_fd *file, int cmd, void *args)
  358. {
  359. return -DFS_STATUS_ENOSYS;
  360. }
  361. int dfs_elm_read(struct dfs_fd *file, void *buf, rt_size_t len)
  362. {
  363. FIL *fd;
  364. FRESULT result;
  365. UINT byte_read;
  366. if (file->type == FT_DIRECTORY)
  367. {
  368. return -DFS_STATUS_EISDIR;
  369. }
  370. fd = (FIL *)(file->data);
  371. RT_ASSERT(fd != RT_NULL);
  372. result = f_read(fd, buf, len, &byte_read);
  373. /* update position */
  374. file->pos = fd->fptr;
  375. if (result == FR_OK)
  376. return byte_read;
  377. return elm_result_to_dfs(result);
  378. }
  379. int dfs_elm_write(struct dfs_fd *file, const void *buf, rt_size_t len)
  380. {
  381. FIL *fd;
  382. FRESULT result;
  383. UINT byte_write;
  384. if (file->type == FT_DIRECTORY)
  385. {
  386. return -DFS_STATUS_EISDIR;
  387. }
  388. fd = (FIL *)(file->data);
  389. RT_ASSERT(fd != RT_NULL);
  390. result = f_write(fd, buf, len, &byte_write);
  391. /* update position and file size */
  392. file->pos = fd->fptr;
  393. file->size = fd->fsize;
  394. if (result == FR_OK)
  395. return byte_write;
  396. return elm_result_to_dfs(result);
  397. }
  398. int dfs_elm_flush(struct dfs_fd *file)
  399. {
  400. FIL *fd;
  401. FRESULT result;
  402. fd = (FIL *)(file->data);
  403. RT_ASSERT(fd != RT_NULL);
  404. result = f_sync(fd);
  405. return elm_result_to_dfs(result);
  406. }
  407. int dfs_elm_lseek(struct dfs_fd *file, rt_off_t offset)
  408. {
  409. FRESULT result = FR_OK;
  410. if (file->type == FT_REGULAR)
  411. {
  412. FIL *fd;
  413. /* regular file type */
  414. fd = (FIL *)(file->data);
  415. RT_ASSERT(fd != RT_NULL);
  416. result = f_lseek(fd, offset);
  417. if (result == FR_OK)
  418. {
  419. /* return current position */
  420. file->pos = fd->fptr;
  421. return fd->fptr;
  422. }
  423. }
  424. else if (file->type == FT_DIRECTORY)
  425. {
  426. /* which is a directory */
  427. DIR *dir;
  428. dir = (DIR *)(file->data);
  429. RT_ASSERT(dir != RT_NULL);
  430. result = f_seekdir(dir, offset / sizeof(struct dirent));
  431. if (result == FR_OK)
  432. {
  433. /* update file position */
  434. file->pos = offset;
  435. return file->pos;
  436. }
  437. }
  438. return elm_result_to_dfs(result);
  439. }
  440. int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  441. {
  442. DIR *dir;
  443. FILINFO fno;
  444. FRESULT result;
  445. rt_uint32_t index;
  446. struct dirent *d;
  447. dir = (DIR *)(file->data);
  448. RT_ASSERT(dir != RT_NULL);
  449. /* make integer count */
  450. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  451. if (count == 0)
  452. return -DFS_STATUS_EINVAL;
  453. #if _USE_LFN
  454. /* allocate long file name */
  455. fno.lfname = rt_malloc(256);
  456. fno.lfsize = 256;
  457. #endif
  458. index = 0;
  459. while (1)
  460. {
  461. char *fn;
  462. d = dirp + index;
  463. result = f_readdir(dir, &fno);
  464. if (result != FR_OK || fno.fname[0] == 0)
  465. break;
  466. #if _USE_LFN
  467. fn = *fno.lfname ? fno.lfname : fno.fname;
  468. #else
  469. fn = fno.fname;
  470. #endif
  471. d->d_type = DFS_DT_UNKNOWN;
  472. if (fno.fattrib & AM_DIR)
  473. d->d_type = DFS_DT_DIR;
  474. else
  475. d->d_type = DFS_DT_REG;
  476. d->d_namlen = (rt_uint8_t)rt_strlen(fn);
  477. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  478. rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);
  479. index ++;
  480. if (index * sizeof(struct dirent) >= count)
  481. break;
  482. }
  483. #if _USE_LFN
  484. rt_free(fno.lfname);
  485. #endif
  486. if (index == 0)
  487. return elm_result_to_dfs(result);
  488. file->pos += index * sizeof(struct dirent);
  489. return index * sizeof(struct dirent);
  490. }
  491. int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path)
  492. {
  493. FRESULT result;
  494. #if _VOLUMES > 1
  495. int vol;
  496. char *drivers_fn;
  497. extern int elm_get_vol(FATFS * fat);
  498. /* add path for ELM FatFS driver support */
  499. vol = elm_get_vol((FATFS *)fs->data);
  500. if (vol < 0)
  501. return -DFS_STATUS_ENOENT;
  502. drivers_fn = rt_malloc(256);
  503. if (drivers_fn == RT_NULL)
  504. return -DFS_STATUS_ENOMEM;
  505. rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
  506. #else
  507. const char *drivers_fn;
  508. drivers_fn = path;
  509. #endif
  510. result = f_unlink(drivers_fn);
  511. #if _VOLUMES > 1
  512. rt_free(drivers_fn);
  513. #endif
  514. return elm_result_to_dfs(result);
  515. }
  516. int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath)
  517. {
  518. FRESULT result;
  519. #if _VOLUMES > 1
  520. char *drivers_oldfn;
  521. const char *drivers_newfn;
  522. int vol;
  523. extern int elm_get_vol(FATFS * fat);
  524. /* add path for ELM FatFS driver support */
  525. vol = elm_get_vol((FATFS *)fs->data);
  526. if (vol < 0)
  527. return -DFS_STATUS_ENOENT;
  528. drivers_oldfn = rt_malloc(256);
  529. if (drivers_oldfn == RT_NULL)
  530. return -DFS_STATUS_ENOMEM;
  531. drivers_newfn = newpath;
  532. rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, oldpath);
  533. #else
  534. const char *drivers_oldfn, *drivers_newfn;
  535. drivers_oldfn = oldpath;
  536. drivers_newfn = newpath;
  537. #endif
  538. result = f_rename(drivers_oldfn, drivers_newfn);
  539. #if _VOLUMES > 1
  540. rt_free(drivers_oldfn);
  541. #endif
  542. return elm_result_to_dfs(result);
  543. }
  544. int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  545. {
  546. FILINFO file_info;
  547. FRESULT result;
  548. #if _VOLUMES > 1
  549. int vol;
  550. char *drivers_fn;
  551. extern int elm_get_vol(FATFS * fat);
  552. /* add path for ELM FatFS driver support */
  553. vol = elm_get_vol((FATFS *)fs->data);
  554. if (vol < 0)
  555. return -DFS_STATUS_ENOENT;
  556. drivers_fn = rt_malloc(256);
  557. if (drivers_fn == RT_NULL)
  558. return -DFS_STATUS_ENOMEM;
  559. rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
  560. #else
  561. const char *drivers_fn;
  562. drivers_fn = path;
  563. #endif
  564. #if _USE_LFN
  565. /* allocate long file name */
  566. file_info.lfname = rt_malloc(256);
  567. file_info.lfsize = 256;
  568. #endif
  569. result = f_stat(drivers_fn, &file_info);
  570. #if _VOLUMES > 1
  571. rt_free(drivers_fn);
  572. #endif
  573. if (result == FR_OK)
  574. {
  575. /* convert to dfs stat structure */
  576. st->st_dev = 0;
  577. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  578. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  579. if (file_info.fattrib & AM_DIR)
  580. {
  581. st->st_mode &= ~DFS_S_IFREG;
  582. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  583. }
  584. if (file_info.fattrib & AM_RDO)
  585. st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH);
  586. st->st_size = file_info.fsize;
  587. st->st_mtime = file_info.ftime;
  588. st->st_blksize = 512;
  589. }
  590. #if _USE_LFN
  591. rt_free(file_info.lfname);
  592. #endif
  593. return elm_result_to_dfs(result);
  594. }
  595. static const struct dfs_filesystem_operation dfs_elm =
  596. {
  597. "elm",
  598. DFS_FS_FLAG_DEFAULT,
  599. dfs_elm_mount,
  600. dfs_elm_unmount,
  601. dfs_elm_mkfs,
  602. dfs_elm_statfs,
  603. dfs_elm_open,
  604. dfs_elm_close,
  605. dfs_elm_ioctl,
  606. dfs_elm_read,
  607. dfs_elm_write,
  608. dfs_elm_flush,
  609. dfs_elm_lseek,
  610. dfs_elm_getdents,
  611. dfs_elm_unlink,
  612. dfs_elm_stat,
  613. dfs_elm_rename,
  614. };
  615. int elm_init(void)
  616. {
  617. /* register fatfs file system */
  618. dfs_register(&dfs_elm);
  619. return 0;
  620. }
  621. /*
  622. * RT-Thread Device Interface for ELM FatFs
  623. */
  624. #include "diskio.h"
  625. /* Initialize a Drive */
  626. DSTATUS disk_initialize(BYTE drv)
  627. {
  628. return 0;
  629. }
  630. /* Return Disk Status */
  631. DSTATUS disk_status(BYTE drv)
  632. {
  633. return 0;
  634. }
  635. /* Read Sector(s) */
  636. DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
  637. {
  638. rt_size_t result;
  639. rt_device_t device = disk[drv];
  640. result = rt_device_read(device, sector, buff, count);
  641. if (result == count)
  642. {
  643. return RES_OK;
  644. }
  645. return RES_ERROR;
  646. }
  647. /* Write Sector(s) */
  648. DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
  649. {
  650. rt_size_t result;
  651. rt_device_t device = disk[drv];
  652. result = rt_device_write(device, sector, buff, count);
  653. if (result == count)
  654. {
  655. return RES_OK;
  656. }
  657. return RES_ERROR;
  658. }
  659. /* Miscellaneous Functions */
  660. DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
  661. {
  662. rt_device_t device = disk[drv];
  663. if (device == RT_NULL)
  664. return RES_ERROR;
  665. if (ctrl == GET_SECTOR_COUNT)
  666. {
  667. struct rt_device_blk_geometry geometry;
  668. rt_memset(&geometry, 0, sizeof(geometry));
  669. rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  670. *(DWORD *)buff = geometry.sector_count;
  671. if (geometry.sector_count == 0)
  672. return RES_ERROR;
  673. }
  674. else if (ctrl == GET_SECTOR_SIZE)
  675. {
  676. struct rt_device_blk_geometry geometry;
  677. rt_memset(&geometry, 0, sizeof(geometry));
  678. rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  679. *(WORD *)buff = (WORD)(geometry.bytes_per_sector);
  680. }
  681. else if (ctrl == GET_BLOCK_SIZE) /* Get erase block size in unit of sectors (DWORD) */
  682. {
  683. struct rt_device_blk_geometry geometry;
  684. rt_memset(&geometry, 0, sizeof(geometry));
  685. rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  686. *(DWORD *)buff = geometry.block_size / geometry.bytes_per_sector;
  687. }
  688. else if (ctrl == CTRL_SYNC)
  689. {
  690. rt_device_control(device, RT_DEVICE_CTRL_BLK_SYNC, RT_NULL);
  691. }
  692. else if (ctrl == CTRL_ERASE_SECTOR)
  693. {
  694. rt_device_control(device, RT_DEVICE_CTRL_BLK_ERASE, buff);
  695. }
  696. return RES_OK;
  697. }
  698. rt_time_t get_fattime(void)
  699. {
  700. return 0;
  701. }
  702. #if _FS_REENTRANT
  703. int ff_cre_syncobj(BYTE drv, _SYNC_t *m)
  704. {
  705. char name[8];
  706. rt_mutex_t mutex;
  707. rt_snprintf(name, sizeof(name), "fat%d", drv);
  708. mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  709. if (mutex != RT_NULL)
  710. {
  711. *m = mutex;
  712. return RT_TRUE;
  713. }
  714. return RT_FALSE;
  715. }
  716. int ff_del_syncobj(_SYNC_t m)
  717. {
  718. if (m != RT_NULL)
  719. rt_mutex_delete(m);
  720. return RT_TRUE;
  721. }
  722. int ff_req_grant(_SYNC_t m)
  723. {
  724. if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK)
  725. return RT_TRUE;
  726. return RT_FALSE;
  727. }
  728. void ff_rel_grant(_SYNC_t m)
  729. {
  730. rt_mutex_release(m);
  731. }
  732. #endif
  733. /* Memory functions */
  734. #if _USE_LFN == 3
  735. /* Allocate memory block */
  736. void *ff_memalloc(UINT size)
  737. {
  738. return rt_malloc(size);
  739. }
  740. /* Free memory block */
  741. void ff_memfree(void *mem)
  742. {
  743. rt_free(mem);
  744. }
  745. #endif /* _USE_LFN == 3 */