Fix problem where symllink alias pointer was being used after free
[yaffs2.git] / direct / yaffs_flashif.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 const char *yaffs_flashif_c_version = "$Id: yaffs_flashif.c,v 1.3 2007-02-14 01:09:06 wookey Exp $";
16
17
18 #include "yportenv.h"
19
20 #include "yaffs_flashif.h"
21 #include "yaffs_guts.h"
22 #include "devextras.h"
23
24
25 #define SIZE_IN_MB 16
26
27 #define BLOCK_SIZE (32 * 528)
28 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
29
30
31
32 typedef struct 
33 {
34         __u8 data[528]; // Data + spare
35 } yflash_Page;
36
37 typedef struct
38 {
39         yflash_Page page[32]; // The pages in the block
40         
41 } yflash_Block;
42
43
44
45 typedef struct
46 {
47         yflash_Block **block;
48         int nBlocks;
49 } yflash_Device;
50
51 static yflash_Device ramdisk;
52
53 static int  CheckInit(yaffs_Device *dev)
54 {
55         static int initialised = 0;
56         
57         int i;
58         int fail = 0;
59         int nAllocated = 0;
60         
61         if(initialised) 
62         {
63                 return YAFFS_OK;
64         }
65
66         initialised = 1;
67         
68         
69         ramdisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
70         
71         ramdisk.block = YMALLOC(sizeof(yflash_Block *) * ramdisk.nBlocks);
72         
73         if(!ramdisk.block) return 0;
74         
75         for(i=0; i <ramdisk.nBlocks; i++)
76         {
77                 ramdisk.block[i] = NULL;
78         }
79         
80         for(i=0; i <ramdisk.nBlocks && !fail; i++)
81         {
82                 if((ramdisk.block[i] = YMALLOC(sizeof(yflash_Block))) == 0)
83                 {
84                         fail = 1;
85                 }
86                 else
87                 {
88                         yflash_EraseBlockInNAND(dev,i);
89                         nAllocated++;
90                 }
91         }
92         
93         if(fail)
94         {
95                 for(i = 0; i < nAllocated; i++)
96                 {
97                         YFREE(ramdisk.block[i]);
98                 }
99                 YFREE(ramdisk.block);
100                 
101                 T(YAFFS_TRACE_ALWAYS,("Allocation failed, could only allocate %dMB of %dMB requested.\n",
102                    nAllocated/64,ramdisk.nBlocks * YAFFS_BYTES_PER_BLOCK));
103                 return 0;
104         }
105         
106         
107         
108         return 1;
109 }
110
111 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
112 {
113         int blk;
114         int pg;
115         
116
117         CheckInit(dev);
118         
119         blk = chunkInNAND/32;
120         pg = chunkInNAND%32;
121         
122         
123         if(data)
124         {
125                 memcpy(ramdisk.block[blk]->page[pg].data,data,512);
126         }
127         
128         
129         if(tags)
130         {
131                 yaffs_PackedTags pt;
132                 yaffs_PackTags(&pt,tags);
133                 memcpy(&ramdisk.block[blk]->page[pg].data[512],&pt,sizeof(pt));
134         }
135
136         return YAFFS_OK;        
137
138 }
139
140
141 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Tags *tags)
142 {
143         int blk;
144         int pg;
145
146         
147         CheckInit(dev);
148         
149         blk = chunkInNAND/32;
150         pg = chunkInNAND%32;
151         
152         
153         if(data)
154         {
155                 memcpy(data,ramdisk.block[blk]->page[pg].data,512);
156         }
157         
158         
159         if(tags)
160         {
161                 yaffs_PackedTags pt;
162                 memcpy(&pt,&ramdisk.block[blk]->page[pg].data[512],sizeof(yaffs_PackedTags));
163                 yaffs_UnpackTags(tags,&pt);
164         }
165
166         return YAFFS_OK;
167 }
168
169
170 int yflash_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
171 {
172         int blk;
173         int pg;
174         int i;
175
176         
177         CheckInit(dev);
178         
179         blk = chunkInNAND/32;
180         pg = chunkInNAND%32;
181         
182         
183         for(i = 0; i < 528; i++)
184         {
185                 if(ramdisk.block[blk]->page[pg].data[i] != 0xFF)
186                 {
187                         return YAFFS_FAIL;
188                 }
189         }
190
191         return YAFFS_OK;
192
193 }
194
195 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
196 {
197         
198         CheckInit(dev);
199         
200         if(blockNumber < 0 || blockNumber >= ramdisk.nBlocks)
201         {
202                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
203                 return YAFFS_FAIL;
204         }
205         else
206         {
207                 memset(ramdisk.block[blockNumber],0xFF,sizeof(yflash_Block));
208                 return YAFFS_OK;
209         }
210         
211 }
212
213 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
214 {
215         return YAFFS_OK;
216         
217 }
218 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
219 {
220         *state = YAFFS_BLOCK_STATE_EMPTY;
221         *sequenceNumber = 0;
222 }
223
224
225 int yflash_InitialiseNAND(yaffs_Device *dev)
226 {
227         return YAFFS_OK;
228 }
229