From 6cc3da131675641cd5af8624183be2e7a9efe7e8 Mon Sep 17 00:00:00 2001 From: Evan Kahn Date: Tue, 21 Jan 2025 18:10:00 -0500 Subject: [PATCH 1/5] esp_tinyuf2: fix nvs hidden key --- components/usb/esp_tinyuf2/esp_tinyuf2.h | 4 ++-- components/usb/esp_tinyuf2/uf2/board_flash.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/usb/esp_tinyuf2/esp_tinyuf2.h b/components/usb/esp_tinyuf2/esp_tinyuf2.h index cbdfd19dc..55b26425e 100644 --- a/components/usb/esp_tinyuf2/esp_tinyuf2.h +++ b/components/usb/esp_tinyuf2/esp_tinyuf2.h @@ -118,7 +118,7 @@ void esp_restart_from_tinyuf2(void); * @return * - ESP_OK: Operation was successful. */ -esp_err_t esp_tinyuf2_set_all_key_hidden(bool if_hidden) +esp_err_t esp_tinyuf2_set_all_key_hidden(bool); /** * @brief Add a key to the hidden keys list. @@ -134,7 +134,7 @@ esp_err_t esp_tinyuf2_set_all_key_hidden(bool if_hidden) * - ESP_ERR_INVALID_ARG: Provided key is NULL. * - ESP_ERR_NO_MEM: Memory allocation failed or maximum number of hidden keys exceeded. */ -esp_err_t esp_tinyuf2_add_key_hidden(const char *key) +esp_err_t esp_tinyuf2_add_key_hidden(const char *); #endif #ifdef __cplusplus diff --git a/components/usb/esp_tinyuf2/uf2/board_flash.c b/components/usb/esp_tinyuf2/uf2/board_flash.c index ff897c978..a85c2d7d7 100644 --- a/components/usb/esp_tinyuf2/uf2/board_flash.c +++ b/components/usb/esp_tinyuf2/uf2/board_flash.c @@ -65,7 +65,7 @@ static nvs_modified_cb_t _modified_cb = NULL; static char _part_name[16] = ""; static char _namespace_name[16] = ""; #ifdef CONFIG_UF2_INI_NVS_VALUE_HIDDEN -char *HIDDEN_STR[CONFIG_UF2_INI_NVS_HIDDEN_MAX_NUM]; +char *hidden_str[CONFIG_UF2_INI_NVS_HIDDEN_MAX_NUM]; size_t hidden_str_num = 0; bool if_all_hidden = false; #endif @@ -232,7 +232,7 @@ static void ini_gen_from_nvs(const char *part, const char *name) char *str = (char *)malloc(len); if ((result = nvs_get_str(nvs, info.key, str, &len)) == ESP_OK) { #ifdef CONFIG_UF2_INI_NVS_VALUE_HIDDEN - if (!check_value_if_hidden(info.key)) { + if (check_value_if_hidden(info.key)) { ini_insert_pair(info.key, "****"); } else #endif From 67a4125f5710fbea16d2e6213e80e97c5b691bff Mon Sep 17 00:00:00 2001 From: Evan Kahn Date: Tue, 21 Jan 2025 18:16:02 -0500 Subject: [PATCH 2/5] include temp fix from github issue 454/ AEGHB-940 --- components/usb/esp_tinyuf2/esp_tinyuf2.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/usb/esp_tinyuf2/esp_tinyuf2.c b/components/usb/esp_tinyuf2/esp_tinyuf2.c index aa028ba98..8ee4a4a47 100644 --- a/components/usb/esp_tinyuf2/esp_tinyuf2.c +++ b/components/usb/esp_tinyuf2/esp_tinyuf2.c @@ -113,6 +113,8 @@ static void _usb_otg_phy_init(bool enable) // Configure USB JTAG PHY #if SOC_USB_SERIAL_JTAG_SUPPORTED phy_conf.controller = USB_PHY_CTRL_SERIAL_JTAG; + phy_conf.otg_mode = USB_PHY_MODE_DEFAULT; + phy_conf.otg_speed = USB_PHY_SPEED_UNDEFINED; usb_new_phy(&phy_conf, &phy_hdl); #endif } From bc83c0a4c6b0eb8db1cecd9c3b400655623d2b31 Mon Sep 17 00:00:00 2001 From: Evan Kahn Date: Sun, 26 Jan 2025 22:03:50 -0500 Subject: [PATCH 3/5] esp_tinyuf2: pass back config string in callback --- components/usb/esp_tinyuf2/esp_tinyuf2.h | 2 +- components/usb/esp_tinyuf2/uf2/board_flash.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/usb/esp_tinyuf2/esp_tinyuf2.h b/components/usb/esp_tinyuf2/esp_tinyuf2.h index 55b26425e..1556cb56c 100644 --- a/components/usb/esp_tinyuf2/esp_tinyuf2.h +++ b/components/usb/esp_tinyuf2/esp_tinyuf2.h @@ -36,7 +36,7 @@ typedef void (*update_complete_cb_t)(void); * @brief user callback called after nvs modified * */ -typedef void (*nvs_modified_cb_t)(void); +typedef void (*nvs_modified_cb_t)(const char*); /** * @brief tinyuf2 configurations diff --git a/components/usb/esp_tinyuf2/uf2/board_flash.c b/components/usb/esp_tinyuf2/uf2/board_flash.c index a85c2d7d7..91aba656b 100644 --- a/components/usb/esp_tinyuf2/uf2/board_flash.c +++ b/components/usb/esp_tinyuf2/uf2/board_flash.c @@ -314,7 +314,7 @@ void board_flash_nvs_update(const char *ini_str) } ini_parse_to_nvs(ini_str); if (_modified_cb) { - _modified_cb(); + _modified_cb(ini_str); } } From 998a86ed89ace941b9df8eec5d96001670cc6b14 Mon Sep 17 00:00:00 2001 From: Evan Kahn Date: Wed, 5 Feb 2025 18:49:24 -0500 Subject: [PATCH 4/5] don't save hidden values --- components/usb/esp_tinyuf2/private_include/board_flash.h | 1 + components/usb/esp_tinyuf2/uf2/board_flash.c | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/components/usb/esp_tinyuf2/private_include/board_flash.h b/components/usb/esp_tinyuf2/private_include/board_flash.h index be141bbe8..06f9ea7a1 100644 --- a/components/usb/esp_tinyuf2/private_include/board_flash.h +++ b/components/usb/esp_tinyuf2/private_include/board_flash.h @@ -74,6 +74,7 @@ #define CFG_UF2_INI_FILE_SIZE CONFIG_UF2_INI_FILE_SIZE extern char *_ini_file; extern char *_ini_file_dummy; +#define NVS_HIDDEN_PLACEHOLDER "****" //--------------------------------------------------------------------+ // Basic API diff --git a/components/usb/esp_tinyuf2/uf2/board_flash.c b/components/usb/esp_tinyuf2/uf2/board_flash.c index 91aba656b..7e1b3dd77 100644 --- a/components/usb/esp_tinyuf2/uf2/board_flash.c +++ b/components/usb/esp_tinyuf2/uf2/board_flash.c @@ -233,7 +233,7 @@ static void ini_gen_from_nvs(const char *part, const char *name) if ((result = nvs_get_str(nvs, info.key, str, &len)) == ESP_OK) { #ifdef CONFIG_UF2_INI_NVS_VALUE_HIDDEN if (check_value_if_hidden(info.key)) { - ini_insert_pair(info.key, "****"); + ini_insert_pair(info.key, NVS_HIDDEN_PLACEHOLDER); } else #endif { @@ -259,6 +259,10 @@ static void ini_gen_from_nvs(const char *part, const char *name) static int nvs_write_back(void* user, const char* section, const char* name, const char* value) { + if (check_value_if_hidden(name) && strcmp(value, NVS_HIDDEN_PLACEHOLDER) == 0) + { + return 1; + } nvs_handle_t nvs = (nvs_handle_t)user; PRINTFD("... [%s]", section); PRINTFD("... (%s=%s)", name, value); From a9da3e7c7af10742d556e6801d317c66b1d9aa95 Mon Sep 17 00:00:00 2001 From: Evan Kahn Date: Fri, 7 Feb 2025 01:43:37 -0500 Subject: [PATCH 5/5] Genericize MSC callback, allowing application to follow update progress --- components/usb/esp_tinyuf2/esp_tinyuf2.c | 2 +- components/usb/esp_tinyuf2/esp_tinyuf2.h | 10 ++++++++-- components/usb/esp_tinyuf2/msc/msc.c | 4 ++++ .../esp_tinyuf2/private_include/board_flash.h | 5 ++++- components/usb/esp_tinyuf2/uf2/board_flash.c | 18 +++++++++++------- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/components/usb/esp_tinyuf2/esp_tinyuf2.c b/components/usb/esp_tinyuf2/esp_tinyuf2.c index 8ee4a4a47..bac90ad4d 100644 --- a/components/usb/esp_tinyuf2/esp_tinyuf2.c +++ b/components/usb/esp_tinyuf2/esp_tinyuf2.c @@ -229,7 +229,7 @@ esp_err_t esp_tinyuf2_install(tinyuf2_ota_config_t *ota_config, tinyuf2_nvs_conf if (ota_config->if_restart) { ESP_LOGW(TAG, "Enable restart, SoC will restart after update complete"); } - board_flash_init(ota_config->subtype, ota_config->label, ota_config->complete_cb, ota_config->if_restart); + board_flash_init(ota_config->subtype, ota_config->label, ota_config->event_cb, ota_config->if_restart); } if (nvs_config) { diff --git a/components/usb/esp_tinyuf2/esp_tinyuf2.h b/components/usb/esp_tinyuf2/esp_tinyuf2.h index 1556cb56c..58a1891f3 100644 --- a/components/usb/esp_tinyuf2/esp_tinyuf2.h +++ b/components/usb/esp_tinyuf2/esp_tinyuf2.h @@ -26,11 +26,17 @@ extern "C" { #define UF2_RESET_REASON_VALUE (CONFIG_UF2_OTA_RESET_REASON_VALUE) +typedef enum { + TINYUF2_UPDATE_COMPLETE = 0, + TINYUF2_UPDATE_PCT = 1, + TINYUF2_UPDATE_MOUNT = 2, +} uf2_update_event_t; + /** * @brief user callback called after uf2 update complete * */ -typedef void (*update_complete_cb_t)(void); +typedef void (*update_event_cb_t)(uf2_update_event_t, uint32_t); /** * @brief user callback called after nvs modified @@ -46,7 +52,7 @@ typedef struct { esp_partition_subtype_t subtype; /*!< Partition subtype. if ESP_PARTITION_SUBTYPE_ANY will use the next_update_partition by default. */ const char *label; /*!< Partition label. Set this value if looking for partition with a specific name. if subtype==ESP_PARTITION_SUBTYPE_ANY, label default to NULL.*/ bool if_restart; /*!< if restart system to new app partition after UF2 flashing done */ - update_complete_cb_t complete_cb; /*!< user callback called after uf2 update complete */ + update_event_cb_t event_cb; /*!< user callback called after uf2 update complete */ } tinyuf2_ota_config_t; /** diff --git a/components/usb/esp_tinyuf2/msc/msc.c b/components/usb/esp_tinyuf2/msc/msc.c index ea3fc15ed..43e9f7186 100644 --- a/components/usb/esp_tinyuf2/msc/msc.c +++ b/components/usb/esp_tinyuf2/msc/msc.c @@ -184,6 +184,8 @@ void tud_msc_write10_complete_cb(uint8_t lun) ESP_LOGI(TAG, "STATE_WRITING_FINISHED"); board_dfu_complete(); } + else + board_event_cb(TINYUF2_UPDATE_PCT, _wr_state.numWritten * 100 / _wr_state.numBlocks); } } @@ -207,8 +209,10 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo if (load_eject) { if (start) { + board_event_cb(TINYUF2_UPDATE_MOUNT, 1); // load disk storage } else { + board_event_cb(TINYUF2_UPDATE_MOUNT, 0); // unload disk storage } } diff --git a/components/usb/esp_tinyuf2/private_include/board_flash.h b/components/usb/esp_tinyuf2/private_include/board_flash.h index 06f9ea7a1..0e4369893 100644 --- a/components/usb/esp_tinyuf2/private_include/board_flash.h +++ b/components/usb/esp_tinyuf2/private_include/board_flash.h @@ -83,6 +83,9 @@ extern char *_ini_file_dummy; // DFU is complete, should reset or jump to application mode and not return void board_dfu_complete(void); +// Access to user callback from msc.c, etc +void board_event_cb(uf2_update_event_t, uint32_t); + // Fill Serial Number and return its length (limit to 16 bytes) uint8_t board_usb_get_serial(uint8_t serial_id[16]); @@ -91,7 +94,7 @@ uint8_t board_usb_get_serial(uint8_t serial_id[16]); //--------------------------------------------------------------------+ // Initialize flash for DFU -void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_complete_cb_t complete_cb, bool if_restart); +void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_event_cb_t event_cb, bool if_restart); void board_flash_deinit(void); // Initialize flash for NVS diff --git a/components/usb/esp_tinyuf2/uf2/board_flash.c b/components/usb/esp_tinyuf2/uf2/board_flash.c index 7e1b3dd77..a36bf68e4 100644 --- a/components/usb/esp_tinyuf2/uf2/board_flash.c +++ b/components/usb/esp_tinyuf2/uf2/board_flash.c @@ -57,7 +57,7 @@ static uint32_t _fl_addr = FLASH_CACHE_INVALID_ADDR; static uint8_t *_fl_buf = NULL; static bool _if_restart = false; -static update_complete_cb_t _complete_cb = NULL; +static update_event_cb_t _event_cb = NULL; static esp_partition_t const* _part_ota = NULL; char *_ini_file = NULL; char *_ini_file_dummy = NULL; @@ -77,14 +77,17 @@ uint8_t board_usb_get_serial(uint8_t serial_id[16]) return 6; } +void board_event_cb(uf2_update_event_t e, uint32_t p) { + if (!_event_cb) + return; + _event_cb(e, p); +} + void board_dfu_complete(void) { esp_ota_set_boot_partition(_part_ota); - if (_complete_cb) { - PRINTF("dfu_complete: run user callback"); - _complete_cb(); - } + board_event_cb(TINYUF2_UPDATE_COMPLETE, 0); if (_if_restart) { /* code */ @@ -94,11 +97,11 @@ void board_dfu_complete(void) } } -void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_complete_cb_t complete_cb, bool if_restart) +void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_event_cb_t event_cb, bool if_restart) { _fl_addr = FLASH_CACHE_INVALID_ADDR; _if_restart = if_restart; - _complete_cb = complete_cb; + _event_cb = event_cb; if (subtype == ESP_PARTITION_SUBTYPE_ANY) { _part_ota = esp_ota_get_next_update_partition(NULL); @@ -261,6 +264,7 @@ static int nvs_write_back(void* user, const char* section, const char* name, { if (check_value_if_hidden(name) && strcmp(value, NVS_HIDDEN_PLACEHOLDER) == 0) { + PRINTFD("Ignore %s", name); return 1; } nvs_handle_t nvs = (nvs_handle_t)user;