Skip to content

Commit 73ab39b

Browse files
committed
scripts/dtc: Update to upstream version v1.4.0
Update to the latest version of dtc with the following notable enhancements and bug fixes: * fdtput: expand fdt if value does not fit * dtc/fdt{get, put}/convert-dtsv0-lexer: convert to new usage helpers * libfdt: Add fdt_next_subnode() to permit easy subnode iteration * utilfdt_read: pass back up the length of data read * util_version: new helper for displaying version info * die: constify format string arg * utilfdt_read_err: use xmalloc funcs * Export fdt_stringlist_contains() * dtc: Drop the '-S is deprecated' warning * dtc/libfdt: sparse fixes * dtc/libfdt: introduce fdt types for annotation by endian checkers * Fix util_is_printable_string * dtc: srcpos_verror() should print to stderr * libfdt: Added missing functions to shared library Shipped bison/flex generated files were built on an Ubuntu 13.10 system. Signed-off-by: Grant Likely <grant.likely@linaro.org>
1 parent a878b91 commit 73ab39b

6 files changed

Lines changed: 294 additions & 82 deletions

File tree

‎scripts/dtc/dtc.c‎

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include "dtc.h"
2222
#include "srcpos.h"
2323

24-
#include "version_gen.h"
25-
2624
/*
2725
* Command line options
2826
*/
@@ -49,55 +47,60 @@ static void fill_fullpaths(struct node *tree, const char *prefix)
4947
fill_fullpaths(child, tree->fullpath);
5048
}
5149

52-
static void __attribute__ ((noreturn)) usage(void)
53-
{
54-
fprintf(stderr, "Usage:\n");
55-
fprintf(stderr, "\tdtc [options] <input file>\n");
56-
fprintf(stderr, "\nOptions:\n");
57-
fprintf(stderr, "\t-h\n");
58-
fprintf(stderr, "\t\tThis help text\n");
59-
fprintf(stderr, "\t-q\n");
60-
fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n");
61-
fprintf(stderr, "\t-I <input format>\n");
62-
fprintf(stderr, "\t\tInput formats are:\n");
63-
fprintf(stderr, "\t\t\tdts - device tree source text\n");
64-
fprintf(stderr, "\t\t\tdtb - device tree blob\n");
65-
fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
66-
fprintf(stderr, "\t-o <output file>\n");
67-
fprintf(stderr, "\t-O <output format>\n");
68-
fprintf(stderr, "\t\tOutput formats are:\n");
69-
fprintf(stderr, "\t\t\tdts - device tree source text\n");
70-
fprintf(stderr, "\t\t\tdtb - device tree blob\n");
71-
fprintf(stderr, "\t\t\tasm - assembler source\n");
72-
fprintf(stderr, "\t-V <output version>\n");
73-
fprintf(stderr, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", DEFAULT_FDT_VERSION);
74-
fprintf(stderr, "\t-d <output dependency file>\n");
75-
fprintf(stderr, "\t-R <number>\n");
76-
fprintf(stderr, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n");
77-
fprintf(stderr, "\t-S <bytes>\n");
78-
fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\n");
79-
fprintf(stderr, "\t-p <bytes>\n");
80-
fprintf(stderr, "\t\tAdd padding to the blob of <bytes> long (extra space)\n");
81-
fprintf(stderr, "\t-b <number>\n");
82-
fprintf(stderr, "\t\tSet the physical boot cpu\n");
83-
fprintf(stderr, "\t-f\n");
84-
fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
85-
fprintf(stderr, "\t-i\n");
86-
fprintf(stderr, "\t\tAdd a path to search for include files\n");
87-
fprintf(stderr, "\t-s\n");
88-
fprintf(stderr, "\t\tSort nodes and properties before outputting (only useful for\n\t\tcomparing trees)\n");
89-
fprintf(stderr, "\t-v\n");
90-
fprintf(stderr, "\t\tPrint DTC version and exit\n");
91-
fprintf(stderr, "\t-H <phandle format>\n");
92-
fprintf(stderr, "\t\tphandle formats are:\n");
93-
fprintf(stderr, "\t\t\tlegacy - \"linux,phandle\" properties only\n");
94-
fprintf(stderr, "\t\t\tepapr - \"phandle\" properties only\n");
95-
fprintf(stderr, "\t\t\tboth - Both \"linux,phandle\" and \"phandle\" properties\n");
96-
fprintf(stderr, "\t-W [no-]<checkname>\n");
97-
fprintf(stderr, "\t-E [no-]<checkname>\n");
98-
fprintf(stderr, "\t\t\tenable or disable warnings and errors\n");
99-
exit(3);
100-
}
50+
/* Usage related data. */
51+
static const char usage_synopsis[] = "dtc [options] <input file>";
52+
static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:fb:i:H:sW:E:hv";
53+
static struct option const usage_long_opts[] = {
54+
{"quiet", no_argument, NULL, 'q'},
55+
{"in-format", a_argument, NULL, 'I'},
56+
{"out", a_argument, NULL, 'o'},
57+
{"out-format", a_argument, NULL, 'O'},
58+
{"out-version", a_argument, NULL, 'V'},
59+
{"out-dependency", a_argument, NULL, 'd'},
60+
{"reserve", a_argument, NULL, 'R'},
61+
{"space", a_argument, NULL, 'S'},
62+
{"pad", a_argument, NULL, 'p'},
63+
{"boot-cpu", a_argument, NULL, 'b'},
64+
{"force", no_argument, NULL, 'f'},
65+
{"include", a_argument, NULL, 'i'},
66+
{"sort", no_argument, NULL, 's'},
67+
{"phandle", a_argument, NULL, 'H'},
68+
{"warning", a_argument, NULL, 'W'},
69+
{"error", a_argument, NULL, 'E'},
70+
{"help", no_argument, NULL, 'h'},
71+
{"version", no_argument, NULL, 'v'},
72+
{NULL, no_argument, NULL, 0x0},
73+
};
74+
static const char * const usage_opts_help[] = {
75+
"\n\tQuiet: -q suppress warnings, -qq errors, -qqq all",
76+
"\n\tInput formats are:\n"
77+
"\t\tdts - device tree source text\n"
78+
"\t\tdtb - device tree blob\n"
79+
"\t\tfs - /proc/device-tree style directory",
80+
"\n\tOutput file",
81+
"\n\tOutput formats are:\n"
82+
"\t\tdts - device tree source text\n"
83+
"\t\tdtb - device tree blob\n"
84+
"\t\tasm - assembler source",
85+
"\n\tBlob version to produce, defaults to %d (for dtb and asm output)", //, DEFAULT_FDT_VERSION);
86+
"\n\tOutput dependency file",
87+
"\n\ttMake space for <number> reserve map entries (for dtb and asm output)",
88+
"\n\tMake the blob at least <bytes> long (extra space)",
89+
"\n\tAdd padding to the blob of <bytes> long (extra space)",
90+
"\n\tSet the physical boot cpu",
91+
"\n\tTry to produce output even if the input tree has errors",
92+
"\n\tAdd a path to search for include files",
93+
"\n\tSort nodes and properties before outputting (useful for comparing trees)",
94+
"\n\tValid phandle formats are:\n"
95+
"\t\tlegacy - \"linux,phandle\" properties only\n"
96+
"\t\tepapr - \"phandle\" properties only\n"
97+
"\t\tboth - Both \"linux,phandle\" and \"phandle\" properties",
98+
"\n\tEnable/disable warnings (prefix with \"no-\")",
99+
"\n\tEnable/disable errors (prefix with \"no-\")",
100+
"\n\tPrint this help and exit",
101+
"\n\tPrint version and exit",
102+
NULL,
103+
};
101104

102105
int main(int argc, char *argv[])
103106
{
@@ -118,8 +121,7 @@ int main(int argc, char *argv[])
118121
minsize = 0;
119122
padsize = 0;
120123

121-
while ((opt = getopt(argc, argv, "hI:O:o:V:d:R:S:p:fqb:i:vH:sW:E:"))
122-
!= EOF) {
124+
while ((opt = util_getopt_long()) != EOF) {
123125
switch (opt) {
124126
case 'I':
125127
inform = optarg;
@@ -158,8 +160,7 @@ int main(int argc, char *argv[])
158160
srcfile_add_search_path(optarg);
159161
break;
160162
case 'v':
161-
printf("Version: %s\n", DTC_VERSION);
162-
exit(0);
163+
util_version();
163164
case 'H':
164165
if (streq(optarg, "legacy"))
165166
phandle_format = PHANDLE_LEGACY;
@@ -185,13 +186,14 @@ int main(int argc, char *argv[])
185186
break;
186187

187188
case 'h':
189+
usage(NULL);
188190
default:
189-
usage();
191+
usage("unknown option");
190192
}
191193
}
192194

193195
if (argc > (optind+1))
194-
usage();
196+
usage("missing files");
195197
else if (argc < (optind+1))
196198
arg = "-";
197199
else
@@ -201,9 +203,6 @@ int main(int argc, char *argv[])
201203
if (minsize && padsize)
202204
die("Can't set both -p and -S\n");
203205

204-
if (minsize)
205-
fprintf(stderr, "DTC: Use of \"-S\" is deprecated; it will be removed soon, use \"-p\" instead\n");
206-
207206
if (depname) {
208207
depfile = fopen(depname, "w");
209208
if (!depfile)

‎scripts/dtc/dtc.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ typedef uint32_t cell_t;
6666
#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
6767

6868
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
69-
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
7069

7170
/* Data blobs */
7271
enum markertype {

‎scripts/dtc/srcpos.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ srcpos_verror(struct srcpos *pos, char const *fmt, va_list va)
297297

298298
srcstr = srcpos_string(pos);
299299

300-
fprintf(stdout, "Error: %s ", srcstr);
301-
vfprintf(stdout, fmt, va);
302-
fprintf(stdout, "\n");
300+
fprintf(stderr, "Error: %s ", srcstr);
301+
vfprintf(stderr, fmt, va);
302+
fprintf(stderr, "\n");
303303
}
304304

305305
void

‎scripts/dtc/util.c‎

Lines changed: 129 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "libfdt.h"
3636
#include "util.h"
37+
#include "version_gen.h"
3738

3839
char *xstrdup(const char *s)
3940
{
@@ -72,7 +73,7 @@ char *join_path(const char *path, const char *name)
7273
int util_is_printable_string(const void *data, int len)
7374
{
7475
const char *s = data;
75-
const char *ss;
76+
const char *ss, *se;
7677

7778
/* zero length is not */
7879
if (len == 0)
@@ -82,13 +83,19 @@ int util_is_printable_string(const void *data, int len)
8283
if (s[len - 1] != '\0')
8384
return 0;
8485

85-
ss = s;
86-
while (*s && isprint(*s))
87-
s++;
86+
se = s + len;
8887

89-
/* not zero, or not done yet */
90-
if (*s != '\0' || (s + 1 - ss) < len)
91-
return 0;
88+
while (s < se) {
89+
ss = s;
90+
while (s < se && *s && isprint(*s))
91+
s++;
92+
93+
/* not zero, or not done yet */
94+
if (*s != '\0' || s == ss)
95+
return 0;
96+
97+
s++;
98+
}
9299

93100
return 1;
94101
}
@@ -191,7 +198,7 @@ char get_escape_char(const char *s, int *i)
191198
return val;
192199
}
193200

194-
int utilfdt_read_err(const char *filename, char **buffp)
201+
int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
195202
{
196203
int fd = 0; /* assume stdin */
197204
char *buf = NULL;
@@ -206,12 +213,12 @@ int utilfdt_read_err(const char *filename, char **buffp)
206213
}
207214

208215
/* Loop until we have read everything */
209-
buf = malloc(bufsize);
216+
buf = xmalloc(bufsize);
210217
do {
211218
/* Expand the buffer to hold the next chunk */
212219
if (offset == bufsize) {
213220
bufsize *= 2;
214-
buf = realloc(buf, bufsize);
221+
buf = xrealloc(buf, bufsize);
215222
if (!buf) {
216223
ret = ENOMEM;
217224
break;
@@ -232,13 +239,20 @@ int utilfdt_read_err(const char *filename, char **buffp)
232239
free(buf);
233240
else
234241
*buffp = buf;
242+
*len = bufsize;
235243
return ret;
236244
}
237245

238-
char *utilfdt_read(const char *filename)
246+
int utilfdt_read_err(const char *filename, char **buffp)
247+
{
248+
off_t len;
249+
return utilfdt_read_err_len(filename, buffp, &len);
250+
}
251+
252+
char *utilfdt_read_len(const char *filename, off_t *len)
239253
{
240254
char *buff;
241-
int ret = utilfdt_read_err(filename, &buff);
255+
int ret = utilfdt_read_err_len(filename, &buff, len);
242256

243257
if (ret) {
244258
fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
@@ -249,6 +263,12 @@ char *utilfdt_read(const char *filename)
249263
return buff;
250264
}
251265

266+
char *utilfdt_read(const char *filename)
267+
{
268+
off_t len;
269+
return utilfdt_read_len(filename, &len);
270+
}
271+
252272
int utilfdt_write_err(const char *filename, const void *blob)
253273
{
254274
int fd = 1; /* assume stdout */
@@ -329,3 +349,100 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
329349
return -1;
330350
return 0;
331351
}
352+
353+
void utilfdt_print_data(const char *data, int len)
354+
{
355+
int i;
356+
const char *p = data;
357+
const char *s;
358+
359+
/* no data, don't print */
360+
if (len == 0)
361+
return;
362+
363+
if (util_is_printable_string(data, len)) {
364+
printf(" = ");
365+
366+
s = data;
367+
do {
368+
printf("\"%s\"", s);
369+
s += strlen(s) + 1;
370+
if (s < data + len)
371+
printf(", ");
372+
} while (s < data + len);
373+
374+
} else if ((len % 4) == 0) {
375+
const uint32_t *cell = (const uint32_t *)data;
376+
377+
printf(" = <");
378+
for (i = 0; i < len; i += 4)
379+
printf("0x%08x%s", fdt32_to_cpu(cell[i]),
380+
i < (len - 4) ? " " : "");
381+
printf(">");
382+
} else {
383+
printf(" = [");
384+
for (i = 0; i < len; i++)
385+
printf("%02x%s", *p++, i < len - 1 ? " " : "");
386+
printf("]");
387+
}
388+
}
389+
390+
void util_version(void)
391+
{
392+
printf("Version: %s\n", DTC_VERSION);
393+
exit(0);
394+
}
395+
396+
void util_usage(const char *errmsg, const char *synopsis,
397+
const char *short_opts, struct option const long_opts[],
398+
const char * const opts_help[])
399+
{
400+
FILE *fp = errmsg ? stderr : stdout;
401+
const char a_arg[] = "<arg>";
402+
size_t a_arg_len = strlen(a_arg) + 1;
403+
size_t i;
404+
int optlen;
405+
406+
fprintf(fp,
407+
"Usage: %s\n"
408+
"\n"
409+
"Options: -[%s]\n", synopsis, short_opts);
410+
411+
/* prescan the --long opt length to auto-align */
412+
optlen = 0;
413+
for (i = 0; long_opts[i].name; ++i) {
414+
/* +1 is for space between --opt and help text */
415+
int l = strlen(long_opts[i].name) + 1;
416+
if (long_opts[i].has_arg == a_argument)
417+
l += a_arg_len;
418+
if (optlen < l)
419+
optlen = l;
420+
}
421+
422+
for (i = 0; long_opts[i].name; ++i) {
423+
/* helps when adding new applets or options */
424+
assert(opts_help[i] != NULL);
425+
426+
/* first output the short flag if it has one */
427+
if (long_opts[i].val > '~')
428+
fprintf(fp, " ");
429+
else
430+
fprintf(fp, " -%c, ", long_opts[i].val);
431+
432+
/* then the long flag */
433+
if (long_opts[i].has_arg == no_argument)
434+
fprintf(fp, "--%-*s", optlen, long_opts[i].name);
435+
else
436+
fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
437+
(int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
438+
439+
/* finally the help text */
440+
fprintf(fp, "%s\n", opts_help[i]);
441+
}
442+
443+
if (errmsg) {
444+
fprintf(fp, "\nError: %s\n", errmsg);
445+
exit(EXIT_FAILURE);
446+
} else
447+
exit(EXIT_SUCCESS);
448+
}

0 commit comments

Comments
 (0)