dtc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "dtc.h"
  21. #include "srcpos.h"
  22. /*
  23. * Command line options
  24. */
  25. int quiet; /* Level of quietness */
  26. int reservenum; /* Number of memory reservation slots */
  27. int minsize; /* Minimum blob size */
  28. int padsize; /* Additional padding to blob */
  29. int phandle_format = PHANDLE_BOTH; /* Use linux,phandle or phandle properties */
  30. static void fill_fullpaths(struct node *tree, const char *prefix)
  31. {
  32. struct node *child;
  33. const char *unit;
  34. tree->fullpath = join_path(prefix, tree->name);
  35. unit = strchr(tree->name, '@');
  36. if (unit)
  37. tree->basenamelen = unit - tree->name;
  38. else
  39. tree->basenamelen = strlen(tree->name);
  40. for_each_child(tree, child)
  41. fill_fullpaths(child, tree->fullpath);
  42. }
  43. /* Usage related data. */
  44. #define FDT_VERSION(version) _FDT_VERSION(version)
  45. #define _FDT_VERSION(version) #version
  46. static const char usage_synopsis[] = "dtc [options] <input file>";
  47. static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:fb:i:H:sW:E:hv";
  48. static struct option const usage_long_opts[] = {
  49. {"quiet", no_argument, NULL, 'q'},
  50. {"in-format", a_argument, NULL, 'I'},
  51. {"out", a_argument, NULL, 'o'},
  52. {"out-format", a_argument, NULL, 'O'},
  53. {"out-version", a_argument, NULL, 'V'},
  54. {"out-dependency", a_argument, NULL, 'd'},
  55. {"reserve", a_argument, NULL, 'R'},
  56. {"space", a_argument, NULL, 'S'},
  57. {"pad", a_argument, NULL, 'p'},
  58. {"boot-cpu", a_argument, NULL, 'b'},
  59. {"force", no_argument, NULL, 'f'},
  60. {"include", a_argument, NULL, 'i'},
  61. {"sort", no_argument, NULL, 's'},
  62. {"phandle", a_argument, NULL, 'H'},
  63. {"warning", a_argument, NULL, 'W'},
  64. {"error", a_argument, NULL, 'E'},
  65. {"help", no_argument, NULL, 'h'},
  66. {"version", no_argument, NULL, 'v'},
  67. {NULL, no_argument, NULL, 0x0},
  68. };
  69. static const char * const usage_opts_help[] = {
  70. "\n\tQuiet: -q suppress warnings, -qq errors, -qqq all",
  71. "\n\tInput formats are:\n"
  72. "\t\tdts - device tree source text\n"
  73. "\t\tdtb - device tree blob\n"
  74. "\t\tfs - /proc/device-tree style directory",
  75. "\n\tOutput file",
  76. "\n\tOutput formats are:\n"
  77. "\t\tdts - device tree source text\n"
  78. "\t\tdtb - device tree blob\n"
  79. "\t\tasm - assembler source",
  80. "\n\tBlob version to produce, defaults to "FDT_VERSION(DEFAULT_FDT_VERSION)" (for dtb and asm output)",
  81. "\n\tOutput dependency file",
  82. "\n\tMake space for <number> reserve map entries (for dtb and asm output)",
  83. "\n\tMake the blob at least <bytes> long (extra space)",
  84. "\n\tAdd padding to the blob of <bytes> long (extra space)",
  85. "\n\tSet the physical boot cpu",
  86. "\n\tTry to produce output even if the input tree has errors",
  87. "\n\tAdd a path to search for include files",
  88. "\n\tSort nodes and properties before outputting (useful for comparing trees)",
  89. "\n\tValid phandle formats are:\n"
  90. "\t\tlegacy - \"linux,phandle\" properties only\n"
  91. "\t\tepapr - \"phandle\" properties only\n"
  92. "\t\tboth - Both \"linux,phandle\" and \"phandle\" properties",
  93. "\n\tEnable/disable warnings (prefix with \"no-\")",
  94. "\n\tEnable/disable errors (prefix with \"no-\")",
  95. "\n\tPrint this help and exit",
  96. "\n\tPrint version and exit",
  97. NULL,
  98. };
  99. int main(int argc, char *argv[])
  100. {
  101. struct boot_info *bi;
  102. const char *inform = "dts";
  103. const char *outform = "dts";
  104. const char *outname = "-";
  105. const char *depname = NULL;
  106. bool force = false, sort = false;
  107. const char *arg;
  108. int opt;
  109. FILE *outf = NULL;
  110. int outversion = DEFAULT_FDT_VERSION;
  111. long long cmdline_boot_cpuid = -1;
  112. quiet = 0;
  113. reservenum = 0;
  114. minsize = 0;
  115. padsize = 0;
  116. while ((opt = util_getopt_long()) != EOF) {
  117. switch (opt) {
  118. case 'I':
  119. inform = optarg;
  120. break;
  121. case 'O':
  122. outform = optarg;
  123. break;
  124. case 'o':
  125. outname = optarg;
  126. break;
  127. case 'V':
  128. outversion = strtol(optarg, NULL, 0);
  129. break;
  130. case 'd':
  131. depname = optarg;
  132. break;
  133. case 'R':
  134. reservenum = strtol(optarg, NULL, 0);
  135. break;
  136. case 'S':
  137. minsize = strtol(optarg, NULL, 0);
  138. break;
  139. case 'p':
  140. padsize = strtol(optarg, NULL, 0);
  141. break;
  142. case 'f':
  143. force = true;
  144. break;
  145. case 'q':
  146. quiet++;
  147. break;
  148. case 'b':
  149. cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
  150. break;
  151. case 'i':
  152. srcfile_add_search_path(optarg);
  153. break;
  154. case 'v':
  155. util_version();
  156. case 'H':
  157. if (streq(optarg, "legacy"))
  158. phandle_format = PHANDLE_LEGACY;
  159. else if (streq(optarg, "epapr"))
  160. phandle_format = PHANDLE_EPAPR;
  161. else if (streq(optarg, "both"))
  162. phandle_format = PHANDLE_BOTH;
  163. else
  164. die("Invalid argument \"%s\" to -H option\n",
  165. optarg);
  166. break;
  167. case 's':
  168. sort = true;
  169. break;
  170. case 'W':
  171. parse_checks_option(true, false, optarg);
  172. break;
  173. case 'E':
  174. parse_checks_option(false, true, optarg);
  175. break;
  176. case 'h':
  177. usage(NULL);
  178. default:
  179. usage("unknown option");
  180. }
  181. }
  182. if (argc > (optind+1))
  183. usage("missing files");
  184. else if (argc < (optind+1))
  185. arg = "-";
  186. else
  187. arg = argv[optind];
  188. /* minsize and padsize are mutually exclusive */
  189. if (minsize && padsize)
  190. die("Can't set both -p and -S\n");
  191. if (depname) {
  192. depfile = fopen(depname, "w");
  193. if (!depfile)
  194. die("Couldn't open dependency file %s: %s\n", depname,
  195. strerror(errno));
  196. fprintf(depfile, "%s:", outname);
  197. }
  198. if (streq(inform, "dts"))
  199. bi = dt_from_source(arg);
  200. else if (streq(inform, "fs"))
  201. bi = dt_from_fs(arg);
  202. else if(streq(inform, "dtb"))
  203. bi = dt_from_blob(arg);
  204. else
  205. die("Unknown input format \"%s\"\n", inform);
  206. if (depfile) {
  207. fputc('\n', depfile);
  208. fclose(depfile);
  209. }
  210. if (cmdline_boot_cpuid != -1)
  211. bi->boot_cpuid_phys = cmdline_boot_cpuid;
  212. fill_fullpaths(bi->dt, "");
  213. process_checks(force, bi);
  214. if (sort)
  215. sort_tree(bi);
  216. if (streq(outname, "-")) {
  217. outf = stdout;
  218. } else {
  219. outf = fopen(outname, "wb");
  220. if (! outf)
  221. die("Couldn't open output file %s: %s\n",
  222. outname, strerror(errno));
  223. }
  224. if (streq(outform, "dts")) {
  225. dt_to_source(outf, bi);
  226. } else if (streq(outform, "dtb")) {
  227. dt_to_blob(outf, bi, outversion);
  228. } else if (streq(outform, "asm")) {
  229. dt_to_asm(outf, bi, outversion);
  230. } else if (streq(outform, "null")) {
  231. /* do nothing */
  232. } else {
  233. die("Unknown output format \"%s\"\n", outform);
  234. }
  235. exit(0);
  236. }