blob: 9d65226df9e404e7a33ecdbb3f715a3a5a13856e [file] [log] [blame]
John Bonesio658f29a2010-11-17 15:28:20 -08001/*
Stephen Warrencd296722012-09-28 21:25:59 +00002 * Copyright 2011 The Chromium Authors, All Rights Reserved.
John Bonesio658f29a2010-11-17 15:28:20 -08003 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
4 *
Stephen Warrencd296722012-09-28 21:25:59 +00005 * util_is_printable_string contributed by
6 * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
7 *
John Bonesio658f29a2010-11-17 15:28:20 -08008 * 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
Stephen Warrencd296722012-09-28 21:25:59 +000024#include <ctype.h>
John Bonesio658f29a2010-11-17 15:28:20 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <string.h>
Stephen Warrencd296722012-09-28 21:25:59 +000029#include <assert.h>
John Bonesio658f29a2010-11-17 15:28:20 -080030
Stephen Warrencd296722012-09-28 21:25:59 +000031#include <errno.h>
32#include <fcntl.h>
33#include <unistd.h>
34
35#include "libfdt.h"
John Bonesio658f29a2010-11-17 15:28:20 -080036#include "util.h"
Grant Likely73ab39b2014-01-21 12:54:49 +000037#include "version_gen.h"
John Bonesio658f29a2010-11-17 15:28:20 -080038
39char *xstrdup(const char *s)
40{
41 int len = strlen(s) + 1;
Rob Herring47605972015-04-29 16:00:05 -050042 char *d = xmalloc(len);
John Bonesio658f29a2010-11-17 15:28:20 -080043
Rob Herring47605972015-04-29 16:00:05 -050044 memcpy(d, s, len);
John Bonesio658f29a2010-11-17 15:28:20 -080045
Rob Herring47605972015-04-29 16:00:05 -050046 return d;
John Bonesio658f29a2010-11-17 15:28:20 -080047}
48
49char *join_path(const char *path, const char *name)
50{
51 int lenp = strlen(path);
52 int lenn = strlen(name);
53 int len;
54 int needslash = 1;
55 char *str;
56
57 len = lenp + lenn + 2;
58 if ((lenp > 0) && (path[lenp-1] == '/')) {
59 needslash = 0;
60 len--;
61 }
62
63 str = xmalloc(len);
64 memcpy(str, path, lenp);
65 if (needslash) {
66 str[lenp] = '/';
67 lenp++;
68 }
69 memcpy(str+lenp, name, lenn+1);
70 return str;
71}
Stephen Warrencd296722012-09-28 21:25:59 +000072
Rob Herring47605972015-04-29 16:00:05 -050073bool util_is_printable_string(const void *data, int len)
Stephen Warrencd296722012-09-28 21:25:59 +000074{
75 const char *s = data;
Grant Likely73ab39b2014-01-21 12:54:49 +000076 const char *ss, *se;
Stephen Warrencd296722012-09-28 21:25:59 +000077
78 /* zero length is not */
79 if (len == 0)
80 return 0;
81
82 /* must terminate with zero */
83 if (s[len - 1] != '\0')
84 return 0;
85
Grant Likely73ab39b2014-01-21 12:54:49 +000086 se = s + len;
Stephen Warrencd296722012-09-28 21:25:59 +000087
Grant Likely73ab39b2014-01-21 12:54:49 +000088 while (s < se) {
89 ss = s;
Rob Herring47605972015-04-29 16:00:05 -050090 while (s < se && *s && isprint((unsigned char)*s))
Grant Likely73ab39b2014-01-21 12:54:49 +000091 s++;
92
93 /* not zero, or not done yet */
94 if (*s != '\0' || s == ss)
95 return 0;
96
97 s++;
98 }
Stephen Warrencd296722012-09-28 21:25:59 +000099
100 return 1;
101}
102
103/*
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.
108 */
109static char get_oct_char(const char *s, int *i)
110{
111 char x[4];
112 char *endx;
113 long val;
114
115 x[3] = '\0';
116 strncpy(x, s + *i, 3);
117
118 val = strtol(x, &endx, 8);
119
120 assert(endx > x);
121
122 (*i) += endx - x;
123 return val;
124}
125
126/*
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.
131 */
132static char get_hex_char(const char *s, int *i)
133{
134 char x[3];
135 char *endx;
136 long val;
137
138 x[2] = '\0';
139 strncpy(x, s + *i, 2);
140
141 val = strtol(x, &endx, 16);
142 if (!(endx > x))
143 die("\\x used with no following hex digits\n");
144
145 (*i) += endx - x;
146 return val;
147}
148
149char get_escape_char(const char *s, int *i)
150{
151 char c = s[*i];
152 int j = *i + 1;
153 char val;
154
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;
195 }
196
197 (*i) = j;
198 return val;
199}
200
Grant Likely73ab39b2014-01-21 12:54:49 +0000201int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
Stephen Warrencd296722012-09-28 21:25:59 +0000202{
203 int fd = 0; /* assume stdin */
204 char *buf = NULL;
205 off_t bufsize = 1024, offset = 0;
206 int ret = 0;
207
208 *buffp = NULL;
209 if (strcmp(filename, "-") != 0) {
210 fd = open(filename, O_RDONLY);
211 if (fd < 0)
212 return errno;
213 }
214
215 /* Loop until we have read everything */
Grant Likely73ab39b2014-01-21 12:54:49 +0000216 buf = xmalloc(bufsize);
Stephen Warrencd296722012-09-28 21:25:59 +0000217 do {
218 /* Expand the buffer to hold the next chunk */
219 if (offset == bufsize) {
220 bufsize *= 2;
Grant Likely73ab39b2014-01-21 12:54:49 +0000221 buf = xrealloc(buf, bufsize);
Stephen Warrencd296722012-09-28 21:25:59 +0000222 }
223
224 ret = read(fd, &buf[offset], bufsize - offset);
225 if (ret < 0) {
226 ret = errno;
227 break;
228 }
229 offset += ret;
230 } while (ret != 0);
231
232 /* Clean up, including closing stdin; return errno on error */
233 close(fd);
234 if (ret)
235 free(buf);
236 else
237 *buffp = buf;
Grant Likely73ab39b2014-01-21 12:54:49 +0000238 *len = bufsize;
Stephen Warrencd296722012-09-28 21:25:59 +0000239 return ret;
240}
241
Grant Likely73ab39b2014-01-21 12:54:49 +0000242int utilfdt_read_err(const char *filename, char **buffp)
243{
244 off_t len;
245 return utilfdt_read_err_len(filename, buffp, &len);
246}
247
248char *utilfdt_read_len(const char *filename, off_t *len)
Stephen Warrencd296722012-09-28 21:25:59 +0000249{
250 char *buff;
Grant Likely73ab39b2014-01-21 12:54:49 +0000251 int ret = utilfdt_read_err_len(filename, &buff, len);
Stephen Warrencd296722012-09-28 21:25:59 +0000252
253 if (ret) {
254 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
255 strerror(ret));
256 return NULL;
257 }
258 /* Successful read */
259 return buff;
260}
261
Grant Likely73ab39b2014-01-21 12:54:49 +0000262char *utilfdt_read(const char *filename)
263{
264 off_t len;
265 return utilfdt_read_len(filename, &len);
266}
267
Stephen Warrencd296722012-09-28 21:25:59 +0000268int utilfdt_write_err(const char *filename, const void *blob)
269{
270 int fd = 1; /* assume stdout */
271 int totalsize;
272 int offset;
273 int ret = 0;
274 const char *ptr = blob;
275
276 if (strcmp(filename, "-") != 0) {
277 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
278 if (fd < 0)
279 return errno;
280 }
281
282 totalsize = fdt_totalsize(blob);
283 offset = 0;
284
285 while (offset < totalsize) {
286 ret = write(fd, ptr + offset, totalsize - offset);
287 if (ret < 0) {
288 ret = -errno;
289 break;
290 }
291 offset += ret;
292 }
293 /* Close the file/stdin; return errno on error */
294 if (fd != 1)
295 close(fd);
296 return ret < 0 ? -ret : 0;
297}
298
299
300int utilfdt_write(const char *filename, const void *blob)
301{
302 int ret = utilfdt_write_err(filename, blob);
303
304 if (ret) {
305 fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
306 strerror(ret));
307 }
308 return ret ? -1 : 0;
309}
310
311int utilfdt_decode_type(const char *fmt, int *type, int *size)
312{
313 int qualifier = 0;
314
315 if (!*fmt)
316 return -1;
317
318 /* get the conversion qualifier */
319 *size = -1;
320 if (strchr("hlLb", *fmt)) {
321 qualifier = *fmt++;
322 if (qualifier == *fmt) {
323 switch (*fmt++) {
324/* TODO: case 'l': qualifier = 'L'; break;*/
325 case 'h':
326 qualifier = 'b';
327 break;
328 }
329 }
330 }
331
332 /* we should now have a type */
333 if ((*fmt == '\0') || !strchr("iuxs", *fmt))
334 return -1;
335
336 /* convert qualifier (bhL) to byte size */
337 if (*fmt != 's')
338 *size = qualifier == 'b' ? 1 :
339 qualifier == 'h' ? 2 :
340 qualifier == 'l' ? 4 : -1;
341 *type = *fmt++;
342
343 /* that should be it! */
344 if (*fmt)
345 return -1;
346 return 0;
347}
Grant Likely73ab39b2014-01-21 12:54:49 +0000348
349void utilfdt_print_data(const char *data, int len)
350{
351 int i;
352 const char *p = data;
353 const char *s;
354
355 /* no data, don't print */
356 if (len == 0)
357 return;
358
359 if (util_is_printable_string(data, len)) {
360 printf(" = ");
361
362 s = data;
363 do {
364 printf("\"%s\"", s);
365 s += strlen(s) + 1;
366 if (s < data + len)
367 printf(", ");
368 } while (s < data + len);
369
370 } else if ((len % 4) == 0) {
371 const uint32_t *cell = (const uint32_t *)data;
372
373 printf(" = <");
Rob Herring47605972015-04-29 16:00:05 -0500374 for (i = 0, len /= 4; i < len; i++)
Grant Likely73ab39b2014-01-21 12:54:49 +0000375 printf("0x%08x%s", fdt32_to_cpu(cell[i]),
Rob Herring47605972015-04-29 16:00:05 -0500376 i < (len - 1) ? " " : "");
Grant Likely73ab39b2014-01-21 12:54:49 +0000377 printf(">");
378 } else {
379 printf(" = [");
380 for (i = 0; i < len; i++)
381 printf("%02x%s", *p++, i < len - 1 ? " " : "");
382 printf("]");
383 }
384}
385
386void util_version(void)
387{
388 printf("Version: %s\n", DTC_VERSION);
389 exit(0);
390}
391
392void util_usage(const char *errmsg, const char *synopsis,
393 const char *short_opts, struct option const long_opts[],
394 const char * const opts_help[])
395{
396 FILE *fp = errmsg ? stderr : stdout;
397 const char a_arg[] = "<arg>";
398 size_t a_arg_len = strlen(a_arg) + 1;
399 size_t i;
400 int optlen;
401
402 fprintf(fp,
403 "Usage: %s\n"
404 "\n"
405 "Options: -[%s]\n", synopsis, short_opts);
406
407 /* prescan the --long opt length to auto-align */
408 optlen = 0;
409 for (i = 0; long_opts[i].name; ++i) {
410 /* +1 is for space between --opt and help text */
411 int l = strlen(long_opts[i].name) + 1;
412 if (long_opts[i].has_arg == a_argument)
413 l += a_arg_len;
414 if (optlen < l)
415 optlen = l;
416 }
417
418 for (i = 0; long_opts[i].name; ++i) {
419 /* helps when adding new applets or options */
420 assert(opts_help[i] != NULL);
421
422 /* first output the short flag if it has one */
423 if (long_opts[i].val > '~')
424 fprintf(fp, " ");
425 else
426 fprintf(fp, " -%c, ", long_opts[i].val);
427
428 /* then the long flag */
429 if (long_opts[i].has_arg == no_argument)
430 fprintf(fp, "--%-*s", optlen, long_opts[i].name);
431 else
432 fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
433 (int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
434
435 /* finally the help text */
436 fprintf(fp, "%s\n", opts_help[i]);
437 }
438
439 if (errmsg) {
440 fprintf(fp, "\nError: %s\n", errmsg);
441 exit(EXIT_FAILURE);
442 } else
443 exit(EXIT_SUCCESS);
444}