xref: /netbsd-src/external/gpl2/dtc/dist/tests/value-labels.c (revision cc7d2833ecf67da5a5ddc470841931eb9f6723e4)
1*cc7d2833Sskrll /*	$NetBSD: value-labels.c,v 1.1.1.3 2019/12/22 12:34:06 skrll Exp $	*/
2d89652e2Sskrll 
3*cc7d2833Sskrll // SPDX-License-Identifier: LGPL-2.1-or-later
4b8ae3907Smacallan /*
5b8ae3907Smacallan  * libfdt - Flat Device Tree manipulation
6b8ae3907Smacallan  *	Test labels within values
7b8ae3907Smacallan  * Copyright (C) 2008 David Gibson, IBM Corporation.
8b8ae3907Smacallan  */
9b8ae3907Smacallan 
10b8ae3907Smacallan #include <stdlib.h>
11b8ae3907Smacallan #include <stdio.h>
12b8ae3907Smacallan #include <string.h>
13b8ae3907Smacallan #include <stdint.h>
14b8ae3907Smacallan #include <errno.h>
15b8ae3907Smacallan 
16b8ae3907Smacallan #include <dlfcn.h>
17b8ae3907Smacallan 
18b8ae3907Smacallan #include <libfdt.h>
19b8ae3907Smacallan 
20b8ae3907Smacallan #include "tests.h"
21b8ae3907Smacallan #include "testdata.h"
22b8ae3907Smacallan 
23b8ae3907Smacallan struct val_label {
24b8ae3907Smacallan 	const char *labelname;
25b8ae3907Smacallan 	int propoff;
26b8ae3907Smacallan };
27b8ae3907Smacallan 
28d89652e2Sskrll static struct val_label labels1[] = {
29b8ae3907Smacallan 	{ "start1", 0 },
30b8ae3907Smacallan 	{ "mid1", 2 },
31b8ae3907Smacallan 	{ "end1", -1 },
32b8ae3907Smacallan };
33b8ae3907Smacallan 
34d89652e2Sskrll static struct val_label labels2[] = {
35b8ae3907Smacallan 	{ "start2", 0 },
36b8ae3907Smacallan 	{ "innerstart2", 0 },
37b8ae3907Smacallan 	{ "innermid2", 4 },
38b8ae3907Smacallan 	{ "innerend2", -1 },
39b8ae3907Smacallan 	{ "end2", -1 },
40b8ae3907Smacallan };
41b8ae3907Smacallan 
42d89652e2Sskrll static struct val_label labels3[] = {
43b8ae3907Smacallan 	{ "start3", 0 },
44b8ae3907Smacallan 	{ "innerstart3", 0 },
45b8ae3907Smacallan 	{ "innermid3", 1 },
46b8ae3907Smacallan 	{ "innerend3", -1 },
47b8ae3907Smacallan 	{ "end3", -1 },
48b8ae3907Smacallan };
49b8ae3907Smacallan 
check_prop_labels(void * sohandle,void * fdt,const char * name,const struct val_label * labels,int n)50b8ae3907Smacallan static void check_prop_labels(void *sohandle, void *fdt, const char *name,
51b8ae3907Smacallan 			      const struct val_label* labels, int n)
52b8ae3907Smacallan {
53b8ae3907Smacallan 	const struct fdt_property *prop;
54b8ae3907Smacallan 	const char *p;
55b8ae3907Smacallan 	int len;
56b8ae3907Smacallan 	int i;
57b8ae3907Smacallan 
58b8ae3907Smacallan 	prop = fdt_get_property(fdt, 0, name, &len);
59b8ae3907Smacallan 	if (!prop)
60b8ae3907Smacallan 		FAIL("Couldn't locate property \"%s\"", name);
61b8ae3907Smacallan 
62b8ae3907Smacallan 	p = dlsym(sohandle, name);
63b8ae3907Smacallan 	if (!p)
64b8ae3907Smacallan 		FAIL("Couldn't locate label symbol \"%s\"", name);
65b8ae3907Smacallan 
66b8ae3907Smacallan 	if (p != (const char *)prop)
67b8ae3907Smacallan 		FAIL("Label \"%s\" does not point to correct property", name);
68b8ae3907Smacallan 
69b8ae3907Smacallan 	for (i = 0; i < n; i++) {
70b8ae3907Smacallan 		int off = labels[i].propoff;
71b8ae3907Smacallan 
72b8ae3907Smacallan 		if (off == -1)
73b8ae3907Smacallan 			off = len;
74b8ae3907Smacallan 
75b8ae3907Smacallan 		p = dlsym(sohandle, labels[i].labelname);
76b8ae3907Smacallan 		if (!p)
77b8ae3907Smacallan 			FAIL("Couldn't locate label symbol \"%s\"", name);
78b8ae3907Smacallan 
79b8ae3907Smacallan 		if ((p - prop->data) != off)
80b8ae3907Smacallan 			FAIL("Label \"%s\" points to offset %ld instead of %d"
81b8ae3907Smacallan 			     "in property \"%s\"", labels[i].labelname,
82b8ae3907Smacallan 			     (long)(p - prop->data), off, name);
83b8ae3907Smacallan 	}
84b8ae3907Smacallan }
85b8ae3907Smacallan 
main(int argc,char * argv[])86b8ae3907Smacallan int main(int argc, char *argv[])
87b8ae3907Smacallan {
88b8ae3907Smacallan 	void *sohandle;
89b8ae3907Smacallan 	void *fdt;
90b8ae3907Smacallan 	int err;
91b8ae3907Smacallan 
92b8ae3907Smacallan 	test_init(argc, argv);
93b8ae3907Smacallan 	if (argc != 2)
94b8ae3907Smacallan 		CONFIG("Usage: %s <so file>", argv[0]);
95b8ae3907Smacallan 
96b8ae3907Smacallan 	sohandle = dlopen(argv[1], RTLD_NOW);
97b8ae3907Smacallan 	if (!sohandle)
98b8ae3907Smacallan 		FAIL("Couldn't dlopen() %s", argv[1]);
99b8ae3907Smacallan 
100b8ae3907Smacallan 	fdt = dlsym(sohandle, "dt_blob_start");
101b8ae3907Smacallan 	if (!fdt)
102b8ae3907Smacallan 		FAIL("Couldn't locate \"dt_blob_start\" symbol in %s",
103b8ae3907Smacallan 		     argv[1]);
104b8ae3907Smacallan 
105b8ae3907Smacallan 	err = fdt_check_header(fdt);
106b8ae3907Smacallan 	if (err != 0)
107b8ae3907Smacallan 		FAIL("%s contains invalid tree: %s", argv[1],
108b8ae3907Smacallan 		     fdt_strerror(err));
109b8ae3907Smacallan 
110b8ae3907Smacallan 
111b8ae3907Smacallan 	check_prop_labels(sohandle, fdt, "prop1", labels1, ARRAY_SIZE(labels1));
112b8ae3907Smacallan 	check_prop_labels(sohandle, fdt, "prop2", labels2, ARRAY_SIZE(labels2));
113b8ae3907Smacallan 	check_prop_labels(sohandle, fdt, "prop3", labels3, ARRAY_SIZE(labels3));
114b8ae3907Smacallan 
115b8ae3907Smacallan 	PASS();
116b8ae3907Smacallan }
117