util.c 8.7 KB

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