blob: cacb5b4c694deaf2fa5bc7820def47ff412d9bac [file] [log] [blame]
David Gibson2f1ccc32007-11-01 16:49:26 +11001/*
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
David Gibsonb16a2bd2007-11-22 14:38:07 +110023#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
90static inline void check_msg(struct check *c, const char *fmt, ...)
91{
92 va_list ap;
93 va_start(ap, fmt);
94
95 if ((c->level < WARN) || (c->level <= quiet))
96 return; /* Suppress message */
97
98 fprintf(stderr, "%s (%s): ",
99 (c->level == ERROR) ? "ERROR" : "Warning", c->name);
100 vfprintf(stderr, fmt, ap);
101 fprintf(stderr, "\n");
102}
103
104#define FAIL(c, fmt, ...) \
105 do { \
106 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
107 (c)->status = FAILED; \
108 check_msg((c), fmt, __VA_ARGS__); \
109 } while (0)
110
111static void check_nodes_props(struct check *c, struct node *dt, struct node *node)
112{
113 struct node *child;
114 struct property *prop;
115
116 TRACE(c, "%s", node->fullpath);
117 if (c->node_fn)
118 c->node_fn(c, dt, node);
119
120 if (c->prop_fn)
121 for_each_property(node, prop) {
122 TRACE(c, "%s\t'%s'", node->fullpath, prop->name);
123 c->prop_fn(c, dt, node, prop);
124 }
125
126 for_each_child(node, child)
127 check_nodes_props(c, dt, child);
128}
129
130static int run_check(struct check *c, struct node *dt)
131{
132 int error = 0;
133 int i;
134
135 assert(!c->inprogress);
136
137 if (c->status != UNCHECKED)
138 goto out;
139
140 c->inprogress = 1;
141
142 for (i = 0; i < c->num_prereqs; i++) {
143 struct check *prq = c->prereq[i];
144 error |= run_check(prq, dt);
145 if (prq->status != PASSED) {
146 c->status = PREREQ;
147 check_msg(c, "Failed prerequisite '%s'",
148 c->prereq[i]->name);
149 }
150 }
151
152 if (c->status != UNCHECKED)
153 goto out;
154
155 if (c->node_fn || c->prop_fn)
156 check_nodes_props(c, dt, dt);
157
158 if (c->tree_fn)
159 c->tree_fn(c, dt);
160 if (c->status == UNCHECKED)
161 c->status = PASSED;
162
163 TRACE(c, "\tCompleted, status %d", c->status);
164
165out:
166 c->inprogress = 0;
167 if ((c->status != PASSED) && (c->level == ERROR))
168 error = 1;
169 return error;
170}
171
David Gibson2f1ccc32007-11-01 16:49:26 +1100172/*
173 * Structural check functions
174 */
175
David Gibsonb16a2bd2007-11-22 14:38:07 +1100176static void check_duplicate_node_names(struct check *c, struct node *dt,
177 struct node *node)
178{
179 struct node *child, *child2;
180
181 for_each_child(node, child)
182 for (child2 = child->next_sibling;
183 child2;
184 child2 = child2->next_sibling)
185 if (streq(child->name, child2->name))
186 FAIL(c, "Duplicate node name %s",
187 child->fullpath);
188}
189NODE_CHECK(duplicate_node_names, NULL, ERROR);
190
191static void check_duplicate_property_names(struct check *c, struct node *dt,
192 struct node *node)
193{
194 struct property *prop, *prop2;
195
196 for_each_property(node, prop)
197 for (prop2 = prop->next; prop2; prop2 = prop2->next)
198 if (streq(prop->name, prop2->name))
199 FAIL(c, "Duplicate property name %s in %s",
200 prop->name, node->fullpath);
201}
202NODE_CHECK(duplicate_property_names, NULL, ERROR);
203
204static void check_explicit_phandles(struct check *c, struct node *root,
205 struct node *node)
206{
207 struct property *prop;
208 struct node *other;
209 cell_t phandle;
210
211 prop = get_property(node, "linux,phandle");
212 if (! prop)
213 return; /* No phandle, that's fine */
214
215 if (prop->val.len != sizeof(cell_t)) {
216 FAIL(c, "%s has bad length (%d) linux,phandle property",
217 node->fullpath, prop->val.len);
218 return;
219 }
220
221 phandle = propval_cell(prop);
222 if ((phandle == 0) || (phandle == -1)) {
223 FAIL(c, "%s has invalid linux,phandle value 0x%x",
224 node->fullpath, phandle);
225 return;
226 }
227
228 other = get_node_by_phandle(root, phandle);
229 if (other) {
230 FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)",
231 node->fullpath, phandle, other->fullpath);
232 return;
233 }
234
235 node->phandle = phandle;
236}
237NODE_CHECK(explicit_phandles, NULL, ERROR);
238
239/*
240 * Reference fixup functions
241 */
242
243static void fixup_phandle_references(struct check *c, struct node *dt,
244 struct node *node, struct property *prop)
245{
David Gibsondc941772007-11-22 14:39:23 +1100246 struct marker *m = prop->val.markers;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100247 struct node *refnode;
248 cell_t phandle;
249
David Gibsondc941772007-11-22 14:39:23 +1100250 for_each_marker_of_type(m, REF_PHANDLE) {
251 assert(m->offset + sizeof(cell_t) <= prop->val.len);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100252
David Gibsondc941772007-11-22 14:39:23 +1100253 refnode = get_node_by_ref(dt, m->ref);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100254 if (! refnode) {
255 FAIL(c, "Reference to non-existent node or label \"%s\"\n",
David Gibsondc941772007-11-22 14:39:23 +1100256 m->ref);
257 continue;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100258 }
259
David Gibsondc941772007-11-22 14:39:23 +1100260 phandle = get_node_phandle(dt, refnode);
261 *((cell_t *)(prop->val.val + m->offset)) = cpu_to_be32(phandle);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100262 }
263}
264CHECK(phandle_references, NULL, NULL, fixup_phandle_references, NULL, ERROR,
265 &duplicate_node_names, &explicit_phandles);
266
267static struct check *check_table[] = {
268 &duplicate_node_names, &duplicate_property_names,
269 &explicit_phandles,
270 &phandle_references,
271};
272
273void process_checks(int force, struct node *dt)
274{
275 int i;
276 int error = 0;
277
278 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
279 struct check *c = check_table[i];
280
281 if (c->level != IGNORE)
282 error = error || run_check(c, dt);
283 }
284
285 if (error) {
286 if (!force) {
287 fprintf(stderr, "ERROR: Input tree has errors, aborting "
288 "(use -f to force output)\n");
289 exit(2);
290 } else if (quiet < 3) {
291 fprintf(stderr, "Warning: Input tree has errors, "
292 "output forced\n");
293 }
294 }
295}
296
297/*
298 * Semantic check functions
299 */
300
David Gibson2f1ccc32007-11-01 16:49:26 +1100301#define ERRMSG(...) if (quiet < 2) fprintf(stderr, "ERROR: " __VA_ARGS__)
302#define WARNMSG(...) if (quiet < 1) fprintf(stderr, "Warning: " __VA_ARGS__)
303
304#define DO_ERR(...) do {ERRMSG(__VA_ARGS__); ok = 0; } while (0)
305
David Gibson2f1ccc32007-11-01 16:49:26 +1100306static int must_be_one_cell(struct property *prop, struct node *node)
307{
308 if (prop->val.len != sizeof(cell_t)) {
309 ERRMSG("\"%s\" property in %s has the wrong length (should be 1 cell)\n",
310 prop->name, node->fullpath);
311 return 0;
312 }
313
314 return 1;
315}
316
317static int must_be_cells(struct property *prop, struct node *node)
318{
319 if ((prop->val.len % sizeof(cell_t)) != 0) {
320 ERRMSG("\"%s\" property in %s is not a multiple of cell size\n",
321 prop->name, node->fullpath);
322 return 0;
323 }
324
325 return 1;
326}
327
328static int must_be_string(struct property *prop, struct node *node)
329{
330 if (! data_is_one_string(prop->val)) {
331 ERRMSG("\"%s\" property in %s is not a string\n",
332 prop->name, node->fullpath);
333 return 0;
334 }
335
336 return 1;
337}
338
339static int name_prop_check(struct property *prop, struct node *node)
340{
341 if ((prop->val.len != node->basenamelen+1)
342 || !strneq(prop->val.val, node->name, node->basenamelen)) {
343 ERRMSG("name property \"%s\" does not match node basename in %s\n",
344 prop->val.val,
345 node->fullpath);
346 return 0;
347 }
348
349 return 1;
350}
351
352static struct {
353 char *propname;
354 int (*check_fn)(struct property *prop, struct node *node);
355} prop_checker_table[] = {
356 {"name", must_be_string},
357 {"name", name_prop_check},
358 {"linux,phandle", must_be_one_cell},
359 {"#address-cells", must_be_one_cell},
360 {"#size-cells", must_be_one_cell},
361 {"reg", must_be_cells},
362 {"model", must_be_string},
363 {"device_type", must_be_string},
364};
365
366static int check_properties(struct node *node)
367{
368 struct property *prop;
369 struct node *child;
370 int i;
371 int ok = 1;
372
373 for_each_property(node, prop)
374 for (i = 0; i < ARRAY_SIZE(prop_checker_table); i++)
375 if (streq(prop->name, prop_checker_table[i].propname))
376 if (! prop_checker_table[i].check_fn(prop, node)) {
377 ok = 0;
378 break;
379 }
380
381 for_each_child(node, child)
382 if (! check_properties(child))
383 ok = 0;
384
385 return ok;
386}
387
388#define CHECK_HAVE(node, propname) \
389 do { \
390 if (! (prop = get_property((node), (propname)))) \
391 DO_ERR("Missing \"%s\" property in %s\n", (propname), \
392 (node)->fullpath); \
393 } while (0);
394
395#define CHECK_HAVE_WARN(node, propname) \
396 do { \
397 if (! (prop = get_property((node), (propname)))) \
398 WARNMSG("%s has no \"%s\" property\n", \
399 (node)->fullpath, (propname)); \
400 } while (0)
401
402#define CHECK_HAVE_STRING(node, propname) \
403 do { \
404 CHECK_HAVE((node), (propname)); \
405 if (prop && !data_is_one_string(prop->val)) \
406 DO_ERR("\"%s\" property in %s is not a string\n", \
407 (propname), (node)->fullpath); \
408 } while (0)
409
410#define CHECK_HAVE_STREQ(node, propname, value) \
411 do { \
412 CHECK_HAVE_STRING((node), (propname)); \
413 if (prop && !streq(prop->val.val, (value))) \
414 DO_ERR("%s has wrong %s, %s (should be %s\n", \
415 (node)->fullpath, (propname), \
416 prop->val.val, (value)); \
417 } while (0)
418
419#define CHECK_HAVE_ONECELL(node, propname) \
420 do { \
421 CHECK_HAVE((node), (propname)); \
422 if (prop && (prop->val.len != sizeof(cell_t))) \
423 DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \
424 } while (0)
425
426#define CHECK_HAVE_WARN_ONECELL(node, propname) \
427 do { \
428 CHECK_HAVE_WARN((node), (propname)); \
429 if (prop && (prop->val.len != sizeof(cell_t))) \
430 DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \
431 } while (0)
432
433#define CHECK_HAVE_WARN_PHANDLE(xnode, propname, root) \
434 do { \
435 struct node *ref; \
436 CHECK_HAVE_WARN_ONECELL((xnode), (propname)); \
437 if (prop) {\
438 cell_t phandle = propval_cell(prop); \
439 if ((phandle == 0) || (phandle == -1)) { \
440 DO_ERR("\"%s\" property in %s contains an invalid phandle %x\n", (propname), (xnode)->fullpath, phandle); \
441 } else { \
442 ref = get_node_by_phandle((root), propval_cell(prop)); \
443 if (! ref) \
444 DO_ERR("\"%s\" property in %s refers to non-existant phandle %x\n", (propname), (xnode)->fullpath, propval_cell(prop)); \
445 } \
446 } \
447 } while (0)
448
449#define CHECK_HAVE_WARN_STRING(node, propname) \
450 do { \
451 CHECK_HAVE_WARN((node), (propname)); \
452 if (prop && !data_is_one_string(prop->val)) \
453 DO_ERR("\"%s\" property in %s is not a string\n", \
454 (propname), (node)->fullpath); \
455 } while (0)
456
457static int check_root(struct node *root)
458{
459 struct property *prop;
460 int ok = 1;
461
462 CHECK_HAVE_STRING(root, "model");
463
464 CHECK_HAVE(root, "#address-cells");
465 CHECK_HAVE(root, "#size-cells");
466
467 CHECK_HAVE_WARN(root, "compatible");
468
469 return ok;
470}
471
472static int check_cpus(struct node *root, int outversion, int boot_cpuid_phys)
473{
474 struct node *cpus, *cpu;
475 struct property *prop;
476 struct node *bootcpu = NULL;
477 int ok = 1;
478
479 cpus = get_subnode(root, "cpus");
480 if (! cpus) {
481 ERRMSG("Missing /cpus node\n");
482 return 0;
483 }
484
485 CHECK_HAVE_WARN(cpus, "#address-cells");
486 CHECK_HAVE_WARN(cpus, "#size-cells");
487
488 for_each_child(cpus, cpu) {
489 CHECK_HAVE_STREQ(cpu, "device_type", "cpu");
490
491 if (cpu->addr_cells != 1)
492 DO_ERR("%s has bad #address-cells value %d (should be 1)\n",
493 cpu->fullpath, cpu->addr_cells);
494 if (cpu->size_cells != 0)
495 DO_ERR("%s has bad #size-cells value %d (should be 0)\n",
496 cpu->fullpath, cpu->size_cells);
497
498 CHECK_HAVE_ONECELL(cpu, "reg");
499 if (prop) {
500 cell_t unitnum;
501 char *eptr;
502
503 unitnum = strtol(get_unitname(cpu), &eptr, 16);
504 if (*eptr) {
505 WARNMSG("%s has bad format unit name %s (should be CPU number\n",
506 cpu->fullpath, get_unitname(cpu));
507 } else if (unitnum != propval_cell(prop)) {
508 WARNMSG("%s unit name \"%s\" does not match \"reg\" property <%x>\n",
509 cpu->fullpath, get_unitname(cpu),
510 propval_cell(prop));
511 }
512 }
513
514/* CHECK_HAVE_ONECELL(cpu, "d-cache-line-size"); */
515/* CHECK_HAVE_ONECELL(cpu, "i-cache-line-size"); */
516 CHECK_HAVE_ONECELL(cpu, "d-cache-size");
517 CHECK_HAVE_ONECELL(cpu, "i-cache-size");
518
519 CHECK_HAVE_WARN_ONECELL(cpu, "clock-frequency");
520 CHECK_HAVE_WARN_ONECELL(cpu, "timebase-frequency");
521
522 prop = get_property(cpu, "linux,boot-cpu");
523 if (prop) {
524 if (prop->val.len)
525 WARNMSG("\"linux,boot-cpu\" property in %s is non-empty\n",
526 cpu->fullpath);
527 if (bootcpu)
528 DO_ERR("Multiple boot cpus (%s and %s)\n",
529 bootcpu->fullpath, cpu->fullpath);
530 else
531 bootcpu = cpu;
532 }
533 }
534
535 if (outversion < 2) {
536 if (! bootcpu)
537 WARNMSG("No cpu has \"linux,boot-cpu\" property\n");
538 } else {
539 if (bootcpu)
540 WARNMSG("\"linux,boot-cpu\" property is deprecated in blob version 2 or higher\n");
541 if (boot_cpuid_phys == 0xfeedbeef)
542 WARNMSG("physical boot CPU not set. Use -b option to set\n");
543 }
544
545 return ok;
546}
547
548static int check_memory(struct node *root)
549{
550 struct node *mem;
551 struct property *prop;
552 int nnodes = 0;
553 int ok = 1;
554
555 for_each_child(root, mem) {
556 if (! strneq(mem->name, "memory", mem->basenamelen))
557 continue;
558
559 nnodes++;
560
561 CHECK_HAVE_STREQ(mem, "device_type", "memory");
562 CHECK_HAVE(mem, "reg");
563 }
564
565 if (nnodes == 0) {
566 ERRMSG("No memory nodes\n");
567 return 0;
568 }
569
570 return ok;
571}
572
573static int check_chosen(struct node *root)
574{
575 struct node *chosen;
576 struct property *prop;
577 int ok = 1;
578
579 chosen = get_subnode(root, "chosen");
580 if (!chosen)
581 return ok;
582
583 /* give warning for obsolete interrupt-controller property */
584 do {
585 if ((prop = get_property(chosen, "interrupt-controller")) != NULL) {
586 WARNMSG("%s has obsolete \"%s\" property\n",
587 chosen->fullpath, "interrupt-controller");
588 }
589 } while (0);
590
591 return ok;
592}
593
594static int check_addr_size_reg(struct node *node,
595 int p_addr_cells, int p_size_cells)
596{
597 int addr_cells = p_addr_cells;
598 int size_cells = p_size_cells;
599 struct property *prop;
600 struct node *child;
601 int ok = 1;
602
603 node->addr_cells = addr_cells;
604 node->size_cells = size_cells;
605
606 prop = get_property(node, "reg");
607 if (prop) {
608 int len = prop->val.len / 4;
609
610 if ((len % (addr_cells+size_cells)) != 0)
611 DO_ERR("\"reg\" property in %s has invalid length (%d) for given #address-cells (%d) and #size-cells (%d)\n",
612 node->fullpath, prop->val.len,
613 addr_cells, size_cells);
614 }
615
616 prop = get_property(node, "#address-cells");
617 if (prop)
618 addr_cells = propval_cell(prop);
619
620 prop = get_property(node, "#size-cells");
621 if (prop)
622 size_cells = propval_cell(prop);
623
624 for_each_child(node, child) {
625 ok = ok && check_addr_size_reg(child, addr_cells, size_cells);
626 }
627
628 return ok;
629}
630
631int check_semantics(struct node *dt, int outversion, int boot_cpuid_phys)
632{
633 int ok = 1;
634
635 ok = ok && check_properties(dt);
636 ok = ok && check_addr_size_reg(dt, -1, -1);
637 ok = ok && check_root(dt);
638 ok = ok && check_cpus(dt, outversion, boot_cpuid_phys);
639 ok = ok && check_memory(dt);
640 ok = ok && check_chosen(dt);
641 if (! ok)
642 return 0;
643
644 return 1;
645}