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