Fix broken comment
[yaffs2.git] / direct / test-framework / yaffs_fileem.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
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.
16  * This is only intended as test code to test persistence etc.
17  */
18
19 #include "yportenv.h"
20 #include "yaffs_trace.h"
21
22 #include "yaffs_flashif.h"
23 #include "yaffs_guts.h"
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h> 
29
30
31
32 #define SIZE_IN_MB 16
33
34 #define BLOCK_SIZE (32 * 528)
35 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
36
37
38
39 typedef struct 
40 {
41         u8 data[528]; // Data + spare
42 } yflash_Page;
43
44 typedef struct
45 {
46         yflash_Page page[32]; // The pages in the block
47         
48 } yflash_Block;
49
50
51
52 typedef struct
53 {
54         int handle;
55         int nBlocks;
56 } yflash_Device;
57
58 static yflash_Device filedisk;
59
60 static int  CheckInit(struct yaffs_dev *dev)
61 {
62         static int initialised = 0;
63         
64         int i;
65
66         
67         int fSize;
68         int written;
69         
70         yflash_Page p;
71         
72         if(initialised) 
73         {
74                 return YAFFS_OK;
75         }
76
77         initialised = 1;
78         
79         
80         filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
81         
82         filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
83         
84         if(filedisk.handle < 0)
85         {
86                 perror("Failed to open yaffs emulation file");
87                 return YAFFS_FAIL;
88         }
89         
90         
91         fSize = lseek(filedisk.handle,0,SEEK_END);
92         
93         if(fSize < SIZE_IN_MB * 1024 * 1024)
94         {
95                 printf("Creating yaffs emulation file\n");
96                 
97                 lseek(filedisk.handle,0,SEEK_SET);
98                 
99                 memset(&p,0xff,sizeof(yflash_Page));
100                 
101                 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
102                 {
103                         written = write(filedisk.handle,&p,sizeof(yflash_Page));
104                         
105                         if(written != sizeof(yflash_Page))
106                         {
107                                 printf("Write failed\n");
108                                 return YAFFS_FAIL;
109                         }
110                 }               
111         }
112         
113         return 1;
114 }
115
116 int yflash_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_spare *spare)
117 {
118         int written;
119
120         CheckInit(dev);
121         
122         
123         
124         if(data)
125         {
126                 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
127                 written = write(filedisk.handle,data,512);
128                 
129                 if(written != 512) return YAFFS_FAIL;
130         }
131         
132         if(spare)
133         {
134                 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
135                 written = write(filedisk.handle,spare,16);
136                 
137                 if(written != 16) return YAFFS_FAIL;
138         }
139         
140
141         return YAFFS_OK;        
142
143 }
144
145
146 int yflash_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_spare *spare)
147 {
148         int nread;
149
150         CheckInit(dev);
151         
152         
153         
154         if(data)
155         {
156                 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
157                 nread = read(filedisk.handle,data,512);
158                 
159                 if(nread != 512) return YAFFS_FAIL;
160         }
161         
162         if(spare)
163         {
164                 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
165                 nread= read(filedisk.handle,spare,16);
166                 
167                 if(nread != 16) return YAFFS_FAIL;
168         }
169         
170
171         return YAFFS_OK;        
172
173 }
174
175
176 int yflash_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
177 {
178
179         int i;
180                 
181         CheckInit(dev);
182         
183         if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
184         {
185                 yaffs_trace(YAFFS_TRACE_ALWAYS,
186                         "Attempt to erase non-existant block %d\n",
187                         blockNumber);
188                 return YAFFS_FAIL;
189         }
190         else
191         {
192         
193                 yflash_Page pg;
194                 
195                 memset(&pg,0xff,sizeof(yflash_Page));
196                 
197                 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
198                 
199                 for(i = 0; i < 32; i++)
200                 {
201                         write(filedisk.handle,&pg,528);
202                 }
203                 return YAFFS_OK;
204         }
205         
206 }
207
208 int yflash_InitialiseNAND(struct yaffs_dev *dev)
209 {
210         
211         return YAFFS_OK;
212 }
213
214