2019-06-20 23:19:38 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-06-08 09:18:34 +02:00
|
|
|
/*
|
|
|
|
* (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dtc.h"
|
2008-05-16 05:22:09 +02:00
|
|
|
#include "srcpos.h"
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
#define FTF_FULLPATH 0x1
|
|
|
|
#define FTF_VARALIGN 0x2
|
|
|
|
#define FTF_NAMEPROPS 0x4
|
|
|
|
#define FTF_BOOTCPUID 0x8
|
|
|
|
#define FTF_STRTABSIZE 0x10
|
2007-03-14 01:02:40 +01:00
|
|
|
#define FTF_STRUCTSIZE 0x20
|
2007-06-10 06:21:31 +02:00
|
|
|
#define FTF_NOPS 0x40
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2005-08-29 04:48:02 +02:00
|
|
|
static struct version_info {
|
2005-06-08 09:18:34 +02:00
|
|
|
int version;
|
|
|
|
int last_comp_version;
|
|
|
|
int hdr_size;
|
|
|
|
int flags;
|
|
|
|
} version_table[] = {
|
2007-09-26 05:11:05 +02:00
|
|
|
{1, 1, FDT_V1_SIZE,
|
2005-06-08 09:18:34 +02:00
|
|
|
FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS},
|
2007-09-26 05:11:05 +02:00
|
|
|
{2, 1, FDT_V2_SIZE,
|
2005-06-08 09:18:34 +02:00
|
|
|
FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID},
|
2007-09-26 05:11:05 +02:00
|
|
|
{3, 1, FDT_V3_SIZE,
|
2005-06-08 09:18:34 +02:00
|
|
|
FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE},
|
2007-09-26 05:11:05 +02:00
|
|
|
{16, 16, FDT_V3_SIZE,
|
2007-06-10 06:21:31 +02:00
|
|
|
FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS},
|
2007-09-26 05:11:05 +02:00
|
|
|
{17, 16, FDT_V17_SIZE,
|
2007-06-10 06:21:31 +02:00
|
|
|
FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
|
2005-06-08 09:18:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct emitter {
|
|
|
|
void (*cell)(void *, cell_t);
|
2017-03-04 14:26:43 +01:00
|
|
|
void (*string)(void *, const char *, int);
|
2005-06-08 09:18:34 +02:00
|
|
|
void (*align)(void *, int);
|
|
|
|
void (*data)(void *, struct data);
|
2010-02-24 08:22:17 +01:00
|
|
|
void (*beginnode)(void *, struct label *labels);
|
|
|
|
void (*endnode)(void *, struct label *labels);
|
|
|
|
void (*property)(void *, struct label *labels);
|
2005-06-08 09:18:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bin_emit_cell(void *e, cell_t val)
|
|
|
|
{
|
|
|
|
struct data *dtbuf = e;
|
|
|
|
|
|
|
|
*dtbuf = data_append_cell(*dtbuf, val);
|
|
|
|
}
|
|
|
|
|
2017-03-04 14:26:43 +01:00
|
|
|
static void bin_emit_string(void *e, const char *str, int len)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
struct data *dtbuf = e;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
len = strlen(str);
|
|
|
|
|
|
|
|
*dtbuf = data_append_data(*dtbuf, str, len);
|
|
|
|
*dtbuf = data_append_byte(*dtbuf, '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bin_emit_align(void *e, int a)
|
|
|
|
{
|
|
|
|
struct data *dtbuf = e;
|
|
|
|
|
|
|
|
*dtbuf = data_append_align(*dtbuf, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bin_emit_data(void *e, struct data d)
|
|
|
|
{
|
|
|
|
struct data *dtbuf = e;
|
|
|
|
|
|
|
|
*dtbuf = data_append_data(*dtbuf, d.val, d.len);
|
|
|
|
}
|
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
static void bin_emit_beginnode(void *e, struct label *labels)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2007-09-26 05:11:05 +02:00
|
|
|
bin_emit_cell(e, FDT_BEGIN_NODE);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
static void bin_emit_endnode(void *e, struct label *labels)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2007-09-26 05:11:05 +02:00
|
|
|
bin_emit_cell(e, FDT_END_NODE);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
static void bin_emit_property(void *e, struct label *labels)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2007-09-26 05:11:05 +02:00
|
|
|
bin_emit_cell(e, FDT_PROP);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2005-08-29 04:48:02 +02:00
|
|
|
static struct emitter bin_emitter = {
|
2005-06-08 09:18:34 +02:00
|
|
|
.cell = bin_emit_cell,
|
|
|
|
.string = bin_emit_string,
|
|
|
|
.align = bin_emit_align,
|
|
|
|
.data = bin_emit_data,
|
|
|
|
.beginnode = bin_emit_beginnode,
|
|
|
|
.endnode = bin_emit_endnode,
|
2007-09-18 03:44:04 +02:00
|
|
|
.property = bin_emit_property,
|
2005-06-08 09:18:34 +02:00
|
|
|
};
|
|
|
|
|
2007-12-04 04:26:15 +01:00
|
|
|
static void emit_label(FILE *f, const char *prefix, const char *label)
|
2005-06-16 06:36:37 +02:00
|
|
|
{
|
|
|
|
fprintf(f, "\t.globl\t%s_%s\n", prefix, label);
|
|
|
|
fprintf(f, "%s_%s:\n", prefix, label);
|
|
|
|
fprintf(f, "_%s_%s:\n", prefix, label);
|
|
|
|
}
|
|
|
|
|
2007-12-04 04:26:15 +01:00
|
|
|
static void emit_offset_label(FILE *f, const char *label, int offset)
|
dtc: implement labels on property data
Extend the parser grammer to allow labels before or after any
property data (string, cell list, or byte list), and any
byte or cell within the property data.
Store the labels using the same linked list structure as node
references, but using a parallel list.
When writing assembly output emit global labels as offsets from
the start of the definition of the data.
Note that the alignment for a cell list is done as part of the
opening < delimiter, not the = or , before it. To label a cell
after a string or byte list put the label inside the cell list.
For example,
prop = zero: [ aa bb ], two: < four: 1234 > eight: ;
will produce labels with offsets 0, 2, 4, and 8 bytes from
the beginning of the data for property prop.
Signed-off-by: Milton Miller <miltonm@bga.com>
2007-07-07 08:18:51 +02:00
|
|
|
{
|
|
|
|
fprintf(f, "\t.globl\t%s\n", label);
|
|
|
|
fprintf(f, "%s\t= . + %d\n", label, offset);
|
|
|
|
}
|
|
|
|
|
2009-01-08 01:47:55 +01:00
|
|
|
#define ASM_EMIT_BELONG(f, fmt, ...) \
|
|
|
|
{ \
|
|
|
|
fprintf((f), "\t.byte\t((" fmt ") >> 24) & 0xff\n", __VA_ARGS__); \
|
|
|
|
fprintf((f), "\t.byte\t((" fmt ") >> 16) & 0xff\n", __VA_ARGS__); \
|
|
|
|
fprintf((f), "\t.byte\t((" fmt ") >> 8) & 0xff\n", __VA_ARGS__); \
|
|
|
|
fprintf((f), "\t.byte\t(" fmt ") & 0xff\n", __VA_ARGS__); \
|
|
|
|
}
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
static void asm_emit_cell(void *e, cell_t val)
|
|
|
|
{
|
|
|
|
FILE *f = e;
|
|
|
|
|
2009-01-08 01:47:55 +01:00
|
|
|
fprintf(f, "\t.byte 0x%02x; .byte 0x%02x; .byte 0x%02x; .byte 0x%02x\n",
|
|
|
|
(val >> 24) & 0xff, (val >> 16) & 0xff,
|
|
|
|
(val >> 8) & 0xff, val & 0xff);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2017-03-04 14:26:43 +01:00
|
|
|
static void asm_emit_string(void *e, const char *str, int len)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
FILE *f = e;
|
|
|
|
|
2017-03-04 14:26:43 +01:00
|
|
|
if (len != 0)
|
|
|
|
fprintf(f, "\t.string\t\"%.*s\"\n", len, str);
|
|
|
|
else
|
|
|
|
fprintf(f, "\t.string\t\"%s\"\n", str);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void asm_emit_align(void *e, int a)
|
|
|
|
{
|
|
|
|
FILE *f = e;
|
|
|
|
|
2009-11-12 03:30:02 +01:00
|
|
|
fprintf(f, "\t.balign\t%d, 0\n", a);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void asm_emit_data(void *e, struct data d)
|
|
|
|
{
|
|
|
|
FILE *f = e;
|
2020-10-12 18:19:43 +02:00
|
|
|
unsigned int off = 0;
|
2008-02-28 10:58:28 +01:00
|
|
|
struct marker *m = d.markers;
|
dtc: implement labels on property data
Extend the parser grammer to allow labels before or after any
property data (string, cell list, or byte list), and any
byte or cell within the property data.
Store the labels using the same linked list structure as node
references, but using a parallel list.
When writing assembly output emit global labels as offsets from
the start of the definition of the data.
Note that the alignment for a cell list is done as part of the
opening < delimiter, not the = or , before it. To label a cell
after a string or byte list put the label inside the cell list.
For example,
prop = zero: [ aa bb ], two: < four: 1234 > eight: ;
will produce labels with offsets 0, 2, 4, and 8 bytes from
the beginning of the data for property prop.
Signed-off-by: Milton Miller <miltonm@bga.com>
2007-07-07 08:18:51 +02:00
|
|
|
|
2008-02-28 10:58:28 +01:00
|
|
|
for_each_marker_of_type(m, LABEL)
|
|
|
|
emit_offset_label(f, m->ref, m->offset);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2008-06-25 05:53:07 +02:00
|
|
|
while ((d.len - off) >= sizeof(uint32_t)) {
|
2020-04-14 07:02:51 +02:00
|
|
|
asm_emit_cell(e, dtb_ld32(d.val + off));
|
2008-06-25 05:53:07 +02:00
|
|
|
off += sizeof(uint32_t);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2008-06-25 06:27:53 +02:00
|
|
|
while ((d.len - off) >= 1) {
|
2005-06-08 09:18:34 +02:00
|
|
|
fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]);
|
|
|
|
off += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(off == d.len);
|
|
|
|
}
|
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
static void asm_emit_beginnode(void *e, struct label *labels)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
FILE *f = e;
|
2010-02-24 08:22:17 +01:00
|
|
|
struct label *l;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
for_each_label(labels, l) {
|
|
|
|
fprintf(f, "\t.globl\t%s\n", l->label);
|
|
|
|
fprintf(f, "%s:\n", l->label);
|
2005-06-16 06:36:37 +02:00
|
|
|
}
|
2009-01-08 01:47:55 +01:00
|
|
|
fprintf(f, "\t/* FDT_BEGIN_NODE */\n");
|
|
|
|
asm_emit_cell(e, FDT_BEGIN_NODE);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
static void asm_emit_endnode(void *e, struct label *labels)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
FILE *f = e;
|
2010-02-24 08:22:17 +01:00
|
|
|
struct label *l;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2009-01-08 01:47:55 +01:00
|
|
|
fprintf(f, "\t/* FDT_END_NODE */\n");
|
|
|
|
asm_emit_cell(e, FDT_END_NODE);
|
2010-02-24 08:22:17 +01:00
|
|
|
for_each_label(labels, l) {
|
|
|
|
fprintf(f, "\t.globl\t%s_end\n", l->label);
|
|
|
|
fprintf(f, "%s_end:\n", l->label);
|
2005-06-16 06:36:37 +02:00
|
|
|
}
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
static void asm_emit_property(void *e, struct label *labels)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
FILE *f = e;
|
2010-02-24 08:22:17 +01:00
|
|
|
struct label *l;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
for_each_label(labels, l) {
|
|
|
|
fprintf(f, "\t.globl\t%s\n", l->label);
|
|
|
|
fprintf(f, "%s:\n", l->label);
|
2005-06-16 06:36:37 +02:00
|
|
|
}
|
2009-01-08 01:47:55 +01:00
|
|
|
fprintf(f, "\t/* FDT_PROP */\n");
|
|
|
|
asm_emit_cell(e, FDT_PROP);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2005-08-29 04:48:02 +02:00
|
|
|
static struct emitter asm_emitter = {
|
2005-06-08 09:18:34 +02:00
|
|
|
.cell = asm_emit_cell,
|
|
|
|
.string = asm_emit_string,
|
|
|
|
.align = asm_emit_align,
|
|
|
|
.data = asm_emit_data,
|
|
|
|
.beginnode = asm_emit_beginnode,
|
|
|
|
.endnode = asm_emit_endnode,
|
2007-09-18 03:44:04 +02:00
|
|
|
.property = asm_emit_property,
|
2005-06-08 09:18:34 +02:00
|
|
|
};
|
|
|
|
|
2007-12-04 04:26:15 +01:00
|
|
|
static int stringtable_insert(struct data *d, const char *str)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2020-10-12 18:19:43 +02:00
|
|
|
unsigned int i;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
/* FIXME: do this more efficiently? */
|
|
|
|
|
|
|
|
for (i = 0; i < d->len; i++) {
|
|
|
|
if (streq(str, d->val + i))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = data_append_data(*d, str, strlen(str)+1);
|
2005-07-11 09:09:42 +02:00
|
|
|
return i;
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flatten_tree(struct node *tree, struct emitter *emit,
|
|
|
|
void *etarget, struct data *strbuf,
|
|
|
|
struct version_info *vi)
|
|
|
|
{
|
|
|
|
struct property *prop;
|
|
|
|
struct node *child;
|
2013-10-28 11:06:53 +01:00
|
|
|
bool seen_name_prop = false;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2012-08-08 06:50:15 +02:00
|
|
|
if (tree->deleted)
|
|
|
|
return;
|
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
emit->beginnode(etarget, tree->labels);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
if (vi->flags & FTF_FULLPATH)
|
|
|
|
emit->string(etarget, tree->fullpath, 0);
|
|
|
|
else
|
|
|
|
emit->string(etarget, tree->name, 0);
|
|
|
|
|
|
|
|
emit->align(etarget, sizeof(cell_t));
|
|
|
|
|
|
|
|
for_each_property(tree, prop) {
|
|
|
|
int nameoff;
|
|
|
|
|
|
|
|
if (streq(prop->name, "name"))
|
2013-10-28 11:06:53 +01:00
|
|
|
seen_name_prop = true;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
nameoff = stringtable_insert(strbuf, prop->name);
|
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
emit->property(etarget, prop->labels);
|
2005-06-08 09:18:34 +02:00
|
|
|
emit->cell(etarget, prop->val.len);
|
|
|
|
emit->cell(etarget, nameoff);
|
|
|
|
|
|
|
|
if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8))
|
|
|
|
emit->align(etarget, 8);
|
|
|
|
|
|
|
|
emit->data(etarget, prop->val);
|
|
|
|
emit->align(etarget, sizeof(cell_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) {
|
2005-06-16 06:36:37 +02:00
|
|
|
emit->property(etarget, NULL);
|
2005-06-08 09:18:34 +02:00
|
|
|
emit->cell(etarget, tree->basenamelen+1);
|
|
|
|
emit->cell(etarget, stringtable_insert(strbuf, "name"));
|
|
|
|
|
|
|
|
if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8))
|
|
|
|
emit->align(etarget, 8);
|
|
|
|
|
|
|
|
emit->string(etarget, tree->name, tree->basenamelen);
|
2005-08-25 07:39:09 +02:00
|
|
|
emit->align(etarget, sizeof(cell_t));
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for_each_child(tree, child) {
|
|
|
|
flatten_tree(child, emit, etarget, strbuf, vi);
|
|
|
|
}
|
|
|
|
|
2010-02-24 08:22:17 +01:00
|
|
|
emit->endnode(etarget, tree->labels);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2005-10-24 10:18:38 +02:00
|
|
|
static struct data flatten_reserve_list(struct reserve_info *reservelist,
|
|
|
|
struct version_info *vi)
|
|
|
|
{
|
|
|
|
struct reserve_info *re;
|
|
|
|
struct data d = empty_data;
|
2007-04-05 04:04:33 +02:00
|
|
|
int j;
|
2005-10-24 10:18:38 +02:00
|
|
|
|
|
|
|
for (re = reservelist; re; re = re->next) {
|
2017-03-06 02:04:45 +01:00
|
|
|
d = data_append_re(d, re->address, re->size);
|
2005-10-24 10:18:38 +02:00
|
|
|
}
|
2007-04-05 04:04:33 +02:00
|
|
|
/*
|
|
|
|
* Add additional reserved slots if the user asked for them.
|
|
|
|
*/
|
|
|
|
for (j = 0; j < reservenum; j++) {
|
2017-03-06 02:04:45 +01:00
|
|
|
d = data_append_re(d, 0, 0);
|
2007-04-05 04:04:33 +02:00
|
|
|
}
|
2007-04-19 03:59:51 +02:00
|
|
|
|
2005-10-24 10:18:38 +02:00
|
|
|
return d;
|
|
|
|
}
|
2007-04-19 03:59:51 +02:00
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
static void make_fdt_header(struct fdt_header *fdt,
|
|
|
|
struct version_info *vi,
|
|
|
|
int reservesize, int dtsize, int strsize,
|
|
|
|
int boot_cpuid_phys)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2005-07-11 09:19:26 +02:00
|
|
|
int reserve_off;
|
2005-10-24 10:18:38 +02:00
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
reservesize += sizeof(struct fdt_reserve_entry);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
memset(fdt, 0xff, sizeof(*fdt));
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2008-06-25 06:27:53 +02:00
|
|
|
fdt->magic = cpu_to_fdt32(FDT_MAGIC);
|
|
|
|
fdt->version = cpu_to_fdt32(vi->version);
|
|
|
|
fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2005-07-11 09:19:26 +02:00
|
|
|
/* Reserve map should be doubleword aligned */
|
|
|
|
reserve_off = ALIGN(vi->hdr_size, 8);
|
|
|
|
|
2008-06-25 06:27:53 +02:00
|
|
|
fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off);
|
|
|
|
fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize);
|
|
|
|
fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize
|
2005-06-08 09:18:34 +02:00
|
|
|
+ dtsize);
|
2008-06-25 06:27:53 +02:00
|
|
|
fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize);
|
2007-04-05 04:04:33 +02:00
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
if (vi->flags & FTF_BOOTCPUID)
|
2008-06-25 06:27:53 +02:00
|
|
|
fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys);
|
2005-06-08 09:18:34 +02:00
|
|
|
if (vi->flags & FTF_STRTABSIZE)
|
2008-06-25 06:27:53 +02:00
|
|
|
fdt->size_dt_strings = cpu_to_fdt32(strsize);
|
2007-03-14 01:02:40 +01:00
|
|
|
if (vi->flags & FTF_STRUCTSIZE)
|
2008-06-25 06:27:53 +02:00
|
|
|
fdt->size_dt_struct = cpu_to_fdt32(dtsize);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2016-05-31 03:58:42 +02:00
|
|
|
void dt_to_blob(FILE *f, struct dt_info *dti, int version)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
struct version_info *vi = NULL;
|
2020-10-12 18:19:43 +02:00
|
|
|
unsigned int i;
|
2007-04-19 03:59:51 +02:00
|
|
|
struct data blob = empty_data;
|
|
|
|
struct data reservebuf = empty_data;
|
|
|
|
struct data dtbuf = empty_data;
|
|
|
|
struct data strbuf = empty_data;
|
2007-09-26 05:11:05 +02:00
|
|
|
struct fdt_header fdt;
|
2007-11-28 17:21:12 +01:00
|
|
|
int padlen = 0;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(version_table); i++) {
|
|
|
|
if (version_table[i].version == version)
|
|
|
|
vi = &version_table[i];
|
|
|
|
}
|
|
|
|
if (!vi)
|
|
|
|
die("Unknown device tree blob version %d\n", version);
|
|
|
|
|
2016-05-31 03:58:42 +02:00
|
|
|
flatten_tree(dti->dt, &bin_emitter, &dtbuf, &strbuf, vi);
|
2007-09-26 05:11:05 +02:00
|
|
|
bin_emit_cell(&dtbuf, FDT_END);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2016-05-31 03:58:42 +02:00
|
|
|
reservebuf = flatten_reserve_list(dti->reservelist, vi);
|
2005-10-24 10:18:38 +02:00
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
/* Make header */
|
2007-09-26 05:11:05 +02:00
|
|
|
make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
|
2016-05-31 03:58:42 +02:00
|
|
|
dti->boot_cpuid_phys);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2007-04-20 04:22:35 +02:00
|
|
|
/*
|
|
|
|
* If the user asked for more space than is used, adjust the totalsize.
|
|
|
|
*/
|
2007-11-28 17:21:12 +01:00
|
|
|
if (minsize > 0) {
|
2008-06-25 06:27:53 +02:00
|
|
|
padlen = minsize - fdt32_to_cpu(fdt.totalsize);
|
2016-07-18 09:56:53 +02:00
|
|
|
if (padlen < 0) {
|
|
|
|
padlen = 0;
|
|
|
|
if (quiet < 1)
|
|
|
|
fprintf(stderr,
|
2018-06-05 01:55:15 +02:00
|
|
|
"Warning: blob size %"PRIu32" >= minimum size %d\n",
|
2016-07-18 09:56:53 +02:00
|
|
|
fdt32_to_cpu(fdt.totalsize), minsize);
|
|
|
|
}
|
2007-04-20 04:22:35 +02:00
|
|
|
}
|
|
|
|
|
2007-11-28 17:21:12 +01:00
|
|
|
if (padsize > 0)
|
|
|
|
padlen = padsize;
|
|
|
|
|
2016-07-18 09:56:53 +02:00
|
|
|
if (alignsize > 0)
|
|
|
|
padlen = ALIGN(fdt32_to_cpu(fdt.totalsize) + padlen, alignsize)
|
|
|
|
- fdt32_to_cpu(fdt.totalsize);
|
|
|
|
|
2007-12-05 00:36:08 +01:00
|
|
|
if (padlen > 0) {
|
2008-06-25 06:27:53 +02:00
|
|
|
int tsize = fdt32_to_cpu(fdt.totalsize);
|
2007-12-05 00:36:08 +01:00
|
|
|
tsize += padlen;
|
2008-06-25 06:27:53 +02:00
|
|
|
fdt.totalsize = cpu_to_fdt32(tsize);
|
2007-12-05 00:36:08 +01:00
|
|
|
}
|
|
|
|
|
2007-04-19 03:59:51 +02:00
|
|
|
/*
|
|
|
|
* Assemble the blob: start with the header, add with alignment
|
|
|
|
* the reserve buffer, add the reserve map terminating zeroes,
|
|
|
|
* the device tree itself, and finally the strings.
|
|
|
|
*/
|
2008-02-28 10:55:37 +01:00
|
|
|
blob = data_append_data(blob, &fdt, vi->hdr_size);
|
2007-04-19 03:59:51 +02:00
|
|
|
blob = data_append_align(blob, 8);
|
|
|
|
blob = data_merge(blob, reservebuf);
|
2007-09-26 05:11:05 +02:00
|
|
|
blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
|
2007-04-19 03:59:51 +02:00
|
|
|
blob = data_merge(blob, dtbuf);
|
|
|
|
blob = data_merge(blob, strbuf);
|
2005-07-11 09:19:26 +02:00
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
/*
|
2007-04-19 03:59:51 +02:00
|
|
|
* If the user asked for more space than is used, pad out the blob.
|
2005-07-15 09:14:24 +02:00
|
|
|
*/
|
2007-12-05 00:36:08 +01:00
|
|
|
if (padlen > 0)
|
2007-04-20 04:22:35 +02:00
|
|
|
blob = data_append_zeroes(blob, padlen);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2008-11-07 01:41:11 +01:00
|
|
|
if (fwrite(blob.val, blob.len, 1, f) != 1) {
|
|
|
|
if (ferror(f))
|
|
|
|
die("Error writing device tree blob: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
else
|
|
|
|
die("Short write on device tree blob\n");
|
|
|
|
}
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2007-04-19 03:59:51 +02:00
|
|
|
/*
|
|
|
|
* data_merge() frees the right-hand element so only the blob
|
|
|
|
* remains to be freed.
|
|
|
|
*/
|
|
|
|
data_free(blob);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2005-08-29 04:48:02 +02:00
|
|
|
static void dump_stringtable_asm(FILE *f, struct data strbuf)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2007-12-04 04:26:15 +01:00
|
|
|
const char *p;
|
2005-06-08 09:18:34 +02:00
|
|
|
int len;
|
|
|
|
|
|
|
|
p = strbuf.val;
|
|
|
|
|
|
|
|
while (p < (strbuf.val + strbuf.len)) {
|
|
|
|
len = strlen(p);
|
|
|
|
fprintf(f, "\t.string \"%s\"\n", p);
|
|
|
|
p += len+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 03:58:42 +02:00
|
|
|
void dt_to_asm(FILE *f, struct dt_info *dti, int version)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
struct version_info *vi = NULL;
|
2020-10-12 18:19:43 +02:00
|
|
|
unsigned int i;
|
2005-06-08 09:18:34 +02:00
|
|
|
struct data strbuf = empty_data;
|
2005-10-24 10:18:38 +02:00
|
|
|
struct reserve_info *re;
|
2007-12-04 04:26:15 +01:00
|
|
|
const char *symprefix = "dt";
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(version_table); i++) {
|
|
|
|
if (version_table[i].version == version)
|
|
|
|
vi = &version_table[i];
|
|
|
|
}
|
|
|
|
if (!vi)
|
|
|
|
die("Unknown device tree blob version %d\n", version);
|
|
|
|
|
|
|
|
fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
|
|
|
|
|
|
|
|
emit_label(f, symprefix, "blob_start");
|
|
|
|
emit_label(f, symprefix, "header");
|
2009-01-08 01:47:55 +01:00
|
|
|
fprintf(f, "\t/* magic */\n");
|
|
|
|
asm_emit_cell(f, FDT_MAGIC);
|
|
|
|
fprintf(f, "\t/* totalsize */\n");
|
|
|
|
ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start",
|
|
|
|
symprefix, symprefix);
|
|
|
|
fprintf(f, "\t/* off_dt_struct */\n");
|
|
|
|
ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start",
|
2005-06-08 09:18:34 +02:00
|
|
|
symprefix, symprefix);
|
2009-01-08 01:47:55 +01:00
|
|
|
fprintf(f, "\t/* off_dt_strings */\n");
|
|
|
|
ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start",
|
2005-06-08 09:18:34 +02:00
|
|
|
symprefix, symprefix);
|
2009-01-08 01:47:55 +01:00
|
|
|
fprintf(f, "\t/* off_mem_rsvmap */\n");
|
|
|
|
ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start",
|
2005-06-08 09:18:34 +02:00
|
|
|
symprefix, symprefix);
|
2009-01-08 01:47:55 +01:00
|
|
|
fprintf(f, "\t/* version */\n");
|
|
|
|
asm_emit_cell(f, vi->version);
|
|
|
|
fprintf(f, "\t/* last_comp_version */\n");
|
|
|
|
asm_emit_cell(f, vi->last_comp_version);
|
|
|
|
|
|
|
|
if (vi->flags & FTF_BOOTCPUID) {
|
|
|
|
fprintf(f, "\t/* boot_cpuid_phys */\n");
|
2016-05-31 03:58:42 +02:00
|
|
|
asm_emit_cell(f, dti->boot_cpuid_phys);
|
2009-01-08 01:47:55 +01:00
|
|
|
}
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2009-01-08 01:47:55 +01:00
|
|
|
if (vi->flags & FTF_STRTABSIZE) {
|
|
|
|
fprintf(f, "\t/* size_dt_strings */\n");
|
|
|
|
ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start",
|
|
|
|
symprefix, symprefix);
|
|
|
|
}
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2009-01-08 01:47:55 +01:00
|
|
|
if (vi->flags & FTF_STRUCTSIZE) {
|
|
|
|
fprintf(f, "\t/* size_dt_struct */\n");
|
|
|
|
ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start",
|
2007-07-07 08:18:47 +02:00
|
|
|
symprefix, symprefix);
|
2009-01-08 01:47:55 +01:00
|
|
|
}
|
2007-07-07 08:18:47 +02:00
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
/*
|
|
|
|
* Reserve map entries.
|
|
|
|
* Align the reserve map to a doubleword boundary.
|
|
|
|
* Each entry is an (address, size) pair of u64 values.
|
|
|
|
* Always supply a zero-sized temination entry.
|
|
|
|
*/
|
2005-07-11 09:19:26 +02:00
|
|
|
asm_emit_align(f, 8);
|
2005-06-08 09:18:34 +02:00
|
|
|
emit_label(f, symprefix, "reserve_map");
|
2005-07-15 09:14:24 +02:00
|
|
|
|
2005-10-24 10:18:38 +02:00
|
|
|
fprintf(f, "/* Memory reserve map from source file */\n");
|
2006-04-19 18:58:45 +02:00
|
|
|
|
|
|
|
/*
|
2019-05-20 10:12:09 +02:00
|
|
|
* Use .long on high and low halves of u64s to avoid .quad
|
2006-04-19 18:58:45 +02:00
|
|
|
* as it appears .quad isn't available in some assemblers.
|
|
|
|
*/
|
2016-05-31 03:58:42 +02:00
|
|
|
for (re = dti->reservelist; re; re = re->next) {
|
2010-02-24 08:22:17 +01:00
|
|
|
struct label *l;
|
|
|
|
|
|
|
|
for_each_label(re->labels, l) {
|
|
|
|
fprintf(f, "\t.globl\t%s\n", l->label);
|
|
|
|
fprintf(f, "%s:\n", l->label);
|
2007-07-07 08:18:49 +02:00
|
|
|
}
|
2017-03-06 02:04:45 +01:00
|
|
|
ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->address >> 32));
|
2009-01-08 01:47:55 +01:00
|
|
|
ASM_EMIT_BELONG(f, "0x%08x",
|
2017-03-06 02:04:45 +01:00
|
|
|
(unsigned int)(re->address & 0xffffffff));
|
|
|
|
ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size >> 32));
|
|
|
|
ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size & 0xffffffff));
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
2007-04-18 00:14:41 +02:00
|
|
|
for (i = 0; i < reservenum; i++) {
|
|
|
|
fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
|
|
|
|
}
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2006-04-19 18:58:45 +02:00
|
|
|
fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
|
2005-07-15 09:14:24 +02:00
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
emit_label(f, symprefix, "struct_start");
|
2016-05-31 03:58:42 +02:00
|
|
|
flatten_tree(dti->dt, &asm_emitter, f, &strbuf, vi);
|
2009-01-08 01:47:55 +01:00
|
|
|
|
|
|
|
fprintf(f, "\t/* FDT_END */\n");
|
|
|
|
asm_emit_cell(f, FDT_END);
|
2005-06-08 09:18:34 +02:00
|
|
|
emit_label(f, symprefix, "struct_end");
|
|
|
|
|
|
|
|
emit_label(f, symprefix, "strings_start");
|
|
|
|
dump_stringtable_asm(f, strbuf);
|
|
|
|
emit_label(f, symprefix, "strings_end");
|
|
|
|
|
|
|
|
emit_label(f, symprefix, "blob_end");
|
|
|
|
|
2007-04-19 03:59:51 +02:00
|
|
|
/*
|
|
|
|
* If the user asked for more space than is used, pad it out.
|
|
|
|
*/
|
|
|
|
if (minsize > 0) {
|
|
|
|
fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
|
|
|
|
minsize, symprefix, symprefix);
|
|
|
|
}
|
2007-11-28 17:21:12 +01:00
|
|
|
if (padsize > 0) {
|
|
|
|
fprintf(f, "\t.space\t%d, 0\n", padsize);
|
|
|
|
}
|
2016-07-18 09:56:53 +02:00
|
|
|
if (alignsize > 0)
|
|
|
|
asm_emit_align(f, alignsize);
|
2007-04-19 03:59:51 +02:00
|
|
|
emit_label(f, symprefix, "blob_abs_end");
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
data_free(strbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct inbuf {
|
|
|
|
char *base, *limit, *ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void inbuf_init(struct inbuf *inb, void *base, void *limit)
|
|
|
|
{
|
|
|
|
inb->base = base;
|
|
|
|
inb->limit = limit;
|
|
|
|
inb->ptr = inb->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flat_read_chunk(struct inbuf *inb, void *p, int len)
|
|
|
|
{
|
|
|
|
if ((inb->ptr + len) > inb->limit)
|
|
|
|
die("Premature end of data parsing flat device tree\n");
|
|
|
|
|
|
|
|
memcpy(p, inb->ptr, len);
|
|
|
|
|
|
|
|
inb->ptr += len;
|
|
|
|
}
|
|
|
|
|
2008-06-25 05:53:07 +02:00
|
|
|
static uint32_t flat_read_word(struct inbuf *inb)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2017-03-06 02:08:53 +01:00
|
|
|
fdt32_t val;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
|
|
|
|
|
|
|
|
flat_read_chunk(inb, &val, sizeof(val));
|
|
|
|
|
2008-06-25 06:27:53 +02:00
|
|
|
return fdt32_to_cpu(val);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flat_realign(struct inbuf *inb, int align)
|
|
|
|
{
|
|
|
|
int off = inb->ptr - inb->base;
|
|
|
|
|
|
|
|
inb->ptr = inb->base + ALIGN(off, align);
|
|
|
|
if (inb->ptr > inb->limit)
|
|
|
|
die("Premature end of data parsing flat device tree\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *flat_read_string(struct inbuf *inb)
|
|
|
|
{
|
|
|
|
int len = 0;
|
2007-12-04 04:26:15 +01:00
|
|
|
const char *p = inb->ptr;
|
2005-06-08 09:18:34 +02:00
|
|
|
char *str;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (p >= inb->limit)
|
|
|
|
die("Premature end of data parsing flat device tree\n");
|
|
|
|
len++;
|
|
|
|
} while ((*p++) != '\0');
|
|
|
|
|
2008-10-03 18:12:33 +02:00
|
|
|
str = xstrdup(inb->ptr);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
inb->ptr += len;
|
|
|
|
|
2008-06-25 05:53:07 +02:00
|
|
|
flat_realign(inb, sizeof(uint32_t));
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct data flat_read_data(struct inbuf *inb, int len)
|
|
|
|
{
|
|
|
|
struct data d = empty_data;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return empty_data;
|
|
|
|
|
|
|
|
d = data_grow_for(d, len);
|
|
|
|
d.len = len;
|
|
|
|
|
|
|
|
flat_read_chunk(inb, d.val, len);
|
|
|
|
|
2008-06-25 05:53:07 +02:00
|
|
|
flat_realign(inb, sizeof(uint32_t));
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *flat_read_stringtable(struct inbuf *inb, int offset)
|
|
|
|
{
|
2007-12-04 04:26:15 +01:00
|
|
|
const char *p;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
p = inb->base + offset;
|
|
|
|
while (1) {
|
2005-08-19 08:11:11 +02:00
|
|
|
if (p >= inb->limit || p < inb->base)
|
2005-07-11 08:45:57 +02:00
|
|
|
die("String offset %d overruns string table\n",
|
|
|
|
offset);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2008-10-03 18:12:33 +02:00
|
|
|
return xstrdup(inb->base + offset);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2005-08-29 04:48:02 +02:00
|
|
|
static struct property *flat_read_property(struct inbuf *dtbuf,
|
|
|
|
struct inbuf *strbuf, int flags)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2008-06-25 05:53:07 +02:00
|
|
|
uint32_t proplen, stroff;
|
2005-06-08 09:18:34 +02:00
|
|
|
char *name;
|
|
|
|
struct data val;
|
|
|
|
|
|
|
|
proplen = flat_read_word(dtbuf);
|
|
|
|
stroff = flat_read_word(dtbuf);
|
|
|
|
|
|
|
|
name = flat_read_stringtable(strbuf, stroff);
|
|
|
|
|
|
|
|
if ((flags & FTF_VARALIGN) && (proplen >= 8))
|
|
|
|
flat_realign(dtbuf, 8);
|
|
|
|
|
|
|
|
val = flat_read_data(dtbuf, proplen);
|
|
|
|
|
annotations: add positions
Extend the parser to record positions, in build_node,
build_node_delete, and build_property.
srcpos structures are added to the property and node types, and to the
parameter lists of the above functions that construct these types.
Nodes and properties that are created by the compiler rather than from
parsing source code have NULL as the srcpos value.
merge_nodes, defined in livetree.c, uses srcpos_extend to combine
multiple positions, resulting in a list of positions. srcpos_extend
is defined in srcpos.c. New elements are added at the end. This
requires the srcpos type, define in srcpos.h, to be a list structure
with a next field. This next field is initialized to NULL in
srcpos.h, in the macro YYLLOC_DEFAULT invoked implicitly by the
generated parser code.
Another change to srcpos.c is to make srcpos_copy always do a full
copy, including a copy of the file substructure. This is required
because when dtc is used on the output of cpp, the successive detected
file names overwrite the file name in the file structure. The next
field does not need to be deep copied, because it is always NULL when
srcpos_copy is called; an assert checks for this. File names are only
updated in uncopied position structures.
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-11-16 17:29:59 +01:00
|
|
|
return build_property(name, val, NULL);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
|
2005-10-24 10:18:38 +02:00
|
|
|
static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
|
2005-07-15 09:14:24 +02:00
|
|
|
{
|
2005-10-24 10:18:38 +02:00
|
|
|
struct reserve_info *reservelist = NULL;
|
|
|
|
struct reserve_info *new;
|
2007-09-26 05:11:05 +02:00
|
|
|
struct fdt_reserve_entry re;
|
2005-07-15 09:14:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
|
|
|
|
* List terminates at an entry with size equal to zero.
|
|
|
|
*
|
|
|
|
* First pass, count entries.
|
|
|
|
*/
|
2005-08-29 05:36:15 +02:00
|
|
|
while (1) {
|
2017-03-06 02:08:53 +01:00
|
|
|
uint64_t address, size;
|
|
|
|
|
2005-08-29 05:36:15 +02:00
|
|
|
flat_read_chunk(inb, &re, sizeof(re));
|
2017-03-06 02:08:53 +01:00
|
|
|
address = fdt64_to_cpu(re.address);
|
|
|
|
size = fdt64_to_cpu(re.size);
|
|
|
|
if (size == 0)
|
2005-08-29 05:36:15 +02:00
|
|
|
break;
|
2005-07-15 09:14:24 +02:00
|
|
|
|
2017-03-06 02:08:53 +01:00
|
|
|
new = build_reserve_entry(address, size);
|
2005-10-24 10:18:38 +02:00
|
|
|
reservelist = add_reserve_entry(reservelist, new);
|
2005-08-29 05:36:15 +02:00
|
|
|
}
|
2005-07-15 09:14:24 +02:00
|
|
|
|
2005-10-24 10:18:38 +02:00
|
|
|
return reservelist;
|
2005-07-15 09:14:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-04 04:26:15 +01:00
|
|
|
static char *nodename_from_path(const char *ppath, const char *cpath)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
int plen;
|
|
|
|
|
2008-02-29 06:51:28 +01:00
|
|
|
plen = strlen(ppath);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2017-10-29 18:56:03 +01:00
|
|
|
if (!strstarts(cpath, ppath))
|
2008-02-29 06:51:28 +01:00
|
|
|
die("Path \"%s\" is not valid as a child of \"%s\"\n",
|
|
|
|
cpath, ppath);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2008-02-29 06:51:28 +01:00
|
|
|
/* root node is a special case */
|
|
|
|
if (!streq(ppath, "/"))
|
|
|
|
plen++;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2008-10-03 18:12:33 +02:00
|
|
|
return xstrdup(cpath + plen);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct node *unflatten_tree(struct inbuf *dtbuf,
|
|
|
|
struct inbuf *strbuf,
|
2008-02-29 06:51:28 +01:00
|
|
|
const char *parent_flatname, int flags)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
struct node *node;
|
2008-02-29 06:51:28 +01:00
|
|
|
char *flatname;
|
2008-06-25 05:53:07 +02:00
|
|
|
uint32_t val;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
annotations: add positions
Extend the parser to record positions, in build_node,
build_node_delete, and build_property.
srcpos structures are added to the property and node types, and to the
parameter lists of the above functions that construct these types.
Nodes and properties that are created by the compiler rather than from
parsing source code have NULL as the srcpos value.
merge_nodes, defined in livetree.c, uses srcpos_extend to combine
multiple positions, resulting in a list of positions. srcpos_extend
is defined in srcpos.c. New elements are added at the end. This
requires the srcpos type, define in srcpos.h, to be a list structure
with a next field. This next field is initialized to NULL in
srcpos.h, in the macro YYLLOC_DEFAULT invoked implicitly by the
generated parser code.
Another change to srcpos.c is to make srcpos_copy always do a full
copy, including a copy of the file substructure. This is required
because when dtc is used on the output of cpp, the successive detected
file names overwrite the file name in the file structure. The next
field does not need to be deep copied, because it is always NULL when
srcpos_copy is called; an assert checks for this. File names are only
updated in uncopied position structures.
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
2018-11-16 17:29:59 +01:00
|
|
|
node = build_node(NULL, NULL, NULL);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2008-02-29 06:51:28 +01:00
|
|
|
flatname = flat_read_string(dtbuf);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2008-02-29 06:51:28 +01:00
|
|
|
if (flags & FTF_FULLPATH)
|
|
|
|
node->name = nodename_from_path(parent_flatname, flatname);
|
|
|
|
else
|
|
|
|
node->name = flatname;
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
struct property *prop;
|
|
|
|
struct node *child;
|
|
|
|
|
|
|
|
val = flat_read_word(dtbuf);
|
|
|
|
switch (val) {
|
2007-09-26 05:11:05 +02:00
|
|
|
case FDT_PROP:
|
2007-09-04 02:43:03 +02:00
|
|
|
if (node->children)
|
|
|
|
fprintf(stderr, "Warning: Flat tree input has "
|
|
|
|
"subnodes preceding a property.\n");
|
2005-06-08 09:18:34 +02:00
|
|
|
prop = flat_read_property(dtbuf, strbuf, flags);
|
|
|
|
add_property(node, prop);
|
|
|
|
break;
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
case FDT_BEGIN_NODE:
|
2008-02-29 06:51:28 +01:00
|
|
|
child = unflatten_tree(dtbuf,strbuf, flatname, flags);
|
2005-06-08 09:18:34 +02:00
|
|
|
add_child(node, child);
|
|
|
|
break;
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
case FDT_END_NODE:
|
2005-06-08 09:18:34 +02:00
|
|
|
break;
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
case FDT_END:
|
|
|
|
die("Premature FDT_END in device tree blob\n");
|
2005-06-08 09:18:34 +02:00
|
|
|
break;
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
case FDT_NOP:
|
2007-06-26 03:30:47 +02:00
|
|
|
if (!(flags & FTF_NOPS))
|
|
|
|
fprintf(stderr, "Warning: NOP tag found in flat tree"
|
|
|
|
" version <16\n");
|
2007-06-10 06:21:31 +02:00
|
|
|
|
2007-06-26 03:30:47 +02:00
|
|
|
/* Ignore */
|
2007-06-10 06:21:31 +02:00
|
|
|
break;
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
default:
|
|
|
|
die("Invalid opcode word %08x in device tree blob\n",
|
|
|
|
val);
|
|
|
|
}
|
2007-09-26 05:11:05 +02:00
|
|
|
} while (val != FDT_END_NODE);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2016-07-11 00:16:52 +02:00
|
|
|
if (node->name != flatname) {
|
|
|
|
free(flatname);
|
|
|
|
}
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
|
2016-05-31 03:58:42 +02:00
|
|
|
struct dt_info *dt_from_blob(const char *fname)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2009-12-08 04:24:42 +01:00
|
|
|
FILE *f;
|
2017-03-06 02:08:53 +01:00
|
|
|
fdt32_t magic_buf, totalsize_buf;
|
2008-06-25 05:53:07 +02:00
|
|
|
uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
|
|
|
|
uint32_t off_dt, off_str, off_mem_rsvmap;
|
2005-06-08 09:18:34 +02:00
|
|
|
int rc;
|
|
|
|
char *blob;
|
2007-09-26 05:11:05 +02:00
|
|
|
struct fdt_header *fdt;
|
2005-06-08 09:18:34 +02:00
|
|
|
char *p;
|
|
|
|
struct inbuf dtbuf, strbuf;
|
2005-07-15 09:14:24 +02:00
|
|
|
struct inbuf memresvbuf;
|
2005-06-08 09:18:34 +02:00
|
|
|
int sizeleft;
|
2005-10-24 10:18:38 +02:00
|
|
|
struct reserve_info *reservelist;
|
2005-06-08 09:18:34 +02:00
|
|
|
struct node *tree;
|
2008-06-25 05:53:07 +02:00
|
|
|
uint32_t val;
|
2005-06-08 09:18:34 +02:00
|
|
|
int flags = 0;
|
|
|
|
|
2009-12-08 04:24:42 +01:00
|
|
|
f = srcfile_relative_open(fname, NULL);
|
2008-05-16 05:22:09 +02:00
|
|
|
|
2017-03-06 02:08:53 +01:00
|
|
|
rc = fread(&magic_buf, sizeof(magic_buf), 1, f);
|
2009-12-08 04:24:42 +01:00
|
|
|
if (ferror(f))
|
2005-06-08 09:18:34 +02:00
|
|
|
die("Error reading DT blob magic number: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
if (rc < 1) {
|
2009-12-08 04:24:42 +01:00
|
|
|
if (feof(f))
|
2005-06-08 09:18:34 +02:00
|
|
|
die("EOF reading DT blob magic number\n");
|
|
|
|
else
|
|
|
|
die("Mysterious short read reading magic number\n");
|
|
|
|
}
|
|
|
|
|
2017-03-06 02:08:53 +01:00
|
|
|
magic = fdt32_to_cpu(magic_buf);
|
2007-09-26 05:11:05 +02:00
|
|
|
if (magic != FDT_MAGIC)
|
2005-06-08 09:18:34 +02:00
|
|
|
die("Blob has incorrect magic number\n");
|
|
|
|
|
2017-03-06 02:08:53 +01:00
|
|
|
rc = fread(&totalsize_buf, sizeof(totalsize_buf), 1, f);
|
2009-12-08 04:24:42 +01:00
|
|
|
if (ferror(f))
|
2005-06-08 09:18:34 +02:00
|
|
|
die("Error reading DT blob size: %s\n", strerror(errno));
|
|
|
|
if (rc < 1) {
|
2009-12-08 04:24:42 +01:00
|
|
|
if (feof(f))
|
2005-06-08 09:18:34 +02:00
|
|
|
die("EOF reading DT blob size\n");
|
|
|
|
else
|
|
|
|
die("Mysterious short read reading blob size\n");
|
|
|
|
}
|
|
|
|
|
2017-03-06 02:08:53 +01:00
|
|
|
totalsize = fdt32_to_cpu(totalsize_buf);
|
2007-09-26 05:11:05 +02:00
|
|
|
if (totalsize < FDT_V1_SIZE)
|
2005-06-08 09:18:34 +02:00
|
|
|
die("DT blob size (%d) is too small\n", totalsize);
|
|
|
|
|
|
|
|
blob = xmalloc(totalsize);
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
fdt = (struct fdt_header *)blob;
|
2008-06-25 06:27:53 +02:00
|
|
|
fdt->magic = cpu_to_fdt32(magic);
|
|
|
|
fdt->totalsize = cpu_to_fdt32(totalsize);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
|
|
|
|
p = blob + sizeof(magic) + sizeof(totalsize);
|
|
|
|
|
|
|
|
while (sizeleft) {
|
2009-12-08 04:24:42 +01:00
|
|
|
if (feof(f))
|
2005-06-08 09:18:34 +02:00
|
|
|
die("EOF before reading %d bytes of DT blob\n",
|
|
|
|
totalsize);
|
|
|
|
|
2009-12-08 04:24:42 +01:00
|
|
|
rc = fread(p, 1, sizeleft, f);
|
|
|
|
if (ferror(f))
|
2005-06-08 09:18:34 +02:00
|
|
|
die("Error reading DT blob: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
sizeleft -= rc;
|
|
|
|
p += rc;
|
|
|
|
}
|
|
|
|
|
2008-06-25 06:27:53 +02:00
|
|
|
off_dt = fdt32_to_cpu(fdt->off_dt_struct);
|
|
|
|
off_str = fdt32_to_cpu(fdt->off_dt_strings);
|
|
|
|
off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap);
|
|
|
|
version = fdt32_to_cpu(fdt->version);
|
|
|
|
boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
if (off_mem_rsvmap >= totalsize)
|
|
|
|
die("Mem Reserve structure offset exceeds total size\n");
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
if (off_dt >= totalsize)
|
|
|
|
die("DT structure offset exceeds total size\n");
|
|
|
|
|
|
|
|
if (off_str > totalsize)
|
|
|
|
die("String table offset exceeds total size\n");
|
|
|
|
|
|
|
|
if (version >= 3) {
|
2008-06-25 06:27:53 +02:00
|
|
|
uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings);
|
2016-01-02 22:43:35 +01:00
|
|
|
if ((off_str+size_str < off_str) || (off_str+size_str > totalsize))
|
2005-06-08 09:18:34 +02:00
|
|
|
die("String table extends past total size\n");
|
2008-02-28 10:55:37 +01:00
|
|
|
inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
|
|
|
|
} else {
|
|
|
|
inbuf_init(&strbuf, blob + off_str, blob + totalsize);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
2007-03-14 01:02:40 +01:00
|
|
|
|
|
|
|
if (version >= 17) {
|
2008-06-25 06:27:53 +02:00
|
|
|
size_dt = fdt32_to_cpu(fdt->size_dt_struct);
|
2016-01-02 22:43:35 +01:00
|
|
|
if ((off_dt+size_dt < off_dt) || (off_dt+size_dt > totalsize))
|
2007-03-14 01:02:40 +01:00
|
|
|
die("Structure block extends past total size\n");
|
|
|
|
}
|
2007-09-18 03:44:04 +02:00
|
|
|
|
2007-03-14 01:02:40 +01:00
|
|
|
if (version < 16) {
|
2005-06-08 09:18:34 +02:00
|
|
|
flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
|
2007-06-10 06:21:31 +02:00
|
|
|
} else {
|
|
|
|
flags |= FTF_NOPS;
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
inbuf_init(&memresvbuf,
|
|
|
|
blob + off_mem_rsvmap, blob + totalsize);
|
2005-06-08 09:18:34 +02:00
|
|
|
inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
|
|
|
|
|
2005-10-24 10:18:38 +02:00
|
|
|
reservelist = flat_read_mem_reserve(&memresvbuf);
|
2005-07-15 09:14:24 +02:00
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
val = flat_read_word(&dtbuf);
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
if (val != FDT_BEGIN_NODE)
|
|
|
|
die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
|
|
|
|
|
|
|
|
val = flat_read_word(&dtbuf);
|
2007-09-26 05:11:05 +02:00
|
|
|
if (val != FDT_END)
|
|
|
|
die("Device tree blob doesn't end with FDT_END\n");
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
free(blob);
|
|
|
|
|
2009-12-08 04:24:42 +01:00
|
|
|
fclose(f);
|
2008-05-16 05:22:09 +02:00
|
|
|
|
2016-05-31 03:58:42 +02:00
|
|
|
return build_dt_info(DTSF_V1, reservelist, tree, boot_cpuid_phys);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|