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. 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. mtd = get_nand_dev_by_index(flash_dev);
  142. if (!mtd) {
  143. error("\nno NAND devices available\n");
  144. return;
  145. }
  146. dev = calloc(1, sizeof(*dev));
  147. mp = strdup(_mp);
  148. if (!dev || !mp) {
  149. /* Alloc error */
  150. printf("Failed to allocate memory\n");
  151. goto err;
  152. }
  153. if (flash_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  154. printf("Flash device invalid\n");
  155. goto err;
  156. }
  157. if (end_block == 0)
  158. end_block = lldiv(mtd->size, mtd->erasesize - 1);
  159. if (end_block < start_block) {
  160. printf("Bad start/end\n");
  161. goto err;
  162. }
  163. chip = mtd_to_nand(mtd);
  164. /* Check for any conflicts */
  165. yaffs_dev_rewind();
  166. while (1) {
  167. chk = yaffs_next_dev();
  168. if (!chk)
  169. break;
  170. if (strcmp(chk->param.name, mp) == 0) {
  171. printf("Mount point name already used\n");
  172. goto err;
  173. }
  174. if (chk->driver_context == mtd &&
  175. yaffs_regions_overlap(
  176. chk->param.start_block, chk->param.end_block,
  177. start_block, end_block)) {
  178. printf("Region overlaps with partition %s\n",
  179. chk->param.name);
  180. goto err;
  181. }
  182. }
  183. /* Seems sane, so configure */
  184. memset(dev, 0, sizeof(*dev));
  185. dev->param.name = mp;
  186. dev->driver_context = mtd;
  187. dev->param.start_block = start_block;
  188. dev->param.end_block = end_block;
  189. dev->param.chunks_per_block = mtd->erasesize / mtd->writesize;
  190. dev->param.total_bytes_per_chunk = mtd->writesize;
  191. dev->param.is_yaffs2 = 1;
  192. dev->param.use_nand_ecc = 1;
  193. dev->param.n_reserved_blocks = 5;
  194. if (chip->ecc.layout->oobavail < sizeof(struct yaffs_packed_tags2))
  195. dev->param.inband_tags = 1;
  196. dev->param.n_caches = 10;
  197. dev->param.write_chunk_tags_fn = nandmtd2_write_chunk_tags;
  198. dev->param.read_chunk_tags_fn = nandmtd2_read_chunk_tags;
  199. dev->param.erase_fn = nandmtd_EraseBlockInNAND;
  200. dev->param.initialise_flash_fn = nandmtd_InitialiseNAND;
  201. dev->param.bad_block_fn = nandmtd2_MarkNANDBlockBad;
  202. dev->param.query_block_fn = nandmtd2_QueryNANDBlock;
  203. yaffs_add_device(dev);
  204. printf("Configures yaffs mount %s: dev %d start block %d, end block %d %s\n",
  205. mp, flash_dev, start_block, end_block,
  206. dev->param.inband_tags ? "using inband tags" : "");
  207. return;
  208. err:
  209. free(dev);
  210. free(mp);
  211. }
  212. void cmd_yaffs_dev_ls(void)
  213. {
  214. struct yaffs_dev *dev;
  215. int flash_dev;
  216. int free_space;
  217. yaffs_dev_rewind();
  218. while (1) {
  219. dev = yaffs_next_dev();
  220. if (!dev)
  221. return;
  222. flash_dev = nand_mtd_to_devnum(dev->driver_context);
  223. printf("%-10s %5d 0x%05x 0x%05x %s",
  224. dev->param.name, flash_dev,
  225. dev->param.start_block, dev->param.end_block,
  226. dev->param.inband_tags ? "using inband tags, " : "");
  227. free_space = yaffs_freespace(dev->param.name);
  228. if (free_space < 0)
  229. printf("not mounted\n");
  230. else
  231. printf("free 0x%x\n", free_space);
  232. }
  233. }
  234. void make_a_file(char *yaffsName, char bval, int sizeOfFile)
  235. {
  236. int outh;
  237. int i;
  238. unsigned char buffer[100];
  239. outh = yaffs_open(yaffsName,
  240. O_CREAT | O_RDWR | O_TRUNC,
  241. S_IREAD | S_IWRITE);
  242. if (outh < 0) {
  243. printf("Error opening file: %d. %s\n", outh, yaffs_error_str());
  244. return;
  245. }
  246. memset(buffer, bval, 100);
  247. do {
  248. i = sizeOfFile;
  249. if (i > 100)
  250. i = 100;
  251. sizeOfFile -= i;
  252. yaffs_write(outh, buffer, i);
  253. } while (sizeOfFile > 0);
  254. yaffs_close(outh);
  255. }
  256. void read_a_file(char *fn)
  257. {
  258. int h;
  259. int i = 0;
  260. unsigned char b;
  261. h = yaffs_open(fn, O_RDWR, 0);
  262. if (h < 0) {
  263. printf("File not found\n");
  264. return;
  265. }
  266. while (yaffs_read(h, &b, 1) > 0) {
  267. printf("%02x ", b);
  268. i++;
  269. if (i > 32) {
  270. printf("\n");
  271. i = 0;;
  272. }
  273. }
  274. printf("\n");
  275. yaffs_close(h);
  276. }
  277. void cmd_yaffs_mount(char *mp)
  278. {
  279. int retval = yaffs_mount(mp);
  280. if (retval < 0)
  281. printf("Error mounting %s, return value: %d, %s\n", mp,
  282. yaffsfs_GetError(), yaffs_error_str());
  283. }
  284. void cmd_yaffs_umount(char *mp)
  285. {
  286. if (yaffs_unmount(mp) == -1)
  287. printf("Error umounting %s, return value: %d, %s\n", mp,
  288. yaffsfs_GetError(), yaffs_error_str());
  289. }
  290. void cmd_yaffs_write_file(char *yaffsName, char bval, int sizeOfFile)
  291. {
  292. make_a_file(yaffsName, bval, sizeOfFile);
  293. }
  294. void cmd_yaffs_read_file(char *fn)
  295. {
  296. read_a_file(fn);
  297. }
  298. void cmd_yaffs_mread_file(char *fn, char *addr)
  299. {
  300. int h;
  301. struct yaffs_stat s;
  302. yaffs_stat(fn, &s);
  303. printf("Copy %s to 0x%p... ", fn, addr);
  304. h = yaffs_open(fn, O_RDWR, 0);
  305. if (h < 0) {
  306. printf("File not found\n");
  307. return;
  308. }
  309. yaffs_read(h, addr, (int)s.st_size);
  310. printf("\t[DONE]\n");
  311. yaffs_close(h);
  312. }
  313. void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
  314. {
  315. int outh;
  316. outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  317. if (outh < 0)
  318. printf("Error opening file: %d, %s\n", outh, yaffs_error_str());
  319. yaffs_write(outh, addr, size);
  320. yaffs_close(outh);
  321. }
  322. void cmd_yaffs_ls(const char *mountpt, int longlist)
  323. {
  324. int i;
  325. yaffs_DIR *d;
  326. struct yaffs_dirent *de;
  327. struct yaffs_stat stat;
  328. char tempstr[255];
  329. d = yaffs_opendir(mountpt);
  330. if (!d) {
  331. printf("opendir failed, %s\n", yaffs_error_str());
  332. return;
  333. }
  334. for (i = 0; (de = yaffs_readdir(d)) != NULL; i++) {
  335. if (longlist) {
  336. sprintf(tempstr, "%s/%s", mountpt, de->d_name);
  337. yaffs_lstat(tempstr, &stat);
  338. printf("%-25s\t%7ld",
  339. de->d_name,
  340. (long)stat.st_size);
  341. printf(" %5d %s\n",
  342. stat.st_ino,
  343. yaffs_file_type_str(&stat));
  344. } else {
  345. printf("%s\n", de->d_name);
  346. }
  347. }
  348. yaffs_closedir(d);
  349. }
  350. void cmd_yaffs_mkdir(const char *dir)
  351. {
  352. int retval = yaffs_mkdir(dir, 0);
  353. if (retval < 0)
  354. printf("yaffs_mkdir returning error: %d, %s\n",
  355. retval, yaffs_error_str());
  356. }
  357. void cmd_yaffs_rmdir(const char *dir)
  358. {
  359. int retval = yaffs_rmdir(dir);
  360. if (retval < 0)
  361. printf("yaffs_rmdir returning error: %d, %s\n",
  362. retval, yaffs_error_str());
  363. }
  364. void cmd_yaffs_rm(const char *path)
  365. {
  366. int retval = yaffs_unlink(path);
  367. if (retval < 0)
  368. printf("yaffs_unlink returning error: %d, %s\n",
  369. retval, yaffs_error_str());
  370. }
  371. void cmd_yaffs_mv(const char *oldPath, const char *newPath)
  372. {
  373. int retval = yaffs_rename(newPath, oldPath);
  374. if (retval < 0)
  375. printf("yaffs_unlink returning error: %d, %s\n",
  376. retval, yaffs_error_str());
  377. }