2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This file handles yaffs1-style tags to allow compatibility with Yaffs1 style
16 #include "yaffs_guts.h"
17 #include "yaffs_tagscompat.h"
18 #include "yaffs_ecc.h"
19 #include "yaffs_getblockinfo.h"
20 #include "yaffs_trace.h"
21 #include "yaffs_endian.h"
23 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
26 /********** Tags ECC calculations *********/
28 void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
30 /* Calculate an ecc */
31 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
38 for (i = 0; i < 8; i++) {
39 for (j = 1; j & 0xff; j <<= 1) {
48 int yaffs_check_tags_ecc(struct yaffs_tags *tags)
50 unsigned ecc = tags->ecc;
52 yaffs_calc_tags_ecc(tags);
56 if (ecc && ecc <= 64) {
57 /* TODO: Handle the failure better. Retire? */
58 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
62 b[ecc / 8] ^= (1 << (ecc & 7));
64 /* Now recvalc the ecc */
65 yaffs_calc_tags_ecc(tags);
67 return 1; /* recovered error */
69 /* Wierd ecc failure value */
70 /* TODO Need to do somethiong here */
71 return -1; /* unrecovered error */
76 /********** Tags **********/
79 * During tags storing/retireval we use a copy of the tags so that
80 * we can modify the endian etc without damaging the previous structure.
82 static void yaffs_load_tags_to_spare(struct yaffs_dev *dev,
83 struct yaffs_spare *spare_ptr,
84 struct yaffs_tags *tags_ptr)
86 union yaffs_tags_union *tu_ptr = (union yaffs_tags_union *)tags_ptr;
87 union yaffs_tags_union tags_stored = *tu_ptr;
89 yaffs_calc_tags_ecc(&tags_stored.as_tags);
91 yaffs_do_endian_u32(dev, &tags_stored.as_u32[0]);
92 yaffs_do_endian_u32(dev, &tags_stored.as_u32[1]);
94 spare_ptr->tb0 = tags_stored.as_bytes[0];
95 spare_ptr->tb1 = tags_stored.as_bytes[1];
96 spare_ptr->tb2 = tags_stored.as_bytes[2];
97 spare_ptr->tb3 = tags_stored.as_bytes[3];
98 spare_ptr->tb4 = tags_stored.as_bytes[4];
99 spare_ptr->tb5 = tags_stored.as_bytes[5];
100 spare_ptr->tb6 = tags_stored.as_bytes[6];
101 spare_ptr->tb7 = tags_stored.as_bytes[7];
104 static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
105 struct yaffs_spare *spare_ptr,
106 struct yaffs_tags *tags_ptr)
108 union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
109 union yaffs_tags_union tags_stored;
112 tags_stored.as_bytes[0] = spare_ptr->tb0;
113 tags_stored.as_bytes[1] = spare_ptr->tb1;
114 tags_stored.as_bytes[2] = spare_ptr->tb2;
115 tags_stored.as_bytes[3] = spare_ptr->tb3;
116 tags_stored.as_bytes[4] = spare_ptr->tb4;
117 tags_stored.as_bytes[5] = spare_ptr->tb5;
118 tags_stored.as_bytes[6] = spare_ptr->tb6;
119 tags_stored.as_bytes[7] = spare_ptr->tb7;
121 yaffs_do_endian_u32(dev, &tags_stored.as_u32[0]);
122 yaffs_do_endian_u32(dev, &tags_stored.as_u32[1]);
126 result = yaffs_check_tags_ecc(tags_ptr);
128 dev->n_tags_ecc_fixed++;
130 dev->n_tags_ecc_unfixed++;
133 static void yaffs_spare_init(struct yaffs_spare *spare)
135 memset(spare, 0xff, sizeof(struct yaffs_spare));
138 static int yaffs_wr_nand(struct yaffs_dev *dev,
139 int nand_chunk, const u8 *data,
140 struct yaffs_spare *spare)
142 int data_size = dev->data_bytes_per_chunk;
144 return dev->drv.drv_write_chunk_fn(dev, nand_chunk,
146 (u8 *) spare, sizeof(*spare));
149 static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
152 struct yaffs_spare *spare,
153 enum yaffs_ecc_result *ecc_result,
157 struct yaffs_spare local_spare;
160 int ecc_result1, ecc_result2;
164 /* If we don't have a real spare, then we use a local one. */
165 /* Need this for the calculation of the ecc */
166 spare = &local_spare;
168 data_size = dev->data_bytes_per_chunk;
169 spare_size = sizeof(struct yaffs_spare);
171 if (dev->param.use_nand_ecc)
172 return dev->drv.drv_read_chunk_fn(dev, nand_chunk,
174 (u8 *) spare, spare_size,
178 /* Handle the ECC at this level. */
180 ret_val = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
182 (u8 *)spare, spare_size,
184 if (!data || !correct_errors)
187 /* Do ECC correction if needed. */
188 yaffs_ecc_calc(data, calc_ecc);
189 ecc_result1 = yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
190 yaffs_ecc_calc(&data[256], calc_ecc);
191 ecc_result2 = yaffs_ecc_correct(&data[256], spare->ecc2, calc_ecc);
193 if (ecc_result1 > 0) {
194 yaffs_trace(YAFFS_TRACE_ERROR,
195 "**>>yaffs ecc error fix performed on chunk %d:0",
198 } else if (ecc_result1 < 0) {
199 yaffs_trace(YAFFS_TRACE_ERROR,
200 "**>>yaffs ecc error unfixed on chunk %d:0",
202 dev->n_ecc_unfixed++;
205 if (ecc_result2 > 0) {
206 yaffs_trace(YAFFS_TRACE_ERROR,
207 "**>>yaffs ecc error fix performed on chunk %d:1",
210 } else if (ecc_result2 < 0) {
211 yaffs_trace(YAFFS_TRACE_ERROR,
212 "**>>yaffs ecc error unfixed on chunk %d:1",
214 dev->n_ecc_unfixed++;
217 if (ecc_result1 || ecc_result2) {
218 /* We had a data problem on this page */
219 yaffs_handle_rd_data_error(dev, nand_chunk);
222 if (ecc_result1 < 0 || ecc_result2 < 0)
223 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
224 else if (ecc_result1 > 0 || ecc_result2 > 0)
225 *ecc_result = YAFFS_ECC_RESULT_FIXED;
227 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
233 * Functions for robustisizing
236 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
238 int flash_block = nand_chunk / dev->param.chunks_per_block;
240 /* Mark the block for retirement */
241 yaffs_get_block_info(dev, flash_block + dev->block_offset)->
243 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
244 "**>>Block %d marked for retirement",
248 * Just do a garbage collection on the affected block
249 * then retire the block
254 static int yaffs_tags_compat_wr(struct yaffs_dev *dev,
256 const u8 *data, const struct yaffs_ext_tags *ext_tags)
258 struct yaffs_spare spare;
259 struct yaffs_tags tags;
261 yaffs_spare_init(&spare);
263 if (ext_tags->is_deleted)
264 spare.page_status = 0;
266 tags.obj_id = ext_tags->obj_id;
267 tags.chunk_id = ext_tags->chunk_id;
269 tags.n_bytes_lsb = ext_tags->n_bytes & (1024 - 1);
271 if (dev->data_bytes_per_chunk >= 1024)
272 tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
274 tags.n_bytes_msb = 3;
276 tags.serial_number = ext_tags->serial_number;
278 if (!dev->param.use_nand_ecc && data) {
279 yaffs_ecc_calc(data, spare.ecc1);
280 yaffs_ecc_calc(&data[256], spare.ecc2);
283 yaffs_load_tags_to_spare(dev, &spare, &tags);
285 return yaffs_wr_nand(dev, nand_chunk, data, &spare);
288 static int yaffs_tags_compat_rd(struct yaffs_dev *dev,
290 u8 *data, struct yaffs_ext_tags *ext_tags)
292 struct yaffs_spare spare;
293 struct yaffs_tags tags;
294 enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
295 static struct yaffs_spare spare_ff;
300 memset(&spare_ff, 0xff, sizeof(spare_ff));
304 if (!yaffs_rd_chunk_nand(dev, nand_chunk,
305 data, &spare, &ecc_result, 1))
308 /* ext_tags may be NULL */
312 deleted = (hweight8(spare.page_status) < 7) ? 1 : 0;
314 ext_tags->is_deleted = deleted;
315 ext_tags->ecc_result = ecc_result;
316 ext_tags->block_bad = 0; /* We're reading it */
317 /* therefore it is not a bad block */
318 ext_tags->chunk_used =
319 memcmp(&spare_ff, &spare, sizeof(spare_ff)) ? 1 : 0;
321 if (ext_tags->chunk_used) {
322 yaffs_get_tags_from_spare(dev, &spare, &tags);
324 ext_tags->obj_id = tags.obj_id;
325 ext_tags->chunk_id = tags.chunk_id;
326 ext_tags->n_bytes = tags.n_bytes_lsb;
328 if (dev->data_bytes_per_chunk >= 1024)
330 (((unsigned)tags.n_bytes_msb) << 10);
332 ext_tags->serial_number = tags.serial_number;
338 static int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
340 struct yaffs_spare spare;
342 memset(&spare, 0xff, sizeof(struct yaffs_spare));
344 spare.block_status = 'Y';
346 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
348 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
354 static int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
356 enum yaffs_block_state *state,
359 struct yaffs_spare spare0, spare1;
360 static struct yaffs_spare spare_ff;
362 enum yaffs_ecc_result dummy;
365 memset(&spare_ff, 0xff, sizeof(spare_ff));
371 /* Look for bad block markers in the first two chunks */
372 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block,
373 NULL, &spare0, &dummy, 0);
374 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
375 NULL, &spare1, &dummy, 0);
377 if (hweight8(spare0.block_status & spare1.block_status) < 7)
378 *state = YAFFS_BLOCK_STATE_DEAD;
379 else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
380 *state = YAFFS_BLOCK_STATE_EMPTY;
382 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
387 void yaffs_tags_compat_install(struct yaffs_dev *dev)
389 if(dev->param.is_yaffs2)
391 if(!dev->tagger.write_chunk_tags_fn)
392 dev->tagger.write_chunk_tags_fn = yaffs_tags_compat_wr;
393 if(!dev->tagger.read_chunk_tags_fn)
394 dev->tagger.read_chunk_tags_fn = yaffs_tags_compat_rd;
395 if(!dev->tagger.query_block_fn)
396 dev->tagger.query_block_fn = yaffs_tags_compat_query_block;
397 if(!dev->tagger.mark_bad_fn)
398 dev->tagger.mark_bad_fn = yaffs_tags_compat_mark_bad;