xref: /rk3399_ARM-atf/common/fdt_wrappers.c (revision c948f77136c42a92d0bb660543a3600c36dcf7f1)
1 /*
2  * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /* Helper functions to offer easier navigation of Device Tree Blob */
8 
9 #include <assert.h>
10 #include <string.h>
11 
12 #include <libfdt.h>
13 
14 #include <common/debug.h>
15 #include <common/fdt_wrappers.h>
16 
17 /*
18  * Read cells from a given property of the given node. At most 2 cells of the
19  * property are read, and pointer is updated. Returns 0 on success, and -1 upon
20  * error
21  */
22 int fdtw_read_cells(const void *dtb, int node, const char *prop,
23 		unsigned int cells, void *value)
24 {
25 	const uint32_t *value_ptr;
26 	uint32_t hi = 0, lo;
27 	int value_len;
28 
29 	assert(dtb != NULL);
30 	assert(prop != NULL);
31 	assert(value != NULL);
32 	assert(node >= 0);
33 
34 	/* We expect either 1 or 2 cell property */
35 	assert(cells <= 2U);
36 
37 	/* Access property and obtain its length (in bytes) */
38 	value_ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
39 			&value_len);
40 	if (value_ptr == NULL) {
41 		WARN("Couldn't find property %s in dtb\n", prop);
42 		return -1;
43 	}
44 
45 	/* Verify that property length accords with cell length */
46 	if (NCELLS((unsigned int)value_len) != cells) {
47 		WARN("Property length mismatch\n");
48 		return -1;
49 	}
50 
51 	if (cells == 2U) {
52 		hi = fdt32_to_cpu(*value_ptr);
53 		value_ptr++;
54 	}
55 
56 	lo = fdt32_to_cpu(*value_ptr);
57 
58 	if (cells == 2U)
59 		*((uint64_t *) value) = ((uint64_t) hi << 32) | lo;
60 	else
61 		*((uint32_t *) value) = lo;
62 
63 	return 0;
64 }
65 
66 /*
67  * Read cells from a given property of the given node. Any number of 32-bit
68  * cells of the property can be read. The fdt pointer is updated. Returns 0 on
69  * success, and -1 on error.
70  */
71 int fdtw_read_array(const void *dtb, int node, const char *prop,
72 		unsigned int cells, void *value)
73 {
74 	const uint32_t *value_ptr;
75 	int value_len;
76 
77 	assert(dtb != NULL);
78 	assert(prop != NULL);
79 	assert(value != NULL);
80 	assert(node >= 0);
81 
82 	/* Access property and obtain its length (in bytes) */
83 	value_ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
84 			&value_len);
85 	if (value_ptr == NULL) {
86 		WARN("Couldn't find property %s in dtb\n", prop);
87 		return -1;
88 	}
89 
90 	/* Verify that property length accords with cell length */
91 	if (NCELLS((unsigned int)value_len) != cells) {
92 		WARN("Property length mismatch\n");
93 		return -1;
94 	}
95 
96 	uint32_t *dst = value;
97 
98 	for (unsigned int i = 0U; i < cells; i++) {
99 		dst[i] = fdt32_to_cpu(value_ptr[i]);
100 	}
101 
102 	return 0;
103 }
104 
105 /*
106  * Read string from a given property of the given node. Up to 'size - 1'
107  * characters are read, and a NUL terminator is added. Returns 0 on success,
108  * and -1 upon error.
109  */
110 int fdtw_read_string(const void *dtb, int node, const char *prop,
111 		char *str, size_t size)
112 {
113 	const char *ptr;
114 	size_t len;
115 
116 	assert(dtb != NULL);
117 	assert(node >= 0);
118 	assert(prop != NULL);
119 	assert(str != NULL);
120 	assert(size > 0U);
121 
122 	ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), NULL);
123 	if (ptr == NULL) {
124 		WARN("Couldn't find property %s in dtb\n", prop);
125 		return -1;
126 	}
127 
128 	len = strlcpy(str, ptr, size);
129 	if (len >= size) {
130 		WARN("String of property %s in dtb has been truncated\n", prop);
131 		return -1;
132 	}
133 
134 	return 0;
135 }
136 
137 /*
138  * Write cells in place to a given property of the given node. At most 2 cells
139  * of the property are written. Returns 0 on success, and -1 upon error.
140  */
141 int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
142 		unsigned int cells, void *value)
143 {
144 	int err, len;
145 
146 	assert(dtb != NULL);
147 	assert(prop != NULL);
148 	assert(value != NULL);
149 	assert(node >= 0);
150 
151 	/* We expect either 1 or 2 cell property */
152 	assert(cells <= 2U);
153 
154 	if (cells == 2U)
155 		*(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value);
156 	else
157 		*(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value);
158 
159 	len = (int)cells * 4;
160 
161 	/* Set property value in place */
162 	err = fdt_setprop_inplace(dtb, node, prop, value, len);
163 	if (err != 0) {
164 		WARN("Modify property %s failed with error %d\n", prop, err);
165 		return -1;
166 	}
167 
168 	return 0;
169 }
170