mkimxboot: add an option to extract the of without processing
[maemo-rb.git] / rbutil / mkimxboot / main.c
blobed88d3e1ffe58147c6139c2fcc7356d1127ed487
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 },
42 #define NR_VARIANTS sizeof(imx_variants) / sizeof(imx_variants[0])
44 static void usage(void)
46 printf("Usage: elftosb [options | file]...\n");
47 printf("Options:\n");
48 printf(" -?/--help\tDisplay this message\n");
49 printf(" -o <file>\tSet output file\n");
50 printf(" -i <file>\tSet input file\n");
51 printf(" -b <file>\tSet boot file\n");
52 printf(" -d/--debug\tEnable debug output\n");
53 printf(" -t <type>\tSet type (dualboot, singleboot, recovery)\n");
54 printf(" -v <v>\tSet variant\n");
55 printf(" -x\t\tDump device informations\n");
56 printf(" -w\tExtract the original firmware\n");
57 printf("Supported variants: (default is standard)\n");
58 printf(" ");
59 for(size_t i = 0; i < NR_VARIANTS; i++)
61 if(i != 0)
62 printf(", ");
63 printf("%s", imx_variants[i].name);
65 printf("\n");
66 printf("By default a dualboot image is built\n");
67 exit(1);
70 int main(int argc, char *argv[])
72 char *infile = NULL;
73 char *outfile = NULL;
74 char *bootfile = NULL;
75 enum imx_firmware_variant_t variant = VARIANT_DEFAULT;
76 enum imx_output_type_t type = IMX_DUALBOOT;
77 bool debug = false;
78 bool extract_of = false;
80 if(argc == 1)
81 usage();
83 while(1)
85 static struct option long_options[] =
87 {"help", no_argument, 0, '?'},
88 {"in-file", no_argument, 0, 'i'},
89 {"out-file", required_argument, 0, 'o'},
90 {"boot-file", required_argument, 0, 'b'},
91 {"debug", no_argument, 0, 'd'},
92 {"type", required_argument, 0, 't'},
93 {"variant", required_argument, 0, 'v'},
94 {"dev-info", no_argument, 0, 'x'},
95 {0, 0, 0, 0}
98 int c = getopt_long(argc, argv, "?di:o:b:t:v:xw", long_options, NULL);
99 if(c == -1)
100 break;
101 switch(c)
103 case 'd':
104 debug = true;
105 break;
106 case '?':
107 usage();
108 break;
109 case 'o':
110 outfile = optarg;
111 break;
112 case 'i':
113 infile = optarg;
114 break;
115 case 'b':
117 bootfile = optarg;
118 break;
120 case 't':
121 if(strcmp(optarg, "dualboot") == 0)
122 type = IMX_DUALBOOT;
123 else if(strcmp(optarg, "singleboot") == 0)
124 type = IMX_SINGLEBOOT;
125 else if(strcmp(optarg, "recovery") == 0)
126 type = IMX_RECOVERY;
127 else
129 printf("Invalid boot type '%s'\n", optarg);
130 return 1;
132 break;
133 case 'v':
135 for(size_t i = 0; i < NR_VARIANTS; i++)
137 if(strcmp(optarg, imx_variants[i].name) == 0)
139 variant = imx_variants[i].variant;
140 goto Lok;
143 printf("Invalid variant '%s'\n", optarg);
144 return 1;
146 Lok:
147 break;
149 case 'x':
150 dump_imx_dev_info("");
151 printf("variant mapping:\n");
152 for(int i = 0; i < sizeof(imx_variants) / sizeof(imx_variants[0]); i++)
153 printf(" %s -> variant=%d\n", imx_variants[i].name, imx_variants[i].variant);
154 break;
155 case 'w':
156 extract_of = true;
157 break;
158 default:
159 abort();
163 if(!infile)
165 printf("You must specify an input file\n");
166 return 1;
168 if(!outfile)
170 printf("You must specify an output file\n");
171 return 1;
173 if(!bootfile && !extract_of)
175 printf("You must specify an boot file\n");
176 return 1;
178 if(optind != argc)
180 printf("Extra arguments on command line\n");
181 return 1;
184 if(extract_of)
186 enum imx_error_t err = extract_firmware(infile, variant, outfile);
187 printf("Result: %d\n", err);
188 return 0;
191 struct imx_option_t opt;
192 memset(&opt, 0, sizeof(opt));
193 opt.debug = debug;
194 opt.output = type;
195 opt.fw_variant = variant;
196 enum imx_error_t err = mkimxboot(infile, bootfile, outfile, opt);
197 printf("Result: %d\n", err);
198 return 0;