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