blob: 4cdad10b70625a504357e061a05cc241da30a161 [file] [log] [blame]
Jon Loeliger879e4d22008-10-03 11:12:33 -05001/*
Simon Glass36204fd2011-09-22 10:11:02 -07002 * Copyright 2011 The Chromium Authors, All Rights Reserved.
Jon Loeliger879e4d22008-10-03 11:12:33 -05003 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
4 *
Simon Glass492f9d52011-07-05 12:02:49 -07005 * util_is_printable_string contributed by
6 * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
7 *
Jon Loeliger879e4d22008-10-03 11:12:33 -05008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 */
23
Simon Glass492f9d52011-07-05 12:02:49 -070024#include <ctype.h>
David Gibson87658742010-03-03 16:38:01 +110025#include <stdio.h>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <string.h>
Anton Staafb43335a2011-09-09 12:16:29 -070029#include <assert.h>
David Gibson87658742010-03-03 16:38:01 +110030
Simon Glass36204fd2011-09-22 10:11:02 -070031#include <errno.h>
32#include <fcntl.h>
33#include <unistd.h>
34
35#include "libfdt.h"
David Gibson87658742010-03-03 16:38:01 +110036#include "util.h"
Mike Frysinger31be4ce2013-04-10 14:29:09 -040037#include "version_gen.h"
Jon Loeliger879e4d22008-10-03 11:12:33 -050038
39char *xstrdup(const char *s)
40{
41 int len = strlen(s) + 1;
42 char *dup = xmalloc(len);
43
44 memcpy(dup, s, len);
45
46 return dup;
47}
David Gibsond68cb362009-12-08 14:24:42 +110048
49char *join_path(const char *path, const char *name)
50{
51 int lenp = strlen(path);
52 int lenn = strlen(name);
53 int len;
54 int needslash = 1;
55 char *str;
56
57 len = lenp + lenn + 2;
58 if ((lenp > 0) && (path[lenp-1] == '/')) {
59 needslash = 0;
60 len--;
61 }
62
63 str = xmalloc(len);
64 memcpy(str, path, lenp);
65 if (needslash) {
66 str[lenp] = '/';
67 lenp++;
68 }
69 memcpy(str+lenp, name, lenn+1);
70 return str;
71}
Simon Glass492f9d52011-07-05 12:02:49 -070072
73int util_is_printable_string(const void *data, int len)
74{
75 const char *s = data;
Pantelis Antoniou1c1efd62013-01-04 21:12:58 +020076 const char *ss, *se;
Simon Glass492f9d52011-07-05 12:02:49 -070077
78 /* zero length is not */
79 if (len == 0)
80 return 0;
81
82 /* must terminate with zero */
83 if (s[len - 1] != '\0')
84 return 0;
85
Pantelis Antoniou1c1efd62013-01-04 21:12:58 +020086 se = s + len;
Simon Glass492f9d52011-07-05 12:02:49 -070087
Pantelis Antoniou1c1efd62013-01-04 21:12:58 +020088 while (s < se) {
89 ss = s;
90 while (s < se && *s && isprint(*s))
91 s++;
92
93 /* not zero, or not done yet */
94 if (*s != '\0' || s == ss)
95 return 0;
96
97 s++;
98 }
Simon Glass492f9d52011-07-05 12:02:49 -070099
100 return 1;
101}
Anton Staafb43335a2011-09-09 12:16:29 -0700102
103/*
104 * Parse a octal encoded character starting at index i in string s. The
105 * resulting character will be returned and the index i will be updated to
106 * point at the character directly after the end of the encoding, this may be
107 * the '\0' terminator of the string.
108 */
109static char get_oct_char(const char *s, int *i)
110{
111 char x[4];
112 char *endx;
113 long val;
114
115 x[3] = '\0';
116 strncpy(x, s + *i, 3);
117
118 val = strtol(x, &endx, 8);
119
120 assert(endx > x);
121
122 (*i) += endx - x;
123 return val;
124}
125
126/*
127 * Parse a hexadecimal encoded character starting at index i in string s. The
128 * resulting character will be returned and the index i will be updated to
129 * point at the character directly after the end of the encoding, this may be
130 * the '\0' terminator of the string.
131 */
132static char get_hex_char(const char *s, int *i)
133{
134 char x[3];
135 char *endx;
136 long val;
137
138 x[2] = '\0';
139 strncpy(x, s + *i, 2);
140
141 val = strtol(x, &endx, 16);
142 if (!(endx > x))
143 die("\\x used with no following hex digits\n");
144
145 (*i) += endx - x;
146 return val;
147}
148
149char get_escape_char(const char *s, int *i)
150{
151 char c = s[*i];
152 int j = *i + 1;
153 char val;
154
155 assert(c);
156 switch (c) {
157 case 'a':
158 val = '\a';
159 break;
160 case 'b':
161 val = '\b';
162 break;
163 case 't':
164 val = '\t';
165 break;
166 case 'n':
167 val = '\n';
168 break;
169 case 'v':
170 val = '\v';
171 break;
172 case 'f':
173 val = '\f';
174 break;
175 case 'r':
176 val = '\r';
177 break;
178 case '0':
179 case '1':
180 case '2':
181 case '3':
182 case '4':
183 case '5':
184 case '6':
185 case '7':
186 j--; /* need to re-read the first digit as
187 * part of the octal value */
188 val = get_oct_char(s, &j);
189 break;
190 case 'x':
191 val = get_hex_char(s, &j);
192 break;
193 default:
194 val = c;
195 }
196
197 (*i) = j;
198 return val;
199}
Simon Glass36204fd2011-09-22 10:11:02 -0700200
Jon Loeliger5543b882013-04-22 15:41:41 -0500201int utilfdt_read_err(const char *filename, char **buffp)
Simon Glass36204fd2011-09-22 10:11:02 -0700202{
203 int fd = 0; /* assume stdin */
204 char *buf = NULL;
205 off_t bufsize = 1024, offset = 0;
206 int ret = 0;
207
208 *buffp = NULL;
209 if (strcmp(filename, "-") != 0) {
210 fd = open(filename, O_RDONLY);
211 if (fd < 0)
212 return errno;
213 }
214
215 /* Loop until we have read everything */
Mike Frysingerf8cb5dd2013-04-10 14:29:06 -0400216 buf = xmalloc(bufsize);
Simon Glass36204fd2011-09-22 10:11:02 -0700217 do {
218 /* Expand the buffer to hold the next chunk */
219 if (offset == bufsize) {
220 bufsize *= 2;
Mike Frysingerf8cb5dd2013-04-10 14:29:06 -0400221 buf = xrealloc(buf, bufsize);
Simon Glass36204fd2011-09-22 10:11:02 -0700222 if (!buf) {
223 ret = ENOMEM;
224 break;
225 }
226 }
227
228 ret = read(fd, &buf[offset], bufsize - offset);
229 if (ret < 0) {
230 ret = errno;
231 break;
232 }
233 offset += ret;
234 } while (ret != 0);
235
236 /* Clean up, including closing stdin; return errno on error */
237 close(fd);
238 if (ret)
239 free(buf);
240 else
241 *buffp = buf;
242 return ret;
243}
244
Jon Loeliger5543b882013-04-22 15:41:41 -0500245char *utilfdt_read(const char *filename)
Simon Glass36204fd2011-09-22 10:11:02 -0700246{
247 char *buff;
Jon Loeliger5543b882013-04-22 15:41:41 -0500248 int ret = utilfdt_read_err(filename, &buff);
Simon Glass36204fd2011-09-22 10:11:02 -0700249
250 if (ret) {
251 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
252 strerror(ret));
253 return NULL;
254 }
255 /* Successful read */
256 return buff;
257}
258
259int utilfdt_write_err(const char *filename, const void *blob)
260{
261 int fd = 1; /* assume stdout */
262 int totalsize;
263 int offset;
264 int ret = 0;
265 const char *ptr = blob;
266
267 if (strcmp(filename, "-") != 0) {
268 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
269 if (fd < 0)
270 return errno;
271 }
272
273 totalsize = fdt_totalsize(blob);
274 offset = 0;
275
276 while (offset < totalsize) {
277 ret = write(fd, ptr + offset, totalsize - offset);
278 if (ret < 0) {
279 ret = -errno;
280 break;
281 }
282 offset += ret;
283 }
284 /* Close the file/stdin; return errno on error */
285 if (fd != 1)
286 close(fd);
287 return ret < 0 ? -ret : 0;
288}
289
290
291int utilfdt_write(const char *filename, const void *blob)
292{
293 int ret = utilfdt_write_err(filename, blob);
294
295 if (ret) {
296 fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
297 strerror(ret));
298 }
299 return ret ? -1 : 0;
300}
301
302int utilfdt_decode_type(const char *fmt, int *type, int *size)
303{
304 int qualifier = 0;
305
David Gibsone2804422012-02-03 17:06:12 +1100306 if (!*fmt)
307 return -1;
308
Simon Glass36204fd2011-09-22 10:11:02 -0700309 /* get the conversion qualifier */
310 *size = -1;
311 if (strchr("hlLb", *fmt)) {
312 qualifier = *fmt++;
313 if (qualifier == *fmt) {
314 switch (*fmt++) {
315/* TODO: case 'l': qualifier = 'L'; break;*/
316 case 'h':
317 qualifier = 'b';
318 break;
319 }
320 }
321 }
322
323 /* we should now have a type */
David Gibsone2804422012-02-03 17:06:12 +1100324 if ((*fmt == '\0') || !strchr("iuxs", *fmt))
Simon Glass36204fd2011-09-22 10:11:02 -0700325 return -1;
326
327 /* convert qualifier (bhL) to byte size */
328 if (*fmt != 's')
329 *size = qualifier == 'b' ? 1 :
330 qualifier == 'h' ? 2 :
331 qualifier == 'l' ? 4 : -1;
332 *type = *fmt++;
333
334 /* that should be it! */
335 if (*fmt)
336 return -1;
337 return 0;
338}
Simon Glassd20391d2013-01-21 12:59:16 -0800339
340void utilfdt_print_data(const char *data, int len)
341{
342 int i;
343 const char *p = data;
344 const char *s;
345
346 /* no data, don't print */
347 if (len == 0)
348 return;
349
350 if (util_is_printable_string(data, len)) {
351 printf(" = ");
352
353 s = data;
354 do {
355 printf("\"%s\"", s);
356 s += strlen(s) + 1;
357 if (s < data + len)
358 printf(", ");
359 } while (s < data + len);
360
361 } else if ((len % 4) == 0) {
362 const uint32_t *cell = (const uint32_t *)data;
363
364 printf(" = <");
365 for (i = 0; i < len; i += 4)
366 printf("0x%08x%s", fdt32_to_cpu(cell[i]),
367 i < (len - 4) ? " " : "");
368 printf(">");
369 } else {
370 printf(" = [");
371 for (i = 0; i < len; i++)
372 printf("%02x%s", *p++, i < len - 1 ? " " : "");
373 printf("]");
374 }
375}
Mike Frysinger31be4ce2013-04-10 14:29:09 -0400376
377void util_version(void)
378{
379 printf("Version: %s\n", DTC_VERSION);
380 exit(0);
381}