1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Testcase for DT overlays()
5  * Copyright (C) 2016 Free Electrons
6  * Copyright (C) 2016 NextThing Co.
7  */
8 
9 #include <stdio.h>
10 
11 #include <libfdt.h>
12 
13 #include "tests.h"
14 
15 #define CHECK(code, expected)					\
16 	{							\
17 		err = (code);					\
18 		if (err != expected)				\
19 			FAIL(#code ": %s", fdt_strerror(err));	\
20 	}
21 
22 /* 4k ought to be enough for anybody */
23 #define FDT_COPY_SIZE	(4 * 1024)
24 
open_dt(char * path)25 static void *open_dt(char *path)
26 {
27 	void *dt, *copy;
28 	int err;
29 
30 	dt = load_blob(path);
31 	copy = xmalloc(FDT_COPY_SIZE);
32 
33 	/*
34 	 * Resize our DTs to 4k so that we have room to operate on
35 	 */
36 	CHECK(fdt_open_into(dt, copy, FDT_COPY_SIZE), 0);
37 
38 	return copy;
39 }
40 
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 	void *fdt_base, *fdt_overlay;
44 	int err;
45 
46 	test_init(argc, argv);
47 	if (argc != 3)
48 		CONFIG("Usage: %s <base dtb> <overlay dtb>", argv[0]);
49 
50 	fdt_base = open_dt(argv[1]);
51 	fdt_overlay = open_dt(argv[2]);
52 
53 	/* Apply the overlay */
54 	CHECK(fdt_overlay_apply(fdt_base, fdt_overlay), -FDT_ERR_BADOVERLAY);
55 
56 	PASS();
57 }
58