blob: 25d4db1e8c2b85eac91b68126c31c1263a187759 [file] [log] [blame]
Tom Rinibd391732017-09-23 12:52:44 -04001/*
2 * Copyright 2011 The Chromium Authors, All Rights Reserved.
3 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
4 *
5 * util_is_printable_string contributed by
6 * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
7 *
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.
12 *
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.
17 *
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
21 * USA
22 */
23
24#include <ctype.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <string.h>
29#include <assert.h>
30
31#include <errno.h>
32#include <fcntl.h>
33#include <unistd.h>
34
35#include "libfdt.h"
36#include "util.h"
37#include "version_gen.h"
38
39char *xstrdup(const char *s)
40{
41 int len = strlen(s) + 1;
42 char *d = xmalloc(len);
43
44 memcpy(d, s, len);
45
46 return d;
47}
48
Patrice Chotard00a7b772025-03-28 17:31:15 +010049char *xstrndup(const char *s, size_t n)
50{
51 size_t len = strnlen(s, n) + 1;
52 char *d = xmalloc(len);
53
54 memcpy(d, s, len - 1);
55 d[len - 1] = '\0';
56
57 return d;
58}
59
Tom Rinibd391732017-09-23 12:52:44 -040060/* based in part from (3) vsnprintf */
61int xasprintf(char **strp, const char *fmt, ...)
62{
63 int n, size = 128; /* start with 128 bytes */
64 char *p;
65 va_list ap;
66
67 /* initial pointer is NULL making the fist realloc to be malloc */
68 p = NULL;
69 while (1) {
70 p = xrealloc(p, size);
71
72 /* Try to print in the allocated space. */
73 va_start(ap, fmt);
74 n = vsnprintf(p, size, fmt, ap);
75 va_end(ap);
76
77 /* If that worked, return the string. */
78 if (n > -1 && n < size)
79 break;
80 /* Else try again with more space. */
81 if (n > -1) /* glibc 2.1 */
82 size = n + 1; /* precisely what is needed */
83 else /* glibc 2.0 */
84 size *= 2; /* twice the old size */
85 }
86 *strp = p;
87 return strlen(p);
88}
89
90char *join_path(const char *path, const char *name)
91{
92 int lenp = strlen(path);
93 int lenn = strlen(name);
94 int len;
95 int needslash = 1;
96 char *str;
97
98 len = lenp + lenn + 2;
99 if ((lenp > 0) && (path[lenp-1] == '/')) {
100 needslash = 0;
101 len--;
102 }
103
104 str = xmalloc(len);
105 memcpy(str, path, lenp);
106 if (needslash) {
107 str[lenp] = '/';
108 lenp++;
109 }
110 memcpy(str+lenp, name, lenn+1);
111 return str;
112}
113
114bool util_is_printable_string(const void *data, int len)
115{
116 const char *s = data;
117 const char *ss, *se;
118
119 /* zero length is not */
120 if (len == 0)
121 return 0;
122
123 /* must terminate with zero */
124 if (s[len - 1] != '\0')
125 return 0;
126
127 se = s + len;
128
129 while (s < se) {
130 ss = s;
131 while (s < se && *s && isprint((unsigned char)*s))
132 s++;
133
134 /* not zero, or not done yet */
135 if (*s != '\0' || s == ss)
136 return 0;
137
138 s++;
139 }
140
141 return 1;
142}
143
144/*
145 * Parse a octal encoded character starting at index i in string s. The
146 * resulting character will be returned and the index i will be updated to
147 * point at the character directly after the end of the encoding, this may be
148 * the '\0' terminator of the string.
149 */
150static char get_oct_char(const char *s, int *i)
151{
152 char x[4];
153 char *endx;
154 long val;
155
156 x[3] = '\0';
157 strncpy(x, s + *i, 3);
158
159 val = strtol(x, &endx, 8);
160
161 assert(endx > x);
162
163 (*i) += endx - x;
164 return val;
165}
166
167/*
168 * Parse a hexadecimal encoded character starting at index i in string s. The
169 * resulting character will be returned and the index i will be updated to
170 * point at the character directly after the end of the encoding, this may be
171 * the '\0' terminator of the string.
172 */
173static char get_hex_char(const char *s, int *i)
174{
175 char x[3];
176 char *endx;
177 long val;
178
179 x[2] = '\0';
180 strncpy(x, s + *i, 2);
181
182 val = strtol(x, &endx, 16);
183 if (!(endx > x))
184 die("\\x used with no following hex digits\n");
185
186 (*i) += endx - x;
187 return val;
188}
189
190char get_escape_char(const char *s, int *i)
191{
192 char c = s[*i];
193 int j = *i + 1;
194 char val;
195
196 switch (c) {
197 case 'a':
198 val = '\a';
199 break;
200 case 'b':
201 val = '\b';
202 break;
203 case 't':
204 val = '\t';
205 break;
206 case 'n':
207 val = '\n';
208 break;
209 case 'v':
210 val = '\v';
211 break;
212 case 'f':
213 val = '\f';
214 break;
215 case 'r':
216 val = '\r';
217 break;
218 case '0':
219 case '1':
220 case '2':
221 case '3':
222 case '4':
223 case '5':
224 case '6':
225 case '7':
226 j--; /* need to re-read the first digit as
227 * part of the octal value */
228 val = get_oct_char(s, &j);
229 break;
230 case 'x':
231 val = get_hex_char(s, &j);
232 break;
233 default:
234 val = c;
235 }
236
237 (*i) = j;
238 return val;
239}
240
241int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
242{
243 int fd = 0; /* assume stdin */
244 char *buf = NULL;
245 off_t bufsize = 1024, offset = 0;
246 int ret = 0;
247
248 *buffp = NULL;
249 if (strcmp(filename, "-") != 0) {
250 fd = open(filename, O_RDONLY);
251 if (fd < 0)
252 return errno;
253 }
254
255 /* Loop until we have read everything */
256 buf = xmalloc(bufsize);
257 do {
258 /* Expand the buffer to hold the next chunk */
259 if (offset == bufsize) {
260 bufsize *= 2;
261 buf = xrealloc(buf, bufsize);
262 }
263
264 ret = read(fd, &buf[offset], bufsize - offset);
265 if (ret < 0) {
266 ret = errno;
267 break;
268 }
269 offset += ret;
270 } while (ret != 0);
271
272 /* Clean up, including closing stdin; return errno on error */
273 close(fd);
274 if (ret)
275 free(buf);
276 else
277 *buffp = buf;
278 *len = bufsize;
279 return ret;
280}
281
282int utilfdt_read_err(const char *filename, char **buffp)
283{
284 off_t len;
285 return utilfdt_read_err_len(filename, buffp, &len);
286}
287
288char *utilfdt_read_len(const char *filename, off_t *len)
289{
290 char *buff;
291 int ret = utilfdt_read_err_len(filename, &buff, len);
292
293 if (ret) {
294 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
295 strerror(ret));
296 return NULL;
297 }
298 /* Successful read */
299 return buff;
300}
301
302char *utilfdt_read(const char *filename)
303{
304 off_t len;
305 return utilfdt_read_len(filename, &len);
306}
307
308int utilfdt_write_err(const char *filename, const void *blob)
309{
310 int fd = 1; /* assume stdout */
311 int totalsize;
312 int offset;
313 int ret = 0;
314 const char *ptr = blob;
315
316 if (strcmp(filename, "-") != 0) {
317 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
318 if (fd < 0)
319 return errno;
320 }
321
322 totalsize = fdt_totalsize(blob);
323 offset = 0;
324
325 while (offset < totalsize) {
326 ret = write(fd, ptr + offset, totalsize - offset);
327 if (ret < 0) {
328 ret = -errno;
329 break;
330 }
331 offset += ret;
332 }
333 /* Close the file/stdin; return errno on error */
334 if (fd != 1)
335 close(fd);
336 return ret < 0 ? -ret : 0;
337}
338
Tom Rinibd391732017-09-23 12:52:44 -0400339int utilfdt_write(const char *filename, const void *blob)
340{
341 int ret = utilfdt_write_err(filename, blob);
342
343 if (ret) {
344 fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
345 strerror(ret));
346 }
347 return ret ? -1 : 0;
348}
349
350int utilfdt_decode_type(const char *fmt, int *type, int *size)
351{
352 int qualifier = 0;
353
354 if (!*fmt)
355 return -1;
356
357 /* get the conversion qualifier */
358 *size = -1;
359 if (strchr("hlLb", *fmt)) {
360 qualifier = *fmt++;
361 if (qualifier == *fmt) {
362 switch (*fmt++) {
363/* TODO: case 'l': qualifier = 'L'; break;*/
364 case 'h':
365 qualifier = 'b';
366 break;
367 }
368 }
369 }
370
371 /* we should now have a type */
372 if ((*fmt == '\0') || !strchr("iuxs", *fmt))
373 return -1;
374
375 /* convert qualifier (bhL) to byte size */
376 if (*fmt != 's')
377 *size = qualifier == 'b' ? 1 :
378 qualifier == 'h' ? 2 :
379 qualifier == 'l' ? 4 : -1;
380 *type = *fmt++;
381
382 /* that should be it! */
383 if (*fmt)
384 return -1;
385 return 0;
386}
387
388void utilfdt_print_data(const char *data, int len)
389{
390 int i;
391 const char *s;
392
393 /* no data, don't print */
394 if (len == 0)
395 return;
396
397 if (util_is_printable_string(data, len)) {
398 printf(" = ");
399
400 s = data;
401 do {
402 printf("\"%s\"", s);
403 s += strlen(s) + 1;
404 if (s < data + len)
405 printf(", ");
406 } while (s < data + len);
407
408 } else if ((len % 4) == 0) {
Tom Rini56aeab52017-09-23 17:30:53 -0400409 const fdt32_t *cell = (const fdt32_t *)data;
Tom Rinibd391732017-09-23 12:52:44 -0400410
411 printf(" = <");
412 for (i = 0, len /= 4; i < len; i++)
413 printf("0x%08x%s", fdt32_to_cpu(cell[i]),
414 i < (len - 1) ? " " : "");
415 printf(">");
416 } else {
417 const unsigned char *p = (const unsigned char *)data;
418 printf(" = [");
419 for (i = 0; i < len; i++)
420 printf("%02x%s", *p++, i < len - 1 ? " " : "");
421 printf("]");
422 }
423}
424
Tom Rini56aeab52017-09-23 17:30:53 -0400425void NORETURN util_version(void)
Tom Rinibd391732017-09-23 12:52:44 -0400426{
427 printf("Version: %s\n", DTC_VERSION);
428 exit(0);
429}
430
Tom Rini56aeab52017-09-23 17:30:53 -0400431void NORETURN util_usage(const char *errmsg, const char *synopsis,
432 const char *short_opts,
433 struct option const long_opts[],
434 const char * const opts_help[])
Tom Rinibd391732017-09-23 12:52:44 -0400435{
436 FILE *fp = errmsg ? stderr : stdout;
437 const char a_arg[] = "<arg>";
438 size_t a_arg_len = strlen(a_arg) + 1;
439 size_t i;
440 int optlen;
441
442 fprintf(fp,
443 "Usage: %s\n"
444 "\n"
445 "Options: -[%s]\n", synopsis, short_opts);
446
447 /* prescan the --long opt length to auto-align */
448 optlen = 0;
449 for (i = 0; long_opts[i].name; ++i) {
450 /* +1 is for space between --opt and help text */
451 int l = strlen(long_opts[i].name) + 1;
452 if (long_opts[i].has_arg == a_argument)
453 l += a_arg_len;
454 if (optlen < l)
455 optlen = l;
456 }
457
458 for (i = 0; long_opts[i].name; ++i) {
459 /* helps when adding new applets or options */
460 assert(opts_help[i] != NULL);
461
462 /* first output the short flag if it has one */
463 if (long_opts[i].val > '~')
464 fprintf(fp, " ");
465 else
466 fprintf(fp, " -%c, ", long_opts[i].val);
467
468 /* then the long flag */
469 if (long_opts[i].has_arg == no_argument)
470 fprintf(fp, "--%-*s", optlen, long_opts[i].name);
471 else
472 fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
473 (int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
474
475 /* finally the help text */
476 fprintf(fp, "%s\n", opts_help[i]);
477 }
478
479 if (errmsg) {
480 fprintf(fp, "\nError: %s\n", errmsg);
481 exit(EXIT_FAILURE);
482 } else
483 exit(EXIT_SUCCESS);
484}