dtc.c 7.1 KB

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