summaryrefslogtreecommitdiff
path: root/ubi-utils/src/reader.c
blob: 7935a154db6863c2326ed7a02152174621dfbfc1 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Copyright (c) International Business Machines Corp., 2006
 *
 * 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: Oliver Lohmann
 *
 * Read in PFI (partial flash image) data and store it into internal
 * data structures for further processing. Take also care about
 * special handling if the data contains PDD (platform description
 * data/boot-parameters).
 */

#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include "bootenv.h"
#include "reader.h"

#define __unused __attribute__((unused))

/* @FIXME hard coded offsets right now - get them from Artem? */
#define NAND2048_DEFAULT_VID_HDR_OFF 1984
#define NAND512_DEFAULT_VID_HDR_OFF  448
#define NOR_DEFAULT_VID_HDR_OFF      64

#define EBUF_PFI(fmt...)						\
	do { int i = snprintf(err_buf, err_buf_size, "%s\n", label);	\
	     snprintf(err_buf + i, err_buf_size - i, fmt);		\
	} while (0)

#define EBUF(fmt...) \
	do { snprintf(err_buf, err_buf_size, fmt); } while (0)


int
read_pdd_data(FILE* fp_pdd, pdd_data_t* pdd_data,
	      char* err_buf, size_t err_buf_size)
{
	int rc = 0;
	bootenv_t pdd = NULL;
	pdd_data_t res = NULL;
	const char* value;

	res = (pdd_data_t) malloc(sizeof(struct pdd_data));
	if (!res) {
		rc = -ENOMEM;
		goto err;
	}
	rc = bootenv_create(&pdd);
	if (rc != 0) {
		goto err;
	}
	rc = bootenv_read_txt(fp_pdd, pdd);
	if (rc != 0) {
		goto err;
	}
	rc = bootenv_get(pdd, "flash_type", &value);
	if (rc != 0) {
		goto err;
	}

	if (strcmp(value, "NAND") == 0) {

		rc = bootenv_get_num(pdd, "flash_page_size",
			     &(res->flash_page_size));
		if (rc != 0) {
			EBUF("Cannot read 'flash_page_size' from pdd.");
			goto err;
		}
		res->flash_type = NAND_FLASH;

		switch (res->flash_page_size) {
		case 512:
			res->vid_hdr_offset = NAND512_DEFAULT_VID_HDR_OFF;
			break;
		case 2048:
			res->vid_hdr_offset = NAND2048_DEFAULT_VID_HDR_OFF;
			break;
		default:
			EBUF("Unsupported  'flash_page_size' %d.",
			     res->flash_page_size);
			goto err;
		}
	}
	else if (strcmp(value, "NOR") == 0){
		res->flash_type = NOR_FLASH;
		res->vid_hdr_offset = NOR_DEFAULT_VID_HDR_OFF;
	}
	else {
		snprintf(err_buf, err_buf_size,
			 "Unkown flash type: %s", value);
		goto err;
	}

	rc = bootenv_get_num(pdd, "flash_eraseblock_size",
			     &(res->eb_size));
	if (rc != 0) {
		EBUF("Cannot read 'flash_eraseblock_size' from pdd.");
		goto err;
	}

	rc = bootenv_get_num(pdd, "flash_size",
			     &(res->flash_size));
	if (rc != 0) {
		EBUF("Cannot read 'flash_size' from pdd.");
		goto err;
	}

	goto out;
 err:
	if (res) {
		free(res);
		res = NULL;
	}
 out:
	bootenv_destroy(&pdd);
	*pdd_data = res;
	return rc;
}

int
read_pfi_raw(pfi_header pfi_hd, FILE* fp_pfi __unused, pfi_raw_t* pfi_raw,
	     const char* label, char* err_buf, size_t err_buf_size)
{
	int rc = 0;
	char tmp_str[PFI_KEYWORD_LEN];
	bootenv_list_t raw_start_list = NULL;
	pfi_raw_t res;

	res = (pfi_raw_t) malloc(sizeof(struct pfi_raw));
	if (!res)
		return -ENOMEM;

	rc = pfi_header_getnumber(pfi_hd, "size", &(res->data_size));
	if (rc != 0) {
		EBUF_PFI("Cannot read 'size' from PFI.");
		goto err;
	}

	rc = pfi_header_getnumber(pfi_hd, "crc", &(res->crc));
	if (rc != 0) {
		EBUF_PFI("Cannot read 'crc' from PFI.");
		goto err;
	}

	rc = pfi_header_getstring(pfi_hd, "raw_starts",
				  tmp_str, PFI_KEYWORD_LEN);
	if (rc != 0) {
		EBUF_PFI("Cannot read 'raw_starts' from PFI.");
		goto err;
	}

	rc = bootenv_list_create(&raw_start_list);
	if (rc != 0) {
		goto err;
	}

	rc = bootenv_list_import(raw_start_list, tmp_str);
	if (rc != 0) {
		EBUF_PFI("Cannot translate PFI value: %s", tmp_str);
		goto err;
	}

	rc = bootenv_list_to_num_vector(raw_start_list,
					(void *) &(res->starts_size),
					&(res->starts));
	if (rc != 0) {
		EBUF_PFI("Cannot create numeric value array: %s", tmp_str);
		goto err;
	}

	goto out;

 err:
	if (res) {
		free(res);
		res = NULL;
	}
 out:
	bootenv_list_destroy(&raw_start_list);
	*pfi_raw = res;
	return rc;
}

int
read_pfi_ubi(pfi_header pfi_hd, FILE* fp_pfi __unused, pfi_ubi_t* pfi_ubi,
	     const char *label, char* err_buf, size_t err_buf_size)
{
	int rc = 0;
	const char** tmp_names = NULL;
	char tmp_str[PFI_KEYWORD_LEN];
	bootenv_list_t ubi_id_list = NULL;
	bootenv_list_t ubi_name_list = NULL;
	pfi_ubi_t res;
	uint32_t i;

	res = (pfi_ubi_t) calloc(1, sizeof(struct pfi_ubi));
	if (!res)
		return -ENOMEM;

	rc = pfi_header_getnumber(pfi_hd, "size", &(res->data_size));
	if (rc != 0) {
		EBUF_PFI("Cannot read 'size' from PFI.");
		goto err;
	}

	rc = pfi_header_getnumber(pfi_hd, "crc", &(res->crc));
	if (rc != 0) {
		EBUF_PFI("Cannot read 'crc' from PFI.");
		goto err;
	}

	rc = pfi_header_getstring(pfi_hd, "ubi_ids", tmp_str, PFI_KEYWORD_LEN);
	if (rc != 0) {
		EBUF_PFI("Cannot read 'ubi_ids' from PFI.");
		goto err;
	}

	rc = bootenv_list_create(&ubi_id_list);
	if (rc != 0) {
		goto err;
	}
	rc = bootenv_list_create(&ubi_name_list);
	if (rc != 0) {
		goto err;
	}

	rc = bootenv_list_import(ubi_id_list, tmp_str);
	if (rc != 0) {
		EBUF_PFI("Cannot translate PFI value: %s", tmp_str);
		goto err;
	}

	rc = bootenv_list_to_num_vector(ubi_id_list, (void *) &(res->ids_size),
					&(res->ids));
	if (rc != 0) {
		EBUF_PFI("Cannot create numeric value array: %s", tmp_str);
		goto err;
	}

	if (res->ids_size == 0) {
		rc = -1;
		EBUF_PFI("Sanity check failed: No ubi_ids specified.");
		goto err;
	}

	rc = pfi_header_getstring(pfi_hd, "ubi_type",
				  tmp_str, PFI_KEYWORD_LEN);
	if (rc != 0) {
		EBUF_PFI("Cannot read 'ubi_type' from PFI.");
		goto err;
	}
	if (strcmp(tmp_str, "static") == 0)
		res->type = pfi_ubi_static;
	else if (strcmp(tmp_str, "dynamic") == 0)
		res->type = pfi_ubi_dynamic;
	else {
		EBUF_PFI("Unknown ubi_type in PFI.");
		goto err;
	}

	rc = pfi_header_getnumber(pfi_hd, "ubi_alignment", &(res->alignment));
	if (rc != 0) {
		EBUF_PFI("Cannot read 'ubi_alignment' from PFI.");
		goto err;
	}

	rc = pfi_header_getnumber(pfi_hd, "ubi_size", &(res->size));
	if (rc != 0) {
		EBUF_PFI("Cannot read 'ubi_size' from PFI.");
		goto err;
	}

	rc = pfi_header_getstring(pfi_hd, "ubi_names",
				  tmp_str, PFI_KEYWORD_LEN);
	if (rc != 0) {
		EBUF_PFI("Cannot read 'ubi_names' from PFI.");
		goto err;
	}

	rc = bootenv_list_import(ubi_name_list, tmp_str);
	if (rc != 0) {
		EBUF_PFI("Cannot translate PFI value: %s", tmp_str);
		goto err;
	}
	rc = bootenv_list_to_vector(ubi_name_list, (void *) &(res->names_size),
				    &(tmp_names));
	if (rc != 0) {
		EBUF_PFI("Cannot create string array: %s", tmp_str);
		goto err;
	}

	if (res->names_size != res->ids_size) {
		EBUF_PFI("Sanity check failed: ubi_ids list does not match "
			 "sizeof ubi_names list.");
		rc = -1;
	}

	/* copy tmp_names to own structure */
	res->names = (char**) calloc(1, res->names_size * sizeof (char*));
	if (res->names == NULL)
		goto err;

	for (i = 0; i < res->names_size; i++) {
		res->names[i] = calloc(PFI_UBI_VOL_NAME_LEN + 1, sizeof(char));
		if (res->names[i] == NULL)
			goto err;
		strncpy(res->names[i], tmp_names[i], PFI_UBI_VOL_NAME_LEN + 1);
	}

	goto out;

 err:
	if (res) {
		if (res->names) {
			for (i = 0; i < res->names_size; i++) {
				if (res->names[i]) {
					free(res->names[i]);
				}
			}
			free(res->names);
		}
		if (res->ids) {
			free(res->ids);
		}
		free(res);
		res = NULL;
	}

 out:
	bootenv_list_destroy(&ubi_id_list);
	bootenv_list_destroy(&ubi_name_list);
	if (tmp_names != NULL)
		free(tmp_names);
	*pfi_ubi = res;
	return rc;
}


int
free_pdd_data(pdd_data_t* pdd_data)
{
	if (*pdd_data) {
		free(*pdd_data);
	}
	*pdd_data = NULL;

	return 0;
}

int
free_pfi_raw(pfi_raw_t* pfi_raw)
{
	pfi_raw_t tmp = *pfi_raw;
	if (tmp) {
		if (tmp->starts)
			free(tmp->starts);
		free(tmp);
	}
	*pfi_raw = NULL;

	return 0;
}

int
free_pfi_ubi(pfi_ubi_t* pfi_ubi)
{
	size_t i;
	pfi_ubi_t tmp = *pfi_ubi;
	if (tmp) {
		if (tmp->ids)
			free(tmp->ids);
		if (tmp->names) {
			for (i = 0; i < tmp->names_size; i++) {
				if (tmp->names[i]) {
					free(tmp->names[i]);
				}
			}
			free(tmp->names);
		}
		free(tmp);
	}
	*pfi_ubi = NULL;

	return 0;
}


int
read_pfi_headers(list_t *pfi_raws, list_t *pfi_ubis, FILE* fp_pfi,
		 char* err_buf, size_t err_buf_size)
{
	int rc = 0;
	char mode[PFI_KEYWORD_LEN];
	char label[PFI_LABEL_LEN];

	*pfi_raws = mk_empty(); pfi_raw_t raw = NULL;
	*pfi_ubis = mk_empty(); pfi_ubi_t ubi = NULL;
	pfi_header pfi_header = NULL;

	/* read all headers from PFI and store them in lists */
	rc = pfi_header_init(&pfi_header);
	if (rc != 0) {
		EBUF("Cannot initialize pfi header.");
		goto err;
	}
	while ((rc == 0) && !feof(fp_pfi)) {
		rc = pfi_header_read(fp_pfi, pfi_header);
		if (rc != 0) {
			if (rc == PFI_DATA_START) {
				rc = 0;
				break; /* data section starts,
					  all headers read */
			}
			else {
				goto err;
			}
		}
		rc = pfi_header_getstring(pfi_header, "label", label,
					  PFI_LABEL_LEN);
		if (rc != 0) {
			EBUF("Cannot read 'label' from PFI.");
			goto err;
		}
		rc = pfi_header_getstring(pfi_header, "mode", mode,
					  PFI_KEYWORD_LEN);
		if (rc != 0) {
			EBUF("Cannot read 'mode' from PFI.");
			goto err;
		}
		if (strcmp(mode, "ubi") == 0) {
			rc = read_pfi_ubi(pfi_header, fp_pfi, &ubi, label,
					  err_buf, err_buf_size);
			if (rc != 0) {
				goto err;
			}
			*pfi_ubis = append_elem(ubi, *pfi_ubis);
		}
		else if (strcmp(mode, "raw") == 0) {
			rc = read_pfi_raw(pfi_header, fp_pfi, &raw, label,
					  err_buf, err_buf_size);
			if (rc != 0) {
				goto err;
			}
			*pfi_raws = append_elem(raw, *pfi_raws);
		}
		else {
			EBUF("Recvieved unknown mode from PFI: %s", mode);
			goto err;
		}
	}
	goto out;

 err:
	*pfi_raws = remove_all((free_func_t)&free_pfi_raw, *pfi_raws);
	*pfi_ubis = remove_all((free_func_t)&free_pfi_ubi, *pfi_ubis);
 out:
	pfi_header_destroy(&pfi_header);
	return rc;

}