Linux 3.16.75
[linux/fpc-iii.git] / scripts / dtc / util.c
blob3055c16e980dc6fa5347c8d7fac4a6f6f674b821
1 /*
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
21 * USA
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <assert.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <unistd.h>
35 #include "libfdt.h"
36 #include "util.h"
37 #include "version_gen.h"
39 char *xstrdup(const char *s)
41 int len = strlen(s) + 1;
42 char *dup = xmalloc(len);
44 memcpy(dup, s, len);
46 return dup;
49 char *join_path(const char *path, const char *name)
51 int lenp = strlen(path);
52 int lenn = strlen(name);
53 int len;
54 int needslash = 1;
55 char *str;
57 len = lenp + lenn + 2;
58 if ((lenp > 0) && (path[lenp-1] == '/')) {
59 needslash = 0;
60 len--;
63 str = xmalloc(len);
64 memcpy(str, path, lenp);
65 if (needslash) {
66 str[lenp] = '/';
67 lenp++;
69 memcpy(str+lenp, name, lenn+1);
70 return str;
73 int util_is_printable_string(const void *data, int len)
75 const char *s = data;
76 const char *ss, *se;
78 /* zero length is not */
79 if (len == 0)
80 return 0;
82 /* must terminate with zero */
83 if (s[len - 1] != '\0')
84 return 0;
86 se = s + len;
88 while (s < se) {
89 ss = s;
90 while (s < se && *s && isprint(*s))
91 s++;
93 /* not zero, or not done yet */
94 if (*s != '\0' || s == ss)
95 return 0;
97 s++;
100 return 1;
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)
111 char x[4];
112 char *endx;
113 long val;
115 x[3] = '\0';
116 strncpy(x, s + *i, 3);
118 val = strtol(x, &endx, 8);
120 assert(endx > x);
122 (*i) += endx - x;
123 return val;
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)
134 char x[3];
135 char *endx;
136 long val;
138 x[2] = '\0';
139 strncpy(x, s + *i, 2);
141 val = strtol(x, &endx, 16);
142 if (!(endx > x))
143 die("\\x used with no following hex digits\n");
145 (*i) += endx - x;
146 return val;
149 char get_escape_char(const char *s, int *i)
151 char c = s[*i];
152 int j = *i + 1;
153 char val;
155 assert(c);
156 switch (c) {
157 case 'a':
158 val = '\a';
159 break;
160 case 'b':
161 val = '\b';
162 break;
163 case 't':
164 val = '\t';
165 break;
166 case 'n':
167 val = '\n';
168 break;
169 case 'v':
170 val = '\v';
171 break;
172 case 'f':
173 val = '\f';
174 break;
175 case 'r':
176 val = '\r';
177 break;
178 case '0':
179 case '1':
180 case '2':
181 case '3':
182 case '4':
183 case '5':
184 case '6':
185 case '7':
186 j--; /* need to re-read the first digit as
187 * part of the octal value */
188 val = get_oct_char(s, &j);
189 break;
190 case 'x':
191 val = get_hex_char(s, &j);
192 break;
193 default:
194 val = c;
197 (*i) = j;
198 return val;
201 int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
203 int fd = 0; /* assume stdin */
204 char *buf = NULL;
205 off_t bufsize = 1024, offset = 0;
206 int ret = 0;
208 *buffp = NULL;
209 if (strcmp(filename, "-") != 0) {
210 fd = open(filename, O_RDONLY);
211 if (fd < 0)
212 return errno;
215 /* Loop until we have read everything */
216 buf = xmalloc(bufsize);
217 do {
218 /* Expand the buffer to hold the next chunk */
219 if (offset == bufsize) {
220 bufsize *= 2;
221 buf = xrealloc(buf, bufsize);
222 if (!buf) {
223 ret = ENOMEM;
224 break;
228 ret = read(fd, &buf[offset], bufsize - offset);
229 if (ret < 0) {
230 ret = errno;
231 break;
233 offset += ret;
234 } while (ret != 0);
236 /* Clean up, including closing stdin; return errno on error */
237 close(fd);
238 if (ret)
239 free(buf);
240 else
241 *buffp = buf;
242 *len = bufsize;
243 return ret;
246 int utilfdt_read_err(const char *filename, char **buffp)
248 off_t len;
249 return utilfdt_read_err_len(filename, buffp, &len);
252 char *utilfdt_read_len(const char *filename, off_t *len)
254 char *buff;
255 int ret = utilfdt_read_err_len(filename, &buff, len);
257 if (ret) {
258 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
259 strerror(ret));
260 return NULL;
262 /* Successful read */
263 return buff;
266 char *utilfdt_read(const char *filename)
268 off_t len;
269 return utilfdt_read_len(filename, &len);
272 int utilfdt_write_err(const char *filename, const void *blob)
274 int fd = 1; /* assume stdout */
275 int totalsize;
276 int offset;
277 int ret = 0;
278 const char *ptr = blob;
280 if (strcmp(filename, "-") != 0) {
281 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
282 if (fd < 0)
283 return errno;
286 totalsize = fdt_totalsize(blob);
287 offset = 0;
289 while (offset < totalsize) {
290 ret = write(fd, ptr + offset, totalsize - offset);
291 if (ret < 0) {
292 ret = -errno;
293 break;
295 offset += ret;
297 /* Close the file/stdin; return errno on error */
298 if (fd != 1)
299 close(fd);
300 return ret < 0 ? -ret : 0;
304 int utilfdt_write(const char *filename, const void *blob)
306 int ret = utilfdt_write_err(filename, blob);
308 if (ret) {
309 fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
310 strerror(ret));
312 return ret ? -1 : 0;
315 int utilfdt_decode_type(const char *fmt, int *type, int *size)
317 int qualifier = 0;
319 if (!*fmt)
320 return -1;
322 /* get the conversion qualifier */
323 *size = -1;
324 if (strchr("hlLb", *fmt)) {
325 qualifier = *fmt++;
326 if (qualifier == *fmt) {
327 switch (*fmt++) {
328 /* TODO: case 'l': qualifier = 'L'; break;*/
329 case 'h':
330 qualifier = 'b';
331 break;
336 /* we should now have a type */
337 if ((*fmt == '\0') || !strchr("iuxs", *fmt))
338 return -1;
340 /* convert qualifier (bhL) to byte size */
341 if (*fmt != 's')
342 *size = qualifier == 'b' ? 1 :
343 qualifier == 'h' ? 2 :
344 qualifier == 'l' ? 4 : -1;
345 *type = *fmt++;
347 /* that should be it! */
348 if (*fmt)
349 return -1;
350 return 0;
353 void utilfdt_print_data(const char *data, int len)
355 int i;
356 const char *p = data;
357 const char *s;
359 /* no data, don't print */
360 if (len == 0)
361 return;
363 if (util_is_printable_string(data, len)) {
364 printf(" = ");
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);
374 } else if ((len % 4) == 0) {
375 const uint32_t *cell = (const uint32_t *)data;
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("]");
390 void util_version(void)
392 printf("Version: %s\n", DTC_VERSION);
393 exit(0);
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[])
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;
406 fprintf(fp,
407 "Usage: %s\n"
408 "\n"
409 "Options: -[%s]\n", synopsis, short_opts);
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;
422 for (i = 0; long_opts[i].name; ++i) {
423 /* helps when adding new applets or options */
424 assert(opts_help[i] != NULL);
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);
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), "");
439 /* finally the help text */
440 fprintf(fp, "%s\n", opts_help[i]);
443 if (errmsg) {
444 fprintf(fp, "\nError: %s\n", errmsg);
445 exit(EXIT_FAILURE);
446 } else
447 exit(EXIT_SUCCESS);