Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * OF flat tree builder |
| 3 | */ |
| 4 | |
| 5 | #include <common.h> |
| 6 | #include <malloc.h> |
| 7 | #include <environment.h> |
| 8 | |
Marian Balakowicz | ff2dcf5 | 2005-10-28 18:08:03 +0200 | [diff] [blame] | 9 | #ifdef CONFIG_OF_FLAT_TREE |
| 10 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 11 | #include <asm/errno.h> |
| 12 | #include <stddef.h> |
| 13 | |
| 14 | #include <ft_build.h> |
| 15 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 16 | /* align addr on a size boundary - adjust address up if needed -- Cort */ |
| 17 | #define _ALIGN(addr,size) (((addr)+(size)-1)&(~((size)-1))) |
| 18 | |
| 19 | static void ft_put_word(struct ft_cxt *cxt, u32 v) |
| 20 | { |
| 21 | if (cxt->overflow) /* do nothing */ |
| 22 | return; |
| 23 | |
| 24 | /* check for overflow */ |
| 25 | if (cxt->p + 4 > cxt->pstr) { |
| 26 | cxt->overflow = 1; |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | *(u32 *) cxt->p = cpu_to_be32(v); |
| 31 | cxt->p += 4; |
| 32 | } |
| 33 | |
| 34 | static inline void ft_put_bin(struct ft_cxt *cxt, const void *data, int sz) |
| 35 | { |
| 36 | u8 *p; |
| 37 | |
| 38 | if (cxt->overflow) /* do nothing */ |
| 39 | return; |
| 40 | |
| 41 | /* next pointer pos */ |
| 42 | p = (u8 *) _ALIGN((unsigned long)cxt->p + sz, 4); |
| 43 | |
| 44 | /* check for overflow */ |
| 45 | if (p > cxt->pstr) { |
| 46 | cxt->overflow = 1; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | memcpy(cxt->p, data, sz); |
| 51 | if ((sz & 3) != 0) |
| 52 | memset(cxt->p + sz, 0, 4 - (sz & 3)); |
| 53 | cxt->p = p; |
| 54 | } |
| 55 | |
| 56 | void ft_begin_node(struct ft_cxt *cxt, const char *name) |
| 57 | { |
| 58 | ft_put_word(cxt, OF_DT_BEGIN_NODE); |
| 59 | ft_put_bin(cxt, name, strlen(name) + 1); |
| 60 | } |
| 61 | |
| 62 | void ft_end_node(struct ft_cxt *cxt) |
| 63 | { |
| 64 | ft_put_word(cxt, OF_DT_END_NODE); |
| 65 | } |
| 66 | |
| 67 | void ft_nop(struct ft_cxt *cxt) |
| 68 | { |
| 69 | ft_put_word(cxt, OF_DT_NOP); |
| 70 | } |
| 71 | |
| 72 | static int lookup_string(struct ft_cxt *cxt, const char *name) |
| 73 | { |
| 74 | u8 *p; |
| 75 | |
| 76 | p = cxt->pstr; |
| 77 | while (p < cxt->pstr_begin) { |
| 78 | if (strcmp(p, name) == 0) |
| 79 | return p - cxt->p_begin; |
| 80 | p += strlen(p) + 1; |
| 81 | } |
| 82 | |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | void ft_prop(struct ft_cxt *cxt, const char *name, const void *data, int sz) |
| 87 | { |
| 88 | int len, off; |
| 89 | |
| 90 | if (cxt->overflow) |
| 91 | return; |
| 92 | |
| 93 | len = strlen(name) + 1; |
| 94 | |
| 95 | off = lookup_string(cxt, name); |
| 96 | if (off == -1) { |
| 97 | /* check if we have space */ |
| 98 | if (cxt->p + 12 + sz + len > cxt->pstr) { |
| 99 | cxt->overflow = 1; |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | cxt->pstr -= len; |
| 104 | memcpy(cxt->pstr, name, len); |
| 105 | off = cxt->pstr - cxt->p_begin; |
| 106 | } |
| 107 | |
| 108 | /* now put offset from beginning of *STRUCTURE* */ |
| 109 | /* will be fixed up at the end */ |
| 110 | ft_put_word(cxt, OF_DT_PROP); |
| 111 | ft_put_word(cxt, sz); |
| 112 | ft_put_word(cxt, off); |
| 113 | ft_put_bin(cxt, data, sz); |
| 114 | } |
| 115 | |
| 116 | void ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str) |
| 117 | { |
| 118 | ft_prop(cxt, name, str, strlen(str) + 1); |
| 119 | } |
| 120 | |
| 121 | void ft_prop_int(struct ft_cxt *cxt, const char *name, int val) |
| 122 | { |
| 123 | u32 v = cpu_to_be32((u32) val); |
| 124 | |
| 125 | ft_prop(cxt, name, &v, 4); |
| 126 | } |
| 127 | |
| 128 | /* start construction of the flat OF tree */ |
| 129 | void ft_begin(struct ft_cxt *cxt, void *blob, int max_size) |
| 130 | { |
| 131 | struct boot_param_header *bph = blob; |
| 132 | u32 off; |
| 133 | |
| 134 | /* clear the cxt */ |
| 135 | memset(cxt, 0, sizeof(*cxt)); |
| 136 | |
| 137 | cxt->bph = bph; |
| 138 | cxt->max_size = max_size; |
| 139 | |
| 140 | /* zero everything in the header area */ |
| 141 | memset(bph, 0, sizeof(*bph)); |
| 142 | |
| 143 | bph->magic = cpu_to_be32(OF_DT_HEADER); |
| 144 | bph->version = cpu_to_be32(0x10); |
| 145 | bph->last_comp_version = cpu_to_be32(0x10); |
| 146 | |
| 147 | /* start pointers */ |
| 148 | cxt->pres_begin = (u8 *) _ALIGN((unsigned long)(bph + 1), 8); |
| 149 | cxt->pres = cxt->pres_begin; |
| 150 | |
| 151 | off = (unsigned long)cxt->pres_begin - (unsigned long)bph; |
| 152 | bph->off_mem_rsvmap = cpu_to_be32(off); |
| 153 | |
| 154 | ((u64 *) cxt->pres)[0] = 0; /* phys = 0, size = 0, terminate */ |
| 155 | ((u64 *) cxt->pres)[1] = 0; |
| 156 | |
| 157 | cxt->p_anchor = cxt->pres + 16; /* over the terminator */ |
| 158 | } |
| 159 | |
| 160 | /* add a reserver physical area to the rsvmap */ |
| 161 | void ft_add_rsvmap(struct ft_cxt *cxt, u64 physaddr, u64 size) |
| 162 | { |
| 163 | ((u64 *) cxt->pres)[0] = cpu_to_be64(physaddr); /* phys = 0, size = 0, terminate */ |
| 164 | ((u64 *) cxt->pres)[1] = cpu_to_be64(size); |
| 165 | |
Kumar Gala | a8b76de | 2006-01-11 16:41:35 -0600 | [diff] [blame] | 166 | cxt->pres += 16; /* advance */ |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 167 | |
| 168 | ((u64 *) cxt->pres)[0] = 0; /* phys = 0, size = 0, terminate */ |
| 169 | ((u64 *) cxt->pres)[1] = 0; |
| 170 | |
| 171 | /* keep track of size */ |
| 172 | cxt->res_size = cxt->pres + 16 - cxt->pres_begin; |
| 173 | |
| 174 | cxt->p_anchor = cxt->pres + 16; /* over the terminator */ |
| 175 | } |
| 176 | |
| 177 | void ft_begin_tree(struct ft_cxt *cxt) |
| 178 | { |
| 179 | cxt->p_begin = cxt->p_anchor; |
| 180 | cxt->pstr_begin = (char *)cxt->bph + cxt->max_size; /* point at the end */ |
| 181 | |
| 182 | cxt->p = cxt->p_begin; |
| 183 | cxt->pstr = cxt->pstr_begin; |
| 184 | } |
| 185 | |
| 186 | int ft_end_tree(struct ft_cxt *cxt) |
| 187 | { |
| 188 | struct boot_param_header *bph = cxt->bph; |
| 189 | int off, sz, sz1; |
| 190 | u32 tag, v; |
| 191 | u8 *p; |
| 192 | |
| 193 | ft_put_word(cxt, OF_DT_END); |
| 194 | |
| 195 | if (cxt->overflow) |
| 196 | return -ENOMEM; |
| 197 | |
| 198 | /* size of the areas */ |
| 199 | cxt->struct_size = cxt->p - cxt->p_begin; |
| 200 | cxt->strings_size = cxt->pstr_begin - cxt->pstr; |
| 201 | |
| 202 | /* the offset we must move */ |
| 203 | off = (cxt->pstr_begin - cxt->p_begin) - cxt->strings_size; |
| 204 | |
| 205 | /* the new strings start */ |
| 206 | cxt->pstr_begin = cxt->p_begin + cxt->struct_size; |
| 207 | |
| 208 | /* move the whole string area */ |
| 209 | memmove(cxt->pstr_begin, cxt->pstr, cxt->strings_size); |
| 210 | |
| 211 | /* now perform the fixup of the strings */ |
| 212 | p = cxt->p_begin; |
| 213 | while ((tag = be32_to_cpu(*(u32 *) p)) != OF_DT_END) { |
| 214 | p += 4; |
| 215 | |
| 216 | if (tag == OF_DT_BEGIN_NODE) { |
| 217 | p = (u8 *) _ALIGN((unsigned long)p + strlen(p) + 1, 4); |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | if (tag == OF_DT_END_NODE || tag == OF_DT_NOP) |
| 222 | continue; |
| 223 | |
| 224 | if (tag != OF_DT_PROP) |
| 225 | return -EINVAL; |
| 226 | |
| 227 | sz = be32_to_cpu(*(u32 *) p); |
| 228 | p += 4; |
| 229 | |
| 230 | v = be32_to_cpu(*(u32 *) p); |
| 231 | v -= off; |
| 232 | *(u32 *) p = cpu_to_be32(v); /* move down */ |
| 233 | p += 4; |
| 234 | |
| 235 | p = (u8 *) _ALIGN((unsigned long)p + sz, 4); |
| 236 | } |
| 237 | |
| 238 | /* fix sizes */ |
| 239 | p = (char *)cxt->bph; |
| 240 | sz = (cxt->pstr_begin + cxt->strings_size) - p; |
| 241 | sz1 = _ALIGN(sz, 16); /* align at 16 bytes */ |
| 242 | if (sz != sz1) |
| 243 | memset(p + sz, 0, sz1 - sz); |
| 244 | bph->totalsize = cpu_to_be32(sz1); |
| 245 | bph->off_dt_struct = cpu_to_be32(cxt->p_begin - p); |
| 246 | bph->off_dt_strings = cpu_to_be32(cxt->pstr_begin - p); |
| 247 | |
| 248 | /* the new strings start */ |
| 249 | cxt->pstr_begin = cxt->p_begin + cxt->struct_size; |
| 250 | cxt->pstr = cxt->pstr_begin + cxt->strings_size; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /**********************************************************************/ |
| 256 | |
| 257 | static inline int isprint(int c) |
| 258 | { |
| 259 | return c >= 0x20 && c <= 0x7e; |
| 260 | } |
| 261 | |
| 262 | static int is_printable_string(const void *data, int len) |
| 263 | { |
| 264 | const char *s = data; |
| 265 | const char *ss; |
| 266 | |
| 267 | /* zero length is not */ |
| 268 | if (len == 0) |
| 269 | return 0; |
| 270 | |
| 271 | /* must terminate with zero */ |
| 272 | if (s[len - 1] != '\0') |
| 273 | return 0; |
| 274 | |
| 275 | ss = s; |
| 276 | while (*s && isprint(*s)) |
| 277 | s++; |
| 278 | |
| 279 | /* not zero, or not done yet */ |
| 280 | if (*s != '\0' || (s + 1 - ss) < len) |
| 281 | return 0; |
| 282 | |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | static void print_data(const void *data, int len) |
| 287 | { |
| 288 | int i; |
| 289 | const u8 *s; |
| 290 | |
| 291 | /* no data, don't print */ |
| 292 | if (len == 0) |
| 293 | return; |
| 294 | |
| 295 | if (is_printable_string(data, len)) { |
| 296 | printf(" = \"%s\"", (char *)data); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | switch (len) { |
| 301 | case 1: /* byte */ |
| 302 | printf(" = <0x%02x>", (*(u8 *) data) & 0xff); |
| 303 | break; |
| 304 | case 2: /* half-word */ |
| 305 | printf(" = <0x%04x>", be16_to_cpu(*(u16 *) data) & 0xffff); |
| 306 | break; |
| 307 | case 4: /* word */ |
| 308 | printf(" = <0x%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU); |
| 309 | break; |
| 310 | case 8: /* double-word */ |
| 311 | printf(" = <0x%16llx>", be64_to_cpu(*(uint64_t *) data)); |
| 312 | break; |
| 313 | default: /* anything else... hexdump */ |
| 314 | printf(" = ["); |
| 315 | for (i = 0, s = data; i < len; i++) |
| 316 | printf("%02x%s", s[i], i < len - 1 ? " " : ""); |
| 317 | printf("]"); |
| 318 | |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void ft_dump_blob(const void *bphp) |
| 324 | { |
| 325 | const struct boot_param_header *bph = bphp; |
| 326 | const uint64_t *p_rsvmap = (const uint64_t *) |
| 327 | ((const char *)bph + be32_to_cpu(bph->off_mem_rsvmap)); |
| 328 | const u32 *p_struct = (const u32 *) |
| 329 | ((const char *)bph + be32_to_cpu(bph->off_dt_struct)); |
| 330 | const u32 *p_strings = (const u32 *) |
| 331 | ((const char *)bph + be32_to_cpu(bph->off_dt_strings)); |
| 332 | u32 tag; |
| 333 | const u32 *p; |
| 334 | const char *s, *t; |
| 335 | int depth, sz, shift; |
| 336 | int i; |
| 337 | uint64_t addr, size; |
| 338 | |
| 339 | if (be32_to_cpu(bph->magic) != OF_DT_HEADER) { |
| 340 | /* not valid tree */ |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | depth = 0; |
| 345 | shift = 4; |
| 346 | |
| 347 | for (i = 0;; i++) { |
| 348 | addr = be64_to_cpu(p_rsvmap[i * 2]); |
| 349 | size = be64_to_cpu(p_rsvmap[i * 2 + 1]); |
| 350 | if (addr == 0 && size == 0) |
| 351 | break; |
| 352 | |
| 353 | printf("/memreserve/ 0x%llx 0x%llx;\n", addr, size); |
| 354 | } |
| 355 | |
| 356 | p = p_struct; |
| 357 | while ((tag = be32_to_cpu(*p++)) != OF_DT_END) { |
| 358 | |
| 359 | /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */ |
| 360 | |
| 361 | if (tag == OF_DT_BEGIN_NODE) { |
| 362 | s = (const char *)p; |
| 363 | p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4); |
| 364 | |
| 365 | printf("%*s%s {\n", depth * shift, "", s); |
| 366 | |
| 367 | depth++; |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | if (tag == OF_DT_END_NODE) { |
| 372 | depth--; |
| 373 | |
| 374 | printf("%*s};\n", depth * shift, ""); |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | if (tag == OF_DT_NOP) { |
| 379 | printf("%*s[NOP]\n", depth * shift, ""); |
| 380 | continue; |
| 381 | } |
| 382 | |
| 383 | if (tag != OF_DT_PROP) { |
| 384 | fprintf(stderr, "%*s ** Unknown tag 0x%08x\n", |
| 385 | depth * shift, "", tag); |
| 386 | break; |
| 387 | } |
| 388 | sz = be32_to_cpu(*p++); |
| 389 | s = (const char *)p_strings + be32_to_cpu(*p++); |
| 390 | t = (const char *)p; |
| 391 | p = (const u32 *)_ALIGN((unsigned long)p + sz, 4); |
| 392 | printf("%*s%s", depth * shift, "", s); |
| 393 | print_data(t, sz); |
| 394 | printf(";\n"); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | void ft_backtrack_node(struct ft_cxt *cxt) |
| 399 | { |
| 400 | if (be32_to_cpu(*(u32 *) (cxt->p - 4)) != OF_DT_END_NODE) |
| 401 | return; /* XXX only for node */ |
| 402 | |
| 403 | cxt->p -= 4; |
| 404 | } |
| 405 | |
| 406 | /* note that the root node of the blob is "peeled" off */ |
| 407 | void ft_merge_blob(struct ft_cxt *cxt, void *blob) |
| 408 | { |
| 409 | struct boot_param_header *bph = (struct boot_param_header *)blob; |
| 410 | u32 *p_struct = (u32 *) ((char *)bph + be32_to_cpu(bph->off_dt_struct)); |
| 411 | u32 *p_strings = |
| 412 | (u32 *) ((char *)bph + be32_to_cpu(bph->off_dt_strings)); |
| 413 | u32 tag, *p; |
| 414 | char *s, *t; |
| 415 | int depth, sz; |
| 416 | |
| 417 | if (be32_to_cpu(*(u32 *) (cxt->p - 4)) != OF_DT_END_NODE) |
| 418 | return; /* XXX only for node */ |
| 419 | |
| 420 | cxt->p -= 4; |
| 421 | |
| 422 | depth = 0; |
| 423 | p = p_struct; |
| 424 | while ((tag = be32_to_cpu(*p++)) != OF_DT_END) { |
| 425 | |
| 426 | /* printf("tag: 0x%08x (%d) - %d\n", tag, p - p_struct, depth); */ |
| 427 | |
| 428 | if (tag == OF_DT_BEGIN_NODE) { |
| 429 | s = (char *)p; |
| 430 | p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4); |
| 431 | |
| 432 | if (depth++ > 0) |
| 433 | ft_begin_node(cxt, s); |
| 434 | |
| 435 | continue; |
| 436 | } |
| 437 | |
| 438 | if (tag == OF_DT_END_NODE) { |
| 439 | ft_end_node(cxt); |
| 440 | if (--depth == 0) |
| 441 | break; |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | if (tag == OF_DT_NOP) |
| 446 | continue; |
| 447 | |
| 448 | if (tag != OF_DT_PROP) |
| 449 | break; |
| 450 | |
| 451 | sz = be32_to_cpu(*p++); |
| 452 | s = (char *)p_strings + be32_to_cpu(*p++); |
| 453 | t = (char *)p; |
| 454 | p = (u32 *) _ALIGN((unsigned long)p + sz, 4); |
| 455 | |
| 456 | ft_prop(cxt, s, t, sz); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void *ft_get_prop(void *bphp, const char *propname, int *szp) |
| 461 | { |
| 462 | struct boot_param_header *bph = bphp; |
| 463 | uint32_t *p_struct = |
| 464 | (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_struct)); |
| 465 | uint32_t *p_strings = |
| 466 | (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_strings)); |
| 467 | uint32_t version = be32_to_cpu(bph->version); |
| 468 | uint32_t tag; |
| 469 | uint32_t *p; |
| 470 | char *s, *t; |
| 471 | char *ss; |
| 472 | int sz; |
| 473 | static char path[256], prop[256]; |
| 474 | |
| 475 | path[0] = '\0'; |
| 476 | |
| 477 | p = p_struct; |
| 478 | while ((tag = be32_to_cpu(*p++)) != OF_DT_END) { |
| 479 | |
| 480 | if (tag == OF_DT_BEGIN_NODE) { |
| 481 | s = (char *)p; |
| 482 | p = (uint32_t *) _ALIGN((unsigned long)p + strlen(s) + |
| 483 | 1, 4); |
| 484 | strcat(path, s); |
| 485 | strcat(path, "/"); |
| 486 | continue; |
| 487 | } |
| 488 | |
| 489 | if (tag == OF_DT_END_NODE) { |
| 490 | path[strlen(path) - 1] = '\0'; |
| 491 | ss = strrchr(path, '/'); |
| 492 | if (ss != NULL) |
| 493 | ss[1] = '\0'; |
| 494 | continue; |
| 495 | } |
| 496 | |
| 497 | if (tag == OF_DT_NOP) |
| 498 | continue; |
| 499 | |
| 500 | if (tag != OF_DT_PROP) |
| 501 | break; |
| 502 | |
| 503 | sz = be32_to_cpu(*p++); |
| 504 | s = (char *)p_strings + be32_to_cpu(*p++); |
| 505 | if (version < 0x10 && sz >= 8) |
| 506 | p = (uint32_t *) _ALIGN((unsigned long)p, 8); |
| 507 | t = (char *)p; |
| 508 | p = (uint32_t *) _ALIGN((unsigned long)p + sz, 4); |
| 509 | |
| 510 | strcpy(prop, path); |
| 511 | strcat(prop, s); |
| 512 | |
| 513 | if (strcmp(prop, propname) == 0) { |
| 514 | *szp = sz; |
| 515 | return t; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | return NULL; |
| 520 | } |
| 521 | |
| 522 | /********************************************************************/ |
| 523 | |
| 524 | extern unsigned char oftree_dtb[]; |
| 525 | extern unsigned int oftree_dtb_len; |
| 526 | |
| 527 | /* Function that returns a character from the environment */ |
| 528 | extern uchar(*env_get_char) (int); |
| 529 | |
| 530 | #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) } |
| 531 | |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 532 | #ifdef CONFIG_OF_HAS_BD_T |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 533 | static const struct { |
| 534 | const char *name; |
| 535 | int offset; |
| 536 | } bd_map[] = { |
| 537 | BDM(memstart), |
| 538 | BDM(memsize), |
| 539 | BDM(flashstart), |
| 540 | BDM(flashsize), |
| 541 | BDM(flashoffset), |
| 542 | BDM(sramstart), |
| 543 | BDM(sramsize), |
| 544 | #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \ |
| 545 | || defined(CONFIG_E500) |
| 546 | BDM(immr_base), |
| 547 | #endif |
| 548 | #if defined(CONFIG_MPC5xxx) |
| 549 | BDM(mbar_base), |
| 550 | #endif |
| 551 | #if defined(CONFIG_MPC83XX) |
| 552 | BDM(immrbar), |
| 553 | #endif |
| 554 | #if defined(CONFIG_MPC8220) |
| 555 | BDM(mbar_base), |
| 556 | BDM(inpfreq), |
| 557 | BDM(pcifreq), |
| 558 | BDM(pevfreq), |
| 559 | BDM(flbfreq), |
| 560 | BDM(vcofreq), |
| 561 | #endif |
| 562 | BDM(bootflags), |
| 563 | BDM(ip_addr), |
| 564 | BDM(intfreq), |
| 565 | BDM(busfreq), |
| 566 | #ifdef CONFIG_CPM2 |
| 567 | BDM(cpmfreq), |
| 568 | BDM(brgfreq), |
| 569 | BDM(sccfreq), |
| 570 | BDM(vco), |
| 571 | #endif |
| 572 | #if defined(CONFIG_MPC5xxx) |
| 573 | BDM(ipbfreq), |
| 574 | BDM(pcifreq), |
| 575 | #endif |
| 576 | BDM(baudrate), |
| 577 | }; |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 578 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 579 | |
Kumar Gala | a8b76de | 2006-01-11 16:41:35 -0600 | [diff] [blame] | 580 | void ft_setup(void *blob, int size, bd_t * bd, ulong initrd_start, ulong initrd_end) |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 581 | { |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 582 | u32 *p; |
| 583 | int len; |
| 584 | struct ft_cxt cxt; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 585 | ulong clock; |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 586 | #if defined(CONFIG_OF_HAS_UBOOT_ENV) |
| 587 | int k, nxt; |
| 588 | #endif |
| 589 | #if defined(CONFIG_OF_HAS_BD_T) |
| 590 | u8 *end; |
| 591 | #endif |
| 592 | #if defined(CONFIG_OF_HAS_UBOOT_ENV) || defined(CONFIG_OF_HAS_BD_T) |
| 593 | int i; |
| 594 | static char tmpenv[256]; |
| 595 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 596 | |
| 597 | /* disable OF tree; booting old kernel */ |
| 598 | if (getenv("disable_of") != NULL) { |
| 599 | memcpy(blob, bd, sizeof(*bd)); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | ft_begin(&cxt, blob, size); |
| 604 | |
Kumar Gala | a8b76de | 2006-01-11 16:41:35 -0600 | [diff] [blame] | 605 | if (initrd_start && initrd_end) |
| 606 | ft_add_rsvmap(&cxt, initrd_start, initrd_end - initrd_start + 1); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 607 | |
| 608 | ft_begin_tree(&cxt); |
| 609 | |
| 610 | ft_begin_node(&cxt, ""); |
| 611 | |
| 612 | ft_end_node(&cxt); |
| 613 | |
| 614 | /* copy RO tree */ |
| 615 | ft_merge_blob(&cxt, oftree_dtb); |
| 616 | |
| 617 | /* back into root */ |
| 618 | ft_backtrack_node(&cxt); |
| 619 | |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 620 | #ifdef CONFIG_OF_HAS_UBOOT_ENV |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 621 | ft_begin_node(&cxt, "u-boot-env"); |
| 622 | |
| 623 | for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 624 | char *s, *lval, *rval; |
| 625 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 626 | for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) ; |
| 627 | s = tmpenv; |
| 628 | for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k) |
| 629 | *s++ = env_get_char(k); |
| 630 | *s++ = '\0'; |
| 631 | lval = tmpenv; |
| 632 | s = strchr(tmpenv, '='); |
| 633 | if (s != NULL) { |
| 634 | *s++ = '\0'; |
| 635 | rval = s; |
| 636 | } else |
| 637 | continue; |
| 638 | ft_prop_str(&cxt, lval, rval); |
| 639 | } |
| 640 | |
| 641 | ft_end_node(&cxt); |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 642 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 643 | |
| 644 | ft_begin_node(&cxt, "chosen"); |
| 645 | |
| 646 | ft_prop_str(&cxt, "name", "chosen"); |
| 647 | ft_prop_str(&cxt, "bootargs", getenv("bootargs")); |
| 648 | ft_prop_int(&cxt, "linux,platform", 0x600); /* what is this? */ |
Kumar Gala | a8b76de | 2006-01-11 16:41:35 -0600 | [diff] [blame] | 649 | if (initrd_start && initrd_end) { |
| 650 | ft_prop_int(&cxt, "linux,initrd-start", initrd_start); |
| 651 | ft_prop_int(&cxt, "linux,initrd-end", initrd_end); |
| 652 | } |
Kumar Gala | e40c2b5 | 2006-01-11 13:59:02 -0600 | [diff] [blame] | 653 | #ifdef OF_STDOUT_PATH |
| 654 | ft_prop_str(&cxt, "linux,stdout-path", OF_STDOUT_PATH); |
| 655 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 656 | |
| 657 | ft_end_node(&cxt); |
| 658 | |
| 659 | ft_end_node(&cxt); /* end root */ |
| 660 | |
| 661 | ft_end_tree(&cxt); |
| 662 | |
| 663 | /* |
| 664 | printf("merged OF-tree\n"); |
| 665 | ft_dump_blob(blob); |
| 666 | */ |
| 667 | |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 668 | #ifdef CONFIG_OF_HAS_BD_T |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 669 | /* paste the bd_t at the end of the flat tree */ |
| 670 | end = (char *)blob + |
| 671 | be32_to_cpu(((struct boot_param_header *)blob)->totalsize); |
| 672 | memcpy(end, bd, sizeof(*bd)); |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 673 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 674 | |
| 675 | #ifdef CONFIG_PPC |
| 676 | |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 677 | #ifdef CONFIG_OF_HAS_BD_T |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 678 | for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) { |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 679 | uint32_t v; |
| 680 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 681 | sprintf(tmpenv, "/bd_t/%s", bd_map[i].name); |
| 682 | v = *(uint32_t *)((char *)bd + bd_map[i].offset); |
| 683 | |
| 684 | p = ft_get_prop(blob, tmpenv, &len); |
| 685 | if (p != NULL) |
| 686 | *p = cpu_to_be32(v); |
| 687 | } |
| 688 | |
| 689 | p = ft_get_prop(blob, "/bd_t/enetaddr", &len); |
| 690 | if (p != NULL) |
| 691 | memcpy(p, bd->bi_enetaddr, 6); |
| 692 | |
| 693 | p = ft_get_prop(blob, "/bd_t/ethspeed", &len); |
| 694 | if (p != NULL) |
| 695 | *p = cpu_to_be32((uint32_t) bd->bi_ethspeed); |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 696 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 697 | |
| 698 | clock = bd->bi_intfreq; |
| 699 | p = ft_get_prop(blob, "/cpus/" OF_CPU "/clock-frequency", &len); |
| 700 | if (p != NULL) |
| 701 | *p = cpu_to_be32(clock); |
| 702 | |
| 703 | #ifdef OF_TBCLK |
| 704 | clock = OF_TBCLK; |
| 705 | p = ft_get_prop(blob, "/cpus/" OF_CPU "/timebase-frequency", &len); |
| 706 | if (p != NULL) |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 707 | *p = cpu_to_be32(clock); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 708 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 709 | #endif /* __powerpc__ */ |
| 710 | |
Kumar Gala | 1e26aa5 | 2006-01-11 13:54:17 -0600 | [diff] [blame] | 711 | #ifdef CONFIG_OF_BOARD_SETUP |
| 712 | ft_board_setup(blob, bd); |
| 713 | #endif |
| 714 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 715 | /* |
| 716 | printf("final OF-tree\n"); |
| 717 | ft_dump_blob(blob); |
| 718 | */ |
| 719 | |
| 720 | } |
| 721 | |
| 722 | #endif |