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 | |
| 34 | enum checklevel { |
| 35 | IGNORE = 0, |
| 36 | WARN = 1, |
| 37 | ERROR = 2, |
| 38 | }; |
| 39 | |
| 40 | enum checkstatus { |
| 41 | UNCHECKED = 0, |
| 42 | PREREQ, |
| 43 | PASSED, |
| 44 | FAILED, |
| 45 | }; |
| 46 | |
| 47 | struct check; |
| 48 | |
| 49 | typedef void (*tree_check_fn)(struct check *c, struct node *dt); |
| 50 | typedef void (*node_check_fn)(struct check *c, struct node *dt, struct node *node); |
| 51 | typedef void (*prop_check_fn)(struct check *c, struct node *dt, |
| 52 | struct node *node, struct property *prop); |
| 53 | |
| 54 | struct check { |
| 55 | const char *name; |
| 56 | tree_check_fn tree_fn; |
| 57 | node_check_fn node_fn; |
| 58 | prop_check_fn prop_fn; |
| 59 | void *data; |
| 60 | enum checklevel level; |
| 61 | enum checkstatus status; |
| 62 | int inprogress; |
| 63 | int num_prereqs; |
| 64 | struct check **prereq; |
| 65 | }; |
| 66 | |
| 67 | #define CHECK(nm, tfn, nfn, pfn, d, lvl, ...) \ |
| 68 | static struct check *nm##_prereqs[] = { __VA_ARGS__ }; \ |
| 69 | static struct check nm = { \ |
| 70 | .name = #nm, \ |
| 71 | .tree_fn = (tfn), \ |
| 72 | .node_fn = (nfn), \ |
| 73 | .prop_fn = (pfn), \ |
| 74 | .data = (d), \ |
| 75 | .level = (lvl), \ |
| 76 | .status = UNCHECKED, \ |
| 77 | .num_prereqs = ARRAY_SIZE(nm##_prereqs), \ |
| 78 | .prereq = nm##_prereqs, \ |
| 79 | }; |
| 80 | |
| 81 | #define TREE_CHECK(nm, d, lvl, ...) \ |
| 82 | CHECK(nm, check_##nm, NULL, NULL, d, lvl, __VA_ARGS__) |
| 83 | #define NODE_CHECK(nm, d, lvl, ...) \ |
| 84 | CHECK(nm, NULL, check_##nm, NULL, d, lvl, __VA_ARGS__) |
| 85 | #define PROP_CHECK(nm, d, lvl, ...) \ |
| 86 | CHECK(nm, NULL, NULL, check_##nm, d, lvl, __VA_ARGS__) |
| 87 | #define BATCH_CHECK(nm, lvl, ...) \ |
| 88 | CHECK(nm, NULL, NULL, NULL, NULL, lvl, __VA_ARGS__) |
| 89 | |
| 90 | #ifdef __GNUC__ |
| 91 | static inline void check_msg(struct check *c, const char *fmt, ...) __attribute__((format (printf, 2, 3))); |
| 92 | #endif |
| 93 | static inline void check_msg(struct check *c, const char *fmt, ...) |
| 94 | { |
| 95 | va_list ap; |
| 96 | va_start(ap, fmt); |
| 97 | |
| 98 | if ((c->level < WARN) || (c->level <= quiet)) |
| 99 | return; /* Suppress message */ |
| 100 | |
| 101 | fprintf(stderr, "%s (%s): ", |
| 102 | (c->level == ERROR) ? "ERROR" : "Warning", c->name); |
| 103 | vfprintf(stderr, fmt, ap); |
| 104 | fprintf(stderr, "\n"); |
| 105 | } |
| 106 | |
| 107 | #define FAIL(c, ...) \ |
| 108 | do { \ |
| 109 | TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \ |
| 110 | (c)->status = FAILED; \ |
| 111 | check_msg((c), __VA_ARGS__); \ |
| 112 | } while (0) |
| 113 | |
| 114 | static void check_nodes_props(struct check *c, struct node *dt, struct node *node) |
| 115 | { |
| 116 | struct node *child; |
| 117 | struct property *prop; |
| 118 | |
| 119 | TRACE(c, "%s", node->fullpath); |
| 120 | if (c->node_fn) |
| 121 | c->node_fn(c, dt, node); |
| 122 | |
| 123 | if (c->prop_fn) |
| 124 | for_each_property(node, prop) { |
| 125 | TRACE(c, "%s\t'%s'", node->fullpath, prop->name); |
| 126 | c->prop_fn(c, dt, node, prop); |
| 127 | } |
| 128 | |
| 129 | for_each_child(node, child) |
| 130 | check_nodes_props(c, dt, child); |
| 131 | } |
| 132 | |
| 133 | static int run_check(struct check *c, struct node *dt) |
| 134 | { |
| 135 | int error = 0; |
| 136 | int i; |
| 137 | |
| 138 | assert(!c->inprogress); |
| 139 | |
| 140 | if (c->status != UNCHECKED) |
| 141 | goto out; |
| 142 | |
| 143 | c->inprogress = 1; |
| 144 | |
| 145 | for (i = 0; i < c->num_prereqs; i++) { |
| 146 | struct check *prq = c->prereq[i]; |
| 147 | error |= run_check(prq, dt); |
| 148 | if (prq->status != PASSED) { |
| 149 | c->status = PREREQ; |
| 150 | check_msg(c, "Failed prerequisite '%s'", |
| 151 | c->prereq[i]->name); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (c->status != UNCHECKED) |
| 156 | goto out; |
| 157 | |
| 158 | if (c->node_fn || c->prop_fn) |
| 159 | check_nodes_props(c, dt, dt); |
| 160 | |
| 161 | if (c->tree_fn) |
| 162 | c->tree_fn(c, dt); |
| 163 | if (c->status == UNCHECKED) |
| 164 | c->status = PASSED; |
| 165 | |
| 166 | TRACE(c, "\tCompleted, status %d", c->status); |
| 167 | |
| 168 | out: |
| 169 | c->inprogress = 0; |
| 170 | if ((c->status != PASSED) && (c->level == ERROR)) |
| 171 | error = 1; |
| 172 | return error; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Utility check functions |
| 177 | */ |
| 178 | |
| 179 | static void check_is_string(struct check *c, struct node *root, |
| 180 | struct node *node) |
| 181 | { |
| 182 | struct property *prop; |
| 183 | char *propname = c->data; |
| 184 | |
| 185 | prop = get_property(node, propname); |
| 186 | if (!prop) |
| 187 | return; /* Not present, assumed ok */ |
| 188 | |
| 189 | if (!data_is_one_string(prop->val)) |
| 190 | FAIL(c, "\"%s\" property in %s is not a string", |
| 191 | propname, node->fullpath); |
| 192 | } |
| 193 | #define CHECK_IS_STRING(nm, propname, lvl) \ |
| 194 | CHECK(nm, NULL, check_is_string, NULL, (propname), (lvl)) |
| 195 | |
| 196 | static void check_is_cell(struct check *c, struct node *root, |
| 197 | struct node *node) |
| 198 | { |
| 199 | struct property *prop; |
| 200 | char *propname = c->data; |
| 201 | |
| 202 | prop = get_property(node, propname); |
| 203 | if (!prop) |
| 204 | return; /* Not present, assumed ok */ |
| 205 | |
| 206 | if (prop->val.len != sizeof(cell_t)) |
| 207 | FAIL(c, "\"%s\" property in %s is not a single cell", |
| 208 | propname, node->fullpath); |
| 209 | } |
| 210 | #define CHECK_IS_CELL(nm, propname, lvl) \ |
| 211 | CHECK(nm, NULL, check_is_cell, NULL, (propname), (lvl)) |
| 212 | |
| 213 | /* |
| 214 | * Structural check functions |
| 215 | */ |
| 216 | |
| 217 | static void check_duplicate_node_names(struct check *c, struct node *dt, |
| 218 | struct node *node) |
| 219 | { |
| 220 | struct node *child, *child2; |
| 221 | |
| 222 | for_each_child(node, child) |
| 223 | for (child2 = child->next_sibling; |
| 224 | child2; |
| 225 | child2 = child2->next_sibling) |
| 226 | if (streq(child->name, child2->name)) |
| 227 | FAIL(c, "Duplicate node name %s", |
| 228 | child->fullpath); |
| 229 | } |
| 230 | NODE_CHECK(duplicate_node_names, NULL, ERROR); |
| 231 | |
| 232 | static void check_duplicate_property_names(struct check *c, struct node *dt, |
| 233 | struct node *node) |
| 234 | { |
| 235 | struct property *prop, *prop2; |
| 236 | |
| 237 | for_each_property(node, prop) |
| 238 | for (prop2 = prop->next; prop2; prop2 = prop2->next) |
| 239 | if (streq(prop->name, prop2->name)) |
| 240 | FAIL(c, "Duplicate property name %s in %s", |
| 241 | prop->name, node->fullpath); |
| 242 | } |
| 243 | NODE_CHECK(duplicate_property_names, NULL, ERROR); |
| 244 | |
| 245 | static void check_explicit_phandles(struct check *c, struct node *root, |
| 246 | struct node *node) |
| 247 | { |
| 248 | struct property *prop; |
| 249 | struct node *other; |
| 250 | cell_t phandle; |
| 251 | |
| 252 | prop = get_property(node, "linux,phandle"); |
| 253 | if (! prop) |
| 254 | return; /* No phandle, that's fine */ |
| 255 | |
| 256 | if (prop->val.len != sizeof(cell_t)) { |
| 257 | FAIL(c, "%s has bad length (%d) linux,phandle property", |
| 258 | node->fullpath, prop->val.len); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | phandle = propval_cell(prop); |
| 263 | if ((phandle == 0) || (phandle == -1)) { |
| 264 | FAIL(c, "%s has invalid linux,phandle value 0x%x", |
| 265 | node->fullpath, phandle); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | other = get_node_by_phandle(root, phandle); |
| 270 | if (other) { |
| 271 | FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)", |
| 272 | node->fullpath, phandle, other->fullpath); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | node->phandle = phandle; |
| 277 | } |
| 278 | NODE_CHECK(explicit_phandles, NULL, ERROR); |
| 279 | |
| 280 | static void check_name_properties(struct check *c, struct node *root, |
| 281 | struct node *node) |
| 282 | { |
| 283 | struct property *prop; |
| 284 | |
| 285 | prop = get_property(node, "name"); |
| 286 | if (!prop) |
| 287 | return; /* No name property, that's fine */ |
| 288 | |
| 289 | if ((prop->val.len != node->basenamelen+1) |
| 290 | || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) |
| 291 | FAIL(c, "\"name\" property in %s is incorrect (\"%s\" instead" |
| 292 | " of base node name)", node->fullpath, prop->val.val); |
| 293 | } |
| 294 | CHECK_IS_STRING(name_is_string, "name", ERROR); |
| 295 | NODE_CHECK(name_properties, NULL, ERROR, &name_is_string); |
| 296 | |
| 297 | /* |
| 298 | * Reference fixup functions |
| 299 | */ |
| 300 | |
| 301 | static void fixup_phandle_references(struct check *c, struct node *dt, |
| 302 | struct node *node, struct property *prop) |
| 303 | { |
| 304 | struct marker *m = prop->val.markers; |
| 305 | struct node *refnode; |
| 306 | cell_t phandle; |
| 307 | |
| 308 | for_each_marker_of_type(m, REF_PHANDLE) { |
| 309 | assert(m->offset + sizeof(cell_t) <= prop->val.len); |
| 310 | |
| 311 | refnode = get_node_by_ref(dt, m->ref); |
| 312 | if (! refnode) { |
| 313 | FAIL(c, "Reference to non-existent node or label \"%s\"\n", |
| 314 | m->ref); |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | phandle = get_node_phandle(dt, refnode); |
| 319 | *((cell_t *)(prop->val.val + m->offset)) = cpu_to_be32(phandle); |
| 320 | } |
| 321 | } |
| 322 | CHECK(phandle_references, NULL, NULL, fixup_phandle_references, NULL, ERROR, |
| 323 | &duplicate_node_names, &explicit_phandles); |
| 324 | |
| 325 | static void fixup_path_references(struct check *c, struct node *dt, |
| 326 | struct node *node, struct property *prop) |
| 327 | { |
| 328 | struct marker *m = prop->val.markers; |
| 329 | struct node *refnode; |
| 330 | char *path; |
| 331 | |
| 332 | for_each_marker_of_type(m, REF_PATH) { |
| 333 | assert(m->offset <= prop->val.len); |
| 334 | |
| 335 | refnode = get_node_by_ref(dt, m->ref); |
| 336 | if (!refnode) { |
| 337 | FAIL(c, "Reference to non-existent node or label \"%s\"\n", |
| 338 | m->ref); |
| 339 | continue; |
| 340 | } |
| 341 | |
| 342 | path = refnode->fullpath; |
| 343 | prop->val = data_insert_at_marker(prop->val, m, path, |
| 344 | strlen(path) + 1); |
| 345 | } |
| 346 | } |
| 347 | CHECK(path_references, NULL, NULL, fixup_path_references, NULL, ERROR, |
| 348 | &duplicate_node_names); |
| 349 | |
| 350 | /* |
| 351 | * Semantic checks |
| 352 | */ |
| 353 | CHECK_IS_CELL(address_cells_is_cell, "#address-cells", WARN); |
| 354 | CHECK_IS_CELL(size_cells_is_cell, "#size-cells", WARN); |
| 355 | CHECK_IS_CELL(interrupt_cells_is_cell, "#interrupt-cells", WARN); |
| 356 | |
| 357 | CHECK_IS_STRING(device_type_is_string, "device_type", WARN); |
| 358 | CHECK_IS_STRING(model_is_string, "model", WARN); |
| 359 | CHECK_IS_STRING(status_is_string, "status", WARN); |
| 360 | |
| 361 | static void fixup_addr_size_cells(struct check *c, struct node *dt, |
| 362 | struct node *node) |
| 363 | { |
| 364 | struct property *prop; |
| 365 | |
| 366 | node->addr_cells = -1; |
| 367 | node->size_cells = -1; |
| 368 | |
| 369 | prop = get_property(node, "#address-cells"); |
| 370 | if (prop) |
| 371 | node->addr_cells = propval_cell(prop); |
| 372 | |
| 373 | prop = get_property(node, "#size-cells"); |
| 374 | if (prop) |
| 375 | node->size_cells = propval_cell(prop); |
| 376 | } |
| 377 | CHECK(addr_size_cells, NULL, fixup_addr_size_cells, NULL, NULL, WARN, |
| 378 | &address_cells_is_cell, &size_cells_is_cell); |
| 379 | |
| 380 | #define node_addr_cells(n) \ |
| 381 | (((n)->addr_cells == -1) ? 2 : (n)->addr_cells) |
| 382 | #define node_size_cells(n) \ |
| 383 | (((n)->size_cells == -1) ? 1 : (n)->size_cells) |
| 384 | |
| 385 | static void check_reg_format(struct check *c, struct node *dt, |
| 386 | struct node *node) |
| 387 | { |
| 388 | struct property *prop; |
| 389 | int addr_cells, size_cells, entrylen; |
| 390 | |
| 391 | prop = get_property(node, "reg"); |
| 392 | if (!prop) |
| 393 | return; /* No "reg", that's fine */ |
| 394 | |
| 395 | if (!node->parent) { |
| 396 | FAIL(c, "Root node has a \"reg\" property"); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | if (prop->val.len == 0) |
| 401 | FAIL(c, "\"reg\" property in %s is empty", node->fullpath); |
| 402 | |
| 403 | addr_cells = node_addr_cells(node->parent); |
| 404 | size_cells = node_size_cells(node->parent); |
| 405 | entrylen = (addr_cells + size_cells) * sizeof(cell_t); |
| 406 | |
| 407 | if ((prop->val.len % entrylen) != 0) |
| 408 | FAIL(c, "\"reg\" property in %s has invalid length (%d bytes) " |
| 409 | "(#address-cells == %d, #size-cells == %d)", |
| 410 | node->fullpath, prop->val.len, addr_cells, size_cells); |
| 411 | } |
| 412 | NODE_CHECK(reg_format, NULL, WARN, &addr_size_cells); |
| 413 | |
| 414 | static void check_ranges_format(struct check *c, struct node *dt, |
| 415 | struct node *node) |
| 416 | { |
| 417 | struct property *prop; |
| 418 | int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen; |
| 419 | |
| 420 | prop = get_property(node, "ranges"); |
| 421 | if (!prop) |
| 422 | return; |
| 423 | |
| 424 | if (!node->parent) { |
| 425 | FAIL(c, "Root node has a \"ranges\" property"); |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | p_addr_cells = node_addr_cells(node->parent); |
| 430 | p_size_cells = node_size_cells(node->parent); |
| 431 | c_addr_cells = node_addr_cells(node); |
| 432 | c_size_cells = node_size_cells(node); |
| 433 | entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t); |
| 434 | |
| 435 | if (prop->val.len == 0) { |
| 436 | if (p_addr_cells != c_addr_cells) |
| 437 | FAIL(c, "%s has empty \"ranges\" property but its " |
| 438 | "#address-cells (%d) differs from %s (%d)", |
| 439 | node->fullpath, c_addr_cells, node->parent->fullpath, |
| 440 | p_addr_cells); |
| 441 | if (p_size_cells != c_size_cells) |
| 442 | FAIL(c, "%s has empty \"ranges\" property but its " |
| 443 | "#size-cells (%d) differs from %s (%d)", |
| 444 | node->fullpath, c_size_cells, node->parent->fullpath, |
| 445 | p_size_cells); |
| 446 | } else if ((prop->val.len % entrylen) != 0) { |
| 447 | FAIL(c, "\"ranges\" property in %s has invalid length (%d bytes) " |
| 448 | "(parent #address-cells == %d, child #address-cells == %d, " |
| 449 | "#size-cells == %d)", node->fullpath, prop->val.len, |
| 450 | p_addr_cells, c_addr_cells, c_size_cells); |
| 451 | } |
| 452 | } |
| 453 | NODE_CHECK(ranges_format, NULL, WARN, &addr_size_cells); |
| 454 | |
| 455 | /* |
| 456 | * Style checks |
| 457 | */ |
| 458 | static void check_avoid_default_addr_size(struct check *c, struct node *dt, |
| 459 | struct node *node) |
| 460 | { |
| 461 | struct property *reg, *ranges; |
| 462 | |
| 463 | if (!node->parent) |
| 464 | return; /* Ignore root node */ |
| 465 | |
| 466 | reg = get_property(node, "reg"); |
| 467 | ranges = get_property(node, "ranges"); |
| 468 | |
| 469 | if (!reg && !ranges) |
| 470 | return; |
| 471 | |
| 472 | if ((node->parent->addr_cells == -1)) |
| 473 | FAIL(c, "Relying on default #address-cells value for %s", |
| 474 | node->fullpath); |
| 475 | |
| 476 | if ((node->parent->size_cells == -1)) |
| 477 | FAIL(c, "Relying on default #size-cells value for %s", |
| 478 | node->fullpath); |
| 479 | } |
| 480 | NODE_CHECK(avoid_default_addr_size, NULL, WARN, &addr_size_cells); |
| 481 | |
| 482 | static void check_obsolete_chosen_interrupt_controller(struct check *c, |
| 483 | struct node *dt) |
| 484 | { |
| 485 | struct node *chosen; |
| 486 | struct property *prop; |
| 487 | |
| 488 | chosen = get_node_by_path(dt, "/chosen"); |
| 489 | if (!chosen) |
| 490 | return; |
| 491 | |
| 492 | prop = get_property(chosen, "interrupt-controller"); |
| 493 | if (prop) |
| 494 | FAIL(c, "/chosen has obsolete \"interrupt-controller\" " |
| 495 | "property"); |
| 496 | } |
| 497 | TREE_CHECK(obsolete_chosen_interrupt_controller, NULL, WARN); |
| 498 | |
| 499 | static struct check *check_table[] = { |
| 500 | &duplicate_node_names, &duplicate_property_names, |
| 501 | &name_is_string, &name_properties, |
| 502 | &explicit_phandles, |
| 503 | &phandle_references, &path_references, |
| 504 | |
| 505 | &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell, |
| 506 | &device_type_is_string, &model_is_string, &status_is_string, |
| 507 | |
| 508 | &addr_size_cells, ®_format, &ranges_format, |
| 509 | |
| 510 | &avoid_default_addr_size, |
| 511 | &obsolete_chosen_interrupt_controller, |
| 512 | }; |
| 513 | |
| 514 | int check_semantics(struct node *dt, int outversion, int boot_cpuid_phys); |
| 515 | |
| 516 | void process_checks(int force, struct boot_info *bi, |
| 517 | int checkflag, int outversion, int boot_cpuid_phys) |
| 518 | { |
| 519 | struct node *dt = bi->dt; |
| 520 | int i; |
| 521 | int error = 0; |
| 522 | |
| 523 | for (i = 0; i < ARRAY_SIZE(check_table); i++) { |
| 524 | struct check *c = check_table[i]; |
| 525 | |
| 526 | if (c->level != IGNORE) |
| 527 | error = error || run_check(c, dt); |
| 528 | } |
| 529 | |
| 530 | if (error) { |
| 531 | if (!force) { |
| 532 | fprintf(stderr, "ERROR: Input tree has errors, aborting " |
| 533 | "(use -f to force output)\n"); |
| 534 | exit(2); |
| 535 | } else if (quiet < 3) { |
| 536 | fprintf(stderr, "Warning: Input tree has errors, " |
| 537 | "output forced\n"); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (checkflag) { |
| 542 | if (error) { |
| 543 | fprintf(stderr, "Warning: Skipping semantic checks due to structural errors\n"); |
| 544 | } else { |
| 545 | if (!check_semantics(bi->dt, outversion, |
| 546 | boot_cpuid_phys)) |
| 547 | fprintf(stderr, "Warning: Input tree has semantic errors\n"); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Semantic check functions |
| 554 | */ |
| 555 | |
| 556 | #define ERRMSG(...) if (quiet < 2) fprintf(stderr, "ERROR: " __VA_ARGS__) |
| 557 | #define WARNMSG(...) if (quiet < 1) fprintf(stderr, "Warning: " __VA_ARGS__) |
| 558 | |
| 559 | #define DO_ERR(...) do {ERRMSG(__VA_ARGS__); ok = 0; } while (0) |
| 560 | |
| 561 | #define CHECK_HAVE(node, propname) \ |
| 562 | do { \ |
| 563 | if (! (prop = get_property((node), (propname)))) \ |
| 564 | DO_ERR("Missing \"%s\" property in %s\n", (propname), \ |
| 565 | (node)->fullpath); \ |
| 566 | } while (0); |
| 567 | |
| 568 | #define CHECK_HAVE_WARN(node, propname) \ |
| 569 | do { \ |
| 570 | if (! (prop = get_property((node), (propname)))) \ |
| 571 | WARNMSG("%s has no \"%s\" property\n", \ |
| 572 | (node)->fullpath, (propname)); \ |
| 573 | } while (0) |
| 574 | |
| 575 | #define CHECK_HAVE_STRING(node, propname) \ |
| 576 | do { \ |
| 577 | CHECK_HAVE((node), (propname)); \ |
| 578 | if (prop && !data_is_one_string(prop->val)) \ |
| 579 | DO_ERR("\"%s\" property in %s is not a string\n", \ |
| 580 | (propname), (node)->fullpath); \ |
| 581 | } while (0) |
| 582 | |
| 583 | #define CHECK_HAVE_STREQ(node, propname, value) \ |
| 584 | do { \ |
| 585 | CHECK_HAVE_STRING((node), (propname)); \ |
| 586 | if (prop && !streq(prop->val.val, (value))) \ |
| 587 | DO_ERR("%s has wrong %s, %s (should be %s\n", \ |
| 588 | (node)->fullpath, (propname), \ |
| 589 | prop->val.val, (value)); \ |
| 590 | } while (0) |
| 591 | |
| 592 | #define CHECK_HAVE_ONECELL(node, propname) \ |
| 593 | do { \ |
| 594 | CHECK_HAVE((node), (propname)); \ |
| 595 | if (prop && (prop->val.len != sizeof(cell_t))) \ |
| 596 | DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \ |
| 597 | } while (0) |
| 598 | |
| 599 | #define CHECK_HAVE_WARN_ONECELL(node, propname) \ |
| 600 | do { \ |
| 601 | CHECK_HAVE_WARN((node), (propname)); \ |
| 602 | if (prop && (prop->val.len != sizeof(cell_t))) \ |
| 603 | DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \ |
| 604 | } while (0) |
| 605 | |
| 606 | #define CHECK_HAVE_WARN_PHANDLE(xnode, propname, root) \ |
| 607 | do { \ |
| 608 | struct node *ref; \ |
| 609 | CHECK_HAVE_WARN_ONECELL((xnode), (propname)); \ |
| 610 | if (prop) {\ |
| 611 | cell_t phandle = propval_cell(prop); \ |
| 612 | if ((phandle == 0) || (phandle == -1)) { \ |
| 613 | DO_ERR("\"%s\" property in %s contains an invalid phandle %x\n", (propname), (xnode)->fullpath, phandle); \ |
| 614 | } else { \ |
| 615 | ref = get_node_by_phandle((root), propval_cell(prop)); \ |
| 616 | if (! ref) \ |
| 617 | DO_ERR("\"%s\" property in %s refers to non-existant phandle %x\n", (propname), (xnode)->fullpath, propval_cell(prop)); \ |
| 618 | } \ |
| 619 | } \ |
| 620 | } while (0) |
| 621 | |
| 622 | #define CHECK_HAVE_WARN_STRING(node, propname) \ |
| 623 | do { \ |
| 624 | CHECK_HAVE_WARN((node), (propname)); \ |
| 625 | if (prop && !data_is_one_string(prop->val)) \ |
| 626 | DO_ERR("\"%s\" property in %s is not a string\n", \ |
| 627 | (propname), (node)->fullpath); \ |
| 628 | } while (0) |
| 629 | |
| 630 | static int check_root(struct node *root) |
| 631 | { |
| 632 | struct property *prop; |
| 633 | int ok = 1; |
| 634 | |
| 635 | CHECK_HAVE_STRING(root, "model"); |
| 636 | CHECK_HAVE_WARN(root, "compatible"); |
| 637 | |
| 638 | return ok; |
| 639 | } |
| 640 | |
| 641 | static int check_cpus(struct node *root, int outversion, int boot_cpuid_phys) |
| 642 | { |
| 643 | struct node *cpus, *cpu; |
| 644 | struct property *prop; |
| 645 | struct node *bootcpu = NULL; |
| 646 | int ok = 1; |
| 647 | |
| 648 | cpus = get_subnode(root, "cpus"); |
| 649 | if (! cpus) { |
| 650 | ERRMSG("Missing /cpus node\n"); |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | if (cpus->addr_cells != 1) |
| 655 | DO_ERR("%s has bad #address-cells value %d (should be 1)\n", |
| 656 | cpus->fullpath, cpus->addr_cells); |
| 657 | if (cpus->size_cells != 0) |
| 658 | DO_ERR("%s has bad #size-cells value %d (should be 0)\n", |
| 659 | cpus->fullpath, cpus->size_cells); |
| 660 | |
| 661 | for_each_child(cpus, cpu) { |
| 662 | CHECK_HAVE_STREQ(cpu, "device_type", "cpu"); |
| 663 | |
| 664 | CHECK_HAVE_ONECELL(cpu, "reg"); |
| 665 | if (prop) { |
| 666 | cell_t unitnum; |
| 667 | char *eptr; |
| 668 | |
| 669 | unitnum = strtol(get_unitname(cpu), &eptr, 16); |
| 670 | if (*eptr) { |
| 671 | WARNMSG("%s has bad format unit name %s (should be CPU number\n", |
| 672 | cpu->fullpath, get_unitname(cpu)); |
| 673 | } else if (unitnum != propval_cell(prop)) { |
| 674 | WARNMSG("%s unit name \"%s\" does not match \"reg\" property <%x>\n", |
| 675 | cpu->fullpath, get_unitname(cpu), |
| 676 | propval_cell(prop)); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /* CHECK_HAVE_ONECELL(cpu, "d-cache-line-size"); */ |
| 681 | /* CHECK_HAVE_ONECELL(cpu, "i-cache-line-size"); */ |
| 682 | CHECK_HAVE_ONECELL(cpu, "d-cache-size"); |
| 683 | CHECK_HAVE_ONECELL(cpu, "i-cache-size"); |
| 684 | |
| 685 | CHECK_HAVE_WARN_ONECELL(cpu, "clock-frequency"); |
| 686 | CHECK_HAVE_WARN_ONECELL(cpu, "timebase-frequency"); |
| 687 | |
| 688 | prop = get_property(cpu, "linux,boot-cpu"); |
| 689 | if (prop) { |
| 690 | if (prop->val.len) |
| 691 | WARNMSG("\"linux,boot-cpu\" property in %s is non-empty\n", |
| 692 | cpu->fullpath); |
| 693 | if (bootcpu) |
| 694 | DO_ERR("Multiple boot cpus (%s and %s)\n", |
| 695 | bootcpu->fullpath, cpu->fullpath); |
| 696 | else |
| 697 | bootcpu = cpu; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | if (outversion < 2) { |
| 702 | if (! bootcpu) |
| 703 | WARNMSG("No cpu has \"linux,boot-cpu\" property\n"); |
| 704 | } else { |
| 705 | if (bootcpu) |
| 706 | WARNMSG("\"linux,boot-cpu\" property is deprecated in blob version 2 or higher\n"); |
| 707 | if (boot_cpuid_phys == 0xfeedbeef) |
| 708 | WARNMSG("physical boot CPU not set. Use -b option to set\n"); |
| 709 | } |
| 710 | |
| 711 | return ok; |
| 712 | } |
| 713 | |
| 714 | static int check_memory(struct node *root) |
| 715 | { |
| 716 | struct node *mem; |
| 717 | struct property *prop; |
| 718 | int nnodes = 0; |
| 719 | int ok = 1; |
| 720 | |
| 721 | for_each_child(root, mem) { |
| 722 | if (! strneq(mem->name, "memory", mem->basenamelen)) |
| 723 | continue; |
| 724 | |
| 725 | nnodes++; |
| 726 | |
| 727 | CHECK_HAVE_STREQ(mem, "device_type", "memory"); |
| 728 | CHECK_HAVE(mem, "reg"); |
| 729 | } |
| 730 | |
| 731 | if (nnodes == 0) { |
| 732 | ERRMSG("No memory nodes\n"); |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | return ok; |
| 737 | } |
| 738 | |
| 739 | int check_semantics(struct node *dt, int outversion, int boot_cpuid_phys) |
| 740 | { |
| 741 | int ok = 1; |
| 742 | |
| 743 | ok = ok && check_root(dt); |
| 744 | ok = ok && check_cpus(dt, outversion, boot_cpuid_phys); |
| 745 | ok = ok && check_memory(dt); |
| 746 | if (! ok) |
| 747 | return 0; |
| 748 | |
| 749 | return 1; |
| 750 | } |