Merge pull request #49 from ucloud/feature_support_set_download_partition

support download partition
This commit is contained in:
ethanDu1 2020-08-10 17:05:51 +08:00 committed by GitHub
commit 33aaf0e8b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 300 additions and 80 deletions

View File

@ -0,0 +1,73 @@
#ifndef _HAL_FLASH_H_
#define _HAL_FLASH_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include "stm32f7xx_hal.h"
#include "uiot_import.h"
#define CURRENT_VERSION_START_ADDR 0x08018000
#define DOWNLOAD_FINISH_FLG_ADDR 0x08018018
#define DOWNLOAD_START_ADDR 0x08100000
#define DOWNLOAD_SUCESS 0X00
#define DOWNLOAD_FAILED 0X01
#define VERSION_BYTE_NUM 24
/**
* @brief FLASH地址中写入一个字节的数据
*
* @param addr FLASH地址
* @param data
* @return 0-success -fail
*/
int HAL_FLASH_Write_Byte(_IN_ uint32_t addr,_IN_ uint32_t data);
/**
* @brief FLASH地址的sector
*
* @param addr FLASH sector
* @return 0-success -fail
*/
int HAL_FLASH_Erase_Sector(_IN_ uint8_t sector, _IN_ uint32_t sector_num);
/**
* @brief FLASH地址的一个字节数据
*
* @param addr FLASH地址
* @return
*/
uint8_t HAL_FLASH_Read_Byte(_IN_ uint8_t addr);
/**
* @brief FLASH地址上锁
*
* @param addr
* @return
*/
void HAL_FLASH_Locked(void);
/**
* @brief FLASH地址解锁
*
* @param addr
* @return
*/
void HAL_FLASH_Unlocked(void);
/**
* @brief
*
* @param char型指针
* @return
*/
//void version_get(char * verptr);
#if defined(__cplusplus)
}
#endif
#endif /* _HAL_FLASH_H_ */

View File

@ -16,12 +16,17 @@
uint32_t download_addr = DOWNLOAD_START_ADDR;
void * HAL_Download_Name_Set(void * handle)
{
return NULL;
}
void * HAL_Download_Init(_IN_ void * name)
{
int i,ret;
char version_info[VERSION_BYTE_NUM];
uint32_t info_addr = CURRENT_VERSION_START_ADDR;
for(i=0; i < VERSION_BYTE_NUM; i++)
for(i = 0; i < VERSION_BYTE_NUM; i++)
{
version_info[i] = HAL_FLASH_Read_Byte(info_addr);
info_addr++;
@ -29,18 +34,20 @@ void * HAL_Download_Init(_IN_ void * name)
if(FAILURE_RET == HAL_FLASH_Erase_Sector(FLASH_SECTOR_3, 1))
return NULL;
info_addr = CURRENT_VERSION_START_ADDR;
HAL_FLASH_Unlocked();
for(i = 0; i < VERSION_BYTE_NUM; i++)
{
ret = HAL_FLASH_Write_Byte(info_addr++,version_info[i]);
if(SUCCESS_RET != ret)
return NULL;
}
ret = HAL_FLASH_Write_Byte(DOWNLOAD_FINISH_FLG_ADDR,DOWNLOAD_FAILED);
ret = HAL_FLASH_Write_Byte(DOWNLOAD_FINISH_FLG_ADDR, DOWNLOAD_FAILED);
if(SUCCESS_RET != ret)
return NULL;
ret = HAL_FLASH_Erase_Sector(FLASH_SECTOR_8, 4);
if(SUCCESS_RET != ret)
return NULL;
return NULL;
HAL_FLASH_Locked();
download_addr = DOWNLOAD_START_ADDR;
return &download_addr;
}
@ -48,14 +55,16 @@ void * HAL_Download_Init(_IN_ void * name)
int HAL_Download_Write(_IN_ void * handle,_IN_ uint32_t total_length,_IN_ uint8_t *buffer_read,_IN_ uint32_t length)
{
uint8_t *readptr = buffer_read;
int i = 0,ret;
int i = 0,ret;
HAL_FLASH_Unlocked();
for(i = length; i > 0; i--){
ret = HAL_FLASH_Write_Byte(download_addr++, *readptr++);
if(ret == FAILURE_RET){
if(FAILURE_RET == ret){
printf("HAL_Download_Write failed!\r\n");
return FAILURE_RET;
}
}
HAL_FLASH_Locked();
return SUCCESS_RET;
}
@ -86,7 +95,8 @@ int HAL_FLASH_Erase_Sector(_IN_ uint8_t sector, _IN_ int sector_num)
{
uint32_t flash_error;
HAL_StatusTypeDef ret;
FLASH_EraseInitTypeDef flash_erase;
FLASH_EraseInitTypeDef flash_erase;
HAL_FLASH_Unlock();
flash_erase.TypeErase = FLASH_TYPEERASE_SECTORS;
flash_erase.Sector = sector;
flash_erase.NbSectors = sector_num;
@ -94,7 +104,8 @@ int HAL_FLASH_Erase_Sector(_IN_ uint8_t sector, _IN_ int sector_num)
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR | FLASH_FLAG_BSY);
ret = HAL_FLASHEx_Erase(&flash_erase, &flash_error);
FLASH_WaitForLastOperation(50000);
FLASH_WaitForLastOperation(50000);
HAL_FLASH_Lock();
if(ret == HAL_ERROR){
printf("erase sector in flash failed!\r\n");
return FAILURE_RET;
@ -109,12 +120,15 @@ uint8_t HAL_FLASH_Read_Byte(_IN_ uint8_t addr)
return data;
}
/*
void version_get(char * verptr)
void HAL_FLASH_Locked(void)
{
int i;
uint32_t version_addr=CURRENT_VERSION_START_ADDR;
for(i=0;i<VERSION_BYTE_NUM;i++)
verptr[i]=*(__IO uint8_t*)( version_addr++);
HAL_FLASH_Lock();
}
*/
void HAL_FLASH_Unlocked(void)
{
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR | FLASH_FLAG_BSY);
}

View File

@ -0,0 +1,15 @@
#ifndef _HAL_FLASH_H_
#define _HAL_FLASH_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include "uiot_import.h"
#if defined(__cplusplus)
}
#endif
#endif /* _HAL_FLASH_H_ */

View File

@ -14,6 +14,50 @@
*/
#include "uiot_import.h"
static int _utils_parse_name(const char *url, char *name) {
char *host_ptr = (char *) strstr(url, "://");
uint32_t name_len = 0;
char *path_ptr;
char *name_ptr;
if (host_ptr == NULL) {
return -1; /* URL is invalid */
}
host_ptr += 3;
path_ptr = strchr(host_ptr, '/');
if (NULL == path_ptr) {
return -2;
}
name_ptr = strchr(path_ptr, '?');
if (NULL == name_ptr) {
return -2;
}
name_len = name_ptr - path_ptr;
memcpy(name, path_ptr + 1, name_len - 1);
name[name_len] = '\0';
return SUCCESS_RET;
}
void * HAL_Download_Name_Set(void * handle)
{
char *url_str = (char *)handle;
char *name_str;
if(NULL == (name_str = HAL_Malloc(strlen(url_str))))
{
printf("malloc url_str failed");
return NULL;
}
if(SUCCESS_RET == _utils_parse_name(url_str, name_str))
return name_str;
else
return NULL;
}
void * HAL_Download_Init(_IN_ void * name)
{
char * file_name =(char *)name;

View File

@ -0,0 +1,73 @@
#ifndef _HAL_FLASH_H_
#define _HAL_FLASH_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include "stm32f7xx_hal.h"
#include "uiot_import.h"
#define CURRENT_VERSION_START_ADDR 0x08018000
#define DOWNLOAD_FINISH_FLG_ADDR 0x08018018
#define DOWNLOAD_START_ADDR 0x08100000
#define DOWNLOAD_SUCESS 0X00
#define DOWNLOAD_FAILED 0X01
#define VERSION_BYTE_NUM 24
/**
* @brief FLASH地址中写入一个字节的数据
*
* @param addr FLASH地址
* @param data
* @return 0-success -fail
*/
int HAL_FLASH_Write_Byte(_IN_ uint32_t addr,_IN_ uint32_t data);
/**
* @brief FLASH地址的sector
*
* @param addr FLASH sector
* @return 0-success -fail
*/
int HAL_FLASH_Erase_Sector(_IN_ uint8_t sector, _IN_ uint32_t sector_num);
/**
* @brief FLASH地址的一个字节数据
*
* @param addr FLASH地址
* @return
*/
uint8_t HAL_FLASH_Read_Byte(_IN_ uint8_t addr);
/**
* @brief FLASH地址上锁
*
* @param addr
* @return
*/
void HAL_FLASH_Locked(void);
/**
* @brief FLASH地址解锁
*
* @param addr
* @return
*/
void HAL_FLASH_Unlocked(void);
/**
* @brief
*
* @param char型指针
* @return
*/
//void version_get(char * verptr);
#if defined(__cplusplus)
}
#endif
#endif /* _HAL_FLASH_H_ */

View File

@ -16,31 +16,38 @@
uint32_t download_addr = DOWNLOAD_START_ADDR;
void * HAL_Download_Name_Set(void * handle)
{
return NULL;
}
void * HAL_Download_Init(_IN_ void * name)
{
int i,ret;
char version_info[VERSION_BYTE_NUM];
uint32_t info_addr = CURRENT_VERSION_START_ADDR;
for(i=0; i < VERSION_BYTE_NUM; i++)
for(i = 0; i < VERSION_BYTE_NUM; i++)
{
version_info[i] = HAL_FLASH_Read_Byte(info_addr);
info_addr++;
}
if(FAILURE_RET == HAL_FLASH_Erase_Sector(FLASH_SECTOR_3))
if(FAILURE_RET == HAL_FLASH_Erase_Sector(FLASH_SECTOR_3, 1))
return NULL;
info_addr = CURRENT_VERSION_START_ADDR;
HAL_FLASH_Unlocked();
for(i = 0; i < VERSION_BYTE_NUM; i++)
{
ret = HAL_FLASH_Write_Byte(info_addr++,version_info[i]);
if(SUCCESS_RET != ret)
return NULL;
}
ret = HAL_FLASH_Write_Byte(DOWNLOAD_FINISH_FLG_ADDR,DOWNLOAD_FAILED);
ret = HAL_FLASH_Write_Byte(DOWNLOAD_FINISH_FLG_ADDR, DOWNLOAD_FAILED);
if(SUCCESS_RET != ret)
return NULL;
ret = HAL_FLASH_Erase_Sector(FLASH_SECTOR_8, 4);
if(SUCCESS_RET != ret)
return NULL;
return NULL;
HAL_FLASH_Locked();
download_addr = DOWNLOAD_START_ADDR;
return &download_addr;
}
@ -48,14 +55,16 @@ void * HAL_Download_Init(_IN_ void * name)
int HAL_Download_Write(_IN_ void * handle,_IN_ uint32_t total_length,_IN_ uint8_t *buffer_read,_IN_ uint32_t length)
{
uint8_t *readptr = buffer_read;
int i = 0,ret;
int i = 0,ret;
HAL_FLASH_Unlocked();
for(i = length; i > 0; i--){
ret = HAL_FLASH_Write_Byte(download_addr++, *readptr++);
if(ret == FAILURE_RET){
if(FAILURE_RET == ret){
printf("HAL_Download_Write failed!\r\n");
return FAILURE_RET;
}
}
HAL_FLASH_Locked();
return SUCCESS_RET;
}
@ -82,11 +91,12 @@ int HAL_FLASH_Write_Byte(_IN_ uint32_t sddr,_IN_ uint32_t data)
else
return SUCCESS_RET;
}
int HAL_FLASH_Erase_Sector(_IN_ uint8_t sector, _IN_ uint8_t sector_num)
int HAL_FLASH_Erase_Sector(_IN_ uint8_t sector, _IN_ uint32_t sector_num)
{
uint32_t flash_error;
HAL_StatusTypeDef ret;
FLASH_EraseInitTypeDef flash_erase;
FLASH_EraseInitTypeDef flash_erase;
HAL_FLASH_Unlock();
flash_erase.TypeErase = FLASH_TYPEERASE_SECTORS;
flash_erase.Sector = sector;
flash_erase.NbSectors = sector_num;
@ -94,7 +104,8 @@ int HAL_FLASH_Erase_Sector(_IN_ uint8_t sector, _IN_ uint8_t sector_num)
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR | FLASH_FLAG_BSY);
ret = HAL_FLASHEx_Erase(&flash_erase, &flash_error);
FLASH_WaitForLastOperation(50000);
FLASH_WaitForLastOperation(50000);
HAL_FLASH_Lock();
if(ret == HAL_ERROR){
printf("erase sector in flash failed!\r\n");
return FAILURE_RET;
@ -109,12 +120,15 @@ uint8_t HAL_FLASH_Read_Byte(_IN_ uint8_t addr)
return data;
}
/*
void version_get(char * verptr)
void HAL_FLASH_Locked(void)
{
int i;
uint32_t version_addr=CURRENT_VERSION_START_ADDR;
for(i=0;i<VERSION_BYTE_NUM;i++)
verptr[i]=*(__IO uint8_t*)( version_addr++);
HAL_FLASH_Lock();
}
*/
void HAL_FLASH_Unlocked(void)
{
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR | FLASH_FLAG_BSY);
}

View File

@ -85,7 +85,7 @@ int ota_lib_get_msg_type(char *json, char **type);
int ota_lib_get_msg_module_ver(char *json, char **module, char **ver);
int ota_lib_get_params(char *json, char **url, char **module, char **file_name, char **version, char **md5,
int ota_lib_get_params(char *json, char **url, char **module, char **download_name, char **version, char **md5,
uint32_t *fileSize);
int ota_lib_gen_upstream_msg(char *buf, size_t bufLen, const char *module, const char *version, int progress,

View File

@ -152,7 +152,7 @@ static void _ota_callback(void *pContext, const char *msg, uint32_t msg_len)
goto do_exit;
}
if (SUCCESS_RET != ota_lib_get_params(msg_str, &h_ota->url, &h_ota->module, &h_ota->download_file_name, &h_ota->version, &h_ota->md5sum, &h_ota->size_file)) {
if (SUCCESS_RET != ota_lib_get_params(msg_str, &h_ota->url, &h_ota->module, &h_ota->download_name, &h_ota->version, &h_ota->md5sum, &h_ota->size_file)) {
LOG_ERROR("Get firmware parameter failed");
goto do_exit;
}
@ -379,8 +379,8 @@ int IOT_OTA_Destroy(void *handle)
HAL_Free(h_ota->md5sum);
}
if (NULL != h_ota->download_file_name) {
HAL_Free(h_ota->download_file_name);
if (NULL != h_ota->download_name) {
HAL_Free(h_ota->download_name);
}
HAL_Free(h_ota);
@ -393,12 +393,26 @@ void IOT_OTA_Clear(void *handle)
ofc_deinit(h_ota->ch_fetch);
memset(h_ota->url, 0, strlen(h_ota->url));
memset(h_ota->module, 0, strlen(h_ota->module));
memset(h_ota->download_file_name, 0, strlen(h_ota->download_file_name));
memset(h_ota->version, 0, strlen(h_ota->version));
memset(h_ota->md5sum, 0, strlen(h_ota->md5sum));
if (NULL != h_ota->url) {
memset(h_ota->url, 0, strlen(h_ota->url));
}
if (NULL != h_ota->module) {
memset(h_ota->module, 0, strlen(h_ota->module));
}
if(NULL != h_ota->download_name){
memset(h_ota->download_name, 0, strlen(h_ota->download_name));
}
if (NULL != h_ota->version) {
memset(h_ota->version, 0, strlen(h_ota->version));
}
if (NULL != h_ota->md5sum) {
memset(h_ota->md5sum, 0, strlen(h_ota->md5sum));
}
h_ota->size_last_fetched = 0;
h_ota->size_fetched = 0;
h_ota->size_file = 0;
@ -706,7 +720,7 @@ int IOT_OTA_fw_download(void *handle)
IOT_OTA_Ioctl(h_ota, OTA_IOCTL_FILE_SIZE, &file_size, 4);
download_handle = HAL_Download_Init(h_ota->download_file_name);
download_handle = HAL_Download_Init(h_ota->download_name);
if(download_handle == NULL)
{
ret = FAILURE_RET;

View File

@ -80,36 +80,7 @@ int ota_lib_get_msg_module_ver(char *json, char **module, char **ver) {
FUNC_EXIT_RC(SUCCESS_RET);
}
static int _utils_parse_name(const char *url, char *name) {
char *host_ptr = (char *) strstr(url, "://");
uint32_t name_len = 0;
char *path_ptr;
char *name_ptr;
if (host_ptr == NULL) {
return -1; /* URL is invalid */
}
host_ptr += 3;
path_ptr = strchr(host_ptr, '/');
if (NULL == path_ptr) {
return -2;
}
name_ptr = strchr(path_ptr, '?');
if (NULL == name_ptr) {
return -2;
}
name_len = name_ptr - path_ptr;
memcpy(name, path_ptr + 1, name_len - 1);
name[name_len] = '\0';
return SUCCESS_RET;
}
int ota_lib_get_params(char *json, char **url, char **module, char **file_name, char **version, char **md5,
int ota_lib_get_params(char *json, char **url, char **module, char **download_name, char **version, char **md5,
uint32_t *fileSize) {
FUNC_ENTRY;
@ -118,7 +89,6 @@ int ota_lib_get_params(char *json, char **url, char **module, char **file_name,
char *version_str;
char *url_str;
char *md5_str;
char *name_str;
/* get module */
if (NULL == (module_str = LITE_json_value_of(MODULE_FIELD, json))) {
@ -150,13 +120,7 @@ int ota_lib_get_params(char *json, char **url, char **module, char **file_name,
}
*url = url_str;
if(NULL == (name_str = HAL_Malloc(strlen(url_str))))
{
LOG_ERROR("malloc url_str failed");
FUNC_EXIT_RC(ERR_OTA_GENERAL_FAILURE);
}
_utils_parse_name(url_str, name_str);
*file_name = name_str;
*download_name = HAL_Download_Name_Set((void*)url_str);
/* get md5 */
if (NULL == (md5_str = LITE_json_value_of(MD5_FIELD, json))) {

View File

@ -88,7 +88,7 @@ typedef struct {
uint32_t size_file; /* size of file */
char *url; /* point to URL */
char *download_file_name; /* download file name */
char *download_name; /* download partition name */
char *module; /* download module name */
char *version; /* point to version */
char *md5sum; /* MD5 string */

View File

@ -29,6 +29,7 @@ extern "C" {
#include "uiot_defs.h"
#include "HAL_Timer_Platform.h"
#include "HAL_Flash_Platform.h"
#include "utils_net.h"
#include "lite-utils.h"
#include "json_parser.h"
@ -318,6 +319,14 @@ int32_t HAL_TCP_Write(_IN_ uintptr_t fd, _IN_ unsigned char *buf, _IN_ size_t le
*/
int32_t HAL_TCP_Read(_IN_ uintptr_t fd, _OU_ unsigned char *buf, _IN_ size_t len, _IN_ uint32_t timeout_ms);
/**
* @brief name
*
* @param handle download_name的指针
* @return download_name的指针
*/
void * HAL_Download_Name_Set(void * handle);
/**
* @brief
*