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.
15 * This is only intended as test code to test persistence etc.
19 #include "yaffs_trace.h"
21 #include "yaffs_flashif.h"
22 #include "yaffs_guts.h"
24 #include <sys/types.h>
33 #define BLOCK_SIZE (32 * 528)
34 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
40 u8 data[528]; // Data + spare
45 yflash_Page page[32]; // The pages in the block
57 static yflash_Device filedisk;
59 static int CheckInit(struct yaffs_dev *dev)
61 static int initialised = 0;
78 filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
80 filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
82 if(filedisk.handle < 0)
84 perror("Failed to open yaffs emulation file");
89 fSize = lseek(filedisk.handle,0,SEEK_END);
91 if(fSize < SIZE_IN_MB * 1024 * 1024)
93 printf("Creating yaffs emulation file\n");
95 lseek(filedisk.handle,0,SEEK_SET);
97 memset(&p,0xff,sizeof(yflash_Page));
99 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
101 written = write(filedisk.handle,&p,sizeof(yflash_Page));
103 if(written != sizeof(yflash_Page))
105 printf("Write failed\n");
114 int yflash_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_spare *spare)
122 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
123 written = write(filedisk.handle,data,512);
125 if(written != 512) return YAFFS_FAIL;
130 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
131 written = write(filedisk.handle,spare,16);
133 if(written != 16) return YAFFS_FAIL;
142 int yflash_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_spare *spare)
150 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
151 nread = read(filedisk.handle,data,512);
153 if(nread != 512) return YAFFS_FAIL;
158 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
159 nread= read(filedisk.handle,spare,16);
161 if(nread != 16) return YAFFS_FAIL;
170 int yflash_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
176 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
178 yaffs_trace(YAFFS_TRACE_ALWAYS,
179 "Attempt to erase non-existant block %d\n",
188 memset(&pg,0xff,sizeof(yflash_Page));
190 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
192 for(i = 0; i < 32; i++)
194 write(filedisk.handle,&pg,528);
201 int yflash_InitialiseNAND(struct yaffs_dev *dev)