blob: 0ee1010bcf33732934955e593c01d566f940168a [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"
22
David Gibsonfc14dad2005-06-08 17:18:34 +100023void data_free(struct data d)
24{
David Gibsondc941772007-11-22 14:39:23 +110025 struct marker *m, *nm;
David Gibson81f2e892005-06-16 17:04:00 +100026
David Gibsondc941772007-11-22 14:39:23 +110027 m = d.markers;
28 while (m) {
29 nm = m->next;
30 free(m->ref);
31 free(m);
32 m = nm;
David Gibson81f2e892005-06-16 17:04:00 +100033 }
34
David Gibsonfc14dad2005-06-08 17:18:34 +100035 if (d.val)
36 free(d.val);
37}
38
39struct data data_grow_for(struct data d, int xlen)
40{
41 struct data nd;
42 int newsize;
43
David Gibsonfc14dad2005-06-08 17:18:34 +100044 if (xlen == 0)
45 return d;
46
Milton Miller46779e82007-07-07 01:18:49 -050047 nd = d;
48
David Gibsonfc14dad2005-06-08 17:18:34 +100049 newsize = xlen;
50
51 while ((d.len + xlen) > newsize)
52 newsize *= 2;
53
David Gibsonfc14dad2005-06-08 17:18:34 +100054 nd.val = xrealloc(d.val, newsize);
David Gibsonfc14dad2005-06-08 17:18:34 +100055
David Gibsonfc14dad2005-06-08 17:18:34 +100056 return nd;
57}
58
David Gibson92cb9a22007-12-04 14:26:15 +110059struct data data_copy_mem(const char *mem, int len)
David Gibsonfc14dad2005-06-08 17:18:34 +100060{
61 struct data d;
62
63 d = data_grow_for(empty_data, len);
64
65 d.len = len;
66 memcpy(d.val, mem, len);
67
68 return d;
69}
70
David Gibson92cb9a22007-12-04 14:26:15 +110071static char get_oct_char(const char *s, int *i)
David Gibsonfc14dad2005-06-08 17:18:34 +100072{
73 char x[4];
74 char *endx;
75 long val;
76
77 x[3] = '\0';
78 x[0] = s[(*i)];
79 if (x[0]) {
80 x[1] = s[(*i)+1];
81 if (x[1])
82 x[2] = s[(*i)+2];
83 }
84
85 val = strtol(x, &endx, 8);
86 if ((endx - x) == 0)
87 fprintf(stderr, "Empty \\nnn escape\n");
David Gibson63dc9c72007-09-18 11:44:04 +100088
David Gibsonfc14dad2005-06-08 17:18:34 +100089 (*i) += endx - x;
90 return val;
91}
92
David Gibson92cb9a22007-12-04 14:26:15 +110093static char get_hex_char(const char *s, int *i)
David Gibsonfc14dad2005-06-08 17:18:34 +100094{
95 char x[3];
96 char *endx;
97 long val;
98
99 x[2] = '\0';
100 x[0] = s[(*i)];
101 if (x[0])
102 x[1] = s[(*i)+1];
103
104 val = strtol(x, &endx, 16);
105 if ((endx - x) == 0)
106 fprintf(stderr, "Empty \\x escape\n");
David Gibson63dc9c72007-09-18 11:44:04 +1000107
David Gibsonfc14dad2005-06-08 17:18:34 +1000108 (*i) += endx - x;
109 return val;
110}
111
David Gibson92cb9a22007-12-04 14:26:15 +1100112struct data data_copy_escape_string(const char *s, int len)
David Gibsonfc14dad2005-06-08 17:18:34 +1000113{
114 int i = 0;
115 struct data d;
116 char *q;
117
118 d = data_grow_for(empty_data, strlen(s)+1);
119
120 q = d.val;
121 while (i < len) {
122 char c = s[i++];
123
124 if (c != '\\') {
125 q[d.len++] = c;
126 continue;
127 }
128
129 c = s[i++];
130 assert(c);
131 switch (c) {
David Gibsona756c122007-10-16 16:42:02 +1000132 case 'a':
133 q[d.len++] = '\a';
134 break;
135 case 'b':
136 q[d.len++] = '\b';
137 break;
David Gibsonfc14dad2005-06-08 17:18:34 +1000138 case 't':
139 q[d.len++] = '\t';
140 break;
141 case 'n':
142 q[d.len++] = '\n';
143 break;
David Gibsona756c122007-10-16 16:42:02 +1000144 case 'v':
145 q[d.len++] = '\v';
146 break;
147 case 'f':
148 q[d.len++] = '\f';
149 break;
David Gibsonfc14dad2005-06-08 17:18:34 +1000150 case 'r':
151 q[d.len++] = '\r';
152 break;
David Gibsonb4ac0492005-10-17 10:27:45 +1000153 case '0':
154 case '1':
155 case '2':
156 case '3':
157 case '4':
158 case '5':
159 case '6':
160 case '7':
David Gibsonfc14dad2005-06-08 17:18:34 +1000161 i--; /* need to re-read the first digit as
162 * part of the octal value */
163 q[d.len++] = get_oct_char(s, &i);
164 break;
165 case 'x':
166 q[d.len++] = get_hex_char(s, &i);
167 break;
168 default:
169 q[d.len++] = c;
170 }
171 }
172
173 q[d.len++] = '\0';
174 return d;
175}
176
177struct data data_copy_file(FILE *f, size_t len)
178{
179 struct data d;
180
181 d = data_grow_for(empty_data, len);
182
183 d.len = len;
184 fread(d.val, len, 1, f);
185
186 return d;
187}
188
David Gibson92cb9a22007-12-04 14:26:15 +1100189struct data data_append_data(struct data d, const void *p, int len)
David Gibsonfc14dad2005-06-08 17:18:34 +1000190{
191 d = data_grow_for(d, len);
192 memcpy(d.val + d.len, p, len);
193 d.len += len;
194 return d;
195}
196
David Gibsonefbbef82007-12-05 10:43:50 +1100197struct data data_insert_at_marker(struct data d, struct marker *m,
198 const void *p, int len)
199{
200 d = data_grow_for(d, len);
201 memmove(d.val + m->offset + len, d.val + m->offset, d.len - m->offset);
202 memcpy(d.val + m->offset, p, len);
203 d.len += len;
204
205 /* Adjust all markers after the one we're inserting at */
206 m = m->next;
207 for_each_marker(m)
208 m->offset += len;
209 return d;
210}
211
David Gibsondc941772007-11-22 14:39:23 +1100212struct data data_append_markers(struct data d, struct marker *m)
David Gibson32da4752007-02-07 16:37:50 +1100213{
David Gibsondc941772007-11-22 14:39:23 +1100214 struct marker **mp = &d.markers;
David Gibson32da4752007-02-07 16:37:50 +1100215
David Gibsondc941772007-11-22 14:39:23 +1100216 /* Find the end of the markerlist */
217 while (*mp)
218 mp = &((*mp)->next);
219 *mp = m;
220 return d;
Milton Miller6a99b132007-07-07 01:18:51 -0500221}
222
223struct data data_merge(struct data d1, struct data d2)
224{
225 struct data d;
David Gibsondc941772007-11-22 14:39:23 +1100226 struct marker *m2 = d2.markers;
Milton Miller6a99b132007-07-07 01:18:51 -0500227
David Gibsondc941772007-11-22 14:39:23 +1100228 d = data_append_markers(data_append_data(d1, d2.val, d2.len), m2);
Milton Miller6a99b132007-07-07 01:18:51 -0500229
David Gibsondc941772007-11-22 14:39:23 +1100230 /* Adjust for the length of d1 */
231 for_each_marker(m2)
232 m2->offset += d1.len;
David Gibson32da4752007-02-07 16:37:50 +1100233
David Gibsondc941772007-11-22 14:39:23 +1100234 d2.markers = NULL; /* So data_free() doesn't clobber them */
David Gibson32da4752007-02-07 16:37:50 +1100235 data_free(d2);
236
237 return d;
238}
239
David Gibsonfc14dad2005-06-08 17:18:34 +1000240struct data data_append_cell(struct data d, cell_t word)
241{
242 cell_t beword = cpu_to_be32(word);
243
244 return data_append_data(d, &beword, sizeof(beword));
245}
246
David Gibson92cb9a22007-12-04 14:26:15 +1100247struct data data_append_re(struct data d, const struct fdt_reserve_entry *re)
David Gibsonf040d952005-10-24 18:18:38 +1000248{
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000249 struct fdt_reserve_entry bere;
David Gibsonf040d952005-10-24 18:18:38 +1000250
251 bere.address = cpu_to_be64(re->address);
252 bere.size = cpu_to_be64(re->size);
253
254 return data_append_data(d, &bere, sizeof(bere));
255}
256
David Gibsonf0517db2005-07-15 17:14:24 +1000257struct data data_append_addr(struct data d, u64 addr)
258{
259 u64 beaddr = cpu_to_be64(addr);
260
261 return data_append_data(d, &beaddr, sizeof(beaddr));
262}
263
David Gibsonfc14dad2005-06-08 17:18:34 +1000264struct data data_append_byte(struct data d, uint8_t byte)
265{
266 return data_append_data(d, &byte, 1);
267}
268
269struct data data_append_zeroes(struct data d, int len)
270{
271 d = data_grow_for(d, len);
272
273 memset(d.val + d.len, 0, len);
274 d.len += len;
275 return d;
276}
277
278struct data data_append_align(struct data d, int align)
279{
280 int newlen = ALIGN(d.len, align);
281 return data_append_zeroes(d, newlen - d.len);
282}
283
David Gibsondc941772007-11-22 14:39:23 +1100284struct data data_add_marker(struct data d, enum markertype type, char *ref)
David Gibson81f2e892005-06-16 17:04:00 +1000285{
David Gibsondc941772007-11-22 14:39:23 +1100286 struct marker *m;
David Gibson81f2e892005-06-16 17:04:00 +1000287
David Gibsondc941772007-11-22 14:39:23 +1100288 m = xmalloc(sizeof(*m));
289 m->offset = d.len;
290 m->type = type;
291 m->ref = ref;
292 m->next = NULL;
David Gibson81f2e892005-06-16 17:04:00 +1000293
David Gibsondc941772007-11-22 14:39:23 +1100294 return data_append_markers(d, m);
Milton Miller6a99b132007-07-07 01:18:51 -0500295}
296
David Gibsonfc14dad2005-06-08 17:18:34 +1000297int data_is_one_string(struct data d)
298{
299 int i;
300 int len = d.len;
301
302 if (len == 0)
303 return 0;
304
305 for (i = 0; i < len-1; i++)
306 if (d.val[i] == '\0')
307 return 0;
308
309 if (d.val[len-1] != '\0')
310 return 0;
311
312 return 1;
313}