1 // SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0+) 2 /* 3 * libfdt - Flat Device Tree manipulation 4 * Copyright (C) 2006 David Gibson, IBM Corporation. 5 * 6 * libfdt is dual licensed: you can use it either under the terms of 7 * the GPL, or the BSD license, at your option. 8 * 9 * a) This library is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License as 11 * published by the Free Software Foundation; either version 2 of the 12 * License, or (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public 20 * License along with this library; if not, write to the Free 21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 22 * MA 02110-1301 USA 23 * 24 * Alternatively, 25 * 26 * b) Redistribution and use in source and binary forms, with or 27 * without modification, are permitted provided that the following 28 * conditions are met: 29 * 30 * 1. Redistributions of source code must retain the above 31 * copyright notice, this list of conditions and the following 32 * disclaimer. 33 * 2. Redistributions in binary form must reproduce the above 34 * copyright notice, this list of conditions and the following 35 * disclaimer in the documentation and/or other materials 36 * provided with the distribution. 37 * 38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 39 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 40 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 41 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 42 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 43 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 49 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 50 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 51 */ 52 #include "libfdt_env.h" 53 54 #include <fdt.h> 55 #include <libfdt.h> 56 57 #include "libfdt_internal.h" 58 59 int fdt_check_header(const void *fdt) 60 { 61 if (fdt_magic(fdt) == FDT_MAGIC) { 62 /* Complete tree */ 63 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) 64 return -FDT_ERR_BADVERSION; 65 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) 66 return -FDT_ERR_BADVERSION; 67 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) { 68 /* Unfinished sequential-write blob */ 69 if (fdt_size_dt_struct(fdt) == 0) 70 return -FDT_ERR_BADSTATE; 71 } else { 72 return -FDT_ERR_BADMAGIC; 73 } 74 75 return 0; 76 } 77 78 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len) 79 { 80 unsigned absoffset = offset + fdt_off_dt_struct(fdt); 81 82 if ((absoffset < offset) 83 || ((absoffset + len) < absoffset) 84 || (absoffset + len) > fdt_totalsize(fdt)) 85 return NULL; 86 87 if (fdt_version(fdt) >= 0x11) 88 if (((offset + len) < offset) 89 || ((offset + len) > fdt_size_dt_struct(fdt))) 90 return NULL; 91 92 return _fdt_offset_ptr(fdt, offset); 93 } 94 95 uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) 96 { 97 const fdt32_t *tagp, *lenp; 98 uint32_t tag; 99 int offset = startoffset; 100 const char *p; 101 102 *nextoffset = -FDT_ERR_TRUNCATED; 103 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE); 104 if (!tagp) 105 return FDT_END; /* premature end */ 106 tag = fdt32_to_cpu(*tagp); 107 offset += FDT_TAGSIZE; 108 109 *nextoffset = -FDT_ERR_BADSTRUCTURE; 110 switch (tag) { 111 case FDT_BEGIN_NODE: 112 /* skip name */ 113 do { 114 p = fdt_offset_ptr(fdt, offset++, 1); 115 } while (p && (*p != '\0')); 116 if (!p) 117 return FDT_END; /* premature end */ 118 break; 119 120 case FDT_PROP: 121 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp)); 122 if (!lenp) 123 return FDT_END; /* premature end */ 124 /* skip-name offset, length and value */ 125 offset += sizeof(struct fdt_property) - FDT_TAGSIZE 126 + fdt32_to_cpu(*lenp); 127 break; 128 129 case FDT_END: 130 case FDT_END_NODE: 131 case FDT_NOP: 132 break; 133 134 default: 135 return FDT_END; 136 } 137 138 if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset)) 139 return FDT_END; /* premature end */ 140 141 *nextoffset = FDT_TAGALIGN(offset); 142 return tag; 143 } 144 145 int _fdt_check_node_offset(const void *fdt, int offset) 146 { 147 if ((offset < 0) || (offset % FDT_TAGSIZE) 148 || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)) 149 return -FDT_ERR_BADOFFSET; 150 151 return offset; 152 } 153 154 int _fdt_check_prop_offset(const void *fdt, int offset) 155 { 156 if ((offset < 0) || (offset % FDT_TAGSIZE) 157 || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)) 158 return -FDT_ERR_BADOFFSET; 159 160 return offset; 161 } 162 163 int fdt_next_node(const void *fdt, int offset, int *depth) 164 { 165 int nextoffset = 0; 166 uint32_t tag; 167 168 if (offset >= 0) 169 if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0) 170 return nextoffset; 171 172 do { 173 offset = nextoffset; 174 tag = fdt_next_tag(fdt, offset, &nextoffset); 175 176 switch (tag) { 177 case FDT_PROP: 178 case FDT_NOP: 179 break; 180 181 case FDT_BEGIN_NODE: 182 if (depth) 183 (*depth)++; 184 break; 185 186 case FDT_END_NODE: 187 if (depth && ((--(*depth)) < 0)) 188 return nextoffset; 189 break; 190 191 case FDT_END: 192 if ((nextoffset >= 0) 193 || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth)) 194 return -FDT_ERR_NOTFOUND; 195 else 196 return nextoffset; 197 } 198 } while (tag != FDT_BEGIN_NODE); 199 200 return offset; 201 } 202 203 int fdt_first_subnode(const void *fdt, int offset) 204 { 205 int depth = 0; 206 207 offset = fdt_next_node(fdt, offset, &depth); 208 if (offset < 0 || depth != 1) 209 return -FDT_ERR_NOTFOUND; 210 211 return offset; 212 } 213 214 int fdt_next_subnode(const void *fdt, int offset) 215 { 216 int depth = 1; 217 218 /* 219 * With respect to the parent, the depth of the next subnode will be 220 * the same as the last. 221 */ 222 do { 223 offset = fdt_next_node(fdt, offset, &depth); 224 if (offset < 0 || depth < 1) 225 return -FDT_ERR_NOTFOUND; 226 } while (depth > 1); 227 228 return offset; 229 } 230 231 const char *_fdt_find_string(const char *strtab, int tabsize, const char *s) 232 { 233 int len = strlen(s) + 1; 234 const char *last = strtab + tabsize - len; 235 const char *p; 236 237 for (p = strtab; p <= last; p++) 238 if (memcmp(p, s, len) == 0) 239 return p; 240 return NULL; 241 } 242 243 int fdt_move(const void *fdt, void *buf, int bufsize) 244 { 245 FDT_CHECK_HEADER(fdt); 246 247 if (fdt_totalsize(fdt) > bufsize) 248 return -FDT_ERR_NOSPACE; 249 250 memmove(buf, fdt, fdt_totalsize(fdt)); 251 return 0; 252 } 253