2005-06-08 09:18:34 +02:00
|
|
|
/*
|
|
|
|
* (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
|
|
|
|
*
|
2007-09-18 03:44:04 +02:00
|
|
|
*
|
2005-06-08 09:18:34 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
2007-09-18 03:44:04 +02:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA
|
2005-06-08 09:18:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dtc.h"
|
|
|
|
|
|
|
|
void data_free(struct data d)
|
|
|
|
{
|
2007-11-22 04:39:23 +01:00
|
|
|
struct marker *m, *nm;
|
|
|
|
|
|
|
|
m = d.markers;
|
|
|
|
while (m) {
|
|
|
|
nm = m->next;
|
|
|
|
free(m->ref);
|
|
|
|
free(m);
|
|
|
|
m = nm;
|
2005-06-16 09:04:00 +02:00
|
|
|
}
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
assert(!d.val || d.asize);
|
|
|
|
|
|
|
|
if (d.val)
|
|
|
|
free(d.val);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct data data_grow_for(struct data d, int xlen)
|
|
|
|
{
|
|
|
|
struct data nd;
|
|
|
|
int newsize;
|
|
|
|
|
|
|
|
/* we must start with an allocated datum */
|
|
|
|
assert(!d.val || d.asize);
|
|
|
|
|
|
|
|
if (xlen == 0)
|
|
|
|
return d;
|
|
|
|
|
2007-07-07 08:18:49 +02:00
|
|
|
nd = d;
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
newsize = xlen;
|
|
|
|
|
|
|
|
while ((d.len + xlen) > newsize)
|
|
|
|
newsize *= 2;
|
|
|
|
|
|
|
|
nd.asize = newsize;
|
|
|
|
nd.val = xrealloc(d.val, newsize);
|
|
|
|
|
|
|
|
assert(nd.asize >= (d.len + xlen));
|
|
|
|
|
|
|
|
return nd;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct data data_copy_mem(char *mem, int len)
|
|
|
|
{
|
|
|
|
struct data d;
|
|
|
|
|
|
|
|
d = data_grow_for(empty_data, len);
|
|
|
|
|
|
|
|
d.len = len;
|
|
|
|
memcpy(d.val, mem, len);
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2005-08-29 04:48:02 +02:00
|
|
|
static char get_oct_char(char *s, int *i)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
char x[4];
|
|
|
|
char *endx;
|
|
|
|
long val;
|
|
|
|
|
|
|
|
x[3] = '\0';
|
|
|
|
x[0] = s[(*i)];
|
|
|
|
if (x[0]) {
|
|
|
|
x[1] = s[(*i)+1];
|
|
|
|
if (x[1])
|
|
|
|
x[2] = s[(*i)+2];
|
|
|
|
}
|
|
|
|
|
|
|
|
val = strtol(x, &endx, 8);
|
|
|
|
if ((endx - x) == 0)
|
|
|
|
fprintf(stderr, "Empty \\nnn escape\n");
|
2007-09-18 03:44:04 +02:00
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
(*i) += endx - x;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2005-08-29 04:48:02 +02:00
|
|
|
static char get_hex_char(char *s, int *i)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
char x[3];
|
|
|
|
char *endx;
|
|
|
|
long val;
|
|
|
|
|
|
|
|
x[2] = '\0';
|
|
|
|
x[0] = s[(*i)];
|
|
|
|
if (x[0])
|
|
|
|
x[1] = s[(*i)+1];
|
|
|
|
|
|
|
|
val = strtol(x, &endx, 16);
|
|
|
|
if ((endx - x) == 0)
|
|
|
|
fprintf(stderr, "Empty \\x escape\n");
|
2007-09-18 03:44:04 +02:00
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
(*i) += endx - x;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct data data_copy_escape_string(char *s, int len)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
struct data d;
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
d = data_grow_for(empty_data, strlen(s)+1);
|
|
|
|
|
|
|
|
q = d.val;
|
|
|
|
while (i < len) {
|
|
|
|
char c = s[i++];
|
|
|
|
|
|
|
|
if (c != '\\') {
|
|
|
|
q[d.len++] = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = s[i++];
|
|
|
|
assert(c);
|
|
|
|
switch (c) {
|
2007-10-16 08:42:02 +02:00
|
|
|
case 'a':
|
|
|
|
q[d.len++] = '\a';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
q[d.len++] = '\b';
|
|
|
|
break;
|
2005-06-08 09:18:34 +02:00
|
|
|
case 't':
|
|
|
|
q[d.len++] = '\t';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
q[d.len++] = '\n';
|
|
|
|
break;
|
2007-10-16 08:42:02 +02:00
|
|
|
case 'v':
|
|
|
|
q[d.len++] = '\v';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
q[d.len++] = '\f';
|
|
|
|
break;
|
2005-06-08 09:18:34 +02:00
|
|
|
case 'r':
|
|
|
|
q[d.len++] = '\r';
|
|
|
|
break;
|
2005-10-17 02:27:45 +02:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
2005-06-08 09:18:34 +02:00
|
|
|
i--; /* need to re-read the first digit as
|
|
|
|
* part of the octal value */
|
|
|
|
q[d.len++] = get_oct_char(s, &i);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
q[d.len++] = get_hex_char(s, &i);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
q[d.len++] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
q[d.len++] = '\0';
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct data data_copy_file(FILE *f, size_t len)
|
|
|
|
{
|
|
|
|
struct data d;
|
|
|
|
|
|
|
|
d = data_grow_for(empty_data, len);
|
|
|
|
|
|
|
|
d.len = len;
|
|
|
|
fread(d.val, len, 1, f);
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct data data_append_data(struct data d, void *p, int len)
|
|
|
|
{
|
|
|
|
d = data_grow_for(d, len);
|
|
|
|
memcpy(d.val + d.len, p, len);
|
|
|
|
d.len += len;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2007-11-22 04:39:23 +01:00
|
|
|
struct data data_append_markers(struct data d, struct marker *m)
|
2007-02-07 06:37:50 +01:00
|
|
|
{
|
2007-11-22 04:39:23 +01:00
|
|
|
struct marker **mp = &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
|
|
|
|
2007-11-22 04:39:23 +01:00
|
|
|
/* Find the end of the markerlist */
|
|
|
|
while (*mp)
|
|
|
|
mp = &((*mp)->next);
|
|
|
|
*mp = m;
|
|
|
|
return d;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
struct data data_merge(struct data d1, struct data d2)
|
|
|
|
{
|
|
|
|
struct data d;
|
2007-11-22 04:39:23 +01:00
|
|
|
struct marker *m2 = d2.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
|
|
|
|
2007-11-22 04:39:23 +01:00
|
|
|
d = data_append_markers(data_append_data(d1, d2.val, d2.len), m2);
|
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
|
|
|
|
2007-11-22 04:39:23 +01:00
|
|
|
/* Adjust for the length of d1 */
|
|
|
|
for_each_marker(m2)
|
|
|
|
m2->offset += d1.len;
|
2007-02-07 06:37:50 +01:00
|
|
|
|
2007-11-22 04:39:23 +01:00
|
|
|
d2.markers = NULL; /* So data_free() doesn't clobber them */
|
2007-02-07 06:37:50 +01:00
|
|
|
data_free(d2);
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
struct data data_append_cell(struct data d, cell_t word)
|
|
|
|
{
|
|
|
|
cell_t beword = cpu_to_be32(word);
|
|
|
|
|
|
|
|
return data_append_data(d, &beword, sizeof(beword));
|
|
|
|
}
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
struct data data_append_re(struct data d, struct fdt_reserve_entry *re)
|
2005-10-24 10:18:38 +02:00
|
|
|
{
|
2007-09-26 05:11:05 +02:00
|
|
|
struct fdt_reserve_entry bere;
|
2005-10-24 10:18:38 +02:00
|
|
|
|
|
|
|
bere.address = cpu_to_be64(re->address);
|
|
|
|
bere.size = cpu_to_be64(re->size);
|
|
|
|
|
|
|
|
return data_append_data(d, &bere, sizeof(bere));
|
|
|
|
}
|
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
struct data data_append_addr(struct data d, u64 addr)
|
|
|
|
{
|
|
|
|
u64 beaddr = cpu_to_be64(addr);
|
|
|
|
|
|
|
|
return data_append_data(d, &beaddr, sizeof(beaddr));
|
|
|
|
}
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
struct data data_append_byte(struct data d, uint8_t byte)
|
|
|
|
{
|
|
|
|
return data_append_data(d, &byte, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct data data_append_zeroes(struct data d, int len)
|
|
|
|
{
|
|
|
|
d = data_grow_for(d, len);
|
|
|
|
|
|
|
|
memset(d.val + d.len, 0, len);
|
|
|
|
d.len += len;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct data data_append_align(struct data d, int align)
|
|
|
|
{
|
|
|
|
int newlen = ALIGN(d.len, align);
|
|
|
|
return data_append_zeroes(d, newlen - d.len);
|
|
|
|
}
|
|
|
|
|
2007-11-22 04:39:23 +01:00
|
|
|
struct data data_add_marker(struct data d, enum markertype type, char *ref)
|
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
|
|
|
{
|
2007-11-22 04:39:23 +01:00
|
|
|
struct marker *m;
|
2007-07-07 08:18:52 +02:00
|
|
|
|
2007-11-22 04:39:23 +01:00
|
|
|
m = xmalloc(sizeof(*m));
|
|
|
|
m->offset = d.len;
|
|
|
|
m->type = type;
|
|
|
|
m->ref = ref;
|
|
|
|
m->next = NULL;
|
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
|
|
|
|
2007-11-22 04:39:23 +01:00
|
|
|
return data_append_markers(d, m);
|
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
|
|
|
}
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
int data_is_one_string(struct data d)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int len = d.len;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < len-1; i++)
|
|
|
|
if (d.val[i] == '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (d.val[len-1] != '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|