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