Blame libfdt/fdt.c

Packit Service 0ee8e1
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
Packit 2ad57b
/*
Packit 2ad57b
 * libfdt - Flat Device Tree manipulation
Packit 2ad57b
 * Copyright (C) 2006 David Gibson, IBM Corporation.
Packit 2ad57b
 */
Packit 2ad57b
#include "libfdt_env.h"
Packit 2ad57b
Packit 2ad57b
#include <fdt.h>
Packit 2ad57b
#include <libfdt.h>
Packit 2ad57b
Packit 2ad57b
#include "libfdt_internal.h"
Packit 2ad57b
Packit Service 0ee8e1
/*
Packit Service 0ee8e1
 * Minimal sanity check for a read-only tree. fdt_ro_probe_() checks
Packit Service 0ee8e1
 * that the given buffer contains what appears to be a flattened
Packit Service 0ee8e1
 * device tree with sane information in its header.
Packit Service 0ee8e1
 */
Packit Service 0ee8e1
int32_t fdt_ro_probe_(const void *fdt)
Packit 2ad57b
{
Packit Service 0ee8e1
	uint32_t totalsize = fdt_totalsize(fdt);
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (can_assume(VALID_DTB))
Packit Service 0ee8e1
		return totalsize;
Packit Service 0ee8e1
Packit 2ad57b
	if (fdt_magic(fdt) == FDT_MAGIC) {
Packit 2ad57b
		/* Complete tree */
Packit Service 0ee8e1
		if (!can_assume(LATEST)) {
Packit Service 0ee8e1
			if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
Packit Service 0ee8e1
				return -FDT_ERR_BADVERSION;
Packit Service 0ee8e1
			if (fdt_last_comp_version(fdt) >
Packit Service 0ee8e1
					FDT_LAST_SUPPORTED_VERSION)
Packit Service 0ee8e1
				return -FDT_ERR_BADVERSION;
Packit Service 0ee8e1
		}
Packit 2ad57b
	} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
Packit 2ad57b
		/* Unfinished sequential-write blob */
Packit Service 0ee8e1
		if (!can_assume(VALID_INPUT) && fdt_size_dt_struct(fdt) == 0)
Packit 2ad57b
			return -FDT_ERR_BADSTATE;
Packit 2ad57b
	} else {
Packit 2ad57b
		return -FDT_ERR_BADMAGIC;
Packit 2ad57b
	}
Packit 2ad57b
Packit Service 0ee8e1
	if (totalsize < INT32_MAX)
Packit Service 0ee8e1
		return totalsize;
Packit Service 0ee8e1
	else
Packit Service 0ee8e1
		return -FDT_ERR_TRUNCATED;
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit Service 0ee8e1
static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	return (off >= hdrsize) && (off <= totalsize);
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit Service 0ee8e1
static int check_block_(uint32_t hdrsize, uint32_t totalsize,
Packit Service 0ee8e1
			uint32_t base, uint32_t size)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	if (!check_off_(hdrsize, totalsize, base))
Packit Service 0ee8e1
		return 0; /* block start out of bounds */
Packit Service 0ee8e1
	if ((base + size) < base)
Packit Service 0ee8e1
		return 0; /* overflow */
Packit Service 0ee8e1
	if (!check_off_(hdrsize, totalsize, base + size))
Packit Service 0ee8e1
		return 0; /* block end out of bounds */
Packit Service 0ee8e1
	return 1;
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit Service 0ee8e1
size_t fdt_header_size_(uint32_t version)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	if (version <= 1)
Packit Service 0ee8e1
		return FDT_V1_SIZE;
Packit Service 0ee8e1
	else if (version <= 2)
Packit Service 0ee8e1
		return FDT_V2_SIZE;
Packit Service 0ee8e1
	else if (version <= 3)
Packit Service 0ee8e1
		return FDT_V3_SIZE;
Packit Service 0ee8e1
	else if (version <= 16)
Packit Service 0ee8e1
		return FDT_V16_SIZE;
Packit Service 0ee8e1
	else
Packit Service 0ee8e1
		return FDT_V17_SIZE;
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit Service 0ee8e1
size_t fdt_header_size(const void *fdt)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	return can_assume(LATEST) ? FDT_V17_SIZE :
Packit Service 0ee8e1
		fdt_header_size_(fdt_version(fdt));
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit Service 0ee8e1
int fdt_check_header(const void *fdt)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	size_t hdrsize;
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (fdt_magic(fdt) != FDT_MAGIC)
Packit Service 0ee8e1
		return -FDT_ERR_BADMAGIC;
Packit Service 0ee8e1
	if (!can_assume(LATEST)) {
Packit Service 0ee8e1
		if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
Packit Service 0ee8e1
		    || (fdt_last_comp_version(fdt) >
Packit Service 0ee8e1
			FDT_LAST_SUPPORTED_VERSION))
Packit Service 0ee8e1
			return -FDT_ERR_BADVERSION;
Packit Service 0ee8e1
		if (fdt_version(fdt) < fdt_last_comp_version(fdt))
Packit Service 0ee8e1
			return -FDT_ERR_BADVERSION;
Packit Service 0ee8e1
	}
Packit Service 0ee8e1
	hdrsize = fdt_header_size(fdt);
Packit Service 0ee8e1
	if (!can_assume(VALID_DTB)) {
Packit Service 0ee8e1
Packit Service 0ee8e1
		if ((fdt_totalsize(fdt) < hdrsize)
Packit Service 0ee8e1
		    || (fdt_totalsize(fdt) > INT_MAX))
Packit Service 0ee8e1
			return -FDT_ERR_TRUNCATED;
Packit Service 0ee8e1
Packit Service 0ee8e1
		/* Bounds check memrsv block */
Packit Service 0ee8e1
		if (!check_off_(hdrsize, fdt_totalsize(fdt),
Packit Service 0ee8e1
				fdt_off_mem_rsvmap(fdt)))
Packit Service 0ee8e1
			return -FDT_ERR_TRUNCATED;
Packit Service 0ee8e1
	}
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (!can_assume(VALID_DTB)) {
Packit Service 0ee8e1
		/* Bounds check structure block */
Packit Service 0ee8e1
		if (!can_assume(LATEST) && fdt_version(fdt) < 17) {
Packit Service 0ee8e1
			if (!check_off_(hdrsize, fdt_totalsize(fdt),
Packit Service 0ee8e1
					fdt_off_dt_struct(fdt)))
Packit Service 0ee8e1
				return -FDT_ERR_TRUNCATED;
Packit Service 0ee8e1
		} else {
Packit Service 0ee8e1
			if (!check_block_(hdrsize, fdt_totalsize(fdt),
Packit Service 0ee8e1
					  fdt_off_dt_struct(fdt),
Packit Service 0ee8e1
					  fdt_size_dt_struct(fdt)))
Packit Service 0ee8e1
				return -FDT_ERR_TRUNCATED;
Packit Service 0ee8e1
		}
Packit Service 0ee8e1
Packit Service 0ee8e1
		/* Bounds check strings block */
Packit Service 0ee8e1
		if (!check_block_(hdrsize, fdt_totalsize(fdt),
Packit Service 0ee8e1
				  fdt_off_dt_strings(fdt),
Packit Service 0ee8e1
				  fdt_size_dt_strings(fdt)))
Packit Service 0ee8e1
			return -FDT_ERR_TRUNCATED;
Packit Service 0ee8e1
	}
Packit Service 0ee8e1
Packit 2ad57b
	return 0;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
Packit 2ad57b
{
Packit 2ad57b
	unsigned absoffset = offset + fdt_off_dt_struct(fdt);
Packit 2ad57b
Packit Service 0ee8e1
	if (!can_assume(VALID_INPUT))
Packit Service 0ee8e1
		if ((absoffset < offset)
Packit Service 0ee8e1
		    || ((absoffset + len) < absoffset)
Packit Service 0ee8e1
		    || (absoffset + len) > fdt_totalsize(fdt))
Packit Service 0ee8e1
			return NULL;
Packit 2ad57b
Packit Service 0ee8e1
	if (can_assume(LATEST) || fdt_version(fdt) >= 0x11)
Packit 2ad57b
		if (((offset + len) < offset)
Packit 2ad57b
		    || ((offset + len) > fdt_size_dt_struct(fdt)))
Packit 2ad57b
			return NULL;
Packit 2ad57b
Packit 2ad57b
	return fdt_offset_ptr_(fdt, offset);
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
Packit 2ad57b
{
Packit 2ad57b
	const fdt32_t *tagp, *lenp;
Packit 2ad57b
	uint32_t tag;
Packit 2ad57b
	int offset = startoffset;
Packit 2ad57b
	const char *p;
Packit 2ad57b
Packit 2ad57b
	*nextoffset = -FDT_ERR_TRUNCATED;
Packit 2ad57b
	tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
Packit Service 0ee8e1
	if (!can_assume(VALID_DTB) && !tagp)
Packit 2ad57b
		return FDT_END; /* premature end */
Packit 2ad57b
	tag = fdt32_to_cpu(*tagp);
Packit 2ad57b
	offset += FDT_TAGSIZE;
Packit 2ad57b
Packit 2ad57b
	*nextoffset = -FDT_ERR_BADSTRUCTURE;
Packit 2ad57b
	switch (tag) {
Packit 2ad57b
	case FDT_BEGIN_NODE:
Packit 2ad57b
		/* skip name */
Packit 2ad57b
		do {
Packit 2ad57b
			p = fdt_offset_ptr(fdt, offset++, 1);
Packit 2ad57b
		} while (p && (*p != '\0'));
Packit Service 0ee8e1
		if (!can_assume(VALID_DTB) && !p)
Packit 2ad57b
			return FDT_END; /* premature end */
Packit 2ad57b
		break;
Packit 2ad57b
Packit 2ad57b
	case FDT_PROP:
Packit 2ad57b
		lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
Packit Service 0ee8e1
		if (!can_assume(VALID_DTB) && !lenp)
Packit 2ad57b
			return FDT_END; /* premature end */
Packit 2ad57b
		/* skip-name offset, length and value */
Packit 2ad57b
		offset += sizeof(struct fdt_property) - FDT_TAGSIZE
Packit 2ad57b
			+ fdt32_to_cpu(*lenp);
Packit Service 0ee8e1
		if (!can_assume(LATEST) &&
Packit Service 0ee8e1
		    fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 &&
Packit Service 0ee8e1
		    ((offset - fdt32_to_cpu(*lenp)) % 8) != 0)
Packit Service 0ee8e1
			offset += 4;
Packit 2ad57b
		break;
Packit 2ad57b
Packit 2ad57b
	case FDT_END:
Packit 2ad57b
	case FDT_END_NODE:
Packit 2ad57b
	case FDT_NOP:
Packit 2ad57b
		break;
Packit 2ad57b
Packit 2ad57b
	default:
Packit 2ad57b
		return FDT_END;
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
Packit 2ad57b
		return FDT_END; /* premature end */
Packit 2ad57b
Packit 2ad57b
	*nextoffset = FDT_TAGALIGN(offset);
Packit 2ad57b
	return tag;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
int fdt_check_node_offset_(const void *fdt, int offset)
Packit 2ad57b
{
Packit Service 0ee8e1
	if (can_assume(VALID_INPUT))
Packit Service 0ee8e1
		return offset;
Packit 2ad57b
	if ((offset < 0) || (offset % FDT_TAGSIZE)
Packit 2ad57b
	    || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
Packit 2ad57b
		return -FDT_ERR_BADOFFSET;
Packit 2ad57b
Packit 2ad57b
	return offset;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
int fdt_check_prop_offset_(const void *fdt, int offset)
Packit 2ad57b
{
Packit 2ad57b
	if ((offset < 0) || (offset % FDT_TAGSIZE)
Packit 2ad57b
	    || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
Packit 2ad57b
		return -FDT_ERR_BADOFFSET;
Packit 2ad57b
Packit 2ad57b
	return offset;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
int fdt_next_node(const void *fdt, int offset, int *depth)
Packit 2ad57b
{
Packit 2ad57b
	int nextoffset = 0;
Packit 2ad57b
	uint32_t tag;
Packit 2ad57b
Packit 2ad57b
	if (offset >= 0)
Packit 2ad57b
		if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
Packit 2ad57b
			return nextoffset;
Packit 2ad57b
Packit 2ad57b
	do {
Packit 2ad57b
		offset = nextoffset;
Packit 2ad57b
		tag = fdt_next_tag(fdt, offset, &nextoffset);
Packit 2ad57b
Packit 2ad57b
		switch (tag) {
Packit 2ad57b
		case FDT_PROP:
Packit 2ad57b
		case FDT_NOP:
Packit 2ad57b
			break;
Packit 2ad57b
Packit 2ad57b
		case FDT_BEGIN_NODE:
Packit 2ad57b
			if (depth)
Packit 2ad57b
				(*depth)++;
Packit 2ad57b
			break;
Packit 2ad57b
Packit 2ad57b
		case FDT_END_NODE:
Packit 2ad57b
			if (depth && ((--(*depth)) < 0))
Packit 2ad57b
				return nextoffset;
Packit 2ad57b
			break;
Packit 2ad57b
Packit 2ad57b
		case FDT_END:
Packit 2ad57b
			if ((nextoffset >= 0)
Packit 2ad57b
			    || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
Packit 2ad57b
				return -FDT_ERR_NOTFOUND;
Packit 2ad57b
			else
Packit 2ad57b
				return nextoffset;
Packit 2ad57b
		}
Packit 2ad57b
	} while (tag != FDT_BEGIN_NODE);
Packit 2ad57b
Packit 2ad57b
	return offset;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
int fdt_first_subnode(const void *fdt, int offset)
Packit 2ad57b
{
Packit 2ad57b
	int depth = 0;
Packit 2ad57b
Packit 2ad57b
	offset = fdt_next_node(fdt, offset, &depth);
Packit 2ad57b
	if (offset < 0 || depth != 1)
Packit 2ad57b
		return -FDT_ERR_NOTFOUND;
Packit 2ad57b
Packit 2ad57b
	return offset;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
int fdt_next_subnode(const void *fdt, int offset)
Packit 2ad57b
{
Packit 2ad57b
	int depth = 1;
Packit 2ad57b
Packit 2ad57b
	/*
Packit 2ad57b
	 * With respect to the parent, the depth of the next subnode will be
Packit 2ad57b
	 * the same as the last.
Packit 2ad57b
	 */
Packit 2ad57b
	do {
Packit 2ad57b
		offset = fdt_next_node(fdt, offset, &depth);
Packit 2ad57b
		if (offset < 0 || depth < 1)
Packit 2ad57b
			return -FDT_ERR_NOTFOUND;
Packit 2ad57b
	} while (depth > 1);
Packit 2ad57b
Packit 2ad57b
	return offset;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)
Packit 2ad57b
{
Packit 2ad57b
	int len = strlen(s) + 1;
Packit 2ad57b
	const char *last = strtab + tabsize - len;
Packit 2ad57b
	const char *p;
Packit 2ad57b
Packit 2ad57b
	for (p = strtab; p <= last; p++)
Packit 2ad57b
		if (memcmp(p, s, len) == 0)
Packit 2ad57b
			return p;
Packit 2ad57b
	return NULL;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
int fdt_move(const void *fdt, void *buf, int bufsize)
Packit 2ad57b
{
Packit Service 0ee8e1
	FDT_RO_PROBE(fdt);
Packit 2ad57b
Packit 2ad57b
	if (fdt_totalsize(fdt) > bufsize)
Packit 2ad57b
		return -FDT_ERR_NOSPACE;
Packit 2ad57b
Packit 2ad57b
	memmove(buf, fdt, fdt_totalsize(fdt));
Packit 2ad57b
	return 0;
Packit 2ad57b
}