dfs_elm.c 15 KB

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