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{ 79318102Sgonzo unsigned absoffset = offset + fdt_off_dt_struct(fdt); 80204431Sraj 81318102Sgonzo if ((absoffset < offset) 82318102Sgonzo || ((absoffset + len) < absoffset) 83318102Sgonzo || (absoffset + len) > fdt_totalsize(fdt)) 84318102Sgonzo return NULL; 85318102Sgonzo 86204431Sraj if (fdt_version(fdt) >= 0x11) 87204431Sraj if (((offset + len) < offset) 88204431Sraj || ((offset + len) > fdt_size_dt_struct(fdt))) 89204431Sraj return NULL; 90204431Sraj 91318102Sgonzo return _fdt_offset_ptr(fdt, offset); 92204431Sraj} 93204431Sraj 94204433Srajuint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) 95204431Sraj{ 96261215Simp 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); 126204431Sraj break; 127204433Sraj 128204433Sraj case FDT_END: 129204433Sraj case FDT_END_NODE: 130204433Sraj case FDT_NOP: 131204433Sraj break; 132204433Sraj 133204433Sraj default: 134204433Sraj return FDT_END; 135204431Sraj } 136204431Sraj 137204433Sraj if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset)) 138204433Sraj return FDT_END; /* premature end */ 139204431Sraj 140204433Sraj *nextoffset = FDT_TAGALIGN(offset); 141204431Sraj return tag; 142204431Sraj} 143204431Sraj 144204431Srajint _fdt_check_node_offset(const void *fdt, int offset) 145204431Sraj{ 146204431Sraj if ((offset < 0) || (offset % FDT_TAGSIZE) 147204431Sraj || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)) 148204431Sraj return -FDT_ERR_BADOFFSET; 149204431Sraj 150204431Sraj return offset; 151204431Sraj} 152204431Sraj 153238742Simpint _fdt_check_prop_offset(const void *fdt, int offset) 154238742Simp{ 155238742Simp if ((offset < 0) || (offset % FDT_TAGSIZE) 156238742Simp || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)) 157238742Simp return -FDT_ERR_BADOFFSET; 158238742Simp 159238742Simp return offset; 160238742Simp} 161238742Simp 162204431Srajint fdt_next_node(const void *fdt, int offset, int *depth) 163204431Sraj{ 164204431Sraj int nextoffset = 0; 165204431Sraj uint32_t tag; 166204431Sraj 167204431Sraj if (offset >= 0) 168204431Sraj if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0) 169204431Sraj return nextoffset; 170204431Sraj 171204431Sraj do { 172204431Sraj offset = nextoffset; 173204431Sraj tag = fdt_next_tag(fdt, offset, &nextoffset); 174204431Sraj 175204431Sraj switch (tag) { 176204431Sraj case FDT_PROP: 177204431Sraj case FDT_NOP: 178204431Sraj break; 179204431Sraj 180204431Sraj case FDT_BEGIN_NODE: 181204431Sraj if (depth) 182204431Sraj (*depth)++; 183204431Sraj break; 184204431Sraj 185204431Sraj case FDT_END_NODE: 186204433Sraj if (depth && ((--(*depth)) < 0)) 187204433Sraj return nextoffset; 188204431Sraj break; 189204431Sraj 190204431Sraj case FDT_END: 191204433Sraj if ((nextoffset >= 0) 192204433Sraj || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth)) 193204433Sraj return -FDT_ERR_NOTFOUND; 194204433Sraj else 195204433Sraj return nextoffset; 196204431Sraj } 197204431Sraj } while (tag != FDT_BEGIN_NODE); 198204431Sraj 199204431Sraj return offset; 200204431Sraj} 201204431Sraj 202261215Simpint fdt_first_subnode(const void *fdt, int offset) 203261215Simp{ 204261215Simp int depth = 0; 205261215Simp 206261215Simp offset = fdt_next_node(fdt, offset, &depth); 207261215Simp if (offset < 0 || depth != 1) 208261215Simp return -FDT_ERR_NOTFOUND; 209261215Simp 210261215Simp return offset; 211261215Simp} 212261215Simp 213261215Simpint fdt_next_subnode(const void *fdt, int offset) 214261215Simp{ 215261215Simp int depth = 1; 216261215Simp 217261215Simp /* 218261215Simp * With respect to the parent, the depth of the next subnode will be 219261215Simp * the same as the last. 220261215Simp */ 221261215Simp do { 222261215Simp offset = fdt_next_node(fdt, offset, &depth); 223261215Simp if (offset < 0 || depth < 1) 224261215Simp return -FDT_ERR_NOTFOUND; 225261215Simp } while (depth > 1); 226261215Simp 227261215Simp return offset; 228261215Simp} 229261215Simp 230204431Srajconst char *_fdt_find_string(const char *strtab, int tabsize, const char *s) 231204431Sraj{ 232204431Sraj int len = strlen(s) + 1; 233204431Sraj const char *last = strtab + tabsize - len; 234204431Sraj const char *p; 235204431Sraj 236204431Sraj for (p = strtab; p <= last; p++) 237204431Sraj if (memcmp(p, s, len) == 0) 238204431Sraj return p; 239204431Sraj return NULL; 240204431Sraj} 241204431Sraj 242204431Srajint fdt_move(const void *fdt, void *buf, int bufsize) 243204431Sraj{ 244204431Sraj FDT_CHECK_HEADER(fdt); 245204431Sraj 246204431Sraj if (fdt_totalsize(fdt) > bufsize) 247204431Sraj return -FDT_ERR_NOSPACE; 248204431Sraj 249204431Sraj memmove(buf, fdt, fdt_totalsize(fdt)); 250204431Sraj return 0; 251204431Sraj} 252