File: boot_raw_fallback.c

package info (click to toggle)
rauc 1.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,348 kB
  • sloc: ansic: 37,058; python: 3,354; sh: 1,393; xml: 53; makefile: 41
file content (465 lines) | stat: -rw-r--r-- 12,957 bytes parent folder | download
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
#include <locale.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib/gstdio.h>

#include "update_handler.h"
#include "common.h"
#include "context.h"

typedef struct {
	gchar *tmpdir;
} BootRawFallbackFixture;

#define BIT(nr) (1UL << (nr))
typedef enum {
	OPT_NONE                      = 0,
	OPT_EXPECT_FAIL               = BIT(0),
	OPT_PRE_INSTALL_PRIMARY_HEAD  = BIT(1),
	OPT_PRE_INSTALL_PRIMARY_IMG   = BIT(2),
	OPT_PRE_INSTALL_FALLBACK_HEAD = BIT(3),
	OPT_PRE_INSTALL_FALLBACK_IMG  = BIT(4),
	OPT_DUMMY_FILL_PRIMARY        = BIT(5),
	OPT_DUMMY_FILL_FALLBACK       = BIT(6),
} BootRawFallbackOptions;

typedef struct {
	BootRawFallbackOptions options;

	GQuark err_domain;
	gint err_code;

	gsize image_size;
	guint64 region_start;
	guint64 region_size;
} BootRawFallbackData;

static gboolean process_file(const gchar *filepath,
		gsize offset,
		gsize size,
		gchar fill,
		gboolean random,
		gboolean compare)
{
	GError *error = NULL;
	g_autoptr(GFile) file = NULL;
	g_autoptr(GFileIOStream) stream = NULL;
	g_autofree gchar *buf = NULL;
	g_autofree gchar *cmp_buf = NULL;
	const gsize buf_size = 4096;
	g_autoptr(GRand) rand = g_rand_new_with_seed(31415);
	gboolean res;

	file = g_file_new_for_path(filepath);
	stream = g_file_open_readwrite(file, NULL, &error);
	g_assert_no_error(error);
	g_assert_nonnull(stream);

	res = g_seekable_seek(G_SEEKABLE(stream), offset, G_SEEK_SET, NULL, &error);
	g_assert_no_error(error);
	g_assert_true(res);

	buf = g_malloc(buf_size);
	memset(buf, fill, buf_size);

	if (compare)
		cmp_buf = g_malloc(buf_size);

	while (size) {
		gsize bytes_to_process = size < buf_size ? size : buf_size;
		gsize bytes_processed;

		if (random) {
			for (gsize i = 0; i < bytes_to_process; i++)
				buf[i] = g_rand_int(rand);
		}

		if (compare) {
			res = g_input_stream_read_all(g_io_stream_get_input_stream(G_IO_STREAM(stream)),
					cmp_buf,
					bytes_to_process,
					&bytes_processed,
					NULL,
					&error);

			if (memcmp(buf, cmp_buf, bytes_processed)) {
				return FALSE;
			}
		} else {
			res = g_output_stream_write_all(g_io_stream_get_output_stream(G_IO_STREAM(stream)),
					buf,
					bytes_to_process,
					&bytes_processed,
					NULL,
					&error);
		}
		g_assert_no_error(error);
		g_assert_true(res);

		size -= bytes_processed;
	}
	return TRUE;
}

static gboolean fill_file(const gchar *filepath,
		gsize offset,
		gsize size,
		gchar fill,
		gboolean random)
{
	return process_file(filepath, offset, size, fill, random, FALSE);
}

static gboolean compare_file(const gchar *filepath,
		gsize offset,
		gsize size,
		gchar fill,
		gboolean random)
{
	return process_file(filepath, offset, size, fill, random, TRUE);
}

static void boot_raw_fallback_fixture_set_up(BootRawFallbackFixture *fixture,
		gconstpointer user_data)
{
	fixture->tmpdir = g_dir_make_tmp("rauc-XXXXXX", NULL);
	g_assert_nonnull(fixture->tmpdir);
}

static void boot_raw_fallback_fixture_tear_down(BootRawFallbackFixture *fixture,
		gconstpointer user_data)
{
	g_assert_true(rm_tree(fixture->tmpdir, NULL));
	g_free(fixture->tmpdir);
}

static RaucImage* create_source_image(const gchar *dirname,
		const gchar *imagename,
		gsize size)
{
	RaucImage *image;
	g_autofree gchar *imagepath = g_build_filename(dirname, imagename, NULL);

	image = r_new_image();
	image->slotclass = g_strdup("rootfs");
	image->filename = g_strdup(imagepath);
	image->checksum.size = size;
	image->checksum.digest = g_strdup("0xdeadbeef");
	image->type = g_strdup("raw");

	g_assert(test_prepare_dummy_file(dirname, imagename, size, "/dev/zero") == 0);

	fill_file(imagepath, 0, size, 0x00, TRUE);

	return image;
}

static void test_boot_raw_fallback(BootRawFallbackFixture *fixture,
		gconstpointer user_data)
{
	BootRawFallbackData *data = (BootRawFallbackData*) user_data;
	gchar *slotpath, *mountprefix, *hookpath = NULL;
	gsize slot_size;
	RaucImage *image;
	RaucSlot *targetslot;
	img_to_slot_handler handler;
	GError *ierror = NULL;
	gboolean res = FALSE;
	const gsize sector_size = 512;
	gsize header_size;

	/* needs to run as root */
	if (!test_running_as_root())
		return;

	/* prepare slot */
	slotpath = g_strdup(g_getenv("RAUC_TEST_BLOCK_LOOP"));
	if (!slotpath) {
		g_test_message("no block device for testing found (define RAUC_TEST_BLOCK_LOOP)");
		g_test_skip("RAUC_TEST_BLOCK_LOOP undefined");
		return;
	}

	slot_size = get_file_size(slotpath, &ierror);
	g_assert_no_error(ierror);
	g_assert(slot_size != 0);

	fill_file(slotpath, 0, slot_size, 0x00, FALSE);

	header_size = sector_size;
	if (data->image_size < header_size)
		header_size = data->image_size;

	/* pre-install parts of primary location */
	if (data->options & OPT_PRE_INSTALL_PRIMARY_HEAD)
		g_assert_true(fill_file(slotpath, data->region_start, header_size, 0x00, TRUE));

	if (data->options & OPT_PRE_INSTALL_PRIMARY_IMG) {
		g_assert_true(fill_file(slotpath, data->region_start, data->image_size, 0x00, TRUE));
		if (!(data->options & OPT_PRE_INSTALL_PRIMARY_HEAD))
			g_assert_true(fill_file(slotpath, data->region_start, header_size, 0x00, FALSE));
	}

	/* pre-install parts of fallback location */
	if (data->options & OPT_PRE_INSTALL_FALLBACK_HEAD)
		g_assert_true(fill_file(slotpath, data->region_start + (data->region_size / 2), header_size, 0x00, TRUE));

	if (data->options & OPT_PRE_INSTALL_FALLBACK_IMG) {
		g_assert_true(fill_file(slotpath, data->region_start + (data->region_size / 2), data->image_size, 0x00, TRUE));
		if (!(data->options & OPT_PRE_INSTALL_FALLBACK_HEAD))
			g_assert_true(fill_file(slotpath, data->region_start + (data->region_size / 2), data->image_size, 0x00, FALSE));
	}

	/* pre-fill primary partition with dummy data */
	if (data->options & OPT_DUMMY_FILL_PRIMARY)
		g_assert_true(fill_file(slotpath, data->region_start, (data->region_size / 2), 0xAA, FALSE));

	/* pre-fill fallback location with dummy data */
	if (data->options & OPT_DUMMY_FILL_FALLBACK)
		g_assert_true(fill_file(slotpath, data->region_start + (data->region_size / 2), (data->region_size / 2), 0xAA, FALSE));

	image = create_source_image(fixture->tmpdir, "image.img", data->image_size);
	g_assert_nonnull(image);

	/* create target slot */
	targetslot = g_new0(RaucSlot, 1);
	targetslot->name = g_intern_string("bootloader.0");
	targetslot->sclass = g_intern_string("bootloader");
	targetslot->device = g_strdup(slotpath);
	targetslot->type = g_strdup("boot-raw-fallback");
	targetslot->region_start = data->region_start;
	targetslot->region_size = data->region_size;
	targetslot->state = ST_INACTIVE;

	/* Set mount path to current temp dir */
	mountprefix = g_build_filename(fixture->tmpdir, "testmount", NULL);
	g_assert_nonnull(mountprefix);
	r_context_conf()->mountprefix = mountprefix;
	r_context();
	g_assert(g_mkdir(mountprefix, 0777) == 0);

	/* get handler for this */
	handler = get_update_handler(image, targetslot, &ierror);
	g_assert_no_error(ierror);
	g_assert_nonnull(handler);

	/* Monitor if the images are updated in the correct order */
	if (!(data->options & OPT_EXPECT_FAIL)) {
		const gchar *names[] = { "primary", "fallback" };
		gint index = 0;

		if (data->options & (OPT_PRE_INSTALL_PRIMARY_HEAD | OPT_DUMMY_FILL_PRIMARY))
			index++;

		for (gint i = 0; i < 2; i++) {
			gchar *msg;
			msg = g_strdup_printf("Updating %s partition at*", names[index % 2]);
			g_test_expect_message(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, msg);
			g_free(msg);
			index++;
		}
	}

	/* Run to perform an update */
	res = handler(image, targetslot, hookpath, &ierror);

	if (data->options & OPT_EXPECT_FAIL) {
		g_assert_error(ierror, data->err_domain, data->err_code);
		g_clear_error(&ierror);
		g_assert_false(res);
		goto out;
	} else {
		g_assert_no_error(ierror);
		g_assert_true(res);
	}

	g_test_assert_expected_messages();

	/* Check if the area before the primary image is untouched */
	g_assert_true(compare_file(slotpath,
			0,
			data->region_start,
			0x00,
			FALSE));

	/* Verify primary image */
	g_assert_true(compare_file(slotpath,
			data->region_start,
			data->image_size,
			0x00,
			TRUE));

	/* Verify that the gap between the primary and fallback image is untouched */
	g_assert_true(compare_file(slotpath,
			data->region_start + data->image_size,
			(data->region_size / 2) - data->image_size,
			0x00,
			FALSE));

	/* Verify fallback image */
	g_assert_true(compare_file(slotpath,
			data->region_start + (data->region_size / 2),
			data->image_size,
			0x00,
			TRUE));

	/* Check if the area after the fallback partition is untouched */
	g_assert_true(compare_file(slotpath,
			data->region_start + (data->region_size / 2) + data->image_size,
			slot_size - (data->region_start + (data->region_size / 2) + data->image_size),
			0x00,
			FALSE));

out:
	g_assert(g_remove(image->filename) == 0);

	g_rmdir(mountprefix);

	g_free(slotpath);
	g_clear_pointer(&hookpath, g_free);
	g_free(mountprefix);
	r_free_image(image);
	r_slot_free(targetslot);
}

#define R_QUOTE(...) #__VA_ARGS__
int main(int argc, char *argv[])
{
	BootRawFallbackData *data;

	setlocale(LC_ALL, "C");

	g_assert(g_setenv("GIO_USE_VFS", "local", TRUE));

	g_test_init(&argc, &argv, NULL);

	/* Standard scenario - fully programmed and valid slot */
	data = &(BootRawFallbackData) {
		.options = OPT_PRE_INSTALL_PRIMARY_HEAD | OPT_PRE_INSTALL_PRIMARY_IMG | OPT_PRE_INSTALL_FALLBACK_HEAD | OPT_PRE_INSTALL_FALLBACK_IMG,
		.err_domain = 0,
		.err_code = 0,
		.image_size = (1 * 1024 * 1024),
		.region_start = (0 * 1024 * 1024),
		.region_size = (4 * 1024 * 1024),
	};
	g_test_add("/boot_raw_fallback/all-valid",
			BootRawFallbackFixture,
			data,
			boot_raw_fallback_fixture_set_up,
			test_boot_raw_fallback,
			boot_raw_fallback_fixture_tear_down);

	/* Interrupted scenario - primary header missing but fallback header and image valid */
	data = &(BootRawFallbackData) {
		.options = OPT_PRE_INSTALL_PRIMARY_IMG | OPT_PRE_INSTALL_FALLBACK_HEAD | OPT_PRE_INSTALL_FALLBACK_IMG,
		.err_domain = 0,
		.err_code = 0,
		.image_size = (1 * 1024 * 1024),
		.region_start = (0 * 1024 * 1024),
		.region_size = (4 * 1024 * 1024),
	};
	g_test_add("/boot_raw_fallback/primary-header-missing",
			BootRawFallbackFixture,
			data,
			boot_raw_fallback_fixture_set_up,
			test_boot_raw_fallback,
			boot_raw_fallback_fixture_tear_down);

	/* Basic test on complete blank slot */
	data = &(BootRawFallbackData) {
		.options = OPT_NONE,
		.err_domain = 0,
		.err_code = 0,
		.image_size = (1 * 1024 * 1024),
		.region_start = (0 * 1024 * 1024),
		.region_size = (4 * 1024 * 1024),
	};
	g_test_add("/boot_raw_fallback/all-blank",
			BootRawFallbackFixture,
			data,
			boot_raw_fallback_fixture_set_up,
			test_boot_raw_fallback,
			boot_raw_fallback_fixture_tear_down);

	/* Basic test on pre-filled (not cleared) partitions */
	data = &(BootRawFallbackData) {
		.options = OPT_DUMMY_FILL_PRIMARY | OPT_DUMMY_FILL_FALLBACK,
		.err_domain = 0,
		.err_code = 0,
		.image_size = (1 * 1024 * 1024),
		.region_start = (0 * 1024 * 1024),
		.region_size = (4 * 1024 * 1024),
	};
	g_test_add("/boot_raw_fallback/prefilled-partitions",
			BootRawFallbackFixture,
			data,
			boot_raw_fallback_fixture_set_up,
			test_boot_raw_fallback,
			boot_raw_fallback_fixture_tear_down);

	/* Image smaller than sector size */
	data = &(BootRawFallbackData) {
		.options = OPT_NONE,
		.err_domain = 0,
		.err_code = 0,
		.image_size = (16),
		.region_start = (0 * 1024),
		.region_size = (2 * 1024),
	};
	g_test_add("/boot_raw_fallback/small-image",
			BootRawFallbackFixture,
			data,
			boot_raw_fallback_fixture_set_up,
			test_boot_raw_fallback,
			boot_raw_fallback_fixture_tear_down);

	/* Image larger than available size */
	data = &(BootRawFallbackData) {
		.options = OPT_EXPECT_FAIL,
		.err_domain = R_UPDATE_ERROR,
		.err_code = R_UPDATE_ERROR_FAILED,
		.image_size = (3 *  1024),
		.region_start = (0 * 1024),
		.region_size = (4 * 1024),
	};
	g_test_add("/boot_raw_fallback/image-too-big",
			BootRawFallbackFixture,
			data,
			boot_raw_fallback_fixture_set_up,
			test_boot_raw_fallback,
			boot_raw_fallback_fixture_tear_down);

	/* Unaligned region start */
	data = &(BootRawFallbackData) {
		.options = OPT_EXPECT_FAIL,
		.err_domain = R_UPDATE_ERROR,
		.err_code = R_UPDATE_ERROR_FAILED,
		.image_size = (1 *  1024),
		.region_start = (0 * 1024) + 2,
		.region_size = (4 * 1024),
	};
	g_test_add("/boot_raw_fallback/unaligned-region-start",
			BootRawFallbackFixture,
			data,
			boot_raw_fallback_fixture_set_up,
			test_boot_raw_fallback,
			boot_raw_fallback_fixture_tear_down);

	/* Unaligned region size */
	data = &(BootRawFallbackData) {
		.options = OPT_EXPECT_FAIL,
		.err_domain = R_UPDATE_ERROR,
		.err_code = R_UPDATE_ERROR_FAILED,
		.image_size = (1 *  1024),
		.region_start = (0 * 1024),
		.region_size = (4 * 1024) + 2,
	};
	g_test_add("/boot_raw_fallback/unaligned-region-size",
			BootRawFallbackFixture,
			data,
			boot_raw_fallback_fixture_set_up,
			test_boot_raw_fallback,
			boot_raw_fallback_fixture_tear_down);

	return g_test_run();
}