blob: d92279ee19c2cce5366fb2e61b8ac3c845c08a90 [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
David Gibsonb7c5eaf2007-12-07 18:08:03 +110090#ifdef __GNUC__
91static inline void check_msg(struct check *c, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
92#endif
David Gibsonb16a2bd2007-11-22 14:38:07 +110093static 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
David Gibsond06cda32007-12-05 09:34:53 +1100107#define FAIL(c, ...) \
David Gibsonb16a2bd2007-11-22 14:38:07 +1100108 do { \
109 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
110 (c)->status = FAILED; \
David Gibsond06cda32007-12-05 09:34:53 +1100111 check_msg((c), __VA_ARGS__); \
David Gibsonb16a2bd2007-11-22 14:38:07 +1100112 } 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
David Gibson2f1ccc32007-11-01 16:49:26 +1100175/*
David Gibson459c9552007-12-05 09:40:23 +1100176 * 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
David Gibsonc21acab2007-12-06 16:59:45 +1100196static 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
David Gibson459c9552007-12-05 09:40:23 +1100213/*
David Gibson2f1ccc32007-11-01 16:49:26 +1100214 * Structural check functions
215 */
216
David Gibsonb16a2bd2007-11-22 14:38:07 +1100217static 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
David Gibsonfa5b5202008-02-27 13:45:13 +1100245#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
David Gibsonb16a2bd2007-11-22 14:38:07 +1100281static void check_explicit_phandles(struct check *c, struct node *root,
282 struct node *node)
283{
284 struct property *prop;
285 struct node *other;
286 cell_t phandle;
287
288 prop = get_property(node, "linux,phandle");
289 if (! prop)
290 return; /* No phandle, that's fine */
291
292 if (prop->val.len != sizeof(cell_t)) {
293 FAIL(c, "%s has bad length (%d) linux,phandle property",
294 node->fullpath, prop->val.len);
295 return;
296 }
297
298 phandle = propval_cell(prop);
299 if ((phandle == 0) || (phandle == -1)) {
300 FAIL(c, "%s has invalid linux,phandle value 0x%x",
301 node->fullpath, phandle);
302 return;
303 }
304
305 other = get_node_by_phandle(root, phandle);
306 if (other) {
307 FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)",
308 node->fullpath, phandle, other->fullpath);
309 return;
310 }
311
312 node->phandle = phandle;
313}
314NODE_CHECK(explicit_phandles, NULL, ERROR);
315
David Gibson459c9552007-12-05 09:40:23 +1100316static void check_name_properties(struct check *c, struct node *root,
317 struct node *node)
318{
319 struct property *prop;
320
321 prop = get_property(node, "name");
322 if (!prop)
323 return; /* No name property, that's fine */
324
325 if ((prop->val.len != node->basenamelen+1)
326 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0))
327 FAIL(c, "\"name\" property in %s is incorrect (\"%s\" instead"
328 " of base node name)", node->fullpath, prop->val.val);
329}
330CHECK_IS_STRING(name_is_string, "name", ERROR);
331NODE_CHECK(name_properties, NULL, ERROR, &name_is_string);
332
David Gibsonb16a2bd2007-11-22 14:38:07 +1100333/*
334 * Reference fixup functions
335 */
336
337static void fixup_phandle_references(struct check *c, struct node *dt,
338 struct node *node, struct property *prop)
339{
David Gibsondc941772007-11-22 14:39:23 +1100340 struct marker *m = prop->val.markers;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100341 struct node *refnode;
342 cell_t phandle;
343
David Gibsondc941772007-11-22 14:39:23 +1100344 for_each_marker_of_type(m, REF_PHANDLE) {
345 assert(m->offset + sizeof(cell_t) <= prop->val.len);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100346
David Gibsondc941772007-11-22 14:39:23 +1100347 refnode = get_node_by_ref(dt, m->ref);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100348 if (! refnode) {
349 FAIL(c, "Reference to non-existent node or label \"%s\"\n",
David Gibsondc941772007-11-22 14:39:23 +1100350 m->ref);
351 continue;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100352 }
353
David Gibsondc941772007-11-22 14:39:23 +1100354 phandle = get_node_phandle(dt, refnode);
355 *((cell_t *)(prop->val.val + m->offset)) = cpu_to_be32(phandle);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100356 }
357}
358CHECK(phandle_references, NULL, NULL, fixup_phandle_references, NULL, ERROR,
359 &duplicate_node_names, &explicit_phandles);
360
David Gibsonefbbef82007-12-05 10:43:50 +1100361static void fixup_path_references(struct check *c, struct node *dt,
362 struct node *node, struct property *prop)
363{
364 struct marker *m = prop->val.markers;
365 struct node *refnode;
366 char *path;
367
368 for_each_marker_of_type(m, REF_PATH) {
369 assert(m->offset <= prop->val.len);
370
371 refnode = get_node_by_ref(dt, m->ref);
372 if (!refnode) {
373 FAIL(c, "Reference to non-existent node or label \"%s\"\n",
374 m->ref);
375 continue;
376 }
377
378 path = refnode->fullpath;
379 prop->val = data_insert_at_marker(prop->val, m, path,
380 strlen(path) + 1);
381 }
382}
383CHECK(path_references, NULL, NULL, fixup_path_references, NULL, ERROR,
384 &duplicate_node_names);
385
David Gibsonc21acab2007-12-06 16:59:45 +1100386/*
387 * Semantic checks
388 */
389CHECK_IS_CELL(address_cells_is_cell, "#address-cells", WARN);
390CHECK_IS_CELL(size_cells_is_cell, "#size-cells", WARN);
391CHECK_IS_CELL(interrupt_cells_is_cell, "#interrupt-cells", WARN);
392
David Gibsonfaf037f2007-12-06 17:01:07 +1100393CHECK_IS_STRING(device_type_is_string, "device_type", WARN);
394CHECK_IS_STRING(model_is_string, "model", WARN);
395CHECK_IS_STRING(status_is_string, "status", WARN);
396
David Gibson7e089d92007-12-07 14:05:55 +1100397static void fixup_addr_size_cells(struct check *c, struct node *dt,
398 struct node *node)
399{
400 struct property *prop;
401
402 node->addr_cells = -1;
403 node->size_cells = -1;
404
405 prop = get_property(node, "#address-cells");
406 if (prop)
407 node->addr_cells = propval_cell(prop);
408
409 prop = get_property(node, "#size-cells");
410 if (prop)
411 node->size_cells = propval_cell(prop);
412}
413CHECK(addr_size_cells, NULL, fixup_addr_size_cells, NULL, NULL, WARN,
414 &address_cells_is_cell, &size_cells_is_cell);
415
416#define node_addr_cells(n) \
417 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
418#define node_size_cells(n) \
419 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
420
421static void check_reg_format(struct check *c, struct node *dt,
422 struct node *node)
423{
424 struct property *prop;
425 int addr_cells, size_cells, entrylen;
426
427 prop = get_property(node, "reg");
428 if (!prop)
429 return; /* No "reg", that's fine */
430
431 if (!node->parent) {
432 FAIL(c, "Root node has a \"reg\" property");
433 return;
434 }
435
436 if (prop->val.len == 0)
437 FAIL(c, "\"reg\" property in %s is empty", node->fullpath);
438
439 addr_cells = node_addr_cells(node->parent);
440 size_cells = node_size_cells(node->parent);
441 entrylen = (addr_cells + size_cells) * sizeof(cell_t);
442
443 if ((prop->val.len % entrylen) != 0)
444 FAIL(c, "\"reg\" property in %s has invalid length (%d bytes) "
445 "(#address-cells == %d, #size-cells == %d)",
446 node->fullpath, prop->val.len, addr_cells, size_cells);
447}
448NODE_CHECK(reg_format, NULL, WARN, &addr_size_cells);
449
450static void check_ranges_format(struct check *c, struct node *dt,
451 struct node *node)
452{
453 struct property *prop;
454 int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
455
456 prop = get_property(node, "ranges");
457 if (!prop)
458 return;
459
460 if (!node->parent) {
461 FAIL(c, "Root node has a \"ranges\" property");
462 return;
463 }
464
465 p_addr_cells = node_addr_cells(node->parent);
466 p_size_cells = node_size_cells(node->parent);
467 c_addr_cells = node_addr_cells(node);
468 c_size_cells = node_size_cells(node);
469 entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
470
471 if (prop->val.len == 0) {
472 if (p_addr_cells != c_addr_cells)
473 FAIL(c, "%s has empty \"ranges\" property but its "
474 "#address-cells (%d) differs from %s (%d)",
475 node->fullpath, c_addr_cells, node->parent->fullpath,
476 p_addr_cells);
477 if (p_size_cells != c_size_cells)
478 FAIL(c, "%s has empty \"ranges\" property but its "
479 "#size-cells (%d) differs from %s (%d)",
480 node->fullpath, c_size_cells, node->parent->fullpath,
481 p_size_cells);
482 } else if ((prop->val.len % entrylen) != 0) {
483 FAIL(c, "\"ranges\" property in %s has invalid length (%d bytes) "
484 "(parent #address-cells == %d, child #address-cells == %d, "
485 "#size-cells == %d)", node->fullpath, prop->val.len,
486 p_addr_cells, c_addr_cells, c_size_cells);
487 }
488}
489NODE_CHECK(ranges_format, NULL, WARN, &addr_size_cells);
490
491/*
492 * Style checks
493 */
494static void check_avoid_default_addr_size(struct check *c, struct node *dt,
495 struct node *node)
496{
497 struct property *reg, *ranges;
498
499 if (!node->parent)
500 return; /* Ignore root node */
501
502 reg = get_property(node, "reg");
503 ranges = get_property(node, "ranges");
504
505 if (!reg && !ranges)
506 return;
507
508 if ((node->parent->addr_cells == -1))
509 FAIL(c, "Relying on default #address-cells value for %s",
510 node->fullpath);
511
512 if ((node->parent->size_cells == -1))
513 FAIL(c, "Relying on default #size-cells value for %s",
514 node->fullpath);
515}
516NODE_CHECK(avoid_default_addr_size, NULL, WARN, &addr_size_cells);
517
David Gibsone4ffc142007-12-07 14:06:11 +1100518static void check_obsolete_chosen_interrupt_controller(struct check *c,
519 struct node *dt)
520{
521 struct node *chosen;
522 struct property *prop;
523
524 chosen = get_node_by_path(dt, "/chosen");
525 if (!chosen)
526 return;
527
528 prop = get_property(chosen, "interrupt-controller");
529 if (prop)
530 FAIL(c, "/chosen has obsolete \"interrupt-controller\" "
531 "property");
532}
533TREE_CHECK(obsolete_chosen_interrupt_controller, NULL, WARN);
534
David Gibsonb16a2bd2007-11-22 14:38:07 +1100535static struct check *check_table[] = {
536 &duplicate_node_names, &duplicate_property_names,
David Gibsonfa5b5202008-02-27 13:45:13 +1100537 &node_name_chars, &node_name_format, &property_name_chars,
David Gibson459c9552007-12-05 09:40:23 +1100538 &name_is_string, &name_properties,
David Gibsonb16a2bd2007-11-22 14:38:07 +1100539 &explicit_phandles,
David Gibsonefbbef82007-12-05 10:43:50 +1100540 &phandle_references, &path_references,
David Gibsonc21acab2007-12-06 16:59:45 +1100541
542 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
David Gibsonfaf037f2007-12-06 17:01:07 +1100543 &device_type_is_string, &model_is_string, &status_is_string,
David Gibson7e089d92007-12-07 14:05:55 +1100544
545 &addr_size_cells, &reg_format, &ranges_format,
546
547 &avoid_default_addr_size,
David Gibsone4ffc142007-12-07 14:06:11 +1100548 &obsolete_chosen_interrupt_controller,
David Gibsonb16a2bd2007-11-22 14:38:07 +1100549};
550
David Gibson376ab6f2007-12-18 14:54:38 +1100551void process_checks(int force, struct boot_info *bi)
David Gibsonb16a2bd2007-11-22 14:38:07 +1100552{
David Gibson2d728162007-12-04 11:49:43 +1100553 struct node *dt = bi->dt;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100554 int i;
555 int error = 0;
556
557 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
558 struct check *c = check_table[i];
559
560 if (c->level != IGNORE)
561 error = error || run_check(c, dt);
562 }
563
564 if (error) {
565 if (!force) {
566 fprintf(stderr, "ERROR: Input tree has errors, aborting "
567 "(use -f to force output)\n");
568 exit(2);
569 } else if (quiet < 3) {
570 fprintf(stderr, "Warning: Input tree has errors, "
571 "output forced\n");
572 }
573 }
David Gibson2f1ccc32007-11-01 16:49:26 +1100574}