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 /* Also, the top 4 bits of the object Id are set to the object type. */
40 #define EXTRA_OBJECT_TYPE_SHIFT (28)
41 #define EXTRA_OBJECT_TYPE_MASK  ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
42 
43 static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
44 {
45 	T(YAFFS_TRACE_MTD,
46 	  (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
47 	   pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
48 	   pt->t.sequenceNumber));
49 }
50 
51 static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
52 {
53 	T(YAFFS_TRACE_MTD,
54 	  (TSTR
55 	   ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
56 	    "%d del %d ser %d seq %d"
57 	    TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
58 	   t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
59 	   t->sequenceNumber));
60 
61 }
62 
63 void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
64 {
65 	pt->t.chunkId = t->chunkId;
66 	pt->t.sequenceNumber = t->sequenceNumber;
67 	pt->t.byteCount = t->byteCount;
68 	pt->t.objectId = t->objectId;
69 
70 	if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
71 		/* Store the extra header info instead */
72 		/* We save the parent object in the chunkId */
73 		pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
74 			| t->extraParentObjectId;
75 		if (t->extraIsShrinkHeader) {
76 			pt->t.chunkId |= EXTRA_SHRINK_FLAG;
77 		}
78 		if (t->extraShadows) {
79 			pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
80 		}
81 
82 		pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
83 		pt->t.objectId |=
84 		    (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
85 
86 		if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
87 			pt->t.byteCount = t->extraEquivalentObjectId;
88 		} else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
89 			pt->t.byteCount = t->extraFileLength;
90 		} else {
91 			pt->t.byteCount = 0;
92 		}
93 	}
94 
95 	yaffs_DumpPackedTags2(pt);
96 	yaffs_DumpTags2(t);
97 
98 #ifndef YAFFS_IGNORE_TAGS_ECC
99 	{
100 		yaffs_ECCCalculateOther((unsigned char *)&pt->t,
101 					sizeof(yaffs_PackedTags2TagsPart),
102 					&pt->ecc);
103 	}
104 #endif
105 }
106 
107 void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
108 {
109 
110 	memset(t, 0, sizeof(yaffs_ExtendedTags));
111 
112 	yaffs_InitialiseTags(t);
113 
114 	if (pt->t.sequenceNumber != 0xFFFFFFFF) {
115 		/* Page is in use */
116 #ifdef YAFFS_IGNORE_TAGS_ECC
117 		{
118 			t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
119 		}
120 #else
121 		{
122 			yaffs_ECCOther ecc;
123 			int result;
124 			yaffs_ECCCalculateOther((unsigned char *)&pt->t,
125 						sizeof
126 						(yaffs_PackedTags2TagsPart),
127 						&ecc);
128 			result =
129 			    yaffs_ECCCorrectOther((unsigned char *)&pt->t,
130 						  sizeof
131 						  (yaffs_PackedTags2TagsPart),
132 						  &pt->ecc, &ecc);
133 			switch(result){
134 				case 0:
135 					t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
136 					break;
137 				case 1:
138 					t->eccResult = YAFFS_ECC_RESULT_FIXED;
139 					break;
140 				case -1:
141 					t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
142 					break;
143 				default:
144 					t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
145 			}
146 		}
147 #endif
148 		t->blockBad = 0;
149 		t->chunkUsed = 1;
150 		t->objectId = pt->t.objectId;
151 		t->chunkId = pt->t.chunkId;
152 		t->byteCount = pt->t.byteCount;
153 		t->chunkDeleted = 0;
154 		t->serialNumber = 0;
155 		t->sequenceNumber = pt->t.sequenceNumber;
156 
157 		/* Do extra header info stuff */
158 
159 		if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
160 			t->chunkId = 0;
161 			t->byteCount = 0;
162 
163 			t->extraHeaderInfoAvailable = 1;
164 			t->extraParentObjectId =
165 			    pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
166 			t->extraIsShrinkHeader =
167 			    (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
168 			t->extraShadows =
169 			    (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
170 			t->extraObjectType =
171 			    pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
172 			t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
173 
174 			if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
175 				t->extraEquivalentObjectId = pt->t.byteCount;
176 			} else {
177 				t->extraFileLength = pt->t.byteCount;
178 			}
179 		}
180 	}
181 
182 	yaffs_DumpPackedTags2(pt);
183 	yaffs_DumpTags2(t);
184 
185 }
186