diff --git a/ports/esp32s2/common-hal/ota/__init__.c b/ports/esp32s2/common-hal/ota/__init__.c index 6d0e8f11b5..97cc67b766 100644 --- a/ports/esp32s2/common-hal/ota/__init__.c +++ b/ports/esp32s2/common-hal/ota/__init__.c @@ -48,7 +48,7 @@ static void __attribute__((noreturn)) task_fatal_error(void) { mp_raise_RuntimeError(translate("OTA Update Failed")); } -void common_hal_ota_flash(const void *buf, const size_t len) { +void common_hal_ota_flash(const void *buf, const size_t len, const int32_t offset) { esp_err_t err; const esp_partition_t *running = esp_ota_get_running_partition(); @@ -108,7 +108,11 @@ void common_hal_ota_flash(const void *buf, const size_t len) { } } - err = esp_ota_write( update_handle, buf, len); + if (offset == -1) { + err = esp_ota_write(update_handle, buf, len); + } else { + err = esp_ota_write_with_offset(update_handle, buf, len, offset); + } if (err != ESP_OK) { ESP_LOGE(TAG, "esp_ota_write failed (%s)", esp_err_to_name(err)); task_fatal_error(); diff --git a/shared-bindings/ota/__init__.c b/shared-bindings/ota/__init__.c index 55858a80e9..84a46c83a2 100644 --- a/shared-bindings/ota/__init__.c +++ b/shared-bindings/ota/__init__.c @@ -36,14 +36,27 @@ STATIC mp_obj_t ota_finish(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(ota_finish_obj, ota_finish); -STATIC mp_obj_t ota_flash(mp_obj_t program_binary_in) { - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(program_binary_in, &bufinfo, MP_BUFFER_READ); +STATIC mp_obj_t ota_flash(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_binary, ARG_offset }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_binary, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_offset, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = -1} }, + }; - common_hal_ota_flash(bufinfo.buf, bufinfo.len); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + if (args[ARG_offset].u_int < -1) { + mp_raise_ValueError(translate("offset must be >= 0")); + } + + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[ARG_binary].u_obj, &bufinfo, MP_BUFFER_READ); + + common_hal_ota_flash(bufinfo.buf, bufinfo.len, args[ARG_offset].u_int); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(ota_flash_obj, ota_flash); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ota_flash_obj, 1, ota_flash); STATIC const mp_rom_map_elem_t ota_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ota) }, diff --git a/shared-bindings/ota/__init__.h b/shared-bindings/ota/__init__.h index 1765c21ad7..9eec604e0b 100644 --- a/shared-bindings/ota/__init__.h +++ b/shared-bindings/ota/__init__.h @@ -30,6 +30,6 @@ #include "py/runtime.h" extern void common_hal_ota_finish(void); -extern void common_hal_ota_flash(const void *buf, const size_t len); +extern void common_hal_ota_flash(const void *buf, const size_t len, const int32_t offset); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_OTA___INIT___H