David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| 3 | * |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | |
| 21 | #include "dtc.h" |
David Gibson | ab870ca | 2005-06-23 15:45:13 +1000 | [diff] [blame] | 22 | #include "flat_dt.h" |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 23 | |
| 24 | #define FTF_FULLPATH 0x1 |
| 25 | #define FTF_VARALIGN 0x2 |
| 26 | #define FTF_NAMEPROPS 0x4 |
| 27 | #define FTF_BOOTCPUID 0x8 |
| 28 | #define FTF_STRTABSIZE 0x10 |
| 29 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame^] | 30 | static struct version_info { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 31 | int version; |
| 32 | int last_comp_version; |
| 33 | int hdr_size; |
| 34 | int flags; |
| 35 | } version_table[] = { |
| 36 | {1, 1, BPH_V1_SIZE, |
| 37 | FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS}, |
| 38 | {2, 1, BPH_V2_SIZE, |
| 39 | FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID}, |
| 40 | {3, 1, BPH_V3_SIZE, |
| 41 | FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE}, |
| 42 | {0x10, 0x10, BPH_V3_SIZE, |
| 43 | FTF_BOOTCPUID|FTF_STRTABSIZE}, |
| 44 | }; |
| 45 | |
| 46 | struct emitter { |
| 47 | void (*cell)(void *, cell_t); |
| 48 | void (*string)(void *, char *, int); |
| 49 | void (*align)(void *, int); |
| 50 | void (*data)(void *, struct data); |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 51 | void (*beginnode)(void *, char *); |
| 52 | void (*endnode)(void *, char *); |
| 53 | void (*property)(void *, char *); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static void bin_emit_cell(void *e, cell_t val) |
| 57 | { |
| 58 | struct data *dtbuf = e; |
| 59 | |
| 60 | *dtbuf = data_append_cell(*dtbuf, val); |
| 61 | } |
| 62 | |
| 63 | static void bin_emit_string(void *e, char *str, int len) |
| 64 | { |
| 65 | struct data *dtbuf = e; |
| 66 | |
| 67 | if (len == 0) |
| 68 | len = strlen(str); |
| 69 | |
| 70 | *dtbuf = data_append_data(*dtbuf, str, len); |
| 71 | *dtbuf = data_append_byte(*dtbuf, '\0'); |
| 72 | } |
| 73 | |
| 74 | static void bin_emit_align(void *e, int a) |
| 75 | { |
| 76 | struct data *dtbuf = e; |
| 77 | |
| 78 | *dtbuf = data_append_align(*dtbuf, a); |
| 79 | } |
| 80 | |
| 81 | static void bin_emit_data(void *e, struct data d) |
| 82 | { |
| 83 | struct data *dtbuf = e; |
| 84 | |
| 85 | *dtbuf = data_append_data(*dtbuf, d.val, d.len); |
| 86 | } |
| 87 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 88 | static void bin_emit_beginnode(void *e, char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 89 | { |
| 90 | bin_emit_cell(e, OF_DT_BEGIN_NODE); |
| 91 | } |
| 92 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 93 | static void bin_emit_endnode(void *e, char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 94 | { |
| 95 | bin_emit_cell(e, OF_DT_END_NODE); |
| 96 | } |
| 97 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 98 | static void bin_emit_property(void *e, char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 99 | { |
| 100 | bin_emit_cell(e, OF_DT_PROP); |
| 101 | } |
| 102 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame^] | 103 | static struct emitter bin_emitter = { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 104 | .cell = bin_emit_cell, |
| 105 | .string = bin_emit_string, |
| 106 | .align = bin_emit_align, |
| 107 | .data = bin_emit_data, |
| 108 | .beginnode = bin_emit_beginnode, |
| 109 | .endnode = bin_emit_endnode, |
| 110 | .property = bin_emit_property, |
| 111 | }; |
| 112 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame^] | 113 | static void emit_label(FILE *f, char *prefix, char *label) |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 114 | { |
| 115 | fprintf(f, "\t.globl\t%s_%s\n", prefix, label); |
| 116 | fprintf(f, "%s_%s:\n", prefix, label); |
| 117 | fprintf(f, "_%s_%s:\n", prefix, label); |
| 118 | } |
| 119 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 120 | static void asm_emit_cell(void *e, cell_t val) |
| 121 | { |
| 122 | FILE *f = e; |
| 123 | |
| 124 | fprintf(f, "\t.long\t0x%x\n", be32_to_cpu(val)); |
| 125 | } |
| 126 | |
| 127 | static void asm_emit_string(void *e, char *str, int len) |
| 128 | { |
| 129 | FILE *f = e; |
| 130 | char c; |
| 131 | |
| 132 | if (len != 0) { |
| 133 | /* XXX: ewww */ |
| 134 | c = str[len]; |
| 135 | str[len] = '\0'; |
| 136 | } |
| 137 | |
| 138 | fprintf(f, "\t.string\t\"%s\"\n", str); |
| 139 | |
| 140 | if (len != 0) { |
| 141 | str[len] = c; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static void asm_emit_align(void *e, int a) |
| 146 | { |
| 147 | FILE *f = e; |
| 148 | |
| 149 | fprintf(f, "\t.balign\t%d\n", a); |
| 150 | } |
| 151 | |
| 152 | static void asm_emit_data(void *e, struct data d) |
| 153 | { |
| 154 | FILE *f = e; |
| 155 | int off = 0; |
| 156 | |
| 157 | while ((d.len - off) >= sizeof(u32)) { |
| 158 | fprintf(f, "\t.long\t0x%x\n", |
| 159 | be32_to_cpu(*((u32 *)(d.val+off)))); |
| 160 | off += sizeof(u32); |
| 161 | } |
| 162 | |
| 163 | if ((d.len - off) >= sizeof(u16)) { |
| 164 | fprintf(f, "\t.short\t0x%hx\n", |
| 165 | be16_to_cpu(*((u16 *)(d.val+off)))); |
| 166 | off += sizeof(u16); |
| 167 | } |
| 168 | |
| 169 | if ((d.len - off) >= 1) { |
| 170 | fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]); |
| 171 | off += 1; |
| 172 | } |
| 173 | |
| 174 | assert(off == d.len); |
| 175 | } |
| 176 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 177 | static void asm_emit_beginnode(void *e, char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 178 | { |
| 179 | FILE *f = e; |
| 180 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 181 | if (label) { |
| 182 | fprintf(f, "\t.globl\t%s\n", label); |
| 183 | fprintf(f, "%s:\n", label); |
| 184 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 185 | fprintf(f, "\t.long\tOF_DT_BEGIN_NODE\n"); |
| 186 | } |
| 187 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 188 | static void asm_emit_endnode(void *e, char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 189 | { |
| 190 | FILE *f = e; |
| 191 | |
| 192 | fprintf(f, "\t.long\tOF_DT_END_NODE\n"); |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 193 | if (label) { |
| 194 | fprintf(f, "\t.globl\t%s_end\n", label); |
| 195 | fprintf(f, "%s_end:\n", label); |
| 196 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 197 | } |
| 198 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 199 | static void asm_emit_property(void *e, char *label) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 200 | { |
| 201 | FILE *f = e; |
| 202 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 203 | if (label) { |
| 204 | fprintf(f, "\t.globl\t%s\n", label); |
| 205 | fprintf(f, "%s:\n", label); |
| 206 | } |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 207 | fprintf(f, "\t.long\tOF_DT_PROP\n"); |
| 208 | } |
| 209 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame^] | 210 | static struct emitter asm_emitter = { |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 211 | .cell = asm_emit_cell, |
| 212 | .string = asm_emit_string, |
| 213 | .align = asm_emit_align, |
| 214 | .data = asm_emit_data, |
| 215 | .beginnode = asm_emit_beginnode, |
| 216 | .endnode = asm_emit_endnode, |
| 217 | .property = asm_emit_property, |
| 218 | }; |
| 219 | |
| 220 | static int stringtable_insert(struct data *d, char *str) |
| 221 | { |
| 222 | int i; |
| 223 | |
| 224 | /* FIXME: do this more efficiently? */ |
| 225 | |
| 226 | for (i = 0; i < d->len; i++) { |
| 227 | if (streq(str, d->val + i)) |
| 228 | return i; |
| 229 | } |
| 230 | |
| 231 | *d = data_append_data(*d, str, strlen(str)+1); |
David Gibson | a6c6957 | 2005-07-11 17:09:42 +1000 | [diff] [blame] | 232 | return i; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static void flatten_tree(struct node *tree, struct emitter *emit, |
| 236 | void *etarget, struct data *strbuf, |
| 237 | struct version_info *vi) |
| 238 | { |
| 239 | struct property *prop; |
| 240 | struct node *child; |
| 241 | int seen_name_prop = 0; |
| 242 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 243 | emit->beginnode(etarget, tree->label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 244 | |
| 245 | if (vi->flags & FTF_FULLPATH) |
| 246 | emit->string(etarget, tree->fullpath, 0); |
| 247 | else |
| 248 | emit->string(etarget, tree->name, 0); |
| 249 | |
| 250 | emit->align(etarget, sizeof(cell_t)); |
| 251 | |
| 252 | for_each_property(tree, prop) { |
| 253 | int nameoff; |
| 254 | |
| 255 | if (streq(prop->name, "name")) |
| 256 | seen_name_prop = 1; |
| 257 | |
| 258 | nameoff = stringtable_insert(strbuf, prop->name); |
| 259 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 260 | emit->property(etarget, prop->label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 261 | emit->cell(etarget, prop->val.len); |
| 262 | emit->cell(etarget, nameoff); |
| 263 | |
| 264 | if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8)) |
| 265 | emit->align(etarget, 8); |
| 266 | |
| 267 | emit->data(etarget, prop->val); |
| 268 | emit->align(etarget, sizeof(cell_t)); |
| 269 | } |
| 270 | |
| 271 | if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) { |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 272 | emit->property(etarget, NULL); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 273 | emit->cell(etarget, tree->basenamelen+1); |
| 274 | emit->cell(etarget, stringtable_insert(strbuf, "name")); |
| 275 | |
| 276 | if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8)) |
| 277 | emit->align(etarget, 8); |
| 278 | |
| 279 | emit->string(etarget, tree->name, tree->basenamelen); |
David Gibson | 4191613 | 2005-08-25 15:39:09 +1000 | [diff] [blame] | 280 | emit->align(etarget, sizeof(cell_t)); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | for_each_child(tree, child) { |
| 284 | flatten_tree(child, emit, etarget, strbuf, vi); |
| 285 | } |
| 286 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 287 | emit->endnode(etarget, tree->label); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void make_bph(struct boot_param_header *bph, |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 291 | struct version_info *vi, |
| 292 | struct data *mem_reserve_data, |
| 293 | int dtsize, int strsize) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 294 | { |
David Gibson | 47f23de | 2005-07-11 17:19:26 +1000 | [diff] [blame] | 295 | int reserve_off; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 296 | int reservenum = mem_reserve_data->len / sizeof(struct reserve_entry); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 297 | int reservesize = (reservenum+1) * sizeof(struct reserve_entry); |
| 298 | |
| 299 | memset(bph, 0xff, sizeof(*bph)); |
| 300 | |
| 301 | bph->magic = cpu_to_be32(OF_DT_HEADER); |
David Gibson | dffc2a8 | 2005-08-25 14:47:20 +1000 | [diff] [blame] | 302 | bph->version = cpu_to_be32(vi->version); |
| 303 | bph->last_comp_version = cpu_to_be32(vi->last_comp_version); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 304 | |
David Gibson | 47f23de | 2005-07-11 17:19:26 +1000 | [diff] [blame] | 305 | /* Reserve map should be doubleword aligned */ |
| 306 | reserve_off = ALIGN(vi->hdr_size, 8); |
| 307 | |
| 308 | bph->off_mem_rsvmap = cpu_to_be32(reserve_off); |
| 309 | bph->off_dt_struct = cpu_to_be32(reserve_off + reservesize); |
| 310 | bph->off_dt_strings = cpu_to_be32(reserve_off + reservesize |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 311 | + dtsize); |
David Gibson | 47f23de | 2005-07-11 17:19:26 +1000 | [diff] [blame] | 312 | bph->totalsize = cpu_to_be32(reserve_off + reservesize |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 313 | + dtsize + strsize); |
| 314 | |
| 315 | if (vi->flags & FTF_BOOTCPUID) |
| 316 | bph->boot_cpuid_phys = 0xfeedbeef; |
| 317 | if (vi->flags & FTF_STRTABSIZE) |
| 318 | bph->size_dt_strings = cpu_to_be32(strsize); |
| 319 | } |
| 320 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 321 | void write_dt_blob(FILE *f, struct boot_info *bi, int version) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 322 | { |
| 323 | struct version_info *vi = NULL; |
| 324 | int i; |
| 325 | struct data dtbuf = empty_data; |
| 326 | struct data strbuf = empty_data; |
| 327 | struct boot_param_header bph; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 328 | struct reserve_entry termre = {.address = 0, .size = 0}; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 329 | |
| 330 | for (i = 0; i < ARRAY_SIZE(version_table); i++) { |
| 331 | if (version_table[i].version == version) |
| 332 | vi = &version_table[i]; |
| 333 | } |
| 334 | if (!vi) |
| 335 | die("Unknown device tree blob version %d\n", version); |
| 336 | |
| 337 | dtbuf = empty_data; |
| 338 | strbuf = empty_data; |
| 339 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 340 | flatten_tree(bi->dt, &bin_emitter, &dtbuf, &strbuf, vi); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 341 | bin_emit_cell(&dtbuf, OF_DT_END); |
| 342 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 343 | /* Make header */ |
| 344 | make_bph(&bph, vi, &bi->mem_reserve_data, dtbuf.len, strbuf.len); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 345 | |
David Gibson | 586606e | 2005-07-14 11:27:24 +1000 | [diff] [blame] | 346 | fwrite(&bph, vi->hdr_size, 1, f); |
| 347 | |
David Gibson | 47f23de | 2005-07-11 17:19:26 +1000 | [diff] [blame] | 348 | /* Align the reserve map to an 8 byte boundary */ |
| 349 | for (i = vi->hdr_size; i < be32_to_cpu(bph.off_mem_rsvmap); i++) |
| 350 | fputc(0, f); |
| 351 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 352 | /* |
| 353 | * Reserve map entries. |
| 354 | * Since the blob is relocatable, the address of the map is not |
| 355 | * determinable here, so no entry is made for the DT itself. |
| 356 | * Each entry is an (address, size) pair of u64 values. |
| 357 | * Always supply a zero-sized temination entry. |
| 358 | */ |
| 359 | fwrite(bi->mem_reserve_data.val, bi->mem_reserve_data.len, 1, f); |
| 360 | fwrite(&termre, sizeof(termre), 1, f); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 361 | |
| 362 | fwrite(dtbuf.val, dtbuf.len, 1, f); |
| 363 | fwrite(strbuf.val, strbuf.len, 1, f); |
| 364 | |
| 365 | if (ferror(f)) |
| 366 | die("Error writing device tree blob: %s\n", strerror(errno)); |
| 367 | |
| 368 | data_free(dtbuf); |
| 369 | data_free(strbuf); |
| 370 | } |
| 371 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame^] | 372 | static void dump_stringtable_asm(FILE *f, struct data strbuf) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 373 | { |
| 374 | char *p; |
| 375 | int len; |
| 376 | |
| 377 | p = strbuf.val; |
| 378 | |
| 379 | while (p < (strbuf.val + strbuf.len)) { |
| 380 | len = strlen(p); |
| 381 | fprintf(f, "\t.string \"%s\"\n", p); |
| 382 | p += len+1; |
| 383 | } |
| 384 | } |
| 385 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 386 | void write_dt_asm(FILE *f, struct boot_info *bi, int version) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 387 | { |
| 388 | struct version_info *vi = NULL; |
| 389 | int i; |
| 390 | struct data strbuf = empty_data; |
| 391 | char *symprefix = "dt"; |
| 392 | |
| 393 | for (i = 0; i < ARRAY_SIZE(version_table); i++) { |
| 394 | if (version_table[i].version == version) |
| 395 | vi = &version_table[i]; |
| 396 | } |
| 397 | if (!vi) |
| 398 | die("Unknown device tree blob version %d\n", version); |
| 399 | |
| 400 | fprintf(f, "/* autogenerated by dtc, do not edit */\n\n"); |
| 401 | fprintf(f, "#define OF_DT_HEADER 0x%x\n", OF_DT_HEADER); |
| 402 | fprintf(f, "#define OF_DT_BEGIN_NODE 0x%x\n", OF_DT_BEGIN_NODE); |
| 403 | fprintf(f, "#define OF_DT_END_NODE 0x%x\n", OF_DT_END_NODE); |
| 404 | fprintf(f, "#define OF_DT_PROP 0x%x\n", OF_DT_PROP); |
| 405 | fprintf(f, "#define OF_DT_END 0x%x\n", OF_DT_END); |
| 406 | fprintf(f, "\n"); |
| 407 | |
| 408 | emit_label(f, symprefix, "blob_start"); |
| 409 | emit_label(f, symprefix, "header"); |
| 410 | fprintf(f, "\t.long\tOF_DT_HEADER /* magic */\n"); |
| 411 | fprintf(f, "\t.long\t_%s_blob_end - _%s_blob_start /* totalsize */\n", |
| 412 | symprefix, symprefix); |
| 413 | fprintf(f, "\t.long\t_%s_struct_start - _%s_blob_start /* off_dt_struct */\n", |
| 414 | symprefix, symprefix); |
| 415 | fprintf(f, "\t.long\t_%s_strings_start - _%s_blob_start /* off_dt_strings */\n", |
| 416 | symprefix, symprefix); |
| 417 | fprintf(f, "\t.long\t_%s_reserve_map - _%s_blob_start /* off_dt_strings */\n", |
| 418 | symprefix, symprefix); |
| 419 | fprintf(f, "\t.long\t%d /* version */\n", vi->version); |
| 420 | fprintf(f, "\t.long\t%d /* last_comp_version */\n", |
| 421 | vi->last_comp_version); |
| 422 | |
| 423 | if (vi->flags & FTF_BOOTCPUID) |
| 424 | fprintf(f, "\t.long\t0xdeadbeef\t/*boot_cpuid_phys*/\n"); |
| 425 | |
| 426 | if (vi->flags & FTF_STRTABSIZE) |
| 427 | fprintf(f, "\t.long\t_%s_strings_end - _%s_strings_start\t/* size_dt_strings */\n", |
| 428 | symprefix, symprefix); |
| 429 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 430 | /* |
| 431 | * Reserve map entries. |
| 432 | * Align the reserve map to a doubleword boundary. |
| 433 | * Each entry is an (address, size) pair of u64 values. |
| 434 | * Since the ASM file variant can relocate and compute the address |
| 435 | * and size of the the device tree itself, and an entry for it. |
| 436 | * Always supply a zero-sized temination entry. |
| 437 | */ |
David Gibson | 47f23de | 2005-07-11 17:19:26 +1000 | [diff] [blame] | 438 | asm_emit_align(f, 8); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 439 | emit_label(f, symprefix, "reserve_map"); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 440 | fprintf(f, "\t.long\t0, _%s_blob_start\n", symprefix); |
| 441 | fprintf(f, "\t.long\t0, _%s_blob_end - _%s_blob_start\n", |
| 442 | symprefix, symprefix); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 443 | |
| 444 | if (bi->mem_reserve_data.len > 0) { |
| 445 | fprintf(f, "/* Memory reserve map from source file */\n"); |
| 446 | asm_emit_data(f, bi->mem_reserve_data); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 447 | } |
| 448 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 449 | fprintf(f, "\t.llong\t0\n"); |
| 450 | fprintf(f, "\t.llong\t0\n"); |
| 451 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 452 | emit_label(f, symprefix, "struct_start"); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 453 | flatten_tree(bi->dt, &asm_emitter, f, &strbuf, vi); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 454 | fprintf(f, "\t.long\tOF_DT_END\n"); |
| 455 | emit_label(f, symprefix, "struct_end"); |
| 456 | |
| 457 | emit_label(f, symprefix, "strings_start"); |
| 458 | dump_stringtable_asm(f, strbuf); |
| 459 | emit_label(f, symprefix, "strings_end"); |
| 460 | |
| 461 | emit_label(f, symprefix, "blob_end"); |
| 462 | |
| 463 | data_free(strbuf); |
| 464 | } |
| 465 | |
| 466 | struct inbuf { |
| 467 | char *base, *limit, *ptr; |
| 468 | }; |
| 469 | |
| 470 | static void inbuf_init(struct inbuf *inb, void *base, void *limit) |
| 471 | { |
| 472 | inb->base = base; |
| 473 | inb->limit = limit; |
| 474 | inb->ptr = inb->base; |
| 475 | } |
| 476 | |
| 477 | static void flat_read_chunk(struct inbuf *inb, void *p, int len) |
| 478 | { |
| 479 | if ((inb->ptr + len) > inb->limit) |
| 480 | die("Premature end of data parsing flat device tree\n"); |
| 481 | |
| 482 | memcpy(p, inb->ptr, len); |
| 483 | |
| 484 | inb->ptr += len; |
| 485 | } |
| 486 | |
| 487 | static u32 flat_read_word(struct inbuf *inb) |
| 488 | { |
| 489 | u32 val; |
| 490 | |
| 491 | assert(((inb->ptr - inb->base) % sizeof(val)) == 0); |
| 492 | |
| 493 | flat_read_chunk(inb, &val, sizeof(val)); |
| 494 | |
| 495 | return be32_to_cpu(val); |
| 496 | } |
| 497 | |
| 498 | static void flat_realign(struct inbuf *inb, int align) |
| 499 | { |
| 500 | int off = inb->ptr - inb->base; |
| 501 | |
| 502 | inb->ptr = inb->base + ALIGN(off, align); |
| 503 | if (inb->ptr > inb->limit) |
| 504 | die("Premature end of data parsing flat device tree\n"); |
| 505 | } |
| 506 | |
| 507 | static char *flat_read_string(struct inbuf *inb) |
| 508 | { |
| 509 | int len = 0; |
| 510 | char *p = inb->ptr; |
| 511 | char *str; |
| 512 | |
| 513 | do { |
| 514 | if (p >= inb->limit) |
| 515 | die("Premature end of data parsing flat device tree\n"); |
| 516 | len++; |
| 517 | } while ((*p++) != '\0'); |
| 518 | |
| 519 | str = strdup(inb->ptr); |
| 520 | |
| 521 | inb->ptr += len; |
| 522 | |
| 523 | flat_realign(inb, sizeof(u32)); |
| 524 | |
| 525 | return str; |
| 526 | } |
| 527 | |
| 528 | static struct data flat_read_data(struct inbuf *inb, int len) |
| 529 | { |
| 530 | struct data d = empty_data; |
| 531 | |
| 532 | if (len == 0) |
| 533 | return empty_data; |
| 534 | |
| 535 | d = data_grow_for(d, len); |
| 536 | d.len = len; |
| 537 | |
| 538 | flat_read_chunk(inb, d.val, len); |
| 539 | |
| 540 | flat_realign(inb, sizeof(u32)); |
| 541 | |
| 542 | return d; |
| 543 | } |
| 544 | |
| 545 | static char *flat_read_stringtable(struct inbuf *inb, int offset) |
| 546 | { |
| 547 | char *p; |
| 548 | |
| 549 | p = inb->base + offset; |
| 550 | while (1) { |
David Gibson | 4ddf7c0 | 2005-08-19 16:11:11 +1000 | [diff] [blame] | 551 | if (p >= inb->limit || p < inb->base) |
David Gibson | 7ee3ffd | 2005-07-11 16:45:57 +1000 | [diff] [blame] | 552 | die("String offset %d overruns string table\n", |
| 553 | offset); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 554 | |
| 555 | if (*p == '\0') |
| 556 | break; |
| 557 | |
| 558 | p++; |
| 559 | } |
| 560 | |
| 561 | return strdup(inb->base + offset); |
| 562 | } |
| 563 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame^] | 564 | static struct property *flat_read_property(struct inbuf *dtbuf, |
| 565 | struct inbuf *strbuf, int flags) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 566 | { |
| 567 | u32 proplen, stroff; |
| 568 | char *name; |
| 569 | struct data val; |
| 570 | |
| 571 | proplen = flat_read_word(dtbuf); |
| 572 | stroff = flat_read_word(dtbuf); |
| 573 | |
| 574 | name = flat_read_stringtable(strbuf, stroff); |
| 575 | |
| 576 | if ((flags & FTF_VARALIGN) && (proplen >= 8)) |
| 577 | flat_realign(dtbuf, 8); |
| 578 | |
| 579 | val = flat_read_data(dtbuf, proplen); |
| 580 | |
David Gibson | 4102d84 | 2005-06-16 14:36:37 +1000 | [diff] [blame] | 581 | return build_property(name, val, NULL); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 582 | } |
| 583 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 584 | |
| 585 | static struct data flat_read_mem_reserve(struct inbuf *inb) |
| 586 | { |
| 587 | char *p; |
| 588 | int len = 0; |
| 589 | int done = 0; |
| 590 | cell_t cells[4]; |
| 591 | struct data d; |
| 592 | |
| 593 | d = empty_data; |
| 594 | |
| 595 | /* |
| 596 | * Each entry is a pair of u64 (addr, size) values for 4 cell_t's. |
| 597 | * List terminates at an entry with size equal to zero. |
| 598 | * |
| 599 | * First pass, count entries. |
| 600 | */ |
| 601 | p = inb->ptr; |
| 602 | do { |
| 603 | flat_read_chunk(inb, &cells[0], 4 * sizeof(cell_t)); |
| 604 | if (cells[2] == 0 && cells[3] == 0) { |
| 605 | done = 1; |
| 606 | } else { |
| 607 | ++len; |
| 608 | } |
| 609 | } while (!done); |
| 610 | |
| 611 | /* |
| 612 | * Back up for pass two, reading the whole data value. |
| 613 | */ |
| 614 | inb->ptr = p; |
| 615 | d = flat_read_data(inb, len * 4 * sizeof(cell_t)); |
| 616 | |
| 617 | return d; |
| 618 | } |
| 619 | |
| 620 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 621 | static char *nodename_from_path(char *ppath, char *cpath) |
| 622 | { |
| 623 | char *lslash; |
| 624 | int plen; |
| 625 | |
| 626 | lslash = strrchr(cpath, '/'); |
| 627 | if (! lslash) |
| 628 | return NULL; |
| 629 | |
| 630 | plen = lslash - cpath; |
| 631 | |
| 632 | if (streq(cpath, "/") && streq(ppath, "")) |
| 633 | return ""; |
| 634 | |
| 635 | if ((plen == 0) && streq(ppath, "/")) |
| 636 | return strdup(lslash+1); |
| 637 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 638 | if (! strneq(ppath, cpath, plen)) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 639 | return NULL; |
| 640 | |
| 641 | return strdup(lslash+1); |
| 642 | } |
| 643 | |
| 644 | static const char PROPCHAR[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,._+*#?-"; |
| 645 | static const char UNITCHAR[] = "0123456789abcdef,"; |
| 646 | |
| 647 | static int check_node_name(char *name) |
| 648 | { |
| 649 | char *atpos; |
| 650 | int basenamelen; |
| 651 | |
| 652 | atpos = strrchr(name, '@'); |
| 653 | |
| 654 | if (atpos) |
| 655 | basenamelen = atpos - name; |
| 656 | else |
| 657 | basenamelen = strlen(name); |
| 658 | |
| 659 | if (strspn(name, PROPCHAR) < basenamelen) |
| 660 | return -1; |
| 661 | |
| 662 | if (atpos |
| 663 | && ((basenamelen + 1 + strspn(atpos+1, UNITCHAR)) < strlen(name))) |
| 664 | return -1; |
| 665 | |
| 666 | return basenamelen; |
| 667 | } |
| 668 | |
| 669 | static struct node *unflatten_tree(struct inbuf *dtbuf, |
| 670 | struct inbuf *strbuf, |
| 671 | char *parent_path, int flags) |
| 672 | { |
| 673 | struct node *node; |
| 674 | u32 val; |
| 675 | |
| 676 | node = build_node(NULL, NULL); |
| 677 | |
| 678 | if (flags & FTF_FULLPATH) { |
| 679 | node->fullpath = flat_read_string(dtbuf); |
| 680 | node->name = nodename_from_path(parent_path, node->fullpath); |
| 681 | |
| 682 | if (! node->name) |
| 683 | die("Path \"%s\" is not valid as a child of \"%s\"\n", |
| 684 | node->fullpath, parent_path); |
| 685 | } else { |
| 686 | node->name = flat_read_string(dtbuf); |
| 687 | node->fullpath = join_path(parent_path, node->name); |
| 688 | } |
| 689 | |
| 690 | node->basenamelen = check_node_name(node->name); |
| 691 | if (node->basenamelen < 0) { |
| 692 | fprintf(stderr, "Warning \"%s\" has incorrect format\n", node->name); |
| 693 | } |
| 694 | |
| 695 | do { |
| 696 | struct property *prop; |
| 697 | struct node *child; |
| 698 | |
| 699 | val = flat_read_word(dtbuf); |
| 700 | switch (val) { |
| 701 | case OF_DT_PROP: |
| 702 | prop = flat_read_property(dtbuf, strbuf, flags); |
| 703 | add_property(node, prop); |
| 704 | break; |
| 705 | |
| 706 | case OF_DT_BEGIN_NODE: |
| 707 | child = unflatten_tree(dtbuf,strbuf, node->fullpath, |
| 708 | flags); |
| 709 | add_child(node, child); |
| 710 | break; |
| 711 | |
| 712 | case OF_DT_END_NODE: |
| 713 | break; |
| 714 | |
| 715 | case OF_DT_END: |
| 716 | die("Premature OF_DT_END in device tree blob\n"); |
| 717 | break; |
| 718 | |
| 719 | default: |
| 720 | die("Invalid opcode word %08x in device tree blob\n", |
| 721 | val); |
| 722 | } |
| 723 | } while (val != OF_DT_END_NODE); |
| 724 | |
| 725 | return node; |
| 726 | } |
| 727 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 728 | |
| 729 | struct boot_info *dt_from_blob(FILE *f) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 730 | { |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 731 | u32 magic, totalsize, version, size_str; |
| 732 | u32 off_dt, off_str, off_mem_rsvmap; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 733 | int rc; |
| 734 | char *blob; |
| 735 | struct boot_param_header *bph; |
| 736 | char *p; |
| 737 | struct inbuf dtbuf, strbuf; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 738 | struct inbuf memresvbuf; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 739 | int sizeleft; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 740 | struct data mem_reserve_data; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 741 | struct node *tree; |
| 742 | u32 val; |
| 743 | int flags = 0; |
| 744 | |
| 745 | rc = fread(&magic, sizeof(magic), 1, f); |
| 746 | if (ferror(f)) |
| 747 | die("Error reading DT blob magic number: %s\n", |
| 748 | strerror(errno)); |
| 749 | if (rc < 1) { |
| 750 | if (feof(f)) |
| 751 | die("EOF reading DT blob magic number\n"); |
| 752 | else |
| 753 | die("Mysterious short read reading magic number\n"); |
| 754 | } |
| 755 | |
| 756 | magic = be32_to_cpu(magic); |
| 757 | if (magic != OF_DT_HEADER) |
| 758 | die("Blob has incorrect magic number\n"); |
| 759 | |
| 760 | rc = fread(&totalsize, sizeof(totalsize), 1, f); |
| 761 | if (ferror(f)) |
| 762 | die("Error reading DT blob size: %s\n", strerror(errno)); |
| 763 | if (rc < 1) { |
| 764 | if (feof(f)) |
| 765 | die("EOF reading DT blob size\n"); |
| 766 | else |
| 767 | die("Mysterious short read reading blob size\n"); |
| 768 | } |
| 769 | |
| 770 | totalsize = be32_to_cpu(totalsize); |
| 771 | if (totalsize < BPH_V1_SIZE) |
| 772 | die("DT blob size (%d) is too small\n", totalsize); |
| 773 | |
| 774 | blob = xmalloc(totalsize); |
| 775 | |
| 776 | bph = (struct boot_param_header *)blob; |
| 777 | bph->magic = cpu_to_be32(magic); |
| 778 | bph->totalsize = cpu_to_be32(totalsize); |
| 779 | |
| 780 | sizeleft = totalsize - sizeof(magic) - sizeof(totalsize); |
| 781 | p = blob + sizeof(magic) + sizeof(totalsize); |
| 782 | |
| 783 | while (sizeleft) { |
| 784 | if (feof(f)) |
| 785 | die("EOF before reading %d bytes of DT blob\n", |
| 786 | totalsize); |
| 787 | |
| 788 | rc = fread(p, 1, sizeleft, f); |
| 789 | if (ferror(f)) |
| 790 | die("Error reading DT blob: %s\n", |
| 791 | strerror(errno)); |
| 792 | |
| 793 | sizeleft -= rc; |
| 794 | p += rc; |
| 795 | } |
| 796 | |
| 797 | off_dt = be32_to_cpu(bph->off_dt_struct); |
| 798 | off_str = be32_to_cpu(bph->off_dt_strings); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 799 | off_mem_rsvmap = be32_to_cpu(bph->off_mem_rsvmap); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 800 | version = be32_to_cpu(bph->version); |
| 801 | |
| 802 | fprintf(stderr, "\tmagic:\t\t\t0x%x\n", magic); |
| 803 | fprintf(stderr, "\ttotalsize:\t\t%d\n", totalsize); |
| 804 | fprintf(stderr, "\toff_dt_struct:\t\t0x%x\n", off_dt); |
| 805 | fprintf(stderr, "\toff_dt_strings:\t\t0x%x\n", off_str); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 806 | fprintf(stderr, "\toff_mem_rsvmap:\t\t0x%x\n", off_mem_rsvmap); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 807 | fprintf(stderr, "\tversion:\t\t0x%x\n", version ); |
| 808 | fprintf(stderr, "\tlast_comp_version:\t0x%x\n", |
| 809 | be32_to_cpu(bph->last_comp_version)); |
| 810 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 811 | if (off_mem_rsvmap >= totalsize) |
| 812 | die("Mem Reserve structure offset exceeds total size\n"); |
| 813 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 814 | if (off_dt >= totalsize) |
| 815 | die("DT structure offset exceeds total size\n"); |
| 816 | |
| 817 | if (off_str > totalsize) |
| 818 | die("String table offset exceeds total size\n"); |
| 819 | |
| 820 | if (version >= 2) |
| 821 | fprintf(stderr, "\tboot_cpuid_phys:\t0x%x\n", |
| 822 | be32_to_cpu(bph->boot_cpuid_phys)); |
| 823 | |
| 824 | if (version >= 3) { |
| 825 | size_str = be32_to_cpu(bph->size_dt_strings); |
| 826 | fprintf(stderr, "\tsize_dt_strings:\t%d\n", size_str); |
| 827 | if (off_str+size_str > totalsize) |
| 828 | die("String table extends past total size\n"); |
| 829 | } |
| 830 | |
| 831 | if (version < 0x10) { |
| 832 | flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN; |
| 833 | } |
| 834 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 835 | inbuf_init(&memresvbuf, |
| 836 | blob + off_mem_rsvmap, blob + totalsize); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 837 | inbuf_init(&dtbuf, blob + off_dt, blob + totalsize); |
| 838 | inbuf_init(&strbuf, blob + off_str, blob + totalsize); |
| 839 | |
| 840 | if (version >= 3) |
| 841 | strbuf.limit = strbuf.base + size_str; |
| 842 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 843 | mem_reserve_data = flat_read_mem_reserve(&memresvbuf); |
| 844 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 845 | val = flat_read_word(&dtbuf); |
| 846 | |
| 847 | if (val != OF_DT_BEGIN_NODE) |
David Gibson | 4191613 | 2005-08-25 15:39:09 +1000 | [diff] [blame] | 848 | die("Device tree blob doesn't begin with OF_DT_BEGIN_NODE (begins with 0x%08x)\n", val); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 849 | |
| 850 | tree = unflatten_tree(&dtbuf, &strbuf, "", flags); |
| 851 | |
| 852 | val = flat_read_word(&dtbuf); |
| 853 | if (val != OF_DT_END) |
| 854 | die("Device tree blob doesn't end with OF_DT_END\n"); |
| 855 | |
| 856 | free(blob); |
| 857 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 858 | return build_boot_info(mem_reserve_data, tree); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 859 | } |