copy_this_file_dfs_elm.c 18 KB

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