1
0

dfs_uffs.c 15 KB

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