yaffs_uboot_glue.c 9.0 KB

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