checks.c 20.6 KB
Newer Older
D
David Gibson 已提交
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
/*
 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2007.
 *
 *
 * This program 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 program 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 program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *                                                                   USA
 */

#include "dtc.h"

#ifdef TRACE_CHECKS
#define TRACE(c, ...) \
	do { \
		fprintf(stderr, "=== %s: ", (c)->name); \
		fprintf(stderr, __VA_ARGS__); \
		fprintf(stderr, "\n"); \
	} while (0)
#else
#define TRACE(c, fmt, ...)	do { } while (0)
#endif

enum checkstatus {
	UNCHECKED = 0,
	PREREQ,
	PASSED,
	FAILED,
};

struct check;

43
typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
D
David Gibson 已提交
44 45 46

struct check {
	const char *name;
47
	check_fn fn;
D
David Gibson 已提交
48
	void *data;
S
Stephen Warren 已提交
49
	bool warn, error;
D
David Gibson 已提交
50
	enum checkstatus status;
51
	bool inprogress;
D
David Gibson 已提交
52 53 54 55
	int num_prereqs;
	struct check **prereq;
};

56 57 58 59 60 61 62 63
#define CHECK_ENTRY(_nm, _fn, _d, _w, _e, ...)	       \
	static struct check *_nm##_prereqs[] = { __VA_ARGS__ }; \
	static struct check _nm = { \
		.name = #_nm, \
		.fn = (_fn), \
		.data = (_d), \
		.warn = (_w), \
		.error = (_e), \
D
David Gibson 已提交
64
		.status = UNCHECKED, \
65 66
		.num_prereqs = ARRAY_SIZE(_nm##_prereqs), \
		.prereq = _nm##_prereqs, \
D
David Gibson 已提交
67
	};
68 69 70 71 72 73
#define WARNING(_nm, _fn, _d, ...) \
	CHECK_ENTRY(_nm, _fn, _d, true, false, __VA_ARGS__)
#define ERROR(_nm, _fn, _d, ...) \
	CHECK_ENTRY(_nm, _fn, _d, false, true, __VA_ARGS__)
#define CHECK(_nm, _fn, _d, ...) \
	CHECK_ENTRY(_nm, _fn, _d, false, false, __VA_ARGS__)
D
David Gibson 已提交
74 75 76 77 78 79 80 81 82

#ifdef __GNUC__
static inline void check_msg(struct check *c, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
#endif
static inline void check_msg(struct check *c, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);

S
Stephen Warren 已提交
83 84 85 86 87 88 89
	if ((c->warn && (quiet < 1))
	    || (c->error && (quiet < 2))) {
		fprintf(stderr, "%s (%s): ",
			(c->error) ? "ERROR" : "Warning", c->name);
		vfprintf(stderr, fmt, ap);
		fprintf(stderr, "\n");
	}
90
	va_end(ap);
D
David Gibson 已提交
91 92 93 94 95 96 97 98 99
}

#define FAIL(c, ...) \
	do { \
		TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
		(c)->status = FAILED; \
		check_msg((c), __VA_ARGS__); \
	} while (0)

100
static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
D
David Gibson 已提交
101 102 103 104
{
	struct node *child;

	TRACE(c, "%s", node->fullpath);
105 106
	if (c->fn)
		c->fn(c, dti, node);
D
David Gibson 已提交
107 108

	for_each_child(node, child)
109
		check_nodes_props(c, dti, child);
D
David Gibson 已提交
110 111
}

112
static bool run_check(struct check *c, struct dt_info *dti)
D
David Gibson 已提交
113
{
114
	struct node *dt = dti->dt;
115
	bool error = false;
D
David Gibson 已提交
116 117 118 119 120 121 122
	int i;

	assert(!c->inprogress);

	if (c->status != UNCHECKED)
		goto out;

123
	c->inprogress = true;
D
David Gibson 已提交
124 125 126

	for (i = 0; i < c->num_prereqs; i++) {
		struct check *prq = c->prereq[i];
127
		error = error || run_check(prq, dti);
D
David Gibson 已提交
128 129 130 131 132 133 134 135 136 137
		if (prq->status != PASSED) {
			c->status = PREREQ;
			check_msg(c, "Failed prerequisite '%s'",
				  c->prereq[i]->name);
		}
	}

	if (c->status != UNCHECKED)
		goto out;

138
	check_nodes_props(c, dti, dt);
D
David Gibson 已提交
139 140 141 142 143 144 145

	if (c->status == UNCHECKED)
		c->status = PASSED;

	TRACE(c, "\tCompleted, status %d", c->status);

out:
146
	c->inprogress = false;
S
Stephen Warren 已提交
147
	if ((c->status != PASSED) && (c->error))
148
		error = true;
D
David Gibson 已提交
149 150 151 152 153 154 155
	return error;
}

/*
 * Utility check functions
 */

S
Stephen Warren 已提交
156
/* A check which always fails, for testing purposes only */
157 158
static inline void check_always_fail(struct check *c, struct dt_info *dti,
				     struct node *node)
S
Stephen Warren 已提交
159 160 161
{
	FAIL(c, "always_fail check");
}
162
CHECK(always_fail, check_always_fail, NULL);
S
Stephen Warren 已提交
163

164
static void check_is_string(struct check *c, struct dt_info *dti,
D
David Gibson 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177
			    struct node *node)
{
	struct property *prop;
	char *propname = c->data;

	prop = get_property(node, propname);
	if (!prop)
		return; /* Not present, assumed ok */

	if (!data_is_one_string(prop->val))
		FAIL(c, "\"%s\" property in %s is not a string",
		     propname, node->fullpath);
}
S
Stephen Warren 已提交
178
#define WARNING_IF_NOT_STRING(nm, propname) \
179
	WARNING(nm, check_is_string, (propname))
S
Stephen Warren 已提交
180
#define ERROR_IF_NOT_STRING(nm, propname) \
181
	ERROR(nm, check_is_string, (propname))
D
David Gibson 已提交
182

183
static void check_is_cell(struct check *c, struct dt_info *dti,
D
David Gibson 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
			  struct node *node)
{
	struct property *prop;
	char *propname = c->data;

	prop = get_property(node, propname);
	if (!prop)
		return; /* Not present, assumed ok */

	if (prop->val.len != sizeof(cell_t))
		FAIL(c, "\"%s\" property in %s is not a single cell",
		     propname, node->fullpath);
}
S
Stephen Warren 已提交
197
#define WARNING_IF_NOT_CELL(nm, propname) \
198
	WARNING(nm, check_is_cell, (propname))
S
Stephen Warren 已提交
199
#define ERROR_IF_NOT_CELL(nm, propname) \
200
	ERROR(nm, check_is_cell, (propname))
D
David Gibson 已提交
201 202 203 204 205

/*
 * Structural check functions
 */

206
static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
D
David Gibson 已提交
207 208 209 210 211 212 213 214 215 216 217 218
				       struct node *node)
{
	struct node *child, *child2;

	for_each_child(node, child)
		for (child2 = child->next_sibling;
		     child2;
		     child2 = child2->next_sibling)
			if (streq(child->name, child2->name))
				FAIL(c, "Duplicate node name %s",
				     child->fullpath);
}
219
ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
D
David Gibson 已提交
220

221
static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
D
David Gibson 已提交
222 223 224 225
					   struct node *node)
{
	struct property *prop, *prop2;

S
Stephen Warren 已提交
226 227 228 229
	for_each_property(node, prop) {
		for (prop2 = prop->next; prop2; prop2 = prop2->next) {
			if (prop2->deleted)
				continue;
D
David Gibson 已提交
230 231 232
			if (streq(prop->name, prop2->name))
				FAIL(c, "Duplicate property name %s in %s",
				     prop->name, node->fullpath);
S
Stephen Warren 已提交
233 234
		}
	}
D
David Gibson 已提交
235
}
236
ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
D
David Gibson 已提交
237

238 239 240 241 242
#define LOWERCASE	"abcdefghijklmnopqrstuvwxyz"
#define UPPERCASE	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define DIGITS		"0123456789"
#define PROPNODECHARS	LOWERCASE UPPERCASE DIGITS ",._+*#?-"

243
static void check_node_name_chars(struct check *c, struct dt_info *dti,
244 245 246 247 248 249 250 251
				  struct node *node)
{
	int n = strspn(node->name, c->data);

	if (n < strlen(node->name))
		FAIL(c, "Bad character '%c' in node %s",
		     node->name[n], node->fullpath);
}
252
ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
253

254
static void check_node_name_format(struct check *c, struct dt_info *dti,
255 256 257 258 259 260
				   struct node *node)
{
	if (strchr(get_unitname(node), '@'))
		FAIL(c, "Node %s has multiple '@' characters in name",
		     node->fullpath);
}
261
ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
262

263 264
static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
				      struct node *node)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
{
	const char *unitname = get_unitname(node);
	struct property *prop = get_property(node, "reg");

	if (!prop) {
		prop = get_property(node, "ranges");
		if (prop && !prop->val.len)
			prop = NULL;
	}

	if (prop) {
		if (!unitname[0])
			FAIL(c, "Node %s has a reg or ranges property, but no unit name",
			    node->fullpath);
	} else {
		if (unitname[0])
			FAIL(c, "Node %s has a unit name, but no reg property",
			    node->fullpath);
	}
}
285
WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
286

287 288
static void check_property_name_chars(struct check *c, struct dt_info *dti,
				      struct node *node)
289
{
290 291 292 293
	struct property *prop;

	for_each_property(node, prop) {
		int n = strspn(prop->name, c->data);
294

295 296 297 298
		if (n < strlen(prop->name))
			FAIL(c, "Bad character '%c' in property name \"%s\", node %s",
			     prop->name[n], prop->name, node->fullpath);
	}
299
}
300
ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
301

302 303 304 305 306 307 308
#define DESCLABEL_FMT	"%s%s%s%s%s"
#define DESCLABEL_ARGS(node,prop,mark)		\
	((mark) ? "value of " : ""),		\
	((prop) ? "'" : ""), \
	((prop) ? (prop)->name : ""), \
	((prop) ? "' in " : ""), (node)->fullpath

309
static void check_duplicate_label(struct check *c, struct dt_info *dti,
310 311 312
				  const char *label, struct node *node,
				  struct property *prop, struct marker *mark)
{
313
	struct node *dt = dti->dt;
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	struct node *othernode = NULL;
	struct property *otherprop = NULL;
	struct marker *othermark = NULL;

	othernode = get_node_by_label(dt, label);

	if (!othernode)
		otherprop = get_property_by_label(dt, label, &othernode);
	if (!othernode)
		othermark = get_marker_label(dt, label, &othernode,
					       &otherprop);

	if (!othernode)
		return;

	if ((othernode != node) || (otherprop != prop) || (othermark != mark))
		FAIL(c, "Duplicate label '%s' on " DESCLABEL_FMT
		     " and " DESCLABEL_FMT,
		     label, DESCLABEL_ARGS(node, prop, mark),
		     DESCLABEL_ARGS(othernode, otherprop, othermark));
}

336
static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
337 338 339
				       struct node *node)
{
	struct label *l;
340
	struct property *prop;
341 342

	for_each_label(node->labels, l)
343 344 345 346
		check_duplicate_label(c, dti, l->label, node, NULL, NULL);

	for_each_property(node, prop) {
		struct marker *m = prop->val.markers;
347

348 349
		for_each_label(prop->labels, l)
			check_duplicate_label(c, dti, l->label, node, prop, NULL);
350

351 352 353
		for_each_marker_of_type(m, LABEL)
			check_duplicate_label(c, dti, m->ref, node, prop, m);
	}
354
}
355
ERROR(duplicate_label, check_duplicate_label_node, NULL);
356

357 358
static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
				 struct node *node, const char *propname)
D
David Gibson 已提交
359
{
360 361
	struct node *root = dti->dt;
	struct property *prop;
362
	struct marker *m;
D
David Gibson 已提交
363 364
	cell_t phandle;

365 366 367
	prop = get_property(node, propname);
	if (!prop)
		return 0;
D
David Gibson 已提交
368 369

	if (prop->val.len != sizeof(cell_t)) {
370 371
		FAIL(c, "%s has bad length (%d) %s property",
		     node->fullpath, prop->val.len, prop->name);
372
		return 0;
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
	}

	m = prop->val.markers;
	for_each_marker_of_type(m, REF_PHANDLE) {
		assert(m->offset == 0);
		if (node != get_node_by_ref(root, m->ref))
			/* "Set this node's phandle equal to some
			 * other node's phandle".  That's nonsensical
			 * by construction. */ {
			FAIL(c, "%s in %s is a reference to another node",
			     prop->name, node->fullpath);
		}
		/* But setting this node's phandle equal to its own
		 * phandle is allowed - that means allocate a unique
		 * phandle for this node, even if it's not otherwise
		 * referenced.  The value will be filled in later, so
389 390
		 * we treat it as having no phandle data for now. */
		return 0;
D
David Gibson 已提交
391 392 393
	}

	phandle = propval_cell(prop);
394

D
David Gibson 已提交
395
	if ((phandle == 0) || (phandle == -1)) {
396 397
		FAIL(c, "%s has bad value (0x%x) in %s property",
		     node->fullpath, phandle, prop->name);
398
		return 0;
D
David Gibson 已提交
399 400
	}

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
	return phandle;
}

static void check_explicit_phandles(struct check *c, struct dt_info *dti,
				    struct node *node)
{
	struct node *root = dti->dt;
	struct node *other;
	cell_t phandle, linux_phandle;

	/* Nothing should have assigned phandles yet */
	assert(!node->phandle);

	phandle = check_phandle_prop(c, dti, node, "phandle");

	linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");

	if (!phandle && !linux_phandle)
		/* No valid phandles; nothing further to check */
		return;

	if (linux_phandle && phandle && (phandle != linux_phandle))
		FAIL(c, "%s has mismatching 'phandle' and 'linux,phandle'"
		     " properties", node->fullpath);

	if (linux_phandle && !phandle)
		phandle = linux_phandle;
428

D
David Gibson 已提交
429
	other = get_node_by_phandle(root, phandle);
430
	if (other && (other != node)) {
D
David Gibson 已提交
431 432 433 434 435 436 437
		FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)",
		     node->fullpath, phandle, other->fullpath);
		return;
	}

	node->phandle = phandle;
}
438
ERROR(explicit_phandles, check_explicit_phandles, NULL);
D
David Gibson 已提交
439

440
static void check_name_properties(struct check *c, struct dt_info *dti,
D
David Gibson 已提交
441 442
				  struct node *node)
{
443 444 445 446 447 448 449
	struct property **pp, *prop = NULL;

	for (pp = &node->proplist; *pp; pp = &((*pp)->next))
		if (streq((*pp)->name, "name")) {
			prop = *pp;
			break;
		}
D
David Gibson 已提交
450 451 452 453 454

	if (!prop)
		return; /* No name property, that's fine */

	if ((prop->val.len != node->basenamelen+1)
455
	    || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
D
David Gibson 已提交
456 457
		FAIL(c, "\"name\" property in %s is incorrect (\"%s\" instead"
		     " of base node name)", node->fullpath, prop->val.val);
458 459 460 461 462 463 464 465
	} else {
		/* The name property is correct, and therefore redundant.
		 * Delete it */
		*pp = prop->next;
		free(prop->name);
		data_free(prop->val);
		free(prop);
	}
D
David Gibson 已提交
466
}
S
Stephen Warren 已提交
467
ERROR_IF_NOT_STRING(name_is_string, "name");
468
ERROR(name_properties, check_name_properties, NULL, &name_is_string);
D
David Gibson 已提交
469 470 471 472 473

/*
 * Reference fixup functions
 */

474 475
static void fixup_phandle_references(struct check *c, struct dt_info *dti,
				     struct node *node)
D
David Gibson 已提交
476
{
477 478
	struct node *dt = dti->dt;
	struct property *prop;
479

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	for_each_property(node, prop) {
		struct marker *m = prop->val.markers;
		struct node *refnode;
		cell_t phandle;

		for_each_marker_of_type(m, REF_PHANDLE) {
			assert(m->offset + sizeof(cell_t) <= prop->val.len);

			refnode = get_node_by_ref(dt, m->ref);
			if (! refnode) {
				if (!(dti->dtsflags & DTSF_PLUGIN))
					FAIL(c, "Reference to non-existent node or "
							"label \"%s\"\n", m->ref);
				else /* mark the entry as unresolved */
					*((cell_t *)(prop->val.val + m->offset)) =
						cpu_to_fdt32(0xffffffff);
				continue;
			}
498

499 500
			phandle = get_node_phandle(dt, refnode);
			*((cell_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
501 502
		}
	}
D
David Gibson 已提交
503
}
504
ERROR(phandle_references, fixup_phandle_references, NULL,
D
David Gibson 已提交
505 506
      &duplicate_node_names, &explicit_phandles);

507 508
static void fixup_path_references(struct check *c, struct dt_info *dti,
				  struct node *node)
D
David Gibson 已提交
509
{
510 511 512 513 514 515 516 517 518 519
	struct node *dt = dti->dt;
	struct property *prop;

	for_each_property(node, prop) {
		struct marker *m = prop->val.markers;
		struct node *refnode;
		char *path;

		for_each_marker_of_type(m, REF_PATH) {
			assert(m->offset <= prop->val.len);
D
David Gibson 已提交
520

521 522 523 524 525 526 527 528 529 530 531
			refnode = get_node_by_ref(dt, m->ref);
			if (!refnode) {
				FAIL(c, "Reference to non-existent node or label \"%s\"\n",
				     m->ref);
				continue;
			}

			path = refnode->fullpath;
			prop->val = data_insert_at_marker(prop->val, m, path,
							  strlen(path) + 1);
		}
D
David Gibson 已提交
532 533
	}
}
534
ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
D
David Gibson 已提交
535 536 537 538

/*
 * Semantic checks
 */
S
Stephen Warren 已提交
539 540 541
WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
D
David Gibson 已提交
542

S
Stephen Warren 已提交
543 544 545
WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
WARNING_IF_NOT_STRING(model_is_string, "model");
WARNING_IF_NOT_STRING(status_is_string, "status");
D
David Gibson 已提交
546

547
static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
D
David Gibson 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
				  struct node *node)
{
	struct property *prop;

	node->addr_cells = -1;
	node->size_cells = -1;

	prop = get_property(node, "#address-cells");
	if (prop)
		node->addr_cells = propval_cell(prop);

	prop = get_property(node, "#size-cells");
	if (prop)
		node->size_cells = propval_cell(prop);
}
563
WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
S
Stephen Warren 已提交
564
	&address_cells_is_cell, &size_cells_is_cell);
D
David Gibson 已提交
565 566 567 568 569 570

#define node_addr_cells(n) \
	(((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
#define node_size_cells(n) \
	(((n)->size_cells == -1) ? 1 : (n)->size_cells)

571
static void check_reg_format(struct check *c, struct dt_info *dti,
D
David Gibson 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
			     struct node *node)
{
	struct property *prop;
	int addr_cells, size_cells, entrylen;

	prop = get_property(node, "reg");
	if (!prop)
		return; /* No "reg", that's fine */

	if (!node->parent) {
		FAIL(c, "Root node has a \"reg\" property");
		return;
	}

	if (prop->val.len == 0)
		FAIL(c, "\"reg\" property in %s is empty", node->fullpath);

	addr_cells = node_addr_cells(node->parent);
	size_cells = node_size_cells(node->parent);
	entrylen = (addr_cells + size_cells) * sizeof(cell_t);

593
	if (!entrylen || (prop->val.len % entrylen) != 0)
D
David Gibson 已提交
594 595 596 597
		FAIL(c, "\"reg\" property in %s has invalid length (%d bytes) "
		     "(#address-cells == %d, #size-cells == %d)",
		     node->fullpath, prop->val.len, addr_cells, size_cells);
}
598
WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
D
David Gibson 已提交
599

600
static void check_ranges_format(struct check *c, struct dt_info *dti,
D
David Gibson 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
				struct node *node)
{
	struct property *prop;
	int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;

	prop = get_property(node, "ranges");
	if (!prop)
		return;

	if (!node->parent) {
		FAIL(c, "Root node has a \"ranges\" property");
		return;
	}

	p_addr_cells = node_addr_cells(node->parent);
	p_size_cells = node_size_cells(node->parent);
	c_addr_cells = node_addr_cells(node);
	c_size_cells = node_size_cells(node);
	entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);

	if (prop->val.len == 0) {
		if (p_addr_cells != c_addr_cells)
			FAIL(c, "%s has empty \"ranges\" property but its "
			     "#address-cells (%d) differs from %s (%d)",
			     node->fullpath, c_addr_cells, node->parent->fullpath,
			     p_addr_cells);
		if (p_size_cells != c_size_cells)
			FAIL(c, "%s has empty \"ranges\" property but its "
			     "#size-cells (%d) differs from %s (%d)",
			     node->fullpath, c_size_cells, node->parent->fullpath,
			     p_size_cells);
	} else if ((prop->val.len % entrylen) != 0) {
		FAIL(c, "\"ranges\" property in %s has invalid length (%d bytes) "
		     "(parent #address-cells == %d, child #address-cells == %d, "
		     "#size-cells == %d)", node->fullpath, prop->val.len,
		     p_addr_cells, c_addr_cells, c_size_cells);
	}
}
639
WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
D
David Gibson 已提交
640 641 642 643

/*
 * Style checks
 */
644
static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
D
David Gibson 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657
					  struct node *node)
{
	struct property *reg, *ranges;

	if (!node->parent)
		return; /* Ignore root node */

	reg = get_property(node, "reg");
	ranges = get_property(node, "ranges");

	if (!reg && !ranges)
		return;

658
	if (node->parent->addr_cells == -1)
D
David Gibson 已提交
659 660 661
		FAIL(c, "Relying on default #address-cells value for %s",
		     node->fullpath);

662
	if (node->parent->size_cells == -1)
D
David Gibson 已提交
663 664 665
		FAIL(c, "Relying on default #size-cells value for %s",
		     node->fullpath);
}
666 667
WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
	&addr_size_cells);
D
David Gibson 已提交
668 669

static void check_obsolete_chosen_interrupt_controller(struct check *c,
670 671
						       struct dt_info *dti,
						       struct node *node)
D
David Gibson 已提交
672
{
673
	struct node *dt = dti->dt;
D
David Gibson 已提交
674 675 676
	struct node *chosen;
	struct property *prop;

677 678 679 680
	if (node != dt)
		return;


D
David Gibson 已提交
681 682 683 684 685 686 687 688 689
	chosen = get_node_by_path(dt, "/chosen");
	if (!chosen)
		return;

	prop = get_property(chosen, "interrupt-controller");
	if (prop)
		FAIL(c, "/chosen has obsolete \"interrupt-controller\" "
		     "property");
}
690 691
WARNING(obsolete_chosen_interrupt_controller,
	check_obsolete_chosen_interrupt_controller, NULL);
D
David Gibson 已提交
692 693 694

static struct check *check_table[] = {
	&duplicate_node_names, &duplicate_property_names,
695
	&node_name_chars, &node_name_format, &property_name_chars,
D
David Gibson 已提交
696
	&name_is_string, &name_properties,
697 698 699

	&duplicate_label,

D
David Gibson 已提交
700 701 702 703 704 705 706 707
	&explicit_phandles,
	&phandle_references, &path_references,

	&address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
	&device_type_is_string, &model_is_string, &status_is_string,

	&addr_size_cells, &reg_format, &ranges_format,

708 709
	&unit_address_vs_reg,

D
David Gibson 已提交
710 711
	&avoid_default_addr_size,
	&obsolete_chosen_interrupt_controller,
S
Stephen Warren 已提交
712 713

	&always_fail,
D
David Gibson 已提交
714 715
};

S
Stephen Warren 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
static void enable_warning_error(struct check *c, bool warn, bool error)
{
	int i;

	/* Raising level, also raise it for prereqs */
	if ((warn && !c->warn) || (error && !c->error))
		for (i = 0; i < c->num_prereqs; i++)
			enable_warning_error(c->prereq[i], warn, error);

	c->warn = c->warn || warn;
	c->error = c->error || error;
}

static void disable_warning_error(struct check *c, bool warn, bool error)
{
	int i;

	/* Lowering level, also lower it for things this is the prereq
	 * for */
	if ((warn && c->warn) || (error && c->error)) {
		for (i = 0; i < ARRAY_SIZE(check_table); i++) {
			struct check *cc = check_table[i];
			int j;

			for (j = 0; j < cc->num_prereqs; j++)
				if (cc->prereq[j] == c)
					disable_warning_error(cc, warn, error);
		}
	}

	c->warn = c->warn && !warn;
	c->error = c->error && !error;
}

750
void parse_checks_option(bool warn, bool error, const char *arg)
S
Stephen Warren 已提交
751 752
{
	int i;
753
	const char *name = arg;
S
Stephen Warren 已提交
754 755
	bool enable = true;

756 757 758
	if ((strncmp(arg, "no-", 3) == 0)
	    || (strncmp(arg, "no_", 3) == 0)) {
		name = arg + 3;
S
Stephen Warren 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
		enable = false;
	}

	for (i = 0; i < ARRAY_SIZE(check_table); i++) {
		struct check *c = check_table[i];

		if (streq(c->name, name)) {
			if (enable)
				enable_warning_error(c, warn, error);
			else
				disable_warning_error(c, warn, error);
			return;
		}
	}

	die("Unrecognized check name \"%s\"\n", name);
}

777
void process_checks(bool force, struct dt_info *dti)
D
David Gibson 已提交
778 779 780 781 782 783 784
{
	int i;
	int error = 0;

	for (i = 0; i < ARRAY_SIZE(check_table); i++) {
		struct check *c = check_table[i];

S
Stephen Warren 已提交
785
		if (c->warn || c->error)
786
			error = error || run_check(c, dti);
D
David Gibson 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799
	}

	if (error) {
		if (!force) {
			fprintf(stderr, "ERROR: Input tree has errors, aborting "
				"(use -f to force output)\n");
			exit(2);
		} else if (quiet < 3) {
			fprintf(stderr, "Warning: Input tree has errors, "
				"output forced\n");
		}
	}
}