dfs_uffs.c 17 KB

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