dtc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
  4. */
  5. #include <sys/stat.h>
  6. #include "dtc.h"
  7. #include "srcpos.h"
  8. /*
  9. * Command line options
  10. */
  11. int quiet; /* Level of quietness */
  12. unsigned int reservenum;/* Number of memory reservation slots */
  13. int minsize; /* Minimum blob size */
  14. int padsize; /* Additional padding to blob */
  15. int alignsize; /* Additional padding to blob according to the alignsize */
  16. int phandle_format = PHANDLE_EPAPR; /* Use linux,phandle or phandle properties */
  17. int generate_symbols; /* enable symbols & fixup support */
  18. int generate_fixups; /* suppress generation of fixups on symbol support */
  19. int auto_label_aliases; /* auto generate labels -> aliases */
  20. int annotate; /* Level of annotation: 1 for input source location
  21. >1 for full input source location. */
  22. static int is_power_of_2(int x)
  23. {
  24. return (x > 0) && ((x & (x - 1)) == 0);
  25. }
  26. static void fill_fullpaths(struct node *tree, const char *prefix)
  27. {
  28. struct node *child;
  29. const char *unit;
  30. tree->fullpath = join_path(prefix, tree->name);
  31. unit = strchr(tree->name, '@');
  32. if (unit)
  33. tree->basenamelen = unit - tree->name;
  34. else
  35. tree->basenamelen = strlen(tree->name);
  36. for_each_child(tree, child)
  37. fill_fullpaths(child, tree->fullpath);
  38. }
  39. /* Usage related data. */
  40. static const char usage_synopsis[] = "dtc [options] <input file>";
  41. static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:a:fb:i:H:sW:E:@LAThv";
  42. static struct option const usage_long_opts[] = {
  43. {"quiet", no_argument, NULL, 'q'},
  44. {"in-format", a_argument, NULL, 'I'},
  45. {"out", a_argument, NULL, 'o'},
  46. {"out-format", a_argument, NULL, 'O'},
  47. {"out-version", a_argument, NULL, 'V'},
  48. {"out-dependency", a_argument, NULL, 'd'},
  49. {"reserve", a_argument, NULL, 'R'},
  50. {"space", a_argument, NULL, 'S'},
  51. {"pad", a_argument, NULL, 'p'},
  52. {"align", a_argument, NULL, 'a'},
  53. {"boot-cpu", a_argument, NULL, 'b'},
  54. {"force", no_argument, NULL, 'f'},
  55. {"include", a_argument, NULL, 'i'},
  56. {"sort", no_argument, NULL, 's'},
  57. {"phandle", a_argument, NULL, 'H'},
  58. {"warning", a_argument, NULL, 'W'},
  59. {"error", a_argument, NULL, 'E'},
  60. {"symbols", no_argument, NULL, '@'},
  61. {"local-fixups", no_argument, NULL, 'L'},
  62. {"auto-alias", no_argument, NULL, 'A'},
  63. {"annotate", no_argument, NULL, 'T'},
  64. {"help", no_argument, NULL, 'h'},
  65. {"version", no_argument, NULL, 'v'},
  66. {NULL, no_argument, NULL, 0x0},
  67. };
  68. static const char * const usage_opts_help[] = {
  69. "\n\tQuiet: -q suppress warnings, -qq errors, -qqq all",
  70. "\n\tInput formats are:\n"
  71. "\t\tdts - device tree source text\n"
  72. "\t\tdtb - device tree blob\n"
  73. "\t\tfs - /proc/device-tree style directory",
  74. "\n\tOutput file",
  75. "\n\tOutput formats are:\n"
  76. "\t\tdts - device tree source text\n"
  77. "\t\tdtb - device tree blob\n"
  78. #ifndef NO_YAML
  79. "\t\tyaml - device tree encoded as YAML\n"
  80. #endif
  81. "\t\tasm - assembler source",
  82. "\n\tBlob version to produce, defaults to "stringify(DEFAULT_FDT_VERSION)" (for dtb and asm output)",
  83. "\n\tOutput dependency file",
  84. "\n\tMake space for <number> reserve map entries (for dtb and asm output)",
  85. "\n\tMake the blob at least <bytes> long (extra space)",
  86. "\n\tAdd padding to the blob of <bytes> long (extra space)",
  87. "\n\tMake the blob align to the <bytes> (extra space)",
  88. "\n\tSet the physical boot cpu",
  89. "\n\tTry to produce output even if the input tree has errors",
  90. "\n\tAdd a path to search for include files",
  91. "\n\tSort nodes and properties before outputting (useful for comparing trees)",
  92. "\n\tValid phandle formats are:\n"
  93. "\t\tlegacy - \"linux,phandle\" properties only\n"
  94. "\t\tepapr - \"phandle\" properties only\n"
  95. "\t\tboth - Both \"linux,phandle\" and \"phandle\" properties",
  96. "\n\tEnable/disable warnings (prefix with \"no-\")",
  97. "\n\tEnable/disable errors (prefix with \"no-\")",
  98. "\n\tEnable generation of symbols",
  99. "\n\tPossibly generates a __local_fixups__ and a __fixups__ node at the root node",
  100. "\n\tEnable auto-alias of labels",
  101. "\n\tAnnotate output .dts with input source file and line (-T -T for more details)",
  102. "\n\tPrint this help and exit",
  103. "\n\tPrint version and exit",
  104. NULL,
  105. };
  106. static const char *guess_type_by_name(const char *fname, const char *fallback)
  107. {
  108. const char *s;
  109. s = strrchr(fname, '.');
  110. if (s == NULL)
  111. return fallback;
  112. if (!strcasecmp(s, ".dts"))
  113. return "dts";
  114. if (!strcasecmp(s, ".yaml"))
  115. return "yaml";
  116. if (!strcasecmp(s, ".dtbo"))
  117. return "dtb";
  118. if (!strcasecmp(s, ".dtb"))
  119. return "dtb";
  120. return fallback;
  121. }
  122. static const char *guess_input_format(const char *fname, const char *fallback)
  123. {
  124. struct stat statbuf;
  125. fdt32_t magic;
  126. FILE *f;
  127. if (stat(fname, &statbuf) != 0)
  128. return fallback;
  129. if (S_ISDIR(statbuf.st_mode))
  130. return "fs";
  131. if (!S_ISREG(statbuf.st_mode))
  132. return fallback;
  133. f = fopen(fname, "r");
  134. if (f == NULL)
  135. return fallback;
  136. if (fread(&magic, 4, 1, f) != 1) {
  137. fclose(f);
  138. return fallback;
  139. }
  140. fclose(f);
  141. if (fdt32_to_cpu(magic) == FDT_MAGIC)
  142. return "dtb";
  143. return guess_type_by_name(fname, fallback);
  144. }
  145. int main(int argc, char *argv[])
  146. {
  147. struct dt_info *dti;
  148. const char *inform = NULL;
  149. const char *outform = NULL;
  150. const char *outname = "-";
  151. const char *depname = NULL;
  152. bool force = false, sort = false;
  153. const char *arg;
  154. int opt;
  155. FILE *outf = NULL;
  156. int outversion = DEFAULT_FDT_VERSION;
  157. long long cmdline_boot_cpuid = -1;
  158. quiet = 0;
  159. reservenum = 0;
  160. minsize = 0;
  161. padsize = 0;
  162. alignsize = 0;
  163. while ((opt = util_getopt_long()) != EOF) {
  164. switch (opt) {
  165. case 'I':
  166. inform = optarg;
  167. break;
  168. case 'O':
  169. outform = optarg;
  170. break;
  171. case 'o':
  172. outname = optarg;
  173. break;
  174. case 'V':
  175. outversion = strtol(optarg, NULL, 0);
  176. break;
  177. case 'd':
  178. depname = optarg;
  179. break;
  180. case 'R':
  181. reservenum = strtoul(optarg, NULL, 0);
  182. break;
  183. case 'S':
  184. minsize = strtol(optarg, NULL, 0);
  185. break;
  186. case 'p':
  187. padsize = strtol(optarg, NULL, 0);
  188. break;
  189. case 'a':
  190. alignsize = strtol(optarg, NULL, 0);
  191. if (!is_power_of_2(alignsize))
  192. die("Invalid argument \"%d\" to -a option\n",
  193. alignsize);
  194. break;
  195. case 'f':
  196. force = true;
  197. break;
  198. case 'q':
  199. quiet++;
  200. break;
  201. case 'b':
  202. cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
  203. break;
  204. case 'i':
  205. srcfile_add_search_path(optarg);
  206. break;
  207. case 'v':
  208. util_version();
  209. case 'H':
  210. if (streq(optarg, "legacy"))
  211. phandle_format = PHANDLE_LEGACY;
  212. else if (streq(optarg, "epapr"))
  213. phandle_format = PHANDLE_EPAPR;
  214. else if (streq(optarg, "both"))
  215. phandle_format = PHANDLE_BOTH;
  216. else
  217. die("Invalid argument \"%s\" to -H option\n",
  218. optarg);
  219. break;
  220. case 's':
  221. sort = true;
  222. break;
  223. case 'W':
  224. parse_checks_option(true, false, optarg);
  225. break;
  226. case 'E':
  227. parse_checks_option(false, true, optarg);
  228. break;
  229. case '@':
  230. generate_symbols = 1;
  231. break;
  232. case 'L':
  233. generate_fixups = 1;
  234. break;
  235. case 'A':
  236. auto_label_aliases = 1;
  237. break;
  238. case 'T':
  239. annotate++;
  240. break;
  241. case 'h':
  242. usage(NULL);
  243. default:
  244. usage("unknown option");
  245. }
  246. }
  247. if (argc > (optind+1))
  248. usage("missing files");
  249. else if (argc < (optind+1))
  250. arg = "-";
  251. else
  252. arg = argv[optind];
  253. /* minsize and padsize are mutually exclusive */
  254. if (minsize && padsize)
  255. die("Can't set both -p and -S\n");
  256. if (depname) {
  257. depfile = fopen(depname, "w");
  258. if (!depfile)
  259. die("Couldn't open dependency file %s: %s\n", depname,
  260. strerror(errno));
  261. fprint_path_escaped(depfile, outname);
  262. fputc(':', depfile);
  263. }
  264. if (inform == NULL)
  265. inform = guess_input_format(arg, "dts");
  266. if (outform == NULL) {
  267. outform = guess_type_by_name(outname, NULL);
  268. if (outform == NULL) {
  269. if (streq(inform, "dts"))
  270. outform = "dtb";
  271. else
  272. outform = "dts";
  273. }
  274. }
  275. if (annotate && (!streq(inform, "dts") || !streq(outform, "dts")))
  276. die("--annotate requires -I dts -O dts\n");
  277. if (streq(inform, "dts"))
  278. dti = dt_from_source(arg);
  279. else if (streq(inform, "fs"))
  280. dti = dt_from_fs(arg);
  281. else if(streq(inform, "dtb"))
  282. dti = dt_from_blob(arg);
  283. else
  284. die("Unknown input format \"%s\"\n", inform);
  285. dti->outname = outname;
  286. if (depfile) {
  287. fputc('\n', depfile);
  288. fclose(depfile);
  289. }
  290. if (cmdline_boot_cpuid != -1)
  291. dti->boot_cpuid_phys = cmdline_boot_cpuid;
  292. fill_fullpaths(dti->dt, "");
  293. /* on a plugin, generate by default */
  294. if (dti->dtsflags & DTSF_PLUGIN) {
  295. generate_fixups = 1;
  296. }
  297. process_checks(force, dti);
  298. if (auto_label_aliases)
  299. generate_label_tree(dti, "aliases", false);
  300. generate_labels_from_tree(dti, "__symbols__");
  301. if (generate_symbols)
  302. generate_label_tree(dti, "__symbols__", true);
  303. fixup_phandles(dti, "__fixups__");
  304. local_fixup_phandles(dti, "__local_fixups__");
  305. if (generate_fixups) {
  306. generate_fixups_tree(dti, "__fixups__");
  307. generate_local_fixups_tree(dti, "__local_fixups__");
  308. }
  309. if (sort)
  310. sort_tree(dti);
  311. if (streq(outname, "-")) {
  312. outf = stdout;
  313. } else {
  314. outf = fopen(outname, "wb");
  315. if (! outf)
  316. die("Couldn't open output file %s: %s\n",
  317. outname, strerror(errno));
  318. }
  319. if (streq(outform, "dts")) {
  320. dt_to_source(outf, dti);
  321. #ifndef NO_YAML
  322. } else if (streq(outform, "yaml")) {
  323. if (!streq(inform, "dts"))
  324. die("YAML output format requires dts input format\n");
  325. dt_to_yaml(outf, dti);
  326. #endif
  327. } else if (streq(outform, "dtb")) {
  328. dt_to_blob(outf, dti, outversion);
  329. } else if (streq(outform, "asm")) {
  330. dt_to_asm(outf, dti, outversion);
  331. } else if (streq(outform, "null")) {
  332. /* do nothing */
  333. } else {
  334. die("Unknown output format \"%s\"\n", outform);
  335. }
  336. exit(0);
  337. }