Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * OF flat tree builder |
Matthew McClintock | 349b6e0 | 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 | 6a63e36 | 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 | ff2dcf5 | 2005-10-28 18:08:03 +0200 | [diff] [blame] | 26 | #ifdef CONFIG_OF_FLAT_TREE |
| 27 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 28 | #include <asm/errno.h> |
| 29 | #include <stddef.h> |
| 30 | |
| 31 | #include <ft_build.h> |
Grant Likely | f6f2edf | 2007-02-20 09:05:07 +0100 | [diff] [blame] | 32 | #include <linux/ctype.h> |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 33 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 34 | #undef DEBUG |
| 35 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 36 | /* align addr on a size boundary - adjust address up if needed -- Cort */ |
| 37 | #define _ALIGN(addr,size) (((addr)+(size)-1)&(~((size)-1))) |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 38 | #ifndef CONFIG_OF_BOOT_CPU |
| 39 | #define CONFIG_OF_BOOT_CPU 0 |
| 40 | #endif |
| 41 | #define SIZE_OF_RSVMAP_ENTRY (2*sizeof(u64)) |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 42 | |
| 43 | static void ft_put_word(struct ft_cxt *cxt, u32 v) |
| 44 | { |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 45 | memmove(cxt->p + sizeof(u32), cxt->p, cxt->p_end - cxt->p); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 46 | |
| 47 | *(u32 *) cxt->p = cpu_to_be32(v); |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 48 | cxt->p += sizeof(u32); |
| 49 | cxt->p_end += sizeof(u32); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static inline void ft_put_bin(struct ft_cxt *cxt, const void *data, int sz) |
| 53 | { |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 54 | int aligned_size = ((u8 *)_ALIGN((unsigned long)cxt->p + sz, |
| 55 | sizeof(u32))) - cxt->p; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 56 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 57 | memmove(cxt->p + aligned_size, cxt->p, cxt->p_end - cxt->p); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 58 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 59 | /* make sure the last bytes are zeroed */ |
| 60 | memset(cxt->p + aligned_size - (aligned_size % sizeof(u32)), 0, |
| 61 | (aligned_size % sizeof(u32))); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 62 | |
| 63 | memcpy(cxt->p, data, sz); |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 64 | |
| 65 | cxt->p += aligned_size; |
| 66 | cxt->p_end += aligned_size; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void ft_begin_node(struct ft_cxt *cxt, const char *name) |
| 70 | { |
| 71 | ft_put_word(cxt, OF_DT_BEGIN_NODE); |
| 72 | ft_put_bin(cxt, name, strlen(name) + 1); |
| 73 | } |
| 74 | |
| 75 | void ft_end_node(struct ft_cxt *cxt) |
| 76 | { |
| 77 | ft_put_word(cxt, OF_DT_END_NODE); |
| 78 | } |
| 79 | |
| 80 | void ft_nop(struct ft_cxt *cxt) |
| 81 | { |
| 82 | ft_put_word(cxt, OF_DT_NOP); |
| 83 | } |
| 84 | |
| 85 | static int lookup_string(struct ft_cxt *cxt, const char *name) |
| 86 | { |
| 87 | u8 *p; |
| 88 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 89 | p = cxt->p; |
| 90 | while (p < cxt->p_end) { |
Matthew McClintock | 5b94882 | 2006-10-11 15:13:01 -0500 | [diff] [blame] | 91 | if (strcmp((char *)p, name) == 0) |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 92 | return p - cxt->p; |
Matthew McClintock | 5b94882 | 2006-10-11 15:13:01 -0500 | [diff] [blame] | 93 | p += strlen((char *)p) + 1; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | void ft_prop(struct ft_cxt *cxt, const char *name, const void *data, int sz) |
| 100 | { |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 101 | int off = 0; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 102 | |
| 103 | off = lookup_string(cxt, name); |
| 104 | if (off == -1) { |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 105 | memcpy(cxt->p_end, name, strlen(name) + 1); |
| 106 | off = cxt->p_end - cxt->p; |
Zhang Wei | c4e0f05 | 2006-08-28 14:25:31 +0800 | [diff] [blame] | 107 | cxt->p_end += strlen(name) + 1; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* now put offset from beginning of *STRUCTURE* */ |
| 111 | /* will be fixed up at the end */ |
| 112 | ft_put_word(cxt, OF_DT_PROP); |
| 113 | ft_put_word(cxt, sz); |
| 114 | ft_put_word(cxt, off); |
| 115 | ft_put_bin(cxt, data, sz); |
| 116 | } |
| 117 | |
| 118 | void ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str) |
| 119 | { |
| 120 | ft_prop(cxt, name, str, strlen(str) + 1); |
| 121 | } |
| 122 | |
| 123 | void ft_prop_int(struct ft_cxt *cxt, const char *name, int val) |
| 124 | { |
| 125 | u32 v = cpu_to_be32((u32) val); |
| 126 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 127 | ft_prop(cxt, name, &v, sizeof(u32)); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 130 | /* pick up and start working on a tree in place */ |
| 131 | void ft_init_cxt(struct ft_cxt *cxt, void *blob) |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 132 | { |
| 133 | struct boot_param_header *bph = blob; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 134 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 135 | memset(cxt, 0, sizeof(*cxt)); |
| 136 | |
| 137 | cxt->bph = bph; |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 138 | bph->boot_cpuid_phys = CONFIG_OF_BOOT_CPU; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 139 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 140 | /* find beginning and end of reserve map table (zeros in last entry) */ |
| 141 | cxt->p_rsvmap = (u8 *)bph + bph->off_mem_rsvmap; |
| 142 | while ( ((uint64_t *)cxt->p_rsvmap)[0] != 0 && |
| 143 | ((uint64_t *)cxt->p_rsvmap)[1] != 0 ) { |
| 144 | cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY; |
| 145 | } |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 146 | |
Matthew McClintock | 5b94882 | 2006-10-11 15:13:01 -0500 | [diff] [blame] | 147 | cxt->p_start = (u8 *)bph + bph->off_dt_struct; |
| 148 | cxt->p_end = (u8 *)bph + bph->totalsize; |
| 149 | cxt->p = (u8 *)bph + bph->off_dt_strings; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* add a reserver physical area to the rsvmap */ |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 153 | void ft_add_rsvmap(struct ft_cxt *cxt, u64 physstart, u64 physend) |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 154 | { |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 155 | memmove(cxt->p_rsvmap + SIZE_OF_RSVMAP_ENTRY, cxt->p_rsvmap, |
| 156 | cxt->p_end - cxt->p_rsvmap); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 157 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 158 | ((u64 *)cxt->p_rsvmap)[0] = cpu_to_be64(physstart); |
| 159 | ((u64 *)cxt->p_rsvmap)[1] = cpu_to_be64(physend); |
| 160 | ((u64 *)cxt->p_rsvmap)[2] = 0; |
| 161 | ((u64 *)cxt->p_rsvmap)[3] = 0; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 162 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 163 | cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY; |
| 164 | cxt->p_start += SIZE_OF_RSVMAP_ENTRY; |
| 165 | cxt->p += SIZE_OF_RSVMAP_ENTRY; |
| 166 | cxt->p_end += SIZE_OF_RSVMAP_ENTRY; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 169 | void ft_end_tree(struct ft_cxt *cxt) |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 170 | { |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 171 | ft_put_word(cxt, OF_DT_END); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 174 | /* update the boot param header with correct values */ |
| 175 | void ft_finalize_tree(struct ft_cxt *cxt) { |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 176 | struct boot_param_header *bph = cxt->bph; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 177 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 178 | bph->totalsize = cxt->p_end - (u8 *)bph; |
| 179 | bph->off_dt_struct = cxt->p_start - (u8 *)bph; |
| 180 | bph->off_dt_strings = cxt->p - (u8 *)bph; |
| 181 | bph->dt_strings_size = cxt->p_end - cxt->p; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 184 | static int is_printable_string(const void *data, int len) |
| 185 | { |
| 186 | const char *s = data; |
| 187 | const char *ss; |
| 188 | |
| 189 | /* zero length is not */ |
| 190 | if (len == 0) |
| 191 | return 0; |
| 192 | |
| 193 | /* must terminate with zero */ |
| 194 | if (s[len - 1] != '\0') |
| 195 | return 0; |
| 196 | |
| 197 | ss = s; |
| 198 | while (*s && isprint(*s)) |
| 199 | s++; |
| 200 | |
| 201 | /* not zero, or not done yet */ |
| 202 | if (*s != '\0' || (s + 1 - ss) < len) |
| 203 | return 0; |
| 204 | |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | static void print_data(const void *data, int len) |
| 209 | { |
| 210 | int i; |
| 211 | const u8 *s; |
| 212 | |
| 213 | /* no data, don't print */ |
| 214 | if (len == 0) |
| 215 | return; |
| 216 | |
| 217 | if (is_printable_string(data, len)) { |
Wolfgang Denk | 98ddb71 | 2006-10-09 12:50:41 +0200 | [diff] [blame] | 218 | puts(" = \""); |
| 219 | puts(data); |
| 220 | puts("\""); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
| 224 | switch (len) { |
| 225 | case 1: /* byte */ |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 226 | printf(" = <%02x>", (*(u8 *) data) & 0xff); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 227 | break; |
| 228 | case 2: /* half-word */ |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 229 | printf(" = <%04x>", be16_to_cpu(*(u16 *) data) & 0xffff); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 230 | break; |
| 231 | case 4: /* word */ |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 232 | printf(" = <%x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 233 | break; |
| 234 | case 8: /* double-word */ |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 235 | printf(" = <%qx>", be64_to_cpu(*(uint64_t *) data)); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 236 | break; |
| 237 | default: /* anything else... hexdump */ |
| 238 | printf(" = ["); |
| 239 | for (i = 0, s = data; i < len; i++) |
| 240 | printf("%02x%s", s[i], i < len - 1 ? " " : ""); |
| 241 | printf("]"); |
| 242 | |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void ft_dump_blob(const void *bphp) |
| 248 | { |
| 249 | const struct boot_param_header *bph = bphp; |
| 250 | const uint64_t *p_rsvmap = (const uint64_t *) |
| 251 | ((const char *)bph + be32_to_cpu(bph->off_mem_rsvmap)); |
| 252 | const u32 *p_struct = (const u32 *) |
| 253 | ((const char *)bph + be32_to_cpu(bph->off_dt_struct)); |
| 254 | const u32 *p_strings = (const u32 *) |
| 255 | ((const char *)bph + be32_to_cpu(bph->off_dt_strings)); |
| 256 | u32 tag; |
| 257 | const u32 *p; |
| 258 | const char *s, *t; |
| 259 | int depth, sz, shift; |
| 260 | int i; |
| 261 | uint64_t addr, size; |
| 262 | |
| 263 | if (be32_to_cpu(bph->magic) != OF_DT_HEADER) { |
| 264 | /* not valid tree */ |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | depth = 0; |
| 269 | shift = 4; |
| 270 | |
| 271 | for (i = 0;; i++) { |
| 272 | addr = be64_to_cpu(p_rsvmap[i * 2]); |
| 273 | size = be64_to_cpu(p_rsvmap[i * 2 + 1]); |
| 274 | if (addr == 0 && size == 0) |
| 275 | break; |
| 276 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 277 | printf("/memreserve/ %qx %qx;\n", addr, size); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | p = p_struct; |
| 281 | while ((tag = be32_to_cpu(*p++)) != OF_DT_END) { |
| 282 | |
| 283 | /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */ |
| 284 | |
| 285 | if (tag == OF_DT_BEGIN_NODE) { |
| 286 | s = (const char *)p; |
| 287 | p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4); |
| 288 | |
| 289 | printf("%*s%s {\n", depth * shift, "", s); |
| 290 | |
| 291 | depth++; |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | if (tag == OF_DT_END_NODE) { |
| 296 | depth--; |
| 297 | |
| 298 | printf("%*s};\n", depth * shift, ""); |
| 299 | continue; |
| 300 | } |
| 301 | |
| 302 | if (tag == OF_DT_NOP) { |
| 303 | printf("%*s[NOP]\n", depth * shift, ""); |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | if (tag != OF_DT_PROP) { |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 308 | fprintf(stderr, "%*s ** Unknown tag 0x%08x at 0x%x\n", |
| 309 | depth * shift, "", tag, --p); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | sz = be32_to_cpu(*p++); |
| 313 | s = (const char *)p_strings + be32_to_cpu(*p++); |
| 314 | t = (const char *)p; |
| 315 | p = (const u32 *)_ALIGN((unsigned long)p + sz, 4); |
| 316 | printf("%*s%s", depth * shift, "", s); |
| 317 | print_data(t, sz); |
| 318 | printf(";\n"); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void ft_backtrack_node(struct ft_cxt *cxt) |
| 323 | { |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 324 | int i = 4; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 325 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 326 | while (be32_to_cpu(*(u32 *) (cxt->p - i)) != OF_DT_END_NODE) |
| 327 | i += 4; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 328 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 329 | memmove (cxt->p - i, cxt->p, cxt->p_end - cxt->p); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 330 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 331 | cxt->p_end -= i; |
| 332 | cxt->p -= i; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void *ft_get_prop(void *bphp, const char *propname, int *szp) |
| 336 | { |
| 337 | struct boot_param_header *bph = bphp; |
| 338 | uint32_t *p_struct = |
| 339 | (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_struct)); |
| 340 | uint32_t *p_strings = |
| 341 | (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_strings)); |
| 342 | uint32_t version = be32_to_cpu(bph->version); |
| 343 | uint32_t tag; |
| 344 | uint32_t *p; |
| 345 | char *s, *t; |
| 346 | char *ss; |
| 347 | int sz; |
| 348 | static char path[256], prop[256]; |
| 349 | |
| 350 | path[0] = '\0'; |
| 351 | |
| 352 | p = p_struct; |
| 353 | while ((tag = be32_to_cpu(*p++)) != OF_DT_END) { |
| 354 | |
| 355 | if (tag == OF_DT_BEGIN_NODE) { |
| 356 | s = (char *)p; |
| 357 | p = (uint32_t *) _ALIGN((unsigned long)p + strlen(s) + |
| 358 | 1, 4); |
| 359 | strcat(path, s); |
| 360 | strcat(path, "/"); |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | if (tag == OF_DT_END_NODE) { |
| 365 | path[strlen(path) - 1] = '\0'; |
| 366 | ss = strrchr(path, '/'); |
| 367 | if (ss != NULL) |
| 368 | ss[1] = '\0'; |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | if (tag == OF_DT_NOP) |
| 373 | continue; |
| 374 | |
| 375 | if (tag != OF_DT_PROP) |
| 376 | break; |
| 377 | |
| 378 | sz = be32_to_cpu(*p++); |
| 379 | s = (char *)p_strings + be32_to_cpu(*p++); |
| 380 | if (version < 0x10 && sz >= 8) |
| 381 | p = (uint32_t *) _ALIGN((unsigned long)p, 8); |
| 382 | t = (char *)p; |
| 383 | p = (uint32_t *) _ALIGN((unsigned long)p + sz, 4); |
| 384 | |
| 385 | strcpy(prop, path); |
| 386 | strcat(prop, s); |
| 387 | |
| 388 | if (strcmp(prop, propname) == 0) { |
| 389 | *szp = sz; |
| 390 | return t; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | /********************************************************************/ |
| 398 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 399 | void ft_setup(void *blob, bd_t * bd, ulong initrd_start, ulong initrd_end) |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 400 | { |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 401 | u32 *p; |
| 402 | int len; |
| 403 | struct ft_cxt cxt; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 404 | ulong clock; |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 405 | |
| 406 | /* disable OF tree; booting old kernel */ |
| 407 | if (getenv("disable_of") != NULL) { |
| 408 | memcpy(blob, bd, sizeof(*bd)); |
| 409 | return; |
| 410 | } |
| 411 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 412 | #ifdef DEBUG |
| 413 | printf ("recieved oftree\n"); |
| 414 | ft_dump_blob(blob); |
| 415 | #endif |
| 416 | |
| 417 | ft_init_cxt(&cxt, blob); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 418 | |
Kumar Gala | a8b76de | 2006-01-11 16:41:35 -0600 | [diff] [blame] | 419 | if (initrd_start && initrd_end) |
| 420 | ft_add_rsvmap(&cxt, initrd_start, initrd_end - initrd_start + 1); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 421 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 422 | /* back into root */ |
| 423 | ft_backtrack_node(&cxt); |
| 424 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 425 | ft_begin_node(&cxt, "chosen"); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 426 | ft_prop_str(&cxt, "name", "chosen"); |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 427 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 428 | ft_prop_str(&cxt, "bootargs", getenv("bootargs")); |
| 429 | ft_prop_int(&cxt, "linux,platform", 0x600); /* what is this? */ |
Kumar Gala | a8b76de | 2006-01-11 16:41:35 -0600 | [diff] [blame] | 430 | if (initrd_start && initrd_end) { |
| 431 | ft_prop_int(&cxt, "linux,initrd-start", initrd_start); |
| 432 | ft_prop_int(&cxt, "linux,initrd-end", initrd_end); |
| 433 | } |
Kumar Gala | e40c2b5 | 2006-01-11 13:59:02 -0600 | [diff] [blame] | 434 | #ifdef OF_STDOUT_PATH |
| 435 | ft_prop_str(&cxt, "linux,stdout-path", OF_STDOUT_PATH); |
| 436 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 437 | |
| 438 | ft_end_node(&cxt); |
| 439 | |
| 440 | ft_end_node(&cxt); /* end root */ |
| 441 | |
| 442 | ft_end_tree(&cxt); |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 443 | ft_finalize_tree(&cxt); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 444 | |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 445 | #ifdef CONFIG_PPC |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 446 | clock = bd->bi_intfreq; |
| 447 | p = ft_get_prop(blob, "/cpus/" OF_CPU "/clock-frequency", &len); |
| 448 | if (p != NULL) |
| 449 | *p = cpu_to_be32(clock); |
| 450 | |
| 451 | #ifdef OF_TBCLK |
| 452 | clock = OF_TBCLK; |
| 453 | p = ft_get_prop(blob, "/cpus/" OF_CPU "/timebase-frequency", &len); |
| 454 | if (p != NULL) |
Kumar Gala | 4ce1431 | 2006-01-11 13:49:31 -0600 | [diff] [blame] | 455 | *p = cpu_to_be32(clock); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 456 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 457 | #endif /* __powerpc__ */ |
| 458 | |
Kumar Gala | 1e26aa5 | 2006-01-11 13:54:17 -0600 | [diff] [blame] | 459 | #ifdef CONFIG_OF_BOARD_SETUP |
| 460 | ft_board_setup(blob, bd); |
| 461 | #endif |
| 462 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 463 | /* in case the size changed in the platform code */ |
| 464 | ft_finalize_tree(&cxt); |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 465 | |
Matthew McClintock | 349b6e0 | 2006-06-28 10:42:24 -0500 | [diff] [blame] | 466 | #ifdef DEBUG |
| 467 | printf("final OF-tree\n"); |
| 468 | ft_dump_blob(blob); |
| 469 | #endif |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 470 | } |
Wolfgang Denk | 6a63e36 | 2005-10-13 01:59:29 +0200 | [diff] [blame] | 471 | #endif |