dfs_elm.c 23 KB

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