yaffs_uboot_glue.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2007 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. /*
  14. * yaffscfg.c The configuration for the "direct" use of yaffs.
  15. *
  16. * This is set up for u-boot.
  17. *
  18. * This version now uses the ydevconfig mechanism to set up partitions.
  19. */
  20. #include <common.h>
  21. #include <config.h>
  22. #include "nand.h"
  23. #include "yaffscfg.h"
  24. #include "yaffsfs.h"
  25. #include "yaffs_packedtags2.h"
  26. #include "yaffs_mtdif.h"
  27. #include "yaffs_mtdif2.h"
  28. #if 0
  29. #include <errno.h>
  30. #else
  31. #include "malloc.h"
  32. #endif
  33. unsigned yaffs_trace_mask = 0x0; /* Disable logging */
  34. static int yaffs_errno;
  35. void yaffs_bug_fn(const char *fn, int n)
  36. {
  37. printf("yaffs bug at %s:%d\n", fn, n);
  38. }
  39. void *yaffsfs_malloc(size_t x)
  40. {
  41. return malloc(x);
  42. }
  43. void yaffsfs_free(void *x)
  44. {
  45. free(x);
  46. }
  47. void yaffsfs_SetError(int err)
  48. {
  49. yaffs_errno = err;
  50. }
  51. int yaffsfs_GetLastError(void)
  52. {
  53. return yaffs_errno;
  54. }
  55. int yaffsfs_GetError(void)
  56. {
  57. return yaffs_errno;
  58. }
  59. void yaffsfs_Lock(void)
  60. {
  61. }
  62. void yaffsfs_Unlock(void)
  63. {
  64. }
  65. __u32 yaffsfs_CurrentTime(void)
  66. {
  67. return 0;
  68. }
  69. void *yaffs_malloc(size_t size)
  70. {
  71. return malloc(size);
  72. }
  73. void yaffs_free(void *ptr)
  74. {
  75. free(ptr);
  76. }
  77. void yaffsfs_LocalInitialisation(void)
  78. {
  79. /* No locking used */
  80. }
  81. static const char *yaffs_file_type_str(struct yaffs_stat *stat)
  82. {
  83. switch (stat->st_mode & S_IFMT) {
  84. case S_IFREG: return "regular file";
  85. case S_IFDIR: return "directory";
  86. case S_IFLNK: return "symlink";
  87. default: return "unknown";
  88. }
  89. }
  90. static const char *yaffs_error_str(void)
  91. {
  92. int error = yaffsfs_GetLastError();
  93. if (error < 0)
  94. error = -error;
  95. switch (error) {
  96. case EBUSY: return "Busy";
  97. case ENODEV: return "No such device";
  98. case EINVAL: return "Invalid parameter";
  99. case ENFILE: return "Too many open files";
  100. case EBADF: return "Bad handle";
  101. case EACCES: return "Wrong permissions";
  102. case EXDEV: return "Not on same device";
  103. case ENOENT: return "No such entry";
  104. case ENOSPC: return "Device full";
  105. case EROFS: return "Read only file system";
  106. case ERANGE: return "Range error";
  107. case ENOTEMPTY: return "Not empty";
  108. case ENAMETOOLONG: return "Name too long";
  109. case ENOMEM: return "Out of memory";
  110. case EFAULT: return "Fault";
  111. case EEXIST: return "Name exists";
  112. case ENOTDIR: return "Not a directory";
  113. case EISDIR: return "Not permitted on a directory";
  114. case ELOOP: return "Symlink loop";
  115. case 0: return "No error";
  116. default: return "Unknown error";
  117. }
  118. }
  119. extern nand_info_t nand_info[];
  120. void cmd_yaffs_tracemask(unsigned set, unsigned mask)
  121. {
  122. if (set)
  123. yaffs_trace_mask = mask;
  124. printf("yaffs trace mask: %08x\n", yaffs_trace_mask);
  125. }
  126. static int yaffs_regions_overlap(int a, int b, int x, int y)
  127. {
  128. return (a <= x && x <= b) ||
  129. (a <= y && y <= b) ||
  130. (x <= a && a <= y) ||
  131. (x <= b && b <= y);
  132. }
  133. void cmd_yaffs_devconfig(char *_mp, int flash_dev,
  134. int start_block, int end_block)
  135. {
  136. struct mtd_info *mtd = NULL;
  137. struct yaffs_dev *dev = NULL;
  138. struct yaffs_dev *chk;
  139. char *mp = NULL;
  140. struct nand_chip *chip;
  141. dev = calloc(1, sizeof(*dev));
  142. mp = strdup(_mp);
  143. mtd = &nand_info[flash_dev];
  144. if (!dev || !mp) {
  145. /* Alloc error */
  146. printf("Failed to allocate memory\n");
  147. goto err;
  148. }
  149. if (flash_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  150. printf("Flash device invalid\n");
  151. goto err;
  152. }
  153. if (end_block == 0)
  154. end_block = mtd->size / mtd->erasesize - 1;
  155. if (end_block < start_block) {
  156. printf("Bad start/end\n");
  157. goto err;
  158. }
  159. chip = mtd->priv;
  160. /* Check for any conflicts */
  161. yaffs_dev_rewind();
  162. while (1) {
  163. chk = yaffs_next_dev();
  164. if (!chk)
  165. break;
  166. if (strcmp(chk->param.name, mp) == 0) {
  167. printf("Mount point name already used\n");
  168. goto err;
  169. }
  170. if (chk->driver_context == mtd &&
  171. yaffs_regions_overlap(
  172. chk->param.start_block, chk->param.end_block,
  173. start_block, end_block)) {
  174. printf("Region overlaps with partition %s\n",
  175. chk->param.name);
  176. goto err;
  177. }
  178. }
  179. /* Seems sane, so configure */
  180. memset(dev, 0, sizeof(*dev));
  181. dev->param.name = mp;
  182. dev->driver_context = mtd;
  183. dev->param.start_block = start_block;
  184. dev->param.end_block = end_block;
  185. dev->param.chunks_per_block = mtd->erasesize / mtd->writesize;
  186. dev->param.total_bytes_per_chunk = mtd->writesize;
  187. dev->param.is_yaffs2 = 1;
  188. dev->param.use_nand_ecc = 1;
  189. dev->param.n_reserved_blocks = 5;
  190. if (chip->ecc.layout->oobavail < sizeof(struct yaffs_packed_tags2))
  191. dev->param.inband_tags = 1;
  192. dev->param.n_caches = 10;
  193. dev->param.write_chunk_tags_fn = nandmtd2_write_chunk_tags;
  194. dev->param.read_chunk_tags_fn = nandmtd2_read_chunk_tags;
  195. dev->param.erase_fn = nandmtd_EraseBlockInNAND;
  196. dev->param.initialise_flash_fn = nandmtd_InitialiseNAND;
  197. dev->param.bad_block_fn = nandmtd2_MarkNANDBlockBad;
  198. dev->param.query_block_fn = nandmtd2_QueryNANDBlock;
  199. yaffs_add_device(dev);
  200. printf("Configures yaffs mount %s: dev %d start block %d, end block %d %s\n",
  201. mp, flash_dev, start_block, end_block,
  202. dev->param.inband_tags ? "using inband tags" : "");
  203. return;
  204. err:
  205. free(dev);
  206. free(mp);
  207. }
  208. void cmd_yaffs_dev_ls(void)
  209. {
  210. struct yaffs_dev *dev;
  211. int flash_dev;
  212. int free_space;
  213. yaffs_dev_rewind();
  214. while (1) {
  215. dev = yaffs_next_dev();
  216. if (!dev)
  217. return;
  218. flash_dev =
  219. ((unsigned) dev->driver_context - (unsigned) nand_info)/
  220. sizeof(nand_info[0]);
  221. printf("%-10s %5d 0x%05x 0x%05x %s",
  222. dev->param.name, flash_dev,
  223. dev->param.start_block, dev->param.end_block,
  224. dev->param.inband_tags ? "using inband tags, " : "");
  225. free_space = yaffs_freespace(dev->param.name);
  226. if (free_space < 0)
  227. printf("not mounted\n");
  228. else
  229. printf("free 0x%x\n", free_space);
  230. }
  231. }
  232. void make_a_file(char *yaffsName, char bval, int sizeOfFile)
  233. {
  234. int outh;
  235. int i;
  236. unsigned char buffer[100];
  237. outh = yaffs_open(yaffsName,
  238. O_CREAT | O_RDWR | O_TRUNC,
  239. S_IREAD | S_IWRITE);
  240. if (outh < 0) {
  241. printf("Error opening file: %d. %s\n", outh, yaffs_error_str());
  242. return;
  243. }
  244. memset(buffer, bval, 100);
  245. do {
  246. i = sizeOfFile;
  247. if (i > 100)
  248. i = 100;
  249. sizeOfFile -= i;
  250. yaffs_write(outh, buffer, i);
  251. } while (sizeOfFile > 0);
  252. yaffs_close(outh);
  253. }
  254. void read_a_file(char *fn)
  255. {
  256. int h;
  257. int i = 0;
  258. unsigned char b;
  259. h = yaffs_open(fn, O_RDWR, 0);
  260. if (h < 0) {
  261. printf("File not found\n");
  262. return;
  263. }
  264. while (yaffs_read(h, &b, 1) > 0) {
  265. printf("%02x ", b);
  266. i++;
  267. if (i > 32) {
  268. printf("\n");
  269. i = 0;;
  270. }
  271. }
  272. printf("\n");
  273. yaffs_close(h);
  274. }
  275. void cmd_yaffs_mount(char *mp)
  276. {
  277. int retval = yaffs_mount(mp);
  278. if (retval < 0)
  279. printf("Error mounting %s, return value: %d, %s\n", mp,
  280. yaffsfs_GetError(), yaffs_error_str());
  281. }
  282. void cmd_yaffs_umount(char *mp)
  283. {
  284. if (yaffs_unmount(mp) == -1)
  285. printf("Error umounting %s, return value: %d, %s\n", mp,
  286. yaffsfs_GetError(), yaffs_error_str());
  287. }
  288. void cmd_yaffs_write_file(char *yaffsName, char bval, int sizeOfFile)
  289. {
  290. make_a_file(yaffsName, bval, sizeOfFile);
  291. }
  292. void cmd_yaffs_read_file(char *fn)
  293. {
  294. read_a_file(fn);
  295. }
  296. void cmd_yaffs_mread_file(char *fn, char *addr)
  297. {
  298. int h;
  299. struct yaffs_stat s;
  300. yaffs_stat(fn, &s);
  301. printf("Copy %s to 0x%p... ", fn, addr);
  302. h = yaffs_open(fn, O_RDWR, 0);
  303. if (h < 0) {
  304. printf("File not found\n");
  305. return;
  306. }
  307. yaffs_read(h, addr, (int)s.st_size);
  308. printf("\t[DONE]\n");
  309. yaffs_close(h);
  310. }
  311. void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
  312. {
  313. int outh;
  314. outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  315. if (outh < 0)
  316. printf("Error opening file: %d, %s\n", outh, yaffs_error_str());
  317. yaffs_write(outh, addr, size);
  318. yaffs_close(outh);
  319. }
  320. void cmd_yaffs_ls(const char *mountpt, int longlist)
  321. {
  322. int i;
  323. yaffs_DIR *d;
  324. struct yaffs_dirent *de;
  325. struct yaffs_stat stat;
  326. char tempstr[255];
  327. d = yaffs_opendir(mountpt);
  328. if (!d) {
  329. printf("opendir failed, %s\n", yaffs_error_str());
  330. return;
  331. }
  332. for (i = 0; (de = yaffs_readdir(d)) != NULL; i++) {
  333. if (longlist) {
  334. sprintf(tempstr, "%s/%s", mountpt, de->d_name);
  335. yaffs_lstat(tempstr, &stat);
  336. printf("%-25s\t%7ld",
  337. de->d_name,
  338. (long)stat.st_size);
  339. printf(" %5d %s\n",
  340. stat.st_ino,
  341. yaffs_file_type_str(&stat));
  342. } else {
  343. printf("%s\n", de->d_name);
  344. }
  345. }
  346. yaffs_closedir(d);
  347. }
  348. void cmd_yaffs_mkdir(const char *dir)
  349. {
  350. int retval = yaffs_mkdir(dir, 0);
  351. if (retval < 0)
  352. printf("yaffs_mkdir returning error: %d, %s\n",
  353. retval, yaffs_error_str());
  354. }
  355. void cmd_yaffs_rmdir(const char *dir)
  356. {
  357. int retval = yaffs_rmdir(dir);
  358. if (retval < 0)
  359. printf("yaffs_rmdir returning error: %d, %s\n",
  360. retval, yaffs_error_str());
  361. }
  362. void cmd_yaffs_rm(const char *path)
  363. {
  364. int retval = yaffs_unlink(path);
  365. if (retval < 0)
  366. printf("yaffs_unlink returning error: %d, %s\n",
  367. retval, yaffs_error_str());
  368. }
  369. void cmd_yaffs_mv(const char *oldPath, const char *newPath)
  370. {
  371. int retval = yaffs_rename(newPath, oldPath);
  372. if (retval < 0)
  373. printf("yaffs_unlink returning error: %d, %s\n",
  374. retval, yaffs_error_str());
  375. }