uffs_fileem_wrap.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. This file is part of UFFS, the Ultra-low-cost Flash File System.
  3. Copyright (C) 2005-2009 Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
  4. UFFS is free software; you can redistribute it and/or modify it under
  5. the GNU Library General Public License as published by the Free Software
  6. Foundation; either version 2 of the License, or (at your option) any
  7. later version.
  8. UFFS is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. or GNU Library General Public License, as applicable, for more details.
  12. You should have received a copy of the GNU General Public License
  13. and GNU Library General Public License along with UFFS; if not, write
  14. to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. As a special exception, if other files instantiate templates or use
  17. macros or inline functions from this file, or you compile this file
  18. and link it with other works to produce a work based on this file,
  19. this file does not by itself cause the resulting work to be covered
  20. by the GNU General Public License. However the source code for this
  21. file must still be made available in accordance with section (3) of
  22. the GNU General Public License v2.
  23. This exception does not invalidate any other reasons why a work based
  24. on this file might be covered by the GNU General Public License.
  25. */
  26. /**
  27. * \file uffs_fileem_wrap.c
  28. *
  29. * \brief file emulator wrapper functions for injecting bad blocks or ECC errors.
  30. *
  31. * \author Ricky Zheng, created Nov, 2010
  32. */
  33. #include <sys/types.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include "uffs_config.h"
  38. #include "uffs/uffs_device.h"
  39. #include "uffs_fileem.h"
  40. #define PFX "femu: "
  41. #define MSGLN(msg,...) uffs_Perror(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
  42. #define MSG(msg,...) uffs_PerrorRaw(UFFS_MSG_NORMAL, msg, ## __VA_ARGS__)
  43. // #define UFFS_FEMU_SHOW_FLASH_IO
  44. #ifdef UFFS_FEMU_ENABLE_INJECTION
  45. struct uffs_FileEmuBitFlip {
  46. int block;
  47. int page;
  48. int offset;
  49. u8 mask;
  50. };
  51. /* simulate bad blocks */
  52. #define FILEEMU_STOCK_BAD_BLOCKS {5, 180} // bad block come from manufacture
  53. #define FILEEMU_ERASE_BAD_BLOCKS {100, 150} // new bad block discovered when erasing
  54. /* simulating bit flip */
  55. #define FILEEMU_WRITE_BIT_FLIP \
  56. { \
  57. {20, 2, 10, 1 << 4}, /* block 20, page 2, offset 10, bit 4 */ \
  58. {24, 4, -3, 1 << 2}, /* block 24, page 4, spare offset 3, bit 2*/ \
  59. {60, 1, 5, 1 << 3}, /* block 60, page 1, offset 5, bit 3 */ \
  60. {66, 1, 15, 1 << 7}, /* block 66, page 1, offset 300, bit 7 */ \
  61. {80, 2, 2, 1 << 1}, /* block 80, page 2, offset 2, bit 1 */ \
  62. {88, 2, 100, 1 << 5}, /* block 88, page 2, offset 100, bit 5 */ \
  63. }
  64. static int femu_InitFlash_wrap(uffs_Device *dev);
  65. static int femu_ReadPage_wrap(uffs_Device *dev, u32 block, u32 page, u8 *data, int data_len, u8 *ecc,
  66. u8 *spare, int spare_len);
  67. static int femu_ReadPageWithLayout_wrap(uffs_Device *dev, u32 block, u32 page, u8* data, int data_len, u8 *ecc,
  68. uffs_TagStore *ts, u8 *ecc_store);
  69. static int femu_WritePage_wrap(uffs_Device *dev, u32 block, u32 page,
  70. const u8 *data, int data_len, const u8 *spare, int spare_len);
  71. static int femu_WritePageWithLayout_wrap(uffs_Device *dev, u32 block, u32 page, const u8* data, int data_len, const u8 *ecc,
  72. const uffs_TagStore *ts);
  73. static int femu_EraseBlock_wrap(uffs_Device *dev, u32 blockNumber);
  74. /////////////////////////////////////////////////////////////////////////////////
  75. void femu_setup_wrapper_functions(uffs_Device *dev)
  76. {
  77. uffs_FileEmu *emu;
  78. emu = (uffs_FileEmu *)(dev->attr->_private);
  79. // setup wrap functions, for inject ECC errors, etc.
  80. memcpy(&emu->ops_orig, dev->ops, sizeof(struct uffs_FlashOpsSt));
  81. if (dev->ops->InitFlash)
  82. dev->ops->InitFlash = femu_InitFlash_wrap;
  83. if (dev->ops->EraseBlock)
  84. dev->ops->EraseBlock = femu_EraseBlock_wrap;
  85. if (dev->ops->ReadPage)
  86. dev->ops->ReadPage = femu_ReadPage_wrap;
  87. if (dev->ops->ReadPageWithLayout)
  88. dev->ops->ReadPageWithLayout = femu_ReadPageWithLayout_wrap;
  89. if (dev->ops->WritePage)
  90. dev->ops->WritePage = femu_WritePage_wrap;
  91. if (dev->ops->WritePageWithLayout)
  92. dev->ops->WritePageWithLayout = femu_WritePageWithLayout_wrap;
  93. }
  94. static int femu_InitFlash_wrap(uffs_Device *dev)
  95. {
  96. int ret;
  97. uffs_FileEmu *emu = (uffs_FileEmu *)(dev->attr->_private);
  98. #ifdef FILEEMU_STOCK_BAD_BLOCKS
  99. u32 bad_blocks[] = FILEEMU_STOCK_BAD_BLOCKS;
  100. int j;
  101. u8 x = 0;
  102. struct uffs_StorageAttrSt *attr = dev->attr;
  103. int full_page_size = attr->page_data_size + attr->spare_size;
  104. int blk_size = full_page_size * attr->pages_per_block;
  105. #endif
  106. if (emu->initCount == 0) {
  107. ret = emu->ops_orig.InitFlash(dev);
  108. #ifdef FILEEMU_STOCK_BAD_BLOCKS
  109. if (ret >= 0) {
  110. for (j = 0; j < ARRAY_SIZE(bad_blocks); j++) {
  111. if (bad_blocks[j] < dev->attr->total_blocks) {
  112. printf(" --- manufacture bad block %d ---\n", bad_blocks[j]);
  113. fseek(emu->fp, bad_blocks[j] * blk_size + attr->page_data_size + dev->attr->block_status_offs, SEEK_SET);
  114. fwrite(&x, 1, 1, emu->fp);
  115. }
  116. }
  117. }
  118. #endif
  119. }
  120. else {
  121. ret = emu->ops_orig.InitFlash(dev);
  122. }
  123. return ret;
  124. }
  125. static int femu_ReadPage_wrap(uffs_Device *dev, u32 block, u32 page, u8 *data, int data_len, u8 *ecc,
  126. u8 *spare, int spare_len)
  127. {
  128. uffs_FileEmu *emu = (uffs_FileEmu *)(dev->attr->_private);
  129. #ifdef UFFS_FEMU_SHOW_FLASH_IO
  130. if (data || spare) {
  131. MSG(PFX " Read block %d page %d", block, page);
  132. if (data)
  133. MSG(" DATA[%d]", data_len);
  134. if (spare)
  135. MSG(" SPARE[%d]", spare_len);
  136. MSG(TENDSTR);
  137. }
  138. #endif
  139. return emu->ops_orig.ReadPage(dev, block, page, data, data_len, ecc, spare, spare_len);
  140. }
  141. static int femu_ReadPageWithLayout_wrap(uffs_Device *dev, u32 block, u32 page, u8* data, int data_len, u8 *ecc,
  142. uffs_TagStore *ts, u8 *ecc_store)
  143. {
  144. uffs_FileEmu *emu = (uffs_FileEmu *)(dev->attr->_private);
  145. #ifdef UFFS_FEMU_SHOW_FLASH_IO
  146. if (data || ts) {
  147. MSG(PFX " Read block %d page %d", block, page);
  148. if (data)
  149. MSG(" DATA[%d]", data_len);
  150. if (ts)
  151. MSG(" TS");
  152. MSG(TENDSTR);
  153. }
  154. #endif
  155. return emu->ops_orig.ReadPageWithLayout(dev, block, page, data, data_len, ecc, ts, ecc_store);
  156. }
  157. ////////////////////// wraper functions ///////////////////////////
  158. static void InjectBitFlip(uffs_Device *dev, u32 block, u32 page)
  159. {
  160. #ifdef FILEEMU_WRITE_BIT_FLIP
  161. uffs_FileEmu *emu = (uffs_FileEmu *)(dev->attr->_private);
  162. struct uffs_FileEmuBitFlip flips[] = FILEEMU_WRITE_BIT_FLIP;
  163. struct uffs_FileEmuBitFlip *x;
  164. u8 buf[UFFS_MAX_PAGE_SIZE + UFFS_MAX_SPARE_SIZE];
  165. u8 *data = buf;
  166. u8 *spare = buf + dev->attr->page_data_size;
  167. int full_page_size = dev->attr->page_data_size + dev->attr->spare_size;
  168. int blk_size = full_page_size * dev->attr->pages_per_block;
  169. int page_offset = block * blk_size + full_page_size * page;
  170. int i;
  171. u8 *p;
  172. fseek(emu->fp, page_offset, SEEK_SET);
  173. fread(buf, 1, full_page_size, emu->fp);
  174. p = NULL;
  175. for (i = 0; i < ARRAY_SIZE(flips); i++) {
  176. x = &flips[i];
  177. if (x->block == block && x->page == page) {
  178. if (x->offset >= 0) {
  179. printf(" --- Inject data bit flip at block%d, page%d, offset%d, mask%d --- \n", block, page, x->offset, x->mask);
  180. p = (u8 *)(data + x->offset);
  181. }
  182. else {
  183. printf(" --- Inject spare bit flip at block%d, page%d, offset%d, mask%d --- \n", block, page, -x->offset, x->mask);
  184. p = (u8 *)(spare - x->offset);
  185. }
  186. *p = (*p & ~x->mask) | (~(*p & x->mask) & x->mask);
  187. }
  188. }
  189. if (p) {
  190. fseek(emu->fp, page_offset, SEEK_SET);
  191. fwrite(buf, 1, full_page_size, emu->fp);
  192. }
  193. #endif
  194. }
  195. static int femu_WritePage_wrap(uffs_Device *dev, u32 block, u32 page,
  196. const u8 *data, int data_len, const u8 *spare, int spare_len)
  197. {
  198. uffs_FileEmu *emu = (uffs_FileEmu *)(dev->attr->_private);
  199. int ret;
  200. #ifdef UFFS_FEMU_SHOW_FLASH_IO
  201. if (data || spare) {
  202. MSG(PFX " Write block %d page %d", block, page);
  203. if (data)
  204. MSG(" DATA[%d]", data_len);
  205. if (spare)
  206. MSG(" SPARE[%d]", spare_len);
  207. MSG(TENDSTR);
  208. }
  209. #endif
  210. ret = emu->ops_orig.WritePage(dev, block, page, data, data_len, spare, spare_len);
  211. InjectBitFlip(dev, block, page);
  212. return ret;
  213. }
  214. static int femu_WritePageWithLayout_wrap(uffs_Device *dev, u32 block, u32 page, const u8* data, int data_len, const u8 *ecc,
  215. const uffs_TagStore *ts)
  216. {
  217. uffs_FileEmu *emu = (uffs_FileEmu *)(dev->attr->_private);
  218. int ret;
  219. #ifdef UFFS_FEMU_SHOW_FLASH_IO
  220. if (data || ts) {
  221. MSG(PFX " Write block %d page %d", block, page);
  222. if (data)
  223. MSG(" DATA[%d]", data_len);
  224. if (ts)
  225. MSG(" TS");
  226. MSG(TENDSTR);
  227. }
  228. #endif
  229. ret = emu->ops_orig.WritePageWithLayout(dev, block, page, data, data_len, ecc, ts);
  230. InjectBitFlip(dev, block, page);
  231. return ret;
  232. }
  233. static int femu_EraseBlock_wrap(uffs_Device *dev, u32 blockNumber)
  234. {
  235. uffs_FileEmu *emu = (uffs_FileEmu *)(dev->attr->_private);
  236. #ifdef FILEEMU_ERASE_BAD_BLOCKS
  237. int blocks[] = FILEEMU_ERASE_BAD_BLOCKS;
  238. int i;
  239. URET ret;
  240. ret = emu->ops_orig.EraseBlock(dev, blockNumber);
  241. for (i = 0; i < ARRAY_SIZE(blocks); i++) {
  242. if (blockNumber == blocks[i]) {
  243. printf(" --- Inject bad block%d when erasing --- \n", blockNumber);
  244. ret = UFFS_FLASH_BAD_BLK;
  245. }
  246. }
  247. return ret;
  248. #else
  249. return emu->ops_orig.EraseBlock(dev, blockNumber);
  250. #endif
  251. }
  252. #endif // UFFS_FEMU_ENABLE_INJECTION
  253. /////////////////////////////////////////////////////////////////////////////////