Blame view

scripts/dtc/util.c 8.5 KB
1a59d1b8e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
658f29a51   John Bonesio   of/flattree: Upda...
2
  /*
cd296721a   Stephen Warren   dtc: import lates...
3
   * Copyright 2011 The Chromium Authors, All Rights Reserved.
658f29a51   John Bonesio   of/flattree: Upda...
4
5
   * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
   *
cd296721a   Stephen Warren   dtc: import lates...
6
7
   * util_is_printable_string contributed by
   *	Pantelis Antoniou <pantelis.antoniou AT gmail.com>
658f29a51   John Bonesio   of/flattree: Upda...
8
   */
cd296721a   Stephen Warren   dtc: import lates...
9
  #include <ctype.h>
658f29a51   John Bonesio   of/flattree: Upda...
10
11
12
13
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdarg.h>
  #include <string.h>
cd296721a   Stephen Warren   dtc: import lates...
14
  #include <assert.h>
0cec114e3   Rob Herring   scripts/dtc: Upda...
15
  #include <inttypes.h>
cd296721a   Stephen Warren   dtc: import lates...
16
17
18
19
  
  #include <errno.h>
  #include <fcntl.h>
  #include <unistd.h>
658f29a51   John Bonesio   of/flattree: Upda...
20

cd296721a   Stephen Warren   dtc: import lates...
21
  #include "libfdt.h"
658f29a51   John Bonesio   of/flattree: Upda...
22
  #include "util.h"
73ab39b14   Grant Likely   scripts/dtc: Upda...
23
  #include "version_gen.h"
658f29a51   John Bonesio   of/flattree: Upda...
24
25
26
27
  
  char *xstrdup(const char *s)
  {
  	int len = strlen(s) + 1;
476059711   Rob Herring   scripts/dtc: Upda...
28
  	char *d = xmalloc(len);
658f29a51   John Bonesio   of/flattree: Upda...
29

476059711   Rob Herring   scripts/dtc: Upda...
30
  	memcpy(d, s, len);
658f29a51   John Bonesio   of/flattree: Upda...
31

476059711   Rob Herring   scripts/dtc: Upda...
32
  	return d;
658f29a51   John Bonesio   of/flattree: Upda...
33
  }
c2e7075ca   Rob Herring   scripts/dtc: Upda...
34
  int xavsprintf_append(char **strp, const char *fmt, va_list ap)
6f05afcbb   Rob Herring   scripts/dtc: Upda...
35
  {
c2e7075ca   Rob Herring   scripts/dtc: Upda...
36
  	int n, size = 0;	/* start with 128 bytes */
6f05afcbb   Rob Herring   scripts/dtc: Upda...
37
  	char *p;
c2e7075ca   Rob Herring   scripts/dtc: Upda...
38
39
40
41
42
43
44
45
46
47
48
49
50
  	va_list ap_copy;
  
  	p = *strp;
  	if (p)
  		size = strlen(p);
  
  	va_copy(ap_copy, ap);
  	n = vsnprintf(NULL, 0, fmt, ap_copy) + 1;
  	va_end(ap_copy);
  
  	p = xrealloc(p, size + n);
  
  	n = vsnprintf(p + size, n, fmt, ap);
6f05afcbb   Rob Herring   scripts/dtc: Upda...
51

6f05afcbb   Rob Herring   scripts/dtc: Upda...
52
53
54
  	*strp = p;
  	return strlen(p);
  }
c2e7075ca   Rob Herring   scripts/dtc: Upda...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  int xasprintf_append(char **strp, const char *fmt, ...)
  {
  	int n;
  	va_list ap;
  
  	va_start(ap, fmt);
  	n = xavsprintf_append(strp, fmt, ap);
  	va_end(ap);
  
  	return n;
  }
  
  int xasprintf(char **strp, const char *fmt, ...)
  {
  	int n;
  	va_list ap;
  
  	*strp = NULL;
  
  	va_start(ap, fmt);
  	n = xavsprintf_append(strp, fmt, ap);
  	va_end(ap);
  
  	return n;
  }
658f29a51   John Bonesio   of/flattree: Upda...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  char *join_path(const char *path, const char *name)
  {
  	int lenp = strlen(path);
  	int lenn = strlen(name);
  	int len;
  	int needslash = 1;
  	char *str;
  
  	len = lenp + lenn + 2;
  	if ((lenp > 0) && (path[lenp-1] == '/')) {
  		needslash = 0;
  		len--;
  	}
  
  	str = xmalloc(len);
  	memcpy(str, path, lenp);
  	if (needslash) {
  		str[lenp] = '/';
  		lenp++;
  	}
  	memcpy(str+lenp, name, lenn+1);
  	return str;
  }
cd296721a   Stephen Warren   dtc: import lates...
103

476059711   Rob Herring   scripts/dtc: Upda...
104
  bool util_is_printable_string(const void *data, int len)
cd296721a   Stephen Warren   dtc: import lates...
105
106
  {
  	const char *s = data;
73ab39b14   Grant Likely   scripts/dtc: Upda...
107
  	const char *ss, *se;
cd296721a   Stephen Warren   dtc: import lates...
108
109
110
111
112
113
114
115
  
  	/* zero length is not */
  	if (len == 0)
  		return 0;
  
  	/* must terminate with zero */
  	if (s[len - 1] != '\0')
  		return 0;
73ab39b14   Grant Likely   scripts/dtc: Upda...
116
  	se = s + len;
cd296721a   Stephen Warren   dtc: import lates...
117

73ab39b14   Grant Likely   scripts/dtc: Upda...
118
119
  	while (s < se) {
  		ss = s;
476059711   Rob Herring   scripts/dtc: Upda...
120
  		while (s < se && *s && isprint((unsigned char)*s))
73ab39b14   Grant Likely   scripts/dtc: Upda...
121
122
123
124
125
126
127
128
  			s++;
  
  		/* not zero, or not done yet */
  		if (*s != '\0' || s == ss)
  			return 0;
  
  		s++;
  	}
cd296721a   Stephen Warren   dtc: import lates...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  
  	return 1;
  }
  
  /*
   * Parse a octal encoded character starting at index i in string s.  The
   * resulting character will be returned and the index i will be updated to
   * point at the character directly after the end of the encoding, this may be
   * the '\0' terminator of the string.
   */
  static char get_oct_char(const char *s, int *i)
  {
  	char x[4];
  	char *endx;
  	long val;
  
  	x[3] = '\0';
  	strncpy(x, s + *i, 3);
  
  	val = strtol(x, &endx, 8);
  
  	assert(endx > x);
  
  	(*i) += endx - x;
  	return val;
  }
  
  /*
   * Parse a hexadecimal encoded character starting at index i in string s.  The
   * resulting character will be returned and the index i will be updated to
   * point at the character directly after the end of the encoding, this may be
   * the '\0' terminator of the string.
   */
  static char get_hex_char(const char *s, int *i)
  {
  	char x[3];
  	char *endx;
  	long val;
  
  	x[2] = '\0';
  	strncpy(x, s + *i, 2);
  
  	val = strtol(x, &endx, 16);
  	if (!(endx  > x))
  		die("\\x used with no following hex digits
  ");
  
  	(*i) += endx - x;
  	return val;
  }
  
  char get_escape_char(const char *s, int *i)
  {
  	char	c = s[*i];
  	int	j = *i + 1;
  	char	val;
cd296721a   Stephen Warren   dtc: import lates...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  	switch (c) {
  	case 'a':
  		val = '\a';
  		break;
  	case 'b':
  		val = '\b';
  		break;
  	case 't':
  		val = '\t';
  		break;
  	case 'n':
  		val = '
  ';
  		break;
  	case 'v':
  		val = '\v';
  		break;
  	case 'f':
  		val = '\f';
  		break;
  	case 'r':
  		val = '\r';
  		break;
  	case '0':
  	case '1':
  	case '2':
  	case '3':
  	case '4':
  	case '5':
  	case '6':
  	case '7':
  		j--; /* need to re-read the first digit as
  		      * part of the octal value */
  		val = get_oct_char(s, &j);
  		break;
  	case 'x':
  		val = get_hex_char(s, &j);
  		break;
  	default:
  		val = c;
  	}
  
  	(*i) = j;
  	return val;
  }
f858927fd   Rob Herring   scripts/dtc: Upda...
230
  int utilfdt_read_err(const char *filename, char **buffp, size_t *len)
cd296721a   Stephen Warren   dtc: import lates...
231
232
233
  {
  	int fd = 0;	/* assume stdin */
  	char *buf = NULL;
f858927fd   Rob Herring   scripts/dtc: Upda...
234
  	size_t bufsize = 1024, offset = 0;
cd296721a   Stephen Warren   dtc: import lates...
235
236
237
238
239
240
241
242
243
244
  	int ret = 0;
  
  	*buffp = NULL;
  	if (strcmp(filename, "-") != 0) {
  		fd = open(filename, O_RDONLY);
  		if (fd < 0)
  			return errno;
  	}
  
  	/* Loop until we have read everything */
73ab39b14   Grant Likely   scripts/dtc: Upda...
245
  	buf = xmalloc(bufsize);
cd296721a   Stephen Warren   dtc: import lates...
246
247
248
249
  	do {
  		/* Expand the buffer to hold the next chunk */
  		if (offset == bufsize) {
  			bufsize *= 2;
73ab39b14   Grant Likely   scripts/dtc: Upda...
250
  			buf = xrealloc(buf, bufsize);
cd296721a   Stephen Warren   dtc: import lates...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  		}
  
  		ret = read(fd, &buf[offset], bufsize - offset);
  		if (ret < 0) {
  			ret = errno;
  			break;
  		}
  		offset += ret;
  	} while (ret != 0);
  
  	/* Clean up, including closing stdin; return errno on error */
  	close(fd);
  	if (ret)
  		free(buf);
  	else
  		*buffp = buf;
f858927fd   Rob Herring   scripts/dtc: Upda...
267
268
  	if (len)
  		*len = bufsize;
cd296721a   Stephen Warren   dtc: import lates...
269
270
  	return ret;
  }
f858927fd   Rob Herring   scripts/dtc: Upda...
271
  char *utilfdt_read(const char *filename, size_t *len)
cd296721a   Stephen Warren   dtc: import lates...
272
273
  {
  	char *buff;
f858927fd   Rob Herring   scripts/dtc: Upda...
274
  	int ret = utilfdt_read_err(filename, &buff, len);
cd296721a   Stephen Warren   dtc: import lates...
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
  
  	if (ret) {
  		fprintf(stderr, "Couldn't open blob from '%s': %s
  ", filename,
  			strerror(ret));
  		return NULL;
  	}
  	/* Successful read */
  	return buff;
  }
  
  int utilfdt_write_err(const char *filename, const void *blob)
  {
  	int fd = 1;	/* assume stdout */
  	int totalsize;
  	int offset;
  	int ret = 0;
  	const char *ptr = blob;
  
  	if (strcmp(filename, "-") != 0) {
  		fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  		if (fd < 0)
  			return errno;
  	}
  
  	totalsize = fdt_totalsize(blob);
  	offset = 0;
  
  	while (offset < totalsize) {
  		ret = write(fd, ptr + offset, totalsize - offset);
  		if (ret < 0) {
  			ret = -errno;
  			break;
  		}
  		offset += ret;
  	}
  	/* Close the file/stdin; return errno on error */
  	if (fd != 1)
  		close(fd);
  	return ret < 0 ? -ret : 0;
  }
  
  
  int utilfdt_write(const char *filename, const void *blob)
  {
  	int ret = utilfdt_write_err(filename, blob);
  
  	if (ret) {
  		fprintf(stderr, "Couldn't write blob to '%s': %s
  ", filename,
  			strerror(ret));
  	}
  	return ret ? -1 : 0;
  }
  
  int utilfdt_decode_type(const char *fmt, int *type, int *size)
  {
  	int qualifier = 0;
  
  	if (!*fmt)
  		return -1;
  
  	/* get the conversion qualifier */
  	*size = -1;
  	if (strchr("hlLb", *fmt)) {
  		qualifier = *fmt++;
  		if (qualifier == *fmt) {
  			switch (*fmt++) {
  /* TODO:		case 'l': qualifier = 'L'; break;*/
  			case 'h':
  				qualifier = 'b';
  				break;
  			}
  		}
  	}
  
  	/* we should now have a type */
  	if ((*fmt == '\0') || !strchr("iuxs", *fmt))
  		return -1;
  
  	/* convert qualifier (bhL) to byte size */
  	if (*fmt != 's')
  		*size = qualifier == 'b' ? 1 :
  				qualifier == 'h' ? 2 :
  				qualifier == 'l' ? 4 : -1;
  	*type = *fmt++;
  
  	/* that should be it! */
  	if (*fmt)
  		return -1;
  	return 0;
  }
73ab39b14   Grant Likely   scripts/dtc: Upda...
367
368
369
370
  
  void utilfdt_print_data(const char *data, int len)
  {
  	int i;
73ab39b14   Grant Likely   scripts/dtc: Upda...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
  	const char *s;
  
  	/* no data, don't print */
  	if (len == 0)
  		return;
  
  	if (util_is_printable_string(data, len)) {
  		printf(" = ");
  
  		s = data;
  		do {
  			printf("\"%s\"", s);
  			s += strlen(s) + 1;
  			if (s < data + len)
  				printf(", ");
  		} while (s < data + len);
  
  	} else if ((len % 4) == 0) {
89d123106   Rob Herring   scripts/dtc: Upda...
389
  		const fdt32_t *cell = (const fdt32_t *)data;
73ab39b14   Grant Likely   scripts/dtc: Upda...
390
391
  
  		printf(" = <");
476059711   Rob Herring   scripts/dtc: Upda...
392
  		for (i = 0, len /= 4; i < len; i++)
0cec114e3   Rob Herring   scripts/dtc: Upda...
393
  			printf("0x%08" PRIx32 "%s", fdt32_to_cpu(cell[i]),
476059711   Rob Herring   scripts/dtc: Upda...
394
  			       i < (len - 1) ? " " : "");
73ab39b14   Grant Likely   scripts/dtc: Upda...
395
396
  		printf(">");
  	} else {
91feabc2e   Rob Herring   scripts/dtc: Upda...
397
  		const unsigned char *p = (const unsigned char *)data;
73ab39b14   Grant Likely   scripts/dtc: Upda...
398
399
400
401
402
403
  		printf(" = [");
  		for (i = 0; i < len; i++)
  			printf("%02x%s", *p++, i < len - 1 ? " " : "");
  		printf("]");
  	}
  }
89d123106   Rob Herring   scripts/dtc: Upda...
404
  void NORETURN util_version(void)
73ab39b14   Grant Likely   scripts/dtc: Upda...
405
406
407
408
409
  {
  	printf("Version: %s
  ", DTC_VERSION);
  	exit(0);
  }
89d123106   Rob Herring   scripts/dtc: Upda...
410
411
412
413
  void NORETURN util_usage(const char *errmsg, const char *synopsis,
  			 const char *short_opts,
  			 struct option const long_opts[],
  			 const char * const opts_help[])
73ab39b14   Grant Likely   scripts/dtc: Upda...
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
  {
  	FILE *fp = errmsg ? stderr : stdout;
  	const char a_arg[] = "<arg>";
  	size_t a_arg_len = strlen(a_arg) + 1;
  	size_t i;
  	int optlen;
  
  	fprintf(fp,
  		"Usage: %s
  "
  		"
  "
  		"Options: -[%s]
  ", synopsis, short_opts);
  
  	/* prescan the --long opt length to auto-align */
  	optlen = 0;
  	for (i = 0; long_opts[i].name; ++i) {
  		/* +1 is for space between --opt and help text */
  		int l = strlen(long_opts[i].name) + 1;
  		if (long_opts[i].has_arg == a_argument)
  			l += a_arg_len;
  		if (optlen < l)
  			optlen = l;
  	}
  
  	for (i = 0; long_opts[i].name; ++i) {
  		/* helps when adding new applets or options */
  		assert(opts_help[i] != NULL);
  
  		/* first output the short flag if it has one */
  		if (long_opts[i].val > '~')
  			fprintf(fp, "      ");
  		else
  			fprintf(fp, "  -%c, ", long_opts[i].val);
  
  		/* then the long flag */
  		if (long_opts[i].has_arg == no_argument)
  			fprintf(fp, "--%-*s", optlen, long_opts[i].name);
  		else
  			fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
  				(int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
  
  		/* finally the help text */
  		fprintf(fp, "%s
  ", opts_help[i]);
  	}
  
  	if (errmsg) {
  		fprintf(fp, "
  Error: %s
  ", errmsg);
  		exit(EXIT_FAILURE);
  	} else
  		exit(EXIT_SUCCESS);
  }