dtc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 accroding 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:@AThv";
  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. {"auto-alias", no_argument, NULL, 'A'},
  62. {"annotate", no_argument, NULL, 'T'},
  63. {"help", no_argument, NULL, 'h'},
  64. {"version", no_argument, NULL, 'v'},
  65. {NULL, no_argument, NULL, 0x0},
  66. };
  67. static const char * const usage_opts_help[] = {
  68. "\n\tQuiet: -q suppress warnings, -qq errors, -qqq all",
  69. "\n\tInput formats are:\n"
  70. "\t\tdts - device tree source text\n"
  71. "\t\tdtb - device tree blob\n"
  72. "\t\tfs - /proc/device-tree style directory",
  73. "\n\tOutput file",
  74. "\n\tOutput formats are:\n"
  75. "\t\tdts - device tree source text\n"
  76. "\t\tdtb - device tree blob\n"
  77. #ifndef NO_YAML
  78. "\t\tyaml - device tree encoded as YAML\n"
  79. #endif
  80. "\t\tasm - assembler source",
  81. "\n\tBlob version to produce, defaults to "stringify(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\tMake the blob align to the <bytes> (extra space)",
  87. "\n\tSet the physical boot cpu",
  88. "\n\tTry to produce output even if the input tree has errors",
  89. "\n\tAdd a path to search for include files",
  90. "\n\tSort nodes and properties before outputting (useful for comparing trees)",
  91. "\n\tValid phandle formats are:\n"
  92. "\t\tlegacy - \"linux,phandle\" properties only\n"
  93. "\t\tepapr - \"phandle\" properties only\n"
  94. "\t\tboth - Both \"linux,phandle\" and \"phandle\" properties",
  95. "\n\tEnable/disable warnings (prefix with \"no-\")",
  96. "\n\tEnable/disable errors (prefix with \"no-\")",
  97. "\n\tEnable generation of symbols",
  98. "\n\tEnable auto-alias of labels",
  99. "\n\tAnnotate output .dts with input source file and line (-T -T for more details)",
  100. "\n\tPrint this help and exit",
  101. "\n\tPrint version and exit",
  102. NULL,
  103. };
  104. static const char *guess_type_by_name(const char *fname, const char *fallback)
  105. {
  106. const char *s;
  107. s = strrchr(fname, '.');
  108. if (s == NULL)
  109. return fallback;
  110. if (!strcasecmp(s, ".dts"))
  111. return "dts";
  112. if (!strcasecmp(s, ".yaml"))
  113. return "yaml";
  114. if (!strcasecmp(s, ".dtbo"))
  115. return "dtb";
  116. if (!strcasecmp(s, ".dtb"))
  117. return "dtb";
  118. return fallback;
  119. }
  120. static const char *guess_input_format(const char *fname, const char *fallback)
  121. {
  122. struct stat statbuf;
  123. fdt32_t magic;
  124. FILE *f;
  125. if (stat(fname, &statbuf) != 0)
  126. return fallback;
  127. if (S_ISDIR(statbuf.st_mode))
  128. return "fs";
  129. if (!S_ISREG(statbuf.st_mode))
  130. return fallback;
  131. f = fopen(fname, "r");
  132. if (f == NULL)
  133. return fallback;
  134. if (fread(&magic, 4, 1, f) != 1) {
  135. fclose(f);
  136. return fallback;
  137. }
  138. fclose(f);
  139. if (fdt32_to_cpu(magic) == FDT_MAGIC)
  140. return "dtb";
  141. return guess_type_by_name(fname, fallback);
  142. }
  143. int main(int argc, char *argv[])
  144. {
  145. struct dt_info *dti;
  146. const char *inform = NULL;
  147. const char *outform = NULL;
  148. const char *outname = "-";
  149. const char *depname = NULL;
  150. bool force = false, sort = false;
  151. const char *arg;
  152. int opt;
  153. FILE *outf = NULL;
  154. int outversion = DEFAULT_FDT_VERSION;
  155. long long cmdline_boot_cpuid = -1;
  156. quiet = 0;
  157. reservenum = 0;
  158. minsize = 0;
  159. padsize = 0;
  160. alignsize = 0;
  161. while ((opt = util_getopt_long()) != EOF) {
  162. switch (opt) {
  163. case 'I':
  164. inform = optarg;
  165. break;
  166. case 'O':
  167. outform = optarg;
  168. break;
  169. case 'o':
  170. outname = optarg;
  171. break;
  172. case 'V':
  173. outversion = strtol(optarg, NULL, 0);
  174. break;
  175. case 'd':
  176. depname = optarg;
  177. break;
  178. case 'R':
  179. reservenum = strtoul(optarg, NULL, 0);
  180. break;
  181. case 'S':
  182. minsize = strtol(optarg, NULL, 0);
  183. break;
  184. case 'p':
  185. padsize = strtol(optarg, NULL, 0);
  186. break;
  187. case 'a':
  188. alignsize = strtol(optarg, NULL, 0);
  189. if (!is_power_of_2(alignsize))
  190. die("Invalid argument \"%d\" to -a option\n",
  191. alignsize);
  192. break;
  193. case 'f':
  194. force = true;
  195. break;
  196. case 'q':
  197. quiet++;
  198. break;
  199. case 'b':
  200. cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
  201. break;
  202. case 'i':
  203. srcfile_add_search_path(optarg);
  204. break;
  205. case 'v':
  206. util_version();
  207. case 'H':
  208. if (streq(optarg, "legacy"))
  209. phandle_format = PHANDLE_LEGACY;
  210. else if (streq(optarg, "epapr"))
  211. phandle_format = PHANDLE_EPAPR;
  212. else if (streq(optarg, "both"))
  213. phandle_format = PHANDLE_BOTH;
  214. else
  215. die("Invalid argument \"%s\" to -H option\n",
  216. optarg);
  217. break;
  218. case 's':
  219. sort = true;
  220. break;
  221. case 'W':
  222. parse_checks_option(true, false, optarg);
  223. break;
  224. case 'E':
  225. parse_checks_option(false, true, optarg);
  226. break;
  227. case '@':
  228. generate_symbols = 1;
  229. break;
  230. case 'A':
  231. auto_label_aliases = 1;
  232. break;
  233. case 'T':
  234. annotate++;
  235. break;
  236. case 'h':
  237. usage(NULL);
  238. default:
  239. usage("unknown option");
  240. }
  241. }
  242. if (argc > (optind+1))
  243. usage("missing files");
  244. else if (argc < (optind+1))
  245. arg = "-";
  246. else
  247. arg = argv[optind];
  248. /* minsize and padsize are mutually exclusive */
  249. if (minsize && padsize)
  250. die("Can't set both -p and -S\n");
  251. if (depname) {
  252. depfile = fopen(depname, "w");
  253. if (!depfile)
  254. die("Couldn't open dependency file %s: %s\n", depname,
  255. strerror(errno));
  256. fprintf(depfile, "%s:", outname);
  257. }
  258. if (inform == NULL)
  259. inform = guess_input_format(arg, "dts");
  260. if (outform == NULL) {
  261. outform = guess_type_by_name(outname, NULL);
  262. if (outform == NULL) {
  263. if (streq(inform, "dts"))
  264. outform = "dtb";
  265. else
  266. outform = "dts";
  267. }
  268. }
  269. if (annotate && (!streq(inform, "dts") || !streq(outform, "dts")))
  270. die("--annotate requires -I dts -O dts\n");
  271. if (streq(inform, "dts"))
  272. dti = dt_from_source(arg);
  273. else if (streq(inform, "fs"))
  274. dti = dt_from_fs(arg);
  275. else if(streq(inform, "dtb"))
  276. dti = dt_from_blob(arg);
  277. else
  278. die("Unknown input format \"%s\"\n", inform);
  279. dti->outname = outname;
  280. if (depfile) {
  281. fputc('\n', depfile);
  282. fclose(depfile);
  283. }
  284. if (cmdline_boot_cpuid != -1)
  285. dti->boot_cpuid_phys = cmdline_boot_cpuid;
  286. fill_fullpaths(dti->dt, "");
  287. /* on a plugin, generate by default */
  288. if (dti->dtsflags & DTSF_PLUGIN) {
  289. generate_fixups = 1;
  290. }
  291. process_checks(force, dti);
  292. if (auto_label_aliases)
  293. generate_label_tree(dti, "aliases", false);
  294. if (generate_symbols)
  295. generate_label_tree(dti, "__symbols__", true);
  296. if (generate_fixups) {
  297. generate_fixups_tree(dti, "__fixups__");
  298. generate_local_fixups_tree(dti, "__local_fixups__");
  299. }
  300. if (sort)
  301. sort_tree(dti);
  302. if (streq(outname, "-")) {
  303. outf = stdout;
  304. } else {
  305. outf = fopen(outname, "wb");
  306. if (! outf)
  307. die("Couldn't open output file %s: %s\n",
  308. outname, strerror(errno));
  309. }
  310. if (streq(outform, "dts")) {
  311. dt_to_source(outf, dti);
  312. #ifndef NO_YAML
  313. } else if (streq(outform, "yaml")) {
  314. if (!streq(inform, "dts"))
  315. die("YAML output format requires dts input format\n");
  316. dt_to_yaml(outf, dti);
  317. #endif
  318. } else if (streq(outform, "dtb")) {
  319. dt_to_blob(outf, dti, outversion);
  320. } else if (streq(outform, "asm")) {
  321. dt_to_asm(outf, dti, outversion);
  322. } else if (streq(outform, "null")) {
  323. /* do nothing */
  324. } else {
  325. die("Unknown output format \"%s\"\n", outform);
  326. }
  327. exit(0);
  328. }