yaffs_ramdisk.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * yaffs_ramdisk.c: yaffs ram disk component
  15. * This provides a ram disk under yaffs.
  16. * NB this is not intended for NAND emulation.
  17. * Use this with dev->useNANDECC enabled, then ECC overheads are not required.
  18. */
  19. /* XXX U-BOOT XXX */
  20. #include <common.h>
  21. const char *yaffs_ramdisk_c_version = "$Id: yaffs_ramdisk.c,v 1.4 2007/02/14 01:09:06 wookey Exp $";
  22. #include "yportenv.h"
  23. #include "yaffs_ramdisk.h"
  24. #include "yaffs_guts.h"
  25. #include "devextras.h"
  26. #include "yaffs_packedtags1.h"
  27. #define SIZE_IN_MB 2
  28. #define BLOCK_SIZE (32 * 528)
  29. #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
  30. typedef struct
  31. {
  32. __u8 data[528]; // Data + spare
  33. } yramdisk_Page;
  34. typedef struct
  35. {
  36. yramdisk_Page page[32]; // The pages in the block
  37. } yramdisk_Block;
  38. typedef struct
  39. {
  40. yramdisk_Block **block;
  41. int nBlocks;
  42. } yramdisk_Device;
  43. static yramdisk_Device ramdisk;
  44. static int CheckInit(yaffs_Device *dev)
  45. {
  46. static int initialised = 0;
  47. int i;
  48. int fail = 0;
  49. //int nBlocks;
  50. int nAllocated = 0;
  51. if(initialised)
  52. {
  53. return YAFFS_OK;
  54. }
  55. initialised = 1;
  56. ramdisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
  57. ramdisk.block = YMALLOC(sizeof(yramdisk_Block *) * ramdisk.nBlocks);
  58. if(!ramdisk.block) return 0;
  59. for(i=0; i <ramdisk.nBlocks; i++)
  60. {
  61. ramdisk.block[i] = NULL;
  62. }
  63. for(i=0; i <ramdisk.nBlocks && !fail; i++)
  64. {
  65. if((ramdisk.block[i] = YMALLOC(sizeof(yramdisk_Block))) == 0)
  66. {
  67. fail = 1;
  68. }
  69. else
  70. {
  71. yramdisk_EraseBlockInNAND(dev,i);
  72. nAllocated++;
  73. }
  74. }
  75. if(fail)
  76. {
  77. for(i = 0; i < nAllocated; i++)
  78. {
  79. YFREE(ramdisk.block[i]);
  80. }
  81. YFREE(ramdisk.block);
  82. T(YAFFS_TRACE_ALWAYS,("Allocation failed, could only allocate %dMB of %dMB requested.\n",
  83. nAllocated/64,ramdisk.nBlocks * 528));
  84. return 0;
  85. }
  86. return 1;
  87. }
  88. int yramdisk_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
  89. {
  90. int blk;
  91. int pg;
  92. CheckInit(dev);
  93. blk = chunkInNAND/32;
  94. pg = chunkInNAND%32;
  95. if(data)
  96. {
  97. memcpy(ramdisk.block[blk]->page[pg].data,data,512);
  98. }
  99. if(tags)
  100. {
  101. yaffs_PackedTags1 pt;
  102. yaffs_PackTags1(&pt,tags);
  103. memcpy(&ramdisk.block[blk]->page[pg].data[512],&pt,sizeof(pt));
  104. }
  105. return YAFFS_OK;
  106. }
  107. int yramdisk_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
  108. {
  109. int blk;
  110. int pg;
  111. CheckInit(dev);
  112. blk = chunkInNAND/32;
  113. pg = chunkInNAND%32;
  114. if(data)
  115. {
  116. memcpy(data,ramdisk.block[blk]->page[pg].data,512);
  117. }
  118. if(tags)
  119. {
  120. yaffs_PackedTags1 pt;
  121. memcpy(&pt,&ramdisk.block[blk]->page[pg].data[512],sizeof(pt));
  122. yaffs_UnpackTags1(tags,&pt);
  123. }
  124. return YAFFS_OK;
  125. }
  126. int yramdisk_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
  127. {
  128. int blk;
  129. int pg;
  130. int i;
  131. CheckInit(dev);
  132. blk = chunkInNAND/32;
  133. pg = chunkInNAND%32;
  134. for(i = 0; i < 528; i++)
  135. {
  136. if(ramdisk.block[blk]->page[pg].data[i] != 0xFF)
  137. {
  138. return YAFFS_FAIL;
  139. }
  140. }
  141. return YAFFS_OK;
  142. }
  143. int yramdisk_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
  144. {
  145. CheckInit(dev);
  146. if(blockNumber < 0 || blockNumber >= ramdisk.nBlocks)
  147. {
  148. T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
  149. return YAFFS_FAIL;
  150. }
  151. else
  152. {
  153. memset(ramdisk.block[blockNumber],0xFF,sizeof(yramdisk_Block));
  154. return YAFFS_OK;
  155. }
  156. }
  157. int yramdisk_InitialiseNAND(yaffs_Device *dev)
  158. {
  159. //dev->useNANDECC = 1; // force on useNANDECC which gets faked.
  160. // This saves us doing ECC checks.
  161. return YAFFS_OK;
  162. }