2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2007.
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.
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.
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
24 #define TRACE(c, ...) \
26 fprintf(stderr, "=== %s: ", (c)->name); \
27 fprintf(stderr, __VA_ARGS__); \
28 fprintf(stderr, "\n"); \
31 #define TRACE(c, fmt, ...) do { } while (0)
43 typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
50 enum checkstatus status;
53 struct check **prereq;
56 #define CHECK_ENTRY(_nm, _fn, _d, _w, _e, ...) \
57 static struct check *_nm##_prereqs[] = { __VA_ARGS__ }; \
58 static struct check _nm = { \
64 .status = UNCHECKED, \
65 .num_prereqs = ARRAY_SIZE(_nm##_prereqs), \
66 .prereq = _nm##_prereqs, \
68 #define WARNING(_nm, _fn, _d, ...) \
69 CHECK_ENTRY(_nm, _fn, _d, true, false, __VA_ARGS__)
70 #define ERROR(_nm, _fn, _d, ...) \
71 CHECK_ENTRY(_nm, _fn, _d, false, true, __VA_ARGS__)
72 #define CHECK(_nm, _fn, _d, ...) \
73 CHECK_ENTRY(_nm, _fn, _d, false, false, __VA_ARGS__)
76 static inline void check_msg(struct check *c, struct dt_info *dti,
77 const char *fmt, ...) __attribute__((format (printf, 3, 4)));
79 static inline void check_msg(struct check *c, struct dt_info *dti,
85 if ((c->warn && (quiet < 1))
86 || (c->error && (quiet < 2))) {
87 fprintf(stderr, "%s: %s (%s): ",
88 strcmp(dti->outname, "-") ? dti->outname : "<stdout>",
89 (c->error) ? "ERROR" : "Warning", c->name);
90 vfprintf(stderr, fmt, ap);
91 fprintf(stderr, "\n");
96 #define FAIL(c, dti, ...) \
98 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
99 (c)->status = FAILED; \
100 check_msg((c), dti, __VA_ARGS__); \
103 static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
107 TRACE(c, "%s", node->fullpath);
111 for_each_child(node, child)
112 check_nodes_props(c, dti, child);
115 static bool run_check(struct check *c, struct dt_info *dti)
117 struct node *dt = dti->dt;
121 assert(!c->inprogress);
123 if (c->status != UNCHECKED)
126 c->inprogress = true;
128 for (i = 0; i < c->num_prereqs; i++) {
129 struct check *prq = c->prereq[i];
130 error = error || run_check(prq, dti);
131 if (prq->status != PASSED) {
133 check_msg(c, dti, "Failed prerequisite '%s'",
138 if (c->status != UNCHECKED)
141 check_nodes_props(c, dti, dt);
143 if (c->status == UNCHECKED)
146 TRACE(c, "\tCompleted, status %d", c->status);
149 c->inprogress = false;
150 if ((c->status != PASSED) && (c->error))
156 * Utility check functions
159 /* A check which always fails, for testing purposes only */
160 static inline void check_always_fail(struct check *c, struct dt_info *dti,
163 FAIL(c, dti, "always_fail check");
165 CHECK(always_fail, check_always_fail, NULL);
167 static void check_is_string(struct check *c, struct dt_info *dti,
170 struct property *prop;
171 char *propname = c->data;
173 prop = get_property(node, propname);
175 return; /* Not present, assumed ok */
177 if (!data_is_one_string(prop->val))
178 FAIL(c, dti, "\"%s\" property in %s is not a string",
179 propname, node->fullpath);
181 #define WARNING_IF_NOT_STRING(nm, propname) \
182 WARNING(nm, check_is_string, (propname))
183 #define ERROR_IF_NOT_STRING(nm, propname) \
184 ERROR(nm, check_is_string, (propname))
186 static void check_is_cell(struct check *c, struct dt_info *dti,
189 struct property *prop;
190 char *propname = c->data;
192 prop = get_property(node, propname);
194 return; /* Not present, assumed ok */
196 if (prop->val.len != sizeof(cell_t))
197 FAIL(c, dti, "\"%s\" property in %s is not a single cell",
198 propname, node->fullpath);
200 #define WARNING_IF_NOT_CELL(nm, propname) \
201 WARNING(nm, check_is_cell, (propname))
202 #define ERROR_IF_NOT_CELL(nm, propname) \
203 ERROR(nm, check_is_cell, (propname))
206 * Structural check functions
209 static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
212 struct node *child, *child2;
214 for_each_child(node, child)
215 for (child2 = child->next_sibling;
217 child2 = child2->next_sibling)
218 if (streq(child->name, child2->name))
219 FAIL(c, dti, "Duplicate node name %s",
222 ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
224 static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
227 struct property *prop, *prop2;
229 for_each_property(node, prop) {
230 for (prop2 = prop->next; prop2; prop2 = prop2->next) {
233 if (streq(prop->name, prop2->name))
234 FAIL(c, dti, "Duplicate property name %s in %s",
235 prop->name, node->fullpath);
239 ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
241 #define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
242 #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
243 #define DIGITS "0123456789"
244 #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
245 #define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
247 static void check_node_name_chars(struct check *c, struct dt_info *dti,
250 int n = strspn(node->name, c->data);
252 if (n < strlen(node->name))
253 FAIL(c, dti, "Bad character '%c' in node %s",
254 node->name[n], node->fullpath);
256 ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
258 static void check_node_name_chars_strict(struct check *c, struct dt_info *dti,
261 int n = strspn(node->name, c->data);
263 if (n < node->basenamelen)
264 FAIL(c, dti, "Character '%c' not recommended in node %s",
265 node->name[n], node->fullpath);
267 CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
269 static void check_node_name_format(struct check *c, struct dt_info *dti,
272 if (strchr(get_unitname(node), '@'))
273 FAIL(c, dti, "Node %s has multiple '@' characters in name",
276 ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
278 static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
281 const char *unitname = get_unitname(node);
282 struct property *prop = get_property(node, "reg");
285 prop = get_property(node, "ranges");
286 if (prop && !prop->val.len)
292 FAIL(c, dti, "Node %s has a reg or ranges property, but no unit name",
296 FAIL(c, dti, "Node %s has a unit name, but no reg property",
300 WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
302 static void check_property_name_chars(struct check *c, struct dt_info *dti,
305 struct property *prop;
307 for_each_property(node, prop) {
308 int n = strspn(prop->name, c->data);
310 if (n < strlen(prop->name))
311 FAIL(c, dti, "Bad character '%c' in property name \"%s\", node %s",
312 prop->name[n], prop->name, node->fullpath);
315 ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
317 static void check_property_name_chars_strict(struct check *c,
321 struct property *prop;
323 for_each_property(node, prop) {
324 const char *name = prop->name;
325 int n = strspn(name, c->data);
327 if (n == strlen(prop->name))
330 /* Certain names are whitelisted */
331 if (streq(name, "device_type"))
335 * # is only allowed at the beginning of property names not counting
338 if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) {
340 n = strspn(name, c->data);
342 if (n < strlen(name))
343 FAIL(c, dti, "Character '%c' not recommended in property name \"%s\", node %s",
344 name[n], prop->name, node->fullpath);
347 CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
349 #define DESCLABEL_FMT "%s%s%s%s%s"
350 #define DESCLABEL_ARGS(node,prop,mark) \
351 ((mark) ? "value of " : ""), \
352 ((prop) ? "'" : ""), \
353 ((prop) ? (prop)->name : ""), \
354 ((prop) ? "' in " : ""), (node)->fullpath
356 static void check_duplicate_label(struct check *c, struct dt_info *dti,
357 const char *label, struct node *node,
358 struct property *prop, struct marker *mark)
360 struct node *dt = dti->dt;
361 struct node *othernode = NULL;
362 struct property *otherprop = NULL;
363 struct marker *othermark = NULL;
365 othernode = get_node_by_label(dt, label);
368 otherprop = get_property_by_label(dt, label, &othernode);
370 othermark = get_marker_label(dt, label, &othernode,
376 if ((othernode != node) || (otherprop != prop) || (othermark != mark))
377 FAIL(c, dti, "Duplicate label '%s' on " DESCLABEL_FMT
378 " and " DESCLABEL_FMT,
379 label, DESCLABEL_ARGS(node, prop, mark),
380 DESCLABEL_ARGS(othernode, otherprop, othermark));
383 static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
387 struct property *prop;
389 for_each_label(node->labels, l)
390 check_duplicate_label(c, dti, l->label, node, NULL, NULL);
392 for_each_property(node, prop) {
393 struct marker *m = prop->val.markers;
395 for_each_label(prop->labels, l)
396 check_duplicate_label(c, dti, l->label, node, prop, NULL);
398 for_each_marker_of_type(m, LABEL)
399 check_duplicate_label(c, dti, m->ref, node, prop, m);
402 ERROR(duplicate_label, check_duplicate_label_node, NULL);
404 static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
405 struct node *node, const char *propname)
407 struct node *root = dti->dt;
408 struct property *prop;
412 prop = get_property(node, propname);
416 if (prop->val.len != sizeof(cell_t)) {
417 FAIL(c, dti, "%s has bad length (%d) %s property",
418 node->fullpath, prop->val.len, prop->name);
422 m = prop->val.markers;
423 for_each_marker_of_type(m, REF_PHANDLE) {
424 assert(m->offset == 0);
425 if (node != get_node_by_ref(root, m->ref))
426 /* "Set this node's phandle equal to some
427 * other node's phandle". That's nonsensical
428 * by construction. */ {
429 FAIL(c, dti, "%s in %s is a reference to another node",
430 prop->name, node->fullpath);
432 /* But setting this node's phandle equal to its own
433 * phandle is allowed - that means allocate a unique
434 * phandle for this node, even if it's not otherwise
435 * referenced. The value will be filled in later, so
436 * we treat it as having no phandle data for now. */
440 phandle = propval_cell(prop);
442 if ((phandle == 0) || (phandle == -1)) {
443 FAIL(c, dti, "%s has bad value (0x%x) in %s property",
444 node->fullpath, phandle, prop->name);
451 static void check_explicit_phandles(struct check *c, struct dt_info *dti,
454 struct node *root = dti->dt;
456 cell_t phandle, linux_phandle;
458 /* Nothing should have assigned phandles yet */
459 assert(!node->phandle);
461 phandle = check_phandle_prop(c, dti, node, "phandle");
463 linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");
465 if (!phandle && !linux_phandle)
466 /* No valid phandles; nothing further to check */
469 if (linux_phandle && phandle && (phandle != linux_phandle))
470 FAIL(c, dti, "%s has mismatching 'phandle' and 'linux,phandle'"
471 " properties", node->fullpath);
473 if (linux_phandle && !phandle)
474 phandle = linux_phandle;
476 other = get_node_by_phandle(root, phandle);
477 if (other && (other != node)) {
478 FAIL(c, dti, "%s has duplicated phandle 0x%x (seen before at %s)",
479 node->fullpath, phandle, other->fullpath);
483 node->phandle = phandle;
485 ERROR(explicit_phandles, check_explicit_phandles, NULL);
487 static void check_name_properties(struct check *c, struct dt_info *dti,
490 struct property **pp, *prop = NULL;
492 for (pp = &node->proplist; *pp; pp = &((*pp)->next))
493 if (streq((*pp)->name, "name")) {
499 return; /* No name property, that's fine */
501 if ((prop->val.len != node->basenamelen+1)
502 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
503 FAIL(c, dti, "\"name\" property in %s is incorrect (\"%s\" instead"
504 " of base node name)", node->fullpath, prop->val.val);
506 /* The name property is correct, and therefore redundant.
510 data_free(prop->val);
514 ERROR_IF_NOT_STRING(name_is_string, "name");
515 ERROR(name_properties, check_name_properties, NULL, &name_is_string);
518 * Reference fixup functions
521 static void fixup_phandle_references(struct check *c, struct dt_info *dti,
524 struct node *dt = dti->dt;
525 struct property *prop;
527 for_each_property(node, prop) {
528 struct marker *m = prop->val.markers;
529 struct node *refnode;
532 for_each_marker_of_type(m, REF_PHANDLE) {
533 assert(m->offset + sizeof(cell_t) <= prop->val.len);
535 refnode = get_node_by_ref(dt, m->ref);
537 if (!(dti->dtsflags & DTSF_PLUGIN))
538 FAIL(c, dti, "Reference to non-existent node or "
539 "label \"%s\"\n", m->ref);
540 else /* mark the entry as unresolved */
541 *((cell_t *)(prop->val.val + m->offset)) =
542 cpu_to_fdt32(0xffffffff);
546 phandle = get_node_phandle(dt, refnode);
547 *((cell_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
551 ERROR(phandle_references, fixup_phandle_references, NULL,
552 &duplicate_node_names, &explicit_phandles);
554 static void fixup_path_references(struct check *c, struct dt_info *dti,
557 struct node *dt = dti->dt;
558 struct property *prop;
560 for_each_property(node, prop) {
561 struct marker *m = prop->val.markers;
562 struct node *refnode;
565 for_each_marker_of_type(m, REF_PATH) {
566 assert(m->offset <= prop->val.len);
568 refnode = get_node_by_ref(dt, m->ref);
570 FAIL(c, dti, "Reference to non-existent node or label \"%s\"\n",
575 path = refnode->fullpath;
576 prop->val = data_insert_at_marker(prop->val, m, path,
581 ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
586 WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
587 WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
588 WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
590 WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
591 WARNING_IF_NOT_STRING(model_is_string, "model");
592 WARNING_IF_NOT_STRING(status_is_string, "status");
594 static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
597 struct property *prop;
599 node->addr_cells = -1;
600 node->size_cells = -1;
602 prop = get_property(node, "#address-cells");
604 node->addr_cells = propval_cell(prop);
606 prop = get_property(node, "#size-cells");
608 node->size_cells = propval_cell(prop);
610 WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
611 &address_cells_is_cell, &size_cells_is_cell);
613 #define node_addr_cells(n) \
614 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
615 #define node_size_cells(n) \
616 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
618 static void check_reg_format(struct check *c, struct dt_info *dti,
621 struct property *prop;
622 int addr_cells, size_cells, entrylen;
624 prop = get_property(node, "reg");
626 return; /* No "reg", that's fine */
629 FAIL(c, dti, "Root node has a \"reg\" property");
633 if (prop->val.len == 0)
634 FAIL(c, dti, "\"reg\" property in %s is empty", node->fullpath);
636 addr_cells = node_addr_cells(node->parent);
637 size_cells = node_size_cells(node->parent);
638 entrylen = (addr_cells + size_cells) * sizeof(cell_t);
640 if (!entrylen || (prop->val.len % entrylen) != 0)
641 FAIL(c, dti, "\"reg\" property in %s has invalid length (%d bytes) "
642 "(#address-cells == %d, #size-cells == %d)",
643 node->fullpath, prop->val.len, addr_cells, size_cells);
645 WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
647 static void check_ranges_format(struct check *c, struct dt_info *dti,
650 struct property *prop;
651 int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
653 prop = get_property(node, "ranges");
658 FAIL(c, dti, "Root node has a \"ranges\" property");
662 p_addr_cells = node_addr_cells(node->parent);
663 p_size_cells = node_size_cells(node->parent);
664 c_addr_cells = node_addr_cells(node);
665 c_size_cells = node_size_cells(node);
666 entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
668 if (prop->val.len == 0) {
669 if (p_addr_cells != c_addr_cells)
670 FAIL(c, dti, "%s has empty \"ranges\" property but its "
671 "#address-cells (%d) differs from %s (%d)",
672 node->fullpath, c_addr_cells, node->parent->fullpath,
674 if (p_size_cells != c_size_cells)
675 FAIL(c, dti, "%s has empty \"ranges\" property but its "
676 "#size-cells (%d) differs from %s (%d)",
677 node->fullpath, c_size_cells, node->parent->fullpath,
679 } else if ((prop->val.len % entrylen) != 0) {
680 FAIL(c, dti, "\"ranges\" property in %s has invalid length (%d bytes) "
681 "(parent #address-cells == %d, child #address-cells == %d, "
682 "#size-cells == %d)", node->fullpath, prop->val.len,
683 p_addr_cells, c_addr_cells, c_size_cells);
686 WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
691 static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
694 struct property *reg, *ranges;
697 return; /* Ignore root node */
699 reg = get_property(node, "reg");
700 ranges = get_property(node, "ranges");
705 if (node->parent->addr_cells == -1)
706 FAIL(c, dti, "Relying on default #address-cells value for %s",
709 if (node->parent->size_cells == -1)
710 FAIL(c, dti, "Relying on default #size-cells value for %s",
713 WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
716 static void check_obsolete_chosen_interrupt_controller(struct check *c,
720 struct node *dt = dti->dt;
722 struct property *prop;
728 chosen = get_node_by_path(dt, "/chosen");
732 prop = get_property(chosen, "interrupt-controller");
734 FAIL(c, dti, "/chosen has obsolete \"interrupt-controller\" "
737 WARNING(obsolete_chosen_interrupt_controller,
738 check_obsolete_chosen_interrupt_controller, NULL);
740 static struct check *check_table[] = {
741 &duplicate_node_names, &duplicate_property_names,
742 &node_name_chars, &node_name_format, &property_name_chars,
743 &name_is_string, &name_properties,
748 &phandle_references, &path_references,
750 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
751 &device_type_is_string, &model_is_string, &status_is_string,
753 &property_name_chars_strict,
754 &node_name_chars_strict,
756 &addr_size_cells, ®_format, &ranges_format,
758 &unit_address_vs_reg,
760 &avoid_default_addr_size,
761 &obsolete_chosen_interrupt_controller,
766 static void enable_warning_error(struct check *c, bool warn, bool error)
770 /* Raising level, also raise it for prereqs */
771 if ((warn && !c->warn) || (error && !c->error))
772 for (i = 0; i < c->num_prereqs; i++)
773 enable_warning_error(c->prereq[i], warn, error);
775 c->warn = c->warn || warn;
776 c->error = c->error || error;
779 static void disable_warning_error(struct check *c, bool warn, bool error)
783 /* Lowering level, also lower it for things this is the prereq
785 if ((warn && c->warn) || (error && c->error)) {
786 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
787 struct check *cc = check_table[i];
790 for (j = 0; j < cc->num_prereqs; j++)
791 if (cc->prereq[j] == c)
792 disable_warning_error(cc, warn, error);
796 c->warn = c->warn && !warn;
797 c->error = c->error && !error;
800 void parse_checks_option(bool warn, bool error, const char *arg)
803 const char *name = arg;
806 if ((strncmp(arg, "no-", 3) == 0)
807 || (strncmp(arg, "no_", 3) == 0)) {
812 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
813 struct check *c = check_table[i];
815 if (streq(c->name, name)) {
817 enable_warning_error(c, warn, error);
819 disable_warning_error(c, warn, error);
824 die("Unrecognized check name \"%s\"\n", name);
827 void process_checks(bool force, struct dt_info *dti)
832 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
833 struct check *c = check_table[i];
835 if (c->warn || c->error)
836 error = error || run_check(c, dti);
841 fprintf(stderr, "ERROR: Input tree has errors, aborting "
842 "(use -f to force output)\n");
844 } else if (quiet < 3) {
845 fprintf(stderr, "Warning: Input tree has errors, "