Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp_tinyuf2: fix nvs hidden key (AEGHB-961) #462

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/usb/esp_tinyuf2/esp_tinyuf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -227,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) {
Expand Down
16 changes: 11 additions & 5 deletions components/usb/esp_tinyuf2/esp_tinyuf2.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ 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
*
*/
typedef void (*nvs_modified_cb_t)(void);
typedef void (*nvs_modified_cb_t)(const char*);

/**
* @brief tinyuf2 configurations
Expand All @@ -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;

/**
Expand Down Expand Up @@ -118,7 +124,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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wormyrocks, is there any difference? Whether there is a formal parameter or not, makes no difference to the function itself.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my machine it won't compile without this change - something to do with the compiler settings in tinyuf2.
Outside of the include guards, all the other declarations in that file follow the same style.

esp_err_t esp_tinyuf2_set_all_key_hidden(bool);

/**
* @brief Add a key to the hidden keys list.
Expand All @@ -134,7 +140,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
Expand Down
4 changes: 4 additions & 0 deletions components/usb/esp_tinyuf2/msc/msc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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
}
}
Expand Down
6 changes: 5 additions & 1 deletion components/usb/esp_tinyuf2/private_include/board_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -82,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]);

Expand All @@ -90,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
Expand Down
30 changes: 19 additions & 11 deletions components/usb/esp_tinyuf2/uf2/board_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@
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;
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
Expand All @@ -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 */
Expand All @@ -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);
Expand Down Expand Up @@ -232,8 +235,8 @@ 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)) {
ini_insert_pair(info.key, "****");
if (check_value_if_hidden(info.key)) {
ini_insert_pair(info.key, NVS_HIDDEN_PLACEHOLDER);
} else
#endif
{
Expand All @@ -259,6 +262,11 @@ 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)
{
PRINTFD("Ignore %s", name);
return 1;
}
nvs_handle_t nvs = (nvs_handle_t)user;
PRINTFD("... [%s]", section);
PRINTFD("... (%s=%s)", name, value);
Expand Down Expand Up @@ -314,7 +322,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);
}
}

Expand Down