From 2cc839982fd3cf38e8914626d84ac2b62f989b98 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E5=BD=AD=E7=A3=8A?= <1624952103@qq.com> Date: Tue, 16 Jul 2024 08:59:57 +0000 Subject: [PATCH] Merge #529 into sdk602patch0626 from refs/changes/529/head MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 合并cywee新固件,添加获取cywee激活状态接口 * refs/changes/529/head: (1 commits squashed) - 合并cywee新固件,添加获取cywee激活状态接口 Signed-off-by: aliyun6865282430 <1624952103@qq.com> Reviewed-by: snickersun@163.com Merged-by: snickersun@163.com CR-link: https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230/change/529 --- .../peripheral/cywee/Inc/custom_factory.h | 13 +- .../drivers/peripheral/cywee/Inc/cywee_hal.h | 1 + .../cywee/src/custom_factory_core.c | 202 +++++++++++++----- .../cywee/src/custom_factory_impl.c | 4 +- .../drivers/peripheral/cywee/src/main_dml.c | 10 + .../drivers/peripheral/utils/custom_utils.c | 5 + .../drivers/peripheral/utils/custom_utils.h | 1 + .../peripheral/utils/custom_utils_at_cmd.c | 15 +- 8 files changed, 188 insertions(+), 63 deletions(-) diff --git a/code/sdk/drivers/peripheral/cywee/Inc/custom_factory.h b/code/sdk/drivers/peripheral/cywee/Inc/custom_factory.h index fe7749c9b..647249ac9 100644 --- a/code/sdk/drivers/peripheral/cywee/Inc/custom_factory.h +++ b/code/sdk/drivers/peripheral/cywee/Inc/custom_factory.h @@ -26,7 +26,7 @@ typedef struct { typedef void (*FN_SENSOR_LISTEN)(pSensorEVT_t); -//void CWM_LibPostInit(FN_SENSOR_LISTEN pfn); +void CWM_LibPostInit(FN_SENSOR_LISTEN pfn); #endif //__CWM_LIB_H__ @@ -34,15 +34,20 @@ typedef void (*FN_SENSOR_LISTEN)(pSensorEVT_t); typedef struct { int sec_write_len; - int sec_empty_value; - int sec_rewritable; + int sec_bytes_per_write; + int sec_blank_value; + int sec_erasable; + int force_write_even_dirty; char *chip_name; + int (*p_func_write_ex)(unsigned int offset, unsigned char *buf, unsigned int len); } reburn_prepare_t; typedef struct { void (*log_print)(const char* str); } print_api_t; +void reburn_setup_api(print_api_t *api); + int secFactory_init(int rand_seed); int secFactory_make_sec(char version, char *code, int *pCode); int secFactory_make_chipInfo(char *strChipInfo); @@ -60,4 +65,4 @@ int impl_sec_read(unsigned char *buf, unsigned int len); int impl_sec_erase(void); int impl_sec_write(unsigned char *buf, unsigned int len); -#endif //__CM_SIMPLE_FACTORY_H__ +#endif //__CUSTOM_FACTORY_H__ diff --git a/code/sdk/drivers/peripheral/cywee/Inc/cywee_hal.h b/code/sdk/drivers/peripheral/cywee/Inc/cywee_hal.h index 69529db30..f9538fa62 100644 --- a/code/sdk/drivers/peripheral/cywee/Inc/cywee_hal.h +++ b/code/sdk/drivers/peripheral/cywee/Inc/cywee_hal.h @@ -557,6 +557,7 @@ void cywee_set_sedentary_interval(int time); void cywee_set_sedentary_switch(int cmd); void cywee_get_acc_switch(int cmd); void cywee_stop(void); +uint8_t cywee_get_active_state(void); extern uint8_t hrs_mode_state; diff --git a/code/sdk/drivers/peripheral/cywee/src/custom_factory_core.c b/code/sdk/drivers/peripheral/cywee/src/custom_factory_core.c index 480aae82e..67558ba3d 100644 --- a/code/sdk/drivers/peripheral/cywee/src/custom_factory_core.c +++ b/code/sdk/drivers/peripheral/cywee/src/custom_factory_core.c @@ -42,17 +42,6 @@ void reburn_log_printf(const char* fmt, ...) { m_reburn_print_api.log_print((const char*)localBuf); } -static int is_sec_empty(uint8_t* buf, uint32_t len) { - for (int i = 0 ; i < len ; i++) { - if (buf[i] != m_var.sec_empty_value) { - printf("is_sec_empty(): block %d not empty(0x%02x)\n", i, buf[i]); - return 0; - } - } - - return 1; -} - static void dump_sec(uint8_t* buf, uint32_t len) { uint32_t offset = 0, remainBytes = len; @@ -60,7 +49,7 @@ static void dump_sec(uint8_t* buf, uint32_t len) { return; while (remainBytes >= 16) { - printf("%08x | %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", offset, + reburn_log_printf("%08x | %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", offset, *(buf + (offset) + 0 ), *(buf + (offset) + 1 ), *(buf + (offset) + 2 ), *(buf + (offset) + 3 ), *(buf + (offset) + 4 ), *(buf + (offset) + 5 ), *(buf + (offset) + 6 ), *(buf + (offset) + 7 ), *(buf + (offset) + 8 ), *(buf + (offset) + 9 ), *(buf + (offset) + 10), *(buf + (offset) + 11), @@ -70,18 +59,123 @@ static void dump_sec(uint8_t* buf, uint32_t len) { } } +static int is_block_blank(uint8_t* buf, uint32_t len, int show_msg) { + dump_sec(buf, len); + + for (int i = 0 ; i < len ; i++) { + if (buf[i] != m_var.sec_blank_value) { + if (show_msg) + reburn_log_printf("[is_block_blank] block %d not empty(0x%02x)\n", i, buf[i]); + return 0; + } + } + + return 1; +} + +static int write_sec(unsigned int sec_is_blank, unsigned char *p_rbuf, unsigned char *p_wbuf, int len) { + int ret; + int total = m_var.sec_write_len; + int bytes_per_write = m_var.sec_bytes_per_write; + unsigned char blank_val = m_var.sec_blank_value; + + if (sec_is_blank) { + reburn_log_printf("[write_sec] mode:write all\n"); + + if (m_var.p_func_write_ex) { + ret = m_var.p_func_write_ex(0, p_wbuf, total); + if (ret != 1) { + reburn_log_printf("[write_sec] write_ex failed(%d)\n", ret); + return -1; + } + } else { + ret = impl_sec_write(p_wbuf, total); + if ( ret != 1) { + reburn_log_printf("[write_sec] write failed(%d)\n", ret); + return -2; + } + } + } else { + reburn_log_printf("[write_sec] mode:%d byte \n", bytes_per_write); + + if (m_var.p_func_write_ex && bytes_per_write > 0) { + } else { + reburn_log_printf("[write_sec] error, not support partial write\n"); + return -3; + } + + for (int i = 0; i < total / bytes_per_write; i ++) { + int offset, block_blank, block_the_same, do_write; + + offset = (i * bytes_per_write); + block_blank = is_block_blank(p_rbuf + offset, bytes_per_write, 0); + do_write = 0; + + if (memcmp(p_rbuf + offset, p_wbuf + offset, bytes_per_write) == 0) + block_the_same = 1; + else + block_the_same = 0; + + if (block_the_same == 0 && block_blank == 0 && m_var.force_write_even_dirty == 0) { + reburn_log_printf("[write_sec] error, force_write_even_dirty=0\n", *(p_rbuf + offset), blank_val); + return -4; + } + + if (block_the_same == 0) { + if (block_blank == 1 || m_var.force_write_even_dirty == 1) + do_write = 1; + } + + if (do_write) { + ret = m_var.p_func_write_ex(offset, p_wbuf + offset, bytes_per_write); + if (ret != 1) { + reburn_log_printf("[write_sec] write_ex failed(%d)\n", ret); + return -5; + } + } + } + } + + return 1; +} + +int erase_and_confirm(unsigned char *pbuf, unsigned int len) { + int ret; + + ret = impl_sec_erase(); + if (ret != 1) { + reburn_log_printf("[erase_and_confirm] erase error(%d)\n", ret); + return -1; + } + + memset(pbuf, 0x0, len); + ret = impl_sec_read(pbuf, len); + if (ret != 1) { + reburn_log_printf("[erase_and_confirm] read error(%d)\n", ret); + return -2; + } + + if (!is_block_blank(pbuf, len, 1)) { + reburn_log_printf("[erase_and_confirm] block is not cleaned\n"); + return -3; + } + + return 1; +} + int reburn_second_dev(void) { SettingControl_t scl; char str_chipInfo[48]; - unsigned char buf[256]; - char EncodeData[512]; - int len = 0, ret = 0; + unsigned char sec_from_device[512]; + unsigned char sec_from_lib[512]; + int len = 0, ret = 0, sec_is_blank = 0; impl_prepare(&m_var); - printf("[reburn_second_dev] Built at %s %s\n", __DATE__, __TIME__); + + reburn_log_printf("[reburn_second_dev] Built at %s %s\n", __DATE__, __TIME__); if (!impl_checkMcuModel()) { - printf("cannot reburn: wrong model \r\n"); + reburn_log_printf("cannot reburn: wrong model\n"); return STS_ERR_WRONG_MODEL; } @@ -89,24 +183,19 @@ int reburn_second_dev(void) { scl.iData[0] = 1; CWM_SettingControl(SCL_GET_LIB_INFO, &scl); if (scl.iData[5] < 3000) { - printf("%d Not a valid reburn library\n", scl.iData[5]); + reburn_log_printf("cannot reburn: %d not a valid reburn library\n", scl.iData[5]); return STS_ERR_INVALID_LIB; } - printf("model:%s version:%d.%d.%d.%d product:%d\n", m_var.chip_name, scl.iData[1], scl.iData[2], scl.iData[3], scl.iData[4], scl.iData[5]); + reburn_log_printf("model:%s version:%d.%d.%d.%d product:%d\n", m_var.chip_name, scl.iData[1], scl.iData[2], scl.iData[3], scl.iData[4], scl.iData[5]); impl_sec_env_init(); impl_stick_env_init(); - // uint8_t buffer[80] = {0}; - // uapi_efuse_read_buffer(buffer, 0x100, 80); - // printf("reburn buffer : "); - // for(int i = 0 ; i < 80 ; i++) - // printf("%d",buffer[i]); - // printf("\n"); + CWM_LibPostInit(NULL); if (secFactory_init(0) != 1) { - printf("secFactory_init fail\n"); + reburn_log_printf("cannot reburn: secFactory_init fail\n"); return STS_ERR_SEC_INIT; } @@ -123,63 +212,64 @@ int reburn_second_dev(void) { scl.iData[5] = 0; scl.iData[6] = 0; CWM_SettingControl(SCL_GET_CHIP_INFO, &scl); - printf("have_security = %d.%d ret_buff_size = %d\n", scl.iData[5], scl.iData[6], scl.iData[4]); - printf("[getinfo]chipInfo=%s\n", str_chipInfo); + reburn_log_printf("have_security = %d.%d ret_buff_size = %d\n", scl.iData[5], scl.iData[6], scl.iData[4]); + reburn_log_printf("[getinfo]chipInfo=%s\n", str_chipInfo); if (scl.iData[5] == 1) { - printf("valid security found\n"); + reburn_log_printf("valid security found\n"); return STS_OK; } - memset(buf, 0x0, sizeof(buf)); - ret = impl_sec_read(buf, m_var.sec_write_len); - if (ret != 1) { - printf("cannot reburn: read error(%d)\n", ret); - return STS_ERR_SEC_READ_FAIL; - } - dump_sec(buf, m_var.sec_write_len); - if (impl_sec_is_locked()) { - printf("cannot reburn: locked\n"); + reburn_log_printf("cannot reburn: locked\n"); return STS_ERR_MEDIUM_LOCKED; } - if (!is_sec_empty(buf, m_var.sec_write_len)) { - if (!m_var.sec_rewritable) { - printf("cannot reburn: block not empty\n"); - return STS_ERR_BLOCK_NOT_EMPTY; - } - ret = impl_sec_erase(); - if (ret != 1) { - printf("cannot reburn: erase error(%d)\n", ret); - return STS_ERR_ERASE_FAILED; + memset(sec_from_device, 0x0, sizeof(sec_from_device)); + ret = impl_sec_read(sec_from_device, m_var.sec_write_len); + if (ret != 1) { + reburn_log_printf("cannot reburn: read error(%d)\n", ret); + return STS_ERR_SEC_READ_FAIL; + } + + if (is_block_blank(sec_from_device, m_var.sec_write_len, 1)) { + sec_is_blank = 1; + } else { + if (m_var.sec_erasable) { + ret = erase_and_confirm(sec_from_device, m_var.sec_write_len); + if (ret != 1) { + reburn_log_printf("cannot reburn: erase not success (%d)\n", ret); + return STS_ERR_ERASE_FAILED; + } else { + sec_is_blank = 1; + } } } if (secFactory_make_chipInfo(str_chipInfo) != 1) { - printf("secFactory_make_chipInfo fail\n"); + reburn_log_printf("secFactory_make_chipInfo fail\n"); return STS_ERR_SEC_MAKE_CHIPINFO; } - printf("[makesec]chipInfo=%s\n", str_chipInfo); + reburn_log_printf("[makesec]chipInfo=%s\n", str_chipInfo); - memset(EncodeData, 0, sizeof(EncodeData)); - ret = secFactory_make_sec(0x01, EncodeData, &len); + memset(sec_from_lib, 0, sizeof(sec_from_lib)); + ret = secFactory_make_sec(0x01, (char*)sec_from_lib, &len); if (ret != 1) { - printf("secFactory_make_sec fail\n"); + reburn_log_printf("secFactory_make_sec fail\n"); return STS_ERR_SEC_MAKE_SEC; } if (len < m_var.sec_write_len) { - printf("sec length error(%d/%d)\n", len, m_var.sec_write_len); + reburn_log_printf("sec length error(%d/%d)\n", len, m_var.sec_write_len); return STS_ERR_SEC_WRITE_LEN; } - ret = impl_sec_write((uint8_t*)&EncodeData[0], len); + ret = write_sec(sec_is_blank, sec_from_device, sec_from_lib, m_var.sec_write_len); if (ret != 1) { - printf("cannot reburn: write error(%d)\n", ret); + reburn_log_printf("cannot reburn: write error(%d)\n", ret); return STS_ERR_SEC_WRITE; } - printf("reburn complete\n"); + reburn_log_printf("reburn complete\n"); return STS_OK; } diff --git a/code/sdk/drivers/peripheral/cywee/src/custom_factory_impl.c b/code/sdk/drivers/peripheral/cywee/src/custom_factory_impl.c index 0e88bc3b3..d8c5d8cc9 100644 --- a/code/sdk/drivers/peripheral/cywee/src/custom_factory_impl.c +++ b/code/sdk/drivers/peripheral/cywee/src/custom_factory_impl.c @@ -14,8 +14,8 @@ extern int cm__hs1__deinit(void); static reburn_prepare_t m_chipCfg = { .sec_write_len = 16, - .sec_empty_value = 0x00, - .sec_rewritable = 0, + .sec_blank_value = 0x00, + .sec_erasable = 0, .chip_name = "YH601S" }; diff --git a/code/sdk/drivers/peripheral/cywee/src/main_dml.c b/code/sdk/drivers/peripheral/cywee/src/main_dml.c index c0bf8f0b6..cbdea312d 100644 --- a/code/sdk/drivers/peripheral/cywee/src/main_dml.c +++ b/code/sdk/drivers/peripheral/cywee/src/main_dml.c @@ -72,10 +72,16 @@ static int cywee_switch = 1; #else static int cywee_switch = 0; #endif +static uint8_t cywee_is_active = 0; int cm__hs1__init(void); int cm__hs1__deinit(void); +uint8_t cywee_get_active_state(void) +{ + return cywee_is_active; +} + void cywee_stop(void) { cywee_switch = 0; @@ -990,6 +996,10 @@ int cywee_main_dml(void) CWM_OS_dbgPrintf("have_security = %d.%d ret_buff_size = %d chipInfo = %s\n", scl.iData[5], scl.iData[6], scl.iData[4], chipInfo); CWM_OS_dbgPrintf("chip_settings = %d, %d, %d\n", scl.iData[9], scl.iData[10], scl.iData[11]); //CWM_OS_dbgPrintf("[DBG] %s %d\n", __func__,__LINE__); + if(scl.iData[5] == 1 && scl.iData[6] == 31010100) + { + cywee_is_active = 1; + } CWM_Dml_LibInit(); //CWM_OS_dbgPrintf("[DBG] %s %d\n", __func__,__LINE__); diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.c b/code/sdk/drivers/peripheral/utils/custom_utils.c index 1efe4b21c..1a6769c14 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.c +++ b/code/sdk/drivers/peripheral/utils/custom_utils.c @@ -2347,3 +2347,8 @@ void uapi_gei_btmac_and_chipinfo(void) wstp_print("^^^^ BTMAC=%s ^^^^ CyweeChipInfo=%s\n",btaddr_str,chipinfo); } + +uint8_t uapi_get_cywee_active_state(void) +{ + return cywee_get_active_state(); +} diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.h b/code/sdk/drivers/peripheral/utils/custom_utils.h index 47f82bb21..86f1b74da 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.h +++ b/code/sdk/drivers/peripheral/utils/custom_utils.h @@ -233,6 +233,7 @@ void uapi_factorytest_hide_test_tp_button(void); void uapi_efuse_test_write(uint8_t *data,uint32_t byte_number,uint8_t len); void uapi_efuse_test_read(uint32_t byte_number,uint8_t len); void uapi_gei_btmac_and_chipinfo(void); +uint8_t uapi_get_cywee_active_state(void); #include "custom_utilscpp.h" diff --git a/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c b/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c index ab31c6011..90ba3fb1d 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c +++ b/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c @@ -782,7 +782,19 @@ static uint32_t custom_lsfd(uint8_t *para, uint32_t paraLen, char *strCommonCmd) lsfd(); return ERRCODE_SUCC; } - + +static uint32_t custom_get_cywee_active_state(uint8_t *para, uint32_t paraLen, char *strCommonCmd) +{ + UNUSED(strCommonCmd); + + uint8_t ret = uapi_get_cywee_active_state(); + if(ret == 1) + wstp_print("cywee is active !!!\n"); + else + wstp_print("cywee is not active !!!\n"); + return ERRCODE_SUCC; +} + static const at_cmd_table_t g_custom_utils_cmd[] = { { (char *)"AT^CUSTOM_SN", custom_utils_setsn, (char *)"123456789ABCDEF2345678" }, { (char *)"AT^CUSTOM_SN_REQ", custom_utils_getsn, (char *)" " }, @@ -841,6 +853,7 @@ static const at_cmd_table_t g_custom_utils_cmd[] = { {(char *)"AT^GETMAC", custom_get_bt_addr, (char *)" "}, {(char *)"AT^GETIMEI", custom_get_imei, (char *)" "}, {(char *)"AT^GETCHIPINFO", custom_get_cywee_chip_info, (char *)" "}, + {(char *)"AT^GETCYWEEACTIVE", custom_get_cywee_active_state, (char *)" "}, }; -- 2.17.1