dtc.c 9.3 KB

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