blob: e27ab30ae8cd3206c8e8e5be98234e2a780263bf [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
David Gibsond06cda32007-12-05 09:34:53 +1100104#define FAIL(c, ...) \
David Gibsonb16a2bd2007-11-22 14:38:07 +1100105 do { \
106 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
107 (c)->status = FAILED; \
David Gibsond06cda32007-12-05 09:34:53 +1100108 check_msg((c), __VA_ARGS__); \
David Gibsonb16a2bd2007-11-22 14:38:07 +1100109 } 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/*
David Gibson459c9552007-12-05 09:40:23 +1100173 * Utility check functions
174 */
175
176static void check_is_string(struct check *c, struct node *root,
177 struct node *node)
178{
179 struct property *prop;
180 char *propname = c->data;
181
182 prop = get_property(node, propname);
183 if (!prop)
184 return; /* Not present, assumed ok */
185
186 if (!data_is_one_string(prop->val))
187 FAIL(c, "\"%s\" property in %s is not a string",
188 propname, node->fullpath);
189}
190#define CHECK_IS_STRING(nm, propname, lvl) \
191 CHECK(nm, NULL, check_is_string, NULL, (propname), (lvl))
192
David Gibsonc21acab2007-12-06 16:59:45 +1100193static void check_is_cell(struct check *c, struct node *root,
194 struct node *node)
195{
196 struct property *prop;
197 char *propname = c->data;
198
199 prop = get_property(node, propname);
200 if (!prop)
201 return; /* Not present, assumed ok */
202
203 if (prop->val.len != sizeof(cell_t))
204 FAIL(c, "\"%s\" property in %s is not a single cell",
205 propname, node->fullpath);
206}
207#define CHECK_IS_CELL(nm, propname, lvl) \
208 CHECK(nm, NULL, check_is_cell, NULL, (propname), (lvl))
209
David Gibson459c9552007-12-05 09:40:23 +1100210/*
David Gibson2f1ccc32007-11-01 16:49:26 +1100211 * Structural check functions
212 */
213
David Gibsonb16a2bd2007-11-22 14:38:07 +1100214static void check_duplicate_node_names(struct check *c, struct node *dt,
215 struct node *node)
216{
217 struct node *child, *child2;
218
219 for_each_child(node, child)
220 for (child2 = child->next_sibling;
221 child2;
222 child2 = child2->next_sibling)
223 if (streq(child->name, child2->name))
224 FAIL(c, "Duplicate node name %s",
225 child->fullpath);
226}
227NODE_CHECK(duplicate_node_names, NULL, ERROR);
228
229static void check_duplicate_property_names(struct check *c, struct node *dt,
230 struct node *node)
231{
232 struct property *prop, *prop2;
233
234 for_each_property(node, prop)
235 for (prop2 = prop->next; prop2; prop2 = prop2->next)
236 if (streq(prop->name, prop2->name))
237 FAIL(c, "Duplicate property name %s in %s",
238 prop->name, node->fullpath);
239}
240NODE_CHECK(duplicate_property_names, NULL, ERROR);
241
242static void check_explicit_phandles(struct check *c, struct node *root,
243 struct node *node)
244{
245 struct property *prop;
246 struct node *other;
247 cell_t phandle;
248
249 prop = get_property(node, "linux,phandle");
250 if (! prop)
251 return; /* No phandle, that's fine */
252
253 if (prop->val.len != sizeof(cell_t)) {
254 FAIL(c, "%s has bad length (%d) linux,phandle property",
255 node->fullpath, prop->val.len);
256 return;
257 }
258
259 phandle = propval_cell(prop);
260 if ((phandle == 0) || (phandle == -1)) {
261 FAIL(c, "%s has invalid linux,phandle value 0x%x",
262 node->fullpath, phandle);
263 return;
264 }
265
266 other = get_node_by_phandle(root, phandle);
267 if (other) {
268 FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)",
269 node->fullpath, phandle, other->fullpath);
270 return;
271 }
272
273 node->phandle = phandle;
274}
275NODE_CHECK(explicit_phandles, NULL, ERROR);
276
David Gibson459c9552007-12-05 09:40:23 +1100277static void check_name_properties(struct check *c, struct node *root,
278 struct node *node)
279{
280 struct property *prop;
281
282 prop = get_property(node, "name");
283 if (!prop)
284 return; /* No name property, that's fine */
285
286 if ((prop->val.len != node->basenamelen+1)
287 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0))
288 FAIL(c, "\"name\" property in %s is incorrect (\"%s\" instead"
289 " of base node name)", node->fullpath, prop->val.val);
290}
291CHECK_IS_STRING(name_is_string, "name", ERROR);
292NODE_CHECK(name_properties, NULL, ERROR, &name_is_string);
293
David Gibsonb16a2bd2007-11-22 14:38:07 +1100294/*
295 * Reference fixup functions
296 */
297
298static void fixup_phandle_references(struct check *c, struct node *dt,
299 struct node *node, struct property *prop)
300{
David Gibsondc941772007-11-22 14:39:23 +1100301 struct marker *m = prop->val.markers;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100302 struct node *refnode;
303 cell_t phandle;
304
David Gibsondc941772007-11-22 14:39:23 +1100305 for_each_marker_of_type(m, REF_PHANDLE) {
306 assert(m->offset + sizeof(cell_t) <= prop->val.len);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100307
David Gibsondc941772007-11-22 14:39:23 +1100308 refnode = get_node_by_ref(dt, m->ref);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100309 if (! refnode) {
310 FAIL(c, "Reference to non-existent node or label \"%s\"\n",
David Gibsondc941772007-11-22 14:39:23 +1100311 m->ref);
312 continue;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100313 }
314
David Gibsondc941772007-11-22 14:39:23 +1100315 phandle = get_node_phandle(dt, refnode);
316 *((cell_t *)(prop->val.val + m->offset)) = cpu_to_be32(phandle);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100317 }
318}
319CHECK(phandle_references, NULL, NULL, fixup_phandle_references, NULL, ERROR,
320 &duplicate_node_names, &explicit_phandles);
321
David Gibsonefbbef82007-12-05 10:43:50 +1100322static void fixup_path_references(struct check *c, struct node *dt,
323 struct node *node, struct property *prop)
324{
325 struct marker *m = prop->val.markers;
326 struct node *refnode;
327 char *path;
328
329 for_each_marker_of_type(m, REF_PATH) {
330 assert(m->offset <= prop->val.len);
331
332 refnode = get_node_by_ref(dt, m->ref);
333 if (!refnode) {
334 FAIL(c, "Reference to non-existent node or label \"%s\"\n",
335 m->ref);
336 continue;
337 }
338
339 path = refnode->fullpath;
340 prop->val = data_insert_at_marker(prop->val, m, path,
341 strlen(path) + 1);
342 }
343}
344CHECK(path_references, NULL, NULL, fixup_path_references, NULL, ERROR,
345 &duplicate_node_names);
346
David Gibsonc21acab2007-12-06 16:59:45 +1100347/*
348 * Semantic checks
349 */
350CHECK_IS_CELL(address_cells_is_cell, "#address-cells", WARN);
351CHECK_IS_CELL(size_cells_is_cell, "#size-cells", WARN);
352CHECK_IS_CELL(interrupt_cells_is_cell, "#interrupt-cells", WARN);
353
David Gibsonfaf037f2007-12-06 17:01:07 +1100354CHECK_IS_STRING(device_type_is_string, "device_type", WARN);
355CHECK_IS_STRING(model_is_string, "model", WARN);
356CHECK_IS_STRING(status_is_string, "status", WARN);
357
David Gibson7e089d92007-12-07 14:05:55 +1100358static void fixup_addr_size_cells(struct check *c, struct node *dt,
359 struct node *node)
360{
361 struct property *prop;
362
363 node->addr_cells = -1;
364 node->size_cells = -1;
365
366 prop = get_property(node, "#address-cells");
367 if (prop)
368 node->addr_cells = propval_cell(prop);
369
370 prop = get_property(node, "#size-cells");
371 if (prop)
372 node->size_cells = propval_cell(prop);
373}
374CHECK(addr_size_cells, NULL, fixup_addr_size_cells, NULL, NULL, WARN,
375 &address_cells_is_cell, &size_cells_is_cell);
376
377#define node_addr_cells(n) \
378 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
379#define node_size_cells(n) \
380 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
381
382static void check_reg_format(struct check *c, struct node *dt,
383 struct node *node)
384{
385 struct property *prop;
386 int addr_cells, size_cells, entrylen;
387
388 prop = get_property(node, "reg");
389 if (!prop)
390 return; /* No "reg", that's fine */
391
392 if (!node->parent) {
393 FAIL(c, "Root node has a \"reg\" property");
394 return;
395 }
396
397 if (prop->val.len == 0)
398 FAIL(c, "\"reg\" property in %s is empty", node->fullpath);
399
400 addr_cells = node_addr_cells(node->parent);
401 size_cells = node_size_cells(node->parent);
402 entrylen = (addr_cells + size_cells) * sizeof(cell_t);
403
404 if ((prop->val.len % entrylen) != 0)
405 FAIL(c, "\"reg\" property in %s has invalid length (%d bytes) "
406 "(#address-cells == %d, #size-cells == %d)",
407 node->fullpath, prop->val.len, addr_cells, size_cells);
408}
409NODE_CHECK(reg_format, NULL, WARN, &addr_size_cells);
410
411static void check_ranges_format(struct check *c, struct node *dt,
412 struct node *node)
413{
414 struct property *prop;
415 int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
416
417 prop = get_property(node, "ranges");
418 if (!prop)
419 return;
420
421 if (!node->parent) {
422 FAIL(c, "Root node has a \"ranges\" property");
423 return;
424 }
425
426 p_addr_cells = node_addr_cells(node->parent);
427 p_size_cells = node_size_cells(node->parent);
428 c_addr_cells = node_addr_cells(node);
429 c_size_cells = node_size_cells(node);
430 entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
431
432 if (prop->val.len == 0) {
433 if (p_addr_cells != c_addr_cells)
434 FAIL(c, "%s has empty \"ranges\" property but its "
435 "#address-cells (%d) differs from %s (%d)",
436 node->fullpath, c_addr_cells, node->parent->fullpath,
437 p_addr_cells);
438 if (p_size_cells != c_size_cells)
439 FAIL(c, "%s has empty \"ranges\" property but its "
440 "#size-cells (%d) differs from %s (%d)",
441 node->fullpath, c_size_cells, node->parent->fullpath,
442 p_size_cells);
443 } else if ((prop->val.len % entrylen) != 0) {
444 FAIL(c, "\"ranges\" property in %s has invalid length (%d bytes) "
445 "(parent #address-cells == %d, child #address-cells == %d, "
446 "#size-cells == %d)", node->fullpath, prop->val.len,
447 p_addr_cells, c_addr_cells, c_size_cells);
448 }
449}
450NODE_CHECK(ranges_format, NULL, WARN, &addr_size_cells);
451
452/*
453 * Style checks
454 */
455static void check_avoid_default_addr_size(struct check *c, struct node *dt,
456 struct node *node)
457{
458 struct property *reg, *ranges;
459
460 if (!node->parent)
461 return; /* Ignore root node */
462
463 reg = get_property(node, "reg");
464 ranges = get_property(node, "ranges");
465
466 if (!reg && !ranges)
467 return;
468
469 if ((node->parent->addr_cells == -1))
470 FAIL(c, "Relying on default #address-cells value for %s",
471 node->fullpath);
472
473 if ((node->parent->size_cells == -1))
474 FAIL(c, "Relying on default #size-cells value for %s",
475 node->fullpath);
476}
477NODE_CHECK(avoid_default_addr_size, NULL, WARN, &addr_size_cells);
478
David Gibsonb16a2bd2007-11-22 14:38:07 +1100479static struct check *check_table[] = {
480 &duplicate_node_names, &duplicate_property_names,
David Gibson459c9552007-12-05 09:40:23 +1100481 &name_is_string, &name_properties,
David Gibsonb16a2bd2007-11-22 14:38:07 +1100482 &explicit_phandles,
David Gibsonefbbef82007-12-05 10:43:50 +1100483 &phandle_references, &path_references,
David Gibsonc21acab2007-12-06 16:59:45 +1100484
485 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
David Gibsonfaf037f2007-12-06 17:01:07 +1100486 &device_type_is_string, &model_is_string, &status_is_string,
David Gibson7e089d92007-12-07 14:05:55 +1100487
488 &addr_size_cells, &reg_format, &ranges_format,
489
490 &avoid_default_addr_size,
David Gibsonb16a2bd2007-11-22 14:38:07 +1100491};
492
David Gibson2d728162007-12-04 11:49:43 +1100493int check_semantics(struct node *dt, int outversion, int boot_cpuid_phys);
494
495void process_checks(int force, struct boot_info *bi,
496 int checkflag, int outversion, int boot_cpuid_phys)
David Gibsonb16a2bd2007-11-22 14:38:07 +1100497{
David Gibson2d728162007-12-04 11:49:43 +1100498 struct node *dt = bi->dt;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100499 int i;
500 int error = 0;
501
502 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
503 struct check *c = check_table[i];
504
505 if (c->level != IGNORE)
506 error = error || run_check(c, dt);
507 }
508
509 if (error) {
510 if (!force) {
511 fprintf(stderr, "ERROR: Input tree has errors, aborting "
512 "(use -f to force output)\n");
513 exit(2);
514 } else if (quiet < 3) {
515 fprintf(stderr, "Warning: Input tree has errors, "
516 "output forced\n");
517 }
518 }
David Gibson2d728162007-12-04 11:49:43 +1100519
520 if (checkflag) {
521 if (error) {
522 fprintf(stderr, "Warning: Skipping semantic checks due to structural errors\n");
523 } else {
524 if (!check_semantics(bi->dt, outversion,
525 boot_cpuid_phys))
526 fprintf(stderr, "Warning: Input tree has semantic errors\n");
527 }
528 }
David Gibsonb16a2bd2007-11-22 14:38:07 +1100529}
530
531/*
532 * Semantic check functions
533 */
534
David Gibson2f1ccc32007-11-01 16:49:26 +1100535#define ERRMSG(...) if (quiet < 2) fprintf(stderr, "ERROR: " __VA_ARGS__)
536#define WARNMSG(...) if (quiet < 1) fprintf(stderr, "Warning: " __VA_ARGS__)
537
538#define DO_ERR(...) do {ERRMSG(__VA_ARGS__); ok = 0; } while (0)
539
David Gibson2f1ccc32007-11-01 16:49:26 +1100540#define CHECK_HAVE(node, propname) \
541 do { \
542 if (! (prop = get_property((node), (propname)))) \
543 DO_ERR("Missing \"%s\" property in %s\n", (propname), \
544 (node)->fullpath); \
545 } while (0);
546
547#define CHECK_HAVE_WARN(node, propname) \
548 do { \
549 if (! (prop = get_property((node), (propname)))) \
550 WARNMSG("%s has no \"%s\" property\n", \
551 (node)->fullpath, (propname)); \
552 } while (0)
553
554#define CHECK_HAVE_STRING(node, propname) \
555 do { \
556 CHECK_HAVE((node), (propname)); \
557 if (prop && !data_is_one_string(prop->val)) \
558 DO_ERR("\"%s\" property in %s is not a string\n", \
559 (propname), (node)->fullpath); \
560 } while (0)
561
562#define CHECK_HAVE_STREQ(node, propname, value) \
563 do { \
564 CHECK_HAVE_STRING((node), (propname)); \
565 if (prop && !streq(prop->val.val, (value))) \
566 DO_ERR("%s has wrong %s, %s (should be %s\n", \
567 (node)->fullpath, (propname), \
568 prop->val.val, (value)); \
569 } while (0)
570
571#define CHECK_HAVE_ONECELL(node, propname) \
572 do { \
573 CHECK_HAVE((node), (propname)); \
574 if (prop && (prop->val.len != sizeof(cell_t))) \
575 DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \
576 } while (0)
577
578#define CHECK_HAVE_WARN_ONECELL(node, propname) \
579 do { \
580 CHECK_HAVE_WARN((node), (propname)); \
581 if (prop && (prop->val.len != sizeof(cell_t))) \
582 DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \
583 } while (0)
584
585#define CHECK_HAVE_WARN_PHANDLE(xnode, propname, root) \
586 do { \
587 struct node *ref; \
588 CHECK_HAVE_WARN_ONECELL((xnode), (propname)); \
589 if (prop) {\
590 cell_t phandle = propval_cell(prop); \
591 if ((phandle == 0) || (phandle == -1)) { \
592 DO_ERR("\"%s\" property in %s contains an invalid phandle %x\n", (propname), (xnode)->fullpath, phandle); \
593 } else { \
594 ref = get_node_by_phandle((root), propval_cell(prop)); \
595 if (! ref) \
596 DO_ERR("\"%s\" property in %s refers to non-existant phandle %x\n", (propname), (xnode)->fullpath, propval_cell(prop)); \
597 } \
598 } \
599 } while (0)
600
601#define CHECK_HAVE_WARN_STRING(node, propname) \
602 do { \
603 CHECK_HAVE_WARN((node), (propname)); \
604 if (prop && !data_is_one_string(prop->val)) \
605 DO_ERR("\"%s\" property in %s is not a string\n", \
606 (propname), (node)->fullpath); \
607 } while (0)
608
609static int check_root(struct node *root)
610{
611 struct property *prop;
612 int ok = 1;
613
614 CHECK_HAVE_STRING(root, "model");
David Gibson2f1ccc32007-11-01 16:49:26 +1100615 CHECK_HAVE_WARN(root, "compatible");
616
617 return ok;
618}
619
620static int check_cpus(struct node *root, int outversion, int boot_cpuid_phys)
621{
622 struct node *cpus, *cpu;
623 struct property *prop;
624 struct node *bootcpu = NULL;
625 int ok = 1;
626
627 cpus = get_subnode(root, "cpus");
628 if (! cpus) {
629 ERRMSG("Missing /cpus node\n");
630 return 0;
631 }
632
David Gibson7e089d92007-12-07 14:05:55 +1100633 if (cpus->addr_cells != 1)
634 DO_ERR("%s has bad #address-cells value %d (should be 1)\n",
635 cpus->fullpath, cpus->addr_cells);
636 if (cpus->size_cells != 0)
637 DO_ERR("%s has bad #size-cells value %d (should be 0)\n",
638 cpus->fullpath, cpus->size_cells);
David Gibson2f1ccc32007-11-01 16:49:26 +1100639
640 for_each_child(cpus, cpu) {
641 CHECK_HAVE_STREQ(cpu, "device_type", "cpu");
642
David Gibson2f1ccc32007-11-01 16:49:26 +1100643 CHECK_HAVE_ONECELL(cpu, "reg");
644 if (prop) {
645 cell_t unitnum;
646 char *eptr;
647
648 unitnum = strtol(get_unitname(cpu), &eptr, 16);
649 if (*eptr) {
650 WARNMSG("%s has bad format unit name %s (should be CPU number\n",
651 cpu->fullpath, get_unitname(cpu));
652 } else if (unitnum != propval_cell(prop)) {
653 WARNMSG("%s unit name \"%s\" does not match \"reg\" property <%x>\n",
654 cpu->fullpath, get_unitname(cpu),
655 propval_cell(prop));
656 }
657 }
658
659/* CHECK_HAVE_ONECELL(cpu, "d-cache-line-size"); */
660/* CHECK_HAVE_ONECELL(cpu, "i-cache-line-size"); */
661 CHECK_HAVE_ONECELL(cpu, "d-cache-size");
662 CHECK_HAVE_ONECELL(cpu, "i-cache-size");
663
664 CHECK_HAVE_WARN_ONECELL(cpu, "clock-frequency");
665 CHECK_HAVE_WARN_ONECELL(cpu, "timebase-frequency");
666
667 prop = get_property(cpu, "linux,boot-cpu");
668 if (prop) {
669 if (prop->val.len)
670 WARNMSG("\"linux,boot-cpu\" property in %s is non-empty\n",
671 cpu->fullpath);
672 if (bootcpu)
673 DO_ERR("Multiple boot cpus (%s and %s)\n",
674 bootcpu->fullpath, cpu->fullpath);
675 else
676 bootcpu = cpu;
677 }
678 }
679
680 if (outversion < 2) {
681 if (! bootcpu)
682 WARNMSG("No cpu has \"linux,boot-cpu\" property\n");
683 } else {
684 if (bootcpu)
685 WARNMSG("\"linux,boot-cpu\" property is deprecated in blob version 2 or higher\n");
686 if (boot_cpuid_phys == 0xfeedbeef)
687 WARNMSG("physical boot CPU not set. Use -b option to set\n");
688 }
689
690 return ok;
691}
692
693static int check_memory(struct node *root)
694{
695 struct node *mem;
696 struct property *prop;
697 int nnodes = 0;
698 int ok = 1;
699
700 for_each_child(root, mem) {
701 if (! strneq(mem->name, "memory", mem->basenamelen))
702 continue;
703
704 nnodes++;
705
706 CHECK_HAVE_STREQ(mem, "device_type", "memory");
707 CHECK_HAVE(mem, "reg");
708 }
709
710 if (nnodes == 0) {
711 ERRMSG("No memory nodes\n");
712 return 0;
713 }
714
715 return ok;
716}
717
718static int check_chosen(struct node *root)
719{
720 struct node *chosen;
721 struct property *prop;
722 int ok = 1;
723
724 chosen = get_subnode(root, "chosen");
725 if (!chosen)
726 return ok;
727
728 /* give warning for obsolete interrupt-controller property */
729 do {
730 if ((prop = get_property(chosen, "interrupt-controller")) != NULL) {
731 WARNMSG("%s has obsolete \"%s\" property\n",
732 chosen->fullpath, "interrupt-controller");
733 }
734 } while (0);
735
736 return ok;
737}
738
David Gibson2f1ccc32007-11-01 16:49:26 +1100739int check_semantics(struct node *dt, int outversion, int boot_cpuid_phys)
740{
741 int ok = 1;
742
David Gibson2f1ccc32007-11-01 16:49:26 +1100743 ok = ok && check_root(dt);
744 ok = ok && check_cpus(dt, outversion, boot_cpuid_phys);
745 ok = ok && check_memory(dt);
746 ok = ok && check_chosen(dt);
747 if (! ok)
748 return 0;
749
750 return 1;
751}