365 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			365 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/****************************************************************************
 | 
						|
 | 
						|
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_MTD_H
 | 
						|
#define IOT_MTD_H
 | 
						|
 | 
						|
#include "iot_flash_api.h"
 | 
						|
#include "iot_flash_layout.h"
 | 
						|
#include "iot_img_hdr.h"
 | 
						|
 | 
						|
#ifdef __cplusplus
 | 
						|
extern "C" {
 | 
						|
#endif
 | 
						|
 | 
						|
#define PARAM_NAME_LEN_MAX  11
 | 
						|
#define PARAM_VAL_LEN_MAX   19
 | 
						|
#define PARAM_END_CHAR      0x0A
 | 
						|
#define PARAM_EMPTY_CHAR    0x20
 | 
						|
#define PARAM_EQUAL_CHAR    0x3D
 | 
						|
#define PARAM_MAX_LENGTH    0x20
 | 
						|
 | 
						|
#define PARAM_STRING_LAST_PART      "last_part"
 | 
						|
#define PARAM_STRING_BOOT_PART      "boot_part"
 | 
						|
#define PARAM_STRING_START_PART     "start_part"
 | 
						|
#define PARAM_STRING_BOOT_CNT       "boot_cnt"
 | 
						|
#define PARAM_STRING_UPGRADE_STS    "upgd_sts"
 | 
						|
#define PARAM_STRING_FW_UNCP        "fw_uncp"
 | 
						|
#define PARAM_STRING_FIRST_RUN      "first_run"
 | 
						|
#define PARAM_STRING_FW_CRC1        "fw_crc1"
 | 
						|
#define PARAM_STRING_FW_SIZE1       "fw_size1"
 | 
						|
#define PARAM_STRING_FW_STS1        "fw_sts1"
 | 
						|
#define PARAM_STRING_FW_CRC2        "fw_crc2"
 | 
						|
#define PARAM_STRING_FW_SIZE2       "fw_size2"
 | 
						|
#define PARAM_STRING_FW_STS2        "fw_sts2"
 | 
						|
#define PARAM_STRING_FW_PERIOD1     "fw_period1"
 | 
						|
#define PARAM_STRING_FW_PERIOD2     "fw_period2"
 | 
						|
#define PARAM_STRING_FW_BLOCK1      "fw_block1"
 | 
						|
#define PARAM_STRING_FW_BLOCK2      "fw_block2"
 | 
						|
#define PARAM_STRING_REINFORCE      "reinforce"
 | 
						|
/* Partition switchover reason and partition before partition switchover.
 | 
						|
 * the quotient divided by 2 indicates the reason for switching partitions,
 | 
						|
 * see dev_switch_part_cause_t. the remainder of 2 indicates the partition number.
 | 
						|
 */
 | 
						|
#define PARAM_STRING_SWITCH_INFO    "sw_info"
 | 
						|
 | 
						|
#define MTD_LOG_HEAD_SIZE   0x40
 | 
						|
 | 
						|
typedef enum {
 | 
						|
    DEV_SWITCH_PART_CAUSE_UPGRADE = 0,
 | 
						|
    DEV_SWITCH_PART_CAUSE_CRASH,    //only record 5 minute crash
 | 
						|
    DEV_SWITCH_PART_CAUSE_OTHER,
 | 
						|
    DEV_SWITCH_PART_CAUSE_MAX,
 | 
						|
} dev_switch_part_cause_t;
 | 
						|
 | 
						|
typedef struct {
 | 
						|
    char name[PARAM_NAME_LEN_MAX];
 | 
						|
    char equal;
 | 
						|
    char value[PARAM_VAL_LEN_MAX];
 | 
						|
    char end;
 | 
						|
} dev_param_t;
 | 
						|
 | 
						|
typedef enum {
 | 
						|
    DEV_ERASE_TYPE_SECTOR = 0,
 | 
						|
    DEV_ERASE_TYPE_PAGE = 1,
 | 
						|
    DEV_ERASE_TYPE_BLOCK64 = 2,
 | 
						|
    DEV_ERASE_TYPE_FULL_PART = 3,
 | 
						|
} dev_erase_type_t;
 | 
						|
 | 
						|
/* log partition head */
 | 
						|
typedef struct _mtd_log_head {
 | 
						|
    uint32_t log_size;
 | 
						|
    uint32_t write_pos;
 | 
						|
} mtd_log_head_t;
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_device_init() function used to initiate mtd device
 | 
						|
 *
 | 
						|
 * @return              HAL_OK - succeed
 | 
						|
 * @return              HAL_ERROR - fail
 | 
						|
 */
 | 
						|
uint32_t mtd_device_init(int mode);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_set_flash_size() function used to set mtd device's flash size
 | 
						|
 *
 | 
						|
 * @return              void
 | 
						|
 */
 | 
						|
void mtd_set_flash_size(uint8_t size);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_get_flash_size() function used to get mtd device's flash size
 | 
						|
 *
 | 
						|
 * @return              flash size enum, see flash_size_t.
 | 
						|
 */
 | 
						|
uint32_t mtd_get_flash_size(void);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_get_psram_state() function used to get mtd device's psram state
 | 
						|
 *
 | 
						|
 * @return              0 psram not enabled. otherwise, psram enabled.
 | 
						|
 */
 | 
						|
uint8_t mtd_get_psram_state();
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_set_psram() function used to set mtd device's psram state
 | 
						|
 *
 | 
						|
 * @return              void
 | 
						|
 */
 | 
						|
void mtd_set_psram_state(uint8_t en);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_open() - function used to open a device part
 | 
						|
 * @param num:          mtd partition number
 | 
						|
 * @param flags:        if flags=1, means open the device directly.
 | 
						|
 *                      There will be some risk.
 | 
						|
 *
 | 
						|
 * @return              -1 - failed to open device
 | 
						|
 * @return              otherwise - return virtual file pointer
 | 
						|
 *
 | 
						|
 */
 | 
						|
int dev_open(int num, uint32_t flags);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_close() - function used to close a device part
 | 
						|
 * @param fd:           the virtual file pointer
 | 
						|
 *
 | 
						|
 * @return              -1 - failed to close device
 | 
						|
 * @return              0 - succeed to close device
 | 
						|
 *
 | 
						|
 */
 | 
						|
int dev_close(int fd);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_seek() - function used to set the file position indicator
 | 
						|
 * @param fd:           the virtual file pointer
 | 
						|
 * @param offset:       the offset position to be set, and uint is byte
 | 
						|
 * @param fromwhere:    the start position, the value is
 | 
						|
 *                      DEV_SEEK_SET: the begin of device partition
 | 
						|
 *                      DEV_SEEK_CUR: current position of device partition
 | 
						|
 *                      DEV_SEEK_END: the end of device partition
 | 
						|
 *
 | 
						|
 * @return              -1 - failed to seek device
 | 
						|
 * @return              otherwise - the newest position
 | 
						|
 *
 | 
						|
 */
 | 
						|
int dev_seek(int fd, uint32_t offset, uint8_t fromwhere);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_read() - function used to read data from device partition
 | 
						|
 * @param fd:           the virtual file pointer
 | 
						|
 * @param buf:          the read value will be saved in this pointer
 | 
						|
 * @param count:        the data length(flash page size,unit is byte)
 | 
						|
 *
 | 
						|
 * @return              -1 - failed to read data
 | 
						|
 * @return              otherwise - the read data number
 | 
						|
 *
 | 
						|
 */
 | 
						|
int dev_read(int fd, void* buf, size_t count);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_write() - function used to write data in device partition
 | 
						|
 * @param fd:           the virtual file pointer
 | 
						|
 * @param buf:          the read value will be saved in this pointer
 | 
						|
 *
 | 
						|
 * @return              -1 - failed to write data
 | 
						|
 * @return              otherwise - the written data size
 | 
						|
 *
 | 
						|
 */
 | 
						|
int dev_write(int fd, void*buf, size_t count);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_write_ext() - function used to write data in device partition
 | 
						|
 * @param fd:           the virtual file pointer
 | 
						|
 * @param buf:          the data value needs to be written to the partition
 | 
						|
 * @param count         the write data lenght
 | 
						|
 * @param is_erase      the partion need to be erased before write
 | 
						|
 *
 | 
						|
 * @return              -1 - failed to write data
 | 
						|
 * @return              otherwise - the written data size
 | 
						|
 *
 | 
						|
 */
 | 
						|
int dev_write_ext(int fd, void *buf, size_t count, uint8_t is_erase);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_get_size_of_erase_once() - function used to get flash write used size
 | 
						|
 * @param part:           partion
 | 
						|
 * @return              0 failed
 | 
						|
 * @return              otherwise - size
 | 
						|
 *
 | 
						|
 */
 | 
						|
int mtd_get_size_of_erase_once(int part);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_erase() - function used to erease data
 | 
						|
 * @param fd:           the virtual file pointer
 | 
						|
 * @param addr:         the offset of partition
 | 
						|
 * @param mode:         the mode of erasing
 | 
						|
 *
 | 
						|
 * @return              -1 - failed to erase data
 | 
						|
 * @return              0 - succeed to erase data
 | 
						|
 *
 | 
						|
 */
 | 
						|
int dev_erase(int fd, uint32_t addr, uint8_t mode);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_device_get_info() - function used to get device ID
 | 
						|
 * @param buf:          the read id will be saved in this pointer
 | 
						|
 * @param type:         the device type
 | 
						|
 *
 | 
						|
 * @return              -1 - failed to get device ID
 | 
						|
 * @return              0 - succeed to get device ID
 | 
						|
 *
 | 
						|
 */
 | 
						|
int mtd_device_get_info(void* buf, uint32_t type);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_log_save_done() - flash log exception done
 | 
						|
 * @param lock:         1: means os lock opened, 0: means os lock closed
 | 
						|
 *
 | 
						|
 * @return              -1 -- initial flash log failed
 | 
						|
 * @return              0 -- initial flash log successful
 | 
						|
 *
 | 
						|
 */
 | 
						|
int32_t iot_log_save_done(uint8_t lock);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_copy_part() - copy partition
 | 
						|
 * @param src_part:  source part number in flash block.
 | 
						|
 * @param dest_part:  destination part number in flash block.
 | 
						|
 *
 | 
						|
 * @return              other value -- failed reason
 | 
						|
 * @return              0 -- copy succeed
 | 
						|
 *
 | 
						|
 */
 | 
						|
uint32_t mtd_copy_part(uint8_t src_part, uint8_t dest_part);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_get_hdr(): get firmware header information of specified partition.
 | 
						|
 * @param part_num: the specified partition.
 | 
						|
 * @param hdr: pointer to save header information.
 | 
						|
 * @return ERR_FAIL: failed; ERR_OK: succeed.
 | 
						|
 */
 | 
						|
uint32_t mtd_get_hdr(uint8_t part_num, imgHdr *hdr);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_get_hdr_img_size(uint8_t part) - get img size in hdr
 | 
						|
 * @param part:  part number in flash block.
 | 
						|
 *
 | 
						|
 * @return img_size
 | 
						|
 *
 | 
						|
 */
 | 
						|
uint32_t mtd_get_hdr_img_size(uint8_t part);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_get_hdr_img_crc(uint8_t part) - get img crc in hdr
 | 
						|
 * @param part:  part number in flash block.
 | 
						|
 *
 | 
						|
 * @return img_crc
 | 
						|
 *
 | 
						|
 */
 | 
						|
uint32_t mtd_get_hdr_img_crc(uint8_t part);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_calc_hdr_img_part_crc(uint8_t part) - calculate img crc in hdr
 | 
						|
 * @param part:  part number in flash block.
 | 
						|
 *
 | 
						|
 * @return img_crc
 | 
						|
 *
 | 
						|
 */
 | 
						|
uint32_t mtd_calc_hdr_img_part_crc(uint8_t part);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_fw_hdr_img_crc_compare(uint8_t part) - compare img crc
 | 
						|
 * @param none.
 | 
						|
 *
 | 
						|
 * @return crc_compare_results: 0=match, 1=mismatch
 | 
						|
 *
 | 
						|
 */
 | 
						|
uint8_t mtd_fw_hdr_img_crc_compare();
 | 
						|
uint8_t dev_get_first_boot_val();
 | 
						|
uint8_t dev_get_first_run_val();
 | 
						|
uint8_t dev_get_boot_cnt();
 | 
						|
uint32_t dev_param_get_value(const char *name, char *value, int *pos);
 | 
						|
uint32_t dev_param_save_value(const char *name, char *value);
 | 
						|
uint32_t dev_param_get_start_flag(uint32_t *flag);
 | 
						|
uint32_t dev_param_get_uncp_flag(uint32_t *flag);
 | 
						|
uint32_t dev_param_get_last_flag(uint32_t *flag);
 | 
						|
uint32_t dev_param_get_boot_flag(uint32_t *flag);
 | 
						|
/**
 | 
						|
 * @brief dev_switch_part_flag() - switch the start flag(start_part\boot_part).
 | 
						|
 * @param cause - see dev_switch_part_cause_t.
 | 
						|
 */
 | 
						|
void dev_switch_part_flag(uint8_t cause);
 | 
						|
void dev_switch_part();
 | 
						|
uint32_t dev_get_boot_fw_part_num(uint8_t *part);
 | 
						|
uint32_t dev_get_boot_pib_part_num(uint8_t *part);
 | 
						|
uint32_t dev_get_boot_cus_part_num(uint8_t *part);
 | 
						|
uint32_t dev_get_upgrade_fw_part_num(uint8_t *part);
 | 
						|
uint32_t dev_get_upgrade_pib_part_num(uint8_t *part);
 | 
						|
uint32_t dev_get_upgrade_cus_part_num(uint8_t *part);
 | 
						|
uint32_t dev_startup_param_value_check(bool_t sync);
 | 
						|
bool_t dev_get_upgrade_flag(void);
 | 
						|
uint32_t dev_get_oem_part_num(uint8_t * num);
 | 
						|
uint32_t dev_get_cal_data_part_num(uint8_t * num);
 | 
						|
uint8_t dev_get_pib_ext_part_num(uint8_t part);
 | 
						|
uint32_t dev_get_part_size(uint8_t part);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_param_get_boot_after_burn() - Get the flag to tell if this
 | 
						|
 * is the first boot after flash-burn burns images.
 | 
						|
 * @param p_flag: Pointer to return the flag. 1 - first boot, 0 - not.
 | 
						|
 * @return ERR_OK - Get flag successfully, ERR_FAIL - Fail to fetch flag.
 | 
						|
 *
 | 
						|
 */
 | 
						|
uint32_t dev_param_get_boot_after_burn(uint32_t *p_flag);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_param_clear_boot_after_burn() - Clear the flag to tell if this
 | 
						|
 * is the first boot after flash-burn burns images.
 | 
						|
 * @return ERR_OK - Clear flag successfully, ERR_FAIL - Fail to clear flag.
 | 
						|
 *
 | 
						|
 */
 | 
						|
uint32_t dev_param_clear_boot_after_burn(void);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief dev_get_upgrade_result() - get the last upgrade result
 | 
						|
 * @param cause - cause of partition switchover, see dev_switch_part_cause_t.
 | 
						|
 * @param upgrade_flag - 1 - upgrade succeeded(old version default), 0 - upgrade failed
 | 
						|
 */
 | 
						|
void dev_get_switch_part_info(uint8_t *cause, uint8_t *upgrade_flag);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_running_fw_get_size() - get the firmware size currently executed.
 | 
						|
 * @return fw size.
 | 
						|
 */
 | 
						|
uint32_t mtd_running_fw_get_size(void);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief mtd_running_fw_read() - get the firmware data currently executed.
 | 
						|
 * @param offset - offset position, ranging from 0 ~ fw size.
 | 
						|
 * @param buffer - buffer to store FW data.
 | 
						|
 * @param length - length to read data.
 | 
						|
 * @return number of bytes read successfully.
 | 
						|
 */
 | 
						|
uint32_t mtd_running_fw_read(uint32_t offset, uint8_t *buffer,
 | 
						|
    uint32_t length);
 | 
						|
 | 
						|
#ifdef __cplusplus
 | 
						|
}
 | 
						|
#endif
 | 
						|
 | 
						|
#endif //IOT_MTD_H
 | 
						|
 |