Add an image maker tool that generates image files for 2k page * 64 devices
[yaffs2.git] / yaffs_packedtags2.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Charles Manning <charles@aleph1.co.uk>
7  *
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.
11  */
12
13 #include "yaffs_packedtags2.h"
14 #include "yportenv.h"
15 #include "yaffs_trace.h"
16 #include "yaffs_endian.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 chunk_id */
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_dump_packed_tags2_tags_only(
41                                 const struct yaffs_packed_tags2_tags_only *ptt)
42 {
43         (void) ptt;
44
45         yaffs_trace(YAFFS_TRACE_MTD,
46                 "packed tags obj %d chunk %d byte %d seq %d",
47                 ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number);
48 }
49
50 static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt)
51 {
52         yaffs_dump_packed_tags2_tags_only(&pt->t);
53 }
54
55 static void yaffs_dump_tags2(const struct yaffs_ext_tags *t)
56 {
57
58         (void) t;
59         yaffs_trace(YAFFS_TRACE_MTD,
60                 "ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d",
61                 t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
62                 t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
63                 t->seq_number);
64
65 }
66
67 static int yaffs_check_tags_extra_packable(const struct yaffs_ext_tags *t)
68 {
69         if (t->chunk_id != 0 || !t->extra_available)
70                 return 0;
71
72         /* Check if the file size is too long to store */
73         if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE &&
74             (t->extra_file_size >> 31) != 0)
75                 return 0;
76         return 1;
77 }
78
79 void yaffs_pack_tags2_tags_only(struct yaffs_dev *dev,
80                                 struct yaffs_packed_tags2_tags_only *ptt,
81                                 const struct yaffs_ext_tags *t)
82 {
83         ptt->chunk_id = t->chunk_id;
84         ptt->seq_number = t->seq_number;
85         ptt->n_bytes = t->n_bytes;
86         ptt->obj_id = t->obj_id;
87
88         /* Only store extra tags for object headers.
89          * If it is a file then only store  if the file size is short\
90          * enough to fit.
91          */
92         if (yaffs_check_tags_extra_packable(t)) {
93                 /* Store the extra header info instead */
94                 /* We save the parent object in the chunk_id */
95                 ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id;
96                 if (t->extra_is_shrink)
97                         ptt->chunk_id |= EXTRA_SHRINK_FLAG;
98                 if (t->extra_shadows)
99                         ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
100
101                 ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
102                 ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
103
104                 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
105                         ptt->n_bytes = t->extra_equiv_id;
106                 else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
107                         ptt->n_bytes = (unsigned) t->extra_file_size;
108                 else
109                         ptt->n_bytes = 0;
110         }
111
112         yaffs_dump_packed_tags2_tags_only(ptt);
113         yaffs_dump_tags2(t);
114         yaffs_do_endian_packed_tags2(dev, ptt);
115 }
116
117 void yaffs_pack_tags2(struct yaffs_dev *dev,
118                       struct yaffs_packed_tags2 *pt,
119                       const struct yaffs_ext_tags *t, int tags_ecc)
120 {
121         yaffs_pack_tags2_tags_only(dev, &pt->t, t);
122
123         if (tags_ecc)
124                 yaffs_ecc_calc_other((unsigned char *)&pt->t,
125                                     sizeof(struct yaffs_packed_tags2_tags_only),
126                                     &pt->ecc);
127 }
128
129 void yaffs_unpack_tags2_tags_only(struct yaffs_dev *dev,
130                                   struct yaffs_ext_tags *t,
131                                   struct yaffs_packed_tags2_tags_only *ptt_ptr)
132 {
133         struct yaffs_packed_tags2_tags_only ptt_copy = *ptt_ptr;
134
135         memset(t, 0, sizeof(struct yaffs_ext_tags));
136
137         if (ptt_copy.seq_number == 0xffffffff)
138                 return;
139
140         yaffs_do_endian_packed_tags2(dev, &ptt_copy);
141
142         t->block_bad = 0;
143         t->chunk_used = 1;
144         t->obj_id = ptt_copy.obj_id;
145         t->chunk_id = ptt_copy.chunk_id;
146         t->n_bytes = ptt_copy.n_bytes;
147         t->is_deleted = 0;
148         t->serial_number = 0;
149         t->seq_number = ptt_copy.seq_number;
150
151         /* Do extra header info stuff */
152         if (ptt_copy.chunk_id & EXTRA_HEADER_INFO_FLAG) {
153                 t->chunk_id = 0;
154                 t->n_bytes = 0;
155
156                 t->extra_available = 1;
157                 t->extra_parent_id = ptt_copy.chunk_id & (~(ALL_EXTRA_FLAGS));
158                 t->extra_is_shrink = ptt_copy.chunk_id & EXTRA_SHRINK_FLAG ? 1 : 0;
159                 t->extra_shadows = ptt_copy.chunk_id & EXTRA_SHADOWS_FLAG ? 1 : 0;
160                 t->extra_obj_type = ptt_copy.obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
161                 t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
162
163                 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
164                         t->extra_equiv_id = ptt_copy.n_bytes;
165                 else
166                         t->extra_file_size = ptt_copy.n_bytes;
167         }
168         yaffs_dump_packed_tags2_tags_only(ptt_ptr);
169         yaffs_dump_tags2(t);
170 }
171
172 void yaffs_unpack_tags2(struct yaffs_dev *dev,
173                         struct yaffs_ext_tags *t,
174                         struct yaffs_packed_tags2 *pt,
175                         int tags_ecc)
176 {
177         enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
178
179         if (pt->t.seq_number != 0xffffffff && tags_ecc) {
180                 /* Chunk is in use and we need to do ECC */
181
182                 struct yaffs_ecc_other ecc;
183                 int result;
184                 yaffs_ecc_calc_other((unsigned char *)&pt->t,
185                                 sizeof(struct yaffs_packed_tags2_tags_only),
186                                 &ecc);
187                 result =
188                     yaffs_ecc_correct_other((unsigned char *)&pt->t,
189                                 sizeof(struct yaffs_packed_tags2_tags_only),
190                                 &pt->ecc, &ecc);
191                 switch (result) {
192                 case 0:
193                         ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
194                         break;
195                 case 1:
196                         ecc_result = YAFFS_ECC_RESULT_FIXED;
197                         break;
198                 case -1:
199                         ecc_result = YAFFS_ECC_RESULT_UNFIXED;
200                         break;
201                 default:
202                         ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
203                 }
204         }
205         yaffs_unpack_tags2_tags_only(dev, t, &pt->t);
206
207         t->ecc_result = ecc_result;
208
209         yaffs_dump_packed_tags2(pt);
210         yaffs_dump_tags2(t);
211 }