Fix Theme Editor build.
[maemo-rb.git] / rbutil / mkimxboot / main.c
blob3299d748d55103a23f5d99141e41a25eb5ec75bc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2011 by Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <getopt.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "mkimxboot.h"
28 struct imx_variant_t
30 const char *name;
31 enum imx_firmware_variant_t variant;
34 struct imx_variant_t imx_variants[] =
36 { "default", VARIANT_DEFAULT },
37 { "zenxfi2-recovery", VARIANT_ZENXFI2_RECOVERY },
38 { "zenxfi2-nand", VARIANT_ZENXFI2_NAND },
39 { "zenxfi2-sd", VARIANT_ZENXFI2_SD },
40 { "zenxfistyle-recovery", VARIANT_ZENXFISTYLE_RECOVERY },
43 #define NR_VARIANTS sizeof(imx_variants) / sizeof(imx_variants[0])
45 static void usage(void)
47 printf("Usage: elftosb [options | file]...\n");
48 printf("Options:\n");
49 printf(" -?/--help\tDisplay this message\n");
50 printf(" -o <file>\tSet output file\n");
51 printf(" -i <file>\tSet input file\n");
52 printf(" -b <file>\tSet boot file\n");
53 printf(" -d/--debug\tEnable debug output\n");
54 printf(" -t <type>\tSet type (dualboot, singleboot, recovery)\n");
55 printf(" -v <v>\tSet variant\n");
56 printf(" -x\t\tDump device informations\n");
57 printf(" -w\tExtract the original firmware\n");
58 printf(" -p <ver>\tForce product and component version\n");
59 printf("Supported variants: (default is standard)\n");
60 printf(" ");
61 for(size_t i = 0; i < NR_VARIANTS; i++)
63 if(i != 0)
64 printf(", ");
65 printf("%s", imx_variants[i].name);
67 printf("\n");
68 printf("By default a dualboot image is built\n");
69 exit(1);
72 int main(int argc, char *argv[])
74 char *infile = NULL;
75 char *outfile = NULL;
76 char *bootfile = NULL;
77 enum imx_firmware_variant_t variant = VARIANT_DEFAULT;
78 enum imx_output_type_t type = IMX_DUALBOOT;
79 bool debug = false;
80 bool extract_of = false;
81 const char *force_version = NULL;
83 if(argc == 1)
84 usage();
86 while(1)
88 static struct option long_options[] =
90 {"help", no_argument, 0, '?'},
91 {"in-file", no_argument, 0, 'i'},
92 {"out-file", required_argument, 0, 'o'},
93 {"boot-file", required_argument, 0, 'b'},
94 {"debug", no_argument, 0, 'd'},
95 {"type", required_argument, 0, 't'},
96 {"variant", required_argument, 0, 'v'},
97 {"dev-info", no_argument, 0, 'x'},
98 {0, 0, 0, 0}
101 int c = getopt_long(argc, argv, "?di:o:b:t:v:xwp:", long_options, NULL);
102 if(c == -1)
103 break;
104 switch(c)
106 case 'd':
107 debug = true;
108 break;
109 case '?':
110 usage();
111 break;
112 case 'o':
113 outfile = optarg;
114 break;
115 case 'i':
116 infile = optarg;
117 break;
118 case 'b':
120 bootfile = optarg;
121 break;
123 case 't':
124 if(strcmp(optarg, "dualboot") == 0)
125 type = IMX_DUALBOOT;
126 else if(strcmp(optarg, "singleboot") == 0)
127 type = IMX_SINGLEBOOT;
128 else if(strcmp(optarg, "recovery") == 0)
129 type = IMX_RECOVERY;
130 else
132 printf("Invalid boot type '%s'\n", optarg);
133 return 1;
135 break;
136 case 'v':
138 for(size_t i = 0; i < NR_VARIANTS; i++)
140 if(strcmp(optarg, imx_variants[i].name) == 0)
142 variant = imx_variants[i].variant;
143 goto Lok;
146 printf("Invalid variant '%s'\n", optarg);
147 return 1;
149 Lok:
150 break;
152 case 'x':
153 dump_imx_dev_info("");
154 printf("variant mapping:\n");
155 for(int i = 0; i < sizeof(imx_variants) / sizeof(imx_variants[0]); i++)
156 printf(" %s -> variant=%d\n", imx_variants[i].name, imx_variants[i].variant);
157 break;
158 case 'w':
159 extract_of = true;
160 break;
161 case 'p':
162 force_version = optarg;
163 break;
164 default:
165 abort();
169 if(!infile)
171 printf("You must specify an input file\n");
172 return 1;
174 if(!outfile)
176 printf("You must specify an output file\n");
177 return 1;
179 if(!bootfile && !extract_of)
181 printf("You must specify an boot file\n");
182 return 1;
184 if(optind != argc)
186 printf("Extra arguments on command line\n");
187 return 1;
190 if(extract_of)
192 enum imx_error_t err = extract_firmware(infile, variant, outfile);
193 printf("Result: %d\n", err);
194 return 0;
197 struct imx_option_t opt;
198 memset(&opt, 0, sizeof(opt));
199 opt.debug = debug;
200 opt.output = type;
201 opt.fw_variant = variant;
202 opt.force_version = force_version;
203 enum imx_error_t err = mkimxboot(infile, bootfile, outfile, opt);
204 printf("Result: %d\n", err);
205 return 0;