David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| 3 | * |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 4 | * |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 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. |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 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 |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "dtc.h" |
| 22 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 23 | void data_free(struct data d) |
| 24 | { |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 25 | struct marker *m, *nm; |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 26 | |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 27 | m = d.markers; |
| 28 | while (m) { |
| 29 | nm = m->next; |
| 30 | free(m->ref); |
| 31 | free(m); |
| 32 | m = nm; |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 33 | } |
| 34 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 35 | if (d.val) |
| 36 | free(d.val); |
| 37 | } |
| 38 | |
| 39 | struct data data_grow_for(struct data d, int xlen) |
| 40 | { |
| 41 | struct data nd; |
| 42 | int newsize; |
| 43 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 44 | if (xlen == 0) |
| 45 | return d; |
| 46 | |
Milton Miller | 46779e8 | 2007-07-07 01:18:49 -0500 | [diff] [blame] | 47 | nd = d; |
| 48 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 49 | newsize = xlen; |
| 50 | |
| 51 | while ((d.len + xlen) > newsize) |
| 52 | newsize *= 2; |
| 53 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 54 | nd.val = xrealloc(d.val, newsize); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 55 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 56 | return nd; |
| 57 | } |
| 58 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 59 | struct data data_copy_mem(const char *mem, int len) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 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 Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 71 | struct data data_copy_escape_string(const char *s, int len) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 72 | { |
| 73 | int i = 0; |
| 74 | struct data d; |
| 75 | char *q; |
| 76 | |
| 77 | d = data_grow_for(empty_data, strlen(s)+1); |
| 78 | |
| 79 | q = d.val; |
| 80 | while (i < len) { |
| 81 | char c = s[i++]; |
| 82 | |
Anton Staaf | b43335a | 2011-09-09 12:16:29 -0700 | [diff] [blame] | 83 | if (c == '\\') |
| 84 | c = get_escape_char(s, &i); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 85 | |
Anton Staaf | b43335a | 2011-09-09 12:16:29 -0700 | [diff] [blame] | 86 | q[d.len++] = c; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | q[d.len++] = '\0'; |
| 90 | return d; |
| 91 | } |
| 92 | |
David Gibson | e37ec7d | 2008-06-11 11:58:39 +1000 | [diff] [blame] | 93 | struct data data_copy_file(FILE *f, size_t maxlen) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 94 | { |
David Gibson | e37ec7d | 2008-06-11 11:58:39 +1000 | [diff] [blame] | 95 | struct data d = empty_data; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 96 | |
David Gibson | e37ec7d | 2008-06-11 11:58:39 +1000 | [diff] [blame] | 97 | while (!feof(f) && (d.len < maxlen)) { |
| 98 | size_t chunksize, ret; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 99 | |
David Gibson | e37ec7d | 2008-06-11 11:58:39 +1000 | [diff] [blame] | 100 | 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 Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 116 | |
| 117 | return d; |
| 118 | } |
| 119 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 120 | struct data data_append_data(struct data d, const void *p, int len) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 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 | |
David Gibson | efbbef8 | 2007-12-05 10:43:50 +1100 | [diff] [blame] | 128 | struct 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 | |
David Gibson | 01a2d8a | 2008-08-04 15:30:13 +1000 | [diff] [blame] | 143 | static struct data data_append_markers(struct data d, struct marker *m) |
David Gibson | 32da475 | 2007-02-07 16:37:50 +1100 | [diff] [blame] | 144 | { |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 145 | struct marker **mp = &d.markers; |
David Gibson | 32da475 | 2007-02-07 16:37:50 +1100 | [diff] [blame] | 146 | |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 147 | /* Find the end of the markerlist */ |
| 148 | while (*mp) |
| 149 | mp = &((*mp)->next); |
| 150 | *mp = m; |
| 151 | return d; |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | struct data data_merge(struct data d1, struct data d2) |
| 155 | { |
| 156 | struct data d; |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 157 | struct marker *m2 = d2.markers; |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame] | 158 | |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 159 | d = data_append_markers(data_append_data(d1, d2.val, d2.len), m2); |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame] | 160 | |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 161 | /* Adjust for the length of d1 */ |
| 162 | for_each_marker(m2) |
| 163 | m2->offset += d1.len; |
David Gibson | 32da475 | 2007-02-07 16:37:50 +1100 | [diff] [blame] | 164 | |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 165 | d2.markers = NULL; /* So data_free() doesn't clobber them */ |
David Gibson | 32da475 | 2007-02-07 16:37:50 +1100 | [diff] [blame] | 166 | data_free(d2); |
| 167 | |
| 168 | return d; |
| 169 | } |
| 170 | |
Anton Staaf | a4b515c | 2011-10-11 10:22:28 -0700 | [diff] [blame] | 171 | struct data data_append_integer(struct data d, uint64_t value, int bits) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 172 | { |
Anton Staaf | a4b515c | 2011-10-11 10:22:28 -0700 | [diff] [blame] | 173 | uint8_t value_8; |
| 174 | uint16_t value_16; |
| 175 | uint32_t value_32; |
| 176 | uint64_t value_64; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 177 | |
Anton Staaf | a4b515c | 2011-10-11 10:22:28 -0700 | [diff] [blame] | 178 | 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 Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 198 | } |
| 199 | |
David Gibson | 92cb9a2 | 2007-12-04 14:26:15 +1100 | [diff] [blame] | 200 | struct data data_append_re(struct data d, const struct fdt_reserve_entry *re) |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 201 | { |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 202 | struct fdt_reserve_entry bere; |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 203 | |
David Gibson | c8c374b | 2008-06-25 14:27:53 +1000 | [diff] [blame] | 204 | bere.address = cpu_to_fdt64(re->address); |
| 205 | bere.size = cpu_to_fdt64(re->size); |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 206 | |
| 207 | return data_append_data(d, &bere, sizeof(bere)); |
| 208 | } |
| 209 | |
Anton Staaf | a4b515c | 2011-10-11 10:22:28 -0700 | [diff] [blame] | 210 | struct data data_append_cell(struct data d, cell_t word) |
| 211 | { |
| 212 | return data_append_integer(d, word, sizeof(word) * 8); |
| 213 | } |
| 214 | |
David Gibson | 5335901 | 2008-06-25 13:53:07 +1000 | [diff] [blame] | 215 | struct data data_append_addr(struct data d, uint64_t addr) |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 216 | { |
Anton Staaf | a4b515c | 2011-10-11 10:22:28 -0700 | [diff] [blame] | 217 | return data_append_integer(d, addr, sizeof(addr) * 8); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 218 | } |
| 219 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 220 | struct data data_append_byte(struct data d, uint8_t byte) |
| 221 | { |
| 222 | return data_append_data(d, &byte, 1); |
| 223 | } |
| 224 | |
| 225 | struct 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 | |
| 234 | struct 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 | |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 240 | struct data data_add_marker(struct data d, enum markertype type, char *ref) |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 241 | { |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 242 | struct marker *m; |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 243 | |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 244 | m = xmalloc(sizeof(*m)); |
| 245 | m->offset = d.len; |
| 246 | m->type = type; |
| 247 | m->ref = ref; |
| 248 | m->next = NULL; |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 249 | |
David Gibson | dc94177 | 2007-11-22 14:39:23 +1100 | [diff] [blame] | 250 | return data_append_markers(d, m); |
Milton Miller | 6a99b13 | 2007-07-07 01:18:51 -0500 | [diff] [blame] | 251 | } |
| 252 | |
David Gibson | 1762537 | 2013-10-28 21:06:53 +1100 | [diff] [blame^] | 253 | bool data_is_one_string(struct data d) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 254 | { |
| 255 | int i; |
| 256 | int len = d.len; |
| 257 | |
| 258 | if (len == 0) |
David Gibson | 1762537 | 2013-10-28 21:06:53 +1100 | [diff] [blame^] | 259 | return false; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 260 | |
| 261 | for (i = 0; i < len-1; i++) |
| 262 | if (d.val[i] == '\0') |
David Gibson | 1762537 | 2013-10-28 21:06:53 +1100 | [diff] [blame^] | 263 | return false; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 264 | |
| 265 | if (d.val[len-1] != '\0') |
David Gibson | 1762537 | 2013-10-28 21:06:53 +1100 | [diff] [blame^] | 266 | return false; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 267 | |
David Gibson | 1762537 | 2013-10-28 21:06:53 +1100 | [diff] [blame^] | 268 | return true; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 269 | } |