dtc.c 6.5 KB

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