util.c 9.4 KB

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