dfs_uffs.c 17 KB

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