HAAS530 Alibaba cloud Internet of things platform ID ² Access practice, FREERTOS+SE chip

ID ² And hardware master driver transplantation

The next part implements ID ² SE chip transplantation

Transplant preparation

SE security chip: FM1230
Hardware circuit: main control - iic - FM1230

The original factory provides the driving example under IIC under STM32
Transplant the IIC driver according to its own master control. Here, GPIO is used to simulate IIC

IIC driven transplantation

Analog IIC driver is quite common. I also found it on the Internet and posted several references:

https://blog.csdn.net/shaguahaha/article/details/70766665
https://blog.csdn.net/return_oops/article/details/80965437

Several points to note are:

  1. IIC address of SE chip, FM1230 is 0xE2
  2. During the migration process, the device FM1230 slave replies, but the data is always incorrect
    The algorithm fails to pass the calculation. There are differences between simulating IIC and using ST standard IIC library [original factory support is very important]

For the communication between IIC and FM1230, the logic analyzer is used to capture packet records during transplantation:

hal related transplantation

After IIC drives OK, STD is mainly involved_ se_ adapter.c,se_ Migration of two functions of driver. C
Referring to the original factory example, there is basically no problem

Verify ID2 related

I didn't migrate this part of the code to ID from the beginning ² After debugging this part separately, run the sample code of FM1230 and the encryption of SM4.

Here are some points to note:

  1. irot_ hal_ get_ In Id2, the exception judgment of annotation in function
irot_result_t irot_hal_get_id2(uint8_t* id, uint32_t* len)
{
    irot_result_t ret;
    irot_result_t close_ret;
    void* handle = NULL;
    uint8_t cmd_buf[MAX_CMD_APDU_LENGTH];
    uint8_t rsp_buf[MAX_RSP_APDU_LENGTH];
    uint32_t rsp_len = sizeof(rsp_buf);

    // open session
    ret = open_session(&handle);
    if (ret != IROT_SUCCESS)
    {
        goto EXIT;
    }

    // select application
#if ID2_SEND_SELECT_COMMAND
    ret = select_application(handle, cmd_buf, rsp_buf, &rsp_len);
    if (ret != IROT_SUCCESS)
    {
        goto EXIT;
    }
#endif
    // get ID
    memset(cmd_buf, 0x00, CMD_APDU_HEAD_LENGTH);
    cmd_buf[INDEX_CLA] = CLA_VALUE;
    cmd_buf[INDEX_INS] = INS_GET_ID;
    cmd_buf[INDEX_LC] = 0x00;
    rsp_len = sizeof(rsp_buf);
    ret = apdu_transmit_wrap(handle, cmd_buf, CMD_APDU_HEAD_LENGTH, rsp_buf, &rsp_len);
    if (ret != IROT_SUCCESS)
    {
        goto EXIT;
    }
    // 3 bytes head
    if (rsp_len != (0x03 + SE_ID2_LENGTH))
    {
        ret = IROT_ERROR_GENERIC;
        goto EXIT;
    }
    // ID2 data
    if (rsp_buf[2] != SE_ID2_LENGTH)
    {
        ret = IROT_ERROR_GENERIC;
        goto EXIT;
    }
    rsp_len -= 0x03;
	/*  Comment out
    if (rsp_len > *len)
    {
        ret = IROT_ERROR_GENERIC;
        goto EXIT;
    }
    else
    */
    {
        // | 2 TAG | 1 len |
        memcpy(id, rsp_buf + 3, rsp_len);
        *len = rsp_len;
    }

EXIT:
    // close session
    close_ret = close_session(handle);

    return ret == IROT_SUCCESS ? close_ret : ret;
}

  1. irot_hal_sym_crypto
    The second parameter is 1, which should also be paid attention to when transplanting.

PS: migrate IIC and system layer to ID after running through the example ² It will be much easier in the sdk

Migrate SE related parts to ID ² sdk

Migrate the corresponding SE chip related codes above to ID ² sdk, look at the directory structure first

Home directory structure

modules/irot directory structure

Migration steps

Overwrite makefile in cmake mode

Just make sure that the corresponding configuration in the makefile is switched to SE

The configuration of CMakeLists.txt under cmake is as follows. The focus on irot is to select the source file under SE directory

cmake_minimum_required(VERSION 3.13)
# Omit some system related

# set id_lib dir
set(id_lib_dir ${CMAKE_CURRENT_SOURCE_DIR}/id_lib)

# my libs path
set(id_lib_path ${CMAKE_CURRENT_SOURCE_DIR}/lib)

function(add_subdirectory_if_exist dir)
	if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${dir})
		if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/CMakeLists.txt)
			add_subdirectory(${dir})
		endif()
	endif()
endfunction()

# add lib crypto
set(ID2_CRYPTO_DIR id_lib/modules/crypto)
add_subdirectory_if_exist(${ID2_CRYPTO_DIR})

# add lib crypto
set(ID2_HAL_DIR id_lib/modules/hal)
add_subdirectory_if_exist(${ID2_HAL_DIR})

# add lib ID2
set(ID2_ID2_DIR id_lib/modules/id2/lib)
add_subdirectory_if_exist(${ID2_ID2_DIR})

# add lib irot
set(ID2_IROT_DIR id_lib/modules/irot/se)
# set(ID2_IROT_DIR id_lib/modules/irot/demo/)
add_subdirectory_if_exist(${ID2_IROT_DIR})

# add lib itls
set(ID2_ITLS_DIR id_lib/modules/itls)
add_subdirectory_if_exist(${ID2_ITLS_DIR})

# add lib osa
set(ID2_OSA_DIR id_lib/modules/osa)
add_subdirectory_if_exist(${ID2_OSA_DIR})

SE directory original makefile file, modify your CMakeLists.txt file according to the key parts of the makefile

Modified CMakeLists.txt file

cmake_minimum_required(VERSION 3.13)

set(SOURCE_TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(BINARY_TOP_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(CMAKE_C_COMPILER_FORCED   1)
set(CMAKE_CXX_COMPILER_FORCED 1)

set(out_hex_dir ${CMAKE_CURRENT_BINARY_DIR}/hex)
# set(out_lib_dir ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(out_lib_dir ${CMAKE_SOURCE_DIR}/lib)
set(out_inc_dir ${CMAKE_CURRENT_BINARY_DIR}/rel)
set(out_inc_dir ${CMAKE_CURRENT_BINARY_DIR}/include)
set(tools_dir ${CMAKE_CURRENT_SOURCE_DIR}/tools)


set(CHIP_NAME myse)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../include/osa
					${CMAKE_CURRENT_SOURCE_DIR}/../../../include/irot
					${CMAKE_CURRENT_SOURCE_DIR}/../../../include/irot/se
					${CMAKE_CURRENT_SOURCE_DIR}/../../../include/crypto
					${CMAKE_CURRENT_SOURCE_DIR}/./inc
					${CMAKE_CURRENT_SOURCE_DIR}/./chipset/${CHIP_NAME}
)

project(irot)
set(target km)

message(STATUS "build target: " ${target}) 
message(STATUS "CMAKE_CURRENT_SOURCE_DIR: " ${CMAKE_CURRENT_SOURCE_DIR}) 
message(STATUS "CMAKE_SOURCE_DIR: " ${CMAKE_SOURCE_DIR}) 

# debug info

add_definitions(-DCONFIG_ID2_DEBUG 
		-DCONFIG_CHIP_KEY_TYPE=CHIP_KEY_TYPE_SM4
		)

file(GLOB SRC_CORE ${CMAKE_CURRENT_SOURCE_DIR}/src/core/*.c)
file(GLOB SRC_LOG ${CMAKE_CURRENT_SOURCE_DIR}/src/log/*.c)
file(GLOB SRC_CHIPSET ${CMAKE_CURRENT_SOURCE_DIR}/chipset/${CHIP_NAME}/se_driver/*.c)

add_library(${target} STATIC
	${SRC_CORE}
	${SRC_LOG}
	${SRC_CHIPSET}
)

set(LIBRARY_OUTPUT_PATH ${out_lib_dir})

# set_target_properties

target_include_directories(${target} PUBLIC include)

Modify the macro definition according to make.settings
add_definitions(-DCONFIG_ID2_DEBUG 
		-DCONFIG_CHIP_KEY_TYPE=CHIP_KEY_TYPE_SM4
		)
Adaptive interface layer

The SM4 encryption method mentioned above is in irot_ hal_ sym_ The second parameter in crypto is changed to 1

include/irot/se/id2_ irot_ Hal. H (the original file name is irot_hal.h)
Modify KEY_ID_ID2 macro is defined as 1

The actual call file is modules/irot/se/src/core/km_to_irot.c

Verification and testing

To verify whether the module migration is successful, you need to compile app/id2_app corresponds to the demo code. Verify whether it is ok

PS: this part is indispensable. If there is no problem with this part, there will be no error in the next step.

lib library check

So far, several key lib static libraries have been obtained

Keywords: Single-Chip Microcomputer IoT Alibaba Cloud

Added by macinjosh on Mon, 25 Oct 2021 11:17:26 +0300