David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 1 | /* |
| 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 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 34 | enum checkstatus { |
| 35 | UNCHECKED = 0, |
| 36 | PREREQ, |
| 37 | PASSED, |
| 38 | FAILED, |
| 39 | }; |
| 40 | |
| 41 | struct check; |
| 42 | |
| 43 | typedef void (*tree_check_fn)(struct check *c, struct node *dt); |
| 44 | typedef void (*node_check_fn)(struct check *c, struct node *dt, struct node *node); |
| 45 | typedef void (*prop_check_fn)(struct check *c, struct node *dt, |
| 46 | struct node *node, struct property *prop); |
| 47 | |
| 48 | struct check { |
| 49 | const char *name; |
| 50 | tree_check_fn tree_fn; |
| 51 | node_check_fn node_fn; |
| 52 | prop_check_fn prop_fn; |
| 53 | void *data; |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 54 | bool warn, error; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 55 | enum checkstatus status; |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 56 | bool inprogress; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 57 | int num_prereqs; |
| 58 | struct check **prereq; |
| 59 | }; |
| 60 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 61 | #define CHECK_ENTRY(nm, tfn, nfn, pfn, d, w, e, ...) \ |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 62 | static struct check *nm##_prereqs[] = { __VA_ARGS__ }; \ |
| 63 | static struct check nm = { \ |
| 64 | .name = #nm, \ |
| 65 | .tree_fn = (tfn), \ |
| 66 | .node_fn = (nfn), \ |
| 67 | .prop_fn = (pfn), \ |
| 68 | .data = (d), \ |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 69 | .warn = (w), \ |
| 70 | .error = (e), \ |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 71 | .status = UNCHECKED, \ |
| 72 | .num_prereqs = ARRAY_SIZE(nm##_prereqs), \ |
| 73 | .prereq = nm##_prereqs, \ |
| 74 | }; |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 75 | #define WARNING(nm, tfn, nfn, pfn, d, ...) \ |
| 76 | CHECK_ENTRY(nm, tfn, nfn, pfn, d, true, false, __VA_ARGS__) |
| 77 | #define ERROR(nm, tfn, nfn, pfn, d, ...) \ |
| 78 | CHECK_ENTRY(nm, tfn, nfn, pfn, d, false, true, __VA_ARGS__) |
| 79 | #define CHECK(nm, tfn, nfn, pfn, d, ...) \ |
| 80 | CHECK_ENTRY(nm, tfn, nfn, pfn, d, false, false, __VA_ARGS__) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 81 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 82 | #define TREE_WARNING(nm, d, ...) \ |
| 83 | WARNING(nm, check_##nm, NULL, NULL, d, __VA_ARGS__) |
| 84 | #define TREE_ERROR(nm, d, ...) \ |
| 85 | ERROR(nm, check_##nm, NULL, NULL, d, __VA_ARGS__) |
| 86 | #define TREE_CHECK(nm, d, ...) \ |
| 87 | CHECK(nm, check_##nm, NULL, NULL, d, __VA_ARGS__) |
| 88 | #define NODE_WARNING(nm, d, ...) \ |
| 89 | WARNING(nm, NULL, check_##nm, NULL, d, __VA_ARGS__) |
| 90 | #define NODE_ERROR(nm, d, ...) \ |
| 91 | ERROR(nm, NULL, check_##nm, NULL, d, __VA_ARGS__) |
| 92 | #define NODE_CHECK(nm, d, ...) \ |
| 93 | CHECK(nm, NULL, check_##nm, NULL, d, __VA_ARGS__) |
| 94 | #define PROP_WARNING(nm, d, ...) \ |
| 95 | WARNING(nm, NULL, NULL, check_##nm, d, __VA_ARGS__) |
| 96 | #define PROP_ERROR(nm, d, ...) \ |
| 97 | ERROR(nm, NULL, NULL, check_##nm, d, __VA_ARGS__) |
| 98 | #define PROP_CHECK(nm, d, ...) \ |
| 99 | CHECK(nm, NULL, NULL, check_##nm, d, __VA_ARGS__) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 100 | |
| 101 | #ifdef __GNUC__ |
| 102 | static inline void check_msg(struct check *c, const char *fmt, ...) __attribute__((format (printf, 2, 3))); |
| 103 | #endif |
| 104 | static inline void check_msg(struct check *c, const char *fmt, ...) |
| 105 | { |
| 106 | va_list ap; |
| 107 | va_start(ap, fmt); |
| 108 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 109 | if ((c->warn && (quiet < 1)) |
| 110 | || (c->error && (quiet < 2))) { |
| 111 | fprintf(stderr, "%s (%s): ", |
| 112 | (c->error) ? "ERROR" : "Warning", c->name); |
| 113 | vfprintf(stderr, fmt, ap); |
| 114 | fprintf(stderr, "\n"); |
| 115 | } |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 116 | va_end(ap); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | #define FAIL(c, ...) \ |
| 120 | do { \ |
| 121 | TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \ |
| 122 | (c)->status = FAILED; \ |
| 123 | check_msg((c), __VA_ARGS__); \ |
| 124 | } while (0) |
| 125 | |
| 126 | static void check_nodes_props(struct check *c, struct node *dt, struct node *node) |
| 127 | { |
| 128 | struct node *child; |
| 129 | struct property *prop; |
| 130 | |
| 131 | TRACE(c, "%s", node->fullpath); |
| 132 | if (c->node_fn) |
| 133 | c->node_fn(c, dt, node); |
| 134 | |
| 135 | if (c->prop_fn) |
| 136 | for_each_property(node, prop) { |
| 137 | TRACE(c, "%s\t'%s'", node->fullpath, prop->name); |
| 138 | c->prop_fn(c, dt, node, prop); |
| 139 | } |
| 140 | |
| 141 | for_each_child(node, child) |
| 142 | check_nodes_props(c, dt, child); |
| 143 | } |
| 144 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 145 | static bool run_check(struct check *c, struct node *dt) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 146 | { |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 147 | bool error = false; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 148 | int i; |
| 149 | |
| 150 | assert(!c->inprogress); |
| 151 | |
| 152 | if (c->status != UNCHECKED) |
| 153 | goto out; |
| 154 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 155 | c->inprogress = true; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 156 | |
| 157 | for (i = 0; i < c->num_prereqs; i++) { |
| 158 | struct check *prq = c->prereq[i]; |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 159 | error = error || run_check(prq, dt); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 160 | if (prq->status != PASSED) { |
| 161 | c->status = PREREQ; |
| 162 | check_msg(c, "Failed prerequisite '%s'", |
| 163 | c->prereq[i]->name); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (c->status != UNCHECKED) |
| 168 | goto out; |
| 169 | |
| 170 | if (c->node_fn || c->prop_fn) |
| 171 | check_nodes_props(c, dt, dt); |
| 172 | |
| 173 | if (c->tree_fn) |
| 174 | c->tree_fn(c, dt); |
| 175 | if (c->status == UNCHECKED) |
| 176 | c->status = PASSED; |
| 177 | |
| 178 | TRACE(c, "\tCompleted, status %d", c->status); |
| 179 | |
| 180 | out: |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 181 | c->inprogress = false; |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 182 | if ((c->status != PASSED) && (c->error)) |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 183 | error = true; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 184 | return error; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Utility check functions |
| 189 | */ |
| 190 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 191 | /* A check which always fails, for testing purposes only */ |
| 192 | static inline void check_always_fail(struct check *c, struct node *dt) |
| 193 | { |
| 194 | FAIL(c, "always_fail check"); |
| 195 | } |
| 196 | TREE_CHECK(always_fail, NULL); |
| 197 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 198 | static void check_is_string(struct check *c, struct node *root, |
| 199 | struct node *node) |
| 200 | { |
| 201 | struct property *prop; |
| 202 | char *propname = c->data; |
| 203 | |
| 204 | prop = get_property(node, propname); |
| 205 | if (!prop) |
| 206 | return; /* Not present, assumed ok */ |
| 207 | |
| 208 | if (!data_is_one_string(prop->val)) |
| 209 | FAIL(c, "\"%s\" property in %s is not a string", |
| 210 | propname, node->fullpath); |
| 211 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 212 | #define WARNING_IF_NOT_STRING(nm, propname) \ |
| 213 | WARNING(nm, NULL, check_is_string, NULL, (propname)) |
| 214 | #define ERROR_IF_NOT_STRING(nm, propname) \ |
| 215 | ERROR(nm, NULL, check_is_string, NULL, (propname)) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 216 | |
| 217 | static void check_is_cell(struct check *c, struct node *root, |
| 218 | struct node *node) |
| 219 | { |
| 220 | struct property *prop; |
| 221 | char *propname = c->data; |
| 222 | |
| 223 | prop = get_property(node, propname); |
| 224 | if (!prop) |
| 225 | return; /* Not present, assumed ok */ |
| 226 | |
| 227 | if (prop->val.len != sizeof(cell_t)) |
| 228 | FAIL(c, "\"%s\" property in %s is not a single cell", |
| 229 | propname, node->fullpath); |
| 230 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 231 | #define WARNING_IF_NOT_CELL(nm, propname) \ |
| 232 | WARNING(nm, NULL, check_is_cell, NULL, (propname)) |
| 233 | #define ERROR_IF_NOT_CELL(nm, propname) \ |
| 234 | ERROR(nm, NULL, check_is_cell, NULL, (propname)) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * Structural check functions |
| 238 | */ |
| 239 | |
| 240 | static void check_duplicate_node_names(struct check *c, struct node *dt, |
| 241 | struct node *node) |
| 242 | { |
| 243 | struct node *child, *child2; |
| 244 | |
| 245 | for_each_child(node, child) |
| 246 | for (child2 = child->next_sibling; |
| 247 | child2; |
| 248 | child2 = child2->next_sibling) |
| 249 | if (streq(child->name, child2->name)) |
| 250 | FAIL(c, "Duplicate node name %s", |
| 251 | child->fullpath); |
| 252 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 253 | NODE_ERROR(duplicate_node_names, NULL); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 254 | |
| 255 | static void check_duplicate_property_names(struct check *c, struct node *dt, |
| 256 | struct node *node) |
| 257 | { |
| 258 | struct property *prop, *prop2; |
| 259 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 260 | for_each_property(node, prop) { |
| 261 | for (prop2 = prop->next; prop2; prop2 = prop2->next) { |
| 262 | if (prop2->deleted) |
| 263 | continue; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 264 | if (streq(prop->name, prop2->name)) |
| 265 | FAIL(c, "Duplicate property name %s in %s", |
| 266 | prop->name, node->fullpath); |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 269 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 270 | NODE_ERROR(duplicate_property_names, NULL); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 271 | |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 272 | #define LOWERCASE "abcdefghijklmnopqrstuvwxyz" |
| 273 | #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 274 | #define DIGITS "0123456789" |
| 275 | #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-" |
| 276 | |
| 277 | static void check_node_name_chars(struct check *c, struct node *dt, |
| 278 | struct node *node) |
| 279 | { |
| 280 | int n = strspn(node->name, c->data); |
| 281 | |
| 282 | if (n < strlen(node->name)) |
| 283 | FAIL(c, "Bad character '%c' in node %s", |
| 284 | node->name[n], node->fullpath); |
| 285 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 286 | NODE_ERROR(node_name_chars, PROPNODECHARS "@"); |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 287 | |
| 288 | static void check_node_name_format(struct check *c, struct node *dt, |
| 289 | struct node *node) |
| 290 | { |
| 291 | if (strchr(get_unitname(node), '@')) |
| 292 | FAIL(c, "Node %s has multiple '@' characters in name", |
| 293 | node->fullpath); |
| 294 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 295 | NODE_ERROR(node_name_format, NULL, &node_name_chars); |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 296 | |
| 297 | static void check_property_name_chars(struct check *c, struct node *dt, |
| 298 | struct node *node, struct property *prop) |
| 299 | { |
| 300 | int n = strspn(prop->name, c->data); |
| 301 | |
| 302 | if (n < strlen(prop->name)) |
| 303 | FAIL(c, "Bad character '%c' in property name \"%s\", node %s", |
| 304 | prop->name[n], prop->name, node->fullpath); |
| 305 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 306 | PROP_ERROR(property_name_chars, PROPNODECHARS); |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 307 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 308 | #define DESCLABEL_FMT "%s%s%s%s%s" |
| 309 | #define DESCLABEL_ARGS(node,prop,mark) \ |
| 310 | ((mark) ? "value of " : ""), \ |
| 311 | ((prop) ? "'" : ""), \ |
| 312 | ((prop) ? (prop)->name : ""), \ |
| 313 | ((prop) ? "' in " : ""), (node)->fullpath |
| 314 | |
| 315 | static void check_duplicate_label(struct check *c, struct node *dt, |
| 316 | const char *label, struct node *node, |
| 317 | struct property *prop, struct marker *mark) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 318 | { |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 319 | struct node *othernode = NULL; |
| 320 | struct property *otherprop = NULL; |
| 321 | struct marker *othermark = NULL; |
| 322 | |
| 323 | othernode = get_node_by_label(dt, label); |
| 324 | |
| 325 | if (!othernode) |
| 326 | otherprop = get_property_by_label(dt, label, &othernode); |
| 327 | if (!othernode) |
| 328 | othermark = get_marker_label(dt, label, &othernode, |
| 329 | &otherprop); |
| 330 | |
| 331 | if (!othernode) |
| 332 | return; |
| 333 | |
| 334 | if ((othernode != node) || (otherprop != prop) || (othermark != mark)) |
| 335 | FAIL(c, "Duplicate label '%s' on " DESCLABEL_FMT |
| 336 | " and " DESCLABEL_FMT, |
| 337 | label, DESCLABEL_ARGS(node, prop, mark), |
| 338 | DESCLABEL_ARGS(othernode, otherprop, othermark)); |
| 339 | } |
| 340 | |
| 341 | static void check_duplicate_label_node(struct check *c, struct node *dt, |
| 342 | struct node *node) |
| 343 | { |
| 344 | struct label *l; |
| 345 | |
| 346 | for_each_label(node->labels, l) |
| 347 | check_duplicate_label(c, dt, l->label, node, NULL, NULL); |
| 348 | } |
| 349 | static void check_duplicate_label_prop(struct check *c, struct node *dt, |
| 350 | struct node *node, struct property *prop) |
| 351 | { |
| 352 | struct marker *m = prop->val.markers; |
| 353 | struct label *l; |
| 354 | |
| 355 | for_each_label(prop->labels, l) |
| 356 | check_duplicate_label(c, dt, l->label, node, prop, NULL); |
| 357 | |
| 358 | for_each_marker_of_type(m, LABEL) |
| 359 | check_duplicate_label(c, dt, m->ref, node, prop, m); |
| 360 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 361 | ERROR(duplicate_label, NULL, check_duplicate_label_node, |
| 362 | check_duplicate_label_prop, NULL); |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 363 | |
| 364 | static void check_explicit_phandles(struct check *c, struct node *root, |
| 365 | struct node *node, struct property *prop) |
| 366 | { |
| 367 | struct marker *m; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 368 | struct node *other; |
| 369 | cell_t phandle; |
| 370 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 371 | if (!streq(prop->name, "phandle") |
| 372 | && !streq(prop->name, "linux,phandle")) |
| 373 | return; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 374 | |
| 375 | if (prop->val.len != sizeof(cell_t)) { |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 376 | FAIL(c, "%s has bad length (%d) %s property", |
| 377 | node->fullpath, prop->val.len, prop->name); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | m = prop->val.markers; |
| 382 | for_each_marker_of_type(m, REF_PHANDLE) { |
| 383 | assert(m->offset == 0); |
| 384 | if (node != get_node_by_ref(root, m->ref)) |
| 385 | /* "Set this node's phandle equal to some |
| 386 | * other node's phandle". That's nonsensical |
| 387 | * by construction. */ { |
| 388 | FAIL(c, "%s in %s is a reference to another node", |
| 389 | prop->name, node->fullpath); |
| 390 | return; |
| 391 | } |
| 392 | /* But setting this node's phandle equal to its own |
| 393 | * phandle is allowed - that means allocate a unique |
| 394 | * phandle for this node, even if it's not otherwise |
| 395 | * referenced. The value will be filled in later, so |
| 396 | * no further checking for now. */ |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 397 | return; |
| 398 | } |
| 399 | |
| 400 | phandle = propval_cell(prop); |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 401 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 402 | if ((phandle == 0) || (phandle == -1)) { |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 403 | FAIL(c, "%s has bad value (0x%x) in %s property", |
| 404 | node->fullpath, phandle, prop->name); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 405 | return; |
| 406 | } |
| 407 | |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 408 | if (node->phandle && (node->phandle != phandle)) |
| 409 | FAIL(c, "%s has %s property which replaces existing phandle information", |
| 410 | node->fullpath, prop->name); |
| 411 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 412 | other = get_node_by_phandle(root, phandle); |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 413 | if (other && (other != node)) { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 414 | FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)", |
| 415 | node->fullpath, phandle, other->fullpath); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | node->phandle = phandle; |
| 420 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 421 | PROP_ERROR(explicit_phandles, NULL); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 422 | |
| 423 | static void check_name_properties(struct check *c, struct node *root, |
| 424 | struct node *node) |
| 425 | { |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 426 | struct property **pp, *prop = NULL; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 427 | |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 428 | for (pp = &node->proplist; *pp; pp = &((*pp)->next)) |
| 429 | if (streq((*pp)->name, "name")) { |
| 430 | prop = *pp; |
| 431 | break; |
| 432 | } |
| 433 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 434 | if (!prop) |
| 435 | return; /* No name property, that's fine */ |
| 436 | |
| 437 | if ((prop->val.len != node->basenamelen+1) |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 438 | || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) { |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 439 | FAIL(c, "\"name\" property in %s is incorrect (\"%s\" instead" |
| 440 | " of base node name)", node->fullpath, prop->val.val); |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 441 | } else { |
| 442 | /* The name property is correct, and therefore redundant. |
| 443 | * Delete it */ |
| 444 | *pp = prop->next; |
| 445 | free(prop->name); |
| 446 | data_free(prop->val); |
| 447 | free(prop); |
| 448 | } |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 449 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 450 | ERROR_IF_NOT_STRING(name_is_string, "name"); |
| 451 | NODE_ERROR(name_properties, NULL, &name_is_string); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 452 | |
| 453 | /* |
| 454 | * Reference fixup functions |
| 455 | */ |
| 456 | |
| 457 | static void fixup_phandle_references(struct check *c, struct node *dt, |
| 458 | struct node *node, struct property *prop) |
| 459 | { |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 460 | struct marker *m = prop->val.markers; |
| 461 | struct node *refnode; |
| 462 | cell_t phandle; |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 463 | |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 464 | for_each_marker_of_type(m, REF_PHANDLE) { |
| 465 | assert(m->offset + sizeof(cell_t) <= prop->val.len); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 466 | |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 467 | refnode = get_node_by_ref(dt, m->ref); |
| 468 | if (! refnode) { |
| 469 | FAIL(c, "Reference to non-existent node or label \"%s\"\n", |
| 470 | m->ref); |
| 471 | continue; |
| 472 | } |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 473 | |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 474 | phandle = get_node_phandle(dt, refnode); |
| 475 | *((cell_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle); |
| 476 | } |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 477 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 478 | ERROR(phandle_references, NULL, NULL, fixup_phandle_references, NULL, |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 479 | &duplicate_node_names, &explicit_phandles); |
| 480 | |
| 481 | static void fixup_path_references(struct check *c, struct node *dt, |
| 482 | struct node *node, struct property *prop) |
| 483 | { |
| 484 | struct marker *m = prop->val.markers; |
| 485 | struct node *refnode; |
| 486 | char *path; |
| 487 | |
| 488 | for_each_marker_of_type(m, REF_PATH) { |
| 489 | assert(m->offset <= prop->val.len); |
| 490 | |
| 491 | refnode = get_node_by_ref(dt, m->ref); |
| 492 | if (!refnode) { |
| 493 | FAIL(c, "Reference to non-existent node or label \"%s\"\n", |
| 494 | m->ref); |
| 495 | continue; |
| 496 | } |
| 497 | |
| 498 | path = refnode->fullpath; |
| 499 | prop->val = data_insert_at_marker(prop->val, m, path, |
| 500 | strlen(path) + 1); |
| 501 | } |
| 502 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 503 | ERROR(path_references, NULL, NULL, fixup_path_references, NULL, |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 504 | &duplicate_node_names); |
| 505 | |
| 506 | /* |
| 507 | * Semantic checks |
| 508 | */ |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 509 | WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells"); |
| 510 | WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells"); |
| 511 | WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells"); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 512 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 513 | WARNING_IF_NOT_STRING(device_type_is_string, "device_type"); |
| 514 | WARNING_IF_NOT_STRING(model_is_string, "model"); |
| 515 | WARNING_IF_NOT_STRING(status_is_string, "status"); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 516 | |
| 517 | static void fixup_addr_size_cells(struct check *c, struct node *dt, |
| 518 | struct node *node) |
| 519 | { |
| 520 | struct property *prop; |
| 521 | |
| 522 | node->addr_cells = -1; |
| 523 | node->size_cells = -1; |
| 524 | |
| 525 | prop = get_property(node, "#address-cells"); |
| 526 | if (prop) |
| 527 | node->addr_cells = propval_cell(prop); |
| 528 | |
| 529 | prop = get_property(node, "#size-cells"); |
| 530 | if (prop) |
| 531 | node->size_cells = propval_cell(prop); |
| 532 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 533 | WARNING(addr_size_cells, NULL, fixup_addr_size_cells, NULL, NULL, |
| 534 | &address_cells_is_cell, &size_cells_is_cell); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 535 | |
| 536 | #define node_addr_cells(n) \ |
| 537 | (((n)->addr_cells == -1) ? 2 : (n)->addr_cells) |
| 538 | #define node_size_cells(n) \ |
| 539 | (((n)->size_cells == -1) ? 1 : (n)->size_cells) |
| 540 | |
| 541 | static void check_reg_format(struct check *c, struct node *dt, |
| 542 | struct node *node) |
| 543 | { |
| 544 | struct property *prop; |
| 545 | int addr_cells, size_cells, entrylen; |
| 546 | |
| 547 | prop = get_property(node, "reg"); |
| 548 | if (!prop) |
| 549 | return; /* No "reg", that's fine */ |
| 550 | |
| 551 | if (!node->parent) { |
| 552 | FAIL(c, "Root node has a \"reg\" property"); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | if (prop->val.len == 0) |
| 557 | FAIL(c, "\"reg\" property in %s is empty", node->fullpath); |
| 558 | |
| 559 | addr_cells = node_addr_cells(node->parent); |
| 560 | size_cells = node_size_cells(node->parent); |
| 561 | entrylen = (addr_cells + size_cells) * sizeof(cell_t); |
| 562 | |
| 563 | if ((prop->val.len % entrylen) != 0) |
| 564 | FAIL(c, "\"reg\" property in %s has invalid length (%d bytes) " |
| 565 | "(#address-cells == %d, #size-cells == %d)", |
| 566 | node->fullpath, prop->val.len, addr_cells, size_cells); |
| 567 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 568 | NODE_WARNING(reg_format, NULL, &addr_size_cells); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 569 | |
| 570 | static void check_ranges_format(struct check *c, struct node *dt, |
| 571 | struct node *node) |
| 572 | { |
| 573 | struct property *prop; |
| 574 | int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen; |
| 575 | |
| 576 | prop = get_property(node, "ranges"); |
| 577 | if (!prop) |
| 578 | return; |
| 579 | |
| 580 | if (!node->parent) { |
| 581 | FAIL(c, "Root node has a \"ranges\" property"); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | p_addr_cells = node_addr_cells(node->parent); |
| 586 | p_size_cells = node_size_cells(node->parent); |
| 587 | c_addr_cells = node_addr_cells(node); |
| 588 | c_size_cells = node_size_cells(node); |
| 589 | entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t); |
| 590 | |
| 591 | if (prop->val.len == 0) { |
| 592 | if (p_addr_cells != c_addr_cells) |
| 593 | FAIL(c, "%s has empty \"ranges\" property but its " |
| 594 | "#address-cells (%d) differs from %s (%d)", |
| 595 | node->fullpath, c_addr_cells, node->parent->fullpath, |
| 596 | p_addr_cells); |
| 597 | if (p_size_cells != c_size_cells) |
| 598 | FAIL(c, "%s has empty \"ranges\" property but its " |
| 599 | "#size-cells (%d) differs from %s (%d)", |
| 600 | node->fullpath, c_size_cells, node->parent->fullpath, |
| 601 | p_size_cells); |
| 602 | } else if ((prop->val.len % entrylen) != 0) { |
| 603 | FAIL(c, "\"ranges\" property in %s has invalid length (%d bytes) " |
| 604 | "(parent #address-cells == %d, child #address-cells == %d, " |
| 605 | "#size-cells == %d)", node->fullpath, prop->val.len, |
| 606 | p_addr_cells, c_addr_cells, c_size_cells); |
| 607 | } |
| 608 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 609 | NODE_WARNING(ranges_format, NULL, &addr_size_cells); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 610 | |
| 611 | /* |
| 612 | * Style checks |
| 613 | */ |
| 614 | static void check_avoid_default_addr_size(struct check *c, struct node *dt, |
| 615 | struct node *node) |
| 616 | { |
| 617 | struct property *reg, *ranges; |
| 618 | |
| 619 | if (!node->parent) |
| 620 | return; /* Ignore root node */ |
| 621 | |
| 622 | reg = get_property(node, "reg"); |
| 623 | ranges = get_property(node, "ranges"); |
| 624 | |
| 625 | if (!reg && !ranges) |
| 626 | return; |
| 627 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 628 | if (node->parent->addr_cells == -1) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 629 | FAIL(c, "Relying on default #address-cells value for %s", |
| 630 | node->fullpath); |
| 631 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 632 | if (node->parent->size_cells == -1) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 633 | FAIL(c, "Relying on default #size-cells value for %s", |
| 634 | node->fullpath); |
| 635 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 636 | NODE_WARNING(avoid_default_addr_size, NULL, &addr_size_cells); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 637 | |
| 638 | static void check_obsolete_chosen_interrupt_controller(struct check *c, |
| 639 | struct node *dt) |
| 640 | { |
| 641 | struct node *chosen; |
| 642 | struct property *prop; |
| 643 | |
| 644 | chosen = get_node_by_path(dt, "/chosen"); |
| 645 | if (!chosen) |
| 646 | return; |
| 647 | |
| 648 | prop = get_property(chosen, "interrupt-controller"); |
| 649 | if (prop) |
| 650 | FAIL(c, "/chosen has obsolete \"interrupt-controller\" " |
| 651 | "property"); |
| 652 | } |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 653 | TREE_WARNING(obsolete_chosen_interrupt_controller, NULL); |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 654 | |
| 655 | static struct check *check_table[] = { |
| 656 | &duplicate_node_names, &duplicate_property_names, |
David Gibson | ed95d74 | 2008-08-07 12:24:17 +1000 | [diff] [blame] | 657 | &node_name_chars, &node_name_format, &property_name_chars, |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 658 | &name_is_string, &name_properties, |
John Bonesio | 658f29a | 2010-11-17 15:28:20 -0800 | [diff] [blame] | 659 | |
| 660 | &duplicate_label, |
| 661 | |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 662 | &explicit_phandles, |
| 663 | &phandle_references, &path_references, |
| 664 | |
| 665 | &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell, |
| 666 | &device_type_is_string, &model_is_string, &status_is_string, |
| 667 | |
| 668 | &addr_size_cells, ®_format, &ranges_format, |
| 669 | |
| 670 | &avoid_default_addr_size, |
| 671 | &obsolete_chosen_interrupt_controller, |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 672 | |
| 673 | &always_fail, |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 674 | }; |
| 675 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 676 | static void enable_warning_error(struct check *c, bool warn, bool error) |
| 677 | { |
| 678 | int i; |
| 679 | |
| 680 | /* Raising level, also raise it for prereqs */ |
| 681 | if ((warn && !c->warn) || (error && !c->error)) |
| 682 | for (i = 0; i < c->num_prereqs; i++) |
| 683 | enable_warning_error(c->prereq[i], warn, error); |
| 684 | |
| 685 | c->warn = c->warn || warn; |
| 686 | c->error = c->error || error; |
| 687 | } |
| 688 | |
| 689 | static void disable_warning_error(struct check *c, bool warn, bool error) |
| 690 | { |
| 691 | int i; |
| 692 | |
| 693 | /* Lowering level, also lower it for things this is the prereq |
| 694 | * for */ |
| 695 | if ((warn && c->warn) || (error && c->error)) { |
| 696 | for (i = 0; i < ARRAY_SIZE(check_table); i++) { |
| 697 | struct check *cc = check_table[i]; |
| 698 | int j; |
| 699 | |
| 700 | for (j = 0; j < cc->num_prereqs; j++) |
| 701 | if (cc->prereq[j] == c) |
| 702 | disable_warning_error(cc, warn, error); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | c->warn = c->warn && !warn; |
| 707 | c->error = c->error && !error; |
| 708 | } |
| 709 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 710 | void parse_checks_option(bool warn, bool error, const char *arg) |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 711 | { |
| 712 | int i; |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 713 | const char *name = arg; |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 714 | bool enable = true; |
| 715 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 716 | if ((strncmp(arg, "no-", 3) == 0) |
| 717 | || (strncmp(arg, "no_", 3) == 0)) { |
| 718 | name = arg + 3; |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 719 | enable = false; |
| 720 | } |
| 721 | |
| 722 | for (i = 0; i < ARRAY_SIZE(check_table); i++) { |
| 723 | struct check *c = check_table[i]; |
| 724 | |
| 725 | if (streq(c->name, name)) { |
| 726 | if (enable) |
| 727 | enable_warning_error(c, warn, error); |
| 728 | else |
| 729 | disable_warning_error(c, warn, error); |
| 730 | return; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | die("Unrecognized check name \"%s\"\n", name); |
| 735 | } |
| 736 | |
Rob Herring | 4760597 | 2015-04-29 16:00:05 -0500 | [diff] [blame] | 737 | void process_checks(bool force, struct boot_info *bi) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 738 | { |
| 739 | struct node *dt = bi->dt; |
| 740 | int i; |
| 741 | int error = 0; |
| 742 | |
| 743 | for (i = 0; i < ARRAY_SIZE(check_table); i++) { |
| 744 | struct check *c = check_table[i]; |
| 745 | |
Stephen Warren | cd29672 | 2012-09-28 21:25:59 +0000 | [diff] [blame] | 746 | if (c->warn || c->error) |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 747 | error = error || run_check(c, dt); |
| 748 | } |
| 749 | |
| 750 | if (error) { |
| 751 | if (!force) { |
| 752 | fprintf(stderr, "ERROR: Input tree has errors, aborting " |
| 753 | "(use -f to force output)\n"); |
| 754 | exit(2); |
| 755 | } else if (quiet < 3) { |
| 756 | fprintf(stderr, "Warning: Input tree has errors, " |
| 757 | "output forced\n"); |
| 758 | } |
| 759 | } |
David Gibson | a4da2e3 | 2007-12-18 15:06:42 +1100 | [diff] [blame] | 760 | } |