blob: 6ae59768f08fde5fbcc5565343cfb3f6b9bcfaba [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"
Chris Phoenixa7686482016-02-22 15:58:11 -080037#include "version_non_gen.h"
Jon Loeliger879e4d22008-10-03 11:12:33 -050038
39char *xstrdup(const char *s)
40{
41 int len = strlen(s) + 1;
Florian Fainelli24cb3d02014-02-01 16:41:59 +110042 char *d = xmalloc(len);
Jon Loeliger879e4d22008-10-03 11:12:33 -050043
Florian Fainelli24cb3d02014-02-01 16:41:59 +110044 memcpy(d, s, len);
Jon Loeliger879e4d22008-10-03 11:12:33 -050045
Florian Fainelli24cb3d02014-02-01 16:41:59 +110046 return d;
Jon Loeliger879e4d22008-10-03 11:12:33 -050047}
David Gibsond68cb362009-12-08 14:24:42 +110048
Pantelis Antoniou9dc40492016-05-24 20:50:35 +030049/* based in part from (3) vsnprintf */
50int xasprintf(char **strp, const char *fmt, ...)
51{
52 int n, size = 128; /* start with 128 bytes */
53 char *p;
54 va_list ap;
55
56 /* initial pointer is NULL making the fist realloc to be malloc */
57 p = NULL;
58 while (1) {
59 p = xrealloc(p, size);
60
61 /* Try to print in the allocated space. */
62 va_start(ap, fmt);
63 n = vsnprintf(p, size, fmt, ap);
64 va_end(ap);
65
66 /* If that worked, return the string. */
67 if (n > -1 && n < size)
68 break;
69 /* Else try again with more space. */
70 if (n > -1) /* glibc 2.1 */
71 size = n + 1; /* precisely what is needed */
72 else /* glibc 2.0 */
73 size *= 2; /* twice the old size */
74 }
75 *strp = p;
76 return strlen(p);
77}
78
David Gibsond68cb362009-12-08 14:24:42 +110079char *join_path(const char *path, const char *name)
80{
81 int lenp = strlen(path);
82 int lenn = strlen(name);
83 int len;
84 int needslash = 1;
85 char *str;
86
87 len = lenp + lenn + 2;
88 if ((lenp > 0) && (path[lenp-1] == '/')) {
89 needslash = 0;
90 len--;
91 }
92
93 str = xmalloc(len);
94 memcpy(str, path, lenp);
95 if (needslash) {
96 str[lenp] = '/';
97 lenp++;
98 }
99 memcpy(str+lenp, name, lenn+1);
100 return str;
101}
Simon Glass492f9d52011-07-05 12:02:49 -0700102
David Gibson17625372013-10-28 21:06:53 +1100103bool util_is_printable_string(const void *data, int len)
Simon Glass492f9d52011-07-05 12:02:49 -0700104{
105 const char *s = data;
Pantelis Antoniou1c1efd62013-01-04 21:12:58 +0200106 const char *ss, *se;
Simon Glass492f9d52011-07-05 12:02:49 -0700107
108 /* zero length is not */
109 if (len == 0)
110 return 0;
111
112 /* must terminate with zero */
113 if (s[len - 1] != '\0')
114 return 0;
115
Pantelis Antoniou1c1efd62013-01-04 21:12:58 +0200116 se = s + len;
Simon Glass492f9d52011-07-05 12:02:49 -0700117
Pantelis Antoniou1c1efd62013-01-04 21:12:58 +0200118 while (s < se) {
119 ss = s;
Serge Lamikhov-Center17119ab2013-12-25 15:26:03 +1100120 while (s < se && *s && isprint((unsigned char)*s))
Pantelis Antoniou1c1efd62013-01-04 21:12:58 +0200121 s++;
122
123 /* not zero, or not done yet */
124 if (*s != '\0' || s == ss)
125 return 0;
126
127 s++;
128 }
Simon Glass492f9d52011-07-05 12:02:49 -0700129
130 return 1;
131}
Anton Staafb43335a2011-09-09 12:16:29 -0700132
133/*
134 * Parse a octal encoded character starting at index i in string s. The
135 * resulting character will be returned and the index i will be updated to
136 * point at the character directly after the end of the encoding, this may be
137 * the '\0' terminator of the string.
138 */
139static char get_oct_char(const char *s, int *i)
140{
141 char x[4];
142 char *endx;
143 long val;
144
145 x[3] = '\0';
146 strncpy(x, s + *i, 3);
147
148 val = strtol(x, &endx, 8);
149
150 assert(endx > x);
151
152 (*i) += endx - x;
153 return val;
154}
155
156/*
157 * Parse a hexadecimal encoded character starting at index i in string s. The
158 * resulting character will be returned and the index i will be updated to
159 * point at the character directly after the end of the encoding, this may be
160 * the '\0' terminator of the string.
161 */
162static char get_hex_char(const char *s, int *i)
163{
164 char x[3];
165 char *endx;
166 long val;
167
168 x[2] = '\0';
169 strncpy(x, s + *i, 2);
170
171 val = strtol(x, &endx, 16);
172 if (!(endx > x))
173 die("\\x used with no following hex digits\n");
174
175 (*i) += endx - x;
176 return val;
177}
178
179char get_escape_char(const char *s, int *i)
180{
181 char c = s[*i];
182 int j = *i + 1;
183 char val;
184
Anton Staafb43335a2011-09-09 12:16:29 -0700185 switch (c) {
186 case 'a':
187 val = '\a';
188 break;
189 case 'b':
190 val = '\b';
191 break;
192 case 't':
193 val = '\t';
194 break;
195 case 'n':
196 val = '\n';
197 break;
198 case 'v':
199 val = '\v';
200 break;
201 case 'f':
202 val = '\f';
203 break;
204 case 'r':
205 val = '\r';
206 break;
207 case '0':
208 case '1':
209 case '2':
210 case '3':
211 case '4':
212 case '5':
213 case '6':
214 case '7':
215 j--; /* need to re-read the first digit as
216 * part of the octal value */
217 val = get_oct_char(s, &j);
218 break;
219 case 'x':
220 val = get_hex_char(s, &j);
221 break;
222 default:
223 val = c;
224 }
225
226 (*i) = j;
227 return val;
228}
Simon Glass36204fd2011-09-22 10:11:02 -0700229
Mike Frysingera6d55e02013-04-08 00:56:54 -0400230int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
Simon Glass36204fd2011-09-22 10:11:02 -0700231{
232 int fd = 0; /* assume stdin */
233 char *buf = NULL;
234 off_t bufsize = 1024, offset = 0;
235 int ret = 0;
236
237 *buffp = NULL;
238 if (strcmp(filename, "-") != 0) {
239 fd = open(filename, O_RDONLY);
240 if (fd < 0)
241 return errno;
242 }
243
244 /* Loop until we have read everything */
Mike Frysingerf8cb5dd2013-04-10 14:29:06 -0400245 buf = xmalloc(bufsize);
Simon Glass36204fd2011-09-22 10:11:02 -0700246 do {
247 /* Expand the buffer to hold the next chunk */
248 if (offset == bufsize) {
249 bufsize *= 2;
Mike Frysingerf8cb5dd2013-04-10 14:29:06 -0400250 buf = xrealloc(buf, bufsize);
Simon Glass36204fd2011-09-22 10:11:02 -0700251 }
252
253 ret = read(fd, &buf[offset], bufsize - offset);
254 if (ret < 0) {
255 ret = errno;
256 break;
257 }
258 offset += ret;
259 } while (ret != 0);
260
261 /* Clean up, including closing stdin; return errno on error */
262 close(fd);
263 if (ret)
264 free(buf);
265 else
266 *buffp = buf;
Mike Frysingera6d55e02013-04-08 00:56:54 -0400267 *len = bufsize;
Simon Glass36204fd2011-09-22 10:11:02 -0700268 return ret;
269}
270
Mike Frysingera6d55e02013-04-08 00:56:54 -0400271int utilfdt_read_err(const char *filename, char **buffp)
272{
273 off_t len;
274 return utilfdt_read_err_len(filename, buffp, &len);
275}
276
277char *utilfdt_read_len(const char *filename, off_t *len)
Simon Glass36204fd2011-09-22 10:11:02 -0700278{
279 char *buff;
Mike Frysingera6d55e02013-04-08 00:56:54 -0400280 int ret = utilfdt_read_err_len(filename, &buff, len);
Simon Glass36204fd2011-09-22 10:11:02 -0700281
282 if (ret) {
283 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
284 strerror(ret));
285 return NULL;
286 }
287 /* Successful read */
288 return buff;
289}
290
Mike Frysingera6d55e02013-04-08 00:56:54 -0400291char *utilfdt_read(const char *filename)
292{
293 off_t len;
294 return utilfdt_read_len(filename, &len);
295}
296
Simon Glass36204fd2011-09-22 10:11:02 -0700297int utilfdt_write_err(const char *filename, const void *blob)
298{
299 int fd = 1; /* assume stdout */
300 int totalsize;
301 int offset;
302 int ret = 0;
303 const char *ptr = blob;
304
305 if (strcmp(filename, "-") != 0) {
306 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
307 if (fd < 0)
308 return errno;
309 }
310
311 totalsize = fdt_totalsize(blob);
312 offset = 0;
313
314 while (offset < totalsize) {
315 ret = write(fd, ptr + offset, totalsize - offset);
316 if (ret < 0) {
317 ret = -errno;
318 break;
319 }
320 offset += ret;
321 }
322 /* Close the file/stdin; return errno on error */
323 if (fd != 1)
324 close(fd);
325 return ret < 0 ? -ret : 0;
326}
327
328
329int utilfdt_write(const char *filename, const void *blob)
330{
331 int ret = utilfdt_write_err(filename, blob);
332
333 if (ret) {
334 fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
335 strerror(ret));
336 }
337 return ret ? -1 : 0;
338}
339
340int utilfdt_decode_type(const char *fmt, int *type, int *size)
341{
342 int qualifier = 0;
343
David Gibsone2804422012-02-03 17:06:12 +1100344 if (!*fmt)
345 return -1;
346
Simon Glass36204fd2011-09-22 10:11:02 -0700347 /* get the conversion qualifier */
348 *size = -1;
349 if (strchr("hlLb", *fmt)) {
350 qualifier = *fmt++;
351 if (qualifier == *fmt) {
352 switch (*fmt++) {
353/* TODO: case 'l': qualifier = 'L'; break;*/
354 case 'h':
355 qualifier = 'b';
356 break;
357 }
358 }
359 }
360
361 /* we should now have a type */
David Gibsone2804422012-02-03 17:06:12 +1100362 if ((*fmt == '\0') || !strchr("iuxs", *fmt))
Simon Glass36204fd2011-09-22 10:11:02 -0700363 return -1;
364
365 /* convert qualifier (bhL) to byte size */
366 if (*fmt != 's')
367 *size = qualifier == 'b' ? 1 :
368 qualifier == 'h' ? 2 :
369 qualifier == 'l' ? 4 : -1;
370 *type = *fmt++;
371
372 /* that should be it! */
373 if (*fmt)
374 return -1;
375 return 0;
376}
Simon Glassd20391d2013-01-21 12:59:16 -0800377
378void utilfdt_print_data(const char *data, int len)
379{
380 int i;
Simon Glassd20391d2013-01-21 12:59:16 -0800381 const char *s;
382
383 /* no data, don't print */
384 if (len == 0)
385 return;
386
387 if (util_is_printable_string(data, len)) {
388 printf(" = ");
389
390 s = data;
391 do {
392 printf("\"%s\"", s);
393 s += strlen(s) + 1;
394 if (s < data + len)
395 printf(", ");
396 } while (s < data + len);
397
398 } else if ((len % 4) == 0) {
David Gibsonbad5b282017-03-06 12:08:53 +1100399 const fdt32_t *cell = (const fdt32_t *)data;
Simon Glassd20391d2013-01-21 12:59:16 -0800400
401 printf(" = <");
Simon Glassc78ca722014-06-18 01:00:23 -0600402 for (i = 0, len /= 4; i < len; i++)
403 printf("0x%08x%s", fdt32_to_cpu(cell[i]),
404 i < (len - 1) ? " " : "");
Simon Glassd20391d2013-01-21 12:59:16 -0800405 printf(">");
406 } else {
David Gibsone5e6df72015-07-09 13:47:19 +1000407 const unsigned char *p = (const unsigned char *)data;
Simon Glassd20391d2013-01-21 12:59:16 -0800408 printf(" = [");
409 for (i = 0; i < len; i++)
410 printf("%02x%s", *p++, i < len - 1 ? " " : "");
411 printf("]");
412 }
413}
Mike Frysinger31be4ce2013-04-10 14:29:09 -0400414
David Gibsonbad5b282017-03-06 12:08:53 +1100415void NORETURN util_version(void)
Mike Frysinger31be4ce2013-04-10 14:29:09 -0400416{
417 printf("Version: %s\n", DTC_VERSION);
418 exit(0);
419}
Mike Frysingerbe8d1c82013-04-15 22:13:12 -0400420
David Gibsonbad5b282017-03-06 12:08:53 +1100421void NORETURN util_usage(const char *errmsg, const char *synopsis,
422 const char *short_opts,
423 struct option const long_opts[],
424 const char * const opts_help[])
Mike Frysingerbe8d1c82013-04-15 22:13:12 -0400425{
426 FILE *fp = errmsg ? stderr : stdout;
427 const char a_arg[] = "<arg>";
428 size_t a_arg_len = strlen(a_arg) + 1;
429 size_t i;
430 int optlen;
431
432 fprintf(fp,
433 "Usage: %s\n"
434 "\n"
435 "Options: -[%s]\n", synopsis, short_opts);
436
437 /* prescan the --long opt length to auto-align */
438 optlen = 0;
439 for (i = 0; long_opts[i].name; ++i) {
440 /* +1 is for space between --opt and help text */
441 int l = strlen(long_opts[i].name) + 1;
442 if (long_opts[i].has_arg == a_argument)
443 l += a_arg_len;
444 if (optlen < l)
445 optlen = l;
446 }
447
448 for (i = 0; long_opts[i].name; ++i) {
449 /* helps when adding new applets or options */
450 assert(opts_help[i] != NULL);
451
452 /* first output the short flag if it has one */
453 if (long_opts[i].val > '~')
454 fprintf(fp, " ");
455 else
456 fprintf(fp, " -%c, ", long_opts[i].val);
457
458 /* then the long flag */
459 if (long_opts[i].has_arg == no_argument)
460 fprintf(fp, "--%-*s", optlen, long_opts[i].name);
461 else
462 fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
463 (int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
464
465 /* finally the help text */
466 fprintf(fp, "%s\n", opts_help[i]);
467 }
468
469 if (errmsg) {
470 fprintf(fp, "\nError: %s\n", errmsg);
471 exit(EXIT_FAILURE);
472 } else
473 exit(EXIT_SUCCESS);
474}