xref: /aosp_15_r20/external/dtc/tests/parent_offset.c (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1*cd60bc56SAndroid Build Coastguard Worker // SPDX-License-Identifier: LGPL-2.1-or-later
2*cd60bc56SAndroid Build Coastguard Worker /*
3*cd60bc56SAndroid Build Coastguard Worker  * libfdt - Flat Device Tree manipulation
4*cd60bc56SAndroid Build Coastguard Worker  *	Testcase for fdt_parent_offset()
5*cd60bc56SAndroid Build Coastguard Worker  * Copyright (C) 2006 David Gibson, IBM Corporation.
6*cd60bc56SAndroid Build Coastguard Worker  */
7*cd60bc56SAndroid Build Coastguard Worker #include <stdlib.h>
8*cd60bc56SAndroid Build Coastguard Worker #include <stdio.h>
9*cd60bc56SAndroid Build Coastguard Worker #include <string.h>
10*cd60bc56SAndroid Build Coastguard Worker #include <stdint.h>
11*cd60bc56SAndroid Build Coastguard Worker 
12*cd60bc56SAndroid Build Coastguard Worker #include <libfdt.h>
13*cd60bc56SAndroid Build Coastguard Worker 
14*cd60bc56SAndroid Build Coastguard Worker #include "tests.h"
15*cd60bc56SAndroid Build Coastguard Worker #include "testdata.h"
16*cd60bc56SAndroid Build Coastguard Worker 
path_parent_len(const char * path)17*cd60bc56SAndroid Build Coastguard Worker static int path_parent_len(const char *path)
18*cd60bc56SAndroid Build Coastguard Worker {
19*cd60bc56SAndroid Build Coastguard Worker 	const char *p = strrchr(path, '/');
20*cd60bc56SAndroid Build Coastguard Worker 
21*cd60bc56SAndroid Build Coastguard Worker 	if (!p)
22*cd60bc56SAndroid Build Coastguard Worker 		TEST_BUG();
23*cd60bc56SAndroid Build Coastguard Worker 	if (p == path)
24*cd60bc56SAndroid Build Coastguard Worker 		return 1;
25*cd60bc56SAndroid Build Coastguard Worker 	else
26*cd60bc56SAndroid Build Coastguard Worker 		return p - path;
27*cd60bc56SAndroid Build Coastguard Worker }
28*cd60bc56SAndroid Build Coastguard Worker 
check_path(struct fdt_header * fdt,const char * path)29*cd60bc56SAndroid Build Coastguard Worker static void check_path(struct fdt_header *fdt, const char *path)
30*cd60bc56SAndroid Build Coastguard Worker {
31*cd60bc56SAndroid Build Coastguard Worker 	char *parentpath;
32*cd60bc56SAndroid Build Coastguard Worker 	int nodeoffset, parentoffset, parentpathoffset, pathparentlen;
33*cd60bc56SAndroid Build Coastguard Worker 
34*cd60bc56SAndroid Build Coastguard Worker 	pathparentlen = path_parent_len(path);
35*cd60bc56SAndroid Build Coastguard Worker 	parentpath = alloca(pathparentlen + 1);
36*cd60bc56SAndroid Build Coastguard Worker 	strncpy(parentpath, path, pathparentlen);
37*cd60bc56SAndroid Build Coastguard Worker 	parentpath[pathparentlen] = '\0';
38*cd60bc56SAndroid Build Coastguard Worker 
39*cd60bc56SAndroid Build Coastguard Worker 	verbose_printf("Path: \"%s\"\tParent: \"%s\"\n", path, parentpath);
40*cd60bc56SAndroid Build Coastguard Worker 
41*cd60bc56SAndroid Build Coastguard Worker 	nodeoffset = fdt_path_offset(fdt, path);
42*cd60bc56SAndroid Build Coastguard Worker 	if (nodeoffset < 0)
43*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(nodeoffset));
44*cd60bc56SAndroid Build Coastguard Worker 
45*cd60bc56SAndroid Build Coastguard Worker 	parentpathoffset = fdt_path_offset(fdt, parentpath);
46*cd60bc56SAndroid Build Coastguard Worker 	if (parentpathoffset < 0)
47*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_path_offset(%s): %s", parentpath,
48*cd60bc56SAndroid Build Coastguard Worker 		     fdt_strerror(parentpathoffset));
49*cd60bc56SAndroid Build Coastguard Worker 
50*cd60bc56SAndroid Build Coastguard Worker 	parentoffset = fdt_parent_offset(fdt, nodeoffset);
51*cd60bc56SAndroid Build Coastguard Worker 	if (parentoffset < 0)
52*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_parent_offset(): %s", fdt_strerror(parentoffset));
53*cd60bc56SAndroid Build Coastguard Worker 
54*cd60bc56SAndroid Build Coastguard Worker 	if (parentoffset != parentpathoffset)
55*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_parent_offset() returns %d instead of %d",
56*cd60bc56SAndroid Build Coastguard Worker 		     parentoffset, parentpathoffset);
57*cd60bc56SAndroid Build Coastguard Worker }
58*cd60bc56SAndroid Build Coastguard Worker 
main(int argc,char * argv[])59*cd60bc56SAndroid Build Coastguard Worker int main(int argc, char *argv[])
60*cd60bc56SAndroid Build Coastguard Worker {
61*cd60bc56SAndroid Build Coastguard Worker 	void *fdt;
62*cd60bc56SAndroid Build Coastguard Worker 	int err;
63*cd60bc56SAndroid Build Coastguard Worker 
64*cd60bc56SAndroid Build Coastguard Worker 	test_init(argc, argv);
65*cd60bc56SAndroid Build Coastguard Worker 	fdt = load_blob_arg(argc, argv);
66*cd60bc56SAndroid Build Coastguard Worker 
67*cd60bc56SAndroid Build Coastguard Worker 	check_path(fdt, "/subnode@1");
68*cd60bc56SAndroid Build Coastguard Worker 	check_path(fdt, "/subnode@2");
69*cd60bc56SAndroid Build Coastguard Worker 	check_path(fdt, "/subnode@1/subsubnode");
70*cd60bc56SAndroid Build Coastguard Worker 	check_path(fdt, "/subnode@2/subsubnode@0");
71*cd60bc56SAndroid Build Coastguard Worker 	err = fdt_parent_offset(fdt, 0);
72*cd60bc56SAndroid Build Coastguard Worker 	if (err != -FDT_ERR_NOTFOUND)
73*cd60bc56SAndroid Build Coastguard Worker 		FAIL("fdt_parent_offset(/) returns %d instead of "
74*cd60bc56SAndroid Build Coastguard Worker 		     "-FDT_ERR_NOTFOUND", err);
75*cd60bc56SAndroid Build Coastguard Worker 
76*cd60bc56SAndroid Build Coastguard Worker 	PASS();
77*cd60bc56SAndroid Build Coastguard Worker }
78