dfs_uffs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. */
  26. #include <rtthread.h>
  27. #include <dfs_fs.h>
  28. #include <dfs_def.h>
  29. #include <rtdevice.h>
  30. #include "dfs_uffs.h"
  31. #include "uffs/uffs_fd.h" /* posix file api is here */
  32. #include "uffs/uffs_mtb.h"
  33. #include "uffs/uffs_mem.h"
  34. #include "uffs/uffs_utils.h"
  35. /*
  36. * RT-Thread DFS Interface for uffs
  37. */
  38. #define UFFS_DEVICE_MAX 2 /* the max partions on a nand deivce*/
  39. #define UFFS_MOUNT_PATH_MAX 128 /* the mount point max length */
  40. #define FILE_PATH_MAX 256 /* the longest file path */
  41. struct _nand_dev
  42. {
  43. struct rt_mtd_nand_device * dev;
  44. struct uffs_StorageAttrSt storage;
  45. uffs_Device uffs_dev;
  46. uffs_MountTable mount_table;
  47. char mount_path[UFFS_MOUNT_PATH_MAX];
  48. void * data; /* when uffs use static buf, it will save ptr here */
  49. };
  50. /* make sure the following struct var had been initilased to 0! */
  51. static struct _nand_dev nand_part[UFFS_DEVICE_MAX] = {0};
  52. static int uffs_result_to_dfs(int result)
  53. {
  54. int status = -1;
  55. result = result < 0 ? -result : result;
  56. switch (result)
  57. {
  58. case UENOERR:/** no error */
  59. break;
  60. case UEACCES:/** Tried to open read-only file for writing, or files sharing mode
  61. does not allow specified operations, or given path is directory */
  62. status = -DFS_STATUS_EINVAL;
  63. break;/* no suitable */
  64. case UEEXIST: /** _O_CREAT and _O_EXCL flags specified, but filename already exists */
  65. status = -DFS_STATUS_EEXIST;
  66. break;
  67. case UEINVAL: /** Invalid oflag or pmode argument */
  68. status = -DFS_STATUS_EINVAL;
  69. break;
  70. case UEMFILE: /** No more file handles available(too many open files) */
  71. status = -1;
  72. break;
  73. case UENOENT: /** file or path not found */
  74. status = -DFS_STATUS_ENOENT;
  75. break;
  76. case UETIME: /** can't set file time */
  77. status = -1;
  78. break;
  79. case UEBADF: /** invalid file handle */
  80. status = -DFS_STATUS_EBADF;
  81. break;
  82. case UENOMEM:/** no enough memory */
  83. status = -DFS_STATUS_ENOSPC;
  84. break;
  85. case UEIOERR: /** I/O error from lower level flash operation */
  86. status = -DFS_STATUS_EIO;
  87. break;
  88. case UENOTDIR: /** Not a directory */
  89. status = -DFS_STATUS_ENOTDIR;
  90. break;
  91. case UEISDIR: /** Is a directory */
  92. status = -DFS_STATUS_EISDIR;
  93. break;
  94. case UEUNKNOWN_ERR:
  95. default:
  96. status = -1;
  97. break; /* unknown error! */
  98. }
  99. return status;
  100. }
  101. static URET _device_init(uffs_Device *dev)
  102. {
  103. dev->attr->_private = NULL; // hook nand_chip data structure to attr->_private
  104. dev->ops = (struct uffs_FlashOpsSt *)&nand_ops;
  105. return U_SUCC;
  106. }
  107. static URET _device_release(uffs_Device *dev)
  108. {
  109. return U_SUCC;
  110. }
  111. static int init_uffs_fs(
  112. struct _nand_dev * nand_part)
  113. {
  114. uffs_MountTable * mtb;
  115. struct rt_mtd_nand_device * nand;
  116. struct uffs_StorageAttrSt * flash_storage;
  117. mtb = &nand_part->mount_table;
  118. nand = nand_part->dev;
  119. flash_storage = &nand_part->storage;
  120. /* setup nand storage attributes */
  121. uffs_setup_storage(flash_storage, nand);
  122. /* register mount table */
  123. if(mtb->dev)
  124. {
  125. /* set memory allocator for uffs */
  126. #if CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR > 0
  127. uffs_MemSetupSystemAllocator(&mtb->dev->mem);
  128. #endif
  129. /* setup device init/release entry */
  130. mtb->dev->Init = _device_init;
  131. mtb->dev->Release = _device_release;
  132. mtb->dev->attr = flash_storage;
  133. uffs_RegisterMountTable(mtb);
  134. }
  135. /* mount uffs partion on nand device */
  136. return uffs_Mount(nand_part->mount_path) == U_SUCC ? 0 : -1;
  137. }
  138. static int dfs_uffs_mount(
  139. struct dfs_filesystem* fs,
  140. unsigned long rwflag,
  141. const void* data)
  142. {
  143. rt_base_t index;
  144. uffs_MountTable * mount_part;
  145. struct rt_mtd_nand_device * dev;
  146. RT_ASSERT(rt_strlen(fs->path) < (UFFS_MOUNT_PATH_MAX-1));
  147. dev = RT_MTD_NAND_DEVICE(fs->dev_id);
  148. /*1. find a empty entry in partition table */
  149. for (index = 0; index < UFFS_DEVICE_MAX ; index ++)
  150. {
  151. if (nand_part[index].dev == RT_NULL)
  152. break;
  153. }
  154. if (index == UFFS_DEVICE_MAX)
  155. return -DFS_STATUS_ENOENT;
  156. /*2. fill partition structure */
  157. nand_part[index].dev = dev;
  158. /* make a right mount path for uffs, end with '/' */
  159. rt_snprintf(nand_part[index].mount_path, UFFS_MOUNT_PATH_MAX, "%s/", fs->path);
  160. if (nand_part[index].mount_path[1] == '/')
  161. nand_part[index].mount_path[1] = 0;
  162. mount_part = &(nand_part[index].mount_table);
  163. mount_part->mount = nand_part[index].mount_path;
  164. mount_part->dev = &(nand_part[index].uffs_dev);
  165. rt_memset(mount_part->dev, 0, sizeof(uffs_Device));//in order to make uffs happy.
  166. mount_part->dev->_private = dev; /* save dev_id into uffs */
  167. mount_part->start_block = dev->block_start;
  168. mount_part->end_block = dev->block_end;
  169. /*3. mount uffs */
  170. if (init_uffs_fs(&nand_part[index]) < 0)
  171. {
  172. return uffs_result_to_dfs(uffs_get_error());
  173. }
  174. return 0;
  175. }
  176. static int dfs_uffs_unmount(struct dfs_filesystem* fs)
  177. {
  178. rt_base_t index;
  179. int result;
  180. /* find the device index and then unmount it */
  181. for (index = 0; index < UFFS_DEVICE_MAX; index++)
  182. {
  183. if (nand_part[index].dev == RT_MTD_NAND_DEVICE(fs->dev_id))
  184. {
  185. nand_part[index].dev = RT_NULL;
  186. result = uffs_UnMount(nand_part[index].mount_path);
  187. if (result != U_SUCC)
  188. break;
  189. result = uffs_UnRegisterMountTable(& nand_part[index].mount_table);
  190. return (result == U_SUCC) ? DFS_STATUS_OK : -1;
  191. }
  192. }
  193. return -DFS_STATUS_ENOENT;
  194. }
  195. static int dfs_uffs_mkfs(rt_device_t dev_id)
  196. {
  197. rt_base_t index;
  198. rt_uint32_t block;
  199. struct rt_mtd_nand_device * mtd;
  200. /*1. find the device index */
  201. for (index = 0; index < UFFS_DEVICE_MAX; index++)
  202. {
  203. if (nand_part[index].dev == (struct rt_mtd_nand_device *)dev_id)
  204. break;
  205. }
  206. if (index == UFFS_DEVICE_MAX)
  207. {
  208. /* can't find device driver */
  209. return -DFS_STATUS_ENOENT;
  210. }
  211. /*2. then unmount the partition */
  212. uffs_Mount(nand_part[index].mount_path);
  213. mtd = nand_part[index].dev;
  214. /*3. erase all blocks on the partition */
  215. block = mtd->block_start;
  216. for (; block <= mtd->block_end; block++)
  217. {
  218. rt_mtd_nand_erase_block(mtd, block);
  219. if (rt_mtd_nand_check_block(mtd, block) != RT_EOK)
  220. {
  221. rt_kprintf("found bad block %d\n", block);
  222. rt_mtd_nand_mark_badblock(mtd, block);
  223. }
  224. }
  225. /*4. remount it */
  226. if (init_uffs_fs(&nand_part[index]) < 0)
  227. {
  228. return uffs_result_to_dfs(uffs_get_error());
  229. }
  230. return DFS_STATUS_OK;
  231. }
  232. static int dfs_uffs_statfs(struct dfs_filesystem* fs,
  233. struct statfs *buf)
  234. {
  235. rt_base_t index;
  236. struct rt_mtd_nand_device * mtd = RT_MTD_NAND_DEVICE(fs->dev_id);
  237. RT_ASSERT(mtd != RT_NULL);
  238. /* find the device index */
  239. for (index = 0; index < UFFS_DEVICE_MAX; index++)
  240. {
  241. if (nand_part[index].dev == (void *)mtd)
  242. break;
  243. }
  244. if (index == UFFS_DEVICE_MAX)
  245. return -DFS_STATUS_ENOENT;
  246. buf->f_bsize = mtd->page_size;
  247. buf->f_blocks = mtd->pages_per_block*
  248. (mtd->block_end - mtd->block_start + 1);
  249. buf->f_bfree = uffs_GetDeviceFree(&nand_part[index].uffs_dev) / mtd->page_size;
  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 & DFS_O_DIRECTORY) /* operations about dir */
  259. {
  260. uffs_DIR * dir;
  261. if (oflag & DFS_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 -DFS_STATUS_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 DFS_STATUS_OK;
  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 & DFS_O_RDONLY) mode |= UO_RDONLY;
  293. if (oflag & DFS_O_WRONLY) mode |= UO_WRONLY;
  294. if (oflag & DFS_O_RDWR) mode |= UO_RDWR;
  295. /* Opens the file, if it is existing. If not, a new file is created. */
  296. if (oflag & DFS_O_CREAT) mode |= UO_CREATE;
  297. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  298. if (oflag & DFS_O_TRUNC) mode |= UO_TRUNC;
  299. /* Creates a new file. The function fails if the file is already existing. */
  300. if (oflag & DFS_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 & DFS_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 & DFS_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 -DFS_STATUS_ENOSYS;
  339. }
  340. static int dfs_uffs_read(struct dfs_fd * file, void* buf, rt_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. rt_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 -DFS_STATUS_EINVAL;
  426. /* allocate file name */
  427. file_path = rt_malloc(FILE_PATH_MAX);
  428. if (file_path == RT_NULL)
  429. return -DFS_STATUS_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 = DFS_DT_REG;
  451. break;
  452. case US_IFDIR: /* regular file */
  453. d->d_type = DFS_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 = DFS_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. struct rt_mtd_nand_device * mtd;
  518. result = uffs_stat(path, &s);
  519. if (result < 0)
  520. return uffs_result_to_dfs(uffs_get_error());
  521. /* convert uffs stat to dfs stat structure */
  522. /* FIXME, these field may not be the same */
  523. st->st_dev = 0;
  524. st->st_mode = s.st_mode;
  525. st->st_size = s.st_size;
  526. st->st_mtime = s.st_mtime;
  527. mtd = RT_MTD_NAND_DEVICE(fs->dev_id);
  528. return 0;
  529. }
  530. static const struct dfs_filesystem_operation dfs_uffs_ops =
  531. {
  532. "uffs", /* file system type: uffs */
  533. #if RTTHREAD_VERSION >= 10100
  534. DFS_FS_FLAG_FULLPATH,
  535. #else
  536. #error "uffs can only work with rtthread whose version should >= 1.01\n"
  537. #endif
  538. dfs_uffs_mount,
  539. dfs_uffs_unmount,
  540. dfs_uffs_mkfs,
  541. dfs_uffs_statfs,
  542. dfs_uffs_open,
  543. dfs_uffs_close,
  544. dfs_uffs_ioctl,
  545. dfs_uffs_read,
  546. dfs_uffs_write,
  547. dfs_uffs_flush,
  548. dfs_uffs_seek,
  549. dfs_uffs_getdents,
  550. dfs_uffs_unlink,
  551. dfs_uffs_stat,
  552. dfs_uffs_rename,
  553. };
  554. int dfs_uffs_init(void)
  555. {
  556. /* register uffs file system */
  557. dfs_register(&dfs_uffs_ops);
  558. if (uffs_InitObjectBuf() == U_SUCC)
  559. {
  560. if (uffs_DirEntryBufInit() == U_SUCC)
  561. {
  562. uffs_InitGlobalFsLock();
  563. return RT_EOK;
  564. }
  565. }
  566. return -RT_ERROR;
  567. }
  568. INIT_FS_EXPORT(dfs_uffs_init);