2 * Copyright 2011 The Chromium Authors, All Rights Reserved.
3 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
5 * util_is_printable_string contributed by
6 * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
8 * 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.
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.
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
37 #include "version_gen.h"
39 char *xstrdup(const char *s)
41 int len = strlen(s) + 1;
42 char *d = xmalloc(len);
49 /* based in part from (3) vsnprintf */
50 int xasprintf(char **strp, const char *fmt, ...)
52 int n, size = 128; /* start with 128 bytes */
56 /* initial pointer is NULL making the fist realloc to be malloc */
59 p = xrealloc(p, size);
61 /* Try to print in the allocated space. */
63 n = vsnprintf(p, size, fmt, ap);
66 /* If that worked, return the string. */
67 if (n > -1 && n < size)
69 /* Else try again with more space. */
70 if (n > -1) /* glibc 2.1 */
71 size = n + 1; /* precisely what is needed */
73 size *= 2; /* twice the old size */
79 char *join_path(const char *path, const char *name)
81 int lenp = strlen(path);
82 int lenn = strlen(name);
87 len = lenp + lenn + 2;
88 if ((lenp > 0) && (path[lenp-1] == '/')) {
94 memcpy(str, path, lenp);
99 memcpy(str+lenp, name, lenn+1);
103 bool util_is_printable_string(const void *data, int len)
105 const char *s = data;
108 /* zero length is not */
112 /* must terminate with zero */
113 if (s[len - 1] != '\0')
120 while (s < se && *s && isprint((unsigned char)*s))
123 /* not zero, or not done yet */
124 if (*s != '\0' || s == ss)
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.
139 static char get_oct_char(const char *s, int *i)
146 strncpy(x, s + *i, 3);
148 val = strtol(x, &endx, 8);
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.
162 static char get_hex_char(const char *s, int *i)
169 strncpy(x, s + *i, 2);
171 val = strtol(x, &endx, 16);
173 die("\\x used with no following hex digits\n");
179 char get_escape_char(const char *s, int *i)
215 j--; /* need to re-read the first digit as
216 * part of the octal value */
217 val = get_oct_char(s, &j);
220 val = get_hex_char(s, &j);
230 int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
232 int fd = 0; /* assume stdin */
234 off_t bufsize = 1024, offset = 0;
238 if (strcmp(filename, "-") != 0) {
239 fd = open(filename, O_RDONLY);
244 /* Loop until we have read everything */
245 buf = xmalloc(bufsize);
247 /* Expand the buffer to hold the next chunk */
248 if (offset == bufsize) {
250 buf = xrealloc(buf, bufsize);
253 ret = read(fd, &buf[offset], bufsize - offset);
261 /* Clean up, including closing stdin; return errno on error */
271 int utilfdt_read_err(const char *filename, char **buffp)
274 return utilfdt_read_err_len(filename, buffp, &len);
277 char *utilfdt_read_len(const char *filename, off_t *len)
280 int ret = utilfdt_read_err_len(filename, &buff, len);
283 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
287 /* Successful read */
291 char *utilfdt_read(const char *filename)
294 return utilfdt_read_len(filename, &len);
297 int utilfdt_write_err(const char *filename, const void *blob)
299 int fd = 1; /* assume stdout */
303 const char *ptr = blob;
305 if (strcmp(filename, "-") != 0) {
306 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
311 totalsize = fdt_totalsize(blob);
314 while (offset < totalsize) {
315 ret = write(fd, ptr + offset, totalsize - offset);
322 /* Close the file/stdin; return errno on error */
325 return ret < 0 ? -ret : 0;
329 int utilfdt_write(const char *filename, const void *blob)
331 int ret = utilfdt_write_err(filename, blob);
334 fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
340 int utilfdt_decode_type(const char *fmt, int *type, int *size)
347 /* get the conversion qualifier */
349 if (strchr("hlLb", *fmt)) {
351 if (qualifier == *fmt) {
353 /* TODO: case 'l': qualifier = 'L'; break;*/
361 /* we should now have a type */
362 if ((*fmt == '\0') || !strchr("iuxs", *fmt))
365 /* convert qualifier (bhL) to byte size */
367 *size = qualifier == 'b' ? 1 :
368 qualifier == 'h' ? 2 :
369 qualifier == 'l' ? 4 : -1;
372 /* that should be it! */
378 void utilfdt_print_data(const char *data, int len)
383 /* no data, don't print */
387 if (util_is_printable_string(data, len)) {
396 } while (s < data + len);
398 } else if ((len % 4) == 0) {
399 const fdt32_t *cell = (const fdt32_t *)data;
402 for (i = 0, len /= 4; i < len; i++)
403 printf("0x%08x%s", fdt32_to_cpu(cell[i]),
404 i < (len - 1) ? " " : "");
407 const unsigned char *p = (const unsigned char *)data;
409 for (i = 0; i < len; i++)
410 printf("%02x%s", *p++, i < len - 1 ? " " : "");
415 void NORETURN util_version(void)
417 printf("Version: %s\n", DTC_VERSION);
421 void 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[])
426 FILE *fp = errmsg ? stderr : stdout;
427 const char a_arg[] = "<arg>";
428 size_t a_arg_len = strlen(a_arg) + 1;
435 "Options: -[%s]\n", synopsis, short_opts);
437 /* prescan the --long opt length to auto-align */
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)
448 for (i = 0; long_opts[i].name; ++i) {
449 /* helps when adding new applets or options */
450 assert(opts_help[i] != NULL);
452 /* first output the short flag if it has one */
453 if (long_opts[i].val > '~')
456 fprintf(fp, " -%c, ", long_opts[i].val);
458 /* then the long flag */
459 if (long_opts[i].has_arg == no_argument)
460 fprintf(fp, "--%-*s", optlen, long_opts[i].name);
462 fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
463 (int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
465 /* finally the help text */
466 fprintf(fp, "%s\n", opts_help[i]);
470 fprintf(fp, "\nError: %s\n", errmsg);