1 /* $NetBSD: setprop_inplace.c,v 1.1.1.3 2019/12/22 12:34:06 skrll Exp $ */
2
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 /*
5 * libfdt - Flat Device Tree manipulation
6 * Testcase for fdt_setprop_inplace()
7 * Copyright (C) 2006 David Gibson, IBM Corporation.
8 */
9
10 #include <inttypes.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <stdint.h>
16
17 #include <libfdt.h>
18
19 #include "tests.h"
20 #include "testdata.h"
21
main(int argc,char * argv[])22 int main(int argc, char *argv[])
23 {
24 void *fdt;
25 const uint32_t *intp;
26 const uint64_t *int64p;
27 const char *strp;
28 char *xstr;
29 int xlen, i;
30 int err;
31
32 test_init(argc, argv);
33 fdt = load_blob_arg(argc, argv);
34
35 intp = check_getprop_cell(fdt, 0, "prop-int", TEST_VALUE_1);
36
37 verbose_printf("Old int value was 0x%08x\n", *intp);
38 err = fdt_setprop_inplace_cell(fdt, 0, "prop-int", ~TEST_VALUE_1);
39 if (err)
40 FAIL("Failed to set \"prop-int\" to 0x%08x: %s",
41 ~TEST_VALUE_1, fdt_strerror(err));
42 intp = check_getprop_cell(fdt, 0, "prop-int", ~TEST_VALUE_1);
43 verbose_printf("New int value is 0x%08x\n", *intp);
44
45 strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
46 TEST_STRING_1);
47
48
49 int64p = check_getprop_64(fdt, 0, "prop-int64", TEST_VALUE64_1);
50
51 verbose_printf("Old int64 value was 0x%016" PRIx64 "\n", *int64p);
52 err = fdt_setprop_inplace_u64(fdt, 0, "prop-int64", ~TEST_VALUE64_1);
53 if (err)
54 FAIL("Failed to set \"prop-int64\" to 0x%016llx: %s",
55 ~TEST_VALUE64_1, fdt_strerror(err));
56 int64p = check_getprop_64(fdt, 0, "prop-int64", ~TEST_VALUE64_1);
57 verbose_printf("New int64 value is 0x%016" PRIx64 "\n", *int64p);
58
59 strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
60 TEST_STRING_1);
61
62 verbose_printf("Old string value was \"%s\"\n", strp);
63 xstr = strdup(strp);
64 xlen = strlen(xstr);
65 for (i = 0; i < xlen; i++)
66 xstr[i] = toupper(xstr[i]);
67 err = fdt_setprop_inplace(fdt, 0, "prop-str", xstr, xlen+1);
68 if (err)
69 FAIL("Failed to set \"prop-str\" to \"%s\": %s",
70 xstr, fdt_strerror(err));
71
72 strp = check_getprop(fdt, 0, "prop-str", xlen+1, xstr);
73 verbose_printf("New string value is \"%s\"\n", strp);
74
75 err = fdt_setprop_inplace_namelen_partial(fdt, 0, "compatible",
76 strlen("compatible"), 4,
77 TEST_STRING_4_PARTIAL,
78 strlen(TEST_STRING_4_PARTIAL));
79 if (err)
80 FAIL("Failed to set \"compatible\": %s\n", fdt_strerror(err));
81
82 check_getprop(fdt, 0, "compatible", strlen(TEST_STRING_4_RESULT) + 1,
83 TEST_STRING_4_RESULT);
84
85 PASS();
86 }
87