1
0

dfs_elm.c 22 KB

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