1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/*
* YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
*
* Copyright (C) 2002-2007 Aleph One Ltd.
* for Toby Churchill Ltd and Brightstar Engineering
*
* Created by Charles Manning <charles@aleph1.co.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
/*
* yaffs_ramem2k.c: RAM emulation in-kernel for 2K pages (YAFFS2)
*/
/* XXX U-BOOT XXX */
#include <common.h>
const char *yaffs_ramem2k_c_version = "$Id: yaffs_ramem2k.c,v 1.3 2007/02/14 01:09:06 wookey Exp $";
#ifndef __KERNEL__
#define CONFIG_YAFFS_RAM_ENABLED
#else
#include <linux/config.h>
#endif
#ifdef CONFIG_YAFFS_RAM_ENABLED
#include "yportenv.h"
#include "yaffs_nandemul2k.h"
#include "yaffs_guts.h"
#include "yaffsinterface.h"
#include "devextras.h"
#include "yaffs_packedtags2.h"
#define EM_SIZE_IN_MEG (32)
#define PAGE_DATA_SIZE (2048)
#define PAGE_SPARE_SIZE (64)
#define PAGES_PER_BLOCK (64)
#define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
#define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
#define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
#define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
typedef struct
{
__u8 data[PAGE_TOTAL_SIZE]; // Data + spare
int empty; // is this empty?
} nandemul_Page;
typedef struct
{
nandemul_Page *page[PAGES_PER_BLOCK];
int damaged;
} nandemul_Block;
typedef struct
{
nandemul_Block**block;
int nBlocks;
} nandemul_Device;
static nandemul_Device ned;
static int sizeInMB = EM_SIZE_IN_MEG;
static void nandemul_yield(int n)
{
#ifdef __KERNEL__
if(n > 0) schedule_timeout(n);
#endif
}
static void nandemul_ReallyEraseBlock(int blockNumber)
{
int i;
nandemul_Block *blk;
if(blockNumber < 0 || blockNumber >= ned.nBlocks)
{
return;
}
blk = ned.block[blockNumber];
for(i = 0; i < PAGES_PER_BLOCK; i++)
{
memset(blk->page[i],0xff,sizeof(nandemul_Page));
blk->page[i]->empty = 1;
}
nandemul_yield(2);
}
static int nandemul2k_CalcNBlocks(void)
{
return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
}
static int CheckInit(void)
{
static int initialised = 0;
int i,j;
int fail = 0;
int nBlocks;
int nAllocated = 0;
if(initialised)
{
return YAFFS_OK;
}
ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
ned.block = YMALLOC(sizeof(nandemul_Block*) * nBlocks );
if(!ned.block) return YAFFS_FAIL;
for(i=fail=0; i <nBlocks; i++)
{
nandemul_Block *blk;
if(!(blk = ned.block[i] = YMALLOC(sizeof(nandemul_Block))))
{
fail = 1;
}
else
{
for(j = 0; j < PAGES_PER_BLOCK; j++)
{
if((blk->page[j] = YMALLOC(sizeof(nandemul_Page))) == 0)
{
fail = 1;
}
}
nandemul_ReallyEraseBlock(i);
ned.block[i]->damaged = 0;
nAllocated++;
}
}
if(fail)
{
//Todo thump pages
for(i = 0; i < nAllocated; i++)
{
YFREE(ned.block[i]);
}
YFREE(ned.block);
T(YAFFS_TRACE_ALWAYS,("Allocation failed, could only allocate %dMB of %dMB requested.\n",
nAllocated/64,sizeInMB));
return 0;
}
ned.nBlocks = nBlocks;
initialised = 1;
return 1;
}
int nandemul2k_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
{
int blk;
int pg;
int i;
__u8 *x;
blk = chunkInNAND/PAGES_PER_BLOCK;
pg = chunkInNAND%PAGES_PER_BLOCK;
if(data)
{
x = ned.block[blk]->page[pg]->data;
for(i = 0; i < PAGE_DATA_SIZE; i++)
{
x[i] &=data[i];
}
ned.block[blk]->page[pg]->empty = 0;
}
if(tags)
{
x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
yaffs_PackTags2((yaffs_PackedTags2 *)x,tags);
}
if(tags || data)
{
nandemul_yield(1);
}
return YAFFS_OK;
}
int nandemul2k_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
{
int blk;
int pg;
__u8 *x;
blk = chunkInNAND/PAGES_PER_BLOCK;
pg = chunkInNAND%PAGES_PER_BLOCK;
if(data)
{
memcpy(data,ned.block[blk]->page[pg]->data,PAGE_DATA_SIZE);
}
if(tags)
{
x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
yaffs_UnpackTags2(tags,(yaffs_PackedTags2 *)x);
}
return YAFFS_OK;
}
static int nandemul2k_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
{
int blk;
int pg;
int i;
blk = chunkInNAND/PAGES_PER_BLOCK;
pg = chunkInNAND%PAGES_PER_BLOCK;
for(i = 0; i < PAGE_TOTAL_SIZE; i++)
{
if(ned.block[blk]->page[pg]->data[i] != 0xFF)
{
return YAFFS_FAIL;
}
}
return YAFFS_OK;
}
int nandemul2k_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
{
if(blockNumber < 0 || blockNumber >= ned.nBlocks)
{
T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
}
else if(ned.block[blockNumber]->damaged)
{
T(YAFFS_TRACE_ALWAYS,("Attempt to erase damaged block %d\n",blockNumber));
}
else
{
nandemul_ReallyEraseBlock(blockNumber);
}
return YAFFS_OK;
}
int nandemul2k_InitialiseNAND(yaffs_Device *dev)
{
CheckInit();
return YAFFS_OK;
}
int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
{
__u8 *x;
x = &ned.block[blockNo]->page[0]->data[PAGE_DATA_SIZE];
memset(x,0,sizeof(yaffs_PackedTags2));
return YAFFS_OK;
}
int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
{
yaffs_ExtendedTags tags;
int chunkNo;
*sequenceNumber = 0;
chunkNo = blockNo * dev->nChunksPerBlock;
nandemul2k_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
if(tags.blockBad)
{
*state = YAFFS_BLOCK_STATE_DEAD;
}
else if(!tags.chunkUsed)
{
*state = YAFFS_BLOCK_STATE_EMPTY;
}
else if(tags.chunkUsed)
{
*state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
*sequenceNumber = tags.sequenceNumber;
}
return YAFFS_OK;
}
int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
#endif //YAFFS_RAM_ENABLED
|