1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2011 The Chromium Authors, All Rights Reserved.
4 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
6 * util_is_printable_string contributed by
7 * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
24 #include "version_gen.h"
26 char *xstrdup(const char *s
)
28 int len
= strlen(s
) + 1;
29 char *d
= xmalloc(len
);
36 int xavsprintf_append(char **strp
, const char *fmt
, va_list ap
)
38 int n
, size
= 0; /* start with 128 bytes */
47 n
= vsnprintf(NULL
, 0, fmt
, ap_copy
) + 1;
50 p
= xrealloc(p
, size
+ n
);
52 n
= vsnprintf(p
+ size
, n
, fmt
, ap
);
58 int xasprintf_append(char **strp
, const char *fmt
, ...)
64 n
= xavsprintf_append(strp
, fmt
, ap
);
70 int xasprintf(char **strp
, const char *fmt
, ...)
78 n
= xavsprintf_append(strp
, fmt
, ap
);
84 char *join_path(const char *path
, const char *name
)
86 int lenp
= strlen(path
);
87 int lenn
= strlen(name
);
92 len
= lenp
+ lenn
+ 2;
93 if ((lenp
> 0) && (path
[lenp
-1] == '/')) {
99 memcpy(str
, path
, lenp
);
104 memcpy(str
+lenp
, name
, lenn
+1);
108 bool util_is_printable_string(const void *data
, int len
)
110 const char *s
= data
;
113 /* zero length is not */
117 /* must terminate with zero */
118 if (s
[len
- 1] != '\0')
125 while (s
< se
&& *s
&& isprint((unsigned char)*s
))
128 /* not zero, or not done yet */
129 if (*s
!= '\0' || s
== ss
)
139 * Parse a octal encoded character starting at index i in string s. The
140 * resulting character will be returned and the index i will be updated to
141 * point at the character directly after the end of the encoding, this may be
142 * the '\0' terminator of the string.
144 static char get_oct_char(const char *s
, int *i
)
151 strncpy(x
, s
+ *i
, 3);
153 val
= strtol(x
, &endx
, 8);
162 * Parse a hexadecimal encoded character starting at index i in string s. The
163 * resulting character will be returned and the index i will be updated to
164 * point at the character directly after the end of the encoding, this may be
165 * the '\0' terminator of the string.
167 static char get_hex_char(const char *s
, int *i
)
174 strncpy(x
, s
+ *i
, 2);
176 val
= strtol(x
, &endx
, 16);
178 die("\\x used with no following hex digits\n");
184 char get_escape_char(const char *s
, int *i
)
220 j
--; /* need to re-read the first digit as
221 * part of the octal value */
222 val
= get_oct_char(s
, &j
);
225 val
= get_hex_char(s
, &j
);
235 int utilfdt_read_err(const char *filename
, char **buffp
, size_t *len
)
237 int fd
= 0; /* assume stdin */
239 size_t bufsize
= 1024, offset
= 0;
243 if (strcmp(filename
, "-") != 0) {
244 fd
= open(filename
, O_RDONLY
);
249 /* Loop until we have read everything */
250 buf
= xmalloc(bufsize
);
252 /* Expand the buffer to hold the next chunk */
253 if (offset
== bufsize
) {
255 buf
= xrealloc(buf
, bufsize
);
258 ret
= read(fd
, &buf
[offset
], bufsize
- offset
);
266 /* Clean up, including closing stdin; return errno on error */
277 char *utilfdt_read(const char *filename
, size_t *len
)
280 int ret
= utilfdt_read_err(filename
, &buff
, len
);
283 fprintf(stderr
, "Couldn't open blob from '%s': %s\n", filename
,
287 /* Successful read */
291 int utilfdt_write_err(const char *filename
, const void *blob
)
293 int fd
= 1; /* assume stdout */
297 const char *ptr
= blob
;
299 if (strcmp(filename
, "-") != 0) {
300 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
305 totalsize
= fdt_totalsize(blob
);
308 while (offset
< totalsize
) {
309 ret
= write(fd
, ptr
+ offset
, totalsize
- offset
);
316 /* Close the file/stdin; return errno on error */
319 return ret
< 0 ? -ret
: 0;
323 int utilfdt_write(const char *filename
, const void *blob
)
325 int ret
= utilfdt_write_err(filename
, blob
);
328 fprintf(stderr
, "Couldn't write blob to '%s': %s\n", filename
,
334 int utilfdt_decode_type(const char *fmt
, int *type
, int *size
)
341 /* get the conversion qualifier */
343 if (strchr("hlLb", *fmt
)) {
345 if (qualifier
== *fmt
) {
347 /* TODO: case 'l': qualifier = 'L'; break;*/
355 /* we should now have a type */
356 if ((*fmt
== '\0') || !strchr("iuxs", *fmt
))
359 /* convert qualifier (bhL) to byte size */
361 *size
= qualifier
== 'b' ? 1 :
362 qualifier
== 'h' ? 2 :
363 qualifier
== 'l' ? 4 : -1;
366 /* that should be it! */
372 void utilfdt_print_data(const char *data
, int len
)
377 /* no data, don't print */
381 if (util_is_printable_string(data
, len
)) {
390 } while (s
< data
+ len
);
392 } else if ((len
% 4) == 0) {
393 const fdt32_t
*cell
= (const fdt32_t
*)data
;
396 for (i
= 0, len
/= 4; i
< len
; i
++)
397 printf("0x%08" PRIx32
"%s", fdt32_to_cpu(cell
[i
]),
398 i
< (len
- 1) ? " " : "");
401 const unsigned char *p
= (const unsigned char *)data
;
403 for (i
= 0; i
< len
; i
++)
404 printf("%02x%s", *p
++, i
< len
- 1 ? " " : "");
409 void NORETURN
util_version(void)
411 printf("Version: %s\n", DTC_VERSION
);
415 void NORETURN
util_usage(const char *errmsg
, const char *synopsis
,
416 const char *short_opts
,
417 struct option
const long_opts
[],
418 const char * const opts_help
[])
420 FILE *fp
= errmsg
? stderr
: stdout
;
421 const char a_arg
[] = "<arg>";
422 size_t a_arg_len
= strlen(a_arg
) + 1;
429 "Options: -[%s]\n", synopsis
, short_opts
);
431 /* prescan the --long opt length to auto-align */
433 for (i
= 0; long_opts
[i
].name
; ++i
) {
434 /* +1 is for space between --opt and help text */
435 int l
= strlen(long_opts
[i
].name
) + 1;
436 if (long_opts
[i
].has_arg
== a_argument
)
442 for (i
= 0; long_opts
[i
].name
; ++i
) {
443 /* helps when adding new applets or options */
444 assert(opts_help
[i
] != NULL
);
446 /* first output the short flag if it has one */
447 if (long_opts
[i
].val
> '~')
450 fprintf(fp
, " -%c, ", long_opts
[i
].val
);
452 /* then the long flag */
453 if (long_opts
[i
].has_arg
== no_argument
)
454 fprintf(fp
, "--%-*s", optlen
, long_opts
[i
].name
);
456 fprintf(fp
, "--%s %s%*s", long_opts
[i
].name
, a_arg
,
457 (int)(optlen
- strlen(long_opts
[i
].name
) - a_arg_len
), "");
459 /* finally the help text */
460 fprintf(fp
, "%s\n", opts_help
[i
]);
464 fprintf(fp
, "\nError: %s\n", errmsg
);