Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * OF flat tree builder |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 3 | * Written by: Pantelis Antoniou <pantelis.antoniou@gmail.com> |
| 4 | * Updated by: Matthew McClintock <msm@freescale.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | * MA 02111-1307 USA |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <common.h> |
| 23 | #include <malloc.h> |
| 24 | #include <environment.h> |
| 25 | |
Marian Balakowicz | fe93483 | 2005-10-28 18:08:03 +0200 | [diff] [blame] | 26 | #ifdef CONFIG_OF_FLAT_TREE |
| 27 | |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 28 | #include <asm/errno.h> |
| 29 | #include <stddef.h> |
| 30 | |
| 31 | #include <ft_build.h> |
| 32 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 33 | #undef DEBUG |
| 34 | |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 35 | /* align addr on a size boundary - adjust address up if needed -- Cort */ |
| 36 | #define _ALIGN(addr,size) (((addr)+(size)-1)&(~((size)-1))) |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 37 | #ifndef CONFIG_OF_BOOT_CPU |
| 38 | #define CONFIG_OF_BOOT_CPU 0 |
| 39 | #endif |
| 40 | #define SIZE_OF_RSVMAP_ENTRY (2*sizeof(u64)) |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 41 | |
| 42 | static void ft_put_word(struct ft_cxt *cxt, u32 v) |
| 43 | { |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 44 | memmove(cxt->p + sizeof(u32), cxt->p, cxt->p_end - cxt->p); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 45 | |
| 46 | *(u32 *) cxt->p = cpu_to_be32(v); |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 47 | cxt->p += sizeof(u32); |
| 48 | cxt->p_end += sizeof(u32); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static inline void ft_put_bin(struct ft_cxt *cxt, const void *data, int sz) |
| 52 | { |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 53 | int aligned_size = ((u8 *)_ALIGN((unsigned long)cxt->p + sz, |
| 54 | sizeof(u32))) - cxt->p; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 55 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 56 | memmove(cxt->p + aligned_size, cxt->p, cxt->p_end - cxt->p); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 57 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 58 | /* make sure the last bytes are zeroed */ |
| 59 | memset(cxt->p + aligned_size - (aligned_size % sizeof(u32)), 0, |
| 60 | (aligned_size % sizeof(u32))); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 61 | |
| 62 | memcpy(cxt->p, data, sz); |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 63 | |
| 64 | cxt->p += aligned_size; |
| 65 | cxt->p_end += aligned_size; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void ft_begin_node(struct ft_cxt *cxt, const char *name) |
| 69 | { |
| 70 | ft_put_word(cxt, OF_DT_BEGIN_NODE); |
| 71 | ft_put_bin(cxt, name, strlen(name) + 1); |
| 72 | } |
| 73 | |
| 74 | void ft_end_node(struct ft_cxt *cxt) |
| 75 | { |
| 76 | ft_put_word(cxt, OF_DT_END_NODE); |
| 77 | } |
| 78 | |
| 79 | void ft_nop(struct ft_cxt *cxt) |
| 80 | { |
| 81 | ft_put_word(cxt, OF_DT_NOP); |
| 82 | } |
| 83 | |
| 84 | static int lookup_string(struct ft_cxt *cxt, const char *name) |
| 85 | { |
| 86 | u8 *p; |
| 87 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 88 | p = cxt->p; |
| 89 | while (p < cxt->p_end) { |
Matthew McClintock | 7376eb8 | 2006-10-11 15:13:01 -0500 | [diff] [blame] | 90 | if (strcmp((char *)p, name) == 0) |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 91 | return p - cxt->p; |
Matthew McClintock | 7376eb8 | 2006-10-11 15:13:01 -0500 | [diff] [blame] | 92 | p += strlen((char *)p) + 1; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | void ft_prop(struct ft_cxt *cxt, const char *name, const void *data, int sz) |
| 99 | { |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 100 | int off = 0; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 101 | |
| 102 | off = lookup_string(cxt, name); |
| 103 | if (off == -1) { |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 104 | memcpy(cxt->p_end, name, strlen(name) + 1); |
| 105 | off = cxt->p_end - cxt->p; |
Zhang Wei | 88c8f49 | 2006-08-28 14:25:31 +0800 | [diff] [blame] | 106 | cxt->p_end += strlen(name) + 1; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* now put offset from beginning of *STRUCTURE* */ |
| 110 | /* will be fixed up at the end */ |
| 111 | ft_put_word(cxt, OF_DT_PROP); |
| 112 | ft_put_word(cxt, sz); |
| 113 | ft_put_word(cxt, off); |
| 114 | ft_put_bin(cxt, data, sz); |
| 115 | } |
| 116 | |
| 117 | void ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str) |
| 118 | { |
| 119 | ft_prop(cxt, name, str, strlen(str) + 1); |
| 120 | } |
| 121 | |
| 122 | void ft_prop_int(struct ft_cxt *cxt, const char *name, int val) |
| 123 | { |
| 124 | u32 v = cpu_to_be32((u32) val); |
| 125 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 126 | ft_prop(cxt, name, &v, sizeof(u32)); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 129 | /* pick up and start working on a tree in place */ |
| 130 | void ft_init_cxt(struct ft_cxt *cxt, void *blob) |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 131 | { |
| 132 | struct boot_param_header *bph = blob; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 133 | |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 134 | memset(cxt, 0, sizeof(*cxt)); |
| 135 | |
| 136 | cxt->bph = bph; |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 137 | bph->boot_cpuid_phys = CONFIG_OF_BOOT_CPU; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 138 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 139 | /* find beginning and end of reserve map table (zeros in last entry) */ |
| 140 | cxt->p_rsvmap = (u8 *)bph + bph->off_mem_rsvmap; |
| 141 | while ( ((uint64_t *)cxt->p_rsvmap)[0] != 0 && |
| 142 | ((uint64_t *)cxt->p_rsvmap)[1] != 0 ) { |
| 143 | cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY; |
| 144 | } |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 145 | |
Matthew McClintock | 7376eb8 | 2006-10-11 15:13:01 -0500 | [diff] [blame] | 146 | cxt->p_start = (u8 *)bph + bph->off_dt_struct; |
| 147 | cxt->p_end = (u8 *)bph + bph->totalsize; |
| 148 | cxt->p = (u8 *)bph + bph->off_dt_strings; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* add a reserver physical area to the rsvmap */ |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 152 | void ft_add_rsvmap(struct ft_cxt *cxt, u64 physstart, u64 physend) |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 153 | { |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 154 | memmove(cxt->p_rsvmap + SIZE_OF_RSVMAP_ENTRY, cxt->p_rsvmap, |
| 155 | cxt->p_end - cxt->p_rsvmap); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 156 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 157 | ((u64 *)cxt->p_rsvmap)[0] = cpu_to_be64(physstart); |
| 158 | ((u64 *)cxt->p_rsvmap)[1] = cpu_to_be64(physend); |
| 159 | ((u64 *)cxt->p_rsvmap)[2] = 0; |
| 160 | ((u64 *)cxt->p_rsvmap)[3] = 0; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 161 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 162 | cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY; |
| 163 | cxt->p_start += SIZE_OF_RSVMAP_ENTRY; |
| 164 | cxt->p += SIZE_OF_RSVMAP_ENTRY; |
| 165 | cxt->p_end += SIZE_OF_RSVMAP_ENTRY; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 168 | void ft_end_tree(struct ft_cxt *cxt) |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 169 | { |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 170 | ft_put_word(cxt, OF_DT_END); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 173 | /* update the boot param header with correct values */ |
| 174 | void ft_finalize_tree(struct ft_cxt *cxt) { |
| 175 | struct boot_param_header *bph = cxt->bph; |
| 176 | |
| 177 | bph->totalsize = cxt->p_end - (u8 *)bph; |
| 178 | bph->off_dt_struct = cxt->p_start - (u8 *)bph; |
| 179 | bph->off_dt_strings = cxt->p - (u8 *)bph; |
| 180 | bph->dt_strings_size = cxt->p_end - cxt->p; |
| 181 | } |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 182 | |
| 183 | static inline int isprint(int c) |
| 184 | { |
| 185 | return c >= 0x20 && c <= 0x7e; |
| 186 | } |
| 187 | |
| 188 | static int is_printable_string(const void *data, int len) |
| 189 | { |
| 190 | const char *s = data; |
| 191 | const char *ss; |
| 192 | |
| 193 | /* zero length is not */ |
| 194 | if (len == 0) |
| 195 | return 0; |
| 196 | |
| 197 | /* must terminate with zero */ |
| 198 | if (s[len - 1] != '\0') |
| 199 | return 0; |
| 200 | |
| 201 | ss = s; |
| 202 | while (*s && isprint(*s)) |
| 203 | s++; |
| 204 | |
| 205 | /* not zero, or not done yet */ |
| 206 | if (*s != '\0' || (s + 1 - ss) < len) |
| 207 | return 0; |
| 208 | |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | static void print_data(const void *data, int len) |
| 213 | { |
| 214 | int i; |
| 215 | const u8 *s; |
| 216 | |
| 217 | /* no data, don't print */ |
| 218 | if (len == 0) |
| 219 | return; |
| 220 | |
| 221 | if (is_printable_string(data, len)) { |
Wolfgang Denk | d262a92 | 2006-10-09 12:50:41 +0200 | [diff] [blame] | 222 | puts(" = \""); |
| 223 | puts(data); |
| 224 | puts("\""); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | |
| 228 | switch (len) { |
| 229 | case 1: /* byte */ |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 230 | printf(" = <%02x>", (*(u8 *) data) & 0xff); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 231 | break; |
| 232 | case 2: /* half-word */ |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 233 | printf(" = <%04x>", be16_to_cpu(*(u16 *) data) & 0xffff); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 234 | break; |
| 235 | case 4: /* word */ |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 236 | printf(" = <%x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 237 | break; |
| 238 | case 8: /* double-word */ |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 239 | printf(" = <%qx>", be64_to_cpu(*(uint64_t *) data)); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 240 | break; |
| 241 | default: /* anything else... hexdump */ |
| 242 | printf(" = ["); |
| 243 | for (i = 0, s = data; i < len; i++) |
| 244 | printf("%02x%s", s[i], i < len - 1 ? " " : ""); |
| 245 | printf("]"); |
| 246 | |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void ft_dump_blob(const void *bphp) |
| 252 | { |
| 253 | const struct boot_param_header *bph = bphp; |
| 254 | const uint64_t *p_rsvmap = (const uint64_t *) |
| 255 | ((const char *)bph + be32_to_cpu(bph->off_mem_rsvmap)); |
| 256 | const u32 *p_struct = (const u32 *) |
| 257 | ((const char *)bph + be32_to_cpu(bph->off_dt_struct)); |
| 258 | const u32 *p_strings = (const u32 *) |
| 259 | ((const char *)bph + be32_to_cpu(bph->off_dt_strings)); |
| 260 | u32 tag; |
| 261 | const u32 *p; |
| 262 | const char *s, *t; |
| 263 | int depth, sz, shift; |
| 264 | int i; |
| 265 | uint64_t addr, size; |
| 266 | |
| 267 | if (be32_to_cpu(bph->magic) != OF_DT_HEADER) { |
| 268 | /* not valid tree */ |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | depth = 0; |
| 273 | shift = 4; |
| 274 | |
| 275 | for (i = 0;; i++) { |
| 276 | addr = be64_to_cpu(p_rsvmap[i * 2]); |
| 277 | size = be64_to_cpu(p_rsvmap[i * 2 + 1]); |
| 278 | if (addr == 0 && size == 0) |
| 279 | break; |
| 280 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 281 | printf("/memreserve/ %qx %qx;\n", addr, size); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | p = p_struct; |
| 285 | while ((tag = be32_to_cpu(*p++)) != OF_DT_END) { |
| 286 | |
| 287 | /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */ |
| 288 | |
| 289 | if (tag == OF_DT_BEGIN_NODE) { |
| 290 | s = (const char *)p; |
| 291 | p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4); |
| 292 | |
| 293 | printf("%*s%s {\n", depth * shift, "", s); |
| 294 | |
| 295 | depth++; |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | if (tag == OF_DT_END_NODE) { |
| 300 | depth--; |
| 301 | |
| 302 | printf("%*s};\n", depth * shift, ""); |
| 303 | continue; |
| 304 | } |
| 305 | |
| 306 | if (tag == OF_DT_NOP) { |
| 307 | printf("%*s[NOP]\n", depth * shift, ""); |
| 308 | continue; |
| 309 | } |
| 310 | |
| 311 | if (tag != OF_DT_PROP) { |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 312 | fprintf(stderr, "%*s ** Unknown tag 0x%08x at 0x%x\n", |
| 313 | depth * shift, "", tag, --p); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | sz = be32_to_cpu(*p++); |
| 317 | s = (const char *)p_strings + be32_to_cpu(*p++); |
| 318 | t = (const char *)p; |
| 319 | p = (const u32 *)_ALIGN((unsigned long)p + sz, 4); |
| 320 | printf("%*s%s", depth * shift, "", s); |
| 321 | print_data(t, sz); |
| 322 | printf(";\n"); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void ft_backtrack_node(struct ft_cxt *cxt) |
| 327 | { |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 328 | int i = 4; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 329 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 330 | while (be32_to_cpu(*(u32 *) (cxt->p - i)) != OF_DT_END_NODE) |
| 331 | i += 4; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 332 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 333 | memmove (cxt->p - i, cxt->p, cxt->p_end - cxt->p); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 334 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 335 | cxt->p_end -= i; |
| 336 | cxt->p -= i; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void *ft_get_prop(void *bphp, const char *propname, int *szp) |
| 340 | { |
| 341 | struct boot_param_header *bph = bphp; |
| 342 | uint32_t *p_struct = |
| 343 | (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_struct)); |
| 344 | uint32_t *p_strings = |
| 345 | (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_strings)); |
| 346 | uint32_t version = be32_to_cpu(bph->version); |
| 347 | uint32_t tag; |
| 348 | uint32_t *p; |
| 349 | char *s, *t; |
| 350 | char *ss; |
| 351 | int sz; |
| 352 | static char path[256], prop[256]; |
| 353 | |
| 354 | path[0] = '\0'; |
| 355 | |
| 356 | p = p_struct; |
| 357 | while ((tag = be32_to_cpu(*p++)) != OF_DT_END) { |
| 358 | |
| 359 | if (tag == OF_DT_BEGIN_NODE) { |
| 360 | s = (char *)p; |
| 361 | p = (uint32_t *) _ALIGN((unsigned long)p + strlen(s) + |
| 362 | 1, 4); |
| 363 | strcat(path, s); |
| 364 | strcat(path, "/"); |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | if (tag == OF_DT_END_NODE) { |
| 369 | path[strlen(path) - 1] = '\0'; |
| 370 | ss = strrchr(path, '/'); |
| 371 | if (ss != NULL) |
| 372 | ss[1] = '\0'; |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | if (tag == OF_DT_NOP) |
| 377 | continue; |
| 378 | |
| 379 | if (tag != OF_DT_PROP) |
| 380 | break; |
| 381 | |
| 382 | sz = be32_to_cpu(*p++); |
| 383 | s = (char *)p_strings + be32_to_cpu(*p++); |
| 384 | if (version < 0x10 && sz >= 8) |
| 385 | p = (uint32_t *) _ALIGN((unsigned long)p, 8); |
| 386 | t = (char *)p; |
| 387 | p = (uint32_t *) _ALIGN((unsigned long)p + sz, 4); |
| 388 | |
| 389 | strcpy(prop, path); |
| 390 | strcat(prop, s); |
| 391 | |
| 392 | if (strcmp(prop, propname) == 0) { |
| 393 | *szp = sz; |
| 394 | return t; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | /********************************************************************/ |
| 402 | |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 403 | /* Function that returns a character from the environment */ |
| 404 | extern uchar(*env_get_char) (int); |
| 405 | |
| 406 | #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) } |
| 407 | |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 408 | #ifdef CONFIG_OF_HAS_BD_T |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 409 | static const struct { |
| 410 | const char *name; |
| 411 | int offset; |
| 412 | } bd_map[] = { |
| 413 | BDM(memstart), |
| 414 | BDM(memsize), |
| 415 | BDM(flashstart), |
| 416 | BDM(flashsize), |
| 417 | BDM(flashoffset), |
| 418 | BDM(sramstart), |
| 419 | BDM(sramsize), |
| 420 | #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \ |
| 421 | || defined(CONFIG_E500) |
| 422 | BDM(immr_base), |
| 423 | #endif |
| 424 | #if defined(CONFIG_MPC5xxx) |
| 425 | BDM(mbar_base), |
| 426 | #endif |
| 427 | #if defined(CONFIG_MPC83XX) |
| 428 | BDM(immrbar), |
| 429 | #endif |
| 430 | #if defined(CONFIG_MPC8220) |
| 431 | BDM(mbar_base), |
| 432 | BDM(inpfreq), |
| 433 | BDM(pcifreq), |
| 434 | BDM(pevfreq), |
| 435 | BDM(flbfreq), |
| 436 | BDM(vcofreq), |
| 437 | #endif |
| 438 | BDM(bootflags), |
| 439 | BDM(ip_addr), |
| 440 | BDM(intfreq), |
| 441 | BDM(busfreq), |
| 442 | #ifdef CONFIG_CPM2 |
| 443 | BDM(cpmfreq), |
| 444 | BDM(brgfreq), |
| 445 | BDM(sccfreq), |
| 446 | BDM(vco), |
| 447 | #endif |
| 448 | #if defined(CONFIG_MPC5xxx) |
| 449 | BDM(ipbfreq), |
| 450 | BDM(pcifreq), |
| 451 | #endif |
| 452 | BDM(baudrate), |
| 453 | }; |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 454 | #endif |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 455 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 456 | void ft_setup(void *blob, bd_t * bd, ulong initrd_start, ulong initrd_end) |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 457 | { |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 458 | u32 *p; |
| 459 | int len; |
| 460 | struct ft_cxt cxt; |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 461 | ulong clock; |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 462 | #if defined(CONFIG_OF_HAS_UBOOT_ENV) |
| 463 | int k, nxt; |
| 464 | #endif |
| 465 | #if defined(CONFIG_OF_HAS_BD_T) |
| 466 | u8 *end; |
| 467 | #endif |
| 468 | #if defined(CONFIG_OF_HAS_UBOOT_ENV) || defined(CONFIG_OF_HAS_BD_T) |
| 469 | int i; |
| 470 | static char tmpenv[256]; |
| 471 | #endif |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 472 | |
| 473 | /* disable OF tree; booting old kernel */ |
| 474 | if (getenv("disable_of") != NULL) { |
| 475 | memcpy(blob, bd, sizeof(*bd)); |
| 476 | return; |
| 477 | } |
| 478 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 479 | #ifdef DEBUG |
| 480 | printf ("recieved oftree\n"); |
| 481 | ft_dump_blob(blob); |
| 482 | #endif |
| 483 | |
| 484 | ft_init_cxt(&cxt, blob); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 485 | |
Kumar Gala | e559a69 | 2006-01-11 16:41:35 -0600 | [diff] [blame] | 486 | if (initrd_start && initrd_end) |
| 487 | ft_add_rsvmap(&cxt, initrd_start, initrd_end - initrd_start + 1); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 488 | |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 489 | /* back into root */ |
| 490 | ft_backtrack_node(&cxt); |
| 491 | |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 492 | #ifdef CONFIG_OF_HAS_UBOOT_ENV |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 493 | ft_begin_node(&cxt, "u-boot-env"); |
| 494 | |
| 495 | for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 496 | char *s, *lval, *rval; |
| 497 | |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 498 | for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) ; |
| 499 | s = tmpenv; |
| 500 | for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k) |
| 501 | *s++ = env_get_char(k); |
| 502 | *s++ = '\0'; |
| 503 | lval = tmpenv; |
| 504 | s = strchr(tmpenv, '='); |
| 505 | if (s != NULL) { |
| 506 | *s++ = '\0'; |
| 507 | rval = s; |
| 508 | } else |
| 509 | continue; |
| 510 | ft_prop_str(&cxt, lval, rval); |
| 511 | } |
| 512 | |
| 513 | ft_end_node(&cxt); |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 514 | #endif |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 515 | |
| 516 | ft_begin_node(&cxt, "chosen"); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 517 | ft_prop_str(&cxt, "name", "chosen"); |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 518 | |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 519 | ft_prop_str(&cxt, "bootargs", getenv("bootargs")); |
| 520 | ft_prop_int(&cxt, "linux,platform", 0x600); /* what is this? */ |
Kumar Gala | e559a69 | 2006-01-11 16:41:35 -0600 | [diff] [blame] | 521 | if (initrd_start && initrd_end) { |
| 522 | ft_prop_int(&cxt, "linux,initrd-start", initrd_start); |
| 523 | ft_prop_int(&cxt, "linux,initrd-end", initrd_end); |
| 524 | } |
Kumar Gala | c2871f0 | 2006-01-11 13:59:02 -0600 | [diff] [blame] | 525 | #ifdef OF_STDOUT_PATH |
| 526 | ft_prop_str(&cxt, "linux,stdout-path", OF_STDOUT_PATH); |
| 527 | #endif |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 528 | |
| 529 | ft_end_node(&cxt); |
| 530 | |
| 531 | ft_end_node(&cxt); /* end root */ |
| 532 | |
| 533 | ft_end_tree(&cxt); |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 534 | ft_finalize_tree(&cxt); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 535 | |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 536 | #ifdef CONFIG_OF_HAS_BD_T |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 537 | /* paste the bd_t at the end of the flat tree */ |
| 538 | end = (char *)blob + |
| 539 | be32_to_cpu(((struct boot_param_header *)blob)->totalsize); |
| 540 | memcpy(end, bd, sizeof(*bd)); |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 541 | #endif |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 542 | |
| 543 | #ifdef CONFIG_PPC |
| 544 | |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 545 | #ifdef CONFIG_OF_HAS_BD_T |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 546 | for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) { |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 547 | uint32_t v; |
| 548 | |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 549 | sprintf(tmpenv, "/bd_t/%s", bd_map[i].name); |
| 550 | v = *(uint32_t *)((char *)bd + bd_map[i].offset); |
| 551 | |
| 552 | p = ft_get_prop(blob, tmpenv, &len); |
| 553 | if (p != NULL) |
| 554 | *p = cpu_to_be32(v); |
| 555 | } |
| 556 | |
| 557 | p = ft_get_prop(blob, "/bd_t/enetaddr", &len); |
| 558 | if (p != NULL) |
| 559 | memcpy(p, bd->bi_enetaddr, 6); |
| 560 | |
| 561 | p = ft_get_prop(blob, "/bd_t/ethspeed", &len); |
| 562 | if (p != NULL) |
| 563 | *p = cpu_to_be32((uint32_t) bd->bi_ethspeed); |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 564 | #endif |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 565 | |
| 566 | clock = bd->bi_intfreq; |
| 567 | p = ft_get_prop(blob, "/cpus/" OF_CPU "/clock-frequency", &len); |
| 568 | if (p != NULL) |
| 569 | *p = cpu_to_be32(clock); |
| 570 | |
| 571 | #ifdef OF_TBCLK |
| 572 | clock = OF_TBCLK; |
| 573 | p = ft_get_prop(blob, "/cpus/" OF_CPU "/timebase-frequency", &len); |
| 574 | if (p != NULL) |
Kumar Gala | e4f880e | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 575 | *p = cpu_to_be32(clock); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 576 | #endif |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 577 | #endif /* __powerpc__ */ |
| 578 | |
Kumar Gala | 4e25313 | 2006-01-11 13:54:17 -0600 | [diff] [blame] | 579 | #ifdef CONFIG_OF_BOARD_SETUP |
| 580 | ft_board_setup(blob, bd); |
| 581 | #endif |
| 582 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 583 | /* in case the size changed in the platform code */ |
| 584 | ft_finalize_tree(&cxt); |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 585 | |
Matthew McClintock | 1b380ec | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 586 | #ifdef DEBUG |
| 587 | printf("final OF-tree\n"); |
| 588 | ft_dump_blob(blob); |
| 589 | #endif |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 590 | } |
Wolfgang Denk | 814d98f | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 591 | #endif |