tizen 2.4 release
[kernel/u-boot-tm1.git] / fs / yaffs2 / yaffs_packedtags2.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 /* XXX U-BOOT XXX */
15 #include <common.h>
16
17 #include "yaffs_packedtags2.h"
18 #include "yportenv.h"
19 #include "yaffs_tagsvalidity.h"
20
21 /* This code packs a set of extended tags into a binary structure for
22  * NAND storage
23  */
24
25 /* Some of the information is "extra" struff which can be packed in to
26  * speed scanning
27  * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
28  */
29
30 /* Extra flags applied to chunkId */
31
32 #define EXTRA_HEADER_INFO_FLAG  0x80000000
33 #define EXTRA_SHRINK_FLAG       0x40000000
34 #define EXTRA_SHADOWS_FLAG      0x20000000
35 #define EXTRA_SPARE_FLAGS       0x10000000
36
37 #define ALL_EXTRA_FLAGS         0xF0000000
38
39 //#define YAFFS_IGNORE_TAGS_ECC 1
40 /* Also, the top 4 bits of the object Id are set to the object type. */
41 #define EXTRA_OBJECT_TYPE_SHIFT (28)
42 #define EXTRA_OBJECT_TYPE_MASK  ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
43
44 static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
45 {
46         T(YAFFS_TRACE_MTD,
47           (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
48            pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
49            pt->t.sequenceNumber));
50 }
51
52 static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
53 {
54         T(YAFFS_TRACE_MTD,
55           (TSTR
56            ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
57             "%d del %d ser %d seq %d"
58             TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
59            t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
60            t->sequenceNumber));
61
62 }
63
64 void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
65 {
66         pt->t.chunkId = t->chunkId;
67         pt->t.sequenceNumber = t->sequenceNumber;
68         pt->t.byteCount = t->byteCount;
69         pt->t.objectId = t->objectId;
70
71         if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
72                 /* Store the extra header info instead */
73                 /* We save the parent object in the chunkId */
74                 pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
75                         | t->extraParentObjectId;
76                 if (t->extraIsShrinkHeader) {
77                         pt->t.chunkId |= EXTRA_SHRINK_FLAG;
78                 }
79                 if (t->extraShadows) {
80                         pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
81                 }
82
83                 pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
84                 pt->t.objectId |=
85                     (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
86
87                 if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
88                         pt->t.byteCount = t->extraEquivalentObjectId;
89                 } else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
90                         pt->t.byteCount = t->extraFileLength;
91                 } else {
92                         pt->t.byteCount = 0;
93                 }
94         }
95
96         yaffs_DumpPackedTags2(pt);
97         yaffs_DumpTags2(t);
98
99 #ifndef YAFFS_IGNORE_TAGS_ECC
100         {
101                 yaffs_ECCCalculateOther((unsigned char *)&pt->t,
102                                         sizeof(yaffs_PackedTags2TagsPart),
103                                         &pt->ecc);
104         }
105 #endif
106 }
107
108 void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
109 {
110
111         memset(t, 0, sizeof(yaffs_ExtendedTags));
112
113         yaffs_InitialiseTags(t);
114
115         if (pt->t.sequenceNumber != 0xFFFFFFFF) {
116                 /* Page is in use */
117 #ifdef YAFFS_IGNORE_TAGS_ECC
118                 {
119                         t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
120                 }
121 #else
122                 {
123                         yaffs_ECCOther ecc;
124                         int result;
125                         yaffs_ECCCalculateOther((unsigned char *)&pt->t,
126                                                 sizeof
127                                                 (yaffs_PackedTags2TagsPart),
128                                                 &ecc);
129                         result =
130                             yaffs_ECCCorrectOther((unsigned char *)&pt->t,
131                                                   sizeof
132                                                   (yaffs_PackedTags2TagsPart),
133                                                   &pt->ecc, &ecc);
134                         switch(result){
135                                 case 0:
136                                         t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
137                                         break;
138                                 case 1:
139                                         t->eccResult = YAFFS_ECC_RESULT_FIXED;
140                                         break;
141                                 case -1:
142                                         t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
143                                         break;
144                                 default:
145                                         t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
146                         }
147                 }
148 #endif
149                 t->blockBad = 0;
150                 t->chunkUsed = 1;
151                 t->objectId = pt->t.objectId;
152                 t->chunkId = pt->t.chunkId;
153                 t->byteCount = pt->t.byteCount;
154                 t->chunkDeleted = 0;
155                 t->serialNumber = 0;
156                 t->sequenceNumber = pt->t.sequenceNumber;
157
158                 /* Do extra header info stuff */
159
160                 if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
161                         t->chunkId = 0;
162                         t->byteCount = 0;
163
164                         t->extraHeaderInfoAvailable = 1;
165                         t->extraParentObjectId =
166                             pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
167                         t->extraIsShrinkHeader =
168                             (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
169                         t->extraShadows =
170                             (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
171                         t->extraObjectType =
172                             pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
173                         t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
174
175                         if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
176                                 t->extraEquivalentObjectId = pt->t.byteCount;
177                         } else {
178                                 t->extraFileLength = pt->t.byteCount;
179                         }
180                 }
181         }
182
183         yaffs_DumpPackedTags2(pt);
184         yaffs_DumpTags2(t);
185
186 }