symbian-qemu-0.9.1-12/dtc-trunk/dtc.c
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     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 
       
    21 #include "dtc.h"
       
    22 #include "srcpos.h"
       
    23 #include <getopt.h>
       
    24 
       
    25 #include "version_gen.h"
       
    26 
       
    27 #ifdef _WIN32
       
    28 #include <fcntl.h>
       
    29 #include <io.h>
       
    30 #endif
       
    31 
       
    32 /*
       
    33  * Command line options
       
    34  */
       
    35 int quiet;		/* Level of quietness */
       
    36 int reservenum;		/* Number of memory reservation slots */
       
    37 int minsize;		/* Minimum blob size */
       
    38 int padsize;		/* Additional padding to blob */
       
    39 
       
    40 char *join_path(const char *path, const char *name)
       
    41 {
       
    42 	int lenp = strlen(path);
       
    43 	int lenn = strlen(name);
       
    44 	int len;
       
    45 	int needslash = 1;
       
    46 	char *str;
       
    47 
       
    48 	len = lenp + lenn + 2;
       
    49 	if ((lenp > 0) && (path[lenp-1] == '/')) {
       
    50 		needslash = 0;
       
    51 		len--;
       
    52 	}
       
    53 
       
    54 	str = xmalloc(len);
       
    55 	memcpy(str, path, lenp);
       
    56 	if (needslash) {
       
    57 		str[lenp] = '/';
       
    58 		lenp++;
       
    59 	}
       
    60 	memcpy(str+lenp, name, lenn+1);
       
    61 	return str;
       
    62 }
       
    63 
       
    64 static void fill_fullpaths(struct node *tree, const char *prefix)
       
    65 {
       
    66 	struct node *child;
       
    67 	const char *unit;
       
    68 
       
    69 	tree->fullpath = join_path(prefix, tree->name);
       
    70 
       
    71 	unit = strchr(tree->name, '@');
       
    72 	if (unit)
       
    73 		tree->basenamelen = unit - tree->name;
       
    74 	else
       
    75 		tree->basenamelen = strlen(tree->name);
       
    76 
       
    77 	for_each_child(tree, child)
       
    78 		fill_fullpaths(child, tree->fullpath);
       
    79 }
       
    80 
       
    81 static void  __attribute__ ((noreturn)) usage(int error)
       
    82 {
       
    83         FILE *f = error ? stderr : stdout;
       
    84 	fprintf(f, "Usage:\n");
       
    85 	fprintf(f, "\tdtc [options] <input file>\n");
       
    86 	fprintf(f, "\nOptions:\n");
       
    87 	fprintf(f, "\t-h\n");
       
    88 	fprintf(f, "\t\tThis help text\n");
       
    89 	fprintf(f, "\t-q\n");
       
    90 	fprintf(f, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n");
       
    91 	fprintf(f, "\t-I <input format>\n");
       
    92 	fprintf(f, "\t\tInput formats are:\n");
       
    93 	fprintf(f, "\t\t\tdts - device tree source text\n");
       
    94 	fprintf(f, "\t\t\tdtb - device tree blob\n");
       
    95 	fprintf(f, "\t\t\tfs - /proc/device-tree style directory\n");
       
    96 	fprintf(f, "\t-o <output file>\n");
       
    97 	fprintf(f, "\t-O <output format>\n");
       
    98 	fprintf(f, "\t\tOutput formats are:\n");
       
    99 	fprintf(f, "\t\t\tdts - device tree source text\n");
       
   100 	fprintf(f, "\t\t\tdtb - device tree blob\n");
       
   101 	fprintf(f, "\t\t\tasm - assembler source\n");
       
   102 	fprintf(f, "\t-V <output version>\n");
       
   103 	fprintf(f, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", DEFAULT_FDT_VERSION);
       
   104 	fprintf(f, "\t-R <number>\n");
       
   105 	fprintf(f, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n");
       
   106 	fprintf(f, "\t-S <bytes>\n");
       
   107 	fprintf(f, "\t\tMake the blob at least <bytes> long (extra space)\n");
       
   108 	fprintf(f, "\t-p <bytes>\n");
       
   109 	fprintf(f, "\t\tAdd padding to the blob of <bytes> long (extra space)\n");
       
   110 	fprintf(f, "\t-b <number>\n");
       
   111 	fprintf(f, "\t\tSet the physical boot cpu\n");
       
   112 	fprintf(f, "\t-f\n");
       
   113 	fprintf(f, "\t\tForce - try to produce output even if the input tree has errors\n");
       
   114 	fprintf(f, "\t-v\n");
       
   115 	fprintf(f, "\t\tPrint DTC version and exit\n");
       
   116 	fprintf(f, "\n");
       
   117 	fprintf(f, "Report bugs to %s\n", BUG_URL);
       
   118 	exit(error ? 3 : 0);
       
   119 }
       
   120 
       
   121 int main(int argc, char *argv[])
       
   122 {
       
   123 	struct boot_info *bi;
       
   124 	const char *inform = "dts";
       
   125 	const char *outform = "dts";
       
   126 	const char *outname = "-";
       
   127 	int force = 0, check = 0;
       
   128 	const char *arg;
       
   129 	int opt;
       
   130 	FILE *outf = NULL;
       
   131 	int outversion = DEFAULT_FDT_VERSION;
       
   132 	long long cmdline_boot_cpuid = -1;
       
   133         static const struct option longopts[] = {
       
   134             {"help", no_argument, NULL, 'h'},
       
   135             {"version", no_argument, NULL, 'v'},
       
   136             {NULL, 0, NULL, 0}
       
   137         };
       
   138 
       
   139 	quiet      = 0;
       
   140 	reservenum = 0;
       
   141 	minsize    = 0;
       
   142 	padsize    = 0;
       
   143 
       
   144 	while ((opt = getopt_long(argc, argv, "hI:O:o:V:R:S:p:fcqb:v",
       
   145                 longopts, NULL)) != EOF) {
       
   146 		switch (opt) {
       
   147 		case 'I':
       
   148 			inform = optarg;
       
   149 			break;
       
   150 		case 'O':
       
   151 			outform = optarg;
       
   152 			break;
       
   153 		case 'o':
       
   154 			outname = optarg;
       
   155 			break;
       
   156 		case 'V':
       
   157 			outversion = strtol(optarg, NULL, 0);
       
   158 			break;
       
   159 		case 'R':
       
   160 			reservenum = strtol(optarg, NULL, 0);
       
   161 			break;
       
   162 		case 'S':
       
   163 			minsize = strtol(optarg, NULL, 0);
       
   164 			break;
       
   165 		case 'p':
       
   166 			padsize = strtol(optarg, NULL, 0);
       
   167 			break;
       
   168 		case 'f':
       
   169 			force = 1;
       
   170 			break;
       
   171 		case 'c':
       
   172 			check = 1;
       
   173 			break;
       
   174 		case 'q':
       
   175 			quiet++;
       
   176 			break;
       
   177 		case 'b':
       
   178 			cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
       
   179 			break;
       
   180 		case 'v':
       
   181 			printf("Version: %s\n", DTC_VERSION);
       
   182 			exit(0);
       
   183 		case 'h':
       
   184 			usage(0);
       
   185 		default:
       
   186                         usage(1);
       
   187 		}
       
   188 	}
       
   189 
       
   190 	if (argc > (optind+1))
       
   191 		usage(1);
       
   192 	else if (argc < (optind+1))
       
   193 		arg = "-";
       
   194 	else
       
   195 		arg = argv[optind];
       
   196 
       
   197 	/* minsize and padsize are mutually exclusive */
       
   198 	if (minsize && padsize)
       
   199 		die("Can't set both -p and -S\n");
       
   200 
       
   201 	fprintf(stderr, "DTC: %s->%s  on file \"%s\"\n",
       
   202 		inform, outform, arg);
       
   203 
       
   204 	if (streq(inform, "dts"))
       
   205 		bi = dt_from_source(arg);
       
   206 	else if (streq(inform, "fs"))
       
   207 		bi = dt_from_fs(arg);
       
   208 	else if(streq(inform, "dtb"))
       
   209 		bi = dt_from_blob(arg);
       
   210 	else
       
   211 		die("Unknown input format \"%s\"\n", inform);
       
   212 
       
   213 	if (cmdline_boot_cpuid != -1)
       
   214 		bi->boot_cpuid_phys = cmdline_boot_cpuid;
       
   215 
       
   216 	fill_fullpaths(bi->dt, "");
       
   217 	process_checks(force, bi);
       
   218 
       
   219 
       
   220 	if (streq(outname, "-")) {
       
   221 		outf = stdout;
       
   222 	} else {
       
   223 		outf = fopen(outname, "w");
       
   224 		if (! outf)
       
   225 			die("Couldn't open output file %s: %s\n",
       
   226 			    outname, strerror(errno));
       
   227 	}
       
   228 
       
   229 	if (streq(outform, "dts")) {
       
   230 		dt_to_source(outf, bi);
       
   231 	} else if (streq(outform, "dtb")) {
       
   232 #ifdef _WIN32
       
   233                 _setmode(_fileno(outf), _O_BINARY);
       
   234 #endif
       
   235 		dt_to_blob(outf, bi, outversion);
       
   236 	} else if (streq(outform, "asm")) {
       
   237 		dt_to_asm(outf, bi, outversion);
       
   238 	} else if (streq(outform, "null")) {
       
   239 		/* do nothing */
       
   240 	} else {
       
   241 		die("Unknown output format \"%s\"\n", outform);
       
   242 	}
       
   243 
       
   244 	exit(0);
       
   245 }