dfs_elm.c 23 KB

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