dfs_elm.c 23 KB

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