dtc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. uint32_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. magic = fdt32_to_cpu(magic);
  146. if (magic == FDT_MAGIC)
  147. return "dtb";
  148. return guess_type_by_name(fname, fallback);
  149. }
  150. int main(int argc, char *argv[])
  151. {
  152. struct dt_info *dti;
  153. const char *inform = NULL;
  154. const char *outform = NULL;
  155. const char *outname = "-";
  156. const char *depname = NULL;
  157. bool force = false, sort = false;
  158. const char *arg;
  159. int opt;
  160. FILE *outf = NULL;
  161. int outversion = DEFAULT_FDT_VERSION;
  162. long long cmdline_boot_cpuid = -1;
  163. quiet = 0;
  164. reservenum = 0;
  165. minsize = 0;
  166. padsize = 0;
  167. alignsize = 0;
  168. while ((opt = util_getopt_long()) != EOF) {
  169. switch (opt) {
  170. case 'I':
  171. inform = optarg;
  172. break;
  173. case 'O':
  174. outform = optarg;
  175. break;
  176. case 'o':
  177. outname = optarg;
  178. break;
  179. case 'V':
  180. outversion = strtol(optarg, NULL, 0);
  181. break;
  182. case 'd':
  183. depname = optarg;
  184. break;
  185. case 'R':
  186. reservenum = strtol(optarg, NULL, 0);
  187. break;
  188. case 'S':
  189. minsize = strtol(optarg, NULL, 0);
  190. break;
  191. case 'p':
  192. padsize = strtol(optarg, NULL, 0);
  193. break;
  194. case 'a':
  195. alignsize = strtol(optarg, NULL, 0);
  196. if (!is_power_of_2(alignsize))
  197. die("Invalid argument \"%d\" to -a option\n",
  198. alignsize);
  199. break;
  200. case 'f':
  201. force = true;
  202. break;
  203. case 'q':
  204. quiet++;
  205. break;
  206. case 'b':
  207. cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
  208. break;
  209. case 'i':
  210. srcfile_add_search_path(optarg);
  211. break;
  212. case 'v':
  213. util_version();
  214. case 'H':
  215. if (streq(optarg, "legacy"))
  216. phandle_format = PHANDLE_LEGACY;
  217. else if (streq(optarg, "epapr"))
  218. phandle_format = PHANDLE_EPAPR;
  219. else if (streq(optarg, "both"))
  220. phandle_format = PHANDLE_BOTH;
  221. else
  222. die("Invalid argument \"%s\" to -H option\n",
  223. optarg);
  224. break;
  225. case 's':
  226. sort = true;
  227. break;
  228. case 'W':
  229. parse_checks_option(true, false, optarg);
  230. break;
  231. case 'E':
  232. parse_checks_option(false, true, optarg);
  233. break;
  234. case '@':
  235. generate_symbols = 1;
  236. break;
  237. case 'A':
  238. auto_label_aliases = 1;
  239. break;
  240. case 'h':
  241. usage(NULL);
  242. default:
  243. usage("unknown option");
  244. }
  245. }
  246. if (argc > (optind+1))
  247. usage("missing files");
  248. else if (argc < (optind+1))
  249. arg = "-";
  250. else
  251. arg = argv[optind];
  252. /* minsize and padsize are mutually exclusive */
  253. if (minsize && padsize)
  254. die("Can't set both -p and -S\n");
  255. if (depname) {
  256. depfile = fopen(depname, "w");
  257. if (!depfile)
  258. die("Couldn't open dependency file %s: %s\n", depname,
  259. strerror(errno));
  260. fprintf(depfile, "%s:", outname);
  261. }
  262. if (inform == NULL)
  263. inform = guess_input_format(arg, "dts");
  264. if (outform == NULL) {
  265. outform = guess_type_by_name(outname, NULL);
  266. if (outform == NULL) {
  267. if (streq(inform, "dts"))
  268. outform = "dtb";
  269. else
  270. outform = "dts";
  271. }
  272. }
  273. if (streq(inform, "dts"))
  274. dti = dt_from_source(arg);
  275. else if (streq(inform, "fs"))
  276. dti = dt_from_fs(arg);
  277. else if(streq(inform, "dtb"))
  278. dti = dt_from_blob(arg);
  279. else
  280. die("Unknown input format \"%s\"\n", inform);
  281. dti->outname = outname;
  282. if (depfile) {
  283. fputc('\n', depfile);
  284. fclose(depfile);
  285. }
  286. if (cmdline_boot_cpuid != -1)
  287. dti->boot_cpuid_phys = cmdline_boot_cpuid;
  288. fill_fullpaths(dti->dt, "");
  289. process_checks(force, dti);
  290. /* on a plugin, generate by default */
  291. if (dti->dtsflags & DTSF_PLUGIN) {
  292. generate_fixups = 1;
  293. }
  294. if (auto_label_aliases)
  295. generate_label_tree(dti, "aliases", false);
  296. if (generate_symbols)
  297. generate_label_tree(dti, "__symbols__", true);
  298. if (generate_fixups) {
  299. generate_fixups_tree(dti, "__fixups__");
  300. generate_local_fixups_tree(dti, "__local_fixups__");
  301. }
  302. if (sort)
  303. sort_tree(dti);
  304. if (streq(outname, "-")) {
  305. outf = stdout;
  306. } else {
  307. outf = fopen(outname, "wb");
  308. if (! outf)
  309. die("Couldn't open output file %s: %s\n",
  310. outname, strerror(errno));
  311. }
  312. if (streq(outform, "dts")) {
  313. dt_to_source(outf, dti);
  314. } else if (streq(outform, "dtb")) {
  315. dt_to_blob(outf, dti, outversion);
  316. } else if (streq(outform, "asm")) {
  317. dt_to_asm(outf, dti, outversion);
  318. } else if (streq(outform, "null")) {
  319. /* do nothing */
  320. } else {
  321. die("Unknown output format \"%s\"\n", outform);
  322. }
  323. exit(0);
  324. }