dfs_elm.c 16 KB

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