1
0

dfs_elm.c 22 KB

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