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 | * |
| 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 | |
| 23 | #if 0 |
| 24 | static struct data data_init_buf(char *buf, int len) |
| 25 | { |
| 26 | struct data d; |
| 27 | |
| 28 | d.asize = 0; |
| 29 | d.len = len; |
| 30 | d.val = buf; |
| 31 | |
| 32 | return d; |
| 33 | } |
| 34 | |
| 35 | struct data data_ref_string(char *str) |
| 36 | { |
| 37 | return data_init_buf(str, strlen(str)+1); |
| 38 | } |
| 39 | #endif |
| 40 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 41 | void fixup_free(struct fixup *f) |
| 42 | { |
| 43 | free(f->ref); |
| 44 | free(f); |
| 45 | } |
| 46 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 47 | void data_free(struct data d) |
| 48 | { |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 49 | struct fixup *f; |
| 50 | |
| 51 | f = d.refs; |
| 52 | while (f) { |
| 53 | struct fixup *nf; |
| 54 | |
| 55 | nf = f->next; |
| 56 | fixup_free(f); |
| 57 | f = nf; |
| 58 | } |
| 59 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 60 | assert(!d.val || d.asize); |
| 61 | |
| 62 | if (d.val) |
| 63 | free(d.val); |
| 64 | } |
| 65 | |
| 66 | struct data data_grow_for(struct data d, int xlen) |
| 67 | { |
| 68 | struct data nd; |
| 69 | int newsize; |
| 70 | |
| 71 | /* we must start with an allocated datum */ |
| 72 | assert(!d.val || d.asize); |
| 73 | |
| 74 | if (xlen == 0) |
| 75 | return d; |
| 76 | |
| 77 | newsize = xlen; |
| 78 | |
| 79 | while ((d.len + xlen) > newsize) |
| 80 | newsize *= 2; |
| 81 | |
| 82 | nd.asize = newsize; |
| 83 | nd.val = xrealloc(d.val, newsize); |
| 84 | nd.len = d.len; |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 85 | nd.refs = d.refs; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 86 | |
| 87 | assert(nd.asize >= (d.len + xlen)); |
| 88 | |
| 89 | return nd; |
| 90 | } |
| 91 | |
| 92 | struct data data_copy_mem(char *mem, int len) |
| 93 | { |
| 94 | struct data d; |
| 95 | |
| 96 | d = data_grow_for(empty_data, len); |
| 97 | |
| 98 | d.len = len; |
| 99 | memcpy(d.val, mem, len); |
| 100 | |
| 101 | return d; |
| 102 | } |
| 103 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 104 | static char get_oct_char(char *s, int *i) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 105 | { |
| 106 | char x[4]; |
| 107 | char *endx; |
| 108 | long val; |
| 109 | |
| 110 | x[3] = '\0'; |
| 111 | x[0] = s[(*i)]; |
| 112 | if (x[0]) { |
| 113 | x[1] = s[(*i)+1]; |
| 114 | if (x[1]) |
| 115 | x[2] = s[(*i)+2]; |
| 116 | } |
| 117 | |
| 118 | val = strtol(x, &endx, 8); |
| 119 | if ((endx - x) == 0) |
| 120 | fprintf(stderr, "Empty \\nnn escape\n"); |
| 121 | |
| 122 | (*i) += endx - x; |
| 123 | return val; |
| 124 | } |
| 125 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 126 | static char get_hex_char(char *s, int *i) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 127 | { |
| 128 | char x[3]; |
| 129 | char *endx; |
| 130 | long val; |
| 131 | |
| 132 | x[2] = '\0'; |
| 133 | x[0] = s[(*i)]; |
| 134 | if (x[0]) |
| 135 | x[1] = s[(*i)+1]; |
| 136 | |
| 137 | val = strtol(x, &endx, 16); |
| 138 | if ((endx - x) == 0) |
| 139 | fprintf(stderr, "Empty \\x escape\n"); |
| 140 | |
| 141 | (*i) += endx - x; |
| 142 | return val; |
| 143 | } |
| 144 | |
| 145 | struct data data_copy_escape_string(char *s, int len) |
| 146 | { |
| 147 | int i = 0; |
| 148 | struct data d; |
| 149 | char *q; |
| 150 | |
| 151 | d = data_grow_for(empty_data, strlen(s)+1); |
| 152 | |
| 153 | q = d.val; |
| 154 | while (i < len) { |
| 155 | char c = s[i++]; |
| 156 | |
| 157 | if (c != '\\') { |
| 158 | q[d.len++] = c; |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | c = s[i++]; |
| 163 | assert(c); |
| 164 | switch (c) { |
| 165 | case 't': |
| 166 | q[d.len++] = '\t'; |
| 167 | break; |
| 168 | case 'n': |
| 169 | q[d.len++] = '\n'; |
| 170 | break; |
| 171 | case 'r': |
| 172 | q[d.len++] = '\r'; |
| 173 | break; |
David Gibson | b4ac049 | 2005-10-17 10:27:45 +1000 | [diff] [blame] | 174 | case '0': |
| 175 | case '1': |
| 176 | case '2': |
| 177 | case '3': |
| 178 | case '4': |
| 179 | case '5': |
| 180 | case '6': |
| 181 | case '7': |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 182 | i--; /* need to re-read the first digit as |
| 183 | * part of the octal value */ |
| 184 | q[d.len++] = get_oct_char(s, &i); |
| 185 | break; |
| 186 | case 'x': |
| 187 | q[d.len++] = get_hex_char(s, &i); |
| 188 | break; |
| 189 | default: |
| 190 | q[d.len++] = c; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | q[d.len++] = '\0'; |
| 195 | return d; |
| 196 | } |
| 197 | |
| 198 | struct data data_copy_file(FILE *f, size_t len) |
| 199 | { |
| 200 | struct data d; |
| 201 | |
| 202 | d = data_grow_for(empty_data, len); |
| 203 | |
| 204 | d.len = len; |
| 205 | fread(d.val, len, 1, f); |
| 206 | |
| 207 | return d; |
| 208 | } |
| 209 | |
| 210 | struct data data_append_data(struct data d, void *p, int len) |
| 211 | { |
| 212 | d = data_grow_for(d, len); |
| 213 | memcpy(d.val + d.len, p, len); |
| 214 | d.len += len; |
| 215 | return d; |
| 216 | } |
| 217 | |
| 218 | struct data data_append_cell(struct data d, cell_t word) |
| 219 | { |
| 220 | cell_t beword = cpu_to_be32(word); |
| 221 | |
| 222 | return data_append_data(d, &beword, sizeof(beword)); |
| 223 | } |
| 224 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame^] | 225 | struct data data_append_re(struct data d, struct reserve_entry *re) |
| 226 | { |
| 227 | struct reserve_entry bere; |
| 228 | |
| 229 | bere.address = cpu_to_be64(re->address); |
| 230 | bere.size = cpu_to_be64(re->size); |
| 231 | |
| 232 | return data_append_data(d, &bere, sizeof(bere)); |
| 233 | } |
| 234 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 235 | struct data data_append_addr(struct data d, u64 addr) |
| 236 | { |
| 237 | u64 beaddr = cpu_to_be64(addr); |
| 238 | |
| 239 | return data_append_data(d, &beaddr, sizeof(beaddr)); |
| 240 | } |
| 241 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 242 | struct data data_append_byte(struct data d, uint8_t byte) |
| 243 | { |
| 244 | return data_append_data(d, &byte, 1); |
| 245 | } |
| 246 | |
| 247 | struct data data_append_zeroes(struct data d, int len) |
| 248 | { |
| 249 | d = data_grow_for(d, len); |
| 250 | |
| 251 | memset(d.val + d.len, 0, len); |
| 252 | d.len += len; |
| 253 | return d; |
| 254 | } |
| 255 | |
| 256 | struct data data_append_align(struct data d, int align) |
| 257 | { |
| 258 | int newlen = ALIGN(d.len, align); |
| 259 | return data_append_zeroes(d, newlen - d.len); |
| 260 | } |
| 261 | |
David Gibson | 81f2e89 | 2005-06-16 17:04:00 +1000 | [diff] [blame] | 262 | struct data data_add_fixup(struct data d, char *ref) |
| 263 | { |
| 264 | struct fixup *f; |
| 265 | struct data nd; |
| 266 | |
| 267 | f = xmalloc(sizeof(*f)); |
| 268 | f->offset = d.len; |
| 269 | f->ref = ref; |
| 270 | f->next = d.refs; |
| 271 | |
| 272 | nd = d; |
| 273 | nd.refs = f; |
| 274 | |
| 275 | return nd; |
| 276 | } |
| 277 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 278 | int data_is_one_string(struct data d) |
| 279 | { |
| 280 | int i; |
| 281 | int len = d.len; |
| 282 | |
| 283 | if (len == 0) |
| 284 | return 0; |
| 285 | |
| 286 | for (i = 0; i < len-1; i++) |
| 287 | if (d.val[i] == '\0') |
| 288 | return 0; |
| 289 | |
| 290 | if (d.val[len-1] != '\0') |
| 291 | return 0; |
| 292 | |
| 293 | return 1; |
| 294 | } |