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