blob: 0a1d81e5f7a0cf46cba88fe360a2afb73e3d2899 [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';
David Gibson2192d462008-03-17 17:03:47 +110078 strncpy(x, s + *i, 3);
David Gibsonfc14dad2005-06-08 17:18:34 +100079
80 val = strtol(x, &endx, 8);
David Gibson2192d462008-03-17 17:03:47 +110081
82 assert(endx > x);
David Gibson63dc9c72007-09-18 11:44:04 +100083
David Gibsonfc14dad2005-06-08 17:18:34 +100084 (*i) += endx - x;
85 return val;
86}
87
David Gibson92cb9a22007-12-04 14:26:15 +110088static char get_hex_char(const char *s, int *i)
David Gibsonfc14dad2005-06-08 17:18:34 +100089{
90 char x[3];
91 char *endx;
92 long val;
93
94 x[2] = '\0';
David Gibson2192d462008-03-17 17:03:47 +110095 strncpy(x, s + *i, 2);
David Gibsonfc14dad2005-06-08 17:18:34 +100096
97 val = strtol(x, &endx, 16);
David Gibson2192d462008-03-17 17:03:47 +110098 if (!(endx > x))
99 die("\\x used with no following hex digits\n");
David Gibson63dc9c72007-09-18 11:44:04 +1000100
David Gibsonfc14dad2005-06-08 17:18:34 +1000101 (*i) += endx - x;
102 return val;
103}
104
David Gibson92cb9a22007-12-04 14:26:15 +1100105struct data data_copy_escape_string(const char *s, int len)
David Gibsonfc14dad2005-06-08 17:18:34 +1000106{
107 int i = 0;
108 struct data d;
109 char *q;
110
111 d = data_grow_for(empty_data, strlen(s)+1);
112
113 q = d.val;
114 while (i < len) {
115 char c = s[i++];
116
117 if (c != '\\') {
118 q[d.len++] = c;
119 continue;
120 }
121
122 c = s[i++];
123 assert(c);
124 switch (c) {
David Gibsona756c122007-10-16 16:42:02 +1000125 case 'a':
126 q[d.len++] = '\a';
127 break;
128 case 'b':
129 q[d.len++] = '\b';
130 break;
David Gibsonfc14dad2005-06-08 17:18:34 +1000131 case 't':
132 q[d.len++] = '\t';
133 break;
134 case 'n':
135 q[d.len++] = '\n';
136 break;
David Gibsona756c122007-10-16 16:42:02 +1000137 case 'v':
138 q[d.len++] = '\v';
139 break;
140 case 'f':
141 q[d.len++] = '\f';
142 break;
David Gibsonfc14dad2005-06-08 17:18:34 +1000143 case 'r':
144 q[d.len++] = '\r';
145 break;
David Gibsonb4ac0492005-10-17 10:27:45 +1000146 case '0':
147 case '1':
148 case '2':
149 case '3':
150 case '4':
151 case '5':
152 case '6':
153 case '7':
David Gibsonfc14dad2005-06-08 17:18:34 +1000154 i--; /* need to re-read the first digit as
155 * part of the octal value */
156 q[d.len++] = get_oct_char(s, &i);
157 break;
158 case 'x':
159 q[d.len++] = get_hex_char(s, &i);
160 break;
161 default:
162 q[d.len++] = c;
163 }
164 }
165
166 q[d.len++] = '\0';
167 return d;
168}
169
170struct data data_copy_file(FILE *f, size_t len)
171{
172 struct data d;
173
174 d = data_grow_for(empty_data, len);
175
176 d.len = len;
177 fread(d.val, len, 1, f);
178
179 return d;
180}
181
David Gibson92cb9a22007-12-04 14:26:15 +1100182struct data data_append_data(struct data d, const void *p, int len)
David Gibsonfc14dad2005-06-08 17:18:34 +1000183{
184 d = data_grow_for(d, len);
185 memcpy(d.val + d.len, p, len);
186 d.len += len;
187 return d;
188}
189
David Gibsonefbbef82007-12-05 10:43:50 +1100190struct data data_insert_at_marker(struct data d, struct marker *m,
191 const void *p, int len)
192{
193 d = data_grow_for(d, len);
194 memmove(d.val + m->offset + len, d.val + m->offset, d.len - m->offset);
195 memcpy(d.val + m->offset, p, len);
196 d.len += len;
197
198 /* Adjust all markers after the one we're inserting at */
199 m = m->next;
200 for_each_marker(m)
201 m->offset += len;
202 return d;
203}
204
David Gibsondc941772007-11-22 14:39:23 +1100205struct data data_append_markers(struct data d, struct marker *m)
David Gibson32da4752007-02-07 16:37:50 +1100206{
David Gibsondc941772007-11-22 14:39:23 +1100207 struct marker **mp = &d.markers;
David Gibson32da4752007-02-07 16:37:50 +1100208
David Gibsondc941772007-11-22 14:39:23 +1100209 /* Find the end of the markerlist */
210 while (*mp)
211 mp = &((*mp)->next);
212 *mp = m;
213 return d;
Milton Miller6a99b132007-07-07 01:18:51 -0500214}
215
216struct data data_merge(struct data d1, struct data d2)
217{
218 struct data d;
David Gibsondc941772007-11-22 14:39:23 +1100219 struct marker *m2 = d2.markers;
Milton Miller6a99b132007-07-07 01:18:51 -0500220
David Gibsondc941772007-11-22 14:39:23 +1100221 d = data_append_markers(data_append_data(d1, d2.val, d2.len), m2);
Milton Miller6a99b132007-07-07 01:18:51 -0500222
David Gibsondc941772007-11-22 14:39:23 +1100223 /* Adjust for the length of d1 */
224 for_each_marker(m2)
225 m2->offset += d1.len;
David Gibson32da4752007-02-07 16:37:50 +1100226
David Gibsondc941772007-11-22 14:39:23 +1100227 d2.markers = NULL; /* So data_free() doesn't clobber them */
David Gibson32da4752007-02-07 16:37:50 +1100228 data_free(d2);
229
230 return d;
231}
232
David Gibsonfc14dad2005-06-08 17:18:34 +1000233struct data data_append_cell(struct data d, cell_t word)
234{
235 cell_t beword = cpu_to_be32(word);
236
237 return data_append_data(d, &beword, sizeof(beword));
238}
239
David Gibson92cb9a22007-12-04 14:26:15 +1100240struct data data_append_re(struct data d, const struct fdt_reserve_entry *re)
David Gibsonf040d952005-10-24 18:18:38 +1000241{
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000242 struct fdt_reserve_entry bere;
David Gibsonf040d952005-10-24 18:18:38 +1000243
244 bere.address = cpu_to_be64(re->address);
245 bere.size = cpu_to_be64(re->size);
246
247 return data_append_data(d, &bere, sizeof(bere));
248}
249
David Gibsonf0517db2005-07-15 17:14:24 +1000250struct data data_append_addr(struct data d, u64 addr)
251{
252 u64 beaddr = cpu_to_be64(addr);
253
254 return data_append_data(d, &beaddr, sizeof(beaddr));
255}
256
David Gibsonfc14dad2005-06-08 17:18:34 +1000257struct data data_append_byte(struct data d, uint8_t byte)
258{
259 return data_append_data(d, &byte, 1);
260}
261
262struct data data_append_zeroes(struct data d, int len)
263{
264 d = data_grow_for(d, len);
265
266 memset(d.val + d.len, 0, len);
267 d.len += len;
268 return d;
269}
270
271struct data data_append_align(struct data d, int align)
272{
273 int newlen = ALIGN(d.len, align);
274 return data_append_zeroes(d, newlen - d.len);
275}
276
David Gibsondc941772007-11-22 14:39:23 +1100277struct data data_add_marker(struct data d, enum markertype type, char *ref)
David Gibson81f2e892005-06-16 17:04:00 +1000278{
David Gibsondc941772007-11-22 14:39:23 +1100279 struct marker *m;
David Gibson81f2e892005-06-16 17:04:00 +1000280
David Gibsondc941772007-11-22 14:39:23 +1100281 m = xmalloc(sizeof(*m));
282 m->offset = d.len;
283 m->type = type;
284 m->ref = ref;
285 m->next = NULL;
David Gibson81f2e892005-06-16 17:04:00 +1000286
David Gibsondc941772007-11-22 14:39:23 +1100287 return data_append_markers(d, m);
Milton Miller6a99b132007-07-07 01:18:51 -0500288}
289
David Gibsonfc14dad2005-06-08 17:18:34 +1000290int data_is_one_string(struct data d)
291{
292 int i;
293 int len = d.len;
294
295 if (len == 0)
296 return 0;
297
298 for (i = 0; i < len-1; i++)
299 if (d.val[i] == '\0')
300 return 0;
301
302 if (d.val[len-1] != '\0')
303 return 0;
304
305 return 1;
306}