1*448a3816Sskrll /*	$NetBSD: dtb_reverse.c,v 1.1.1.3 2019/12/22 12:34:06 skrll Exp $	*/
229e6f9daSskrll 
3*448a3816Sskrll // SPDX-License-Identifier: LGPL-2.1-or-later
4ad6eb39cSmacallan /*
5ad6eb39cSmacallan  * libfdt - Flat Device Tree manipulation
6ad6eb39cSmacallan  *	Tests if two given dtbs are structurally equal (including order)
7ad6eb39cSmacallan  * Copyright (C) 2010 David Gibson, IBM Corporation.
8ad6eb39cSmacallan  */
9ad6eb39cSmacallan 
10ad6eb39cSmacallan #include <stdlib.h>
11ad6eb39cSmacallan #include <stdio.h>
12ad6eb39cSmacallan #include <string.h>
13ad6eb39cSmacallan #include <stdint.h>
14ad6eb39cSmacallan #include <limits.h>
15ad6eb39cSmacallan 
16ad6eb39cSmacallan #include <libfdt.h>
17ad6eb39cSmacallan 
18ad6eb39cSmacallan #include "tests.h"
19ad6eb39cSmacallan #include "testdata.h"
20ad6eb39cSmacallan 
21ad6eb39cSmacallan #define CHECK(code) \
22ad6eb39cSmacallan 	{ \
23ad6eb39cSmacallan 		err = (code); \
24ad6eb39cSmacallan 		if (err) \
25ad6eb39cSmacallan 			FAIL(#code ": %s", fdt_strerror(err)); \
26ad6eb39cSmacallan 	}
27ad6eb39cSmacallan 
reverse_reservemap(void * in,void * out,int n)28ad6eb39cSmacallan static void reverse_reservemap(void *in, void *out, int n)
29ad6eb39cSmacallan {
30ad6eb39cSmacallan 	int err;
31ad6eb39cSmacallan 	uint64_t addr, size;
32ad6eb39cSmacallan 
33ad6eb39cSmacallan 	verbose_printf("reverse_reservemap(): %d/%d\n",
34ad6eb39cSmacallan 		       n, fdt_num_mem_rsv(in));
35ad6eb39cSmacallan 
36ad6eb39cSmacallan 	if (n < (fdt_num_mem_rsv(in)-1))
37ad6eb39cSmacallan 		reverse_reservemap(in, out, n+1);
38ad6eb39cSmacallan 
39ad6eb39cSmacallan 	CHECK(fdt_get_mem_rsv(in, n, &addr, &size));
40ad6eb39cSmacallan 	CHECK(fdt_add_reservemap_entry(out, addr, size));
41ad6eb39cSmacallan 	verbose_printf("Added entry 0x%llx 0x%llx\n",
42ad6eb39cSmacallan 		       (unsigned long long)addr, (unsigned long long)size);
43ad6eb39cSmacallan }
44ad6eb39cSmacallan 
reverse_properties(void * in,void * out,int offset)45ad6eb39cSmacallan static void reverse_properties(void *in, void *out, int offset)
46ad6eb39cSmacallan {
47ad6eb39cSmacallan 	int err;
48ad6eb39cSmacallan 	int len;
49ad6eb39cSmacallan 	const char *name;
50ad6eb39cSmacallan 	const void *data;
51ad6eb39cSmacallan 
52ad6eb39cSmacallan 	data = fdt_getprop_by_offset(in, offset, &name, &len);
53ad6eb39cSmacallan 	if (!data)
54ad6eb39cSmacallan 		FAIL("fdt_getprop_by_offset(): %s\n", fdt_strerror(len));
55ad6eb39cSmacallan 
56ad6eb39cSmacallan 	verbose_printf("reverse_properties(): offset=%d  name=%s\n",
57ad6eb39cSmacallan 		       offset, name);
58ad6eb39cSmacallan 
59ad6eb39cSmacallan 	offset = fdt_next_property_offset(in, offset);
60ad6eb39cSmacallan 	if (offset >= 0)
61ad6eb39cSmacallan 		reverse_properties(in, out, offset);
62ad6eb39cSmacallan 	else if (offset != -FDT_ERR_NOTFOUND)
63ad6eb39cSmacallan 		FAIL("fdt_next_property_offset(): %s\n", fdt_strerror(offset));
64ad6eb39cSmacallan 
65ad6eb39cSmacallan 	CHECK(fdt_property(out, name, data, len));
66ad6eb39cSmacallan 	verbose_printf("  -> output property %s\n", name);
67ad6eb39cSmacallan }
68ad6eb39cSmacallan 
69ad6eb39cSmacallan static void reverse_node(void *in, void *out, int nodeoffset);
70ad6eb39cSmacallan 
reverse_children(void * in,void * out,int offset)71ad6eb39cSmacallan static void reverse_children(void *in, void *out, int offset)
72ad6eb39cSmacallan {
73ad6eb39cSmacallan 	int err;
74ad6eb39cSmacallan 	int nextoffset = offset;
75ad6eb39cSmacallan 	int depth = 1;
76ad6eb39cSmacallan 
77ad6eb39cSmacallan 	do {
78ad6eb39cSmacallan 		char path[PATH_MAX];
79ad6eb39cSmacallan 
80ad6eb39cSmacallan 		CHECK(fdt_get_path(in, nextoffset, path, sizeof(path)));
81ad6eb39cSmacallan 		verbose_printf("reverse_children() offset=%d nextoffset=%d [%s]"
82ad6eb39cSmacallan 			       " depth=%d\n", offset, nextoffset, path, depth);
83ad6eb39cSmacallan 
84ad6eb39cSmacallan 		nextoffset = fdt_next_node(in, nextoffset, &depth);
85ad6eb39cSmacallan 	} while ((depth >= 0) && (depth != 1));
86ad6eb39cSmacallan 
87ad6eb39cSmacallan 	if (depth == 1)
88ad6eb39cSmacallan 		reverse_children(in, out, nextoffset);
89ad6eb39cSmacallan 
90ad6eb39cSmacallan 	reverse_node(in, out, offset);
91ad6eb39cSmacallan }
92ad6eb39cSmacallan 
reverse_node(void * in,void * out,int nodeoffset)93ad6eb39cSmacallan static void reverse_node(void *in, void *out, int nodeoffset)
94ad6eb39cSmacallan {
95ad6eb39cSmacallan 	const char *name = fdt_get_name(in, nodeoffset, NULL);
96ad6eb39cSmacallan 	char path[PATH_MAX];
97ad6eb39cSmacallan 	int err;
98ad6eb39cSmacallan 	int offset;
99ad6eb39cSmacallan 	int depth = 0;
100ad6eb39cSmacallan 
101ad6eb39cSmacallan 	CHECK(fdt_get_path(in, nodeoffset, path, sizeof(path)));
102ad6eb39cSmacallan 	verbose_printf("reverse_node(): nodeoffset=%d [%s]\n",
103ad6eb39cSmacallan 		       nodeoffset, path);
104ad6eb39cSmacallan 
105ad6eb39cSmacallan 	CHECK(fdt_begin_node(out, name));
106ad6eb39cSmacallan 
107ad6eb39cSmacallan 	offset = fdt_first_property_offset(in, nodeoffset);
108ad6eb39cSmacallan 	if (offset >= 0)
109ad6eb39cSmacallan 		reverse_properties(in, out, offset);
110ad6eb39cSmacallan 	else if (offset != -FDT_ERR_NOTFOUND)
111ad6eb39cSmacallan 		FAIL("fdt_first_property(): %s\n", fdt_strerror(offset));
112ad6eb39cSmacallan 
113ad6eb39cSmacallan 	offset = fdt_next_node(in, nodeoffset, &depth);
114ad6eb39cSmacallan 
115ad6eb39cSmacallan 	if (depth == 1)
116ad6eb39cSmacallan 		reverse_children(in, out, offset);
117ad6eb39cSmacallan 
118ad6eb39cSmacallan 	CHECK(fdt_end_node(out));
119ad6eb39cSmacallan }
120ad6eb39cSmacallan 
main(int argc,char * argv[])121ad6eb39cSmacallan int main(int argc, char *argv[])
122ad6eb39cSmacallan {
123ad6eb39cSmacallan 	void *in, *out;
124ad6eb39cSmacallan 	char outname[PATH_MAX];
125ad6eb39cSmacallan 	int bufsize;
126ad6eb39cSmacallan 	int err;
127ad6eb39cSmacallan 
128ad6eb39cSmacallan 	test_init(argc, argv);
129ad6eb39cSmacallan 	if (argc != 2)
130ad6eb39cSmacallan 		CONFIG("Usage: %s <dtb file>", argv[0]);
131ad6eb39cSmacallan 
132ad6eb39cSmacallan 	in = load_blob(argv[1]);
133ad6eb39cSmacallan 	sprintf(outname, "%s.reversed.test.dtb", argv[1]);
134ad6eb39cSmacallan 
135ad6eb39cSmacallan 	bufsize = fdt_totalsize(in);
136ad6eb39cSmacallan 	out = xmalloc(bufsize);
137ad6eb39cSmacallan 
138ad6eb39cSmacallan 	CHECK(fdt_create(out, bufsize));
139ad6eb39cSmacallan 
140ad6eb39cSmacallan 	fdt_set_boot_cpuid_phys(out, fdt_boot_cpuid_phys(in));
141ad6eb39cSmacallan 
142ad6eb39cSmacallan 	reverse_reservemap(in, out, 0);
143ad6eb39cSmacallan 	CHECK(fdt_finish_reservemap(out));
144ad6eb39cSmacallan 
145ad6eb39cSmacallan 	reverse_node(in, out, 0);
146ad6eb39cSmacallan 
147ad6eb39cSmacallan 	CHECK(fdt_finish(out));
148ad6eb39cSmacallan 
149ad6eb39cSmacallan 	save_blob(outname, out);
150ad6eb39cSmacallan 
151ad6eb39cSmacallan 	PASS();
152ad6eb39cSmacallan }
153