fdt_rw.c 12.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*
 * libfdt - Flat Device Tree manipulation
 * Copyright (C) 2006 David Gibson, IBM Corporation.
 *
 * libfdt is dual licensed: you can use it either under the terms of
 * the GPL, or the BSD license, at your option.
 *
 *  a) This library is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of the
 *     License, or (at your option) any later version.
 *
 *     This library is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public
 *     License along with this library; if not, write to the Free
 *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 *     MA 02110-1301 USA
 *
 * Alternatively,
 *
 *  b) Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *     1. Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *     2. Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "libfdt_env.h"

#include <fdt.h>
#include <libfdt.h>

#include "libfdt_internal.h"

58
static int _fdt_blocks_misordered(const void *fdt,
59 60
			      int mem_rsv_size, int struct_size)
{
61
	return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
62 63 64 65 66 67 68 69
		|| (fdt_off_dt_struct(fdt) <
		    (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
		|| (fdt_off_dt_strings(fdt) <
		    (fdt_off_dt_struct(fdt) + struct_size))
		|| (fdt_totalsize(fdt) <
		    (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
}

70
static int _fdt_rw_check_header(void *fdt)
71
{
72
	FDT_CHECK_HEADER(fdt);
73 74 75

	if (fdt_version(fdt) < 17)
		return -FDT_ERR_BADVERSION;
76 77
	if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry),
				   fdt_size_dt_struct(fdt)))
78 79 80 81 82 83 84
		return -FDT_ERR_BADLAYOUT;
	if (fdt_version(fdt) > 17)
		fdt_set_version(fdt, 17);

	return 0;
}

85
#define FDT_RW_CHECK_HEADER(fdt) \
86 87
	{ \
		int err; \
88
		if ((err = _fdt_rw_check_header(fdt)) != 0) \
89 90 91
			return err; \
	}

92
static inline int _fdt_data_size(void *fdt)
93 94 95 96
{
	return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
}

97
static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen)
98
{
99 100
	char *p = splicepoint;
	char *end = (char *)fdt + _fdt_data_size(fdt);
101 102 103

	if (((p + oldlen) < p) || ((p + oldlen) > end))
		return -FDT_ERR_BADOFFSET;
104
	if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
105 106 107 108 109
		return -FDT_ERR_NOSPACE;
	memmove(p + newlen, p + oldlen, end - p - oldlen);
	return 0;
}

110 111
static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
			       int oldn, int newn)
112 113 114
{
	int delta = (newn - oldn) * sizeof(*p);
	int err;
115
	err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
116 117 118 119 120 121 122
	if (err)
		return err;
	fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
	return 0;
}

123 124
static int _fdt_splice_struct(void *fdt, void *p,
			      int oldlen, int newlen)
125 126 127 128
{
	int delta = newlen - oldlen;
	int err;

129
	if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
130 131 132 133 134 135 136
		return err;

	fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
	return 0;
}

137
static int _fdt_splice_string(void *fdt, int newlen)
138
{
139 140
	void *p = (char *)fdt
		+ fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
141 142
	int err;

143
	if ((err = _fdt_splice(fdt, p, 0, newlen)))
144 145 146 147 148 149
		return err;

	fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
	return 0;
}

150
static int _fdt_find_add_string(void *fdt, const char *s)
151 152 153 154 155 156 157 158 159 160 161 162 163
{
	char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
	const char *p;
	char *new;
	int len = strlen(s) + 1;
	int err;

	p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
	if (p)
		/* found it */
		return (p - strtab);

	new = strtab + fdt_size_dt_strings(fdt);
164
	err = _fdt_splice_string(fdt, len);
165 166 167 168 169 170 171 172 173 174 175 176
	if (err)
		return err;

	memcpy(new, s, len);
	return (new - strtab);
}

int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
{
	struct fdt_reserve_entry *re;
	int err;

177
	FDT_RW_CHECK_HEADER(fdt);
178 179

	re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
180
	err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
181 182 183 184 185 186 187 188 189 190 191 192 193
	if (err)
		return err;

	re->address = cpu_to_fdt64(address);
	re->size = cpu_to_fdt64(size);
	return 0;
}

int fdt_del_mem_rsv(void *fdt, int n)
{
	struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
	int err;

194 195
	FDT_RW_CHECK_HEADER(fdt);

196 197 198
	if (n >= fdt_num_mem_rsv(fdt))
		return -FDT_ERR_NOTFOUND;

199
	err = _fdt_splice_mem_rsv(fdt, re, 1, 0);
200 201 202 203 204
	if (err)
		return err;
	return 0;
}

205 206
static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
				int len, struct fdt_property **prop)
207 208 209 210 211 212 213 214
{
	int oldlen;
	int err;

	*prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
	if (! (*prop))
		return oldlen;

215 216
	if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
				      FDT_TAGALIGN(len))))
217 218 219 220 221 222
		return err;

	(*prop)->len = cpu_to_fdt32(len);
	return 0;
}

223 224
static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
			     int len, struct fdt_property **prop)
225 226 227 228 229 230
{
	int proplen;
	int nextoffset;
	int namestroff;
	int err;

231 232
	if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
		return nextoffset;
233

234
	namestroff = _fdt_find_add_string(fdt, name);
235 236 237 238
	if (namestroff < 0)
		return namestroff;

	*prop = _fdt_offset_ptr_w(fdt, nextoffset);
239
	proplen = sizeof(**prop) + FDT_TAGALIGN(len);
240

241
	err = _fdt_splice_struct(fdt, *prop, 0, proplen);
242 243 244 245 246 247 248 249 250
	if (err)
		return err;

	(*prop)->tag = cpu_to_fdt32(FDT_PROP);
	(*prop)->nameoff = cpu_to_fdt32(namestroff);
	(*prop)->len = cpu_to_fdt32(len);
	return 0;
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
int fdt_set_name(void *fdt, int nodeoffset, const char *name)
{
	char *namep;
	int oldlen, newlen;
	int err;

	FDT_RW_CHECK_HEADER(fdt);

	namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
	if (!namep)
		return oldlen;

	newlen = strlen(name);

	err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
				 FDT_TAGALIGN(newlen+1));
	if (err)
		return err;

	memcpy(namep, name, newlen+1);
	return 0;
}

274 275 276 277 278 279
int fdt_setprop(void *fdt, int nodeoffset, const char *name,
		const void *val, int len)
{
	struct fdt_property *prop;
	int err;

280
	FDT_RW_CHECK_HEADER(fdt);
281

282
	err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
283
	if (err == -FDT_ERR_NOTFOUND)
284
		err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
285 286 287 288 289 290 291 292 293 294 295 296
	if (err)
		return err;

	memcpy(prop->data, val, len);
	return 0;
}

int fdt_delprop(void *fdt, int nodeoffset, const char *name)
{
	struct fdt_property *prop;
	int len, proplen;

297
	FDT_RW_CHECK_HEADER(fdt);
298 299 300 301 302

	prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
	if (! prop)
		return len;

303 304
	proplen = sizeof(*prop) + FDT_TAGALIGN(len);
	return _fdt_splice_struct(fdt, prop, proplen, 0);
305 306 307 308 309 310 311 312 313 314 315 316
}

int fdt_add_subnode_namelen(void *fdt, int parentoffset,
			    const char *name, int namelen)
{
	struct fdt_node_header *nh;
	int offset, nextoffset;
	int nodelen;
	int err;
	uint32_t tag;
	uint32_t *endtag;

317
	FDT_RW_CHECK_HEADER(fdt);
318 319 320 321 322 323 324 325 326 327 328 329

	offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
	if (offset >= 0)
		return -FDT_ERR_EXISTS;
	else if (offset != -FDT_ERR_NOTFOUND)
		return offset;

	/* Try to place the new node after the parent's properties */
	fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
	do {
		offset = nextoffset;
		tag = fdt_next_tag(fdt, offset, &nextoffset);
330
	} while ((tag == FDT_PROP) || (tag == FDT_NOP));
331 332

	nh = _fdt_offset_ptr_w(fdt, offset);
333
	nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
334

335
	err = _fdt_splice_struct(fdt, nh, 0, nodelen);
336 337 338 339
	if (err)
		return err;

	nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
340
	memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
341
	memcpy(nh->name, name, namelen);
342
	endtag = (uint32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
343 344 345 346 347 348 349 350 351 352 353 354 355 356
	*endtag = cpu_to_fdt32(FDT_END_NODE);

	return offset;
}

int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
{
	return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
}

int fdt_del_node(void *fdt, int nodeoffset)
{
	int endoffset;

357
	FDT_RW_CHECK_HEADER(fdt);
358 359 360 361 362

	endoffset = _fdt_node_end_offset(fdt, nodeoffset);
	if (endoffset < 0)
		return endoffset;

363 364
	return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
				  endoffset - nodeoffset, 0);
365 366
}

367 368
static void _fdt_packblocks(const char *old, char *new,
			    int mem_rsv_size, int struct_size)
369 370 371
{
	int mem_rsv_off, struct_off, strings_off;

372
	mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
373 374 375
	struct_off = mem_rsv_off + mem_rsv_size;
	strings_off = struct_off + struct_size;

376 377
	memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
	fdt_set_off_mem_rsvmap(new, mem_rsv_off);
378

379 380 381
	memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
	fdt_set_off_dt_struct(new, struct_off);
	fdt_set_size_dt_struct(new, struct_size);
382

383 384 385 386
	memmove(new + strings_off, old + fdt_off_dt_strings(old),
		fdt_size_dt_strings(old));
	fdt_set_off_dt_strings(new, strings_off);
	fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
387 388 389 390 391 392 393
}

int fdt_open_into(const void *fdt, void *buf, int bufsize)
{
	int err;
	int mem_rsv_size, struct_size;
	int newsize;
394 395 396
	const char *fdtstart = fdt;
	const char *fdtend = fdtstart + fdt_totalsize(fdt);
	char *tmp;
397

398
	FDT_CHECK_HEADER(fdt);
399 400 401 402 403 404 405 406 407 408 409 410

	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
		* sizeof(struct fdt_reserve_entry);

	if (fdt_version(fdt) >= 17) {
		struct_size = fdt_size_dt_struct(fdt);
	} else {
		struct_size = 0;
		while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
			;
	}

411
	if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
412 413 414 415 416 417 418 419 420 421 422
		/* no further work necessary */
		err = fdt_move(fdt, buf, bufsize);
		if (err)
			return err;
		fdt_set_version(buf, 17);
		fdt_set_size_dt_struct(buf, struct_size);
		fdt_set_totalsize(buf, bufsize);
		return 0;
	}

	/* Need to reorder */
423
	newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
424 425 426 427 428
		+ struct_size + fdt_size_dt_strings(fdt);

	if (bufsize < newsize)
		return -FDT_ERR_NOSPACE;

429 430 431 432 433 434 435
	/* First attempt to build converted tree at beginning of buffer */
	tmp = buf;
	/* But if that overlaps with the old tree... */
	if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
		/* Try right after the old tree instead */
		tmp = (char *)(uintptr_t)fdtend;
		if ((tmp + newsize) > ((char *)buf + bufsize))
436 437 438
			return -FDT_ERR_NOSPACE;
	}

439
	_fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	memmove(buf, tmp, newsize);

	fdt_set_magic(buf, FDT_MAGIC);
	fdt_set_totalsize(buf, bufsize);
	fdt_set_version(buf, 17);
	fdt_set_last_comp_version(buf, 16);
	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));

	return 0;
}

int fdt_pack(void *fdt)
{
	int mem_rsv_size;

455
	FDT_RW_CHECK_HEADER(fdt);
456 457 458

	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
		* sizeof(struct fdt_reserve_entry);
459 460
	_fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
	fdt_set_totalsize(fdt, _fdt_data_size(fdt));
461 462 463

	return 0;
}