summaryrefslogtreecommitdiff
path: root/rtas/flash/block_lists.c
blob: e632fd0bd5d468f4fb382e4735957921814cd2b9 (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
/******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <product.h>
#include <stdio.h>
#include "block_lists.h"

unsigned char sig_org[] = FLASHFS_PLATFORM_MAGIC;

/* this function is part of the crc_lib assembler code */
unsigned long check_flash_image(unsigned long, unsigned long, unsigned long);

/* this functions needs to be implemented by the board specific flash code
 * the functions always get 32 bytes and needs to deal with the data */
void write_flash(unsigned long, unsigned short *);

int progress = 0;

int
print_progress(void)
{
	static int i = 3;
	switch (i--) {
	case 3:
		printf("\b|");
		break;
	case 2:
		printf("\b/");
		break;
	case 1:
		printf("\b-");
		break;
	case 0:
		printf("\b\\");
	default:
		i = 3;
	}
	return 0;
}

void
print_hash(void)
{
	printf("\b# ");
}

void
print_writing(void)
{
	int counter = 42;
	printf("\nWriting Flash: |");
	while (counter--)
		printf(" ");
	printf("|");
	counter = 41;
	while (counter--)
		printf("\b");

}

int
get_block_list_version(unsigned char *data)
{
	if (data[0] == 0x01)
		return 1;
	return 0;
}

static long
get_image_size(unsigned long *data, unsigned long length)
{
	long size = 0;
	unsigned long i;
	for (i = 0; i < length / 8; i += 2) {
		size += data[1 + i];
	}
	return size;
}

static long
get_image_size_v0(unsigned long *data)
{
	unsigned long bl_size = data[0];
	return get_image_size(data + 1, bl_size - 8);
}

static long
get_image_size_v1(unsigned long *data)
{
	unsigned long *bl_addr = data;
	unsigned long bl_size;
	unsigned long *next;
	long size = 0;
	while (bl_addr) {
		bl_size = bl_addr[0];
		next = (unsigned long *) bl_addr[1];
		bl_size = bl_size & 0x00FFFFFFFFFFFFFFUL;
		size += get_image_size(bl_addr + 2, bl_size - 0x10);
		bl_addr = next;
	}
	return size;
}

long
get_size(unsigned long *data, int version)
{
	if (version == 1)
		return get_image_size_v1(data);
	return get_image_size_v0(data);
}

static unsigned long
write_one_block(unsigned long *block, unsigned long length,
		unsigned long offset)
{
	unsigned long block_addr = (unsigned long) block;
	unsigned long i = 0;
	static unsigned int hash;
	if (offset == 0)
		hash = 0;

	for (i = 0; i < length; i += 32, offset += 32, block_addr += 32) {
		write_flash(offset, (unsigned short *) block_addr);
		if (offset % 10 == 0) {
			print_progress();
		}
		if (offset > hash * progress) {
			print_hash();
			hash++;
		}
	}

	return offset;
}

static unsigned long
write_one_list(unsigned long *bl, unsigned long length, unsigned long offset)
{
	unsigned long i;
	// 0x10: /8 for pointer /2 it has to be done in steps of 2
	for (i = 0; i < length / 0x10; i++) {
		offset =
		    write_one_block((unsigned long *) *bl, *(bl + 1), offset);
		bl += 2;
	}
	return offset;
}

void
write_block_list(unsigned long *bl, int version)
{
	unsigned long offset = 0;
	unsigned long *bl_addr = bl;
	unsigned long bl_size;
	unsigned long *next;

	if (version == 0) {
		// -8 = removed header length
		write_one_list(bl + 1, *(bl) - 8, offset);
		return;
	}

	while (bl_addr) {
		bl_size = bl_addr[0];
		next = (unsigned long *) bl_addr[1];
		bl_size = bl_size & 0x00FFFFFFFFFFFFFFUL;
		// -0x10 = removed header length
		offset = write_one_list(bl_addr + 2, bl_size - 0x10, offset);
		bl_addr = next;
	}

}

static int
check_one_list(unsigned long *bl, unsigned long length, unsigned long crc)
{
	unsigned long i;
	// 0x10: /8 for pointer /2 it has to be done in steps of 2
	for (i = 0; i < length / 0x10; i++) {
		crc = check_flash_image((unsigned long) *bl, *(bl + 1), crc);
		bl += 2;
	}
	return crc;
}

int
image_check_crc(unsigned long *bl, int version)
{
	unsigned long *bl_addr = bl;
	unsigned long bl_size;
	unsigned long *next;
	unsigned long crc = 0;

	if (version == 0) {
		// -8 = removed header length
		return check_one_list(bl + 1, *(bl) - 8, crc);
	}

	while (bl_addr) {
		bl_size = bl_addr[0];
		next = (unsigned long *) bl_addr[1];
		bl_size = bl_size & 0x00FFFFFFFFFFFFFFUL;
		// -0x10 = removed header length
		crc = check_one_list(bl_addr + 2, bl_size - 0x10, crc);
		bl_addr = next;
	}
	return crc;
}

static int
check_platform_one_list(unsigned long *bl, unsigned long bytesec)
{
	unsigned long pos = bytesec;
	unsigned char *sig_tmp, *sig;
	unsigned long size = 0;
	sig = sig_org;

	while (size < bytesec) {
		size += bl[1];

		while (size > pos) {	// 32 == FLASHFS_PLATFORM_MAGIC length
			sig_tmp = (unsigned char *) (bl[0] + pos);
			if (*sig++ != *sig_tmp)
				return -1;
			if (*sig_tmp == '\0' || (pos == bytesec + 32)) {
				pos = bytesec + 32;
				break;
			}
			pos++;
		}
		if (pos == (bytesec + 32))
			return 0;
		bl += 2;
	}
	return 0;
}

int
check_platform(unsigned long *bl, unsigned int bytesec, int version)
{
	unsigned long *bl_addr = bl;
	unsigned long bl_size;
	unsigned long *next;
	unsigned long *ptr;
	ptr = bl;

	if (version == 0) {
		ptr += 1;	// -8 = removed header length
		return check_platform_one_list(ptr, bytesec);
	}
	while (bl_addr) {
		ptr = bl_addr + 2;	// -0x10 = removed header length
		bl_size = bl_addr[0];
		next = (unsigned long *) bl_addr[1];
		bl_size = bl_size & 0x00FFFFFFFFFFFFFFUL;
		if ((bl_size - 0x10) == 0) {
			bl_addr = next;
			continue;
		}
		if (check_platform_one_list(ptr, bytesec) == 0)
			return 0;

		bl_addr = next;
	}
	return -1;
}