dfs_elm.c 23 KB

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