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"); |
| 591 | if (! chosen) { |
| 592 | ERRMSG("Missing /chosen node\n"); |
| 593 | return 0; |
| 594 | } |
| 595 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 596 | CHECK_HAVE_WARN_STRING(chosen, "bootargs"); |
| 597 | CHECK_HAVE_WARN_STRING(chosen, "linux,stdout-path"); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 598 | |
Stuart Yoder | 5ae78ad | 2007-02-19 11:28:27 -0600 | [diff] [blame] | 599 | /* give warning for obsolete interrupt-controller property */ |
| 600 | do { |
| 601 | if ((prop = get_property(chosen, "interrupt-controller")) != NULL) { |
| 602 | WARNMSG("%s has obsolete \"%s\" property\n", |
| 603 | chosen->fullpath, "interrupt-controller"); |
| 604 | } |
| 605 | } while (0); |
| 606 | |
| 607 | return ok; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static int check_addr_size_reg(struct node *node, |
| 611 | int p_addr_cells, int p_size_cells) |
| 612 | { |
| 613 | int addr_cells = p_addr_cells; |
| 614 | int size_cells = p_size_cells; |
| 615 | struct property *prop; |
| 616 | struct node *child; |
| 617 | int ok = 1; |
| 618 | |
| 619 | node->addr_cells = addr_cells; |
| 620 | node->size_cells = size_cells; |
| 621 | |
| 622 | prop = get_property(node, "reg"); |
| 623 | if (prop) { |
| 624 | int len = prop->val.len / 4; |
| 625 | |
| 626 | if ((len % (addr_cells+size_cells)) != 0) |
| 627 | DO_ERR("\"reg\" property in %s has invalid length (%d) for given #address-cells (%d) and #size-cells (%d)\n", |
| 628 | node->fullpath, prop->val.len, |
| 629 | addr_cells, size_cells); |
| 630 | } |
| 631 | |
| 632 | prop = get_property(node, "#address-cells"); |
| 633 | if (prop) |
| 634 | addr_cells = propval_cell(prop); |
| 635 | |
| 636 | prop = get_property(node, "#size-cells"); |
| 637 | if (prop) |
| 638 | size_cells = propval_cell(prop); |
| 639 | |
| 640 | for_each_child(node, child) { |
| 641 | ok = ok && check_addr_size_reg(child, addr_cells, size_cells); |
| 642 | } |
| 643 | |
| 644 | return ok; |
| 645 | } |
| 646 | |
| 647 | static int check_phandles(struct node *root, struct node *node) |
| 648 | { |
| 649 | struct property *prop; |
| 650 | struct node *child, *other; |
| 651 | cell_t phandle; |
| 652 | int ok = 1; |
| 653 | |
| 654 | prop = get_property(node, "linux,phandle"); |
| 655 | if (prop) { |
| 656 | phandle = propval_cell(prop); |
| 657 | if ((phandle == 0) || (phandle == -1)) { |
| 658 | DO_ERR("%s has invalid linux,phandle %x\n", |
| 659 | node->fullpath, phandle); |
| 660 | } else { |
| 661 | other = get_node_by_phandle(root, phandle); |
| 662 | if (other) |
| 663 | DO_ERR("%s has duplicated phandle %x (seen before at %s)\n", |
| 664 | node->fullpath, phandle, other->fullpath); |
| 665 | |
| 666 | node->phandle = phandle; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | for_each_child(node, child) |
| 671 | ok = ok && check_phandles(root, child); |
| 672 | |
| 673 | return 1; |
| 674 | } |
| 675 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 676 | static cell_t get_node_phandle(struct node *root, struct node *node) |
| 677 | { |
| 678 | static cell_t phandle = 1; /* FIXME: ick, static local */ |
| 679 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 680 | if ((node->phandle != 0) && (node->phandle != -1)) |
| 681 | return node->phandle; |
| 682 | |
| 683 | assert(! get_property(node, "linux,phandle")); |
| 684 | |
| 685 | while (get_node_by_phandle(root, phandle)) |
| 686 | phandle++; |
| 687 | |
| 688 | node->phandle = phandle; |
| 689 | add_property(node, |
| 690 | build_property("linux,phandle", |
| 691 | data_append_cell(empty_data, phandle), |
| 692 | NULL)); |
| 693 | |
| 694 | return node->phandle; |
| 695 | } |
| 696 | |
| 697 | static void apply_fixup(struct node *root, struct property *prop, |
| 698 | struct fixup *f) |
| 699 | { |
| 700 | struct node *refnode; |
| 701 | cell_t phandle; |
| 702 | |
David Gibson | c226ddc | 2007-02-07 14:29:07 +1100 | [diff] [blame] | 703 | if (f->ref[0] == '/') { |
| 704 | /* Reference to full path */ |
| 705 | refnode = get_node_by_path(root, f->ref); |
| 706 | if (! refnode) |
| 707 | die("Reference to non-existent node \"%s\"\n", f->ref); |
| 708 | } else { |
| 709 | refnode = get_node_by_label(root, f->ref); |
| 710 | if (! refnode) |
| 711 | die("Reference to non-existent node label \"%s\"\n", f->ref); |
| 712 | } |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 713 | |
| 714 | phandle = get_node_phandle(root, refnode); |
| 715 | |
| 716 | assert(f->offset + sizeof(cell_t) <= prop->val.len); |
| 717 | |
| 718 | *((cell_t *)(prop->val.val + f->offset)) = cpu_to_be32(phandle); |
| 719 | } |
| 720 | |
| 721 | static void fixup_phandles(struct node *root, struct node *node) |
| 722 | { |
| 723 | struct property *prop; |
| 724 | struct node *child; |
| 725 | |
| 726 | for_each_property(node, prop) { |
| 727 | struct fixup *f = prop->val.refs; |
| 728 | |
| 729 | while (f) { |
| 730 | apply_fixup(root, prop, f); |
| 731 | prop->val.refs = f->next; |
| 732 | fixup_free(f); |
| 733 | f = prop->val.refs; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | for_each_child(node, child) |
| 738 | fixup_phandles(root, child); |
| 739 | } |
| 740 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 741 | 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] | 742 | { |
| 743 | int ok = 1; |
| 744 | |
| 745 | if (! check_structure(dt)) |
| 746 | return 0; |
| 747 | |
| 748 | ok = ok && check_addr_size_reg(dt, -1, -1); |
| 749 | ok = ok && check_phandles(dt, dt); |
| 750 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 751 | fixup_phandles(dt, dt); |
| 752 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 753 | if (! ok) |
| 754 | return 0; |
| 755 | |
| 756 | ok = ok && check_root(dt); |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 757 | ok = ok && check_cpus(dt, outversion, boot_cpuid_phys); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 758 | ok = ok && check_memory(dt); |
| 759 | ok = ok && check_chosen(dt); |
| 760 | if (! ok) |
| 761 | return 0; |
| 762 | |
| 763 | return 1; |
| 764 | } |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 765 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 766 | struct boot_info *build_boot_info(struct reserve_info *reservelist, |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 767 | struct node *tree) |
| 768 | { |
| 769 | struct boot_info *bi; |
| 770 | |
| 771 | bi = xmalloc(sizeof(*bi)); |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 772 | bi->reservelist = reservelist; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 773 | bi->dt = tree; |
| 774 | |
| 775 | return bi; |
| 776 | } |