Files
kunlun/inc/pib/iot_oem.h

325 lines
9.8 KiB
C
Executable File

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#ifndef IOT_OEM_H
#define IOT_OEM_H
/* os shim includes */
#include "os_types.h"
/* common includes */
#include "iot_oem_api.h"
/* oem special structure includes */
#include "iot_oem_struct_v0.h"
#include "iot_oem_struct_v1.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* OEM section is the place to store per moudle configurations, like unique
* MAC address. OEM payload start at 0x40 in flash. since 64bytes used for
* section header description.
*/
/* pack for the structures in the whole file */
#pragma pack(push) // save the pack status
#pragma pack(1) // 1 byte align
#if (HW_PLATFORM == HW_PLATFORM_SIMU)
#define IOT_OEM_SECTION_START_ADDR (0)
#else
#define IOT_OEM_SECTION_START_ADDR (64)
#endif
#define IOT_OEM_SECTION_SEC_ADDR (3072)
#define IOT_OEM_EXT_SECTION_OFFSET(fw_size) \
(((fw_size+31)&(~0x1f))+IOT_OEM_SECTION_START_ADDR)
#define IOT_OEM_EXT_MAGIC_STR "_oem_ext"
#define IOT_OEM_DUMMY_LEN 106
/* use simple pair passcode for security check */
#define IOT_OEM_SEC_TYPE_PASSCODE 1
/* define chip type */
#define IOT_CHIP_TYPE_DEF 0
#define IOT_CHIP_TYPE_HZ3011 1
#define IOT_CHIP_TYPE_HZ3001 2
#define IOT_CHIP_TYPE_WQ3011 3
#define IOT_CHIP_TYPE_WQ3001 4
#define IOT_CHIP_TYPE_HZ3021 5
#define IOT_CHIP_TYPE_MT8201 6
#define IOT_CHIP_TYPE_FLX6610 7
#define IOT_CHIP_TYPE_FLX6611 8
#define IOT_CHIP_TYPE_HZ3211 9
#define IOT_CHIP_TYPE_HZ3201 10
#define IOT_CHIP_TYPE_WQ3031 11
#define IOT_CHIP_TYPE_QJ5580 12
#define IOT_CHIP_TYPE_QJ5582 13
#define IOT_CHIP_TYPE_FLX6710 14
#define IOT_CHIP_TYPE_FLX6711 15
#define IOT_CHIP_TYPE_SPE7301 16
#define IOT_CHIP_TYPE_SPE7303 17
#define IOT_CHIP_TYPE_GX2001 18
#define IOT_CHIP_TYPE_GX2011 19
#define IOT_CHIP_TYPE_HZ5202 20
#define IOT_CHIP_TYPE_DT0530 21
#define IOT_CHIP_TYPE_DT0531 22
#define IOT_CHIP_TYPE_YP8801 23
#define IOT_CHIP_TYPE_YP8811 24
#define IOT_CHIP_SEC_ECC_CURVE_LEN 1
#define IOT_CHIP_SEC_ECC_KEY_PUB_LEN 64
#define IOT_CHIP_SEC_ECC_SIGN_LEN 64
#define IOT_CHIP_SEC_SM2_CURVE_LEN 1
#define IOT_CHIP_SEC_SM2_KEY_PUB_LEN 64
#define IOT_CHIP_SEC_SM2_SIGN_LEN 64
#define IOT_CHIP_SEC_ECC_KEY_PRI_LEN 32
#define IOT_CHIP_SEC_SM2_KEY_PRI_LEN 32
/* security sm2 user id table length */
#define IOT_SEC_SM2_USER_ID_LEN 16
/* root sm2 public key length */
#define IOT_SEC_ROOT_SM2_PUB_LEN 64
typedef enum _iot_oem_ver_e {
IOT_OEM_VER_V0 = 0,
IOT_OEM_VER_V1 = 1,
IOT_OEM_VER_MAX,
IOT_OEM_VER_INVALID = 0x3f, /* 6 bits */
} iot_oem_ver_e;
typedef struct _iot_misc_info_t {
uint16_t vendor_id;
uint8_t chip_mmid[IOT_CHIP_MMID_LEN];
uint16_t chip_code;
/* see IOT_OEM_SEC_TYPE_XXX */
uint8_t sec_type;
/* simple pair passcode value */
uint16_t passcode;
/* hardware version info */
iot_oem_hw_ver_info_t hw_ver_info;
/* cert baund set */
uint32_t cert_baund;
/* default 0 - see IOT_CHIP_TYPE_XXX */
uint8_t chip_type;
uint8_t misc_dummy[IOT_OEM_DUMMY_LEN];
}iot_oem_misc_cfg_t;
typedef struct _iot_oem_ip_info {
/* 0 for disable oem ip info, 1 for enable */
uint8_t ip_enable;
/* see IOT_OEM_IP_VERSION_XXX. */
uint8_t ip_ver;
/* 0 for CCO define ip, 1 for customer define IP. */
uint8_t ip4_method;
/* ipv4 addr. */
uint8_t ipv4[IOT_IP4_ADDR_LEN];
/* ipv4 netmask addr. */
uint8_t ipv4_mask[IOT_IP4_ADDR_LEN];
/* local port used. */
uint16_t ipv4_port;
} iot_oem_ip_info_t;
typedef struct _iot_oem_security_hdr {
/* security data crc */
uint32_t crc;
/* version id */
uint8_t ver;
/* security data length */
uint16_t len;
/* reserved */
uint8_t rsvd;
} iot_oem_security_hdr_t;
typedef struct _iot_oem_security_param_v1 {
/* ecc elliptic curve type. 1 - brainpoolP256r1, others - reserved */
uint8_t ecc_curve_type;
/* ecc public key */
uint8_t ecc_key_pub[IOT_CHIP_SEC_ECC_KEY_PUB_LEN];
/* ecc signature */
uint8_t ecc_sign[IOT_CHIP_SEC_ECC_SIGN_LEN];
/* sm2 curve type. always 0 */
uint8_t sm2_curve_type;
/* sm2 public key */
uint8_t sm2_key_pub[IOT_CHIP_SEC_SM2_KEY_PUB_LEN];
/* sm2 signature */
uint8_t sm2_sign[IOT_CHIP_SEC_SM2_SIGN_LEN];
/* ecc private key */
uint8_t ecc_key_pri[IOT_CHIP_SEC_ECC_KEY_PRI_LEN];
/* sm2 private key */
uint8_t sm2_key_pri[IOT_CHIP_SEC_SM2_KEY_PRI_LEN];
} iot_oem_security_param_v1_t;
typedef struct _iot_oem_security_cfg_v1 {
/* security data header */
iot_oem_security_hdr_t hdr;
/* security data */
iot_oem_security_param_v1_t data;
} iot_oem_security_cfg_v1_t;
typedef struct _iot_cus_cfg_t {
/* start charge threshold voltage, unit:mv. */
int16_t start_charge_thr_volt;
/* stop charge threshold voltage, unit:mv. */
int16_t stop_charge_thr_volt;
/* reserved for future use. */
uint8_t padding[60];
} iot_cus_cfg_t;
typedef struct _iot_oem_pad_cfg_t {
uint8_t padding[110];
}iot_oem_pad_cfg_t;
typedef struct _iot_oem_cfg {
iot_oem_base_cfg_t base_cfg;
iot_oem_misc_cfg_t misc_cfg;
/* V0 version board_id uses 32bit, and V1 version takes 32bit apart
* for compatibility, little-endian.
*/
uint32_t board_id : 10,
board_cfg_ver : 6,
board_cfg_resv : 16;
/* in order to reduce the changes of oem structure,
* 642 bytes(space occupied by iot_board_info_v0_t) are shared here.
*/
union {
iot_board_info_v0_t board_cfg[2];
iot_board_info_v1_t board_cfg_v1;
};
iot_oem_ip_info_t ip_info;
iot_cus_cfg_t cus_cfg;
iot_oem_pad_cfg_t pad_cfg;
} iot_oem_cfg_t;
/* security sm2 user id */
extern const uint8_t g_iot_sec_sm2_user_id[IOT_SEC_SM2_USER_ID_LEN];
/* security sm2 root public key */
extern const uint8_t g_iot_sec_root_sm2_pub_v1[IOT_SEC_ROOT_SM2_PUB_LEN];
/**
* @brief iot_oem_read_mtd() - read oem data from flash oem info section.
* @param buf: pointer to receive oem data.
* @param size: length to receive oem data.
* @param offset: offset of the oem info section.
* @retval 0 success; others fail.
*/
uint32_t iot_oem_read_mtd(uint8_t *buf, uint32_t size, uint32_t offset);
/**
* @brief iot_oem_read_mtd_ext() - read oem ext data from flash fw section.
* @param buf: pointer to receive oem data.
* @param size: length to receive oem data.
* @retval 0 success; others fail.
*/
uint32_t iot_oem_read_mtd_ext(uint8_t *buf, uint32_t size);
/**
* @brief iot_oem_init - init oem function.
* warning, iot_oem_init should be called after pib load
* @param role_cco : ture means cco. otherwise sta
*/
void iot_oem_init(bool_t role_cco);
/**
* @brief iot_oem_get_cfg() - read oem cfg from flash oem info section.
* @param oemcfg: pointer to receive oem cfg
* @retval 0 success; others fail
*/
uint32_t iot_oem_get_cfg(iot_oem_cfg_t** oemcfg);
/**
* @brief iot_oem_set_cfg() - write oem cfg to flash oem info section.
* @param oemcfg: pointer to receive oem cfg
* @retval 0 success; others fail
*/
uint32_t iot_oem_set_cfg(iot_oem_cfg_t* oemcfg);
/**
* @brief iot_oem_get_cus_cfg() - read cus cfg from flash oem cus cfg section.
* @param iot_cus_cfg_t: pointer to receive cus cfg
* @retval 0 success; others fail
*/
uint32_t iot_oem_get_cus_cfg(iot_cus_cfg_t** cus_cfg);
/**
* @brief iot_oem_set_module_type() - set module type.
* @param mt: module type
*/
void iot_oem_set_module_type(uint32_t mt);
/**
* @brief iot_oem_get_mtd_sts() - get mtd status from global oem.
* @param oemcfg: void
* @retval 0 success; others fail
*/
uint32_t iot_oem_get_mtd_sts(void);
/**
* @brief iot_oem_set_chip_code() - set chip code to flash.
* @param chip_code: chip code
* @retval 0 success; others fail
*/
uint32_t iot_oem_set_chip_code(uint16_t chip_code);
/**
* @brief iot_oem_set_vendor_id() - set vendor id to flash.
* @param vendor_id: vendor id
* @retval 0 success; others fail
*/
uint32_t iot_oem_set_vendor_id(uint16_t vendor_id);
/**
* @brief: get passcode info.
* @retval - return passcode value, 0 means not available.
*/
uint16_t iot_oem_get_passcode();
/**
* @brief iot_oem_get_chip_type() - get value of chip type .
* @param none.
* @retval enum value of chip_type, see IOT_CHIP_TYPE_XXX.
*/
uint8_t iot_oem_get_chip_type();
/**
* @brief iot_oem_get_security_cfg_v1() - get security config.
* @param none
* @retval security parameter pointer.
*/
iot_oem_security_param_v1_t *iot_oem_get_security_cfg_v1();
/**
* @brief iot_oem_set_security_cfg_v1() - set security config.
* @param param: security config
* @retval see ERR_XXX.
*/
uint32_t iot_oem_set_security_cfg_v1(iot_oem_security_param_v1_t *param);
#pragma pack(pop) // restore the pack status
#ifdef __cplusplus
}
#endif
#endif