From d5325ac3aaade4eda90df889cb2451e1b8266984 Mon Sep 17 00:00:00 2001 From: andy <1414772332@qq.com> Date: Sat, 26 Oct 2024 14:10:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20ah=20fw=20=E5=9C=A8fw=20im?= =?UTF-8?q?g=20=E4=B9=8B=E5=90=8E=E8=AF=BB=E5=8F=96=E9=99=84=E5=8A=A0?= =?UTF-8?q?=E7=9A=84oem=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- driver/inc/board_info.h | 4 +++ driver/src/hw3/board_info_hw.c | 19 +++++++-------- inc/pib/iot_oem.h | 13 ++++++++++ mfgtool/ah/add_header_v1.c | 39 +++++++++++++++++++++++++++++- mfgtool/oem_tool/hw3/oem_tool_hw.c | 3 ++- pib/src/iot_oem.c | 32 ++++++++++++++++++++---- 6 files changed, 93 insertions(+), 17 deletions(-) diff --git a/driver/inc/board_info.h b/driver/inc/board_info.h index 97ec46d..96cd19a 100644 --- a/driver/inc/board_info.h +++ b/driver/inc/board_info.h @@ -20,6 +20,10 @@ Information is free from patent or copyright infringement. extern "C" { #endif + +#define BOARD_OEM_INFO_SIZE_MAX (8192) + + /** * @brief load oem configuration information from oem partition, * then set module type. diff --git a/driver/src/hw3/board_info_hw.c b/driver/src/hw3/board_info_hw.c index 2ade79d..caddf9c 100644 --- a/driver/src/hw3/board_info_hw.c +++ b/driver/src/hw3/board_info_hw.c @@ -387,14 +387,6 @@ void board_oem_signal_mtx_binding(iot_board_info_v1_t *rc_info) -// 抓取额外的差分oem数据 -static uint32_t board_load_oem_cfg_ext(uint8_t *buff,uint32_t buff_size) -{ - return 0; -} - - - @@ -416,6 +408,7 @@ uint32_t board_load_oem_cfg() uint8_t diff_idx_array[BOARD_RESOURCE_NUM_MAX] = {0}; uint32_t crc_cal; uint8_t gpio; + int read_oem_ext_flag=0; /* init cpu index value */ #if (BUILD_AMP_TYPE == IOT_BUILD_AMP_NONE) @@ -438,7 +431,7 @@ uint32_t board_load_oem_cfg() /* [TODO]: optimize the logic to look up only on the first startup */ iot_layout_get_part_size(PART_NUM_OEM, &oem_part_size); oem_diff_offset = sizeof(iot_oem_cfg_t) + IOT_OEM_SECTION_START_ADDR; - buf_oem = os_mem_malloc(IOT_DRIVER_MID, oem_part_size - oem_diff_offset); + buf_oem = os_mem_malloc(IOT_DRIVER_MID, BOARD_OEM_INFO_SIZE_MAX); if (buf_oem == NULL) { ret = ERR_FAIL; goto out; @@ -464,7 +457,13 @@ uint32_t board_load_oem_cfg() hdr_check: if (hw_ver_cal != hw_ver_rc) { if (ptr_hdr->have_next == 0) { - goto out; + if(read_oem_ext_flag || + iot_oem_read_mtd_ext(&buf_oem[offset],BOARD_OEM_INFO_SIZE_MAX-offset)){ + goto out; + } else { + ptr_hdr->have_next = 1; + read_oem_ext_flag = 1; + } } // 统计有多少个rc table cnt_rc++; diff --git a/inc/pib/iot_oem.h b/inc/pib/iot_oem.h index 846a545..5fa0072 100755 --- a/inc/pib/iot_oem.h +++ b/inc/pib/iot_oem.h @@ -48,6 +48,11 @@ extern "C" { #define IOT_OEM_SECTION_SEC_ADDR (3072) +#define IOT_OEM_EXT_SECTION_OFFSET(fw_size) \ + (((fw_size+31)&(~0x1f))+IOT_OEM_SECTION_START_ADDR) + +#define IOT_OEM_EXT_MAGIC_STR "_oem_ext" + #define IOT_OEM_DUMMY_LEN 106 /* use simple pair passcode for security check */ #define IOT_OEM_SEC_TYPE_PASSCODE 1 @@ -219,6 +224,14 @@ extern const uint8_t g_iot_sec_root_sm2_pub_v1[IOT_SEC_ROOT_SM2_PUB_LEN]; */ uint32_t iot_oem_read_mtd(uint8_t *buf, uint32_t size, uint32_t offset); +/** + * @brief iot_oem_read_mtd_ext() - read oem ext data from flash fw section. + * @param buf: pointer to receive oem data. + * @param size: length to receive oem data. + * @retval 0 success; others fail. + */ +uint32_t iot_oem_read_mtd_ext(uint8_t *buf, uint32_t size); + /** * @brief iot_oem_init - init oem function. * warning, iot_oem_init should be called after pib load diff --git a/mfgtool/ah/add_header_v1.c b/mfgtool/ah/add_header_v1.c index de6d016..a261881 100644 --- a/mfgtool/ah/add_header_v1.c +++ b/mfgtool/ah/add_header_v1.c @@ -5,7 +5,7 @@ #include #include #include -#include "sha256.h" +#include "../ram/inc/sha256.h" #include "iot_img_hdr.h" char default_hash_iv[]={0x12,0x34,0x56,0x78,0x87,0x65,0x43,0x21,0x5A,0x5A,0x5A,0x5A,0x00,0x7E,0xC3,0x00}; @@ -575,6 +575,38 @@ int ah_GetConfig(char *pcfgName, ah_cfg *pcfg) #define PACKAGE_HEADER_SIZE 96 + + +// 填充额外的oem数据 +int ah_add_oem_ext(char *buf) +{ + int file_size; + char *buf_start=buf; + FILE *fp = NULL; + struct stat st; + const char *file_name="oem_ext.bin"; + const char *oem_magic_str="_oem_ext"; + if((0 != stat(file_name, &st)) + ||(NULL == (fp=fopen(file_name, "r")))) + { + printf(NEW_LINE"Cannot open image file(%s)."NEW_LINE, file_name); + return 0; + } + memcpy(buf,oem_magic_str,strlen(oem_magic_str)); + buf += strlen(oem_magic_str); + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + fseek(fp, 0, SEEK_SET); + fread(buf, 1, file_size, fp); + buf += file_size; + fclose(fp); + fp = NULL; + return buf-buf_start; +} + + + + // 这里添加的是img的头 void ah_fillHeader(char* p_buf, ah_cfg *cfg, int cnt, int fileSize, int _CRC, unsigned char *sha) @@ -998,6 +1030,11 @@ int ah_funConstruct(char argc, char *argv[]) /*update buffer pointer*/ p_buf += len; + // 在这里添加 oem_ext.bin + if(cfg.image[cnt].image_type == imgV1FRWPLC){ + p_buf+=ah_add_oem_ext(p_buf); + } + /*new bin file's size*/ len = p_buf - p_saved_buf; total_size += len; diff --git a/mfgtool/oem_tool/hw3/oem_tool_hw.c b/mfgtool/oem_tool/hw3/oem_tool_hw.c index 04feb33..41adef9 100644 --- a/mfgtool/oem_tool/hw3/oem_tool_hw.c +++ b/mfgtool/oem_tool/hw3/oem_tool_hw.c @@ -8,8 +8,9 @@ #include "../../../inc/hw/reg/riscv3/2/gpio_mtx_sig.h" -#include "oem_common.h" +#include "../oem_common.h" #include "oem_tool_hw.h" +#include "iot_oem_struct_v1.h" #define RC_TOTAL_GPIO 128 #define RC_TOTAL_UART IOT_UART_MAX_NUM_V1 diff --git a/pib/src/iot_oem.c b/pib/src/iot_oem.c index 3deb682..d5d0677 100644 --- a/pib/src/iot_oem.c +++ b/pib/src/iot_oem.c @@ -31,6 +31,7 @@ Information is free from patent or copyright infringement. #include "iot_uart_api.h" #include "iot_crc_api.h" #include "iot_crypto_dsa_api.h" +#include "iot_string.h" /** * user type for iot @@ -110,14 +111,25 @@ uint32_t iot_oem_read_mtd(uint8_t *buf, uint32_t size, uint32_t offset) // 抓取额外的差分oem数据 -uint32_t iot_oem_read_mtd_ext(uint8_t *buff,size_t buff_size) +uint32_t iot_oem_read_mtd_ext(uint8_t *buff,uint32_t buff_size) { - int fd; - int ret; + int fd = 0; int offset; int status; - int fw_prtition=PART_NUM_FW1; + int fw_size; + uint32_t ret = ERR_FAIL; + uint8_t fw_prtition = PART_NUM_FW1; + char str_buff[10]={0}; + const char *oem_ext_magic_str = IOT_OEM_EXT_MAGIC_STR; + int oem_ext_magic_str_len = strlen(oem_ext_magic_str); + do { + status = dev_get_boot_fw_part_num(&fw_prtition); + if (status) { + break; + } + fw_size=mtd_get_hdr_img_size(fw_prtition); + offset=IOT_OEM_EXT_SECTION_OFFSET(fw_size); fd = dev_open(fw_prtition, 0); if (fd < 0) { fd = 0; @@ -127,6 +139,17 @@ uint32_t iot_oem_read_mtd_ext(uint8_t *buff,size_t buff_size) if (status < 0) { break; } + status = dev_read(fd, buff, oem_ext_magic_str_len); + if (status < 0) { + break; + } + if(iot_strcmp(str_buff,oem_ext_magic_str) != 0){ + break; + } + status = dev_seek(fd, offset+oem_ext_magic_str_len, DEV_SEEK_SET); + if (status < 0) { + break; + } status = dev_read(fd, buff, buff_size); if (status < 0) { break; @@ -137,7 +160,6 @@ uint32_t iot_oem_read_mtd_ext(uint8_t *buff,size_t buff_size) dev_close(fd); } return ret; - }