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