dtc.c 9.3 KB

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