dtc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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-s\n");
  78. fprintf(stderr, "\t\tSort nodes and properties before outputting (only useful for\n\t\tcomparing trees)\n");
  79. fprintf(stderr, "\t-v\n");
  80. fprintf(stderr, "\t\tPrint DTC version and exit\n");
  81. fprintf(stderr, "\t-H <phandle format>\n");
  82. fprintf(stderr, "\t\tphandle formats are:\n");
  83. fprintf(stderr, "\t\t\tlegacy - \"linux,phandle\" properties only\n");
  84. fprintf(stderr, "\t\t\tepapr - \"phandle\" properties only\n");
  85. fprintf(stderr, "\t\t\tboth - Both \"linux,phandle\" and \"phandle\" properties\n");
  86. exit(3);
  87. }
  88. int main(int argc, char *argv[])
  89. {
  90. struct boot_info *bi;
  91. const char *inform = "dts";
  92. const char *outform = "dts";
  93. const char *outname = "-";
  94. const char *depname = NULL;
  95. int force = 0, sort = 0;
  96. const char *arg;
  97. int opt;
  98. FILE *outf = NULL;
  99. int outversion = DEFAULT_FDT_VERSION;
  100. long long cmdline_boot_cpuid = -1;
  101. quiet = 0;
  102. reservenum = 0;
  103. minsize = 0;
  104. padsize = 0;
  105. while ((opt = getopt(argc, argv, "hI:O:o:V:d:R:S:p:fcqb:vH:s"))
  106. != EOF) {
  107. switch (opt) {
  108. case 'I':
  109. inform = optarg;
  110. break;
  111. case 'O':
  112. outform = optarg;
  113. break;
  114. case 'o':
  115. outname = optarg;
  116. break;
  117. case 'V':
  118. outversion = strtol(optarg, NULL, 0);
  119. break;
  120. case 'd':
  121. depname = optarg;
  122. break;
  123. case 'R':
  124. reservenum = strtol(optarg, NULL, 0);
  125. break;
  126. case 'S':
  127. minsize = strtol(optarg, NULL, 0);
  128. break;
  129. case 'p':
  130. padsize = strtol(optarg, NULL, 0);
  131. break;
  132. case 'f':
  133. force = 1;
  134. break;
  135. case 'q':
  136. quiet++;
  137. break;
  138. case 'b':
  139. cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
  140. break;
  141. case 'v':
  142. printf("Version: %s\n", DTC_VERSION);
  143. exit(0);
  144. case 'H':
  145. if (streq(optarg, "legacy"))
  146. phandle_format = PHANDLE_LEGACY;
  147. else if (streq(optarg, "epapr"))
  148. phandle_format = PHANDLE_EPAPR;
  149. else if (streq(optarg, "both"))
  150. phandle_format = PHANDLE_BOTH;
  151. else
  152. die("Invalid argument \"%s\" to -H option\n",
  153. optarg);
  154. break;
  155. case 's':
  156. sort = 1;
  157. break;
  158. case 'h':
  159. default:
  160. usage();
  161. }
  162. }
  163. if (argc > (optind+1))
  164. usage();
  165. else if (argc < (optind+1))
  166. arg = "-";
  167. else
  168. arg = argv[optind];
  169. /* minsize and padsize are mutually exclusive */
  170. if (minsize && padsize)
  171. die("Can't set both -p and -S\n");
  172. if (minsize)
  173. fprintf(stderr, "DTC: Use of \"-S\" is deprecated; it will be removed soon, use \"-p\" instead\n");
  174. fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
  175. inform, outform, arg);
  176. if (depname) {
  177. depfile = fopen(depname, "w");
  178. if (!depfile)
  179. die("Couldn't open dependency file %s: %s\n", depname,
  180. strerror(errno));
  181. fprintf(depfile, "%s:", outname);
  182. }
  183. if (streq(inform, "dts"))
  184. bi = dt_from_source(arg);
  185. else if (streq(inform, "fs"))
  186. bi = dt_from_fs(arg);
  187. else if(streq(inform, "dtb"))
  188. bi = dt_from_blob(arg);
  189. else
  190. die("Unknown input format \"%s\"\n", inform);
  191. if (depfile) {
  192. fputc('\n', depfile);
  193. fclose(depfile);
  194. }
  195. if (cmdline_boot_cpuid != -1)
  196. bi->boot_cpuid_phys = cmdline_boot_cpuid;
  197. fill_fullpaths(bi->dt, "");
  198. process_checks(force, bi);
  199. if (sort)
  200. sort_tree(bi);
  201. if (streq(outname, "-")) {
  202. outf = stdout;
  203. } else {
  204. outf = fopen(outname, "w");
  205. if (! outf)
  206. die("Couldn't open output file %s: %s\n",
  207. outname, strerror(errno));
  208. }
  209. if (streq(outform, "dts")) {
  210. dt_to_source(outf, bi);
  211. } else if (streq(outform, "dtb")) {
  212. dt_to_blob(outf, bi, outversion);
  213. } else if (streq(outform, "asm")) {
  214. dt_to_asm(outf, bi, outversion);
  215. } else if (streq(outform, "null")) {
  216. /* do nothing */
  217. } else {
  218. die("Unknown output format \"%s\"\n", outform);
  219. }
  220. exit(0);
  221. }