*** empty log message ***
[yaffs/.git] / direct / yaffs_fileem.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.
16 // THis is only intended as test code to test persistence etc.
17
18 const char *yaffs_flashif_c_version = "$Id: yaffs_fileem.c,v 1.1 2003-01-21 03:32:17 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
33
34 #define SIZE_IN_MB 16
35
36 #define BLOCK_SIZE (32 * 528)
37 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
38
39
40
41 typedef struct 
42 {
43         __u8 data[528]; // Data + spare
44 } yflash_Page;
45
46 typedef struct
47 {
48         yflash_Page page[32]; // The pages in the block
49         
50 } yflash_Block;
51
52
53
54 typedef struct
55 {
56         int handle;
57         int nBlocks;
58 } yflash_Device;
59
60 static yflash_Device filedisk;
61
62 static int  CheckInit(yaffs_Device *dev)
63 {
64         static int initialised = 0;
65         
66         int i;
67
68         
69         int fSize;
70         int written;
71         
72         yflash_Page p;
73         
74         if(initialised) 
75         {
76                 return YAFFS_OK;
77         }
78
79         initialised = 1;
80         
81         
82         filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
83         
84         filedisk.handle = open("yaffsemfile", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
85         
86         if(filedisk.handle < 0)
87         {
88                 perror("Failed to open yaffs emulation file");
89                 return YAFFS_FAIL;
90         }
91         
92         
93         fSize = lseek(filedisk.handle,0,SEEK_END);
94         
95         if(fSize < SIZE_IN_MB * 1024 * 1024)
96         {
97                 printf("Creating yaffs emulation file\n");
98                 
99                 lseek(filedisk.handle,0,SEEK_SET);
100                 
101                 memset(&p,0xff,sizeof(yflash_Page));
102                 
103                 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
104                 {
105                         written = write(filedisk.handle,&p,sizeof(yflash_Page));
106                         
107                         if(written != sizeof(yflash_Page))
108                         {
109                                 printf("Write failed\n");
110                                 return YAFFS_FAIL;
111                         }
112                 }               
113         }
114         
115         return 1;
116 }
117
118 int yflash_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
119 {
120         int written;
121
122         CheckInit(dev);
123         
124         
125         
126         if(data)
127         {
128                 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
129                 written = write(filedisk.handle,data,512);
130                 
131                 if(written != 512) return YAFFS_FAIL;
132         }
133         
134         if(spare)
135         {
136                 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
137                 written = write(filedisk.handle,spare,16);
138                 
139                 if(written != 16) return YAFFS_FAIL;
140         }
141         
142
143         return YAFFS_OK;        
144
145 }
146
147
148 int yflash_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
149 {
150         int nread;
151
152         CheckInit(dev);
153         
154         
155         
156         if(data)
157         {
158                 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
159                 nread = read(filedisk.handle,data,512);
160                 
161                 if(nread != 512) return YAFFS_FAIL;
162         }
163         
164         if(spare)
165         {
166                 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
167                 nread= read(filedisk.handle,spare,16);
168                 
169                 if(nread != 16) return YAFFS_FAIL;
170         }
171         
172
173         return YAFFS_OK;        
174
175 }
176
177
178 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
179 {
180
181         int i;
182                 
183         CheckInit(dev);
184         
185         if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
186         {
187                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",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(yaffs_Device *dev)
209 {
210         dev->useNANDECC = 1; // force on useNANDECC which gets faked. 
211                                                  // This saves us doing ECC checks.
212         
213         return YAFFS_OK;
214 }
215
216