Linux Audio

Check our new training course

Loading...
v3.5.6
 
  1/*
  2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2007.
  3 *
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License as
  7 * published by the Free Software Foundation; either version 2 of the
  8 * License, or (at your option) any later version.
  9 *
 10 *  This program is distributed in the hope that it will be useful,
 11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13 *  General Public License for more details.
 14 *
 15 *  You should have received a copy of the GNU General Public License
 16 *  along with this program; if not, write to the Free Software
 17 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 18 *                                                                   USA
 19 */
 20
 21#include "dtc.h"
 
 22
 23#ifdef TRACE_CHECKS
 24#define TRACE(c, ...) \
 25	do { \
 26		fprintf(stderr, "=== %s: ", (c)->name); \
 27		fprintf(stderr, __VA_ARGS__); \
 28		fprintf(stderr, "\n"); \
 29	} while (0)
 30#else
 31#define TRACE(c, fmt, ...)	do { } while (0)
 32#endif
 33
 34enum checklevel {
 35	IGNORE = 0,
 36	WARN = 1,
 37	ERROR = 2,
 38};
 39
 40enum checkstatus {
 41	UNCHECKED = 0,
 42	PREREQ,
 43	PASSED,
 44	FAILED,
 45};
 46
 47struct check;
 48
 49typedef void (*tree_check_fn)(struct check *c, struct node *dt);
 50typedef void (*node_check_fn)(struct check *c, struct node *dt, struct node *node);
 51typedef void (*prop_check_fn)(struct check *c, struct node *dt,
 52			      struct node *node, struct property *prop);
 53
 54struct check {
 55	const char *name;
 56	tree_check_fn tree_fn;
 57	node_check_fn node_fn;
 58	prop_check_fn prop_fn;
 59	void *data;
 60	enum checklevel level;
 61	enum checkstatus status;
 62	int inprogress;
 63	int num_prereqs;
 64	struct check **prereq;
 65};
 66
 67#define CHECK(nm, tfn, nfn, pfn, d, lvl, ...) \
 68	static struct check *nm##_prereqs[] = { __VA_ARGS__ }; \
 69	static struct check nm = { \
 70		.name = #nm, \
 71		.tree_fn = (tfn), \
 72		.node_fn = (nfn), \
 73		.prop_fn = (pfn), \
 74		.data = (d), \
 75		.level = (lvl), \
 76		.status = UNCHECKED, \
 77		.num_prereqs = ARRAY_SIZE(nm##_prereqs), \
 78		.prereq = nm##_prereqs, \
 79	};
 80
 81#define TREE_CHECK(nm, d, lvl, ...) \
 82	CHECK(nm, check_##nm, NULL, NULL, d, lvl, __VA_ARGS__)
 83#define NODE_CHECK(nm, d, lvl, ...) \
 84	CHECK(nm, NULL, check_##nm, NULL, d, lvl, __VA_ARGS__)
 85#define PROP_CHECK(nm, d, lvl, ...) \
 86	CHECK(nm, NULL, NULL, check_##nm, d, lvl, __VA_ARGS__)
 87#define BATCH_CHECK(nm, lvl, ...) \
 88	CHECK(nm, NULL, NULL, NULL, NULL, lvl, __VA_ARGS__)
 89
 90#ifdef __GNUC__
 91static inline void check_msg(struct check *c, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
 92#endif
 93static inline void check_msg(struct check *c, const char *fmt, ...)
 94{
 95	va_list ap;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 96	va_start(ap, fmt);
 
 
 97
 98	if ((c->level < WARN) || (c->level <= quiet))
 99		return; /* Suppress message */
100
101	fprintf(stderr, "%s (%s): ",
102		(c->level == ERROR) ? "ERROR" : "Warning", c->name);
103	vfprintf(stderr, fmt, ap);
104	fprintf(stderr, "\n");
 
 
 
 
 
 
 
 
 
105}
106
107#define FAIL(c, ...) \
108	do { \
109		TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
110		(c)->status = FAILED; \
111		check_msg((c), __VA_ARGS__); \
 
 
 
 
 
 
 
112	} while (0)
113
114static void check_nodes_props(struct check *c, struct node *dt, struct node *node)
 
115{
116	struct node *child;
117	struct property *prop;
118
119	TRACE(c, "%s", node->fullpath);
120	if (c->node_fn)
121		c->node_fn(c, dt, node);
122
123	if (c->prop_fn)
124		for_each_property(node, prop) {
125			TRACE(c, "%s\t'%s'", node->fullpath, prop->name);
126			c->prop_fn(c, dt, node, prop);
127		}
128
129	for_each_child(node, child)
130		check_nodes_props(c, dt, child);
131}
132
133static int run_check(struct check *c, struct node *dt)
134{
135	int error = 0;
 
 
 
 
 
 
 
 
 
136	int i;
137
138	assert(!c->inprogress);
139
140	if (c->status != UNCHECKED)
141		goto out;
142
143	c->inprogress = 1;
144
145	for (i = 0; i < c->num_prereqs; i++) {
146		struct check *prq = c->prereq[i];
147		error |= run_check(prq, dt);
148		if (prq->status != PASSED) {
149			c->status = PREREQ;
150			check_msg(c, "Failed prerequisite '%s'",
151				  c->prereq[i]->name);
152		}
153	}
154
155	if (c->status != UNCHECKED)
156		goto out;
157
158	if (c->node_fn || c->prop_fn)
159		check_nodes_props(c, dt, dt);
160
161	if (c->tree_fn)
162		c->tree_fn(c, dt);
163	if (c->status == UNCHECKED)
164		c->status = PASSED;
165
166	TRACE(c, "\tCompleted, status %d", c->status);
167
168out:
169	c->inprogress = 0;
170	if ((c->status != PASSED) && (c->level == ERROR))
171		error = 1;
172	return error;
173}
174
175/*
176 * Utility check functions
177 */
178
179static void check_is_string(struct check *c, struct node *root,
 
 
 
 
 
 
 
 
180			    struct node *node)
181{
182	struct property *prop;
183	char *propname = c->data;
184
185	prop = get_property(node, propname);
186	if (!prop)
187		return; /* Not present, assumed ok */
188
189	if (!data_is_one_string(prop->val))
190		FAIL(c, "\"%s\" property in %s is not a string",
191		     propname, node->fullpath);
192}
193#define CHECK_IS_STRING(nm, propname, lvl) \
194	CHECK(nm, NULL, check_is_string, NULL, (propname), (lvl))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
196static void check_is_cell(struct check *c, struct node *root,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197			  struct node *node)
198{
199	struct property *prop;
200	char *propname = c->data;
201
202	prop = get_property(node, propname);
203	if (!prop)
204		return; /* Not present, assumed ok */
205
206	if (prop->val.len != sizeof(cell_t))
207		FAIL(c, "\"%s\" property in %s is not a single cell",
208		     propname, node->fullpath);
209}
210#define CHECK_IS_CELL(nm, propname, lvl) \
211	CHECK(nm, NULL, check_is_cell, NULL, (propname), (lvl))
 
 
212
213/*
214 * Structural check functions
215 */
216
217static void check_duplicate_node_names(struct check *c, struct node *dt,
218				       struct node *node)
219{
220	struct node *child, *child2;
221
222	for_each_child(node, child)
223		for (child2 = child->next_sibling;
224		     child2;
225		     child2 = child2->next_sibling)
226			if (streq(child->name, child2->name))
227				FAIL(c, "Duplicate node name %s",
228				     child->fullpath);
229}
230NODE_CHECK(duplicate_node_names, NULL, ERROR);
231
232static void check_duplicate_property_names(struct check *c, struct node *dt,
233					   struct node *node)
234{
235	struct property *prop, *prop2;
236
237	for_each_property(node, prop)
238		for (prop2 = prop->next; prop2; prop2 = prop2->next)
 
 
239			if (streq(prop->name, prop2->name))
240				FAIL(c, "Duplicate property name %s in %s",
241				     prop->name, node->fullpath);
 
242}
243NODE_CHECK(duplicate_property_names, NULL, ERROR);
244
245#define LOWERCASE	"abcdefghijklmnopqrstuvwxyz"
246#define UPPERCASE	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
247#define DIGITS		"0123456789"
248#define PROPNODECHARS	LOWERCASE UPPERCASE DIGITS ",._+*#?-"
 
 
249
250static void check_node_name_chars(struct check *c, struct node *dt,
251				  struct node *node)
252{
253	int n = strspn(node->name, c->data);
254
255	if (n < strlen(node->name))
256		FAIL(c, "Bad character '%c' in node %s",
257		     node->name[n], node->fullpath);
258}
259NODE_CHECK(node_name_chars, PROPNODECHARS "@", ERROR);
260
261static void check_node_name_format(struct check *c, struct node *dt,
 
 
 
 
 
 
 
 
 
 
 
262				   struct node *node)
263{
264	if (strchr(get_unitname(node), '@'))
265		FAIL(c, "Node %s has multiple '@' characters in name",
266		     node->fullpath);
267}
268NODE_CHECK(node_name_format, NULL, ERROR, &node_name_chars);
269
270static void check_property_name_chars(struct check *c, struct node *dt,
271				      struct node *node, struct property *prop)
 
272{
273	int n = strspn(prop->name, c->data);
 
274
275	if (n < strlen(prop->name))
276		FAIL(c, "Bad character '%c' in property name \"%s\", node %s",
277		     prop->name[n], prop->name, node->fullpath);
278}
279PROP_CHECK(property_name_chars, PROPNODECHARS, ERROR);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
281#define DESCLABEL_FMT	"%s%s%s%s%s"
282#define DESCLABEL_ARGS(node,prop,mark)		\
283	((mark) ? "value of " : ""),		\
284	((prop) ? "'" : ""), \
285	((prop) ? (prop)->name : ""), \
286	((prop) ? "' in " : ""), (node)->fullpath
287
288static void check_duplicate_label(struct check *c, struct node *dt,
289				  const char *label, struct node *node,
290				  struct property *prop, struct marker *mark)
291{
 
292	struct node *othernode = NULL;
293	struct property *otherprop = NULL;
294	struct marker *othermark = NULL;
295
296	othernode = get_node_by_label(dt, label);
297
298	if (!othernode)
299		otherprop = get_property_by_label(dt, label, &othernode);
300	if (!othernode)
301		othermark = get_marker_label(dt, label, &othernode,
302					       &otherprop);
303
304	if (!othernode)
305		return;
306
307	if ((othernode != node) || (otherprop != prop) || (othermark != mark))
308		FAIL(c, "Duplicate label '%s' on " DESCLABEL_FMT
309		     " and " DESCLABEL_FMT,
310		     label, DESCLABEL_ARGS(node, prop, mark),
311		     DESCLABEL_ARGS(othernode, otherprop, othermark));
312}
313
314static void check_duplicate_label_node(struct check *c, struct node *dt,
315				       struct node *node)
316{
317	struct label *l;
 
318
319	for_each_label(node->labels, l)
320		check_duplicate_label(c, dt, l->label, node, NULL, NULL);
321}
322static void check_duplicate_label_prop(struct check *c, struct node *dt,
323				       struct node *node, struct property *prop)
324{
325	struct marker *m = prop->val.markers;
326	struct label *l;
327
328	for_each_label(prop->labels, l)
329		check_duplicate_label(c, dt, l->label, node, prop, NULL);
330
331	for_each_marker_of_type(m, LABEL)
332		check_duplicate_label(c, dt, m->ref, node, prop, m);
 
 
 
 
333}
334CHECK(duplicate_label, NULL, check_duplicate_label_node,
335      check_duplicate_label_prop, NULL, ERROR);
336
337static void check_explicit_phandles(struct check *c, struct node *root,
338				    struct node *node, struct property *prop)
339{
 
 
340	struct marker *m;
341	struct node *other;
342	cell_t phandle;
343
344	if (!streq(prop->name, "phandle")
345	    && !streq(prop->name, "linux,phandle"))
346		return;
347
348	if (prop->val.len != sizeof(cell_t)) {
349		FAIL(c, "%s has bad length (%d) %s property",
350		     node->fullpath, prop->val.len, prop->name);
351		return;
352	}
353
354	m = prop->val.markers;
355	for_each_marker_of_type(m, REF_PHANDLE) {
356		assert(m->offset == 0);
357		if (node != get_node_by_ref(root, m->ref))
358			/* "Set this node's phandle equal to some
359			 * other node's phandle".  That's nonsensical
360			 * by construction. */ {
361			FAIL(c, "%s in %s is a reference to another node",
362			     prop->name, node->fullpath);
363			return;
364		}
365		/* But setting this node's phandle equal to its own
366		 * phandle is allowed - that means allocate a unique
367		 * phandle for this node, even if it's not otherwise
368		 * referenced.  The value will be filled in later, so
369		 * no further checking for now. */
370		return;
371	}
372
373	phandle = propval_cell(prop);
374
375	if ((phandle == 0) || (phandle == -1)) {
376		FAIL(c, "%s has bad value (0x%x) in %s property",
377		     node->fullpath, phandle, prop->name);
378		return;
379	}
380
381	if (node->phandle && (node->phandle != phandle))
382		FAIL(c, "%s has %s property which replaces existing phandle information",
383		     node->fullpath, prop->name);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
385	other = get_node_by_phandle(root, phandle);
386	if (other && (other != node)) {
387		FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)",
388		     node->fullpath, phandle, other->fullpath);
389		return;
390	}
391
392	node->phandle = phandle;
393}
394PROP_CHECK(explicit_phandles, NULL, ERROR);
395
396static void check_name_properties(struct check *c, struct node *root,
397				  struct node *node)
398{
399	struct property **pp, *prop = NULL;
400
401	for (pp = &node->proplist; *pp; pp = &((*pp)->next))
402		if (streq((*pp)->name, "name")) {
403			prop = *pp;
404			break;
405		}
406
407	if (!prop)
408		return; /* No name property, that's fine */
409
410	if ((prop->val.len != node->basenamelen+1)
411	    || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
412		FAIL(c, "\"name\" property in %s is incorrect (\"%s\" instead"
413		     " of base node name)", node->fullpath, prop->val.val);
414	} else {
415		/* The name property is correct, and therefore redundant.
416		 * Delete it */
417		*pp = prop->next;
418		free(prop->name);
419		data_free(prop->val);
420		free(prop);
421	}
422}
423CHECK_IS_STRING(name_is_string, "name", ERROR);
424NODE_CHECK(name_properties, NULL, ERROR, &name_is_string);
425
426/*
427 * Reference fixup functions
428 */
429
430static void fixup_phandle_references(struct check *c, struct node *dt,
431				     struct node *node, struct property *prop)
432{
433	struct marker *m = prop->val.markers;
434	struct node *refnode;
435	cell_t phandle;
436
437	for_each_marker_of_type(m, REF_PHANDLE) {
438		assert(m->offset + sizeof(cell_t) <= prop->val.len);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439
440		refnode = get_node_by_ref(dt, m->ref);
441		if (! refnode) {
442			FAIL(c, "Reference to non-existent node or label \"%s\"\n",
443			     m->ref);
444			continue;
445		}
446
447		phandle = get_node_phandle(dt, refnode);
448		*((cell_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
449	}
450}
451CHECK(phandle_references, NULL, NULL, fixup_phandle_references, NULL, ERROR,
452      &duplicate_node_names, &explicit_phandles);
453
454static void fixup_path_references(struct check *c, struct node *dt,
455				  struct node *node, struct property *prop)
456{
457	struct marker *m = prop->val.markers;
458	struct node *refnode;
459	char *path;
460
461	for_each_marker_of_type(m, REF_PATH) {
462		assert(m->offset <= prop->val.len);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463
464		refnode = get_node_by_ref(dt, m->ref);
465		if (!refnode) {
466			FAIL(c, "Reference to non-existent node or label \"%s\"\n",
467			     m->ref);
468			continue;
469		}
470
471		path = refnode->fullpath;
472		prop->val = data_insert_at_marker(prop->val, m, path,
473						  strlen(path) + 1);
474	}
475}
476CHECK(path_references, NULL, NULL, fixup_path_references, NULL, ERROR,
477      &duplicate_node_names);
 
 
 
 
 
 
 
 
 
478
479/*
480 * Semantic checks
481 */
482CHECK_IS_CELL(address_cells_is_cell, "#address-cells", WARN);
483CHECK_IS_CELL(size_cells_is_cell, "#size-cells", WARN);
484CHECK_IS_CELL(interrupt_cells_is_cell, "#interrupt-cells", WARN);
485
486CHECK_IS_STRING(device_type_is_string, "device_type", WARN);
487CHECK_IS_STRING(model_is_string, "model", WARN);
488CHECK_IS_STRING(status_is_string, "status", WARN);
489
490static void fixup_addr_size_cells(struct check *c, struct node *dt,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
491				  struct node *node)
492{
493	struct property *prop;
494
495	node->addr_cells = -1;
496	node->size_cells = -1;
497
498	prop = get_property(node, "#address-cells");
499	if (prop)
500		node->addr_cells = propval_cell(prop);
501
502	prop = get_property(node, "#size-cells");
503	if (prop)
504		node->size_cells = propval_cell(prop);
505}
506CHECK(addr_size_cells, NULL, fixup_addr_size_cells, NULL, NULL, WARN,
507      &address_cells_is_cell, &size_cells_is_cell);
508
509#define node_addr_cells(n) \
510	(((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
511#define node_size_cells(n) \
512	(((n)->size_cells == -1) ? 1 : (n)->size_cells)
513
514static void check_reg_format(struct check *c, struct node *dt,
515			     struct node *node)
516{
517	struct property *prop;
518	int addr_cells, size_cells, entrylen;
519
520	prop = get_property(node, "reg");
521	if (!prop)
522		return; /* No "reg", that's fine */
523
524	if (!node->parent) {
525		FAIL(c, "Root node has a \"reg\" property");
526		return;
527	}
528
529	if (prop->val.len == 0)
530		FAIL(c, "\"reg\" property in %s is empty", node->fullpath);
531
532	addr_cells = node_addr_cells(node->parent);
533	size_cells = node_size_cells(node->parent);
534	entrylen = (addr_cells + size_cells) * sizeof(cell_t);
535
536	if ((prop->val.len % entrylen) != 0)
537		FAIL(c, "\"reg\" property in %s has invalid length (%d bytes) "
538		     "(#address-cells == %d, #size-cells == %d)",
539		     node->fullpath, prop->val.len, addr_cells, size_cells);
540}
541NODE_CHECK(reg_format, NULL, WARN, &addr_size_cells);
542
543static void check_ranges_format(struct check *c, struct node *dt,
544				struct node *node)
545{
546	struct property *prop;
547	int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
 
548
549	prop = get_property(node, "ranges");
550	if (!prop)
551		return;
552
553	if (!node->parent) {
554		FAIL(c, "Root node has a \"ranges\" property");
 
555		return;
556	}
557
558	p_addr_cells = node_addr_cells(node->parent);
559	p_size_cells = node_size_cells(node->parent);
560	c_addr_cells = node_addr_cells(node);
561	c_size_cells = node_size_cells(node);
562	entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
563
564	if (prop->val.len == 0) {
565		if (p_addr_cells != c_addr_cells)
566			FAIL(c, "%s has empty \"ranges\" property but its "
567			     "#address-cells (%d) differs from %s (%d)",
568			     node->fullpath, c_addr_cells, node->parent->fullpath,
569			     p_addr_cells);
570		if (p_size_cells != c_size_cells)
571			FAIL(c, "%s has empty \"ranges\" property but its "
572			     "#size-cells (%d) differs from %s (%d)",
573			     node->fullpath, c_size_cells, node->parent->fullpath,
574			     p_size_cells);
575	} else if ((prop->val.len % entrylen) != 0) {
576		FAIL(c, "\"ranges\" property in %s has invalid length (%d bytes) "
577		     "(parent #address-cells == %d, child #address-cells == %d, "
578		     "#size-cells == %d)", node->fullpath, prop->val.len,
579		     p_addr_cells, c_addr_cells, c_size_cells);
580	}
581}
582NODE_CHECK(ranges_format, NULL, WARN, &addr_size_cells);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583
584/*
585 * Style checks
586 */
587static void check_avoid_default_addr_size(struct check *c, struct node *dt,
588					  struct node *node)
589{
590	struct property *reg, *ranges;
591
592	if (!node->parent)
593		return; /* Ignore root node */
594
595	reg = get_property(node, "reg");
596	ranges = get_property(node, "ranges");
597
598	if (!reg && !ranges)
599		return;
600
601	if ((node->parent->addr_cells == -1))
602		FAIL(c, "Relying on default #address-cells value for %s",
603		     node->fullpath);
604
605	if ((node->parent->size_cells == -1))
606		FAIL(c, "Relying on default #size-cells value for %s",
607		     node->fullpath);
608}
609NODE_CHECK(avoid_default_addr_size, NULL, WARN, &addr_size_cells);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
610
611static void check_obsolete_chosen_interrupt_controller(struct check *c,
612						       struct node *dt)
 
613{
 
614	struct node *chosen;
615	struct property *prop;
616
 
 
 
 
617	chosen = get_node_by_path(dt, "/chosen");
618	if (!chosen)
619		return;
620
621	prop = get_property(chosen, "interrupt-controller");
622	if (prop)
623		FAIL(c, "/chosen has obsolete \"interrupt-controller\" "
624		     "property");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
625}
626TREE_CHECK(obsolete_chosen_interrupt_controller, NULL, WARN);
627
628static struct check *check_table[] = {
629	&duplicate_node_names, &duplicate_property_names,
630	&node_name_chars, &node_name_format, &property_name_chars,
631	&name_is_string, &name_properties,
632
633	&duplicate_label,
634
635	&explicit_phandles,
636	&phandle_references, &path_references,
 
637
638	&address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
639	&device_type_is_string, &model_is_string, &status_is_string,
 
640
641	&addr_size_cells, &reg_format, &ranges_format,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
642
643	&avoid_default_addr_size,
 
 
 
644	&obsolete_chosen_interrupt_controller,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
645};
646
647void process_checks(int force, struct boot_info *bi)
648{
649	struct node *dt = bi->dt;
650	int i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
651	int error = 0;
652
653	for (i = 0; i < ARRAY_SIZE(check_table); i++) {
654		struct check *c = check_table[i];
655
656		if (c->level != IGNORE)
657			error = error || run_check(c, dt);
658	}
659
660	if (error) {
661		if (!force) {
662			fprintf(stderr, "ERROR: Input tree has errors, aborting "
663				"(use -f to force output)\n");
664			exit(2);
665		} else if (quiet < 3) {
666			fprintf(stderr, "Warning: Input tree has errors, "
667				"output forced\n");
668		}
669	}
670}
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2007.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   4 */
   5
   6#include "dtc.h"
   7#include "srcpos.h"
   8
   9#ifdef TRACE_CHECKS
  10#define TRACE(c, ...) \
  11	do { \
  12		fprintf(stderr, "=== %s: ", (c)->name); \
  13		fprintf(stderr, __VA_ARGS__); \
  14		fprintf(stderr, "\n"); \
  15	} while (0)
  16#else
  17#define TRACE(c, fmt, ...)	do { } while (0)
  18#endif
  19
 
 
 
 
 
 
  20enum checkstatus {
  21	UNCHECKED = 0,
  22	PREREQ,
  23	PASSED,
  24	FAILED,
  25};
  26
  27struct check;
  28
  29typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
 
 
 
  30
  31struct check {
  32	const char *name;
  33	check_fn fn;
  34	const void *data;
  35	bool warn, error;
 
 
  36	enum checkstatus status;
  37	bool inprogress;
  38	int num_prereqs;
  39	struct check **prereq;
  40};
  41
  42#define CHECK_ENTRY(nm_, fn_, d_, w_, e_, ...)	       \
  43	static struct check *nm_##_prereqs[] = { __VA_ARGS__ }; \
  44	static struct check nm_ = { \
  45		.name = #nm_, \
  46		.fn = (fn_), \
  47		.data = (d_), \
  48		.warn = (w_), \
  49		.error = (e_), \
 
  50		.status = UNCHECKED, \
  51		.num_prereqs = ARRAY_SIZE(nm_##_prereqs), \
  52		.prereq = nm_##_prereqs, \
  53	};
  54#define WARNING(nm_, fn_, d_, ...) \
  55	CHECK_ENTRY(nm_, fn_, d_, true, false, __VA_ARGS__)
  56#define ERROR(nm_, fn_, d_, ...) \
  57	CHECK_ENTRY(nm_, fn_, d_, false, true, __VA_ARGS__)
  58#define CHECK(nm_, fn_, d_, ...) \
  59	CHECK_ENTRY(nm_, fn_, d_, false, false, __VA_ARGS__)
  60
  61static inline void  PRINTF(5, 6) check_msg(struct check *c, struct dt_info *dti,
  62					   struct node *node,
  63					   struct property *prop,
  64					   const char *fmt, ...)
 
 
 
  65{
  66	va_list ap;
  67	char *str = NULL;
  68	struct srcpos *pos = NULL;
  69	char *file_str;
  70
  71	if (!(c->warn && (quiet < 1)) && !(c->error && (quiet < 2)))
  72		return;
  73
  74	if (prop && prop->srcpos)
  75		pos = prop->srcpos;
  76	else if (node && node->srcpos)
  77		pos = node->srcpos;
  78
  79	if (pos) {
  80		file_str = srcpos_string(pos);
  81		xasprintf(&str, "%s", file_str);
  82		free(file_str);
  83	} else if (streq(dti->outname, "-")) {
  84		xasprintf(&str, "<stdout>");
  85	} else {
  86		xasprintf(&str, "%s", dti->outname);
  87	}
  88
  89	xasprintf_append(&str, ": %s (%s): ",
  90			(c->error) ? "ERROR" : "Warning", c->name);
  91
  92	if (node) {
  93		if (prop)
  94			xasprintf_append(&str, "%s:%s: ", node->fullpath, prop->name);
  95		else
  96			xasprintf_append(&str, "%s: ", node->fullpath);
  97	}
  98
  99	va_start(ap, fmt);
 100	xavsprintf_append(&str, fmt, ap);
 101	va_end(ap);
 102
 103	xasprintf_append(&str, "\n");
 
 104
 105	if (!prop && pos) {
 106		pos = node->srcpos;
 107		while (pos->next) {
 108			pos = pos->next;
 109
 110			file_str = srcpos_string(pos);
 111			xasprintf_append(&str, "  also defined at %s\n", file_str);
 112			free(file_str);
 113		}
 114	}
 115
 116	fputs(str, stderr);
 117	free(str);
 118}
 119
 120#define FAIL(c, dti, node, ...)						\
 121	do {								\
 122		TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__);	\
 123		(c)->status = FAILED;					\
 124		check_msg((c), dti, node, NULL, __VA_ARGS__);		\
 125	} while (0)
 126
 127#define FAIL_PROP(c, dti, node, prop, ...)				\
 128	do {								\
 129		TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__);	\
 130		(c)->status = FAILED;					\
 131		check_msg((c), dti, node, prop, __VA_ARGS__);		\
 132	} while (0)
 133
 134
 135static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
 136{
 137	struct node *child;
 
 138
 139	TRACE(c, "%s", node->fullpath);
 140	if (c->fn)
 141		c->fn(c, dti, node);
 
 
 
 
 
 
 142
 143	for_each_child(node, child)
 144		check_nodes_props(c, dti, child);
 145}
 146
 147static bool is_multiple_of(int multiple, int divisor)
 148{
 149	if (divisor == 0)
 150		return multiple == 0;
 151	else
 152		return (multiple % divisor) == 0;
 153}
 154
 155static bool run_check(struct check *c, struct dt_info *dti)
 156{
 157	struct node *dt = dti->dt;
 158	bool error = false;
 159	int i;
 160
 161	assert(!c->inprogress);
 162
 163	if (c->status != UNCHECKED)
 164		goto out;
 165
 166	c->inprogress = true;
 167
 168	for (i = 0; i < c->num_prereqs; i++) {
 169		struct check *prq = c->prereq[i];
 170		error = error || run_check(prq, dti);
 171		if (prq->status != PASSED) {
 172			c->status = PREREQ;
 173			check_msg(c, dti, NULL, NULL, "Failed prerequisite '%s'",
 174				  c->prereq[i]->name);
 175		}
 176	}
 177
 178	if (c->status != UNCHECKED)
 179		goto out;
 180
 181	check_nodes_props(c, dti, dt);
 
 182
 
 
 183	if (c->status == UNCHECKED)
 184		c->status = PASSED;
 185
 186	TRACE(c, "\tCompleted, status %d", c->status);
 187
 188out:
 189	c->inprogress = false;
 190	if ((c->status != PASSED) && (c->error))
 191		error = true;
 192	return error;
 193}
 194
 195/*
 196 * Utility check functions
 197 */
 198
 199/* A check which always fails, for testing purposes only */
 200static inline void check_always_fail(struct check *c, struct dt_info *dti,
 201				     struct node *node)
 202{
 203	FAIL(c, dti, node, "always_fail check");
 204}
 205CHECK(always_fail, check_always_fail, NULL);
 206
 207static void check_is_string(struct check *c, struct dt_info *dti,
 208			    struct node *node)
 209{
 210	struct property *prop;
 211	const char *propname = c->data;
 212
 213	prop = get_property(node, propname);
 214	if (!prop)
 215		return; /* Not present, assumed ok */
 216
 217	if (!data_is_one_string(prop->val))
 218		FAIL_PROP(c, dti, node, prop, "property is not a string");
 
 219}
 220#define WARNING_IF_NOT_STRING(nm, propname) \
 221	WARNING(nm, check_is_string, (propname))
 222#define ERROR_IF_NOT_STRING(nm, propname) \
 223	ERROR(nm, check_is_string, (propname))
 224
 225static void check_is_string_list(struct check *c, struct dt_info *dti,
 226				 struct node *node)
 227{
 228	int rem, l;
 229	struct property *prop;
 230	const char *propname = c->data;
 231	char *str;
 232
 233	prop = get_property(node, propname);
 234	if (!prop)
 235		return; /* Not present, assumed ok */
 236
 237	str = prop->val.val;
 238	rem = prop->val.len;
 239	while (rem > 0) {
 240		l = strnlen(str, rem);
 241		if (l == rem) {
 242			FAIL_PROP(c, dti, node, prop, "property is not a string list");
 243			break;
 244		}
 245		rem -= l + 1;
 246		str += l + 1;
 247	}
 248}
 249#define WARNING_IF_NOT_STRING_LIST(nm, propname) \
 250	WARNING(nm, check_is_string_list, (propname))
 251#define ERROR_IF_NOT_STRING_LIST(nm, propname) \
 252	ERROR(nm, check_is_string_list, (propname))
 253
 254static void check_is_cell(struct check *c, struct dt_info *dti,
 255			  struct node *node)
 256{
 257	struct property *prop;
 258	const char *propname = c->data;
 259
 260	prop = get_property(node, propname);
 261	if (!prop)
 262		return; /* Not present, assumed ok */
 263
 264	if (prop->val.len != sizeof(cell_t))
 265		FAIL_PROP(c, dti, node, prop, "property is not a single cell");
 
 266}
 267#define WARNING_IF_NOT_CELL(nm, propname) \
 268	WARNING(nm, check_is_cell, (propname))
 269#define ERROR_IF_NOT_CELL(nm, propname) \
 270	ERROR(nm, check_is_cell, (propname))
 271
 272/*
 273 * Structural check functions
 274 */
 275
 276static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
 277				       struct node *node)
 278{
 279	struct node *child, *child2;
 280
 281	for_each_child(node, child)
 282		for (child2 = child->next_sibling;
 283		     child2;
 284		     child2 = child2->next_sibling)
 285			if (streq(child->name, child2->name))
 286				FAIL(c, dti, child2, "Duplicate node name");
 
 287}
 288ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
 289
 290static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
 291					   struct node *node)
 292{
 293	struct property *prop, *prop2;
 294
 295	for_each_property(node, prop) {
 296		for (prop2 = prop->next; prop2; prop2 = prop2->next) {
 297			if (prop2->deleted)
 298				continue;
 299			if (streq(prop->name, prop2->name))
 300				FAIL_PROP(c, dti, node, prop, "Duplicate property name");
 301		}
 302	}
 303}
 304ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
 305
 306#define LOWERCASE	"abcdefghijklmnopqrstuvwxyz"
 307#define UPPERCASE	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 308#define DIGITS		"0123456789"
 309#define NODECHARS	LOWERCASE UPPERCASE DIGITS ",._+-@"
 310#define PROPCHARS	LOWERCASE UPPERCASE DIGITS ",._+*#?-"
 311#define PROPNODECHARSSTRICT	LOWERCASE UPPERCASE DIGITS ",-"
 312
 313static void check_node_name_chars(struct check *c, struct dt_info *dti,
 314				  struct node *node)
 315{
 316	size_t n = strspn(node->name, c->data);
 317
 318	if (n < strlen(node->name))
 319		FAIL(c, dti, node, "Bad character '%c' in node name",
 320		     node->name[n]);
 321}
 322ERROR(node_name_chars, check_node_name_chars, NODECHARS);
 323
 324static void check_node_name_chars_strict(struct check *c, struct dt_info *dti,
 325					 struct node *node)
 326{
 327	int n = strspn(node->name, c->data);
 328
 329	if (n < node->basenamelen)
 330		FAIL(c, dti, node, "Character '%c' not recommended in node name",
 331		     node->name[n]);
 332}
 333CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
 334
 335static void check_node_name_format(struct check *c, struct dt_info *dti,
 336				   struct node *node)
 337{
 338	if (strchr(get_unitname(node), '@'))
 339		FAIL(c, dti, node, "multiple '@' characters in node name");
 
 340}
 341ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
 342
 343static void check_node_name_vs_property_name(struct check *c,
 344					     struct dt_info *dti,
 345					     struct node *node)
 346{
 347	if (!node->parent)
 348		return;
 349
 350	if (get_property(node->parent, node->name)) {
 351		FAIL(c, dti, node, "node name and property name conflict");
 352	}
 353}
 354WARNING(node_name_vs_property_name, check_node_name_vs_property_name,
 355	NULL, &node_name_chars);
 356
 357static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
 358				      struct node *node)
 359{
 360	const char *unitname = get_unitname(node);
 361	struct property *prop = get_property(node, "reg");
 362
 363	if (get_subnode(node, "__overlay__")) {
 364		/* HACK: Overlay fragments are a special case */
 365		return;
 366	}
 367
 368	if (!prop) {
 369		prop = get_property(node, "ranges");
 370		if (prop && !prop->val.len)
 371			prop = NULL;
 372	}
 373
 374	if (prop) {
 375		if (!unitname[0])
 376			FAIL(c, dti, node, "node has a reg or ranges property, but no unit name");
 377	} else {
 378		if (unitname[0])
 379			FAIL(c, dti, node, "node has a unit name, but no reg or ranges property");
 380	}
 381}
 382WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
 383
 384static void check_property_name_chars(struct check *c, struct dt_info *dti,
 385				      struct node *node)
 386{
 387	struct property *prop;
 388
 389	for_each_property(node, prop) {
 390		size_t n = strspn(prop->name, c->data);
 391
 392		if (n < strlen(prop->name))
 393			FAIL_PROP(c, dti, node, prop, "Bad character '%c' in property name",
 394				  prop->name[n]);
 395	}
 396}
 397ERROR(property_name_chars, check_property_name_chars, PROPCHARS);
 398
 399static void check_property_name_chars_strict(struct check *c,
 400					     struct dt_info *dti,
 401					     struct node *node)
 402{
 403	struct property *prop;
 404
 405	for_each_property(node, prop) {
 406		const char *name = prop->name;
 407		size_t n = strspn(name, c->data);
 408
 409		if (n == strlen(prop->name))
 410			continue;
 411
 412		/* Certain names are whitelisted */
 413		if (streq(name, "device_type"))
 414			continue;
 415
 416		/*
 417		 * # is only allowed at the beginning of property names not counting
 418		 * the vendor prefix.
 419		 */
 420		if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) {
 421			name += n + 1;
 422			n = strspn(name, c->data);
 423		}
 424		if (n < strlen(name))
 425			FAIL_PROP(c, dti, node, prop, "Character '%c' not recommended in property name",
 426				  name[n]);
 427	}
 428}
 429CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
 430
 431#define DESCLABEL_FMT	"%s%s%s%s%s"
 432#define DESCLABEL_ARGS(node,prop,mark)		\
 433	((mark) ? "value of " : ""),		\
 434	((prop) ? "'" : ""), \
 435	((prop) ? (prop)->name : ""), \
 436	((prop) ? "' in " : ""), (node)->fullpath
 437
 438static void check_duplicate_label(struct check *c, struct dt_info *dti,
 439				  const char *label, struct node *node,
 440				  struct property *prop, struct marker *mark)
 441{
 442	struct node *dt = dti->dt;
 443	struct node *othernode = NULL;
 444	struct property *otherprop = NULL;
 445	struct marker *othermark = NULL;
 446
 447	othernode = get_node_by_label(dt, label);
 448
 449	if (!othernode)
 450		otherprop = get_property_by_label(dt, label, &othernode);
 451	if (!othernode)
 452		othermark = get_marker_label(dt, label, &othernode,
 453					       &otherprop);
 454
 455	if (!othernode)
 456		return;
 457
 458	if ((othernode != node) || (otherprop != prop) || (othermark != mark))
 459		FAIL(c, dti, node, "Duplicate label '%s' on " DESCLABEL_FMT
 460		     " and " DESCLABEL_FMT,
 461		     label, DESCLABEL_ARGS(node, prop, mark),
 462		     DESCLABEL_ARGS(othernode, otherprop, othermark));
 463}
 464
 465static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
 466				       struct node *node)
 467{
 468	struct label *l;
 469	struct property *prop;
 470
 471	for_each_label(node->labels, l)
 472		check_duplicate_label(c, dti, l->label, node, NULL, NULL);
 
 
 
 
 
 
 473
 474	for_each_property(node, prop) {
 475		struct marker *m = prop->val.markers;
 476
 477		for_each_label(prop->labels, l)
 478			check_duplicate_label(c, dti, l->label, node, prop, NULL);
 479
 480		for_each_marker_of_type(m, LABEL)
 481			check_duplicate_label(c, dti, m->ref, node, prop, m);
 482	}
 483}
 484ERROR(duplicate_label, check_duplicate_label_node, NULL);
 
 485
 486static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
 487				 struct node *node, const char *propname)
 488{
 489	struct node *root = dti->dt;
 490	struct property *prop;
 491	struct marker *m;
 
 492	cell_t phandle;
 493
 494	prop = get_property(node, propname);
 495	if (!prop)
 496		return 0;
 497
 498	if (prop->val.len != sizeof(cell_t)) {
 499		FAIL_PROP(c, dti, node, prop, "bad length (%d) %s property",
 500			  prop->val.len, prop->name);
 501		return 0;
 502	}
 503
 504	m = prop->val.markers;
 505	for_each_marker_of_type(m, REF_PHANDLE) {
 506		assert(m->offset == 0);
 507		if (node != get_node_by_ref(root, m->ref))
 508			/* "Set this node's phandle equal to some
 509			 * other node's phandle".  That's nonsensical
 510			 * by construction. */ {
 511			FAIL(c, dti, node, "%s is a reference to another node",
 512			     prop->name);
 
 513		}
 514		/* But setting this node's phandle equal to its own
 515		 * phandle is allowed - that means allocate a unique
 516		 * phandle for this node, even if it's not otherwise
 517		 * referenced.  The value will be filled in later, so
 518		 * we treat it as having no phandle data for now. */
 519		return 0;
 520	}
 521
 522	phandle = propval_cell(prop);
 523
 524	if (!phandle_is_valid(phandle)) {
 525		FAIL_PROP(c, dti, node, prop, "bad value (0x%x) in %s property",
 526		     phandle, prop->name);
 527		return 0;
 528	}
 529
 530	return phandle;
 531}
 532
 533static void check_explicit_phandles(struct check *c, struct dt_info *dti,
 534				    struct node *node)
 535{
 536	struct node *root = dti->dt;
 537	struct node *other;
 538	cell_t phandle, linux_phandle;
 539
 540	/* Nothing should have assigned phandles yet */
 541	assert(!node->phandle);
 542
 543	phandle = check_phandle_prop(c, dti, node, "phandle");
 544
 545	linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");
 546
 547	if (!phandle && !linux_phandle)
 548		/* No valid phandles; nothing further to check */
 549		return;
 550
 551	if (linux_phandle && phandle && (phandle != linux_phandle))
 552		FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'"
 553		     " properties");
 554
 555	if (linux_phandle && !phandle)
 556		phandle = linux_phandle;
 557
 558	other = get_node_by_phandle(root, phandle);
 559	if (other && (other != node)) {
 560		FAIL(c, dti, node, "duplicated phandle 0x%x (seen before at %s)",
 561		     phandle, other->fullpath);
 562		return;
 563	}
 564
 565	node->phandle = phandle;
 566}
 567ERROR(explicit_phandles, check_explicit_phandles, NULL);
 568
 569static void check_name_properties(struct check *c, struct dt_info *dti,
 570				  struct node *node)
 571{
 572	struct property **pp, *prop = NULL;
 573
 574	for (pp = &node->proplist; *pp; pp = &((*pp)->next))
 575		if (streq((*pp)->name, "name")) {
 576			prop = *pp;
 577			break;
 578		}
 579
 580	if (!prop)
 581		return; /* No name property, that's fine */
 582
 583	if ((prop->val.len != node->basenamelen + 1U)
 584	    || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
 585		FAIL(c, dti, node, "\"name\" property is incorrect (\"%s\" instead"
 586		     " of base node name)", prop->val.val);
 587	} else {
 588		/* The name property is correct, and therefore redundant.
 589		 * Delete it */
 590		*pp = prop->next;
 591		free(prop->name);
 592		data_free(prop->val);
 593		free(prop);
 594	}
 595}
 596ERROR_IF_NOT_STRING(name_is_string, "name");
 597ERROR(name_properties, check_name_properties, NULL, &name_is_string);
 598
 599/*
 600 * Reference fixup functions
 601 */
 602
 603static void fixup_phandle_references(struct check *c, struct dt_info *dti,
 604				     struct node *node)
 605{
 606	struct node *dt = dti->dt;
 607	struct property *prop;
 
 608
 609	for_each_property(node, prop) {
 610		struct marker *m = prop->val.markers;
 611		struct node *refnode;
 612		cell_t phandle;
 613
 614		for_each_marker_of_type(m, REF_PHANDLE) {
 615			assert(m->offset + sizeof(cell_t) <= prop->val.len);
 616
 617			refnode = get_node_by_ref(dt, m->ref);
 618			if (! refnode) {
 619				if (!(dti->dtsflags & DTSF_PLUGIN))
 620					FAIL(c, dti, node, "Reference to non-existent node or "
 621							"label \"%s\"\n", m->ref);
 622				else /* mark the entry as unresolved */
 623					*((fdt32_t *)(prop->val.val + m->offset)) =
 624						cpu_to_fdt32(0xffffffff);
 625				continue;
 626			}
 627
 628			phandle = get_node_phandle(dt, refnode);
 629			*((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
 
 
 
 
 630
 631			reference_node(refnode);
 632		}
 633	}
 634}
 635ERROR(phandle_references, fixup_phandle_references, NULL,
 636      &duplicate_node_names, &explicit_phandles);
 637
 638static void fixup_path_references(struct check *c, struct dt_info *dti,
 639				  struct node *node)
 640{
 641	struct node *dt = dti->dt;
 642	struct property *prop;
 
 643
 644	for_each_property(node, prop) {
 645		struct marker *m = prop->val.markers;
 646		struct node *refnode;
 647		char *path;
 648
 649		for_each_marker_of_type(m, REF_PATH) {
 650			assert(m->offset <= prop->val.len);
 651
 652			refnode = get_node_by_ref(dt, m->ref);
 653			if (!refnode) {
 654				FAIL(c, dti, node, "Reference to non-existent node or label \"%s\"\n",
 655				     m->ref);
 656				continue;
 657			}
 658
 659			path = refnode->fullpath;
 660			prop->val = data_insert_at_marker(prop->val, m, path,
 661							  strlen(path) + 1);
 662
 663			reference_node(refnode);
 
 
 
 
 664		}
 
 
 
 
 665	}
 666}
 667ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
 668
 669static void fixup_omit_unused_nodes(struct check *c, struct dt_info *dti,
 670				    struct node *node)
 671{
 672	if (generate_symbols && node->labels)
 673		return;
 674	if (node->omit_if_unused && !node->is_referenced)
 675		delete_node(node);
 676}
 677ERROR(omit_unused_nodes, fixup_omit_unused_nodes, NULL, &phandle_references, &path_references);
 678
 679/*
 680 * Semantic checks
 681 */
 682WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
 683WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
 684
 685WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
 686WARNING_IF_NOT_STRING(model_is_string, "model");
 687WARNING_IF_NOT_STRING(status_is_string, "status");
 688WARNING_IF_NOT_STRING(label_is_string, "label");
 689
 690WARNING_IF_NOT_STRING_LIST(compatible_is_string_list, "compatible");
 691
 692static void check_names_is_string_list(struct check *c, struct dt_info *dti,
 693				       struct node *node)
 694{
 695	struct property *prop;
 696
 697	for_each_property(node, prop) {
 698		if (!strends(prop->name, "-names"))
 699			continue;
 700
 701		c->data = prop->name;
 702		check_is_string_list(c, dti, node);
 703	}
 704}
 705WARNING(names_is_string_list, check_names_is_string_list, NULL);
 706
 707static void check_alias_paths(struct check *c, struct dt_info *dti,
 708				    struct node *node)
 709{
 710	struct property *prop;
 711
 712	if (!streq(node->name, "aliases"))
 713		return;
 714
 715	for_each_property(node, prop) {
 716		if (streq(prop->name, "phandle")
 717		    || streq(prop->name, "linux,phandle")) {
 718			continue;
 719		}
 720
 721		if (!prop->val.val || !get_node_by_path(dti->dt, prop->val.val)) {
 722			FAIL_PROP(c, dti, node, prop, "aliases property is not a valid node (%s)",
 723				  prop->val.val);
 724			continue;
 725		}
 726		if (strspn(prop->name, LOWERCASE DIGITS "-") != strlen(prop->name))
 727			FAIL(c, dti, node, "aliases property name must include only lowercase and '-'");
 728	}
 729}
 730WARNING(alias_paths, check_alias_paths, NULL);
 731
 732static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
 733				  struct node *node)
 734{
 735	struct property *prop;
 736
 737	node->addr_cells = -1;
 738	node->size_cells = -1;
 739
 740	prop = get_property(node, "#address-cells");
 741	if (prop)
 742		node->addr_cells = propval_cell(prop);
 743
 744	prop = get_property(node, "#size-cells");
 745	if (prop)
 746		node->size_cells = propval_cell(prop);
 747}
 748WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
 749	&address_cells_is_cell, &size_cells_is_cell);
 750
 751#define node_addr_cells(n) \
 752	(((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
 753#define node_size_cells(n) \
 754	(((n)->size_cells == -1) ? 1 : (n)->size_cells)
 755
 756static void check_reg_format(struct check *c, struct dt_info *dti,
 757			     struct node *node)
 758{
 759	struct property *prop;
 760	int addr_cells, size_cells, entrylen;
 761
 762	prop = get_property(node, "reg");
 763	if (!prop)
 764		return; /* No "reg", that's fine */
 765
 766	if (!node->parent) {
 767		FAIL(c, dti, node, "Root node has a \"reg\" property");
 768		return;
 769	}
 770
 771	if (prop->val.len == 0)
 772		FAIL_PROP(c, dti, node, prop, "property is empty");
 773
 774	addr_cells = node_addr_cells(node->parent);
 775	size_cells = node_size_cells(node->parent);
 776	entrylen = (addr_cells + size_cells) * sizeof(cell_t);
 777
 778	if (!is_multiple_of(prop->val.len, entrylen))
 779		FAIL_PROP(c, dti, node, prop, "property has invalid length (%d bytes) "
 780			  "(#address-cells == %d, #size-cells == %d)",
 781			  prop->val.len, addr_cells, size_cells);
 782}
 783WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
 784
 785static void check_ranges_format(struct check *c, struct dt_info *dti,
 786				struct node *node)
 787{
 788	struct property *prop;
 789	int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
 790	const char *ranges = c->data;
 791
 792	prop = get_property(node, ranges);
 793	if (!prop)
 794		return;
 795
 796	if (!node->parent) {
 797		FAIL_PROP(c, dti, node, prop, "Root node has a \"%s\" property",
 798			  ranges);
 799		return;
 800	}
 801
 802	p_addr_cells = node_addr_cells(node->parent);
 803	p_size_cells = node_size_cells(node->parent);
 804	c_addr_cells = node_addr_cells(node);
 805	c_size_cells = node_size_cells(node);
 806	entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
 807
 808	if (prop->val.len == 0) {
 809		if (p_addr_cells != c_addr_cells)
 810			FAIL_PROP(c, dti, node, prop, "empty \"%s\" property but its "
 811				  "#address-cells (%d) differs from %s (%d)",
 812				  ranges, c_addr_cells, node->parent->fullpath,
 813				  p_addr_cells);
 814		if (p_size_cells != c_size_cells)
 815			FAIL_PROP(c, dti, node, prop, "empty \"%s\" property but its "
 816				  "#size-cells (%d) differs from %s (%d)",
 817				  ranges, c_size_cells, node->parent->fullpath,
 818				  p_size_cells);
 819	} else if (!is_multiple_of(prop->val.len, entrylen)) {
 820		FAIL_PROP(c, dti, node, prop, "\"%s\" property has invalid length (%d bytes) "
 821			  "(parent #address-cells == %d, child #address-cells == %d, "
 822			  "#size-cells == %d)", ranges, prop->val.len,
 823			  p_addr_cells, c_addr_cells, c_size_cells);
 824	}
 825}
 826WARNING(ranges_format, check_ranges_format, "ranges", &addr_size_cells);
 827WARNING(dma_ranges_format, check_ranges_format, "dma-ranges", &addr_size_cells);
 828
 829static const struct bus_type pci_bus = {
 830	.name = "PCI",
 831};
 832
 833static void check_pci_bridge(struct check *c, struct dt_info *dti, struct node *node)
 834{
 835	struct property *prop;
 836	cell_t *cells;
 837
 838	prop = get_property(node, "device_type");
 839	if (!prop || !streq(prop->val.val, "pci"))
 840		return;
 841
 842	node->bus = &pci_bus;
 843
 844	if (!strprefixeq(node->name, node->basenamelen, "pci") &&
 845	    !strprefixeq(node->name, node->basenamelen, "pcie"))
 846		FAIL(c, dti, node, "node name is not \"pci\" or \"pcie\"");
 847
 848	prop = get_property(node, "ranges");
 849	if (!prop)
 850		FAIL(c, dti, node, "missing ranges for PCI bridge (or not a bridge)");
 851
 852	if (node_addr_cells(node) != 3)
 853		FAIL(c, dti, node, "incorrect #address-cells for PCI bridge");
 854	if (node_size_cells(node) != 2)
 855		FAIL(c, dti, node, "incorrect #size-cells for PCI bridge");
 856
 857	prop = get_property(node, "bus-range");
 858	if (!prop)
 859		return;
 860
 861	if (prop->val.len != (sizeof(cell_t) * 2)) {
 862		FAIL_PROP(c, dti, node, prop, "value must be 2 cells");
 863		return;
 864	}
 865	cells = (cell_t *)prop->val.val;
 866	if (fdt32_to_cpu(cells[0]) > fdt32_to_cpu(cells[1]))
 867		FAIL_PROP(c, dti, node, prop, "1st cell must be less than or equal to 2nd cell");
 868	if (fdt32_to_cpu(cells[1]) > 0xff)
 869		FAIL_PROP(c, dti, node, prop, "maximum bus number must be less than 256");
 870}
 871WARNING(pci_bridge, check_pci_bridge, NULL,
 872	&device_type_is_string, &addr_size_cells);
 873
 874static void check_pci_device_bus_num(struct check *c, struct dt_info *dti, struct node *node)
 875{
 876	struct property *prop;
 877	unsigned int bus_num, min_bus, max_bus;
 878	cell_t *cells;
 879
 880	if (!node->parent || (node->parent->bus != &pci_bus))
 881		return;
 882
 883	prop = get_property(node, "reg");
 884	if (!prop)
 885		return;
 886
 887	cells = (cell_t *)prop->val.val;
 888	bus_num = (fdt32_to_cpu(cells[0]) & 0x00ff0000) >> 16;
 889
 890	prop = get_property(node->parent, "bus-range");
 891	if (!prop) {
 892		min_bus = max_bus = 0;
 893	} else {
 894		cells = (cell_t *)prop->val.val;
 895		min_bus = fdt32_to_cpu(cells[0]);
 896		max_bus = fdt32_to_cpu(cells[1]);
 897	}
 898	if ((bus_num < min_bus) || (bus_num > max_bus))
 899		FAIL_PROP(c, dti, node, prop, "PCI bus number %d out of range, expected (%d - %d)",
 900			  bus_num, min_bus, max_bus);
 901}
 902WARNING(pci_device_bus_num, check_pci_device_bus_num, NULL, &reg_format, &pci_bridge);
 903
 904static void check_pci_device_reg(struct check *c, struct dt_info *dti, struct node *node)
 905{
 906	struct property *prop;
 907	const char *unitname = get_unitname(node);
 908	char unit_addr[5];
 909	unsigned int dev, func, reg;
 910	cell_t *cells;
 911
 912	if (!node->parent || (node->parent->bus != &pci_bus))
 913		return;
 914
 915	prop = get_property(node, "reg");
 916	if (!prop)
 917		return;
 918
 919	cells = (cell_t *)prop->val.val;
 920	if (cells[1] || cells[2])
 921		FAIL_PROP(c, dti, node, prop, "PCI reg config space address cells 2 and 3 must be 0");
 922
 923	reg = fdt32_to_cpu(cells[0]);
 924	dev = (reg & 0xf800) >> 11;
 925	func = (reg & 0x700) >> 8;
 926
 927	if (reg & 0xff000000)
 928		FAIL_PROP(c, dti, node, prop, "PCI reg address is not configuration space");
 929	if (reg & 0x000000ff)
 930		FAIL_PROP(c, dti, node, prop, "PCI reg config space address register number must be 0");
 931
 932	if (func == 0) {
 933		snprintf(unit_addr, sizeof(unit_addr), "%x", dev);
 934		if (streq(unitname, unit_addr))
 935			return;
 936	}
 937
 938	snprintf(unit_addr, sizeof(unit_addr), "%x,%x", dev, func);
 939	if (streq(unitname, unit_addr))
 940		return;
 941
 942	FAIL(c, dti, node, "PCI unit address format error, expected \"%s\"",
 943	     unit_addr);
 944}
 945WARNING(pci_device_reg, check_pci_device_reg, NULL, &reg_format, &pci_bridge);
 946
 947static const struct bus_type simple_bus = {
 948	.name = "simple-bus",
 949};
 950
 951static bool node_is_compatible(struct node *node, const char *compat)
 952{
 953	struct property *prop;
 954	const char *str, *end;
 955
 956	prop = get_property(node, "compatible");
 957	if (!prop)
 958		return false;
 959
 960	for (str = prop->val.val, end = str + prop->val.len; str < end;
 961	     str += strnlen(str, end - str) + 1) {
 962		if (streq(str, compat))
 963			return true;
 964	}
 965	return false;
 966}
 967
 968static void check_simple_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
 969{
 970	if (node_is_compatible(node, "simple-bus"))
 971		node->bus = &simple_bus;
 972}
 973WARNING(simple_bus_bridge, check_simple_bus_bridge, NULL,
 974	&addr_size_cells, &compatible_is_string_list);
 975
 976static void check_simple_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
 977{
 978	struct property *prop;
 979	const char *unitname = get_unitname(node);
 980	char unit_addr[17];
 981	unsigned int size;
 982	uint64_t reg = 0;
 983	cell_t *cells = NULL;
 984
 985	if (!node->parent || (node->parent->bus != &simple_bus))
 986		return;
 987
 988	prop = get_property(node, "reg");
 989	if (prop)
 990		cells = (cell_t *)prop->val.val;
 991	else {
 992		prop = get_property(node, "ranges");
 993		if (prop && prop->val.len)
 994			/* skip of child address */
 995			cells = ((cell_t *)prop->val.val) + node_addr_cells(node);
 996	}
 997
 998	if (!cells) {
 999		if (node->parent->parent && !(node->bus == &simple_bus))
1000			FAIL(c, dti, node, "missing or empty reg/ranges property");
1001		return;
1002	}
1003
1004	size = node_addr_cells(node->parent);
1005	while (size--)
1006		reg = (reg << 32) | fdt32_to_cpu(*(cells++));
1007
1008	snprintf(unit_addr, sizeof(unit_addr), "%"PRIx64, reg);
1009	if (!streq(unitname, unit_addr))
1010		FAIL(c, dti, node, "simple-bus unit address format error, expected \"%s\"",
1011		     unit_addr);
1012}
1013WARNING(simple_bus_reg, check_simple_bus_reg, NULL, &reg_format, &simple_bus_bridge);
1014
1015static const struct bus_type i2c_bus = {
1016	.name = "i2c-bus",
1017};
1018
1019static void check_i2c_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
1020{
1021	if (strprefixeq(node->name, node->basenamelen, "i2c-bus") ||
1022	    strprefixeq(node->name, node->basenamelen, "i2c-arb")) {
1023		node->bus = &i2c_bus;
1024	} else if (strprefixeq(node->name, node->basenamelen, "i2c")) {
1025		struct node *child;
1026		for_each_child(node, child) {
1027			if (strprefixeq(child->name, node->basenamelen, "i2c-bus"))
1028				return;
1029		}
1030		node->bus = &i2c_bus;
1031	} else
1032		return;
1033
1034	if (!node->children)
1035		return;
1036
1037	if (node_addr_cells(node) != 1)
1038		FAIL(c, dti, node, "incorrect #address-cells for I2C bus");
1039	if (node_size_cells(node) != 0)
1040		FAIL(c, dti, node, "incorrect #size-cells for I2C bus");
1041
1042}
1043WARNING(i2c_bus_bridge, check_i2c_bus_bridge, NULL, &addr_size_cells);
1044
1045#define I2C_OWN_SLAVE_ADDRESS	(1U << 30)
1046#define I2C_TEN_BIT_ADDRESS	(1U << 31)
1047
1048static void check_i2c_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
1049{
1050	struct property *prop;
1051	const char *unitname = get_unitname(node);
1052	char unit_addr[17];
1053	uint32_t reg = 0;
1054	int len;
1055	cell_t *cells = NULL;
1056
1057	if (!node->parent || (node->parent->bus != &i2c_bus))
1058		return;
1059
1060	prop = get_property(node, "reg");
1061	if (prop)
1062		cells = (cell_t *)prop->val.val;
1063
1064	if (!cells) {
1065		FAIL(c, dti, node, "missing or empty reg property");
1066		return;
1067	}
1068
1069	reg = fdt32_to_cpu(*cells);
1070	/* Ignore I2C_OWN_SLAVE_ADDRESS */
1071	reg &= ~I2C_OWN_SLAVE_ADDRESS;
1072	snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1073	if (!streq(unitname, unit_addr))
1074		FAIL(c, dti, node, "I2C bus unit address format error, expected \"%s\"",
1075		     unit_addr);
1076
1077	for (len = prop->val.len; len > 0; len -= 4) {
1078		reg = fdt32_to_cpu(*(cells++));
1079		/* Ignore I2C_OWN_SLAVE_ADDRESS */
1080		reg &= ~I2C_OWN_SLAVE_ADDRESS;
1081
1082		if (reg & I2C_TEN_BIT_ADDRESS) {
1083			if ((reg & ~I2C_TEN_BIT_ADDRESS) > 0x3ff)
1084				FAIL_PROP(c, dti, node, prop, "I2C address must be less than 10-bits, got \"0x%x\"",
1085				  reg);
1086		} else if (reg > 0x7f)
1087			FAIL_PROP(c, dti, node, prop, "I2C address must be less than 7-bits, got \"0x%x\". Set I2C_TEN_BIT_ADDRESS for 10 bit addresses or fix the property",
1088				  reg);
1089	}
1090}
1091WARNING(i2c_bus_reg, check_i2c_bus_reg, NULL, &reg_format, &i2c_bus_bridge);
1092
1093static const struct bus_type spi_bus = {
1094	.name = "spi-bus",
1095};
1096
1097static void check_spi_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
1098{
1099	int spi_addr_cells = 1;
1100
1101	if (strprefixeq(node->name, node->basenamelen, "spi")) {
1102		node->bus = &spi_bus;
1103	} else {
1104		/* Try to detect SPI buses which don't have proper node name */
1105		struct node *child;
1106
1107		if (node_addr_cells(node) != 1 || node_size_cells(node) != 0)
1108			return;
1109
1110		for_each_child(node, child) {
1111			struct property *prop;
1112			for_each_property(child, prop) {
1113				if (strstarts(prop->name, "spi-")) {
1114					node->bus = &spi_bus;
1115					break;
1116				}
1117			}
1118			if (node->bus == &spi_bus)
1119				break;
1120		}
1121
1122		if (node->bus == &spi_bus && get_property(node, "reg"))
1123			FAIL(c, dti, node, "node name for SPI buses should be 'spi'");
1124	}
1125	if (node->bus != &spi_bus || !node->children)
1126		return;
1127
1128	if (get_property(node, "spi-slave"))
1129		spi_addr_cells = 0;
1130	if (node_addr_cells(node) != spi_addr_cells)
1131		FAIL(c, dti, node, "incorrect #address-cells for SPI bus");
1132	if (node_size_cells(node) != 0)
1133		FAIL(c, dti, node, "incorrect #size-cells for SPI bus");
1134
1135}
1136WARNING(spi_bus_bridge, check_spi_bus_bridge, NULL, &addr_size_cells);
1137
1138static void check_spi_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
1139{
1140	struct property *prop;
1141	const char *unitname = get_unitname(node);
1142	char unit_addr[9];
1143	uint32_t reg = 0;
1144	cell_t *cells = NULL;
1145
1146	if (!node->parent || (node->parent->bus != &spi_bus))
1147		return;
1148
1149	if (get_property(node->parent, "spi-slave"))
1150		return;
1151
1152	prop = get_property(node, "reg");
1153	if (prop)
1154		cells = (cell_t *)prop->val.val;
1155
1156	if (!cells) {
1157		FAIL(c, dti, node, "missing or empty reg property");
1158		return;
1159	}
1160
1161	reg = fdt32_to_cpu(*cells);
1162	snprintf(unit_addr, sizeof(unit_addr), "%x", reg);
1163	if (!streq(unitname, unit_addr))
1164		FAIL(c, dti, node, "SPI bus unit address format error, expected \"%s\"",
1165		     unit_addr);
1166}
1167WARNING(spi_bus_reg, check_spi_bus_reg, NULL, &reg_format, &spi_bus_bridge);
1168
1169static void check_unit_address_format(struct check *c, struct dt_info *dti,
1170				      struct node *node)
1171{
1172	const char *unitname = get_unitname(node);
1173
1174	if (node->parent && node->parent->bus)
1175		return;
1176
1177	if (!unitname[0])
1178		return;
1179
1180	if (!strncmp(unitname, "0x", 2)) {
1181		FAIL(c, dti, node, "unit name should not have leading \"0x\"");
1182		/* skip over 0x for next test */
1183		unitname += 2;
1184	}
1185	if (unitname[0] == '0' && isxdigit((unsigned char)unitname[1]))
1186		FAIL(c, dti, node, "unit name should not have leading 0s");
1187}
1188WARNING(unit_address_format, check_unit_address_format, NULL,
1189	&node_name_format, &pci_bridge, &simple_bus_bridge);
1190
1191/*
1192 * Style checks
1193 */
1194static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
1195					  struct node *node)
1196{
1197	struct property *reg, *ranges;
1198
1199	if (!node->parent)
1200		return; /* Ignore root node */
1201
1202	reg = get_property(node, "reg");
1203	ranges = get_property(node, "ranges");
1204
1205	if (!reg && !ranges)
1206		return;
1207
1208	if (node->parent->addr_cells == -1)
1209		FAIL(c, dti, node, "Relying on default #address-cells value");
1210
1211	if (node->parent->size_cells == -1)
1212		FAIL(c, dti, node, "Relying on default #size-cells value");
 
 
1213}
1214WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
1215	&addr_size_cells);
1216
1217static void check_avoid_unnecessary_addr_size(struct check *c, struct dt_info *dti,
1218					      struct node *node)
1219{
1220	struct property *prop;
1221	struct node *child;
1222	bool has_reg = false;
1223
1224	if (!node->parent || node->addr_cells < 0 || node->size_cells < 0)
1225		return;
1226
1227	if (get_property(node, "ranges") || get_property(node, "dma-ranges") || !node->children)
1228		return;
1229
1230	for_each_child(node, child) {
1231		prop = get_property(child, "reg");
1232		if (prop)
1233			has_reg = true;
1234	}
1235
1236	if (!has_reg)
1237		FAIL(c, dti, node, "unnecessary #address-cells/#size-cells without \"ranges\", \"dma-ranges\" or child \"reg\" property");
1238}
1239WARNING(avoid_unnecessary_addr_size, check_avoid_unnecessary_addr_size, NULL, &avoid_default_addr_size);
1240
1241static bool node_is_disabled(struct node *node)
1242{
1243	struct property *prop;
1244
1245	prop = get_property(node, "status");
1246	if (prop) {
1247		char *str = prop->val.val;
1248		if (streq("disabled", str))
1249			return true;
1250	}
1251
1252	return false;
1253}
1254
1255static void check_unique_unit_address_common(struct check *c,
1256						struct dt_info *dti,
1257						struct node *node,
1258						bool disable_check)
1259{
1260	struct node *childa;
1261
1262	if (node->addr_cells < 0 || node->size_cells < 0)
1263		return;
1264
1265	if (!node->children)
1266		return;
1267
1268	for_each_child(node, childa) {
1269		struct node *childb;
1270		const char *addr_a = get_unitname(childa);
1271
1272		if (!strlen(addr_a))
1273			continue;
1274
1275		if (disable_check && node_is_disabled(childa))
1276			continue;
1277
1278		for_each_child(node, childb) {
1279			const char *addr_b = get_unitname(childb);
1280			if (childa == childb)
1281				break;
1282
1283			if (disable_check && node_is_disabled(childb))
1284				continue;
1285
1286			if (streq(addr_a, addr_b))
1287				FAIL(c, dti, childb, "duplicate unit-address (also used in node %s)", childa->fullpath);
1288		}
1289	}
1290}
1291
1292static void check_unique_unit_address(struct check *c, struct dt_info *dti,
1293					      struct node *node)
1294{
1295	check_unique_unit_address_common(c, dti, node, false);
1296}
1297WARNING(unique_unit_address, check_unique_unit_address, NULL, &avoid_default_addr_size);
1298
1299static void check_unique_unit_address_if_enabled(struct check *c, struct dt_info *dti,
1300					      struct node *node)
1301{
1302	check_unique_unit_address_common(c, dti, node, true);
1303}
1304CHECK_ENTRY(unique_unit_address_if_enabled, check_unique_unit_address_if_enabled,
1305	    NULL, false, false, &avoid_default_addr_size);
1306
1307static void check_obsolete_chosen_interrupt_controller(struct check *c,
1308						       struct dt_info *dti,
1309						       struct node *node)
1310{
1311	struct node *dt = dti->dt;
1312	struct node *chosen;
1313	struct property *prop;
1314
1315	if (node != dt)
1316		return;
1317
1318
1319	chosen = get_node_by_path(dt, "/chosen");
1320	if (!chosen)
1321		return;
1322
1323	prop = get_property(chosen, "interrupt-controller");
1324	if (prop)
1325		FAIL_PROP(c, dti, node, prop,
1326			  "/chosen has obsolete \"interrupt-controller\" property");
1327}
1328WARNING(obsolete_chosen_interrupt_controller,
1329	check_obsolete_chosen_interrupt_controller, NULL);
1330
1331static void check_chosen_node_is_root(struct check *c, struct dt_info *dti,
1332				      struct node *node)
1333{
1334	if (!streq(node->name, "chosen"))
1335		return;
1336
1337	if (node->parent != dti->dt)
1338		FAIL(c, dti, node, "chosen node must be at root node");
1339}
1340WARNING(chosen_node_is_root, check_chosen_node_is_root, NULL);
1341
1342static void check_chosen_node_bootargs(struct check *c, struct dt_info *dti,
1343				       struct node *node)
1344{
1345	struct property *prop;
1346
1347	if (!streq(node->name, "chosen"))
1348		return;
1349
1350	prop = get_property(node, "bootargs");
1351	if (!prop)
1352		return;
1353
1354	c->data = prop->name;
1355	check_is_string(c, dti, node);
1356}
1357WARNING(chosen_node_bootargs, check_chosen_node_bootargs, NULL);
1358
1359static void check_chosen_node_stdout_path(struct check *c, struct dt_info *dti,
1360					  struct node *node)
1361{
1362	struct property *prop;
1363
1364	if (!streq(node->name, "chosen"))
1365		return;
1366
1367	prop = get_property(node, "stdout-path");
1368	if (!prop) {
1369		prop = get_property(node, "linux,stdout-path");
1370		if (!prop)
1371			return;
1372		FAIL_PROP(c, dti, node, prop, "Use 'stdout-path' instead");
1373	}
1374
1375	c->data = prop->name;
1376	check_is_string(c, dti, node);
1377}
1378WARNING(chosen_node_stdout_path, check_chosen_node_stdout_path, NULL);
1379
1380struct provider {
1381	const char *prop_name;
1382	const char *cell_name;
1383	bool optional;
1384};
1385
1386static void check_property_phandle_args(struct check *c,
1387					struct dt_info *dti,
1388					struct node *node,
1389					struct property *prop,
1390					const struct provider *provider)
1391{
1392	struct node *root = dti->dt;
1393	unsigned int cell, cellsize = 0;
1394
1395	if (!is_multiple_of(prop->val.len, sizeof(cell_t))) {
1396		FAIL_PROP(c, dti, node, prop,
1397			  "property size (%d) is invalid, expected multiple of %zu",
1398			  prop->val.len, sizeof(cell_t));
1399		return;
1400	}
1401
1402	for (cell = 0; cell < prop->val.len / sizeof(cell_t); cell += cellsize + 1) {
1403		struct node *provider_node;
1404		struct property *cellprop;
1405		cell_t phandle;
1406		unsigned int expected;
1407
1408		phandle = propval_cell_n(prop, cell);
1409		/*
1410		 * Some bindings use a cell value 0 or -1 to skip over optional
1411		 * entries when each index position has a specific definition.
1412		 */
1413		if (!phandle_is_valid(phandle)) {
1414			/* Give up if this is an overlay with external references */
1415			if (dti->dtsflags & DTSF_PLUGIN)
1416				break;
1417
1418			cellsize = 0;
1419			continue;
1420		}
1421
1422		/* If we have markers, verify the current cell is a phandle */
1423		if (prop->val.markers) {
1424			struct marker *m = prop->val.markers;
1425			for_each_marker_of_type(m, REF_PHANDLE) {
1426				if (m->offset == (cell * sizeof(cell_t)))
1427					break;
1428			}
1429			if (!m)
1430				FAIL_PROP(c, dti, node, prop,
1431					  "cell %d is not a phandle reference",
1432					  cell);
1433		}
1434
1435		provider_node = get_node_by_phandle(root, phandle);
1436		if (!provider_node) {
1437			FAIL_PROP(c, dti, node, prop,
1438				  "Could not get phandle node for (cell %d)",
1439				  cell);
1440			break;
1441		}
1442
1443		cellprop = get_property(provider_node, provider->cell_name);
1444		if (cellprop) {
1445			cellsize = propval_cell(cellprop);
1446		} else if (provider->optional) {
1447			cellsize = 0;
1448		} else {
1449			FAIL(c, dti, node, "Missing property '%s' in node %s or bad phandle (referred from %s[%d])",
1450			     provider->cell_name,
1451			     provider_node->fullpath,
1452			     prop->name, cell);
1453			break;
1454		}
1455
1456		expected = (cell + cellsize + 1) * sizeof(cell_t);
1457		if ((expected <= cell) || prop->val.len < expected) {
1458			FAIL_PROP(c, dti, node, prop,
1459				  "property size (%d) too small for cell size %u",
1460				  prop->val.len, cellsize);
1461			break;
1462		}
1463	}
1464}
1465
1466static void check_provider_cells_property(struct check *c,
1467					  struct dt_info *dti,
1468				          struct node *node)
1469{
1470	const struct provider *provider = c->data;
1471	struct property *prop;
1472
1473	prop = get_property(node, provider->prop_name);
1474	if (!prop)
1475		return;
1476
1477	check_property_phandle_args(c, dti, node, prop, provider);
1478}
1479#define WARNING_PROPERTY_PHANDLE_CELLS(nm, propname, cells_name, ...) \
1480	static struct provider nm##_provider = { (propname), (cells_name), __VA_ARGS__ }; \
1481	WARNING_IF_NOT_CELL(nm##_is_cell, cells_name); \
1482	WARNING(nm##_property, check_provider_cells_property, &nm##_provider, &nm##_is_cell, &phandle_references);
1483
1484WARNING_PROPERTY_PHANDLE_CELLS(clocks, "clocks", "#clock-cells");
1485WARNING_PROPERTY_PHANDLE_CELLS(cooling_device, "cooling-device", "#cooling-cells");
1486WARNING_PROPERTY_PHANDLE_CELLS(dmas, "dmas", "#dma-cells");
1487WARNING_PROPERTY_PHANDLE_CELLS(hwlocks, "hwlocks", "#hwlock-cells");
1488WARNING_PROPERTY_PHANDLE_CELLS(interrupts_extended, "interrupts-extended", "#interrupt-cells");
1489WARNING_PROPERTY_PHANDLE_CELLS(io_channels, "io-channels", "#io-channel-cells");
1490WARNING_PROPERTY_PHANDLE_CELLS(iommus, "iommus", "#iommu-cells");
1491WARNING_PROPERTY_PHANDLE_CELLS(mboxes, "mboxes", "#mbox-cells");
1492WARNING_PROPERTY_PHANDLE_CELLS(msi_parent, "msi-parent", "#msi-cells", true);
1493WARNING_PROPERTY_PHANDLE_CELLS(mux_controls, "mux-controls", "#mux-control-cells");
1494WARNING_PROPERTY_PHANDLE_CELLS(phys, "phys", "#phy-cells");
1495WARNING_PROPERTY_PHANDLE_CELLS(power_domains, "power-domains", "#power-domain-cells");
1496WARNING_PROPERTY_PHANDLE_CELLS(pwms, "pwms", "#pwm-cells");
1497WARNING_PROPERTY_PHANDLE_CELLS(resets, "resets", "#reset-cells");
1498WARNING_PROPERTY_PHANDLE_CELLS(sound_dai, "sound-dai", "#sound-dai-cells");
1499WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors, "thermal-sensors", "#thermal-sensor-cells");
1500
1501static bool prop_is_gpio(struct property *prop)
1502{
1503	/*
1504	 * *-gpios and *-gpio can appear in property names,
1505	 * so skip over any false matches (only one known ATM)
1506	 */
1507	if (strends(prop->name, ",nr-gpios"))
1508		return false;
1509
1510	return strends(prop->name, "-gpios") ||
1511		streq(prop->name, "gpios") ||
1512		strends(prop->name, "-gpio") ||
1513		streq(prop->name, "gpio");
1514}
1515
1516static void check_gpios_property(struct check *c,
1517					  struct dt_info *dti,
1518				          struct node *node)
1519{
1520	struct property *prop;
1521
1522	/* Skip GPIO hog nodes which have 'gpios' property */
1523	if (get_property(node, "gpio-hog"))
1524		return;
1525
1526	for_each_property(node, prop) {
1527		struct provider provider;
1528
1529		if (!prop_is_gpio(prop))
1530			continue;
1531
1532		provider.prop_name = prop->name;
1533		provider.cell_name = "#gpio-cells";
1534		provider.optional = false;
1535		check_property_phandle_args(c, dti, node, prop, &provider);
1536	}
1537
1538}
1539WARNING(gpios_property, check_gpios_property, NULL, &phandle_references);
1540
1541static void check_deprecated_gpio_property(struct check *c,
1542					   struct dt_info *dti,
1543				           struct node *node)
1544{
1545	struct property *prop;
1546
1547	for_each_property(node, prop) {
1548		if (!prop_is_gpio(prop))
1549			continue;
1550
1551		if (!strends(prop->name, "gpio"))
1552			continue;
1553
1554		FAIL_PROP(c, dti, node, prop,
1555			  "'[*-]gpio' is deprecated, use '[*-]gpios' instead");
1556	}
1557
1558}
1559CHECK(deprecated_gpio_property, check_deprecated_gpio_property, NULL);
1560
1561static bool node_is_interrupt_provider(struct node *node)
1562{
1563	struct property *prop;
1564
1565	prop = get_property(node, "interrupt-controller");
1566	if (prop)
1567		return true;
1568
1569	prop = get_property(node, "interrupt-map");
1570	if (prop)
1571		return true;
1572
1573	return false;
1574}
1575
1576static void check_interrupt_provider(struct check *c,
1577				     struct dt_info *dti,
1578				     struct node *node)
1579{
1580	struct property *prop;
1581	bool irq_provider = node_is_interrupt_provider(node);
1582
1583	prop = get_property(node, "#interrupt-cells");
1584	if (irq_provider && !prop) {
1585		FAIL(c, dti, node,
1586		     "Missing '#interrupt-cells' in interrupt provider");
1587		return;
1588	}
1589
1590	if (!irq_provider && prop) {
1591		FAIL(c, dti, node,
1592		     "'#interrupt-cells' found, but node is not an interrupt provider");
1593		return;
1594	}
1595}
1596WARNING(interrupt_provider, check_interrupt_provider, NULL, &interrupts_extended_is_cell);
1597
1598static void check_interrupt_map(struct check *c,
1599				struct dt_info *dti,
1600				struct node *node)
1601{
1602	struct node *root = dti->dt;
1603	struct property *prop, *irq_map_prop;
1604	size_t cellsize, cell, map_cells;
1605
1606	irq_map_prop = get_property(node, "interrupt-map");
1607	if (!irq_map_prop)
1608		return;
1609
1610	if (node->addr_cells < 0) {
1611		FAIL(c, dti, node,
1612		     "Missing '#address-cells' in interrupt-map provider");
1613		return;
1614	}
1615	cellsize = node_addr_cells(node);
1616	cellsize += propval_cell(get_property(node, "#interrupt-cells"));
1617
1618	prop = get_property(node, "interrupt-map-mask");
1619	if (prop && (prop->val.len != (cellsize * sizeof(cell_t))))
1620		FAIL_PROP(c, dti, node, prop,
1621			  "property size (%d) is invalid, expected %zu",
1622			  prop->val.len, cellsize * sizeof(cell_t));
1623
1624	if (!is_multiple_of(irq_map_prop->val.len, sizeof(cell_t))) {
1625		FAIL_PROP(c, dti, node, irq_map_prop,
1626			  "property size (%d) is invalid, expected multiple of %zu",
1627			  irq_map_prop->val.len, sizeof(cell_t));
1628		return;
1629	}
1630
1631	map_cells = irq_map_prop->val.len / sizeof(cell_t);
1632	for (cell = 0; cell < map_cells; ) {
1633		struct node *provider_node;
1634		struct property *cellprop;
1635		int phandle;
1636		size_t parent_cellsize;
1637
1638		if ((cell + cellsize) >= map_cells) {
1639			FAIL_PROP(c, dti, node, irq_map_prop,
1640				  "property size (%d) too small, expected > %zu",
1641				  irq_map_prop->val.len, (cell + cellsize) * sizeof(cell_t));
1642			break;
1643		}
1644		cell += cellsize;
1645
1646		phandle = propval_cell_n(irq_map_prop, cell);
1647		if (!phandle_is_valid(phandle)) {
1648			/* Give up if this is an overlay with external references */
1649			if (!(dti->dtsflags & DTSF_PLUGIN))
1650				FAIL_PROP(c, dti, node, irq_map_prop,
1651					  "Cell %zu is not a phandle(%d)",
1652					  cell, phandle);
1653			break;
1654		}
1655
1656		provider_node = get_node_by_phandle(root, phandle);
1657		if (!provider_node) {
1658			FAIL_PROP(c, dti, node, irq_map_prop,
1659				  "Could not get phandle(%d) node for (cell %zu)",
1660				  phandle, cell);
1661			break;
1662		}
1663
1664		cellprop = get_property(provider_node, "#interrupt-cells");
1665		if (cellprop) {
1666			parent_cellsize = propval_cell(cellprop);
1667		} else {
1668			FAIL(c, dti, node, "Missing property '#interrupt-cells' in node %s or bad phandle (referred from interrupt-map[%zu])",
1669			     provider_node->fullpath, cell);
1670			break;
1671		}
1672
1673		cellprop = get_property(provider_node, "#address-cells");
1674		if (cellprop)
1675			parent_cellsize += propval_cell(cellprop);
1676
1677		cell += 1 + parent_cellsize;
1678		if (cell > map_cells)
1679			FAIL_PROP(c, dti, node, irq_map_prop,
1680				"property size (%d) mismatch, expected %zu",
1681				irq_map_prop->val.len, cell * sizeof(cell_t));
1682	}
1683}
1684WARNING(interrupt_map, check_interrupt_map, NULL, &phandle_references, &addr_size_cells, &interrupt_provider);
1685
1686static void check_interrupts_property(struct check *c,
1687				      struct dt_info *dti,
1688				      struct node *node)
1689{
1690	struct node *root = dti->dt;
1691	struct node *irq_node = NULL, *parent = node;
1692	struct property *irq_prop, *prop = NULL;
1693	cell_t irq_cells, phandle;
1694
1695	irq_prop = get_property(node, "interrupts");
1696	if (!irq_prop)
1697		return;
1698
1699	if (!is_multiple_of(irq_prop->val.len, sizeof(cell_t)))
1700		FAIL_PROP(c, dti, node, irq_prop, "size (%d) is invalid, expected multiple of %zu",
1701		     irq_prop->val.len, sizeof(cell_t));
1702
1703	while (parent && !prop) {
1704		if (parent != node && node_is_interrupt_provider(parent)) {
1705			irq_node = parent;
1706			break;
1707		}
1708
1709		prop = get_property(parent, "interrupt-parent");
1710		if (prop) {
1711			phandle = propval_cell(prop);
1712			if (!phandle_is_valid(phandle)) {
1713				/* Give up if this is an overlay with
1714				 * external references */
1715				if (dti->dtsflags & DTSF_PLUGIN)
1716					return;
1717				FAIL_PROP(c, dti, parent, prop, "Invalid phandle");
1718				continue;
1719			}
1720
1721			irq_node = get_node_by_phandle(root, phandle);
1722			if (!irq_node) {
1723				FAIL_PROP(c, dti, parent, prop, "Bad phandle");
1724				return;
1725			}
1726			if (!node_is_interrupt_provider(irq_node))
1727				FAIL(c, dti, irq_node,
1728				     "Missing interrupt-controller or interrupt-map property");
1729
1730			break;
1731		}
1732
1733		parent = parent->parent;
1734	}
1735
1736	if (!irq_node) {
1737		FAIL(c, dti, node, "Missing interrupt-parent");
1738		return;
1739	}
1740
1741	prop = get_property(irq_node, "#interrupt-cells");
1742	if (!prop) {
1743		/* We warn about that already in another test. */
1744		return;
1745	}
1746
1747	irq_cells = propval_cell(prop);
1748	if (!is_multiple_of(irq_prop->val.len, irq_cells * sizeof(cell_t))) {
1749		FAIL_PROP(c, dti, node, prop,
1750			  "size is (%d), expected multiple of %d",
1751			  irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
1752	}
1753}
1754WARNING(interrupts_property, check_interrupts_property, &phandle_references);
1755
1756static const struct bus_type graph_port_bus = {
1757	.name = "graph-port",
1758};
1759
1760static const struct bus_type graph_ports_bus = {
1761	.name = "graph-ports",
1762};
1763
1764static void check_graph_nodes(struct check *c, struct dt_info *dti,
1765			      struct node *node)
1766{
1767	struct node *child;
1768
1769	for_each_child(node, child) {
1770		if (!(strprefixeq(child->name, child->basenamelen, "endpoint") ||
1771		      get_property(child, "remote-endpoint")))
1772			continue;
1773
1774                /* The root node cannot be a port */
1775		if (!node->parent) {
1776			FAIL(c, dti, node, "root node contains endpoint node '%s', potentially misplaced remote-endpoint property", child->name);
1777			continue;
1778		}
1779		node->bus = &graph_port_bus;
1780
1781		/* The parent of 'port' nodes can be either 'ports' or a device */
1782		if (!node->parent->bus &&
1783		    (streq(node->parent->name, "ports") || get_property(node, "reg")))
1784			node->parent->bus = &graph_ports_bus;
1785
1786		break;
1787	}
1788
1789}
1790WARNING(graph_nodes, check_graph_nodes, NULL);
1791
1792static void check_graph_reg(struct check *c, struct dt_info *dti,
1793			    struct node *node)
1794{
1795	char unit_addr[9];
1796	const char *unitname = get_unitname(node);
1797	struct property *prop;
1798
1799	prop = get_property(node, "reg");
1800	if (!prop || !unitname)
1801		return;
1802
1803	if (!(prop->val.val && prop->val.len == sizeof(cell_t))) {
1804		FAIL(c, dti, node, "graph node malformed 'reg' property");
1805		return;
1806	}
1807
1808	snprintf(unit_addr, sizeof(unit_addr), "%x", propval_cell(prop));
1809	if (!streq(unitname, unit_addr))
1810		FAIL(c, dti, node, "graph node unit address error, expected \"%s\"",
1811		     unit_addr);
1812
1813	if (node->parent->addr_cells != 1)
1814		FAIL_PROP(c, dti, node, get_property(node, "#address-cells"),
1815			  "graph node '#address-cells' is %d, must be 1",
1816			  node->parent->addr_cells);
1817	if (node->parent->size_cells != 0)
1818		FAIL_PROP(c, dti, node, get_property(node, "#size-cells"),
1819			  "graph node '#size-cells' is %d, must be 0",
1820			  node->parent->size_cells);
1821}
1822
1823static void check_graph_port(struct check *c, struct dt_info *dti,
1824			     struct node *node)
1825{
1826	if (node->bus != &graph_port_bus)
1827		return;
1828
1829	check_graph_reg(c, dti, node);
1830
1831	/* skip checks below for overlays */
1832	if (dti->dtsflags & DTSF_PLUGIN)
1833		return;
1834
1835	if (!strprefixeq(node->name, node->basenamelen, "port"))
1836		FAIL(c, dti, node, "graph port node name should be 'port'");
1837}
1838WARNING(graph_port, check_graph_port, NULL, &graph_nodes);
1839
1840static struct node *get_remote_endpoint(struct check *c, struct dt_info *dti,
1841					struct node *endpoint)
1842{
1843	cell_t phandle;
1844	struct node *node;
1845	struct property *prop;
1846
1847	prop = get_property(endpoint, "remote-endpoint");
1848	if (!prop)
1849		return NULL;
1850
1851	phandle = propval_cell(prop);
1852	/* Give up if this is an overlay with external references */
1853	if (!phandle_is_valid(phandle))
1854		return NULL;
1855
1856	node = get_node_by_phandle(dti->dt, phandle);
1857	if (!node)
1858		FAIL_PROP(c, dti, endpoint, prop, "graph phandle is not valid");
1859
1860	return node;
1861}
1862
1863static void check_graph_endpoint(struct check *c, struct dt_info *dti,
1864				 struct node *node)
1865{
1866	struct node *remote_node;
1867
1868	if (!node->parent || node->parent->bus != &graph_port_bus)
1869		return;
1870
1871	check_graph_reg(c, dti, node);
1872
1873	/* skip checks below for overlays */
1874	if (dti->dtsflags & DTSF_PLUGIN)
1875		return;
1876
1877	if (!strprefixeq(node->name, node->basenamelen, "endpoint"))
1878		FAIL(c, dti, node, "graph endpoint node name should be 'endpoint'");
1879
1880	remote_node = get_remote_endpoint(c, dti, node);
1881	if (!remote_node)
1882		return;
1883
1884	if (get_remote_endpoint(c, dti, remote_node) != node)
1885		FAIL(c, dti, node, "graph connection to node '%s' is not bidirectional",
1886		     remote_node->fullpath);
1887}
1888WARNING(graph_endpoint, check_graph_endpoint, NULL, &graph_nodes);
1889
1890static void check_graph_child_address(struct check *c, struct dt_info *dti,
1891				      struct node *node)
1892{
1893	int cnt = 0;
1894	struct node *child;
1895
1896	if (node->bus != &graph_ports_bus && node->bus != &graph_port_bus)
1897		return;
1898
1899	for_each_child(node, child) {
1900		struct property *prop = get_property(child, "reg");
1901
1902		/* No error if we have any non-zero unit address */
1903                if (prop && propval_cell(prop) != 0 )
1904			return;
1905
1906		cnt++;
1907	}
1908
1909	if (cnt == 1 && node->addr_cells != -1)
1910		FAIL(c, dti, node, "graph node has single child node '%s', #address-cells/#size-cells are not necessary",
1911		     node->children->name);
1912}
1913WARNING(graph_child_address, check_graph_child_address, NULL, &graph_nodes, &graph_port, &graph_endpoint);
1914
1915static struct check *check_table[] = {
1916	&duplicate_node_names, &duplicate_property_names,
1917	&node_name_chars, &node_name_format, &property_name_chars,
1918	&name_is_string, &name_properties, &node_name_vs_property_name,
1919
1920	&duplicate_label,
1921
1922	&explicit_phandles,
1923	&phandle_references, &path_references,
1924	&omit_unused_nodes,
1925
1926	&address_cells_is_cell, &size_cells_is_cell,
1927	&device_type_is_string, &model_is_string, &status_is_string,
1928	&label_is_string,
1929
1930	&compatible_is_string_list, &names_is_string_list,
1931
1932	&property_name_chars_strict,
1933	&node_name_chars_strict,
1934
1935	&addr_size_cells, &reg_format, &ranges_format, &dma_ranges_format,
1936
1937	&unit_address_vs_reg,
1938	&unit_address_format,
1939
1940	&pci_bridge,
1941	&pci_device_reg,
1942	&pci_device_bus_num,
1943
1944	&simple_bus_bridge,
1945	&simple_bus_reg,
1946
1947	&i2c_bus_bridge,
1948	&i2c_bus_reg,
1949
1950	&spi_bus_bridge,
1951	&spi_bus_reg,
1952
1953	&avoid_default_addr_size,
1954	&avoid_unnecessary_addr_size,
1955	&unique_unit_address,
1956	&unique_unit_address_if_enabled,
1957	&obsolete_chosen_interrupt_controller,
1958	&chosen_node_is_root, &chosen_node_bootargs, &chosen_node_stdout_path,
1959
1960	&clocks_property,
1961	&clocks_is_cell,
1962	&cooling_device_property,
1963	&cooling_device_is_cell,
1964	&dmas_property,
1965	&dmas_is_cell,
1966	&hwlocks_property,
1967	&hwlocks_is_cell,
1968	&interrupts_extended_property,
1969	&interrupts_extended_is_cell,
1970	&io_channels_property,
1971	&io_channels_is_cell,
1972	&iommus_property,
1973	&iommus_is_cell,
1974	&mboxes_property,
1975	&mboxes_is_cell,
1976	&msi_parent_property,
1977	&msi_parent_is_cell,
1978	&mux_controls_property,
1979	&mux_controls_is_cell,
1980	&phys_property,
1981	&phys_is_cell,
1982	&power_domains_property,
1983	&power_domains_is_cell,
1984	&pwms_property,
1985	&pwms_is_cell,
1986	&resets_property,
1987	&resets_is_cell,
1988	&sound_dai_property,
1989	&sound_dai_is_cell,
1990	&thermal_sensors_property,
1991	&thermal_sensors_is_cell,
1992
1993	&deprecated_gpio_property,
1994	&gpios_property,
1995	&interrupts_property,
1996	&interrupt_provider,
1997	&interrupt_map,
1998
1999	&alias_paths,
2000
2001	&graph_nodes, &graph_child_address, &graph_port, &graph_endpoint,
2002
2003	&always_fail,
2004};
2005
2006static void enable_warning_error(struct check *c, bool warn, bool error)
2007{
 
2008	int i;
2009
2010	/* Raising level, also raise it for prereqs */
2011	if ((warn && !c->warn) || (error && !c->error))
2012		for (i = 0; i < c->num_prereqs; i++)
2013			enable_warning_error(c->prereq[i], warn, error);
2014
2015	c->warn = c->warn || warn;
2016	c->error = c->error || error;
2017}
2018
2019static void disable_warning_error(struct check *c, bool warn, bool error)
2020{
2021	unsigned int i;
2022
2023	/* Lowering level, also lower it for things this is the prereq
2024	 * for */
2025	if ((warn && c->warn) || (error && c->error)) {
2026		for (i = 0; i < ARRAY_SIZE(check_table); i++) {
2027			struct check *cc = check_table[i];
2028			int j;
2029
2030			for (j = 0; j < cc->num_prereqs; j++)
2031				if (cc->prereq[j] == c)
2032					disable_warning_error(cc, warn, error);
2033		}
2034	}
2035
2036	c->warn = c->warn && !warn;
2037	c->error = c->error && !error;
2038}
2039
2040void parse_checks_option(bool warn, bool error, const char *arg)
2041{
2042	unsigned int i;
2043	const char *name = arg;
2044	bool enable = true;
2045
2046	if ((strncmp(arg, "no-", 3) == 0)
2047	    || (strncmp(arg, "no_", 3) == 0)) {
2048		name = arg + 3;
2049		enable = false;
2050	}
2051
2052	for (i = 0; i < ARRAY_SIZE(check_table); i++) {
2053		struct check *c = check_table[i];
2054
2055		if (streq(c->name, name)) {
2056			if (enable)
2057				enable_warning_error(c, warn, error);
2058			else
2059				disable_warning_error(c, warn, error);
2060			return;
2061		}
2062	}
2063
2064	die("Unrecognized check name \"%s\"\n", name);
2065}
2066
2067void process_checks(bool force, struct dt_info *dti)
2068{
2069	unsigned int i;
2070	int error = 0;
2071
2072	for (i = 0; i < ARRAY_SIZE(check_table); i++) {
2073		struct check *c = check_table[i];
2074
2075		if (c->warn || c->error)
2076			error = error || run_check(c, dti);
2077	}
2078
2079	if (error) {
2080		if (!force) {
2081			fprintf(stderr, "ERROR: Input tree has errors, aborting "
2082				"(use -f to force output)\n");
2083			exit(2);
2084		} else if (quiet < 3) {
2085			fprintf(stderr, "Warning: Input tree has errors, "
2086				"output forced\n");
2087		}
2088	}
2089}