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