10000 esp8266/modesp: Add check_fw() function to check integrity of the fir… · rpavlik/circuitpython@e33d238 · GitHub
[go: up one dir, main page]

Skip to content

Commit e33d238

Browse files
committed
esp8266/modesp: Add check_fw() function to check integrity of the firmware.
Requires firmware generated by the latest makeimg.py (which stores size and md5 of the firmware together with the firmware itself).
1 parent bf47b71 commit e33d238

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

esp8266/etshal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ void ets_timer_arm_new(os_timer_t *tim, uint32_t millis, bool repeat, bool is_mi
2020
void ets_timer_setfn(os_timer_t *tim, ETSTimerFunc callback, void *cb_data);
2121
void ets_timer_disarm(os_timer_t *tim);
2222

23+
// Opaque structure
24+
typedef char MD5_CTX[64];
25+
26+
void MD5Init(MD5_CTX *context);
27+
void MD5Update(MD5_CTX *context, const void *data, unsigned int len);
28+
void MD5Final(unsigned char digest[16], MD5_CTX *context);
29+
2330
// These prototypes are for recent SDKs with "malloc tracking"
2431
void *pvPortMalloc(unsigned sz, const char *fname, int line);
2532
void vPortFree(void *p, const char *fname, int line);

esp8266/modesp.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,24 @@ STATIC mp_obj_t esp_flash_size(void) {
633633
}
634634
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_size_obj, esp_flash_size);
635635

636+
STATIC mp_obj_t esp_check_fw(void) {
637+
MD5_CTX ctx;
638+
uint32_t *sz_p = (uint32_t*)0x40208ffc;
639+
printf("size: %d\n", *sz_p);
640+
MD5Init(&ctx);
641+
MD5Update(&ctx, (char*)0x40200004, *sz_p - 4);
642+
unsigned char digest[16];
643+
MD5Final(digest, &ctx);
644+
printf("md5: ");
645+
for (int i = 0; i < 16; i++) {
646+
printf("%02x", digest[i]);
647+
}
648+
printf("\n");
649+
return mp_obj_new_bool(memcmp(digest, (void*)(0x40200000 + *sz_p), sizeof(digest)) == 0);
650+
}
651+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_check_fw_obj, esp_check_fw);
652+
653+
636654
STATIC mp_obj_t esp_neopixel_write_(mp_obj_t pin, mp_obj_t buf, mp_obj_t is800k) {
637655
mp_buffer_info_t bufinfo;
638656
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
@@ -703,6 +721,7 @@ STATIC const mp_map_elem_t esp_module_globals_table[] = {
703721
{ MP_OBJ_NEW_QSTR(MP_QSTR_dht_readinto), (mp_obj_t)&dht_readinto_obj },
704722
{ MP_OBJ_NEW_QSTR(MP_QSTR_freemem), (mp_obj_t)&esp_freemem_obj },
705723
{ MP_OBJ_NEW_QSTR(MP_QSTR_meminfo), (mp_obj_t)&esp_meminfo_obj },
724+
{ MP_OBJ_NEW_QSTR(MP_QSTR_check_fw), (mp_obj_t)&esp_check_fw_obj },
706725
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj }, // TODO delete/rename/move elsewhere
707726
{ MP_OBJ_NEW_QSTR(MP_QSTR_malloc), (mp_obj_t)&esp_malloc_obj },
708727
{ MP_OBJ_NEW_QSTR(MP_QSTR_free), (mp_obj_t)&esp_free_obj },

0 commit comments

Comments
 (0)
0