comparison packages/fs/yaffs/current/src/ecos-yaffs-nand.c @ 300:9c8ff034abb0

Graft in 20090826 source snapshot
author Ross Younger <wry@ecoscentric.com>
date Tue, 03 Nov 2009 12:15:09 +0000
parents
children 16de60f90faa
comparison
equal deleted inserted replaced
299:3ca4a11adcdf 300:9c8ff034abb0
1 //=============================================================================
2 //
3 // ecos-yaffs-nand.c
4 //
5 // Glue for YAFFS to the eCos NAND library
6 //
7 //=============================================================================
8 // ####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2009 eCosCentric Limited.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later
16 // version.
17 //
18 // eCos is distributed in the hope that it will be useful, but WITHOUT
19 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 //
27 // As a special exception, if other files instantiate templates or use
28 // macros or inline functions from this file, or you compile this file
29 // and link it with other works to produce a work based on this file,
30 // this file does not by itself cause the resulting work to be covered by
31 // the GNU General Public License. However the source code for this file
32 // must still be made available in accordance with section (3) of the GNU
33 // General Public License v2.
34 //
35 // This exception does not invalidate any other reasons why a work based
36 // on this file might be covered by the GNU General Public License.
37 // -------------------------------------------
38 // ####ECOSGPLCOPYRIGHTEND####
39 //=============================================================================
40 //#####DESCRIPTIONBEGIN####
41 //
42 // Author(s): wry
43 // Date: 2009-05-07
44 //
45 //####DESCRIPTIONEND####
46 //=============================================================================
47
48 #include "ecos-yaffs.h"
49 #include "ecos-yaffs-nand.h"
50 #include "yaffs_packedtags1.h"
51 #include "yaffs_packedtags2.h"
52 #include "yaffs_tagscompat.h"
53 #include <cyg/nand/util.h> // for BLOCK2PAGEADDR
54
55 int eyaffs_initialiseNAND (struct yaffs_DeviceStruct * dev)
56 {
57 // Nothing to do - we've already inited it in mount().
58 //cyg_nand_partition *part = (cyg_nand_partition*) dev->genericDevice;
59 return YAFFS_OK;
60 }
61
62 int eyaffs_deinitialiseNAND (struct yaffs_DeviceStruct * dev)
63 {
64 // Nothing to do
65 //cyg_nand_partition *part = (cyg_nand_partition*) dev->genericDevice;
66 return YAFFS_OK;
67 }
68
69 int eyaffs_eraseBlockInNAND (struct yaffs_DeviceStruct * dev, int blockInNAND)
70 {
71 cyg_nand_partition *part = (cyg_nand_partition*) dev->genericDevice;
72 int rv = cyg_nand_erase_block(part, blockInNAND);
73 return rv==0 ? YAFFS_OK : YAFFS_FAIL;
74 }
75
76 // If writing a yaffs_PackedTags1, we store only its eight meaningful bytes
77 #define PACKEDTAGS1_OOBSIZE 8
78
79 int eyaffs_writeChunkWithTagsToNAND (struct yaffs_DeviceStruct * dev,
80 int chunkInNAND, const __u8 * data,
81 const yaffs_ExtendedTags * tags)
82 {
83 cyg_nand_partition *part = (cyg_nand_partition*) dev->genericDevice;
84 int rv;
85
86 if (!dev->isYaffs2) {
87 #ifdef CONFIG_YAFFS_NO_YAFFS1
88 rv = 1;
89 #else
90 yaffs_PackedTags1 pt1;
91
92 if (tags->chunkDeleted) {
93 memset(&pt1, 0xff, sizeof(pt1));
94 pt1.deleted = 0;
95 //((CYG_BYTE*)&pt1)[8] = 0; // TODO: support 9BYTE_TAGS, if there is a demand to (requires corresponding fixup in the ECC check below)
96 } else {
97 yaffs_PackTags1(&pt1, tags);
98 yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
99 }
100 rv = cyg_nand_write_page(part, chunkInNAND, data, data ? dev->nDataBytesPerChunk : 0, &pt1, PACKEDTAGS1_OOBSIZE);
101 #endif
102 } else {
103 #ifdef CYGSEM_FS_YAFFS_OMIT_YAFFS2_CODE
104 return YAFFS_FAIL;
105 #else
106 // YAFFS2 mode.
107 // N.B. We are not doing ECC - the NAND layer does that - so don't
108 // need to bother with the whole tags struct, just the bit that matters.
109
110 if (!data || !tags) {
111 T(YAFFS_TRACE_ERROR,
112 (TSTR("**>> yaffs2: both data and tags must be present" TENDSTR)));
113 YBUG();
114 return YAFFS_FAIL;
115 }
116
117 if (dev->inbandTags) {
118 yaffs_PackedTags2TagsPart *pt2tp;
119 pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
120 yaffs_PackTags2TagsPart(pt2tp, tags);
121 rv = cyg_nand_write_page(part, chunkInNAND, data, dev->totalBytesPerChunk, 0, 0);
122 } else {
123 yaffs_PackedTags2TagsPart pt;
124 yaffs_PackTags2TagsPart(&pt, tags);
125 rv = cyg_nand_write_page(part, chunkInNAND, data, dev->nDataBytesPerChunk, &pt, sizeof(yaffs_PackedTags2TagsPart));
126 }
127 #endif
128 }
129
130 // all done:
131 return rv==0 ? YAFFS_OK : YAFFS_FAIL;
132 }
133
134 int eyaffs_readChunkWithTagsFromNAND (struct yaffs_DeviceStruct * dev,
135 int chunkInNAND, __u8 * data,
136 yaffs_ExtendedTags * tags)
137 {
138 cyg_nand_partition *part = (cyg_nand_partition*) dev->genericDevice;
139 int localData = 0;
140 int rv;
141
142 if (!dev->isYaffs2) {
143 #ifdef CONFIG_YAFFS_NO_YAFFS1
144 rv = 1;
145 #else
146 yaffs_PackedTags1 pt1;
147 memset(&pt1, 0xff, sizeof(yaffs_PackedTags1));
148
149 rv = cyg_nand_read_page(part, chunkInNAND, data, data ? dev->nDataBytesPerChunk : 0, &pt1, PACKEDTAGS1_OOBSIZE);
150
151 if (rv == 0) {
152 if (yaffs_CheckFF((CYG_BYTE*)&pt1, 8)) {
153 // block is empty
154 if (tags) {
155 memset(tags, 0, sizeof(*tags));
156 tags->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
157 }
158 } else {
159 // must check and correct tags' mini-ecc.
160 int deleted = !pt1.deleted; // this would break the tags ECC.
161 pt1.deleted = 1;
162 // TODO: support 9BYTE_TAGS if there is a demand.
163 int rv2 = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
164 if (rv2 < 0) {
165 T(YAFFS_TRACE_ERROR,
166 (TSTR ("**>>yaffs ecc error unfixed on chunk %d:0"
167 TENDSTR), chunkInNAND));
168 dev->eccUnfixed++;
169 rv = EIO;
170 } else {
171 if (tags) {
172 yaffs_UnpackTags1(tags, &pt1);
173 tags->chunkDeleted = deleted;
174 tags->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
175 }
176
177 if (rv2 > 0) {
178 T(YAFFS_TRACE_ERROR,
179 (TSTR ("**>>yaffs ecc tags error fix performed on chunk %d:0"
180 TENDSTR), chunkInNAND));
181 dev->eccFixed++;
182 if (tags)
183 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
184 }
185 }
186 }
187 } // else reading the NAND failed; drop out
188 #endif
189
190 } else { // YAFFS2 mode
191 #ifdef CYGSEM_FS_YAFFS_OMIT_YAFFS2_CODE
192 return YAFFS_FAIL;
193 #else
194 if (dev->inbandTags) {
195 if (!data) {
196 localData = 1;
197 data = yaffs_GetTempBuffer(dev, __LINE__);
198 }
199
200 rv = cyg_nand_read_page(part, chunkInNAND, data, data ? dev->totalBytesPerChunk : 0, 0, 0);
201 if (tags) {
202 yaffs_PackedTags2TagsPart *pt2tp;
203 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
204 yaffs_UnpackTags2TagsPart(tags, pt2tp);
205 }
206 } else {
207 yaffs_PackedTags2TagsPart pt;
208 rv = cyg_nand_read_page(part, chunkInNAND, data, data ? dev->nDataBytesPerChunk : 0, &pt, sizeof(yaffs_PackedTags2TagsPart));
209 if (tags)
210 yaffs_UnpackTags2TagsPart(tags, &pt);
211 }
212 #endif
213 }
214
215 // all done:
216 if (localData)
217 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
218
219 if (rv != 0) {
220 if (tags) {
221 // mark the tags as clearly useless
222 memset(tags, 0, sizeof(yaffs_ExtendedTags));
223 tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
224 tags->blockBad = 1;
225 }
226 return YAFFS_FAIL;
227 }
228 return YAFFS_OK;
229 // TODO: go through yaffs_guts and ensure that all calls to ReadChunksWithTagsFromNAND DTRT on FAIL or if the read tags aren't valid.
230 }
231
232 int eyaffs_markNANDBlockBad (struct yaffs_DeviceStruct * dev, int blockNo)
233 {
234 cyg_nand_partition *part = (cyg_nand_partition*) dev->genericDevice;
235 int rv = cyg_nand_bbt_markbad(part, blockNo);
236 return rv==0 ? YAFFS_OK : YAFFS_FAIL;
237 }
238
239 int eyaffs_queryNANDBlock (struct yaffs_DeviceStruct * dev, int blockNo,
240 yaffs_BlockState * state, __u32 *sequenceNumber)
241 {
242 yaffs_ExtendedTags tags;
243 cyg_nand_page_addr pg;
244 cyg_nand_partition *part = (cyg_nand_partition*) dev->genericDevice;
245
246 int rv = cyg_nand_bbt_query(part, blockNo);
247 if (rv < 0) return YAFFS_FAIL;
248
249 switch(rv) {
250 case CYG_NAND_BBT_OK:
251 // Now we need to read the tags from the OOB area of the first page
252 pg = CYG_NAND_BLOCK2PAGEADDR(part->dev, blockNo);
253 // TODO: This is quite inefficient on small page devices in
254 // YAFFS2 mode with inbandTags enabled, as the whole page has
255 // to be read. Some day, it would be nice to implement part-page
256 // reads in the NAND layer where this can be efficiently done.
257 int rv2 = eyaffs_readChunkWithTagsFromNAND(dev, pg, 0, &tags);
258 if (rv2 == YAFFS_FAIL) {
259 (void) cyg_nand_bbt_markbad(part, blockNo);
260 goto deadblock;
261 }
262 if (tags.chunkUsed) {
263 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
264 *sequenceNumber = tags.sequenceNumber;
265 return YAFFS_OK;
266 }
267 *state = YAFFS_BLOCK_STATE_EMPTY;
268 *sequenceNumber = 0;
269 return YAFFS_OK;
270
271 case CYG_NAND_BBT_WORNBAD:
272 case CYG_NAND_BBT_RESERVED:
273 case CYG_NAND_BBT_FACTORY_BAD:
274 deadblock:
275 *state = YAFFS_BLOCK_STATE_DEAD;
276 *sequenceNumber = 0;
277 return YAFFS_OK;
278 default:
279 YBUG(); // can't happen
280 return YAFFS_FAIL;
281 }
282 }
283