1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Testcase for fdt_setprop()
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include <stdint.h>
13
14 #include <libfdt.h>
15
16 #include "tests.h"
17 #include "testdata.h"
18
19 #define SPACE 65536
20 #define NEW_STRING "here is quite a long test string, blah blah blah"
21
main(int argc,char * argv[])22 int main(int argc, char *argv[])
23 {
24 void *fdt;
25 void *buf;
26 const uint32_t *intp;
27 const char *strp;
28 int err;
29
30 test_init(argc, argv);
31 fdt = load_blob_arg(argc, argv);
32
33 buf = xmalloc(SPACE);
34
35 err = fdt_open_into(fdt, buf, SPACE);
36 if (err)
37 FAIL("fdt_open_into(): %s", fdt_strerror(err));
38
39 fdt = buf;
40
41 intp = check_getprop_cell(fdt, 0, "prop-int", TEST_VALUE_1);
42
43 verbose_printf("Old int value was 0x%08x\n", *intp);
44 err = fdt_setprop_string(fdt, 0, "prop-int", NEW_STRING);
45 if (err)
46 FAIL("Failed to set \"prop-int\" to \"%s\": %s",
47 NEW_STRING, fdt_strerror(err));
48
49 strp = check_getprop_string(fdt, 0, "prop-int", NEW_STRING);
50 verbose_printf("New value is \"%s\"\n", strp);
51
52 strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
53 TEST_STRING_1);
54
55 verbose_printf("Old string value was \"%s\"\n", strp);
56 err = fdt_setprop_empty(fdt, 0, "prop-str");
57 if (err)
58 FAIL("Failed to empty \"prop-str\": %s",
59 fdt_strerror(err));
60
61 check_getprop(fdt, 0, "prop-str", 0, NULL);
62
63 err = fdt_setprop_u32(fdt, 0, "prop-u32", TEST_VALUE_2);
64 if (err)
65 FAIL("Failed to set \"prop-u32\" to 0x%08x: %s",
66 TEST_VALUE_2, fdt_strerror(err));
67 check_getprop_cell(fdt, 0, "prop-u32", TEST_VALUE_2);
68
69 err = fdt_setprop_cell(fdt, 0, "prop-cell", TEST_VALUE_2);
70 if (err)
71 FAIL("Failed to set \"prop-cell\" to 0x%08x: %s",
72 TEST_VALUE_2, fdt_strerror(err));
73 check_getprop_cell(fdt, 0, "prop-cell", TEST_VALUE_2);
74
75 err = fdt_setprop_u64(fdt, 0, "prop-u64", TEST_VALUE64_1);
76 if (err)
77 FAIL("Failed to set \"prop-u64\" to 0x%016llx: %s",
78 TEST_VALUE64_1, fdt_strerror(err));
79 check_getprop_64(fdt, 0, "prop-u64", TEST_VALUE64_1);
80
81 PASS();
82 }
83