dtc.c revision 204433
150477Speter/* 2139749Simp * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. 3196008Smjacob * 4167403Smjacob * 5196008Smjacob * This program is free software; you can redistribute it and/or 6167403Smjacob * modify it under the terms of the GNU General Public License as 7167403Smjacob * published by the Free Software Foundation; either version 2 of the 8167403Smjacob * License, or (at your option) any later version. 9196008Smjacob * 10167403Smjacob * This program is distributed in the hope that it will be useful, 11167403Smjacob * but WITHOUT ANY WARRANTY; without even the implied warranty of 12167403Smjacob * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13167403Smjacob * General Public License for more details. 14167403Smjacob * 15196008Smjacob * You should have received a copy of the GNU General Public License 16167403Smjacob * along with this program; if not, write to the Free Software 17167403Smjacob * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 18167403Smjacob * USA 19167403Smjacob */ 20167403Smjacob 21167403Smjacob#include "dtc.h" 22167403Smjacob#include "srcpos.h" 23167403Smjacob 24167403Smjacob#include "version_gen.h" 25167403Smjacob 26167403Smjacob/* 27196008Smjacob * Command line options 28167403Smjacob */ 29167403Smjacobint quiet; /* Level of quietness */ 3035388Smjacobint reservenum; /* Number of memory reservation slots */ 3135388Smjacobint minsize; /* Minimum blob size */ 3235388Smjacobint padsize; /* Additional padding to blob */ 3335388Smjacobint phandle_format = PHANDLE_BOTH; /* Use linux,phandle or phandle properties */ 3435388Smjacob 3535388Smjacobchar *join_path(const char *path, const char *name) 3644819Smjacob{ 37163899Smjacob int lenp = strlen(path); 3835388Smjacob int lenn = strlen(name); 3935388Smjacob int len; 4035388Smjacob int needslash = 1; 41163899Smjacob char *str; 4235388Smjacob 4335388Smjacob len = lenp + lenn + 2; 4435388Smjacob if ((lenp > 0) && (path[lenp-1] == '/')) { 45163899Smjacob needslash = 0; 4642131Smjacob len--; 4735388Smjacob } 48155704Smjacob 49163899Smjacob str = xmalloc(len); 50155704Smjacob memcpy(str, path, lenp); 5153487Smjacob if (needslash) { 5235388Smjacob str[lenp] = '/'; 53203444Smjacob lenp++; 54163899Smjacob } 5539235Sgibbs memcpy(str+lenp, name, lenn+1); 5635388Smjacob return str; 5743420Smjacob} 5835388Smjacob 59155704Smjacobstatic void fill_fullpaths(struct node *tree, const char *prefix) 6035388Smjacob{ 61196008Smjacob struct node *child; 62163899Smjacob const char *unit; 63163899Smjacob 64155704Smjacob tree->fullpath = join_path(prefix, tree->name); 65196008Smjacob 66163899Smjacob unit = strchr(tree->name, '@'); 67155704Smjacob if (unit) 68155704Smjacob tree->basenamelen = unit - tree->name; 69155704Smjacob else 70167821Smjacob tree->basenamelen = strlen(tree->name); 71155704Smjacob 72155704Smjacob for_each_child(tree, child) 7335388Smjacob fill_fullpaths(child, tree->fullpath); 7435388Smjacob} 7564087Smjacob 7664087Smjacobstatic void __attribute__ ((noreturn)) usage(void) 7764087Smjacob{ 7882689Smjacob fprintf(stderr, "Usage:\n"); 79196008Smjacob fprintf(stderr, "\tdtc [options] <input file>\n"); 80164272Smjacob fprintf(stderr, "\nOptions:\n"); 81196008Smjacob fprintf(stderr, "\t-h\n"); 8249909Smjacob fprintf(stderr, "\t\tThis help text\n"); 8361772Smjacob fprintf(stderr, "\t-q\n"); 8449909Smjacob fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n"); 8582689Smjacob fprintf(stderr, "\t-I <input format>\n"); 8664087Smjacob fprintf(stderr, "\t\tInput formats are:\n"); 8764087Smjacob fprintf(stderr, "\t\t\tdts - device tree source text\n"); 8853487Smjacob fprintf(stderr, "\t\t\tdtb - device tree blob\n"); 8982689Smjacob fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n"); 9082689Smjacob fprintf(stderr, "\t-o <output file>\n"); 9153487Smjacob fprintf(stderr, "\t-O <output format>\n"); 9253487Smjacob fprintf(stderr, "\t\tOutput formats are:\n"); 9353487Smjacob fprintf(stderr, "\t\t\tdts - device tree source text\n"); 9453487Smjacob fprintf(stderr, "\t\t\tdtb - device tree blob\n"); 9553487Smjacob fprintf(stderr, "\t\t\tasm - assembler source\n"); 9653487Smjacob fprintf(stderr, "\t-V <output version>\n"); 9753487Smjacob fprintf(stderr, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", DEFAULT_FDT_VERSION); 9853487Smjacob fprintf(stderr, "\t-R <number>\n"); 9953487Smjacob fprintf(stderr, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n"); 10053487Smjacob fprintf(stderr, "\t-S <bytes>\n"); 101196008Smjacob fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\n"); 102196008Smjacob fprintf(stderr, "\t-p <bytes>\n"); 10353487Smjacob fprintf(stderr, "\t\tAdd padding to the blob of <bytes> long (extra space)\n"); 104155704Smjacob fprintf(stderr, "\t-b <number>\n"); 105155704Smjacob fprintf(stderr, "\t\tSet the physical boot cpu\n"); 10653487Smjacob fprintf(stderr, "\t-f\n"); 10753487Smjacob fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n"); 10853487Smjacob fprintf(stderr, "\t-v\n"); 10953487Smjacob fprintf(stderr, "\t\tPrint DTC version and exit\n"); 11053487Smjacob fprintf(stderr, "\t-H <phandle format>\n"); 11153487Smjacob fprintf(stderr, "\t\tphandle formats are:\n"); 11264087Smjacob fprintf(stderr, "\t\t\tlegacy - \"linux,phandle\" properties only\n"); 11364087Smjacob fprintf(stderr, "\t\t\tepapr - \"phandle\" properties only\n"); 11453487Smjacob fprintf(stderr, "\t\t\tboth - Both \"linux,phandle\" and \"phandle\" properties\n"); 11553487Smjacob exit(3); 11653487Smjacob} 11753487Smjacob 11853487Smjacobint main(int argc, char *argv[]) 11953487Smjacob{ 12053487Smjacob struct boot_info *bi; 12164087Smjacob const char *inform = "dts"; 12264087Smjacob const char *outform = "dts"; 12364087Smjacob const char *outname = "-"; 12464087Smjacob int force = 0, check = 0; 12564087Smjacob const char *arg; 12664087Smjacob int opt; 12764087Smjacob FILE *outf = NULL; 12864087Smjacob int outversion = DEFAULT_FDT_VERSION; 12964087Smjacob long long cmdline_boot_cpuid = -1; 13064087Smjacob 13164087Smjacob quiet = 0; 132163899Smjacob reservenum = 0; 13364087Smjacob minsize = 0; 13464087Smjacob padsize = 0; 13564087Smjacob 13664087Smjacob while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:p:fcqb:vH:")) != EOF) { 13764087Smjacob switch (opt) { 13864087Smjacob case 'I': 13939235Sgibbs inform = optarg; 14064087Smjacob break; 14164087Smjacob case 'O': 14293706Smjacob outform = optarg; 14393706Smjacob break; 14493706Smjacob case 'o': 14564087Smjacob outname = optarg; 14664087Smjacob break; 14793706Smjacob case 'V': 148155704Smjacob outversion = strtol(optarg, NULL, 0); 14939235Sgibbs break; 15039235Sgibbs case 'R': 15165140Smjacob reservenum = strtol(optarg, NULL, 0); 15239235Sgibbs break; 15352347Smjacob case 'S': 15465140Smjacob minsize = strtol(optarg, NULL, 0); 15565140Smjacob break; 15653487Smjacob case 'p': 157218691Smarius padsize = strtol(optarg, NULL, 0); 158218691Smarius break; 159218691Smarius case 'f': 16087635Smjacob force = 1; 16153487Smjacob break; 162196008Smjacob case 'c': 163218691Smarius check = 1; 164196008Smjacob break; 165196008Smjacob case 'q': 166196008Smjacob quiet++; 16735388Smjacob break; 16846968Smjacob case 'b': 16935388Smjacob cmdline_boot_cpuid = strtoll(optarg, NULL, 0); 17035388Smjacob break; 171196008Smjacob case 'v': 172196008Smjacob printf("Version: %s\n", DTC_VERSION); 173196008Smjacob exit(0); 174196008Smjacob case 'H': 17552347Smjacob if (streq(optarg, "legacy")) 17652347Smjacob phandle_format = PHANDLE_LEGACY; 17735388Smjacob else if (streq(optarg, "epapr")) 17835388Smjacob phandle_format = PHANDLE_EPAPR; 17942461Smjacob else if (streq(optarg, "both")) 180163899Smjacob phandle_format = PHANDLE_BOTH; 18143420Smjacob else 18235388Smjacob die("Invalid argument \"%s\" to -H option\n", 18345040Smjacob optarg); 18465140Smjacob break; 18535388Smjacob 18652347Smjacob case 'h': 187155704Smjacob default: 188155704Smjacob usage(); 189155704Smjacob } 190155704Smjacob } 191155704Smjacob 192155704Smjacob if (argc > (optind+1)) 19335388Smjacob usage(); 194196008Smjacob else if (argc < (optind+1)) 19580582Smjacob arg = "-"; 19646968Smjacob else 19780582Smjacob arg = argv[optind]; 19843420Smjacob 19943420Smjacob /* minsize and padsize are mutually exclusive */ 20080582Smjacob if (minsize && padsize) 20180582Smjacob die("Can't set both -p and -S\n"); 20280582Smjacob 203155704Smjacob if (minsize) 204155704Smjacob fprintf(stderr, "DTC: Use of \"-S\" is deprecated; it will be removed soon, use \"-p\" instead\n"); 205155704Smjacob 206155704Smjacob fprintf(stderr, "DTC: %s->%s on file \"%s\"\n", 207155704Smjacob inform, outform, arg); 208155704Smjacob 20935388Smjacob if (streq(inform, "dts")) 21046968Smjacob bi = dt_from_source(arg); 21135388Smjacob else if (streq(inform, "fs")) 21235388Smjacob bi = dt_from_fs(arg); 21335388Smjacob else if(streq(inform, "dtb")) 21435388Smjacob bi = dt_from_blob(arg); 21539235Sgibbs else 21639235Sgibbs die("Unknown input format \"%s\"\n", inform); 21739235Sgibbs 21839235Sgibbs if (cmdline_boot_cpuid != -1) 21939235Sgibbs bi->boot_cpuid_phys = cmdline_boot_cpuid; 22039235Sgibbs 22139235Sgibbs fill_fullpaths(bi->dt, ""); 22239235Sgibbs process_checks(force, bi); 22365140Smjacob 22465140Smjacob 22565140Smjacob if (streq(outname, "-")) { 22643793Smjacob outf = stdout; 22739235Sgibbs } else { 22835388Smjacob outf = fopen(outname, "w"); 22945040Smjacob if (! outf) 23065140Smjacob die("Couldn't open output file %s: %s\n", 23165140Smjacob outname, strerror(errno)); 23265140Smjacob } 23365140Smjacob 23452347Smjacob if (streq(outform, "dts")) { 23552347Smjacob dt_to_source(outf, bi); 23652347Smjacob } else if (streq(outform, "dtb")) { 23752347Smjacob dt_to_blob(outf, bi, outversion); 23835388Smjacob } else if (streq(outform, "asm")) { 23935388Smjacob dt_to_asm(outf, bi, outversion); 24035388Smjacob } else if (streq(outform, "null")) { 24135388Smjacob /* do nothing */ 242164909Smjacob } else { 243163899Smjacob die("Unknown output format \"%s\"\n", outform); 244163899Smjacob } 245163899Smjacob 24652347Smjacob exit(0); 247196008Smjacob} 248196008Smjacob