dtc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. char *join_path(const char *path, const char *name)
  31. {
  32. int lenp = strlen(path);
  33. int lenn = strlen(name);
  34. int len;
  35. int needslash = 1;
  36. char *str;
  37. len = lenp + lenn + 2;
  38. if ((lenp > 0) && (path[lenp-1] == '/')) {
  39. needslash = 0;
  40. len--;
  41. }
  42. str = xmalloc(len);
  43. memcpy(str, path, lenp);
  44. if (needslash) {
  45. str[lenp] = '/';
  46. lenp++;
  47. }
  48. memcpy(str+lenp, name, lenn+1);
  49. return str;
  50. }
  51. static void fill_fullpaths(struct node *tree, const char *prefix)
  52. {
  53. struct node *child;
  54. const char *unit;
  55. tree->fullpath = join_path(prefix, tree->name);
  56. unit = strchr(tree->name, '@');
  57. if (unit)
  58. tree->basenamelen = unit - tree->name;
  59. else
  60. tree->basenamelen = strlen(tree->name);
  61. for_each_child(tree, child)
  62. fill_fullpaths(child, tree->fullpath);
  63. }
  64. static void __attribute__ ((noreturn)) usage(void)
  65. {
  66. fprintf(stderr, "Usage:\n");
  67. fprintf(stderr, "\tdtc [options] <input file>\n");
  68. fprintf(stderr, "\nOptions:\n");
  69. fprintf(stderr, "\t-h\n");
  70. fprintf(stderr, "\t\tThis help text\n");
  71. fprintf(stderr, "\t-q\n");
  72. fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n");
  73. fprintf(stderr, "\t-I <input format>\n");
  74. fprintf(stderr, "\t\tInput formats are:\n");
  75. fprintf(stderr, "\t\t\tdts - device tree source text\n");
  76. fprintf(stderr, "\t\t\tdtb - device tree blob\n");
  77. fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
  78. fprintf(stderr, "\t-o <output file>\n");
  79. fprintf(stderr, "\t-O <output format>\n");
  80. fprintf(stderr, "\t\tOutput formats are:\n");
  81. fprintf(stderr, "\t\t\tdts - device tree source text\n");
  82. fprintf(stderr, "\t\t\tdtb - device tree blob\n");
  83. fprintf(stderr, "\t\t\tasm - assembler source\n");
  84. fprintf(stderr, "\t-V <output version>\n");
  85. fprintf(stderr, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", DEFAULT_FDT_VERSION);
  86. fprintf(stderr, "\t-R <number>\n");
  87. fprintf(stderr, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n");
  88. fprintf(stderr, "\t-S <bytes>\n");
  89. fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\n");
  90. fprintf(stderr, "\t-p <bytes>\n");
  91. fprintf(stderr, "\t\tAdd padding to the blob of <bytes> long (extra space)\n");
  92. fprintf(stderr, "\t-b <number>\n");
  93. fprintf(stderr, "\t\tSet the physical boot cpu\n");
  94. fprintf(stderr, "\t-f\n");
  95. fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
  96. fprintf(stderr, "\t-v\n");
  97. fprintf(stderr, "\t\tPrint DTC version and exit\n");
  98. exit(3);
  99. }
  100. int main(int argc, char *argv[])
  101. {
  102. struct boot_info *bi;
  103. const char *inform = "dts";
  104. const char *outform = "dts";
  105. const char *outname = "-";
  106. int force = 0, check = 0;
  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 = getopt(argc, argv, "hI:O:o:V:R:S:p:fcqb:v")) != 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 'R':
  131. reservenum = strtol(optarg, NULL, 0);
  132. break;
  133. case 'S':
  134. minsize = strtol(optarg, NULL, 0);
  135. break;
  136. case 'p':
  137. padsize = strtol(optarg, NULL, 0);
  138. break;
  139. case 'f':
  140. force = 1;
  141. break;
  142. case 'c':
  143. check = 1;
  144. break;
  145. case 'q':
  146. quiet++;
  147. break;
  148. case 'b':
  149. cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
  150. break;
  151. case 'v':
  152. printf("Version: %s\n", DTC_VERSION);
  153. exit(0);
  154. case 'h':
  155. default:
  156. usage();
  157. }
  158. }
  159. if (argc > (optind+1))
  160. usage();
  161. else if (argc < (optind+1))
  162. arg = "-";
  163. else
  164. arg = argv[optind];
  165. /* minsize and padsize are mutually exclusive */
  166. if (minsize && padsize)
  167. die("Can't set both -p and -S\n");
  168. fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
  169. inform, outform, arg);
  170. if (streq(inform, "dts"))
  171. bi = dt_from_source(arg);
  172. else if (streq(inform, "fs"))
  173. bi = dt_from_fs(arg);
  174. else if(streq(inform, "dtb"))
  175. bi = dt_from_blob(arg);
  176. else
  177. die("Unknown input format \"%s\"\n", inform);
  178. if (cmdline_boot_cpuid != -1)
  179. bi->boot_cpuid_phys = cmdline_boot_cpuid;
  180. fill_fullpaths(bi->dt, "");
  181. process_checks(force, bi);
  182. if (streq(outname, "-")) {
  183. outf = stdout;
  184. } else {
  185. outf = fopen(outname, "w");
  186. if (! outf)
  187. die("Couldn't open output file %s: %s\n",
  188. outname, strerror(errno));
  189. }
  190. if (streq(outform, "dts")) {
  191. dt_to_source(outf, bi);
  192. } else if (streq(outform, "dtb")) {
  193. dt_to_blob(outf, bi, outversion);
  194. } else if (streq(outform, "asm")) {
  195. dt_to_asm(outf, bi, outversion);
  196. } else if (streq(outform, "null")) {
  197. /* do nothing */
  198. } else {
  199. die("Unknown output format \"%s\"\n", outform);
  200. }
  201. exit(0);
  202. }