-
Notifications
You must be signed in to change notification settings - Fork 10
/
add-secrets.c
211 lines (186 loc) · 4.95 KB
/
add-secrets.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
// SPDX-FileCopyrightText: © 2020 Foundation Devices, Inc. <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include <libgen.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <getopt.h>
#define EXTENSION "-secrets"
static char* bootloader;
static char* secrets;
static bool debug_log_level;
static void usage(char* name) {
printf("Usage: %s\n", name);
printf(
"\t-d: debug logging\n"
"\t-b <bootloader binary>: full path to bootloader binary file\n"
"\t-s <secrets binary>: full path to secrets binary file\n"
"\t-h: this message\n");
exit(1);
}
static void process_args(int argc, char** argv) {
int c = 0;
while ((c = getopt(argc, argv, "dhb:s:")) != -1) {
switch (c) {
case 'b':
bootloader = optarg;
break;
case 's':
secrets = optarg;
break;
case 'd':
debug_log_level = true;
break;
case 'h':
default:
usage(argv[0]);
break;
}
}
}
static int read_file(char* path, uint8_t** buffer, size_t* size) {
size_t ret = 0;
struct stat info;
FILE* fp;
fp = fopen(path, "r");
if (fp == NULL) {
printf("failed to open %s\n", path);
return -1;
}
stat(path, &info);
*buffer = (uint8_t*)calloc(1, info.st_size + sizeof(ulong));
if (*buffer == NULL) {
printf("insufficient memory\n");
return -1;
}
ret = fread(*buffer, 1, info.st_size, fp);
if (ret != info.st_size) {
fclose(fp);
free(*buffer);
return -1;
}
fclose(fp);
*size = info.st_size;
return 0;
}
static int add_secrets(char* bl, char* secrets) {
int iret = 0;
size_t ret = 0;
size_t bl_len;
size_t secrets_len;
uint8_t* bl_buf = NULL;
uint8_t* secrets_buf = NULL;
FILE* fp = NULL;
size_t outfile_sz = 0;
char* outfile;
char* path;
char* filename;
char* file;
char* tmp;
if (bl == NULL) {
printf("bootloader not specified\n");
return 1;
}
tmp = strdup(bl);
filename = basename(tmp);
if (filename == NULL) {
free(tmp);
printf("basename() failed\n");
return 1;
}
path = dirname(tmp);
if (path == NULL) {
free(tmp);
printf("dirname() failed\n");
return 1;
}
file = strtok(filename, ".");
if (file == NULL) {
free(tmp);
printf("strtok() failed\n");
return 1;
}
outfile_sz = strlen(bl) + strlen(EXTENSION) + 1;
outfile = calloc(sizeof(char), outfile_sz);
if (outfile == NULL) {
free(tmp);
printf("insufficient memory\n");
return 1;
}
// TODO(jeandudey): replace with snprintf_s
iret = snprintf(outfile, outfile_sz, "%s/%s%s.bin", path, file, EXTENSION);
if (iret < 0) {
free(outfile);
free(tmp);
return 1;
}
if (debug_log_level) printf("Reading %s...", bl);
iret = read_file(bl, &bl_buf, &bl_len);
if (iret < 0) {
printf("file %s has no data\n", bl);
free(outfile);
free(tmp);
return 1;
}
if (debug_log_level) printf("done\n");
if (debug_log_level) printf("Reading %s...", secrets);
iret = read_file(secrets, &secrets_buf, &secrets_len);
if (iret < 0) {
printf("file %s has no data\n", secrets);
free(bl_buf);
free(outfile);
free(tmp);
return 1;
}
if (debug_log_level) printf("done\n");
fp = fopen(outfile, "wb");
if (fp == NULL) {
printf("failed to open %s\n", outfile);
free(secrets_buf);
free(bl_buf);
free(outfile);
free(tmp);
return 1;
}
if (debug_log_level) printf("Writing bootloader to %s - ", outfile);
ret = fwrite(bl_buf, 1, bl_len, fp);
if (ret != bl_len) {
printf("\n%s write failed - check disk space\n", outfile);
fclose(fp);
unlink(outfile);
free(secrets_buf);
free(bl_buf);
free(outfile);
free(tmp);
return 1;
}
if (debug_log_level) printf("done\n");
if (debug_log_level) printf("Writing secrets to %s - ", outfile);
ret = fwrite(secrets_buf, 1, secrets_len, fp);
if (ret != secrets_len) {
printf("\n%s write failed - check disk space\n", outfile);
free(secrets_buf);
free(bl_buf);
free(outfile);
free(tmp);
fclose(fp);
return 1;
}
if (debug_log_level) printf("done\n");
free(bl_buf);
free(secrets_buf);
free(outfile);
free(tmp);
fclose(fp);
return 0;
}
int main(int argc, char* argv[]) {
process_args(argc, argv);
return add_secrets(bootloader, secrets);
}