blob: aa37a16c889186aff62274ec708e89dea7dbd971 [file] [log] [blame]
David Gibsona4da2e32007-12-18 15:06:42 +11001/*
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
23void data_free(struct data d)
24{
25 struct marker *m, *nm;
26
27 m = d.markers;
28 while (m) {
29 nm = m->next;
30 free(m->ref);
31 free(m);
32 m = nm;
33 }
34
David Gibsona4da2e32007-12-18 15:06:42 +110035 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 Gibsona4da2e32007-12-18 15:06:42 +110044 if (xlen == 0)
45 return d;
46
47 nd = d;
48
49 newsize = xlen;
50
51 while ((d.len + xlen) > newsize)
52 newsize *= 2;
53
David Gibsona4da2e32007-12-18 15:06:42 +110054 nd.val = xrealloc(d.val, newsize);
55
David Gibsona4da2e32007-12-18 15:06:42 +110056 return nd;
57}
58
59struct data data_copy_mem(const char *mem, int len)
60{
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 Gibsona4da2e32007-12-18 15:06:42 +110071struct data data_copy_escape_string(const char *s, int len)
72{
73 int i = 0;
74 struct data d;
75 char *q;
76
Rob Herring47605972015-04-29 16:00:05 -050077 d = data_grow_for(empty_data, len + 1);
David Gibsona4da2e32007-12-18 15:06:42 +110078
79 q = d.val;
80 while (i < len) {
81 char c = s[i++];
82
Stephen Warrencd296722012-09-28 21:25:59 +000083 if (c == '\\')
84 c = get_escape_char(s, &i);
David Gibsona4da2e32007-12-18 15:06:42 +110085
Stephen Warrencd296722012-09-28 21:25:59 +000086 q[d.len++] = c;
David Gibsona4da2e32007-12-18 15:06:42 +110087 }
88
89 q[d.len++] = '\0';
90 return d;
91}
92
David Gibsoned95d742008-08-07 12:24:17 +100093struct data data_copy_file(FILE *f, size_t maxlen)
David Gibsona4da2e32007-12-18 15:06:42 +110094{
David Gibsoned95d742008-08-07 12:24:17 +100095 struct data d = empty_data;
David Gibsona4da2e32007-12-18 15:06:42 +110096
David Gibsoned95d742008-08-07 12:24:17 +100097 while (!feof(f) && (d.len < maxlen)) {
98 size_t chunksize, ret;
David Gibsona4da2e32007-12-18 15:06:42 +110099
David Gibsoned95d742008-08-07 12:24:17 +1000100 if (maxlen == -1)
101 chunksize = 4096;
102 else
103 chunksize = maxlen - d.len;
104
105 d = data_grow_for(d, chunksize);
106 ret = fread(d.val + d.len, 1, chunksize, f);
107
108 if (ferror(f))
109 die("Error reading file into data: %s", strerror(errno));
110
111 if (d.len + ret < d.len)
112 die("Overflow reading file into data\n");
113
114 d.len += ret;
115 }
David Gibsona4da2e32007-12-18 15:06:42 +1100116
117 return d;
118}
119
120struct data data_append_data(struct data d, const void *p, int len)
121{
122 d = data_grow_for(d, len);
123 memcpy(d.val + d.len, p, len);
124 d.len += len;
125 return d;
126}
127
128struct data data_insert_at_marker(struct data d, struct marker *m,
129 const void *p, int len)
130{
131 d = data_grow_for(d, len);
132 memmove(d.val + m->offset + len, d.val + m->offset, d.len - m->offset);
133 memcpy(d.val + m->offset, p, len);
134 d.len += len;
135
136 /* Adjust all markers after the one we're inserting at */
137 m = m->next;
138 for_each_marker(m)
139 m->offset += len;
140 return d;
141}
142
Josh Triplett5ccd9912009-10-16 15:53:55 -0700143static struct data data_append_markers(struct data d, struct marker *m)
David Gibsona4da2e32007-12-18 15:06:42 +1100144{
145 struct marker **mp = &d.markers;
146
147 /* Find the end of the markerlist */
148 while (*mp)
149 mp = &((*mp)->next);
150 *mp = m;
151 return d;
152}
153
154struct data data_merge(struct data d1, struct data d2)
155{
156 struct data d;
157 struct marker *m2 = d2.markers;
158
159 d = data_append_markers(data_append_data(d1, d2.val, d2.len), m2);
160
161 /* Adjust for the length of d1 */
162 for_each_marker(m2)
163 m2->offset += d1.len;
164
165 d2.markers = NULL; /* So data_free() doesn't clobber them */
166 data_free(d2);
167
168 return d;
169}
170
Stephen Warrencd296722012-09-28 21:25:59 +0000171struct data data_append_integer(struct data d, uint64_t value, int bits)
David Gibsona4da2e32007-12-18 15:06:42 +1100172{
Stephen Warrencd296722012-09-28 21:25:59 +0000173 uint8_t value_8;
Rob Herring89d12312017-03-21 09:01:08 -0500174 fdt16_t value_16;
175 fdt32_t value_32;
176 fdt64_t value_64;
David Gibsona4da2e32007-12-18 15:06:42 +1100177
Stephen Warrencd296722012-09-28 21:25:59 +0000178 switch (bits) {
179 case 8:
180 value_8 = value;
181 return data_append_data(d, &value_8, 1);
182
183 case 16:
184 value_16 = cpu_to_fdt16(value);
185 return data_append_data(d, &value_16, 2);
186
187 case 32:
188 value_32 = cpu_to_fdt32(value);
189 return data_append_data(d, &value_32, 4);
190
191 case 64:
192 value_64 = cpu_to_fdt64(value);
193 return data_append_data(d, &value_64, 8);
194
195 default:
196 die("Invalid literal size (%d)\n", bits);
197 }
David Gibsona4da2e32007-12-18 15:06:42 +1100198}
199
Rob Herring89d12312017-03-21 09:01:08 -0500200struct data data_append_re(struct data d, uint64_t address, uint64_t size)
David Gibsona4da2e32007-12-18 15:06:42 +1100201{
Rob Herring89d12312017-03-21 09:01:08 -0500202 struct fdt_reserve_entry re;
David Gibsona4da2e32007-12-18 15:06:42 +1100203
Rob Herring89d12312017-03-21 09:01:08 -0500204 re.address = cpu_to_fdt64(address);
205 re.size = cpu_to_fdt64(size);
David Gibsona4da2e32007-12-18 15:06:42 +1100206
Rob Herring89d12312017-03-21 09:01:08 -0500207 return data_append_data(d, &re, sizeof(re));
David Gibsona4da2e32007-12-18 15:06:42 +1100208}
209
Stephen Warrencd296722012-09-28 21:25:59 +0000210struct data data_append_cell(struct data d, cell_t word)
211{
212 return data_append_integer(d, word, sizeof(word) * 8);
213}
214
David Gibsoned95d742008-08-07 12:24:17 +1000215struct data data_append_addr(struct data d, uint64_t addr)
David Gibsona4da2e32007-12-18 15:06:42 +1100216{
Stephen Warrencd296722012-09-28 21:25:59 +0000217 return data_append_integer(d, addr, sizeof(addr) * 8);
David Gibsona4da2e32007-12-18 15:06:42 +1100218}
219
220struct data data_append_byte(struct data d, uint8_t byte)
221{
222 return data_append_data(d, &byte, 1);
223}
224
225struct data data_append_zeroes(struct data d, int len)
226{
227 d = data_grow_for(d, len);
228
229 memset(d.val + d.len, 0, len);
230 d.len += len;
231 return d;
232}
233
234struct data data_append_align(struct data d, int align)
235{
236 int newlen = ALIGN(d.len, align);
237 return data_append_zeroes(d, newlen - d.len);
238}
239
240struct data data_add_marker(struct data d, enum markertype type, char *ref)
241{
242 struct marker *m;
243
244 m = xmalloc(sizeof(*m));
245 m->offset = d.len;
246 m->type = type;
247 m->ref = ref;
248 m->next = NULL;
249
250 return data_append_markers(d, m);
251}
252
Rob Herring47605972015-04-29 16:00:05 -0500253bool data_is_one_string(struct data d)
David Gibsona4da2e32007-12-18 15:06:42 +1100254{
255 int i;
256 int len = d.len;
257
258 if (len == 0)
Rob Herring47605972015-04-29 16:00:05 -0500259 return false;
David Gibsona4da2e32007-12-18 15:06:42 +1100260
261 for (i = 0; i < len-1; i++)
262 if (d.val[i] == '\0')
Rob Herring47605972015-04-29 16:00:05 -0500263 return false;
David Gibsona4da2e32007-12-18 15:06:42 +1100264
265 if (d.val[len-1] != '\0')
Rob Herring47605972015-04-29 16:00:05 -0500266 return false;
David Gibsona4da2e32007-12-18 15:06:42 +1100267
Rob Herring47605972015-04-29 16:00:05 -0500268 return true;
David Gibsona4da2e32007-12-18 15:06:42 +1100269}