copy_this_file_dfs_elm.c 19 KB

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