summaryrefslogtreecommitdiffstats
path: root/common/bbu.c
blob: 00bec32a860a378bd114938105024ffb9a012b49 (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
/*
 * bbu.c - barebox update functions
 *
 * Copyright (c) 2012 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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.
 */
#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>

static LIST_HEAD(bbu_image_handlers);

int bbu_handlers_iterate(int (*fn)(struct bbu_handler *, void *), void *ctx)
{
	struct bbu_handler *handler;

	list_for_each_entry(handler, &bbu_image_handlers, list) {
		int ret;

		ret = fn(handler, ctx);
		if (ret)
			return ret;
	}

	return 0;
}

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;

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

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

	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)
{
	struct device_node *root_node;
	const char *machine, *str;
	int ret;
	const struct imd_header *of_compat;

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

	of_compat = imd_find_type(data->imd_data, IMD_TYPE_OF_COMPATIBLE);
	if (!of_compat)
		return 0;

	root_node = of_get_root_node();
	if (!root_node)
		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;
	}

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

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

	return 0;
}

static int bbu_check_metadata(struct bbu_data *data)
{
	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_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);
	if (ret)
		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;
};

static int bbu_std_file_handler(struct bbu_handler *handler,
					struct bbu_data *data)
{
	struct bbu_std *std = container_of(handler, struct bbu_std, handler);
	int fd, ret;
	enum filetype filetype;
	struct stat s;
	unsigned oflags = O_WRONLY;

	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;
	}

	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) {
		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;
}

/**
 * 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 checkd 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;

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

	return ret;
}