blob: 49514f8ddc85324d9ddf4d1da6ebabac2f0dc3cb [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * 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.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
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
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
20
21#include "dtc.h"
David Gibsona742aad2008-05-16 13:22:09 +100022#include "srcpos.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100023
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
David Gibson46c88df2007-03-14 11:02:40 +110029#define FTF_STRUCTSIZE 0x20
Milton Millerce243222007-06-09 23:21:31 -050030#define FTF_NOPS 0x40
David Gibsonfc14dad2005-06-08 17:18:34 +100031
David Gibson230f2532005-08-29 12:48:02 +100032static struct version_info {
David Gibsonfc14dad2005-06-08 17:18:34 +100033 int version;
34 int last_comp_version;
35 int hdr_size;
36 int flags;
37} version_table[] = {
David Gibsonfb7c7ac2007-09-26 13:11:05 +100038 {1, 1, FDT_V1_SIZE,
David Gibsonfc14dad2005-06-08 17:18:34 +100039 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS},
David Gibsonfb7c7ac2007-09-26 13:11:05 +100040 {2, 1, FDT_V2_SIZE,
David Gibsonfc14dad2005-06-08 17:18:34 +100041 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID},
David Gibsonfb7c7ac2007-09-26 13:11:05 +100042 {3, 1, FDT_V3_SIZE,
David Gibsonfc14dad2005-06-08 17:18:34 +100043 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE},
David Gibsonfb7c7ac2007-09-26 13:11:05 +100044 {16, 16, FDT_V3_SIZE,
Milton Millerce243222007-06-09 23:21:31 -050045 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS},
David Gibsonfb7c7ac2007-09-26 13:11:05 +100046 {17, 16, FDT_V17_SIZE,
Milton Millerce243222007-06-09 23:21:31 -050047 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
David Gibsonfc14dad2005-06-08 17:18:34 +100048};
49
50struct emitter {
51 void (*cell)(void *, cell_t);
52 void (*string)(void *, char *, int);
53 void (*align)(void *, int);
54 void (*data)(void *, struct data);
David Gibson92cb9a22007-12-04 14:26:15 +110055 void (*beginnode)(void *, const char *);
56 void (*endnode)(void *, const char *);
57 void (*property)(void *, const char *);
David Gibsonfc14dad2005-06-08 17:18:34 +100058};
59
60static void bin_emit_cell(void *e, cell_t val)
61{
62 struct data *dtbuf = e;
63
64 *dtbuf = data_append_cell(*dtbuf, val);
65}
66
67static void bin_emit_string(void *e, char *str, int len)
68{
69 struct data *dtbuf = e;
70
71 if (len == 0)
72 len = strlen(str);
73
74 *dtbuf = data_append_data(*dtbuf, str, len);
75 *dtbuf = data_append_byte(*dtbuf, '\0');
76}
77
78static void bin_emit_align(void *e, int a)
79{
80 struct data *dtbuf = e;
81
82 *dtbuf = data_append_align(*dtbuf, a);
83}
84
85static void bin_emit_data(void *e, struct data d)
86{
87 struct data *dtbuf = e;
88
89 *dtbuf = data_append_data(*dtbuf, d.val, d.len);
90}
91
David Gibson92cb9a22007-12-04 14:26:15 +110092static void bin_emit_beginnode(void *e, const char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +100093{
David Gibsonfb7c7ac2007-09-26 13:11:05 +100094 bin_emit_cell(e, FDT_BEGIN_NODE);
David Gibsonfc14dad2005-06-08 17:18:34 +100095}
96
David Gibson92cb9a22007-12-04 14:26:15 +110097static void bin_emit_endnode(void *e, const char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +100098{
David Gibsonfb7c7ac2007-09-26 13:11:05 +100099 bin_emit_cell(e, FDT_END_NODE);
David Gibsonfc14dad2005-06-08 17:18:34 +1000100}
101
David Gibson92cb9a22007-12-04 14:26:15 +1100102static void bin_emit_property(void *e, const char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +1000103{
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000104 bin_emit_cell(e, FDT_PROP);
David Gibsonfc14dad2005-06-08 17:18:34 +1000105}
106
David Gibson230f2532005-08-29 12:48:02 +1000107static struct emitter bin_emitter = {
David Gibsonfc14dad2005-06-08 17:18:34 +1000108 .cell = bin_emit_cell,
109 .string = bin_emit_string,
110 .align = bin_emit_align,
111 .data = bin_emit_data,
112 .beginnode = bin_emit_beginnode,
113 .endnode = bin_emit_endnode,
David Gibson63dc9c72007-09-18 11:44:04 +1000114 .property = bin_emit_property,
David Gibsonfc14dad2005-06-08 17:18:34 +1000115};
116
David Gibson92cb9a22007-12-04 14:26:15 +1100117static void emit_label(FILE *f, const char *prefix, const char *label)
David Gibson4102d842005-06-16 14:36:37 +1000118{
119 fprintf(f, "\t.globl\t%s_%s\n", prefix, label);
120 fprintf(f, "%s_%s:\n", prefix, label);
121 fprintf(f, "_%s_%s:\n", prefix, label);
122}
123
David Gibson92cb9a22007-12-04 14:26:15 +1100124static void emit_offset_label(FILE *f, const char *label, int offset)
Milton Miller6a99b132007-07-07 01:18:51 -0500125{
126 fprintf(f, "\t.globl\t%s\n", label);
127 fprintf(f, "%s\t= . + %d\n", label, offset);
128}
129
David Gibsonfc14dad2005-06-08 17:18:34 +1000130static void asm_emit_cell(void *e, cell_t val)
131{
132 FILE *f = e;
133
Mark A. Greer7a9f6632006-03-15 18:59:24 -0700134 fprintf(f, "\t.long\t0x%x\n", val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000135}
136
137static void asm_emit_string(void *e, char *str, int len)
138{
139 FILE *f = e;
Jon Loeliger2d50f8f2007-10-18 09:46:52 -0500140 char c = 0;
David Gibsonfc14dad2005-06-08 17:18:34 +1000141
142 if (len != 0) {
143 /* XXX: ewww */
144 c = str[len];
145 str[len] = '\0';
146 }
David Gibson63dc9c72007-09-18 11:44:04 +1000147
David Gibsonfc14dad2005-06-08 17:18:34 +1000148 fprintf(f, "\t.string\t\"%s\"\n", str);
149
150 if (len != 0) {
151 str[len] = c;
152 }
153}
154
155static void asm_emit_align(void *e, int a)
156{
157 FILE *f = e;
158
159 fprintf(f, "\t.balign\t%d\n", a);
160}
161
162static void asm_emit_data(void *e, struct data d)
163{
164 FILE *f = e;
165 int off = 0;
David Gibson5ac97df2008-02-28 20:58:28 +1100166 struct marker *m = d.markers;
Milton Miller6a99b132007-07-07 01:18:51 -0500167
David Gibson5ac97df2008-02-28 20:58:28 +1100168 for_each_marker_of_type(m, LABEL)
169 emit_offset_label(f, m->ref, m->offset);
David Gibsonfc14dad2005-06-08 17:18:34 +1000170
David Gibson53359012008-06-25 13:53:07 +1000171 while ((d.len - off) >= sizeof(uint32_t)) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000172 fprintf(f, "\t.long\t0x%x\n",
David Gibson53359012008-06-25 13:53:07 +1000173 be32_to_cpu(*((uint32_t *)(d.val+off))));
174 off += sizeof(uint32_t);
David Gibsonfc14dad2005-06-08 17:18:34 +1000175 }
176
David Gibson53359012008-06-25 13:53:07 +1000177 if ((d.len - off) >= sizeof(uint16_t)) {
David Gibson63dc9c72007-09-18 11:44:04 +1000178 fprintf(f, "\t.short\t0x%hx\n",
David Gibson53359012008-06-25 13:53:07 +1000179 be16_to_cpu(*((uint16_t *)(d.val+off))));
180 off += sizeof(uint16_t);
David Gibsonfc14dad2005-06-08 17:18:34 +1000181 }
182
183 if ((d.len - off) >= 1) {
184 fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]);
185 off += 1;
186 }
187
188 assert(off == d.len);
189}
190
David Gibson92cb9a22007-12-04 14:26:15 +1100191static void asm_emit_beginnode(void *e, const char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +1000192{
193 FILE *f = e;
194
David Gibson4102d842005-06-16 14:36:37 +1000195 if (label) {
196 fprintf(f, "\t.globl\t%s\n", label);
197 fprintf(f, "%s:\n", label);
198 }
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000199 fprintf(f, "\t.long\tFDT_BEGIN_NODE\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000200}
201
David Gibson92cb9a22007-12-04 14:26:15 +1100202static void asm_emit_endnode(void *e, const char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +1000203{
204 FILE *f = e;
205
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000206 fprintf(f, "\t.long\tFDT_END_NODE\n");
David Gibson4102d842005-06-16 14:36:37 +1000207 if (label) {
208 fprintf(f, "\t.globl\t%s_end\n", label);
209 fprintf(f, "%s_end:\n", label);
210 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000211}
212
David Gibson92cb9a22007-12-04 14:26:15 +1100213static void asm_emit_property(void *e, const char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +1000214{
215 FILE *f = e;
216
David Gibson4102d842005-06-16 14:36:37 +1000217 if (label) {
218 fprintf(f, "\t.globl\t%s\n", label);
219 fprintf(f, "%s:\n", label);
220 }
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000221 fprintf(f, "\t.long\tFDT_PROP\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000222}
223
David Gibson230f2532005-08-29 12:48:02 +1000224static struct emitter asm_emitter = {
David Gibsonfc14dad2005-06-08 17:18:34 +1000225 .cell = asm_emit_cell,
226 .string = asm_emit_string,
227 .align = asm_emit_align,
228 .data = asm_emit_data,
229 .beginnode = asm_emit_beginnode,
230 .endnode = asm_emit_endnode,
David Gibson63dc9c72007-09-18 11:44:04 +1000231 .property = asm_emit_property,
David Gibsonfc14dad2005-06-08 17:18:34 +1000232};
233
David Gibson92cb9a22007-12-04 14:26:15 +1100234static int stringtable_insert(struct data *d, const char *str)
David Gibsonfc14dad2005-06-08 17:18:34 +1000235{
236 int i;
237
238 /* FIXME: do this more efficiently? */
239
240 for (i = 0; i < d->len; i++) {
241 if (streq(str, d->val + i))
242 return i;
243 }
244
245 *d = data_append_data(*d, str, strlen(str)+1);
David Gibsona6c69572005-07-11 17:09:42 +1000246 return i;
David Gibsonfc14dad2005-06-08 17:18:34 +1000247}
248
249static void flatten_tree(struct node *tree, struct emitter *emit,
250 void *etarget, struct data *strbuf,
251 struct version_info *vi)
252{
253 struct property *prop;
254 struct node *child;
255 int seen_name_prop = 0;
256
David Gibson4102d842005-06-16 14:36:37 +1000257 emit->beginnode(etarget, tree->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000258
259 if (vi->flags & FTF_FULLPATH)
260 emit->string(etarget, tree->fullpath, 0);
261 else
262 emit->string(etarget, tree->name, 0);
263
264 emit->align(etarget, sizeof(cell_t));
265
266 for_each_property(tree, prop) {
267 int nameoff;
268
269 if (streq(prop->name, "name"))
270 seen_name_prop = 1;
271
272 nameoff = stringtable_insert(strbuf, prop->name);
273
David Gibson4102d842005-06-16 14:36:37 +1000274 emit->property(etarget, prop->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000275 emit->cell(etarget, prop->val.len);
276 emit->cell(etarget, nameoff);
277
278 if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8))
279 emit->align(etarget, 8);
280
281 emit->data(etarget, prop->val);
282 emit->align(etarget, sizeof(cell_t));
283 }
284
285 if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) {
David Gibson4102d842005-06-16 14:36:37 +1000286 emit->property(etarget, NULL);
David Gibsonfc14dad2005-06-08 17:18:34 +1000287 emit->cell(etarget, tree->basenamelen+1);
288 emit->cell(etarget, stringtable_insert(strbuf, "name"));
289
290 if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8))
291 emit->align(etarget, 8);
292
293 emit->string(etarget, tree->name, tree->basenamelen);
David Gibson41916132005-08-25 15:39:09 +1000294 emit->align(etarget, sizeof(cell_t));
David Gibsonfc14dad2005-06-08 17:18:34 +1000295 }
296
297 for_each_child(tree, child) {
298 flatten_tree(child, emit, etarget, strbuf, vi);
299 }
300
David Gibson4102d842005-06-16 14:36:37 +1000301 emit->endnode(etarget, tree->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000302}
303
David Gibsonf040d952005-10-24 18:18:38 +1000304static struct data flatten_reserve_list(struct reserve_info *reservelist,
305 struct version_info *vi)
306{
307 struct reserve_info *re;
308 struct data d = empty_data;
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000309 static struct fdt_reserve_entry null_re = {0,0};
Jerry Van Baren4384b232007-04-04 22:04:33 -0400310 int j;
David Gibsonf040d952005-10-24 18:18:38 +1000311
312 for (re = reservelist; re; re = re->next) {
313 d = data_append_re(d, &re->re);
314 }
Jerry Van Baren4384b232007-04-04 22:04:33 -0400315 /*
316 * Add additional reserved slots if the user asked for them.
317 */
318 for (j = 0; j < reservenum; j++) {
319 d = data_append_re(d, &null_re);
320 }
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400321
David Gibsonf040d952005-10-24 18:18:38 +1000322 return d;
323}
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400324
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000325static void make_fdt_header(struct fdt_header *fdt,
326 struct version_info *vi,
327 int reservesize, int dtsize, int strsize,
328 int boot_cpuid_phys)
David Gibsonfc14dad2005-06-08 17:18:34 +1000329{
David Gibson47f23de2005-07-11 17:19:26 +1000330 int reserve_off;
David Gibsonf040d952005-10-24 18:18:38 +1000331
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000332 reservesize += sizeof(struct fdt_reserve_entry);
David Gibsonfc14dad2005-06-08 17:18:34 +1000333
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000334 memset(fdt, 0xff, sizeof(*fdt));
David Gibsonfc14dad2005-06-08 17:18:34 +1000335
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000336 fdt->magic = cpu_to_be32(FDT_MAGIC);
337 fdt->version = cpu_to_be32(vi->version);
338 fdt->last_comp_version = cpu_to_be32(vi->last_comp_version);
David Gibsonfc14dad2005-06-08 17:18:34 +1000339
David Gibson47f23de2005-07-11 17:19:26 +1000340 /* Reserve map should be doubleword aligned */
341 reserve_off = ALIGN(vi->hdr_size, 8);
342
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000343 fdt->off_mem_rsvmap = cpu_to_be32(reserve_off);
344 fdt->off_dt_struct = cpu_to_be32(reserve_off + reservesize);
345 fdt->off_dt_strings = cpu_to_be32(reserve_off + reservesize
David Gibsonfc14dad2005-06-08 17:18:34 +1000346 + dtsize);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000347 fdt->totalsize = cpu_to_be32(reserve_off + reservesize + dtsize + strsize);
Jerry Van Baren4384b232007-04-04 22:04:33 -0400348
David Gibsonfc14dad2005-06-08 17:18:34 +1000349 if (vi->flags & FTF_BOOTCPUID)
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000350 fdt->boot_cpuid_phys = cpu_to_be32(boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000351 if (vi->flags & FTF_STRTABSIZE)
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000352 fdt->size_dt_strings = cpu_to_be32(strsize);
David Gibson46c88df2007-03-14 11:02:40 +1100353 if (vi->flags & FTF_STRUCTSIZE)
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000354 fdt->size_dt_struct = cpu_to_be32(dtsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000355}
356
David Gibson548767f2008-05-16 13:22:57 +1000357void dt_to_blob(FILE *f, struct boot_info *bi, int version)
David Gibsonfc14dad2005-06-08 17:18:34 +1000358{
359 struct version_info *vi = NULL;
360 int i;
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400361 struct data blob = empty_data;
362 struct data reservebuf = empty_data;
363 struct data dtbuf = empty_data;
364 struct data strbuf = empty_data;
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000365 struct fdt_header fdt;
Kumar Gala2b7dc8d2007-11-28 10:21:12 -0600366 int padlen = 0;
David Gibsonfc14dad2005-06-08 17:18:34 +1000367
368 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
369 if (version_table[i].version == version)
370 vi = &version_table[i];
371 }
372 if (!vi)
373 die("Unknown device tree blob version %d\n", version);
374
David Gibsonf0517db2005-07-15 17:14:24 +1000375 flatten_tree(bi->dt, &bin_emitter, &dtbuf, &strbuf, vi);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000376 bin_emit_cell(&dtbuf, FDT_END);
David Gibsonfc14dad2005-06-08 17:18:34 +1000377
David Gibsonf040d952005-10-24 18:18:38 +1000378 reservebuf = flatten_reserve_list(bi->reservelist, vi);
379
David Gibsonf0517db2005-07-15 17:14:24 +1000380 /* Make header */
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000381 make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
David Gibson548767f2008-05-16 13:22:57 +1000382 bi->boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000383
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400384 /*
Jerry Van Baren7ea144f2007-04-19 22:22:35 -0400385 * If the user asked for more space than is used, adjust the totalsize.
386 */
Kumar Gala2b7dc8d2007-11-28 10:21:12 -0600387 if (minsize > 0) {
388 padlen = minsize - be32_to_cpu(fdt.totalsize);
389 if ((padlen < 0) && (quiet < 1))
Jerry Van Baren7ea144f2007-04-19 22:22:35 -0400390 fprintf(stderr,
391 "Warning: blob size %d >= minimum size %d\n",
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000392 be32_to_cpu(fdt.totalsize), minsize);
Jerry Van Baren7ea144f2007-04-19 22:22:35 -0400393 }
394
Kumar Gala2b7dc8d2007-11-28 10:21:12 -0600395 if (padsize > 0)
396 padlen = padsize;
397
Kumar Gala80c72a82007-12-04 17:36:08 -0600398 if (padlen > 0) {
399 int tsize = be32_to_cpu(fdt.totalsize);
400 tsize += padlen;
401 fdt.totalsize = cpu_to_be32(tsize);
402 }
403
Jerry Van Baren7ea144f2007-04-19 22:22:35 -0400404 /*
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400405 * Assemble the blob: start with the header, add with alignment
406 * the reserve buffer, add the reserve map terminating zeroes,
407 * the device tree itself, and finally the strings.
408 */
David Gibsona266e5c2008-02-28 20:55:37 +1100409 blob = data_append_data(blob, &fdt, vi->hdr_size);
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400410 blob = data_append_align(blob, 8);
411 blob = data_merge(blob, reservebuf);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000412 blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400413 blob = data_merge(blob, dtbuf);
414 blob = data_merge(blob, strbuf);
David Gibson47f23de2005-07-11 17:19:26 +1000415
David Gibsonf0517db2005-07-15 17:14:24 +1000416 /*
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400417 * If the user asked for more space than is used, pad out the blob.
David Gibsonf0517db2005-07-15 17:14:24 +1000418 */
Kumar Gala80c72a82007-12-04 17:36:08 -0600419 if (padlen > 0)
Jerry Van Baren7ea144f2007-04-19 22:22:35 -0400420 blob = data_append_zeroes(blob, padlen);
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400421
422 fwrite(blob.val, blob.len, 1, f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000423
424 if (ferror(f))
425 die("Error writing device tree blob: %s\n", strerror(errno));
426
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400427 /*
428 * data_merge() frees the right-hand element so only the blob
429 * remains to be freed.
430 */
431 data_free(blob);
David Gibsonfc14dad2005-06-08 17:18:34 +1000432}
433
David Gibson230f2532005-08-29 12:48:02 +1000434static void dump_stringtable_asm(FILE *f, struct data strbuf)
David Gibsonfc14dad2005-06-08 17:18:34 +1000435{
David Gibson92cb9a22007-12-04 14:26:15 +1100436 const char *p;
David Gibsonfc14dad2005-06-08 17:18:34 +1000437 int len;
438
439 p = strbuf.val;
440
441 while (p < (strbuf.val + strbuf.len)) {
442 len = strlen(p);
443 fprintf(f, "\t.string \"%s\"\n", p);
444 p += len+1;
445 }
446}
447
David Gibson548767f2008-05-16 13:22:57 +1000448void dt_to_asm(FILE *f, struct boot_info *bi, int version)
David Gibsonfc14dad2005-06-08 17:18:34 +1000449{
450 struct version_info *vi = NULL;
451 int i;
452 struct data strbuf = empty_data;
David Gibsonf040d952005-10-24 18:18:38 +1000453 struct reserve_info *re;
David Gibson92cb9a22007-12-04 14:26:15 +1100454 const char *symprefix = "dt";
David Gibsonfc14dad2005-06-08 17:18:34 +1000455
456 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
457 if (version_table[i].version == version)
458 vi = &version_table[i];
459 }
460 if (!vi)
461 die("Unknown device tree blob version %d\n", version);
462
463 fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000464 fprintf(f, "#define FDT_MAGIC 0x%x\n", FDT_MAGIC);
465 fprintf(f, "#define FDT_BEGIN_NODE 0x%x\n", FDT_BEGIN_NODE);
466 fprintf(f, "#define FDT_END_NODE 0x%x\n", FDT_END_NODE);
467 fprintf(f, "#define FDT_PROP 0x%x\n", FDT_PROP);
468 fprintf(f, "#define FDT_END 0x%x\n", FDT_END);
David Gibsonfc14dad2005-06-08 17:18:34 +1000469 fprintf(f, "\n");
470
471 emit_label(f, symprefix, "blob_start");
472 emit_label(f, symprefix, "header");
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000473 fprintf(f, "\t.long\tFDT_MAGIC\t\t\t\t/* magic */\n");
Milton Miller7f456662007-07-07 01:18:53 -0500474 fprintf(f, "\t.long\t_%s_blob_abs_end - _%s_blob_start\t/* totalsize */\n",
David Gibsonfc14dad2005-06-08 17:18:34 +1000475 symprefix, symprefix);
Milton Miller7f456662007-07-07 01:18:53 -0500476 fprintf(f, "\t.long\t_%s_struct_start - _%s_blob_start\t/* off_dt_struct */\n",
David Gibsonfc14dad2005-06-08 17:18:34 +1000477 symprefix, symprefix);
Milton Miller7f456662007-07-07 01:18:53 -0500478 fprintf(f, "\t.long\t_%s_strings_start - _%s_blob_start\t/* off_dt_strings */\n",
David Gibsonfc14dad2005-06-08 17:18:34 +1000479 symprefix, symprefix);
Milton Miller7f456662007-07-07 01:18:53 -0500480 fprintf(f, "\t.long\t_%s_reserve_map - _%s_blob_start\t/* off_dt_strings */\n",
David Gibsonfc14dad2005-06-08 17:18:34 +1000481 symprefix, symprefix);
Milton Miller7f456662007-07-07 01:18:53 -0500482 fprintf(f, "\t.long\t%d\t\t\t\t\t/* version */\n", vi->version);
483 fprintf(f, "\t.long\t%d\t\t\t\t\t/* last_comp_version */\n",
David Gibsonfc14dad2005-06-08 17:18:34 +1000484 vi->last_comp_version);
485
486 if (vi->flags & FTF_BOOTCPUID)
Milton Miller7f456662007-07-07 01:18:53 -0500487 fprintf(f, "\t.long\t%i\t\t\t\t\t/* boot_cpuid_phys */\n",
David Gibson548767f2008-05-16 13:22:57 +1000488 bi->boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000489
490 if (vi->flags & FTF_STRTABSIZE)
491 fprintf(f, "\t.long\t_%s_strings_end - _%s_strings_start\t/* size_dt_strings */\n",
492 symprefix, symprefix);
493
Milton Miller81fda8a2007-07-07 01:18:47 -0500494 if (vi->flags & FTF_STRUCTSIZE)
495 fprintf(f, "\t.long\t_%s_struct_end - _%s_struct_start\t/* size_dt_struct */\n",
496 symprefix, symprefix);
497
David Gibsonf0517db2005-07-15 17:14:24 +1000498 /*
499 * Reserve map entries.
500 * Align the reserve map to a doubleword boundary.
501 * Each entry is an (address, size) pair of u64 values.
David Gibsonf0517db2005-07-15 17:14:24 +1000502 * Always supply a zero-sized temination entry.
503 */
David Gibson47f23de2005-07-11 17:19:26 +1000504 asm_emit_align(f, 8);
David Gibsonfc14dad2005-06-08 17:18:34 +1000505 emit_label(f, symprefix, "reserve_map");
David Gibsonf0517db2005-07-15 17:14:24 +1000506
David Gibsonf040d952005-10-24 18:18:38 +1000507 fprintf(f, "/* Memory reserve map from source file */\n");
Jon Loeliger05ae3d82006-04-19 11:58:45 -0500508
509 /*
510 * Use .long on high and low halfs of u64s to avoid .quad
511 * as it appears .quad isn't available in some assemblers.
512 */
David Gibsonf040d952005-10-24 18:18:38 +1000513 for (re = bi->reservelist; re; re = re->next) {
Milton Millerd4290332007-07-07 01:18:49 -0500514 if (re->label) {
515 fprintf(f, "\t.globl\t%s\n", re->label);
516 fprintf(f, "%s:\n", re->label);
517 }
Milton Miller445d55d2007-07-07 01:18:54 -0500518 fprintf(f, "\t.long\t0x%08x, 0x%08x\n",
Jon Loeliger05ae3d82006-04-19 11:58:45 -0500519 (unsigned int)(re->re.address >> 32),
520 (unsigned int)(re->re.address & 0xffffffff));
Milton Miller445d55d2007-07-07 01:18:54 -0500521 fprintf(f, "\t.long\t0x%08x, 0x%08x\n",
Jon Loeliger05ae3d82006-04-19 11:58:45 -0500522 (unsigned int)(re->re.size >> 32),
523 (unsigned int)(re->re.size & 0xffffffff));
David Gibsonfc14dad2005-06-08 17:18:34 +1000524 }
Jerry Van Barenca25e542007-04-17 18:14:41 -0400525 for (i = 0; i < reservenum; i++) {
526 fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
527 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000528
Jon Loeliger05ae3d82006-04-19 11:58:45 -0500529 fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000530
David Gibsonfc14dad2005-06-08 17:18:34 +1000531 emit_label(f, symprefix, "struct_start");
David Gibsonf0517db2005-07-15 17:14:24 +1000532 flatten_tree(bi->dt, &asm_emitter, f, &strbuf, vi);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000533 fprintf(f, "\t.long\tFDT_END\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000534 emit_label(f, symprefix, "struct_end");
535
536 emit_label(f, symprefix, "strings_start");
537 dump_stringtable_asm(f, strbuf);
538 emit_label(f, symprefix, "strings_end");
539
540 emit_label(f, symprefix, "blob_end");
541
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400542 /*
543 * If the user asked for more space than is used, pad it out.
544 */
545 if (minsize > 0) {
546 fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
547 minsize, symprefix, symprefix);
548 }
Kumar Gala2b7dc8d2007-11-28 10:21:12 -0600549 if (padsize > 0) {
550 fprintf(f, "\t.space\t%d, 0\n", padsize);
551 }
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400552 emit_label(f, symprefix, "blob_abs_end");
553
David Gibsonfc14dad2005-06-08 17:18:34 +1000554 data_free(strbuf);
555}
556
557struct inbuf {
558 char *base, *limit, *ptr;
559};
560
561static void inbuf_init(struct inbuf *inb, void *base, void *limit)
562{
563 inb->base = base;
564 inb->limit = limit;
565 inb->ptr = inb->base;
566}
567
568static void flat_read_chunk(struct inbuf *inb, void *p, int len)
569{
570 if ((inb->ptr + len) > inb->limit)
571 die("Premature end of data parsing flat device tree\n");
572
573 memcpy(p, inb->ptr, len);
574
575 inb->ptr += len;
576}
577
David Gibson53359012008-06-25 13:53:07 +1000578static uint32_t flat_read_word(struct inbuf *inb)
David Gibsonfc14dad2005-06-08 17:18:34 +1000579{
David Gibson53359012008-06-25 13:53:07 +1000580 uint32_t val;
David Gibsonfc14dad2005-06-08 17:18:34 +1000581
582 assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
583
584 flat_read_chunk(inb, &val, sizeof(val));
585
586 return be32_to_cpu(val);
587}
588
589static void flat_realign(struct inbuf *inb, int align)
590{
591 int off = inb->ptr - inb->base;
592
593 inb->ptr = inb->base + ALIGN(off, align);
594 if (inb->ptr > inb->limit)
595 die("Premature end of data parsing flat device tree\n");
596}
597
598static char *flat_read_string(struct inbuf *inb)
599{
600 int len = 0;
David Gibson92cb9a22007-12-04 14:26:15 +1100601 const char *p = inb->ptr;
David Gibsonfc14dad2005-06-08 17:18:34 +1000602 char *str;
603
604 do {
605 if (p >= inb->limit)
606 die("Premature end of data parsing flat device tree\n");
607 len++;
608 } while ((*p++) != '\0');
609
610 str = strdup(inb->ptr);
611
612 inb->ptr += len;
613
David Gibson53359012008-06-25 13:53:07 +1000614 flat_realign(inb, sizeof(uint32_t));
David Gibsonfc14dad2005-06-08 17:18:34 +1000615
616 return str;
617}
618
619static struct data flat_read_data(struct inbuf *inb, int len)
620{
621 struct data d = empty_data;
622
623 if (len == 0)
624 return empty_data;
625
626 d = data_grow_for(d, len);
627 d.len = len;
628
629 flat_read_chunk(inb, d.val, len);
630
David Gibson53359012008-06-25 13:53:07 +1000631 flat_realign(inb, sizeof(uint32_t));
David Gibsonfc14dad2005-06-08 17:18:34 +1000632
633 return d;
634}
635
636static char *flat_read_stringtable(struct inbuf *inb, int offset)
637{
David Gibson92cb9a22007-12-04 14:26:15 +1100638 const char *p;
David Gibsonfc14dad2005-06-08 17:18:34 +1000639
640 p = inb->base + offset;
641 while (1) {
David Gibson4ddf7c02005-08-19 16:11:11 +1000642 if (p >= inb->limit || p < inb->base)
David Gibson7ee3ffd2005-07-11 16:45:57 +1000643 die("String offset %d overruns string table\n",
644 offset);
David Gibsonfc14dad2005-06-08 17:18:34 +1000645
646 if (*p == '\0')
647 break;
648
649 p++;
650 }
651
652 return strdup(inb->base + offset);
653}
654
David Gibson230f2532005-08-29 12:48:02 +1000655static struct property *flat_read_property(struct inbuf *dtbuf,
656 struct inbuf *strbuf, int flags)
David Gibsonfc14dad2005-06-08 17:18:34 +1000657{
David Gibson53359012008-06-25 13:53:07 +1000658 uint32_t proplen, stroff;
David Gibsonfc14dad2005-06-08 17:18:34 +1000659 char *name;
660 struct data val;
661
662 proplen = flat_read_word(dtbuf);
663 stroff = flat_read_word(dtbuf);
664
665 name = flat_read_stringtable(strbuf, stroff);
666
667 if ((flags & FTF_VARALIGN) && (proplen >= 8))
668 flat_realign(dtbuf, 8);
669
670 val = flat_read_data(dtbuf, proplen);
671
David Gibson4102d842005-06-16 14:36:37 +1000672 return build_property(name, val, NULL);
David Gibsonfc14dad2005-06-08 17:18:34 +1000673}
674
David Gibsonf0517db2005-07-15 17:14:24 +1000675
David Gibsonf040d952005-10-24 18:18:38 +1000676static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
David Gibsonf0517db2005-07-15 17:14:24 +1000677{
David Gibsonf040d952005-10-24 18:18:38 +1000678 struct reserve_info *reservelist = NULL;
679 struct reserve_info *new;
David Gibson92cb9a22007-12-04 14:26:15 +1100680 const char *p;
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000681 struct fdt_reserve_entry re;
David Gibsonf0517db2005-07-15 17:14:24 +1000682
683 /*
684 * Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
685 * List terminates at an entry with size equal to zero.
686 *
687 * First pass, count entries.
688 */
689 p = inb->ptr;
David Gibson6c0f3672005-08-29 13:36:15 +1000690 while (1) {
691 flat_read_chunk(inb, &re, sizeof(re));
Michael Neuling332c5362006-07-07 23:28:10 +1000692 re.address = be64_to_cpu(re.address);
693 re.size = be64_to_cpu(re.size);
David Gibson6c0f3672005-08-29 13:36:15 +1000694 if (re.size == 0)
695 break;
David Gibsonf0517db2005-07-15 17:14:24 +1000696
David Gibsonf040d952005-10-24 18:18:38 +1000697 new = build_reserve_entry(re.address, re.size, NULL);
698 reservelist = add_reserve_entry(reservelist, new);
David Gibson6c0f3672005-08-29 13:36:15 +1000699 }
David Gibsonf0517db2005-07-15 17:14:24 +1000700
David Gibsonf040d952005-10-24 18:18:38 +1000701 return reservelist;
David Gibsonf0517db2005-07-15 17:14:24 +1000702}
703
704
David Gibson92cb9a22007-12-04 14:26:15 +1100705static char *nodename_from_path(const char *ppath, const char *cpath)
David Gibsonfc14dad2005-06-08 17:18:34 +1000706{
David Gibsonfc14dad2005-06-08 17:18:34 +1000707 int plen;
708
David Gibsonb2de5182008-02-29 16:51:28 +1100709 plen = strlen(ppath);
David Gibsonfc14dad2005-06-08 17:18:34 +1000710
David Gibsonb2de5182008-02-29 16:51:28 +1100711 if (!strneq(ppath, cpath, plen))
712 die("Path \"%s\" is not valid as a child of \"%s\"\n",
713 cpath, ppath);
David Gibsonfc14dad2005-06-08 17:18:34 +1000714
David Gibsonb2de5182008-02-29 16:51:28 +1100715 /* root node is a special case */
716 if (!streq(ppath, "/"))
717 plen++;
David Gibsonfc14dad2005-06-08 17:18:34 +1000718
David Gibsonb2de5182008-02-29 16:51:28 +1100719 return strdup(cpath + plen);
David Gibsonfc14dad2005-06-08 17:18:34 +1000720}
721
722static struct node *unflatten_tree(struct inbuf *dtbuf,
723 struct inbuf *strbuf,
David Gibsonb2de5182008-02-29 16:51:28 +1100724 const char *parent_flatname, int flags)
David Gibsonfc14dad2005-06-08 17:18:34 +1000725{
726 struct node *node;
David Gibsonb2de5182008-02-29 16:51:28 +1100727 char *flatname;
David Gibson53359012008-06-25 13:53:07 +1000728 uint32_t val;
David Gibsonfc14dad2005-06-08 17:18:34 +1000729
730 node = build_node(NULL, NULL);
731
David Gibsonb2de5182008-02-29 16:51:28 +1100732 flatname = flat_read_string(dtbuf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000733
David Gibsonb2de5182008-02-29 16:51:28 +1100734 if (flags & FTF_FULLPATH)
735 node->name = nodename_from_path(parent_flatname, flatname);
736 else
737 node->name = flatname;
David Gibsonfc14dad2005-06-08 17:18:34 +1000738
739 do {
740 struct property *prop;
741 struct node *child;
742
743 val = flat_read_word(dtbuf);
744 switch (val) {
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000745 case FDT_PROP:
David Gibson592ea582007-09-04 10:43:03 +1000746 if (node->children)
747 fprintf(stderr, "Warning: Flat tree input has "
748 "subnodes preceding a property.\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000749 prop = flat_read_property(dtbuf, strbuf, flags);
750 add_property(node, prop);
751 break;
752
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000753 case FDT_BEGIN_NODE:
David Gibsonb2de5182008-02-29 16:51:28 +1100754 child = unflatten_tree(dtbuf,strbuf, flatname, flags);
David Gibsonfc14dad2005-06-08 17:18:34 +1000755 add_child(node, child);
756 break;
757
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000758 case FDT_END_NODE:
David Gibsonfc14dad2005-06-08 17:18:34 +1000759 break;
760
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000761 case FDT_END:
762 die("Premature FDT_END in device tree blob\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000763 break;
764
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000765 case FDT_NOP:
David Gibson07387742007-06-26 11:30:47 +1000766 if (!(flags & FTF_NOPS))
767 fprintf(stderr, "Warning: NOP tag found in flat tree"
768 " version <16\n");
Milton Millerce243222007-06-09 23:21:31 -0500769
David Gibson07387742007-06-26 11:30:47 +1000770 /* Ignore */
Milton Millerce243222007-06-09 23:21:31 -0500771 break;
772
David Gibsonfc14dad2005-06-08 17:18:34 +1000773 default:
774 die("Invalid opcode word %08x in device tree blob\n",
775 val);
776 }
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000777 } while (val != FDT_END_NODE);
David Gibsonfc14dad2005-06-08 17:18:34 +1000778
779 return node;
780}
781
David Gibsonf0517db2005-07-15 17:14:24 +1000782
David Gibsona742aad2008-05-16 13:22:09 +1000783struct boot_info *dt_from_blob(const char *fname)
David Gibsonfc14dad2005-06-08 17:18:34 +1000784{
David Gibsona742aad2008-05-16 13:22:09 +1000785 struct dtc_file *dtcf;
David Gibson53359012008-06-25 13:53:07 +1000786 uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
787 uint32_t off_dt, off_str, off_mem_rsvmap;
David Gibsonfc14dad2005-06-08 17:18:34 +1000788 int rc;
789 char *blob;
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000790 struct fdt_header *fdt;
David Gibsonfc14dad2005-06-08 17:18:34 +1000791 char *p;
792 struct inbuf dtbuf, strbuf;
David Gibsonf0517db2005-07-15 17:14:24 +1000793 struct inbuf memresvbuf;
David Gibsonfc14dad2005-06-08 17:18:34 +1000794 int sizeleft;
David Gibsonf040d952005-10-24 18:18:38 +1000795 struct reserve_info *reservelist;
David Gibsonfc14dad2005-06-08 17:18:34 +1000796 struct node *tree;
David Gibson53359012008-06-25 13:53:07 +1000797 uint32_t val;
David Gibsonfc14dad2005-06-08 17:18:34 +1000798 int flags = 0;
799
David Gibsona742aad2008-05-16 13:22:09 +1000800 dtcf = dtc_open_file(fname, NULL);
801
802 rc = fread(&magic, sizeof(magic), 1, dtcf->file);
803 if (ferror(dtcf->file))
David Gibsonfc14dad2005-06-08 17:18:34 +1000804 die("Error reading DT blob magic number: %s\n",
805 strerror(errno));
806 if (rc < 1) {
David Gibsona742aad2008-05-16 13:22:09 +1000807 if (feof(dtcf->file))
David Gibsonfc14dad2005-06-08 17:18:34 +1000808 die("EOF reading DT blob magic number\n");
809 else
810 die("Mysterious short read reading magic number\n");
811 }
812
813 magic = be32_to_cpu(magic);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000814 if (magic != FDT_MAGIC)
David Gibsonfc14dad2005-06-08 17:18:34 +1000815 die("Blob has incorrect magic number\n");
816
David Gibsona742aad2008-05-16 13:22:09 +1000817 rc = fread(&totalsize, sizeof(totalsize), 1, dtcf->file);
818 if (ferror(dtcf->file))
David Gibsonfc14dad2005-06-08 17:18:34 +1000819 die("Error reading DT blob size: %s\n", strerror(errno));
820 if (rc < 1) {
David Gibsona742aad2008-05-16 13:22:09 +1000821 if (feof(dtcf->file))
David Gibsonfc14dad2005-06-08 17:18:34 +1000822 die("EOF reading DT blob size\n");
823 else
824 die("Mysterious short read reading blob size\n");
825 }
826
827 totalsize = be32_to_cpu(totalsize);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000828 if (totalsize < FDT_V1_SIZE)
David Gibsonfc14dad2005-06-08 17:18:34 +1000829 die("DT blob size (%d) is too small\n", totalsize);
830
831 blob = xmalloc(totalsize);
832
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000833 fdt = (struct fdt_header *)blob;
834 fdt->magic = cpu_to_be32(magic);
835 fdt->totalsize = cpu_to_be32(totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000836
837 sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
838 p = blob + sizeof(magic) + sizeof(totalsize);
839
840 while (sizeleft) {
David Gibsona742aad2008-05-16 13:22:09 +1000841 if (feof(dtcf->file))
David Gibsonfc14dad2005-06-08 17:18:34 +1000842 die("EOF before reading %d bytes of DT blob\n",
843 totalsize);
844
David Gibsona742aad2008-05-16 13:22:09 +1000845 rc = fread(p, 1, sizeleft, dtcf->file);
846 if (ferror(dtcf->file))
David Gibsonfc14dad2005-06-08 17:18:34 +1000847 die("Error reading DT blob: %s\n",
848 strerror(errno));
849
850 sizeleft -= rc;
851 p += rc;
852 }
853
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000854 off_dt = be32_to_cpu(fdt->off_dt_struct);
855 off_str = be32_to_cpu(fdt->off_dt_strings);
856 off_mem_rsvmap = be32_to_cpu(fdt->off_mem_rsvmap);
857 version = be32_to_cpu(fdt->version);
David Gibson548767f2008-05-16 13:22:57 +1000858 boot_cpuid_phys = be32_to_cpu(fdt->boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000859
David Gibsonf0517db2005-07-15 17:14:24 +1000860 if (off_mem_rsvmap >= totalsize)
861 die("Mem Reserve structure offset exceeds total size\n");
862
David Gibsonfc14dad2005-06-08 17:18:34 +1000863 if (off_dt >= totalsize)
864 die("DT structure offset exceeds total size\n");
865
866 if (off_str > totalsize)
867 die("String table offset exceeds total size\n");
868
David Gibsonfc14dad2005-06-08 17:18:34 +1000869 if (version >= 3) {
David Gibson53359012008-06-25 13:53:07 +1000870 uint32_t size_str = be32_to_cpu(fdt->size_dt_strings);
David Gibsonfc14dad2005-06-08 17:18:34 +1000871 if (off_str+size_str > totalsize)
872 die("String table extends past total size\n");
David Gibsona266e5c2008-02-28 20:55:37 +1100873 inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
874 } else {
875 inbuf_init(&strbuf, blob + off_str, blob + totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000876 }
David Gibson46c88df2007-03-14 11:02:40 +1100877
878 if (version >= 17) {
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000879 size_dt = be32_to_cpu(fdt->size_dt_struct);
David Gibson46c88df2007-03-14 11:02:40 +1100880 if (off_dt+size_dt > totalsize)
881 die("Structure block extends past total size\n");
882 }
David Gibson63dc9c72007-09-18 11:44:04 +1000883
David Gibson46c88df2007-03-14 11:02:40 +1100884 if (version < 16) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000885 flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
Milton Millerce243222007-06-09 23:21:31 -0500886 } else {
887 flags |= FTF_NOPS;
David Gibsonfc14dad2005-06-08 17:18:34 +1000888 }
889
David Gibsonf0517db2005-07-15 17:14:24 +1000890 inbuf_init(&memresvbuf,
891 blob + off_mem_rsvmap, blob + totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000892 inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000893
David Gibsonf040d952005-10-24 18:18:38 +1000894 reservelist = flat_read_mem_reserve(&memresvbuf);
David Gibsonf0517db2005-07-15 17:14:24 +1000895
David Gibsonfc14dad2005-06-08 17:18:34 +1000896 val = flat_read_word(&dtbuf);
897
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000898 if (val != FDT_BEGIN_NODE)
899 die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000900
901 tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
902
903 val = flat_read_word(&dtbuf);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000904 if (val != FDT_END)
905 die("Device tree blob doesn't end with FDT_END\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000906
907 free(blob);
908
David Gibsona742aad2008-05-16 13:22:09 +1000909 dtc_close_file(dtcf);
910
David Gibson548767f2008-05-16 13:22:57 +1000911 return build_boot_info(reservelist, tree, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000912}