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. */
  31. #include <rtthread.h>
  32. #include "ffconf.h"
  33. #include "ff.h"
  34. #include <string.h>
  35. #include <time.h>
  36. /* ELM FatFs provide a DIR struct */
  37. #define HAVE_DIR_STRUCTURE
  38. #include <dfs_fs.h>
  39. #include <dfs_def.h>
  40. static rt_device_t disk[_VOLUMES] = {0};
  41. static int elm_result_to_dfs(FRESULT result)
  42. {
  43. int status = DFS_STATUS_OK;
  44. switch (result)
  45. {
  46. case FR_OK:
  47. break;
  48. case FR_NO_FILE:
  49. case FR_NO_PATH:
  50. case FR_NO_FILESYSTEM:
  51. status = -DFS_STATUS_ENOENT;
  52. break;
  53. case FR_INVALID_NAME:
  54. status = -DFS_STATUS_EINVAL;
  55. break;
  56. case FR_EXIST:
  57. case FR_INVALID_OBJECT:
  58. status = -DFS_STATUS_EEXIST;
  59. break;
  60. case FR_DISK_ERR:
  61. case FR_NOT_READY:
  62. case FR_INT_ERR:
  63. status = -DFS_STATUS_EIO;
  64. break;
  65. case FR_WRITE_PROTECTED:
  66. case FR_DENIED:
  67. status = -DFS_STATUS_EROFS;
  68. break;
  69. case FR_MKFS_ABORTED:
  70. status = -DFS_STATUS_EINVAL;
  71. break;
  72. default:
  73. status = -1;
  74. break;
  75. }
  76. return status;
  77. }
  78. /* results:
  79. * -1, no space to install fatfs driver
  80. * >= 0, there is an space to install fatfs driver
  81. */
  82. static int get_disk(rt_device_t id)
  83. {
  84. int index;
  85. for (index = 0; index < _VOLUMES; index ++)
  86. {
  87. if (disk[index] == id)
  88. return index;
  89. }
  90. return -1;
  91. }
  92. int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  93. {
  94. FATFS *fat;
  95. FRESULT result;
  96. int index;
  97. struct rt_device_blk_geometry geometry;
  98. /* get an empty position */
  99. index = get_disk(RT_NULL);
  100. if (index == -1)
  101. return -DFS_STATUS_ENOENT;
  102. /* save device */
  103. disk[index] = fs->dev_id;
  104. /* check sector size */
  105. if (rt_device_control(fs->dev_id, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry) == RT_EOK)
  106. {
  107. if (geometry.bytes_per_sector > _MAX_SS)
  108. {
  109. rt_kprintf("The sector size of device is greater than the sector size of FAT.\n");
  110. return -DFS_STATUS_EINVAL;
  111. }
  112. }
  113. fat = (FATFS *)rt_malloc(sizeof(FATFS));
  114. if (fat == RT_NULL)
  115. {
  116. disk[index] = RT_NULL;
  117. return -DFS_STATUS_ENOMEM;
  118. }
  119. /* mount fatfs, always 0 logic driver */
  120. result = f_mount(fat,"", (BYTE)index);
  121. if (result == FR_OK)
  122. {
  123. char drive[8];
  124. DIR *dir;
  125. rt_snprintf(drive, sizeof(drive), "%d:/", index);
  126. dir = (DIR *)rt_malloc(sizeof(DIR));
  127. if (dir == RT_NULL)
  128. {
  129. f_mount(RT_NULL,"",(BYTE)index);
  130. disk[index] = RT_NULL;
  131. rt_free(fat);
  132. return -DFS_STATUS_ENOMEM;
  133. }
  134. /* open the root directory to test whether the fatfs is valid */
  135. result = f_opendir(dir, drive);
  136. if (result != FR_OK)
  137. goto __err;
  138. /* mount succeed! */
  139. fs->data = fat;
  140. rt_free(dir);
  141. return 0;
  142. }
  143. __err:
  144. f_mount(RT_NULL, "", (BYTE)index);
  145. disk[index] = RT_NULL;
  146. rt_free(fat);
  147. return elm_result_to_dfs(result);
  148. }
  149. int dfs_elm_unmount(struct dfs_filesystem *fs)
  150. {
  151. FATFS *fat;
  152. FRESULT result;
  153. int index;
  154. fat = (FATFS *)fs->data;
  155. RT_ASSERT(fat != RT_NULL);
  156. /* find the device index and then umount it */
  157. index = get_disk(fs->dev_id);
  158. if (index == -1) /* not found */
  159. return -DFS_STATUS_ENOENT;
  160. result = f_mount(RT_NULL, "", (BYTE)index);
  161. if (result != FR_OK)
  162. return elm_result_to_dfs(result);
  163. fs->data = RT_NULL;
  164. disk[index] = RT_NULL;
  165. rt_free(fat);
  166. return DFS_STATUS_OK;
  167. }
  168. int dfs_elm_mkfs(rt_device_t dev_id)
  169. {
  170. #define FSM_STATUS_INIT 0
  171. #define FSM_STATUS_USE_TEMP_DRIVER 1
  172. FATFS *fat = RT_NULL;
  173. BYTE *work;
  174. int flag;
  175. FRESULT result;
  176. int index;
  177. work = rt_malloc(_MAX_SS);
  178. if(RT_NULL == work) {
  179. return -DFS_STATUS_ENOMEM;
  180. }
  181. if (dev_id == RT_NULL)
  182. return -DFS_STATUS_EINVAL;
  183. /* if the device is already mounted, then just do mkfs to the drv,
  184. * while if it is not mounted yet, then find an empty drive to do mkfs
  185. */
  186. flag = FSM_STATUS_INIT;
  187. index = get_disk(dev_id);
  188. if (index == -1)
  189. {
  190. /* not found the device id */
  191. index = get_disk(RT_NULL);
  192. if (index == -1)
  193. {
  194. /* no space to store an temp driver */
  195. rt_kprintf("sorry, there is no space to do mkfs! \n");
  196. return -DFS_STATUS_ENOSPC;
  197. }
  198. else
  199. {
  200. fat = rt_malloc(sizeof(FATFS));
  201. if (fat == RT_NULL)
  202. return -DFS_STATUS_ENOMEM;
  203. flag = FSM_STATUS_USE_TEMP_DRIVER;
  204. disk[index] = dev_id;
  205. /* try to open device */
  206. rt_device_open(dev_id, RT_DEVICE_OFLAG_RDWR);
  207. /* just fill the FatFs[vol] in ff.c, or mkfs will failded!
  208. * consider this condition: you just umount the elm fat,
  209. * then the space in FatFs[index] is released, and now do mkfs
  210. * on the disk, you will get a failure. so we need f_mount here,
  211. * just fill the FatFS[index] in elm fatfs to make mkfs work.
  212. */
  213. f_mount(fat, "", (BYTE)index);
  214. }
  215. }
  216. /* [IN] Logical drive number */
  217. /* [IN] Format options */
  218. /* [IN] Size of the allocation unit */
  219. /* [-] Working buffer */
  220. /* [IN] Size of working buffer */
  221. result = f_mkfs("", FM_ANY, 0, work, _MAX_SS);
  222. rt_free(work);
  223. /* check flag status, we need clear the temp driver stored in disk[] */
  224. if (flag == FSM_STATUS_USE_TEMP_DRIVER)
  225. {
  226. rt_free(fat);
  227. f_mount(RT_NULL, "",(BYTE)index);
  228. disk[index] = RT_NULL;
  229. /* close device */
  230. rt_device_close(dev_id);
  231. }
  232. if (result != FR_OK)
  233. {
  234. rt_kprintf("format error\n");
  235. return elm_result_to_dfs(result);
  236. }
  237. return DFS_STATUS_OK;
  238. }
  239. int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf)
  240. {
  241. FATFS *f;
  242. FRESULT res;
  243. char driver[4];
  244. DWORD fre_clust, fre_sect, tot_sect;
  245. RT_ASSERT(fs != RT_NULL);
  246. RT_ASSERT(buf != RT_NULL);
  247. f = (FATFS *)fs->data;
  248. rt_snprintf(driver, sizeof(driver), "%d:", f->drv);
  249. res = f_getfree(driver, &fre_clust, &f);
  250. if (res)
  251. return elm_result_to_dfs(res);
  252. /* Get total sectors and free sectors */
  253. tot_sect = (f->n_fatent - 2) * f->csize;
  254. fre_sect = fre_clust * f->csize;
  255. buf->f_bfree = fre_sect;
  256. buf->f_blocks = tot_sect;
  257. #if _MAX_SS != 512
  258. buf->f_bsize = f->ssize;
  259. #else
  260. buf->f_bsize = 512;
  261. #endif
  262. return 0;
  263. }
  264. int dfs_elm_open(struct dfs_fd *file)
  265. {
  266. FIL *fd;
  267. BYTE mode;
  268. FRESULT result;
  269. char *drivers_fn;
  270. #if (_VOLUMES > 1)
  271. int vol;
  272. extern int elm_get_vol(FATFS * fat);
  273. /* add path for ELM FatFS driver support */
  274. vol = elm_get_vol((FATFS *)file->fs->data);
  275. if (vol < 0)
  276. return -DFS_STATUS_ENOENT;
  277. drivers_fn = rt_malloc(256);
  278. if (drivers_fn == RT_NULL)
  279. return -DFS_STATUS_ENOMEM;
  280. rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path);
  281. #else
  282. drivers_fn = file->path;
  283. #endif
  284. if (file->flags & DFS_O_DIRECTORY)
  285. {
  286. DIR *dir;
  287. if (file->flags & DFS_O_CREAT)
  288. {
  289. result = f_mkdir(drivers_fn);
  290. if (result != FR_OK)
  291. {
  292. #if _VOLUMES > 1
  293. rt_free(drivers_fn);
  294. #endif
  295. return elm_result_to_dfs(result);
  296. }
  297. }
  298. /* open directory */
  299. dir = (DIR *)rt_malloc(sizeof(DIR));
  300. if (dir == RT_NULL)
  301. {
  302. #if _VOLUMES > 1
  303. rt_free(drivers_fn);
  304. #endif
  305. return -DFS_STATUS_ENOMEM;
  306. }
  307. result = f_opendir(dir, drivers_fn);
  308. #if _VOLUMES > 1
  309. rt_free(drivers_fn);
  310. #endif
  311. if (result != FR_OK)
  312. {
  313. rt_free(dir);
  314. return elm_result_to_dfs(result);
  315. }
  316. file->data = dir;
  317. return DFS_STATUS_OK;
  318. }
  319. else
  320. {
  321. mode = FA_READ;
  322. if (file->flags & DFS_O_WRONLY)
  323. mode |= FA_WRITE;
  324. if ((file->flags & DFS_O_ACCMODE) & DFS_O_RDWR)
  325. mode |= FA_WRITE;
  326. /* Opens the file, if it is existing. If not, a new file is created. */
  327. if (file->flags & DFS_O_CREAT)
  328. mode |= FA_OPEN_ALWAYS;
  329. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  330. if (file->flags & DFS_O_TRUNC)
  331. mode |= FA_CREATE_ALWAYS;
  332. /* Creates a new file. The function fails if the file is already existing. */
  333. if (file->flags & DFS_O_EXCL)
  334. mode |= FA_CREATE_NEW;
  335. /* allocate a fd */
  336. fd = (FIL *)rt_malloc(sizeof(FIL));
  337. if (fd == RT_NULL)
  338. {
  339. #if _VOLUMES > 1
  340. rt_free(drivers_fn);
  341. #endif
  342. return -DFS_STATUS_ENOMEM;
  343. }
  344. result = f_open(fd, drivers_fn, mode);
  345. #if _VOLUMES > 1
  346. rt_free(drivers_fn);
  347. #endif
  348. if (result == FR_OK)
  349. {
  350. file->pos = fd->fptr;
  351. file->size = f_size(fd);
  352. file->data = fd;
  353. if (file->flags & DFS_O_APPEND)
  354. {
  355. /* seek to the end of file */
  356. f_lseek(fd, f_size(fd));
  357. file->pos = fd->fptr;
  358. }
  359. }
  360. else
  361. {
  362. /* open failed, return */
  363. rt_free(fd);
  364. return elm_result_to_dfs(result);
  365. }
  366. }
  367. return DFS_STATUS_OK;
  368. }
  369. int dfs_elm_close(struct dfs_fd *file)
  370. {
  371. FRESULT result;
  372. result = FR_OK;
  373. if (file->type == FT_DIRECTORY)
  374. {
  375. DIR *dir;
  376. dir = (DIR *)(file->data);
  377. RT_ASSERT(dir != RT_NULL);
  378. /* release memory */
  379. rt_free(dir);
  380. }
  381. else if (file->type == FT_REGULAR)
  382. {
  383. FIL *fd;
  384. fd = (FIL *)(file->data);
  385. RT_ASSERT(fd != RT_NULL);
  386. result = f_close(fd);
  387. if (result == FR_OK)
  388. {
  389. /* release memory */
  390. rt_free(fd);
  391. }
  392. }
  393. return elm_result_to_dfs(result);
  394. }
  395. int dfs_elm_ioctl(struct dfs_fd *file, int cmd, void *args)
  396. {
  397. return -DFS_STATUS_ENOSYS;
  398. }
  399. int dfs_elm_read(struct dfs_fd *file, void *buf, rt_size_t len)
  400. {
  401. FIL *fd;
  402. FRESULT result;
  403. UINT byte_read;
  404. if (file->type == FT_DIRECTORY)
  405. {
  406. return -DFS_STATUS_EISDIR;
  407. }
  408. fd = (FIL *)(file->data);
  409. RT_ASSERT(fd != RT_NULL);
  410. result = f_read(fd, buf, len, &byte_read);
  411. /* update position */
  412. file->pos = fd->fptr;
  413. if (result == FR_OK)
  414. return byte_read;
  415. return elm_result_to_dfs(result);
  416. }
  417. int dfs_elm_write(struct dfs_fd *file, const void *buf, rt_size_t len)
  418. {
  419. FIL *fd;
  420. FRESULT result;
  421. UINT byte_write;
  422. if (file->type == FT_DIRECTORY)
  423. {
  424. return -DFS_STATUS_EISDIR;
  425. }
  426. fd = (FIL *)(file->data);
  427. RT_ASSERT(fd != RT_NULL);
  428. result = f_write(fd, buf, len, &byte_write);
  429. /* update position and file size */
  430. file->pos = fd->fptr;
  431. file->size = f_size(fd);
  432. if (result == FR_OK)
  433. return byte_write;
  434. return elm_result_to_dfs(result);
  435. }
  436. int dfs_elm_flush(struct dfs_fd *file)
  437. {
  438. FIL *fd;
  439. FRESULT result;
  440. fd = (FIL *)(file->data);
  441. RT_ASSERT(fd != RT_NULL);
  442. result = f_sync(fd);
  443. return elm_result_to_dfs(result);
  444. }
  445. int dfs_elm_lseek(struct dfs_fd *file, rt_off_t offset)
  446. {
  447. FRESULT result = FR_OK;
  448. if (file->type == FT_REGULAR)
  449. {
  450. FIL *fd;
  451. /* regular file type */
  452. fd = (FIL *)(file->data);
  453. RT_ASSERT(fd != RT_NULL);
  454. result = f_lseek(fd, offset);
  455. if (result == FR_OK)
  456. {
  457. /* return current position */
  458. file->pos = fd->fptr;
  459. return fd->fptr;
  460. }
  461. }
  462. else if (file->type == FT_DIRECTORY)
  463. {
  464. /* which is a directory */
  465. DIR *dir;
  466. dir = (DIR *)(file->data);
  467. RT_ASSERT(dir != RT_NULL);
  468. result = f_seekdir(dir, offset / sizeof(struct dirent));
  469. if (result == FR_OK)
  470. {
  471. /* update file position */
  472. file->pos = offset;
  473. return file->pos;
  474. }
  475. }
  476. return elm_result_to_dfs(result);
  477. }
  478. int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  479. {
  480. DIR *dir;
  481. FILINFO fno;
  482. FRESULT result;
  483. rt_uint32_t index;
  484. struct dirent *d;
  485. dir = (DIR *)(file->data);
  486. RT_ASSERT(dir != RT_NULL);
  487. /* make integer count */
  488. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  489. if (count == 0)
  490. return -DFS_STATUS_EINVAL;
  491. index = 0;
  492. while (1)
  493. {
  494. char *fn;
  495. d = dirp + index;
  496. result = f_readdir(dir, &fno);
  497. if (result != FR_OK || fno.fname[0] == 0)
  498. break;
  499. #if _USE_LFN
  500. fn = *fno.fname ? fno.fname : fno.altname;
  501. #else
  502. fn = fno.fname;
  503. #endif
  504. d->d_type = DFS_DT_UNKNOWN;
  505. if (fno.fattrib & AM_DIR)
  506. d->d_type = DFS_DT_DIR;
  507. else
  508. d->d_type = DFS_DT_REG;
  509. d->d_namlen = (rt_uint8_t)rt_strlen(fn);
  510. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  511. rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);
  512. index ++;
  513. if (index * sizeof(struct dirent) >= count)
  514. break;
  515. }
  516. if (index == 0)
  517. return elm_result_to_dfs(result);
  518. file->pos += index * sizeof(struct dirent);
  519. return index * sizeof(struct dirent);
  520. }
  521. int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path)
  522. {
  523. FRESULT result;
  524. #if _VOLUMES > 1
  525. int vol;
  526. char *drivers_fn;
  527. extern int elm_get_vol(FATFS * fat);
  528. /* add path for ELM FatFS driver support */
  529. vol = elm_get_vol((FATFS *)fs->data);
  530. if (vol < 0)
  531. return -DFS_STATUS_ENOENT;
  532. drivers_fn = rt_malloc(256);
  533. if (drivers_fn == RT_NULL)
  534. return -DFS_STATUS_ENOMEM;
  535. rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
  536. #else
  537. const char *drivers_fn;
  538. drivers_fn = path;
  539. #endif
  540. result = f_unlink(drivers_fn);
  541. #if _VOLUMES > 1
  542. rt_free(drivers_fn);
  543. #endif
  544. return elm_result_to_dfs(result);
  545. }
  546. int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath)
  547. {
  548. FRESULT result;
  549. #if _VOLUMES > 1
  550. char *drivers_oldfn;
  551. const char *drivers_newfn;
  552. int vol;
  553. extern int elm_get_vol(FATFS * fat);
  554. /* add path for ELM FatFS driver support */
  555. vol = elm_get_vol((FATFS *)fs->data);
  556. if (vol < 0)
  557. return -DFS_STATUS_ENOENT;
  558. drivers_oldfn = rt_malloc(256);
  559. if (drivers_oldfn == RT_NULL)
  560. return -DFS_STATUS_ENOMEM;
  561. drivers_newfn = newpath;
  562. rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, oldpath);
  563. #else
  564. const char *drivers_oldfn, *drivers_newfn;
  565. drivers_oldfn = oldpath;
  566. drivers_newfn = newpath;
  567. #endif
  568. result = f_rename(drivers_oldfn, drivers_newfn);
  569. #if _VOLUMES > 1
  570. rt_free(drivers_oldfn);
  571. #endif
  572. return elm_result_to_dfs(result);
  573. }
  574. int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  575. {
  576. FILINFO file_info;
  577. FRESULT result;
  578. #if _VOLUMES > 1
  579. int vol;
  580. char *drivers_fn;
  581. extern int elm_get_vol(FATFS * fat);
  582. /* add path for ELM FatFS driver support */
  583. vol = elm_get_vol((FATFS *)fs->data);
  584. if (vol < 0)
  585. return -DFS_STATUS_ENOENT;
  586. drivers_fn = rt_malloc(256);
  587. if (drivers_fn == RT_NULL)
  588. return -DFS_STATUS_ENOMEM;
  589. rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
  590. #else
  591. const char *drivers_fn;
  592. drivers_fn = path;
  593. #endif
  594. result = f_stat(drivers_fn, &file_info);
  595. #if _VOLUMES > 1
  596. rt_free(drivers_fn);
  597. #endif
  598. if (result == FR_OK)
  599. {
  600. /* convert to dfs stat structure */
  601. st->st_dev = 0;
  602. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  603. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  604. if (file_info.fattrib & AM_DIR)
  605. {
  606. st->st_mode &= ~DFS_S_IFREG;
  607. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  608. }
  609. if (file_info.fattrib & AM_RDO)
  610. st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH);
  611. st->st_size = file_info.fsize;
  612. st->st_blksize = 512;
  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 */