1204431Sraj/* 2204431Sraj * libfdt - Flat Device Tree manipulation 3204431Sraj * Copyright (C) 2006 David Gibson, IBM Corporation. 4204431Sraj * 5204431Sraj * libfdt is dual licensed: you can use it either under the terms of 6204431Sraj * the GPL, or the BSD license, at your option. 7204431Sraj * 8204431Sraj * a) This library is free software; you can redistribute it and/or 9204431Sraj * modify it under the terms of the GNU General Public License as 10204431Sraj * published by the Free Software Foundation; either version 2 of the 11204431Sraj * License, or (at your option) any later version. 12204431Sraj * 13204431Sraj * This library is distributed in the hope that it will be useful, 14204431Sraj * but WITHOUT ANY WARRANTY; without even the implied warranty of 15204431Sraj * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16204431Sraj * GNU General Public License for more details. 17204431Sraj * 18204431Sraj * You should have received a copy of the GNU General Public 19204431Sraj * License along with this library; if not, write to the Free 20204431Sraj * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 21204431Sraj * MA 02110-1301 USA 22204431Sraj * 23204431Sraj * Alternatively, 24204431Sraj * 25204431Sraj * b) Redistribution and use in source and binary forms, with or 26204431Sraj * without modification, are permitted provided that the following 27204431Sraj * conditions are met: 28204431Sraj * 29204431Sraj * 1. Redistributions of source code must retain the above 30204431Sraj * copyright notice, this list of conditions and the following 31204431Sraj * disclaimer. 32204431Sraj * 2. Redistributions in binary form must reproduce the above 33204431Sraj * copyright notice, this list of conditions and the following 34204431Sraj * disclaimer in the documentation and/or other materials 35204431Sraj * provided with the distribution. 36204431Sraj * 37204431Sraj * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 38204431Sraj * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 39204431Sraj * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 40204431Sraj * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 41204431Sraj * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 42204431Sraj * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43204431Sraj * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44204431Sraj * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45204431Sraj * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46204431Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 47204431Sraj * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 48204431Sraj * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 49204431Sraj * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50204431Sraj */ 51204431Sraj#include "libfdt_env.h" 52204431Sraj 53204431Sraj#include <fdt.h> 54204431Sraj#include <libfdt.h> 55204431Sraj 56204431Sraj#include "libfdt_internal.h" 57204431Sraj 58204431Srajint fdt_check_header(const void *fdt) 59204431Sraj{ 60204431Sraj if (fdt_magic(fdt) == FDT_MAGIC) { 61204431Sraj /* Complete tree */ 62204431Sraj if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) 63204431Sraj return -FDT_ERR_BADVERSION; 64204431Sraj if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) 65204431Sraj return -FDT_ERR_BADVERSION; 66204431Sraj } else if (fdt_magic(fdt) == FDT_SW_MAGIC) { 67204431Sraj /* Unfinished sequential-write blob */ 68204431Sraj if (fdt_size_dt_struct(fdt) == 0) 69204431Sraj return -FDT_ERR_BADSTATE; 70204431Sraj } else { 71204431Sraj return -FDT_ERR_BADMAGIC; 72204431Sraj } 73204431Sraj 74204431Sraj return 0; 75204431Sraj} 76204431Sraj 77204433Srajconst void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len) 78204431Sraj{ 79328459Skevans unsigned absoffset = offset + fdt_off_dt_struct(fdt); 80204431Sraj 81328459Skevans if ((absoffset < offset) 82328459Skevans || ((absoffset + len) < absoffset) 83328459Skevans || (absoffset + len) > fdt_totalsize(fdt)) 84328459Skevans return NULL; 85328459Skevans 86204431Sraj if (fdt_version(fdt) >= 0x11) 87204431Sraj if (((offset + len) < offset) 88204431Sraj || ((offset + len) > fdt_size_dt_struct(fdt))) 89204431Sraj return NULL; 90204431Sraj 91328459Skevans return fdt_offset_ptr_(fdt, offset); 92204431Sraj} 93204431Sraj 94204433Srajuint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) 95204431Sraj{ 96328459Skevans const fdt32_t *tagp, *lenp; 97204431Sraj uint32_t tag; 98204433Sraj int offset = startoffset; 99204431Sraj const char *p; 100204431Sraj 101204433Sraj *nextoffset = -FDT_ERR_TRUNCATED; 102204431Sraj tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE); 103204433Sraj if (!tagp) 104204431Sraj return FDT_END; /* premature end */ 105204431Sraj tag = fdt32_to_cpu(*tagp); 106204431Sraj offset += FDT_TAGSIZE; 107204431Sraj 108204433Sraj *nextoffset = -FDT_ERR_BADSTRUCTURE; 109204431Sraj switch (tag) { 110204431Sraj case FDT_BEGIN_NODE: 111204431Sraj /* skip name */ 112204431Sraj do { 113204431Sraj p = fdt_offset_ptr(fdt, offset++, 1); 114204431Sraj } while (p && (*p != '\0')); 115204433Sraj if (!p) 116204433Sraj return FDT_END; /* premature end */ 117204431Sraj break; 118204433Sraj 119204431Sraj case FDT_PROP: 120204431Sraj lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp)); 121204433Sraj if (!lenp) 122204433Sraj return FDT_END; /* premature end */ 123204433Sraj /* skip-name offset, length and value */ 124204433Sraj offset += sizeof(struct fdt_property) - FDT_TAGSIZE 125204433Sraj + fdt32_to_cpu(*lenp); 126328824Skevans if (fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 && 127328824Skevans ((offset - fdt32_to_cpu(*lenp)) % 8) != 0) 128328824Skevans offset += 4; 129204431Sraj break; 130204433Sraj 131204433Sraj case FDT_END: 132204433Sraj case FDT_END_NODE: 133204433Sraj case FDT_NOP: 134204433Sraj break; 135204433Sraj 136204433Sraj default: 137204433Sraj return FDT_END; 138204431Sraj } 139204431Sraj 140204433Sraj if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset)) 141204433Sraj return FDT_END; /* premature end */ 142204431Sraj 143204433Sraj *nextoffset = FDT_TAGALIGN(offset); 144204431Sraj return tag; 145204431Sraj} 146204431Sraj 147328459Skevansint fdt_check_node_offset_(const void *fdt, int offset) 148204431Sraj{ 149204431Sraj if ((offset < 0) || (offset % FDT_TAGSIZE) 150204431Sraj || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)) 151204431Sraj return -FDT_ERR_BADOFFSET; 152204431Sraj 153204431Sraj return offset; 154204431Sraj} 155204431Sraj 156328459Skevansint fdt_check_prop_offset_(const void *fdt, int offset) 157238742Simp{ 158238742Simp if ((offset < 0) || (offset % FDT_TAGSIZE) 159238742Simp || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)) 160238742Simp return -FDT_ERR_BADOFFSET; 161238742Simp 162238742Simp return offset; 163238742Simp} 164238742Simp 165204431Srajint fdt_next_node(const void *fdt, int offset, int *depth) 166204431Sraj{ 167204431Sraj int nextoffset = 0; 168204431Sraj uint32_t tag; 169204431Sraj 170204431Sraj if (offset >= 0) 171328459Skevans if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0) 172204431Sraj return nextoffset; 173204431Sraj 174204431Sraj do { 175204431Sraj offset = nextoffset; 176204431Sraj tag = fdt_next_tag(fdt, offset, &nextoffset); 177204431Sraj 178204431Sraj switch (tag) { 179204431Sraj case FDT_PROP: 180204431Sraj case FDT_NOP: 181204431Sraj break; 182204431Sraj 183204431Sraj case FDT_BEGIN_NODE: 184204431Sraj if (depth) 185204431Sraj (*depth)++; 186204431Sraj break; 187204431Sraj 188204431Sraj case FDT_END_NODE: 189204433Sraj if (depth && ((--(*depth)) < 0)) 190204433Sraj return nextoffset; 191204431Sraj break; 192204431Sraj 193204431Sraj case FDT_END: 194204433Sraj if ((nextoffset >= 0) 195204433Sraj || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth)) 196204433Sraj return -FDT_ERR_NOTFOUND; 197204433Sraj else 198204433Sraj return nextoffset; 199204431Sraj } 200204431Sraj } while (tag != FDT_BEGIN_NODE); 201204431Sraj 202204431Sraj return offset; 203204431Sraj} 204204431Sraj 205328459Skevansint fdt_first_subnode(const void *fdt, int offset) 206204431Sraj{ 207328459Skevans int depth = 0; 208328459Skevans 209328459Skevans offset = fdt_next_node(fdt, offset, &depth); 210328459Skevans if (offset < 0 || depth != 1) 211328459Skevans return -FDT_ERR_NOTFOUND; 212328459Skevans 213328459Skevans return offset; 214328459Skevans} 215328459Skevans 216328459Skevansint fdt_next_subnode(const void *fdt, int offset) 217328459Skevans{ 218328459Skevans int depth = 1; 219328459Skevans 220328459Skevans /* 221328459Skevans * With respect to the parent, the depth of the next subnode will be 222328459Skevans * the same as the last. 223328459Skevans */ 224328459Skevans do { 225328459Skevans offset = fdt_next_node(fdt, offset, &depth); 226328459Skevans if (offset < 0 || depth < 1) 227328459Skevans return -FDT_ERR_NOTFOUND; 228328459Skevans } while (depth > 1); 229328459Skevans 230328459Skevans return offset; 231328459Skevans} 232328459Skevans 233328459Skevansconst char *fdt_find_string_(const char *strtab, int tabsize, const char *s) 234328459Skevans{ 235204431Sraj int len = strlen(s) + 1; 236204431Sraj const char *last = strtab + tabsize - len; 237204431Sraj const char *p; 238204431Sraj 239204431Sraj for (p = strtab; p <= last; p++) 240204431Sraj if (memcmp(p, s, len) == 0) 241204431Sraj return p; 242204431Sraj return NULL; 243204431Sraj} 244204431Sraj 245204431Srajint fdt_move(const void *fdt, void *buf, int bufsize) 246204431Sraj{ 247204431Sraj FDT_CHECK_HEADER(fdt); 248204431Sraj 249204431Sraj if (fdt_totalsize(fdt) > bufsize) 250204431Sraj return -FDT_ERR_NOSPACE; 251204431Sraj 252204431Sraj memmove(buf, fdt, fdt_totalsize(fdt)); 253204431Sraj return 0; 254204431Sraj} 255