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