xref: /netbsd-src/external/gpl2/dtc/dist/tests/check_path.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: check_path.c,v 1.1.1.2 2019/12/22 12:34:07 skrll Exp $	*/
2 
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 /*
5  * libfdt - Flat Device Tree manipulation
6  *	Testcase for node existence
7  * Copyright (C) 2016 Konsulko Inc.
8  */
9 
10 #include <stdio.h>
11 
12 #include <libfdt.h>
13 
14 #include "tests.h"
15 
16 #define CHECK(code) \
17 	{ \
18 		int err = (code); \
19 		if (err) \
20 			FAIL(#code ": %s", fdt_strerror(err)); \
21 	}
22 
23 /* 4k ought to be enough for anybody */
24 #define FDT_COPY_SIZE	(4 * 1024)
25 
26 static void *open_dt(char *path)
27 {
28 	void *dt, *copy;
29 
30 	dt = load_blob(path);
31 	copy = xmalloc(FDT_COPY_SIZE);
32 
33 	/*
34 	 * Resize our DTs to 4k so that we have room to operate on
35 	 */
36 	CHECK(fdt_open_into(dt, copy, FDT_COPY_SIZE));
37 
38 	return copy;
39 }
40 
41 int main(int argc, char *argv[])
42 {
43 	void *fdt_base;
44 	int fail_config, exists, check_exists;
45 
46 	test_init(argc, argv);
47 	fail_config = 0;
48 
49 	if (argc != 4)
50 		fail_config = 1;
51 
52 	if (!fail_config) {
53 		if (!strcmp(argv[2], "exists"))
54 			check_exists = 1;
55 		else if (!strcmp(argv[2], "not-exists"))
56 			check_exists = 0;
57 		else
58 			fail_config = 1;
59 	}
60 
61 	if (fail_config)
62 		CONFIG("Usage: %s <base dtb> <[exists|not-exists]> <node-path>", argv[0]);
63 
64 	fdt_base = open_dt(argv[1]);
65 
66 	exists = fdt_path_offset(fdt_base, argv[3]) >= 0;
67 
68 	if (exists == check_exists)
69 		PASS();
70 	else
71 		FAIL();
72 }
73