dfs_elm.c 16 KB

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