dtc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. 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(2);
  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 *inf = NULL;
  110. FILE *outf = NULL;
  111. int outversion = DEFAULT_FDT_VERSION;
  112. int boot_cpuid_phys = 0xfeedbeef;
  113. quiet = 0;
  114. reservenum = 0;
  115. minsize = 0;
  116. padsize = 0;
  117. while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:p:fcqb:v")) != EOF) {
  118. switch (opt) {
  119. case 'I':
  120. inform = optarg;
  121. break;
  122. case 'O':
  123. outform = optarg;
  124. break;
  125. case 'o':
  126. outname = optarg;
  127. break;
  128. case 'V':
  129. outversion = strtol(optarg, NULL, 0);
  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 'c':
  144. check = 1;
  145. break;
  146. case 'q':
  147. quiet++;
  148. break;
  149. case 'b':
  150. boot_cpuid_phys = strtol(optarg, NULL, 0);
  151. break;
  152. case 'v':
  153. printf("Version: %s\n", DTC_VERSION);
  154. exit(0);
  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. }
  170. fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
  171. inform, outform, arg);
  172. if (streq(inform, "dts")) {
  173. bi = dt_from_source(arg);
  174. } else if (streq(inform, "fs")) {
  175. bi = dt_from_fs(arg);
  176. } else if(streq(inform, "dtb")) {
  177. inf = dtc_open_file(arg);
  178. bi = dt_from_blob(inf);
  179. } else {
  180. die("Unknown input format \"%s\"\n", inform);
  181. }
  182. if (inf && (inf != stdin))
  183. fclose(inf);
  184. if (! bi || ! bi->dt)
  185. die("Couldn't read input tree\n");
  186. process_checks(force, bi, check, outversion, boot_cpuid_phys);
  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, boot_cpuid_phys);
  199. } else if (streq(outform, "asm")) {
  200. dt_to_asm(outf, bi, outversion, boot_cpuid_phys);
  201. } else if (streq(outform, "null")) {
  202. /* do nothing */
  203. } else {
  204. die("Unknown output format \"%s\"\n", outform);
  205. }
  206. exit(0);
  207. }