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 char *xstrndup(const char *s
, size_t n
)
38 size_t len
= strnlen(s
, n
) + 1;
39 char *d
= xmalloc(len
);
41 memcpy(d
, s
, len
- 1);
47 int xavsprintf_append(char **strp
, const char *fmt
, va_list ap
)
49 int n
, size
= 0; /* start with 128 bytes */
58 n
= vsnprintf(NULL
, 0, fmt
, ap_copy
) + 1;
61 p
= xrealloc(p
, size
+ n
);
63 n
= vsnprintf(p
+ size
, n
, fmt
, ap
);
69 int xasprintf_append(char **strp
, const char *fmt
, ...)
75 n
= xavsprintf_append(strp
, fmt
, ap
);
81 int xasprintf(char **strp
, const char *fmt
, ...)
89 n
= xavsprintf_append(strp
, fmt
, ap
);
95 char *join_path(const char *path
, const char *name
)
97 int lenp
= strlen(path
);
98 int lenn
= strlen(name
);
103 len
= lenp
+ lenn
+ 2;
104 if ((lenp
> 0) && (path
[lenp
-1] == '/')) {
110 memcpy(str
, path
, lenp
);
115 memcpy(str
+lenp
, name
, lenn
+1);
119 bool util_is_printable_string(const void *data
, int len
)
121 const char *s
= data
;
124 /* zero length is not */
128 /* must terminate with zero */
129 if (s
[len
- 1] != '\0')
136 while (s
< se
&& *s
&& isprint((unsigned char)*s
))
139 /* not zero, or not done yet */
140 if (*s
!= '\0' || s
== ss
)
150 * Parse a octal encoded character starting at index i in string s. The
151 * resulting character will be returned and the index i will be updated to
152 * point at the character directly after the end of the encoding, this may be
153 * the '\0' terminator of the string.
155 static char get_oct_char(const char *s
, int *i
)
162 strncpy(x
, s
+ *i
, 3);
164 val
= strtol(x
, &endx
, 8);
173 * Parse a hexadecimal encoded character starting at index i in string s. The
174 * resulting character will be returned and the index i will be updated to
175 * point at the character directly after the end of the encoding, this may be
176 * the '\0' terminator of the string.
178 static char get_hex_char(const char *s
, int *i
)
185 strncpy(x
, s
+ *i
, 2);
187 val
= strtol(x
, &endx
, 16);
189 die("\\x used with no following hex digits\n");
195 char get_escape_char(const char *s
, int *i
)
231 j
--; /* need to re-read the first digit as
232 * part of the octal value */
233 val
= get_oct_char(s
, &j
);
236 val
= get_hex_char(s
, &j
);
246 int utilfdt_read_err(const char *filename
, char **buffp
, size_t *len
)
248 int fd
= 0; /* assume stdin */
250 size_t bufsize
= 1024, offset
= 0;
254 if (strcmp(filename
, "-") != 0) {
255 fd
= open(filename
, O_RDONLY
);
260 /* Loop until we have read everything */
261 buf
= xmalloc(bufsize
);
263 /* Expand the buffer to hold the next chunk */
264 if (offset
== bufsize
) {
266 buf
= xrealloc(buf
, bufsize
);
269 ret
= read(fd
, &buf
[offset
], bufsize
- offset
);
277 /* Clean up, including closing stdin; return errno on error */
288 char *utilfdt_read(const char *filename
, size_t *len
)
291 int ret
= utilfdt_read_err(filename
, &buff
, len
);
294 fprintf(stderr
, "Couldn't open blob from '%s': %s\n", filename
,
298 /* Successful read */
302 int utilfdt_write_err(const char *filename
, const void *blob
)
304 int fd
= 1; /* assume stdout */
308 const char *ptr
= blob
;
310 if (strcmp(filename
, "-") != 0) {
311 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
316 totalsize
= fdt_totalsize(blob
);
319 while (offset
< totalsize
) {
320 ret
= write(fd
, ptr
+ offset
, totalsize
- offset
);
327 /* Close the file/stdin; return errno on error */
330 return ret
< 0 ? -ret
: 0;
334 int utilfdt_write(const char *filename
, const void *blob
)
336 int ret
= utilfdt_write_err(filename
, blob
);
339 fprintf(stderr
, "Couldn't write blob to '%s': %s\n", filename
,
345 int utilfdt_decode_type(const char *fmt
, int *type
, int *size
)
352 /* get the conversion qualifier */
354 if (strchr("hlLb", *fmt
)) {
356 if (qualifier
== *fmt
) {
358 /* TODO: case 'l': qualifier = 'L'; break;*/
366 /* we should now have a type */
367 if ((*fmt
== '\0') || !strchr("iuxsr", *fmt
))
370 /* convert qualifier (bhL) to byte size */
371 if (*fmt
!= 's' && *fmt
!= 'r')
372 *size
= qualifier
== 'b' ? 1 :
373 qualifier
== 'h' ? 2 :
374 qualifier
== 'l' ? 4 : -1;
377 /* that should be it! */
383 void utilfdt_print_data(const char *data
, int len
)
388 /* no data, don't print */
392 if (util_is_printable_string(data
, len
)) {
401 } while (s
< data
+ len
);
403 } else if ((len
% 4) == 0) {
404 const fdt32_t
*cell
= (const fdt32_t
*)data
;
407 for (i
= 0, len
/= 4; i
< len
; i
++)
408 printf("0x%08" PRIx32
"%s", fdt32_to_cpu(cell
[i
]),
409 i
< (len
- 1) ? " " : "");
412 const unsigned char *p
= (const unsigned char *)data
;
414 for (i
= 0; i
< len
; i
++)
415 printf("%02x%s", *p
++, i
< len
- 1 ? " " : "");
420 void NORETURN
util_version(void)
422 printf("Version: %s\n", DTC_VERSION
);
426 void NORETURN
util_usage(const char *errmsg
, const char *synopsis
,
427 const char *short_opts
,
428 struct option
const long_opts
[],
429 const char * const opts_help
[])
431 FILE *fp
= errmsg
? stderr
: stdout
;
432 const char a_arg
[] = "<arg>";
433 size_t a_arg_len
= strlen(a_arg
) + 1;
440 "Options: -[%s]\n", synopsis
, short_opts
);
442 /* prescan the --long opt length to auto-align */
444 for (i
= 0; long_opts
[i
].name
; ++i
) {
445 /* +1 is for space between --opt and help text */
446 int l
= strlen(long_opts
[i
].name
) + 1;
447 if (long_opts
[i
].has_arg
== a_argument
)
453 for (i
= 0; long_opts
[i
].name
; ++i
) {
454 /* helps when adding new applets or options */
455 assert(opts_help
[i
] != NULL
);
457 /* first output the short flag if it has one */
458 if (long_opts
[i
].val
> '~')
461 fprintf(fp
, " -%c, ", long_opts
[i
].val
);
463 /* then the long flag */
464 if (long_opts
[i
].has_arg
== no_argument
)
465 fprintf(fp
, "--%-*s", optlen
, long_opts
[i
].name
);
467 fprintf(fp
, "--%s %s%*s", long_opts
[i
].name
, a_arg
,
468 (int)(optlen
- strlen(long_opts
[i
].name
) - a_arg_len
), "");
470 /* finally the help text */
471 fprintf(fp
, "%s\n", opts_help
[i
]);
475 fprintf(fp
, "\nError: %s\n", errmsg
);