aboutsummaryrefslogtreecommitdiff
path: root/ubi-utils/src/eb_chain.c
blob: a018ae69d959bd8fdbe64de1f77551f73908be53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (c) International Business Machines Corp., 2006, 2007
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Author:  Drake Dowsett, dowsett@de.ibm.com
 * Contact: Andreas Arnez, arnez@de.ibm.com
 */

/* see eb_chain.h */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "unubi_analyze.h"
#include "crc32.h"

#define COPY(dst, src)							\
	do {								\
		dst = malloc(sizeof(*dst));				\
		if (dst == NULL)					\
			return -ENOMEM;					\
		memcpy(dst, src, sizeof(*dst));				\
	} while (0)


/**
 * inserts an eb_info into the chain starting at head, then searching
 * linearly for the correct position;
 * new should contain valid vid and ec headers and the data_crc should
 * already have been checked before insertion, otherwise the chain
 * could be have un an undesired manner;
 * returns -ENOMEM if alloc fails, otherwise SHOULD always return 0,
 * if not, the code reached the last line and returned -EAGAIN,
 * meaning there is a bug or a case not being handled here;
 **/
int
eb_chain_insert(struct eb_info **head, struct eb_info *new)
{
	uint32_t vol, num, ver;
	uint32_t new_vol, new_num, new_ver;
	struct eb_info *prev, *cur, *hist, *ins;
	struct eb_info **prev_ptr;

	if ((head == NULL) || (new == NULL))
		return 0;

	if (*head == NULL) {
		COPY(*head, new);
		(*head)->next = NULL;
		return 0;
	}

	new_vol = __be32_to_cpu(new->vid.vol_id);
	new_num = __be32_to_cpu(new->vid.lnum);
	new_ver = __be32_to_cpu(new->vid.leb_ver);

	/** TRAVERSE HORIZONTALY **/

	cur = *head;
	prev = NULL;

	/* traverse until vol_id/lnum align */
	vol = __be32_to_cpu(cur->vid.vol_id);
	num = __be32_to_cpu(cur->vid.lnum);
	while ((new_vol > vol) || ((new_vol == vol) && (new_num > num))) {
		/* insert new at end of chain */
		if (cur->next == NULL) {
			COPY(ins, new);
			ins->next = NULL;
			cur->next = ins;
			return 0;
		}

		prev = cur;
		cur = cur->next;
		vol = __be32_to_cpu(cur->vid.vol_id);
		num = __be32_to_cpu(cur->vid.lnum);
	}

	if (prev == NULL)
		prev_ptr = head;
	else
		prev_ptr = &(prev->next);

	/* insert new into the middle of chain */
	if ((new_vol != vol) || (new_num != num)) {
		COPY(ins, new);
		ins->next = cur;
		*prev_ptr = ins;
		return 0;
	}

	/** TRAVERSE VERTICALY **/

	hist = cur;
	prev = NULL;

	/* traverse until versions align */
	ver = __be32_to_cpu(cur->vid.leb_ver);
	while (new_ver < ver) {
		/* insert new at bottom of history */
		if (hist->older == NULL) {
			COPY(ins, new);
			ins->next = NULL;
			ins->older = NULL;
			hist->older = ins;
			return 0;
		}

		prev = hist;
		hist = hist->older;
		ver = __be32_to_cpu(hist->vid.leb_ver);
	}

	if (prev == NULL) {
		/* replace active version */
		COPY(ins, new);
		ins->next = hist->next;
		*prev_ptr = ins;

		/* place cur in vertical histroy */
		ins->older = hist;
		hist->next = NULL;
		return 0;
	}

	/* insert between versions, beneath active version */
	COPY(ins, new);
	ins->next = NULL;
	ins->older = prev->older;
	prev->older = ins;
	return 0;
}


/**
 * sets the pointer at pos to the position of the first entry in the chain
 * with of vol_id and, if given, with the same lnum as *lnum;
 * if there is no entry in the chain, then *pos is NULL on return;
 * always returns 0;
 **/
int
eb_chain_position(struct eb_info **head, uint32_t vol_id, uint32_t *lnum,
		  struct eb_info **pos)
{
	uint32_t vol, num;
	struct eb_info *cur;

	if ((head == NULL) || (*head == NULL) || (pos == NULL))
		return 0;

	*pos = NULL;

	cur = *head;
	while (cur != NULL) {
		vol = __be32_to_cpu(cur->vid.vol_id);
		num = __be32_to_cpu(cur->vid.lnum);

		if ((vol_id == vol) && ((lnum == NULL) || (*lnum == num))) {
			*pos = cur;
			return 0;
		}

		cur = cur->next;
	}

	return 0;
}


/**
 * prints to stream, the vol_id, lnum and leb_ver for each entry in the
 * chain, starting at head;
 * this is intended for debuging purposes;
 * always returns 0;
 *
 * FIXME I do not like the double list traversion ...
 **/
int
eb_chain_print(FILE* stream, struct eb_info *head)
{
	struct eb_info *cur;

	if (stream == NULL)
		stream = stdout;

	if (head == NULL) {
		fprintf(stream, "EMPTY\n");
		return 0;
	}
	/*               012345678012345678012345678012301230123 0123 01234567 0123457 01234567*/
	fprintf(stream, "VOL_ID   LNUM     LEB_VER  EC  VID DAT  PBLK PADDR    DSIZE   EC\n");
	cur = head;
	while (cur != NULL) {
		struct eb_info *hist;

		fprintf(stream, "%08x %-8u %08x %-4s%-4s",
			__be32_to_cpu(cur->vid.vol_id),
			__be32_to_cpu(cur->vid.lnum),
			__be32_to_cpu(cur->vid.leb_ver),
			cur->ec_crc_ok   ? "ok":"bad",
			cur->vid_crc_ok  ? "ok":"bad");
		if (cur->vid.vol_type == UBI_VID_STATIC)
			fprintf(stream, "%-4s", cur->data_crc_ok ? "ok":"bad");
		else	fprintf(stream, "%-4s", cur->data_crc_ok ? "ok":"ign");
		fprintf(stream, " %-4d %08x %-8u %-8llu\n", cur->phys_block,
			cur->phys_addr, __be32_to_cpu(cur->vid.data_size),
			__be64_to_cpu(cur->ec.ec));

		hist = cur->older;
		while (hist != NULL) {
			fprintf(stream, "%08x %-8u %08x %-4s%-4s",
				__be32_to_cpu(hist->vid.vol_id),
				__be32_to_cpu(hist->vid.lnum),
				__be32_to_cpu(hist->vid.leb_ver),
				hist->ec_crc_ok   ? "ok":"bad",
				hist->vid_crc_ok  ? "ok":"bad");
			if (hist->vid.vol_type == UBI_VID_STATIC)
				fprintf(stream, "%-4s", hist->data_crc_ok ? "ok":"bad");
			else	fprintf(stream, "%-4s", hist->data_crc_ok ? "ok":"ign");
			fprintf(stream, " %-4d %08x %-8u %-8llu (*)\n",
				hist->phys_block, hist->phys_addr,
				__be32_to_cpu(hist->vid.data_size),
				__be64_to_cpu(hist->ec.ec));

			hist = hist->older;
		}
		cur = cur->next;
	}

	return 0;
}


/**
 * frees the memory of the entire chain, starting at head;
 * head will be NULL on return;
 * always returns 0;
 **/
int
eb_chain_destroy(struct eb_info **head)
{
	if (head == NULL)
		return 0;

	while (*head != NULL) {
		struct eb_info *cur;
		struct eb_info *hist;

		cur = *head;
		*head = (*head)->next;

		hist = cur->older;
		while (hist != NULL) {
			struct eb_info *temp;

			temp = hist;
			hist = hist->older;
			free(temp);
		}
		free(cur);
	}
	return 0;
}