David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| 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 | /* |
| 24 | * Tree building functions |
| 25 | */ |
| 26 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 27 | struct property *build_property(char *name, struct data val, char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 28 | { |
| 29 | struct property *new = xmalloc(sizeof(*new)); |
| 30 | |
| 31 | new->name = name; |
| 32 | new->val = val; |
| 33 | |
| 34 | new->next = NULL; |
| 35 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 36 | new->label = label; |
| 37 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 38 | return new; |
| 39 | } |
| 40 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 41 | struct property *chain_property(struct property *first, struct property *list) |
| 42 | { |
| 43 | assert(first->next == NULL); |
| 44 | |
| 45 | first->next = list; |
| 46 | return first; |
| 47 | } |
| 48 | |
| 49 | struct node *build_node(struct property *proplist, struct node *children) |
| 50 | { |
| 51 | struct node *new = xmalloc(sizeof(*new)); |
| 52 | struct node *child; |
| 53 | |
| 54 | memset(new, 0, sizeof(*new)); |
| 55 | |
| 56 | new->proplist = proplist; |
| 57 | new->children = children; |
| 58 | |
| 59 | for_each_child(new, child) { |
| 60 | child->parent = new; |
| 61 | } |
| 62 | |
| 63 | return new; |
| 64 | } |
| 65 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 66 | struct node *name_node(struct node *node, char *name, char * label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 67 | { |
| 68 | assert(node->name == NULL); |
| 69 | |
| 70 | node->name = name; |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 71 | |
| 72 | node->label = label; |
| 73 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 74 | return node; |
| 75 | } |
| 76 | |
| 77 | struct node *chain_node(struct node *first, struct node *list) |
| 78 | { |
| 79 | assert(first->next_sibling == NULL); |
| 80 | |
| 81 | first->next_sibling = list; |
| 82 | return first; |
| 83 | } |
| 84 | |
| 85 | void add_property(struct node *node, struct property *prop) |
| 86 | { |
David Gibson | 740a19a | 2005-10-21 17:26:45 +1000 | [diff] [blame] | 87 | struct property **p; |
| 88 | |
| 89 | prop->next = NULL; |
| 90 | |
| 91 | p = &node->proplist; |
| 92 | while (*p) |
| 93 | p = &((*p)->next); |
| 94 | |
| 95 | *p = prop; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void add_child(struct node *parent, struct node *child) |
| 99 | { |
David Gibson | 740a19a | 2005-10-21 17:26:45 +1000 | [diff] [blame] | 100 | struct node **p; |
| 101 | |
| 102 | child->next_sibling = NULL; |
| 103 | |
| 104 | p = &parent->children; |
| 105 | while (*p) |
| 106 | p = &((*p)->next_sibling); |
| 107 | |
| 108 | *p = child; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 109 | } |
| 110 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 111 | struct reserve_info *build_reserve_entry(u64 address, u64 size, char *label) |
| 112 | { |
| 113 | struct reserve_info *new = xmalloc(sizeof(*new)); |
| 114 | |
| 115 | new->re.address = address; |
| 116 | new->re.size = size; |
| 117 | |
| 118 | new->next = NULL; |
| 119 | |
| 120 | new->label = label; |
| 121 | |
| 122 | return new; |
| 123 | } |
| 124 | |
| 125 | struct reserve_info *chain_reserve_entry(struct reserve_info *first, |
| 126 | struct reserve_info *list) |
| 127 | { |
| 128 | assert(first->next == NULL); |
| 129 | |
| 130 | first->next = list; |
| 131 | return first; |
| 132 | } |
| 133 | |
| 134 | struct reserve_info *add_reserve_entry(struct reserve_info *list, |
| 135 | struct reserve_info *new) |
| 136 | { |
| 137 | struct reserve_info *last; |
| 138 | |
| 139 | new->next = NULL; |
| 140 | |
| 141 | if (! list) |
| 142 | return new; |
| 143 | |
| 144 | for (last = list; last->next; last = last->next) |
| 145 | ; |
| 146 | |
| 147 | last->next = new; |
| 148 | |
| 149 | return list; |
| 150 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Tree accessor functions |
| 154 | */ |
| 155 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 156 | static char *get_unitname(struct node *node) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 157 | { |
| 158 | if (node->name[node->basenamelen] == '\0') |
| 159 | return ""; |
| 160 | else |
| 161 | return node->name + node->basenamelen + 1; |
| 162 | } |
| 163 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 164 | static struct property *get_property(struct node *node, char *propname) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 165 | { |
| 166 | struct property *prop; |
| 167 | |
| 168 | for_each_property(node, prop) |
| 169 | if (streq(prop->name, propname)) |
| 170 | return prop; |
| 171 | |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | static cell_t propval_cell(struct property *prop) |
| 176 | { |
| 177 | assert(prop->val.len == sizeof(cell_t)); |
| 178 | return be32_to_cpu(*((cell_t *)prop->val.val)); |
| 179 | } |
| 180 | |
| 181 | static struct node *get_subnode(struct node *node, char *nodename) |
| 182 | { |
| 183 | struct node *child; |
| 184 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 185 | for_each_child(node, child) |
| 186 | if (streq(child->name, nodename)) |
| 187 | return child; |
| 188 | |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | static struct node *get_node_by_path(struct node *tree, char *path) |
| 193 | { |
| 194 | char *p; |
| 195 | struct node *child; |
| 196 | |
| 197 | if (!path || ! (*path)) |
| 198 | return tree; |
| 199 | |
| 200 | while (path[0] == '/') |
| 201 | path++; |
| 202 | |
| 203 | p = strchr(path, '/'); |
| 204 | |
| 205 | for_each_child(tree, child) { |
| 206 | if (p && strneq(path, child->name, p-path)) |
| 207 | return get_node_by_path(child, p+1); |
| 208 | else if (!p && streq(path, child->name)) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 209 | return child; |
| 210 | } |
| 211 | |
| 212 | return NULL; |
| 213 | } |
| 214 | |
David Gibson | c226ddc | 2007-02-07 14:29:07 +1100 | [diff] [blame] | 215 | static struct node *get_node_by_label(struct node *tree, const char *label) |
| 216 | { |
| 217 | struct node *child, *node; |
| 218 | |
| 219 | assert(label && (strlen(label) > 0)); |
| 220 | |
| 221 | if (tree->label && streq(tree->label, label)) |
| 222 | return tree; |
| 223 | |
| 224 | for_each_child(tree, child) { |
| 225 | node = get_node_by_label(child, label); |
| 226 | if (node) |
| 227 | return node; |
| 228 | } |
| 229 | |
| 230 | return NULL; |
| 231 | } |
| 232 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 233 | static struct node *get_node_by_phandle(struct node *tree, cell_t phandle) |
| 234 | { |
| 235 | struct node *child, *node; |
| 236 | |
| 237 | assert((phandle != 0) && (phandle != -1)); |
| 238 | |
| 239 | if (tree->phandle == phandle) |
| 240 | return tree; |
| 241 | |
| 242 | for_each_child(tree, child) { |
| 243 | node = get_node_by_phandle(child, phandle); |
| 244 | if (node) |
| 245 | return node; |
| 246 | } |
| 247 | |
| 248 | return NULL; |
| 249 | } |
David Gibson | c226ddc | 2007-02-07 14:29:07 +1100 | [diff] [blame] | 250 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 251 | /* |
| 252 | * Tree checking functions |
| 253 | */ |
| 254 | |
Jerry Van Baren | cd1da87 | 2007-03-18 16:49:24 -0400 | [diff] [blame] | 255 | #define ERRMSG(...) if (quiet < 2) fprintf(stderr, "ERROR: " __VA_ARGS__) |
| 256 | #define WARNMSG(...) if (quiet < 1) fprintf(stderr, "Warning: " __VA_ARGS__) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 257 | |
| 258 | static int must_be_one_cell(struct property *prop, struct node *node) |
| 259 | { |
| 260 | if (prop->val.len != sizeof(cell_t)) { |
| 261 | ERRMSG("\"%s\" property in %s has the wrong length (should be 1 cell)\n", |
| 262 | prop->name, node->fullpath); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | return 1; |
| 267 | } |
| 268 | |
| 269 | static int must_be_cells(struct property *prop, struct node *node) |
| 270 | { |
| 271 | if ((prop->val.len % sizeof(cell_t)) != 0) { |
| 272 | ERRMSG("\"%s\" property in %s is not a multiple of cell size\n", |
| 273 | prop->name, node->fullpath); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | static int must_be_string(struct property *prop, struct node *node) |
| 281 | { |
| 282 | if (! data_is_one_string(prop->val)) { |
| 283 | ERRMSG("\"%s\" property in %s is not a string\n", |
| 284 | prop->name, node->fullpath); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | return 1; |
| 289 | } |
| 290 | |
| 291 | static int name_prop_check(struct property *prop, struct node *node) |
| 292 | { |
| 293 | if ((prop->val.len != node->basenamelen+1) |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 294 | || !strneq(prop->val.val, node->name, node->basenamelen)) { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 295 | ERRMSG("name property \"%s\" does not match node basename in %s\n", |
| 296 | prop->val.val, |
| 297 | node->fullpath); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | return 1; |
| 302 | } |
| 303 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 304 | static struct { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 305 | char *propname; |
| 306 | int (*check_fn)(struct property *prop, struct node *node); |
| 307 | } prop_checker_table[] = { |
| 308 | {"name", must_be_string}, |
| 309 | {"name", name_prop_check}, |
| 310 | {"linux,phandle", must_be_one_cell}, |
| 311 | {"#address-cells", must_be_one_cell}, |
| 312 | {"#size-cells", must_be_one_cell}, |
| 313 | {"reg", must_be_cells}, |
| 314 | {"model", must_be_string}, |
| 315 | {"device_type", must_be_string}, |
| 316 | }; |
| 317 | |
| 318 | #define DO_ERR(...) do {ERRMSG(__VA_ARGS__); ok = 0; } while (0) |
| 319 | |
| 320 | static int check_properties(struct node *node) |
| 321 | { |
| 322 | struct property *prop, *prop2; |
| 323 | int ok = 1; |
| 324 | |
| 325 | for_each_property(node, prop) { |
| 326 | int i; |
| 327 | |
| 328 | /* check for duplicates */ |
| 329 | /* FIXME: do this more efficiently */ |
| 330 | for (prop2 = prop->next; prop2; prop2 = prop2->next) { |
| 331 | if (streq(prop->name, prop2->name)) { |
| 332 | DO_ERR("Duplicate propertyname %s in node %s\n", |
| 333 | prop->name, node->fullpath); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /* check name length */ |
| 339 | if (strlen(prop->name) > MAX_PROPNAME_LEN) |
David Gibson | cba839c | 2005-10-20 13:56:23 +1000 | [diff] [blame] | 340 | WARNMSG("Property name %s is too long in %s\n", |
| 341 | prop->name, node->fullpath); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 342 | |
| 343 | /* check this property */ |
| 344 | for (i = 0; i < ARRAY_SIZE(prop_checker_table); i++) { |
| 345 | if (streq(prop->name, prop_checker_table[i].propname)) |
| 346 | if (! prop_checker_table[i].check_fn(prop, node)) { |
| 347 | ok = 0; |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | return ok; |
| 354 | } |
| 355 | |
| 356 | static int check_node_name(struct node *node) |
| 357 | { |
| 358 | int ok = 1; |
| 359 | int len = strlen(node->name); |
| 360 | |
| 361 | if (len == 0 && node->parent) |
| 362 | DO_ERR("Empty, non-root nodename at %s\n", node->fullpath); |
| 363 | |
| 364 | if (len > MAX_NODENAME_LEN) |
| 365 | DO_ERR("Overlength nodename at %s\n", node->fullpath); |
| 366 | |
| 367 | |
| 368 | return ok; |
| 369 | } |
| 370 | |
| 371 | static int check_structure(struct node *tree) |
| 372 | { |
| 373 | struct node *child, *child2; |
| 374 | int ok = 1; |
| 375 | |
| 376 | if (! check_node_name(tree)) |
| 377 | ok = 0; |
| 378 | |
| 379 | if (! check_properties(tree)) |
| 380 | ok = 0; |
| 381 | |
| 382 | for_each_child(tree, child) { |
| 383 | /* Check for duplicates */ |
| 384 | |
| 385 | for (child2 = child->next_sibling; |
| 386 | child2; |
| 387 | child2 = child2->next_sibling) { |
| 388 | if (streq(child->name, child2->name)) |
| 389 | DO_ERR("Duplicate node name %s\n", |
| 390 | child->fullpath); |
| 391 | } |
| 392 | if (! check_structure(child)) |
| 393 | ok = 0; |
| 394 | } |
| 395 | |
| 396 | return ok; |
| 397 | } |
| 398 | |
| 399 | #define CHECK_HAVE(node, propname) \ |
| 400 | do { \ |
| 401 | if (! (prop = get_property((node), (propname)))) \ |
| 402 | DO_ERR("Missing \"%s\" property in %s\n", (propname), \ |
| 403 | (node)->fullpath); \ |
| 404 | } while (0); |
| 405 | |
| 406 | #define CHECK_HAVE_WARN(node, propname) \ |
| 407 | do { \ |
| 408 | if (! (prop = get_property((node), (propname)))) \ |
| 409 | WARNMSG("%s has no \"%s\" property\n", \ |
| 410 | (node)->fullpath, (propname)); \ |
| 411 | } while (0) |
| 412 | |
| 413 | #define CHECK_HAVE_STRING(node, propname) \ |
| 414 | do { \ |
| 415 | CHECK_HAVE((node), (propname)); \ |
| 416 | if (prop && !data_is_one_string(prop->val)) \ |
| 417 | DO_ERR("\"%s\" property in %s is not a string\n", \ |
| 418 | (propname), (node)->fullpath); \ |
| 419 | } while (0) |
| 420 | |
| 421 | #define CHECK_HAVE_STREQ(node, propname, value) \ |
| 422 | do { \ |
| 423 | CHECK_HAVE_STRING((node), (propname)); \ |
| 424 | if (prop && !streq(prop->val.val, (value))) \ |
| 425 | DO_ERR("%s has wrong %s, %s (should be %s\n", \ |
| 426 | (node)->fullpath, (propname), \ |
| 427 | prop->val.val, (value)); \ |
| 428 | } while (0) |
| 429 | |
| 430 | #define CHECK_HAVE_ONECELL(node, propname) \ |
| 431 | do { \ |
| 432 | CHECK_HAVE((node), (propname)); \ |
| 433 | if (prop && (prop->val.len != sizeof(cell_t))) \ |
| 434 | DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \ |
| 435 | } while (0) |
| 436 | |
| 437 | #define CHECK_HAVE_WARN_ONECELL(node, propname) \ |
| 438 | do { \ |
| 439 | CHECK_HAVE_WARN((node), (propname)); \ |
| 440 | if (prop && (prop->val.len != sizeof(cell_t))) \ |
| 441 | DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \ |
| 442 | } while (0) |
| 443 | |
| 444 | #define CHECK_HAVE_WARN_PHANDLE(xnode, propname, root) \ |
| 445 | do { \ |
| 446 | struct node *ref; \ |
| 447 | CHECK_HAVE_WARN_ONECELL((xnode), (propname)); \ |
| 448 | if (prop) {\ |
David Gibson | 5438239 | 2007-01-31 15:45:26 +1100 | [diff] [blame] | 449 | cell_t phandle = propval_cell(prop); \ |
| 450 | if ((phandle == 0) || (phandle == -1)) { \ |
| 451 | DO_ERR("\"%s\" property in %s contains an invalid phandle %x\n", (propname), (xnode)->fullpath, phandle); \ |
| 452 | } else { \ |
| 453 | ref = get_node_by_phandle((root), propval_cell(prop)); \ |
| 454 | if (! ref) \ |
| 455 | DO_ERR("\"%s\" property in %s refers to non-existant phandle %x\n", (propname), (xnode)->fullpath, propval_cell(prop)); \ |
| 456 | } \ |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 457 | } \ |
| 458 | } while (0) |
| 459 | |
| 460 | #define CHECK_HAVE_WARN_STRING(node, propname) \ |
| 461 | do { \ |
| 462 | CHECK_HAVE_WARN((node), (propname)); \ |
| 463 | if (prop && !data_is_one_string(prop->val)) \ |
| 464 | DO_ERR("\"%s\" property in %s is not a string\n", \ |
| 465 | (propname), (node)->fullpath); \ |
| 466 | } while (0) |
| 467 | |
| 468 | static int check_root(struct node *root) |
| 469 | { |
| 470 | struct property *prop; |
| 471 | int ok = 1; |
| 472 | |
| 473 | CHECK_HAVE_STRING(root, "model"); |
| 474 | |
| 475 | CHECK_HAVE(root, "#address-cells"); |
| 476 | CHECK_HAVE(root, "#size-cells"); |
| 477 | |
| 478 | CHECK_HAVE_WARN(root, "compatible"); |
| 479 | |
| 480 | return ok; |
| 481 | } |
| 482 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 483 | static int check_cpus(struct node *root, int outversion, int boot_cpuid_phys) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 484 | { |
| 485 | struct node *cpus, *cpu; |
| 486 | struct property *prop; |
| 487 | struct node *bootcpu = NULL; |
| 488 | int ok = 1; |
| 489 | |
| 490 | cpus = get_subnode(root, "cpus"); |
| 491 | if (! cpus) { |
| 492 | ERRMSG("Missing /cpus node\n"); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | CHECK_HAVE_WARN(cpus, "#address-cells"); |
| 497 | CHECK_HAVE_WARN(cpus, "#size-cells"); |
| 498 | |
| 499 | for_each_child(cpus, cpu) { |
| 500 | CHECK_HAVE_STREQ(cpu, "device_type", "cpu"); |
| 501 | |
| 502 | if (cpu->addr_cells != 1) |
| 503 | DO_ERR("%s has bad #address-cells value %d (should be 1)\n", |
| 504 | cpu->fullpath, cpu->addr_cells); |
| 505 | if (cpu->size_cells != 0) |
| 506 | DO_ERR("%s has bad #size-cells value %d (should be 0)\n", |
| 507 | cpu->fullpath, cpu->size_cells); |
| 508 | |
| 509 | CHECK_HAVE_ONECELL(cpu, "reg"); |
| 510 | if (prop) { |
| 511 | cell_t unitnum; |
| 512 | char *eptr; |
| 513 | |
| 514 | unitnum = strtol(get_unitname(cpu), &eptr, 16); |
Jerry Van Baren | cd1da87 | 2007-03-18 16:49:24 -0400 | [diff] [blame] | 515 | if (*eptr) { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 516 | WARNMSG("%s has bad format unit name %s (should be CPU number\n", |
| 517 | cpu->fullpath, get_unitname(cpu)); |
Jerry Van Baren | cd1da87 | 2007-03-18 16:49:24 -0400 | [diff] [blame] | 518 | } else if (unitnum != propval_cell(prop)) { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 519 | WARNMSG("%s unit name \"%s\" does not match \"reg\" property <%x>\n", |
| 520 | cpu->fullpath, get_unitname(cpu), |
| 521 | propval_cell(prop)); |
Jerry Van Baren | cd1da87 | 2007-03-18 16:49:24 -0400 | [diff] [blame] | 522 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* CHECK_HAVE_ONECELL(cpu, "d-cache-line-size"); */ |
| 526 | /* CHECK_HAVE_ONECELL(cpu, "i-cache-line-size"); */ |
| 527 | CHECK_HAVE_ONECELL(cpu, "d-cache-size"); |
| 528 | CHECK_HAVE_ONECELL(cpu, "i-cache-size"); |
| 529 | |
| 530 | CHECK_HAVE_WARN_ONECELL(cpu, "clock-frequency"); |
| 531 | CHECK_HAVE_WARN_ONECELL(cpu, "timebase-frequency"); |
| 532 | |
| 533 | prop = get_property(cpu, "linux,boot-cpu"); |
| 534 | if (prop) { |
| 535 | if (prop->val.len) |
| 536 | WARNMSG("\"linux,boot-cpu\" property in %s is non-empty\n", |
| 537 | cpu->fullpath); |
| 538 | if (bootcpu) |
| 539 | DO_ERR("Multiple boot cpus (%s and %s)\n", |
| 540 | bootcpu->fullpath, cpu->fullpath); |
| 541 | else |
| 542 | bootcpu = cpu; |
| 543 | } |
| 544 | } |
| 545 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 546 | if (outversion < 2) { |
| 547 | if (! bootcpu) |
| 548 | WARNMSG("No cpu has \"linux,boot-cpu\" property\n"); |
| 549 | } else { |
| 550 | if (bootcpu) |
| 551 | WARNMSG("\"linux,boot-cpu\" property is deprecated in blob version 2 or higher\n"); |
| 552 | if (boot_cpuid_phys == 0xfeedbeef) |
| 553 | WARNMSG("physical boot CPU not set. Use -b option to set\n"); |
| 554 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 555 | |
| 556 | return ok; |
| 557 | } |
| 558 | |
| 559 | static int check_memory(struct node *root) |
| 560 | { |
| 561 | struct node *mem; |
| 562 | struct property *prop; |
| 563 | int nnodes = 0; |
| 564 | int ok = 1; |
| 565 | |
| 566 | for_each_child(root, mem) { |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 567 | if (! strneq(mem->name, "memory", mem->basenamelen)) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 568 | continue; |
| 569 | |
| 570 | nnodes++; |
| 571 | |
| 572 | CHECK_HAVE_STREQ(mem, "device_type", "memory"); |
| 573 | CHECK_HAVE(mem, "reg"); |
| 574 | } |
| 575 | |
| 576 | if (nnodes == 0) { |
| 577 | ERRMSG("No memory nodes\n"); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | return ok; |
| 582 | } |
| 583 | |
| 584 | static int check_chosen(struct node *root) |
| 585 | { |
| 586 | struct node *chosen; |
| 587 | struct property *prop; |
| 588 | int ok = 1; |
| 589 | |
| 590 | chosen = get_subnode(root, "chosen"); |
Scott Wood | b29597d | 2007-03-22 11:11:09 -0500 | [diff] [blame^] | 591 | if (!chosen) |
| 592 | return ok; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 593 | |
Stuart Yoder | 5ae78ad | 2007-02-19 11:28:27 -0600 | [diff] [blame] | 594 | /* give warning for obsolete interrupt-controller property */ |
| 595 | do { |
| 596 | if ((prop = get_property(chosen, "interrupt-controller")) != NULL) { |
| 597 | WARNMSG("%s has obsolete \"%s\" property\n", |
| 598 | chosen->fullpath, "interrupt-controller"); |
| 599 | } |
| 600 | } while (0); |
| 601 | |
| 602 | return ok; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static int check_addr_size_reg(struct node *node, |
| 606 | int p_addr_cells, int p_size_cells) |
| 607 | { |
| 608 | int addr_cells = p_addr_cells; |
| 609 | int size_cells = p_size_cells; |
| 610 | struct property *prop; |
| 611 | struct node *child; |
| 612 | int ok = 1; |
| 613 | |
| 614 | node->addr_cells = addr_cells; |
| 615 | node->size_cells = size_cells; |
| 616 | |
| 617 | prop = get_property(node, "reg"); |
| 618 | if (prop) { |
| 619 | int len = prop->val.len / 4; |
| 620 | |
| 621 | if ((len % (addr_cells+size_cells)) != 0) |
| 622 | DO_ERR("\"reg\" property in %s has invalid length (%d) for given #address-cells (%d) and #size-cells (%d)\n", |
| 623 | node->fullpath, prop->val.len, |
| 624 | addr_cells, size_cells); |
| 625 | } |
| 626 | |
| 627 | prop = get_property(node, "#address-cells"); |
| 628 | if (prop) |
| 629 | addr_cells = propval_cell(prop); |
| 630 | |
| 631 | prop = get_property(node, "#size-cells"); |
| 632 | if (prop) |
| 633 | size_cells = propval_cell(prop); |
| 634 | |
| 635 | for_each_child(node, child) { |
| 636 | ok = ok && check_addr_size_reg(child, addr_cells, size_cells); |
| 637 | } |
| 638 | |
| 639 | return ok; |
| 640 | } |
| 641 | |
| 642 | static int check_phandles(struct node *root, struct node *node) |
| 643 | { |
| 644 | struct property *prop; |
| 645 | struct node *child, *other; |
| 646 | cell_t phandle; |
| 647 | int ok = 1; |
| 648 | |
| 649 | prop = get_property(node, "linux,phandle"); |
| 650 | if (prop) { |
| 651 | phandle = propval_cell(prop); |
| 652 | if ((phandle == 0) || (phandle == -1)) { |
| 653 | DO_ERR("%s has invalid linux,phandle %x\n", |
| 654 | node->fullpath, phandle); |
| 655 | } else { |
| 656 | other = get_node_by_phandle(root, phandle); |
| 657 | if (other) |
| 658 | DO_ERR("%s has duplicated phandle %x (seen before at %s)\n", |
| 659 | node->fullpath, phandle, other->fullpath); |
| 660 | |
| 661 | node->phandle = phandle; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | for_each_child(node, child) |
| 666 | ok = ok && check_phandles(root, child); |
| 667 | |
| 668 | return 1; |
| 669 | } |
| 670 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 671 | static cell_t get_node_phandle(struct node *root, struct node *node) |
| 672 | { |
| 673 | static cell_t phandle = 1; /* FIXME: ick, static local */ |
| 674 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 675 | if ((node->phandle != 0) && (node->phandle != -1)) |
| 676 | return node->phandle; |
| 677 | |
| 678 | assert(! get_property(node, "linux,phandle")); |
| 679 | |
| 680 | while (get_node_by_phandle(root, phandle)) |
| 681 | phandle++; |
| 682 | |
| 683 | node->phandle = phandle; |
| 684 | add_property(node, |
| 685 | build_property("linux,phandle", |
| 686 | data_append_cell(empty_data, phandle), |
| 687 | NULL)); |
| 688 | |
| 689 | return node->phandle; |
| 690 | } |
| 691 | |
| 692 | static void apply_fixup(struct node *root, struct property *prop, |
| 693 | struct fixup *f) |
| 694 | { |
| 695 | struct node *refnode; |
| 696 | cell_t phandle; |
| 697 | |
David Gibson | c226ddc | 2007-02-07 14:29:07 +1100 | [diff] [blame] | 698 | if (f->ref[0] == '/') { |
| 699 | /* Reference to full path */ |
| 700 | refnode = get_node_by_path(root, f->ref); |
| 701 | if (! refnode) |
| 702 | die("Reference to non-existent node \"%s\"\n", f->ref); |
| 703 | } else { |
| 704 | refnode = get_node_by_label(root, f->ref); |
| 705 | if (! refnode) |
| 706 | die("Reference to non-existent node label \"%s\"\n", f->ref); |
| 707 | } |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 708 | |
| 709 | phandle = get_node_phandle(root, refnode); |
| 710 | |
| 711 | assert(f->offset + sizeof(cell_t) <= prop->val.len); |
| 712 | |
| 713 | *((cell_t *)(prop->val.val + f->offset)) = cpu_to_be32(phandle); |
| 714 | } |
| 715 | |
| 716 | static void fixup_phandles(struct node *root, struct node *node) |
| 717 | { |
| 718 | struct property *prop; |
| 719 | struct node *child; |
| 720 | |
| 721 | for_each_property(node, prop) { |
| 722 | struct fixup *f = prop->val.refs; |
| 723 | |
| 724 | while (f) { |
| 725 | apply_fixup(root, prop, f); |
| 726 | prop->val.refs = f->next; |
| 727 | fixup_free(f); |
| 728 | f = prop->val.refs; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | for_each_child(node, child) |
| 733 | fixup_phandles(root, child); |
| 734 | } |
| 735 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 736 | int check_device_tree(struct node *dt, int outversion, int boot_cpuid_phys) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 737 | { |
| 738 | int ok = 1; |
| 739 | |
| 740 | if (! check_structure(dt)) |
| 741 | return 0; |
| 742 | |
| 743 | ok = ok && check_addr_size_reg(dt, -1, -1); |
| 744 | ok = ok && check_phandles(dt, dt); |
| 745 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 746 | fixup_phandles(dt, dt); |
| 747 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 748 | if (! ok) |
| 749 | return 0; |
| 750 | |
| 751 | ok = ok && check_root(dt); |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 752 | ok = ok && check_cpus(dt, outversion, boot_cpuid_phys); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 753 | ok = ok && check_memory(dt); |
| 754 | ok = ok && check_chosen(dt); |
| 755 | if (! ok) |
| 756 | return 0; |
| 757 | |
| 758 | return 1; |
| 759 | } |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 760 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 761 | struct boot_info *build_boot_info(struct reserve_info *reservelist, |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 762 | struct node *tree) |
| 763 | { |
| 764 | struct boot_info *bi; |
| 765 | |
| 766 | bi = xmalloc(sizeof(*bi)); |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 767 | bi->reservelist = reservelist; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 768 | bi->dt = tree; |
| 769 | |
| 770 | return bi; |
| 771 | } |