yaffs_fileem.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * This provides a YAFFS nand emulation on a file.
  15. * This is only intended as test code to test persistence etc.
  16. */
  17. /* XXX U-BOOT XXX */
  18. #include <common.h>
  19. const char *yaffs_flashif_c_version = "$Id: yaffs_fileem.c,v 1.3 2007/02/14 01:09:06 wookey Exp $";
  20. #include "yportenv.h"
  21. #include "yaffs_flashif.h"
  22. #include "yaffs_guts.h"
  23. #include "devextras.h"
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #define SIZE_IN_MB 16
  29. #define BLOCK_SIZE (32 * 528)
  30. #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
  31. typedef struct
  32. {
  33. __u8 data[528]; // Data + spare
  34. } yflash_Page;
  35. typedef struct
  36. {
  37. yflash_Page page[32]; // The pages in the block
  38. } yflash_Block;
  39. typedef struct
  40. {
  41. int handle;
  42. int nBlocks;
  43. } yflash_Device;
  44. static yflash_Device filedisk;
  45. static int CheckInit(yaffs_Device *dev)
  46. {
  47. static int initialised = 0;
  48. int i;
  49. int fSize;
  50. int written;
  51. yflash_Page p;
  52. if(initialised)
  53. {
  54. return YAFFS_OK;
  55. }
  56. initialised = 1;
  57. filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
  58. filedisk.handle = open("yaffsemfile", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
  59. if(filedisk.handle < 0)
  60. {
  61. perror("Failed to open yaffs emulation file");
  62. return YAFFS_FAIL;
  63. }
  64. fSize = lseek(filedisk.handle,0,SEEK_END);
  65. if(fSize < SIZE_IN_MB * 1024 * 1024)
  66. {
  67. printf("Creating yaffs emulation file\n");
  68. lseek(filedisk.handle,0,SEEK_SET);
  69. memset(&p,0xff,sizeof(yflash_Page));
  70. for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
  71. {
  72. written = write(filedisk.handle,&p,sizeof(yflash_Page));
  73. if(written != sizeof(yflash_Page))
  74. {
  75. printf("Write failed\n");
  76. return YAFFS_FAIL;
  77. }
  78. }
  79. }
  80. return 1;
  81. }
  82. int yflash_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
  83. {
  84. int written;
  85. CheckInit(dev);
  86. if(data)
  87. {
  88. lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
  89. written = write(filedisk.handle,data,512);
  90. if(written != 512) return YAFFS_FAIL;
  91. }
  92. if(spare)
  93. {
  94. lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
  95. written = write(filedisk.handle,spare,16);
  96. if(written != 16) return YAFFS_FAIL;
  97. }
  98. return YAFFS_OK;
  99. }
  100. int yflash_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
  101. {
  102. int nread;
  103. CheckInit(dev);
  104. if(data)
  105. {
  106. lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
  107. nread = read(filedisk.handle,data,512);
  108. if(nread != 512) return YAFFS_FAIL;
  109. }
  110. if(spare)
  111. {
  112. lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
  113. nread= read(filedisk.handle,spare,16);
  114. if(nread != 16) return YAFFS_FAIL;
  115. }
  116. return YAFFS_OK;
  117. }
  118. int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
  119. {
  120. int i;
  121. CheckInit(dev);
  122. if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
  123. {
  124. T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
  125. return YAFFS_FAIL;
  126. }
  127. else
  128. {
  129. yflash_Page pg;
  130. memset(&pg,0xff,sizeof(yflash_Page));
  131. lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
  132. for(i = 0; i < 32; i++)
  133. {
  134. write(filedisk.handle,&pg,528);
  135. }
  136. return YAFFS_OK;
  137. }
  138. }
  139. int yflash_InitialiseNAND(yaffs_Device *dev)
  140. {
  141. dev->useNANDECC = 1; // force on useNANDECC which gets faked.
  142. // This saves us doing ECC checks.
  143. return YAFFS_OK;
  144. }