dtc.c revision 204431
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 */ 33204431Sraj 34204431Srajchar *join_path(const char *path, const char *name) 35204431Sraj{ 36204431Sraj int lenp = strlen(path); 37204431Sraj int lenn = strlen(name); 38204431Sraj int len; 39204431Sraj int needslash = 1; 40204431Sraj char *str; 41204431Sraj 42204431Sraj len = lenp + lenn + 2; 43204431Sraj if ((lenp > 0) && (path[lenp-1] == '/')) { 44204431Sraj needslash = 0; 45204431Sraj len--; 46204431Sraj } 47204431Sraj 48204431Sraj str = xmalloc(len); 49204431Sraj memcpy(str, path, lenp); 50204431Sraj if (needslash) { 51204431Sraj str[lenp] = '/'; 52204431Sraj lenp++; 53204431Sraj } 54204431Sraj memcpy(str+lenp, name, lenn+1); 55204431Sraj return str; 56204431Sraj} 57204431Sraj 58204431Srajstatic void fill_fullpaths(struct node *tree, const char *prefix) 59204431Sraj{ 60204431Sraj struct node *child; 61204431Sraj const char *unit; 62204431Sraj 63204431Sraj tree->fullpath = join_path(prefix, tree->name); 64204431Sraj 65204431Sraj unit = strchr(tree->name, '@'); 66204431Sraj if (unit) 67204431Sraj tree->basenamelen = unit - tree->name; 68204431Sraj else 69204431Sraj tree->basenamelen = strlen(tree->name); 70204431Sraj 71204431Sraj for_each_child(tree, child) 72204431Sraj fill_fullpaths(child, tree->fullpath); 73204431Sraj} 74204431Sraj 75204431Srajstatic void __attribute__ ((noreturn)) usage(void) 76204431Sraj{ 77204431Sraj fprintf(stderr, "Usage:\n"); 78204431Sraj fprintf(stderr, "\tdtc [options] <input file>\n"); 79204431Sraj fprintf(stderr, "\nOptions:\n"); 80204431Sraj fprintf(stderr, "\t-h\n"); 81204431Sraj fprintf(stderr, "\t\tThis help text\n"); 82204431Sraj fprintf(stderr, "\t-q\n"); 83204431Sraj fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n"); 84204431Sraj fprintf(stderr, "\t-I <input format>\n"); 85204431Sraj fprintf(stderr, "\t\tInput formats are:\n"); 86204431Sraj fprintf(stderr, "\t\t\tdts - device tree source text\n"); 87204431Sraj fprintf(stderr, "\t\t\tdtb - device tree blob\n"); 88204431Sraj fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n"); 89204431Sraj fprintf(stderr, "\t-o <output file>\n"); 90204431Sraj fprintf(stderr, "\t-O <output format>\n"); 91204431Sraj fprintf(stderr, "\t\tOutput formats are:\n"); 92204431Sraj fprintf(stderr, "\t\t\tdts - device tree source text\n"); 93204431Sraj fprintf(stderr, "\t\t\tdtb - device tree blob\n"); 94204431Sraj fprintf(stderr, "\t\t\tasm - assembler source\n"); 95204431Sraj fprintf(stderr, "\t-V <output version>\n"); 96204431Sraj fprintf(stderr, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", DEFAULT_FDT_VERSION); 97204431Sraj fprintf(stderr, "\t-R <number>\n"); 98204431Sraj fprintf(stderr, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n"); 99204431Sraj fprintf(stderr, "\t-S <bytes>\n"); 100204431Sraj fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\n"); 101204431Sraj fprintf(stderr, "\t-p <bytes>\n"); 102204431Sraj fprintf(stderr, "\t\tAdd padding to the blob of <bytes> long (extra space)\n"); 103204431Sraj fprintf(stderr, "\t-b <number>\n"); 104204431Sraj fprintf(stderr, "\t\tSet the physical boot cpu\n"); 105204431Sraj fprintf(stderr, "\t-f\n"); 106204431Sraj fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n"); 107204431Sraj fprintf(stderr, "\t-v\n"); 108204431Sraj fprintf(stderr, "\t\tPrint DTC version and exit\n"); 109204431Sraj exit(3); 110204431Sraj} 111204431Sraj 112204431Srajint main(int argc, char *argv[]) 113204431Sraj{ 114204431Sraj struct boot_info *bi; 115204431Sraj const char *inform = "dts"; 116204431Sraj const char *outform = "dts"; 117204431Sraj const char *outname = "-"; 118204431Sraj int force = 0, check = 0; 119204431Sraj const char *arg; 120204431Sraj int opt; 121204431Sraj FILE *outf = NULL; 122204431Sraj int outversion = DEFAULT_FDT_VERSION; 123204431Sraj long long cmdline_boot_cpuid = -1; 124204431Sraj 125204431Sraj quiet = 0; 126204431Sraj reservenum = 0; 127204431Sraj minsize = 0; 128204431Sraj padsize = 0; 129204431Sraj 130204431Sraj while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:p:fcqb:v")) != EOF) { 131204431Sraj switch (opt) { 132204431Sraj case 'I': 133204431Sraj inform = optarg; 134204431Sraj break; 135204431Sraj case 'O': 136204431Sraj outform = optarg; 137204431Sraj break; 138204431Sraj case 'o': 139204431Sraj outname = optarg; 140204431Sraj break; 141204431Sraj case 'V': 142204431Sraj outversion = strtol(optarg, NULL, 0); 143204431Sraj break; 144204431Sraj case 'R': 145204431Sraj reservenum = strtol(optarg, NULL, 0); 146204431Sraj break; 147204431Sraj case 'S': 148204431Sraj minsize = strtol(optarg, NULL, 0); 149204431Sraj break; 150204431Sraj case 'p': 151204431Sraj padsize = strtol(optarg, NULL, 0); 152204431Sraj break; 153204431Sraj case 'f': 154204431Sraj force = 1; 155204431Sraj break; 156204431Sraj case 'c': 157204431Sraj check = 1; 158204431Sraj break; 159204431Sraj case 'q': 160204431Sraj quiet++; 161204431Sraj break; 162204431Sraj case 'b': 163204431Sraj cmdline_boot_cpuid = strtoll(optarg, NULL, 0); 164204431Sraj break; 165204431Sraj case 'v': 166204431Sraj printf("Version: %s\n", DTC_VERSION); 167204431Sraj exit(0); 168204431Sraj case 'h': 169204431Sraj default: 170204431Sraj usage(); 171204431Sraj } 172204431Sraj } 173204431Sraj 174204431Sraj if (argc > (optind+1)) 175204431Sraj usage(); 176204431Sraj else if (argc < (optind+1)) 177204431Sraj arg = "-"; 178204431Sraj else 179204431Sraj arg = argv[optind]; 180204431Sraj 181204431Sraj /* minsize and padsize are mutually exclusive */ 182204431Sraj if (minsize && padsize) 183204431Sraj die("Can't set both -p and -S\n"); 184204431Sraj 185204431Sraj fprintf(stderr, "DTC: %s->%s on file \"%s\"\n", 186204431Sraj inform, outform, arg); 187204431Sraj 188204431Sraj if (streq(inform, "dts")) 189204431Sraj bi = dt_from_source(arg); 190204431Sraj else if (streq(inform, "fs")) 191204431Sraj bi = dt_from_fs(arg); 192204431Sraj else if(streq(inform, "dtb")) 193204431Sraj bi = dt_from_blob(arg); 194204431Sraj else 195204431Sraj die("Unknown input format \"%s\"\n", inform); 196204431Sraj 197204431Sraj if (cmdline_boot_cpuid != -1) 198204431Sraj bi->boot_cpuid_phys = cmdline_boot_cpuid; 199204431Sraj 200204431Sraj fill_fullpaths(bi->dt, ""); 201204431Sraj process_checks(force, bi); 202204431Sraj 203204431Sraj 204204431Sraj if (streq(outname, "-")) { 205204431Sraj outf = stdout; 206204431Sraj } else { 207204431Sraj outf = fopen(outname, "w"); 208204431Sraj if (! outf) 209204431Sraj die("Couldn't open output file %s: %s\n", 210204431Sraj outname, strerror(errno)); 211204431Sraj } 212204431Sraj 213204431Sraj if (streq(outform, "dts")) { 214204431Sraj dt_to_source(outf, bi); 215204431Sraj } else if (streq(outform, "dtb")) { 216204431Sraj dt_to_blob(outf, bi, outversion); 217204431Sraj } else if (streq(outform, "asm")) { 218204431Sraj dt_to_asm(outf, bi, outversion); 219204431Sraj } else if (streq(outform, "null")) { 220204431Sraj /* do nothing */ 221204431Sraj } else { 222204431Sraj die("Unknown output format \"%s\"\n", outform); 223204431Sraj } 224204431Sraj 225204431Sraj exit(0); 226204431Sraj} 227