summaryrefslogtreecommitdiff
path: root/tests/dtb_reverse.c
blob: 95b6c1c90e0ce8c13fb567de81ab92ecb2ac43cb (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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 * libfdt - Flat Device Tree manipulation
 *	Tests if two given dtbs are structurally equal (including order)
 * Copyright (C) 2010 David Gibson, IBM Corporation.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>

#include <libfdt.h>

#include "tests.h"
#include "testdata.h"

#define CHECK(code) \
	{ \
		err = (code); \
		if (err) \
			FAIL(#code ": %s", fdt_strerror(err)); \
	}

static void reverse_reservemap(void *in, void *out, int n)
{
	int err;
	uint64_t addr, size;

	verbose_printf("reverse_reservemap(): %d/%d\n",
		       n, fdt_num_mem_rsv(in));

	if (n < (fdt_num_mem_rsv(in)-1))
		reverse_reservemap(in, out, n+1);

	CHECK(fdt_get_mem_rsv(in, n, &addr, &size));
	CHECK(fdt_add_reservemap_entry(out, addr, size));
	verbose_printf("Added entry 0x%llx 0x%llx\n",
		       (unsigned long long)addr, (unsigned long long)size);
}

static void reverse_properties(void *in, void *out, int offset)
{
	int err;
	int len;
	const char *name;
	const void *data;

	data = fdt_getprop_by_offset(in, offset, &name, &len);
	if (!data)
		FAIL("fdt_getprop_by_offset(): %s\n", fdt_strerror(len));

	verbose_printf("reverse_properties(): offset=%d  name=%s\n",
		       offset, name);

	offset = fdt_next_property_offset(in, offset);
	if (offset >= 0)
		reverse_properties(in, out, offset);
	else if (offset != -FDT_ERR_NOTFOUND)
		FAIL("fdt_next_property_offset(): %s\n", fdt_strerror(offset));

	CHECK(fdt_property(out, name, data, len));
	verbose_printf("  -> output property %s\n", name);
}

static void reverse_node(void *in, void *out, int nodeoffset);

static void reverse_children(void *in, void *out, int offset)
{
	int err;
	int nextoffset = offset;
	int depth = 1;

	do {
		char path[PATH_MAX];

		CHECK(fdt_get_path(in, nextoffset, path, sizeof(path)));
		verbose_printf("reverse_children() offset=%d nextoffset=%d [%s]"
			       " depth=%d\n", offset, nextoffset, path, depth);

		nextoffset = fdt_next_node(in, nextoffset, &depth);
	} while ((depth >= 0) && (depth != 1));

	if (depth == 1)
		reverse_children(in, out, nextoffset);

	reverse_node(in, out, offset);
}

static void reverse_node(void *in, void *out, int nodeoffset)
{
	const char *name = fdt_get_name(in, nodeoffset, NULL);
	char path[PATH_MAX];
	int err;
	int offset;
	int depth = 0;

	CHECK(fdt_get_path(in, nodeoffset, path, sizeof(path)));
	verbose_printf("reverse_node(): nodeoffset=%d [%s]\n",
		       nodeoffset, path);

	CHECK(fdt_begin_node(out, name));

	offset = fdt_first_property_offset(in, nodeoffset);
	if (offset >= 0)
		reverse_properties(in, out, offset);
	else if (offset != -FDT_ERR_NOTFOUND)
		FAIL("fdt_first_property(): %s\n", fdt_strerror(offset));

	offset = fdt_next_node(in, nodeoffset, &depth);

	if (depth == 1)
		reverse_children(in, out, offset);

	CHECK(fdt_end_node(out));
}

int main(int argc, char *argv[])
{
	void *in, *out;
	char outname[PATH_MAX];
	int bufsize;
	int err;

	test_init(argc, argv);
	if (argc != 2)
		CONFIG("Usage: %s <dtb file>", argv[0]);

	in = load_blob(argv[1]);
	sprintf(outname, "%s.reversed.test.dtb", argv[1]);

	bufsize = fdt_totalsize(in);
	out = xmalloc(bufsize);

	CHECK(fdt_create(out, bufsize));

	fdt_set_boot_cpuid_phys(out, fdt_boot_cpuid_phys(in));

	reverse_reservemap(in, out, 0);
	CHECK(fdt_finish_reservemap(out));

	reverse_node(in, out, 0);

	CHECK(fdt_finish(out));

	save_blob(outname, out);

	PASS();
}