3a63aee1b26cbf667514a92d002781f99c305940
[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 "devextras.h"
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <unistd.h> 
34
35
36
37 #define SIZE_IN_MB 16
38
39 #define BLOCK_SIZE (32 * 528)
40 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
41
42
43
44 typedef struct 
45 {
46         u8 data[528]; // Data + spare
47 } yflash_Page;
48
49 typedef struct
50 {
51         yflash_Page page[32]; // The pages in the block
52         
53 } yflash_Block;
54
55
56
57 typedef struct
58 {
59         int handle;
60         int nBlocks;
61 } yflash_Device;
62
63 static yflash_Device filedisk;
64
65 static int  CheckInit(struct yaffs_dev *dev)
66 {
67         static int initialised = 0;
68         
69         int i;
70
71         
72         int fSize;
73         int written;
74         
75         yflash_Page p;
76         
77         if(initialised) 
78         {
79                 return YAFFS_OK;
80         }
81
82         initialised = 1;
83         
84         
85         filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
86         
87         filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
88         
89         if(filedisk.handle < 0)
90         {
91                 perror("Failed to open yaffs emulation file");
92                 return YAFFS_FAIL;
93         }
94         
95         
96         fSize = lseek(filedisk.handle,0,SEEK_END);
97         
98         if(fSize < SIZE_IN_MB * 1024 * 1024)
99         {
100                 printf("Creating yaffs emulation file\n");
101                 
102                 lseek(filedisk.handle,0,SEEK_SET);
103                 
104                 memset(&p,0xff,sizeof(yflash_Page));
105                 
106                 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
107                 {
108                         written = write(filedisk.handle,&p,sizeof(yflash_Page));
109                         
110                         if(written != sizeof(yflash_Page))
111                         {
112                                 printf("Write failed\n");
113                                 return YAFFS_FAIL;
114                         }
115                 }               
116         }
117         
118         return 1;
119 }
120
121 int yflash_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const yaffs_spare *spare)
122 {
123         int written;
124
125         CheckInit(dev);
126         
127         
128         
129         if(data)
130         {
131                 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
132                 written = write(filedisk.handle,data,512);
133                 
134                 if(written != 512) return YAFFS_FAIL;
135         }
136         
137         if(spare)
138         {
139                 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
140                 written = write(filedisk.handle,spare,16);
141                 
142                 if(written != 16) return YAFFS_FAIL;
143         }
144         
145
146         return YAFFS_OK;        
147
148 }
149
150
151 int yflash_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, yaffs_spare *spare)
152 {
153         int nread;
154
155         CheckInit(dev);
156         
157         
158         
159         if(data)
160         {
161                 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
162                 nread = read(filedisk.handle,data,512);
163                 
164                 if(nread != 512) return YAFFS_FAIL;
165         }
166         
167         if(spare)
168         {
169                 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
170                 nread= read(filedisk.handle,spare,16);
171                 
172                 if(nread != 16) return YAFFS_FAIL;
173         }
174         
175
176         return YAFFS_OK;        
177
178 }
179
180
181 int yflash_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
182 {
183
184         int i;
185                 
186         CheckInit(dev);
187         
188         if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
189         {
190                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",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