aboutsummaryrefslogtreecommitdiff
path: root/tests/appendprop_addrrange.c
blob: b079fbaec3f4dbb744a3a5709c58145f267f83ff (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
/*
 * libfdt - Flat Device Tree manipulation
 *	Testcase for fdt_appendprop_addrrange()
 * Copyright (C) 2018 AKASHI Takahiro, Linaro Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include <libfdt.h>

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

int main(int argc, char *argv[])
{
	void *fdt, *buf;
	int offset, xac, xsc, num, i, err;
	uint64_t addr, size;

	if (argc != 5)
		CONFIG("Usage: %s <dtb file> <address-cells> <size-cells> <num>\n",
		       argv[0]);

	test_init(argc, argv);
	fdt = load_blob(argv[1]);
	xac = strtol(argv[2], NULL, 10);
	xsc = strtol(argv[3], NULL, 10);
	num = strtol(argv[4], NULL, 10);

	buf = xmalloc(0x1000);
	if (!buf)
		FAIL("Couldn't allocate temporary buffer");
	err = fdt_open_into(fdt, buf, 0x1000);
	if (err)
		FAIL("fdt_open_into(): %s", fdt_strerror(err));

	fdt = buf;

	/* Set up */
	err = fdt_setprop_cell(fdt, 0, "#address-cells", xac);
	if (err)
		FAIL("fdt_setprop_cell(\"#address-cells\"): %s",
		     fdt_strerror(err));
	err = fdt_setprop_cell(fdt, 0, "#size-cells", xsc);
	if (err)
		FAIL("fdt_setprop_cell(\"#size-cells\"): %s",
		     fdt_strerror(err));

	offset = fdt_path_offset(fdt, "/node@1");
	if (offset < 0)
		FAIL("Couldn't find path %s", "/node@1");

	addr = TEST_MEMREGION_ADDR;
	if (xac > 1)
		addr += TEST_MEMREGION_ADDR_HI;
	size = TEST_MEMREGION_SIZE;
	if (xsc > 1)
		size += TEST_MEMREGION_SIZE_HI;

	/*
	 * Do test
	 */
	/* 1. repeat append's */
	for (i = 0; i < num; i++) {
		err = fdt_appendprop_addrrange(fdt, 0, offset,
					       "prop-memregion", addr, size);
		if (err)
			FAIL("Failed to append[%d] \"prop-memregion\": %s",
			     i, fdt_strerror(err));

		check_getprop_addrrange(fdt, 0, offset, "prop-memregion",
					i + 1);

		addr += size;
		size += TEST_MEMREGION_SIZE_INC;
	}

	/* 2. default property name */
	addr = TEST_MEMREGION_ADDR;
	if (xac > 1)
		addr += TEST_MEMREGION_ADDR_HI;
	size = TEST_MEMREGION_SIZE;
	if (xsc > 1)
		size += TEST_MEMREGION_SIZE_HI;

	err = fdt_appendprop_addrrange(fdt, 0, offset, "reg", addr, size);
	if (err)
		FAIL("Failed to set \"reg\": %s", fdt_strerror(err));
	check_getprop_addrrange(fdt, 0, offset, "reg", 1);

	PASS();
}