yaffscfg.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 = 0xFFFFFFFF;
  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. // Stuff to configure YAFFS
  104. // Stuff to initialise anything special (eg lock semaphore).
  105. yaffsfs_LocalInitialisation();
  106. // Set up devices
  107. /* XXX U-BOOT XXX */
  108. #if 0
  109. // /ram
  110. ramDev.nBytesPerChunk = 512;
  111. ramDev.nChunksPerBlock = 32;
  112. ramDev.nReservedBlocks = 2; // Set this smaller for RAM
  113. ramDev.startBlock = 1; // Can't use block 0
  114. ramDev.endBlock = 127; // Last block in 2MB.
  115. ramDev.useNANDECC = 1;
  116. ramDev.nShortOpCaches = 0; // Disable caching on this device.
  117. ramDev.genericDevice = (void *) 0; // Used to identify the device in fstat.
  118. ramDev.writeChunkWithTagsToNAND = yramdisk_WriteChunkWithTagsToNAND;
  119. ramDev.readChunkWithTagsFromNAND = yramdisk_ReadChunkWithTagsFromNAND;
  120. ramDev.eraseBlockInNAND = yramdisk_EraseBlockInNAND;
  121. ramDev.initialiseNAND = yramdisk_InitialiseNAND;
  122. // /boot
  123. bootDev.nBytesPerChunk = 612;
  124. bootDev.nChunksPerBlock = 32;
  125. bootDev.nReservedBlocks = 5;
  126. bootDev.startBlock = 1; // Can't use block 0
  127. bootDev.endBlock = 127; // Last block in 2MB.
  128. bootDev.useNANDECC = 0; // use YAFFS's ECC
  129. bootDev.nShortOpCaches = 10; // Use caches
  130. bootDev.genericDevice = (void *) 1; // Used to identify the device in fstat.
  131. bootDev.writeChunkToNAND = yflash_WriteChunkToNAND;
  132. bootDev.readChunkFromNAND = yflash_ReadChunkFromNAND;
  133. bootDev.eraseBlockInNAND = yflash_EraseBlockInNAND;
  134. bootDev.initialiseNAND = yflash_InitialiseNAND;
  135. #endif
  136. // /flash
  137. flashDev->nReservedBlocks = 5;
  138. // flashDev->nShortOpCaches = (options.no_cache) ? 0 : 10;
  139. flashDev->nShortOpCaches = 10; // Use caches
  140. flashDev->useNANDECC = 0; // do not use YAFFS's ECC
  141. if (yaffsVersion == 2)
  142. {
  143. flashDev->writeChunkWithTagsToNAND = nandmtd2_WriteChunkWithTagsToNAND;
  144. flashDev->readChunkWithTagsFromNAND = nandmtd2_ReadChunkWithTagsFromNAND;
  145. flashDev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
  146. flashDev->queryNANDBlock = nandmtd2_QueryNANDBlock;
  147. flashDev->spareBuffer = YMALLOC(mtd->oobsize);
  148. flashDev->isYaffs2 = 1;
  149. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
  150. flashDev->nDataBytesPerChunk = mtd->writesize;
  151. flashDev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
  152. #else
  153. flashDev->nDataBytesPerChunk = mtd->oobblock;
  154. flashDev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
  155. #endif
  156. nBlocks = mtd->size / mtd->erasesize;
  157. flashDev->nCheckpointReservedBlocks = 10;
  158. flashDev->startBlock = 0;
  159. flashDev->endBlock = nBlocks - 1;
  160. }
  161. else
  162. {
  163. flashDev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
  164. flashDev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
  165. flashDev->isYaffs2 = 0;
  166. nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
  167. flashDev->startBlock = 320;
  168. flashDev->endBlock = nBlocks - 1;
  169. flashDev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
  170. flashDev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
  171. }
  172. /* ... and common functions */
  173. flashDev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
  174. flashDev->initialiseNAND = nandmtd_InitialiseNAND;
  175. yaffs_initialise(yaffsfs_config);
  176. return 0;
  177. }
  178. void make_a_file(char *yaffsName,char bval,int sizeOfFile)
  179. {
  180. int outh;
  181. int i;
  182. unsigned char buffer[100];
  183. outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  184. if (outh < 0)
  185. {
  186. printf("Error opening file: %d\n", outh);
  187. return;
  188. }
  189. memset(buffer,bval,100);
  190. do{
  191. i = sizeOfFile;
  192. if(i > 100) i = 100;
  193. sizeOfFile -= i;
  194. yaffs_write(outh,buffer,i);
  195. } while (sizeOfFile > 0);
  196. yaffs_close(outh);
  197. }
  198. void read_a_file(char *fn)
  199. {
  200. int h;
  201. int i = 0;
  202. unsigned char b;
  203. h = yaffs_open(fn, O_RDWR,0);
  204. if(h<0)
  205. {
  206. printf("File not found\n");
  207. return;
  208. }
  209. while(yaffs_read(h,&b,1)> 0)
  210. {
  211. printf("%02x ",b);
  212. i++;
  213. if(i > 32)
  214. {
  215. printf("\n");
  216. i = 0;;
  217. }
  218. }
  219. printf("\n");
  220. yaffs_close(h);
  221. }
  222. void cmd_yaffs_mount(char *mp)
  223. {
  224. yaffs_StartUp();
  225. int retval = yaffs_mount(mp);
  226. if( retval != -1)
  227. isMounted = 1;
  228. else
  229. printf("Error mounting %s, return value: %d\n", mp, yaffsfs_GetError());
  230. }
  231. static void checkMount(void)
  232. {
  233. if( !isMounted )
  234. {
  235. cmd_yaffs_mount(MOUNT_POINT);
  236. }
  237. }
  238. void cmd_yaffs_umount(char *mp)
  239. {
  240. checkMount();
  241. if( yaffs_unmount(mp) == -1)
  242. printf("Error umounting %s, return value: %d\n", mp, yaffsfs_GetError());
  243. }
  244. void cmd_yaffs_write_file(char *yaffsName,char bval,int sizeOfFile)
  245. {
  246. checkMount();
  247. make_a_file(yaffsName,bval,sizeOfFile);
  248. }
  249. void cmd_yaffs_read_file(char *fn)
  250. {
  251. checkMount();
  252. read_a_file(fn);
  253. }
  254. void cmd_yaffs_mread_file(char *fn, char *addr)
  255. {
  256. int h;
  257. struct yaffs_stat s;
  258. checkMount();
  259. yaffs_stat(fn,&s);
  260. printf ("Copy %s to 0x%08x... ", fn, addr);
  261. h = yaffs_open(fn, O_RDWR,0);
  262. if(h<0)
  263. {
  264. printf("File not found\n");
  265. return;
  266. }
  267. yaffs_read(h,addr,(int)s.st_size);
  268. printf("\t[DONE]\n");
  269. yaffs_close(h);
  270. }
  271. void cmd_yaffs_mwrite_file(char *fn, char *addr, int size)
  272. {
  273. int outh;
  274. checkMount();
  275. outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
  276. if (outh < 0)
  277. {
  278. printf("Error opening file: %d\n", outh);
  279. }
  280. yaffs_write(outh,addr,size);
  281. yaffs_close(outh);
  282. }
  283. void cmd_yaffs_ls(const char *mountpt, int longlist)
  284. {
  285. int i;
  286. yaffs_DIR *d;
  287. yaffs_dirent *de;
  288. struct yaffs_stat stat;
  289. char tempstr[255];
  290. checkMount();
  291. d = yaffs_opendir(mountpt);
  292. if(!d)
  293. {
  294. printf("opendir failed\n");
  295. }
  296. else
  297. {
  298. for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
  299. {
  300. if (longlist)
  301. {
  302. sprintf(tempstr, "%s/%s", mountpt, de->d_name);
  303. yaffs_stat(tempstr, &stat);
  304. printf("%-25s\t%7d\n",de->d_name, stat.st_size);
  305. }
  306. else
  307. {
  308. printf("%s\n",de->d_name);
  309. }
  310. }
  311. }
  312. }
  313. void cmd_yaffs_mkdir(const char *dir)
  314. {
  315. checkMount();
  316. int retval = yaffs_mkdir(dir, 0);
  317. if ( retval < 0)
  318. printf("yaffs_mkdir returning error: %d\n", retval);
  319. }
  320. void cmd_yaffs_rmdir(const char *dir)
  321. {
  322. checkMount();
  323. int retval = yaffs_rmdir(dir);
  324. if ( retval < 0)
  325. printf("yaffs_rmdir returning error: %d\n", retval);
  326. }
  327. void cmd_yaffs_rm(const char *path)
  328. {
  329. checkMount();
  330. int retval = yaffs_unlink(path);
  331. if ( retval < 0)
  332. printf("yaffs_unlink returning error: %d\n", retval);
  333. }
  334. void cmd_yaffs_mv(const char *oldPath, const char *newPath)
  335. {
  336. checkMount();
  337. int retval = yaffs_rename(newPath, oldPath);
  338. if ( retval < 0)
  339. printf("yaffs_unlink returning error: %d\n", retval);
  340. }