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)
49 typedef void (*tree_check_fn)(struct check *c, struct node *dt);
50 typedef void (*node_check_fn)(struct check *c, struct node *dt, struct node *node);
51 typedef void (*prop_check_fn)(struct check *c, struct node *dt,
52 struct node *node, struct property *prop);
56 tree_check_fn tree_fn;
57 node_check_fn node_fn;
58 prop_check_fn prop_fn;
60 enum checklevel level;
61 enum checkstatus status;
64 struct check **prereq;
67 #define CHECK(nm, tfn, nfn, pfn, d, lvl, ...) \
68 static struct check *nm##_prereqs[] = { __VA_ARGS__ }; \
69 static struct check nm = { \
76 .status = UNCHECKED, \
77 .num_prereqs = ARRAY_SIZE(nm##_prereqs), \
78 .prereq = nm##_prereqs, \
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__)
91 static inline void check_msg(struct check *c, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
93 static inline void check_msg(struct check *c, const char *fmt, ...)
98 if ((c->level < WARN) || (c->level <= quiet))
99 return; /* Suppress message */
101 fprintf(stderr, "%s (%s): ",
102 (c->level == ERROR) ? "ERROR" : "Warning", c->name);
103 vfprintf(stderr, fmt, ap);
104 fprintf(stderr, "\n");
107 #define FAIL(c, ...) \
109 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
110 (c)->status = FAILED; \
111 check_msg((c), __VA_ARGS__); \
114 static void check_nodes_props(struct check *c, struct node *dt, struct node *node)
117 struct property *prop;
119 TRACE(c, "%s", node->fullpath);
121 c->node_fn(c, dt, node);
124 for_each_property(node, prop) {
125 TRACE(c, "%s\t'%s'", node->fullpath, prop->name);
126 c->prop_fn(c, dt, node, prop);
129 for_each_child(node, child)
130 check_nodes_props(c, dt, child);
133 static int run_check(struct check *c, struct node *dt)
138 assert(!c->inprogress);
140 if (c->status != UNCHECKED)
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) {
150 check_msg(c, "Failed prerequisite '%s'",
155 if (c->status != UNCHECKED)
158 if (c->node_fn || c->prop_fn)
159 check_nodes_props(c, dt, dt);
163 if (c->status == UNCHECKED)
166 TRACE(c, "\tCompleted, status %d", c->status);
170 if ((c->status != PASSED) && (c->level == ERROR))
176 * Utility check functions
179 static void check_is_string(struct check *c, struct node *root,
182 struct property *prop;
183 char *propname = c->data;
185 prop = get_property(node, propname);
187 return; /* Not present, assumed ok */
189 if (!data_is_one_string(prop->val))
190 FAIL(c, "\"%s\" property in %s is not a string",
191 propname, node->fullpath);
193 #define CHECK_IS_STRING(nm, propname, lvl) \
194 CHECK(nm, NULL, check_is_string, NULL, (propname), (lvl))
196 static void check_is_cell(struct check *c, struct node *root,
199 struct property *prop;
200 char *propname = c->data;
202 prop = get_property(node, propname);
204 return; /* Not present, assumed ok */
206 if (prop->val.len != sizeof(cell_t))
207 FAIL(c, "\"%s\" property in %s is not a single cell",
208 propname, node->fullpath);
210 #define CHECK_IS_CELL(nm, propname, lvl) \
211 CHECK(nm, NULL, check_is_cell, NULL, (propname), (lvl))
214 * Structural check functions
217 static void check_duplicate_node_names(struct check *c, struct node *dt,
220 struct node *child, *child2;
222 for_each_child(node, child)
223 for (child2 = child->next_sibling;
225 child2 = child2->next_sibling)
226 if (streq(child->name, child2->name))
227 FAIL(c, "Duplicate node name %s",
230 NODE_CHECK(duplicate_node_names, NULL, ERROR);
232 static void check_duplicate_property_names(struct check *c, struct node *dt,
235 struct property *prop, *prop2;
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);
243 NODE_CHECK(duplicate_property_names, NULL, ERROR);
245 static void check_explicit_phandles(struct check *c, struct node *root,
248 struct property *prop;
252 prop = get_property(node, "linux,phandle");
254 return; /* No phandle, that's fine */
256 if (prop->val.len != sizeof(cell_t)) {
257 FAIL(c, "%s has bad length (%d) linux,phandle property",
258 node->fullpath, prop->val.len);
262 phandle = propval_cell(prop);
263 if ((phandle == 0) || (phandle == -1)) {
264 FAIL(c, "%s has invalid linux,phandle value 0x%x",
265 node->fullpath, phandle);
269 other = get_node_by_phandle(root, phandle);
271 FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)",
272 node->fullpath, phandle, other->fullpath);
276 node->phandle = phandle;
278 NODE_CHECK(explicit_phandles, NULL, ERROR);
280 static void check_name_properties(struct check *c, struct node *root,
283 struct property *prop;
285 prop = get_property(node, "name");
287 return; /* No name property, that's fine */
289 if ((prop->val.len != node->basenamelen+1)
290 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0))
291 FAIL(c, "\"name\" property in %s is incorrect (\"%s\" instead"
292 " of base node name)", node->fullpath, prop->val.val);
294 CHECK_IS_STRING(name_is_string, "name", ERROR);
295 NODE_CHECK(name_properties, NULL, ERROR, &name_is_string);
298 * Reference fixup functions
301 static void fixup_phandle_references(struct check *c, struct node *dt,
302 struct node *node, struct property *prop)
304 struct marker *m = prop->val.markers;
305 struct node *refnode;
308 for_each_marker_of_type(m, REF_PHANDLE) {
309 assert(m->offset + sizeof(cell_t) <= prop->val.len);
311 refnode = get_node_by_ref(dt, m->ref);
313 FAIL(c, "Reference to non-existent node or label \"%s\"\n",
318 phandle = get_node_phandle(dt, refnode);
319 *((cell_t *)(prop->val.val + m->offset)) = cpu_to_be32(phandle);
322 CHECK(phandle_references, NULL, NULL, fixup_phandle_references, NULL, ERROR,
323 &duplicate_node_names, &explicit_phandles);
325 static void fixup_path_references(struct check *c, struct node *dt,
326 struct node *node, struct property *prop)
328 struct marker *m = prop->val.markers;
329 struct node *refnode;
332 for_each_marker_of_type(m, REF_PATH) {
333 assert(m->offset <= prop->val.len);
335 refnode = get_node_by_ref(dt, m->ref);
337 FAIL(c, "Reference to non-existent node or label \"%s\"\n",
342 path = refnode->fullpath;
343 prop->val = data_insert_at_marker(prop->val, m, path,
347 CHECK(path_references, NULL, NULL, fixup_path_references, NULL, ERROR,
348 &duplicate_node_names);
353 CHECK_IS_CELL(address_cells_is_cell, "#address-cells", WARN);
354 CHECK_IS_CELL(size_cells_is_cell, "#size-cells", WARN);
355 CHECK_IS_CELL(interrupt_cells_is_cell, "#interrupt-cells", WARN);
357 CHECK_IS_STRING(device_type_is_string, "device_type", WARN);
358 CHECK_IS_STRING(model_is_string, "model", WARN);
359 CHECK_IS_STRING(status_is_string, "status", WARN);
361 static void fixup_addr_size_cells(struct check *c, struct node *dt,
364 struct property *prop;
366 node->addr_cells = -1;
367 node->size_cells = -1;
369 prop = get_property(node, "#address-cells");
371 node->addr_cells = propval_cell(prop);
373 prop = get_property(node, "#size-cells");
375 node->size_cells = propval_cell(prop);
377 CHECK(addr_size_cells, NULL, fixup_addr_size_cells, NULL, NULL, WARN,
378 &address_cells_is_cell, &size_cells_is_cell);
380 #define node_addr_cells(n) \
381 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
382 #define node_size_cells(n) \
383 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
385 static void check_reg_format(struct check *c, struct node *dt,
388 struct property *prop;
389 int addr_cells, size_cells, entrylen;
391 prop = get_property(node, "reg");
393 return; /* No "reg", that's fine */
396 FAIL(c, "Root node has a \"reg\" property");
400 if (prop->val.len == 0)
401 FAIL(c, "\"reg\" property in %s is empty", node->fullpath);
403 addr_cells = node_addr_cells(node->parent);
404 size_cells = node_size_cells(node->parent);
405 entrylen = (addr_cells + size_cells) * sizeof(cell_t);
407 if ((prop->val.len % entrylen) != 0)
408 FAIL(c, "\"reg\" property in %s has invalid length (%d bytes) "
409 "(#address-cells == %d, #size-cells == %d)",
410 node->fullpath, prop->val.len, addr_cells, size_cells);
412 NODE_CHECK(reg_format, NULL, WARN, &addr_size_cells);
414 static void check_ranges_format(struct check *c, struct node *dt,
417 struct property *prop;
418 int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
420 prop = get_property(node, "ranges");
425 FAIL(c, "Root node has a \"ranges\" property");
429 p_addr_cells = node_addr_cells(node->parent);
430 p_size_cells = node_size_cells(node->parent);
431 c_addr_cells = node_addr_cells(node);
432 c_size_cells = node_size_cells(node);
433 entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
435 if (prop->val.len == 0) {
436 if (p_addr_cells != c_addr_cells)
437 FAIL(c, "%s has empty \"ranges\" property but its "
438 "#address-cells (%d) differs from %s (%d)",
439 node->fullpath, c_addr_cells, node->parent->fullpath,
441 if (p_size_cells != c_size_cells)
442 FAIL(c, "%s has empty \"ranges\" property but its "
443 "#size-cells (%d) differs from %s (%d)",
444 node->fullpath, c_size_cells, node->parent->fullpath,
446 } else if ((prop->val.len % entrylen) != 0) {
447 FAIL(c, "\"ranges\" property in %s has invalid length (%d bytes) "
448 "(parent #address-cells == %d, child #address-cells == %d, "
449 "#size-cells == %d)", node->fullpath, prop->val.len,
450 p_addr_cells, c_addr_cells, c_size_cells);
453 NODE_CHECK(ranges_format, NULL, WARN, &addr_size_cells);
458 static void check_avoid_default_addr_size(struct check *c, struct node *dt,
461 struct property *reg, *ranges;
464 return; /* Ignore root node */
466 reg = get_property(node, "reg");
467 ranges = get_property(node, "ranges");
472 if ((node->parent->addr_cells == -1))
473 FAIL(c, "Relying on default #address-cells value for %s",
476 if ((node->parent->size_cells == -1))
477 FAIL(c, "Relying on default #size-cells value for %s",
480 NODE_CHECK(avoid_default_addr_size, NULL, WARN, &addr_size_cells);
482 static void check_obsolete_chosen_interrupt_controller(struct check *c,
486 struct property *prop;
488 chosen = get_node_by_path(dt, "/chosen");
492 prop = get_property(chosen, "interrupt-controller");
494 FAIL(c, "/chosen has obsolete \"interrupt-controller\" "
497 TREE_CHECK(obsolete_chosen_interrupt_controller, NULL, WARN);
499 static struct check *check_table[] = {
500 &duplicate_node_names, &duplicate_property_names,
501 &name_is_string, &name_properties,
503 &phandle_references, &path_references,
505 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
506 &device_type_is_string, &model_is_string, &status_is_string,
508 &addr_size_cells, ®_format, &ranges_format,
510 &avoid_default_addr_size,
511 &obsolete_chosen_interrupt_controller,
514 int check_semantics(struct node *dt, int outversion, int boot_cpuid_phys);
516 void process_checks(int force, struct boot_info *bi,
517 int checkflag, int outversion, int boot_cpuid_phys)
519 struct node *dt = bi->dt;
523 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
524 struct check *c = check_table[i];
526 if (c->level != IGNORE)
527 error = error || run_check(c, dt);
532 fprintf(stderr, "ERROR: Input tree has errors, aborting "
533 "(use -f to force output)\n");
535 } else if (quiet < 3) {
536 fprintf(stderr, "Warning: Input tree has errors, "
543 fprintf(stderr, "Warning: Skipping semantic checks due to structural errors\n");
545 if (!check_semantics(bi->dt, outversion,
547 fprintf(stderr, "Warning: Input tree has semantic errors\n");
553 * Semantic check functions
556 #define ERRMSG(...) if (quiet < 2) fprintf(stderr, "ERROR: " __VA_ARGS__)
557 #define WARNMSG(...) if (quiet < 1) fprintf(stderr, "Warning: " __VA_ARGS__)
559 #define DO_ERR(...) do {ERRMSG(__VA_ARGS__); ok = 0; } while (0)
561 #define CHECK_HAVE(node, propname) \
563 if (! (prop = get_property((node), (propname)))) \
564 DO_ERR("Missing \"%s\" property in %s\n", (propname), \
568 #define CHECK_HAVE_WARN(node, propname) \
570 if (! (prop = get_property((node), (propname)))) \
571 WARNMSG("%s has no \"%s\" property\n", \
572 (node)->fullpath, (propname)); \
575 #define CHECK_HAVE_STRING(node, propname) \
577 CHECK_HAVE((node), (propname)); \
578 if (prop && !data_is_one_string(prop->val)) \
579 DO_ERR("\"%s\" property in %s is not a string\n", \
580 (propname), (node)->fullpath); \
583 #define CHECK_HAVE_STREQ(node, propname, value) \
585 CHECK_HAVE_STRING((node), (propname)); \
586 if (prop && !streq(prop->val.val, (value))) \
587 DO_ERR("%s has wrong %s, %s (should be %s\n", \
588 (node)->fullpath, (propname), \
589 prop->val.val, (value)); \
592 #define CHECK_HAVE_ONECELL(node, propname) \
594 CHECK_HAVE((node), (propname)); \
595 if (prop && (prop->val.len != sizeof(cell_t))) \
596 DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \
599 #define CHECK_HAVE_WARN_ONECELL(node, propname) \
601 CHECK_HAVE_WARN((node), (propname)); \
602 if (prop && (prop->val.len != sizeof(cell_t))) \
603 DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \
606 #define CHECK_HAVE_WARN_PHANDLE(xnode, propname, root) \
609 CHECK_HAVE_WARN_ONECELL((xnode), (propname)); \
611 cell_t phandle = propval_cell(prop); \
612 if ((phandle == 0) || (phandle == -1)) { \
613 DO_ERR("\"%s\" property in %s contains an invalid phandle %x\n", (propname), (xnode)->fullpath, phandle); \
615 ref = get_node_by_phandle((root), propval_cell(prop)); \
617 DO_ERR("\"%s\" property in %s refers to non-existant phandle %x\n", (propname), (xnode)->fullpath, propval_cell(prop)); \
622 #define CHECK_HAVE_WARN_STRING(node, propname) \
624 CHECK_HAVE_WARN((node), (propname)); \
625 if (prop && !data_is_one_string(prop->val)) \
626 DO_ERR("\"%s\" property in %s is not a string\n", \
627 (propname), (node)->fullpath); \
630 static int check_root(struct node *root)
632 struct property *prop;
635 CHECK_HAVE_STRING(root, "model");
636 CHECK_HAVE_WARN(root, "compatible");
641 static int check_cpus(struct node *root, int outversion, int boot_cpuid_phys)
643 struct node *cpus, *cpu;
644 struct property *prop;
645 struct node *bootcpu = NULL;
648 cpus = get_subnode(root, "cpus");
650 ERRMSG("Missing /cpus node\n");
654 if (cpus->addr_cells != 1)
655 DO_ERR("%s has bad #address-cells value %d (should be 1)\n",
656 cpus->fullpath, cpus->addr_cells);
657 if (cpus->size_cells != 0)
658 DO_ERR("%s has bad #size-cells value %d (should be 0)\n",
659 cpus->fullpath, cpus->size_cells);
661 for_each_child(cpus, cpu) {
662 CHECK_HAVE_STREQ(cpu, "device_type", "cpu");
664 CHECK_HAVE_ONECELL(cpu, "reg");
669 unitnum = strtol(get_unitname(cpu), &eptr, 16);
671 WARNMSG("%s has bad format unit name %s (should be CPU number\n",
672 cpu->fullpath, get_unitname(cpu));
673 } else if (unitnum != propval_cell(prop)) {
674 WARNMSG("%s unit name \"%s\" does not match \"reg\" property <%x>\n",
675 cpu->fullpath, get_unitname(cpu),
680 /* CHECK_HAVE_ONECELL(cpu, "d-cache-line-size"); */
681 /* CHECK_HAVE_ONECELL(cpu, "i-cache-line-size"); */
682 CHECK_HAVE_ONECELL(cpu, "d-cache-size");
683 CHECK_HAVE_ONECELL(cpu, "i-cache-size");
685 CHECK_HAVE_WARN_ONECELL(cpu, "clock-frequency");
686 CHECK_HAVE_WARN_ONECELL(cpu, "timebase-frequency");
688 prop = get_property(cpu, "linux,boot-cpu");
691 WARNMSG("\"linux,boot-cpu\" property in %s is non-empty\n",
694 DO_ERR("Multiple boot cpus (%s and %s)\n",
695 bootcpu->fullpath, cpu->fullpath);
701 if (outversion < 2) {
703 WARNMSG("No cpu has \"linux,boot-cpu\" property\n");
706 WARNMSG("\"linux,boot-cpu\" property is deprecated in blob version 2 or higher\n");
707 if (boot_cpuid_phys == 0xfeedbeef)
708 WARNMSG("physical boot CPU not set. Use -b option to set\n");
714 static int check_memory(struct node *root)
717 struct property *prop;
721 for_each_child(root, mem) {
722 if (! strneq(mem->name, "memory", mem->basenamelen))
727 CHECK_HAVE_STREQ(mem, "device_type", "memory");
728 CHECK_HAVE(mem, "reg");
732 ERRMSG("No memory nodes\n");
739 int check_semantics(struct node *dt, int outversion, int boot_cpuid_phys)
743 ok = ok && check_root(dt);
744 ok = ok && check_cpus(dt, outversion, boot_cpuid_phys);
745 ok = ok && check_memory(dt);