yaffs_uboot_glue.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. 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 = lldiv(mtd->size, mtd->erasesize - 1);
  155. if (end_block < start_block) {
  156. printf("Bad start/end\n");
  157. goto err;
  158. }
  159. chip = mtd_to_nand(mtd);
  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 = nand_mtd_to_devnum(dev->driver_context);
  219. printf("%-10s %5d 0x%05x 0x%05x %s",
  220. dev->param.name, flash_dev,
  221. dev->param.start_block, dev->param.end_block,
  222. dev->param.inband_tags ? "using inband tags, " : "");
  223. free_space = yaffs_freespace(dev->param.name);
  224. if (free_space < 0)
  225. printf("not mounted\n");
  226. else
  227. printf("free 0x%x\n", free_space);
  228. }
  229. }
  230. void make_a_file(char *yaffsName, char bval, int sizeOfFile)
  231. {
  232. int outh;
  233. int i;
  234. unsigned char buffer[100];
  235. outh = yaffs_open(yaffsName,
  236. O_CREAT | O_RDWR | O_TRUNC,
  237. S_IREAD | S_IWRITE);
  238. if (outh < 0) {
  239. printf("Error opening file: %d. %s\n", outh, yaffs_error_str());
  240. return;
  241. }
  242. memset(buffer, bval, 100);
  243. do {
  244. i = sizeOfFile;
  245. if (i > 100)
  246. i = 100;
  247. sizeOfFile -= i;
  248. yaffs_write(outh, buffer, i);
  249. } while (sizeOfFile > 0);
  250. yaffs_close(outh);
  251. }
  252. void read_a_file(char *fn)
  253. {
  254. int h;
  255. int i = 0;
  256. unsigned char b;
  257. h = yaffs_open(fn, O_RDWR, 0);
  258. if (h < 0) {
  259. printf("File not found\n");
  260. return;
  261. }
  262. while (yaffs_read(h, &b, 1) > 0) {
  263. printf("%02x ", b);
  264. i++;
  265. if (i > 32) {
  266. printf("\n");
  267. i = 0;
  268. }
  269. }
  270. printf("\n");
  271. yaffs_close(h);
  272. }
  273. void cmd_yaffs_mount(char *mp)
  274. {
  275. int retval = yaffs_mount(mp);
  276. if (retval < 0)
  277. printf("Error mounting %s, return value: %d, %s\n", mp,
  278. yaffsfs_GetError(), yaffs_error_str());
  279. }
  280. void cmd_yaffs_umount(char *mp)
  281. {
  282. if (yaffs_unmount(mp) == -1)
  283. printf("Error umounting %s, return value: %d, %s\n", mp,
  284. yaffsfs_GetError(), yaffs_error_str());
  285. }
  286. void cmd_yaffs_write_file(char *yaffsName, char bval, int sizeOfFile)
  287. {
  288. make_a_file(yaffsName, bval, sizeOfFile);
  289. }
  290. void cmd_yaffs_read_file(char *fn)
  291. {
  292. read_a_file(fn);
  293. }
  294. void cmd_yaffs_mread_file(char *fn, char *addr)
  295. {
  296. int h;
  297. struct yaffs_stat s;
  298. yaffs_stat(fn, &s);
  299. printf("Copy %s to 0x%p... ", fn, addr);
  300. h = yaffs_open(fn, O_RDWR, 0);
  301. if (h < 0) {
  302. printf("File not found\n");
  303. return;
  304. }
  305. yaffs_read(h, addr, (int)s.st_size);
  306. printf("\t[DONE]\n");
  307. yaffs_close(h);
  308. }
  309. void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
  310. {
  311. int outh;
  312. outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  313. if (outh < 0)
  314. printf("Error opening file: %d, %s\n", outh, yaffs_error_str());
  315. yaffs_write(outh, addr, size);
  316. yaffs_close(outh);
  317. }
  318. void cmd_yaffs_ls(const char *mountpt, int longlist)
  319. {
  320. int i;
  321. yaffs_DIR *d;
  322. struct yaffs_dirent *de;
  323. struct yaffs_stat stat;
  324. char tempstr[255];
  325. d = yaffs_opendir(mountpt);
  326. if (!d) {
  327. printf("opendir failed, %s\n", yaffs_error_str());
  328. return;
  329. }
  330. for (i = 0; (de = yaffs_readdir(d)) != NULL; i++) {
  331. if (longlist) {
  332. sprintf(tempstr, "%s/%s", mountpt, de->d_name);
  333. yaffs_lstat(tempstr, &stat);
  334. printf("%-25s\t%7ld",
  335. de->d_name,
  336. (long)stat.st_size);
  337. printf(" %5d %s\n",
  338. stat.st_ino,
  339. yaffs_file_type_str(&stat));
  340. } else {
  341. printf("%s\n", de->d_name);
  342. }
  343. }
  344. yaffs_closedir(d);
  345. }
  346. void cmd_yaffs_mkdir(const char *dir)
  347. {
  348. int retval = yaffs_mkdir(dir, 0);
  349. if (retval < 0)
  350. printf("yaffs_mkdir returning error: %d, %s\n",
  351. retval, yaffs_error_str());
  352. }
  353. void cmd_yaffs_rmdir(const char *dir)
  354. {
  355. int retval = yaffs_rmdir(dir);
  356. if (retval < 0)
  357. printf("yaffs_rmdir returning error: %d, %s\n",
  358. retval, yaffs_error_str());
  359. }
  360. void cmd_yaffs_rm(const char *path)
  361. {
  362. int retval = yaffs_unlink(path);
  363. if (retval < 0)
  364. printf("yaffs_unlink returning error: %d, %s\n",
  365. retval, yaffs_error_str());
  366. }
  367. void cmd_yaffs_mv(const char *oldPath, const char *newPath)
  368. {
  369. int retval = yaffs_rename(newPath, oldPath);
  370. if (retval < 0)
  371. printf("yaffs_unlink returning error: %d, %s\n",
  372. retval, yaffs_error_str());
  373. }