Merge with /home/tur/proj/051_uboot_linux_v38b/u-boot
[platform/kernel/u-boot.git] / common / ft_build.c
1 /*
2  * OF flat tree builder
3  */
4
5 #include <common.h>
6 #include <malloc.h>
7 #include <environment.h>
8
9 #ifdef CONFIG_OF_FLAT_TREE
10
11 #include <asm/errno.h>
12 #include <stddef.h>
13
14 #include <ft_build.h>
15
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
166         cxt->pres += 16;        /* 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
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                 puts(" = \"");
297                 puts(data);
298                 puts("\"");
299                 return;
300         }
301
302         switch (len) {
303         case 1:         /* byte */
304                 printf(" = <0x%02x>", (*(u8 *) data) & 0xff);
305                 break;
306         case 2:         /* half-word */
307                 printf(" = <0x%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
308                 break;
309         case 4:         /* word */
310                 printf(" = <0x%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
311                 break;
312         case 8:         /* double-word */
313                 printf(" = <0x%16llx>", be64_to_cpu(*(uint64_t *) data));
314                 break;
315         default:                /* anything else... hexdump */
316                 printf(" = [");
317                 for (i = 0, s = data; i < len; i++)
318                         printf("%02x%s", s[i], i < len - 1 ? " " : "");
319                 printf("]");
320
321                 break;
322         }
323 }
324
325 void ft_dump_blob(const void *bphp)
326 {
327         const struct boot_param_header *bph = bphp;
328         const uint64_t *p_rsvmap = (const uint64_t *)
329                 ((const char *)bph + be32_to_cpu(bph->off_mem_rsvmap));
330         const u32 *p_struct = (const u32 *)
331                 ((const char *)bph + be32_to_cpu(bph->off_dt_struct));
332         const u32 *p_strings = (const u32 *)
333                 ((const char *)bph + be32_to_cpu(bph->off_dt_strings));
334         u32 tag;
335         const u32 *p;
336         const char *s, *t;
337         int depth, sz, shift;
338         int i;
339         uint64_t addr, size;
340
341         if (be32_to_cpu(bph->magic) != OF_DT_HEADER) {
342                 /* not valid tree */
343                 return;
344         }
345
346         depth = 0;
347         shift = 4;
348
349         for (i = 0;; i++) {
350                 addr = be64_to_cpu(p_rsvmap[i * 2]);
351                 size = be64_to_cpu(p_rsvmap[i * 2 + 1]);
352                 if (addr == 0 && size == 0)
353                         break;
354
355                 printf("/memreserve/ 0x%llx 0x%llx;\n", addr, size);
356         }
357
358         p = p_struct;
359         while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
360
361                 /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */
362
363                 if (tag == OF_DT_BEGIN_NODE) {
364                         s = (const char *)p;
365                         p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4);
366
367                         printf("%*s%s {\n", depth * shift, "", s);
368
369                         depth++;
370                         continue;
371                 }
372
373                 if (tag == OF_DT_END_NODE) {
374                         depth--;
375
376                         printf("%*s};\n", depth * shift, "");
377                         continue;
378                 }
379
380                 if (tag == OF_DT_NOP) {
381                         printf("%*s[NOP]\n", depth * shift, "");
382                         continue;
383                 }
384
385                 if (tag != OF_DT_PROP) {
386                         fprintf(stderr, "%*s ** Unknown tag 0x%08x\n",
387                                 depth * shift, "", tag);
388                         break;
389                 }
390                 sz = be32_to_cpu(*p++);
391                 s = (const char *)p_strings + be32_to_cpu(*p++);
392                 t = (const char *)p;
393                 p = (const u32 *)_ALIGN((unsigned long)p + sz, 4);
394                 printf("%*s%s", depth * shift, "", s);
395                 print_data(t, sz);
396                 printf(";\n");
397         }
398 }
399
400 void ft_backtrack_node(struct ft_cxt *cxt)
401 {
402         if (be32_to_cpu(*(u32 *) (cxt->p - 4)) != OF_DT_END_NODE)
403                 return;         /* XXX only for node */
404
405         cxt->p -= 4;
406 }
407
408 /* note that the root node of the blob is "peeled" off */
409 void ft_merge_blob(struct ft_cxt *cxt, void *blob)
410 {
411         struct boot_param_header *bph = (struct boot_param_header *)blob;
412         u32 *p_struct = (u32 *) ((char *)bph + be32_to_cpu(bph->off_dt_struct));
413         u32 *p_strings =
414             (u32 *) ((char *)bph + be32_to_cpu(bph->off_dt_strings));
415         u32 tag, *p;
416         char *s, *t;
417         int depth, sz;
418
419         if (be32_to_cpu(*(u32 *) (cxt->p - 4)) != OF_DT_END_NODE)
420                 return;         /* XXX only for node */
421
422         cxt->p -= 4;
423
424         depth = 0;
425         p = p_struct;
426         while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
427
428                 /* printf("tag: 0x%08x (%d) - %d\n", tag, p - p_struct, depth); */
429
430                 if (tag == OF_DT_BEGIN_NODE) {
431                         s = (char *)p;
432                         p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4);
433
434                         if (depth++ > 0)
435                                 ft_begin_node(cxt, s);
436
437                         continue;
438                 }
439
440                 if (tag == OF_DT_END_NODE) {
441                         ft_end_node(cxt);
442                         if (--depth == 0)
443                                 break;
444                         continue;
445                 }
446
447                 if (tag == OF_DT_NOP)
448                         continue;
449
450                 if (tag != OF_DT_PROP)
451                         break;
452
453                 sz = be32_to_cpu(*p++);
454                 s = (char *)p_strings + be32_to_cpu(*p++);
455                 t = (char *)p;
456                 p = (u32 *) _ALIGN((unsigned long)p + sz, 4);
457
458                 ft_prop(cxt, s, t, sz);
459         }
460 }
461
462 void *ft_get_prop(void *bphp, const char *propname, int *szp)
463 {
464         struct boot_param_header *bph = bphp;
465         uint32_t *p_struct =
466             (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_struct));
467         uint32_t *p_strings =
468             (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_strings));
469         uint32_t version = be32_to_cpu(bph->version);
470         uint32_t tag;
471         uint32_t *p;
472         char *s, *t;
473         char *ss;
474         int sz;
475         static char path[256], prop[256];
476
477         path[0] = '\0';
478
479         p = p_struct;
480         while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
481
482                 if (tag == OF_DT_BEGIN_NODE) {
483                         s = (char *)p;
484                         p = (uint32_t *) _ALIGN((unsigned long)p + strlen(s) +
485                                                 1, 4);
486                         strcat(path, s);
487                         strcat(path, "/");
488                         continue;
489                 }
490
491                 if (tag == OF_DT_END_NODE) {
492                         path[strlen(path) - 1] = '\0';
493                         ss = strrchr(path, '/');
494                         if (ss != NULL)
495                                 ss[1] = '\0';
496                         continue;
497                 }
498
499                 if (tag == OF_DT_NOP)
500                         continue;
501
502                 if (tag != OF_DT_PROP)
503                         break;
504
505                 sz = be32_to_cpu(*p++);
506                 s = (char *)p_strings + be32_to_cpu(*p++);
507                 if (version < 0x10 && sz >= 8)
508                         p = (uint32_t *) _ALIGN((unsigned long)p, 8);
509                 t = (char *)p;
510                 p = (uint32_t *) _ALIGN((unsigned long)p + sz, 4);
511
512                 strcpy(prop, path);
513                 strcat(prop, s);
514
515                 if (strcmp(prop, propname) == 0) {
516                         *szp = sz;
517                         return t;
518                 }
519         }
520
521         return NULL;
522 }
523
524 /********************************************************************/
525
526 extern unsigned char oftree_dtb[];
527 extern unsigned int oftree_dtb_len;
528
529 /* Function that returns a character from the environment */
530 extern uchar(*env_get_char) (int);
531
532 #define BDM(x)  {       .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
533
534 #ifdef CONFIG_OF_HAS_BD_T
535 static const struct {
536         const char *name;
537         int offset;
538 } bd_map[] = {
539         BDM(memstart),
540         BDM(memsize),
541         BDM(flashstart),
542         BDM(flashsize),
543         BDM(flashoffset),
544         BDM(sramstart),
545         BDM(sramsize),
546 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
547         || defined(CONFIG_E500)
548         BDM(immr_base),
549 #endif
550 #if defined(CONFIG_MPC5xxx)
551         BDM(mbar_base),
552 #endif
553 #if defined(CONFIG_MPC83XX)
554         BDM(immrbar),
555 #endif
556 #if defined(CONFIG_MPC8220)
557         BDM(mbar_base),
558         BDM(inpfreq),
559         BDM(pcifreq),
560         BDM(pevfreq),
561         BDM(flbfreq),
562         BDM(vcofreq),
563 #endif
564         BDM(bootflags),
565         BDM(ip_addr),
566         BDM(intfreq),
567         BDM(busfreq),
568 #ifdef CONFIG_CPM2
569         BDM(cpmfreq),
570         BDM(brgfreq),
571         BDM(sccfreq),
572         BDM(vco),
573 #endif
574 #if defined(CONFIG_MPC5xxx)
575         BDM(ipbfreq),
576         BDM(pcifreq),
577 #endif
578         BDM(baudrate),
579 };
580 #endif
581
582 void ft_setup(void *blob, int size, bd_t * bd, ulong initrd_start, ulong initrd_end)
583 {
584         u32 *p;
585         int len;
586         struct ft_cxt cxt;
587         ulong clock;
588 #if defined(CONFIG_OF_HAS_UBOOT_ENV)
589         int k, nxt;
590 #endif
591 #if defined(CONFIG_OF_HAS_BD_T)
592         u8 *end;
593 #endif
594 #if defined(CONFIG_OF_HAS_UBOOT_ENV) || defined(CONFIG_OF_HAS_BD_T)
595         int i;
596         static char tmpenv[256];
597 #endif
598
599         /* disable OF tree; booting old kernel */
600         if (getenv("disable_of") != NULL) {
601                 memcpy(blob, bd, sizeof(*bd));
602                 return;
603         }
604
605         ft_begin(&cxt, blob, size);
606
607         if (initrd_start && initrd_end)
608                 ft_add_rsvmap(&cxt, initrd_start, initrd_end - initrd_start + 1);
609
610         ft_begin_tree(&cxt);
611
612         ft_begin_node(&cxt, "");
613
614         ft_end_node(&cxt);
615
616         /* copy RO tree */
617         ft_merge_blob(&cxt, oftree_dtb);
618
619         /* back into root */
620         ft_backtrack_node(&cxt);
621
622 #ifdef CONFIG_OF_HAS_UBOOT_ENV
623         ft_begin_node(&cxt, "u-boot-env");
624
625         for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
626                 char *s, *lval, *rval;
627
628                 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) ;
629                 s = tmpenv;
630                 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
631                         *s++ = env_get_char(k);
632                 *s++ = '\0';
633                 lval = tmpenv;
634                 s = strchr(tmpenv, '=');
635                 if (s != NULL) {
636                         *s++ = '\0';
637                         rval = s;
638                 } else
639                         continue;
640                 ft_prop_str(&cxt, lval, rval);
641         }
642
643         ft_end_node(&cxt);
644 #endif
645
646         ft_begin_node(&cxt, "chosen");
647
648         ft_prop_str(&cxt, "name", "chosen");
649         ft_prop_str(&cxt, "bootargs", getenv("bootargs"));
650         ft_prop_int(&cxt, "linux,platform", 0x600);     /* what is this? */
651         if (initrd_start && initrd_end) {
652                 ft_prop_int(&cxt, "linux,initrd-start", initrd_start);
653                 ft_prop_int(&cxt, "linux,initrd-end", initrd_end);
654         }
655 #ifdef OF_STDOUT_PATH
656         ft_prop_str(&cxt, "linux,stdout-path", OF_STDOUT_PATH);
657 #endif
658
659         ft_end_node(&cxt);
660
661         ft_end_node(&cxt);      /* end root */
662
663         ft_end_tree(&cxt);
664
665         /*
666            printf("merged OF-tree\n");
667            ft_dump_blob(blob);
668          */
669
670 #ifdef CONFIG_OF_HAS_BD_T
671         /* paste the bd_t at the end of the flat tree */
672         end = (char *)blob +
673             be32_to_cpu(((struct boot_param_header *)blob)->totalsize);
674         memcpy(end, bd, sizeof(*bd));
675 #endif
676
677 #ifdef CONFIG_PPC
678
679 #ifdef CONFIG_OF_HAS_BD_T
680         for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
681                 uint32_t v;
682
683                 sprintf(tmpenv, "/bd_t/%s", bd_map[i].name);
684                 v = *(uint32_t *)((char *)bd + bd_map[i].offset);
685
686                 p = ft_get_prop(blob, tmpenv, &len);
687                 if (p != NULL)
688                         *p = cpu_to_be32(v);
689         }
690
691         p = ft_get_prop(blob, "/bd_t/enetaddr", &len);
692         if (p != NULL)
693                 memcpy(p, bd->bi_enetaddr, 6);
694
695         p = ft_get_prop(blob, "/bd_t/ethspeed", &len);
696         if (p != NULL)
697                 *p = cpu_to_be32((uint32_t) bd->bi_ethspeed);
698 #endif
699
700         clock = bd->bi_intfreq;
701         p = ft_get_prop(blob, "/cpus/" OF_CPU "/clock-frequency", &len);
702         if (p != NULL)
703                 *p = cpu_to_be32(clock);
704
705 #ifdef OF_TBCLK
706         clock = OF_TBCLK;
707         p = ft_get_prop(blob, "/cpus/" OF_CPU "/timebase-frequency", &len);
708         if (p != NULL)
709                 *p = cpu_to_be32(clock);
710 #endif
711 #endif                          /* __powerpc__ */
712
713 #ifdef CONFIG_OF_BOARD_SETUP
714         ft_board_setup(blob, bd);
715 #endif
716
717         /*
718            printf("final OF-tree\n");
719            ft_dump_blob(blob);
720          */
721
722 }
723
724 #endif