a791214fb891f74fe5a4e6c791d7766c3f30e548
[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         int i;
64         int fSize;
65         int written;
66
67         yflash_Page p;
68
69         (void)dev;
70
71         if(initialised)
72         {
73                 return YAFFS_OK;
74         }
75
76         initialised = 1;
77
78
79         filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
80
81         filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
82
83         if(filedisk.handle < 0)
84         {
85                 perror("Failed to open yaffs emulation file");
86                 return YAFFS_FAIL;
87         }
88
89
90         fSize = lseek(filedisk.handle,0,SEEK_END);
91
92         if(fSize < SIZE_IN_MB * 1024 * 1024)
93         {
94                 printf("Creating yaffs emulation file\n");
95
96                 lseek(filedisk.handle,0,SEEK_SET);
97
98                 memset(&p,0xff,sizeof(yflash_Page));
99
100                 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
101                 {
102                         written = write(filedisk.handle,&p,sizeof(yflash_Page));
103
104                         if(written != sizeof(yflash_Page))
105                         {
106                                 printf("Write failed\n");
107                                 return YAFFS_FAIL;
108                         }
109                 }
110         }
111
112         return 1;
113 }
114
115 int yflash_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_spare *spare)
116 {
117         int written;
118
119         CheckInit(dev);
120
121         if(data)
122         {
123                 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
124                 written = write(filedisk.handle,data,512);
125
126                 if(written != 512) return YAFFS_FAIL;
127         }
128
129         if(spare)
130         {
131                 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
132                 written = write(filedisk.handle,spare,16);
133
134                 if(written != 16) return YAFFS_FAIL;
135         }
136
137
138         return YAFFS_OK;
139
140 }
141
142
143 int yflash_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_spare *spare)
144 {
145         int nread;
146
147         CheckInit(dev);
148
149         if(data)
150         {
151                 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
152                 nread = read(filedisk.handle,data,512);
153
154                 if(nread != 512) return YAFFS_FAIL;
155         }
156
157         if(spare)
158         {
159                 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
160                 nread= read(filedisk.handle,spare,16);
161
162                 if(nread != 16) return YAFFS_FAIL;
163         }
164
165
166         return YAFFS_OK;
167
168 }
169
170
171 int yflash_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
172 {
173         int i;
174
175         CheckInit(dev);
176
177         if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
178         {
179                 yaffs_trace(YAFFS_TRACE_ALWAYS,
180                         "Attempt to erase non-existant block %d\n",
181                         blockNumber);
182                 return YAFFS_FAIL;
183         }
184         else
185         {
186
187                 yflash_Page pg;
188
189                 memset(&pg,0xff,sizeof(yflash_Page));
190
191                 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
192
193                 for(i = 0; i < 32; i++)
194                 {
195                         write(filedisk.handle,&pg,528);
196                 }
197                 return YAFFS_OK;
198         }
199
200 }
201
202 int yflash_InitialiseNAND(struct yaffs_dev *dev)
203 {
204         (void) dev;
205
206         return YAFFS_OK;
207 }
208
209