dfs_elm.c 21 KB

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