dfs_elm.c 23 KB

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