-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathbl2.c
511 lines (445 loc) · 10.2 KB
/
bl2.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <endian.h>
#include <unistd.h>
#include <fcntl.h>
#include "gxlimg.h"
#include "bl2.h"
#include "ssl.h"
#define FOUT_MODE_DFT (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
#define htole8(val) (val)
#define le8toh(val) (val)
#define bh_wr(h, sz, off, val) \
(*(uint ## sz ## _t *)((h) + off) = htole ## sz(val))
#define bh_rd(h, sz, off) \
(le ## sz ## toh(*(uint ## sz ## _t *)((h) + off)))
#define BL2HDR_SZ 0x1000
#define BL2IV_SZ 0x10
#define BL2BLKHDR_SZ 0x40
#define BL2SIG_SZ 0x200
#define BL2KEY_SZ 0xD80
#define BL2KEYHDR_SZ 0x30
#define BL2SHA2_SZ 0x20
#define BL2HDR_MAGIC (*(uint32_t *)"@AML")
/**
* BL2 binary file context
*/
struct bl2 {
size_t payloadsz;
size_t totlen;
size_t hash_start; /* Start of Hashed payload */
size_t hash_size; /* Size of hashed payload */
uint8_t flag;
};
#define BF_RSA (1 << 0)
#define BF_IS_RSA(h) ((h)->flag & BF_RSA)
#define BF_SET_RSA(h) ((h)->flag |= BF_RSA)
/**
* Initialize bl2 context from BL2 binary file descriptor
*
* @param bl2: BL2 binary descriptor to init
* @param fd: BL2 binary file descriptor
* @return: 0 on success, negative number otherwise
*/
static int gi_bl2_init(struct bl2 *bl2, int fd)
{
off_t fsz;
fsz = lseek(fd, 0, SEEK_END);
if(fsz < 0) {
PERR("Cannot seek file: ");
return (int)fsz;
}
bl2->payloadsz = fsz;
if (fsz == 0xc000) /* GXL */
bl2->payloadsz -= BL2HDR_SZ;
bl2->totlen = bl2->payloadsz + BL2HDR_SZ;
bl2->flag = 0; /* Not RSA signature support yet */
bl2->hash_start = BL2BLKHDR_SZ + BL2SHA2_SZ;
bl2->hash_size = bl2->totlen - BL2IV_SZ - bl2->hash_start;
return 0;
}
/**
* Read a block of data from a file
*
* @param fd: File descriptor to read a block from
* @param blk: Filled with read data
* @param sz: Size of block to read from file
* @return: Negative number on error, read size otherwise. The only reason that
* return value could be different from sz on success is when EOF has been
* encountered while reading the file.
*/
static ssize_t gi_bl2_read_blk(int fd, uint8_t *blk, size_t sz)
{
size_t i;
ssize_t nr = 1;
for(i = 0; (i < sz) && (nr != 0); i += nr) {
nr = read(fd, blk + i, sz - i);
if(nr < 0)
goto out;
}
nr = i;
out:
return nr;
}
/**
* Write a block of data into a file
*
* @param fd: File descriptor to write a block into
* @param blk: Actual block data
* @param sz: Size of block to write into file
* @return: Negative number on error, sz otherwise.
*/
static ssize_t gi_bl2_write_blk(int fd, uint8_t *blk, size_t sz)
{
size_t i;
ssize_t nr;
for(i = 0; i < sz; i += nr) {
nr = write(fd, blk + i, sz - i);
if(nr < 0)
goto out;
}
nr = i;
out:
return nr;
}
static int gi_bl2_dump_hdr(struct bl2 const *bl2, int fd)
{
uint8_t hdr[BL2BLKHDR_SZ] = {};
uint8_t rd[BL2IV_SZ];
ssize_t nr;
off_t off;
int ret;
if(BF_IS_RSA(bl2)) {
ERR("BL2 RSA signature not supported yet\n");
ret = -EINVAL;
goto out;
}
ret = gi_random(rd, BL2IV_SZ);
if(ret < 0)
goto out;
off = lseek(fd, 0, SEEK_SET);
if(off < 0) {
PERR("Cannot seek file: ");
return (int)off;
}
bh_wr(hdr, 32, 0x00, BL2HDR_MAGIC);
bh_wr(hdr, 32, 0x04, bl2->totlen - BL2IV_SZ);
bh_wr(hdr, 8, 0x08, BL2BLKHDR_SZ);
bh_wr(hdr, 8, 0x0a, 1);
bh_wr(hdr, 8, 0x0b, 1);
bh_wr(hdr, 32, 0x10, 0); /* SHA256 signature, no RSA */
bh_wr(hdr, 32, 0x14, BL2BLKHDR_SZ);
bh_wr(hdr, 32, 0x18, BL2SIG_SZ);
bh_wr(hdr, 32, 0x1c, bl2->hash_start); /* Beginning of hashed payload */
bh_wr(hdr, 32, 0x20, 0); /* Null RSA KEY type */
bh_wr(hdr, 32, 0x24, BL2BLKHDR_SZ + BL2SIG_SZ); /* RSA KEY Offset */
bh_wr(hdr, 32, 0x28, BL2HDR_SZ - BL2IV_SZ - BL2BLKHDR_SZ - BL2SIG_SZ);
bh_wr(hdr, 32, 0x2c, bl2->hash_size);
bh_wr(hdr, 32, 0x34, BL2HDR_SZ - BL2IV_SZ); /* Payload offset */
bh_wr(hdr, 32, 0x38, bl2->totlen - BL2HDR_SZ);
nr = gi_bl2_write_blk(fd, rd, sizeof(rd));
if(nr != sizeof(rd)) {
PERR("Failed to write random number in bl2 boot img: ");
ret = (int)nr;
goto out;
}
nr = gi_bl2_write_blk(fd, hdr, sizeof(hdr));
if(nr != sizeof(hdr)) {
PERR("Failed to write header in bl2 boot img: ");
ret = (int)nr;
goto out;
}
ret = 0;
out:
return ret;
}
/**
* Read BL2 header from image
*
* @param bl2: Filled up with BL2 header info
* @param fd: File to read header from
*
* @return: 0 on success, negative number otherwise
*/
static int gi_bl2_read_hdr(struct bl2 *bl2, int fd)
{
uint8_t hdr[BL2BLKHDR_SZ];
off_t off;
int ret;
off = lseek(fd, BL2IV_SZ, SEEK_SET); /* Skip IV */
if(off < 0) {
PERR("Cannot seek file: ");
return (int)off;
}
ret = gi_bl2_read_blk(fd, hdr, sizeof(hdr));
if(ret < 0) {
ERR("Cannot get header from bl2 file\n");
goto out;
}
ret = -EINVAL;
if(bh_rd(hdr, 32, 0x00) != BL2HDR_MAGIC) {
ERR("Invalid BL2 header\n");
goto out;
}
bl2->totlen = bh_rd(hdr, 32, 0x04) + BL2IV_SZ;
bl2->hash_start = bh_rd(hdr, 32, 0x1c); /* Beginning of hashed payload */
bl2->hash_size = bh_rd(hdr, 32, 0x2c);
bl2->payloadsz = bl2->totlen - BL2HDR_SZ;
ret = 0;
out:
return ret;
}
static int gi_bl2_dump_key(struct bl2 const *bl2, int fd)
{
uint32_t val;
off_t off;
int ret;
if(BF_IS_RSA(bl2)) {
ERR("BL2 RSA signature not supported yet\n");
return -EINVAL;
}
off = lseek(fd, BL2IV_SZ + BL2BLKHDR_SZ + BL2SIG_SZ + 0x18, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
val = htole32(0x298);
gi_bl2_write_blk(fd, (uint8_t *)(&val), 4);
/* TODO What is this offset */
off = lseek(fd, BL2IV_SZ + 0x8ec, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
val = htole32(0x240);
gi_bl2_write_blk(fd, (uint8_t *)(&val), 4);
/* TODO What is this offset */
off = lseek(fd, BL2IV_SZ + 0xb20, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
val = htole32(0x298);
gi_bl2_write_blk(fd, (uint8_t *)(&val), 4);
ret = 0;
out:
return ret;
}
static int gi_bl2_dump_binary(struct bl2 const *bl2, int fout, int fin)
{
uint8_t block[1024];
size_t nr;
ssize_t rd, wr;
off_t off;
int ret;
off = lseek(fin, 0, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
off = lseek(fout, BL2HDR_SZ, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
for(nr = 0; nr < bl2->payloadsz; nr += rd) {
rd = gi_bl2_read_blk(fin, block, sizeof(block));
if(rd <= 0) {
ret = (int)rd;
goto out;
}
wr = gi_bl2_write_blk(fout, block, sizeof(block));
if(wr != rd) {
ret = (int)wr;
goto out;
}
}
ret = 0;
out:
return ret;
}
static int gi_bl2_sign(struct bl2 const *bl2, int fd)
{
EVP_MD_CTX *ctx;
uint8_t tmp[1024];
uint8_t hash[BL2SHA2_SZ];
size_t i;
ssize_t nr;
off_t off;
int ret;
ctx = EVP_MD_CTX_new();
if(ctx == NULL) {
SSLERR(ret, "Cannot create digest context: ");
goto out;
}
ret = EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
if(ret != 1) {
SSLERR(ret, "Cannot init digest context: ");
goto out;
}
/* Hash header */
off = lseek(fd, BL2IV_SZ, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_bl2_read_blk(fd, tmp, BL2BLKHDR_SZ);
if((nr < 0) || (nr != BL2BLKHDR_SZ)) {
PERR("Cannot read header from fd %d: ", fd);
ret = (int)nr;
goto out;
}
ret = EVP_DigestUpdate(ctx, tmp, nr);
if(ret != 1) {
SSLERR(ret, "Cannot hash header block: ");
goto out;
}
/* Hash payload */
off = lseek(fd, BL2IV_SZ + bl2->hash_start, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
for(i = 0; i < bl2->hash_size; i += nr) {
nr = gi_bl2_read_blk(fd, tmp, sizeof(tmp));
if(nr < 0) {
PERR("Cannot read fd %d:", fd);
ret = (int)nr;
goto out;
}
ret = EVP_DigestUpdate(ctx, tmp, nr);
if(ret != 1) {
SSLERR(ret, "Cannot hash data block: ");
goto out;
}
}
ret = EVP_DigestFinal_ex(ctx, hash, NULL);
if(ret != 1) {
SSLERR(ret, "Cannot finalize hash: ");
goto out;
}
/* Only SHA256 signature is supported so far */
off = lseek(fd, BL2IV_SZ + BL2BLKHDR_SZ, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_bl2_write_blk(fd, hash, BL2SHA2_SZ);
if(nr != BL2SHA2_SZ) {
PERR("Cannot write SHA sig in fd %d:", fd);
ret = (int)nr;
goto out;
}
ret = 0;
out:
EVP_MD_CTX_free(ctx);
return ret;
}
int gi_bl2_sign_img(char const *fin, char const *fout)
{
struct bl2 bl2;
int fdin = -1, fdout = -1, ret;
DBG("Create bl2 boot image from %s in %s\n", fin, fout);
fdin = open(fin, O_RDONLY);
if(fdin < 0) {
PERR("Cannot open file %s", fin);
ret = -errno;
goto out;
}
fdout = open(fout, O_RDWR | O_CREAT, FOUT_MODE_DFT);
if(fdout < 0) {
PERR("Cannot open file %s", fout);
ret = -errno;
goto out;
}
ret = ftruncate(fdout, 0);
if(ret < 0)
goto out;
ret = gi_bl2_init(&bl2, fdin);
if(ret < 0)
goto out;
/* Fill the whole file with zeros */
ret = ftruncate(fdout, bl2.totlen);
if(ret < 0)
goto out;
ret = gi_bl2_dump_hdr(&bl2, fdout);
if(ret < 0)
goto out;
ret = gi_bl2_dump_key(&bl2, fdout);
if(ret < 0)
goto out;
ret = gi_bl2_dump_binary(&bl2, fdout, fdin);
if(ret < 0)
goto out;
ret = gi_bl2_sign(&bl2, fdout);
out:
if(fdin >= 0)
close(fdin);
if(fdout >= 0)
close(fdout);
return ret;
}
int gi_bl2_unsign_img(char const *fin, char const *fout)
{
struct bl2 bl2 = {};
ssize_t rd, wr;
size_t len;
off_t off;
int fdin = -1, fdout = -1, ret;
uint8_t block[1024];
DBG("Extract bl2 boot image from signed %s in %s\n", fin, fout);
fdin = open(fin, O_RDONLY);
if(fdin < 0) {
PERR("Cannot open file %s", fin);
ret = -errno;
goto out;
}
fdout = open(fout, O_RDWR | O_CREAT, FOUT_MODE_DFT);
if(fdout < 0) {
PERR("Cannot open file %s", fout);
ret = -errno;
goto out;
}
ret = gi_bl2_read_hdr(&bl2, fdin);
if(ret != 0) {
ERR("Cannot get BL2 header\n");
goto out;
}
ret = ftruncate(fdout, 0);
if(ret < 0)
goto out;
off = lseek(fdin, BL2HDR_SZ, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
len = bl2.payloadsz;
if (len + BL2HDR_SZ == 0xc000) /* GXL */
len = 0xc000;
ret = ftruncate(fdout, len);
if(ret < 0)
goto out;
do {
rd = gi_bl2_read_blk(fdin, block, MIN(len, sizeof(block)));
if(rd <= 0) {
ret = (int)rd;
goto out;
}
wr = gi_bl2_write_blk(fdout, block, rd);
if(wr != rd) {
ret = (int)wr;
goto out;
}
len -= rd;
} while((rd != 0) && (len != 0));
ret = 0;
out:
if(fdin >= 0)
close(fdin);
if(fdout >= 0)
close(fdout);
return ret;
}