2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
15 * This provides a YAFFS nand emulation on a file for emulating 2kB pages.
16 * This is only intended as test code to test persistence etc.
19 const char *yaffs_flashif2_c_version = "$Id: yaffs_fileem2k.c,v 1.24 2010-02-18 01:18:04 charles Exp $";
23 #include "yaffs_trace.h"
25 #include "yaffs_flashif2.h"
26 #include "yaffs_guts.h"
27 #include "yaffs_fileem2k.h"
28 #include "yaffs_packedtags2.h"
29 #include "yaffs_tagsvalidity.h"
32 #include <sys/types.h>
39 #define REPORT_ERROR 0
43 u8 data[PAGE_SIZE]; // Data + spare
48 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
54 #define MAX_HANDLES 20
55 #define BLOCKS_PER_HANDLE (32*8)
59 int handle[MAX_HANDLES];
63 static yflash_Device filedisk;
65 int yaffs_test_partial_write = 0;
67 extern int random_seed;
68 extern int simulate_power_failure;
69 static int remaining_ops;
70 static int nops_so_far;
75 static void yflash2_MaybePowerFail(unsigned int nand_chunk, int failPoint)
82 if(simulate_power_failure &&
84 printf("Simulated power failure after %d operations\n",nops_so_far);
85 printf(" power failed on nand_chunk %d, at fail point %d\n",
86 nand_chunk, failPoint);
95 static u8 localBuffer[PAGE_SIZE];
97 static char *NToName(char *buf,int n)
99 sprintf(buf,"emfile-2k-%d",n);
103 static char dummyBuffer[BLOCK_SIZE];
105 static int GetBlockFileHandle(int n)
115 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
117 fSize = lseek(h,0,SEEK_END);
118 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
119 if(fSize < requiredSize){
120 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
121 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
131 static int CheckInit(void)
133 static int initialised = 0;
148 remaining_ops = (rand() % 1000) * 5;
149 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
153 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
155 for(i = 0; i < MAX_HANDLES; i++)
156 filedisk.handle[i] = -1;
158 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
159 filedisk.handle[i] = GetBlockFileHandle(i);
166 int yflash2_GetNumberOfBlocks(void)
170 return filedisk.nBlocks;
173 int yflash2_WriteChunkWithTagsToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
182 T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %p tags %p" TENDSTR),nand_chunk, data, tags));
187 if(dev->param.inband_tags){
189 struct yaffs_packed_tags2_tags_only * pt2tp;
190 pt2tp = (struct yaffs_packed_tags2_tags_only *)&data[dev->data_bytes_per_chunk];
191 yaffs_pack_tags2_tags_only(pt2tp,tags);
193 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
194 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
196 lseek(h,pos,SEEK_SET);
197 written = write(h,data,dev->param.total_bytes_per_chunk);
200 if(yaffs_test_partial_write){
205 if(written != dev->param.total_bytes_per_chunk) return YAFFS_FAIL;
211 /* First do a write of a partial page */
217 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
218 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
221 memcpy(localBuffer,data, dev->data_bytes_per_chunk);
223 n_partials = rand()%20;
225 for(i = 0; i < n_partials; i++){
226 bpos = rand() % dev->data_bytes_per_chunk;
228 localBuffer[bpos] |= (1 << (rand() & 7));
231 if(REPORT_ERROR && memcmp(localBuffer,data,dev->data_bytes_per_chunk))
232 printf("nand simulator: data does not match\n");
234 lseek(h,pos,SEEK_SET);
235 written = write(h,localBuffer,dev->data_bytes_per_chunk);
237 if(yaffs_test_partial_write){
243 if(written != dev->data_bytes_per_chunk) return YAFFS_FAIL;
245 // yflash2_MaybePowerFail(nand_chunk,1);
249 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
250 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
252 lseek(h,pos,SEEK_SET);
254 if( 0 && dev->param.is_yaffs2)
257 written = write(h,tags,sizeof(struct yaffs_ext_tags));
258 if(written != sizeof(struct yaffs_ext_tags)) return YAFFS_FAIL;
262 struct yaffs_packed_tags2 pt;
263 yaffs_pack_tags2(&pt,tags, !dev->param.no_tags_ecc);
264 u8 * ptab = (u8 *)&pt;
266 nRead = read(h,localBuffer,sizeof(pt));
267 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
268 if(localBuffer[i] != 0xFF){
269 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
270 nand_chunk,i,localBuffer[i]);
275 for(i = 0; i < sizeof(pt); i++)
276 localBuffer[i] &= ptab[i];
278 n_partials = rand()% sizeof(pt);
280 for(i = 0; i < n_partials; i++){
281 bpos = rand() % sizeof(pt);
283 localBuffer[bpos] |= (1 << (rand() & 7));
286 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
287 printf("nand sim: tags corruption\n");
289 lseek(h,pos,SEEK_SET);
291 written = write(h,localBuffer,sizeof(pt));
292 if(written != sizeof(pt)) return YAFFS_FAIL;
296 //yflash2_MaybePowerFail(nand_chunk,2);
298 /* Next do the whole write */
301 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
302 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
305 memset(localBuffer,0xFF, PAGE_SIZE);
306 for(i = 0; i < dev->data_bytes_per_chunk; i++){
307 localBuffer[i] &= data[i];
310 if(REPORT_ERROR && memcmp(localBuffer,data,dev->data_bytes_per_chunk))
311 printf("nand simulator: data does not match\n");
313 lseek(h,pos,SEEK_SET);
314 written = write(h,localBuffer,dev->data_bytes_per_chunk);
316 if(yaffs_test_partial_write){
322 if(written != dev->data_bytes_per_chunk) return YAFFS_FAIL;
327 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
328 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
330 lseek(h,pos,SEEK_SET);
332 if( 0 && dev->param.is_yaffs2)
335 written = write(h,tags,sizeof(struct yaffs_ext_tags));
336 if(written != sizeof(struct yaffs_ext_tags)) return YAFFS_FAIL;
340 struct yaffs_packed_tags2 pt;
341 yaffs_pack_tags2(&pt,tags,!dev->param.no_tags_ecc);
342 u8 * ptab = (u8 *)&pt;
344 nRead = read(h,localBuffer,sizeof(pt));
345 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
346 if(localBuffer[i] != 0xFF){
347 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
348 nand_chunk,i,localBuffer[i]);
353 for(i = 0; i < sizeof(pt); i++)
354 localBuffer[i] &= ptab[i];
356 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
357 printf("nand sim: tags corruption\n");
359 lseek(h,pos,SEEK_SET);
361 written = write(h,localBuffer,sizeof(pt));
362 if(written != sizeof(pt)) return YAFFS_FAIL;
366 yflash2_MaybePowerFail(nand_chunk,3);
373 int yaffs_check_all_ff(const u8 *ptr, int n)
378 if(*ptr!=0xFF) return 0;
385 static int fail300 = 1;
386 static int fail320 = 1;
388 static int failRead10 = 2;
390 int yflash2_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
396 int retval = YAFFS_OK;
399 T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %p tags %p" TENDSTR),nand_chunk, data, tags));
406 if(dev->param.inband_tags){
407 /* Got to suck the tags out of the data area */
410 data = yaffs_get_temp_buffer(dev,__LINE__);
414 struct yaffs_packed_tags2_tags_only * pt2tp;
415 pt2tp = (struct yaffs_packed_tags2_tags_only *)&data[dev->data_bytes_per_chunk];
418 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
419 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
421 lseek(h,pos,SEEK_SET);
423 nRead = read(h, data,dev->param.total_bytes_per_chunk);
425 yaffs_unpack_tags2_tags_only(tags,pt2tp);
427 if(nread != dev->param.total_bytes_per_chunk)
431 yaffs_release_temp_buffer(dev,data,__LINE__);
442 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
443 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
444 lseek(h,pos,SEEK_SET);
445 nread = read(h,data,dev->data_bytes_per_chunk);
448 if(nread != dev->data_bytes_per_chunk)
454 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
455 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
456 lseek(h,pos,SEEK_SET);
458 if(0 && dev->param.is_yaffs2)
460 nread= read(h,tags,sizeof(struct yaffs_ext_tags));
461 if(nread != sizeof(struct yaffs_ext_tags))
463 if(yaffs_check_all_ff((u8 *)tags,sizeof(struct yaffs_ext_tags)))
465 yaffs_init_tags(tags);
469 tags->chunk_used = 1;
474 struct yaffs_packed_tags2 pt;
475 nread= read(h,&pt,sizeof(pt));
476 yaffs_unpack_tags2(tags,&pt, !dev->param.no_tags_ecc);
477 #ifdef SIMULATE_FAILURES
478 if((nand_chunk >> 6) == 100) {
479 if(fail300 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR){
480 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
485 if((nand_chunk >> 6) == 110) {
486 if(fail320 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR){
487 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
492 if(failRead10>0 && nand_chunk == 10){
497 if(nread != sizeof(pt))
510 int yflash2_MarkNANDBlockBad(struct yaffs_dev *dev, int block_no)
515 struct yaffs_packed_tags2 pt;
519 memset(&pt,0,sizeof(pt));
520 h = filedisk.handle[(block_no / ( BLOCKS_PER_HANDLE))];
521 lseek(h,((block_no % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
522 written = write(h,&pt,sizeof(pt));
524 if(written != sizeof(pt)) return YAFFS_FAIL;
531 int yflash2_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
539 //printf("erase block %d\n",blockNumber);
541 if(blockNumber == 320)
544 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
546 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
559 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
560 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE,SEEK_SET);
561 for(i = 0; i < dev->param.chunks_per_block; i++)
563 write(h,pg,PAGE_SIZE);
565 pos = lseek(h, 0,SEEK_CUR);
572 int yflash2_InitialiseNAND(struct yaffs_dev *dev)
582 int yflash2_QueryNANDBlock(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, u32 *seq_number)
584 struct yaffs_ext_tags tags;
589 chunkNo = block_no * dev->param.chunks_per_block;
591 yflash2_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
594 *state = YAFFS_BLOCK_STATE_DEAD;
596 else if(!tags.chunk_used)
598 *state = YAFFS_BLOCK_STATE_EMPTY;
600 else if(tags.chunk_used)
602 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
603 *seq_number = tags.seq_number;