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.
20 #include "yaffs_trace.h"
22 #include "yaffs_flashif2.h"
23 #include "yaffs_guts.h"
24 #include "yaffs_fileem2k.h"
25 #include "yaffs_packedtags2.h"
28 #include <sys/types.h>
36 /* #define SIZE_IN_MB 128 */
38 #define PAGE_DATA_SIZE (2048)
39 #define PAGE_SPARE_SIZE (64)
40 #define PAGE_SIZE (PAGE_DATA_SIZE + PAGE_SPARE_SIZE)
41 #define PAGES_PER_BLOCK (64)
42 #define BLOCK_DATA_SIZE (PAGE_DATA_SIZE * PAGES_PER_BLOCK)
43 #define BLOCK_SIZE (PAGES_PER_BLOCK * (PAGE_SIZE))
44 #define BLOCKS_PER_MB ((1024*1024)/BLOCK_DATA_SIZE)
45 #define SIZE_IN_BLOCKS (BLOCKS_PER_MB * SIZE_IN_MB)
49 #define SIZE_IN_MB 128
50 #define PAGE_DATA_SIZE (512)
51 #define SPARE_SIZE (16)
52 #define PAGE_SIZE (PAGE_DATA_SIZE + SPARE_SIZE)
53 #define PAGES_PER_BLOCK (32)
54 #define BLOCK_DATA_SIZE (PAGE_SIZE * PAGES_PER_BLOCK)
55 #define BLOCK_SIZE (PAGES_PER_BLOCK * (PAGE_SIZE))
56 #define BLOCKS_PER_MB ((1024*1024)/BLOCK_DATA_SIZE)
57 #define SIZE_IN_BLOCKS (BLOCKS_PER_MB * SIZE_IN_MB)
61 #define REPORT_ERROR 0
65 u8 data[PAGE_SIZE]; // Data + spare
70 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
76 #define MAX_HANDLES 20
77 #define BLOCKS_PER_HANDLE (32*8)
81 int handle[MAX_HANDLES];
85 static yflash_Device filedisk;
87 int yaffs_test_partial_write = 0;
89 extern int random_seed;
90 extern int simulate_power_failure;
91 static int remaining_ops;
92 static int nops_so_far;
97 static void yflash2_MaybePowerFail(unsigned int nand_chunk, int failPoint)
104 if(simulate_power_failure &&
106 printf("Simulated power failure after %d operations\n",nops_so_far);
107 printf(" power failed on nand_chunk %d, at fail point %d\n",
108 nand_chunk, failPoint);
117 static u8 localBuffer[PAGE_SIZE];
119 static char *NToName(char *buf,int n)
121 sprintf(buf,"emfile-2k-%d",n);
125 static char dummyBuffer[BLOCK_SIZE];
127 static int GetBlockFileHandle(int n)
137 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
139 fSize = lseek(h,0,SEEK_END);
140 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
141 if(fSize < requiredSize){
142 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
143 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
153 static int CheckInit(void)
155 static int initialised = 0;
170 remaining_ops = (rand() % 1000) * 5;
171 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
175 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
177 for(i = 0; i < MAX_HANDLES; i++)
178 filedisk.handle[i] = -1;
180 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
181 filedisk.handle[i] = GetBlockFileHandle(i);
188 int yflash2_GetNumberOfBlocks(void)
192 return filedisk.nBlocks;
196 int yaffs_check_all_ff(const u8 *ptr, int n)
201 if(*ptr!=0xFF) return 0;
208 static int yflash2_WriteChunk(struct yaffs_dev *dev, int nand_chunk,
209 const u8 *data, int data_len,
210 const u8 *oob, int oob_len)
212 int retval = YAFFS_OK;
219 if (data && data_len) {
220 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
221 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
222 lseek(h,pos,SEEK_SET);
223 nwritten = write(h,data,data_len);
224 if(nwritten != data_len)
228 if (oob && oob_len) {
229 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
230 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
231 lseek(h,pos,SEEK_SET);
232 nwritten = write(h,oob,oob_len);
233 if(nwritten != oob_len)
237 yflash2_MaybePowerFail(nand_chunk,3);
242 static int yflash2_ReadChunk(struct yaffs_dev *dev, int nand_chunk,
243 u8 *data, int data_len,
244 u8 *oob, int oob_len,
245 enum yaffs_ecc_result *ecc_result)
247 int retval = YAFFS_OK;
254 if (data && data_len) {
255 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
256 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
257 lseek(h,pos,SEEK_SET);
258 nread = read(h,data,data_len);
259 if(nread != data_len)
263 if (oob && oob_len) {
264 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
265 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
266 lseek(h,pos,SEEK_SET);
267 nread = read(h,oob,oob_len);
273 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
278 static int yflash2_EraseBlock(struct yaffs_dev *dev, int block_no)
285 if(block_no < 0 || block_no >= filedisk.nBlocks)
287 yaffs_trace(YAFFS_TRACE_ALWAYS,"Attempt to erase non-existant block %d",block_no);
300 h = filedisk.handle[(block_no / ( BLOCKS_PER_HANDLE))];
301 lseek(h,((block_no % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE,SEEK_SET);
302 for(i = 0; i < dev->param.chunks_per_block; i++)
304 write(h,pg,PAGE_SIZE);
306 pos = lseek(h, 0,SEEK_CUR);
312 static int yflash2_MarkBad(struct yaffs_dev *dev, int block_no)
317 struct yaffs_packed_tags2 pt;
321 memset(&pt,0,sizeof(pt));
322 h = filedisk.handle[(block_no / ( BLOCKS_PER_HANDLE))];
323 lseek(h,((block_no % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
324 written = write(h,&pt,sizeof(pt));
326 if(written != sizeof(pt))
333 static int yflash2_CheckBad(struct yaffs_dev *dev, int block_no)
341 static int yflash2_Initialise(struct yaffs_dev *dev)
350 struct yaffs_dev *yflash2_install_drv(const char *name)
352 struct yaffs_dev *dev = NULL;
353 struct yaffs_param *param;
354 struct yaffs_driver *drv;
356 dev = malloc(sizeof(*dev));
361 memset(dev, 0, sizeof(*dev));
363 dev->param.name = strdup(name);
365 if(!dev->param.name) {
372 drv->drv_write_chunk_fn = yflash2_WriteChunk;
373 drv->drv_read_chunk_fn = yflash2_ReadChunk;
374 drv->drv_erase_fn = yflash2_EraseBlock;
375 drv->drv_mark_bad_fn = yflash2_MarkBad;
376 drv->drv_check_bad_fn = yflash2_CheckBad;
377 drv->drv_initialise_fn = yflash2_Initialise;
381 param->total_bytes_per_chunk = 2048;
382 param->chunks_per_block = 64;
383 param->start_block = 0;
384 param->end_block = yflash2_GetNumberOfBlocks()-1;
385 param->is_yaffs2 = 1;
386 param->use_nand_ecc=1;
388 param->n_reserved_blocks = 5;
389 param->wide_tnodes_disabled=0;
390 param->refresh_period = 1000;
391 param->n_caches = 10; // Use caches
393 param->enable_xattr = 1;
395 /* dev->driver_context is not used by this simulator */
397 yaffs_add_device(dev);