dtc.c 6.9 KB

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