dfs_elm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. #if _VOLUMES > 1
  262. rt_free(drivers_fn);
  263. #endif
  264. return -DFS_STATUS_ENOMEM;
  265. }
  266. result = f_open(fd, drivers_fn, mode);
  267. #if _VOLUMES > 1
  268. rt_free(drivers_fn);
  269. #endif
  270. if (result == FR_OK)
  271. {
  272. file->pos = fd->fptr;
  273. file->size = fd->fsize;
  274. file->data = fd;
  275. if (file->flags & DFS_O_APPEND)
  276. {
  277. file->pos = f_lseek(fd, fd->fsize);
  278. }
  279. }
  280. else
  281. {
  282. /* open failed, return */
  283. rt_free(fd);
  284. return elm_result_to_dfs(result);
  285. }
  286. }
  287. return DFS_STATUS_OK;
  288. }
  289. int dfs_elm_close(struct dfs_fd *file)
  290. {
  291. FRESULT result;
  292. result = FR_OK;
  293. if (file->type == FT_DIRECTORY)
  294. {
  295. DIR *dir;
  296. dir = (DIR *)(file->data);
  297. RT_ASSERT(dir != RT_NULL);
  298. /* release memory */
  299. rt_free(dir);
  300. }
  301. else if (file->type == FT_REGULAR)
  302. {
  303. FIL *fd;
  304. fd = (FIL *)(file->data);
  305. RT_ASSERT(fd != RT_NULL);
  306. result = f_close(fd);
  307. if (result == FR_OK)
  308. {
  309. /* release memory */
  310. rt_free(fd);
  311. }
  312. }
  313. return elm_result_to_dfs(result);
  314. }
  315. int dfs_elm_ioctl(struct dfs_fd *file, int cmd, void *args)
  316. {
  317. return -DFS_STATUS_ENOSYS;
  318. }
  319. int dfs_elm_read(struct dfs_fd *file, void *buf, rt_size_t len)
  320. {
  321. FIL *fd;
  322. FRESULT result;
  323. UINT byte_read;
  324. if (file->type == FT_DIRECTORY)
  325. {
  326. return -DFS_STATUS_EISDIR;
  327. }
  328. fd = (FIL *)(file->data);
  329. RT_ASSERT(fd != RT_NULL);
  330. result = f_read(fd, buf, len, &byte_read);
  331. /* update position */
  332. file->pos = fd->fptr;
  333. if (result == FR_OK)
  334. return byte_read;
  335. return elm_result_to_dfs(result);
  336. }
  337. int dfs_elm_write(struct dfs_fd *file, const void *buf, rt_size_t len)
  338. {
  339. FIL *fd;
  340. FRESULT result;
  341. UINT byte_write;
  342. if (file->type == FT_DIRECTORY)
  343. {
  344. return -DFS_STATUS_EISDIR;
  345. }
  346. fd = (FIL *)(file->data);
  347. RT_ASSERT(fd != RT_NULL);
  348. result = f_write(fd, buf, len, &byte_write);
  349. /* update position and file size */
  350. file->pos = fd->fptr;
  351. file->size = fd->fsize;
  352. if (result == FR_OK)
  353. return byte_write;
  354. return elm_result_to_dfs(result);
  355. }
  356. int dfs_elm_flush(struct dfs_fd *file)
  357. {
  358. FIL *fd;
  359. FRESULT result;
  360. fd = (FIL *)(file->data);
  361. RT_ASSERT(fd != RT_NULL);
  362. result = f_sync(fd);
  363. return elm_result_to_dfs(result);
  364. }
  365. int dfs_elm_lseek(struct dfs_fd *file, rt_off_t offset)
  366. {
  367. FRESULT result = FR_OK;
  368. if (file->type == FT_REGULAR)
  369. {
  370. FIL *fd;
  371. /* regular file type */
  372. fd = (FIL *)(file->data);
  373. RT_ASSERT(fd != RT_NULL);
  374. result = f_lseek(fd, offset);
  375. if (result == FR_OK)
  376. {
  377. /* return current position */
  378. return fd->fptr;
  379. }
  380. }
  381. else if (file->type == FT_DIRECTORY)
  382. {
  383. /* which is a directory */
  384. DIR *dir;
  385. dir = (DIR *)(file->data);
  386. RT_ASSERT(dir != RT_NULL);
  387. result = f_seekdir(dir, offset / sizeof(struct dirent));
  388. if (result == FR_OK)
  389. {
  390. /* update file position */
  391. file->pos = offset;
  392. return file->pos;
  393. }
  394. }
  395. return elm_result_to_dfs(result);
  396. }
  397. int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  398. {
  399. DIR *dir;
  400. FILINFO fno;
  401. FRESULT result;
  402. rt_uint32_t index;
  403. struct dirent *d;
  404. dir = (DIR *)(file->data);
  405. RT_ASSERT(dir != RT_NULL);
  406. /* make integer count */
  407. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  408. if (count == 0)
  409. return -DFS_STATUS_EINVAL;
  410. #if _USE_LFN
  411. /* allocate long file name */
  412. fno.lfname = rt_malloc(256);
  413. fno.lfsize = 256;
  414. #endif
  415. index = 0;
  416. while (1)
  417. {
  418. char *fn;
  419. d = dirp + index;
  420. result = f_readdir(dir, &fno);
  421. if (result != FR_OK || fno.fname[0] == 0)
  422. break;
  423. #if _USE_LFN
  424. fn = *fno.lfname? fno.lfname : fno.fname;
  425. #else
  426. fn = fno.fname;
  427. #endif
  428. d->d_type = DFS_DT_UNKNOWN;
  429. if (fno.fattrib & AM_DIR)
  430. d->d_type = DFS_DT_DIR;
  431. else
  432. d->d_type = DFS_DT_REG;
  433. d->d_namlen = rt_strlen(fn);
  434. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  435. rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);
  436. index ++;
  437. if (index * sizeof(struct dirent) >= count)
  438. break;
  439. }
  440. #if _USE_LFN
  441. rt_free(fno.lfname);
  442. #endif
  443. if (index == 0)
  444. return elm_result_to_dfs(result);
  445. file->pos += index * sizeof(struct dirent);
  446. return index * sizeof(struct dirent);
  447. }
  448. int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path)
  449. {
  450. FRESULT result;
  451. #if _VOLUMES > 1
  452. int vol;
  453. char *drivers_fn;
  454. extern int elm_get_vol(FATFS *fat);
  455. /* add path for ELM FatFS driver support */
  456. vol = elm_get_vol((FATFS *)fs->data);
  457. if (vol < 0)
  458. return -DFS_STATUS_ENOENT;
  459. drivers_fn = rt_malloc(256);
  460. if (drivers_fn == RT_NULL)
  461. return -DFS_STATUS_ENOMEM;
  462. rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
  463. #else
  464. const char *drivers_fn;
  465. drivers_fn = path;
  466. #endif
  467. result = f_unlink(drivers_fn);
  468. #if _VOLUMES > 1
  469. rt_free(drivers_fn);
  470. #endif
  471. return elm_result_to_dfs(result);
  472. }
  473. int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath)
  474. {
  475. FRESULT result;
  476. #if _VOLUMES > 1
  477. char *drivers_oldfn;
  478. const char *drivers_newfn;
  479. int vol;
  480. extern int elm_get_vol(FATFS *fat);
  481. /* add path for ELM FatFS driver support */
  482. vol = elm_get_vol((FATFS *)fs->data);
  483. if (vol < 0)
  484. return -DFS_STATUS_ENOENT;
  485. drivers_oldfn = rt_malloc(256);
  486. if (drivers_oldfn == RT_NULL)
  487. return -DFS_STATUS_ENOMEM;
  488. drivers_newfn = newpath;
  489. rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, oldpath);
  490. #else
  491. const char *drivers_oldfn, *drivers_newfn;
  492. drivers_oldfn = oldpath;
  493. drivers_newfn = newpath;
  494. #endif
  495. result = f_rename(drivers_oldfn, drivers_newfn);
  496. #if _VOLUMES > 1
  497. rt_free(drivers_oldfn);
  498. #endif
  499. return elm_result_to_dfs(result);
  500. }
  501. int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  502. {
  503. FILINFO file_info;
  504. FRESULT result;
  505. #if _VOLUMES > 1
  506. int vol;
  507. char *drivers_fn;
  508. extern int elm_get_vol(FATFS *fat);
  509. /* add path for ELM FatFS driver support */
  510. vol = elm_get_vol((FATFS *)fs->data);
  511. if (vol < 0)
  512. return -DFS_STATUS_ENOENT;
  513. drivers_fn = rt_malloc(256);
  514. if (drivers_fn == RT_NULL)
  515. return -DFS_STATUS_ENOMEM;
  516. rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
  517. #else
  518. const char *drivers_fn;
  519. drivers_fn = path;
  520. #endif
  521. #if _USE_LFN
  522. /* allocate long file name */
  523. file_info.lfname = rt_malloc(256);
  524. file_info.lfsize = 256;
  525. #endif
  526. result = f_stat(drivers_fn, &file_info);
  527. #if _VOLUMES > 1
  528. rt_free(drivers_fn);
  529. #endif
  530. if (result == FR_OK)
  531. {
  532. /* convert to dfs stat structure */
  533. st->st_dev = 0;
  534. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  535. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  536. if (file_info.fattrib & AM_DIR)
  537. {
  538. st->st_mode &= ~DFS_S_IFREG;
  539. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  540. }
  541. if (file_info.fattrib & AM_RDO)
  542. st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH);
  543. st->st_size = file_info.fsize;
  544. st->st_mtime = file_info.ftime;
  545. st->st_blksize = 512;
  546. }
  547. #if _USE_LFN
  548. rt_free(file_info.lfname);
  549. #endif
  550. return elm_result_to_dfs(result);
  551. }
  552. static const struct dfs_filesystem_operation dfs_elm =
  553. {
  554. "elm",
  555. DFS_FS_FLAG_DEFAULT,
  556. dfs_elm_mount,
  557. dfs_elm_unmount,
  558. dfs_elm_mkfs,
  559. dfs_elm_statfs,
  560. dfs_elm_open,
  561. dfs_elm_close,
  562. dfs_elm_ioctl,
  563. dfs_elm_read,
  564. dfs_elm_write,
  565. dfs_elm_flush,
  566. dfs_elm_lseek,
  567. dfs_elm_getdents,
  568. dfs_elm_unlink,
  569. dfs_elm_stat,
  570. dfs_elm_rename,
  571. };
  572. int elm_init(void)
  573. {
  574. /* register fatfs file system */
  575. dfs_register(&dfs_elm);
  576. return 0;
  577. }
  578. /*
  579. * RT-Thread Device Interface for ELM FatFs
  580. */
  581. #include "diskio.h"
  582. /* Initialize a Drive */
  583. DSTATUS disk_initialize(BYTE drv)
  584. {
  585. return 0;
  586. }
  587. /* Return Disk Status */
  588. DSTATUS disk_status(BYTE drv)
  589. {
  590. return 0;
  591. }
  592. /* Read Sector(s) */
  593. DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
  594. {
  595. rt_size_t result;
  596. rt_device_t device = disk[drv];
  597. result = rt_device_read(device, sector, buff, count);
  598. if (result == count)
  599. {
  600. return RES_OK;
  601. }
  602. return RES_ERROR;
  603. }
  604. /* Write Sector(s) */
  605. DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
  606. {
  607. rt_size_t result;
  608. rt_device_t device = disk[drv];
  609. result = rt_device_write(device, sector, buff, count);
  610. if (result == count)
  611. {
  612. return RES_OK;
  613. }
  614. return RES_ERROR;
  615. }
  616. /* Miscellaneous Functions */
  617. DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
  618. {
  619. rt_device_t device = disk[drv];
  620. if (device == RT_NULL)
  621. return RES_ERROR;
  622. if (ctrl == GET_SECTOR_COUNT)
  623. {
  624. struct rt_device_blk_geometry geometry;
  625. rt_memset(&geometry, 0, sizeof(geometry));
  626. rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  627. *(DWORD *)buff = geometry.sector_count;
  628. if (geometry.sector_count == 0)
  629. return RES_ERROR;
  630. }
  631. else if (ctrl == GET_SECTOR_SIZE)
  632. {
  633. struct rt_device_blk_geometry geometry;
  634. rt_memset(&geometry, 0, sizeof(geometry));
  635. rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  636. *(WORD *)buff = geometry.bytes_per_sector;
  637. }
  638. else if (ctrl == GET_BLOCK_SIZE) /* Get erase block size in unit of sectors (DWORD) */
  639. {
  640. struct rt_device_blk_geometry geometry;
  641. rt_memset(&geometry, 0, sizeof(geometry));
  642. rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  643. *(DWORD *)buff = geometry.block_size/geometry.bytes_per_sector;
  644. }
  645. else if (ctrl == CTRL_SYNC)
  646. rt_device_control(device, RT_DEVICE_CTRL_BLK_SYNC, RT_NULL);
  647. return RES_OK;
  648. }
  649. rt_time_t get_fattime(void)
  650. {
  651. return 0;
  652. }
  653. #if _FS_REENTRANT
  654. int ff_cre_syncobj(BYTE drv, _SYNC_t *m)
  655. {
  656. char name[8];
  657. rt_mutex_t mutex;
  658. rt_snprintf(name, sizeof(name), "fat%d", drv);
  659. mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  660. if (mutex != RT_NULL)
  661. {
  662. *m = mutex;
  663. return RT_TRUE;
  664. }
  665. return RT_FALSE;
  666. }
  667. int ff_del_syncobj(_SYNC_t m)
  668. {
  669. rt_mutex_delete(m);
  670. return RT_TRUE;
  671. }
  672. int ff_req_grant(_SYNC_t m)
  673. {
  674. if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK)
  675. return RT_TRUE;
  676. return RT_FALSE;
  677. }
  678. void ff_rel_grant(_SYNC_t m)
  679. {
  680. rt_mutex_release(m);
  681. }
  682. #endif
  683. /* Memory functions */
  684. #if _USE_LFN == 3
  685. /* Allocate memory block */
  686. void* ff_memalloc (UINT size)
  687. {
  688. return rt_malloc(size);
  689. }
  690. /* Free memory block */
  691. void ff_memfree (void* mem)
  692. {
  693. rt_free(mem);
  694. }
  695. #endif /* _USE_LFN == 3 */