| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * libfdt - Flat Device Tree manipulation |
|---|
| 3 | 4 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
|---|
| 4 | | - * |
|---|
| 5 | | - * libfdt is dual licensed: you can use it either under the terms of |
|---|
| 6 | | - * the GPL, or the BSD license, at your option. |
|---|
| 7 | | - * |
|---|
| 8 | | - * a) This library is free software; you can redistribute it and/or |
|---|
| 9 | | - * modify it under the terms of the GNU General Public License as |
|---|
| 10 | | - * published by the Free Software Foundation; either version 2 of the |
|---|
| 11 | | - * License, or (at your option) any later version. |
|---|
| 12 | | - * |
|---|
| 13 | | - * This library is distributed in the hope that it will be useful, |
|---|
| 14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | | - * GNU General Public License for more details. |
|---|
| 17 | | - * |
|---|
| 18 | | - * You should have received a copy of the GNU General Public |
|---|
| 19 | | - * License along with this library; if not, write to the Free |
|---|
| 20 | | - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
|---|
| 21 | | - * MA 02110-1301 USA |
|---|
| 22 | | - * |
|---|
| 23 | | - * Alternatively, |
|---|
| 24 | | - * |
|---|
| 25 | | - * b) Redistribution and use in source and binary forms, with or |
|---|
| 26 | | - * without modification, are permitted provided that the following |
|---|
| 27 | | - * conditions are met: |
|---|
| 28 | | - * |
|---|
| 29 | | - * 1. Redistributions of source code must retain the above |
|---|
| 30 | | - * copyright notice, this list of conditions and the following |
|---|
| 31 | | - * disclaimer. |
|---|
| 32 | | - * 2. Redistributions in binary form must reproduce the above |
|---|
| 33 | | - * copyright notice, this list of conditions and the following |
|---|
| 34 | | - * disclaimer in the documentation and/or other materials |
|---|
| 35 | | - * provided with the distribution. |
|---|
| 36 | | - * |
|---|
| 37 | | - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
|---|
| 38 | | - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
|---|
| 39 | | - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|---|
| 40 | | - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 41 | | - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|---|
| 42 | | - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 43 | | - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|---|
| 44 | | - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 45 | | - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 46 | | - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|---|
| 47 | | - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 48 | | - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|---|
| 49 | | - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 50 | 5 | */ |
|---|
| 51 | 6 | #include "libfdt_env.h" |
|---|
| 52 | 7 | |
|---|
| .. | .. |
|---|
| 67 | 22 | (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt))); |
|---|
| 68 | 23 | } |
|---|
| 69 | 24 | |
|---|
| 70 | | -static int fdt_rw_check_header_(void *fdt) |
|---|
| 25 | +static int fdt_rw_probe_(void *fdt) |
|---|
| 71 | 26 | { |
|---|
| 72 | | - FDT_CHECK_HEADER(fdt); |
|---|
| 27 | + if (can_assume(VALID_DTB)) |
|---|
| 28 | + return 0; |
|---|
| 29 | + FDT_RO_PROBE(fdt); |
|---|
| 73 | 30 | |
|---|
| 74 | | - if (fdt_version(fdt) < 17) |
|---|
| 31 | + if (!can_assume(LATEST) && fdt_version(fdt) < 17) |
|---|
| 75 | 32 | return -FDT_ERR_BADVERSION; |
|---|
| 76 | 33 | if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry), |
|---|
| 77 | 34 | fdt_size_dt_struct(fdt))) |
|---|
| 78 | 35 | return -FDT_ERR_BADLAYOUT; |
|---|
| 79 | | - if (fdt_version(fdt) > 17) |
|---|
| 36 | + if (!can_assume(LATEST) && fdt_version(fdt) > 17) |
|---|
| 80 | 37 | fdt_set_version(fdt, 17); |
|---|
| 81 | 38 | |
|---|
| 82 | 39 | return 0; |
|---|
| 83 | 40 | } |
|---|
| 84 | 41 | |
|---|
| 85 | | -#define FDT_RW_CHECK_HEADER(fdt) \ |
|---|
| 42 | +#define FDT_RW_PROBE(fdt) \ |
|---|
| 86 | 43 | { \ |
|---|
| 87 | 44 | int err_; \ |
|---|
| 88 | | - if ((err_ = fdt_rw_check_header_(fdt)) != 0) \ |
|---|
| 45 | + if ((err_ = fdt_rw_probe_(fdt)) != 0) \ |
|---|
| 89 | 46 | return err_; \ |
|---|
| 90 | 47 | } |
|---|
| 91 | 48 | |
|---|
| 92 | | -static inline int fdt_data_size_(void *fdt) |
|---|
| 49 | +static inline unsigned int fdt_data_size_(void *fdt) |
|---|
| 93 | 50 | { |
|---|
| 94 | 51 | return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt); |
|---|
| 95 | 52 | } |
|---|
| .. | .. |
|---|
| 97 | 54 | static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen) |
|---|
| 98 | 55 | { |
|---|
| 99 | 56 | char *p = splicepoint; |
|---|
| 100 | | - char *end = (char *)fdt + fdt_data_size_(fdt); |
|---|
| 57 | + unsigned int dsize = fdt_data_size_(fdt); |
|---|
| 58 | + size_t soff = p - (char *)fdt; |
|---|
| 101 | 59 | |
|---|
| 102 | | - if (((p + oldlen) < p) || ((p + oldlen) > end)) |
|---|
| 60 | + if ((oldlen < 0) || (soff + oldlen < soff) || (soff + oldlen > dsize)) |
|---|
| 103 | 61 | return -FDT_ERR_BADOFFSET; |
|---|
| 104 | | - if ((p < (char *)fdt) || ((end - oldlen + newlen) < (char *)fdt)) |
|---|
| 62 | + if ((p < (char *)fdt) || (dsize + newlen < (unsigned)oldlen)) |
|---|
| 105 | 63 | return -FDT_ERR_BADOFFSET; |
|---|
| 106 | | - if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt))) |
|---|
| 64 | + if (dsize - oldlen + newlen > fdt_totalsize(fdt)) |
|---|
| 107 | 65 | return -FDT_ERR_NOSPACE; |
|---|
| 108 | | - memmove(p + newlen, p + oldlen, end - p - oldlen); |
|---|
| 66 | + memmove(p + newlen, p + oldlen, ((char *)fdt + dsize) - (p + oldlen)); |
|---|
| 109 | 67 | return 0; |
|---|
| 110 | 68 | } |
|---|
| 111 | 69 | |
|---|
| .. | .. |
|---|
| 136 | 94 | return 0; |
|---|
| 137 | 95 | } |
|---|
| 138 | 96 | |
|---|
| 97 | +/* Must only be used to roll back in case of error */ |
|---|
| 98 | +static void fdt_del_last_string_(void *fdt, const char *s) |
|---|
| 99 | +{ |
|---|
| 100 | + int newlen = strlen(s) + 1; |
|---|
| 101 | + |
|---|
| 102 | + fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) - newlen); |
|---|
| 103 | +} |
|---|
| 104 | + |
|---|
| 139 | 105 | static int fdt_splice_string_(void *fdt, int newlen) |
|---|
| 140 | 106 | { |
|---|
| 141 | 107 | void *p = (char *)fdt |
|---|
| .. | .. |
|---|
| 149 | 115 | return 0; |
|---|
| 150 | 116 | } |
|---|
| 151 | 117 | |
|---|
| 152 | | -static int fdt_find_add_string_(void *fdt, const char *s) |
|---|
| 118 | +/** |
|---|
| 119 | + * fdt_find_add_string_() - Find or allocate a string |
|---|
| 120 | + * |
|---|
| 121 | + * @fdt: pointer to the device tree to check/adjust |
|---|
| 122 | + * @s: string to find/add |
|---|
| 123 | + * @allocated: Set to 0 if the string was found, 1 if not found and so |
|---|
| 124 | + * allocated. Ignored if can_assume(NO_ROLLBACK) |
|---|
| 125 | + * @return offset of string in the string table (whether found or added) |
|---|
| 126 | + */ |
|---|
| 127 | +static int fdt_find_add_string_(void *fdt, const char *s, int *allocated) |
|---|
| 153 | 128 | { |
|---|
| 154 | 129 | char *strtab = (char *)fdt + fdt_off_dt_strings(fdt); |
|---|
| 155 | 130 | const char *p; |
|---|
| 156 | 131 | char *new; |
|---|
| 157 | 132 | int len = strlen(s) + 1; |
|---|
| 158 | 133 | int err; |
|---|
| 134 | + |
|---|
| 135 | + if (!can_assume(NO_ROLLBACK)) |
|---|
| 136 | + *allocated = 0; |
|---|
| 159 | 137 | |
|---|
| 160 | 138 | p = fdt_find_string_(strtab, fdt_size_dt_strings(fdt), s); |
|---|
| 161 | 139 | if (p) |
|---|
| .. | .. |
|---|
| 167 | 145 | if (err) |
|---|
| 168 | 146 | return err; |
|---|
| 169 | 147 | |
|---|
| 148 | + if (!can_assume(NO_ROLLBACK)) |
|---|
| 149 | + *allocated = 1; |
|---|
| 150 | + |
|---|
| 170 | 151 | memcpy(new, s, len); |
|---|
| 171 | 152 | return (new - strtab); |
|---|
| 172 | 153 | } |
|---|
| .. | .. |
|---|
| 176 | 157 | struct fdt_reserve_entry *re; |
|---|
| 177 | 158 | int err; |
|---|
| 178 | 159 | |
|---|
| 179 | | - FDT_RW_CHECK_HEADER(fdt); |
|---|
| 160 | + FDT_RW_PROBE(fdt); |
|---|
| 180 | 161 | |
|---|
| 181 | 162 | re = fdt_mem_rsv_w_(fdt, fdt_num_mem_rsv(fdt)); |
|---|
| 182 | 163 | err = fdt_splice_mem_rsv_(fdt, re, 0, 1); |
|---|
| .. | .. |
|---|
| 192 | 173 | { |
|---|
| 193 | 174 | struct fdt_reserve_entry *re = fdt_mem_rsv_w_(fdt, n); |
|---|
| 194 | 175 | |
|---|
| 195 | | - FDT_RW_CHECK_HEADER(fdt); |
|---|
| 176 | + FDT_RW_PROBE(fdt); |
|---|
| 196 | 177 | |
|---|
| 197 | 178 | if (n >= fdt_num_mem_rsv(fdt)) |
|---|
| 198 | 179 | return -FDT_ERR_NOTFOUND; |
|---|
| .. | .. |
|---|
| 225 | 206 | int nextoffset; |
|---|
| 226 | 207 | int namestroff; |
|---|
| 227 | 208 | int err; |
|---|
| 209 | + int allocated; |
|---|
| 228 | 210 | |
|---|
| 229 | 211 | if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0) |
|---|
| 230 | 212 | return nextoffset; |
|---|
| 231 | 213 | |
|---|
| 232 | | - namestroff = fdt_find_add_string_(fdt, name); |
|---|
| 214 | + namestroff = fdt_find_add_string_(fdt, name, &allocated); |
|---|
| 233 | 215 | if (namestroff < 0) |
|---|
| 234 | 216 | return namestroff; |
|---|
| 235 | 217 | |
|---|
| .. | .. |
|---|
| 237 | 219 | proplen = sizeof(**prop) + FDT_TAGALIGN(len); |
|---|
| 238 | 220 | |
|---|
| 239 | 221 | err = fdt_splice_struct_(fdt, *prop, 0, proplen); |
|---|
| 240 | | - if (err) |
|---|
| 222 | + if (err) { |
|---|
| 223 | + /* Delete the string if we failed to add it */ |
|---|
| 224 | + if (!can_assume(NO_ROLLBACK) && allocated) |
|---|
| 225 | + fdt_del_last_string_(fdt, name); |
|---|
| 241 | 226 | return err; |
|---|
| 227 | + } |
|---|
| 242 | 228 | |
|---|
| 243 | 229 | (*prop)->tag = cpu_to_fdt32(FDT_PROP); |
|---|
| 244 | 230 | (*prop)->nameoff = cpu_to_fdt32(namestroff); |
|---|
| .. | .. |
|---|
| 252 | 238 | int oldlen, newlen; |
|---|
| 253 | 239 | int err; |
|---|
| 254 | 240 | |
|---|
| 255 | | - FDT_RW_CHECK_HEADER(fdt); |
|---|
| 241 | + FDT_RW_PROBE(fdt); |
|---|
| 256 | 242 | |
|---|
| 257 | 243 | namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen); |
|---|
| 258 | 244 | if (!namep) |
|---|
| .. | .. |
|---|
| 275 | 261 | struct fdt_property *prop; |
|---|
| 276 | 262 | int err; |
|---|
| 277 | 263 | |
|---|
| 278 | | - FDT_RW_CHECK_HEADER(fdt); |
|---|
| 264 | + FDT_RW_PROBE(fdt); |
|---|
| 279 | 265 | |
|---|
| 280 | 266 | err = fdt_resize_property_(fdt, nodeoffset, name, len, &prop); |
|---|
| 281 | 267 | if (err == -FDT_ERR_NOTFOUND) |
|---|
| .. | .. |
|---|
| 308 | 294 | struct fdt_property *prop; |
|---|
| 309 | 295 | int err, oldlen, newlen; |
|---|
| 310 | 296 | |
|---|
| 311 | | - FDT_RW_CHECK_HEADER(fdt); |
|---|
| 297 | + FDT_RW_PROBE(fdt); |
|---|
| 312 | 298 | |
|---|
| 313 | 299 | prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen); |
|---|
| 314 | 300 | if (prop) { |
|---|
| .. | .. |
|---|
| 334 | 320 | struct fdt_property *prop; |
|---|
| 335 | 321 | int len, proplen; |
|---|
| 336 | 322 | |
|---|
| 337 | | - FDT_RW_CHECK_HEADER(fdt); |
|---|
| 323 | + FDT_RW_PROBE(fdt); |
|---|
| 338 | 324 | |
|---|
| 339 | 325 | prop = fdt_get_property_w(fdt, nodeoffset, name, &len); |
|---|
| 340 | 326 | if (!prop) |
|---|
| .. | .. |
|---|
| 354 | 340 | uint32_t tag; |
|---|
| 355 | 341 | fdt32_t *endtag; |
|---|
| 356 | 342 | |
|---|
| 357 | | - FDT_RW_CHECK_HEADER(fdt); |
|---|
| 343 | + FDT_RW_PROBE(fdt); |
|---|
| 358 | 344 | |
|---|
| 359 | 345 | offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen); |
|---|
| 360 | 346 | if (offset >= 0) |
|---|
| .. | .. |
|---|
| 394 | 380 | { |
|---|
| 395 | 381 | int endoffset; |
|---|
| 396 | 382 | |
|---|
| 397 | | - FDT_RW_CHECK_HEADER(fdt); |
|---|
| 383 | + FDT_RW_PROBE(fdt); |
|---|
| 398 | 384 | |
|---|
| 399 | 385 | endoffset = fdt_node_end_offset_(fdt, nodeoffset); |
|---|
| 400 | 386 | if (endoffset < 0) |
|---|
| .. | .. |
|---|
| 435 | 421 | const char *fdtend = fdtstart + fdt_totalsize(fdt); |
|---|
| 436 | 422 | char *tmp; |
|---|
| 437 | 423 | |
|---|
| 438 | | - FDT_CHECK_HEADER(fdt); |
|---|
| 424 | + FDT_RO_PROBE(fdt); |
|---|
| 439 | 425 | |
|---|
| 440 | 426 | mem_rsv_size = (fdt_num_mem_rsv(fdt)+1) |
|---|
| 441 | 427 | * sizeof(struct fdt_reserve_entry); |
|---|
| 442 | 428 | |
|---|
| 443 | | - if (fdt_version(fdt) >= 17) { |
|---|
| 429 | + if (can_assume(LATEST) || fdt_version(fdt) >= 17) { |
|---|
| 444 | 430 | struct_size = fdt_size_dt_struct(fdt); |
|---|
| 445 | 431 | } else { |
|---|
| 446 | 432 | struct_size = 0; |
|---|
| .. | .. |
|---|
| 450 | 436 | return struct_size; |
|---|
| 451 | 437 | } |
|---|
| 452 | 438 | |
|---|
| 453 | | - if (!fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) { |
|---|
| 439 | + if (can_assume(LIBFDT_ORDER) || |
|---|
| 440 | + !fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) { |
|---|
| 454 | 441 | /* no further work necessary */ |
|---|
| 455 | 442 | err = fdt_move(fdt, buf, bufsize); |
|---|
| 456 | 443 | if (err) |
|---|
| .. | .. |
|---|
| 494 | 481 | { |
|---|
| 495 | 482 | int mem_rsv_size; |
|---|
| 496 | 483 | |
|---|
| 497 | | - FDT_RW_CHECK_HEADER(fdt); |
|---|
| 484 | + FDT_RW_PROBE(fdt); |
|---|
| 498 | 485 | |
|---|
| 499 | 486 | mem_rsv_size = (fdt_num_mem_rsv(fdt)+1) |
|---|
| 500 | 487 | * sizeof(struct fdt_reserve_entry); |
|---|