dtc.c revision 204433
1204431Sraj/* 2204431Sraj * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. 3204431Sraj * 4204431Sraj * 5204431Sraj * This program is free software; you can redistribute it and/or 6204431Sraj * modify it under the terms of the GNU General Public License as 7204431Sraj * published by the Free Software Foundation; either version 2 of the 8204431Sraj * License, or (at your option) any later version. 9204431Sraj * 10204431Sraj * This program is distributed in the hope that it will be useful, 11204431Sraj * but WITHOUT ANY WARRANTY; without even the implied warranty of 12204431Sraj * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13204431Sraj * General Public License for more details. 14204431Sraj * 15204431Sraj * You should have received a copy of the GNU General Public License 16204431Sraj * along with this program; if not, write to the Free Software 17204431Sraj * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 18204431Sraj * USA 19204431Sraj */ 20204431Sraj 21204431Sraj#include "dtc.h" 22204431Sraj#include "srcpos.h" 23204431Sraj 24204431Sraj#include "version_gen.h" 25204431Sraj 26204431Sraj/* 27204431Sraj * Command line options 28204431Sraj */ 29204431Srajint quiet; /* Level of quietness */ 30204431Srajint reservenum; /* Number of memory reservation slots */ 31204431Srajint minsize; /* Minimum blob size */ 32204431Srajint padsize; /* Additional padding to blob */ 33204433Srajint phandle_format = PHANDLE_BOTH; /* Use linux,phandle or phandle properties */ 34204431Sraj 35204431Srajchar *join_path(const char *path, const char *name) 36204431Sraj{ 37204431Sraj int lenp = strlen(path); 38204431Sraj int lenn = strlen(name); 39204431Sraj int len; 40204431Sraj int needslash = 1; 41204431Sraj char *str; 42204431Sraj 43204431Sraj len = lenp + lenn + 2; 44204431Sraj if ((lenp > 0) && (path[lenp-1] == '/')) { 45204431Sraj needslash = 0; 46204431Sraj len--; 47204431Sraj } 48204431Sraj 49204431Sraj str = xmalloc(len); 50204431Sraj memcpy(str, path, lenp); 51204431Sraj if (needslash) { 52204431Sraj str[lenp] = '/'; 53204431Sraj lenp++; 54204431Sraj } 55204431Sraj memcpy(str+lenp, name, lenn+1); 56204431Sraj return str; 57204431Sraj} 58204431Sraj 59204431Srajstatic void fill_fullpaths(struct node *tree, const char *prefix) 60204431Sraj{ 61204431Sraj struct node *child; 62204431Sraj const char *unit; 63204431Sraj 64204431Sraj tree->fullpath = join_path(prefix, tree->name); 65204431Sraj 66204431Sraj unit = strchr(tree->name, '@'); 67204431Sraj if (unit) 68204431Sraj tree->basenamelen = unit - tree->name; 69204431Sraj else 70204431Sraj tree->basenamelen = strlen(tree->name); 71204431Sraj 72204431Sraj for_each_child(tree, child) 73204431Sraj fill_fullpaths(child, tree->fullpath); 74204431Sraj} 75204431Sraj 76204431Srajstatic void __attribute__ ((noreturn)) usage(void) 77204431Sraj{ 78204431Sraj fprintf(stderr, "Usage:\n"); 79204431Sraj fprintf(stderr, "\tdtc [options] <input file>\n"); 80204431Sraj fprintf(stderr, "\nOptions:\n"); 81204431Sraj fprintf(stderr, "\t-h\n"); 82204431Sraj fprintf(stderr, "\t\tThis help text\n"); 83204431Sraj fprintf(stderr, "\t-q\n"); 84204431Sraj fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n"); 85204431Sraj fprintf(stderr, "\t-I <input format>\n"); 86204431Sraj fprintf(stderr, "\t\tInput formats are:\n"); 87204431Sraj fprintf(stderr, "\t\t\tdts - device tree source text\n"); 88204431Sraj fprintf(stderr, "\t\t\tdtb - device tree blob\n"); 89204431Sraj fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n"); 90204431Sraj fprintf(stderr, "\t-o <output file>\n"); 91204431Sraj fprintf(stderr, "\t-O <output format>\n"); 92204431Sraj fprintf(stderr, "\t\tOutput formats are:\n"); 93204431Sraj fprintf(stderr, "\t\t\tdts - device tree source text\n"); 94204431Sraj fprintf(stderr, "\t\t\tdtb - device tree blob\n"); 95204431Sraj fprintf(stderr, "\t\t\tasm - assembler source\n"); 96204431Sraj fprintf(stderr, "\t-V <output version>\n"); 97204431Sraj fprintf(stderr, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", DEFAULT_FDT_VERSION); 98204431Sraj fprintf(stderr, "\t-R <number>\n"); 99204431Sraj fprintf(stderr, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n"); 100204431Sraj fprintf(stderr, "\t-S <bytes>\n"); 101204431Sraj fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\n"); 102204431Sraj fprintf(stderr, "\t-p <bytes>\n"); 103204431Sraj fprintf(stderr, "\t\tAdd padding to the blob of <bytes> long (extra space)\n"); 104204431Sraj fprintf(stderr, "\t-b <number>\n"); 105204431Sraj fprintf(stderr, "\t\tSet the physical boot cpu\n"); 106204431Sraj fprintf(stderr, "\t-f\n"); 107204431Sraj fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n"); 108204431Sraj fprintf(stderr, "\t-v\n"); 109204431Sraj fprintf(stderr, "\t\tPrint DTC version and exit\n"); 110204433Sraj fprintf(stderr, "\t-H <phandle format>\n"); 111204433Sraj fprintf(stderr, "\t\tphandle formats are:\n"); 112204433Sraj fprintf(stderr, "\t\t\tlegacy - \"linux,phandle\" properties only\n"); 113204433Sraj fprintf(stderr, "\t\t\tepapr - \"phandle\" properties only\n"); 114204433Sraj fprintf(stderr, "\t\t\tboth - Both \"linux,phandle\" and \"phandle\" properties\n"); 115204431Sraj exit(3); 116204431Sraj} 117204431Sraj 118204431Srajint main(int argc, char *argv[]) 119204431Sraj{ 120204431Sraj struct boot_info *bi; 121204431Sraj const char *inform = "dts"; 122204431Sraj const char *outform = "dts"; 123204431Sraj const char *outname = "-"; 124204431Sraj int force = 0, check = 0; 125204431Sraj const char *arg; 126204431Sraj int opt; 127204431Sraj FILE *outf = NULL; 128204431Sraj int outversion = DEFAULT_FDT_VERSION; 129204431Sraj long long cmdline_boot_cpuid = -1; 130204431Sraj 131204431Sraj quiet = 0; 132204431Sraj reservenum = 0; 133204431Sraj minsize = 0; 134204431Sraj padsize = 0; 135204431Sraj 136204433Sraj while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:p:fcqb:vH:")) != EOF) { 137204431Sraj switch (opt) { 138204431Sraj case 'I': 139204431Sraj inform = optarg; 140204431Sraj break; 141204431Sraj case 'O': 142204431Sraj outform = optarg; 143204431Sraj break; 144204431Sraj case 'o': 145204431Sraj outname = optarg; 146204431Sraj break; 147204431Sraj case 'V': 148204431Sraj outversion = strtol(optarg, NULL, 0); 149204431Sraj break; 150204431Sraj case 'R': 151204431Sraj reservenum = strtol(optarg, NULL, 0); 152204431Sraj break; 153204431Sraj case 'S': 154204431Sraj minsize = strtol(optarg, NULL, 0); 155204431Sraj break; 156204431Sraj case 'p': 157204431Sraj padsize = strtol(optarg, NULL, 0); 158204431Sraj break; 159204431Sraj case 'f': 160204431Sraj force = 1; 161204431Sraj break; 162204431Sraj case 'c': 163204431Sraj check = 1; 164204431Sraj break; 165204431Sraj case 'q': 166204431Sraj quiet++; 167204431Sraj break; 168204431Sraj case 'b': 169204431Sraj cmdline_boot_cpuid = strtoll(optarg, NULL, 0); 170204431Sraj break; 171204431Sraj case 'v': 172204431Sraj printf("Version: %s\n", DTC_VERSION); 173204431Sraj exit(0); 174204433Sraj case 'H': 175204433Sraj if (streq(optarg, "legacy")) 176204433Sraj phandle_format = PHANDLE_LEGACY; 177204433Sraj else if (streq(optarg, "epapr")) 178204433Sraj phandle_format = PHANDLE_EPAPR; 179204433Sraj else if (streq(optarg, "both")) 180204433Sraj phandle_format = PHANDLE_BOTH; 181204433Sraj else 182204433Sraj die("Invalid argument \"%s\" to -H option\n", 183204433Sraj optarg); 184204433Sraj break; 185204433Sraj 186204431Sraj case 'h': 187204431Sraj default: 188204431Sraj usage(); 189204431Sraj } 190204431Sraj } 191204431Sraj 192204431Sraj if (argc > (optind+1)) 193204431Sraj usage(); 194204431Sraj else if (argc < (optind+1)) 195204431Sraj arg = "-"; 196204431Sraj else 197204431Sraj arg = argv[optind]; 198204431Sraj 199204431Sraj /* minsize and padsize are mutually exclusive */ 200204431Sraj if (minsize && padsize) 201204431Sraj die("Can't set both -p and -S\n"); 202204431Sraj 203204433Sraj if (minsize) 204204433Sraj fprintf(stderr, "DTC: Use of \"-S\" is deprecated; it will be removed soon, use \"-p\" instead\n"); 205204433Sraj 206204431Sraj fprintf(stderr, "DTC: %s->%s on file \"%s\"\n", 207204431Sraj inform, outform, arg); 208204431Sraj 209204431Sraj if (streq(inform, "dts")) 210204431Sraj bi = dt_from_source(arg); 211204431Sraj else if (streq(inform, "fs")) 212204431Sraj bi = dt_from_fs(arg); 213204431Sraj else if(streq(inform, "dtb")) 214204431Sraj bi = dt_from_blob(arg); 215204431Sraj else 216204431Sraj die("Unknown input format \"%s\"\n", inform); 217204431Sraj 218204431Sraj if (cmdline_boot_cpuid != -1) 219204431Sraj bi->boot_cpuid_phys = cmdline_boot_cpuid; 220204431Sraj 221204431Sraj fill_fullpaths(bi->dt, ""); 222204431Sraj process_checks(force, bi); 223204431Sraj 224204431Sraj 225204431Sraj if (streq(outname, "-")) { 226204431Sraj outf = stdout; 227204431Sraj } else { 228204431Sraj outf = fopen(outname, "w"); 229204431Sraj if (! outf) 230204431Sraj die("Couldn't open output file %s: %s\n", 231204431Sraj outname, strerror(errno)); 232204431Sraj } 233204431Sraj 234204431Sraj if (streq(outform, "dts")) { 235204431Sraj dt_to_source(outf, bi); 236204431Sraj } else if (streq(outform, "dtb")) { 237204431Sraj dt_to_blob(outf, bi, outversion); 238204431Sraj } else if (streq(outform, "asm")) { 239204431Sraj dt_to_asm(outf, bi, outversion); 240204431Sraj } else if (streq(outform, "null")) { 241204431Sraj /* do nothing */ 242204431Sraj } else { 243204431Sraj die("Unknown output format \"%s\"\n", outform); 244204431Sraj } 245204431Sraj 246204431Sraj exit(0); 247204431Sraj} 248