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
, const char *fmt
, ...) __attribute__((format (printf
, 2, 3)));
78 static inline void check_msg(struct check
*c
, const char *fmt
, ...)
83 if ((c
->warn
&& (quiet
< 1))
84 || (c
->error
&& (quiet
< 2))) {
85 fprintf(stderr
, "%s (%s): ",
86 (c
->error
) ? "ERROR" : "Warning", c
->name
);
87 vfprintf(stderr
, fmt
, ap
);
88 fprintf(stderr
, "\n");
93 #define FAIL(c, ...) \
95 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
96 (c)->status = FAILED; \
97 check_msg((c), __VA_ARGS__); \
100 static void check_nodes_props(struct check
*c
, struct dt_info
*dti
, struct node
*node
)
104 TRACE(c
, "%s", node
->fullpath
);
108 for_each_child(node
, child
)
109 check_nodes_props(c
, dti
, child
);
112 static bool run_check(struct check
*c
, struct dt_info
*dti
)
114 struct node
*dt
= dti
->dt
;
118 assert(!c
->inprogress
);
120 if (c
->status
!= UNCHECKED
)
123 c
->inprogress
= true;
125 for (i
= 0; i
< c
->num_prereqs
; i
++) {
126 struct check
*prq
= c
->prereq
[i
];
127 error
= error
|| run_check(prq
, dti
);
128 if (prq
->status
!= PASSED
) {
130 check_msg(c
, "Failed prerequisite '%s'",
135 if (c
->status
!= UNCHECKED
)
138 check_nodes_props(c
, dti
, dt
);
140 if (c
->status
== UNCHECKED
)
143 TRACE(c
, "\tCompleted, status %d", c
->status
);
146 c
->inprogress
= false;
147 if ((c
->status
!= PASSED
) && (c
->error
))
153 * Utility check functions
156 /* A check which always fails, for testing purposes only */
157 static inline void check_always_fail(struct check
*c
, struct dt_info
*dti
,
160 FAIL(c
, "always_fail check");
162 CHECK(always_fail
, check_always_fail
, NULL
);
164 static void check_is_string(struct check
*c
, struct dt_info
*dti
,
167 struct property
*prop
;
168 char *propname
= c
->data
;
170 prop
= get_property(node
, propname
);
172 return; /* Not present, assumed ok */
174 if (!data_is_one_string(prop
->val
))
175 FAIL(c
, "\"%s\" property in %s is not a string",
176 propname
, node
->fullpath
);
178 #define WARNING_IF_NOT_STRING(nm, propname) \
179 WARNING(nm, check_is_string, (propname))
180 #define ERROR_IF_NOT_STRING(nm, propname) \
181 ERROR(nm, check_is_string, (propname))
183 static void check_is_cell(struct check
*c
, struct dt_info
*dti
,
186 struct property
*prop
;
187 char *propname
= c
->data
;
189 prop
= get_property(node
, propname
);
191 return; /* Not present, assumed ok */
193 if (prop
->val
.len
!= sizeof(cell_t
))
194 FAIL(c
, "\"%s\" property in %s is not a single cell",
195 propname
, node
->fullpath
);
197 #define WARNING_IF_NOT_CELL(nm, propname) \
198 WARNING(nm, check_is_cell, (propname))
199 #define ERROR_IF_NOT_CELL(nm, propname) \
200 ERROR(nm, check_is_cell, (propname))
203 * Structural check functions
206 static void check_duplicate_node_names(struct check
*c
, struct dt_info
*dti
,
209 struct node
*child
, *child2
;
211 for_each_child(node
, child
)
212 for (child2
= child
->next_sibling
;
214 child2
= child2
->next_sibling
)
215 if (streq(child
->name
, child2
->name
))
216 FAIL(c
, "Duplicate node name %s",
219 ERROR(duplicate_node_names
, check_duplicate_node_names
, NULL
);
221 static void check_duplicate_property_names(struct check
*c
, struct dt_info
*dti
,
224 struct property
*prop
, *prop2
;
226 for_each_property(node
, prop
) {
227 for (prop2
= prop
->next
; prop2
; prop2
= prop2
->next
) {
230 if (streq(prop
->name
, prop2
->name
))
231 FAIL(c
, "Duplicate property name %s in %s",
232 prop
->name
, node
->fullpath
);
236 ERROR(duplicate_property_names
, check_duplicate_property_names
, NULL
);
238 #define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
239 #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
240 #define DIGITS "0123456789"
241 #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
243 static void check_node_name_chars(struct check
*c
, struct dt_info
*dti
,
246 int n
= strspn(node
->name
, c
->data
);
248 if (n
< strlen(node
->name
))
249 FAIL(c
, "Bad character '%c' in node %s",
250 node
->name
[n
], node
->fullpath
);
252 ERROR(node_name_chars
, check_node_name_chars
, PROPNODECHARS
"@");
254 static void check_node_name_format(struct check
*c
, struct dt_info
*dti
,
257 if (strchr(get_unitname(node
), '@'))
258 FAIL(c
, "Node %s has multiple '@' characters in name",
261 ERROR(node_name_format
, check_node_name_format
, NULL
, &node_name_chars
);
263 static void check_unit_address_vs_reg(struct check
*c
, struct dt_info
*dti
,
266 const char *unitname
= get_unitname(node
);
267 struct property
*prop
= get_property(node
, "reg");
270 prop
= get_property(node
, "ranges");
271 if (prop
&& !prop
->val
.len
)
277 FAIL(c
, "Node %s has a reg or ranges property, but no unit name",
281 FAIL(c
, "Node %s has a unit name, but no reg property",
285 WARNING(unit_address_vs_reg
, check_unit_address_vs_reg
, NULL
);
287 static void check_property_name_chars(struct check
*c
, struct dt_info
*dti
,
290 struct property
*prop
;
292 for_each_property(node
, prop
) {
293 int n
= strspn(prop
->name
, c
->data
);
295 if (n
< strlen(prop
->name
))
296 FAIL(c
, "Bad character '%c' in property name \"%s\", node %s",
297 prop
->name
[n
], prop
->name
, node
->fullpath
);
300 ERROR(property_name_chars
, check_property_name_chars
, PROPNODECHARS
);
302 #define DESCLABEL_FMT "%s%s%s%s%s"
303 #define DESCLABEL_ARGS(node,prop,mark) \
304 ((mark) ? "value of " : ""), \
305 ((prop) ? "'" : ""), \
306 ((prop) ? (prop)->name : ""), \
307 ((prop) ? "' in " : ""), (node)->fullpath
309 static void check_duplicate_label(struct check
*c
, struct dt_info
*dti
,
310 const char *label
, struct node
*node
,
311 struct property
*prop
, struct marker
*mark
)
313 struct node
*dt
= dti
->dt
;
314 struct node
*othernode
= NULL
;
315 struct property
*otherprop
= NULL
;
316 struct marker
*othermark
= NULL
;
318 othernode
= get_node_by_label(dt
, label
);
321 otherprop
= get_property_by_label(dt
, label
, &othernode
);
323 othermark
= get_marker_label(dt
, label
, &othernode
,
329 if ((othernode
!= node
) || (otherprop
!= prop
) || (othermark
!= mark
))
330 FAIL(c
, "Duplicate label '%s' on " DESCLABEL_FMT
331 " and " DESCLABEL_FMT
,
332 label
, DESCLABEL_ARGS(node
, prop
, mark
),
333 DESCLABEL_ARGS(othernode
, otherprop
, othermark
));
336 static void check_duplicate_label_node(struct check
*c
, struct dt_info
*dti
,
340 struct property
*prop
;
342 for_each_label(node
->labels
, l
)
343 check_duplicate_label(c
, dti
, l
->label
, node
, NULL
, NULL
);
345 for_each_property(node
, prop
) {
346 struct marker
*m
= prop
->val
.markers
;
348 for_each_label(prop
->labels
, l
)
349 check_duplicate_label(c
, dti
, l
->label
, node
, prop
, NULL
);
351 for_each_marker_of_type(m
, LABEL
)
352 check_duplicate_label(c
, dti
, m
->ref
, node
, prop
, m
);
355 ERROR(duplicate_label
, check_duplicate_label_node
, NULL
);
357 static cell_t
check_phandle_prop(struct check
*c
, struct dt_info
*dti
,
358 struct node
*node
, const char *propname
)
360 struct node
*root
= dti
->dt
;
361 struct property
*prop
;
365 prop
= get_property(node
, propname
);
369 if (prop
->val
.len
!= sizeof(cell_t
)) {
370 FAIL(c
, "%s has bad length (%d) %s property",
371 node
->fullpath
, prop
->val
.len
, prop
->name
);
375 m
= prop
->val
.markers
;
376 for_each_marker_of_type(m
, REF_PHANDLE
) {
377 assert(m
->offset
== 0);
378 if (node
!= get_node_by_ref(root
, m
->ref
))
379 /* "Set this node's phandle equal to some
380 * other node's phandle". That's nonsensical
381 * by construction. */ {
382 FAIL(c
, "%s in %s is a reference to another node",
383 prop
->name
, node
->fullpath
);
385 /* But setting this node's phandle equal to its own
386 * phandle is allowed - that means allocate a unique
387 * phandle for this node, even if it's not otherwise
388 * referenced. The value will be filled in later, so
389 * we treat it as having no phandle data for now. */
393 phandle
= propval_cell(prop
);
395 if ((phandle
== 0) || (phandle
== -1)) {
396 FAIL(c
, "%s has bad value (0x%x) in %s property",
397 node
->fullpath
, phandle
, prop
->name
);
404 static void check_explicit_phandles(struct check
*c
, struct dt_info
*dti
,
407 struct node
*root
= dti
->dt
;
409 cell_t phandle
, linux_phandle
;
411 /* Nothing should have assigned phandles yet */
412 assert(!node
->phandle
);
414 phandle
= check_phandle_prop(c
, dti
, node
, "phandle");
416 linux_phandle
= check_phandle_prop(c
, dti
, node
, "linux,phandle");
418 if (!phandle
&& !linux_phandle
)
419 /* No valid phandles; nothing further to check */
422 if (linux_phandle
&& phandle
&& (phandle
!= linux_phandle
))
423 FAIL(c
, "%s has mismatching 'phandle' and 'linux,phandle'"
424 " properties", node
->fullpath
);
426 if (linux_phandle
&& !phandle
)
427 phandle
= linux_phandle
;
429 other
= get_node_by_phandle(root
, phandle
);
430 if (other
&& (other
!= node
)) {
431 FAIL(c
, "%s has duplicated phandle 0x%x (seen before at %s)",
432 node
->fullpath
, phandle
, other
->fullpath
);
436 node
->phandle
= phandle
;
438 ERROR(explicit_phandles
, check_explicit_phandles
, NULL
);
440 static void check_name_properties(struct check
*c
, struct dt_info
*dti
,
443 struct property
**pp
, *prop
= NULL
;
445 for (pp
= &node
->proplist
; *pp
; pp
= &((*pp
)->next
))
446 if (streq((*pp
)->name
, "name")) {
452 return; /* No name property, that's fine */
454 if ((prop
->val
.len
!= node
->basenamelen
+1)
455 || (memcmp(prop
->val
.val
, node
->name
, node
->basenamelen
) != 0)) {
456 FAIL(c
, "\"name\" property in %s is incorrect (\"%s\" instead"
457 " of base node name)", node
->fullpath
, prop
->val
.val
);
459 /* The name property is correct, and therefore redundant.
463 data_free(prop
->val
);
467 ERROR_IF_NOT_STRING(name_is_string
, "name");
468 ERROR(name_properties
, check_name_properties
, NULL
, &name_is_string
);
471 * Reference fixup functions
474 static void fixup_phandle_references(struct check
*c
, struct dt_info
*dti
,
477 struct node
*dt
= dti
->dt
;
478 struct property
*prop
;
480 for_each_property(node
, prop
) {
481 struct marker
*m
= prop
->val
.markers
;
482 struct node
*refnode
;
485 for_each_marker_of_type(m
, REF_PHANDLE
) {
486 assert(m
->offset
+ sizeof(cell_t
) <= prop
->val
.len
);
488 refnode
= get_node_by_ref(dt
, m
->ref
);
490 if (!(dti
->dtsflags
& DTSF_PLUGIN
))
491 FAIL(c
, "Reference to non-existent node or "
492 "label \"%s\"\n", m
->ref
);
493 else /* mark the entry as unresolved */
494 *((cell_t
*)(prop
->val
.val
+ m
->offset
)) =
495 cpu_to_fdt32(0xffffffff);
499 phandle
= get_node_phandle(dt
, refnode
);
500 *((cell_t
*)(prop
->val
.val
+ m
->offset
)) = cpu_to_fdt32(phandle
);
504 ERROR(phandle_references
, fixup_phandle_references
, NULL
,
505 &duplicate_node_names
, &explicit_phandles
);
507 static void fixup_path_references(struct check
*c
, struct dt_info
*dti
,
510 struct node
*dt
= dti
->dt
;
511 struct property
*prop
;
513 for_each_property(node
, prop
) {
514 struct marker
*m
= prop
->val
.markers
;
515 struct node
*refnode
;
518 for_each_marker_of_type(m
, REF_PATH
) {
519 assert(m
->offset
<= prop
->val
.len
);
521 refnode
= get_node_by_ref(dt
, m
->ref
);
523 FAIL(c
, "Reference to non-existent node or label \"%s\"\n",
528 path
= refnode
->fullpath
;
529 prop
->val
= data_insert_at_marker(prop
->val
, m
, path
,
534 ERROR(path_references
, fixup_path_references
, NULL
, &duplicate_node_names
);
539 WARNING_IF_NOT_CELL(address_cells_is_cell
, "#address-cells");
540 WARNING_IF_NOT_CELL(size_cells_is_cell
, "#size-cells");
541 WARNING_IF_NOT_CELL(interrupt_cells_is_cell
, "#interrupt-cells");
543 WARNING_IF_NOT_STRING(device_type_is_string
, "device_type");
544 WARNING_IF_NOT_STRING(model_is_string
, "model");
545 WARNING_IF_NOT_STRING(status_is_string
, "status");
547 static void fixup_addr_size_cells(struct check
*c
, struct dt_info
*dti
,
550 struct property
*prop
;
552 node
->addr_cells
= -1;
553 node
->size_cells
= -1;
555 prop
= get_property(node
, "#address-cells");
557 node
->addr_cells
= propval_cell(prop
);
559 prop
= get_property(node
, "#size-cells");
561 node
->size_cells
= propval_cell(prop
);
563 WARNING(addr_size_cells
, fixup_addr_size_cells
, NULL
,
564 &address_cells_is_cell
, &size_cells_is_cell
);
566 #define node_addr_cells(n) \
567 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
568 #define node_size_cells(n) \
569 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
571 static void check_reg_format(struct check
*c
, struct dt_info
*dti
,
574 struct property
*prop
;
575 int addr_cells
, size_cells
, entrylen
;
577 prop
= get_property(node
, "reg");
579 return; /* No "reg", that's fine */
582 FAIL(c
, "Root node has a \"reg\" property");
586 if (prop
->val
.len
== 0)
587 FAIL(c
, "\"reg\" property in %s is empty", node
->fullpath
);
589 addr_cells
= node_addr_cells(node
->parent
);
590 size_cells
= node_size_cells(node
->parent
);
591 entrylen
= (addr_cells
+ size_cells
) * sizeof(cell_t
);
593 if (!entrylen
|| (prop
->val
.len
% entrylen
) != 0)
594 FAIL(c
, "\"reg\" property in %s has invalid length (%d bytes) "
595 "(#address-cells == %d, #size-cells == %d)",
596 node
->fullpath
, prop
->val
.len
, addr_cells
, size_cells
);
598 WARNING(reg_format
, check_reg_format
, NULL
, &addr_size_cells
);
600 static void check_ranges_format(struct check
*c
, struct dt_info
*dti
,
603 struct property
*prop
;
604 int c_addr_cells
, p_addr_cells
, c_size_cells
, p_size_cells
, entrylen
;
606 prop
= get_property(node
, "ranges");
611 FAIL(c
, "Root node has a \"ranges\" property");
615 p_addr_cells
= node_addr_cells(node
->parent
);
616 p_size_cells
= node_size_cells(node
->parent
);
617 c_addr_cells
= node_addr_cells(node
);
618 c_size_cells
= node_size_cells(node
);
619 entrylen
= (p_addr_cells
+ c_addr_cells
+ c_size_cells
) * sizeof(cell_t
);
621 if (prop
->val
.len
== 0) {
622 if (p_addr_cells
!= c_addr_cells
)
623 FAIL(c
, "%s has empty \"ranges\" property but its "
624 "#address-cells (%d) differs from %s (%d)",
625 node
->fullpath
, c_addr_cells
, node
->parent
->fullpath
,
627 if (p_size_cells
!= c_size_cells
)
628 FAIL(c
, "%s has empty \"ranges\" property but its "
629 "#size-cells (%d) differs from %s (%d)",
630 node
->fullpath
, c_size_cells
, node
->parent
->fullpath
,
632 } else if ((prop
->val
.len
% entrylen
) != 0) {
633 FAIL(c
, "\"ranges\" property in %s has invalid length (%d bytes) "
634 "(parent #address-cells == %d, child #address-cells == %d, "
635 "#size-cells == %d)", node
->fullpath
, prop
->val
.len
,
636 p_addr_cells
, c_addr_cells
, c_size_cells
);
639 WARNING(ranges_format
, check_ranges_format
, NULL
, &addr_size_cells
);
644 static void check_avoid_default_addr_size(struct check
*c
, struct dt_info
*dti
,
647 struct property
*reg
, *ranges
;
650 return; /* Ignore root node */
652 reg
= get_property(node
, "reg");
653 ranges
= get_property(node
, "ranges");
658 if (node
->parent
->addr_cells
== -1)
659 FAIL(c
, "Relying on default #address-cells value for %s",
662 if (node
->parent
->size_cells
== -1)
663 FAIL(c
, "Relying on default #size-cells value for %s",
666 WARNING(avoid_default_addr_size
, check_avoid_default_addr_size
, NULL
,
669 static void check_obsolete_chosen_interrupt_controller(struct check
*c
,
673 struct node
*dt
= dti
->dt
;
675 struct property
*prop
;
681 chosen
= get_node_by_path(dt
, "/chosen");
685 prop
= get_property(chosen
, "interrupt-controller");
687 FAIL(c
, "/chosen has obsolete \"interrupt-controller\" "
690 WARNING(obsolete_chosen_interrupt_controller
,
691 check_obsolete_chosen_interrupt_controller
, NULL
);
693 static struct check
*check_table
[] = {
694 &duplicate_node_names
, &duplicate_property_names
,
695 &node_name_chars
, &node_name_format
, &property_name_chars
,
696 &name_is_string
, &name_properties
,
701 &phandle_references
, &path_references
,
703 &address_cells_is_cell
, &size_cells_is_cell
, &interrupt_cells_is_cell
,
704 &device_type_is_string
, &model_is_string
, &status_is_string
,
706 &addr_size_cells
, ®_format
, &ranges_format
,
708 &unit_address_vs_reg
,
710 &avoid_default_addr_size
,
711 &obsolete_chosen_interrupt_controller
,
716 static void enable_warning_error(struct check
*c
, bool warn
, bool error
)
720 /* Raising level, also raise it for prereqs */
721 if ((warn
&& !c
->warn
) || (error
&& !c
->error
))
722 for (i
= 0; i
< c
->num_prereqs
; i
++)
723 enable_warning_error(c
->prereq
[i
], warn
, error
);
725 c
->warn
= c
->warn
|| warn
;
726 c
->error
= c
->error
|| error
;
729 static void disable_warning_error(struct check
*c
, bool warn
, bool error
)
733 /* Lowering level, also lower it for things this is the prereq
735 if ((warn
&& c
->warn
) || (error
&& c
->error
)) {
736 for (i
= 0; i
< ARRAY_SIZE(check_table
); i
++) {
737 struct check
*cc
= check_table
[i
];
740 for (j
= 0; j
< cc
->num_prereqs
; j
++)
741 if (cc
->prereq
[j
] == c
)
742 disable_warning_error(cc
, warn
, error
);
746 c
->warn
= c
->warn
&& !warn
;
747 c
->error
= c
->error
&& !error
;
750 void parse_checks_option(bool warn
, bool error
, const char *arg
)
753 const char *name
= arg
;
756 if ((strncmp(arg
, "no-", 3) == 0)
757 || (strncmp(arg
, "no_", 3) == 0)) {
762 for (i
= 0; i
< ARRAY_SIZE(check_table
); i
++) {
763 struct check
*c
= check_table
[i
];
765 if (streq(c
->name
, name
)) {
767 enable_warning_error(c
, warn
, error
);
769 disable_warning_error(c
, warn
, error
);
774 die("Unrecognized check name \"%s\"\n", name
);
777 void process_checks(bool force
, struct dt_info
*dti
)
782 for (i
= 0; i
< ARRAY_SIZE(check_table
); i
++) {
783 struct check
*c
= check_table
[i
];
785 if (c
->warn
|| c
->error
)
786 error
= error
|| run_check(c
, dti
);
791 fprintf(stderr
, "ERROR: Input tree has errors, aborting "
792 "(use -f to force output)\n");
794 } else if (quiet
< 3) {
795 fprintf(stderr
, "Warning: Input tree has errors, "