dfs_elm.c 22 KB

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