yaffs_uboot_glue.c 9.1 KB

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