blob: e67fdb00548074f1cd04255e0e17aff355f9c31d [file] [log] [blame]
Soby Mathewb9fccca2017-11-06 13:56:40 +00001/*
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>
Antonio Nino Diazd85c8782018-06-26 10:34:07 +010010#include <string.h>
Soby Mathewb9fccca2017-11-06 13:56:40 +000011
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <libfdt.h>
13
14#include <common/debug.h>
15#include <common/fdt_wrappers.h>
16
Soby Mathewb9fccca2017-11-06 13:56:40 +000017/*
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 */
22int 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
Soby Mathewcc364842018-02-21 01:16:39 +000029 assert(dtb != NULL);
30 assert(prop != NULL);
31 assert(value != NULL);
Soby Mathewb9fccca2017-11-06 13:56:40 +000032 assert(node >= 0);
33
34 /* We expect either 1 or 2 cell property */
Soby Mathewcc364842018-02-21 01:16:39 +000035 assert(cells <= 2U);
Soby Mathewb9fccca2017-11-06 13:56:40 +000036
37 /* Access property and obtain its length (in bytes) */
Soby Mathewcc364842018-02-21 01:16:39 +000038 value_ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
Soby Mathewb9fccca2017-11-06 13:56:40 +000039 &value_len);
40 if (value_ptr == NULL) {
41 WARN("Couldn't find property %s in dtb\n", prop);
42 return -1;
43 }
44
Soby Mathewb9fccca2017-11-06 13:56:40 +000045 /* Verify that property length accords with cell length */
Soby Mathewcc364842018-02-21 01:16:39 +000046 if (NCELLS((unsigned int)value_len) != cells) {
Soby Mathewb9fccca2017-11-06 13:56:40 +000047 WARN("Property length mismatch\n");
48 return -1;
49 }
50
Soby Mathewcc364842018-02-21 01:16:39 +000051 if (cells == 2U) {
Soby Mathewb9fccca2017-11-06 13:56:40 +000052 hi = fdt32_to_cpu(*value_ptr);
53 value_ptr++;
54 }
55
56 lo = fdt32_to_cpu(*value_ptr);
57
Soby Mathewcc364842018-02-21 01:16:39 +000058 if (cells == 2U)
Soby Mathewb9fccca2017-11-06 13:56:40 +000059 *((uint64_t *) value) = ((uint64_t) hi << 32) | lo;
60 else
61 *((uint32_t *) value) = lo;
62
63 return 0;
64}
65
66/*
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010067 * 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 */
71int 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/*
Antonio Nino Diazd85c8782018-06-26 10:34:07 +0100106 * 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 */
110int 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/*
Soby Mathewb9fccca2017-11-06 13:56:40 +0000138 * 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 */
141int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
142 unsigned int cells, void *value)
143{
144 int err, len;
145
Soby Mathewcc364842018-02-21 01:16:39 +0000146 assert(dtb != NULL);
147 assert(prop != NULL);
148 assert(value != NULL);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000149 assert(node >= 0);
150
151 /* We expect either 1 or 2 cell property */
Soby Mathewcc364842018-02-21 01:16:39 +0000152 assert(cells <= 2U);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000153
Soby Mathewcc364842018-02-21 01:16:39 +0000154 if (cells == 2U)
Soby Mathewb9fccca2017-11-06 13:56:40 +0000155 *(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value);
156 else
157 *(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value);
158
Soby Mathewcc364842018-02-21 01:16:39 +0000159 len = (int)cells * 4;
Soby Mathewb9fccca2017-11-06 13:56:40 +0000160
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}