-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.c
518 lines (481 loc) · 10.2 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#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_BLSIGN,
GA_BLUNSIGN,
GA_BLENC,
GA_BLDEC,
GA_BLDECRYPT,
GA_FIPIMG,
GA_EXTIMG,
};
/**
* Type of binary file (e.g. BL2, BL3)
*/
enum gi_type {
GT_INVAL,
GT_BL2,
GT_DDRFW,
GT_BL3X,
GT_BL30,
};
/**
* 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)
/**
* Maximum number of DDR Firmwares
*/
#define MAX_DDRFW 9
/**
* Parsed options for FIP image creation
*/
struct gi_fipopt {
char const *bl2;
char const *ddrfw[MAX_DDRFW];
unsigned int ddrfw_count;
char const *bl30;
char const *bl301;
char const *bl31;
char const *bl33;
char const *fout;
char const *rev;
};
#define GI_FIPOPT_INIT(go) do \
{ \
(go)->bl2 = NULL; \
memset(&(go)->ddrfw, 0, sizeof((go)->ddrfw)); \
(go)->ddrfw_count = 0; \
(go)->bl30 = NULL; \
(go)->bl301 = NULL; \
(go)->bl31 = NULL; \
(go)->bl33 = NULL; \
(go)->fout = NULL; \
(go)->rev = NULL; \
} while(0)
/**
* Parsed options for FIP image extraction
*/
struct gi_extopt {
char const *fip;
char const *dir;
};
#define GI_EXTOPT_INIT(go) do \
{ \
(go)->fip = NULL; \
(go)->dir = NULL; \
} while(0)
/**
* Parsed options
*/
struct gi_opt {
enum gi_act act;
union {
struct gi_blopt blopt;
struct gi_fipopt fipopt;
struct gi_extopt extopt;
};
};
#define GI_OPT_INIT(go) do \
{ \
bzero(go, sizeof(*go)); \
(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_EXTOPT_VALID(go) \
(((go)->act == GA_EXTIMG) && ((go)->extopt.fip != NULL) && \
((go)->extopt.dir != 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_EXTOPT_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 bl30 or bl3x or fip)\n");
ERR("\n\tbl2 and bl3x options :\n");
ERR("\t---------------------\n");
ERR("\t-e, --extract\n");
ERR("\t\textract the different binary images from <fin> boot image\n");
ERR("\t\tand store them in fout directory\n");
ERR("\t-s, --sign\n");
ERR("\t\tsign a boot image from <fin> binary image\n");
ERR("\t-u, --unsign\n");
ERR("\t\tget a boot image from <fin> signed binary image\n");
ERR("\t-c, --encrypt\n");
ERR("\t\tcreate and encrypt a bl boot image from <fin> binary image\n");
ERR("\t-d, --decrypt\n");
ERR("\t\tdecrypt a bl boot image from <fin> store it in <fout>\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--ddrfw\n");
ERR("\t\tDDR Firmware(s) file(s) 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--bl301\n");
ERR("\t\tBL301 optional 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");
ERR("\t--rev\n");
ERR("\t\tFIP format revision (v2 or v3)\n");
}
/**
* Sign 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_sign_img(struct gi_opt *gopt)
{
int ret = -1;
switch(gopt->blopt.type) {
case GT_BL2:
case GT_BL30:
ret = gi_bl2_sign_img(gopt->blopt.fin, gopt->blopt.fout);
break;
case GT_BL3X:
ret = gi_bl3_sign_img(gopt->blopt.fin, gopt->blopt.fout);
break;
default:
break;
}
return ret;
}
/**
* Extract a bootloader boot image from a signed one
*
* @param gopt: Boot image creating options
* @return: 0 on success, negative number otherwise
*/
static int gi_unsign_img(struct gi_opt *gopt)
{
int ret = -1;
switch(gopt->blopt.type) {
case GT_BL2:
case GT_BL30:
ret = gi_bl2_unsign_img(gopt->blopt.fin, gopt->blopt.fout);
break;
default:
break;
}
return ret;
}
/**
* Encrypt a bootloader boot image ready to be flashed onto a SD card
*
* @param gopt: Boot image options
* @return: 0 on success, negative number otherwise
*/
static int gi_encrypt_img(struct gi_opt *gopt)
{
int ret = -1;
switch(gopt->blopt.type) {
case GT_BL3X:
ret = gi_bl3_encrypt_img(gopt->blopt.fin, gopt->blopt.fout);
break;
default:
break;
}
return ret;
}
/**
* Decrypt a bootloader boot image
*
* @param gopt: Boot image options
* @return: 0 on success, negative number otherwise
*/
static int gi_decrypt_img(struct gi_opt *gopt)
{
int ret = -1;
switch(gopt->blopt.type) {
case GT_BL3X:
ret = gi_bl3_decrypt_img(gopt->blopt.fin, gopt->blopt.fout);
break;
default:
break;
}
return ret;
}
/**
* Extract all bootloader binaries from a boot image
*
* @param gopt: Boot image extraction options
* @return: 0 on success, negative number otherwise
*/
static int gi_extract(struct gi_opt *gopt)
{
int ret;
DBG("Extract files from FIP boot image %s\n", gopt->extopt.fip);
ret = gi_fip_extract(gopt->extopt.fip, gopt->extopt.dir);
return ret;
}
/**
* 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.ddrfw,
gopt->fipopt.ddrfw_count, gopt->fipopt.bl30,
gopt->fipopt.bl301, gopt->fipopt.bl31,
gopt->fipopt.bl33, gopt->fipopt.fout,
gopt->fipopt.rev);
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 = "sign",
.has_arg = 0,
.flag = NULL,
.val = 's',
},
{
.name = "unsign",
.has_arg = 0,
.flag = NULL,
.val = 'u',
},
{
.name = "encrypt",
.has_arg = 0,
.flag = NULL,
.val = 'c',
},
{
.name = "decrypt",
.has_arg = 0,
.flag = NULL,
.val = 'd',
},
{
.name = "extract",
.has_arg = 0,
.flag = NULL,
.val = 'e',
},
{
.name = "bl2",
.has_arg = 1,
.flag = NULL,
.val = '2',
},
{
.name = "ddrfw",
.has_arg = 1,
.flag = NULL,
.val = '6',
},
{
.name = "bl30",
.has_arg = 1,
.flag = NULL,
.val = '0',
},
{
.name = "bl301",
.has_arg = 1,
.flag = NULL,
.val = '4',
},
{
.name = "bl31",
.has_arg = 1,
.flag = NULL,
.val = '1',
},
{
.name = "bl33",
.has_arg = 1,
.flag = NULL,
.val = '3',
},
{
.name = "rev",
.has_arg = 1,
.flag = NULL,
.val = '5',
},
};
struct gi_blopt blopt;
struct gi_fipopt fipopt;
struct gi_extopt extopt;
int ret;
int idx;
GI_OPT_INIT(gopt);
GI_BLOPT_INIT(&blopt);
GI_FIPOPT_INIT(&fipopt);
GI_EXTOPT_INIT(&extopt);
while((ret = getopt_long(argc, argv, "ecdsut:", opt, &idx)) != -1) {
switch(ret) {
case 't':
if(strcmp(optarg, "bl2") == 0) {
blopt.type = GT_BL2;
} else if(strcmp(optarg, "bl3x") == 0) {
blopt.type = GT_BL3X;
} else if(strcmp(optarg, "bl30") == 0) {
blopt.type = GT_BL30;
} 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 's':
gopt->act = GA_BLSIGN;
break;
case 'u':
gopt->act = GA_BLUNSIGN;
break;
case 'c':
gopt->act = GA_BLENC;
break;
case 'd':
gopt->act = GA_BLDEC;
break;
case 'e':
gopt->act = GA_EXTIMG;
break;
case '2':
fipopt.bl2 = optarg;
break;
case '6':
fipopt.ddrfw[fipopt.ddrfw_count++] = optarg;
break;
case '0':
fipopt.bl30 = optarg;
break;
case '4':
fipopt.bl301 = optarg;
break;
case '1':
fipopt.bl31 = optarg;
break;
case '3':
fipopt.bl33 = optarg;
break;
case '5':
fipopt.rev = 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;
}
if(gopt->act == GA_EXTIMG) {
extopt.fip = argv[optind];
extopt.dir = argv[optind + 1];
memcpy(&gopt->extopt, &extopt, sizeof(extopt));
} else {
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_BLSIGN:
ret = gi_sign_img(&opt);
break;
case GA_BLUNSIGN:
ret = gi_unsign_img(&opt);
break;
case GA_BLENC:
ret = gi_encrypt_img(&opt);
break;
case GA_BLDEC:
ret = gi_decrypt_img(&opt);
break;
case GA_EXTIMG:
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;
}