2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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"
31 #include <sys/types.h>
38 #define REPORT_ERROR 0
42 u8 data[PAGE_SIZE]; // Data + spare
47 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
53 #define MAX_HANDLES 20
54 #define BLOCKS_PER_HANDLE (32*8)
58 int handle[MAX_HANDLES];
62 static yflash_Device filedisk;
64 int yaffs_test_partial_write = 0;
66 extern int random_seed;
67 extern int simulate_power_failure;
68 static int remaining_ops;
69 static int nops_so_far;
74 static void yflash2_MaybePowerFail(unsigned int nand_chunk, int failPoint)
81 if(simulate_power_failure &&
83 printf("Simulated power failure after %d operations\n",nops_so_far);
84 printf(" power failed on nand_chunk %d, at fail point %d\n",
85 nand_chunk, failPoint);
94 static u8 localBuffer[PAGE_SIZE];
96 static char *NToName(char *buf,int n)
98 sprintf(buf,"emfile-2k-%d",n);
102 static char dummyBuffer[BLOCK_SIZE];
104 static int GetBlockFileHandle(int n)
114 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
116 fSize = lseek(h,0,SEEK_END);
117 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
118 if(fSize < requiredSize){
119 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
120 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
130 static int CheckInit(void)
132 static int initialised = 0;
147 remaining_ops = (rand() % 1000) * 5;
148 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
152 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
154 for(i = 0; i < MAX_HANDLES; i++)
155 filedisk.handle[i] = -1;
157 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
158 filedisk.handle[i] = GetBlockFileHandle(i);
165 int yflash2_GetNumberOfBlocks(void)
169 return filedisk.nBlocks;
172 int yflash2_WriteChunkWithTagsToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
181 yaffs_trace(YAFFS_TRACE_MTD, "write chunk %d data %p tags %p",nand_chunk, data, tags);
186 if(dev->param.inband_tags){
188 struct yaffs_packed_tags2_tags_only * pt2tp;
189 pt2tp = (struct yaffs_packed_tags2_tags_only *)&data[dev->data_bytes_per_chunk];
190 yaffs_pack_tags2_tags_only(pt2tp,tags);
192 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
193 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
195 lseek(h,pos,SEEK_SET);
196 written = write(h,data,dev->param.total_bytes_per_chunk);
199 if(yaffs_test_partial_write){
204 if(written != dev->param.total_bytes_per_chunk) return YAFFS_FAIL;
210 /* First do a write of a partial page */
216 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
217 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
220 memcpy(localBuffer,data, dev->data_bytes_per_chunk);
222 n_partials = rand()%20;
224 for(i = 0; i < n_partials; i++){
225 bpos = rand() % dev->data_bytes_per_chunk;
227 localBuffer[bpos] |= (1 << (rand() & 7));
230 if(REPORT_ERROR && memcmp(localBuffer,data,dev->data_bytes_per_chunk))
231 printf("nand simulator: data does not match\n");
233 lseek(h,pos,SEEK_SET);
234 written = write(h,localBuffer,dev->data_bytes_per_chunk);
236 if(yaffs_test_partial_write){
242 if(written != dev->data_bytes_per_chunk) return YAFFS_FAIL;
244 // yflash2_MaybePowerFail(nand_chunk,1);
248 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
249 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
251 lseek(h,pos,SEEK_SET);
253 if( 0 && dev->param.is_yaffs2)
256 written = write(h,tags,sizeof(struct yaffs_ext_tags));
257 if(written != sizeof(struct yaffs_ext_tags)) return YAFFS_FAIL;
261 struct yaffs_packed_tags2 pt;
262 yaffs_pack_tags2(&pt,tags, !dev->param.no_tags_ecc);
263 u8 * ptab = (u8 *)&pt;
265 nRead = read(h,localBuffer,sizeof(pt));
266 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
267 if(localBuffer[i] != 0xFF){
268 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
269 nand_chunk,i,localBuffer[i]);
274 for(i = 0; i < sizeof(pt); i++)
275 localBuffer[i] &= ptab[i];
277 n_partials = rand()% sizeof(pt);
279 for(i = 0; i < n_partials; i++){
280 bpos = rand() % sizeof(pt);
282 localBuffer[bpos] |= (1 << (rand() & 7));
285 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
286 printf("nand sim: tags corruption\n");
288 lseek(h,pos,SEEK_SET);
290 written = write(h,localBuffer,sizeof(pt));
291 if(written != sizeof(pt)) return YAFFS_FAIL;
295 //yflash2_MaybePowerFail(nand_chunk,2);
297 /* Next do the whole write */
300 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
301 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
304 memset(localBuffer,0xFF, PAGE_SIZE);
305 for(i = 0; i < dev->data_bytes_per_chunk; i++){
306 localBuffer[i] &= data[i];
309 if(REPORT_ERROR && memcmp(localBuffer,data,dev->data_bytes_per_chunk))
310 printf("nand simulator: data does not match\n");
312 lseek(h,pos,SEEK_SET);
313 written = write(h,localBuffer,dev->data_bytes_per_chunk);
315 if(yaffs_test_partial_write){
321 if(written != dev->data_bytes_per_chunk) return YAFFS_FAIL;
326 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
327 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
329 lseek(h,pos,SEEK_SET);
331 if( 0 && dev->param.is_yaffs2)
334 written = write(h,tags,sizeof(struct yaffs_ext_tags));
335 if(written != sizeof(struct yaffs_ext_tags)) return YAFFS_FAIL;
339 struct yaffs_packed_tags2 pt;
340 yaffs_pack_tags2(&pt,tags,!dev->param.no_tags_ecc);
341 u8 * ptab = (u8 *)&pt;
343 nRead = read(h,localBuffer,sizeof(pt));
344 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
345 if(localBuffer[i] != 0xFF){
346 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
347 nand_chunk,i,localBuffer[i]);
352 for(i = 0; i < sizeof(pt); i++)
353 localBuffer[i] &= ptab[i];
355 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
356 printf("nand sim: tags corruption\n");
358 lseek(h,pos,SEEK_SET);
360 written = write(h,localBuffer,sizeof(pt));
361 if(written != sizeof(pt)) return YAFFS_FAIL;
365 yflash2_MaybePowerFail(nand_chunk,3);
372 int yaffs_check_all_ff(const u8 *ptr, int n)
377 if(*ptr!=0xFF) return 0;
384 static int fail300 = 1;
385 static int fail320 = 1;
387 static int failRead10 = 2;
389 int yflash2_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
395 int retval = YAFFS_OK;
398 yaffs_trace(YAFFS_TRACE_MTD,"read chunk %d data %p tags %p",nand_chunk, data, tags);
405 if(dev->param.inband_tags){
406 /* Got to suck the tags out of the data area */
409 data = yaffs_get_temp_buffer(dev);
413 struct yaffs_packed_tags2_tags_only * pt2tp;
414 pt2tp = (struct yaffs_packed_tags2_tags_only *)&data[dev->data_bytes_per_chunk];
417 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
418 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
420 lseek(h,pos,SEEK_SET);
422 nRead = read(h, data,dev->param.total_bytes_per_chunk);
424 yaffs_unpack_tags2_tags_only(tags,pt2tp);
426 if(nread != dev->param.total_bytes_per_chunk)
430 yaffs_release_temp_buffer(dev, data);
441 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
442 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
443 lseek(h,pos,SEEK_SET);
444 nread = read(h,data,dev->data_bytes_per_chunk);
447 if(nread != dev->data_bytes_per_chunk)
453 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
454 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
455 lseek(h,pos,SEEK_SET);
457 if(0 && dev->param.is_yaffs2)
459 nread= read(h,tags,sizeof(struct yaffs_ext_tags));
460 if(nread != sizeof(struct yaffs_ext_tags))
462 if(yaffs_check_all_ff((u8 *)tags, sizeof(struct yaffs_ext_tags)))
463 memset(tags, 0, sizeof(struct yaffs_ext_tags));
465 tags->chunk_used = 1;
469 struct yaffs_packed_tags2 pt;
470 nread= read(h,&pt,sizeof(pt));
471 yaffs_unpack_tags2(tags,&pt, !dev->param.no_tags_ecc);
472 #ifdef SIMULATE_FAILURES
473 if((nand_chunk >> 6) == 100) {
474 if(fail300 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR){
475 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
480 if((nand_chunk >> 6) == 110) {
481 if(fail320 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR){
482 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
487 if(failRead10>0 && nand_chunk == 10){
492 if(nread != sizeof(pt))
505 int yflash2_MarkNANDBlockBad(struct yaffs_dev *dev, int block_no)
510 struct yaffs_packed_tags2 pt;
514 memset(&pt,0,sizeof(pt));
515 h = filedisk.handle[(block_no / ( BLOCKS_PER_HANDLE))];
516 lseek(h,((block_no % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
517 written = write(h,&pt,sizeof(pt));
519 if(written != sizeof(pt)) return YAFFS_FAIL;
526 int yflash2_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
534 //printf("erase block %d\n",blockNumber);
536 if(blockNumber == 320)
539 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
541 yaffs_trace(YAFFS_TRACE_ALWAYS,"Attempt to erase non-existant block %d",blockNumber);
554 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
555 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE,SEEK_SET);
556 for(i = 0; i < dev->param.chunks_per_block; i++)
558 write(h,pg,PAGE_SIZE);
560 pos = lseek(h, 0,SEEK_CUR);
567 int yflash2_InitialiseNAND(struct yaffs_dev *dev)
577 int yflash2_QueryNANDBlock(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, u32 *seq_number)
579 struct yaffs_ext_tags tags;
584 chunkNo = block_no * dev->param.chunks_per_block;
586 yflash2_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
589 *state = YAFFS_BLOCK_STATE_DEAD;
591 else if(!tags.chunk_used)
593 *state = YAFFS_BLOCK_STATE_EMPTY;
595 else if(tags.chunk_used)
597 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
598 *seq_number = tags.seq_number;