dfs_elm.c 20 KB

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