dfs_elm.c 22 KB

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