/
ota.c
447 lines (383 loc) · 13.6 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
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
// Over-the-air update HTTP handlers
// By Thorsten von Eicken, 2015
#include <c_types.h>
#include <user_interface.h>
#include <mem.h>
#include "osapi_release.h"
#include <upgrade.h>
#include <espconn.h>
#include <espmissingincludes.h>
#include "ota.h"
#define OTA_BUFF_SZ 512
#define OTA_CHUNK_SZ 512
// Request handler
struct OtaConn;
typedef int16_t OtaHandler(struct OtaConn *oc);
// Descriptor for an OTA request connection
typedef struct OtaConn {
struct espconn *conn;
char *rxBuffer; // buffer to accumulate request into
uint16_t rxBufFill; // number of characters in the rxBuffer
bool closing; // just waiting for sent callback to disconnect
int32_t rxBufOff; // offset into req body of first char in buffer, -1:in header
uint32_t reqLen; // length of request body
OtaHandler *handler; // handler to process this request
} OtaConn;
// Format for the response we send
static char *responseFmt =
"HTTP/1.1 %d %s\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %d\r\n"
"Connection: close\r\n"
"Cache-Control: no-store, no-cache, must-revalidate\r\n"
"\r\n%s";
/*
* \brief: releaseConn deallocates everything held by a connection.
*
* It doesn't cause any actual communication to appen, so any disconnect call needs to be made
* separately
*/
static void releaseConn(OtaConn *oc) {
if (!oc) return;
if (oc->rxBuffer) os_free(oc->rxBuffer);
os_memset(oc, 0, sizeof(OtaConn));
}
static void abortConn(OtaConn *oc) {
struct espconn *conn = oc->conn;
os_printf("OTA: aborting\n");
if (conn->reverse != oc) return;
conn->reverse = NULL;
espconn_disconnect(conn);
releaseConn(oc);
}
/*
* \brief: sendResponse sends an HTTP response with a given status code and null-terminated text
*
* This function is very naive because it assumes that the full response can be sent just like
* that in one go. This is OK because we only send very tiny responses.
*/
static void sendResponse(OtaConn *oc, uint16_t code, char *text) {
char *status = code < 400 ? "OK" : "ERROR"; // hacky but it works
// allocate buffer to print the response into
uint16_t len = os_strlen(responseFmt)+os_strlen(status)+os_strlen(text);
char buf[len];
// print the response and send it
len = os_sprintf(buf, responseFmt, code, status, os_strlen(text), text);
if (code < 400) os_printf("OTA: %d %s\n", code, status);
else os_printf("OTA: %d %s <<%s>>\n", code, status, text);
int8_t err;
if ((err=espconn_send(oc->conn, (uint8_t*)buf, os_strlen(buf))) != 0) {
os_printf("OTA: send failed err=%d\n", err);
abortConn(oc);
}
}
/**
* \brief: check_header checks that the header of the firmware blob looks like actual firmware...
*
* It returns an error string if something is amiss
*/
static char* check_header(void *buf, int which) {
uint8_t *cd = (uint8_t *)buf;
uint32_t *buf32 = buf;
os_printf("OTA hdr %p: %08lX %08lX %08lX %08lX\n", buf,
(long unsigned int)buf32[0],
(long unsigned int)buf32[1],
(long unsigned int)buf32[2],
(long unsigned int)buf32[3]);
if (cd[0] != 0xEA) return "IROM magic missing";
if (cd[1] != 4 || cd[2] > 3 || (cd[3]>>4) > 6) return "bad flash header";
if (cd[3] < 3 && cd[3] != which) return "Wrong partition binary";
if (((uint16_t *)buf)[3] != 0x4010) return "Invalid entry addr";
if (((uint32_t *)buf)[2] != 0) return "Invalid start offset";
return NULL;
}
/**
* \brief: Handler for the /flash/next request which returns which firmware needs to be uploaded next
*
* Sends a response with either "user1.bin" or "user2.bin" as bodies. Returns the number of
* bytes consumed from the request, which is the full RX buffer.
*/
static int16_t otaHandleNext(OtaConn *oc) {
uint8 id = system_upgrade_userbin_check();
char *next = id ? "user1.bin" : "user2.bin";
sendResponse(oc, 200, next);
os_printf("OTA: Next firmware: %s (%d=0x%08x)\n", next, id, system_get_userbin_addr());
return -1; // we're *done*
}
// Information about flash size based on flash memory map as returned by system_get_flash_size_map:
// 4Mb_256, 2Mb, 8Mb_512, 16Mb_512, 32Mb_512, 16Mb_1024, 32Mb_1024
static enum flash_size_map flashSizeMap;
// Start address of user2.bin (address of user1 is always 0x1000)
static uint32_t flashUser2Addr[] = {
260*1024, 0, 516*1024, 516*1024, 516*1024, 1028*1024, 1028*1024,
};
// Max size of flash firmware
static uint32_t flashMaxSize[] = {
(256-4-16)*1024, 0, // really: too small...
(512-4-16)*1024, (512-4-16)*1024, (512-4-16)*1024, // 512KB firmware partitions
(1024-4-16)*1024, (1024-4-16)*1024, // 1024 KB firmware partitions
};
/*
* \brief: otaHandleUpload handles a POST to update the firmware
*
* Returns the number of bytes consumed from the request, -1 if a response has been sent.
*/
static int16_t otaHandleUpload(OtaConn *oc) {
uint32_t offset = oc->rxBufOff;
// assume no error yet...
char *err = NULL;
uint16_t code = 400;
// check overall size
if (oc->reqLen > flashMaxSize[flashSizeMap]) {
os_printf("OTA: FW too large: %ld > %ld\n",
(long int) oc->reqLen,
(long int) flashMaxSize[flashSizeMap]);
err = "Firmware image too large";
} else if (oc->reqLen < OTA_CHUNK_SZ) {
os_printf("OTA: FW too small: %ld\n", (long int) oc->reqLen);
err = "Firmware too small";
}
// check that we have at least OTA_CHUNK_SZ bytes buffered or it's the last chunk, else we can't write
if (oc->rxBufFill < OTA_CHUNK_SZ && offset+OTA_CHUNK_SZ <= oc->reqLen) return 0;
uint16_t toFlash = OTA_CHUNK_SZ;
if (oc->rxBufFill < toFlash) toFlash = oc->rxBufFill;
// let's see which partition we need to flash
uint8 id = system_upgrade_userbin_check();
// check that data starts with an appropriate header
if (err == NULL && offset == 0) err = check_header(oc->rxBuffer+oc->rxBufOff, 1-id);
// return an error if there is one
if (err != NULL) {
os_printf("OTA Error %d: %s\n", code, err);
sendResponse(oc, code, err);
return -1;
}
// let's see which what flash address we're uploading to
uint32_t address = id ? 0x1000 : flashUser2Addr[flashSizeMap];
address += offset;
// erase next flash block if necessary
if (address % SPI_FLASH_SEC_SIZE == 0){
os_printf("OTA Flashing 0x%05lx\n", (long unsigned int) address);
spi_flash_erase_sector(address/SPI_FLASH_SEC_SIZE);
}
// write the data
//os_printf("Writing %d bytes at 0x%05x (%d of %d)\n", connData->post->buffSize, address,
// connData->post->received, connData->post->len);
spi_flash_write(address, (uint32 *)oc->rxBuffer, toFlash);
if (offset + toFlash == oc->reqLen) {
sendResponse(oc, 200, "");
return -1;
}
return toFlash;
}
static ETSTimer flash_reboot_timer;
/*
* \brief: otaRebootFirmware Handle request to reboot into the new firmware
*/
static int16_t otaHandleReboot(OtaConn *oc) {
// sanity-check that the 'next' partition actually contains something that looks like
// valid firmware
uint8 id = system_upgrade_userbin_check();
uint32_t address = id == 1 ? 0x1000 : flashUser2Addr[flashSizeMap];
os_printf("OTA: Checking validity at %p\n", (void *)address);
uint32 buf[8];
spi_flash_read(address, buf, sizeof(buf));
char *err = check_header(buf, 1-id);
if (err != NULL) {
os_printf("OTA Error %d: %s\n", 400, err);
sendResponse(oc, 400, err);
return -1;
}
// send empty OK response
sendResponse(oc, 200, "");
// Schedule a reboot
system_upgrade_flag_set(UPGRADE_FLAG_FINISH);
os_timer_disarm(&flash_reboot_timer);
os_timer_setfn(&flash_reboot_timer, (os_timer_func_t *)system_upgrade_reboot, NULL);
os_timer_arm(&flash_reboot_timer, 2000, 1);
return -1; // we're *done*
}
// Mapping from URLs to request handler functions
#define NUM_HANDLERS 3
static struct {
char *verb;
char *path;
OtaHandler *handler;
} otaHandlers[NUM_HANDLERS] = {
{"GET", "/flash/next", otaHandleNext },
{"POST", "/flash/upload", otaHandleUpload },
{"POST", "/flash/reboot", otaHandleReboot },
};
static OtaHandler *findHandler(char *verb, char *path) {
uint16_t i=0;
for (i=0; i<NUM_HANDLERS; i++) {
if (os_strcmp(verb, otaHandlers[i].verb) == 0 &&
os_strcmp(path, otaHandlers[i].path) == 0) {
return otaHandlers[i].handler;
}
}
return NULL;
}
/*
* \brief: processHeader examines the received buffer and processes the http header if there is one
*
* Returns the length of the header, 0 if none is found yet, -1 if an error response has been sent
*/
static int16_t processHeader(OtaConn *oc) {
// see whether we have the full header by looking for CR-LF-CR-LF
uint16_t i;
for (i=0; i<oc->rxBufFill-3; i++) {
if (oc->rxBuffer[i+0] == '\r' && oc->rxBuffer[i+1] == '\n' &&
oc->rxBuffer[i+2] == '\r' && oc->rxBuffer[i+3] == '\n') {
break;
}
}
if (i == oc->rxBufFill-3) return 0; // nope
oc->rxBuffer[i+3] = 0; // make this null-terminated
//os_printf("OTA recv: <<%s>>\n", oc->rxBuffer);
// find the end of the header line
char *hdrEnd = os_strstr(oc->rxBuffer, "\r\n"); // guaranteed to be found
*hdrEnd = 0;
// find start of path/uri
char *path = os_strchr(oc->rxBuffer, ' ');
if (!path) {
sendResponse(oc, 400, "Invalid HTTP request");
return -1;
}
*path++ = 0;
// find start of http version
char *vers = os_strchr(path, ' ');
if (vers) *vers = 0;
// find handler
OtaHandler *handler = findHandler(oc->rxBuffer, path);
if (!handler) {
sendResponse(oc, 404, "Not found");
return -1;
}
oc->handler = handler;
os_printf("OTA: %s %s\n", oc->rxBuffer, path);
// determine content-length
char *clHdr = os_strstr(hdrEnd+1, "Content-Length:");
if (clHdr) {
oc->reqLen = atoi(clHdr+strlen("Content-Length:"));
os_printf("OTA: content len=%ld\n", (long int) oc->reqLen);
}
return i+4;
}
static OtaConn otaConn[1]; // allocate a single connection for now
static struct espconn otaListener; // listening socket
static esp_tcp otaListenerTcp;
/*
* \brief: Receive data from the HTTP client
*/
static void otaRecvCb(void *arg, char *data, unsigned short len) {
struct espconn *conn = arg;
OtaConn *oc = conn->reverse;
if (!oc || oc != otaConn || conn != otaConn[0].conn) return;
if (oc->closing) return;
// allocate a buffer if we have none
if (oc->rxBuffer == NULL) {
oc->rxBuffer = os_zalloc(OTA_BUFF_SZ);
if (!oc->rxBuffer) goto error; // out of memory, disconnect
oc->rxBufFill = 0;
}
int16_t num = 0;
do {
if (oc->rxBufFill == OTA_BUFF_SZ) goto error; // buffer full, disconnect
// copy as much as we can into the rx buffer
uint16_t cpy = OTA_BUFF_SZ - oc->rxBufFill;
if (cpy > len) cpy = len;
os_memcpy(oc->rxBuffer + oc->rxBufFill, data, cpy);
data += cpy;
len -= cpy;
oc->rxBufFill += cpy;
//os_printf("OTA recv: len=%d fill=%d off=%ld\n", len, oc->rxBufFill, oc->rxBufOff);
if (oc->rxBufOff < 0 && oc->rxBufFill > 0) {
// process the request header
num = processHeader(oc);
// if we processed the header, shift it out of the buffer
if (num > 0) {
os_memmove(oc->rxBuffer, oc->rxBuffer + num, oc->rxBufFill-num);
oc->rxBufOff = 0;
oc->rxBufFill -= num;
// if request has no body, invoke handler here and bail out
if (oc->reqLen == 0) {
oc->rxBufFill = 0;
if (oc->rxBuffer) os_free(oc->rxBuffer);
oc->rxBuffer = NULL;
(*oc->handler)(oc);
return;
}
}
} else if (oc->rxBufOff >= 0 && oc->rxBufFill > 0 && oc->handler) {
// process request body
num = (*oc->handler)(oc);
if (num > 0) {
os_memmove(oc->rxBuffer, oc->rxBuffer + num, oc->rxBufFill-num);
oc->rxBufOff += num;
oc->rxBufFill -= num;
}
} else {
num = 0;
}
//os_printf("num=%d\n", num);
// if we've responded we're ready for the close
if (num < 0) {
oc->closing = true;
return;
}
} while (len > 0 || num > 0);
return;
error:
abortConn(oc);
}
static void otaDisconCb(void *arg) {
struct espconn *conn = arg;
//os_printf("OTA: disconnect\n");
OtaConn *oc = conn->reverse;
if (oc) releaseConn(oc);
}
static void otaResetCb(void *arg, sint8 errType) {
struct espconn *conn = arg;
os_printf("OTA: conn reset, err=%d\n", errType);
OtaConn *oc = conn->reverse;
if (oc) releaseConn(oc);
}
/*
* \brief: New connection callback
*/
static void otaConnectCb(void *arg)
{
struct espconn *conn = arg;
// If the connection is in use, kill it
if (otaConn[0].conn != NULL) {
abortConn(otaConn+0);
}
os_printf("OTA: connect from ...\n");
// Initialize new connection
os_memset(otaConn, 0, sizeof(OtaConn));
otaConn[0].conn = conn;
conn->reverse = otaConn+0;
otaConn[0].rxBufOff = -1; // we start with the http header
espconn_set_opt(conn, ESPCONN_REUSEADDR|ESPCONN_NODELAY);
espconn_regist_recvcb(conn, otaRecvCb);
espconn_regist_disconcb(conn, otaDisconCb);
espconn_regist_reconcb(conn, otaResetCb);
}
/*
* \brief: Initialize the OTA server
*/
void otaInit(int port) {
flashSizeMap = system_get_flash_size_map();
os_memset(otaConn, 0, sizeof(otaConn));
os_memset(&otaListener, 0, sizeof(otaListener));
os_memset(&otaListenerTcp, 0, sizeof(otaListenerTcp));
// set-up hte listener
otaListener.type = ESPCONN_TCP;
otaListener.state = ESPCONN_NONE;
otaListener.proto.tcp = &otaListenerTcp;
otaListenerTcp.local_port = port;
espconn_regist_connectcb(&otaListener, otaConnectCb);
espconn_accept(&otaListener);
espconn_tcp_set_max_con_allow(&otaListener, 2); // kill first conn if there is a 2nd
espconn_regist_time(&otaListener, 30, 0);
}