copy_this_file_dfs_elm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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_def.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. st->st_blksize = 512;
  531. }
  532. #if _USE_LFN
  533. rt_free(file_info.lfname);
  534. #endif
  535. return elm_result_to_dfs(result);
  536. }
  537. static const struct dfs_filesystem_operation dfs_elm =
  538. {
  539. "elm",
  540. dfs_elm_mount,
  541. dfs_elm_unmount,
  542. dfs_elm_mkfs,
  543. dfs_elm_statfs,
  544. dfs_elm_open,
  545. dfs_elm_close,
  546. dfs_elm_ioctl,
  547. dfs_elm_read,
  548. dfs_elm_write,
  549. dfs_elm_flush,
  550. dfs_elm_lseek,
  551. dfs_elm_getdents,
  552. dfs_elm_unlink,
  553. dfs_elm_stat,
  554. dfs_elm_rename,
  555. };
  556. int elm_init(void)
  557. {
  558. #ifdef DFS_ELMFAT_INTERFACE_EFM
  559. int i;
  560. for (i = 0; i < _VOLUMES; i++)
  561. {
  562. diskStat[i] = STA_NOINIT;
  563. }
  564. #endif
  565. /* register fatfs file system */
  566. dfs_register(&dfs_elm);
  567. return 0;
  568. }
  569. /*
  570. * RT-Thread Device Interface for ELM FatFs
  571. */
  572. #ifdef DFS_ELMFAT_INTERFACE_EFM
  573. /*-----------------------------------------------------------------------*/
  574. /* Initialize Disk Drive */
  575. /*-----------------------------------------------------------------------*/
  576. DSTATUS disk_initialize (
  577. BYTE drv /* Physical drive nmuber */
  578. )
  579. {
  580. rt_device_t device = disk[drv];
  581. if (!device)
  582. {
  583. return RES_ERROR;
  584. }
  585. if (diskStat[drv] & STA_NODISK)
  586. {
  587. /* No card in the socket */
  588. return diskStat[drv];
  589. }
  590. /* Initialize hardware: the actual operation is performed in dfs_mount() */
  591. diskStat[drv] &= ~STA_NOINIT;
  592. return diskStat[drv];
  593. }
  594. /*-----------------------------------------------------------------------*/
  595. /* Get Disk Status */
  596. /*-----------------------------------------------------------------------*/
  597. DSTATUS disk_status (
  598. BYTE drv /* Physical drive nmuber */
  599. )
  600. {
  601. return diskStat[drv];
  602. }
  603. /*-----------------------------------------------------------------------*/
  604. /* Read Sector(s) */
  605. /*-----------------------------------------------------------------------*/
  606. DRESULT disk_read (
  607. BYTE drv, /* Physical drive nmuber */
  608. BYTE *buff, /* Pointer to the data buffer to store read data */
  609. DWORD sector, /* Start sector number (LBA) */
  610. BYTE count /* Sector count (1..255) */
  611. )
  612. {
  613. rt_device_t device = disk[drv];
  614. if (!device)
  615. {
  616. return RES_ERROR;
  617. }
  618. if (!count)
  619. {
  620. return RES_PARERR;
  621. }
  622. if (diskStat[drv] & STA_NOINIT)
  623. {
  624. return RES_NOTRDY;
  625. }
  626. if (rt_device_read(device, sector, buff, count) != count)
  627. {
  628. return RES_ERROR;
  629. }
  630. else
  631. {
  632. return RES_OK;
  633. }
  634. }
  635. /*-----------------------------------------------------------------------*/
  636. /* Write Sector(s) */
  637. /*-----------------------------------------------------------------------*/
  638. #if _READONLY == 0
  639. DRESULT disk_write (
  640. BYTE drv, /* Physical drive nmuber */
  641. const BYTE *buff, /* Pointer to the data to be written */
  642. DWORD sector, /* Start sector number (LBA) */
  643. BYTE count /* Sector count (1..255) */
  644. )
  645. {
  646. rt_device_t device = disk[drv];
  647. if (!device)
  648. {
  649. return RES_ERROR;
  650. }
  651. if (!count)
  652. {
  653. return RES_PARERR;
  654. }
  655. if (diskStat[drv] & STA_NOINIT)
  656. {
  657. return RES_NOTRDY;
  658. }
  659. if (diskStat[drv] & STA_PROTECT)
  660. {
  661. return RES_WRPRT;
  662. }
  663. if (rt_device_write(device, sector, buff, count) != count)
  664. {
  665. return RES_ERROR;
  666. }
  667. else
  668. {
  669. return RES_OK;
  670. }
  671. }
  672. #endif /* _READONLY */
  673. /*-----------------------------------------------------------------------*/
  674. /* Miscellaneous Functions */
  675. /*-----------------------------------------------------------------------*/
  676. DRESULT disk_ioctl (
  677. BYTE drv, /* Physical drive nmuber */
  678. BYTE ctrl, /* Control code */
  679. void *buff /* Buffer to send/receive data block */
  680. )
  681. {
  682. rt_device_t device = disk[drv];
  683. if (!device)
  684. {
  685. return RES_ERROR;
  686. }
  687. if (diskStat[drv] & STA_NOINIT)
  688. {
  689. return RES_NOTRDY;
  690. }
  691. if (rt_device_control(device, ctrl, buff) != RT_EOK)
  692. {
  693. return RES_ERROR;
  694. }
  695. else
  696. {
  697. return RES_OK;
  698. }
  699. }
  700. #else
  701. #include "diskio.h"
  702. /* Inidialize a Drive */
  703. DSTATUS disk_initialize(BYTE drv)
  704. {
  705. return 0;
  706. }
  707. /* Return Disk Status */
  708. DSTATUS disk_status(BYTE drv)
  709. {
  710. return 0;
  711. }
  712. /* Read Sector(s) */
  713. DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
  714. {
  715. rt_size_t result;
  716. rt_device_t device = disk[drv];
  717. result = rt_device_read(device, sector, buff, count);
  718. if (result == count)
  719. {
  720. return RES_OK;
  721. }
  722. return RES_ERROR;
  723. }
  724. /* Write Sector(s) */
  725. DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
  726. {
  727. rt_size_t result;
  728. rt_device_t device = disk[drv];
  729. result = rt_device_write(device, sector, buff, count);
  730. if (result == count)
  731. {
  732. return RES_OK;
  733. }
  734. return RES_ERROR;
  735. }
  736. /* Miscellaneous Functions */
  737. DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
  738. {
  739. rt_device_t device = disk[drv];
  740. if (device == RT_NULL)
  741. return RES_ERROR;
  742. if (ctrl == GET_SECTOR_COUNT)
  743. {
  744. struct rt_device_blk_geometry geometry;
  745. rt_memset(&geometry, 0, sizeof(geometry));
  746. rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  747. *(DWORD *)buff = geometry.sector_count;
  748. if (geometry.sector_count == 0)
  749. return RES_ERROR;
  750. }
  751. else if (ctrl == GET_SECTOR_SIZE)
  752. {
  753. struct rt_device_blk_geometry geometry;
  754. rt_memset(&geometry, 0, sizeof(geometry));
  755. rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  756. *(WORD *)buff = geometry.bytes_per_sector;
  757. }
  758. else if (ctrl == GET_BLOCK_SIZE) /* Get erase block size in unit of sectors (DWORD) */
  759. {
  760. struct rt_device_blk_geometry geometry;
  761. rt_memset(&geometry, 0, sizeof(geometry));
  762. rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  763. *(DWORD *)buff = geometry.block_size/geometry.bytes_per_sector;
  764. }
  765. return RES_OK;
  766. }
  767. #endif
  768. rt_time_t get_fattime(void)
  769. {
  770. return 0;
  771. }
  772. #if _FS_REENTRANT
  773. int ff_cre_syncobj(BYTE drv, _SYNC_t *m)
  774. {
  775. char name[8];
  776. rt_mutex_t mutex;
  777. rt_snprintf(name, sizeof(name), "fat%d", drv);
  778. mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  779. if (mutex != RT_NULL)
  780. {
  781. *m = mutex;
  782. return RT_TRUE;
  783. }
  784. return RT_FALSE;
  785. }
  786. int ff_del_syncobj(_SYNC_t m)
  787. {
  788. rt_mutex_delete(m);
  789. return RT_TRUE;
  790. }
  791. int ff_req_grant(_SYNC_t m)
  792. {
  793. if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK)
  794. return RT_TRUE;
  795. return RT_FALSE;
  796. }
  797. void ff_rel_grant(_SYNC_t m)
  798. {
  799. rt_mutex_release(m);
  800. }
  801. #endif