summaryrefslogtreecommitdiffstats
path: root/common/bbu.c
blob: ba2566acdfad982949e53a0394960c299f5867cb (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// SPDX-License-Identifier: GPL-2.0-only
/*
 * bbu.c - barebox update functions
 *
 * Copyright (c) 2012 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 */

#define pr_fmt(fmt) "bbu: " fmt

#include <common.h>
#include <bbu.h>
#include <linux/list.h>
#include <errno.h>
#include <readkey.h>
#include <filetype.h>
#include <libfile.h>
#include <fs.h>
#include <fcntl.h>
#include <malloc.h>
#include <linux/stat.h>
#include <image-metadata.h>
#include <environment.h>
#include <file-list.h>

static LIST_HEAD(bbu_image_handlers);

static void append_bbu_entry(const char *_name,
			     const char *devicefile,
			     struct file_list *files)
{
	char *name;

	name = basprintf("bbu-%s", _name);

	if (file_list_add_entry(files, name, devicefile, 0))
		pr_warn("duplicate partition name %s\n", name);

	free(name);
}

void bbu_append_handlers_to_file_list(struct file_list *files)
{
	struct bbu_handler *handler;

	list_for_each_entry(handler, &bbu_image_handlers, list) {
		const char *cdevname;
		struct stat s;
		char *devpath;

		cdevname = devpath_to_name(handler->devicefile);
		device_detect_by_name(cdevname);

		devpath = basprintf("/dev/%s", cdevname);

		if (stat(devpath, &s) == 0) {
			append_bbu_entry(handler->name, devpath, files);
		} else {
			pr_info("Skipping unavailable handler bbu-%s\n",
				handler->name);
		}

		free(devpath);
	}
}

int bbu_force(struct bbu_data *data, const char *fmt, ...)
{
	va_list args;

	printf("UPDATE: ");

	va_start(args, fmt);

	vprintf(fmt, args);

	va_end(args);

	if (!(data->flags & BBU_FLAG_FORCE))
		goto out;

	if (!data->force)
		goto out;

	data->force--;

	printf(" (forced)\n");

	return 1;
out:
	printf("\n");

	return 0;
}

int bbu_confirm(struct bbu_data *data)
{
	int key;
	const char *prompt;

	if (data->flags & BBU_FLAG_YES)
		prompt = ".";
	else
		prompt = " (y/n)?";

	if (data->imagefile)
		printf("update barebox on %s from %s using handler %s%s\n",
			data->devicefile, data->imagefile,
			data->handler_name, prompt);
	else
		printf("Refresh barebox on %s using handler %s%s\n",
			data->devicefile, data->handler_name, prompt);

	if (data->flags & BBU_FLAG_YES)
		return 0;

	key = read_key();

	if (key == 'y') {
		printf("updating barebox...\n");
		return 0;
	}

	return -EINTR;
}

struct bbu_handler *bbu_find_handler_by_name(const char *name)
{
	struct bbu_handler *handler;

	list_for_each_entry(handler, &bbu_image_handlers, list) {
		if (!name) {
			if (handler->flags & BBU_HANDLER_FLAG_DEFAULT)
				return handler;
			continue;
		}

		if (!strcmp(handler->name, name))
			return handler;
	}

	return NULL;
}

struct bbu_handler *bbu_find_handler_by_device(const char *devicepath)
{
	struct bbu_handler *handler;
	const char *devname = devpath_to_name(devicepath);

	if (!devicepath)
		return NULL;

	list_for_each_entry(handler, &bbu_image_handlers, list) {
		if (!strcmp(handler->devicefile, devicepath))
			return handler;
	}

	if (devname != devicepath)
		return bbu_find_handler_by_device(devname);

	return NULL;
}

static int bbu_check_of_compat(struct bbu_data *data, unsigned short of_compat_nr)
{
	const struct imd_header *imd = data->imd_data;
	const struct imd_header *of_compat;
	struct device_node *root_node;
	const char *machine, *str;
	int ret;

	if (!IS_ENABLED(CONFIG_OFDEVICE) || !IS_ENABLED(CONFIG_IMD))
		return 0;

	root_node = of_get_root_node();
	if (!root_node)
		return 0;

	if (!of_compat_nr)
		return 0;

	ret = of_property_read_string(root_node, "compatible", &machine);
	if (ret)
		return 0;

	for (; of_compat_nr; of_compat_nr--) {
		of_compat = imd_find_type(imd, IMD_TYPE_OF_COMPATIBLE);
		if (!of_compat)
			return 0;

		str = imd_string_data(of_compat, 0);

		if (of_machine_is_compatible(str)) {
			pr_info("Devicetree compatible \"%s\" matches current machine\n", str);
			return 0;
		}

		pr_debug("machine is incompatible with \"%s\", have \"%s\"\n",
			 str, machine);

		imd = of_compat;
	}

	if (!bbu_force(data, "incompatible machine \"%s\"\n", machine))
		return -EINVAL;

	return 0;
}

static int bbu_check_metadata(struct bbu_data *data)
{
	unsigned short imd_of_compat_nr = 0;
	const struct imd_header *imd;
	int ret;
	char *str;

	if (!IS_ENABLED(CONFIG_IMD))
		return 0;

	if (!data->image)
		return 0;

	data->imd_data = imd_get(data->image, data->len);
	if (IS_ERR(data->imd_data)) {
		data->imd_data = NULL;
		return 0;
	}

	printf("Image Metadata:\n");
	imd_for_each(data->imd_data, imd) {
		uint32_t type = imd_read_type(imd);

		if (imd_read_type(imd) == IMD_TYPE_OF_COMPATIBLE)
			imd_of_compat_nr++;

		if (!imd_is_string(type))
			continue;

		str = imd_concat_strings(imd);

		printf("  %s: %s\n", imd_type_to_name(type), str);
		free(str);
	}

	ret = bbu_check_of_compat(data, imd_of_compat_nr);
	if (ret)
		return ret;

	ret = imd_verify_crc32((void *)data->image, data->len);
	if (ret == -EILSEQ && !(data->flags & BBU_FLAG_FORCE))
		return ret;

	return 0;
}

/*
 * do a barebox update with data from *data
 */
int barebox_update(struct bbu_data *data, struct bbu_handler *handler)
{
	int ret;

	if (!data->image && !data->imagefile &&
	    !(handler->flags & BBU_HANDLER_CAN_REFRESH)) {
		pr_err("No Image file given\n");
		return -EINVAL;
	}

	if (!data->handler_name)
		data->handler_name = handler->name;

	if (!data->devicefile)
		data->devicefile = handler->devicefile;

	ret = bbu_check_metadata(data);
	if (ret)
		return ret;

	ret = handler->handler(handler, data);
	if (ret) {
		printf("update %s\n", (ret == -EINTR) ? "aborted" : "failed");
		return ret;
	}

	printf("update succeeded\n");
	return 0;
}

/*
 * print a list of all registered update handlers
 */
void bbu_handlers_list(void)
{
	struct bbu_handler *handler;

	if (list_empty(&bbu_image_handlers))
		printf("(none)\n");

	list_for_each_entry(handler, &bbu_image_handlers, list)
		printf("%s%-11s -> %-10s\n",
				handler->flags & BBU_HANDLER_FLAG_DEFAULT ?
				"* " : "  ",
				handler->name,
				handler->devicefile);
}

/*
 * register a new update handler
 */
int bbu_register_handler(struct bbu_handler *handler)
{
	if (bbu_find_handler_by_name(handler->name))
		return -EBUSY;

	if (handler->flags & BBU_HANDLER_FLAG_DEFAULT &&
	    bbu_find_handler_by_name(NULL))
		return -EBUSY;

	list_add_tail(&handler->list, &bbu_image_handlers);

	return 0;
}

struct bbu_std {
	struct bbu_handler handler;
	enum filetype filetype;
};

int bbu_mmcboot_handler(struct bbu_handler *handler, struct bbu_data *data,
			int (*chained_handler)(struct bbu_handler *, struct bbu_data *))
{
	struct bbu_data _data = *data;
	int ret;
	char *devicefile = NULL, *bootpartvar = NULL, *bootackvar = NULL;
	const char *bootpart;
	const char *devname = devpath_to_name(data->devicefile);

	ret = device_detect_by_name(devname);
	if (ret) {
		pr_err("Couldn't detect device '%s'\n", devname);
		return ret;
	}

	ret = asprintf(&bootpartvar, "%s.boot", devname);
	if (ret < 0)
		return ret;

	bootpart = getenv(bootpartvar);
	if (!bootpart) {
		ret = -ENOENT;
		goto out;
	}

	if (!strcmp(bootpart, "boot0")) {
		bootpart = "boot1";
	} else {
		bootpart = "boot0";
	}

	ret = asprintf(&devicefile, "/dev/%s.%s", devname, bootpart);
	if (ret < 0)
		goto out;

	_data.devicefile = devicefile;

	ret = chained_handler(handler, &_data);
	if (ret < 0)
		goto out;

	if (handler->flags & BBU_HANDLER_FLAG_MMC_BOOT_ACK) {
		ret = asprintf(&bootackvar, "%s.boot_ack", devname);
		if (ret < 0)
			goto out;

		ret = setenv(bootackvar, "1");
		if (ret)
			goto out;
	}

	/* on success switch boot source */
	ret = setenv(bootpartvar, bootpart);

out:
	free(bootackvar);
	free(devicefile);
	free(bootpartvar);

	return ret;
}

static int bbu_internal_mmcboot_update(struct bbu_handler *handler,
				       struct bbu_data *data)
{
	int ret;

	ret = bbu_mmcboot_handler(handler, data, bbu_std_file_handler);
	if (ret == -ENOENT)
		pr_err("Couldn't read the value of .boot parameter\n");

	return ret;
}

int bbu_mmcboot_register_handler(const char *name,
				 const char *devicefile,
				 unsigned long flags)
{
	struct bbu_handler *handler;
	int ret;

	handler = xzalloc(sizeof(*handler));
	handler->devicefile = devicefile;
	handler->name = name;
	handler->handler = bbu_internal_mmcboot_update;
	handler->flags = flags;

	ret = bbu_register_handler(handler);
	if (ret)
		free(handler);

	return ret;
}

int bbu_std_file_handler(struct bbu_handler *handler,
			 struct bbu_data *data)
{
	int fd, ret;
	struct stat s;
	unsigned oflags = O_WRONLY;

	device_detect_by_name(devpath_to_name(data->devicefile));

	ret = stat(data->devicefile, &s);
	if (ret) {
		oflags |= O_CREAT;
	} else {
		if (!S_ISREG(s.st_mode) && s.st_size < data->len) {
			printf("Image (%zd) is too big for device (%lld)\n",
					data->len, s.st_size);
		}
	}

	ret = bbu_confirm(data);
	if (ret)
		return ret;

	fd = open(data->devicefile, oflags);
	if (fd < 0)
		return fd;

	ret = protect(fd, data->len, 0, 0);
	if (ret && (ret != -ENOSYS) && (ret != -ENOTSUPP)) {
		printf("unprotecting %s failed with %s\n", data->devicefile,
				strerror(-ret));
		goto err_close;
	}

	ret = erase(fd, data->len, 0);
	if (ret && ret != -ENOSYS) {
		printf("erasing %s failed with %s\n", data->devicefile,
				strerror(-ret));
		goto err_close;
	}

	ret = write_full(fd, data->image, data->len);
	if (ret < 0)
		goto err_close;

	protect(fd, data->len, 0, 1);

	ret = 0;

err_close:
	close(fd);

	return ret;
}

static int bbu_std_file_handler_checked(struct bbu_handler *handler,
					struct bbu_data *data)
{
	struct bbu_std *std = container_of(handler, struct bbu_std, handler);
	enum filetype filetype;

	filetype = file_detect_type(data->image, data->len);
	if (filetype != std->filetype) {
		if (!bbu_force(data, "incorrect image type. Expected: %s, got %s",
				file_type_to_string(std->filetype),
				file_type_to_string(filetype)))
			return -EINVAL;
	}

	return bbu_std_file_handler(handler, data);
}

/**
 * bbu_register_std_file_update() - register a barebox update handler for a
 *                                  standard file-to-device-copy operation
 * @name:	Name of the handler
 * @flags:	BBU_HANDLER_FLAG_* flags
 * @devicefile: the file to write the update image to
 * @imagetype:	The filetype that the update image must have
 *
 * This update handler us suitable for a standard file-to-device copy operation.
 * The handler checks for a filetype and unprotects/erases the device if
 * necessary. If devicefile belongs to a device then the device is checked for
 * enough space before touching it.
 *
 * Return: 0 if successful, negative error code otherwise
 */
int bbu_register_std_file_update(const char *name, unsigned long flags,
		const char *devicefile, enum filetype imagetype)
{
	struct bbu_std *std;
	struct bbu_handler *handler;
	int ret;

	std = xzalloc(sizeof(*std));
	std->filetype = imagetype;

	handler = &std->handler;

	handler->flags = flags;
	handler->devicefile = devicefile;
	handler->name = name;
	handler->handler = bbu_std_file_handler_checked;

	ret = bbu_register_handler(handler);
	if (ret)
		free(std);

	return ret;
}