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