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