From de07298213b60cf3016d28b126e9e3dacdd4a067 Mon Sep 17 00:00:00 2001 From: Shen Weilong Date: Fri, 14 Mar 2025 20:56:52 +0800 Subject: [PATCH] feat(ble/controller): Added memory boundary check for ESP32-C6 and ESP32-H2 --- components/bt/controller/esp32c6/Kconfig.in | 5 +- components/bt/controller/esp32c6/ble.c | 58 +++++++++++++++++++ components/bt/controller/esp32c6/esp_bt_cfg.h | 6 ++ components/bt/controller/esp32h2/Kconfig.in | 5 +- components/bt/controller/esp32h2/ble.c | 52 +++++++++++++++++ components/bt/controller/esp32h2/esp_bt_cfg.h | 6 ++ .../bt/controller/lib_esp32c6/esp32c6-bt-lib | 2 +- .../bt/controller/lib_esp32h2/esp32h2-bt-lib | 2 +- .../bt/include/esp32c6/include/esp_bt.h | 2 + .../bt/include/esp32h2/include/esp_bt.h | 2 + 10 files changed, 136 insertions(+), 4 deletions(-) diff --git a/components/bt/controller/esp32c6/Kconfig.in b/components/bt/controller/esp32c6/Kconfig.in index 70165672f6..8be6745af1 100644 --- a/components/bt/controller/esp32c6/Kconfig.in +++ b/components/bt/controller/esp32c6/Kconfig.in @@ -1,6 +1,5 @@ menu "HCI Config" - choice BT_LE_HCI_INTERFACE prompt "HCI mode" default BT_LE_HCI_INTERFACE_USE_RAM @@ -434,6 +433,10 @@ menu "Controller debug features" default n help Retain scene with GDB to capture info, requires disabling WDT (CONFIG_ESP_INT_WDT, CONFIG_ESP_TASK_WDT_EN). + + config BT_LE_PTR_CHECK_ENABLED + bool "Enable boundary check for internal memory" + default n endmenu config BT_LE_LL_RESOLV_LIST_SIZE diff --git a/components/bt/controller/esp32c6/ble.c b/components/bt/controller/esp32c6/ble.c index 3aca469ee1..f93f15e9b8 100644 --- a/components/bt/controller/esp32c6/ble.c +++ b/components/bt/controller/esp32c6/ble.c @@ -7,6 +7,7 @@ #include "sdkconfig.h" #include "esp_bt_cfg.h" +#include "esp_bit_defs.h" /* External functions or variables ************************************************************************ @@ -28,9 +29,56 @@ int conn_errorSim_enable(void); void conn_errorSim_disable(void); #endif // CONFIG_BT_LE_ERROR_SIM_ENABLED +#if (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) +void adv_stack_enableClearLegacyAdvVsCmd(bool en); +void scan_stack_enableAdvFlowCtrlVsCmd(bool en); +void advFilter_stack_enableDupExcListVsCmd(bool en); +void arr_stack_enableMultiConnVsCmd(bool en); +void pcl_stack_enableSetRssiThreshVsCmd(bool en); +void chanSel_stack_enableSetCsaVsCmd(bool en); +void log_stack_enableLogsRelatedVsCmd(bool en); +void hci_stack_enableSetVsEvtMaskVsCmd(bool en); +void winWiden_stack_enableSetConstPeerScaVsCmd(bool en); +#if CONFIG_IDF_TARGET_ESP32C61_ECO3 +void conn_stack_enableSetPrefTxRxCntVsCmd(bool en); +#endif // CONFIG_IDF_TARGET_ESP32C61_ECO3 + +void adv_stack_enableScanReqRxdVsEvent(bool en); +void conn_stack_enableChanMapUpdCompVsEvent(bool en); +void sleep_stack_enableWakeupVsEvent(bool en); +#endif // (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + /* Local functions definition *************************************************************************** */ +#if (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) +void ble_stack_enableVsCmds(bool en) +{ + adv_stack_enableClearLegacyAdvVsCmd(en); + advFilter_stack_enableDupExcListVsCmd(en); + scan_stack_enableAdvFlowCtrlVsCmd(en); + arr_stack_enableMultiConnVsCmd(en); + pcl_stack_enableSetRssiThreshVsCmd(en); + chanSel_stack_enableSetCsaVsCmd(en); + log_stack_enableLogsRelatedVsCmd(en); + hci_stack_enableSetVsEvtMaskVsCmd(en); + winWiden_stack_enableSetConstPeerScaVsCmd(en); +#if CONFIG_IDF_TARGET_ESP32C61_ECO3 + conn_stack_enableSetPrefTxRxCntVsCmd(en); +#endif // CONFIG_IDF_TARGET_ESP32C61_ECO3 +} + +void ble_stack_enableVsEvents(bool en) +{ + adv_stack_enableScanReqRxdVsEvent(en); + conn_stack_enableChanMapUpdCompVsEvent(en); + +#if CONFIG_BT_LE_SLEEP_ENABLE + sleep_stack_enableWakeupVsEvent(en); +#endif // CONFIG_BT_LE_SLEEP_ENABLE +} +#endif // (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + int ble_stack_initEnv(void) { int rc; @@ -90,11 +138,21 @@ int ble_stack_enable(void) #endif // CONFIG_BT_LE_ERROR_SIM_ENABLED #endif // DEFAULT_BT_LE_MAX_CONNECTIONS +#if (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + ble_stack_enableVsCmds(true); + ble_stack_enableVsEvents(true); +#endif // (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + return 0; } void ble_stack_disable(void) { +#if (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + ble_stack_enableVsEvents(false); + ble_stack_enableVsCmds(false); +#endif // (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + #if DEFAULT_BT_LE_MAX_CONNECTIONS #if CONFIG_BT_LE_ERROR_SIM_ENABLED conn_errorSim_disable(); diff --git a/components/bt/controller/esp32c6/esp_bt_cfg.h b/components/bt/controller/esp32c6/esp_bt_cfg.h index 3d8edd55de..6621c28cb4 100644 --- a/components/bt/controller/esp32c6/esp_bt_cfg.h +++ b/components/bt/controller/esp32c6/esp_bt_cfg.h @@ -204,6 +204,12 @@ extern "C" { #define DEFAULT_BT_LE_VHCI_ENABLED (0) #endif +#ifdef CONFIG_BT_LE_PTR_CHECK_ENABLED +#define DEFAULT_BT_LE_PTR_CHECK_ENABLED (CONFIG_BT_LE_PTR_CHECK_ENABLED) +#else +#define DEFAULT_BT_LE_PTR_CHECK_ENABLED (0) +#endif + #ifdef CONFIG_BT_LE_SLEEP_ENABLE #define NIMBLE_SLEEP_ENABLE CONFIG_BT_LE_SLEEP_ENABLE #else diff --git a/components/bt/controller/esp32h2/Kconfig.in b/components/bt/controller/esp32h2/Kconfig.in index d270ca4913..c3aa384262 100644 --- a/components/bt/controller/esp32h2/Kconfig.in +++ b/components/bt/controller/esp32h2/Kconfig.in @@ -1,6 +1,5 @@ menu "HCI Config" - choice BT_LE_HCI_INTERFACE prompt "HCI mode" default BT_LE_HCI_INTERFACE_USE_RAM @@ -425,6 +424,10 @@ menu "Controller debug features" default n help Retain scene with GDB to capture info, requires disabling WDT (CONFIG_ESP_INT_WDT, CONFIG_ESP_TASK_WDT_EN). + + config BT_LE_PTR_CHECK_ENABLED + bool "Enable boundary check for internal memory" + default n endmenu config BT_LE_LL_RESOLV_LIST_SIZE diff --git a/components/bt/controller/esp32h2/ble.c b/components/bt/controller/esp32h2/ble.c index 3aca469ee1..caa8d85543 100644 --- a/components/bt/controller/esp32h2/ble.c +++ b/components/bt/controller/esp32h2/ble.c @@ -7,6 +7,7 @@ #include "sdkconfig.h" #include "esp_bt_cfg.h" +#include "esp_bit_defs.h" /* External functions or variables ************************************************************************ @@ -28,9 +29,50 @@ int conn_errorSim_enable(void); void conn_errorSim_disable(void); #endif // CONFIG_BT_LE_ERROR_SIM_ENABLED +#if (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) +void adv_stack_enableClearLegacyAdvVsCmd(bool en); +void scan_stack_enableAdvFlowCtrlVsCmd(bool en); +void advFilter_stack_enableDupExcListVsCmd(bool en); +void arr_stack_enableMultiConnVsCmd(bool en); +void pcl_stack_enableSetRssiThreshVsCmd(bool en); +void chanSel_stack_enableSetCsaVsCmd(bool en); +void log_stack_enableLogsRelatedVsCmd(bool en); +void hci_stack_enableSetVsEvtMaskVsCmd(bool en); +void winWiden_stack_enableSetConstPeerScaVsCmd(bool en); + +void adv_stack_enableScanReqRxdVsEvent(bool en); +void conn_stack_enableChanMapUpdCompVsEvent(bool en); +void sleep_stack_enableWakeupVsEvent(bool en); +#endif // (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + /* Local functions definition *************************************************************************** */ +#if (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) +void ble_stack_enableVsCmds(bool en) +{ + adv_stack_enableClearLegacyAdvVsCmd(en); + advFilter_stack_enableDupExcListVsCmd(en); + scan_stack_enableAdvFlowCtrlVsCmd(en); + arr_stack_enableMultiConnVsCmd(en); + pcl_stack_enableSetRssiThreshVsCmd(en); + chanSel_stack_enableSetCsaVsCmd(en); + log_stack_enableLogsRelatedVsCmd(en); + hci_stack_enableSetVsEvtMaskVsCmd(en); + winWiden_stack_enableSetConstPeerScaVsCmd(en); +} + +void ble_stack_enableVsEvents(bool en) +{ + adv_stack_enableScanReqRxdVsEvent(en); + conn_stack_enableChanMapUpdCompVsEvent(en); + +#if CONFIG_BT_LE_SLEEP_ENABLE + sleep_stack_enableWakeupVsEvent(en); +#endif // CONFIG_BT_LE_SLEEP_ENABLE +} +#endif // (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + int ble_stack_initEnv(void) { int rc; @@ -90,11 +132,21 @@ int ble_stack_enable(void) #endif // CONFIG_BT_LE_ERROR_SIM_ENABLED #endif // DEFAULT_BT_LE_MAX_CONNECTIONS +#if (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + ble_stack_enableVsCmds(true); + ble_stack_enableVsEvents(true); +#endif // (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + return 0; } void ble_stack_disable(void) { +#if (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + ble_stack_enableVsEvents(false); + ble_stack_enableVsCmds(false); +#endif // (CONFIG_BT_NIMBLE_ENABLED || CONFIG_BT_BLUEDROID_ENABLED) + #if DEFAULT_BT_LE_MAX_CONNECTIONS #if CONFIG_BT_LE_ERROR_SIM_ENABLED conn_errorSim_disable(); diff --git a/components/bt/controller/esp32h2/esp_bt_cfg.h b/components/bt/controller/esp32h2/esp_bt_cfg.h index 3d8edd55de..6621c28cb4 100644 --- a/components/bt/controller/esp32h2/esp_bt_cfg.h +++ b/components/bt/controller/esp32h2/esp_bt_cfg.h @@ -204,6 +204,12 @@ extern "C" { #define DEFAULT_BT_LE_VHCI_ENABLED (0) #endif +#ifdef CONFIG_BT_LE_PTR_CHECK_ENABLED +#define DEFAULT_BT_LE_PTR_CHECK_ENABLED (CONFIG_BT_LE_PTR_CHECK_ENABLED) +#else +#define DEFAULT_BT_LE_PTR_CHECK_ENABLED (0) +#endif + #ifdef CONFIG_BT_LE_SLEEP_ENABLE #define NIMBLE_SLEEP_ENABLE CONFIG_BT_LE_SLEEP_ENABLE #else diff --git a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib index 5577830961..246541ab93 160000 --- a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib +++ b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib @@ -1 +1 @@ -Subproject commit 55778309611becb63829929912df61d4ff74bb89 +Subproject commit 246541ab93e853eba1591784a5253b219c0414f9 diff --git a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib index e9d636c148..a3192174a9 160000 --- a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib +++ b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib @@ -1 +1 @@ -Subproject commit e9d636c148ffb31b9f28563b2674b35ef09f4293 +Subproject commit a3192174a9aef015a9ceca983acdb9fd1a198445 diff --git a/components/bt/include/esp32c6/include/esp_bt.h b/components/bt/include/esp32c6/include/esp_bt.h index 27b9fd16b3..e6e1dc4eba 100644 --- a/components/bt/include/esp32c6/include/esp_bt.h +++ b/components/bt/include/esp32c6/include/esp_bt.h @@ -223,6 +223,7 @@ typedef struct { - 0 - Disable (default) - 1 - Enable */ uint8_t vhci_enabled; /*!< VHCI mode is enabled */ + uint8_t ptr_check_enabled; /*!< Enable boundary check for internal memory. */ uint32_t config_magic; /*!< Magic number for configuration validation */ } esp_bt_controller_config_t; @@ -278,6 +279,7 @@ typedef struct { .ble_chan_ass_en = DEFAULT_BT_LE_CTRL_CHAN_ASS_EN, \ .ble_data_lenth_zero_aux = DEFAULT_BT_LE_CTRL_ADV_DATA_LENGTH_ZERO_AUX, \ .vhci_enabled = DEFAULT_BT_LE_VHCI_ENABLED, \ + .ptr_check_enabled = DEFAULT_BT_LE_PTR_CHECK_ENABLED, \ .config_magic = CONFIG_MAGIC, \ } diff --git a/components/bt/include/esp32h2/include/esp_bt.h b/components/bt/include/esp32h2/include/esp_bt.h index 9a7545b25c..6b412834c0 100644 --- a/components/bt/include/esp32h2/include/esp_bt.h +++ b/components/bt/include/esp32h2/include/esp_bt.h @@ -227,6 +227,7 @@ typedef struct { - 0 - Disable (default) - 1 - Enable */ uint8_t vhci_enabled; /*!< VHCI is enabled */ + uint8_t ptr_check_enabled; /*!< Enable boundary check for internal memory. */ uint32_t config_magic; /*!< Configuration magic value */ } esp_bt_controller_config_t; @@ -282,6 +283,7 @@ typedef struct { .ble_chan_ass_en = DEFAULT_BT_LE_CTRL_CHAN_ASS_EN, \ .ble_data_lenth_zero_aux = DEFAULT_BT_LE_CTRL_ADV_DATA_LENGTH_ZERO_AUX, \ .vhci_enabled = DEFAULT_BT_LE_VHCI_ENABLED, \ + .ptr_check_enabled = DEFAULT_BT_LE_PTR_CHECK_ENABLED, \ .config_magic = CONFIG_MAGIC, \ }