47437d537887032b56d7615cb9239b352c4be305
[yaffs2.git] / direct / yaffs_fileem2k.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * yaffs_ramdisk.c: yaffs ram disk component
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  *
13  */
14
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.
17
18 const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.9 2006-11-08 09:49:47 charles Exp $";
19
20
21 #include "yportenv.h"
22
23 #include "yaffs_flashif.h"
24 #include "yaffs_guts.h"
25 #include "devextras.h"
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h> 
31
32 #include "yaffs_fileem2k.h"
33 #include "yaffs_packedtags2.h"
34
35 #define SIMULATE_FAILURES
36
37 typedef struct 
38 {
39         __u8 data[PAGE_SIZE]; // Data + spare
40 } yflash_Page;
41
42 typedef struct
43 {
44         yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
45         
46 } yflash_Block;
47
48
49
50 #define MAX_HANDLES 20
51 #define BLOCKS_PER_HANDLE 8000
52
53 typedef struct
54 {
55         int handle[MAX_HANDLES];
56         int nBlocks;
57 } yflash_Device;
58
59 static yflash_Device filedisk;
60
61 int yaffs_testPartialWrite = 0;
62
63
64 static char *NToName(char *buf,int n)
65 {
66         sprintf(buf,"emfile%d",n);
67         return buf;
68 }
69
70 static char dummyBuffer[BLOCK_SIZE];
71
72 static int GetBlockFileHandle(int n)
73 {
74         int h;
75         int requiredSize;
76         
77         char name[40];
78         NToName(name,n);
79         int fSize;
80         int i;
81         
82         h =  open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
83         if(h >= 0){
84                 fSize = lseek(h,0,SEEK_END);
85                 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
86                 if(fSize < requiredSize){
87                    for(i = 0; i < BLOCKS_PER_HANDLE; i++)
88                         if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
89                                 return -1;
90                         
91                 }
92         }
93         
94         return h;
95
96 }
97
98 static int  CheckInit(void)
99 {
100         static int initialised = 0;
101         int h;
102         int i;
103
104         
105         off_t fSize;
106         off_t requiredSize;
107         int written;
108         int blk;
109         
110         yflash_Page p;
111         
112         if(initialised) 
113         {
114                 return YAFFS_OK;
115         }
116
117         initialised = 1;
118         
119         memset(dummyBuffer,0xff,sizeof(dummyBuffer));
120         
121         
122         filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
123
124         for(i = 0; i <  MAX_HANDLES; i++)
125                 filedisk.handle[i] = -1;
126         
127         for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
128                 filedisk.handle[i] = GetBlockFileHandle(i);
129         
130         
131         return 1;
132 }
133
134
135 int yflash_GetNumberOfBlocks(void)
136 {
137         CheckInit();
138         
139         return filedisk.nBlocks;
140 }
141
142 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
143 {
144         int written;
145         int pos;
146         int h;
147         
148         T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
149
150         CheckInit();
151         
152         
153         
154         if(data)
155         {
156                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
157                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
158                 
159                 lseek(h,pos,SEEK_SET);
160                 written = write(h,data,dev->nDataBytesPerChunk);
161                 
162                 if(yaffs_testPartialWrite){
163                         close(h);
164                         exit(1);
165                 }
166                 
167 #ifdef SIMULATE_FAILURES
168                         if((chunkInNAND >> 6) == 100) 
169                           written = 0;
170
171                         if((chunkInNAND >> 6) == 110) 
172                           written = 0;
173 #endif
174
175
176                 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
177         }
178         
179         if(tags)
180         {
181                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
182                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
183                 
184                 lseek(h,pos,SEEK_SET);
185                 if( 0 && dev->isYaffs2)
186                 {
187                         
188                         written = write(h,tags,sizeof(yaffs_ExtendedTags));
189                         if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
190                 }
191                 else
192                 {
193                         yaffs_PackedTags2 pt;
194                         yaffs_PackTags2(&pt,tags);
195
196                         written = write(h,&pt,sizeof(pt));
197                         if(written != sizeof(pt)) return YAFFS_FAIL;
198                 }
199         }
200         
201
202         return YAFFS_OK;        
203
204 }
205
206 int yaffs_CheckAllFF(const __u8 *ptr, int n)
207 {
208         while(n)
209         {
210                 n--;
211                 if(*ptr!=0xFF) return 0;
212                 ptr++;
213         }
214         return 1;
215 }
216
217
218 static int fail300 = 1;
219 static int fail320 = 1;
220
221 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
222 {
223         int nread;
224         int pos;
225         int h;
226         
227         T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
228         
229         CheckInit();
230         
231         
232         
233         if(data)
234         {
235
236                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
237                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];             
238                 lseek(h,pos,SEEK_SET);
239                 nread = read(h,data,dev->nDataBytesPerChunk);
240                 
241                 if(nread != dev->nDataBytesPerChunk) return YAFFS_FAIL;
242         }
243         
244         if(tags)
245         {
246                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
247                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];             
248                 lseek(h,pos,SEEK_SET);
249
250                 if(0 && dev->isYaffs2)
251                 {
252                         nread= read(h,tags,sizeof(yaffs_ExtendedTags));
253                         if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
254                         if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
255                         {
256                                 yaffs_InitialiseTags(tags);
257                         }
258                         else
259                         {
260                                 tags->chunkUsed = 1;
261                         }
262                 }
263                 else
264                 {
265                         yaffs_PackedTags2 pt;
266                         nread= read(h,&pt,sizeof(pt));
267                         yaffs_UnpackTags2(tags,&pt);
268 #ifdef SIMULATE_FAILURES
269                         if((chunkInNAND >> 6) == 100) {
270                             if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
271                                tags->eccResult = YAFFS_ECC_RESULT_FIXED;
272                                fail300 = 0;
273                             }
274                             
275                         }
276                         if((chunkInNAND >> 6) == 110) {
277                             if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
278                                tags->eccResult = YAFFS_ECC_RESULT_FIXED;
279                                fail320 = 0;
280                             }
281                         }
282 #endif
283                         if(nread != sizeof(pt)) return YAFFS_FAIL;
284                 }
285         }
286         
287
288         return YAFFS_OK;        
289
290 }
291
292
293 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
294 {
295         int written;
296         int h;
297         
298         yaffs_PackedTags2 pt;
299
300         CheckInit();
301         
302         memset(&pt,0,sizeof(pt));
303         h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
304         lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
305         written = write(h,&pt,sizeof(pt));
306                 
307         if(written != sizeof(pt)) return YAFFS_FAIL;
308         
309         
310         return YAFFS_OK;
311         
312 }
313
314 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
315 {
316
317         int i;
318         int h;
319                 
320         CheckInit();
321         
322         if(blockNumber == 320)
323                 fail320 = 1;
324         
325         if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
326         {
327                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
328                 return YAFFS_FAIL;
329         }
330         else
331         {
332         
333                 __u8 pg[PAGE_SIZE];
334                 int syz = PAGE_SIZE;
335                 int pos;
336                 
337                 memset(pg,0xff,syz);
338                 
339
340                 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
341                 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);               
342                 for(i = 0; i < dev->nChunksPerBlock; i++)
343                 {
344                         write(h,pg,PAGE_SIZE);
345                 }
346                 pos = lseek(h, 0,SEEK_CUR);
347                 
348                 return YAFFS_OK;
349         }
350         
351 }
352
353 int yflash_InitialiseNAND(yaffs_Device *dev)
354 {
355         CheckInit();
356         
357         return YAFFS_OK;
358 }
359
360
361
362
363 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
364 {
365         yaffs_ExtendedTags tags;
366         int chunkNo;
367
368         *sequenceNumber = 0;
369         
370         chunkNo = blockNo * dev->nChunksPerBlock;
371         
372         yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
373         if(tags.blockBad)
374         {
375                 *state = YAFFS_BLOCK_STATE_DEAD;
376         }
377         else if(!tags.chunkUsed)
378         {
379                 *state = YAFFS_BLOCK_STATE_EMPTY;
380         }
381         else if(tags.chunkUsed)
382         {
383                 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
384                 *sequenceNumber = tags.sequenceNumber;
385         }
386         return YAFFS_OK;
387 }
388