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 char *join_path(const char *path
, const char *name
)
51 int lenp
= strlen(path
);
52 int lenn
= strlen(name
);
57 len
= lenp
+ lenn
+ 2;
58 if ((lenp
> 0) && (path
[lenp
-1] == '/')) {
64 memcpy(str
, path
, lenp
);
69 memcpy(str
+lenp
, name
, lenn
+1);
73 bool util_is_printable_string(const void *data
, int len
)
78 /* zero length is not */
82 /* must terminate with zero */
83 if (s
[len
- 1] != '\0')
90 while (s
< se
&& *s
&& isprint((unsigned char)*s
))
93 /* not zero, or not done yet */
94 if (*s
!= '\0' || s
== ss
)
104 * Parse a octal encoded character starting at index i in string s. The
105 * resulting character will be returned and the index i will be updated to
106 * point at the character directly after the end of the encoding, this may be
107 * the '\0' terminator of the string.
109 static char get_oct_char(const char *s
, int *i
)
116 strncpy(x
, s
+ *i
, 3);
118 val
= strtol(x
, &endx
, 8);
127 * Parse a hexadecimal encoded character starting at index i in string s. The
128 * resulting character will be returned and the index i will be updated to
129 * point at the character directly after the end of the encoding, this may be
130 * the '\0' terminator of the string.
132 static char get_hex_char(const char *s
, int *i
)
139 strncpy(x
, s
+ *i
, 2);
141 val
= strtol(x
, &endx
, 16);
143 die("\\x used with no following hex digits\n");
149 char get_escape_char(const char *s
, int *i
)
186 j
--; /* need to re-read the first digit as
187 * part of the octal value */
188 val
= get_oct_char(s
, &j
);
191 val
= get_hex_char(s
, &j
);
201 int utilfdt_read_err_len(const char *filename
, char **buffp
, off_t
*len
)
203 int fd
= 0; /* assume stdin */
205 off_t bufsize
= 1024, offset
= 0;
209 if (strcmp(filename
, "-") != 0) {
210 fd
= open(filename
, O_RDONLY
);
215 /* Loop until we have read everything */
216 buf
= xmalloc(bufsize
);
218 /* Expand the buffer to hold the next chunk */
219 if (offset
== bufsize
) {
221 buf
= xrealloc(buf
, bufsize
);
224 ret
= read(fd
, &buf
[offset
], bufsize
- offset
);
232 /* Clean up, including closing stdin; return errno on error */
242 int utilfdt_read_err(const char *filename
, char **buffp
)
245 return utilfdt_read_err_len(filename
, buffp
, &len
);
248 char *utilfdt_read_len(const char *filename
, off_t
*len
)
251 int ret
= utilfdt_read_err_len(filename
, &buff
, len
);
254 fprintf(stderr
, "Couldn't open blob from '%s': %s\n", filename
,
258 /* Successful read */
262 char *utilfdt_read(const char *filename
)
265 return utilfdt_read_len(filename
, &len
);
268 int utilfdt_write_err(const char *filename
, const void *blob
)
270 int fd
= 1; /* assume stdout */
274 const char *ptr
= blob
;
276 if (strcmp(filename
, "-") != 0) {
277 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
282 totalsize
= fdt_totalsize(blob
);
285 while (offset
< totalsize
) {
286 ret
= write(fd
, ptr
+ offset
, totalsize
- offset
);
293 /* Close the file/stdin; return errno on error */
296 return ret
< 0 ? -ret
: 0;
300 int utilfdt_write(const char *filename
, const void *blob
)
302 int ret
= utilfdt_write_err(filename
, blob
);
305 fprintf(stderr
, "Couldn't write blob to '%s': %s\n", filename
,
311 int utilfdt_decode_type(const char *fmt
, int *type
, int *size
)
318 /* get the conversion qualifier */
320 if (strchr("hlLb", *fmt
)) {
322 if (qualifier
== *fmt
) {
324 /* TODO: case 'l': qualifier = 'L'; break;*/
332 /* we should now have a type */
333 if ((*fmt
== '\0') || !strchr("iuxs", *fmt
))
336 /* convert qualifier (bhL) to byte size */
338 *size
= qualifier
== 'b' ? 1 :
339 qualifier
== 'h' ? 2 :
340 qualifier
== 'l' ? 4 : -1;
343 /* that should be it! */
349 void utilfdt_print_data(const char *data
, int len
)
352 const char *p
= data
;
355 /* no data, don't print */
359 if (util_is_printable_string(data
, len
)) {
368 } while (s
< data
+ len
);
370 } else if ((len
% 4) == 0) {
371 const uint32_t *cell
= (const uint32_t *)data
;
374 for (i
= 0, len
/= 4; i
< len
; i
++)
375 printf("0x%08x%s", fdt32_to_cpu(cell
[i
]),
376 i
< (len
- 1) ? " " : "");
380 for (i
= 0; i
< len
; i
++)
381 printf("%02x%s", *p
++, i
< len
- 1 ? " " : "");
386 void util_version(void)
388 printf("Version: %s\n", DTC_VERSION
);
392 void util_usage(const char *errmsg
, const char *synopsis
,
393 const char *short_opts
, struct option
const long_opts
[],
394 const char * const opts_help
[])
396 FILE *fp
= errmsg
? stderr
: stdout
;
397 const char a_arg
[] = "<arg>";
398 size_t a_arg_len
= strlen(a_arg
) + 1;
405 "Options: -[%s]\n", synopsis
, short_opts
);
407 /* prescan the --long opt length to auto-align */
409 for (i
= 0; long_opts
[i
].name
; ++i
) {
410 /* +1 is for space between --opt and help text */
411 int l
= strlen(long_opts
[i
].name
) + 1;
412 if (long_opts
[i
].has_arg
== a_argument
)
418 for (i
= 0; long_opts
[i
].name
; ++i
) {
419 /* helps when adding new applets or options */
420 assert(opts_help
[i
] != NULL
);
422 /* first output the short flag if it has one */
423 if (long_opts
[i
].val
> '~')
426 fprintf(fp
, " -%c, ", long_opts
[i
].val
);
428 /* then the long flag */
429 if (long_opts
[i
].has_arg
== no_argument
)
430 fprintf(fp
, "--%-*s", optlen
, long_opts
[i
].name
);
432 fprintf(fp
, "--%s %s%*s", long_opts
[i
].name
, a_arg
,
433 (int)(optlen
- strlen(long_opts
[i
].name
) - a_arg_len
), "");
435 /* finally the help text */
436 fprintf(fp
, "%s\n", opts_help
[i
]);
440 fprintf(fp
, "\nError: %s\n", errmsg
);