diff --git a/examples/boot2_iap/CMakeLists.txt b/examples/boot2_iap/CMakeLists.txt
index 958a842c..59b0f58e 100644
--- a/examples/boot2_iap/CMakeLists.txt
+++ b/examples/boot2_iap/CMakeLists.txt
@@ -1,9 +1,23 @@
-set(TARGET_REQUIRED_LIBS xz)
+if((${CHIP} STREQUAL "bl702"))
+ set(TARGET_REQUIRED_LIBS xz usb_stack)
+ set(BSP_COMMON_DIR ${CMAKE_SOURCE_DIR}/bsp/bsp_common)
+ set(TARGET_REQUIRED_PRIVATE_INCLUDE ${BSP_COMMON_DIR}/usb)
+elseif ((${CHIP} STREQUAL "bl602"))
+ set(TARGET_REQUIRED_LIBS xz)
+endif()
+
+
list(APPEND TARGET_REQUIRED_SRCS blsp_common.c blsp_media_boot.c )
list(APPEND TARGET_REQUIRED_SRCS blsp_boot_parser.c blsp_boot_decompress.c blsp_port.c )
list(APPEND TARGET_REQUIRED_SRCS bflb_eflash_loader_uart.c ) #bflb_eflash_loader_gpio.c
list(APPEND TARGET_REQUIRED_SRCS bflb_eflash_loader_cmds.c )
+
+list(APPEND TARGET_REQUIRED_SRCS bflb_eflash_loader_interface.c )
+if((${CHIP} STREQUAL "bl702"))
+ list(APPEND TARGET_REQUIRED_SRCS bflb_eflash_loader_usb.c )
+ list(APPEND TARGET_REQUIRED_SRCS ${BSP_COMMON_DIR}/usb/usb_dc.c ${BSP_COMMON_DIR}/usb/uart_interface.c )
+endif()
SET(LINKER_SCRIPT ${BOOT2_LINKER_SCRIPT})
diff --git a/examples/boot2_iap/bflb_eflash_loader.h b/examples/boot2_iap/bflb_eflash_loader.h
index b8b642ec..d2acd2cc 100644
--- a/examples/boot2_iap/bflb_eflash_loader.h
+++ b/examples/boot2_iap/bflb_eflash_loader.h
@@ -36,6 +36,11 @@
#ifndef __BFLB_EFLASH_LOADER_H__
#define __BFLB_EFLASH_LOADER_H__
+#include "bflb_eflash_loader_interface.h"
+#include "bflb_eflash_loader_cmds.h"
+#include "bflb_eflash_loader_uart.h"
+#include "bflb_eflash_loader_usb.h"
+
/*error code definition*/
typedef enum tag_eflash_loader_error_code_t {
BFLB_EFLASH_LOADER_SUCCESS = 0x00,
@@ -129,11 +134,7 @@ typedef enum tag_eflash_loader_error_code_t {
#define OFFSET(TYPE, MEMBER) ((uint32_t)(&(((TYPE *)0)->MEMBER)))
-/*read data buffer from flash or boot interface*/
-extern volatile uint32_t g_rx_buf_index;
-extern volatile uint32_t g_rx_buf_len;
-extern uint32_t g_eflash_loader_readbuf[2][(BFLB_EFLASH_LOADER_READBUF_SIZE + 3) / 4];
-extern uint32_t g_eflash_loader_cmd_ack_buf[16];
+
uint8_t bootrom_read_boot_mode(void);
void bflb_eflash_loader_init_uart_gpio(uint8_t eflash_loader_uart_pin_select);
diff --git a/examples/boot2_iap/bflb_eflash_loader_cmds.c b/examples/boot2_iap/bflb_eflash_loader_cmds.c
index e521253d..93a49045 100644
--- a/examples/boot2_iap/bflb_eflash_loader_cmds.c
+++ b/examples/boot2_iap/bflb_eflash_loader_cmds.c
@@ -1,690 +1,709 @@
-/**
- ******************************************************************************
- * @file blsp_eflash_loader_cmds.c
- * @version V1.2
- * @date
- * @brief This file is the peripheral case header file
- ******************************************************************************
- * @attention
- *
- *
© COPYRIGHT(c) 2018 Bouffalo Lab
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of Bouffalo Lab nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
-#include "bflb_eflash_loader_cmds.h"
-#include "bflb_eflash_loader.h"
-#include "bflb_eflash_loader_uart.h"
-#include "stdio.h"
-#include "stdint.h"
-#include "string.h"
-#include "softcrc.h"
-#include "bflb_platform.h"
-#include "partition.h"
-#include "hal_flash.h"
-#include "hal_sec_hash.h"
-#include "blsp_media_boot.h"
-
-/*for mass read comamnd(flash read and efuse read) is only valid for UART
- this is due to ack buffer is g_eflash_loader_readbuf */
-#define eflash_loader_cmd_mass_ack_buf g_eflash_loader_readbuf[1]
-#define BFLB_EFLASH_LOADER_CHECK_LEN 2048
-#define BFLB_EFLASH_MAX_SIZE 2 * 1024 * 1024
-
-/*add for verify using SHA-256*/
-uint32_t g_sha_tmp_buf[16] = { 0 };
-uint32_t g_padding[16] = { 0 };
-uint32_t g_sha_in_buf[(BFLB_EFLASH_LOADER_READBUF_SIZE + 3) / 4] = { 0 };
-static uint32_t g_eflash_loader_error = 0;
-static struct image_cfg_t image_cfg;
-static struct bootrom_img_ctrl_t img_ctrl;
-static struct segment_header_t segment_hdr;
-
-uint32_t eflash_loader_cmd_ack_buf[16];
-extern struct device *download_uart;
-extern struct device *dev_check_hash;
-
-/*for bl602*/
-static int32_t bflb_bootrom_cmd_get_bootinfo(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_bootrom_cmd_load_bootheader(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_bootrom_cmd_load_segheader(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_bootrom_cmd_load_segdata(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_bootrom_cmd_check_img(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_bootrom_cmd_run(uint16_t cmd, uint8_t *data, uint16_t len);
-static void bflb_bootrom_cmd_ack(uint32_t result);
-
-/* for bl702 */
-static int32_t bflb_eflash_loader_cmd_read_jedec_id(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_reset(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_erase_flash(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_write_flash(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_read_flash(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_readSha_flash(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_xip_readSha_flash(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_write_flash_check(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_set_flash_para(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_xip_read_flash_start(uint16_t cmd, uint8_t *data, uint16_t len);
-static int32_t bflb_eflash_loader_cmd_xip_read_flash_finish(uint16_t cmd, uint8_t *data, uint16_t len);
-
-static const struct eflash_loader_cmd_cfg_t eflash_loader_cmds[] = {
- /*for bl602*/
- { BFLB_EFLASH_LOADER_CMD_GET_BOOTINFO, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_get_bootinfo },
- { BFLB_EFLASH_LOADER_CMD_LOAD_BOOTHEADER, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_load_bootheader },
- { BFLB_EFLASH_LOADER_CMD_LOAD_SEGHEADER, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_load_segheader },
- { BFLB_EFLASH_LOADER_CMD_LOAD_SEGDATA, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_load_segdata },
- { BFLB_EFLASH_LOADER_CMD_CHECK_IMG, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_check_img },
- { BFLB_EFLASH_LOADER_CMD_RUN, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_run },
-
- /* for bl702 */
- { BFLB_EFLASH_LOADER_CMD_RESET, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_reset },
- { BFLB_EFLASH_LOADER_CMD_FLASH_ERASE, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_erase_flash },
- { BFLB_EFLASH_LOADER_CMD_FLASH_WRITE, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_write_flash },
- { BFLB_EFLASH_LOADER_CMD_FLASH_READ, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_read_flash },
- { BFLB_EFLASH_LOADER_CMD_FLASH_WRITE_CHECK, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_write_flash_check },
- { BFLB_EFLASH_LOADER_CMD_FLASH_SET_PARA, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_set_flash_para },
- { BFLB_EFLASH_LOADER_CMD_FLASH_READSHA, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_readSha_flash },
- { BFLB_EFLASH_LOADER_CMD_FLASH_XIP_READSHA, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_xip_readSha_flash },
- { BFLB_EFLASH_LOADER_CMD_XIP_READ_START, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_xip_read_flash_start },
- { BFLB_EFLASH_LOADER_CMD_XIP_READ_FINISH, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_xip_read_flash_finish },
- { BFLB_EFLASH_LOADER_CMD_FLASH_READ_JEDECID, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_read_jedec_id },
-};
-
-/* ack host with command process result */
-static void bflb_bootrom_cmd_ack(uint32_t result)
-{
- if (result == 0) {
- /*OK*/
- eflash_loader_cmd_ack_buf[0] = BFLB_BOOTROM_CMD_ACK;
- bflb_eflash_loader_uart_send((uint32_t *)eflash_loader_cmd_ack_buf, 2);
- return;
- } else {
- /* FL+Error code(2bytes) */
- eflash_loader_cmd_ack_buf[0] = BFLB_BOOTROM_CMD_NACK | ((result << 16) & 0xffff0000);
- bflb_eflash_loader_uart_send(eflash_loader_cmd_ack_buf, 4);
- }
-}
-
-/* for bl602 eflash loader */
-static int32_t bflb_bootrom_cmd_get_bootinfo(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- /*OK(2)+len(2)+bootrom version(4)+OTP(16)*/
- uint8_t *bootinfo = (uint8_t *)eflash_loader_cmd_ack_buf;
- uint8_t otp_cfg[16] = { 0 };
-
- MSG("get bootinfo\r\n");
-
- eflash_loader_cmd_ack_buf[0] = BFLB_BOOTROM_CMD_ACK;
- bootinfo[2] = 0x14;
- bootinfo[3] = 00;
- *((uint32_t *)(bootinfo + 4)) = BFLB_BOOTROM_VERSION;
- memcpy(bootinfo + 8, &otp_cfg, 16);
-
- bflb_eflash_loader_uart_send(eflash_loader_cmd_ack_buf, bootinfo[2] + 4);
-
- return BFLB_EFLASH_LOADER_SUCCESS;
-}
-
-int32_t bflb_bootrom_parse_bootheader(uint8_t *data)
-{
- struct bootheader_t *header = (struct bootheader_t *)data;
- uint32_t crc;
- uint32_t crcpass = 0;
-
- if (header->bootcfg.bval.crc_ignore == 1 && header->crc32 == BOOTROM_DEADBEEF_VAL) {
- crcpass = 1;
- } else {
- crc = BFLB_Soft_CRC32((uint8_t *)header, sizeof(struct bootheader_t) - sizeof(header->crc32));
- if (header->crc32 == crc) {
- crcpass = 1;
- }
- }
-
- if (crcpass) {
- if (header->bootcfg.bval.notload_in_bootrom) {
- return BFLB_EFLASH_LOADER_IMG_BOOTHEADER_NOT_LOAD_ERROR;
- }
-
- /* for boot2, one CPU only */
- /*get which CPU's img it is*/
-
- if (0 == memcmp((void *)&header->magiccode, "BFNP", sizeof(header->magiccode))) {
- //current_cpu=i;
- img_ctrl.pkhash_src = 0; //boot_cpu_cfg[i].pkhash_src;
- } else {
- return BFLB_EFLASH_LOADER_IMG_BOOTHEADER_MAGIC_ERROR;
- }
-
- image_cfg.entrypoint = 0;
- /* set image valid 0 as default */
- image_cfg.img_valid = 0;
-
- /* deal with pll config */
- //bflb_bootrom_clk_set_from_user(&header->clkCfg,1);
-
- /* encrypt and sign */
- image_cfg.encrypt_type = header->bootcfg.bval.encrypt_type;
- image_cfg.sign_type = header->bootcfg.bval.sign;
- image_cfg.key_sel = header->bootcfg.bval.key_sel;
-
- /* xip relative */
- image_cfg.no_segment = header->bootcfg.bval.no_segment;
- image_cfg.cache_select = header->bootcfg.bval.cache_select;
- image_cfg.aes_region_lock = header->bootcfg.bval.aes_region_lock;
- image_cfg.halt_ap = header->bootcfg.bval.halt_ap;
- image_cfg.cache_way_disable = header->bootcfg.bval.cache_way_disable;
- image_cfg.hash_ignore = header->bootcfg.bval.hash_ignore;
-
- /* firmware len*/
- image_cfg.img_segment_info.segment_cnt = header->img_segment_info.segment_cnt;
-
- /*boot entry and flash offset */
- image_cfg.entrypoint = header->bootentry;
- image_cfg.img_start.flashoffset = header->img_start.flashoffset;
-
- if (image_cfg.img_segment_info.segment_cnt == 0) {
- return BFLB_EFLASH_LOADER_IMG_SEGMENT_CNT_ERROR;
- }
- /*clear segment_cnt*/
- img_ctrl.segment_cnt = 0;
-
- } else {
- return BFLB_EFLASH_LOADER_IMG_BOOTHEADER_CRC_ERROR;
- }
-
- return BFLB_EFLASH_LOADER_SUCCESS;
-}
-
-static int32_t bflb_bootrom_cmd_load_bootheader(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
-
- MSG("load bootheader\r\n");
-
- if (len != sizeof(struct bootheader_t)) {
- ret = BFLB_EFLASH_LOADER_IMG_BOOTHEADER_LEN_ERROR;
- } else {
- ret = bflb_bootrom_parse_bootheader(data);
-
- if (BFLB_EFLASH_LOADER_SUCCESS == ret) {
- if (image_cfg.sign_type) {
- img_ctrl.state = BOOTROM_IMG_PK;
- } else if (image_cfg.encrypt_type) {
- img_ctrl.state = BOOTROM_IMG_AESIV;
- } else {
- img_ctrl.state = BOOTROM_IMG_SEGHEADER;
+/**
+ ******************************************************************************
+ * @file blsp_eflash_loader_cmds.c
+ * @version V1.2
+ * @date
+ * @brief This file is the peripheral case header file
+ ******************************************************************************
+ * @attention
+ *
+ * © COPYRIGHT(c) 2018 Bouffalo Lab
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Bouffalo Lab nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+#include "bflb_eflash_loader.h"
+#include "stdio.h"
+#include "stdint.h"
+#include "string.h"
+#include "softcrc.h"
+#include "bflb_platform.h"
+#include "partition.h"
+#include "hal_flash.h"
+#include "hal_sec_hash.h"
+#include "blsp_media_boot.h"
+
+#define BFLB_EFLASH_LOADER_CHECK_LEN 2048
+#define BFLB_EFLASH_MAX_SIZE 2 * 1024 * 1024
+
+
+
+
+
+
+extern struct device *download_uart;
+extern struct device *dev_check_hash;
+
+#if BLSP_BOOT2_SUPPORT_EFLASH_LOADER_RAM
+static struct image_cfg_t image_cfg;
+static struct bootrom_img_ctrl_t img_ctrl;
+static struct segment_header_t segment_hdr;
+uint32_t eflash_loader_cmd_ack_buf[16];
+/*for bl602*/
+static int32_t bflb_bootrom_cmd_get_bootinfo(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_bootrom_cmd_load_bootheader(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_bootrom_cmd_load_segheader(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_bootrom_cmd_load_segdata(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_bootrom_cmd_check_img(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_bootrom_cmd_run(uint16_t cmd, uint8_t *data, uint16_t len);
+static void bflb_bootrom_cmd_ack(uint32_t result);
+#endif
+
+
+#if BLSP_BOOT2_SUPPORT_EFLASH_LOADER_FLASH
+static uint32_t g_eflash_loader_error = 0;
+/* for bl702 */
+static int32_t bflb_eflash_loader_cmd_read_jedec_id(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_reset(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_erase_flash(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_write_flash(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_read_flash(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_readSha_flash(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_xip_readSha_flash(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_write_flash_check(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_set_flash_para(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_xip_read_flash_start(uint16_t cmd, uint8_t *data, uint16_t len);
+static int32_t bflb_eflash_loader_cmd_xip_read_flash_finish(uint16_t cmd, uint8_t *data, uint16_t len);
+#endif
+
+static const struct eflash_loader_cmd_cfg_t eflash_loader_cmds[] = {
+#if BLSP_BOOT2_SUPPORT_EFLASH_LOADER_RAM
+ /*for bl602*/
+ { BFLB_EFLASH_LOADER_CMD_GET_BOOTINFO, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_get_bootinfo },
+ { BFLB_EFLASH_LOADER_CMD_LOAD_BOOTHEADER, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_load_bootheader },
+ { BFLB_EFLASH_LOADER_CMD_LOAD_SEGHEADER, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_load_segheader },
+ { BFLB_EFLASH_LOADER_CMD_LOAD_SEGDATA, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_load_segdata },
+ { BFLB_EFLASH_LOADER_CMD_CHECK_IMG, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_check_img },
+ { BFLB_EFLASH_LOADER_CMD_RUN, EFLASH_LOADER_CMD_ENABLE, bflb_bootrom_cmd_run },
+#endif
+
+#if BLSP_BOOT2_SUPPORT_EFLASH_LOADER_FLASH
+ /* for bl702 */
+ { BFLB_EFLASH_LOADER_CMD_RESET, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_reset },
+ { BFLB_EFLASH_LOADER_CMD_FLASH_ERASE, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_erase_flash },
+ { BFLB_EFLASH_LOADER_CMD_FLASH_WRITE, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_write_flash },
+ { BFLB_EFLASH_LOADER_CMD_FLASH_READ, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_read_flash },
+ { BFLB_EFLASH_LOADER_CMD_FLASH_WRITE_CHECK, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_write_flash_check },
+ { BFLB_EFLASH_LOADER_CMD_FLASH_SET_PARA, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_set_flash_para },
+ { BFLB_EFLASH_LOADER_CMD_FLASH_READSHA, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_readSha_flash },
+ { BFLB_EFLASH_LOADER_CMD_FLASH_XIP_READSHA, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_xip_readSha_flash },
+ { BFLB_EFLASH_LOADER_CMD_XIP_READ_START, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_xip_read_flash_start },
+ { BFLB_EFLASH_LOADER_CMD_XIP_READ_FINISH, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_xip_read_flash_finish },
+ { BFLB_EFLASH_LOADER_CMD_FLASH_READ_JEDECID, EFLASH_LOADER_CMD_ENABLE, bflb_eflash_loader_cmd_read_jedec_id },
+#endif
+};
+
+#if BLSP_BOOT2_SUPPORT_EFLASH_LOADER_RAM
+/* ack host with command process result */
+static void bflb_bootrom_cmd_ack(uint32_t result)
+{
+ if (result == 0) {
+ /*OK*/
+ eflash_loader_cmd_ack_buf[0] = BFLB_BOOTROM_CMD_ACK;
+ bflb_eflash_loader_if_write((uint32_t *)eflash_loader_cmd_ack_buf, 2);
+ return;
+ } else {
+ /* FL+Error code(2bytes) */
+ eflash_loader_cmd_ack_buf[0] = BFLB_BOOTROM_CMD_NACK | ((result << 16) & 0xffff0000);
+ bflb_eflash_loader_if_write(eflash_loader_cmd_ack_buf, 4);
+ }
+}
+
+/* for bl602 eflash loader */
+static int32_t bflb_bootrom_cmd_get_bootinfo(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ /*OK(2)+len(2)+bootrom version(4)+OTP(16)*/
+ uint8_t *bootinfo = (uint8_t *)eflash_loader_cmd_ack_buf;
+ uint8_t otp_cfg[16] = { 0 };
+
+ MSG("get bootinfo\r\n");
+
+ eflash_loader_cmd_ack_buf[0] = BFLB_BOOTROM_CMD_ACK;
+ bootinfo[2] = 0x14;
+ bootinfo[3] = 00;
+ *((uint32_t *)(bootinfo + 4)) = BFLB_BOOTROM_VERSION;
+ memcpy(bootinfo + 8, &otp_cfg, 16);
+
+ bflb_eflash_loader_if_write(eflash_loader_cmd_ack_buf, bootinfo[2] + 4);
+
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+int32_t bflb_bootrom_parse_bootheader(uint8_t *data)
+{
+ struct bootheader_t *header = (struct bootheader_t *)data;
+ uint32_t crc;
+ uint32_t crcpass = 0;
+
+ if (header->bootcfg.bval.crc_ignore == 1 && header->crc32 == BOOTROM_DEADBEEF_VAL) {
+ crcpass = 1;
+ } else {
+ crc = BFLB_Soft_CRC32((uint8_t *)header, sizeof(struct bootheader_t) - sizeof(header->crc32));
+ if (header->crc32 == crc) {
+ crcpass = 1;
+ }
+ }
+
+ if (crcpass) {
+ if (header->bootcfg.bval.notload_in_bootrom) {
+ return BFLB_EFLASH_LOADER_IMG_BOOTHEADER_NOT_LOAD_ERROR;
+ }
+
+ /* for boot2, one CPU only */
+ /*get which CPU's img it is*/
+
+ if (0 == memcmp((void *)&header->magiccode, "BFNP", sizeof(header->magiccode))) {
+ //current_cpu=i;
+ img_ctrl.pkhash_src = 0; //boot_cpu_cfg[i].pkhash_src;
+ } else {
+ return BFLB_EFLASH_LOADER_IMG_BOOTHEADER_MAGIC_ERROR;
+ }
+
+ image_cfg.entrypoint = 0;
+ /* set image valid 0 as default */
+ image_cfg.img_valid = 0;
+
+ /* deal with pll config */
+ //bflb_bootrom_clk_set_from_user(&header->clkCfg,1);
+
+ /* encrypt and sign */
+ image_cfg.encrypt_type = header->bootcfg.bval.encrypt_type;
+ image_cfg.sign_type = header->bootcfg.bval.sign;
+ image_cfg.key_sel = header->bootcfg.bval.key_sel;
+
+ /* xip relative */
+ image_cfg.no_segment = header->bootcfg.bval.no_segment;
+ image_cfg.cache_select = header->bootcfg.bval.cache_select;
+ image_cfg.aes_region_lock = header->bootcfg.bval.aes_region_lock;
+ image_cfg.halt_ap = header->bootcfg.bval.halt_ap;
+ image_cfg.cache_way_disable = header->bootcfg.bval.cache_way_disable;
+ image_cfg.hash_ignore = header->bootcfg.bval.hash_ignore;
+
+ /* firmware len*/
+ image_cfg.img_segment_info.segment_cnt = header->img_segment_info.segment_cnt;
+
+ /*boot entry and flash offset */
+ image_cfg.entrypoint = header->bootentry;
+ image_cfg.img_start.flashoffset = header->img_start.flashoffset;
+
+ if (image_cfg.img_segment_info.segment_cnt == 0) {
+ return BFLB_EFLASH_LOADER_IMG_SEGMENT_CNT_ERROR;
+ }
+ /*clear segment_cnt*/
+ img_ctrl.segment_cnt = 0;
+
+ } else {
+ return BFLB_EFLASH_LOADER_IMG_BOOTHEADER_CRC_ERROR;
+ }
+
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+static int32_t bflb_bootrom_cmd_load_bootheader(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+
+ MSG("load bootheader\r\n");
+
+ if (len != sizeof(struct bootheader_t)) {
+ ret = BFLB_EFLASH_LOADER_IMG_BOOTHEADER_LEN_ERROR;
+ } else {
+ ret = bflb_bootrom_parse_bootheader(data);
+
+ if (BFLB_EFLASH_LOADER_SUCCESS == ret) {
+ if (image_cfg.sign_type) {
+ img_ctrl.state = BOOTROM_IMG_PK;
+ } else if (image_cfg.encrypt_type) {
+ img_ctrl.state = BOOTROM_IMG_AESIV;
+ } else {
+ img_ctrl.state = BOOTROM_IMG_SEGHEADER;
+ }
+ }
+ }
+
+ bflb_bootrom_cmd_ack(ret);
+
+ return ret;
+}
+
+static int32_t bflb_bootrom_is_boot_dst_valid(uint32_t addr, uint32_t len)
+{
+ return 1;
+}
+
+int32_t bflb_bootrom_parse_seg_header(uint8_t *data)
+{
+ struct segment_header_t *hdr;
+
+ hdr = (struct segment_header_t *)data;
+
+ if (hdr->crc32 == BFLB_Soft_CRC32(hdr, sizeof(struct segment_header_t) - 4)) {
+ memcpy(&segment_hdr, hdr, sizeof(struct segment_header_t));
+ if (bflb_bootrom_is_boot_dst_valid(segment_hdr.destaddr, segment_hdr.len) == 1) {
+ return BFLB_EFLASH_LOADER_SUCCESS;
+ } else {
+ return BFLB_EFLASH_LOADER_IMG_SECTIONHEADER_DST_ERROR;
+ }
+ } else {
+ return BFLB_EFLASH_LOADER_IMG_SECTIONHEADER_CRC_ERROR;
+ }
+}
+
+static int32_t bflb_bootrom_cmd_load_segheader(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+ uint8_t *segdatainfo = (uint8_t *)eflash_loader_cmd_ack_buf;
+
+ MSG("load SegHdr\r\n");
+
+ if (img_ctrl.state != BOOTROM_IMG_SEGHEADER) {
+ ret = BFLB_EFLASH_LOADER_CMD_SEQ_ERROR;
+ } else {
+ if (len != sizeof(struct segment_header_t)) {
+ ret = BFLB_EFLASH_LOADER_IMG_SECTIONHEADER_LEN_ERROR;
+ } else {
+ memset(&segment_hdr, 0, sizeof(struct segment_header_t));
+ img_ctrl.segdata_recv_len = 0;
+ ret = bflb_bootrom_parse_seg_header(data);
+ if (ret == BFLB_EFLASH_LOADER_SUCCESS) {
+ //bflb_bootrom_printe("dest=%08x,len=%d\r\n",segment_hdr.destaddr,segment_hdr.len);
+ img_ctrl.state = BOOTROM_IMG_SEGDATA;
+ }
+ }
+ }
+
+ /* if segheader is encrypted, then ack segment len*/
+ if (ret == BFLB_EFLASH_LOADER_SUCCESS) {
+ /*ack segdata len*/
+ eflash_loader_cmd_ack_buf[0] = BFLB_BOOTROM_CMD_ACK;
+ segdatainfo[2] = sizeof(segment_hdr);
+ segdatainfo[3] = 0x00;
+ memcpy(&segdatainfo[4], (void *)&segment_hdr, sizeof(segment_hdr));
+ bflb_eflash_loader_if_write(eflash_loader_cmd_ack_buf, segdatainfo[2] + 4);
+ } else {
+ bflb_bootrom_cmd_ack(ret);
+ }
+ return ret;
+}
+
+static int32_t bflb_bootrom_cmd_load_segdata(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+
+ MSG("load SegData\r\n");
+
+ if (img_ctrl.state != BOOTROM_IMG_SEGDATA) {
+ ret = BFLB_EFLASH_LOADER_CMD_SEQ_ERROR;
+ goto finished;
+ }
+
+ if (image_cfg.encrypt_type && len % 16 != 0) {
+ ret = BFLB_EFLASH_LOADER_IMG_SECTIONDATA_LEN_ERROR;
+ //bflb_bootrom_printe("len error,len=%d\r\n",len);
+ goto finished;
+ }
+
+ if (img_ctrl.segdata_recv_len + len > segment_hdr.len) {
+ ret = BFLB_EFLASH_LOADER_IMG_SECTIONDATA_TLEN_ERROR;
+ //bflb_bootrom_printe("tlen error,receive=%d,indicator=%d\r\n",
+ //img_ctrl.segdata_recv_len+len,segment_hdr.len);
+ goto finished;
+ }
+
+ /*no encryption,copy directlly */
+ ARCH_MemCpy_Fast((void *)segment_hdr.destaddr, data, len);
+ MSG("segment_hdr.destaddr 0x%08x, len %d,data %02x %02x %02x %02x \r\n", segment_hdr.destaddr, len, data[0], data[1], data[2], data[3]);
+ img_ctrl.segdata_recv_len += len;
+ segment_hdr.destaddr += len;
+
+ if (img_ctrl.segdata_recv_len >= segment_hdr.len) {
+ /*finish loading one segment*/
+ img_ctrl.segment_cnt++;
+ if (img_ctrl.segment_cnt == image_cfg.img_segment_info.segment_cnt) {
+ img_ctrl.state = BOOTROM_IMG_CHECK;
+ } else {
+ img_ctrl.state = BOOTROM_IMG_SEGHEADER;
+ }
+ }
+
+finished:
+ bflb_bootrom_cmd_ack(ret);
+
+ return ret;
+}
+
+static int32_t bflb_bootrom_cmd_check_img(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+
+ MSG("check img\r\n");
+
+ if (img_ctrl.state != BOOTROM_IMG_CHECK) {
+ ret = BFLB_EFLASH_LOADER_IMG_HALFBAKED_ERROR;
+ bflb_bootrom_cmd_ack(ret);
+ return ret;
+ }
+
+ /*default,set state to BOOTROM_IMG_BOOTHEADER*/
+ img_ctrl.state = BOOTROM_IMG_BOOTHEADER;
+
+ /*finally, check hash and signature*/
+ ret = BFLB_EFLASH_LOADER_SUCCESS; //bflb_bootrom_check_hash();
+ if (ret == BFLB_EFLASH_LOADER_SUCCESS) {
+ ret = BFLB_EFLASH_LOADER_SUCCESS; //bflb_bootrom_check_signature();
+ bflb_bootrom_cmd_ack(ret);
+ if (ret == BFLB_EFLASH_LOADER_SUCCESS) {
+ img_ctrl.state = BOOTROM_IMG_RUN;
+ image_cfg.img_valid = 1;
+ }
+ } else {
+ bflb_bootrom_cmd_ack(ret);
+ }
+
+ return ret;
+}
+
+static void bflb_eflash_loader_exit_delay_us(uint32_t cnt)
+{
+ volatile uint32_t i = (32 / 5) * cnt;
+ while (i--)
+ ;
+}
+
+static void bflb_eflash_loader_jump_entry(void)
+{
+ pentry_t pentry = 0;
+
+ /* deal NP's entry point */
+ if (image_cfg.img_valid) {
+ pentry = (pentry_t)image_cfg.entrypoint;
+ MSG("image_cfg.entrypoint 0x%08x\r\n", image_cfg.entrypoint);
+ if (pentry != NULL) {
+ pentry();
+ }
+ }
+
+ /*if cann't jump(open jtag only?),stay here */
+ while (1) {
+ /*use soft delay only */
+ bflb_eflash_loader_exit_delay_us(100);
+ }
+}
+
+static int32_t bflb_bootrom_cmd_run(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+
+ MSG("run\r\n");
+
+ if (img_ctrl.state != BOOTROM_IMG_RUN) {
+ ret = BFLB_EFLASH_LOADER_CMD_SEQ_ERROR;
+ bflb_bootrom_cmd_ack(ret);
+ return ret;
+ }
+
+ bflb_bootrom_cmd_ack(ret);
+ //bflb_eflash_loader_usart_if_wait_tx_idle(BFLB_BOOTROM_IF_TX_IDLE_TIMEOUT);
+ bflb_platform_delay_ms(BFLB_BOOTROM_IF_TX_IDLE_TIMEOUT);
+
+ /* get msp and pc value */
+
+ MSG("image_cfg.img_valid %d\r\n", image_cfg.img_valid);
+
+ if (image_cfg.img_valid) {
+ if (image_cfg.entrypoint == 0) {
+ if (image_cfg.img_start.ramaddr != 0) {
+ image_cfg.entrypoint = image_cfg.img_start.ramaddr;
+ } else {
+ image_cfg.entrypoint = BL_TCM_BASE;
+ }
+ }
+ }
+ MSG("jump\r\n");
+
+ /*jump to entry */
+ bflb_eflash_loader_jump_entry();
+ return ret;
+}
+
+#endif
+
+
+
+
+#if BLSP_BOOT2_SUPPORT_EFLASH_LOADER_FLASH
+/* ack host with command process result */
+static void bflb_eflash_loader_cmd_ack(uint32_t result)
+{
+ if (result == 0) {
+ /*OK*/
+ g_eflash_loader_cmd_ack_buf[0] = BFLB_EFLASH_LOADER_CMD_ACK;
+ bflb_eflash_loader_if_write((uint32_t *)g_eflash_loader_cmd_ack_buf, 2);
+ return;
+ } else {
+ /* FL+Error code(2bytes) */
+ g_eflash_loader_cmd_ack_buf[0] = BFLB_EFLASH_LOADER_CMD_NACK | ((result << 16) & 0xffff0000);
+ bflb_eflash_loader_if_write(g_eflash_loader_cmd_ack_buf, 4);
+ }
+}
+
+static int32_t bflb_eflash_loader_cmd_read_jedec_id(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ uint32_t ackdata[2];
+ uint8_t *tmp_buf;
+ MSG("JID\n");
+ ackdata[0] = BFLB_EFLASH_LOADER_CMD_ACK;
+ tmp_buf = (uint8_t *)ackdata;
+ /*ack read jedec ID */
+ tmp_buf[2] = 4;
+ tmp_buf[3] = 0;
+ flash_read_jedec_id((uint8_t *)&ackdata[1]);
+ bflb_eflash_loader_if_write((uint32_t *)ackdata, 4 + 4);
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+static int32_t bflb_eflash_loader_cmd_reset(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+
+ pt_table_set_iap_para(&p_iap_param);
+ pt_table_dump();
+ MSG("RST\n");
+
+ bflb_eflash_loader_cmd_ack(ret);
+ bflb_eflash_loader_if_wait_tx_idle(BFLB_EFLASH_LOADER_IF_TX_IDLE_TIMEOUT);
+
+ /* add for bl702, will impact on boot pin read */
+ hal_boot2_set_psmode_status(0x594c440B);
+
+ /* FPGA POR RST NOT work,so add system reset */
+ bflb_platform_delay_us(10);
+ hal_boot2_sw_system_reset();
+
+ return ret;
+}
+
+static int32_t bflb_eflash_loader_cmd_erase_flash(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+ uint32_t startaddr, endaddr;
+
+ MSG("E\n");
+
+ if (len != 8) {
+ ret = BFLB_EFLASH_LOADER_FLASH_ERASE_PARA_ERROR;
+ } else {
+ /*clean write error, since write usually behand erase*/
+ g_eflash_loader_error = BFLB_EFLASH_LOADER_SUCCESS;
+
+ memcpy(&startaddr, data, 4);
+ memcpy(&endaddr, data + 4, 4);
+
+ p_iap_param.iap_img_len = endaddr - startaddr + 1;
+
+ MSG("from%08xto%08x\n", p_iap_param.iap_start_addr, p_iap_param.iap_start_addr + p_iap_param.iap_img_len - 1);
+
+ if (SUCCESS != flash_erase(p_iap_param.iap_start_addr, p_iap_param.iap_img_len)) {
+ MSG("fail\n");
+ ret = BFLB_EFLASH_LOADER_FLASH_ERASE_ERROR;
+ }
+ }
+
+ bflb_eflash_loader_cmd_ack(ret);
+ return ret;
+}
+
+static int32_t ATTR_TCM_SECTION bflb_eflash_loader_cmd_write_flash(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+ uint32_t write_len;
+
+ MSG("W\n");
+
+ if (len <= 4) {
+ ret = BFLB_EFLASH_LOADER_FLASH_WRITE_PARA_ERROR;
+ } else {
+ //memcpy(&startaddr,data,4);
+ write_len = len - 4;
+ MSG("to%08x,%d\n", p_iap_param.iap_write_addr, write_len);
+
+ if (p_iap_param.iap_write_addr < 0xffffffff) {
+ if (SUCCESS != flash_write(p_iap_param.iap_write_addr, data + 4, write_len)) {
+ /*error , response again with error */
+ MSG("fail\r\n");
+ ret = BFLB_EFLASH_LOADER_FLASH_WRITE_ERROR;
+ g_eflash_loader_error = ret;
+ } else {
+ bflb_eflash_loader_cmd_ack(ret);
+ p_iap_param.iap_write_addr += write_len;
+ //bflb_eflash_loader_printe("Write suss\r\n");
+ return BFLB_EFLASH_LOADER_SUCCESS;
+ }
+ } else {
+ ret = BFLB_EFLASH_LOADER_FLASH_WRITE_ADDR_ERROR;
+ }
+ MSG("%d\n", bflb_platform_get_time_us());
+ }
+
+ bflb_eflash_loader_cmd_ack(ret);
+ return ret;
+}
+
+static int32_t bflb_eflash_loader_cmd_read_flash(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+static int32_t bflb_eflash_loader_cmd_xip_read_flash_start(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+
+ bflb_eflash_loader_cmd_ack(ret);
+ return ret;
+}
+
+static int32_t bflb_eflash_loader_cmd_xip_read_flash_finish(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+
+ MSG("exit\n");
+ bflb_eflash_loader_cmd_ack(ret);
+ return ret;
+}
+
+static int32_t bflb_eflash_loader_cmd_readSha_flash(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+static int32_t bflb_eflash_loader_cmd_xip_readSha_flash(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+ uint32_t startaddr, read_len;
+ //SEC_Eng_SHA256_Ctx sha_ctx;
+ //SEC_ENG_SHA_ID_Type shaId = SEC_ENG_SHA_ID0;
+ uint16_t sha_len = 32;
+ uint8_t ackdata[32+4];
+ MSG("XRSha\n");
+ uint8_t *g_sha_in_buf = vmalloc(BFLB_EFLASH_LOADER_READBUF_SIZE);
+ if (len != 8) {
+ ret = BFLB_EFLASH_LOADER_FLASH_WRITE_PARA_ERROR;
+ bflb_eflash_loader_cmd_ack(ret);
+ } else {
+ startaddr = p_iap_param.iap_start_addr;
+ read_len = p_iap_param.iap_img_len;
+ MSG("from%08x,%d\n", startaddr, read_len);
+ //MSG("!!!Be careful that SHA input data should locate at OCRAM \n");
+ /* Init SHA and input SHA temp buffer for scattered data and g_padding buffer */
+ //Sec_Eng_SHA256_Init(&sha_ctx, shaId, SEC_ENG_SHA256, g_sha_tmp_buf, g_padding);
+ //Sec_Eng_SHA_Start(shaId);
+ device_unregister("dev_check_hash");
+ sec_hash_sha256_register(SEC_HASH0_INDEX,"dev_check_hash");
+ dev_check_hash = device_find("dev_check_hash");
+ if(dev_check_hash){
+ ret = device_open(dev_check_hash, 0);
+ if(ret){
+ MSG_ERR("hash dev open err\r\n");
+ return BFLB_BOOT2_FAIL;
}
+ }else{
+ MSG_ERR("hash dev find err\r\n");
+ return BFLB_BOOT2_FAIL;
}
- }
-
- bflb_bootrom_cmd_ack(ret);
-
- return ret;
-}
-
-static int32_t bflb_bootrom_is_boot_dst_valid(uint32_t addr, uint32_t len)
-{
- return 1;
-}
-
-int32_t bflb_bootrom_parse_seg_header(uint8_t *data)
-{
- struct segment_header_t *hdr;
-
- hdr = (struct segment_header_t *)data;
-
- if (hdr->crc32 == BFLB_Soft_CRC32(hdr, sizeof(struct segment_header_t) - 4)) {
- memcpy(&segment_hdr, hdr, sizeof(struct segment_header_t));
- if (bflb_bootrom_is_boot_dst_valid(segment_hdr.destaddr, segment_hdr.len) == 1) {
- return BFLB_EFLASH_LOADER_SUCCESS;
- } else {
- return BFLB_EFLASH_LOADER_IMG_SECTIONHEADER_DST_ERROR;
- }
- } else {
- return BFLB_EFLASH_LOADER_IMG_SECTIONHEADER_CRC_ERROR;
- }
-}
-
-static int32_t bflb_bootrom_cmd_load_segheader(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
- uint8_t *segdatainfo = (uint8_t *)eflash_loader_cmd_ack_buf;
-
- MSG("load SegHdr\r\n");
-
- if (img_ctrl.state != BOOTROM_IMG_SEGHEADER) {
- ret = BFLB_EFLASH_LOADER_CMD_SEQ_ERROR;
- } else {
- if (len != sizeof(struct segment_header_t)) {
- ret = BFLB_EFLASH_LOADER_IMG_SECTIONHEADER_LEN_ERROR;
- } else {
- memset(&segment_hdr, 0, sizeof(struct segment_header_t));
- img_ctrl.segdata_recv_len = 0;
- ret = bflb_bootrom_parse_seg_header(data);
- if (ret == BFLB_EFLASH_LOADER_SUCCESS) {
- //bflb_bootrom_printe("dest=%08x,len=%d\r\n",segment_hdr.destaddr,segment_hdr.len);
- img_ctrl.state = BOOTROM_IMG_SEGDATA;
- }
- }
- }
-
- /* if segheader is encrypted, then ack segment len*/
- if (ret == BFLB_EFLASH_LOADER_SUCCESS) {
- /*ack segdata len*/
- eflash_loader_cmd_ack_buf[0] = BFLB_BOOTROM_CMD_ACK;
- segdatainfo[2] = sizeof(segment_hdr);
- segdatainfo[3] = 0x00;
- memcpy(&segdatainfo[4], (void *)&segment_hdr, sizeof(segment_hdr));
- bflb_eflash_loader_uart_send(eflash_loader_cmd_ack_buf, segdatainfo[2] + 4);
- } else {
- bflb_bootrom_cmd_ack(ret);
- }
- return ret;
-}
-
-static int32_t bflb_bootrom_cmd_load_segdata(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
-
- MSG("load SegData\r\n");
-
- if (img_ctrl.state != BOOTROM_IMG_SEGDATA) {
- ret = BFLB_EFLASH_LOADER_CMD_SEQ_ERROR;
- goto finished;
- }
-
- if (image_cfg.encrypt_type && len % 16 != 0) {
- ret = BFLB_EFLASH_LOADER_IMG_SECTIONDATA_LEN_ERROR;
- //bflb_bootrom_printe("len error,len=%d\r\n",len);
- goto finished;
- }
-
- if (img_ctrl.segdata_recv_len + len > segment_hdr.len) {
- ret = BFLB_EFLASH_LOADER_IMG_SECTIONDATA_TLEN_ERROR;
- //bflb_bootrom_printe("tlen error,receive=%d,indicator=%d\r\n",
- //img_ctrl.segdata_recv_len+len,segment_hdr.len);
- goto finished;
- }
-
- /*no encryption,copy directlly */
- ARCH_MemCpy_Fast((void *)segment_hdr.destaddr, data, len);
- MSG("segment_hdr.destaddr 0x%08x, len %d,data %02x %02x %02x %02x \r\n", segment_hdr.destaddr, len, data[0], data[1], data[2], data[3]);
- img_ctrl.segdata_recv_len += len;
- segment_hdr.destaddr += len;
-
- if (img_ctrl.segdata_recv_len >= segment_hdr.len) {
- /*finish loading one segment*/
- img_ctrl.segment_cnt++;
- if (img_ctrl.segment_cnt == image_cfg.img_segment_info.segment_cnt) {
- img_ctrl.state = BOOTROM_IMG_CHECK;
- } else {
- img_ctrl.state = BOOTROM_IMG_SEGHEADER;
- }
- }
-
-finished:
- bflb_bootrom_cmd_ack(ret);
-
- return ret;
-}
-
-static int32_t bflb_bootrom_cmd_check_img(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
-
- MSG("check img\r\n");
-
- if (img_ctrl.state != BOOTROM_IMG_CHECK) {
- ret = BFLB_EFLASH_LOADER_IMG_HALFBAKED_ERROR;
- bflb_bootrom_cmd_ack(ret);
- return ret;
- }
-
- /*default,set state to BOOTROM_IMG_BOOTHEADER*/
- img_ctrl.state = BOOTROM_IMG_BOOTHEADER;
-
- /*finally, check hash and signature*/
- ret = BFLB_EFLASH_LOADER_SUCCESS; //bflb_bootrom_check_hash();
- if (ret == BFLB_EFLASH_LOADER_SUCCESS) {
- ret = BFLB_EFLASH_LOADER_SUCCESS; //bflb_bootrom_check_signature();
- bflb_bootrom_cmd_ack(ret);
- if (ret == BFLB_EFLASH_LOADER_SUCCESS) {
- img_ctrl.state = BOOTROM_IMG_RUN;
- image_cfg.img_valid = 1;
- }
- } else {
- bflb_bootrom_cmd_ack(ret);
- }
-
- return ret;
-}
-
-static void bflb_eflash_loader_exit_delay_us(uint32_t cnt)
-{
- volatile uint32_t i = (32 / 5) * cnt;
- while (i--)
- ;
-}
-
-static void bflb_eflash_loader_jump_entry(void)
-{
- pentry_t pentry = 0;
-
- /* deal NP's entry point */
- if (image_cfg.img_valid) {
- pentry = (pentry_t)image_cfg.entrypoint;
- MSG("image_cfg.entrypoint 0x%08x\r\n", image_cfg.entrypoint);
- if (pentry != NULL) {
- pentry();
- }
- }
-
- /*if cann't jump(open jtag only?),stay here */
- while (1) {
- /*use soft delay only */
- bflb_eflash_loader_exit_delay_us(100);
- }
-}
-
-static int32_t bflb_bootrom_cmd_run(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
-
- MSG("run\r\n");
-
- if (img_ctrl.state != BOOTROM_IMG_RUN) {
- ret = BFLB_EFLASH_LOADER_CMD_SEQ_ERROR;
- bflb_bootrom_cmd_ack(ret);
- return ret;
- }
-
- bflb_bootrom_cmd_ack(ret);
- //bflb_eflash_loader_usart_if_wait_tx_idle(BFLB_BOOTROM_IF_TX_IDLE_TIMEOUT);
- bflb_platform_delay_ms(BFLB_BOOTROM_IF_TX_IDLE_TIMEOUT);
-
- /* get msp and pc value */
-
- MSG("image_cfg.img_valid %d\r\n", image_cfg.img_valid);
-
- if (image_cfg.img_valid) {
- if (image_cfg.entrypoint == 0) {
- if (image_cfg.img_start.ramaddr != 0) {
- image_cfg.entrypoint = image_cfg.img_start.ramaddr;
- } else {
- image_cfg.entrypoint = BL_TCM_BASE;
- }
- }
- }
- MSG("jump\r\n");
-
- /*jump to entry */
- bflb_eflash_loader_jump_entry();
- return ret;
-}
-
-/* int boot command control */
-void bflb_eflash_loader_cmd_init()
-{
- g_eflash_loader_error = BFLB_EFLASH_LOADER_SUCCESS;
-}
-
-/* ack host with command process result */
-static void bflb_eflash_loader_cmd_ack(uint32_t result)
-{
- if (result == 0) {
- /*OK*/
- g_eflash_loader_cmd_ack_buf[0] = BFLB_EFLASH_LOADER_CMD_ACK;
- bflb_eflash_loader_uart_send((uint32_t *)g_eflash_loader_cmd_ack_buf, 2);
- return;
- } else {
- /* FL+Error code(2bytes) */
- g_eflash_loader_cmd_ack_buf[0] = BFLB_EFLASH_LOADER_CMD_NACK | ((result << 16) & 0xffff0000);
- bflb_eflash_loader_uart_send(g_eflash_loader_cmd_ack_buf, 4);
- }
-}
-
-static int32_t bflb_eflash_loader_cmd_read_jedec_id(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- uint8_t *ackdata = (uint8_t *)eflash_loader_cmd_mass_ack_buf;
-
- MSG("JID\n");
- eflash_loader_cmd_mass_ack_buf[0] = BFLB_EFLASH_LOADER_CMD_ACK;
-
- /*ack read jedec ID */
- ackdata[2] = 4;
- ackdata[3] = 0;
- flash_read_jedec_id((uint8_t *)&eflash_loader_cmd_mass_ack_buf[1]);
- bflb_eflash_loader_uart_send((uint32_t *)ackdata, 4 + 4);
- return BFLB_EFLASH_LOADER_SUCCESS;
-}
-
-static int32_t bflb_eflash_loader_cmd_reset(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
-
- pt_table_set_iap_para(&p_iap_param);
- pt_table_dump();
- MSG("RST\n");
-
- bflb_eflash_loader_cmd_ack(ret);
- bflb_eflash_loader_usart_wait_tx_idle(BFLB_EFLASH_LOADER_IF_TX_IDLE_TIMEOUT);
-
- /* add for bl702, will impact on boot pin read */
- hal_boot2_set_psmode_status(0x594c440B);
-
- /* FPGA POR RST NOT work,so add system reset */
- bflb_platform_delay_us(10);
- hal_boot2_sw_system_reset();
-
- return ret;
-}
-
-static int32_t bflb_eflash_loader_cmd_erase_flash(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
- uint32_t startaddr, endaddr;
-
- MSG("E\n");
-
- if (len != 8) {
- ret = BFLB_EFLASH_LOADER_FLASH_ERASE_PARA_ERROR;
- } else {
- /*clean write error, since write usually behand erase*/
- g_eflash_loader_error = BFLB_EFLASH_LOADER_SUCCESS;
-
- memcpy(&startaddr, data, 4);
- memcpy(&endaddr, data + 4, 4);
-
- p_iap_param.iap_img_len = endaddr - startaddr + 1;
-
- MSG("from%08xto%08x\n", p_iap_param.iap_start_addr, p_iap_param.iap_start_addr + p_iap_param.iap_img_len - 1);
-
- if (SUCCESS != flash_erase(p_iap_param.iap_start_addr, p_iap_param.iap_img_len)) {
- MSG("fail\n");
- ret = BFLB_EFLASH_LOADER_FLASH_ERASE_ERROR;
- }
- }
-
- bflb_eflash_loader_cmd_ack(ret);
- return ret;
-}
-
-static int32_t ATTR_TCM_SECTION bflb_eflash_loader_cmd_write_flash(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
- uint32_t write_len;
-
- MSG("W\n");
-
- if (len <= 4) {
- ret = BFLB_EFLASH_LOADER_FLASH_WRITE_PARA_ERROR;
- } else {
- //memcpy(&startaddr,data,4);
- write_len = len - 4;
- MSG("to%08x,%d\n", p_iap_param.iap_write_addr, write_len);
-
- if (p_iap_param.iap_write_addr < 0xffffffff) {
- if (SUCCESS != flash_write(p_iap_param.iap_write_addr, data + 4, write_len)) {
- /*error , response again with error */
- MSG("fail\r\n");
- ret = BFLB_EFLASH_LOADER_FLASH_WRITE_ERROR;
- g_eflash_loader_error = ret;
- } else {
- bflb_eflash_loader_cmd_ack(ret);
- p_iap_param.iap_write_addr += write_len;
- //bflb_eflash_loader_printe("Write suss\r\n");
- return BFLB_EFLASH_LOADER_SUCCESS;
- }
- } else {
- ret = BFLB_EFLASH_LOADER_FLASH_WRITE_ADDR_ERROR;
- }
- MSG("%d\n", bflb_platform_get_time_us());
- }
-
- bflb_eflash_loader_cmd_ack(ret);
- return ret;
-}
-
-static int32_t bflb_eflash_loader_cmd_read_flash(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- return BFLB_EFLASH_LOADER_SUCCESS;
-}
-
-static int32_t bflb_eflash_loader_cmd_xip_read_flash_start(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
-
- bflb_eflash_loader_cmd_ack(ret);
- return ret;
-}
-
-static int32_t bflb_eflash_loader_cmd_xip_read_flash_finish(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
-
- MSG("exit\n");
- bflb_eflash_loader_cmd_ack(ret);
- return ret;
-}
-
-static int32_t bflb_eflash_loader_cmd_readSha_flash(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- return BFLB_EFLASH_LOADER_SUCCESS;
-}
-
-static int32_t bflb_eflash_loader_cmd_xip_readSha_flash(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
- uint32_t startaddr, read_len;
- //SEC_Eng_SHA256_Ctx sha_ctx;
- //SEC_ENG_SHA_ID_Type shaId = SEC_ENG_SHA_ID0;
- uint16_t sha_len = 32;
- uint8_t *ackdata = (uint8_t *)eflash_loader_cmd_mass_ack_buf;
- MSG("XRSha\n");
-
- if (len != 8) {
- ret = BFLB_EFLASH_LOADER_FLASH_WRITE_PARA_ERROR;
- bflb_eflash_loader_cmd_ack(ret);
- } else {
- startaddr = p_iap_param.iap_start_addr;
- read_len = p_iap_param.iap_img_len;
- MSG("from%08x,%d\n", startaddr, read_len);
- //MSG("!!!Be careful that SHA input data should locate at OCRAM \n");
- /* Init SHA and input SHA temp buffer for scattered data and g_padding buffer */
- //Sec_Eng_SHA256_Init(&sha_ctx, shaId, SEC_ENG_SHA256, g_sha_tmp_buf, g_padding);
- //Sec_Eng_SHA_Start(shaId);
- device_open(dev_check_hash, 0);
-
- while (read_len > 0) {
- if (read_len > BFLB_EFLASH_LOADER_READBUF_SIZE) {
- blsp_mediaboot_read(startaddr, (uint8_t *)g_sha_in_buf, BFLB_EFLASH_LOADER_READBUF_SIZE);
- /*cal sha here*/
- //Sec_Eng_SHA256_Update(&sha_ctx, shaId, (uint8_t *)g_sha_in_buf, BFLB_EFLASH_LOADER_READBUF_SIZE);
- device_write(dev_check_hash, 0, g_sha_in_buf, BFLB_EFLASH_LOADER_READBUF_SIZE);
- read_len -= BFLB_EFLASH_LOADER_READBUF_SIZE;
- startaddr += BFLB_EFLASH_LOADER_READBUF_SIZE;
- } else {
- blsp_mediaboot_read(startaddr, (uint8_t *)g_sha_in_buf, read_len);
- /*cal sha here*/
- //Sec_Eng_SHA256_Update(&sha_ctx, shaId, (uint8_t *)g_sha_in_buf, read_len);
- device_write(dev_check_hash, 0, g_sha_in_buf, read_len);
- read_len -= read_len;
- startaddr += read_len;
- }
- }
-
- //Sec_Eng_SHA256_Finish(&sha_ctx, shaId, &ackdata[4]);
- device_read(dev_check_hash, 0, &ackdata[4], 0);
- device_close(dev_check_hash);
-
- for (sha_len = 0; sha_len < 32; sha_len++) {
- MSG("\r\n");
- MSG("%02x ", ackdata[4 + sha_len]);
- }
-
- sha_len = 32;
- /*ack read data */
- ackdata[0] = BFLB_EFLASH_LOADER_CMD_ACK & 0xff;
- ackdata[1] = (BFLB_EFLASH_LOADER_CMD_ACK >> 8) & 0xff;
- ackdata[2] = sha_len & 0xff;
- ackdata[3] = (sha_len >> 8) & 0xff;
- bflb_eflash_loader_uart_send((uint32_t *)ackdata, sha_len + 4);
- }
-
- return ret;
-}
-
-static int32_t bflb_eflash_loader_cmd_write_flash_check(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- MSG("WC\n");
-
- bflb_eflash_loader_cmd_ack(g_eflash_loader_error);
-
- return BFLB_EFLASH_LOADER_SUCCESS;
-}
-
-static int32_t bflb_eflash_loader_cmd_set_flash_para(uint16_t cmd, uint8_t *data, uint16_t len)
-{
- bflb_eflash_loader_cmd_ack(BFLB_EFLASH_LOADER_SUCCESS);
- return BFLB_EFLASH_LOADER_SUCCESS;
-}
-
-int32_t bflb_eflash_loader_cmd_process(uint8_t cmdid, uint8_t *data, uint16_t len)
-{
- int i = 0;
- int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
-
- for (i = 0; i < sizeof(eflash_loader_cmds) / sizeof(eflash_loader_cmds[0]); i++) {
- if (eflash_loader_cmds[i].cmd == cmdid) {
- if (EFLASH_LOADER_CMD_ENABLE == eflash_loader_cmds[i].enabled && NULL != eflash_loader_cmds[i].cmd_process) {
- ret = eflash_loader_cmds[i].cmd_process(cmdid, data, len);
- } else {
- return BFLB_EFLASH_LOADER_CMD_ID_ERROR;
- }
-
- break;
- }
- }
-
- return ret;
-}
+ //device_open(dev_check_hash, 0);
+
+ while (read_len > 0) {
+ if (read_len > BFLB_EFLASH_LOADER_READBUF_SIZE) {
+ blsp_mediaboot_read(startaddr, g_sha_in_buf, BFLB_EFLASH_LOADER_READBUF_SIZE);
+ /*cal sha here*/
+ //Sec_Eng_SHA256_Update(&sha_ctx, shaId, (uint8_t *)g_sha_in_buf, BFLB_EFLASH_LOADER_READBUF_SIZE);
+ device_write(dev_check_hash, 0, g_sha_in_buf, BFLB_EFLASH_LOADER_READBUF_SIZE);
+ read_len -= BFLB_EFLASH_LOADER_READBUF_SIZE;
+ startaddr += BFLB_EFLASH_LOADER_READBUF_SIZE;
+ } else {
+ blsp_mediaboot_read(startaddr, g_sha_in_buf, read_len);
+ /*cal sha here*/
+ //Sec_Eng_SHA256_Update(&sha_ctx, shaId, (uint8_t *)g_sha_in_buf, read_len);
+ device_write(dev_check_hash, 0, g_sha_in_buf, read_len);
+ read_len -= read_len;
+ startaddr += read_len;
+ }
+ }
+
+ //Sec_Eng_SHA256_Finish(&sha_ctx, shaId, &ackdata[4]);
+ device_read(dev_check_hash, 0, &ackdata[4], 0);
+ device_close(dev_check_hash);
+
+ for (sha_len = 0; sha_len < 32; sha_len++) {
+ MSG("\r\n");
+ MSG("%02x ", ackdata[4 + sha_len]);
+ }
+
+ sha_len = 32;
+ /*ack read data */
+ ackdata[0] = BFLB_EFLASH_LOADER_CMD_ACK & 0xff;
+ ackdata[1] = (BFLB_EFLASH_LOADER_CMD_ACK >> 8) & 0xff;
+ ackdata[2] = sha_len & 0xff;
+ ackdata[3] = (sha_len >> 8) & 0xff;
+ bflb_eflash_loader_if_write((uint32_t *)ackdata, sha_len + 4);
+ }
+
+ return ret;
+}
+
+static int32_t bflb_eflash_loader_cmd_write_flash_check(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ MSG("WC\n");
+
+ bflb_eflash_loader_cmd_ack(g_eflash_loader_error);
+
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+static int32_t bflb_eflash_loader_cmd_set_flash_para(uint16_t cmd, uint8_t *data, uint16_t len)
+{
+ bflb_eflash_loader_cmd_ack(BFLB_EFLASH_LOADER_SUCCESS);
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+#endif
+
+int32_t bflb_eflash_loader_cmd_process(uint8_t cmdid, uint8_t *data, uint16_t len)
+{
+ int i = 0;
+ int32_t ret = BFLB_EFLASH_LOADER_SUCCESS;
+
+ for (i = 0; i < sizeof(eflash_loader_cmds) / sizeof(eflash_loader_cmds[0]); i++) {
+ if (eflash_loader_cmds[i].cmd == cmdid) {
+ if (EFLASH_LOADER_CMD_ENABLE == eflash_loader_cmds[i].enabled && NULL != eflash_loader_cmds[i].cmd_process) {
+ ret = eflash_loader_cmds[i].cmd_process(cmdid, data, len);
+ } else {
+ return BFLB_EFLASH_LOADER_CMD_ID_ERROR;
+ }
+
+ break;
+ }
+ }
+
+ return ret;
+}
diff --git a/examples/boot2_iap/bflb_eflash_loader_cmds.h b/examples/boot2_iap/bflb_eflash_loader_cmds.h
index e1cad567..549eca41 100644
--- a/examples/boot2_iap/bflb_eflash_loader_cmds.h
+++ b/examples/boot2_iap/bflb_eflash_loader_cmds.h
@@ -100,7 +100,6 @@
#define BFLB_BOOTROM_IF_TX_IDLE_TIMEOUT 40 /*ms*/
-void bflb_eflash_loader_cmd_init(void);
void bflb_eflash_loader_cmd_disable(uint8_t cmdid);
void bflb_eflash_loader_cmd_enable(uint8_t cmdid);
int32_t bflb_eflash_loader_cmd_process(uint8_t cmdid, uint8_t *data, uint16_t len);
diff --git a/examples/boot2_iap/bflb_eflash_loader_interface.c b/examples/boot2_iap/bflb_eflash_loader_interface.c
new file mode 100644
index 00000000..b5878418
--- /dev/null
+++ b/examples/boot2_iap/bflb_eflash_loader_interface.c
@@ -0,0 +1,169 @@
+#include "bflb_eflash_loader.h"
+#include "partition.h"
+#include "blsp_common.h"
+#include "xz_config.h"
+#include "blsp_port.h"
+#include "hal_boot2.h"
+
+
+uint8_t *g_eflash_loader_readbuf[2];
+volatile uint32_t g_rx_buf_index = 0;
+volatile uint32_t g_rx_buf_len = 0;
+uint32_t g_eflash_loader_cmd_ack_buf[16];
+
+
+static eflash_loader_if_cfg_t eflash_loader_if_cfg;
+
+
+eflash_loader_if_cfg_t * bflb_eflash_loader_if_set(eflash_loader_if_type_t type)
+{
+ switch(type){
+ case BFLB_EFLASH_LOADER_IF_UART:
+ eflash_loader_if_cfg.if_type=(uint8_t)BFLB_EFLASH_LOADER_IF_UART;
+ eflash_loader_if_cfg.if_type_onfail=(uint8_t)BFLB_EFLASH_LOADER_IF_JLINK;
+ eflash_loader_if_cfg.disabled=0;
+ eflash_loader_if_cfg.maxlen=BFLB_EFLASH_LOADER_READBUF_SIZE;
+ eflash_loader_if_cfg.timeout=BFLB_EFLASH_LOADER_IF_UART_RX_TIMEOUT;
+ eflash_loader_if_cfg.boot_if_init=bflb_eflash_loader_uart_init;
+ eflash_loader_if_cfg.boot_if_handshake_poll=bflb_eflash_loader_uart_handshake_poll;
+ eflash_loader_if_cfg.boot_if_recv=bflb_eflash_loader_uart_recv;
+ eflash_loader_if_cfg.boot_if_send=bflb_eflash_loader_uart_send;
+ eflash_loader_if_cfg.boot_if_wait_tx_idle=bflb_eflash_loader_usart_wait_tx_idle;
+ eflash_loader_if_cfg.boot_if_deinit=bflb_eflash_loader_uart_deinit;
+ //eflash_loader_if_cfg.boot_if_changerate=bflb_eflash_loader_uart_change_rate;
+
+ return &eflash_loader_if_cfg;
+#if BLSP_BOOT2_SUPPORT_USB_IAP
+ case BFLB_EFLASH_LOADER_IF_USB:
+ eflash_loader_if_cfg.if_type=(uint8_t)BFLB_EFLASH_LOADER_IF_USB;
+ eflash_loader_if_cfg.if_type_onfail=(uint8_t)BFLB_EFLASH_LOADER_IF_UART;
+ eflash_loader_if_cfg.disabled=0;
+ eflash_loader_if_cfg.maxlen=BFLB_EFLASH_LOADER_READBUF_SIZE;
+ eflash_loader_if_cfg.timeout=BFLB_EFLASH_LOADER_IF_USB_RX_TIMEOUT;
+ eflash_loader_if_cfg.boot_if_init=bflb_eflash_loader_usb_init;
+ eflash_loader_if_cfg.boot_if_handshake_poll=bflb_eflash_loader_usb_handshake_poll;
+ eflash_loader_if_cfg.boot_if_recv=bflb_eflash_loader_uart_recv;
+ eflash_loader_if_cfg.boot_if_send=bflb_eflash_loader_usb_send;
+ eflash_loader_if_cfg.boot_if_wait_tx_idle=bflb_eflash_loader_usb_wait_tx_idle;
+ eflash_loader_if_cfg.boot_if_deinit=bflb_eflash_loader_usb_deinit;
+ //eflash_loader_if_cfg.boot_if_changerate=bflb_eflash_loader_uart_change_rate;
+
+ return &eflash_loader_if_cfg;
+#endif
+ default:
+ break;
+ }
+
+ return NULL;
+}
+
+int32_t bflb_eflash_loader_if_init()
+{
+ return eflash_loader_if_cfg.boot_if_init();
+}
+
+int32_t bflb_eflash_loader_if_handshake_poll(uint32_t timeout)
+{
+ return eflash_loader_if_cfg.boot_if_handshake_poll(timeout);
+}
+
+uint32_t *bflb_eflash_loader_if_read(uint32_t *read_len)
+{
+ return eflash_loader_if_cfg.boot_if_recv(read_len,eflash_loader_if_cfg.maxlen,eflash_loader_if_cfg.timeout);
+}
+
+int32_t bflb_eflash_loader_if_write(uint32_t *data,uint32_t len)
+{
+ return eflash_loader_if_cfg.boot_if_send(data,len);
+}
+
+int32_t bflb_eflash_loader_if_wait_tx_idle(uint32_t timeout)
+{
+ return eflash_loader_if_cfg.boot_if_wait_tx_idle(timeout);
+}
+
+int32_t bflb_eflash_loader_if_deinit()
+{
+ return eflash_loader_if_cfg.boot_if_deinit();
+}
+
+
+int32_t bflb_eflash_loader_main()
+{
+ int32_t ret;
+ uint32_t total_len;
+ uint32_t i, tmp, cmd_len;
+ uint8_t *recv_buf = NULL;
+ uint8_t err_cnt = 0;
+ uint8_t to_cnt = 0;
+
+ MSG("bflb_eflash_loader_main\r\n");
+ pt_table_dump();
+ ret = pt_table_get_iap_para(&p_iap_param);
+ if(0 != ret){
+ MSG("no valid partition table\r\n");
+ return -1;
+ }
+
+ while (1) {
+ to_cnt = 0;
+ total_len = 0;
+
+ do {
+ total_len = 0;
+ recv_buf = (uint8_t *)bflb_eflash_loader_if_read(&total_len);
+
+ if (total_len <= 0) {
+ to_cnt++;
+ }
+ } while (to_cnt < 2 && total_len <= 0);
+
+ if (to_cnt >= 2 || total_len <= 0) {
+ MSG("rcv err break\r\n");
+ break;
+ }
+
+ MSG("Recv\r\n");
+ //eflash_loader_dump_data(recv_buf,total_len);
+ cmd_len = recv_buf[2] + (recv_buf[3] << 8);
+ MSG("cmd_len %d\r\n", cmd_len);
+
+ /* Check checksum*/
+ if (recv_buf[1] != 0) {
+ tmp = 0;
+
+ for (i = 2; i < cmd_len + 4; i++) {
+ tmp += recv_buf[i];
+ }
+
+ if ((tmp & 0xff) != recv_buf[1]) {
+ /* FL+Error code(2bytes) */
+ MSG("Checksum error %02x\r\n", tmp & 0xff);
+ g_eflash_loader_cmd_ack_buf[0] = BFLB_EFLASH_LOADER_CMD_NACK | ((BFLB_EFLASH_LOADER_CMD_CRC_ERROR << 16) & 0xffff0000);
+ bflb_eflash_loader_if_write(g_eflash_loader_cmd_ack_buf, 4);//ToDo
+ continue;
+ }
+ }
+
+ ret = bflb_eflash_loader_cmd_process(recv_buf[0], recv_buf + 4, cmd_len);
+
+ if (ret != BFLB_EFLASH_LOADER_SUCCESS) {
+ MSG(" CMD Pro Ret %d\r\n", ret);
+
+ err_cnt++;
+
+ if (err_cnt > 2) {
+ break;
+ }
+ }
+ }
+
+ /* read data finished,deinit and go on*/
+ bflb_eflash_loader_if_deinit();//ToDo
+
+ return 0;
+}
+
+
+
+
diff --git a/examples/boot2_iap/bflb_eflash_loader_interface.h b/examples/boot2_iap/bflb_eflash_loader_interface.h
new file mode 100644
index 00000000..cfeb35c3
--- /dev/null
+++ b/examples/boot2_iap/bflb_eflash_loader_interface.h
@@ -0,0 +1,70 @@
+#ifndef __BFLB_EFLASH_LOADER_INTERFACE_H__
+#define __BFLB_EFLASH_LOADER_INTERFACE_H__
+
+#include "bflb_eflash_loader.h"
+#include "stdio.h"
+#include "stdint.h"
+#include "string.h"
+
+
+#define BFLB_EFLASH_LOADER_IF_TX_IDLE_TIMEOUT 4 /*ms*/
+#define BFLB_EFLASH_LOADER_HAND_SHAKE_BYTE 0x55
+#define BFLB_EFLASH_LAODER_HAND_SHAKE_SUSS_COUNT 5
+
+typedef enum tag_eflash_loader_if_type_t
+{
+ //BFLB_EFLASH_LOADER_IF_FLASH=0x01,
+ BFLB_EFLASH_LOADER_IF_UART=0x0,
+ BFLB_EFLASH_LOADER_IF_JLINK,
+ BFLB_EFLASH_LOADER_IF_SDIO,
+ BFLB_EFLASH_LOADER_IF_USB,
+ BFLB_EFLASH_LOADER_IF_ALL,
+}eflash_loader_if_type_t;
+
+typedef int32_t (*boot_if_init_p)(void);
+typedef int32_t (*boot_if_handshake_poll_p)(uint32_t timeout);
+typedef uint32_t *(*boot_if_read_p)(uint32_t *len,uint32_t maxlen,uint32_t timeout);
+typedef int32_t (*boot_if_write_p)(uint32_t *data, uint32_t len);
+typedef int32_t (*boot_if_wait_tx_idle_p)(uint32_t timeout);
+typedef int32_t (*boot_if_deinit_p)(void);
+typedef int32_t (*boot_if_change_rate_p)(uint32_t oldval, uint32_t newval);
+
+typedef struct tag_eflash_loader_if_cfg_t
+{
+ uint8_t if_type; //interface type
+ uint8_t if_type_onfail; //if fail move to this interface
+ uint8_t disabled; //enable this if
+ uint8_t rsvd;
+
+ //pentry_t boot_entry[2];
+
+ uint16_t maxlen; //max len for one buffer
+ uint16_t timeout; //ms
+
+ boot_if_init_p boot_if_init; //init function pointer
+ boot_if_handshake_poll_p boot_if_handshake_poll; //shake hand poll function pointer
+ boot_if_read_p boot_if_recv; //read function pointer
+ boot_if_write_p boot_if_send; //write function pointer
+ boot_if_wait_tx_idle_p boot_if_wait_tx_idle; //wait tx idle function pointer
+ boot_if_deinit_p boot_if_deinit; //deinit function pointer
+ boot_if_change_rate_p boot_if_changerate; //change rate function pointer
+
+}eflash_loader_if_cfg_t;
+
+
+eflash_loader_if_cfg_t * bflb_eflash_loader_if_set(eflash_loader_if_type_t type);
+int32_t bflb_eflash_loader_if_init();
+int32_t bflb_eflash_loader_if_handshake_poll(uint32_t timeout);
+uint32_t *bflb_eflash_loader_if_read(uint32_t *read_len);
+int32_t bflb_eflash_loader_if_write(uint32_t *data,uint32_t len);
+int32_t bflb_eflash_loader_if_wait_tx_idle(uint32_t timeout);
+int32_t bflb_eflash_loader_if_deinit();
+int32_t bflb_eflash_loader_main(void);
+
+extern uint8_t *g_eflash_loader_readbuf[2];
+/*read data buffer from flash or boot interface*/
+extern volatile uint32_t g_rx_buf_index;
+extern volatile uint32_t g_rx_buf_len;
+extern uint32_t g_eflash_loader_cmd_ack_buf[16];
+
+#endif
diff --git a/examples/boot2_iap/bflb_eflash_loader_uart.c b/examples/boot2_iap/bflb_eflash_loader_uart.c
index 28a00577..c247b0dc 100644
--- a/examples/boot2_iap/bflb_eflash_loader_uart.c
+++ b/examples/boot2_iap/bflb_eflash_loader_uart.c
@@ -33,23 +33,18 @@
*
******************************************************************************
*/
-#include "bflb_eflash_loader_uart.h"
-#include "bflb_eflash_loader_cmds.h"
#include "bflb_eflash_loader.h"
#include "bflb_platform.h"
#include "blsp_port.h"
+#include "blsp_common.h"
#include "partition.h"
#include "hal_uart.h"
#include "drv_device.h"
+#include "hal_boot2.h"
static uint32_t g_detected_baudrate;
-/* data buffer for read data and decrypt */
-volatile uint32_t g_rx_buf_index = 0;
-volatile uint32_t g_rx_buf_len = 0;
-uint32_t g_eflash_loader_readbuf[2][(BFLB_EFLASH_LOADER_READBUF_SIZE + 3) / 4];
-uint32_t g_eflash_loader_cmd_ack_buf[16];
static void bflb_eflash_loader_usart_if_deinit();
struct device *download_uart = NULL;
@@ -69,7 +64,7 @@ void bflb_dump_data(uint8_t * buf, uint32_t size)
}
static void ATTR_TCM_SECTION uart0_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
{
- uint8_t *buf = (uint8_t *)g_eflash_loader_readbuf[g_rx_buf_index];
+ uint8_t *buf = g_eflash_loader_readbuf[g_rx_buf_index];
if (state == UART_EVENT_RX_FIFO) {
//g_rx_buf_len += device_read(download_uart,0,buf,BFLB_EFLASH_LOADER_READBUF_SIZE-g_rx_buf_len);
arch_memcpy(buf + g_rx_buf_len, args, size);
@@ -83,6 +78,7 @@ static void ATTR_TCM_SECTION uart0_irq_callback(struct device *dev, void *args,
static void bflb_eflash_loader_usart_if_init(uint32_t bdrate)
{
+ hal_boot2_uart_gpio_init();
#if ((BLSP_BOOT2_MODE == BOOT2_MODE_RELEASE) || (BLSP_BOOT2_MODE == BOOT2_MODE_DEBUG))
uart_register(0, "debug_log");
download_uart = device_find("debug_log");
@@ -196,7 +192,7 @@ int32_t bflb_eflash_loader_uart_init()
return BFLB_EFLASH_LOADER_SUCCESS;
}
-int32_t bflb_eflash_loader_uart_handshake_poll()
+int32_t bflb_eflash_loader_uart_handshake_poll(uint32_t timeout)
{
uint8_t buf[UART_FIFO_LEN];
uint32_t i;
@@ -225,11 +221,11 @@ int32_t bflb_eflash_loader_uart_handshake_poll()
if (handshake_count >= BFLB_EFLASH_LAODER_HAND_SHAKE_SUSS_COUNT) {
//reinit uart
- MSG("handshake %d 0x55 rcv\r\n", handshake_count);
+ MSG("iap handshake %d 0x55 rcv\r\n", handshake_count);
} else {
- MSG("handshake err\r\n");
- return BFLB_EFLASH_LOADER_HANDSHAKE_FAIL;
+ MSG("iap handshake err\r\n");
+ return -1;
}
/*receive shake hanad signal*/
@@ -242,7 +238,6 @@ int32_t bflb_eflash_loader_uart_handshake_poll()
/*init rx info */
g_rx_buf_index = 0;
g_rx_buf_len = 0;
- BL_WR_WORD(g_eflash_loader_readbuf[g_rx_buf_index], 0);
#if (BLSP_BOOT2_MODE == BOOT2_MODE_DEBUG)
bflb_platform_print_set(1);
@@ -255,8 +250,15 @@ int32_t bflb_eflash_loader_uart_handshake_poll()
device_open(download_uart, DEVICE_OFLAG_STREAM_TX);
}
#endif
+
+ simple_malloc_init(g_malloc_buf, sizeof(g_malloc_buf));
+ g_eflash_loader_readbuf[0] = vmalloc(BFLB_EFLASH_LOADER_READBUF_SIZE);
+ g_eflash_loader_readbuf[1] = vmalloc(BFLB_EFLASH_LOADER_READBUF_SIZE);
+ arch_memset(g_eflash_loader_readbuf[0], 0, BFLB_EFLASH_LOADER_READBUF_SIZE);
+ arch_memset(g_eflash_loader_readbuf[1], 0, BFLB_EFLASH_LOADER_READBUF_SIZE);
+
bflb_eflash_loader_usart_if_enable_int();
- return BFLB_EFLASH_LOADER_HANDSHAKE_SUSS;
+ return 0;
}
uint32_t *bflb_eflash_loader_uart_recv(uint32_t *recv_len, uint32_t maxlen, uint32_t timeout)
@@ -305,77 +307,5 @@ int32_t bflb_eflash_loader_uart_deinit()
return BFLB_EFLASH_LOADER_SUCCESS;
}
-void bflb_eflash_loader_main()
-{
- int32_t ret;
- uint32_t total_len;
- uint32_t i, tmp, cmd_len;
- uint8_t *recv_buf = NULL;
- uint8_t err_cnt = 0;
- uint8_t to_cnt = 0;
- MSG("bflb_eflash_loader_main\r\n");
- pt_table_dump();
- ret = pt_table_get_iap_para(&p_iap_param);
- if(0 != ret){
- MSG("no valid partition table\r\n");
- return;
- }
- bflb_eflash_loader_cmd_init();
- while (1) {
- to_cnt = 0;
- total_len = 0;
-
- do {
- total_len = 0;
- recv_buf = (uint8_t *)bflb_eflash_loader_uart_recv(&total_len, BFLB_EFLASH_LOADER_READBUF_SIZE, BFLB_EFLASH_LOADER_IF_UART_RX_TIMEOUT);
-
- if (total_len <= 0) {
- to_cnt++;
- }
- } while (to_cnt < 2 && total_len <= 0);
-
- if (to_cnt >= 2 || total_len <= 0) {
- MSG("rcv err break\r\n");
- break;
- }
-
- MSG("Recv\r\n");
- //eflash_loader_dump_data(recv_buf,total_len);
- cmd_len = recv_buf[2] + (recv_buf[3] << 8);
- MSG("cmd_len %d\r\n", cmd_len);
-
- /* Check checksum*/
- if (recv_buf[1] != 0) {
- tmp = 0;
-
- for (i = 2; i < cmd_len + 4; i++) {
- tmp += recv_buf[i];
- }
-
- if ((tmp & 0xff) != recv_buf[1]) {
- /* FL+Error code(2bytes) */
- MSG("Checksum error %02x\r\n", tmp & 0xff);
- g_eflash_loader_cmd_ack_buf[0] = BFLB_EFLASH_LOADER_CMD_NACK | ((BFLB_EFLASH_LOADER_CMD_CRC_ERROR << 16) & 0xffff0000);
- bflb_eflash_loader_uart_send(g_eflash_loader_cmd_ack_buf, 4);
- continue;
- }
- }
-
- ret = bflb_eflash_loader_cmd_process(recv_buf[0], recv_buf + 4, cmd_len);
-
- if (ret != BFLB_EFLASH_LOADER_SUCCESS) {
- MSG(" CMD Pro Ret %d\r\n", ret);
-
- err_cnt++;
-
- if (err_cnt > 2) {
- break;
- }
- }
- }
-
- /* read data finished,deinit and go on*/
- bflb_eflash_loader_uart_deinit();
-}
diff --git a/examples/boot2_iap/bflb_eflash_loader_uart.h b/examples/boot2_iap/bflb_eflash_loader_uart.h
index 0d3135a0..f764c6ce 100644
--- a/examples/boot2_iap/bflb_eflash_loader_uart.h
+++ b/examples/boot2_iap/bflb_eflash_loader_uart.h
@@ -40,7 +40,7 @@
int32_t bflb_eflash_loader_uart_init();
-int32_t bflb_eflash_loader_uart_handshake_poll();
+int32_t bflb_eflash_loader_uart_handshake_poll(uint32_t timeout);
uint32_t *bflb_eflash_loader_uart_recv(uint32_t *recv_len, uint32_t maxlen, uint32_t timeout);
@@ -52,17 +52,10 @@ int32_t bflb_eflash_loader_uart_change_rate(uint32_t oldval, uint32_t newval);
int32_t bflb_eflash_loader_uart_deinit(void);
-void bflb_eflash_loader_main();
-typedef enum {
- BFLB_EFLASH_LOADER_HANDSHAKE_SUSS = 0,
- BFLB_EFLASH_LOADER_HANDSHAKE_FAIL,
-} eflash_handshake_ret;
#define AUTO_BAUDRATE_CHECK_TIME_MAX_MS 2
-#define BFLB_EFLASH_LOADER_IF_TX_IDLE_TIMEOUT 4 /*ms*/
#define BFLB_EFLASH_LOADER_IF_UART_RX_TIMEOUT 8000 /*ms*/
-#define BFLB_EFLASH_LOADER_HAND_SHAKE_BYTE 0x55
-#define BFLB_EFLASH_LAODER_HAND_SHAKE_SUSS_COUNT 5
+
#endif
diff --git a/examples/boot2_iap/bflb_eflash_loader_usb.c b/examples/boot2_iap/bflb_eflash_loader_usb.c
new file mode 100644
index 00000000..2297d4c1
--- /dev/null
+++ b/examples/boot2_iap/bflb_eflash_loader_usb.c
@@ -0,0 +1,341 @@
+/**
+ ******************************************************************************
+ * @file blsp_eflash_loader_usb.c
+ * @version V1.2
+ * @date
+ * @brief This file is the peripheral case header file
+ ******************************************************************************
+ * @attention
+ *
+ * © COPYRIGHT(c) 2018 Bouffalo Lab
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Bouffalo Lab nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+#include "blsp_port.h"
+#include "hal_boot2.h"
+#if BLSP_BOOT2_SUPPORT_USB_IAP
+#include "bflb_eflash_loader.h"
+#include "hal_usb.h"
+#include "usbd_core.h"
+#include "usbd_cdc.h"
+#include "blsp_common.h"
+#include "xz_config.h"
+
+#define CDC_IN_EP 0x82
+#define CDC_OUT_EP 0x01
+#define CDC_INT_EP 0x83
+
+#define USBD_VID 0xFFFF
+#define USBD_PID 0xFFFF
+#define USBD_MAX_POWER 100
+#define USBD_LANGID_STRING 1033
+
+#define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN)
+
+USB_DESC_SECTION const uint8_t cdc_descriptor[] = {
+ USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x02, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
+ USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
+ CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, 0x02),
+ ///////////////////////////////////////
+ /// string0 descriptor
+ ///////////////////////////////////////
+ USB_LANGID_INIT(USBD_LANGID_STRING),
+ ///////////////////////////////////////
+ /// string1 descriptor
+ ///////////////////////////////////////
+ 0x12, /* bLength */
+ USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
+ 'B', 0x00, /* wcChar0 */
+ 'o', 0x00, /* wcChar1 */
+ 'u', 0x00, /* wcChar2 */
+ 'f', 0x00, /* wcChar3 */
+ 'f', 0x00, /* wcChar4 */
+ 'a', 0x00, /* wcChar5 */
+ 'l', 0x00, /* wcChar6 */
+ 'o', 0x00, /* wcChar7 */
+ ///////////////////////////////////////
+ /// string2 descriptor
+ ///////////////////////////////////////
+ 0x24, /* bLength */
+ USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
+ 'B', 0x00, /* wcChar0 */
+ 'o', 0x00, /* wcChar1 */
+ 'u', 0x00, /* wcChar2 */
+ 'f', 0x00, /* wcChar3 */
+ 'f', 0x00, /* wcChar4 */
+ 'a', 0x00, /* wcChar5 */
+ 'l', 0x00, /* wcChar6 */
+ 'o', 0x00, /* wcChar7 */
+ ' ', 0x00, /* wcChar8 */
+ 'C', 0x00, /* wcChar9 */
+ 'D', 0x00, /* wcChar10 */
+ 'C', 0x00, /* wcChar11 */
+ ' ', 0x00, /* wcChar13 */
+ 'D', 0x00, /* wcChar14 */
+ 'E', 0x00, /* wcChar15 */
+ 'M', 0x00, /* wcChar16 */
+ 'O', 0x00, /* wcChar17 */
+ ///////////////////////////////////////
+ /// string3 descriptor
+ ///////////////////////////////////////
+ 0x16, /* bLength */
+ USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
+ '2', 0x00, /* wcChar0 */
+ '0', 0x00, /* wcChar1 */
+ '2', 0x00, /* wcChar2 */
+ '1', 0x00, /* wcChar3 */
+ '0', 0x00, /* wcChar4 */
+ '3', 0x00, /* wcChar5 */
+ '1', 0x00, /* wcChar6 */
+ '0', 0x00, /* wcChar7 */
+ '0', 0x00, /* wcChar8 */
+ '0', 0x00, /* wcChar9 */
+ ///////////////////////////////////////
+ /// device qualifier descriptor
+ ///////////////////////////////////////
+ 0x0a,
+ USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
+ 0x00,
+ 0x02,
+ 0x02,
+ 0x02,
+ 0x01,
+ 0x40,
+ 0x01,
+ 0x00,
+
+ 0x00
+};
+
+
+
+void usb_send(uint8_t *buf, uint32_t len)
+{
+// while (len >= 64) {
+// usbd_ep_write(0x82, buf, 64, NULL);
+// buf += 64;
+// len -= 64;
+// }
+ usbd_ep_write(0x82, buf, len, NULL);
+ if(len == 64){
+ usbd_ep_write(0x82, NULL, 0, NULL);
+ }
+// if (len > 0) {
+// usbd_ep_write(0x82, buf, len, NULL);
+// } else {
+// usbd_ep_write(0x82, NULL, 0, NULL);
+// }
+}
+
+void usbd_cdc_acm_bulk_out(uint8_t ep)
+{
+ uint32_t actual_read_length = 0;
+ uint8_t out_buffer[64];
+ uint8_t *buf = g_eflash_loader_readbuf[g_rx_buf_index];
+
+
+ if (usbd_ep_read(ep, out_buffer, 64, &actual_read_length) < 0) {
+ USBD_LOG_DBG("Read DATA Packet failed\r\n");
+ usbd_ep_set_stall(ep);
+ return;
+ }
+
+// bflb_dump_data(out_buffer,actual_read_length);
+// usb_send(out_buffer,actual_read_length);
+
+ if(g_rx_buf_len + actual_read_length < BFLB_EFLASH_LOADER_READBUF_SIZE){
+ arch_memcpy(buf + g_rx_buf_len, out_buffer, actual_read_length);
+ g_rx_buf_len += actual_read_length;
+ }else{
+ g_rx_buf_len = 0;
+ }
+ //MSG("%d\r\n",g_rx_buf_len);
+ usbd_ep_read(ep, NULL, 0, NULL);
+
+
+}
+
+usbd_class_t cdc_class;
+usbd_interface_t cdc_cmd_intf;
+usbd_interface_t cdc_data_intf;
+
+usbd_endpoint_t cdc_out_ep = {
+ .ep_addr = CDC_OUT_EP,
+ .ep_cb = usbd_cdc_acm_bulk_out
+};
+
+usbd_endpoint_t cdc_in_ep = {
+ .ep_addr = CDC_IN_EP,
+ .ep_cb = NULL
+};
+
+extern struct device *usb_dc_init(void);
+
+
+void bflb_eflash_loader_usb_if_init(void)
+{
+ struct device *usb_fs;
+
+ hal_boot2_debug_usb_port_init();
+
+ simple_malloc_init(g_malloc_buf, sizeof(g_malloc_buf));
+ g_eflash_loader_readbuf[0] = vmalloc(BFLB_EFLASH_LOADER_READBUF_SIZE);
+ g_eflash_loader_readbuf[1] = vmalloc(BFLB_EFLASH_LOADER_READBUF_SIZE);
+ arch_memset(g_eflash_loader_readbuf[0], 0, BFLB_EFLASH_LOADER_READBUF_SIZE);
+ arch_memset(g_eflash_loader_readbuf[1], 0, BFLB_EFLASH_LOADER_READBUF_SIZE);
+
+ usbd_desc_register(cdc_descriptor);
+
+ usbd_cdc_add_acm_interface(&cdc_class, &cdc_cmd_intf);
+ usbd_cdc_add_acm_interface(&cdc_class, &cdc_data_intf);
+ usbd_interface_add_endpoint(&cdc_data_intf, &cdc_out_ep);
+ usbd_interface_add_endpoint(&cdc_data_intf, &cdc_in_ep);
+
+ usb_fs = usb_dc_init();
+
+ if (usb_fs) {
+ device_control(usb_fs, DEVICE_CTRL_SET_INT, (void *)(USB_EP1_DATA_OUT_IT));
+ }
+
+ while (!usb_device_is_configured()) {
+ }
+
+}
+
+
+int32_t bflb_eflash_loader_usb_send(uint32_t *data, uint32_t len)
+{
+ usb_send((uint8_t *)data,len);
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+int32_t bflb_eflash_loader_usb_wait_tx_idle(uint32_t timeout)
+{
+ /*UART now can't judge tx idle now*/
+ bflb_platform_delay_ms(timeout);
+
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+
+
+int32_t bflb_eflash_loader_usb_init()
+{
+ bflb_eflash_loader_usb_deinit();
+ bflb_eflash_loader_usb_if_init();
+
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+int32_t bflb_eflash_loader_check_handshake_buf(uint8_t *buf, uint32_t len)
+{
+ if(len == 0)
+ return -1;
+
+ for(int i = 0; i < len; i++){
+ if(buf[i] != BFLB_EFLASH_LOADER_HAND_SHAKE_BYTE)
+ return -1;
+ }
+ return 0;
+}
+
+int32_t bflb_eflash_loader_usb_handshake_poll(uint32_t timeout)
+{
+ uint64_t time_now;
+ uint32_t handshake_count = 0;
+ uint8_t *buf = g_eflash_loader_readbuf[g_rx_buf_index];
+
+ time_now = bflb_platform_get_time_ms();
+
+ MSG("usb iap handshake poll\r\n");
+ do {
+ if (g_rx_buf_len >= 16) {
+ for (int i = 0; i < 16; i++) {
+ if (buf[i] == BFLB_EFLASH_LOADER_HAND_SHAKE_BYTE) {
+ handshake_count++;
+
+ if (handshake_count > BFLB_EFLASH_LAODER_HAND_SHAKE_SUSS_COUNT) {
+ break;
+ }
+ }
+ }
+ if (handshake_count > BFLB_EFLASH_LAODER_HAND_SHAKE_SUSS_COUNT) {
+ break;
+ }
+ }
+ } while ((timeout == 0) ? 1 : (bflb_platform_get_time_ms() - time_now < timeout * 1000));
+
+
+
+ if (handshake_count >= BFLB_EFLASH_LAODER_HAND_SHAKE_SUSS_COUNT) {
+ MSG("iap handshake %d 0x55 rcv\r\n", handshake_count);
+
+ } else {
+ MSG("iap handshake err\r\n");
+ return -1;
+ }
+
+ /*receive shake hanad signal*/
+ usb_send((uint8_t *)"OK", 2);
+ //ARCH_Delay_MS(400);
+ bflb_platform_delay_ms(400);
+ /*init rx info */
+ g_rx_buf_index = 0;
+ g_rx_buf_len = 0;
+
+ time_now = bflb_platform_get_time_ms();
+ do{
+ if(g_rx_buf_len > 0){
+ if(0 == bflb_eflash_loader_check_handshake_buf(buf, g_rx_buf_len)){
+ g_rx_buf_len = 0;
+ time_now = bflb_platform_get_time_ms();
+ }else{
+ break;
+ }
+ }
+ }while(bflb_platform_get_time_ms() - time_now < 50);
+
+
+
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+uint32_t *bflb_eflash_loader_usb_recv(uint32_t *recv_len, uint32_t maxlen, uint32_t timeout)
+{
+ return NULL;
+}
+
+int32_t bflb_eflash_loader_usb_change_rate(uint32_t oldval, uint32_t newval)
+{
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+
+int32_t bflb_eflash_loader_usb_deinit()
+{
+ return BFLB_EFLASH_LOADER_SUCCESS;
+}
+#endif
+
+
diff --git a/examples/boot2_iap/bflb_eflash_loader_usb.h b/examples/boot2_iap/bflb_eflash_loader_usb.h
new file mode 100644
index 00000000..35e30297
--- /dev/null
+++ b/examples/boot2_iap/bflb_eflash_loader_usb.h
@@ -0,0 +1,60 @@
+/**
+ ******************************************************************************
+ * @file blsp_eflash_loader_usb.h
+ * @version V1.2
+ * @date
+ * @brief This file is the peripheral case header file
+ ******************************************************************************
+ * @attention
+ *
+ * © COPYRIGHT(c) 2018 Bouffalo Lab
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Bouffalo Lab nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+#ifndef __BFLB_EFLASH_LOADER_USB_H__
+#define __BFLB_EFLASH_LOADER_USB_H__
+
+#include "stdint.h"
+
+int32_t bflb_eflash_loader_usb_init();
+
+int32_t bflb_eflash_loader_usb_handshake_poll();
+
+uint32_t *bflb_eflash_loader_usb_recv(uint32_t *recv_len, uint32_t maxlen, uint32_t timeout);
+
+int32_t bflb_eflash_loader_usb_send(uint32_t *data, uint32_t len);
+
+int32_t bflb_eflash_loader_usb_wait_tx_idle(uint32_t timeout);
+
+int32_t bflb_eflash_loader_usb_change_rate(uint32_t oldval, uint32_t newval);
+
+int32_t bflb_eflash_loader_usb_deinit(void);
+
+
+
+
+#define BFLB_EFLASH_LOADER_IF_USB_RX_TIMEOUT 8000 /*ms*/
+
+#endif
diff --git a/examples/boot2_iap/blsp_boot2_iap.c b/examples/boot2_iap/blsp_boot2_iap.c
index b88a6a42..285ac7eb 100644
--- a/examples/boot2_iap/blsp_boot2_iap.c
+++ b/examples/boot2_iap/blsp_boot2_iap.c
@@ -35,7 +35,7 @@
*/
#include "bflb_platform.h"
-#include "bflb_eflash_loader_uart.h"
+#include "bflb_eflash_loader_interface.h"
#include "blsp_port.h"
#include "blsp_bootinfo.h"
#include "blsp_media_boot.h"
@@ -50,12 +50,13 @@
#include "hal_clock.h"
-uint8_t g_boot2_read_buf[BFLB_BOOT2_READBUF_SIZE] __attribute__((section(".noinit_data")));
+uint8_t *g_boot2_read_buf;
boot2_image_config g_boot_img_cfg[2];
boot2_efuse_hw_config g_efuse_cfg;
uint8_t g_ps_mode = BFLB_PSM_ACTIVE;
uint8_t g_cpu_count;
uint32_t g_user_hash_ignored = 0;
+uint8_t g_usb_init_flag = 0;
@@ -70,7 +71,19 @@ uint32_t g_user_hash_ignored = 0;
static void blsp_boot2_on_error(void *log)
{
while (1) {
- if (BFLB_EFLASH_LOADER_HANDSHAKE_SUSS == bflb_eflash_loader_uart_handshake_poll()) {
+#if BLSP_BOOT2_SUPPORT_USB_IAP
+ bflb_eflash_loader_if_set(BFLB_EFLASH_LOADER_IF_USB);
+ if(0 == g_usb_init_flag){
+ bflb_eflash_loader_if_init();
+ g_usb_init_flag = 1;
+ }
+
+ if (0 == bflb_eflash_loader_if_handshake_poll(1)) {
+ bflb_eflash_loader_main();
+ }
+#endif
+ bflb_eflash_loader_if_set(BFLB_EFLASH_LOADER_IF_UART);
+ if (0 == bflb_eflash_loader_if_handshake_poll(0)) {
bflb_eflash_loader_main();
}
@@ -87,12 +100,12 @@ static void blsp_boot2_on_error(void *log)
* @return None
*
*******************************************************************************/
-static void blsp_dump_pt_entry(pt_table_entry_config *pt_entry)
+void blsp_dump_pt_entry(pt_table_entry_config *pt_entry)
{
MSG("Name=%s\r\n", pt_entry->name);
MSG("Age=%d\r\n", (unsigned int)pt_entry->age);
MSG("active Index=%d\r\n", (unsigned int)pt_entry->active_index);
- MSG("active start_address=%08x\r\n", (unsigned int)pt_entry->start_address[pt_entry->active_index]);
+ MSG("active start_address=%08x\r\n", (unsigned int)pt_entry->start_address[pt_entry->active_index & 0x01]);
}
/****************************************************************************/ /**
@@ -163,6 +176,8 @@ static int blsp_boot2_do_fw_copy(pt_table_id_type active_id, pt_table_stuff_conf
uint32_t deal_len = 0;
uint32_t cur_len = 0;
+ MSG("OTA copy src address %08x, dest address %08x, total len %d\r\n",src_address,dest_address,total_len);
+
if (SUCCESS != flash_erase(dest_address, dest_max_size)) {
MSG_ERR("Erase flash fail");
return BFLB_BOOT2_FLASH_ERASE_ERROR;
@@ -171,8 +186,8 @@ static int blsp_boot2_do_fw_copy(pt_table_id_type active_id, pt_table_stuff_conf
while (deal_len < total_len) {
cur_len = total_len - deal_len;
- if (cur_len > sizeof(g_boot2_read_buf)) {
- cur_len = sizeof(g_boot2_read_buf);
+ if (cur_len > BFLB_BOOT2_READBUF_SIZE) {
+ cur_len = BFLB_BOOT2_READBUF_SIZE;
}
if (BFLB_BOOT2_SUCCESS != blsp_mediaboot_read(src_address, g_boot2_read_buf, cur_len)) {
@@ -222,7 +237,7 @@ static int blsp_boot2_deal_one_fw(pt_table_id_type active_id, pt_table_stuff_con
if (PT_ERROR_SUCCESS != ret) {
MSG_ERR("Entry not found\r\n");
} else {
- blsp_dump_pt_entry(pt_entry);
+ //blsp_dump_pt_entry(pt_entry);
MSG("Check Img\r\n");
#if BLSP_BOOT2_SUPPORT_DECOMPRESS
if(blsp_boot2_check_xz_fw(active_id,pt_stuff,pt_entry)==1){
@@ -231,6 +246,7 @@ static int blsp_boot2_deal_one_fw(pt_table_id_type active_id, pt_table_stuff_con
#endif
/* Check if this partition need copy */
if (pt_entry->active_index >= 2) {
+ MSG("Find OTA image, do image copy\r\n");
if (BFLB_BOOT2_SUCCESS == blsp_boot2_do_fw_copy(active_id, pt_stuff, pt_entry)) {
pt_entry->active_index = !(pt_entry->active_index & 0x01);
@@ -240,7 +256,10 @@ static int blsp_boot2_deal_one_fw(pt_table_id_type active_id, pt_table_stuff_con
if (ret != PT_ERROR_SUCCESS) {
MSG_ERR("Update Partition table entry fail, After Image Copy\r\n");
return BFLB_BOOT2_FAIL;
+ }else{
+ MSG("OTA image copy done\r\n");
}
+
return 0;
}
}
@@ -296,7 +315,7 @@ static void blsp_boot2_get_mfg_startreq(pt_table_id_type active_id, pt_table_stu
ret = pt_table_get_active_entries_by_name(pt_stuff, (uint8_t *)"mfg", pt_entry);
if (PT_ERROR_SUCCESS == ret) {
- MSG("XIP_SFlash_Read_Need_Lock_Ext:%08x,", pt_entry->start_address[0] + MFG_START_REQUEST_OFFSET);
+ MSG("read mfg flag addr:%08x,", pt_entry->start_address[0] + MFG_START_REQUEST_OFFSET);
flash_read(pt_entry->start_address[0] + MFG_START_REQUEST_OFFSET, tmp, sizeof(tmp) - 1);
MSG("%s\r\n", tmp);
if (tmp[0] == '0' || tmp[0] == '1') {
@@ -330,6 +349,7 @@ int main(void)
uint8_t boot_need_rollback[BFLB_BOOT2_CPU_MAX] = { 0 };
uint8_t pt_parsed = 1;
uint8_t user_fw_name[9] = { 0 };
+ uint32_t user_fw;
#ifdef BLSP_BOOT2_ROLLBACK
uint8_t roll_backed = 0;
#endif
@@ -343,7 +363,8 @@ int main(void)
system_mtimer_clock_init();
peripheral_clock_init();
-
+
+
#if (BLSP_BOOT2_MODE == BOOT2_MODE_RELEASE)
bflb_platform_print_set(1);
#endif
@@ -356,8 +377,17 @@ int main(void)
bflb_platform_print_set(0);
hal_boot2_debug_uart_gpio_init();
#endif
- hal_boot2_uart_gpio_init();
- bflb_eflash_loader_uart_init();
+
+
+ bflb_eflash_loader_if_set(BFLB_EFLASH_LOADER_IF_UART);
+ bflb_eflash_loader_if_init();
+
+
+
+
+ simple_malloc_init(g_malloc_buf, sizeof(g_malloc_buf));
+ g_boot2_read_buf = vmalloc(BFLB_BOOT2_READBUF_SIZE);
+
hal_boot2_custom();
flash_init();
@@ -370,7 +400,6 @@ int main(void)
} else {
MSG("BLSP_Boot2_SP:%s,%s\r\n", __DATE__, __TIME__);
}
-
#ifdef BL_SDK_VER
MSG("sdk:%s\r\n", BL_SDK_VER);
#else
@@ -396,12 +425,27 @@ int main(void)
g_ps_mode = blsp_read_power_save_mode();
/* Get User specified FW */
- arch_memcpy(user_fw_name, hal_boot2_get_user_fw(), 4);
-
+ user_fw = hal_boot2_get_user_fw();
+ arch_memcpy(user_fw_name, &user_fw, 4);
+ MSG("user_fw %s\r\n",user_fw_name);
/* Set flash operation function, read via xip */
pt_table_set_flash_operation(flash_erase, flash_write, flash_read);
+#if BLSP_BOOT2_SUPPORT_USB_IAP
+ if(memcmp(user_fw_name,(char *)"USB",3) == 0){
+ hal_boot2_clr_user_fw();
+ bflb_eflash_loader_if_set(BFLB_EFLASH_LOADER_IF_USB);
+ bflb_eflash_loader_if_init();
+ g_usb_init_flag = 1;
+ if (0 == bflb_eflash_loader_if_handshake_poll(user_fw_name[3])) {
+ bflb_eflash_loader_main();
+ }
+ }
+#endif
+
+ pt_table_dump();
+
while (1) {
mfg_mode_flag = 0;
@@ -412,7 +456,7 @@ int main(void)
blsp_boot2_on_error("No valid PT\r\n");
}
- MSG("Active PT:%d,%d\r\n", active_id, pt_table_stuff[active_id].pt_table.age);
+ MSG("Active PT:%d,Age %d\r\n", active_id, pt_table_stuff[active_id].pt_table.age);
blsp_boot2_get_mfg_startreq(active_id, &pt_table_stuff[active_id], &pt_entry[0], user_fw_name);
@@ -499,7 +543,7 @@ int main(void)
break;
}
- MSG("Boot return %d\r\n", ret);
+ MSG("Boot return 0x%04x\r\n", ret);
MSG("Check Rollback\r\n");
for (i = 0; i < g_cpu_count; i++) {
@@ -526,7 +570,19 @@ int main(void)
MSG_ERR("Media boot return %d\r\n", ret);
while (1) {
- if (BFLB_EFLASH_LOADER_HANDSHAKE_SUSS == bflb_eflash_loader_uart_handshake_poll()) {
+#if BLSP_BOOT2_SUPPORT_USB_IAP
+ bflb_eflash_loader_if_set(BFLB_EFLASH_LOADER_IF_USB);
+ if(0 == g_usb_init_flag){
+ bflb_eflash_loader_if_init();
+ g_usb_init_flag = 1;
+ }
+
+ if (0 == bflb_eflash_loader_if_handshake_poll(1)) {
+ bflb_eflash_loader_main();
+ }
+#endif
+ bflb_eflash_loader_if_set(BFLB_EFLASH_LOADER_IF_UART);
+ if (0 == bflb_eflash_loader_if_handshake_poll(0)) {
bflb_eflash_loader_main();
}
diff --git a/examples/boot2_iap/blsp_boot_decompress.c b/examples/boot2_iap/blsp_boot_decompress.c
index 6e416f25..042e53b7 100644
--- a/examples/boot2_iap/blsp_boot_decompress.c
+++ b/examples/boot2_iap/blsp_boot_decompress.c
@@ -43,10 +43,9 @@
#include "partition.h"
#include "hal_flash.h"
-#define BFLB_BOOT2_XZ_MALLOC_BUF_SIZE 64*1024
-static uint8_t g_xz_output[BFLB_BOOT2_READBUF_SIZE] __attribute__((section(".noinit_data")));
-static uint8_t g_malloc_buf[BFLB_BOOT2_XZ_MALLOC_BUF_SIZE] __attribute__((section(".noinit_data")));
+#define BFLB_BOOT2_XZ_WRITE_BUF_SIZE 4*1024
+#define BFLB_BOOT2_XZ_READ_BUF_SIZE 4*1024
@@ -87,30 +86,30 @@ static int32_t blsp_boot2_fw_decompress(uint32_t src_address, uint32_t dest_addr
return BFLB_BOOT2_MEM_ERROR;
}
- b.in = g_boot2_read_buf;
+ b.in = vmalloc(BFLB_BOOT2_XZ_READ_BUF_SIZE);
b.in_pos = 0;
b.in_size = 0;
- b.out = g_xz_output;
+ b.out = vmalloc(BFLB_BOOT2_XZ_WRITE_BUF_SIZE);
b.out_pos = 0;
- b.out_size = sizeof(g_xz_output);
+ b.out_size = BFLB_BOOT2_XZ_WRITE_BUF_SIZE;
while (1) {
if (b.in_pos == b.in_size) {
MSG("XZ Feeding\r\n");
- if (BFLB_BOOT2_SUCCESS != blsp_mediaboot_read(src_address, g_boot2_read_buf, sizeof(g_boot2_read_buf))) {
+ if (BFLB_BOOT2_SUCCESS != blsp_mediaboot_read(src_address, (uint8_t *)b.in, BFLB_BOOT2_XZ_READ_BUF_SIZE)) {
MSG_ERR("Read XZFW fail\r\n");
return BFLB_BOOT2_FLASH_READ_ERROR;
}
- b.in_size = sizeof(g_boot2_read_buf);
+ b.in_size = BFLB_BOOT2_XZ_READ_BUF_SIZE;
b.in_pos = 0;
- src_address += sizeof(g_boot2_read_buf);
+ src_address += BFLB_BOOT2_XZ_READ_BUF_SIZE;
}
ret = xz_dec_run(s, &b);
- if (b.out_pos == sizeof(g_xz_output)) {
+ if (b.out_pos == BFLB_BOOT2_XZ_WRITE_BUF_SIZE) {
//if (fwrite(out, 1, b.out_pos, stdout) != b.out_pos) {
// msg = "Write error\n";
// goto error;
@@ -118,11 +117,11 @@ static int32_t blsp_boot2_fw_decompress(uint32_t src_address, uint32_t dest_addr
MSG("XZ outputing\r\n");
if (dest_max_size > 0) {
- flash_write(dest_address, g_xz_output, sizeof(g_xz_output));
+ flash_write(dest_address, b.out, BFLB_BOOT2_XZ_WRITE_BUF_SIZE);
}
- dest_address += sizeof(g_xz_output);
- *p_dest_size += sizeof(g_xz_output);
+ dest_address += BFLB_BOOT2_XZ_WRITE_BUF_SIZE;
+ *p_dest_size += BFLB_BOOT2_XZ_WRITE_BUF_SIZE;
b.out_pos = 0;
}
@@ -137,7 +136,7 @@ static int32_t blsp_boot2_fw_decompress(uint32_t src_address, uint32_t dest_addr
//}
if (b.out_pos > 0) {
if (dest_max_size > 0) {
- flash_write(dest_address, g_xz_output, b.out_pos);
+ flash_write(dest_address, b.out, b.out_pos);
}
dest_address += b.out_pos;
@@ -198,6 +197,9 @@ int32_t blsp_boot2_update_fw(pt_table_id_type active_id, pt_table_stuff_config *
uint32_t new_fw_len;
int32_t ret;
+ MSG("try to decompress,xz start address %08x,dest address %08x\r\n",\
+ pt_entry->start_address[active_index],\
+ pt_entry->start_address[!(active_index & 0x01)]);
/* Try to check Image integrity: try to decompress */
if (BFLB_BOOT2_SUCCESS != blsp_boot2_fw_decompress(pt_entry->start_address[active_index],
@@ -217,8 +219,29 @@ int32_t blsp_boot2_update_fw(pt_table_id_type active_id, pt_table_stuff_config *
return BFLB_BOOT2_SUCCESS;
}
+ MSG("get new fw len %d\r\n",new_fw_len);
+
+ if(new_fw_len > pt_entry->max_len[!(active_index & 0x01)]){
+ MSG("decompressed image will overlap partition table max size, quit!\r\n");
+#ifdef BLSP_BOOT2_ROLLBACK
+ /* Decompress fail, try to rollback to old one */
+ pt_entry->active_index = !(active_index & 0x01);
+ pt_entry->age++;
+ ret = pt_table_update_entry((pt_table_id_type)(!active_id), pt_stuff, pt_entry);
+
+ if (ret != PT_ERROR_SUCCESS) {
+ MSG_ERR("Rollback Update Partition table entry fail\r\n");
+ return BFLB_BOOT2_FAIL;
+ }
+#endif
+ return BFLB_BOOT2_SUCCESS;
+ }
+ MSG("Do decompress,xz start address %08x,dest address %08x\r\n",\
+ pt_entry->start_address[active_index],\
+ pt_entry->start_address[!(active_index & 0x01)]);
+
/* Do decompress */
if (BFLB_BOOT2_SUCCESS == blsp_boot2_fw_decompress(pt_entry->start_address[active_index],
pt_entry->start_address[!(active_index & 0x01)],
@@ -237,6 +260,7 @@ int32_t blsp_boot2_update_fw(pt_table_id_type active_id, pt_table_stuff_config *
return BFLB_BOOT2_FAIL;
}
+ MSG("get new fw len %d\r\n",new_fw_len);
return BFLB_BOOT2_SUCCESS;
}
diff --git a/examples/boot2_iap/blsp_boot_parser.c b/examples/boot2_iap/blsp_boot_parser.c
index 44953916..c45a9adf 100644
--- a/examples/boot2_iap/blsp_boot_parser.c
+++ b/examples/boot2_iap/blsp_boot_parser.c
@@ -87,6 +87,7 @@ int32_t blsp_boot_parse_bootheader(boot2_image_config *boot_img_cfg, uint8_t *da
uint32_t crc_pass = 0;
uint32_t i = 0;
uint32_t *phash = (uint32_t *)header->hash;
+ int32_t ret;
if (header->bootCfg.bval.crcIgnore == 1 && header->crc32 == BFLB_BOOT2_DEADBEEF_VAL) {
MSG("Crc ignored\r\n");
@@ -187,12 +188,17 @@ int32_t blsp_boot_parse_bootheader(boot2_image_config *boot_img_cfg, uint8_t *da
/* Start hash here*/
//Sec_Eng_SHA256_Init(&g_sha_ctx, SEC_ENG_SHA_ID0, SEC_ENG_SHA256, g_sha_tmp_buf, g_padding);
//Sec_Eng_SHA_Start(SEC_ENG_SHA_ID0);
+ device_unregister("dev_check_hash");
sec_hash_sha256_register(SEC_HASH0_INDEX,"dev_check_hash");
dev_check_hash = device_find("dev_check_hash");
if(dev_check_hash){
- device_open(dev_check_hash, 0);
+ ret = device_open(dev_check_hash, 0);
+ if(ret){
+ MSG_ERR("hash dev open err\r\n");
+ return BFLB_BOOT2_FAIL;
+ }
}else{
- MSG_ERR("hash dev open err\r\n");
+ MSG_ERR("hash dev find err\r\n");
return BFLB_BOOT2_FAIL;
}
@@ -219,6 +225,7 @@ int32_t blsp_boot_parse_pkey(boot2_image_config *g_boot_img_cfg, uint8_t *data,
{
boot_pk_config *cfg = (boot_pk_config *)data;
uint32_t pk_hash[BFLB_BOOT2_PK_HASH_SIZE / 4];
+ int32_t ret;
if (cfg->crc32 == BFLB_Soft_CRC32((uint8_t *)cfg, sizeof(boot_pk_config) - 4)) {
@@ -231,7 +238,20 @@ int32_t blsp_boot_parse_pkey(boot2_image_config *g_boot_img_cfg, uint8_t *data,
device_write(dev_check_hash, 0, data, BFLB_BOOT2_ECC_KEYXSIZE + BFLB_BOOT2_ECC_KEYYSIZE);
device_read(dev_check_hash,0,pk_hash,0);
- device_open(dev_check_hash, 0);
+
+ device_unregister("dev_check_hash");
+ sec_hash_sha256_register(SEC_HASH0_INDEX,"dev_check_hash");
+ dev_check_hash = device_find("dev_check_hash");
+ if(dev_check_hash){
+ ret = device_open(dev_check_hash, 0);
+ if(ret){
+ MSG_ERR("hash dev open err\r\n");
+ return BFLB_BOOT2_FAIL;
+ }
+ }else{
+ MSG_ERR("hash dev find err\r\n");
+ return BFLB_BOOT2_FAIL;
+ }
/* Check pk is valid */
if (own == 1) {
@@ -335,7 +355,7 @@ int32_t blsp_boot_parser_check_signature(boot2_image_config *g_boot_img_cfg)
uint64_t startTime = 0;
sec_ecdsa_handle_t ecdsaHandle;
- MSG("%d,%d\r\n", g_ps_mode, g_efuse_cfg.hbn_check_sign);
+ MSG("ps_mode %d,efuse hbn_check_sign %d\r\n", g_ps_mode, g_efuse_cfg.hbn_check_sign);
if(g_ps_mode == BFLB_PSM_HBN && (!g_efuse_cfg.hbn_check_sign))
{
@@ -362,10 +382,9 @@ int32_t blsp_boot_parser_check_signature(boot2_image_config *g_boot_img_cfg)
return BFLB_BOOT2_IMG_SIGN_ERROR;
}
- MSG("Time=%d ms\r\n", (unsigned int)(bflb_platform_get_time_ms() - startTime));
+ MSG("Sign suss,Time=%d ms\r\n", (unsigned int)(bflb_platform_get_time_ms() - startTime));
}
- MSG("Success\r\n");
return BFLB_BOOT2_SUCCESS;
}
@@ -394,7 +413,7 @@ int32_t blsp_boot_parser_check_hash(boot2_image_config *g_boot_img_cfg)
blsp_dump_data(g_boot_img_cfg->img_hash, BFLB_BOOT2_HASH_SIZE);
return BFLB_BOOT2_IMG_HASH_ERROR;
} else {
- MSG("Success\r\n");
+ MSG("Hash Success\r\n");
}
}
diff --git a/examples/boot2_iap/blsp_bootinfo.h b/examples/boot2_iap/blsp_bootinfo.h
index 5f1ce0bb..9d139cf6 100644
--- a/examples/boot2_iap/blsp_bootinfo.h
+++ b/examples/boot2_iap/blsp_bootinfo.h
@@ -259,7 +259,7 @@ extern boot2_image_config g_boot_img_cfg[2];
extern boot2_efuse_hw_config g_efuse_cfg;
extern uint8_t g_ps_mode;
extern uint8_t g_cpu_count;
-extern uint8_t g_boot2_read_buf[BFLB_BOOT2_READBUF_SIZE];
+extern uint8_t *g_boot2_read_buf;
diff --git a/examples/boot2_iap/blsp_common.c b/examples/boot2_iap/blsp_common.c
index 08f974e4..21bb7384 100644
--- a/examples/boot2_iap/blsp_common.c
+++ b/examples/boot2_iap/blsp_common.c
@@ -46,6 +46,8 @@
#include "hal_boot2.h"
#include "bflb_eflash_loader.h"
+uint8_t g_malloc_buf[BFLB_BOOT2_XZ_MALLOC_BUF_SIZE] __attribute__((section(".noinit_data")));
+
int32_t blsp_boot2_set_encrypt(uint8_t index, boot2_image_config *g_boot_img_cfg);
/****************************************************************************/ /**
diff --git a/examples/boot2_iap/blsp_common.h b/examples/boot2_iap/blsp_common.h
index 0a8400a5..c3974278 100644
--- a/examples/boot2_iap/blsp_common.h
+++ b/examples/boot2_iap/blsp_common.h
@@ -38,6 +38,8 @@
#include "stdint.h"
+#define BFLB_BOOT2_XZ_MALLOC_BUF_SIZE 80*1024
+
#define BLSP_BOOT2_CP_FLAG 0x02
#define BLSP_BOOT2_MP_FLAG 0x01
#define BLSP_BOOT2_SP_FLAG 0x00
@@ -53,6 +55,7 @@ uint32_t blsp_boot2_get_baudrate(void);
uint8_t blsp_boot2_get_tx_gpio(void);
+extern uint8_t g_malloc_buf[BFLB_BOOT2_XZ_MALLOC_BUF_SIZE];
#endif /* __BLSP_COMMON_H__ */
diff --git a/examples/boot2_iap/blsp_media_boot.c b/examples/boot2_iap/blsp_media_boot.c
index 0bae7508..d789ec03 100644
--- a/examples/boot2_iap/blsp_media_boot.c
+++ b/examples/boot2_iap/blsp_media_boot.c
@@ -43,7 +43,7 @@
#include "blsp_boot_parser.h"
#include "blsp_media_boot.h"
#include "softcrc.h"
-#include "bflb_eflash_loader_uart.h"
+#include "bflb_eflash_loader_interface.h"
extern int main(void);
extern struct device *dev_check_hash;
@@ -272,6 +272,7 @@ static int32_t blsp_mediaboot_parse_one_fw(boot2_image_config *boot_img_cfg, uin
/* Flash image */
if (!boot_img_cfg->hash_ignore) {
MSG("Cal hash\r\n");
+ MSG("calc hash addr 0x%08x,len %d\r\n",img_addr,boot_img_cfg->img_segment_info.img_len);
ret = blsp_mediaboot_cal_hash(img_addr,
boot_img_cfg->img_segment_info.img_len);
@@ -281,6 +282,7 @@ static int32_t blsp_mediaboot_parse_one_fw(boot2_image_config *boot_img_cfg, uin
}
ret = blsp_boot_parser_check_hash(boot_img_cfg);
+ device_unregister("dev_check_hash");
if (ret != BFLB_BOOT2_SUCCESS) {
return ret;
@@ -403,15 +405,15 @@ int32_t blsp_mediaboot_main(uint32_t cpu_boot_header_addr[BFLB_BOOT2_CPU_MAX], u
g_boot_img_cfg[1].cache_way_disable = 0xf;
}
- MSG("%08x,%08x\r\n", g_boot_img_cfg[0].msp_val, g_boot_img_cfg[0].entry_point);
- MSG("%08x,%08x\r\n", g_boot_img_cfg[1].msp_val, g_boot_img_cfg[1].entry_point);
- MSG("%08x,%08x\r\n", g_boot_img_cfg[0].img_start.flash_offset, g_boot_img_cfg[0].cache_way_disable);
- MSG("%08x,%08x\r\n", g_boot_img_cfg[1].img_start.flash_offset, g_boot_img_cfg[1].cache_way_disable);
- MSG("CPU Count %d,%d\r\n", g_cpu_count, g_boot_img_cfg[0].halt_cpu1);
+ //MSG("%08x,%08x\r\n", g_boot_img_cfg[0].msp_val, g_boot_img_cfg[0].entry_point);
+ //MSG("%08x,%08x\r\n", g_boot_img_cfg[1].msp_val, g_boot_img_cfg[1].entry_point);
+ MSG("flash_offset[0] %08x,cache_dis[0] %02x\r\n", g_boot_img_cfg[0].img_start.flash_offset, g_boot_img_cfg[0].cache_way_disable);
+ MSG("flash_offset[1] %08x,cache_dis[1] %02x\r\n", g_boot_img_cfg[1].img_start.flash_offset, g_boot_img_cfg[1].cache_way_disable);
+ MSG("CPU Count %d,halt_cpu1 %d\r\n", g_cpu_count, g_boot_img_cfg[0].halt_cpu1);
blsp_boot2_show_timer();
- if (BFLB_EFLASH_LOADER_HANDSHAKE_SUSS == bflb_eflash_loader_uart_handshake_poll()) {
+ if (0 == bflb_eflash_loader_if_handshake_poll(0)) {
bflb_eflash_loader_main();
}
diff --git a/examples/boot2_iap/blsp_port.c b/examples/boot2_iap/blsp_port.c
index 90fdb1d4..45982e63 100644
--- a/examples/boot2_iap/blsp_port.c
+++ b/examples/boot2_iap/blsp_port.c
@@ -102,7 +102,7 @@ uint8_t blsp_read_power_save_mode(void)
void blsp_boot2_pass_parameter(void *data, uint32_t len)
{
static uint8_t *p_parameter = NULL;
- MSG("boot2_pass_param_addr %08x\r\n", &__boot2_pass_param_addr);
+
if (len == 0) {
//GLB_Set_EM_Sel(0); //system init has done
//p_parameter = (uint8_t *)(0x42020000 + 60 * 1024);
@@ -111,6 +111,7 @@ void blsp_boot2_pass_parameter(void *data, uint32_t len)
return;
}
+ MSG("pass param addr %08x,len %d\r\n", p_parameter,len);
ARCH_MemCpy_Fast(p_parameter, data, len);
p_parameter += len;
}
diff --git a/examples/boot2_iap/blsp_port.h b/examples/boot2_iap/blsp_port.h
index 47297b4b..56738b7c 100644
--- a/examples/boot2_iap/blsp_port.h
+++ b/examples/boot2_iap/blsp_port.h
@@ -39,17 +39,22 @@
#include "stdint.h"
#include "misc.h"
#include "hal_flash.h"
+#include "xz_config.h"
-#define MFG_START_REQUEST_OFFSET ((4 + 184) * 1024)
-#define BLSP_BOOT2_XIP_BASE BL_FLASH_XIP_BASE
+#define MFG_START_REQUEST_OFFSET ((4 + 184) * 1024)
+#define BLSP_BOOT2_XIP_BASE BL_FLASH_XIP_BASE
#define BLSP_BOOT2_ROLLBACK
-#define BLSP_BOOT2_SUPPORT_DECOMPRESS HAL_BOOT2_SUPPORT_DECOMPRESS
+#define BLSP_BOOT2_SUPPORT_DECOMPRESS HAL_BOOT2_SUPPORT_DECOMPRESS
+#define BLSP_BOOT2_SUPPORT_USB_IAP HAL_BOOT2_SUPPORT_USB_IAP
+#define BLSP_BOOT2_SUPPORT_EFLASH_LOADER_RAM HAL_BOOT2_SUPPORT_EFLASH_LOADER_RAM
+#define BLSP_BOOT2_SUPPORT_EFLASH_LOADER_FLASH HAL_BOOT2_SUPPORT_EFLASH_LOADER_FLASH
+
#define BOOT2_MODE_RELEASE 0x01
#define BOOT2_MODE_DEBUG 0x02
#define BOOT2_MODE_DEEP_DEBUG 0x04
-#define BLSP_BOOT2_MODE BOOT2_MODE_RELEASE
+#define BLSP_BOOT2_MODE BOOT2_MODE_DEBUG
uint32_t blsp_boot2_get_cpu_count(void);
diff --git a/examples/boot2_iap/blsp_version.h b/examples/boot2_iap/blsp_version.h
index 1256d93a..889d42b7 100644
--- a/examples/boot2_iap/blsp_version.h
+++ b/examples/boot2_iap/blsp_version.h
@@ -36,6 +36,6 @@
#ifndef __MCU_SDK_VERSION_H__
#define __MCU_SDK_VERSION_H__
-#define BL_SDK_VER "3.0"
+#define BL_SDK_VER "5.0"
#endif