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 int xavsprintf_append(char **strp, const char *fmt, va_list ap)
51 int n, size = 0; /* start with 128 bytes */
60 n = vsnprintf(NULL, 0, fmt, ap_copy) + 1;
63 p = xrealloc(p, size + n);
65 n = vsnprintf(p + size, n, fmt, ap);
71 int xasprintf_append(char **strp, const char *fmt, ...)
77 n = xavsprintf_append(strp, fmt, ap);
83 int xasprintf(char **strp, const char *fmt, ...)
91 n = xavsprintf_append(strp, fmt, ap);
97 char *join_path(const char *path, const char *name)
99 int lenp = strlen(path);
100 int lenn = strlen(name);
105 len = lenp + lenn + 2;
106 if ((lenp > 0) && (path[lenp-1] == '/')) {
112 memcpy(str, path, lenp);
117 memcpy(str+lenp, name, lenn+1);
121 bool util_is_printable_string(const void *data, int len)
123 const char *s = data;
126 /* zero length is not */
130 /* must terminate with zero */
131 if (s[len - 1] != '\0')
138 while (s < se && *s && isprint((unsigned char)*s))
141 /* not zero, or not done yet */
142 if (*s != '\0' || s == ss)
152 * Parse a octal encoded character starting at index i in string s. The
153 * resulting character will be returned and the index i will be updated to
154 * point at the character directly after the end of the encoding, this may be
155 * the '\0' terminator of the string.
157 static char get_oct_char(const char *s, int *i)
164 strncpy(x, s + *i, 3);
166 val = strtol(x, &endx, 8);
175 * Parse a hexadecimal encoded character starting at index i in string s. The
176 * resulting character will be returned and the index i will be updated to
177 * point at the character directly after the end of the encoding, this may be
178 * the '\0' terminator of the string.
180 static char get_hex_char(const char *s, int *i)
187 strncpy(x, s + *i, 2);
189 val = strtol(x, &endx, 16);
191 die("\\x used with no following hex digits\n");
197 char get_escape_char(const char *s, int *i)
233 j--; /* need to re-read the first digit as
234 * part of the octal value */
235 val = get_oct_char(s, &j);
238 val = get_hex_char(s, &j);
248 int utilfdt_read_err(const char *filename, char **buffp, size_t *len)
250 int fd = 0; /* assume stdin */
252 size_t bufsize = 1024, offset = 0;
256 if (strcmp(filename, "-") != 0) {
257 fd = open(filename, O_RDONLY);
262 /* Loop until we have read everything */
263 buf = xmalloc(bufsize);
265 /* Expand the buffer to hold the next chunk */
266 if (offset == bufsize) {
268 buf = xrealloc(buf, bufsize);
271 ret = read(fd, &buf[offset], bufsize - offset);
279 /* Clean up, including closing stdin; return errno on error */
290 char *utilfdt_read(const char *filename, size_t *len)
293 int ret = utilfdt_read_err(filename, &buff, len);
296 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
300 /* Successful read */
304 int utilfdt_write_err(const char *filename, const void *blob)
306 int fd = 1; /* assume stdout */
310 const char *ptr = blob;
312 if (strcmp(filename, "-") != 0) {
313 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
318 totalsize = fdt_totalsize(blob);
321 while (offset < totalsize) {
322 ret = write(fd, ptr + offset, totalsize - offset);
329 /* Close the file/stdin; return errno on error */
332 return ret < 0 ? -ret : 0;
336 int utilfdt_write(const char *filename, const void *blob)
338 int ret = utilfdt_write_err(filename, blob);
341 fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
347 int utilfdt_decode_type(const char *fmt, int *type, int *size)
354 /* get the conversion qualifier */
356 if (strchr("hlLb", *fmt)) {
358 if (qualifier == *fmt) {
360 /* TODO: case 'l': qualifier = 'L'; break;*/
368 /* we should now have a type */
369 if ((*fmt == '\0') || !strchr("iuxs", *fmt))
372 /* convert qualifier (bhL) to byte size */
374 *size = qualifier == 'b' ? 1 :
375 qualifier == 'h' ? 2 :
376 qualifier == 'l' ? 4 : -1;
379 /* that should be it! */
385 void utilfdt_print_data(const char *data, int len)
390 /* no data, don't print */
394 if (util_is_printable_string(data, len)) {
403 } while (s < data + len);
405 } else if ((len % 4) == 0) {
406 const fdt32_t *cell = (const fdt32_t *)data;
409 for (i = 0, len /= 4; i < len; i++)
410 printf("0x%08x%s", fdt32_to_cpu(cell[i]),
411 i < (len - 1) ? " " : "");
414 const unsigned char *p = (const unsigned char *)data;
416 for (i = 0; i < len; i++)
417 printf("%02x%s", *p++, i < len - 1 ? " " : "");
422 void NORETURN util_version(void)
424 printf("Version: %s\n", DTC_VERSION);
428 void NORETURN util_usage(const char *errmsg, const char *synopsis,
429 const char *short_opts,
430 struct option const long_opts[],
431 const char * const opts_help[])
433 FILE *fp = errmsg ? stderr : stdout;
434 const char a_arg[] = "<arg>";
435 size_t a_arg_len = strlen(a_arg) + 1;
442 "Options: -[%s]\n", synopsis, short_opts);
444 /* prescan the --long opt length to auto-align */
446 for (i = 0; long_opts[i].name; ++i) {
447 /* +1 is for space between --opt and help text */
448 int l = strlen(long_opts[i].name) + 1;
449 if (long_opts[i].has_arg == a_argument)
455 for (i = 0; long_opts[i].name; ++i) {
456 /* helps when adding new applets or options */
457 assert(opts_help[i] != NULL);
459 /* first output the short flag if it has one */
460 if (long_opts[i].val > '~')
463 fprintf(fp, " -%c, ", long_opts[i].val);
465 /* then the long flag */
466 if (long_opts[i].has_arg == no_argument)
467 fprintf(fp, "--%-*s", optlen, long_opts[i].name);
469 fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
470 (int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
472 /* finally the help text */
473 fprintf(fp, "%s\n", opts_help[i]);
477 fprintf(fp, "\nError: %s\n", errmsg);