blob: 1907a1a91701ad2d76dda123947ec6dcc2132853 [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
4 *
5 * 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.
14 *
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
19 */
20
21#include "dtc.h"
22
David Gibson81f2e892005-06-16 17:04:00 +100023void fixup_free(struct fixup *f)
24{
25 free(f->ref);
26 free(f);
27}
28
David Gibsonfc14dad2005-06-08 17:18:34 +100029void data_free(struct data d)
30{
David Gibson81f2e892005-06-16 17:04:00 +100031 struct fixup *f;
32
33 f = d.refs;
34 while (f) {
35 struct fixup *nf;
36
37 nf = f->next;
38 fixup_free(f);
39 f = nf;
40 }
41
David Gibsonfc14dad2005-06-08 17:18:34 +100042 assert(!d.val || d.asize);
43
44 if (d.val)
45 free(d.val);
46}
47
48struct data data_grow_for(struct data d, int xlen)
49{
50 struct data nd;
51 int newsize;
52
53 /* we must start with an allocated datum */
54 assert(!d.val || d.asize);
55
56 if (xlen == 0)
57 return d;
58
59 newsize = xlen;
60
61 while ((d.len + xlen) > newsize)
62 newsize *= 2;
63
64 nd.asize = newsize;
65 nd.val = xrealloc(d.val, newsize);
66 nd.len = d.len;
David Gibson81f2e892005-06-16 17:04:00 +100067 nd.refs = d.refs;
David Gibsonfc14dad2005-06-08 17:18:34 +100068
69 assert(nd.asize >= (d.len + xlen));
70
71 return nd;
72}
73
74struct data data_copy_mem(char *mem, int len)
75{
76 struct data d;
77
78 d = data_grow_for(empty_data, len);
79
80 d.len = len;
81 memcpy(d.val, mem, len);
82
83 return d;
84}
85
David Gibson230f2532005-08-29 12:48:02 +100086static char get_oct_char(char *s, int *i)
David Gibsonfc14dad2005-06-08 17:18:34 +100087{
88 char x[4];
89 char *endx;
90 long val;
91
92 x[3] = '\0';
93 x[0] = s[(*i)];
94 if (x[0]) {
95 x[1] = s[(*i)+1];
96 if (x[1])
97 x[2] = s[(*i)+2];
98 }
99
100 val = strtol(x, &endx, 8);
101 if ((endx - x) == 0)
102 fprintf(stderr, "Empty \\nnn escape\n");
103
104 (*i) += endx - x;
105 return val;
106}
107
David Gibson230f2532005-08-29 12:48:02 +1000108static char get_hex_char(char *s, int *i)
David Gibsonfc14dad2005-06-08 17:18:34 +1000109{
110 char x[3];
111 char *endx;
112 long val;
113
114 x[2] = '\0';
115 x[0] = s[(*i)];
116 if (x[0])
117 x[1] = s[(*i)+1];
118
119 val = strtol(x, &endx, 16);
120 if ((endx - x) == 0)
121 fprintf(stderr, "Empty \\x escape\n");
122
123 (*i) += endx - x;
124 return val;
125}
126
127struct data data_copy_escape_string(char *s, int len)
128{
129 int i = 0;
130 struct data d;
131 char *q;
132
133 d = data_grow_for(empty_data, strlen(s)+1);
134
135 q = d.val;
136 while (i < len) {
137 char c = s[i++];
138
139 if (c != '\\') {
140 q[d.len++] = c;
141 continue;
142 }
143
144 c = s[i++];
145 assert(c);
146 switch (c) {
147 case 't':
148 q[d.len++] = '\t';
149 break;
150 case 'n':
151 q[d.len++] = '\n';
152 break;
153 case 'r':
154 q[d.len++] = '\r';
155 break;
David Gibsonb4ac0492005-10-17 10:27:45 +1000156 case '0':
157 case '1':
158 case '2':
159 case '3':
160 case '4':
161 case '5':
162 case '6':
163 case '7':
David Gibsonfc14dad2005-06-08 17:18:34 +1000164 i--; /* need to re-read the first digit as
165 * part of the octal value */
166 q[d.len++] = get_oct_char(s, &i);
167 break;
168 case 'x':
169 q[d.len++] = get_hex_char(s, &i);
170 break;
171 default:
172 q[d.len++] = c;
173 }
174 }
175
176 q[d.len++] = '\0';
177 return d;
178}
179
180struct data data_copy_file(FILE *f, size_t len)
181{
182 struct data d;
183
184 d = data_grow_for(empty_data, len);
185
186 d.len = len;
187 fread(d.val, len, 1, f);
188
189 return d;
190}
191
192struct data data_append_data(struct data d, void *p, int len)
193{
194 d = data_grow_for(d, len);
195 memcpy(d.val + d.len, p, len);
196 d.len += len;
197 return d;
198}
199
David Gibson32da4752007-02-07 16:37:50 +1100200struct data data_merge(struct data d1, struct data d2)
201{
202 struct data d;
203 struct fixup **ff;
204 struct fixup *f, *f2;
205
206 d = data_append_data(d1, d2.val, d2.len);
207
208 /* Extract d2's fixups */
209 f2 = d2.refs;
210 d2.refs = NULL;
211
212 /* Tack them onto d's list of fixups */
213 ff = &d.refs;
214 while (*ff)
215 ff = &((*ff)->next);
216 *ff = f2;
217
218 /* And correct them for their new position */
219 for (f = f2; f; f = f->next)
220 f->offset += d1.len;
221
222 data_free(d2);
223
224 return d;
225}
226
David Gibsonfc14dad2005-06-08 17:18:34 +1000227struct data data_append_cell(struct data d, cell_t word)
228{
229 cell_t beword = cpu_to_be32(word);
230
231 return data_append_data(d, &beword, sizeof(beword));
232}
233
David Gibsonf040d952005-10-24 18:18:38 +1000234struct data data_append_re(struct data d, struct reserve_entry *re)
235{
236 struct reserve_entry bere;
237
238 bere.address = cpu_to_be64(re->address);
239 bere.size = cpu_to_be64(re->size);
240
241 return data_append_data(d, &bere, sizeof(bere));
242}
243
David Gibsonf0517db2005-07-15 17:14:24 +1000244struct data data_append_addr(struct data d, u64 addr)
245{
246 u64 beaddr = cpu_to_be64(addr);
247
248 return data_append_data(d, &beaddr, sizeof(beaddr));
249}
250
David Gibsonfc14dad2005-06-08 17:18:34 +1000251struct data data_append_byte(struct data d, uint8_t byte)
252{
253 return data_append_data(d, &byte, 1);
254}
255
256struct data data_append_zeroes(struct data d, int len)
257{
258 d = data_grow_for(d, len);
259
260 memset(d.val + d.len, 0, len);
261 d.len += len;
262 return d;
263}
264
265struct data data_append_align(struct data d, int align)
266{
267 int newlen = ALIGN(d.len, align);
268 return data_append_zeroes(d, newlen - d.len);
269}
270
David Gibson81f2e892005-06-16 17:04:00 +1000271struct data data_add_fixup(struct data d, char *ref)
272{
273 struct fixup *f;
274 struct data nd;
275
276 f = xmalloc(sizeof(*f));
277 f->offset = d.len;
278 f->ref = ref;
279 f->next = d.refs;
280
281 nd = d;
282 nd.refs = f;
283
284 return nd;
285}
286
David Gibsonfc14dad2005-06-08 17:18:34 +1000287int data_is_one_string(struct data d)
288{
289 int i;
290 int len = d.len;
291
292 if (len == 0)
293 return 0;
294
295 for (i = 0; i < len-1; i++)
296 if (d.val[i] == '\0')
297 return 0;
298
299 if (d.val[len-1] != '\0')
300 return 0;
301
302 return 1;
303}