-
Notifications
You must be signed in to change notification settings - Fork 0
/
ota.c
380 lines (329 loc) · 10.5 KB
/
ota.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
#define GETTEXT_PACKAGE "gtk20"
#include <unistd.h>
#include <sys/reboot.h>
#include <thingymcconfig/client_glib.h>
#include <thingymcconfig/logging.h>
#include <teenynet/http.h>
#include "ota.h"
#include "args.h"
#include "jsonparserutils.h"
#include "crypto.h"
#include "manifest.h"
#include "utils.h"
#include "mtd.h"
#include "stamp.h"
static gchar* host;
static gchar* path;
static struct crypto_keys* keys;
static struct manifest_manifest* manifest = NULL;
static guint currentversion = 0;
static gint64 manifestfetchedat;
static struct manifest_image* targetimage = NULL;
static gboolean waitingtoreboot = FALSE;
static gboolean dryrun = FALSE;
static gboolean force = FALSE;
static gchar** mtds = NULL;
static guint timeoutsource = 0;
static ThingyMcConfigClient* client;
static gboolean connectivitystate = FALSE;
static gboolean responsecallback(const struct teenyhttp_response* response,
gpointer user_data) {
const gchar* contenttype = user_data;
return response->code == 200
&& (strcmp(contenttype, response->contenttype) == 0);
}
static GHashTable* munchbootargs() {
GHashTable* table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
g_free);
// use the uboot supplied boot args as uboot fills this in
// even if the kernel ignores it
const gchar* fdtnode = "/sys/firmware/devicetree/base/chosen/bootargs";
gsize bootargssz;
gchar* bootargs;
if (!g_file_get_contents(fdtnode, &bootargs, &bootargssz, NULL)) {
g_message("failed to read bootargs from dt");
goto err_readbootargs;
}
GRegex* keyregexp = g_regex_new("(ota\.)([a-z]*)=([0-9,a-f,x]*)", 0, 0,
NULL);
g_assert(keyregexp != NULL);
GMatchInfo* match;
if (g_regex_match_full(keyregexp, bootargs, bootargssz, 0, 0, &match,
NULL)) {
while (g_match_info_matches(match)) {
gchar* key = g_match_info_fetch(match, 2);
gchar* value = g_match_info_fetch(match, 3);
g_message("ota.%s=%s", key, value);
g_hash_table_insert(table, key, value);
g_match_info_next(match, NULL);
}
}
g_match_info_free(match);
g_regex_unref(keyregexp);
err_readbootargs: //
return table;
}
static void onendtoendconnectionsuccess() {
if (!connectivitystate) {
connectivitystate = TRUE;
thingymcconfig_client_sendconnectivitystate(client, connectivitystate);
}
}
static void updatemanifest() {
if (targetimage != NULL) {
g_message("target image selected, not updating manifest");
return;
}
gchar* sigpath = buildpath(path, OTA_SIG, NULL);
GByteArray* sigbuffer = g_byte_array_new();
if (!teenyhttp_get(host, sigpath, responsecallback,
MANIFEST_CONTENTTYPE, teenyhttp_datacallback_bytebuffer, sigbuffer)) {
g_message("failed to fetch sig");
goto err_fetchsig;
}
GPtrArray* sigs = manifest_signatures_deserialise((gchar*) sigbuffer->data,
sigbuffer->len);
if (sigs == NULL) {
g_message("failed to parse signatures or no usable signatures");
goto err_parsesig;
}
gchar* manifestpath = buildpath(path, OTA_MANIFEST, NULL);
GByteArray* manifestbuffer = g_byte_array_new();
if (!teenyhttp_get(host, manifestpath, responsecallback,
MANIFEST_CONTENTTYPE, teenyhttp_datacallback_bytebuffer, manifestbuffer)) {
g_message("failed to fetch manifest");
goto err_fetchmanifest;
}
struct crypto_checksigcntx chksigcntx = { .what = "manifest", .data =
manifestbuffer->data, .len = manifestbuffer->len, .keys = keys,
.cont = TRUE };
g_ptr_array_foreach(sigs, crypto_checksig, &chksigcntx);
if (!chksigcntx.cont) {
g_message("manifest sig check failed");
goto err_manifestsig;
}
struct manifest_manifest* newmanifest = manifest_deserialise(
(gchar*) manifestbuffer->data, manifestbuffer->len);
if (newmanifest == NULL) {
g_message("failed to parse manifest");
goto err_manifestparse;
}
if (manifest != NULL) {
if (newmanifest->serial <= manifest->serial) {
g_message(
"new manifest is older or the same version as the current one, ignoring");
manifest_free(newmanifest);
goto out;
} else
manifest_free(manifest);
}
manifest = newmanifest;
manifestfetchedat = g_get_real_time();
onendtoendconnectionsuccess();
out: //
err_manifestparse: //
err_manifestsig: //
err_fetchmanifest: //
g_free(manifestpath);
g_byte_array_free(manifestbuffer, TRUE);
err_parsesig: //
err_fetchsig: //
g_free(sigpath);
g_byte_array_free(sigbuffer, TRUE);
}
static void ota_image_findcandidate(gpointer data, gpointer user_data) {
struct manifest_image* image = data;
GPtrArray* candidates = user_data;
g_message("checking image %s", image->uuid);
if (!image->enabled) {
g_message("image isn't enabled");
return;
} else if (!force && image->version <= currentversion) {
g_message("image version %d isn't higher than %d", image->version,
currentversion);
return;
}
g_ptr_array_add(candidates, image);
}
static gint ota_image_score(gconstpointer a, gconstpointer b) {
const struct manifest_image* left = *((struct manifest_image**) a);
const struct manifest_image* right = *((struct manifest_image**) b);
return left->version - right->version;
}
static void ota_checkimages() {
if (manifest == NULL || targetimage != NULL)
return;
g_message("looking for update..");
if (manifest->images->len == 0) {
g_message("manifest contains no images");
return;
}
GPtrArray* candidates = g_ptr_array_new();
g_ptr_array_foreach(manifest->images, ota_image_findcandidate, candidates);
if (candidates->len > 0) {
g_message("have %d candidates", candidates->len);
g_ptr_array_sort(candidates, ota_image_score);
targetimage = g_ptr_array_index(candidates, candidates->len - 1);
g_message("scheduled update to image %s(%u)", targetimage->uuid,
targetimage->version);
}
g_ptr_array_free(candidates, TRUE);
}
static const gchar* ota_findpassive() {
GHashTable* bootargs = munchbootargs();
gchar* mtd = mtds[0];
if (!g_hash_table_contains(bootargs, "part")) {
g_message(
"failed to find image offset in bootargs, first mtd will be used");
goto err_nopartkey;
}
gchar* partkey = g_hash_table_lookup(bootargs, "part");
guint64 offset = g_ascii_strtoull(partkey, NULL, 16);
gchar* activepart = mtd_foroffset(offset);
if (activepart == NULL) {
g_message(
"failed to find partition for offset %u, first mtd will be used",
(unsigned ) offset);
goto err_badoffset;
}
g_message("active partition is %s", activepart);
gchar* passive = NULL;
for (gchar** part = mtds; *part != NULL; part++) {
if (strcmp(*part, activepart) != 0) {
passive = *part;
break;
}
}
g_assert(passive != NULL);
g_message("selected %s as passive partition", passive);
mtd = passive;
err_nopassive: //
err_badoffset: //
err_nopartkey: //
g_hash_table_unref(bootargs);
return mtd;
}
static void ota_tryupdate() {
if (targetimage == NULL)
return;
gchar* imagepath = buildpath(path, targetimage->uuid, NULL);
GByteArray* imagebuffer = g_byte_array_new();
teenyhttp_get_simple(host, imagepath, teenyhttp_datacallback_bytebuffer,
imagebuffer);
if (imagebuffer->len != targetimage->size) {
g_message(
"downloaded image size doesn't match manifest %u vs %" G_GSIZE_FORMAT,
imagebuffer->len, targetimage->size);
goto err_imagelen;
}
struct crypto_checksigcntx cntx = { .what = "image", .data =
imagebuffer->data, .len = imagebuffer->len, .keys = keys, .cont =
TRUE };
g_ptr_array_foreach(targetimage->signatures, crypto_checksig, &cntx);
if (!cntx.cont) {
g_message("image signature verification failed");
goto err_imagesig;
}
if (!dryrun) {
const gchar* mtd = ota_findpassive();
g_message("erasing passive partition...");
if (!mtd_erase(mtd))
goto err_mtderase;
g_message("installing image...");
if (!mtd_writeimage(mtd, imagebuffer->data, imagebuffer->len))
goto err_mtdwrite;
g_message("scheduling reboot...");
waitingtoreboot = TRUE;
reboot(RB_AUTOBOOT);
}
err_mtdwrite: //
err_mtderase: //
err_imagelen: //
err_imagesig: //
g_byte_array_free(imagebuffer, TRUE);
}
static gboolean timeout(gpointer user_data) {
updatemanifest();
ota_checkimages();
ota_tryupdate();
return waitingtoreboot ? G_SOURCE_REMOVE : G_SOURCE_CONTINUE;
}
static void ota_daemon_connected(void) {
thingymcconfig_client_sendappstate(client);
}
static void ota_supplicant_connected(void) {
timeout(NULL);
timeoutsource = g_timeout_add_seconds(60 * 10, timeout, NULL);
}
static void ota_supplicant_disconnected(void) {
if (timeoutsource != 0) {
g_source_remove(timeoutsource);
timeoutsource = 0;
}
}
static void ota_daemon_disconnected(void) {
}
int main(int argc, char** argv) {
int ret = 0;
host = "thingy.jp";
path = "/ota/spibeagle";
gchar* arg_configdir = OTA_CONFIGDIR_DEFAULT;
gchar* logfile = NULL;
GError* error = NULL;
GOptionEntry entries[] = { ARGS_HOST, ARGS_PATH, ARGS_CONFIGDIR, ARGS_MTD,
ARGS_DRYRUN, ARGS_FORCE, ARGS_LOG, { NULL } };
GOptionContext* optioncontext = g_option_context_new(NULL);
g_option_context_add_main_entries(optioncontext, entries,
GETTEXT_PACKAGE);
if (!g_option_context_parse(optioncontext, &argc, &argv, &error)) {
g_print("option parsing failed: %s\n", error->message);
ret = 1;
goto err_args;
}
logging_init(logfile);
if (!dryrun) {
int nummtds = mtds != NULL ? g_strv_length(mtds) : 0;
if (nummtds < 2) {
g_message("you must specify at least two mtd partitions");
goto err_args;
} else if (nummtds > 2) {
//TODO some message about not using more than two mtds here
}
if (!mtd_init(mtds))
goto err_mtdinit;
}
gchar* pubkeypath = buildpath(arg_configdir, OTA_CONFIGDIR_SUBDIR_KEYS,
CRYPTO_KEYNAME_RSA_PUB, NULL);
keys = crypto_readkeys(pubkeypath, NULL);
g_free(pubkeypath);
if (keys == NULL) {
g_message("failed to load keys");
goto err_loadkeys;
}
gchar* stamppath = buildpath(arg_configdir, STAMPFILE, NULL);
struct stamp_stamp* stamp = stamp_loadstamp(stamppath);
if (stamp == NULL)
goto err_loadstamp;
currentversion = stamp->version;
stamp_freestamp(stamp);
teenyhttp_init();
client = thingymcconfig_client_new("ota");
g_signal_connect(client, THINGYMCCONFIG_DETAILEDSIGNAL_DAEMON_CONNECTED,
ota_daemon_connected, NULL);
g_signal_connect(client, THINGYMCCONFIG_DETAILEDSIGNAL_SUPPLICANT_CONNECTED,
ota_supplicant_connected, NULL);
g_signal_connect(client,
THINGYMCCONFIG_DETAILEDSIGNAL_SUPPLICANT_DISCONNECTED,
ota_supplicant_disconnected, NULL);
g_signal_connect(client, THINGYMCCONFIG_DETAILEDSIGNAL_DAEMON_DISCONNECTED,
ota_daemon_disconnected, NULL);
thingymcconfig_client_lazyconnect(client);
GMainLoop* mainloop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(mainloop);
thingymcconfig_client_free(client);
err_loadstamp: //
err_loadkeys: //
err_mtdinit: //
err_args: //
return ret;
}