2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * This provides a YAFFS nand emulation on a file for emulating 2kB pages.
15 * This is only intended as test code to test persistence etc.
19 #include "yaffs_trace.h"
21 #include "yaffs_flashif2.h"
22 #include "yaffs_guts.h"
23 #include "yaffs_fileem2k.h"
24 #include "yaffs_packedtags2.h"
27 #include <sys/types.h>
35 /* #define SIZE_IN_MB 128 */
37 #define PAGE_DATA_SIZE (2048)
38 #define PAGE_SPARE_SIZE (64)
39 #define PAGE_SIZE (PAGE_DATA_SIZE + PAGE_SPARE_SIZE)
40 #define PAGES_PER_BLOCK (64)
41 #define BLOCK_DATA_SIZE (PAGE_DATA_SIZE * PAGES_PER_BLOCK)
42 #define BLOCK_SIZE (PAGES_PER_BLOCK * (PAGE_SIZE))
43 #define BLOCKS_PER_MB ((1024*1024)/BLOCK_DATA_SIZE)
44 #define SIZE_IN_BLOCKS (BLOCKS_PER_MB * SIZE_IN_MB)
48 #define SIZE_IN_MB 128
49 #define PAGE_DATA_SIZE (512)
50 #define SPARE_SIZE (16)
51 #define PAGE_SIZE (PAGE_DATA_SIZE + SPARE_SIZE)
52 #define PAGES_PER_BLOCK (32)
53 #define BLOCK_DATA_SIZE (PAGE_SIZE * PAGES_PER_BLOCK)
54 #define BLOCK_SIZE (PAGES_PER_BLOCK * (PAGE_SIZE))
55 #define BLOCKS_PER_MB ((1024*1024)/BLOCK_DATA_SIZE)
56 #define SIZE_IN_BLOCKS (BLOCKS_PER_MB * SIZE_IN_MB)
60 #define REPORT_ERROR 0
64 u8 data[PAGE_SIZE]; // Data + spare
69 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
75 #define MAX_HANDLES 20
76 #define BLOCKS_PER_HANDLE (32*8)
80 int handle[MAX_HANDLES];
84 static yflash_Device filedisk;
86 int yaffs_test_partial_write = 0;
88 extern int random_seed;
89 extern int simulate_power_failure;
90 static int remaining_ops;
91 static int nops_so_far;
95 static void yflash2_MaybePowerFail(unsigned int nand_chunk, int failPoint)
102 if(simulate_power_failure &&
104 printf("Simulated power failure after %d operations\n",nops_so_far);
105 printf(" power failed on nand_chunk %d, at fail point %d\n",
106 nand_chunk, failPoint);
111 static char *NToName(char *buf,int n)
113 sprintf(buf,"emfile-2k-%d",n);
117 static char dummyBuffer[BLOCK_SIZE];
119 static int GetBlockFileHandle(int n)
129 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
131 fSize = lseek(h,0,SEEK_END);
132 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
133 if(fSize < requiredSize){
134 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
135 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
145 static int CheckInit(void)
147 static int initialised = 0;
162 remaining_ops = (rand() % 1000) * 5;
163 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
167 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
169 for(i = 0; i < MAX_HANDLES; i++)
170 filedisk.handle[i] = -1;
172 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
173 filedisk.handle[i] = GetBlockFileHandle(i);
180 int yflash2_GetNumberOfBlocks(void)
184 return filedisk.nBlocks;
188 int yaffs_check_all_ff(const u8 *ptr, int n)
193 if(*ptr!=0xFF) return 0;
200 static int yflash2_WriteChunk(struct yaffs_dev *dev, int nand_chunk,
201 const u8 *data, int data_len,
202 const u8 *oob, int oob_len)
204 int retval = YAFFS_OK;
211 if (data && data_len) {
212 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
213 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
214 lseek(h,pos,SEEK_SET);
215 nwritten = write(h,data,data_len);
216 if(nwritten != data_len)
220 if (oob && oob_len) {
221 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
222 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
223 lseek(h,pos,SEEK_SET);
224 nwritten = write(h,oob,oob_len);
225 if(nwritten != oob_len)
229 yflash2_MaybePowerFail(nand_chunk,3);
234 static int yflash2_ReadChunk(struct yaffs_dev *dev, int nand_chunk,
235 u8 *data, int data_len,
236 u8 *oob, int oob_len,
237 enum yaffs_ecc_result *ecc_result)
239 int retval = YAFFS_OK;
246 if (data && data_len) {
247 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
248 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
249 lseek(h,pos,SEEK_SET);
250 nread = read(h,data,data_len);
251 if(nread != data_len)
255 if (oob && oob_len) {
256 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
257 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
258 lseek(h,pos,SEEK_SET);
259 nread = read(h,oob,oob_len);
265 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
270 static int yflash2_EraseBlock(struct yaffs_dev *dev, int block_no)
277 if(block_no < 0 || block_no >= filedisk.nBlocks)
279 yaffs_trace(YAFFS_TRACE_ALWAYS,"Attempt to erase non-existant block %d",block_no);
291 h = filedisk.handle[(block_no / ( BLOCKS_PER_HANDLE))];
292 lseek(h,((block_no % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE,SEEK_SET);
293 for(i = 0; i < dev->param.chunks_per_block; i++)
295 write(h,pg,PAGE_SIZE);
302 static int yflash2_MarkBad(struct yaffs_dev *dev, int block_no)
307 struct yaffs_packed_tags2 pt;
311 memset(&pt,0,sizeof(pt));
312 h = filedisk.handle[(block_no / ( BLOCKS_PER_HANDLE))];
313 lseek(h,((block_no % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
314 written = write(h,&pt,sizeof(pt));
316 if(written != sizeof(pt))
323 static int yflash2_CheckBad(struct yaffs_dev *dev, int block_no)
331 static int yflash2_Initialise(struct yaffs_dev *dev)
340 struct yaffs_dev *yflash2_install_drv(const char *name)
342 struct yaffs_dev *dev = NULL;
343 struct yaffs_param *param;
344 struct yaffs_driver *drv;
346 dev = malloc(sizeof(*dev));
351 memset(dev, 0, sizeof(*dev));
353 dev->param.name = strdup(name);
355 if(!dev->param.name) {
362 drv->drv_write_chunk_fn = yflash2_WriteChunk;
363 drv->drv_read_chunk_fn = yflash2_ReadChunk;
364 drv->drv_erase_fn = yflash2_EraseBlock;
365 drv->drv_mark_bad_fn = yflash2_MarkBad;
366 drv->drv_check_bad_fn = yflash2_CheckBad;
367 drv->drv_initialise_fn = yflash2_Initialise;
371 param->total_bytes_per_chunk = 2048;
372 param->chunks_per_block = 64;
373 param->start_block = 0;
374 param->end_block = yflash2_GetNumberOfBlocks()-1;
375 param->is_yaffs2 = 1;
376 param->use_nand_ecc=1;
378 param->n_reserved_blocks = 5;
379 param->wide_tnodes_disabled=0;
380 param->refresh_period = 1000;
381 param->n_caches = 10; // Use caches
383 param->enable_xattr = 1;
385 /* dev->driver_context is not used by this simulator */
387 yaffs_add_device(dev);