blob: 65a274f84087eea80f94d76b4f9987a081845617 [file] [log] [blame]
Wolfgang Denk814d98f2005-10-13 01:59:29 +02001/*
2 * OF flat tree builder
3 */
4
5#include <common.h>
6#include <malloc.h>
7#include <environment.h>
8
Marian Balakowiczfe934832005-10-28 18:08:03 +02009#ifdef CONFIG_OF_FLAT_TREE
10
Wolfgang Denk814d98f2005-10-13 01:59:29 +020011#include <asm/errno.h>
12#include <stddef.h>
13
14#include <ft_build.h>
15
Wolfgang Denk814d98f2005-10-13 01:59:29 +020016/* align addr on a size boundary - adjust address up if needed -- Cort */
17#define _ALIGN(addr,size) (((addr)+(size)-1)&(~((size)-1)))
18
19static 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
34static 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
56void 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
62void ft_end_node(struct ft_cxt *cxt)
63{
64 ft_put_word(cxt, OF_DT_END_NODE);
65}
66
67void ft_nop(struct ft_cxt *cxt)
68{
69 ft_put_word(cxt, OF_DT_NOP);
70}
71
72static 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
86void 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
116void 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
121void 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 */
129void 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 */
161void 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
166 cxt->pres += 18; /* advance */
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
177void 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
186int 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
257static inline int isprint(int c)
258{
259 return c >= 0x20 && c <= 0x7e;
260}
261
262static 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
286static 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
323void 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
398void 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 */
407void 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
460void *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
524extern unsigned char oftree_dtb[];
525extern unsigned int oftree_dtb_len;
526
527/* Function that returns a character from the environment */
528extern uchar(*env_get_char) (int);
529
530#define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
531
532static const struct {
533 const char *name;
534 int offset;
535} bd_map[] = {
536 BDM(memstart),
537 BDM(memsize),
538 BDM(flashstart),
539 BDM(flashsize),
540 BDM(flashoffset),
541 BDM(sramstart),
542 BDM(sramsize),
543#if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
544 || defined(CONFIG_E500)
545 BDM(immr_base),
546#endif
547#if defined(CONFIG_MPC5xxx)
548 BDM(mbar_base),
549#endif
550#if defined(CONFIG_MPC83XX)
551 BDM(immrbar),
552#endif
553#if defined(CONFIG_MPC8220)
554 BDM(mbar_base),
555 BDM(inpfreq),
556 BDM(pcifreq),
557 BDM(pevfreq),
558 BDM(flbfreq),
559 BDM(vcofreq),
560#endif
561 BDM(bootflags),
562 BDM(ip_addr),
563 BDM(intfreq),
564 BDM(busfreq),
565#ifdef CONFIG_CPM2
566 BDM(cpmfreq),
567 BDM(brgfreq),
568 BDM(sccfreq),
569 BDM(vco),
570#endif
571#if defined(CONFIG_MPC5xxx)
572 BDM(ipbfreq),
573 BDM(pcifreq),
574#endif
575 BDM(baudrate),
576};
577
578void ft_setup(void *blob, int size, bd_t * bd)
579{
580 DECLARE_GLOBAL_DATA_PTR;
581 u8 *end;
582 u32 *p;
583 int len;
584 struct ft_cxt cxt;
585 int i, k, nxt;
586 static char tmpenv[256];
587 char *s, *lval, *rval;
588 ulong clock;
589 uint32_t v;
590
591 /* disable OF tree; booting old kernel */
592 if (getenv("disable_of") != NULL) {
593 memcpy(blob, bd, sizeof(*bd));
594 return;
595 }
596
597 ft_begin(&cxt, blob, size);
598
599 /* fs_add_rsvmap not used */
600
601 ft_begin_tree(&cxt);
602
603 ft_begin_node(&cxt, "");
604
605 ft_end_node(&cxt);
606
607 /* copy RO tree */
608 ft_merge_blob(&cxt, oftree_dtb);
609
610 /* back into root */
611 ft_backtrack_node(&cxt);
612
613 ft_begin_node(&cxt, "u-boot-env");
614
615 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
616 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) ;
617 s = tmpenv;
618 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
619 *s++ = env_get_char(k);
620 *s++ = '\0';
621 lval = tmpenv;
622 s = strchr(tmpenv, '=');
623 if (s != NULL) {
624 *s++ = '\0';
625 rval = s;
626 } else
627 continue;
628 ft_prop_str(&cxt, lval, rval);
629 }
630
631 ft_end_node(&cxt);
632
633 ft_begin_node(&cxt, "chosen");
634
635 ft_prop_str(&cxt, "name", "chosen");
636 ft_prop_str(&cxt, "bootargs", getenv("bootargs"));
637 ft_prop_int(&cxt, "linux,platform", 0x600); /* what is this? */
638
639 ft_end_node(&cxt);
640
641 ft_end_node(&cxt); /* end root */
642
643 ft_end_tree(&cxt);
644
645 /*
646 printf("merged OF-tree\n");
647 ft_dump_blob(blob);
648 */
649
650 /* paste the bd_t at the end of the flat tree */
651 end = (char *)blob +
652 be32_to_cpu(((struct boot_param_header *)blob)->totalsize);
653 memcpy(end, bd, sizeof(*bd));
654
655#ifdef CONFIG_PPC
656
657 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
658 sprintf(tmpenv, "/bd_t/%s", bd_map[i].name);
659 v = *(uint32_t *)((char *)bd + bd_map[i].offset);
660
661 p = ft_get_prop(blob, tmpenv, &len);
662 if (p != NULL)
663 *p = cpu_to_be32(v);
664 }
665
666 p = ft_get_prop(blob, "/bd_t/enetaddr", &len);
667 if (p != NULL)
668 memcpy(p, bd->bi_enetaddr, 6);
669
670 p = ft_get_prop(blob, "/bd_t/ethspeed", &len);
671 if (p != NULL)
672 *p = cpu_to_be32((uint32_t) bd->bi_ethspeed);
673
674 clock = bd->bi_intfreq;
675 p = ft_get_prop(blob, "/cpus/" OF_CPU "/clock-frequency", &len);
676 if (p != NULL)
677 *p = cpu_to_be32(clock);
678
679#ifdef OF_TBCLK
680 clock = OF_TBCLK;
681 p = ft_get_prop(blob, "/cpus/" OF_CPU "/timebase-frequency", &len);
682 if (p != NULL)
683 *p = cpu_to_be32(OF_TBCLK);
684#endif
685
686#endif /* __powerpc__ */
687
688 /*
689 printf("final OF-tree\n");
690 ft_dump_blob(blob);
691 */
692
693}
694
695#endif