-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathbl3.c
164 lines (137 loc) · 2.97 KB
/
bl3.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include "gxlimg.h"
#include "bl3.h"
#include "amlcblk.h"
#include "amlsblk.h"
#define FOUT_MODE_DFT (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
/**
* Sign a BL3 boot image
*
* @param fin: Path of BL3 binary input file
* @param fout: Path of BL3 boot image output file
* @return: 0 on success, negative number otherwise
*/
int gi_bl3_sign_img(char const *fin, char const *fout)
{
struct amlsblk asb;
int fdin = -1, fdout = -1, ret;
DBG("Sign bl3x %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_amlsblk_init(&asb, fdin);
if(ret < 0)
goto out;
ret = gi_amlsblk_hash_payload(&asb, fdin);
if(ret < 0)
goto out;
ret = gi_amlsblk_build_header(&asb);
if(ret < 0)
goto out;
ret = gi_amlsblk_flush_data(&asb, fdin, fdout);
out:
if(fdout >= 0)
close(fdout);
if(fdin >= 0)
close(fdin);
return ret;
}
/**
* Encrypt a BL3 boot image
*
* @param fin: Path of BL3 binary input file
* @param fout: Path of BL3 boot image output file
* @return: 0 on success, negative number otherwise
*/
int gi_bl3_encrypt_img(char const *fin, char const *fout)
{
struct amlcblk acb;
int fdin = -1, fdout = -1, ret;
DBG("Encode bl3 %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_amlcblk_init(&acb, fdin);
if(ret < 0)
goto out;
ret = gi_amlcblk_aes_enc(&acb, fdout, fdin);
if(ret < 0)
goto out;
ret = gi_amlcblk_dump_hdr(&acb, fdout);
out:
if(fdout >= 0)
close(fdout);
if(fdin >= 0)
close(fdin);
return ret;
}
/**
* Decrypt a BL3 boot image
*
* @param fin: Path of BL3 boot image to decode
* @param fout: Path of result BL3 binary file
* @return: 0 on success, negative number otherwise
*/
int gi_bl3_decrypt_img(char const *fin, char const *fout)
{
struct amlcblk acb;
int fdin = -1, fdout = -1, ret;
DBG("Decrypt bl3 %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_WRONLY | 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_amlcblk_read_hdr(&acb, fdin);
if(ret < 0)
goto out;
ret = gi_amlcblk_aes_dec(&acb, fdout, fdin);
if(ret < 0)
goto out;
out:
if(fdout >= 0)
close(fdout);
if(fdin >= 0)
close(fdin);
return ret;
}