Loading...
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, ®_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}
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 checkstatus {
35 UNCHECKED = 0,
36 PREREQ,
37 PASSED,
38 FAILED,
39};
40
41struct check;
42
43typedef void (*tree_check_fn)(struct check *c, struct node *dt);
44typedef void (*node_check_fn)(struct check *c, struct node *dt, struct node *node);
45typedef void (*prop_check_fn)(struct check *c, struct node *dt,
46 struct node *node, struct property *prop);
47
48struct check {
49 const char *name;
50 tree_check_fn tree_fn;
51 node_check_fn node_fn;
52 prop_check_fn prop_fn;
53 void *data;
54 bool warn, error;
55 enum checkstatus status;
56 bool inprogress;
57 int num_prereqs;
58 struct check **prereq;
59};
60
61#define CHECK_ENTRY(nm, tfn, nfn, pfn, d, w, e, ...) \
62 static struct check *nm##_prereqs[] = { __VA_ARGS__ }; \
63 static struct check nm = { \
64 .name = #nm, \
65 .tree_fn = (tfn), \
66 .node_fn = (nfn), \
67 .prop_fn = (pfn), \
68 .data = (d), \
69 .warn = (w), \
70 .error = (e), \
71 .status = UNCHECKED, \
72 .num_prereqs = ARRAY_SIZE(nm##_prereqs), \
73 .prereq = nm##_prereqs, \
74 };
75#define WARNING(nm, tfn, nfn, pfn, d, ...) \
76 CHECK_ENTRY(nm, tfn, nfn, pfn, d, true, false, __VA_ARGS__)
77#define ERROR(nm, tfn, nfn, pfn, d, ...) \
78 CHECK_ENTRY(nm, tfn, nfn, pfn, d, false, true, __VA_ARGS__)
79#define CHECK(nm, tfn, nfn, pfn, d, ...) \
80 CHECK_ENTRY(nm, tfn, nfn, pfn, d, false, false, __VA_ARGS__)
81
82#define TREE_WARNING(nm, d, ...) \
83 WARNING(nm, check_##nm, NULL, NULL, d, __VA_ARGS__)
84#define TREE_ERROR(nm, d, ...) \
85 ERROR(nm, check_##nm, NULL, NULL, d, __VA_ARGS__)
86#define TREE_CHECK(nm, d, ...) \
87 CHECK(nm, check_##nm, NULL, NULL, d, __VA_ARGS__)
88#define NODE_WARNING(nm, d, ...) \
89 WARNING(nm, NULL, check_##nm, NULL, d, __VA_ARGS__)
90#define NODE_ERROR(nm, d, ...) \
91 ERROR(nm, NULL, check_##nm, NULL, d, __VA_ARGS__)
92#define NODE_CHECK(nm, d, ...) \
93 CHECK(nm, NULL, check_##nm, NULL, d, __VA_ARGS__)
94#define PROP_WARNING(nm, d, ...) \
95 WARNING(nm, NULL, NULL, check_##nm, d, __VA_ARGS__)
96#define PROP_ERROR(nm, d, ...) \
97 ERROR(nm, NULL, NULL, check_##nm, d, __VA_ARGS__)
98#define PROP_CHECK(nm, d, ...) \
99 CHECK(nm, NULL, NULL, check_##nm, d, __VA_ARGS__)
100
101#ifdef __GNUC__
102static inline void check_msg(struct check *c, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
103#endif
104static inline void check_msg(struct check *c, const char *fmt, ...)
105{
106 va_list ap;
107 va_start(ap, fmt);
108
109 if ((c->warn && (quiet < 1))
110 || (c->error && (quiet < 2))) {
111 fprintf(stderr, "%s (%s): ",
112 (c->error) ? "ERROR" : "Warning", c->name);
113 vfprintf(stderr, fmt, ap);
114 fprintf(stderr, "\n");
115 }
116 va_end(ap);
117}
118
119#define FAIL(c, ...) \
120 do { \
121 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
122 (c)->status = FAILED; \
123 check_msg((c), __VA_ARGS__); \
124 } while (0)
125
126static void check_nodes_props(struct check *c, struct node *dt, struct node *node)
127{
128 struct node *child;
129 struct property *prop;
130
131 TRACE(c, "%s", node->fullpath);
132 if (c->node_fn)
133 c->node_fn(c, dt, node);
134
135 if (c->prop_fn)
136 for_each_property(node, prop) {
137 TRACE(c, "%s\t'%s'", node->fullpath, prop->name);
138 c->prop_fn(c, dt, node, prop);
139 }
140
141 for_each_child(node, child)
142 check_nodes_props(c, dt, child);
143}
144
145static bool run_check(struct check *c, struct node *dt)
146{
147 bool error = false;
148 int i;
149
150 assert(!c->inprogress);
151
152 if (c->status != UNCHECKED)
153 goto out;
154
155 c->inprogress = true;
156
157 for (i = 0; i < c->num_prereqs; i++) {
158 struct check *prq = c->prereq[i];
159 error = error || run_check(prq, dt);
160 if (prq->status != PASSED) {
161 c->status = PREREQ;
162 check_msg(c, "Failed prerequisite '%s'",
163 c->prereq[i]->name);
164 }
165 }
166
167 if (c->status != UNCHECKED)
168 goto out;
169
170 if (c->node_fn || c->prop_fn)
171 check_nodes_props(c, dt, dt);
172
173 if (c->tree_fn)
174 c->tree_fn(c, dt);
175 if (c->status == UNCHECKED)
176 c->status = PASSED;
177
178 TRACE(c, "\tCompleted, status %d", c->status);
179
180out:
181 c->inprogress = false;
182 if ((c->status != PASSED) && (c->error))
183 error = true;
184 return error;
185}
186
187/*
188 * Utility check functions
189 */
190
191/* A check which always fails, for testing purposes only */
192static inline void check_always_fail(struct check *c, struct node *dt)
193{
194 FAIL(c, "always_fail check");
195}
196TREE_CHECK(always_fail, NULL);
197
198static void check_is_string(struct check *c, struct node *root,
199 struct node *node)
200{
201 struct property *prop;
202 char *propname = c->data;
203
204 prop = get_property(node, propname);
205 if (!prop)
206 return; /* Not present, assumed ok */
207
208 if (!data_is_one_string(prop->val))
209 FAIL(c, "\"%s\" property in %s is not a string",
210 propname, node->fullpath);
211}
212#define WARNING_IF_NOT_STRING(nm, propname) \
213 WARNING(nm, NULL, check_is_string, NULL, (propname))
214#define ERROR_IF_NOT_STRING(nm, propname) \
215 ERROR(nm, NULL, check_is_string, NULL, (propname))
216
217static void check_is_cell(struct check *c, struct node *root,
218 struct node *node)
219{
220 struct property *prop;
221 char *propname = c->data;
222
223 prop = get_property(node, propname);
224 if (!prop)
225 return; /* Not present, assumed ok */
226
227 if (prop->val.len != sizeof(cell_t))
228 FAIL(c, "\"%s\" property in %s is not a single cell",
229 propname, node->fullpath);
230}
231#define WARNING_IF_NOT_CELL(nm, propname) \
232 WARNING(nm, NULL, check_is_cell, NULL, (propname))
233#define ERROR_IF_NOT_CELL(nm, propname) \
234 ERROR(nm, NULL, check_is_cell, NULL, (propname))
235
236/*
237 * Structural check functions
238 */
239
240static void check_duplicate_node_names(struct check *c, struct node *dt,
241 struct node *node)
242{
243 struct node *child, *child2;
244
245 for_each_child(node, child)
246 for (child2 = child->next_sibling;
247 child2;
248 child2 = child2->next_sibling)
249 if (streq(child->name, child2->name))
250 FAIL(c, "Duplicate node name %s",
251 child->fullpath);
252}
253NODE_ERROR(duplicate_node_names, NULL);
254
255static void check_duplicate_property_names(struct check *c, struct node *dt,
256 struct node *node)
257{
258 struct property *prop, *prop2;
259
260 for_each_property(node, prop) {
261 for (prop2 = prop->next; prop2; prop2 = prop2->next) {
262 if (prop2->deleted)
263 continue;
264 if (streq(prop->name, prop2->name))
265 FAIL(c, "Duplicate property name %s in %s",
266 prop->name, node->fullpath);
267 }
268 }
269}
270NODE_ERROR(duplicate_property_names, NULL);
271
272#define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
273#define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
274#define DIGITS "0123456789"
275#define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
276
277static void check_node_name_chars(struct check *c, struct node *dt,
278 struct node *node)
279{
280 int n = strspn(node->name, c->data);
281
282 if (n < strlen(node->name))
283 FAIL(c, "Bad character '%c' in node %s",
284 node->name[n], node->fullpath);
285}
286NODE_ERROR(node_name_chars, PROPNODECHARS "@");
287
288static void check_node_name_format(struct check *c, struct node *dt,
289 struct node *node)
290{
291 if (strchr(get_unitname(node), '@'))
292 FAIL(c, "Node %s has multiple '@' characters in name",
293 node->fullpath);
294}
295NODE_ERROR(node_name_format, NULL, &node_name_chars);
296
297static void check_unit_address_vs_reg(struct check *c, struct node *dt,
298 struct node *node)
299{
300 const char *unitname = get_unitname(node);
301 struct property *prop = get_property(node, "reg");
302
303 if (!prop) {
304 prop = get_property(node, "ranges");
305 if (prop && !prop->val.len)
306 prop = NULL;
307 }
308
309 if (prop) {
310 if (!unitname[0])
311 FAIL(c, "Node %s has a reg or ranges property, but no unit name",
312 node->fullpath);
313 } else {
314 if (unitname[0])
315 FAIL(c, "Node %s has a unit name, but no reg property",
316 node->fullpath);
317 }
318}
319NODE_WARNING(unit_address_vs_reg, NULL);
320
321static void check_property_name_chars(struct check *c, struct node *dt,
322 struct node *node, struct property *prop)
323{
324 int n = strspn(prop->name, c->data);
325
326 if (n < strlen(prop->name))
327 FAIL(c, "Bad character '%c' in property name \"%s\", node %s",
328 prop->name[n], prop->name, node->fullpath);
329}
330PROP_ERROR(property_name_chars, PROPNODECHARS);
331
332#define DESCLABEL_FMT "%s%s%s%s%s"
333#define DESCLABEL_ARGS(node,prop,mark) \
334 ((mark) ? "value of " : ""), \
335 ((prop) ? "'" : ""), \
336 ((prop) ? (prop)->name : ""), \
337 ((prop) ? "' in " : ""), (node)->fullpath
338
339static void check_duplicate_label(struct check *c, struct node *dt,
340 const char *label, struct node *node,
341 struct property *prop, struct marker *mark)
342{
343 struct node *othernode = NULL;
344 struct property *otherprop = NULL;
345 struct marker *othermark = NULL;
346
347 othernode = get_node_by_label(dt, label);
348
349 if (!othernode)
350 otherprop = get_property_by_label(dt, label, &othernode);
351 if (!othernode)
352 othermark = get_marker_label(dt, label, &othernode,
353 &otherprop);
354
355 if (!othernode)
356 return;
357
358 if ((othernode != node) || (otherprop != prop) || (othermark != mark))
359 FAIL(c, "Duplicate label '%s' on " DESCLABEL_FMT
360 " and " DESCLABEL_FMT,
361 label, DESCLABEL_ARGS(node, prop, mark),
362 DESCLABEL_ARGS(othernode, otherprop, othermark));
363}
364
365static void check_duplicate_label_node(struct check *c, struct node *dt,
366 struct node *node)
367{
368 struct label *l;
369
370 for_each_label(node->labels, l)
371 check_duplicate_label(c, dt, l->label, node, NULL, NULL);
372}
373static void check_duplicate_label_prop(struct check *c, struct node *dt,
374 struct node *node, struct property *prop)
375{
376 struct marker *m = prop->val.markers;
377 struct label *l;
378
379 for_each_label(prop->labels, l)
380 check_duplicate_label(c, dt, l->label, node, prop, NULL);
381
382 for_each_marker_of_type(m, LABEL)
383 check_duplicate_label(c, dt, m->ref, node, prop, m);
384}
385ERROR(duplicate_label, NULL, check_duplicate_label_node,
386 check_duplicate_label_prop, NULL);
387
388static void check_explicit_phandles(struct check *c, struct node *root,
389 struct node *node, struct property *prop)
390{
391 struct marker *m;
392 struct node *other;
393 cell_t phandle;
394
395 if (!streq(prop->name, "phandle")
396 && !streq(prop->name, "linux,phandle"))
397 return;
398
399 if (prop->val.len != sizeof(cell_t)) {
400 FAIL(c, "%s has bad length (%d) %s property",
401 node->fullpath, prop->val.len, prop->name);
402 return;
403 }
404
405 m = prop->val.markers;
406 for_each_marker_of_type(m, REF_PHANDLE) {
407 assert(m->offset == 0);
408 if (node != get_node_by_ref(root, m->ref))
409 /* "Set this node's phandle equal to some
410 * other node's phandle". That's nonsensical
411 * by construction. */ {
412 FAIL(c, "%s in %s is a reference to another node",
413 prop->name, node->fullpath);
414 return;
415 }
416 /* But setting this node's phandle equal to its own
417 * phandle is allowed - that means allocate a unique
418 * phandle for this node, even if it's not otherwise
419 * referenced. The value will be filled in later, so
420 * no further checking for now. */
421 return;
422 }
423
424 phandle = propval_cell(prop);
425
426 if ((phandle == 0) || (phandle == -1)) {
427 FAIL(c, "%s has bad value (0x%x) in %s property",
428 node->fullpath, phandle, prop->name);
429 return;
430 }
431
432 if (node->phandle && (node->phandle != phandle))
433 FAIL(c, "%s has %s property which replaces existing phandle information",
434 node->fullpath, prop->name);
435
436 other = get_node_by_phandle(root, phandle);
437 if (other && (other != node)) {
438 FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)",
439 node->fullpath, phandle, other->fullpath);
440 return;
441 }
442
443 node->phandle = phandle;
444}
445PROP_ERROR(explicit_phandles, NULL);
446
447static void check_name_properties(struct check *c, struct node *root,
448 struct node *node)
449{
450 struct property **pp, *prop = NULL;
451
452 for (pp = &node->proplist; *pp; pp = &((*pp)->next))
453 if (streq((*pp)->name, "name")) {
454 prop = *pp;
455 break;
456 }
457
458 if (!prop)
459 return; /* No name property, that's fine */
460
461 if ((prop->val.len != node->basenamelen+1)
462 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
463 FAIL(c, "\"name\" property in %s is incorrect (\"%s\" instead"
464 " of base node name)", node->fullpath, prop->val.val);
465 } else {
466 /* The name property is correct, and therefore redundant.
467 * Delete it */
468 *pp = prop->next;
469 free(prop->name);
470 data_free(prop->val);
471 free(prop);
472 }
473}
474ERROR_IF_NOT_STRING(name_is_string, "name");
475NODE_ERROR(name_properties, NULL, &name_is_string);
476
477/*
478 * Reference fixup functions
479 */
480
481static void fixup_phandle_references(struct check *c, struct node *dt,
482 struct node *node, struct property *prop)
483{
484 struct marker *m = prop->val.markers;
485 struct node *refnode;
486 cell_t phandle;
487
488 for_each_marker_of_type(m, REF_PHANDLE) {
489 assert(m->offset + sizeof(cell_t) <= prop->val.len);
490
491 refnode = get_node_by_ref(dt, m->ref);
492 if (! refnode) {
493 FAIL(c, "Reference to non-existent node or label \"%s\"\n",
494 m->ref);
495 continue;
496 }
497
498 phandle = get_node_phandle(dt, refnode);
499 *((cell_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
500 }
501}
502ERROR(phandle_references, NULL, NULL, fixup_phandle_references, NULL,
503 &duplicate_node_names, &explicit_phandles);
504
505static void fixup_path_references(struct check *c, struct node *dt,
506 struct node *node, struct property *prop)
507{
508 struct marker *m = prop->val.markers;
509 struct node *refnode;
510 char *path;
511
512 for_each_marker_of_type(m, REF_PATH) {
513 assert(m->offset <= prop->val.len);
514
515 refnode = get_node_by_ref(dt, m->ref);
516 if (!refnode) {
517 FAIL(c, "Reference to non-existent node or label \"%s\"\n",
518 m->ref);
519 continue;
520 }
521
522 path = refnode->fullpath;
523 prop->val = data_insert_at_marker(prop->val, m, path,
524 strlen(path) + 1);
525 }
526}
527ERROR(path_references, NULL, NULL, fixup_path_references, NULL,
528 &duplicate_node_names);
529
530/*
531 * Semantic checks
532 */
533WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
534WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
535WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
536
537WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
538WARNING_IF_NOT_STRING(model_is_string, "model");
539WARNING_IF_NOT_STRING(status_is_string, "status");
540
541static void fixup_addr_size_cells(struct check *c, struct node *dt,
542 struct node *node)
543{
544 struct property *prop;
545
546 node->addr_cells = -1;
547 node->size_cells = -1;
548
549 prop = get_property(node, "#address-cells");
550 if (prop)
551 node->addr_cells = propval_cell(prop);
552
553 prop = get_property(node, "#size-cells");
554 if (prop)
555 node->size_cells = propval_cell(prop);
556}
557WARNING(addr_size_cells, NULL, fixup_addr_size_cells, NULL, NULL,
558 &address_cells_is_cell, &size_cells_is_cell);
559
560#define node_addr_cells(n) \
561 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
562#define node_size_cells(n) \
563 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
564
565static void check_reg_format(struct check *c, struct node *dt,
566 struct node *node)
567{
568 struct property *prop;
569 int addr_cells, size_cells, entrylen;
570
571 prop = get_property(node, "reg");
572 if (!prop)
573 return; /* No "reg", that's fine */
574
575 if (!node->parent) {
576 FAIL(c, "Root node has a \"reg\" property");
577 return;
578 }
579
580 if (prop->val.len == 0)
581 FAIL(c, "\"reg\" property in %s is empty", node->fullpath);
582
583 addr_cells = node_addr_cells(node->parent);
584 size_cells = node_size_cells(node->parent);
585 entrylen = (addr_cells + size_cells) * sizeof(cell_t);
586
587 if (!entrylen || (prop->val.len % entrylen) != 0)
588 FAIL(c, "\"reg\" property in %s has invalid length (%d bytes) "
589 "(#address-cells == %d, #size-cells == %d)",
590 node->fullpath, prop->val.len, addr_cells, size_cells);
591}
592NODE_WARNING(reg_format, NULL, &addr_size_cells);
593
594static void check_ranges_format(struct check *c, struct node *dt,
595 struct node *node)
596{
597 struct property *prop;
598 int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
599
600 prop = get_property(node, "ranges");
601 if (!prop)
602 return;
603
604 if (!node->parent) {
605 FAIL(c, "Root node has a \"ranges\" property");
606 return;
607 }
608
609 p_addr_cells = node_addr_cells(node->parent);
610 p_size_cells = node_size_cells(node->parent);
611 c_addr_cells = node_addr_cells(node);
612 c_size_cells = node_size_cells(node);
613 entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
614
615 if (prop->val.len == 0) {
616 if (p_addr_cells != c_addr_cells)
617 FAIL(c, "%s has empty \"ranges\" property but its "
618 "#address-cells (%d) differs from %s (%d)",
619 node->fullpath, c_addr_cells, node->parent->fullpath,
620 p_addr_cells);
621 if (p_size_cells != c_size_cells)
622 FAIL(c, "%s has empty \"ranges\" property but its "
623 "#size-cells (%d) differs from %s (%d)",
624 node->fullpath, c_size_cells, node->parent->fullpath,
625 p_size_cells);
626 } else if ((prop->val.len % entrylen) != 0) {
627 FAIL(c, "\"ranges\" property in %s has invalid length (%d bytes) "
628 "(parent #address-cells == %d, child #address-cells == %d, "
629 "#size-cells == %d)", node->fullpath, prop->val.len,
630 p_addr_cells, c_addr_cells, c_size_cells);
631 }
632}
633NODE_WARNING(ranges_format, NULL, &addr_size_cells);
634
635/*
636 * Style checks
637 */
638static void check_avoid_default_addr_size(struct check *c, struct node *dt,
639 struct node *node)
640{
641 struct property *reg, *ranges;
642
643 if (!node->parent)
644 return; /* Ignore root node */
645
646 reg = get_property(node, "reg");
647 ranges = get_property(node, "ranges");
648
649 if (!reg && !ranges)
650 return;
651
652 if (node->parent->addr_cells == -1)
653 FAIL(c, "Relying on default #address-cells value for %s",
654 node->fullpath);
655
656 if (node->parent->size_cells == -1)
657 FAIL(c, "Relying on default #size-cells value for %s",
658 node->fullpath);
659}
660NODE_WARNING(avoid_default_addr_size, NULL, &addr_size_cells);
661
662static void check_obsolete_chosen_interrupt_controller(struct check *c,
663 struct node *dt)
664{
665 struct node *chosen;
666 struct property *prop;
667
668 chosen = get_node_by_path(dt, "/chosen");
669 if (!chosen)
670 return;
671
672 prop = get_property(chosen, "interrupt-controller");
673 if (prop)
674 FAIL(c, "/chosen has obsolete \"interrupt-controller\" "
675 "property");
676}
677TREE_WARNING(obsolete_chosen_interrupt_controller, NULL);
678
679static struct check *check_table[] = {
680 &duplicate_node_names, &duplicate_property_names,
681 &node_name_chars, &node_name_format, &property_name_chars,
682 &name_is_string, &name_properties,
683
684 &duplicate_label,
685
686 &explicit_phandles,
687 &phandle_references, &path_references,
688
689 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
690 &device_type_is_string, &model_is_string, &status_is_string,
691
692 &addr_size_cells, ®_format, &ranges_format,
693
694 &unit_address_vs_reg,
695
696 &avoid_default_addr_size,
697 &obsolete_chosen_interrupt_controller,
698
699 &always_fail,
700};
701
702static void enable_warning_error(struct check *c, bool warn, bool error)
703{
704 int i;
705
706 /* Raising level, also raise it for prereqs */
707 if ((warn && !c->warn) || (error && !c->error))
708 for (i = 0; i < c->num_prereqs; i++)
709 enable_warning_error(c->prereq[i], warn, error);
710
711 c->warn = c->warn || warn;
712 c->error = c->error || error;
713}
714
715static void disable_warning_error(struct check *c, bool warn, bool error)
716{
717 int i;
718
719 /* Lowering level, also lower it for things this is the prereq
720 * for */
721 if ((warn && c->warn) || (error && c->error)) {
722 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
723 struct check *cc = check_table[i];
724 int j;
725
726 for (j = 0; j < cc->num_prereqs; j++)
727 if (cc->prereq[j] == c)
728 disable_warning_error(cc, warn, error);
729 }
730 }
731
732 c->warn = c->warn && !warn;
733 c->error = c->error && !error;
734}
735
736void parse_checks_option(bool warn, bool error, const char *arg)
737{
738 int i;
739 const char *name = arg;
740 bool enable = true;
741
742 if ((strncmp(arg, "no-", 3) == 0)
743 || (strncmp(arg, "no_", 3) == 0)) {
744 name = arg + 3;
745 enable = false;
746 }
747
748 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
749 struct check *c = check_table[i];
750
751 if (streq(c->name, name)) {
752 if (enable)
753 enable_warning_error(c, warn, error);
754 else
755 disable_warning_error(c, warn, error);
756 return;
757 }
758 }
759
760 die("Unrecognized check name \"%s\"\n", name);
761}
762
763void process_checks(bool force, struct boot_info *bi)
764{
765 struct node *dt = bi->dt;
766 int i;
767 int error = 0;
768
769 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
770 struct check *c = check_table[i];
771
772 if (c->warn || c->error)
773 error = error || run_check(c, dt);
774 }
775
776 if (error) {
777 if (!force) {
778 fprintf(stderr, "ERROR: Input tree has errors, aborting "
779 "(use -f to force output)\n");
780 exit(2);
781 } else if (quiet < 3) {
782 fprintf(stderr, "Warning: Input tree has errors, "
783 "output forced\n");
784 }
785 }
786}