forked from repk/gxlimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfip.c
536 lines (487 loc) · 10.2 KB
/
fip.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#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 <sys/stat.h>
#include "gxlimg.h"
#include "fip.h"
#include "amlcblk.h"
#define FOUT_MODE_DFT (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
#define BL31_ENTRY_MAGIC (0x87654321)
#define BL31_MAGIC (0x12348765)
#define BL2SZ (0xc000)
/**
* 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_fip_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_fip_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;
}
/**
* Safely create a temporary file
*
* @param path: Path of temporary file, should ends with 6 X. On success those
* X will be replaced with the unique file suffix created.
* @return: fd on success, negative number otherwise
*/
static int gi_fip_create_tmp(char *path)
{
mode_t oldmask;
int fd;
oldmask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
fd = mkstemp(path);
(void)umask(oldmask);
return fd;
}
/**
* List of supported boot image
*/
enum FIP_BOOT_IMG {
FBI_BL2,
FBI_BL30,
FBI_BL31,
FBI_BL32,
FBI_BL33,
};
typedef uint8_t uuid_t[16];
/**
* Default uuid for each boot image
*/
static uuid_t const uuid_list[] = {
[FBI_BL2] = {
0x5f, 0xf9, 0xec, 0x0b, 0x4d, 0x22, 0x3e, 0x4d,
0xa5, 0x44, 0xc3, 0x9d, 0x81, 0xc7, 0x3f, 0x0a
},
[FBI_BL30] = {
0x97, 0x66, 0xfd, 0x3d, 0x89, 0xbe, 0xe8, 0x49,
0xae, 0x5d, 0x78, 0xa1, 0x40, 0x60, 0x82, 0x13
},
[FBI_BL31] = {
0x47, 0xd4, 0x08, 0x6d, 0x4c, 0xfe, 0x98, 0x46,
0x9b, 0x95, 0x29, 0x50, 0xcb, 0xbd, 0x5a, 0x00
},
[FBI_BL32] = {
0x05, 0xd0, 0xe1, 0x89, 0x53, 0xdc, 0x13, 0x47,
0x8d, 0x2b, 0x50, 0x0a, 0x4b, 0x7a, 0x3e, 0x38
},
[FBI_BL33] = {
0xd6, 0xd0, 0xee, 0xa7, 0xfc, 0xea, 0xd5, 0x4b,
0x97, 0x82, 0x99, 0x34, 0xf2, 0x34, 0xb6, 0xe4
},
};
/**
* FIP header structure
*/
#pragma pack(push, 1)
struct fip_toc_header {
/**
* FIP magic
*/
uint32_t name;
/**
* Vendor specific number
*/
uint32_t serial_number;
/**
* Flags, reserved for later use
*/
uint64_t flags;
};
#pragma pack(pop)
#define FT_NAME 0xaa640001
#define FT_SERIAL 0x12345678
#define FIP_TOC_HEADER \
&((struct fip_toc_header){ \
.name = htole32(FT_NAME), \
.serial_number = htole32(FT_SERIAL) \
})
/**
* Header for a FIP table of content entry
*/
#pragma pack(push, 1)
struct fip_toc_entry {
/**
* uuid of the image entry
*/
uuid_t uuid;
/**
* Offset of the image from the FIP base address
*/
uint64_t offset;
/**
* Size of the FIP entry image
*/
uint64_t size;
/**
* Flags for the FIP entry image
*/
uint64_t flags;
};
#pragma pack(pop)
#define FTE_BL31HDR_SZ 0x50
#define FTE_BL31HDR_OFF(nr) (0x430 + FTE_BL31HDR_SZ * (nr))
/* Get fip entry offset from fip base */
#define FTE_OFF(nr) \
(sizeof(struct fip_toc_header) + nr * sizeof(struct fip_toc_entry))
#define FIP_SZ 0x4000
#define FIP_TEMPPATH "/tmp/fip.bin.XXXXXX"
#define FIP_TEMPPATHSZ sizeof(FIP_TEMPPATH)
/**
* FIP handler
*/
struct fip {
/**
* Current image copied data size
*/
size_t cursz;
/**
* Number of entry in FIP table of content
*/
size_t nrentries;
/**
* Temporary Fip file descriptor
*/
int fd;
/**
* Temporary file path
*/
char path[FIP_TEMPPATHSZ];
};
/**
* Init a FIP handler
*
* @param fip: FIP handler to init
* @return: 0 on success, negative number otherwise
*/
static inline int fip_init(struct fip *fip)
{
unsigned long long init = 0xffffffffffffffffULL;
ssize_t nr;
size_t i;
off_t off;
int ret;
strncpy(fip->path, FIP_TEMPPATH, sizeof(fip->path));
fip->cursz = FIP_SZ;
fip->nrentries = 0;
fip->fd = gi_fip_create_tmp(fip->path);
if(fip->fd < 0) {
PERR("Cannot create fip temp: ");
ret = -errno;
goto out;
}
ret = ftruncate(fip->fd, FIP_SZ - 0x200);
if(ret < 0) {
PERR("Cannot truncate fip toc header: ");
close(fip->fd);
ret = -errno;
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)FIP_TOC_HEADER,
sizeof(*FIP_TOC_HEADER));
if(nr < 0) {
PERR("Cannot write fip toc header: ");
close(fip->fd);
ret = -errno;
goto out;
}
/* End of toc entry */
off = lseek(fip->fd, 0xc00, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
for(i = 0; i < 0x80 / sizeof(init); ++i) {
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&init, sizeof(init));
if(nr < 0) {
PERR("Cannot write fip toc last entry: ");
close(fip->fd);
ret = -errno;
goto out;
}
}
ret = 0;
out:
return ret;
}
/**
* Cleanup a FIP handler
*
* @param fip: FIP handler to clean
*/
static inline void fip_cleanup(struct fip *fip)
{
close(fip->fd);
(void)remove(fip->path);
}
/**
* Copy a file as-is in another file at specific offset
*
* @param fdin: Src file to copy
* @param fdout: Dest file to copy into
* @param off: Offset at which src file should be copy into dest file
* @return: 0 on success, negative number otherwise
*/
static int gi_fip_dump_img(int fdin, int fdout, size_t off)
{
ssize_t nrd, nwr;
off_t o;
int ret;
uint8_t block[512];
o = lseek(fdout, off, SEEK_SET);
if(o < 0) {
SEEK_ERR(o, ret);
goto out;
}
do {
nrd = gi_fip_read_blk(fdin, block, sizeof(block));
if(nrd < 0)
continue;
nwr = gi_fip_write_blk(fdout, block, nrd);
if(nwr < 0) {
PERR("Cannot write to file\n");
ret = -errno;
goto out;
}
} while(nrd > 0);
if(nrd < 0) {
PERR("Cannot read file\n");
ret = -errno;
goto out;
}
ret = 0;
out:
return ret;
}
/**
* Add a bootloder image in boot image
*
* @param fip: Fip handler
* @param fdout: Final boot image file
* @param fdin: Bootloader image to add
* @param type: Type of bootloader image
*/
static int gi_fip_add(struct fip *fip, int fdout, int fdin,
enum FIP_BOOT_IMG type)
{
static uint32_t const bl31magic[] = {
BL31_ENTRY_MAGIC,
0x1,
};
struct fip_toc_entry entry;
size_t sz;
ssize_t nr;
off_t off;
int ret;
uint8_t buf[FTE_BL31HDR_SZ];
off = lseek(fdin, 0, SEEK_END);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
sz = (size_t)off;
memcpy(entry.uuid, uuid_list[type], sizeof(entry.uuid));
entry.offset = fip->cursz;
entry.flags = 0;
entry.size = sz;
off = lseek(fip->fd, FTE_OFF(fip->nrentries), SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&entry, sizeof(entry));
if(nr < 0) {
PERR("Cannot write FIP entry\n");
ret = -errno;
goto out;
}
off = lseek(fdin, 256, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_read_blk(fdin, buf, sizeof(buf));
if(nr <= 0) {
PERR("Cannot read BL image entry\n");
ret = -errno;
goto out;
}
if(le32toh(*(uint32_t *)buf) == BL31_MAGIC) {
off = lseek(fip->fd, 1024, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&bl31magic,
sizeof(bl31magic));
if(nr < 0) {
PERR("Cannot write BL31 entry header\n");
ret = -errno;
goto out;
}
off = lseek(fip->fd, FTE_BL31HDR_OFF(fip->nrentries),
SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, buf, sizeof(buf));
if(nr < 0) {
PERR("Cannot write BL31 entry header data\n");
ret = -errno;
goto out;
}
}
off = lseek(fdin, 0, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
gi_fip_dump_img(fdin, fdout, BL2SZ + entry.offset);
fip->cursz += ROUNDUP(sz, 0x4000);
++fip->nrentries;
ret = 0;
out:
return ret;
}
/**
* Create a Amlogic bootable image
*
* @param bl2: BL2 boot image to add
* @param bl30: BL30 boot image to add
* @param bl31: BL31 boot image to add
* @param bl33: BL33 boot image to add
* @return: 0 on success, negative number otherwise
*/
int gi_fip_create(char const *bl2, char const *bl30, char const *bl31,
char const *bl33, char const *fout)
{
struct fip fip;
struct amlcblk acb;
struct {
char const *path;
enum FIP_BOOT_IMG type;
} fip_bin_path[] = {
{
.path = bl30,
.type = FBI_BL30,
},
{
.path = bl31,
.type = FBI_BL31,
},
{
.path = bl33,
.type = FBI_BL33,
},
};
size_t i;
off_t off;
int fdin = -1, fdout = -1, tmpfd = -1, ret;
char fippath[] = "/tmp/fip.enc.XXXXXX";
DBG("Create FIP final image in %s\n", fout);
ret = fip_init(&fip);
if(ret < 0)
goto exit;
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;
fdin = open(bl2, O_RDONLY);
if(fdin < 0) {
PERR("Cannot open bl2 %s", bl2);
ret = -errno;
goto out;
}
ret = gi_fip_dump_img(fdin, fdout, 0);
if(ret < 0)
goto out;
/* Add all BL3* images */
for(i = 0; i < ARRAY_SIZE(fip_bin_path); ++i) {
close(fdin);
fdin = open(fip_bin_path[i].path, O_RDONLY);
if(fdin < 0) {
PERR("Cannot open bl %s: ", fip_bin_path[i].path);
ret = -errno;
goto out;
}
ret = gi_fip_add(&fip, fdout, fdin, fip_bin_path[i].type);
if(ret < 0)
goto out;
}
tmpfd = gi_fip_create_tmp(fippath);
if(tmpfd < 0)
goto out;
ret = gi_amlcblk_init(&acb, fip.fd);
if(ret < 0)
goto out;
ret = gi_amlcblk_aes_enc(&acb, tmpfd, fip.fd);
if(ret < 0)
goto out;
ret = gi_amlcblk_dump_hdr(&acb, tmpfd);
if(ret < 0)
goto out;
off = lseek(tmpfd, 0, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
ret = gi_fip_dump_img(tmpfd, fdout, BL2SZ);
out:
if(tmpfd >= 0) {
close(tmpfd);
(void)remove(fippath);
}
if(fdout >= 0)
close(fdout);
if(fdin >= 0)
close(fdin);
fip_cleanup(&fip);
exit:
return ret;
}