1
0

dfs_elm.c 23 KB

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