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 | * |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 4 | * |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 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. |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 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 |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 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); |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 44 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 45 | first->next = list; |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 46 | return first; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 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 | { |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 235 | struct node *child, *node; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 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 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 336 | |
| 337 | /* check name length */ |
| 338 | if (strlen(prop->name) > MAX_PROPNAME_LEN) |
David Gibson | cba839c | 2005-10-20 13:56:23 +1000 | [diff] [blame] | 339 | WARNMSG("Property name %s is too long in %s\n", |
| 340 | prop->name, node->fullpath); |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 341 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 342 | /* check this property */ |
| 343 | for (i = 0; i < ARRAY_SIZE(prop_checker_table); i++) { |
| 344 | if (streq(prop->name, prop_checker_table[i].propname)) |
| 345 | if (! prop_checker_table[i].check_fn(prop, node)) { |
| 346 | ok = 0; |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return ok; |
| 353 | } |
| 354 | |
| 355 | static int check_node_name(struct node *node) |
| 356 | { |
| 357 | int ok = 1; |
| 358 | int len = strlen(node->name); |
| 359 | |
| 360 | if (len == 0 && node->parent) |
| 361 | DO_ERR("Empty, non-root nodename at %s\n", node->fullpath); |
| 362 | |
| 363 | if (len > MAX_NODENAME_LEN) |
| 364 | DO_ERR("Overlength nodename at %s\n", node->fullpath); |
| 365 | |
| 366 | |
| 367 | return ok; |
| 368 | } |
| 369 | |
| 370 | static int check_structure(struct node *tree) |
| 371 | { |
| 372 | struct node *child, *child2; |
| 373 | int ok = 1; |
| 374 | |
| 375 | if (! check_node_name(tree)) |
| 376 | ok = 0; |
| 377 | |
| 378 | if (! check_properties(tree)) |
| 379 | ok = 0; |
| 380 | |
| 381 | for_each_child(tree, child) { |
| 382 | /* Check for duplicates */ |
| 383 | |
| 384 | for (child2 = child->next_sibling; |
| 385 | child2; |
| 386 | child2 = child2->next_sibling) { |
| 387 | if (streq(child->name, child2->name)) |
| 388 | DO_ERR("Duplicate node name %s\n", |
| 389 | child->fullpath); |
| 390 | } |
| 391 | if (! check_structure(child)) |
| 392 | ok = 0; |
| 393 | } |
| 394 | |
| 395 | return ok; |
| 396 | } |
| 397 | |
| 398 | #define CHECK_HAVE(node, propname) \ |
| 399 | do { \ |
| 400 | if (! (prop = get_property((node), (propname)))) \ |
| 401 | DO_ERR("Missing \"%s\" property in %s\n", (propname), \ |
| 402 | (node)->fullpath); \ |
| 403 | } while (0); |
| 404 | |
| 405 | #define CHECK_HAVE_WARN(node, propname) \ |
| 406 | do { \ |
| 407 | if (! (prop = get_property((node), (propname)))) \ |
| 408 | WARNMSG("%s has no \"%s\" property\n", \ |
| 409 | (node)->fullpath, (propname)); \ |
| 410 | } while (0) |
| 411 | |
| 412 | #define CHECK_HAVE_STRING(node, propname) \ |
| 413 | do { \ |
| 414 | CHECK_HAVE((node), (propname)); \ |
| 415 | if (prop && !data_is_one_string(prop->val)) \ |
| 416 | DO_ERR("\"%s\" property in %s is not a string\n", \ |
| 417 | (propname), (node)->fullpath); \ |
| 418 | } while (0) |
| 419 | |
| 420 | #define CHECK_HAVE_STREQ(node, propname, value) \ |
| 421 | do { \ |
| 422 | CHECK_HAVE_STRING((node), (propname)); \ |
| 423 | if (prop && !streq(prop->val.val, (value))) \ |
| 424 | DO_ERR("%s has wrong %s, %s (should be %s\n", \ |
| 425 | (node)->fullpath, (propname), \ |
| 426 | prop->val.val, (value)); \ |
| 427 | } while (0) |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 428 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 429 | #define CHECK_HAVE_ONECELL(node, propname) \ |
| 430 | do { \ |
| 431 | CHECK_HAVE((node), (propname)); \ |
| 432 | if (prop && (prop->val.len != sizeof(cell_t))) \ |
| 433 | DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \ |
| 434 | } while (0) |
| 435 | |
| 436 | #define CHECK_HAVE_WARN_ONECELL(node, propname) \ |
| 437 | do { \ |
| 438 | CHECK_HAVE_WARN((node), (propname)); \ |
| 439 | if (prop && (prop->val.len != sizeof(cell_t))) \ |
| 440 | DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \ |
| 441 | } while (0) |
| 442 | |
| 443 | #define CHECK_HAVE_WARN_PHANDLE(xnode, propname, root) \ |
| 444 | do { \ |
| 445 | struct node *ref; \ |
| 446 | CHECK_HAVE_WARN_ONECELL((xnode), (propname)); \ |
| 447 | if (prop) {\ |
David Gibson | 5438239 | 2007-01-31 15:45:26 +1100 | [diff] [blame] | 448 | cell_t phandle = propval_cell(prop); \ |
| 449 | if ((phandle == 0) || (phandle == -1)) { \ |
| 450 | DO_ERR("\"%s\" property in %s contains an invalid phandle %x\n", (propname), (xnode)->fullpath, phandle); \ |
| 451 | } else { \ |
| 452 | ref = get_node_by_phandle((root), propval_cell(prop)); \ |
| 453 | if (! ref) \ |
| 454 | DO_ERR("\"%s\" property in %s refers to non-existant phandle %x\n", (propname), (xnode)->fullpath, propval_cell(prop)); \ |
| 455 | } \ |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 456 | } \ |
| 457 | } while (0) |
| 458 | |
| 459 | #define CHECK_HAVE_WARN_STRING(node, propname) \ |
| 460 | do { \ |
| 461 | CHECK_HAVE_WARN((node), (propname)); \ |
| 462 | if (prop && !data_is_one_string(prop->val)) \ |
| 463 | DO_ERR("\"%s\" property in %s is not a string\n", \ |
| 464 | (propname), (node)->fullpath); \ |
| 465 | } while (0) |
| 466 | |
| 467 | static int check_root(struct node *root) |
| 468 | { |
| 469 | struct property *prop; |
| 470 | int ok = 1; |
| 471 | |
| 472 | CHECK_HAVE_STRING(root, "model"); |
| 473 | |
| 474 | CHECK_HAVE(root, "#address-cells"); |
| 475 | CHECK_HAVE(root, "#size-cells"); |
| 476 | |
| 477 | CHECK_HAVE_WARN(root, "compatible"); |
| 478 | |
| 479 | return ok; |
| 480 | } |
| 481 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 482 | 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] | 483 | { |
| 484 | struct node *cpus, *cpu; |
| 485 | struct property *prop; |
| 486 | struct node *bootcpu = NULL; |
| 487 | int ok = 1; |
| 488 | |
| 489 | cpus = get_subnode(root, "cpus"); |
| 490 | if (! cpus) { |
| 491 | ERRMSG("Missing /cpus node\n"); |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | CHECK_HAVE_WARN(cpus, "#address-cells"); |
| 496 | CHECK_HAVE_WARN(cpus, "#size-cells"); |
| 497 | |
| 498 | for_each_child(cpus, cpu) { |
| 499 | CHECK_HAVE_STREQ(cpu, "device_type", "cpu"); |
| 500 | |
| 501 | if (cpu->addr_cells != 1) |
| 502 | DO_ERR("%s has bad #address-cells value %d (should be 1)\n", |
| 503 | cpu->fullpath, cpu->addr_cells); |
| 504 | if (cpu->size_cells != 0) |
| 505 | DO_ERR("%s has bad #size-cells value %d (should be 0)\n", |
| 506 | cpu->fullpath, cpu->size_cells); |
| 507 | |
| 508 | CHECK_HAVE_ONECELL(cpu, "reg"); |
| 509 | if (prop) { |
| 510 | cell_t unitnum; |
| 511 | char *eptr; |
| 512 | |
| 513 | unitnum = strtol(get_unitname(cpu), &eptr, 16); |
Jerry Van Baren | cd1da87 | 2007-03-18 16:49:24 -0400 | [diff] [blame] | 514 | if (*eptr) { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 515 | WARNMSG("%s has bad format unit name %s (should be CPU number\n", |
| 516 | cpu->fullpath, get_unitname(cpu)); |
Jerry Van Baren | cd1da87 | 2007-03-18 16:49:24 -0400 | [diff] [blame] | 517 | } else if (unitnum != propval_cell(prop)) { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 518 | WARNMSG("%s unit name \"%s\" does not match \"reg\" property <%x>\n", |
| 519 | cpu->fullpath, get_unitname(cpu), |
| 520 | propval_cell(prop)); |
Jerry Van Baren | cd1da87 | 2007-03-18 16:49:24 -0400 | [diff] [blame] | 521 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* CHECK_HAVE_ONECELL(cpu, "d-cache-line-size"); */ |
| 525 | /* CHECK_HAVE_ONECELL(cpu, "i-cache-line-size"); */ |
| 526 | CHECK_HAVE_ONECELL(cpu, "d-cache-size"); |
| 527 | CHECK_HAVE_ONECELL(cpu, "i-cache-size"); |
| 528 | |
| 529 | CHECK_HAVE_WARN_ONECELL(cpu, "clock-frequency"); |
| 530 | CHECK_HAVE_WARN_ONECELL(cpu, "timebase-frequency"); |
| 531 | |
| 532 | prop = get_property(cpu, "linux,boot-cpu"); |
| 533 | if (prop) { |
| 534 | if (prop->val.len) |
| 535 | WARNMSG("\"linux,boot-cpu\" property in %s is non-empty\n", |
| 536 | cpu->fullpath); |
| 537 | if (bootcpu) |
| 538 | DO_ERR("Multiple boot cpus (%s and %s)\n", |
| 539 | bootcpu->fullpath, cpu->fullpath); |
| 540 | else |
| 541 | bootcpu = cpu; |
| 542 | } |
| 543 | } |
| 544 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 545 | if (outversion < 2) { |
| 546 | if (! bootcpu) |
| 547 | WARNMSG("No cpu has \"linux,boot-cpu\" property\n"); |
| 548 | } else { |
| 549 | if (bootcpu) |
| 550 | WARNMSG("\"linux,boot-cpu\" property is deprecated in blob version 2 or higher\n"); |
| 551 | if (boot_cpuid_phys == 0xfeedbeef) |
| 552 | WARNMSG("physical boot CPU not set. Use -b option to set\n"); |
| 553 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 554 | |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 555 | return ok; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static int check_memory(struct node *root) |
| 559 | { |
| 560 | struct node *mem; |
| 561 | struct property *prop; |
| 562 | int nnodes = 0; |
| 563 | int ok = 1; |
| 564 | |
| 565 | for_each_child(root, mem) { |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 566 | if (! strneq(mem->name, "memory", mem->basenamelen)) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 567 | continue; |
| 568 | |
| 569 | nnodes++; |
| 570 | |
| 571 | CHECK_HAVE_STREQ(mem, "device_type", "memory"); |
| 572 | CHECK_HAVE(mem, "reg"); |
| 573 | } |
| 574 | |
| 575 | if (nnodes == 0) { |
| 576 | ERRMSG("No memory nodes\n"); |
| 577 | return 0; |
| 578 | } |
| 579 | |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 580 | return ok; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static int check_chosen(struct node *root) |
| 584 | { |
| 585 | struct node *chosen; |
| 586 | struct property *prop; |
| 587 | int ok = 1; |
| 588 | |
| 589 | chosen = get_subnode(root, "chosen"); |
Scott Wood | b29597d | 2007-03-22 11:11:09 -0500 | [diff] [blame] | 590 | if (!chosen) |
| 591 | return ok; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 592 | |
Stuart Yoder | 5ae78ad | 2007-02-19 11:28:27 -0600 | [diff] [blame] | 593 | /* give warning for obsolete interrupt-controller property */ |
| 594 | do { |
| 595 | if ((prop = get_property(chosen, "interrupt-controller")) != NULL) { |
| 596 | WARNMSG("%s has obsolete \"%s\" property\n", |
| 597 | chosen->fullpath, "interrupt-controller"); |
| 598 | } |
| 599 | } while (0); |
| 600 | |
| 601 | return ok; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static int check_addr_size_reg(struct node *node, |
| 605 | int p_addr_cells, int p_size_cells) |
| 606 | { |
| 607 | int addr_cells = p_addr_cells; |
| 608 | int size_cells = p_size_cells; |
| 609 | struct property *prop; |
| 610 | struct node *child; |
| 611 | int ok = 1; |
| 612 | |
| 613 | node->addr_cells = addr_cells; |
| 614 | node->size_cells = size_cells; |
| 615 | |
| 616 | prop = get_property(node, "reg"); |
| 617 | if (prop) { |
| 618 | int len = prop->val.len / 4; |
| 619 | |
| 620 | if ((len % (addr_cells+size_cells)) != 0) |
| 621 | DO_ERR("\"reg\" property in %s has invalid length (%d) for given #address-cells (%d) and #size-cells (%d)\n", |
| 622 | node->fullpath, prop->val.len, |
| 623 | addr_cells, size_cells); |
| 624 | } |
| 625 | |
| 626 | prop = get_property(node, "#address-cells"); |
| 627 | if (prop) |
| 628 | addr_cells = propval_cell(prop); |
| 629 | |
| 630 | prop = get_property(node, "#size-cells"); |
| 631 | if (prop) |
| 632 | size_cells = propval_cell(prop); |
| 633 | |
| 634 | for_each_child(node, child) { |
| 635 | ok = ok && check_addr_size_reg(child, addr_cells, size_cells); |
| 636 | } |
| 637 | |
| 638 | return ok; |
| 639 | } |
| 640 | |
| 641 | static int check_phandles(struct node *root, struct node *node) |
| 642 | { |
| 643 | struct property *prop; |
| 644 | struct node *child, *other; |
| 645 | cell_t phandle; |
| 646 | int ok = 1; |
| 647 | |
| 648 | prop = get_property(node, "linux,phandle"); |
| 649 | if (prop) { |
| 650 | phandle = propval_cell(prop); |
| 651 | if ((phandle == 0) || (phandle == -1)) { |
| 652 | DO_ERR("%s has invalid linux,phandle %x\n", |
| 653 | node->fullpath, phandle); |
| 654 | } else { |
| 655 | other = get_node_by_phandle(root, phandle); |
| 656 | if (other) |
| 657 | DO_ERR("%s has duplicated phandle %x (seen before at %s)\n", |
| 658 | node->fullpath, phandle, other->fullpath); |
| 659 | |
| 660 | node->phandle = phandle; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | for_each_child(node, child) |
| 665 | ok = ok && check_phandles(root, child); |
| 666 | |
| 667 | return 1; |
| 668 | } |
| 669 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 670 | static cell_t get_node_phandle(struct node *root, struct node *node) |
| 671 | { |
| 672 | static cell_t phandle = 1; /* FIXME: ick, static local */ |
| 673 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 674 | if ((node->phandle != 0) && (node->phandle != -1)) |
| 675 | return node->phandle; |
| 676 | |
| 677 | assert(! get_property(node, "linux,phandle")); |
| 678 | |
| 679 | while (get_node_by_phandle(root, phandle)) |
| 680 | phandle++; |
| 681 | |
| 682 | node->phandle = phandle; |
| 683 | add_property(node, |
| 684 | build_property("linux,phandle", |
| 685 | data_append_cell(empty_data, phandle), |
| 686 | NULL)); |
| 687 | |
| 688 | return node->phandle; |
| 689 | } |
| 690 | |
| 691 | static void apply_fixup(struct node *root, struct property *prop, |
| 692 | struct fixup *f) |
| 693 | { |
| 694 | struct node *refnode; |
| 695 | cell_t phandle; |
| 696 | |
David Gibson | c226ddc | 2007-02-07 14:29:07 +1100 | [diff] [blame] | 697 | if (f->ref[0] == '/') { |
| 698 | /* Reference to full path */ |
| 699 | refnode = get_node_by_path(root, f->ref); |
| 700 | if (! refnode) |
| 701 | die("Reference to non-existent node \"%s\"\n", f->ref); |
| 702 | } else { |
| 703 | refnode = get_node_by_label(root, f->ref); |
| 704 | if (! refnode) |
| 705 | die("Reference to non-existent node label \"%s\"\n", f->ref); |
| 706 | } |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 707 | |
| 708 | phandle = get_node_phandle(root, refnode); |
| 709 | |
| 710 | assert(f->offset + sizeof(cell_t) <= prop->val.len); |
| 711 | |
| 712 | *((cell_t *)(prop->val.val + f->offset)) = cpu_to_be32(phandle); |
| 713 | } |
| 714 | |
| 715 | static void fixup_phandles(struct node *root, struct node *node) |
| 716 | { |
| 717 | struct property *prop; |
| 718 | struct node *child; |
| 719 | |
| 720 | for_each_property(node, prop) { |
| 721 | struct fixup *f = prop->val.refs; |
| 722 | |
| 723 | while (f) { |
| 724 | apply_fixup(root, prop, f); |
| 725 | prop->val.refs = f->next; |
| 726 | fixup_free(f); |
| 727 | f = prop->val.refs; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | for_each_child(node, child) |
| 732 | fixup_phandles(root, child); |
| 733 | } |
| 734 | |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 735 | 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] | 736 | { |
| 737 | int ok = 1; |
| 738 | |
| 739 | if (! check_structure(dt)) |
| 740 | return 0; |
| 741 | |
| 742 | ok = ok && check_addr_size_reg(dt, -1, -1); |
| 743 | ok = ok && check_phandles(dt, dt); |
| 744 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 745 | fixup_phandles(dt, dt); |
| 746 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 747 | if (! ok) |
| 748 | return 0; |
| 749 | |
| 750 | ok = ok && check_root(dt); |
Michael Neuling | 38e8f8f | 2006-05-31 08:31:51 +1000 | [diff] [blame] | 751 | ok = ok && check_cpus(dt, outversion, boot_cpuid_phys); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 752 | ok = ok && check_memory(dt); |
| 753 | ok = ok && check_chosen(dt); |
| 754 | if (! ok) |
| 755 | return 0; |
| 756 | |
| 757 | return 1; |
| 758 | } |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 759 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 760 | struct boot_info *build_boot_info(struct reserve_info *reservelist, |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 761 | struct node *tree) |
| 762 | { |
| 763 | struct boot_info *bi; |
| 764 | |
| 765 | bi = xmalloc(sizeof(*bi)); |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 766 | bi->reservelist = reservelist; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 767 | bi->dt = tree; |
| 768 | |
| 769 | return bi; |
| 770 | } |