blob: 40879677c8c3af28bc646a98bd2c67c73ad55989 [file] [log] [blame]
Tom Rinibd391732017-09-23 12:52:44 -04001/*
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
23#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 checkstatus {
35 UNCHECKED = 0,
36 PREREQ,
37 PASSED,
38 FAILED,
39};
40
41struct check;
42
43typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
44
45struct check {
46 const char *name;
47 check_fn fn;
48 void *data;
49 bool warn, error;
50 enum checkstatus status;
51 bool inprogress;
52 int num_prereqs;
53 struct check **prereq;
54};
55
Rob Herringcd5f70d2018-05-19 14:13:53 +020056#define CHECK_ENTRY(nm_, fn_, d_, w_, e_, ...) \
57 static struct check *nm_##_prereqs[] = { __VA_ARGS__ }; \
58 static struct check nm_ = { \
59 .name = #nm_, \
60 .fn = (fn_), \
61 .data = (d_), \
62 .warn = (w_), \
63 .error = (e_), \
Tom Rinibd391732017-09-23 12:52:44 -040064 .status = UNCHECKED, \
Rob Herringcd5f70d2018-05-19 14:13:53 +020065 .num_prereqs = ARRAY_SIZE(nm_##_prereqs), \
66 .prereq = nm_##_prereqs, \
Tom Rinibd391732017-09-23 12:52:44 -040067 };
Rob Herringcd5f70d2018-05-19 14:13:53 +020068#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__)
Tom Rinibd391732017-09-23 12:52:44 -040074
Rob Herringcd5f70d2018-05-19 14:13:53 +020075static inline void PRINTF(5, 6) check_msg(struct check *c, struct dt_info *dti,
76 struct node *node,
77 struct property *prop,
Tom Rini56aeab52017-09-23 17:30:53 -040078 const char *fmt, ...)
Tom Rinibd391732017-09-23 12:52:44 -040079{
80 va_list ap;
81 va_start(ap, fmt);
82
83 if ((c->warn && (quiet < 1))
84 || (c->error && (quiet < 2))) {
85 fprintf(stderr, "%s: %s (%s): ",
86 strcmp(dti->outname, "-") ? dti->outname : "<stdout>",
87 (c->error) ? "ERROR" : "Warning", c->name);
Rob Herringcd5f70d2018-05-19 14:13:53 +020088 if (node) {
89 fprintf(stderr, "%s", node->fullpath);
90 if (prop)
91 fprintf(stderr, ":%s", prop->name);
92 fputs(": ", stderr);
93 }
Tom Rinibd391732017-09-23 12:52:44 -040094 vfprintf(stderr, fmt, ap);
95 fprintf(stderr, "\n");
96 }
97 va_end(ap);
98}
99
Rob Herringcd5f70d2018-05-19 14:13:53 +0200100#define FAIL(c, dti, node, ...) \
101 do { \
102 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
103 (c)->status = FAILED; \
104 check_msg((c), dti, node, NULL, __VA_ARGS__); \
105 } while (0)
106
107#define FAIL_PROP(c, dti, node, prop, ...) \
Tom Rinibd391732017-09-23 12:52:44 -0400108 do { \
109 TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
110 (c)->status = FAILED; \
Rob Herringcd5f70d2018-05-19 14:13:53 +0200111 check_msg((c), dti, node, prop, __VA_ARGS__); \
Tom Rinibd391732017-09-23 12:52:44 -0400112 } while (0)
113
Rob Herringcd5f70d2018-05-19 14:13:53 +0200114
Tom Rinibd391732017-09-23 12:52:44 -0400115static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
116{
117 struct node *child;
118
119 TRACE(c, "%s", node->fullpath);
120 if (c->fn)
121 c->fn(c, dti, node);
122
123 for_each_child(node, child)
124 check_nodes_props(c, dti, child);
125}
126
127static bool run_check(struct check *c, struct dt_info *dti)
128{
129 struct node *dt = dti->dt;
130 bool error = false;
131 int i;
132
133 assert(!c->inprogress);
134
135 if (c->status != UNCHECKED)
136 goto out;
137
138 c->inprogress = true;
139
140 for (i = 0; i < c->num_prereqs; i++) {
141 struct check *prq = c->prereq[i];
142 error = error || run_check(prq, dti);
143 if (prq->status != PASSED) {
144 c->status = PREREQ;
Rob Herringcd5f70d2018-05-19 14:13:53 +0200145 check_msg(c, dti, NULL, NULL, "Failed prerequisite '%s'",
Tom Rinibd391732017-09-23 12:52:44 -0400146 c->prereq[i]->name);
147 }
148 }
149
150 if (c->status != UNCHECKED)
151 goto out;
152
153 check_nodes_props(c, dti, dt);
154
155 if (c->status == UNCHECKED)
156 c->status = PASSED;
157
158 TRACE(c, "\tCompleted, status %d", c->status);
159
160out:
161 c->inprogress = false;
162 if ((c->status != PASSED) && (c->error))
163 error = true;
164 return error;
165}
166
167/*
168 * Utility check functions
169 */
170
171/* A check which always fails, for testing purposes only */
172static inline void check_always_fail(struct check *c, struct dt_info *dti,
173 struct node *node)
174{
Rob Herringcd5f70d2018-05-19 14:13:53 +0200175 FAIL(c, dti, node, "always_fail check");
Tom Rinibd391732017-09-23 12:52:44 -0400176}
177CHECK(always_fail, check_always_fail, NULL);
178
179static void check_is_string(struct check *c, struct dt_info *dti,
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))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200190 FAIL_PROP(c, dti, node, prop, "property is not a string");
Tom Rinibd391732017-09-23 12:52:44 -0400191}
192#define WARNING_IF_NOT_STRING(nm, propname) \
193 WARNING(nm, check_is_string, (propname))
194#define ERROR_IF_NOT_STRING(nm, propname) \
195 ERROR(nm, check_is_string, (propname))
196
Rob Herringcd5f70d2018-05-19 14:13:53 +0200197static void check_is_string_list(struct check *c, struct dt_info *dti,
198 struct node *node)
199{
200 int rem, l;
201 struct property *prop;
202 char *propname = c->data;
203 char *str;
204
205 prop = get_property(node, propname);
206 if (!prop)
207 return; /* Not present, assumed ok */
208
209 str = prop->val.val;
210 rem = prop->val.len;
211 while (rem > 0) {
212 l = strnlen(str, rem);
213 if (l == rem) {
214 FAIL_PROP(c, dti, node, prop, "property is not a string list");
215 break;
216 }
217 rem -= l + 1;
218 str += l + 1;
219 }
220}
221#define WARNING_IF_NOT_STRING_LIST(nm, propname) \
222 WARNING(nm, check_is_string_list, (propname))
223#define ERROR_IF_NOT_STRING_LIST(nm, propname) \
224 ERROR(nm, check_is_string_list, (propname))
225
Tom Rinibd391732017-09-23 12:52:44 -0400226static void check_is_cell(struct check *c, struct dt_info *dti,
227 struct node *node)
228{
229 struct property *prop;
230 char *propname = c->data;
231
232 prop = get_property(node, propname);
233 if (!prop)
234 return; /* Not present, assumed ok */
235
236 if (prop->val.len != sizeof(cell_t))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200237 FAIL_PROP(c, dti, node, prop, "property is not a single cell");
Tom Rinibd391732017-09-23 12:52:44 -0400238}
239#define WARNING_IF_NOT_CELL(nm, propname) \
240 WARNING(nm, check_is_cell, (propname))
241#define ERROR_IF_NOT_CELL(nm, propname) \
242 ERROR(nm, check_is_cell, (propname))
243
244/*
245 * Structural check functions
246 */
247
248static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
249 struct node *node)
250{
251 struct node *child, *child2;
252
253 for_each_child(node, child)
254 for (child2 = child->next_sibling;
255 child2;
256 child2 = child2->next_sibling)
257 if (streq(child->name, child2->name))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200258 FAIL(c, dti, node, "Duplicate node name");
Tom Rinibd391732017-09-23 12:52:44 -0400259}
260ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
261
262static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
263 struct node *node)
264{
265 struct property *prop, *prop2;
266
267 for_each_property(node, prop) {
268 for (prop2 = prop->next; prop2; prop2 = prop2->next) {
269 if (prop2->deleted)
270 continue;
271 if (streq(prop->name, prop2->name))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200272 FAIL_PROP(c, dti, node, prop, "Duplicate property name");
Tom Rinibd391732017-09-23 12:52:44 -0400273 }
274 }
275}
276ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
277
278#define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
279#define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
280#define DIGITS "0123456789"
281#define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
282#define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
283
284static void check_node_name_chars(struct check *c, struct dt_info *dti,
285 struct node *node)
286{
287 int n = strspn(node->name, c->data);
288
289 if (n < strlen(node->name))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200290 FAIL(c, dti, node, "Bad character '%c' in node name",
291 node->name[n]);
Tom Rinibd391732017-09-23 12:52:44 -0400292}
293ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
294
295static void check_node_name_chars_strict(struct check *c, struct dt_info *dti,
296 struct node *node)
297{
298 int n = strspn(node->name, c->data);
299
300 if (n < node->basenamelen)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200301 FAIL(c, dti, node, "Character '%c' not recommended in node name",
302 node->name[n]);
Tom Rinibd391732017-09-23 12:52:44 -0400303}
304CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
305
306static void check_node_name_format(struct check *c, struct dt_info *dti,
307 struct node *node)
308{
309 if (strchr(get_unitname(node), '@'))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200310 FAIL(c, dti, node, "multiple '@' characters in node name");
Tom Rinibd391732017-09-23 12:52:44 -0400311}
312ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
313
314static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
315 struct node *node)
316{
317 const char *unitname = get_unitname(node);
318 struct property *prop = get_property(node, "reg");
319
320 if (!prop) {
321 prop = get_property(node, "ranges");
322 if (prop && !prop->val.len)
323 prop = NULL;
324 }
325
326 if (prop) {
327 if (!unitname[0])
Rob Herringcd5f70d2018-05-19 14:13:53 +0200328 FAIL(c, dti, node, "node has a reg or ranges property, but no unit name");
Tom Rinibd391732017-09-23 12:52:44 -0400329 } else {
330 if (unitname[0])
Rob Herringcd5f70d2018-05-19 14:13:53 +0200331 FAIL(c, dti, node, "node has a unit name, but no reg property");
Tom Rinibd391732017-09-23 12:52:44 -0400332 }
333}
334WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
335
336static void check_property_name_chars(struct check *c, struct dt_info *dti,
337 struct node *node)
338{
339 struct property *prop;
340
341 for_each_property(node, prop) {
342 int n = strspn(prop->name, c->data);
343
344 if (n < strlen(prop->name))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200345 FAIL_PROP(c, dti, node, prop, "Bad character '%c' in property name",
346 prop->name[n]);
Tom Rinibd391732017-09-23 12:52:44 -0400347 }
348}
349ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
350
351static void check_property_name_chars_strict(struct check *c,
352 struct dt_info *dti,
353 struct node *node)
354{
355 struct property *prop;
356
357 for_each_property(node, prop) {
358 const char *name = prop->name;
359 int n = strspn(name, c->data);
360
361 if (n == strlen(prop->name))
362 continue;
363
364 /* Certain names are whitelisted */
365 if (streq(name, "device_type"))
366 continue;
367
368 /*
369 * # is only allowed at the beginning of property names not counting
370 * the vendor prefix.
371 */
372 if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) {
373 name += n + 1;
374 n = strspn(name, c->data);
375 }
376 if (n < strlen(name))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200377 FAIL_PROP(c, dti, node, prop, "Character '%c' not recommended in property name",
378 name[n]);
Tom Rinibd391732017-09-23 12:52:44 -0400379 }
380}
381CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
382
383#define DESCLABEL_FMT "%s%s%s%s%s"
384#define DESCLABEL_ARGS(node,prop,mark) \
385 ((mark) ? "value of " : ""), \
386 ((prop) ? "'" : ""), \
387 ((prop) ? (prop)->name : ""), \
388 ((prop) ? "' in " : ""), (node)->fullpath
389
390static void check_duplicate_label(struct check *c, struct dt_info *dti,
391 const char *label, struct node *node,
392 struct property *prop, struct marker *mark)
393{
394 struct node *dt = dti->dt;
395 struct node *othernode = NULL;
396 struct property *otherprop = NULL;
397 struct marker *othermark = NULL;
398
399 othernode = get_node_by_label(dt, label);
400
401 if (!othernode)
402 otherprop = get_property_by_label(dt, label, &othernode);
403 if (!othernode)
404 othermark = get_marker_label(dt, label, &othernode,
405 &otherprop);
406
407 if (!othernode)
408 return;
409
410 if ((othernode != node) || (otherprop != prop) || (othermark != mark))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200411 FAIL(c, dti, node, "Duplicate label '%s' on " DESCLABEL_FMT
Tom Rinibd391732017-09-23 12:52:44 -0400412 " and " DESCLABEL_FMT,
413 label, DESCLABEL_ARGS(node, prop, mark),
414 DESCLABEL_ARGS(othernode, otherprop, othermark));
415}
416
417static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
418 struct node *node)
419{
420 struct label *l;
421 struct property *prop;
422
423 for_each_label(node->labels, l)
424 check_duplicate_label(c, dti, l->label, node, NULL, NULL);
425
426 for_each_property(node, prop) {
427 struct marker *m = prop->val.markers;
428
429 for_each_label(prop->labels, l)
430 check_duplicate_label(c, dti, l->label, node, prop, NULL);
431
432 for_each_marker_of_type(m, LABEL)
433 check_duplicate_label(c, dti, m->ref, node, prop, m);
434 }
435}
436ERROR(duplicate_label, check_duplicate_label_node, NULL);
437
438static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
439 struct node *node, const char *propname)
440{
441 struct node *root = dti->dt;
442 struct property *prop;
443 struct marker *m;
444 cell_t phandle;
445
446 prop = get_property(node, propname);
447 if (!prop)
448 return 0;
449
450 if (prop->val.len != sizeof(cell_t)) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200451 FAIL_PROP(c, dti, node, prop, "bad length (%d) %s property",
452 prop->val.len, prop->name);
Tom Rinibd391732017-09-23 12:52:44 -0400453 return 0;
454 }
455
456 m = prop->val.markers;
457 for_each_marker_of_type(m, REF_PHANDLE) {
458 assert(m->offset == 0);
459 if (node != get_node_by_ref(root, m->ref))
460 /* "Set this node's phandle equal to some
461 * other node's phandle". That's nonsensical
462 * by construction. */ {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200463 FAIL(c, dti, node, "%s is a reference to another node",
464 prop->name);
Tom Rinibd391732017-09-23 12:52:44 -0400465 }
466 /* But setting this node's phandle equal to its own
467 * phandle is allowed - that means allocate a unique
468 * phandle for this node, even if it's not otherwise
469 * referenced. The value will be filled in later, so
470 * we treat it as having no phandle data for now. */
471 return 0;
472 }
473
474 phandle = propval_cell(prop);
475
476 if ((phandle == 0) || (phandle == -1)) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200477 FAIL_PROP(c, dti, node, prop, "bad value (0x%x) in %s property",
478 phandle, prop->name);
Tom Rinibd391732017-09-23 12:52:44 -0400479 return 0;
480 }
481
482 return phandle;
483}
484
485static void check_explicit_phandles(struct check *c, struct dt_info *dti,
486 struct node *node)
487{
488 struct node *root = dti->dt;
489 struct node *other;
490 cell_t phandle, linux_phandle;
491
492 /* Nothing should have assigned phandles yet */
493 assert(!node->phandle);
494
495 phandle = check_phandle_prop(c, dti, node, "phandle");
496
497 linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");
498
499 if (!phandle && !linux_phandle)
500 /* No valid phandles; nothing further to check */
501 return;
502
503 if (linux_phandle && phandle && (phandle != linux_phandle))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200504 FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'"
505 " properties");
Tom Rinibd391732017-09-23 12:52:44 -0400506
507 if (linux_phandle && !phandle)
508 phandle = linux_phandle;
509
510 other = get_node_by_phandle(root, phandle);
511 if (other && (other != node)) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200512 FAIL(c, dti, node, "duplicated phandle 0x%x (seen before at %s)",
513 phandle, other->fullpath);
Tom Rinibd391732017-09-23 12:52:44 -0400514 return;
515 }
516
517 node->phandle = phandle;
518}
519ERROR(explicit_phandles, check_explicit_phandles, NULL);
520
521static void check_name_properties(struct check *c, struct dt_info *dti,
522 struct node *node)
523{
524 struct property **pp, *prop = NULL;
525
526 for (pp = &node->proplist; *pp; pp = &((*pp)->next))
527 if (streq((*pp)->name, "name")) {
528 prop = *pp;
529 break;
530 }
531
532 if (!prop)
533 return; /* No name property, that's fine */
534
535 if ((prop->val.len != node->basenamelen+1)
536 || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200537 FAIL(c, dti, node, "\"name\" property is incorrect (\"%s\" instead"
538 " of base node name)", prop->val.val);
Tom Rinibd391732017-09-23 12:52:44 -0400539 } else {
540 /* The name property is correct, and therefore redundant.
541 * Delete it */
542 *pp = prop->next;
543 free(prop->name);
544 data_free(prop->val);
545 free(prop);
546 }
547}
548ERROR_IF_NOT_STRING(name_is_string, "name");
549ERROR(name_properties, check_name_properties, NULL, &name_is_string);
550
551/*
552 * Reference fixup functions
553 */
554
555static void fixup_phandle_references(struct check *c, struct dt_info *dti,
556 struct node *node)
557{
558 struct node *dt = dti->dt;
559 struct property *prop;
560
561 for_each_property(node, prop) {
562 struct marker *m = prop->val.markers;
563 struct node *refnode;
564 cell_t phandle;
565
566 for_each_marker_of_type(m, REF_PHANDLE) {
567 assert(m->offset + sizeof(cell_t) <= prop->val.len);
568
569 refnode = get_node_by_ref(dt, m->ref);
570 if (! refnode) {
571 if (!(dti->dtsflags & DTSF_PLUGIN))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200572 FAIL(c, dti, node, "Reference to non-existent node or "
Tom Rinibd391732017-09-23 12:52:44 -0400573 "label \"%s\"\n", m->ref);
574 else /* mark the entry as unresolved */
Tom Rini56aeab52017-09-23 17:30:53 -0400575 *((fdt32_t *)(prop->val.val + m->offset)) =
Tom Rinibd391732017-09-23 12:52:44 -0400576 cpu_to_fdt32(0xffffffff);
577 continue;
578 }
579
580 phandle = get_node_phandle(dt, refnode);
Tom Rini56aeab52017-09-23 17:30:53 -0400581 *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
Maxime Riparda96fe172020-01-21 10:23:17 +0000582
583 reference_node(refnode);
Tom Rinibd391732017-09-23 12:52:44 -0400584 }
585 }
586}
587ERROR(phandle_references, fixup_phandle_references, NULL,
588 &duplicate_node_names, &explicit_phandles);
589
590static void fixup_path_references(struct check *c, struct dt_info *dti,
591 struct node *node)
592{
593 struct node *dt = dti->dt;
594 struct property *prop;
595
596 for_each_property(node, prop) {
597 struct marker *m = prop->val.markers;
598 struct node *refnode;
599 char *path;
600
601 for_each_marker_of_type(m, REF_PATH) {
602 assert(m->offset <= prop->val.len);
603
604 refnode = get_node_by_ref(dt, m->ref);
605 if (!refnode) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200606 FAIL(c, dti, node, "Reference to non-existent node or label \"%s\"\n",
Tom Rinibd391732017-09-23 12:52:44 -0400607 m->ref);
608 continue;
609 }
610
611 path = refnode->fullpath;
612 prop->val = data_insert_at_marker(prop->val, m, path,
613 strlen(path) + 1);
Maxime Riparda96fe172020-01-21 10:23:17 +0000614
615 reference_node(refnode);
Tom Rinibd391732017-09-23 12:52:44 -0400616 }
617 }
618}
619ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
620
Maxime Riparda96fe172020-01-21 10:23:17 +0000621static void fixup_omit_unused_nodes(struct check *c, struct dt_info *dti,
622 struct node *node)
623{
624 if (node->omit_if_unused && !node->is_referenced)
625 delete_node(node);
626}
627ERROR(omit_unused_nodes, fixup_omit_unused_nodes, NULL, &phandle_references, &path_references);
628
Tom Rinibd391732017-09-23 12:52:44 -0400629/*
630 * Semantic checks
631 */
632WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
633WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
634WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
635
636WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
637WARNING_IF_NOT_STRING(model_is_string, "model");
638WARNING_IF_NOT_STRING(status_is_string, "status");
Rob Herringcd5f70d2018-05-19 14:13:53 +0200639WARNING_IF_NOT_STRING(label_is_string, "label");
640
641WARNING_IF_NOT_STRING_LIST(compatible_is_string_list, "compatible");
642
643static void check_names_is_string_list(struct check *c, struct dt_info *dti,
644 struct node *node)
645{
646 struct property *prop;
647
648 for_each_property(node, prop) {
649 const char *s = strrchr(prop->name, '-');
650 if (!s || !streq(s, "-names"))
651 continue;
652
653 c->data = prop->name;
654 check_is_string_list(c, dti, node);
655 }
656}
657WARNING(names_is_string_list, check_names_is_string_list, NULL);
658
659static void check_alias_paths(struct check *c, struct dt_info *dti,
660 struct node *node)
661{
662 struct property *prop;
663
664 if (!streq(node->name, "aliases"))
665 return;
666
667 for_each_property(node, prop) {
668 if (!prop->val.val || !get_node_by_path(dti->dt, prop->val.val)) {
669 FAIL_PROP(c, dti, node, prop, "aliases property is not a valid node (%s)",
670 prop->val.val);
671 continue;
672 }
673 if (strspn(prop->name, LOWERCASE DIGITS "-") != strlen(prop->name))
674 FAIL(c, dti, node, "aliases property name must include only lowercase and '-'");
675 }
676}
677WARNING(alias_paths, check_alias_paths, NULL);
Tom Rinibd391732017-09-23 12:52:44 -0400678
679static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
680 struct node *node)
681{
682 struct property *prop;
683
684 node->addr_cells = -1;
685 node->size_cells = -1;
686
687 prop = get_property(node, "#address-cells");
688 if (prop)
689 node->addr_cells = propval_cell(prop);
690
691 prop = get_property(node, "#size-cells");
692 if (prop)
693 node->size_cells = propval_cell(prop);
694}
695WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
696 &address_cells_is_cell, &size_cells_is_cell);
697
698#define node_addr_cells(n) \
699 (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
700#define node_size_cells(n) \
701 (((n)->size_cells == -1) ? 1 : (n)->size_cells)
702
703static void check_reg_format(struct check *c, struct dt_info *dti,
704 struct node *node)
705{
706 struct property *prop;
707 int addr_cells, size_cells, entrylen;
708
709 prop = get_property(node, "reg");
710 if (!prop)
711 return; /* No "reg", that's fine */
712
713 if (!node->parent) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200714 FAIL(c, dti, node, "Root node has a \"reg\" property");
Tom Rinibd391732017-09-23 12:52:44 -0400715 return;
716 }
717
718 if (prop->val.len == 0)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200719 FAIL_PROP(c, dti, node, prop, "property is empty");
Tom Rinibd391732017-09-23 12:52:44 -0400720
721 addr_cells = node_addr_cells(node->parent);
722 size_cells = node_size_cells(node->parent);
723 entrylen = (addr_cells + size_cells) * sizeof(cell_t);
724
725 if (!entrylen || (prop->val.len % entrylen) != 0)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200726 FAIL_PROP(c, dti, node, prop, "property has invalid length (%d bytes) "
727 "(#address-cells == %d, #size-cells == %d)",
728 prop->val.len, addr_cells, size_cells);
Tom Rinibd391732017-09-23 12:52:44 -0400729}
730WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
731
732static void check_ranges_format(struct check *c, struct dt_info *dti,
733 struct node *node)
734{
735 struct property *prop;
736 int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
737
738 prop = get_property(node, "ranges");
739 if (!prop)
740 return;
741
742 if (!node->parent) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200743 FAIL_PROP(c, dti, node, prop, "Root node has a \"ranges\" property");
Tom Rinibd391732017-09-23 12:52:44 -0400744 return;
745 }
746
747 p_addr_cells = node_addr_cells(node->parent);
748 p_size_cells = node_size_cells(node->parent);
749 c_addr_cells = node_addr_cells(node);
750 c_size_cells = node_size_cells(node);
751 entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
752
753 if (prop->val.len == 0) {
754 if (p_addr_cells != c_addr_cells)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200755 FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
756 "#address-cells (%d) differs from %s (%d)",
757 c_addr_cells, node->parent->fullpath,
758 p_addr_cells);
Tom Rinibd391732017-09-23 12:52:44 -0400759 if (p_size_cells != c_size_cells)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200760 FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
761 "#size-cells (%d) differs from %s (%d)",
762 c_size_cells, node->parent->fullpath,
763 p_size_cells);
Tom Rinibd391732017-09-23 12:52:44 -0400764 } else if ((prop->val.len % entrylen) != 0) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200765 FAIL_PROP(c, dti, node, prop, "\"ranges\" property has invalid length (%d bytes) "
766 "(parent #address-cells == %d, child #address-cells == %d, "
767 "#size-cells == %d)", prop->val.len,
768 p_addr_cells, c_addr_cells, c_size_cells);
Tom Rinibd391732017-09-23 12:52:44 -0400769 }
770}
771WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
772
Tom Rini0eb096d2017-09-23 17:31:59 -0400773static const struct bus_type pci_bus = {
774 .name = "PCI",
775};
776
777static void check_pci_bridge(struct check *c, struct dt_info *dti, struct node *node)
778{
779 struct property *prop;
780 cell_t *cells;
781
782 prop = get_property(node, "device_type");
783 if (!prop || !streq(prop->val.val, "pci"))
784 return;
785
786 node->bus = &pci_bus;
787
Rob Herringcd5f70d2018-05-19 14:13:53 +0200788 if (!strprefixeq(node->name, node->basenamelen, "pci") &&
789 !strprefixeq(node->name, node->basenamelen, "pcie"))
790 FAIL(c, dti, node, "node name is not \"pci\" or \"pcie\"");
Tom Rini0eb096d2017-09-23 17:31:59 -0400791
792 prop = get_property(node, "ranges");
793 if (!prop)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200794 FAIL(c, dti, node, "missing ranges for PCI bridge (or not a bridge)");
Tom Rini0eb096d2017-09-23 17:31:59 -0400795
796 if (node_addr_cells(node) != 3)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200797 FAIL(c, dti, node, "incorrect #address-cells for PCI bridge");
Tom Rini0eb096d2017-09-23 17:31:59 -0400798 if (node_size_cells(node) != 2)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200799 FAIL(c, dti, node, "incorrect #size-cells for PCI bridge");
Tom Rini0eb096d2017-09-23 17:31:59 -0400800
801 prop = get_property(node, "bus-range");
802 if (!prop) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200803 FAIL(c, dti, node, "missing bus-range for PCI bridge");
Tom Rini0eb096d2017-09-23 17:31:59 -0400804 return;
805 }
806 if (prop->val.len != (sizeof(cell_t) * 2)) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200807 FAIL_PROP(c, dti, node, prop, "value must be 2 cells");
Tom Rini0eb096d2017-09-23 17:31:59 -0400808 return;
809 }
810 cells = (cell_t *)prop->val.val;
811 if (fdt32_to_cpu(cells[0]) > fdt32_to_cpu(cells[1]))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200812 FAIL_PROP(c, dti, node, prop, "1st cell must be less than or equal to 2nd cell");
Tom Rini0eb096d2017-09-23 17:31:59 -0400813 if (fdt32_to_cpu(cells[1]) > 0xff)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200814 FAIL_PROP(c, dti, node, prop, "maximum bus number must be less than 256");
Tom Rini0eb096d2017-09-23 17:31:59 -0400815}
816WARNING(pci_bridge, check_pci_bridge, NULL,
817 &device_type_is_string, &addr_size_cells);
818
819static void check_pci_device_bus_num(struct check *c, struct dt_info *dti, struct node *node)
820{
821 struct property *prop;
822 unsigned int bus_num, min_bus, max_bus;
823 cell_t *cells;
824
825 if (!node->parent || (node->parent->bus != &pci_bus))
826 return;
827
828 prop = get_property(node, "reg");
829 if (!prop)
830 return;
831
832 cells = (cell_t *)prop->val.val;
833 bus_num = (fdt32_to_cpu(cells[0]) & 0x00ff0000) >> 16;
834
835 prop = get_property(node->parent, "bus-range");
836 if (!prop) {
837 min_bus = max_bus = 0;
838 } else {
839 cells = (cell_t *)prop->val.val;
840 min_bus = fdt32_to_cpu(cells[0]);
841 max_bus = fdt32_to_cpu(cells[0]);
842 }
843 if ((bus_num < min_bus) || (bus_num > max_bus))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200844 FAIL_PROP(c, dti, node, prop, "PCI bus number %d out of range, expected (%d - %d)",
845 bus_num, min_bus, max_bus);
Tom Rini0eb096d2017-09-23 17:31:59 -0400846}
847WARNING(pci_device_bus_num, check_pci_device_bus_num, NULL, &reg_format, &pci_bridge);
848
849static void check_pci_device_reg(struct check *c, struct dt_info *dti, struct node *node)
850{
851 struct property *prop;
852 const char *unitname = get_unitname(node);
853 char unit_addr[5];
854 unsigned int dev, func, reg;
855 cell_t *cells;
856
857 if (!node->parent || (node->parent->bus != &pci_bus))
858 return;
859
860 prop = get_property(node, "reg");
861 if (!prop) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200862 FAIL(c, dti, node, "missing PCI reg property");
Tom Rini0eb096d2017-09-23 17:31:59 -0400863 return;
864 }
865
866 cells = (cell_t *)prop->val.val;
867 if (cells[1] || cells[2])
Rob Herringcd5f70d2018-05-19 14:13:53 +0200868 FAIL_PROP(c, dti, node, prop, "PCI reg config space address cells 2 and 3 must be 0");
Tom Rini0eb096d2017-09-23 17:31:59 -0400869
870 reg = fdt32_to_cpu(cells[0]);
871 dev = (reg & 0xf800) >> 11;
872 func = (reg & 0x700) >> 8;
873
874 if (reg & 0xff000000)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200875 FAIL_PROP(c, dti, node, prop, "PCI reg address is not configuration space");
Tom Rini0eb096d2017-09-23 17:31:59 -0400876 if (reg & 0x000000ff)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200877 FAIL_PROP(c, dti, node, prop, "PCI reg config space address register number must be 0");
Tom Rini0eb096d2017-09-23 17:31:59 -0400878
879 if (func == 0) {
880 snprintf(unit_addr, sizeof(unit_addr), "%x", dev);
881 if (streq(unitname, unit_addr))
882 return;
883 }
884
885 snprintf(unit_addr, sizeof(unit_addr), "%x,%x", dev, func);
886 if (streq(unitname, unit_addr))
887 return;
888
Rob Herringcd5f70d2018-05-19 14:13:53 +0200889 FAIL(c, dti, node, "PCI unit address format error, expected \"%s\"",
890 unit_addr);
Tom Rini0eb096d2017-09-23 17:31:59 -0400891}
892WARNING(pci_device_reg, check_pci_device_reg, NULL, &reg_format, &pci_bridge);
893
894static const struct bus_type simple_bus = {
895 .name = "simple-bus",
896};
897
898static bool node_is_compatible(struct node *node, const char *compat)
899{
900 struct property *prop;
901 const char *str, *end;
902
903 prop = get_property(node, "compatible");
904 if (!prop)
905 return false;
906
907 for (str = prop->val.val, end = str + prop->val.len; str < end;
908 str += strnlen(str, end - str) + 1) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200909 if (strprefixeq(str, end - str, compat))
Tom Rini0eb096d2017-09-23 17:31:59 -0400910 return true;
911 }
912 return false;
913}
914
915static void check_simple_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
916{
917 if (node_is_compatible(node, "simple-bus"))
918 node->bus = &simple_bus;
919}
920WARNING(simple_bus_bridge, check_simple_bus_bridge, NULL, &addr_size_cells);
921
922static void check_simple_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
923{
924 struct property *prop;
925 const char *unitname = get_unitname(node);
926 char unit_addr[17];
927 unsigned int size;
928 uint64_t reg = 0;
929 cell_t *cells = NULL;
930
931 if (!node->parent || (node->parent->bus != &simple_bus))
932 return;
933
934 prop = get_property(node, "reg");
935 if (prop)
936 cells = (cell_t *)prop->val.val;
937 else {
938 prop = get_property(node, "ranges");
939 if (prop && prop->val.len)
940 /* skip of child address */
941 cells = ((cell_t *)prop->val.val) + node_addr_cells(node);
942 }
943
944 if (!cells) {
945 if (node->parent->parent && !(node->bus == &simple_bus))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200946 FAIL(c, dti, node, "missing or empty reg/ranges property");
Tom Rini0eb096d2017-09-23 17:31:59 -0400947 return;
948 }
949
950 size = node_addr_cells(node->parent);
951 while (size--)
952 reg = (reg << 32) | fdt32_to_cpu(*(cells++));
953
954 snprintf(unit_addr, sizeof(unit_addr), "%"PRIx64, reg);
955 if (!streq(unitname, unit_addr))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200956 FAIL(c, dti, node, "simple-bus unit address format error, expected \"%s\"",
957 unit_addr);
Tom Rini0eb096d2017-09-23 17:31:59 -0400958}
959WARNING(simple_bus_reg, check_simple_bus_reg, NULL, &reg_format, &simple_bus_bridge);
960
961static void check_unit_address_format(struct check *c, struct dt_info *dti,
962 struct node *node)
963{
964 const char *unitname = get_unitname(node);
965
966 if (node->parent && node->parent->bus)
967 return;
968
969 if (!unitname[0])
970 return;
971
972 if (!strncmp(unitname, "0x", 2)) {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200973 FAIL(c, dti, node, "unit name should not have leading \"0x\"");
Tom Rini0eb096d2017-09-23 17:31:59 -0400974 /* skip over 0x for next test */
975 unitname += 2;
976 }
977 if (unitname[0] == '0' && isxdigit(unitname[1]))
Rob Herringcd5f70d2018-05-19 14:13:53 +0200978 FAIL(c, dti, node, "unit name should not have leading 0s");
Tom Rini0eb096d2017-09-23 17:31:59 -0400979}
980WARNING(unit_address_format, check_unit_address_format, NULL,
981 &node_name_format, &pci_bridge, &simple_bus_bridge);
982
Tom Rinibd391732017-09-23 12:52:44 -0400983/*
984 * Style checks
985 */
986static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
987 struct node *node)
988{
989 struct property *reg, *ranges;
990
991 if (!node->parent)
992 return; /* Ignore root node */
993
994 reg = get_property(node, "reg");
995 ranges = get_property(node, "ranges");
996
997 if (!reg && !ranges)
998 return;
999
1000 if (node->parent->addr_cells == -1)
Rob Herringcd5f70d2018-05-19 14:13:53 +02001001 FAIL(c, dti, node, "Relying on default #address-cells value");
Tom Rinibd391732017-09-23 12:52:44 -04001002
1003 if (node->parent->size_cells == -1)
Rob Herringcd5f70d2018-05-19 14:13:53 +02001004 FAIL(c, dti, node, "Relying on default #size-cells value");
Tom Rinibd391732017-09-23 12:52:44 -04001005}
1006WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
1007 &addr_size_cells);
1008
Rob Herringcd5f70d2018-05-19 14:13:53 +02001009static void check_avoid_unnecessary_addr_size(struct check *c, struct dt_info *dti,
1010 struct node *node)
1011{
1012 struct property *prop;
1013 struct node *child;
1014 bool has_reg = false;
1015
1016 if (!node->parent || node->addr_cells < 0 || node->size_cells < 0)
1017 return;
1018
1019 if (get_property(node, "ranges") || !node->children)
1020 return;
1021
1022 for_each_child(node, child) {
1023 prop = get_property(child, "reg");
1024 if (prop)
1025 has_reg = true;
1026 }
1027
1028 if (!has_reg)
1029 FAIL(c, dti, node, "unnecessary #address-cells/#size-cells without \"ranges\" or child \"reg\" property");
1030}
1031WARNING(avoid_unnecessary_addr_size, check_avoid_unnecessary_addr_size, NULL, &avoid_default_addr_size);
1032
Tom Rinibd391732017-09-23 12:52:44 -04001033static void check_obsolete_chosen_interrupt_controller(struct check *c,
1034 struct dt_info *dti,
1035 struct node *node)
1036{
1037 struct node *dt = dti->dt;
1038 struct node *chosen;
1039 struct property *prop;
1040
1041 if (node != dt)
1042 return;
1043
1044
1045 chosen = get_node_by_path(dt, "/chosen");
1046 if (!chosen)
1047 return;
1048
1049 prop = get_property(chosen, "interrupt-controller");
1050 if (prop)
Rob Herringcd5f70d2018-05-19 14:13:53 +02001051 FAIL_PROP(c, dti, node, prop,
1052 "/chosen has obsolete \"interrupt-controller\" property");
Tom Rinibd391732017-09-23 12:52:44 -04001053}
1054WARNING(obsolete_chosen_interrupt_controller,
1055 check_obsolete_chosen_interrupt_controller, NULL);
1056
Rob Herringcd5f70d2018-05-19 14:13:53 +02001057static void check_chosen_node_is_root(struct check *c, struct dt_info *dti,
1058 struct node *node)
1059{
1060 if (!streq(node->name, "chosen"))
1061 return;
1062
1063 if (node->parent != dti->dt)
1064 FAIL(c, dti, node, "chosen node must be at root node");
1065}
1066WARNING(chosen_node_is_root, check_chosen_node_is_root, NULL);
1067
1068static void check_chosen_node_bootargs(struct check *c, struct dt_info *dti,
1069 struct node *node)
1070{
1071 struct property *prop;
1072
1073 if (!streq(node->name, "chosen"))
1074 return;
1075
1076 prop = get_property(node, "bootargs");
1077 if (!prop)
1078 return;
1079
1080 c->data = prop->name;
1081 check_is_string(c, dti, node);
1082}
1083WARNING(chosen_node_bootargs, check_chosen_node_bootargs, NULL);
1084
1085static void check_chosen_node_stdout_path(struct check *c, struct dt_info *dti,
1086 struct node *node)
1087{
1088 struct property *prop;
1089
1090 if (!streq(node->name, "chosen"))
1091 return;
1092
1093 prop = get_property(node, "stdout-path");
1094 if (!prop) {
1095 prop = get_property(node, "linux,stdout-path");
1096 if (!prop)
1097 return;
1098 FAIL_PROP(c, dti, node, prop, "Use 'stdout-path' instead");
1099 }
1100
1101 c->data = prop->name;
1102 check_is_string(c, dti, node);
1103}
1104WARNING(chosen_node_stdout_path, check_chosen_node_stdout_path, NULL);
1105
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001106struct provider {
1107 const char *prop_name;
1108 const char *cell_name;
1109 bool optional;
1110};
1111
1112static void check_property_phandle_args(struct check *c,
1113 struct dt_info *dti,
1114 struct node *node,
1115 struct property *prop,
1116 const struct provider *provider)
1117{
1118 struct node *root = dti->dt;
1119 int cell, cellsize = 0;
1120
1121 if (prop->val.len % sizeof(cell_t)) {
Rob Herringcd5f70d2018-05-19 14:13:53 +02001122 FAIL_PROP(c, dti, node, prop,
1123 "property size (%d) is invalid, expected multiple of %zu",
1124 prop->val.len, sizeof(cell_t));
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001125 return;
1126 }
1127
1128 for (cell = 0; cell < prop->val.len / sizeof(cell_t); cell += cellsize + 1) {
1129 struct node *provider_node;
1130 struct property *cellprop;
1131 int phandle;
1132
1133 phandle = propval_cell_n(prop, cell);
1134 /*
1135 * Some bindings use a cell value 0 or -1 to skip over optional
1136 * entries when each index position has a specific definition.
1137 */
1138 if (phandle == 0 || phandle == -1) {
Rob Herring86abb7f2018-05-19 14:13:52 +02001139 /* Give up if this is an overlay with external references */
1140 if (dti->dtsflags & DTSF_PLUGIN)
1141 break;
1142
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001143 cellsize = 0;
1144 continue;
1145 }
1146
1147 /* If we have markers, verify the current cell is a phandle */
1148 if (prop->val.markers) {
1149 struct marker *m = prop->val.markers;
1150 for_each_marker_of_type(m, REF_PHANDLE) {
1151 if (m->offset == (cell * sizeof(cell_t)))
1152 break;
1153 }
1154 if (!m)
Rob Herringcd5f70d2018-05-19 14:13:53 +02001155 FAIL_PROP(c, dti, node, prop,
1156 "cell %d is not a phandle reference",
1157 cell);
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001158 }
1159
1160 provider_node = get_node_by_phandle(root, phandle);
1161 if (!provider_node) {
Rob Herringcd5f70d2018-05-19 14:13:53 +02001162 FAIL_PROP(c, dti, node, prop,
1163 "Could not get phandle node for (cell %d)",
1164 cell);
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001165 break;
1166 }
1167
1168 cellprop = get_property(provider_node, provider->cell_name);
1169 if (cellprop) {
1170 cellsize = propval_cell(cellprop);
1171 } else if (provider->optional) {
1172 cellsize = 0;
1173 } else {
Rob Herringcd5f70d2018-05-19 14:13:53 +02001174 FAIL(c, dti, node, "Missing property '%s' in node %s or bad phandle (referred from %s[%d])",
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001175 provider->cell_name,
1176 provider_node->fullpath,
Rob Herringcd5f70d2018-05-19 14:13:53 +02001177 prop->name, cell);
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001178 break;
1179 }
1180
1181 if (prop->val.len < ((cell + cellsize + 1) * sizeof(cell_t))) {
Rob Herringcd5f70d2018-05-19 14:13:53 +02001182 FAIL_PROP(c, dti, node, prop,
1183 "property size (%d) too small for cell size %d",
1184 prop->val.len, cellsize);
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001185 }
1186 }
1187}
1188
1189static void check_provider_cells_property(struct check *c,
1190 struct dt_info *dti,
1191 struct node *node)
1192{
1193 struct provider *provider = c->data;
1194 struct property *prop;
1195
1196 prop = get_property(node, provider->prop_name);
1197 if (!prop)
1198 return;
1199
1200 check_property_phandle_args(c, dti, node, prop, provider);
1201}
1202#define WARNING_PROPERTY_PHANDLE_CELLS(nm, propname, cells_name, ...) \
1203 static struct provider nm##_provider = { (propname), (cells_name), __VA_ARGS__ }; \
1204 WARNING(nm##_property, check_provider_cells_property, &nm##_provider, &phandle_references);
1205
1206WARNING_PROPERTY_PHANDLE_CELLS(clocks, "clocks", "#clock-cells");
1207WARNING_PROPERTY_PHANDLE_CELLS(cooling_device, "cooling-device", "#cooling-cells");
1208WARNING_PROPERTY_PHANDLE_CELLS(dmas, "dmas", "#dma-cells");
1209WARNING_PROPERTY_PHANDLE_CELLS(hwlocks, "hwlocks", "#hwlock-cells");
1210WARNING_PROPERTY_PHANDLE_CELLS(interrupts_extended, "interrupts-extended", "#interrupt-cells");
1211WARNING_PROPERTY_PHANDLE_CELLS(io_channels, "io-channels", "#io-channel-cells");
1212WARNING_PROPERTY_PHANDLE_CELLS(iommus, "iommus", "#iommu-cells");
1213WARNING_PROPERTY_PHANDLE_CELLS(mboxes, "mboxes", "#mbox-cells");
1214WARNING_PROPERTY_PHANDLE_CELLS(msi_parent, "msi-parent", "#msi-cells", true);
1215WARNING_PROPERTY_PHANDLE_CELLS(mux_controls, "mux-controls", "#mux-control-cells");
1216WARNING_PROPERTY_PHANDLE_CELLS(phys, "phys", "#phy-cells");
1217WARNING_PROPERTY_PHANDLE_CELLS(power_domains, "power-domains", "#power-domain-cells");
1218WARNING_PROPERTY_PHANDLE_CELLS(pwms, "pwms", "#pwm-cells");
1219WARNING_PROPERTY_PHANDLE_CELLS(resets, "resets", "#reset-cells");
Rob Herringcd5f70d2018-05-19 14:13:53 +02001220WARNING_PROPERTY_PHANDLE_CELLS(sound_dai, "sound-dai", "#sound-dai-cells");
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001221WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors, "thermal-sensors", "#thermal-sensor-cells");
1222
1223static bool prop_is_gpio(struct property *prop)
1224{
1225 char *str;
1226
1227 /*
1228 * *-gpios and *-gpio can appear in property names,
1229 * so skip over any false matches (only one known ATM)
1230 */
1231 if (strstr(prop->name, "nr-gpio"))
1232 return false;
1233
1234 str = strrchr(prop->name, '-');
1235 if (str)
1236 str++;
1237 else
1238 str = prop->name;
1239 if (!(streq(str, "gpios") || streq(str, "gpio")))
1240 return false;
1241
1242 return true;
1243}
1244
1245static void check_gpios_property(struct check *c,
1246 struct dt_info *dti,
1247 struct node *node)
1248{
1249 struct property *prop;
1250
1251 /* Skip GPIO hog nodes which have 'gpios' property */
1252 if (get_property(node, "gpio-hog"))
1253 return;
1254
1255 for_each_property(node, prop) {
1256 struct provider provider;
1257
1258 if (!prop_is_gpio(prop))
1259 continue;
1260
1261 provider.prop_name = prop->name;
1262 provider.cell_name = "#gpio-cells";
1263 provider.optional = false;
1264 check_property_phandle_args(c, dti, node, prop, &provider);
1265 }
1266
1267}
1268WARNING(gpios_property, check_gpios_property, NULL, &phandle_references);
1269
1270static void check_deprecated_gpio_property(struct check *c,
1271 struct dt_info *dti,
1272 struct node *node)
1273{
1274 struct property *prop;
1275
1276 for_each_property(node, prop) {
1277 char *str;
1278
1279 if (!prop_is_gpio(prop))
1280 continue;
1281
1282 str = strstr(prop->name, "gpio");
1283 if (!streq(str, "gpio"))
1284 continue;
1285
Rob Herringcd5f70d2018-05-19 14:13:53 +02001286 FAIL_PROP(c, dti, node, prop,
1287 "'[*-]gpio' is deprecated, use '[*-]gpios' instead");
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001288 }
1289
1290}
1291CHECK(deprecated_gpio_property, check_deprecated_gpio_property, NULL);
1292
1293static bool node_is_interrupt_provider(struct node *node)
1294{
1295 struct property *prop;
1296
1297 prop = get_property(node, "interrupt-controller");
1298 if (prop)
1299 return true;
1300
1301 prop = get_property(node, "interrupt-map");
1302 if (prop)
1303 return true;
1304
1305 return false;
1306}
1307static void check_interrupts_property(struct check *c,
1308 struct dt_info *dti,
1309 struct node *node)
1310{
1311 struct node *root = dti->dt;
1312 struct node *irq_node = NULL, *parent = node;
1313 struct property *irq_prop, *prop = NULL;
1314 int irq_cells, phandle;
1315
1316 irq_prop = get_property(node, "interrupts");
1317 if (!irq_prop)
1318 return;
1319
1320 if (irq_prop->val.len % sizeof(cell_t))
Rob Herringcd5f70d2018-05-19 14:13:53 +02001321 FAIL_PROP(c, dti, node, irq_prop, "size (%d) is invalid, expected multiple of %zu",
1322 irq_prop->val.len, sizeof(cell_t));
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001323
1324 while (parent && !prop) {
1325 if (parent != node && node_is_interrupt_provider(parent)) {
1326 irq_node = parent;
1327 break;
1328 }
1329
1330 prop = get_property(parent, "interrupt-parent");
1331 if (prop) {
1332 phandle = propval_cell(prop);
Rob Herring86abb7f2018-05-19 14:13:52 +02001333 /* Give up if this is an overlay with external references */
1334 if ((phandle == 0 || phandle == -1) &&
1335 (dti->dtsflags & DTSF_PLUGIN))
1336 return;
1337
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001338 irq_node = get_node_by_phandle(root, phandle);
1339 if (!irq_node) {
Rob Herringcd5f70d2018-05-19 14:13:53 +02001340 FAIL_PROP(c, dti, parent, prop, "Bad phandle");
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001341 return;
1342 }
1343 if (!node_is_interrupt_provider(irq_node))
Rob Herringcd5f70d2018-05-19 14:13:53 +02001344 FAIL(c, dti, irq_node,
1345 "Missing interrupt-controller or interrupt-map property");
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001346
1347 break;
1348 }
1349
1350 parent = parent->parent;
1351 }
1352
1353 if (!irq_node) {
Rob Herringcd5f70d2018-05-19 14:13:53 +02001354 FAIL(c, dti, node, "Missing interrupt-parent");
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001355 return;
1356 }
1357
1358 prop = get_property(irq_node, "#interrupt-cells");
1359 if (!prop) {
Rob Herringcd5f70d2018-05-19 14:13:53 +02001360 FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001361 return;
1362 }
1363
1364 irq_cells = propval_cell(prop);
1365 if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
Rob Herringcd5f70d2018-05-19 14:13:53 +02001366 FAIL_PROP(c, dti, node, prop,
1367 "size is (%d), expected multiple of %d",
1368 irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001369 }
1370}
1371WARNING(interrupts_property, check_interrupts_property, &phandle_references);
1372
Tom Rinibd391732017-09-23 12:52:44 -04001373static struct check *check_table[] = {
1374 &duplicate_node_names, &duplicate_property_names,
1375 &node_name_chars, &node_name_format, &property_name_chars,
1376 &name_is_string, &name_properties,
1377
1378 &duplicate_label,
1379
1380 &explicit_phandles,
1381 &phandle_references, &path_references,
Maxime Riparda96fe172020-01-21 10:23:17 +00001382 &omit_unused_nodes,
Tom Rinibd391732017-09-23 12:52:44 -04001383
1384 &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
1385 &device_type_is_string, &model_is_string, &status_is_string,
Rob Herringcd5f70d2018-05-19 14:13:53 +02001386 &label_is_string,
1387
1388 &compatible_is_string_list, &names_is_string_list,
Tom Rinibd391732017-09-23 12:52:44 -04001389
1390 &property_name_chars_strict,
1391 &node_name_chars_strict,
1392
1393 &addr_size_cells, &reg_format, &ranges_format,
1394
1395 &unit_address_vs_reg,
Tom Rini0eb096d2017-09-23 17:31:59 -04001396 &unit_address_format,
1397
1398 &pci_bridge,
1399 &pci_device_reg,
1400 &pci_device_bus_num,
1401
1402 &simple_bus_bridge,
1403 &simple_bus_reg,
Tom Rinibd391732017-09-23 12:52:44 -04001404
1405 &avoid_default_addr_size,
Rob Herringcd5f70d2018-05-19 14:13:53 +02001406 &avoid_unnecessary_addr_size,
Tom Rinibd391732017-09-23 12:52:44 -04001407 &obsolete_chosen_interrupt_controller,
Rob Herringcd5f70d2018-05-19 14:13:53 +02001408 &chosen_node_is_root, &chosen_node_bootargs, &chosen_node_stdout_path,
Tom Rinibd391732017-09-23 12:52:44 -04001409
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001410 &clocks_property,
1411 &cooling_device_property,
1412 &dmas_property,
1413 &hwlocks_property,
1414 &interrupts_extended_property,
1415 &io_channels_property,
1416 &iommus_property,
1417 &mboxes_property,
1418 &msi_parent_property,
1419 &mux_controls_property,
1420 &phys_property,
1421 &power_domains_property,
1422 &pwms_property,
1423 &resets_property,
Rob Herringcd5f70d2018-05-19 14:13:53 +02001424 &sound_dai_property,
Masahiro Yamada09a9dca2017-10-17 13:42:42 +09001425 &thermal_sensors_property,
1426
1427 &deprecated_gpio_property,
1428 &gpios_property,
1429 &interrupts_property,
1430
Rob Herringcd5f70d2018-05-19 14:13:53 +02001431 &alias_paths,
1432
Tom Rinibd391732017-09-23 12:52:44 -04001433 &always_fail,
1434};
1435
1436static void enable_warning_error(struct check *c, bool warn, bool error)
1437{
1438 int i;
1439
1440 /* Raising level, also raise it for prereqs */
1441 if ((warn && !c->warn) || (error && !c->error))
1442 for (i = 0; i < c->num_prereqs; i++)
1443 enable_warning_error(c->prereq[i], warn, error);
1444
1445 c->warn = c->warn || warn;
1446 c->error = c->error || error;
1447}
1448
1449static void disable_warning_error(struct check *c, bool warn, bool error)
1450{
1451 int i;
1452
1453 /* Lowering level, also lower it for things this is the prereq
1454 * for */
1455 if ((warn && c->warn) || (error && c->error)) {
1456 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1457 struct check *cc = check_table[i];
1458 int j;
1459
1460 for (j = 0; j < cc->num_prereqs; j++)
1461 if (cc->prereq[j] == c)
1462 disable_warning_error(cc, warn, error);
1463 }
1464 }
1465
1466 c->warn = c->warn && !warn;
1467 c->error = c->error && !error;
1468}
1469
1470void parse_checks_option(bool warn, bool error, const char *arg)
1471{
1472 int i;
1473 const char *name = arg;
1474 bool enable = true;
1475
1476 if ((strncmp(arg, "no-", 3) == 0)
1477 || (strncmp(arg, "no_", 3) == 0)) {
1478 name = arg + 3;
1479 enable = false;
1480 }
1481
1482 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1483 struct check *c = check_table[i];
1484
1485 if (streq(c->name, name)) {
1486 if (enable)
1487 enable_warning_error(c, warn, error);
1488 else
1489 disable_warning_error(c, warn, error);
1490 return;
1491 }
1492 }
1493
1494 die("Unrecognized check name \"%s\"\n", name);
1495}
1496
1497void process_checks(bool force, struct dt_info *dti)
1498{
1499 int i;
1500 int error = 0;
1501
1502 for (i = 0; i < ARRAY_SIZE(check_table); i++) {
1503 struct check *c = check_table[i];
1504
1505 if (c->warn || c->error)
1506 error = error || run_check(c, dti);
1507 }
1508
1509 if (error) {
1510 if (!force) {
1511 fprintf(stderr, "ERROR: Input tree has errors, aborting "
1512 "(use -f to force output)\n");
1513 exit(2);
1514 } else if (quiet < 3) {
1515 fprintf(stderr, "Warning: Input tree has errors, "
1516 "output forced\n");
1517 }
1518 }
1519}