dtc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. * USA
  19. */
  20. #include <sys/stat.h>
  21. #include "dtc.h"
  22. #include "srcpos.h"
  23. /*
  24. * Command line options
  25. */
  26. int quiet; /* Level of quietness */
  27. int reservenum; /* Number of memory reservation slots */
  28. int minsize; /* Minimum blob size */
  29. int padsize; /* Additional padding to blob */
  30. int phandle_format = PHANDLE_BOTH; /* Use linux,phandle or phandle properties */
  31. static void fill_fullpaths(struct node *tree, const char *prefix)
  32. {
  33. struct node *child;
  34. const char *unit;
  35. tree->fullpath = join_path(prefix, tree->name);
  36. unit = strchr(tree->name, '@');
  37. if (unit)
  38. tree->basenamelen = unit - tree->name;
  39. else
  40. tree->basenamelen = strlen(tree->name);
  41. for_each_child(tree, child)
  42. fill_fullpaths(child, tree->fullpath);
  43. }
  44. /* Usage related data. */
  45. #define FDT_VERSION(version) _FDT_VERSION(version)
  46. #define _FDT_VERSION(version) #version
  47. static const char usage_synopsis[] = "dtc [options] <input file>";
  48. static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:fb:i:H:sW:E:hv";
  49. static struct option const usage_long_opts[] = {
  50. {"quiet", no_argument, NULL, 'q'},
  51. {"in-format", a_argument, NULL, 'I'},
  52. {"out", a_argument, NULL, 'o'},
  53. {"out-format", a_argument, NULL, 'O'},
  54. {"out-version", a_argument, NULL, 'V'},
  55. {"out-dependency", a_argument, NULL, 'd'},
  56. {"reserve", a_argument, NULL, 'R'},
  57. {"space", a_argument, NULL, 'S'},
  58. {"pad", a_argument, NULL, 'p'},
  59. {"boot-cpu", a_argument, NULL, 'b'},
  60. {"force", no_argument, NULL, 'f'},
  61. {"include", a_argument, NULL, 'i'},
  62. {"sort", no_argument, NULL, 's'},
  63. {"phandle", a_argument, NULL, 'H'},
  64. {"warning", a_argument, NULL, 'W'},
  65. {"error", a_argument, NULL, 'E'},
  66. {"help", no_argument, NULL, 'h'},
  67. {"version", no_argument, NULL, 'v'},
  68. {NULL, no_argument, NULL, 0x0},
  69. };
  70. static const char * const usage_opts_help[] = {
  71. "\n\tQuiet: -q suppress warnings, -qq errors, -qqq all",
  72. "\n\tInput formats are:\n"
  73. "\t\tdts - device tree source text\n"
  74. "\t\tdtb - device tree blob\n"
  75. "\t\tfs - /proc/device-tree style directory",
  76. "\n\tOutput file",
  77. "\n\tOutput formats are:\n"
  78. "\t\tdts - device tree source text\n"
  79. "\t\tdtb - device tree blob\n"
  80. "\t\tasm - assembler source",
  81. "\n\tBlob version to produce, defaults to "FDT_VERSION(DEFAULT_FDT_VERSION)" (for dtb and asm output)",
  82. "\n\tOutput dependency file",
  83. "\n\tMake space for <number> reserve map entries (for dtb and asm output)",
  84. "\n\tMake the blob at least <bytes> long (extra space)",
  85. "\n\tAdd padding to the blob of <bytes> long (extra space)",
  86. "\n\tSet the physical boot cpu",
  87. "\n\tTry to produce output even if the input tree has errors",
  88. "\n\tAdd a path to search for include files",
  89. "\n\tSort nodes and properties before outputting (useful for comparing trees)",
  90. "\n\tValid phandle formats are:\n"
  91. "\t\tlegacy - \"linux,phandle\" properties only\n"
  92. "\t\tepapr - \"phandle\" properties only\n"
  93. "\t\tboth - Both \"linux,phandle\" and \"phandle\" properties",
  94. "\n\tEnable/disable warnings (prefix with \"no-\")",
  95. "\n\tEnable/disable errors (prefix with \"no-\")",
  96. "\n\tPrint this help and exit",
  97. "\n\tPrint version and exit",
  98. NULL,
  99. };
  100. static const char *guess_type_by_name(const char *fname, const char *fallback)
  101. {
  102. const char *s;
  103. s = strrchr(fname, '.');
  104. if (s == NULL)
  105. return fallback;
  106. if (!strcasecmp(s, ".dts"))
  107. return "dts";
  108. if (!strcasecmp(s, ".dtb"))
  109. return "dtb";
  110. return fallback;
  111. }
  112. static const char *guess_input_format(const char *fname, const char *fallback)
  113. {
  114. struct stat statbuf;
  115. uint32_t magic;
  116. FILE *f;
  117. if (stat(fname, &statbuf) != 0)
  118. return fallback;
  119. if (S_ISDIR(statbuf.st_mode))
  120. return "fs";
  121. if (!S_ISREG(statbuf.st_mode))
  122. return fallback;
  123. f = fopen(fname, "r");
  124. if (f == NULL)
  125. return fallback;
  126. if (fread(&magic, 4, 1, f) != 1) {
  127. fclose(f);
  128. return fallback;
  129. }
  130. fclose(f);
  131. magic = fdt32_to_cpu(magic);
  132. if (magic == FDT_MAGIC)
  133. return "dtb";
  134. return guess_type_by_name(fname, fallback);
  135. }
  136. int main(int argc, char *argv[])
  137. {
  138. struct boot_info *bi;
  139. const char *inform = NULL;
  140. const char *outform = NULL;
  141. const char *outname = "-";
  142. const char *depname = NULL;
  143. bool force = false, sort = false;
  144. const char *arg;
  145. int opt;
  146. FILE *outf = NULL;
  147. int outversion = DEFAULT_FDT_VERSION;
  148. long long cmdline_boot_cpuid = -1;
  149. quiet = 0;
  150. reservenum = 0;
  151. minsize = 0;
  152. padsize = 0;
  153. while ((opt = util_getopt_long()) != EOF) {
  154. switch (opt) {
  155. case 'I':
  156. inform = optarg;
  157. break;
  158. case 'O':
  159. outform = optarg;
  160. break;
  161. case 'o':
  162. outname = optarg;
  163. break;
  164. case 'V':
  165. outversion = strtol(optarg, NULL, 0);
  166. break;
  167. case 'd':
  168. depname = optarg;
  169. break;
  170. case 'R':
  171. reservenum = strtol(optarg, NULL, 0);
  172. break;
  173. case 'S':
  174. minsize = strtol(optarg, NULL, 0);
  175. break;
  176. case 'p':
  177. padsize = strtol(optarg, NULL, 0);
  178. break;
  179. case 'f':
  180. force = true;
  181. break;
  182. case 'q':
  183. quiet++;
  184. break;
  185. case 'b':
  186. cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
  187. break;
  188. case 'i':
  189. srcfile_add_search_path(optarg);
  190. break;
  191. case 'v':
  192. util_version();
  193. case 'H':
  194. if (streq(optarg, "legacy"))
  195. phandle_format = PHANDLE_LEGACY;
  196. else if (streq(optarg, "epapr"))
  197. phandle_format = PHANDLE_EPAPR;
  198. else if (streq(optarg, "both"))
  199. phandle_format = PHANDLE_BOTH;
  200. else
  201. die("Invalid argument \"%s\" to -H option\n",
  202. optarg);
  203. break;
  204. case 's':
  205. sort = true;
  206. break;
  207. case 'W':
  208. parse_checks_option(true, false, optarg);
  209. break;
  210. case 'E':
  211. parse_checks_option(false, true, optarg);
  212. break;
  213. case 'h':
  214. usage(NULL);
  215. default:
  216. usage("unknown option");
  217. }
  218. }
  219. if (argc > (optind+1))
  220. usage("missing files");
  221. else if (argc < (optind+1))
  222. arg = "-";
  223. else
  224. arg = argv[optind];
  225. /* minsize and padsize are mutually exclusive */
  226. if (minsize && padsize)
  227. die("Can't set both -p and -S\n");
  228. if (depname) {
  229. depfile = fopen(depname, "w");
  230. if (!depfile)
  231. die("Couldn't open dependency file %s: %s\n", depname,
  232. strerror(errno));
  233. fprintf(depfile, "%s:", outname);
  234. }
  235. if (inform == NULL)
  236. inform = guess_input_format(arg, "dts");
  237. if (outform == NULL) {
  238. outform = guess_type_by_name(outname, NULL);
  239. if (outform == NULL) {
  240. if (streq(inform, "dts"))
  241. outform = "dtb";
  242. else
  243. outform = "dts";
  244. }
  245. }
  246. if (streq(inform, "dts"))
  247. bi = dt_from_source(arg);
  248. else if (streq(inform, "fs"))
  249. bi = dt_from_fs(arg);
  250. else if(streq(inform, "dtb"))
  251. bi = dt_from_blob(arg);
  252. else
  253. die("Unknown input format \"%s\"\n", inform);
  254. if (depfile) {
  255. fputc('\n', depfile);
  256. fclose(depfile);
  257. }
  258. if (cmdline_boot_cpuid != -1)
  259. bi->boot_cpuid_phys = cmdline_boot_cpuid;
  260. fill_fullpaths(bi->dt, "");
  261. process_checks(force, bi);
  262. if (sort)
  263. sort_tree(bi);
  264. if (streq(outname, "-")) {
  265. outf = stdout;
  266. } else {
  267. outf = fopen(outname, "wb");
  268. if (! outf)
  269. die("Couldn't open output file %s: %s\n",
  270. outname, strerror(errno));
  271. }
  272. if (streq(outform, "dts")) {
  273. dt_to_source(outf, bi);
  274. } else if (streq(outform, "dtb")) {
  275. dt_to_blob(outf, bi, outversion);
  276. } else if (streq(outform, "asm")) {
  277. dt_to_asm(outf, bi, outversion);
  278. } else if (streq(outform, "null")) {
  279. /* do nothing */
  280. } else {
  281. die("Unknown output format \"%s\"\n", outform);
  282. }
  283. exit(0);
  284. }