dfs_uffs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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, const char *fs_name)
  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. RT_ASSERT(file->fnode->ref_count > 0);
  246. if (file->fnode->ref_count > 1)
  247. {
  248. if (file->fnode->type == FT_DIRECTORY
  249. && !(file->flags & O_DIRECTORY))
  250. {
  251. return -ENOENT;
  252. }
  253. file->pos = 0;
  254. return 0;
  255. }
  256. oflag = file->flags;
  257. if (oflag & O_DIRECTORY) /* operations about dir */
  258. {
  259. uffs_DIR *dir;
  260. if (oflag & O_CREAT) /* create a dir*/
  261. {
  262. if (uffs_mkdir(file->fnode->path) < 0)
  263. return uffs_result_to_dfs(uffs_get_error());
  264. }
  265. /* open dir */
  266. file_path = rt_malloc(FILE_PATH_MAX);
  267. if (file_path == RT_NULL)
  268. return -ENOMEM;
  269. if (file->fnode->path[0] == '/' && !(file->fnode->path[1] == 0))
  270. rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->fnode->path);
  271. else
  272. {
  273. file_path[0] = '/';
  274. file_path[1] = 0;
  275. }
  276. dir = uffs_opendir(file_path);
  277. if (dir == RT_NULL)
  278. {
  279. rt_free(file_path);
  280. return uffs_result_to_dfs(uffs_get_error());
  281. }
  282. /* save this pointer,will used by dfs_uffs_getdents*/
  283. file->fnode->data = dir;
  284. file->fnode->type = FT_DIRECTORY;
  285. rt_free(file_path);
  286. return RT_EOK;
  287. }
  288. /* regular file operations */
  289. /* int uffs_open(const char *name, int oflag, ...); what is this?
  290. * uffs_open can open dir!! **/
  291. mode = 0;
  292. if (oflag & O_RDONLY) mode |= UO_RDONLY;
  293. if (oflag & O_WRONLY) mode |= UO_WRONLY;
  294. if (oflag & O_RDWR) mode |= UO_RDWR;
  295. /* Opens the file, if it is existing. If not, a new file is created. */
  296. if (oflag & O_CREAT) mode |= UO_CREATE;
  297. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  298. if (oflag & O_TRUNC) mode |= UO_TRUNC;
  299. /* Creates a new file. The function fails if the file is already existing. */
  300. if (oflag & O_EXCL) mode |= UO_EXCL;
  301. fd = uffs_open(file->fnode->path, mode);
  302. if (fd < 0)
  303. {
  304. return uffs_result_to_dfs(uffs_get_error());
  305. }
  306. /* save this pointer, it will be used when calling read(), write(),
  307. * flush(), seek(), and will be free when calling close()*/
  308. file->fnode->data = (void *)fd;
  309. file->pos = uffs_seek(fd, 0, USEEK_CUR);
  310. file->fnode->size = uffs_seek(fd, 0, USEEK_END);
  311. uffs_seek(fd, file->pos, USEEK_SET);
  312. if (oflag & O_APPEND)
  313. {
  314. file->pos = uffs_seek(fd, 0, USEEK_END);
  315. }
  316. return 0;
  317. }
  318. static int dfs_uffs_close(struct dfs_fd *file)
  319. {
  320. int oflag;
  321. int fd;
  322. RT_ASSERT(file->fnode->ref_count > 0);
  323. if (file->fnode->ref_count > 1)
  324. {
  325. return 0;
  326. }
  327. oflag = file->flags;
  328. if (oflag & O_DIRECTORY)
  329. {
  330. /* operations about dir */
  331. if (uffs_closedir((uffs_DIR *)(file->fnode->data)) < 0)
  332. return uffs_result_to_dfs(uffs_get_error());
  333. return 0;
  334. }
  335. /* regular file operations */
  336. fd = (int)(file->fnode->data);
  337. if (uffs_close(fd) == 0)
  338. return 0;
  339. return uffs_result_to_dfs(uffs_get_error());
  340. }
  341. static int dfs_uffs_ioctl(struct dfs_fd *file, int cmd, void *args)
  342. {
  343. return -ENOSYS;
  344. }
  345. static int dfs_uffs_read(struct dfs_fd *file, void *buf, size_t len)
  346. {
  347. int fd;
  348. int char_read;
  349. fd = (int)(file->fnode->data);
  350. char_read = uffs_read(fd, buf, len);
  351. if (char_read < 0)
  352. return uffs_result_to_dfs(uffs_get_error());
  353. /* update position */
  354. file->pos = uffs_seek(fd, 0, USEEK_CUR);
  355. return char_read;
  356. }
  357. static int dfs_uffs_write(struct dfs_fd *file,
  358. const void *buf,
  359. size_t len)
  360. {
  361. int fd;
  362. int char_write;
  363. fd = (int)(file->fnode->data);
  364. char_write = uffs_write(fd, buf, len);
  365. if (char_write < 0)
  366. return uffs_result_to_dfs(uffs_get_error());
  367. /* update position */
  368. file->pos = uffs_seek(fd, 0, USEEK_CUR);
  369. return char_write;
  370. }
  371. static int dfs_uffs_flush(struct dfs_fd *file)
  372. {
  373. int fd;
  374. int result;
  375. fd = (int)(file->fnode->data);
  376. result = uffs_flush(fd);
  377. if (result < 0)
  378. return uffs_result_to_dfs(uffs_get_error());
  379. return 0;
  380. }
  381. int uffs_seekdir(uffs_DIR *dir, long offset)
  382. {
  383. int i = 0;
  384. while (i < offset)
  385. {
  386. if (uffs_readdir(dir) == RT_NULL)
  387. return -1;
  388. i++;
  389. }
  390. return 0;
  391. }
  392. static int dfs_uffs_seek(struct dfs_fd *file,
  393. rt_off_t offset)
  394. {
  395. int result;
  396. /* set offset as current offset */
  397. if (file->fnode->type == FT_DIRECTORY)
  398. {
  399. uffs_rewinddir((uffs_DIR *)(file->fnode->data));
  400. result = uffs_seekdir((uffs_DIR *)(file->fnode->data), offset / sizeof(struct dirent));
  401. if (result >= 0)
  402. {
  403. file->pos = offset;
  404. return offset;
  405. }
  406. }
  407. else if (file->fnode->type == FT_REGULAR)
  408. {
  409. result = uffs_seek((int)(file->fnode->data), offset, USEEK_SET);
  410. if (result >= 0)
  411. return offset;
  412. }
  413. return uffs_result_to_dfs(uffs_get_error());
  414. }
  415. /* return the size of struct dirent*/
  416. static int dfs_uffs_getdents(
  417. struct dfs_fd *file,
  418. struct dirent *dirp,
  419. uint32_t count)
  420. {
  421. rt_uint32_t index;
  422. char *file_path;
  423. struct dirent *d;
  424. uffs_DIR *dir;
  425. struct uffs_dirent *uffs_d;
  426. dir = (uffs_DIR *)(file->fnode->data);
  427. RT_ASSERT(dir != RT_NULL);
  428. /* round count, count is always 1 */
  429. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  430. if (count == 0) return -EINVAL;
  431. /* allocate file name */
  432. file_path = rt_malloc(FILE_PATH_MAX);
  433. if (file_path == RT_NULL)
  434. return -ENOMEM;
  435. index = 0;
  436. /* usually, the while loop should only be looped only once! */
  437. while (1)
  438. {
  439. struct uffs_stat s;
  440. d = dirp + index;
  441. uffs_d = uffs_readdir(dir);
  442. if (uffs_d == RT_NULL)
  443. {
  444. rt_free(file_path);
  445. return (uffs_result_to_dfs(uffs_get_error()));
  446. }
  447. if (file->fnode->path[0] == '/' && !(file->fnode->path[1] == 0))
  448. rt_snprintf(file_path, FILE_PATH_MAX, "%s/%s", file->fnode->path, uffs_d->d_name);
  449. else
  450. rt_strncpy(file_path, uffs_d->d_name, FILE_PATH_MAX);
  451. uffs_stat(file_path, &s);
  452. switch (s.st_mode & US_IFMT) /* file type mark */
  453. {
  454. case US_IFREG: /* directory */
  455. d->d_type = DT_REG;
  456. break;
  457. case US_IFDIR: /* regular file */
  458. d->d_type = DT_DIR;
  459. break;
  460. case US_IFLNK: /* symbolic link */
  461. case US_IREAD: /* read permission */
  462. case US_IWRITE:/* write permission */
  463. default:
  464. d->d_type = DT_UNKNOWN;
  465. break;
  466. }
  467. /* write the rest args of struct dirent* dirp */
  468. d->d_namlen = rt_strlen(uffs_d->d_name);
  469. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  470. rt_strncpy(d->d_name, uffs_d->d_name, rt_strlen(uffs_d->d_name) + 1);
  471. index ++;
  472. if (index * sizeof(struct dirent) >= count)
  473. break;
  474. }
  475. /* free file name buf */
  476. rt_free(file_path);
  477. if (index == 0)
  478. return uffs_result_to_dfs(uffs_get_error());
  479. file->pos += index * sizeof(struct dirent);
  480. return index * sizeof(struct dirent);
  481. }
  482. static int dfs_uffs_unlink(struct dfs_filesystem *fs, const char *path)
  483. {
  484. int result;
  485. struct uffs_stat s;
  486. /* judge file type, dir is to be delete by uffs_rmdir, others by uffs_remove */
  487. if (uffs_lstat(path, &s) < 0)
  488. {
  489. return uffs_result_to_dfs(uffs_get_error());
  490. }
  491. switch (s.st_mode & US_IFMT)
  492. {
  493. case US_IFREG:
  494. result = uffs_remove(path);
  495. break;
  496. case US_IFDIR:
  497. result = uffs_rmdir(path);
  498. break;
  499. default:
  500. /* unknown file type */
  501. return -1;
  502. }
  503. if (result < 0)
  504. return uffs_result_to_dfs(uffs_get_error());
  505. return 0;
  506. }
  507. static int dfs_uffs_rename(
  508. struct dfs_filesystem *fs,
  509. const char *oldpath,
  510. const char *newpath)
  511. {
  512. int result;
  513. result = uffs_rename(oldpath, newpath);
  514. if (result < 0)
  515. return uffs_result_to_dfs(uffs_get_error());
  516. return 0;
  517. }
  518. static int dfs_uffs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  519. {
  520. int result;
  521. struct uffs_stat s;
  522. result = uffs_stat(path, &s);
  523. if (result < 0)
  524. return uffs_result_to_dfs(uffs_get_error());
  525. /* convert uffs stat to dfs stat structure */
  526. /* FIXME, these field may not be the same */
  527. st->st_dev = 0;
  528. st->st_mode = s.st_mode;
  529. st->st_size = s.st_size;
  530. st->st_mtime = s.st_mtime;
  531. return 0;
  532. }
  533. static const struct dfs_file_ops dfs_uffs_fops =
  534. {
  535. dfs_uffs_open,
  536. dfs_uffs_close,
  537. dfs_uffs_ioctl,
  538. dfs_uffs_read,
  539. dfs_uffs_write,
  540. dfs_uffs_flush,
  541. dfs_uffs_seek,
  542. dfs_uffs_getdents,
  543. };
  544. static const struct dfs_filesystem_ops dfs_uffs_ops =
  545. {
  546. "uffs", /* file system type: uffs */
  547. #if RTTHREAD_VERSION >= 10100
  548. DFS_FS_FLAG_FULLPATH,
  549. #else
  550. #error "uffs can only work with rtthread whose version should >= 1.01\n"
  551. #endif
  552. &dfs_uffs_fops,
  553. dfs_uffs_mount,
  554. dfs_uffs_unmount,
  555. dfs_uffs_mkfs,
  556. dfs_uffs_statfs,
  557. dfs_uffs_unlink,
  558. dfs_uffs_stat,
  559. dfs_uffs_rename,
  560. };
  561. int dfs_uffs_init(void)
  562. {
  563. /* register uffs file system */
  564. dfs_register(&dfs_uffs_ops);
  565. if (uffs_InitObjectBuf() == U_SUCC)
  566. {
  567. if (uffs_DirEntryBufInit() == U_SUCC)
  568. {
  569. uffs_InitGlobalFsLock();
  570. return RT_EOK;
  571. }
  572. }
  573. return -RT_ERROR;
  574. }
  575. INIT_COMPONENT_EXPORT(dfs_uffs_init);