forked from repk/gxlimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
326 lines (300 loc) · 6.43 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "gxlimg.h"
#include "bl2.h"
#include "bl3.h"
#include "fip.h"
#define _PROGNAME_DFT "gxlimg"
#define PROGNAME(argc, argv) (((argc) > 0) ? (argv)[0] : _PROGNAME_DFT)
#define USAGE(argc, argv) usage(PROGNAME(argc, argv))
/**
* Supported actions (e.g. create a boot image, ...)
*/
enum gi_act {
GA_INVAL,
GA_BLCREATE,
GA_BLEXTRACT,
GA_FIPIMG,
};
/**
* Type of binary file (e.g. BL2, BL3)
*/
enum gi_type {
GT_INVAL,
GT_BL2,
GT_BL3,
};
/**
* Parsed options for BL2/BL3 image creation
*/
struct gi_blopt {
enum gi_type type;
char const *fin;
char const *fout;
};
#define GI_BLOPT_INIT(go) do \
{ \
(go)->type = GT_INVAL; \
(go)->fin = NULL; \
(go)->fout = NULL; \
} while(0)
/**
* Parsed options for FIP image creation
*/
struct gi_fipopt {
char const *bl2;
char const *bl30;
char const *bl31;
char const *bl33;
char const *fout;
};
#define GI_FIPOPT_INIT(go) do \
{ \
(go)->bl2 = NULL; \
(go)->bl30 = NULL; \
(go)->bl31 = NULL; \
(go)->bl33 = NULL; \
(go)->fout = NULL; \
} while(0)
/**
* Parsed options
*/
struct gi_opt {
enum gi_act act;
union {
struct gi_blopt blopt;
struct gi_fipopt fipopt;
};
};
#define GI_OPT_INIT(go) do \
{ \
(go)->act = GA_INVAL; \
} while(0)
#define GI_FIPOPT_VALID(go) \
(((go)->act == GA_FIPIMG) && ((go)->fipopt.bl2 != NULL) && \
((go)->fipopt.bl30 != NULL) && ((go)->fipopt.bl31 != NULL) && \
((go)->fipopt.bl33 != NULL) && ((go)->fipopt.fout != NULL))
#define GI_BLOPT_VALID(go) \
(((go)->act != GA_INVAL) && ((go)->blopt.type != GT_INVAL) && \
((go)->blopt.fin != NULL) && ((go)->blopt.fout != NULL))
#define GI_OPT_VALID(go) (GI_FIPOPT_VALID(go) || GI_BLOPT_VALID(go))
static void usage(char const *progname)
{
ERR("Usage:\n");
ERR("\t%s [OPTION] <fin> <fout>\n", progname);
ERR("\t%s -t fip [OPTION] <fout>\n\n", progname);
ERR("\t-t, --type\n");
ERR("\t\ttype of <fin> file (bl2 or bl3 or fip)\n");
ERR("\n\tbl2 and bl3 options :\n");
ERR("\t---------------------\n");
ERR("\t-e, --extract\n");
ERR("\t\textract and decode a binary image from <fin> boot image\n");
ERR("\t-c, --create\n");
ERR("\t\tcreate and encode a boot image from <fin> binary image\n");
ERR("\n\tfip options :\n");
ERR("\t--------------\n");
ERR("\t--bl2\n");
ERR("\t\tBL2 boot file to add in final boot image\n");
ERR("\t--bl30\n");
ERR("\t\tBL30 boot file to add in final boot image\n");
ERR("\t--bl31\n");
ERR("\t\tBL31 boot file to add in final boot image\n");
ERR("\t--bl33\n");
ERR("\t\tBL31 boot file to add in final boot image\n");
}
/**
* Create a bootloader boot image ready to be flashed onto a SD card
*
* @param gopt: Boot image creating options
* @return: 0 on success, negative number otherwise
*/
static int gi_create_img(struct gi_opt *gopt)
{
int ret = -1;
switch(gopt->blopt.type) {
case GT_BL2:
ret = gi_bl2_create_img(gopt->blopt.fin, gopt->blopt.fout);
break;
case GT_BL3:
ret = gi_bl3_create_img(gopt->blopt.fin, gopt->blopt.fout);
break;
default:
break;
}
return ret;
}
/**
* Extract a bootloader code from a bootloader boot image
*
* @param gopt: Boot image extraction options
* @return: 0 on success, negative number otherwise
*/
static int gi_extract(struct gi_opt *gopt)
{
(void)gopt;
ERR("Extracting a binary bl from boot image is not implemented yet\n");
return -1;
}
/**
* Create a FIP boot final image
*
* @param gopt: Boot image creation options
* @return: 0 on success, negative number otherwise
*/
static int gi_fipimg_create(struct gi_opt *gopt)
{
int ret;
DBG("Creating final FIP boot image %s\n", gopt->fipopt.fout);
ret = gi_fip_create(gopt->fipopt.bl2, gopt->fipopt.bl30,
gopt->fipopt.bl31, gopt->fipopt.bl33,
gopt->fipopt.fout);
return ret;
}
/**
* Parse program arguments
*
* @param gopt: Filled with parsed options
* @param argc: number of argument to parse
* @param argv: Array of argument
* @return: 0 on success, negative number otherwise
*/
static int parse_args(struct gi_opt *gopt, int argc, char *argv[])
{
struct option opt[] = {
{
.name = "type",
.has_arg = 1,
.flag = NULL,
.val = 't',
},
{
.name = "create",
.has_arg = 0,
.flag = NULL,
.val = 'c',
},
{
.name = "extract",
.has_arg = 0,
.flag = NULL,
.val = 'e',
},
{
.name = "bl2",
.has_arg = 1,
.flag = NULL,
.val = '2',
},
{
.name = "bl30",
.has_arg = 1,
.flag = NULL,
.val = '0',
},
{
.name = "bl31",
.has_arg = 1,
.flag = NULL,
.val = '1',
},
{
.name = "bl33",
.has_arg = 1,
.flag = NULL,
.val = '3',
},
};
struct gi_blopt blopt;
struct gi_fipopt fipopt;
int ret;
int idx;
GI_OPT_INIT(gopt);
GI_BLOPT_INIT(&blopt);
GI_FIPOPT_INIT(&fipopt);
while((ret = getopt_long(argc, argv, "ect:", opt, &idx)) != -1) {
switch(ret) {
case 't':
if(strcmp(optarg, "bl2") == 0) {
blopt.type = GT_BL2;
} else if(strcmp(optarg, "bl3") == 0) {
blopt.type = GT_BL3;
} else if(strcmp(optarg, "fip") == 0) {
gopt->act = GA_FIPIMG;
} else {
ERR("%s: invalid <fin> type %s\n",
PROGNAME(argc, argv), optarg);
goto out;
}
break;
case 'c':
gopt->act = GA_BLCREATE;
break;
case 'e':
gopt->act = GA_BLEXTRACT;
break;
case '2':
fipopt.bl2 = optarg;
break;
case '0':
fipopt.bl30 = optarg;
break;
case '1':
fipopt.bl31 = optarg;
break;
case '3':
fipopt.bl33 = optarg;
break;
case '?':
goto out;
};
}
if(gopt->act == GA_FIPIMG) {
if(optind + 1 > argc) {
ERR("%s: <fout> is mandatory\n", PROGNAME(argc, argv));
goto out;
}
fipopt.fout = argv[optind];
memcpy(&gopt->fipopt, &fipopt, sizeof(fipopt));
} else {
if(optind + 2 > argc) {
ERR("%s: <fin> and <fout> are mandatory\n",
PROGNAME(argc, argv));
goto out;
}
blopt.fin = argv[optind];
blopt.fout = argv[optind + 1];
memcpy(&gopt->blopt, &blopt, sizeof(blopt));
}
out:
return (GI_OPT_VALID(gopt)) ? 0 : -1;
}
int main(int argc, char *argv[])
{
struct gi_opt opt;
int ret;
ret = parse_args(&opt, argc, argv);
if(ret < 0) {
USAGE(argc, argv);
goto out;
}
switch(opt.act) {
case GA_BLCREATE:
ret = gi_create_img(&opt);
break;
case GA_BLEXTRACT:
ret = gi_extract(&opt);
break;
case GA_FIPIMG:
ret = gi_fipimg_create(&opt);
break;
default:
ERR("Invalid action %d\n", opt.act);
ret = -1;
break;
}
out:
return ret;
}