dfs_uffs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-10-22 prife the first version
  9. * 2012-03-28 prife use mtd device interface
  10. * 2012-04-05 prife update uffs with official repo and use uffs_UnMount/Mount
  11. * 2017-04-12 lizhen9880 fix the f_bsize and f_blocks issue in function dfs_uffs_statfs
  12. */
  13. #include <rtthread.h>
  14. #include <dfs_fs.h>
  15. #include <dfs_file.h>
  16. #include <rtdevice.h>
  17. #include "dfs_uffs.h"
  18. #include "uffs/uffs_fd.h" /* posix file api is here */
  19. #include "uffs/uffs_mtb.h"
  20. #include "uffs/uffs_mem.h"
  21. #include "uffs/uffs_utils.h"
  22. /*
  23. * RT-Thread DFS Interface for uffs
  24. */
  25. #define UFFS_DEVICE_MAX 2 /* the max partions on a nand deivce*/
  26. #define UFFS_MOUNT_PATH_MAX 128 /* the mount point max length */
  27. #define FILE_PATH_MAX 256 /* the longest file path */
  28. struct _nand_dev
  29. {
  30. struct rt_mtd_nand_device *dev;
  31. struct uffs_StorageAttrSt storage;
  32. uffs_Device uffs_dev;
  33. uffs_MountTable mount_table;
  34. char mount_path[UFFS_MOUNT_PATH_MAX];
  35. void *data; /* when uffs use static buf, it will save ptr here */
  36. };
  37. /* make sure the following struct var had been initilased to 0! */
  38. static struct _nand_dev nand_part[UFFS_DEVICE_MAX] = {0};
  39. static int uffs_result_to_dfs(int result)
  40. {
  41. int status = -1;
  42. result = result < 0 ? -result : result;
  43. switch (result)
  44. {
  45. case UENOERR:/** no error */
  46. break;
  47. case UEACCES:/** Tried to open read-only file for writing, or files sharing mode
  48. does not allow specified operations, or given path is directory */
  49. status = -EINVAL;
  50. break;/* no suitable */
  51. case UEEXIST: /** _O_CREAT and _O_EXCL flags specified, but filename already exists */
  52. status = -EEXIST;
  53. break;
  54. case UEINVAL: /** Invalid oflag or pmode argument */
  55. status = -EINVAL;
  56. break;
  57. case UEMFILE: /** No more file handles available(too many open files) */
  58. status = -1;
  59. break;
  60. case UENOENT: /** file or path not found */
  61. status = -ENOENT;
  62. break;
  63. case UETIME: /** can't set file time */
  64. status = -1;
  65. break;
  66. case UEBADF: /** invalid file handle */
  67. status = -EBADF;
  68. break;
  69. case UENOMEM:/** no enough memory */
  70. status = -ENOSPC;
  71. break;
  72. case UEIOERR: /** I/O error from lower level flash operation */
  73. status = -EIO;
  74. break;
  75. case UENOTDIR: /** Not a directory */
  76. status = -ENOTDIR;
  77. break;
  78. case UEISDIR: /** Is a directory */
  79. status = -EISDIR;
  80. break;
  81. case UEUNKNOWN_ERR:
  82. default:
  83. status = -1;
  84. break; /* unknown error! */
  85. }
  86. return status;
  87. }
  88. static URET _device_init(uffs_Device *dev)
  89. {
  90. dev->attr->_private = NULL; // hook nand_chip data structure to attr->_private
  91. dev->ops = (struct uffs_FlashOpsSt *)&nand_ops;
  92. return U_SUCC;
  93. }
  94. static URET _device_release(uffs_Device *dev)
  95. {
  96. return U_SUCC;
  97. }
  98. static int init_uffs_fs(
  99. struct _nand_dev *nand_part)
  100. {
  101. uffs_MountTable *mtb;
  102. struct rt_mtd_nand_device *nand;
  103. struct uffs_StorageAttrSt *flash_storage;
  104. mtb = &nand_part->mount_table;
  105. nand = nand_part->dev;
  106. flash_storage = &nand_part->storage;
  107. /* setup nand storage attributes */
  108. uffs_setup_storage(flash_storage, nand);
  109. /* register mount table */
  110. if (mtb->dev)
  111. {
  112. /* set memory allocator for uffs */
  113. #if CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR > 0
  114. uffs_MemSetupSystemAllocator(&mtb->dev->mem);
  115. #endif
  116. /* setup device init/release entry */
  117. mtb->dev->Init = _device_init;
  118. mtb->dev->Release = _device_release;
  119. mtb->dev->attr = flash_storage;
  120. uffs_RegisterMountTable(mtb);
  121. }
  122. /* mount uffs partion on nand device */
  123. return uffs_Mount(nand_part->mount_path) == U_SUCC ? 0 : -1;
  124. }
  125. static int dfs_uffs_mount(
  126. struct dfs_filesystem *fs,
  127. unsigned long rwflag,
  128. const void *data)
  129. {
  130. rt_base_t index;
  131. uffs_MountTable *mount_part;
  132. struct rt_mtd_nand_device *dev;
  133. RT_ASSERT(rt_strlen(fs->path) < (UFFS_MOUNT_PATH_MAX - 1));
  134. dev = RT_MTD_NAND_DEVICE(fs->dev_id);
  135. /*1. find a empty entry in partition table */
  136. for (index = 0; index < UFFS_DEVICE_MAX ; index ++)
  137. {
  138. if (nand_part[index].dev == RT_NULL)
  139. break;
  140. }
  141. if (index == UFFS_DEVICE_MAX)
  142. return -ENOENT;
  143. /*2. fill partition structure */
  144. nand_part[index].dev = dev;
  145. /* make a right mount path for uffs, end with '/' */
  146. rt_snprintf(nand_part[index].mount_path, UFFS_MOUNT_PATH_MAX, "%s/", fs->path);
  147. if (nand_part[index].mount_path[1] == '/')
  148. nand_part[index].mount_path[1] = 0;
  149. mount_part = &(nand_part[index].mount_table);
  150. mount_part->mount = nand_part[index].mount_path;
  151. mount_part->dev = &(nand_part[index].uffs_dev);
  152. rt_memset(mount_part->dev, 0, sizeof(uffs_Device));//in order to make uffs happy.
  153. mount_part->dev->_private = dev; /* save dev_id into uffs */
  154. mount_part->start_block = dev->block_start;
  155. mount_part->end_block = dev->block_end;
  156. /*3. mount uffs */
  157. if (init_uffs_fs(&nand_part[index]) < 0)
  158. {
  159. return uffs_result_to_dfs(uffs_get_error());
  160. }
  161. return 0;
  162. }
  163. static int dfs_uffs_unmount(struct dfs_filesystem *fs)
  164. {
  165. rt_base_t index;
  166. int result;
  167. /* find the device index and then unmount it */
  168. for (index = 0; index < UFFS_DEVICE_MAX; index++)
  169. {
  170. if (nand_part[index].dev == RT_MTD_NAND_DEVICE(fs->dev_id))
  171. {
  172. nand_part[index].dev = RT_NULL;
  173. result = uffs_UnMount(nand_part[index].mount_path);
  174. if (result != U_SUCC)
  175. break;
  176. result = uffs_UnRegisterMountTable(& nand_part[index].mount_table);
  177. return (result == U_SUCC) ? RT_EOK : -1;
  178. }
  179. }
  180. return -ENOENT;
  181. }
  182. static int dfs_uffs_mkfs(rt_device_t dev_id)
  183. {
  184. rt_base_t index;
  185. rt_uint32_t block;
  186. struct rt_mtd_nand_device *mtd;
  187. /*1. find the device index */
  188. for (index = 0; index < UFFS_DEVICE_MAX; index++)
  189. {
  190. if (nand_part[index].dev == (struct rt_mtd_nand_device *)dev_id)
  191. break;
  192. }
  193. if (index == UFFS_DEVICE_MAX)
  194. {
  195. /* can't find device driver */
  196. return -ENOENT;
  197. }
  198. /*2. then unmount the partition */
  199. uffs_UnMount(nand_part[index].mount_path);
  200. mtd = nand_part[index].dev;
  201. /*3. erase all blocks on the partition */
  202. block = mtd->block_start;
  203. for (; block <= mtd->block_end; block++)
  204. {
  205. rt_mtd_nand_erase_block(mtd, block);
  206. #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
  207. if (rt_mtd_nand_check_block(mtd, block) != RT_EOK)
  208. {
  209. rt_kprintf("found bad block %d\n", block);
  210. rt_mtd_nand_mark_badblock(mtd, block);
  211. }
  212. #endif
  213. }
  214. /*4. remount it */
  215. if (init_uffs_fs(&nand_part[index]) < 0)
  216. {
  217. return uffs_result_to_dfs(uffs_get_error());
  218. }
  219. return RT_EOK;
  220. }
  221. static int dfs_uffs_statfs(struct dfs_filesystem *fs,
  222. struct statfs *buf)
  223. {
  224. rt_base_t index;
  225. struct rt_mtd_nand_device *mtd = RT_MTD_NAND_DEVICE(fs->dev_id);
  226. RT_ASSERT(mtd != RT_NULL);
  227. /* find the device index */
  228. for (index = 0; index < UFFS_DEVICE_MAX; index++)
  229. {
  230. if (nand_part[index].dev == (void *)mtd)
  231. break;
  232. }
  233. if (index == UFFS_DEVICE_MAX)
  234. return -ENOENT;
  235. buf->f_bsize = mtd->page_size * mtd->pages_per_block;
  236. buf->f_blocks = (mtd->block_end - mtd->block_start + 1);
  237. buf->f_bfree = uffs_GetDeviceFree(&nand_part[index].uffs_dev) / buf->f_bsize ;
  238. return 0;
  239. }
  240. static int dfs_uffs_open(struct dfs_fd *file)
  241. {
  242. int fd;
  243. int oflag, mode;
  244. char *file_path;
  245. oflag = file->flags;
  246. if (oflag & O_DIRECTORY) /* operations about dir */
  247. {
  248. uffs_DIR *dir;
  249. if (oflag & O_CREAT) /* create a dir*/
  250. {
  251. if (uffs_mkdir(file->path) < 0)
  252. return uffs_result_to_dfs(uffs_get_error());
  253. }
  254. /* open dir */
  255. file_path = rt_malloc(FILE_PATH_MAX);
  256. if (file_path == RT_NULL)
  257. return -ENOMEM;
  258. if (file->path[0] == '/' && !(file->path[1] == 0))
  259. rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->path);
  260. else
  261. {
  262. file_path[0] = '/';
  263. file_path[1] = 0;
  264. }
  265. dir = uffs_opendir(file_path);
  266. if (dir == RT_NULL)
  267. {
  268. rt_free(file_path);
  269. return uffs_result_to_dfs(uffs_get_error());
  270. }
  271. /* save this pointer,will used by dfs_uffs_getdents*/
  272. file->data = dir;
  273. rt_free(file_path);
  274. return RT_EOK;
  275. }
  276. /* regular file operations */
  277. /* int uffs_open(const char *name, int oflag, ...); what is this?
  278. * uffs_open can open dir!! **/
  279. mode = 0;
  280. if (oflag & O_RDONLY) mode |= UO_RDONLY;
  281. if (oflag & O_WRONLY) mode |= UO_WRONLY;
  282. if (oflag & O_RDWR) mode |= UO_RDWR;
  283. /* Opens the file, if it is existing. If not, a new file is created. */
  284. if (oflag & O_CREAT) mode |= UO_CREATE;
  285. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  286. if (oflag & O_TRUNC) mode |= UO_TRUNC;
  287. /* Creates a new file. The function fails if the file is already existing. */
  288. if (oflag & O_EXCL) mode |= UO_EXCL;
  289. fd = uffs_open(file->path, mode);
  290. if (fd < 0)
  291. {
  292. return uffs_result_to_dfs(uffs_get_error());
  293. }
  294. /* save this pointer, it will be used when calling read(), write(),
  295. * flush(), seek(), and will be free when calling close()*/
  296. file->data = (void *)fd;
  297. file->pos = uffs_seek(fd, 0, USEEK_CUR);
  298. file->size = uffs_seek(fd, 0, USEEK_END);
  299. uffs_seek(fd, file->pos, USEEK_SET);
  300. if (oflag & O_APPEND)
  301. {
  302. file->pos = uffs_seek(fd, 0, USEEK_END);
  303. }
  304. return 0;
  305. }
  306. static int dfs_uffs_close(struct dfs_fd *file)
  307. {
  308. int oflag;
  309. int fd;
  310. oflag = file->flags;
  311. if (oflag & O_DIRECTORY)
  312. {
  313. /* operations about dir */
  314. if (uffs_closedir((uffs_DIR *)(file->data)) < 0)
  315. return uffs_result_to_dfs(uffs_get_error());
  316. return 0;
  317. }
  318. /* regular file operations */
  319. fd = (int)(file->data);
  320. if (uffs_close(fd) == 0)
  321. return 0;
  322. return uffs_result_to_dfs(uffs_get_error());
  323. }
  324. static int dfs_uffs_ioctl(struct dfs_fd *file, int cmd, void *args)
  325. {
  326. return -ENOSYS;
  327. }
  328. static int dfs_uffs_read(struct dfs_fd *file, void *buf, size_t len)
  329. {
  330. int fd;
  331. int char_read;
  332. fd = (int)(file->data);
  333. char_read = uffs_read(fd, buf, len);
  334. if (char_read < 0)
  335. return uffs_result_to_dfs(uffs_get_error());
  336. /* update position */
  337. file->pos = uffs_seek(fd, 0, USEEK_CUR);
  338. return char_read;
  339. }
  340. static int dfs_uffs_write(struct dfs_fd *file,
  341. const void *buf,
  342. size_t len)
  343. {
  344. int fd;
  345. int char_write;
  346. fd = (int)(file->data);
  347. char_write = uffs_write(fd, buf, len);
  348. if (char_write < 0)
  349. return uffs_result_to_dfs(uffs_get_error());
  350. /* update position */
  351. file->pos = uffs_seek(fd, 0, USEEK_CUR);
  352. return char_write;
  353. }
  354. static int dfs_uffs_flush(struct dfs_fd *file)
  355. {
  356. int fd;
  357. int result;
  358. fd = (int)(file->data);
  359. result = uffs_flush(fd);
  360. if (result < 0)
  361. return uffs_result_to_dfs(uffs_get_error());
  362. return 0;
  363. }
  364. int uffs_seekdir(uffs_DIR *dir, long offset)
  365. {
  366. int i = 0;
  367. while (i < offset)
  368. {
  369. if (uffs_readdir(dir) == RT_NULL)
  370. return -1;
  371. i++;
  372. }
  373. return 0;
  374. }
  375. static int dfs_uffs_seek(struct dfs_fd *file,
  376. rt_off_t offset)
  377. {
  378. int result;
  379. /* set offset as current offset */
  380. if (file->type == FT_DIRECTORY)
  381. {
  382. uffs_rewinddir((uffs_DIR *)(file->data));
  383. result = uffs_seekdir((uffs_DIR *)(file->data), offset / sizeof(struct dirent));
  384. if (result >= 0)
  385. {
  386. file->pos = offset;
  387. return offset;
  388. }
  389. }
  390. else if (file->type == FT_REGULAR)
  391. {
  392. result = uffs_seek((int)(file->data), offset, USEEK_SET);
  393. if (result >= 0)
  394. return offset;
  395. }
  396. return uffs_result_to_dfs(uffs_get_error());
  397. }
  398. /* return the size of struct dirent*/
  399. static int dfs_uffs_getdents(
  400. struct dfs_fd *file,
  401. struct dirent *dirp,
  402. uint32_t count)
  403. {
  404. rt_uint32_t index;
  405. char *file_path;
  406. struct dirent *d;
  407. uffs_DIR *dir;
  408. struct uffs_dirent *uffs_d;
  409. dir = (uffs_DIR *)(file->data);
  410. RT_ASSERT(dir != RT_NULL);
  411. /* round count, count is always 1 */
  412. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  413. if (count == 0) return -EINVAL;
  414. /* allocate file name */
  415. file_path = rt_malloc(FILE_PATH_MAX);
  416. if (file_path == RT_NULL)
  417. return -ENOMEM;
  418. index = 0;
  419. /* usually, the while loop should only be looped only once! */
  420. while (1)
  421. {
  422. struct uffs_stat s;
  423. d = dirp + index;
  424. uffs_d = uffs_readdir(dir);
  425. if (uffs_d == RT_NULL)
  426. {
  427. rt_free(file_path);
  428. return (uffs_result_to_dfs(uffs_get_error()));
  429. }
  430. if (file->path[0] == '/' && !(file->path[1] == 0))
  431. rt_snprintf(file_path, FILE_PATH_MAX, "%s/%s", file->path, uffs_d->d_name);
  432. else
  433. rt_strncpy(file_path, uffs_d->d_name, FILE_PATH_MAX);
  434. uffs_stat(file_path, &s);
  435. switch (s.st_mode & US_IFMT) /* file type mark */
  436. {
  437. case US_IFREG: /* directory */
  438. d->d_type = DT_REG;
  439. break;
  440. case US_IFDIR: /* regular file */
  441. d->d_type = DT_DIR;
  442. break;
  443. case US_IFLNK: /* symbolic link */
  444. case US_IREAD: /* read permission */
  445. case US_IWRITE:/* write permission */
  446. default:
  447. d->d_type = DT_UNKNOWN;
  448. break;
  449. }
  450. /* write the rest args of struct dirent* dirp */
  451. d->d_namlen = rt_strlen(uffs_d->d_name);
  452. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  453. rt_strncpy(d->d_name, uffs_d->d_name, rt_strlen(uffs_d->d_name) + 1);
  454. index ++;
  455. if (index * sizeof(struct dirent) >= count)
  456. break;
  457. }
  458. /* free file name buf */
  459. rt_free(file_path);
  460. if (index == 0)
  461. return uffs_result_to_dfs(uffs_get_error());
  462. file->pos += index * sizeof(struct dirent);
  463. return index * sizeof(struct dirent);
  464. }
  465. static int dfs_uffs_unlink(struct dfs_filesystem *fs, const char *path)
  466. {
  467. int result;
  468. struct uffs_stat s;
  469. /* judge file type, dir is to be delete by uffs_rmdir, others by uffs_remove */
  470. if (uffs_lstat(path, &s) < 0)
  471. {
  472. return uffs_result_to_dfs(uffs_get_error());
  473. }
  474. switch (s.st_mode & US_IFMT)
  475. {
  476. case US_IFREG:
  477. result = uffs_remove(path);
  478. break;
  479. case US_IFDIR:
  480. result = uffs_rmdir(path);
  481. break;
  482. default:
  483. /* unknown file type */
  484. return -1;
  485. }
  486. if (result < 0)
  487. return uffs_result_to_dfs(uffs_get_error());
  488. return 0;
  489. }
  490. static int dfs_uffs_rename(
  491. struct dfs_filesystem *fs,
  492. const char *oldpath,
  493. const char *newpath)
  494. {
  495. int result;
  496. result = uffs_rename(oldpath, newpath);
  497. if (result < 0)
  498. return uffs_result_to_dfs(uffs_get_error());
  499. return 0;
  500. }
  501. static int dfs_uffs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  502. {
  503. int result;
  504. struct uffs_stat s;
  505. result = uffs_stat(path, &s);
  506. if (result < 0)
  507. return uffs_result_to_dfs(uffs_get_error());
  508. /* convert uffs stat to dfs stat structure */
  509. /* FIXME, these field may not be the same */
  510. st->st_dev = 0;
  511. st->st_mode = s.st_mode;
  512. st->st_size = s.st_size;
  513. st->st_mtime = s.st_mtime;
  514. return 0;
  515. }
  516. static const struct dfs_file_ops dfs_uffs_fops =
  517. {
  518. dfs_uffs_open,
  519. dfs_uffs_close,
  520. dfs_uffs_ioctl,
  521. dfs_uffs_read,
  522. dfs_uffs_write,
  523. dfs_uffs_flush,
  524. dfs_uffs_seek,
  525. dfs_uffs_getdents,
  526. };
  527. static const struct dfs_filesystem_ops dfs_uffs_ops =
  528. {
  529. "uffs", /* file system type: uffs */
  530. #if RTTHREAD_VERSION >= 10100
  531. DFS_FS_FLAG_FULLPATH,
  532. #else
  533. #error "uffs can only work with rtthread whose version should >= 1.01\n"
  534. #endif
  535. &dfs_uffs_fops,
  536. dfs_uffs_mount,
  537. dfs_uffs_unmount,
  538. dfs_uffs_mkfs,
  539. dfs_uffs_statfs,
  540. dfs_uffs_unlink,
  541. dfs_uffs_stat,
  542. dfs_uffs_rename,
  543. };
  544. int dfs_uffs_init(void)
  545. {
  546. /* register uffs file system */
  547. dfs_register(&dfs_uffs_ops);
  548. if (uffs_InitObjectBuf() == U_SUCC)
  549. {
  550. if (uffs_DirEntryBufInit() == U_SUCC)
  551. {
  552. uffs_InitGlobalFsLock();
  553. return RT_EOK;
  554. }
  555. }
  556. return -RT_ERROR;
  557. }
  558. INIT_COMPONENT_EXPORT(dfs_uffs_init);