dfs_elm.c 23 KB

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