yaffs_uboot_glue.c 9.0 KB

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