Some tinkering on test harness and st_xxx to yst_xxx changes
[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.4 2005-07-18 23:12:00 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
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 typedef struct
51 {
52         int handle;
53         int nBlocks;
54 } yflash_Device;
55
56 static yflash_Device filedisk;
57
58 static int  CheckInit(void)
59 {
60         static int initialised = 0;
61         
62         int i;
63
64         
65         int fSize;
66         int written;
67         
68         yflash_Page p;
69         
70         if(initialised) 
71         {
72                 return YAFFS_OK;
73         }
74
75         initialised = 1;
76         
77         
78         filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
79         
80         filedisk.handle = open("yaffsemfile2k", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
81         
82         if(filedisk.handle < 0)
83         {
84                 perror("Failed to open yaffs emulation file");
85                 return YAFFS_FAIL;
86         }
87         
88         
89         fSize = lseek(filedisk.handle,0,SEEK_END);
90         
91         if(fSize < filedisk.nBlocks * BLOCK_SIZE)
92         {
93                 printf("Creating yaffs emulation file\n");
94                 
95                 lseek(filedisk.handle,0,SEEK_SET);
96                 
97                 memset(&p,0xff,sizeof(yflash_Page));
98                 
99                 for(i = 0; i <  filedisk.nBlocks * BLOCK_SIZE; i+= PAGE_SIZE)
100                 {
101                         written = write(filedisk.handle,&p,sizeof(yflash_Page));
102                         
103                         if(written != sizeof(yflash_Page))
104                         {
105                                 printf("Write failed\n");
106                                 return YAFFS_FAIL;
107                         }
108                 }               
109         }
110         else
111         {
112                 filedisk.nBlocks = fSize/(BLOCK_SIZE);
113         }
114         
115         return 1;
116 }
117
118
119 int yflash_GetNumberOfBlocks(void)
120 {
121         CheckInit();
122         
123         return filedisk.nBlocks;
124 }
125
126 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
127 {
128         int written;
129         int pos;
130
131         CheckInit();
132         
133         
134         
135         if(data)
136         {
137                 pos = chunkInNAND * PAGE_SIZE;
138                 lseek(filedisk.handle,pos,SEEK_SET);
139                 written = write(filedisk.handle,data,dev->nBytesPerChunk);
140                 
141                 if(written != dev->nBytesPerChunk) return YAFFS_FAIL;
142         }
143         
144         if(tags)
145         {
146                 pos = chunkInNAND * PAGE_SIZE + PAGE_DATA_SIZE;
147                 lseek(filedisk.handle,pos,SEEK_SET);
148                 if( 0 && dev->isYaffs2)
149                 {
150                         
151                         written = write(filedisk.handle,tags,sizeof(yaffs_ExtendedTags));
152                         if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
153                 }
154                 else
155                 {
156                         yaffs_PackedTags2 pt;
157                         yaffs_PackTags2(&pt,tags);
158
159                         written = write(filedisk.handle,&pt,sizeof(pt));
160                         if(written != sizeof(pt)) return YAFFS_FAIL;
161                 }
162         }
163         
164
165         return YAFFS_OK;        
166
167 }
168
169 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
170 {
171         int written;
172         
173         yaffs_PackedTags2 pt;
174
175         CheckInit();
176         
177         memset(&pt,0,sizeof(pt));
178         lseek(filedisk.handle,(blockNo * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
179         written = write(filedisk.handle,&pt,sizeof(pt));
180                 
181         if(written != sizeof(pt)) return YAFFS_FAIL;
182         
183         
184         return YAFFS_OK;
185         
186 }
187
188 int yaffs_CheckAllFF(const __u8 *ptr, int n)
189 {
190         while(n)
191         {
192                 n--;
193                 if(*ptr!=0xFF) return 0;
194                 ptr++;
195         }
196         return 1;
197 }
198
199
200 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
201 {
202         int nread;
203         int pos;
204
205         CheckInit();
206         
207         
208         
209         if(data)
210         {
211                 pos = chunkInNAND * PAGE_SIZE;
212                 lseek(filedisk.handle,pos,SEEK_SET);
213                 nread = read(filedisk.handle,data,dev->nBytesPerChunk);
214                 
215                 if(nread != dev->nBytesPerChunk) return YAFFS_FAIL;
216         }
217         
218         if(tags)
219         {
220                 pos = chunkInNAND * PAGE_SIZE + PAGE_DATA_SIZE;
221                 lseek(filedisk.handle,pos,SEEK_SET);
222                 if(0 && dev->isYaffs2)
223                 {
224                         nread= read(filedisk.handle,tags,sizeof(yaffs_ExtendedTags));
225                         if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
226                         if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
227                         {
228                                 yaffs_InitialiseTags(tags);
229                         }
230                         else
231                         {
232                                 tags->chunkUsed = 1;
233                         }
234                 }
235                 else
236                 {
237                         yaffs_PackedTags2 pt;
238                         nread= read(filedisk.handle,&pt,sizeof(pt));
239                         yaffs_UnpackTags2(tags,&pt);
240                         if(nread != sizeof(pt)) return YAFFS_FAIL;
241                 }
242         }
243         
244
245         return YAFFS_OK;        
246
247 }
248
249
250 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
251 {
252
253         int i;
254                 
255         CheckInit();
256         
257         if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
258         {
259                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
260                 return YAFFS_FAIL;
261         }
262         else
263         {
264         
265                 __u8 pg[PAGE_SIZE];
266                 int syz = PAGE_SIZE;
267                 int pos;
268                 
269                 memset(pg,0xff,syz);
270                 
271                 pos = lseek(filedisk.handle, blockNumber * dev->nChunksPerBlock * PAGE_SIZE, SEEK_SET);
272                 
273                 for(i = 0; i < dev->nChunksPerBlock; i++)
274                 {
275                         write(filedisk.handle,pg,PAGE_SIZE);
276                 }
277                 pos = lseek(filedisk.handle, 0,SEEK_CUR);
278                 
279                 return YAFFS_OK;
280         }
281         
282 }
283
284 int yflash_InitialiseNAND(yaffs_Device *dev)
285 {
286         CheckInit();
287         
288         return YAFFS_OK;
289 }
290
291
292
293
294 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
295 {
296         yaffs_ExtendedTags tags;
297         int chunkNo;
298
299         *sequenceNumber = 0;
300         
301         chunkNo = blockNo * dev->nChunksPerBlock;
302         
303         yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
304         if(tags.blockBad)
305         {
306                 *state = YAFFS_BLOCK_STATE_DEAD;
307         }
308         else if(!tags.chunkUsed)
309         {
310                 *state = YAFFS_BLOCK_STATE_EMPTY;
311         }
312         else if(tags.chunkUsed)
313         {
314                 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
315                 *sequenceNumber = tags.sequenceNumber;
316         }
317         return YAFFS_OK;
318 }
319