-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcuscanner.c
283 lines (250 loc) · 8.66 KB
/
mcuscanner.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
#include <sys/types.h>
#include <sys/uio.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include "image.h"
static int verbose = 0;
static int input_fd = STDIN_FILENO;
static void usage()
{
printf("\nReads an McuBoot image from stdin and dumps the header and TLV data.\n");
printf("See https://github.com/mcu-tools/mcuboot/blob/main/docs/design.md for details.\n\n");
printf("Usage: mcuscanner -f <format> [-v]\n");
printf(" -v : verbose output\n");
exit(1);
}
static void sig_handler(int signo)
{
signal(signo, SIG_IGN);
fprintf(stderr, "Interrupted\n");
exit(1);
}
static size_t readBytes(void *buf, size_t len) {
size_t bytes_read = 0;
int rc = -1;
char *dest = buf;
while (bytes_read < len && rc != 0) {
rc = read(input_fd, dest, len - bytes_read);
if (rc < 0) {
perror("Read error");
exit(1);
}
bytes_read += rc;
if (rc == 0 && bytes_read < len) {
if (verbose) {
printf("Wanted %ld bytes but reached end of file after %ld bytes\n", len, bytes_read);
}
}
}
return bytes_read;
}
static void skipBytes(size_t len) {
size_t bytes_read = 0;
char buf[8192];
while (len > 0) {
size_t num_to_read = sizeof(buf);
if (len < num_to_read) {
num_to_read = len;
}
int rc = read(input_fd, buf, num_to_read);
if (rc < 0) {
perror("Read error");
exit(1);
}
len -= rc;
if (rc == 0 && len > 0) {
fprintf(stderr, "Unexpected EOF\n");
exit(1);
}
}
}
static int isLegacy(struct image_header *header) {
return header->ih_magic == IMAGE_MAGIC_V1;
}
static void dump_header_flags(uint32_t flags) {
if (flags & IMAGE_F_ENCRYPTED_AES128) {
printf(" ENCRYPTED(AES128)");
} else if (flags & IMAGE_F_ENCRYPTED_AES256) {
printf(" ENCRYPTED(AES256)");
}
}
static void dump_header(struct image_header *header) {
printf("HEADER:\n");
printf(" Magic: 0x%x (isLegacy = %s)\n", header->ih_magic, isLegacy(header) ? "true" : "false");
printf(" Load address: 0x%x\n", header->ih_load_addr);
printf(" Header size: 0x%x (%d bytes)\n", header->ih_hdr_size, header->ih_hdr_size);
printf(" Protected TLV size: 0x%x (%d bytes)\n", header->ih_protect_tlv_size, header->ih_protect_tlv_size);
printf(" Image size: 0x%x (%d bytes)\n", header->ih_img_size, header->ih_img_size);
printf(" Flags: 0x%x", header->ih_flags);
dump_header_flags(header->ih_flags);
printf("\n");
struct image_version *v = &header->ih_ver;
printf(" Version: %d.%d.%d-%d\n", v->iv_major, v->iv_minor, v->iv_revision, v->iv_build_num);
}
static void verify_header(struct image_header *header) {
if (header->ih_magic != IMAGE_MAGIC && header->ih_magic != IMAGE_MAGIC_V1) {
fprintf(stderr, "Bad header magic 0x%x\n", header->ih_magic);
exit(1);
}
// if (header->ih_hdr_size != IMAGE_HEADER_SIZE) {
// fprintf(stderr, "Unsupported header size got %d expected %d\n", header->ih_hdr_size, IMAGE_HEADER_SIZE);
// exit(1);
// }
}
static int isProtected(struct image_tlv_info *tlv_info) {
return tlv_info->it_magic == IMAGE_TLV_PROT_INFO_MAGIC;
}
static void dump_tlv_info(struct image_tlv_info *tlv_info) {
printf("%sPROTECTED TLV INFO HEADER:\n", isProtected(tlv_info) ? "" : "UN");
printf(" Magic: 0x%x (isProtected = %s)\n", tlv_info->it_magic, isProtected(tlv_info) ? "true" : "false");
printf(" Total size (including this header): 0x%x (%d bytes)\n", tlv_info->it_tlv_tot, tlv_info->it_tlv_tot);
}
static void verify_tlv_info(struct image_tlv_info *tlv_info) {
if (tlv_info->it_magic != IMAGE_TLV_INFO_MAGIC && tlv_info->it_magic != IMAGE_TLV_PROT_INFO_MAGIC) {
fprintf(stderr, "Bad TLV magic 0x%x\n", tlv_info->it_magic);
exit(1);
}
}
static char *tlv_type(uint8_t type) {
if (type & IMAGE_TLV_KEYHASH) {
return "Hash of the public key";
}
if (type & IMAGE_TLV_SHA256) {
return "SHA256 of image hdr and body";
}
if (type & IMAGE_TLV_RSA2048_PSS) {
return "RSA2048 of hash output";
}
if (type & IMAGE_TLV_ECDSA224) {
return "ECDSA of hash output - Not supported anymore";
}
if (type & IMAGE_TLV_ECDSA_SIG) {
return "ECDSA of hash output";
}
if (type & IMAGE_TLV_RSA3072_PSS) {
return "RSA3072 of hash output";
}
if (type & IMAGE_TLV_ED25519) {
return "ED25519 of hash output";
}
if (type & IMAGE_TLV_ENC_RSA2048) {
return "Key encrypted with RSA-OAEP-2048";
}
if (type & IMAGE_TLV_ENC_KW) {
return "Key encrypted with AES-KW-128 or 256";
}
if (type & IMAGE_TLV_ENC_EC256) {
return "Key encrypted with ECIES-P256";
}
if (type & IMAGE_TLV_ENC_X25519) {
return "Key encrypted with ECIES-X25519";
}
if (type & IMAGE_TLV_DEPENDENCY) {
return "Image depends on other image";
}
if (type & IMAGE_TLV_SEC_CNT) {
return "Security counter";
}
return "unknown";
}
static void dump_tlv_entry(struct image_tlv *tlv, int isProtected) {
printf("%sPROTECTED TLV ENTRY:\n", isProtected ? "" : "UN");
printf(" Type: 0x%x (%s)\n", tlv->it_type, tlv_type(tlv->it_type));
printf(" Size: 0x%x (%d bytes)\n", tlv->it_len, tlv->it_len);
}
static void dump_tlv_entry_data(unsigned char *data, int len) {
printf(" Data: ");
while (len-- > 0) {
printf("%x", *data++);
}
printf("\n");
}
// Pass total_size = -1 for unlimited
static void dump_tlv_entries(int total_size, int isProtected) {
unsigned char data_buf[65535];
int bytes_read = 0;
struct image_tlv tlv;
size_t tlv_entry_size_read = readBytes(&tlv, sizeof(tlv));
while (tlv_entry_size_read == sizeof(tlv) && bytes_read < total_size) {
bytes_read += tlv_entry_size_read;
dump_tlv_entry(&tlv, isProtected);
if (readBytes(data_buf, tlv.it_len) != tlv.it_len) {
fprintf(stderr, "Error reading %d bytes of TLV data\n", tlv.it_len);
exit(1);
}
bytes_read += tlv.it_len;
dump_tlv_entry_data(data_buf, tlv.it_len);
if (total_size == -1 || bytes_read < total_size) {
tlv_entry_size_read = readBytes(&tlv, sizeof(tlv));
}
}
}
int main(int argc, char *argv[]) {
int opt;
while((opt = getopt(argc, argv, "v")) != -1)
{
switch(opt)
{
case 'v':
verbose = 1;
break;
case ':':
fprintf(stderr, "parameter required for option -%c\n", optopt);
usage();
break;
default:
usage();
break;
}
}
if ((signal(SIGINT, sig_handler) == SIG_ERR) ||
(signal(SIGTERM, sig_handler) == SIG_ERR))
{
perror("Error installing signal handler");
exit(1);
}
struct image_header header;
readBytes(&header, sizeof(header));
dump_header(&header);
verify_header(&header);
if (header.ih_hdr_size > sizeof(header)) {
int bytes_to_skip = header.ih_hdr_size - sizeof(header);
if (verbose) {
printf("Skipping %d additional header bytes\n", bytes_to_skip);
}
skipBytes(bytes_to_skip);
}
skipBytes(header.ih_img_size);
printf("IMAGE BINARY: %d bytes\n", header.ih_img_size);
if (isLegacy(&header)) {
printf("TLV INFO HEADER: none - is legacy format\n");
// Legacy format has no leading info section, we just read trailer entries until EOF
dump_tlv_entries(-1, 0);
return 0;
}
struct image_tlv_info tlv_info;
int tlv_info_bytes_read = readBytes(&tlv_info, sizeof(tlv_info));
dump_tlv_info(&tlv_info);
verify_tlv_info(&tlv_info);
if (header.ih_protect_tlv_size > 0) {
int protected_tlv_byte_count = header.ih_protect_tlv_size - sizeof(tlv_info);
dump_tlv_entries(protected_tlv_byte_count, 1);
int unprotected_tlv_byte_count = tlv_info.it_tlv_tot - header.ih_protect_tlv_size;
if (unprotected_tlv_byte_count > 0) {
dump_tlv_entries(unprotected_tlv_byte_count, 0);
}
} else {
dump_tlv_entries(tlv_info.it_tlv_tot - sizeof(tlv_info), 0);
}
tlv_info_bytes_read = readBytes(&tlv_info, sizeof(tlv_info));
while (tlv_info_bytes_read == sizeof(tlv_info)) {
dump_tlv_info(&tlv_info);
verify_tlv_info(&tlv_info);
dump_tlv_entries(tlv_info.it_tlv_tot - sizeof(tlv_info), 0);
tlv_info_bytes_read = readBytes(&tlv_info, sizeof(tlv_info));
}
printf("END\n");
return 0;
}