copy_this_file_dfs_elm.c 19 KB

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