yaffs_fileem.c 3.7 KB

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