Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 1 | // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 2 | /* |
| 3 | * libfdt - Flat Device Tree manipulation |
| 4 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 5 | */ |
| 6 | #include "libfdt_env.h" |
| 7 | |
| 8 | #include <fdt.h> |
| 9 | #include <libfdt.h> |
| 10 | |
| 11 | #include "libfdt_internal.h" |
| 12 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 13 | static int fdt_blocks_misordered_(const void *fdt, |
| 14 | int mem_rsv_size, int struct_size) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 15 | { |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 16 | if (!fdt_chk_basic()) |
| 17 | return false; |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 18 | return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8)) |
| 19 | || (fdt_off_dt_struct(fdt) < |
| 20 | (fdt_off_mem_rsvmap(fdt) + mem_rsv_size)) |
| 21 | || (fdt_off_dt_strings(fdt) < |
| 22 | (fdt_off_dt_struct(fdt) + struct_size)) |
| 23 | || (fdt_totalsize(fdt) < |
| 24 | (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt))); |
| 25 | } |
| 26 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 27 | static int fdt_rw_probe_(void *fdt) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 28 | { |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 29 | if (!fdt_chk_basic()) |
| 30 | return 0; |
| 31 | FDT_RO_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 32 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 33 | if (fdt_chk_version() && fdt_version(fdt) < 17) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 34 | return -FDT_ERR_BADVERSION; |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 35 | if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry), |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 36 | fdt_size_dt_struct(fdt))) |
| 37 | return -FDT_ERR_BADLAYOUT; |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 38 | if (fdt_chk_version() && fdt_version(fdt) > 17) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 39 | fdt_set_version(fdt, 17); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 44 | #define FDT_RW_PROBE(fdt) \ |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 45 | { \ |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 46 | int err_; \ |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 47 | if (fdt_chk_extra() && (err_ = fdt_rw_probe_(fdt)) != 0) \ |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 48 | return err_; \ |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 51 | static inline int fdt_data_size_(void *fdt) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 52 | { |
| 53 | return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt); |
| 54 | } |
| 55 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 56 | static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 57 | { |
| 58 | char *p = splicepoint; |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 59 | char *end = (char *)fdt + fdt_data_size_(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 60 | |
| 61 | if (((p + oldlen) < p) || ((p + oldlen) > end)) |
| 62 | return -FDT_ERR_BADOFFSET; |
| 63 | if ((p < (char *)fdt) || ((end - oldlen + newlen) < (char *)fdt)) |
| 64 | return -FDT_ERR_BADOFFSET; |
| 65 | if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt))) |
| 66 | return -FDT_ERR_NOSPACE; |
| 67 | memmove(p + newlen, p + oldlen, end - p - oldlen); |
| 68 | return 0; |
| 69 | } |
| 70 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 71 | static int fdt_splice_mem_rsv_(void *fdt, struct fdt_reserve_entry *p, |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 72 | int oldn, int newn) |
| 73 | { |
| 74 | int delta = (newn - oldn) * sizeof(*p); |
| 75 | int err; |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 76 | err = fdt_splice_(fdt, p, oldn * sizeof(*p), newn * sizeof(*p)); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 77 | if (err) |
| 78 | return err; |
| 79 | fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta); |
| 80 | fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta); |
| 81 | return 0; |
| 82 | } |
| 83 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 84 | static int fdt_splice_struct_(void *fdt, void *p, |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 85 | int oldlen, int newlen) |
| 86 | { |
| 87 | int delta = newlen - oldlen; |
| 88 | int err; |
| 89 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 90 | if ((err = fdt_splice_(fdt, p, oldlen, newlen))) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 91 | return err; |
| 92 | |
| 93 | fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta); |
| 94 | fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta); |
| 95 | return 0; |
| 96 | } |
| 97 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 98 | /* Must only be used to roll back in case of error */ |
| 99 | static void fdt_del_last_string_(void *fdt, const char *s) |
| 100 | { |
| 101 | int newlen = strlen(s) + 1; |
| 102 | |
| 103 | fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) - newlen); |
| 104 | } |
| 105 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 106 | static int fdt_splice_string_(void *fdt, int newlen) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 107 | { |
| 108 | void *p = (char *)fdt |
| 109 | + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt); |
| 110 | int err; |
| 111 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 112 | if ((err = fdt_splice_(fdt, p, 0, newlen))) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 113 | return err; |
| 114 | |
| 115 | fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen); |
| 116 | return 0; |
| 117 | } |
| 118 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 119 | /** |
| 120 | * fdt_find_add_string_() - Find or allocate a string |
| 121 | * |
| 122 | * @fdt: pointer to the device tree to check/adjust |
| 123 | * @s: string to find/add |
| 124 | * @allocated: Set to 0 if the string was found, 1 if not found and so |
| 125 | * allocated. Ignored if !fdt_chk_basic() |
| 126 | * @return offset of string in the string table (whether found or added) |
| 127 | */ |
| 128 | static int fdt_find_add_string_(void *fdt, const char *s, int *allocated) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 129 | { |
| 130 | char *strtab = (char *)fdt + fdt_off_dt_strings(fdt); |
| 131 | const char *p; |
| 132 | char *new; |
| 133 | int len = strlen(s) + 1; |
| 134 | int err; |
| 135 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 136 | if (fdt_chk_basic()) |
| 137 | *allocated = 0; |
| 138 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 139 | p = fdt_find_string_(strtab, fdt_size_dt_strings(fdt), s); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 140 | if (p) |
| 141 | /* found it */ |
| 142 | return (p - strtab); |
| 143 | |
| 144 | new = strtab + fdt_size_dt_strings(fdt); |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 145 | err = fdt_splice_string_(fdt, len); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 146 | if (err) |
| 147 | return err; |
| 148 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 149 | if (fdt_chk_basic()) |
| 150 | *allocated = 1; |
| 151 | |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 152 | memcpy(new, s, len); |
| 153 | return (new - strtab); |
| 154 | } |
| 155 | |
| 156 | int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size) |
| 157 | { |
| 158 | struct fdt_reserve_entry *re; |
| 159 | int err; |
| 160 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 161 | FDT_RW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 162 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 163 | re = fdt_mem_rsv_w_(fdt, fdt_num_mem_rsv(fdt)); |
| 164 | err = fdt_splice_mem_rsv_(fdt, re, 0, 1); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 165 | if (err) |
| 166 | return err; |
| 167 | |
| 168 | re->address = cpu_to_fdt64(address); |
| 169 | re->size = cpu_to_fdt64(size); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int fdt_del_mem_rsv(void *fdt, int n) |
| 174 | { |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 175 | struct fdt_reserve_entry *re = fdt_mem_rsv_w_(fdt, n); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 176 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 177 | FDT_RW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 178 | |
| 179 | if (n >= fdt_num_mem_rsv(fdt)) |
| 180 | return -FDT_ERR_NOTFOUND; |
| 181 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 182 | return fdt_splice_mem_rsv_(fdt, re, 1, 0); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 185 | static int fdt_resize_property_(void *fdt, int nodeoffset, const char *name, |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 186 | int len, struct fdt_property **prop) |
| 187 | { |
| 188 | int oldlen; |
| 189 | int err; |
| 190 | |
| 191 | *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen); |
Tom Rini | 2d4c225 | 2017-09-23 17:31:59 -0400 | [diff] [blame] | 192 | if (!*prop) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 193 | return oldlen; |
| 194 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 195 | if ((err = fdt_splice_struct_(fdt, (*prop)->data, FDT_TAGALIGN(oldlen), |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 196 | FDT_TAGALIGN(len)))) |
| 197 | return err; |
| 198 | |
| 199 | (*prop)->len = cpu_to_fdt32(len); |
| 200 | return 0; |
| 201 | } |
| 202 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 203 | static int fdt_add_property_(void *fdt, int nodeoffset, const char *name, |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 204 | int len, struct fdt_property **prop) |
| 205 | { |
| 206 | int proplen; |
| 207 | int nextoffset; |
| 208 | int namestroff; |
| 209 | int err; |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 210 | int allocated; |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 211 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 212 | if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 213 | return nextoffset; |
| 214 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 215 | namestroff = fdt_find_add_string_(fdt, name, &allocated); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 216 | if (namestroff < 0) |
| 217 | return namestroff; |
| 218 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 219 | *prop = fdt_offset_ptr_w_(fdt, nextoffset); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 220 | proplen = sizeof(**prop) + FDT_TAGALIGN(len); |
| 221 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 222 | err = fdt_splice_struct_(fdt, *prop, 0, proplen); |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 223 | if (err) { |
| 224 | /* Delete the string if we failed to add it */ |
| 225 | if (fdt_chk_basic() && allocated) |
| 226 | fdt_del_last_string_(fdt, name); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 227 | return err; |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 228 | } |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 229 | |
| 230 | (*prop)->tag = cpu_to_fdt32(FDT_PROP); |
| 231 | (*prop)->nameoff = cpu_to_fdt32(namestroff); |
| 232 | (*prop)->len = cpu_to_fdt32(len); |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | int fdt_set_name(void *fdt, int nodeoffset, const char *name) |
| 237 | { |
| 238 | char *namep; |
| 239 | int oldlen, newlen; |
| 240 | int err; |
| 241 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 242 | FDT_RW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 243 | |
| 244 | namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen); |
| 245 | if (!namep) |
| 246 | return oldlen; |
| 247 | |
| 248 | newlen = strlen(name); |
| 249 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 250 | err = fdt_splice_struct_(fdt, namep, FDT_TAGALIGN(oldlen+1), |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 251 | FDT_TAGALIGN(newlen+1)); |
| 252 | if (err) |
| 253 | return err; |
| 254 | |
| 255 | memcpy(namep, name, newlen+1); |
| 256 | return 0; |
| 257 | } |
| 258 | |
Tom Rini | 2d4c225 | 2017-09-23 17:31:59 -0400 | [diff] [blame] | 259 | int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name, |
| 260 | int len, void **prop_data) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 261 | { |
| 262 | struct fdt_property *prop; |
| 263 | int err; |
| 264 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 265 | FDT_RW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 266 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 267 | err = fdt_resize_property_(fdt, nodeoffset, name, len, &prop); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 268 | if (err == -FDT_ERR_NOTFOUND) |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 269 | err = fdt_add_property_(fdt, nodeoffset, name, len, &prop); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 270 | if (err) |
| 271 | return err; |
| 272 | |
Tom Rini | 2d4c225 | 2017-09-23 17:31:59 -0400 | [diff] [blame] | 273 | *prop_data = prop->data; |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | int fdt_setprop(void *fdt, int nodeoffset, const char *name, |
| 278 | const void *val, int len) |
| 279 | { |
| 280 | void *prop_data; |
| 281 | int err; |
| 282 | |
| 283 | err = fdt_setprop_placeholder(fdt, nodeoffset, name, len, &prop_data); |
| 284 | if (err) |
| 285 | return err; |
| 286 | |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 287 | if (len) |
Tom Rini | 2d4c225 | 2017-09-23 17:31:59 -0400 | [diff] [blame] | 288 | memcpy(prop_data, val, len); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | int fdt_appendprop(void *fdt, int nodeoffset, const char *name, |
| 293 | const void *val, int len) |
| 294 | { |
| 295 | struct fdt_property *prop; |
| 296 | int err, oldlen, newlen; |
| 297 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 298 | FDT_RW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 299 | |
| 300 | prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen); |
| 301 | if (prop) { |
| 302 | newlen = len + oldlen; |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 303 | err = fdt_splice_struct_(fdt, prop->data, |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 304 | FDT_TAGALIGN(oldlen), |
| 305 | FDT_TAGALIGN(newlen)); |
| 306 | if (err) |
| 307 | return err; |
| 308 | prop->len = cpu_to_fdt32(newlen); |
| 309 | memcpy(prop->data + oldlen, val, len); |
| 310 | } else { |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 311 | err = fdt_add_property_(fdt, nodeoffset, name, len, &prop); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 312 | if (err) |
| 313 | return err; |
| 314 | memcpy(prop->data, val, len); |
| 315 | } |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | int fdt_delprop(void *fdt, int nodeoffset, const char *name) |
| 320 | { |
| 321 | struct fdt_property *prop; |
| 322 | int len, proplen; |
| 323 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 324 | FDT_RW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 325 | |
| 326 | prop = fdt_get_property_w(fdt, nodeoffset, name, &len); |
Tom Rini | 2d4c225 | 2017-09-23 17:31:59 -0400 | [diff] [blame] | 327 | if (!prop) |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 328 | return len; |
| 329 | |
| 330 | proplen = sizeof(*prop) + FDT_TAGALIGN(len); |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 331 | return fdt_splice_struct_(fdt, prop, proplen, 0); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | int fdt_add_subnode_namelen(void *fdt, int parentoffset, |
| 335 | const char *name, int namelen) |
| 336 | { |
| 337 | struct fdt_node_header *nh; |
| 338 | int offset, nextoffset; |
| 339 | int nodelen; |
| 340 | int err; |
| 341 | uint32_t tag; |
| 342 | fdt32_t *endtag; |
| 343 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 344 | FDT_RW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 345 | |
| 346 | offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen); |
| 347 | if (offset >= 0) |
| 348 | return -FDT_ERR_EXISTS; |
| 349 | else if (offset != -FDT_ERR_NOTFOUND) |
| 350 | return offset; |
| 351 | |
| 352 | /* Try to place the new node after the parent's properties */ |
| 353 | fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */ |
| 354 | do { |
| 355 | offset = nextoffset; |
| 356 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
| 357 | } while ((tag == FDT_PROP) || (tag == FDT_NOP)); |
| 358 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 359 | nh = fdt_offset_ptr_w_(fdt, offset); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 360 | nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE; |
| 361 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 362 | err = fdt_splice_struct_(fdt, nh, 0, nodelen); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 363 | if (err) |
| 364 | return err; |
| 365 | |
| 366 | nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); |
| 367 | memset(nh->name, 0, FDT_TAGALIGN(namelen+1)); |
| 368 | memcpy(nh->name, name, namelen); |
| 369 | endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE); |
| 370 | *endtag = cpu_to_fdt32(FDT_END_NODE); |
| 371 | |
| 372 | return offset; |
| 373 | } |
| 374 | |
| 375 | int fdt_add_subnode(void *fdt, int parentoffset, const char *name) |
| 376 | { |
| 377 | return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name)); |
| 378 | } |
| 379 | |
| 380 | int fdt_del_node(void *fdt, int nodeoffset) |
| 381 | { |
| 382 | int endoffset; |
| 383 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 384 | FDT_RW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 385 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 386 | endoffset = fdt_node_end_offset_(fdt, nodeoffset); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 387 | if (endoffset < 0) |
| 388 | return endoffset; |
| 389 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 390 | return fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset), |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 391 | endoffset - nodeoffset, 0); |
| 392 | } |
| 393 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 394 | static void fdt_packblocks_(const char *old, char *new, |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 395 | int mem_rsv_size, int struct_size) |
| 396 | { |
| 397 | int mem_rsv_off, struct_off, strings_off; |
| 398 | |
| 399 | mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8); |
| 400 | struct_off = mem_rsv_off + mem_rsv_size; |
| 401 | strings_off = struct_off + struct_size; |
| 402 | |
| 403 | memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size); |
| 404 | fdt_set_off_mem_rsvmap(new, mem_rsv_off); |
| 405 | |
| 406 | memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size); |
| 407 | fdt_set_off_dt_struct(new, struct_off); |
| 408 | fdt_set_size_dt_struct(new, struct_size); |
| 409 | |
| 410 | memmove(new + strings_off, old + fdt_off_dt_strings(old), |
| 411 | fdt_size_dt_strings(old)); |
| 412 | fdt_set_off_dt_strings(new, strings_off); |
| 413 | fdt_set_size_dt_strings(new, fdt_size_dt_strings(old)); |
| 414 | } |
| 415 | |
| 416 | int fdt_open_into(const void *fdt, void *buf, int bufsize) |
| 417 | { |
| 418 | int err; |
| 419 | int mem_rsv_size, struct_size; |
| 420 | int newsize; |
| 421 | const char *fdtstart = fdt; |
| 422 | const char *fdtend = fdtstart + fdt_totalsize(fdt); |
| 423 | char *tmp; |
| 424 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 425 | FDT_RO_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 426 | |
| 427 | mem_rsv_size = (fdt_num_mem_rsv(fdt)+1) |
| 428 | * sizeof(struct fdt_reserve_entry); |
| 429 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 430 | if (!fdt_chk_version() || fdt_version(fdt) >= 17) { |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 431 | struct_size = fdt_size_dt_struct(fdt); |
| 432 | } else { |
| 433 | struct_size = 0; |
| 434 | while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END) |
| 435 | ; |
| 436 | if (struct_size < 0) |
| 437 | return struct_size; |
| 438 | } |
| 439 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 440 | if (!fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) { |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 441 | /* no further work necessary */ |
| 442 | err = fdt_move(fdt, buf, bufsize); |
| 443 | if (err) |
| 444 | return err; |
| 445 | fdt_set_version(buf, 17); |
| 446 | fdt_set_size_dt_struct(buf, struct_size); |
| 447 | fdt_set_totalsize(buf, bufsize); |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* Need to reorder */ |
| 452 | newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size |
| 453 | + struct_size + fdt_size_dt_strings(fdt); |
| 454 | |
| 455 | if (bufsize < newsize) |
| 456 | return -FDT_ERR_NOSPACE; |
| 457 | |
| 458 | /* First attempt to build converted tree at beginning of buffer */ |
| 459 | tmp = buf; |
| 460 | /* But if that overlaps with the old tree... */ |
| 461 | if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) { |
| 462 | /* Try right after the old tree instead */ |
| 463 | tmp = (char *)(uintptr_t)fdtend; |
| 464 | if ((tmp + newsize) > ((char *)buf + bufsize)) |
| 465 | return -FDT_ERR_NOSPACE; |
| 466 | } |
| 467 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 468 | fdt_packblocks_(fdt, tmp, mem_rsv_size, struct_size); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 469 | memmove(buf, tmp, newsize); |
| 470 | |
| 471 | fdt_set_magic(buf, FDT_MAGIC); |
| 472 | fdt_set_totalsize(buf, bufsize); |
| 473 | fdt_set_version(buf, 17); |
| 474 | fdt_set_last_comp_version(buf, 16); |
| 475 | fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt)); |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | int fdt_pack(void *fdt) |
| 481 | { |
| 482 | int mem_rsv_size; |
| 483 | |
Simon Glass | f0921f5 | 2019-10-27 09:47:42 -0600 | [diff] [blame^] | 484 | FDT_RW_PROBE(fdt); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 485 | |
| 486 | mem_rsv_size = (fdt_num_mem_rsv(fdt)+1) |
| 487 | * sizeof(struct fdt_reserve_entry); |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 488 | fdt_packblocks_(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt)); |
| 489 | fdt_set_totalsize(fdt, fdt_data_size_(fdt)); |
Tom Rini | c0e032e | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 490 | |
| 491 | return 0; |
| 492 | } |