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