yaffscfg.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 file is intended to be modified to your requirements.
  17. * There is no need to redistribute this file.
  18. */
  19. /* XXX U-BOOT XXX */
  20. #include <common.h>
  21. #include <config.h>
  22. #include "nand.h"
  23. #include "yaffscfg.h"
  24. #include "yaffsfs.h"
  25. #include "yaffs_packedtags2.h"
  26. #include "yaffs_mtdif.h"
  27. #include "yaffs_mtdif2.h"
  28. #if 0
  29. #include <errno.h>
  30. #else
  31. #include "malloc.h"
  32. #endif
  33. unsigned yaffs_traceMask = 0x0; /* Disable logging */
  34. static int yaffs_errno = 0;
  35. void yaffsfs_SetError(int err)
  36. {
  37. //Do whatever to set error
  38. yaffs_errno = err;
  39. }
  40. int yaffsfs_GetError(void)
  41. {
  42. return yaffs_errno;
  43. }
  44. void yaffsfs_Lock(void)
  45. {
  46. }
  47. void yaffsfs_Unlock(void)
  48. {
  49. }
  50. __u32 yaffsfs_CurrentTime(void)
  51. {
  52. return 0;
  53. }
  54. void *yaffs_malloc(size_t size)
  55. {
  56. return malloc(size);
  57. }
  58. void yaffs_free(void *ptr)
  59. {
  60. free(ptr);
  61. }
  62. void yaffsfs_LocalInitialisation(void)
  63. {
  64. // Define locking semaphore.
  65. }
  66. // Configuration for:
  67. // /ram 2MB ramdisk
  68. // /boot 2MB boot disk (flash)
  69. // /flash 14MB flash disk (flash)
  70. // NB Though /boot and /flash occupy the same physical device they
  71. // are still disticnt "yaffs_Devices. You may think of these as "partitions"
  72. // using non-overlapping areas in the same device.
  73. //
  74. #include "yaffs_ramdisk.h"
  75. #include "yaffs_flashif.h"
  76. static int isMounted = 0;
  77. #define MOUNT_POINT "/flash"
  78. extern nand_info_t nand_info[];
  79. /* XXX U-BOOT XXX */
  80. #if 0
  81. static yaffs_Device ramDev;
  82. static yaffs_Device bootDev;
  83. static yaffs_Device flashDev;
  84. #endif
  85. static yaffsfs_DeviceConfiguration yaffsfs_config[] = {
  86. /* XXX U-BOOT XXX */
  87. #if 0
  88. { "/ram", &ramDev},
  89. { "/boot", &bootDev},
  90. { "/flash", &flashDev},
  91. #else
  92. { MOUNT_POINT, 0},
  93. #endif
  94. {(void *)0,(void *)0}
  95. };
  96. int yaffs_StartUp(void)
  97. {
  98. struct mtd_info *mtd = &nand_info[0];
  99. int yaffsVersion = 2;
  100. int nBlocks;
  101. yaffs_Device *flashDev = calloc(1, sizeof(yaffs_Device));
  102. yaffsfs_config[0].dev = flashDev;
  103. /* store the mtd device for later use */
  104. flashDev->genericDevice = mtd;
  105. // Stuff to configure YAFFS
  106. // Stuff to initialise anything special (eg lock semaphore).
  107. yaffsfs_LocalInitialisation();
  108. // Set up devices
  109. /* XXX U-BOOT XXX */
  110. #if 0
  111. // /ram
  112. ramDev.nBytesPerChunk = 512;
  113. ramDev.nChunksPerBlock = 32;
  114. ramDev.nReservedBlocks = 2; // Set this smaller for RAM
  115. ramDev.startBlock = 1; // Can't use block 0
  116. ramDev.endBlock = 127; // Last block in 2MB.
  117. ramDev.useNANDECC = 1;
  118. ramDev.nShortOpCaches = 0; // Disable caching on this device.
  119. ramDev.genericDevice = (void *) 0; // Used to identify the device in fstat.
  120. ramDev.writeChunkWithTagsToNAND = yramdisk_WriteChunkWithTagsToNAND;
  121. ramDev.readChunkWithTagsFromNAND = yramdisk_ReadChunkWithTagsFromNAND;
  122. ramDev.eraseBlockInNAND = yramdisk_EraseBlockInNAND;
  123. ramDev.initialiseNAND = yramdisk_InitialiseNAND;
  124. // /boot
  125. bootDev.nBytesPerChunk = 612;
  126. bootDev.nChunksPerBlock = 32;
  127. bootDev.nReservedBlocks = 5;
  128. bootDev.startBlock = 1; // Can't use block 0
  129. bootDev.endBlock = 127; // Last block in 2MB.
  130. bootDev.useNANDECC = 0; // use YAFFS's ECC
  131. bootDev.nShortOpCaches = 10; // Use caches
  132. bootDev.genericDevice = (void *) 1; // Used to identify the device in fstat.
  133. bootDev.writeChunkToNAND = yflash_WriteChunkToNAND;
  134. bootDev.readChunkFromNAND = yflash_ReadChunkFromNAND;
  135. bootDev.eraseBlockInNAND = yflash_EraseBlockInNAND;
  136. bootDev.initialiseNAND = yflash_InitialiseNAND;
  137. #endif
  138. // /flash
  139. flashDev->nReservedBlocks = 5;
  140. // flashDev->nShortOpCaches = (options.no_cache) ? 0 : 10;
  141. flashDev->nShortOpCaches = 10; // Use caches
  142. flashDev->useNANDECC = 0; // do not use YAFFS's ECC
  143. if (yaffsVersion == 2)
  144. {
  145. flashDev->writeChunkWithTagsToNAND = nandmtd2_WriteChunkWithTagsToNAND;
  146. flashDev->readChunkWithTagsFromNAND = nandmtd2_ReadChunkWithTagsFromNAND;
  147. flashDev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
  148. flashDev->queryNANDBlock = nandmtd2_QueryNANDBlock;
  149. flashDev->spareBuffer = YMALLOC(mtd->oobsize);
  150. flashDev->isYaffs2 = 1;
  151. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
  152. flashDev->nDataBytesPerChunk = mtd->writesize;
  153. flashDev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
  154. #else
  155. flashDev->nDataBytesPerChunk = mtd->oobblock;
  156. flashDev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
  157. #endif
  158. nBlocks = mtd->size / mtd->erasesize;
  159. flashDev->nCheckpointReservedBlocks = 10;
  160. flashDev->startBlock = 0;
  161. flashDev->endBlock = nBlocks - 1;
  162. }
  163. else
  164. {
  165. flashDev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
  166. flashDev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
  167. flashDev->isYaffs2 = 0;
  168. nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
  169. flashDev->startBlock = 320;
  170. flashDev->endBlock = nBlocks - 1;
  171. flashDev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
  172. flashDev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
  173. }
  174. /* ... and common functions */
  175. flashDev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
  176. flashDev->initialiseNAND = nandmtd_InitialiseNAND;
  177. yaffs_initialise(yaffsfs_config);
  178. return 0;
  179. }
  180. void make_a_file(char *yaffsName,char bval,int sizeOfFile)
  181. {
  182. int outh;
  183. int i;
  184. unsigned char buffer[100];
  185. outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  186. if (outh < 0)
  187. {
  188. printf("Error opening file: %d\n", outh);
  189. return;
  190. }
  191. memset(buffer,bval,100);
  192. do{
  193. i = sizeOfFile;
  194. if(i > 100) i = 100;
  195. sizeOfFile -= i;
  196. yaffs_write(outh,buffer,i);
  197. } while (sizeOfFile > 0);
  198. yaffs_close(outh);
  199. }
  200. void read_a_file(char *fn)
  201. {
  202. int h;
  203. int i = 0;
  204. unsigned char b;
  205. h = yaffs_open(fn, O_RDWR,0);
  206. if(h<0)
  207. {
  208. printf("File not found\n");
  209. return;
  210. }
  211. while(yaffs_read(h,&b,1)> 0)
  212. {
  213. printf("%02x ",b);
  214. i++;
  215. if(i > 32)
  216. {
  217. printf("\n");
  218. i = 0;;
  219. }
  220. }
  221. printf("\n");
  222. yaffs_close(h);
  223. }
  224. void cmd_yaffs_mount(char *mp)
  225. {
  226. yaffs_StartUp();
  227. int retval = yaffs_mount(mp);
  228. if( retval != -1)
  229. isMounted = 1;
  230. else
  231. printf("Error mounting %s, return value: %d\n", mp, yaffsfs_GetError());
  232. }
  233. static void checkMount(void)
  234. {
  235. if( !isMounted )
  236. {
  237. cmd_yaffs_mount(MOUNT_POINT);
  238. }
  239. }
  240. void cmd_yaffs_umount(char *mp)
  241. {
  242. checkMount();
  243. if( yaffs_unmount(mp) == -1)
  244. printf("Error umounting %s, return value: %d\n", mp, yaffsfs_GetError());
  245. }
  246. void cmd_yaffs_write_file(char *yaffsName,char bval,int sizeOfFile)
  247. {
  248. checkMount();
  249. make_a_file(yaffsName,bval,sizeOfFile);
  250. }
  251. void cmd_yaffs_read_file(char *fn)
  252. {
  253. checkMount();
  254. read_a_file(fn);
  255. }
  256. void cmd_yaffs_mread_file(char *fn, char *addr)
  257. {
  258. int h;
  259. struct yaffs_stat s;
  260. checkMount();
  261. yaffs_stat(fn,&s);
  262. printf ("Copy %s to 0x%p... ", fn, addr);
  263. h = yaffs_open(fn, O_RDWR,0);
  264. if(h<0)
  265. {
  266. printf("File not found\n");
  267. return;
  268. }
  269. yaffs_read(h,addr,(int)s.st_size);
  270. printf("\t[DONE]\n");
  271. yaffs_close(h);
  272. }
  273. void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
  274. {
  275. int outh;
  276. checkMount();
  277. outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  278. if (outh < 0)
  279. {
  280. printf("Error opening file: %d\n", outh);
  281. }
  282. yaffs_write(outh,addr,size);
  283. yaffs_close(outh);
  284. }
  285. void cmd_yaffs_ls(const char *mountpt, int longlist)
  286. {
  287. int i;
  288. yaffs_DIR *d;
  289. yaffs_dirent *de;
  290. struct yaffs_stat stat;
  291. char tempstr[255];
  292. checkMount();
  293. d = yaffs_opendir(mountpt);
  294. if(!d)
  295. {
  296. printf("opendir failed\n");
  297. }
  298. else
  299. {
  300. for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
  301. {
  302. if (longlist)
  303. {
  304. sprintf(tempstr, "%s/%s", mountpt, de->d_name);
  305. yaffs_stat(tempstr, &stat);
  306. printf("%-25s\t%7ld\n",de->d_name, stat.st_size);
  307. }
  308. else
  309. {
  310. printf("%s\n",de->d_name);
  311. }
  312. }
  313. }
  314. }
  315. void cmd_yaffs_mkdir(const char *dir)
  316. {
  317. checkMount();
  318. int retval = yaffs_mkdir(dir, 0);
  319. if ( retval < 0)
  320. printf("yaffs_mkdir returning error: %d\n", retval);
  321. }
  322. void cmd_yaffs_rmdir(const char *dir)
  323. {
  324. checkMount();
  325. int retval = yaffs_rmdir(dir);
  326. if ( retval < 0)
  327. printf("yaffs_rmdir returning error: %d\n", retval);
  328. }
  329. void cmd_yaffs_rm(const char *path)
  330. {
  331. checkMount();
  332. int retval = yaffs_unlink(path);
  333. if ( retval < 0)
  334. printf("yaffs_unlink returning error: %d\n", retval);
  335. }
  336. void cmd_yaffs_mv(const char *oldPath, const char *newPath)
  337. {
  338. checkMount();
  339. int retval = yaffs_rename(newPath, oldPath);
  340. if ( retval < 0)
  341. printf("yaffs_unlink returning error: %d\n", retval);
  342. }