提交全部资料
This commit is contained in:
109
1.主程序源代码/User/easydb/src/easyflash.c
Normal file
109
1.主程序源代码/User/easydb/src/easyflash.c
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* This file is part of the EasyFlash Library.
|
||||
*
|
||||
* Copyright (c) 2014-2019, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: Initialize interface for this library.
|
||||
* Created on: 2014-09-09
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* This all Backup Area Flash storage index. All used flash area configure is under here.
|
||||
* |----------------------------| Storage Size
|
||||
* | Environment variables area | ENV area size @see ENV_AREA_SIZE
|
||||
* |----------------------------|
|
||||
* | Saved log area | Log area size @see LOG_AREA_SIZE
|
||||
* |----------------------------|
|
||||
* |(IAP)Downloaded application | IAP already downloaded application, unfixed size
|
||||
* |----------------------------|
|
||||
*
|
||||
* @note all area sizes must be aligned with EF_ERASE_MIN_SIZE
|
||||
*
|
||||
* The EasyFlash add the NG (Next Generation) mode start from V4.0. All old mode before V4.0, called LEGACY mode.
|
||||
*
|
||||
* - NG (Next Generation) mode is default mode from V4.0. It's easy to settings, only defined the ENV_AREA_SIZE.
|
||||
* - The LEGACY mode has been DEPRECATED. It is NOT RECOMMENDED to continue using.
|
||||
* Beacuse it will use ram to buffer the ENV and spend more flash erase times.
|
||||
* If you want use it please using the V3.X version.
|
||||
*/
|
||||
|
||||
#include <easyflash.h>
|
||||
|
||||
#if !defined(EF_START_ADDR)
|
||||
#error "Please configure backup area start address (in ef_cfg.h)"
|
||||
#endif
|
||||
|
||||
#if !defined(EF_ERASE_MIN_SIZE)
|
||||
#error "Please configure minimum size of flash erasure (in ef_cfg.h)"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* EasyFlash system initialize.
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode easyflash_init(void) {
|
||||
extern EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size);
|
||||
extern EfErrCode ef_env_init(ef_env const *default_env, size_t default_env_size);
|
||||
// extern EfErrCode ef_iap_init(void);
|
||||
extern EfErrCode ef_log_init(void);
|
||||
|
||||
size_t default_env_set_size = 0;
|
||||
const ef_env *default_env_set;
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
static bool init_ok = false;
|
||||
|
||||
if (init_ok) {
|
||||
return EF_NO_ERR;
|
||||
}
|
||||
|
||||
result = ef_port_init(&default_env_set, &default_env_set_size);
|
||||
|
||||
#ifdef EF_USING_ENV
|
||||
if (result == EF_NO_ERR) {
|
||||
result = ef_env_init(default_env_set, default_env_set_size);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef EF_USING_IAP
|
||||
if (result == EF_NO_ERR) {
|
||||
result = ef_iap_init();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef EF_USING_LOG
|
||||
if (result == EF_NO_ERR) {
|
||||
result = ef_log_init();
|
||||
}
|
||||
#endif
|
||||
|
||||
if (result == EF_NO_ERR) {
|
||||
init_ok = true;
|
||||
EF_INFO("EasyFlash V%s is initialize success.\n", EF_SW_VERSION);
|
||||
} else {
|
||||
EF_INFO("EasyFlash V%s is initialize fail.\n", EF_SW_VERSION);
|
||||
}
|
||||
// EF_INFO("You can get the latest version on https://github.com/armink/EasyFlash .\n");
|
||||
|
||||
return result;
|
||||
}
|
||||
1841
1.主程序源代码/User/easydb/src/ef_env.c
Normal file
1841
1.主程序源代码/User/easydb/src/ef_env.c
Normal file
File diff suppressed because it is too large
Load Diff
922
1.主程序源代码/User/easydb/src/ef_env_legacy.c
Normal file
922
1.主程序源代码/User/easydb/src/ef_env_legacy.c
Normal file
@@ -0,0 +1,922 @@
|
||||
/*
|
||||
* This file is part of the EasyFlash Library.
|
||||
*
|
||||
* Copyright (c) 2014-2018, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: Environment variables operating interface. (normal mode)
|
||||
* Created on: 2014-10-06
|
||||
*/
|
||||
|
||||
#include <easyflash.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if defined(EF_USING_ENV) && defined(EF_ENV_USING_LEGACY_MODE)
|
||||
|
||||
#ifndef EF_ENV_USING_WL_MODE
|
||||
|
||||
#if defined(EF_USING_ENV) && (!defined(ENV_USER_SETTING_SIZE) || !defined(ENV_AREA_SIZE))
|
||||
#error "Please configure user setting ENV size or ENV area size (in ef_cfg.h)"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* ENV area has 2 sections
|
||||
* 1. System section
|
||||
* It storages ENV parameters. (Units: Word)
|
||||
* 2. Data section
|
||||
* It storages all ENV. Storage format is key=value\0.
|
||||
* All ENV must be 4 bytes alignment. The remaining part must fill '\0'.
|
||||
*
|
||||
* @note Word = 4 Bytes in this file
|
||||
* @note When using power fail safeguard mode, it has two ENV areas(Area0, Area1).
|
||||
*/
|
||||
|
||||
/* flash ENV parameters index and size in system section */
|
||||
enum {
|
||||
/* data section ENV end address index in system section */
|
||||
ENV_PARAM_INDEX_END_ADDR = 0,
|
||||
|
||||
#ifdef EF_ENV_USING_PFS_MODE
|
||||
/* saved count for ENV area */
|
||||
ENV_PARAM_INDEX_SAVED_COUNT,
|
||||
#endif
|
||||
|
||||
#ifdef EF_ENV_AUTO_UPDATE
|
||||
/* current version number for ENV */
|
||||
ENV_PARAM_INDEX_VER_NUM,
|
||||
#endif
|
||||
|
||||
/* data section CRC32 code index in system section */
|
||||
ENV_PARAM_INDEX_DATA_CRC,
|
||||
/* flash ENV parameters word size */
|
||||
ENV_PARAM_WORD_SIZE,
|
||||
/* flash ENV parameters byte size */
|
||||
ENV_PARAM_BYTE_SIZE = ENV_PARAM_WORD_SIZE * 4,
|
||||
};
|
||||
|
||||
/* default ENV set, must be initialized by user */
|
||||
static ef_env const *default_env_set;
|
||||
/* default ENV set size, must be initialized by user */
|
||||
static size_t default_env_set_size = 0;
|
||||
/* ENV ram cache */
|
||||
static uint32_t env_cache[ENV_USER_SETTING_SIZE / 4] = { 0 };
|
||||
/* ENV start address in flash */
|
||||
static uint32_t env_start_addr = 0;
|
||||
/* ENV ram cache has changed when ENV created, deleted and changed value. */
|
||||
static bool env_cache_changed = false;
|
||||
/* initialize OK flag */
|
||||
static bool init_ok = false;
|
||||
|
||||
#ifdef EF_ENV_USING_PFS_MODE
|
||||
/* current load ENV area address */
|
||||
static uint32_t cur_load_area_addr = 0;
|
||||
/* next save ENV area address */
|
||||
static uint32_t next_save_area_addr = 0;
|
||||
#endif
|
||||
|
||||
static uint32_t get_env_system_addr(void);
|
||||
static uint32_t get_env_data_addr(void);
|
||||
static uint32_t get_env_end_addr(void);
|
||||
static void set_env_end_addr(uint32_t end_addr);
|
||||
static EfErrCode write_env(const char *key, const char *value);
|
||||
static char *find_env(const char *key);
|
||||
static EfErrCode del_env(const char *key);
|
||||
static size_t get_env_data_size(void);
|
||||
static size_t get_env_user_used_size(void);
|
||||
static EfErrCode create_env(const char *key, const char *value);
|
||||
static uint32_t calc_env_crc(void);
|
||||
static bool env_crc_is_ok(void);
|
||||
#ifdef EF_ENV_AUTO_UPDATE
|
||||
static EfErrCode env_auto_update(void);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Flash ENV initialize.
|
||||
*
|
||||
* @param default_env default ENV set for user
|
||||
* @param default_env_size default ENV set size
|
||||
*
|
||||
* @note user_size must equal with total_size in normal mode
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_env_init(ef_env const *default_env, size_t default_env_size) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
EF_ASSERT(ENV_AREA_SIZE);
|
||||
EF_ASSERT(ENV_USER_SETTING_SIZE);
|
||||
EF_ASSERT(EF_ERASE_MIN_SIZE);
|
||||
/* must be word alignment for ENV */
|
||||
EF_ASSERT(ENV_USER_SETTING_SIZE % 4 == 0);
|
||||
EF_ASSERT(ENV_AREA_SIZE % 4 == 0);
|
||||
EF_ASSERT(default_env);
|
||||
EF_ASSERT(default_env_size < ENV_USER_SETTING_SIZE);
|
||||
|
||||
#ifndef EF_ENV_USING_PFS_MODE
|
||||
/* total_size must be aligned with erase_min_size */
|
||||
if (ENV_USER_SETTING_SIZE % EF_ERASE_MIN_SIZE == 0) {
|
||||
EF_ASSERT(ENV_USER_SETTING_SIZE == ENV_AREA_SIZE);
|
||||
} else {
|
||||
EF_ASSERT((ENV_USER_SETTING_SIZE / EF_ERASE_MIN_SIZE + 1)*EF_ERASE_MIN_SIZE == ENV_AREA_SIZE);
|
||||
}
|
||||
#else
|
||||
/* total_size must be aligned with erase_min_size */
|
||||
if (ENV_USER_SETTING_SIZE % EF_ERASE_MIN_SIZE == 0) {
|
||||
/* it has double area when used power fail safeguard mode */
|
||||
EF_ASSERT(2 * ENV_USER_SETTING_SIZE == ENV_AREA_SIZE);
|
||||
} else {
|
||||
/* it has double area when used power fail safeguard mode */
|
||||
EF_ASSERT(2 * (ENV_USER_SETTING_SIZE / EF_ERASE_MIN_SIZE + 1)*EF_ERASE_MIN_SIZE == ENV_AREA_SIZE);
|
||||
}
|
||||
#endif
|
||||
|
||||
env_start_addr = EF_START_ADDR;
|
||||
default_env_set = default_env;
|
||||
default_env_set_size = default_env_size;
|
||||
|
||||
EF_DEBUG("ENV start address is 0x%08X, size is %d bytes.\n", EF_START_ADDR, ENV_AREA_SIZE);
|
||||
|
||||
result = ef_load_env();
|
||||
|
||||
#ifdef EF_ENV_AUTO_UPDATE
|
||||
if (result == EF_NO_ERR) {
|
||||
env_auto_update();
|
||||
}
|
||||
#endif
|
||||
|
||||
if (result == EF_NO_ERR) {
|
||||
init_ok = true;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* ENV set default.
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_env_set_default(void) {
|
||||
extern EfErrCode ef_env_ver_num_set_default(void);
|
||||
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
size_t i;
|
||||
|
||||
EF_ASSERT(default_env_set);
|
||||
EF_ASSERT(default_env_set_size);
|
||||
|
||||
/* lock the ENV cache */
|
||||
ef_port_env_lock();
|
||||
|
||||
/* set environment end address is at data section start address */
|
||||
set_env_end_addr(get_env_data_addr());
|
||||
|
||||
#ifdef EF_ENV_USING_PFS_MODE
|
||||
/* set saved count to default 0 */
|
||||
env_cache[ENV_PARAM_INDEX_SAVED_COUNT] = 0;
|
||||
#endif
|
||||
|
||||
#ifdef EF_ENV_AUTO_UPDATE
|
||||
/* initialize version number */
|
||||
env_cache[ENV_PARAM_INDEX_VER_NUM] = EF_ENV_VER_NUM;
|
||||
#endif
|
||||
|
||||
/* create default ENV */
|
||||
for (i = 0; i < default_env_set_size; i++) {
|
||||
create_env(default_env_set[i].key, default_env_set[i].value);
|
||||
}
|
||||
|
||||
/* unlock the ENV cache */
|
||||
ef_port_env_unlock();
|
||||
|
||||
result = ef_save_env();
|
||||
|
||||
#ifdef EF_ENV_USING_PFS_MODE
|
||||
/* reset other PFS area's data */
|
||||
if (result == EF_NO_ERR) {
|
||||
env_cache_changed = true;
|
||||
result = ef_save_env();
|
||||
}
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ENV system section start address.
|
||||
*
|
||||
* @return system section start address
|
||||
*/
|
||||
static uint32_t get_env_system_addr(void) {
|
||||
#ifndef EF_ENV_USING_PFS_MODE
|
||||
return env_start_addr;
|
||||
#else
|
||||
return cur_load_area_addr;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ENV data section start address.
|
||||
*
|
||||
* @return data section start address
|
||||
*/
|
||||
static uint32_t get_env_data_addr(void) {
|
||||
return get_env_system_addr() + ENV_PARAM_BYTE_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ENV end address.
|
||||
* It's the first word in ENV.
|
||||
*
|
||||
* @return ENV end address
|
||||
*/
|
||||
static uint32_t get_env_end_addr(void) {
|
||||
/* it is the first word */
|
||||
return env_cache[ENV_PARAM_INDEX_END_ADDR];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ENV end address.
|
||||
* It's the first word in ENV.
|
||||
*
|
||||
* @param end_addr ENV end address
|
||||
*/
|
||||
static void set_env_end_addr(uint32_t end_addr) {
|
||||
env_cache[ENV_PARAM_INDEX_END_ADDR] = end_addr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current ENV data section size.
|
||||
*
|
||||
* @return size
|
||||
*/
|
||||
static size_t get_env_data_size(void) {
|
||||
if (get_env_end_addr() > get_env_data_addr()) {
|
||||
return get_env_end_addr() - get_env_data_addr();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current user used ENV size.
|
||||
*
|
||||
* @return bytes
|
||||
*/
|
||||
static size_t get_env_user_used_size(void) {
|
||||
if (get_env_end_addr() > get_env_system_addr()) {
|
||||
return get_env_end_addr() - get_env_system_addr();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current ENV already write bytes.
|
||||
*
|
||||
* @return write bytes
|
||||
*/
|
||||
size_t ef_get_env_write_bytes(void) {
|
||||
#ifndef EF_ENV_USING_PFS_MODE
|
||||
return get_env_user_used_size();
|
||||
#else
|
||||
return get_env_user_used_size() * 2;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an ENV at the end of cache.
|
||||
*
|
||||
* @param key ENV name
|
||||
* @param value ENV value
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
static EfErrCode write_env(const char *key, const char *value) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
size_t key_len = strlen(key), value_len = strlen(value), env_str_len;
|
||||
char *env_cache_bak = (char *)env_cache;
|
||||
|
||||
/* calculate ENV storage length, contain '=' and '\0'. */
|
||||
env_str_len = key_len + value_len + 2;
|
||||
if (env_str_len % 4 != 0) {
|
||||
env_str_len = (env_str_len / 4 + 1) * 4;
|
||||
}
|
||||
/* check capacity of ENV */
|
||||
if (env_str_len + get_env_user_used_size() >= ENV_USER_SETTING_SIZE) {
|
||||
return EF_ENV_FULL;
|
||||
}
|
||||
|
||||
/* calculate current ENV ram cache end address */
|
||||
env_cache_bak += get_env_user_used_size();
|
||||
|
||||
/* copy key name */
|
||||
memcpy(env_cache_bak, key, key_len);
|
||||
env_cache_bak += key_len;
|
||||
/* copy equal sign */
|
||||
*env_cache_bak = '=';
|
||||
env_cache_bak++;
|
||||
/* copy value */
|
||||
memcpy(env_cache_bak, value, value_len);
|
||||
env_cache_bak += value_len;
|
||||
/* fill '\0' for string end sign */
|
||||
*env_cache_bak = '\0';
|
||||
env_cache_bak ++;
|
||||
/* fill '\0' for word alignment */
|
||||
memset(env_cache_bak, 0, env_str_len - (key_len + value_len + 2));
|
||||
set_env_end_addr(get_env_end_addr() + env_str_len);
|
||||
/* ENV ram cache has changed */
|
||||
env_cache_changed = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find ENV.
|
||||
*
|
||||
* @param key ENV name
|
||||
*
|
||||
* @return found ENV in ram cache
|
||||
*/
|
||||
static char *find_env(const char *key) {
|
||||
char *env_start, *env_end, *env, *found_env = NULL;
|
||||
size_t key_len = strlen(key), env_len;
|
||||
|
||||
if ((key == NULL) || *key == '\0') {
|
||||
EF_INFO("Flash ENV name must be not empty!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* from data section start to data section end */
|
||||
env_start = (char *) ((char *) env_cache + ENV_PARAM_BYTE_SIZE);
|
||||
env_end = (char *) ((char *) env_cache + get_env_user_used_size());
|
||||
|
||||
/* ENV is null */
|
||||
if (env_start == env_end) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env = env_start;
|
||||
while (env < env_end) {
|
||||
/* the key length must be equal */
|
||||
if (!strncmp(env, key, key_len) && (env[key_len] == '=')) {
|
||||
found_env = env;
|
||||
break;
|
||||
} else {
|
||||
/* calculate ENV length, contain '\0'. */
|
||||
env_len = strlen(env) + 1;
|
||||
/* next ENV and word alignment */
|
||||
if (env_len % 4 == 0) {
|
||||
env += env_len;
|
||||
} else {
|
||||
env += (env_len / 4 + 1) * 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
return found_env;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the ENV is not exist, create it.
|
||||
* @see flash_write_env
|
||||
*
|
||||
* @param key ENV name
|
||||
* @param value ENV value
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
static EfErrCode create_env(const char *key, const char *value) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
EF_ASSERT(key);
|
||||
EF_ASSERT(value);
|
||||
|
||||
if ((key == NULL) || *key == '\0') {
|
||||
EF_INFO("Flash ENV name must be not empty!\n");
|
||||
return EF_ENV_NAME_ERR;
|
||||
}
|
||||
|
||||
if (strchr(key, '=')) {
|
||||
EF_INFO("Flash ENV name can't contain '='.\n");
|
||||
return EF_ENV_NAME_ERR;
|
||||
}
|
||||
|
||||
/* find ENV */
|
||||
if (find_env(key)) {
|
||||
EF_INFO("The name of \"%s\" is already exist.\n", key);
|
||||
return EF_ENV_NAME_EXIST;
|
||||
}
|
||||
/* write ENV at the end of cache */
|
||||
result = write_env(key, value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an ENV in cache.
|
||||
*
|
||||
* @param key ENV name
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
static EfErrCode del_env(const char *key) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
char *del_env = NULL;
|
||||
size_t del_env_length, remain_env_length;
|
||||
|
||||
EF_ASSERT(key);
|
||||
|
||||
if ((key == NULL) || *key == '\0') {
|
||||
EF_INFO("Flash ENV name must be not NULL!\n");
|
||||
return EF_ENV_NAME_ERR;
|
||||
}
|
||||
|
||||
if (strchr(key, '=')) {
|
||||
EF_INFO("Flash ENV name or value can't contain '='.\n");
|
||||
return EF_ENV_NAME_ERR;
|
||||
}
|
||||
|
||||
/* find ENV */
|
||||
del_env = find_env(key);
|
||||
|
||||
if (!del_env) {
|
||||
EF_INFO("Not find \"%s\" in ENV.\n", key);
|
||||
return EF_ENV_NAME_ERR;
|
||||
}
|
||||
del_env_length = strlen(del_env);
|
||||
/* '\0' also must be as ENV length */
|
||||
del_env_length ++;
|
||||
/* the address must multiple of 4 */
|
||||
if (del_env_length % 4 != 0) {
|
||||
del_env_length = (del_env_length / 4 + 1) * 4;
|
||||
}
|
||||
/* calculate remain ENV length */
|
||||
remain_env_length = get_env_data_size()
|
||||
- (((uint32_t) del_env + del_env_length) - ((uint32_t) env_cache + ENV_PARAM_BYTE_SIZE));
|
||||
/* remain ENV move forward */
|
||||
memcpy(del_env, del_env + del_env_length, remain_env_length);
|
||||
/* reset ENV end address */
|
||||
set_env_end_addr(get_env_end_addr() - del_env_length);
|
||||
/* ENV ram cache has changed */
|
||||
env_cache_changed = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an ENV.If it value is NULL, delete it.
|
||||
* If not find it in ENV table, then create it.
|
||||
*
|
||||
* @param key ENV name
|
||||
* @param value ENV value
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_set_env(const char *key, const char *value) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
char *old_env, *old_value;
|
||||
|
||||
if (!init_ok) {
|
||||
EF_INFO("ENV isn't initialize OK.\n");
|
||||
return EF_ENV_INIT_FAILED;
|
||||
}
|
||||
|
||||
/* lock the ENV cache */
|
||||
ef_port_env_lock();
|
||||
|
||||
/* if ENV value is NULL, delete it */
|
||||
if (value == NULL) {
|
||||
result = del_env(key);
|
||||
} else {
|
||||
old_env = find_env(key);
|
||||
/* If find this ENV, then compare the new value and old value. */
|
||||
if (old_env) {
|
||||
/* find the old value address */
|
||||
old_env = strchr(old_env, '=');
|
||||
old_value = old_env + 1;
|
||||
/* If it is changed then delete it and recreate it */
|
||||
if (strcmp(old_value, value)) {
|
||||
result = del_env(key);
|
||||
if (result == EF_NO_ERR) {
|
||||
result = create_env(key, value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = create_env(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/* unlock the ENV cache */
|
||||
ef_port_env_unlock();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Del an ENV.
|
||||
*
|
||||
* @param key ENV name
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_del_env(const char *key) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
if (!init_ok) {
|
||||
EF_INFO("ENV isn't initialize OK.\n");
|
||||
return EF_ENV_INIT_FAILED;
|
||||
}
|
||||
|
||||
/* lock the ENV cache */
|
||||
ef_port_env_lock();
|
||||
|
||||
result = del_env(key);
|
||||
|
||||
/* unlock the ENV cache */
|
||||
ef_port_env_unlock();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an ENV value by key name.
|
||||
*
|
||||
* @param key ENV name
|
||||
*
|
||||
* @return value
|
||||
*/
|
||||
char *ef_get_env(const char *key) {
|
||||
char *env = NULL, *value = NULL;
|
||||
|
||||
if (!init_ok) {
|
||||
EF_INFO("ENV isn't initialize OK.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* find ENV */
|
||||
env = find_env(key);
|
||||
|
||||
if (env == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
/* get value address */
|
||||
value = strchr(env, '=');
|
||||
if (value != NULL) {
|
||||
/* the equal sign next character is value */
|
||||
value++;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
/**
|
||||
* Print ENV.
|
||||
*/
|
||||
void ef_print_env(void) {
|
||||
uint32_t *env_cache_data_addr = env_cache + ENV_PARAM_WORD_SIZE,
|
||||
*env_cache_end_addr =
|
||||
(uint32_t *) (env_cache + ENV_PARAM_WORD_SIZE + get_env_data_size() / 4);
|
||||
uint8_t j;
|
||||
char c;
|
||||
|
||||
if (!init_ok) {
|
||||
EF_INFO("ENV isn't initialize OK.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (; env_cache_data_addr < env_cache_end_addr; env_cache_data_addr += 1) {
|
||||
for (j = 0; j < 4; j++) {
|
||||
c = (*env_cache_data_addr) >> (8 * j);
|
||||
ef_print("%c", c);
|
||||
if (c == '\0') {
|
||||
ef_print("\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef EF_ENV_USING_PFS_MODE
|
||||
ef_print("\nmode: normal\n");
|
||||
ef_print("size: %ld/%ld bytes.\n", get_env_user_used_size(), ENV_USER_SETTING_SIZE);
|
||||
#else
|
||||
ef_print("\nmode: power fail safeguard\n");
|
||||
ef_print("size: %ld/%ld bytes, write bytes %ld/%ld.\n", get_env_user_used_size(),
|
||||
ENV_USER_SETTING_SIZE, ef_get_env_write_bytes(), ENV_AREA_SIZE);
|
||||
ef_print("saved count: %ld\n", env_cache[ENV_PARAM_INDEX_SAVED_COUNT]);
|
||||
#endif
|
||||
|
||||
#ifdef EF_ENV_AUTO_UPDATE
|
||||
ef_print("ver num: %d\n", env_cache[ENV_PARAM_INDEX_VER_NUM]);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Load flash ENV to ram.
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
#ifndef EF_ENV_USING_PFS_MODE
|
||||
EfErrCode ef_load_env(void) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
uint32_t *env_cache_bak, env_end_addr;
|
||||
|
||||
/* read ENV end address from flash */
|
||||
ef_port_read(get_env_system_addr() + ENV_PARAM_INDEX_END_ADDR * 4, &env_end_addr, 4);
|
||||
/* if ENV is not initialize or flash has dirty data, set default for it */
|
||||
if ((env_end_addr == 0xFFFFFFFF) || (env_end_addr < env_start_addr)
|
||||
|| (env_end_addr > env_start_addr + ENV_USER_SETTING_SIZE)) {
|
||||
result = ef_env_set_default();
|
||||
} else {
|
||||
/* set ENV end address */
|
||||
set_env_end_addr(env_end_addr);
|
||||
|
||||
env_cache_bak = env_cache + ENV_PARAM_WORD_SIZE;
|
||||
/* read all ENV from flash */
|
||||
ef_port_read(get_env_data_addr(), env_cache_bak, get_env_data_size());
|
||||
/* read ENV CRC code from flash */
|
||||
ef_port_read(get_env_system_addr() + ENV_PARAM_INDEX_DATA_CRC * 4,
|
||||
&env_cache[ENV_PARAM_INDEX_DATA_CRC] , 4);
|
||||
/* if ENV CRC32 check is fault, set default for it */
|
||||
if (!env_crc_is_ok()) {
|
||||
EF_INFO("Warning: ENV CRC check failed. Set it to default.\n");
|
||||
result = ef_env_set_default();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#else
|
||||
EfErrCode ef_load_env(void) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
uint32_t area0_start_address = env_start_addr, area1_start_address = env_start_addr
|
||||
+ ENV_AREA_SIZE / 2;
|
||||
uint32_t area0_end_addr, area1_end_addr, area0_crc, area1_crc, area0_saved_count, area1_saved_count;
|
||||
bool area0_is_valid = true, area1_is_valid = true;
|
||||
/* read ENV area end address from flash */
|
||||
ef_port_read(area0_start_address + ENV_PARAM_INDEX_END_ADDR * 4, &area0_end_addr, 4);
|
||||
ef_port_read(area1_start_address + ENV_PARAM_INDEX_END_ADDR * 4, &area1_end_addr, 4);
|
||||
if ((area0_end_addr == 0xFFFFFFFF) || (area0_end_addr < area0_start_address)
|
||||
|| (area0_end_addr > area0_start_address + ENV_USER_SETTING_SIZE)) {
|
||||
area0_is_valid = false;
|
||||
}
|
||||
if ((area1_end_addr == 0xFFFFFFFF) || (area1_end_addr < area1_start_address)
|
||||
|| (area1_end_addr > area1_start_address + ENV_USER_SETTING_SIZE)) {
|
||||
area1_is_valid = false;
|
||||
}
|
||||
/* check area0 CRC when it is valid */
|
||||
if (area0_is_valid) {
|
||||
/* read ENV area0 crc32 code from flash */
|
||||
ef_port_read(area0_start_address + ENV_PARAM_INDEX_DATA_CRC * 4, &area0_crc, 4);
|
||||
/* read ENV from ENV area0 */
|
||||
ef_port_read(area0_start_address, env_cache, area0_end_addr - area0_start_address);
|
||||
/* current load ENV area address is area0 start address */
|
||||
cur_load_area_addr = area0_start_address;
|
||||
if (!env_crc_is_ok()) {
|
||||
area0_is_valid = false;
|
||||
}
|
||||
}
|
||||
/* check area1 CRC when it is valid */
|
||||
if (area1_is_valid) {
|
||||
/* read ENV area1 crc32 code from flash */
|
||||
ef_port_read(area1_start_address + ENV_PARAM_INDEX_DATA_CRC * 4, &area1_crc, 4);
|
||||
/* read ENV from ENV area1 */
|
||||
ef_port_read(area1_start_address, env_cache, area1_end_addr - area1_start_address);
|
||||
/* current load ENV area address is area1 start address */
|
||||
cur_load_area_addr = area1_start_address;
|
||||
if (!env_crc_is_ok()) {
|
||||
area1_is_valid = false;
|
||||
}
|
||||
}
|
||||
/* all ENV area CRC is OK then compare saved count */
|
||||
if (area0_is_valid && area1_is_valid) {
|
||||
/* read ENV area saved count from flash */
|
||||
ef_port_read(area0_start_address + ENV_PARAM_INDEX_SAVED_COUNT * 4,
|
||||
&area0_saved_count, 4);
|
||||
ef_port_read(area1_start_address + ENV_PARAM_INDEX_SAVED_COUNT * 4,
|
||||
&area1_saved_count, 4);
|
||||
/* the bigger saved count area is valid */
|
||||
if ((area0_saved_count > area1_saved_count) || ((area0_saved_count == 0) && (area1_saved_count == 0xFFFFFFFF))) {
|
||||
area1_is_valid = false;
|
||||
} else {
|
||||
area0_is_valid = false;
|
||||
}
|
||||
}
|
||||
if (area0_is_valid) {
|
||||
/* current load ENV area address is area0 start address */
|
||||
cur_load_area_addr = area0_start_address;
|
||||
/* next save ENV area address is area1 start address */
|
||||
next_save_area_addr = area1_start_address;
|
||||
/* read all ENV from area0 */
|
||||
ef_port_read(area0_start_address, env_cache, area0_end_addr - area0_start_address);
|
||||
} else if (area1_is_valid) {
|
||||
/* next save ENV area address is area0 start address */
|
||||
next_save_area_addr = area0_start_address;
|
||||
} else {
|
||||
/* current load ENV area address is area1 start address */
|
||||
cur_load_area_addr = area1_start_address;
|
||||
/* next save ENV area address is area0 start address */
|
||||
next_save_area_addr = area0_start_address;
|
||||
/* set the ENV to default */
|
||||
result = ef_env_set_default();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Save ENV to flash.
|
||||
*/
|
||||
EfErrCode ef_save_env(void) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
uint32_t write_addr, write_size;
|
||||
|
||||
/* ENV ram cache has not changed don't need to save */
|
||||
if (!env_cache_changed) {
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifndef EF_ENV_USING_PFS_MODE
|
||||
write_addr = get_env_system_addr();
|
||||
write_size = get_env_user_used_size();
|
||||
/* calculate and cache CRC32 code */
|
||||
env_cache[ENV_PARAM_INDEX_DATA_CRC] = calc_env_crc();
|
||||
#else
|
||||
write_addr = next_save_area_addr;
|
||||
write_size = get_env_user_used_size();
|
||||
/* replace next_save_area_addr with cur_load_area_addr */
|
||||
next_save_area_addr = cur_load_area_addr;
|
||||
cur_load_area_addr = write_addr;
|
||||
/* change the ENV end address to next save area address */
|
||||
set_env_end_addr(write_addr + write_size);
|
||||
/* ENV area saved count +1 */
|
||||
env_cache[ENV_PARAM_INDEX_SAVED_COUNT]++;
|
||||
/* calculate and cache CRC32 code */
|
||||
env_cache[ENV_PARAM_INDEX_DATA_CRC] = calc_env_crc();
|
||||
#endif
|
||||
|
||||
/* erase ENV */
|
||||
result = ef_port_erase(write_addr, write_size);
|
||||
switch (result) {
|
||||
case EF_NO_ERR: {
|
||||
EF_DEBUG("Erased ENV OK.\n");
|
||||
break;
|
||||
}
|
||||
case EF_ERASE_ERR: {
|
||||
EF_INFO("Error: Erased ENV fault! Start address is 0x%08X, size is %ld.\n", write_addr, write_size);
|
||||
/* will return when erase fault */
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/* write ENV to flash */
|
||||
result = ef_port_write(write_addr, env_cache, write_size);
|
||||
switch (result) {
|
||||
case EF_NO_ERR: {
|
||||
EF_DEBUG("Saved ENV OK.\n");
|
||||
break;
|
||||
}
|
||||
case EF_WRITE_ERR: {
|
||||
EF_INFO("Error: Saved ENV fault! Start address is 0x%08X, size is %ld.\n", write_addr, write_size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
env_cache_changed = false;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the cached ENV CRC32 value.
|
||||
*
|
||||
* @return CRC32 value
|
||||
*/
|
||||
static uint32_t calc_env_crc(void) {
|
||||
uint32_t crc32 = 0;
|
||||
|
||||
/* Calculate the ENV end address CRC32. The 4 is ENV end address bytes size. */
|
||||
crc32 = ef_calc_crc32(crc32, &env_cache[ENV_PARAM_INDEX_END_ADDR], 4);
|
||||
|
||||
#ifdef EF_ENV_USING_PFS_MODE
|
||||
/* Calculate the ENV area saved count CRC32. */
|
||||
crc32 = ef_calc_crc32(crc32, &env_cache[ENV_PARAM_INDEX_SAVED_COUNT], 4);
|
||||
#endif
|
||||
|
||||
/* Calculate the all ENV data CRC32. */
|
||||
crc32 = ef_calc_crc32(crc32, &env_cache[ENV_PARAM_WORD_SIZE], get_env_data_size());
|
||||
|
||||
EF_DEBUG("Calculate ENV CRC32 number is 0x%08X.\n", crc32);
|
||||
|
||||
return crc32;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the ENV CRC32
|
||||
*
|
||||
* @return true is ok
|
||||
*/
|
||||
static bool env_crc_is_ok(void) {
|
||||
if (calc_env_crc() == env_cache[ENV_PARAM_INDEX_DATA_CRC]) {
|
||||
EF_DEBUG("Verify ENV CRC32 result is OK.\n");
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set and save an ENV. If set ENV is success then will save it.
|
||||
*
|
||||
* @param key ENV name
|
||||
* @param value ENV value
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_set_and_save_env(const char *key, const char *value) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
result = ef_set_env(key, value);
|
||||
|
||||
if (result == EF_NO_ERR) {
|
||||
result = ef_save_env();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Del and save an ENV. If del ENV is success then will save it.
|
||||
*
|
||||
* @param key ENV name
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_del_and_save_env(const char *key) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
result = ef_del_env(key);
|
||||
|
||||
if (result == EF_NO_ERR) {
|
||||
result = ef_save_env();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef EF_ENV_AUTO_UPDATE
|
||||
/**
|
||||
* Auto update ENV to latest default when current EF_ENV_VER is changed.
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
static EfErrCode env_auto_update(void)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
/* lock the ENV cache */
|
||||
ef_port_env_lock();
|
||||
|
||||
/* read ENV version number from flash*/
|
||||
ef_port_read(get_env_system_addr() + ENV_PARAM_INDEX_VER_NUM * 4,
|
||||
&env_cache[ENV_PARAM_INDEX_VER_NUM] , 4);
|
||||
|
||||
/* check version number */
|
||||
if (env_cache[ENV_PARAM_INDEX_VER_NUM] != EF_ENV_VER_NUM) {
|
||||
env_cache_changed = true;
|
||||
/* update version number */
|
||||
env_cache[ENV_PARAM_INDEX_VER_NUM] = EF_ENV_VER_NUM;
|
||||
/* add a new ENV when it's not found */
|
||||
for (i = 0; i < default_env_set_size; i++) {
|
||||
if (find_env(default_env_set[i].key) == NULL) {
|
||||
create_env(default_env_set[i].key, default_env_set[i].value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* unlock the ENV cache */
|
||||
ef_port_env_unlock();
|
||||
|
||||
return ef_save_env();
|
||||
}
|
||||
#endif /* EF_ENV_AUTO_UPDATE */
|
||||
|
||||
#endif /* EF_ENV_USING_WL_MODE */
|
||||
|
||||
#endif /* defined(EF_USING_ENV) && defined(EF_ENV_USING_LEGACY_MODE) */
|
||||
1082
1.主程序源代码/User/easydb/src/ef_env_legacy_wl.c
Normal file
1082
1.主程序源代码/User/easydb/src/ef_env_legacy_wl.c
Normal file
File diff suppressed because it is too large
Load Diff
289
1.主程序源代码/User/easydb/src/ef_iap.c
Normal file
289
1.主程序源代码/User/easydb/src/ef_iap.c
Normal file
@@ -0,0 +1,289 @@
|
||||
/*
|
||||
* This file is part of the EasyFlash Library.
|
||||
*
|
||||
* Copyright (c) 2015-2017, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: IAP(In-Application Programming) operating interface.
|
||||
* Created on: 2015-01-05
|
||||
*/
|
||||
|
||||
#include <easyflash.h>
|
||||
|
||||
#ifdef EF_USING_IAP
|
||||
|
||||
/* IAP section backup application section start address in flash */
|
||||
static uint32_t bak_app_start_addr = 0;
|
||||
|
||||
/**
|
||||
* Flash IAP function initialize.
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_iap_init(void) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
bak_app_start_addr = EF_START_ADDR ;
|
||||
|
||||
#if defined(EF_USING_ENV)
|
||||
bak_app_start_addr += ENV_AREA_SIZE;
|
||||
#endif
|
||||
|
||||
#if defined(EF_USING_LOG)
|
||||
bak_app_start_addr += LOG_AREA_SIZE;
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase backup area application data.
|
||||
*
|
||||
* @param app_size application size
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_erase_bak_app(size_t app_size) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
result = ef_port_erase(ef_get_bak_app_start_addr(), app_size);
|
||||
switch (result) {
|
||||
case EF_NO_ERR: {
|
||||
EF_INFO("Erased backup area application OK.\n");
|
||||
break;
|
||||
}
|
||||
case EF_ERASE_ERR: {
|
||||
EF_INFO("Warning: Erase backup area application fault!\n");
|
||||
/* will return when erase fault */
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase user old application by using specified erase function.
|
||||
*
|
||||
* @param user_app_addr application entry address
|
||||
* @param app_size application size
|
||||
* @param app_erase user specified application erase function
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_erase_spec_user_app(uint32_t user_app_addr, size_t app_size,
|
||||
EfErrCode (*app_erase)(uint32_t addr, size_t size)) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
result = app_erase(user_app_addr, app_size);
|
||||
switch (result) {
|
||||
case EF_NO_ERR: {
|
||||
EF_INFO("Erased user application OK.\n");
|
||||
break;
|
||||
}
|
||||
case EF_ERASE_ERR: {
|
||||
EF_INFO("Warning: Erase user application fault!\n");
|
||||
/* will return when erase fault */
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase user old application by using default `ef_port_erase` function.
|
||||
*
|
||||
* @param user_app_addr application entry address
|
||||
* @param app_size application size
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_erase_user_app(uint32_t user_app_addr, size_t app_size) {
|
||||
return ef_erase_spec_user_app(user_app_addr, app_size, ef_port_erase);
|
||||
}
|
||||
|
||||
/**
|
||||
* Erase old bootloader
|
||||
*
|
||||
* @param bl_addr bootloader entry address
|
||||
* @param bl_size bootloader size
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_erase_bl(uint32_t bl_addr, size_t bl_size) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
result = ef_port_erase(bl_addr, bl_size);
|
||||
switch (result) {
|
||||
case EF_NO_ERR: {
|
||||
EF_INFO("Erased bootloader OK.\n");
|
||||
break;
|
||||
}
|
||||
case EF_ERASE_ERR: {
|
||||
EF_INFO("Warning: Erase bootloader fault!\n");
|
||||
/* will return when erase fault */
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data of application to backup area.
|
||||
*
|
||||
* @param data a part of application
|
||||
* @param size data size
|
||||
* @param cur_size current write application size
|
||||
* @param total_size application total size
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_write_data_to_bak(uint8_t *data, size_t size, size_t *cur_size,
|
||||
size_t total_size) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
/* make sure don't write excess data */
|
||||
if (*cur_size + size > total_size) {
|
||||
size = total_size - *cur_size;
|
||||
}
|
||||
|
||||
result = ef_port_write(ef_get_bak_app_start_addr() + *cur_size, (uint32_t *) data, size);
|
||||
switch (result) {
|
||||
case EF_NO_ERR: {
|
||||
*cur_size += size;
|
||||
EF_DEBUG("Write data to backup area OK.\n");
|
||||
break;
|
||||
}
|
||||
case EF_WRITE_ERR: {
|
||||
EF_INFO("Warning: Write data to backup area fault!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy backup area application to application entry by using specified write function.
|
||||
*
|
||||
* @param user_app_addr application entry address
|
||||
* @param app_size application size
|
||||
* @param app_write user specified application write function
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_copy_spec_app_from_bak(uint32_t user_app_addr, size_t app_size,
|
||||
EfErrCode (*app_write)(uint32_t addr, const uint32_t *buf, size_t size)) {
|
||||
size_t cur_size;
|
||||
uint32_t app_cur_addr, bak_cur_addr;
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
/* 32 words size buffer */
|
||||
uint32_t buff[32];
|
||||
|
||||
/* cycle copy data */
|
||||
for (cur_size = 0; cur_size < app_size; cur_size += sizeof(buff)) {
|
||||
app_cur_addr = user_app_addr + cur_size;
|
||||
bak_cur_addr = ef_get_bak_app_start_addr() + cur_size;
|
||||
ef_port_read(bak_cur_addr, buff, sizeof(buff));
|
||||
result = app_write(app_cur_addr, buff, sizeof(buff));
|
||||
if (result != EF_NO_ERR) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (result) {
|
||||
case EF_NO_ERR: {
|
||||
EF_INFO("Write data to application entry OK.\n");
|
||||
break;
|
||||
}
|
||||
case EF_WRITE_ERR: {
|
||||
EF_INFO("Warning: Write data to application entry fault!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy backup area application to application entry by using default `ef_port_write` function.
|
||||
*
|
||||
* @param user_app_addr application entry address
|
||||
* @param app_size application size
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_copy_app_from_bak(uint32_t user_app_addr, size_t app_size) {
|
||||
return ef_copy_spec_app_from_bak(user_app_addr, app_size, ef_port_write);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy backup area bootloader to bootloader entry.
|
||||
*
|
||||
* @param bl_addr bootloader entry address
|
||||
* @param bl_size bootloader size
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_copy_bl_from_bak(uint32_t bl_addr, size_t bl_size) {
|
||||
size_t cur_size;
|
||||
uint32_t bl_cur_addr, bak_cur_addr;
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
/* 32 words buffer */
|
||||
uint32_t buff[32];
|
||||
|
||||
/* cycle copy data by 32bytes buffer */
|
||||
for (cur_size = 0; cur_size < bl_size; cur_size += sizeof(buff)) {
|
||||
bl_cur_addr = bl_addr + cur_size;
|
||||
bak_cur_addr = ef_get_bak_app_start_addr() + cur_size;
|
||||
ef_port_read(bak_cur_addr, buff, sizeof(buff));
|
||||
result = ef_port_write(bl_cur_addr, buff, sizeof(buff));
|
||||
if (result != EF_NO_ERR) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (result) {
|
||||
case EF_NO_ERR: {
|
||||
EF_INFO("Write data to bootloader entry OK.\n");
|
||||
break;
|
||||
}
|
||||
case EF_WRITE_ERR: {
|
||||
EF_INFO("Warning: Write data to bootloader entry fault!\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get IAP section start address in flash.
|
||||
*
|
||||
* @return size
|
||||
*/
|
||||
uint32_t ef_get_bak_app_start_addr(void) {
|
||||
return bak_app_start_addr;
|
||||
}
|
||||
|
||||
#endif /* EF_USING_IAP */
|
||||
731
1.主程序源代码/User/easydb/src/ef_log.c
Normal file
731
1.主程序源代码/User/easydb/src/ef_log.c
Normal file
@@ -0,0 +1,731 @@
|
||||
/*
|
||||
* This file is part of the EasyFlash Library.
|
||||
*
|
||||
* Copyright (c) 2015-2019, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: Save logs to flash.
|
||||
* Created on: 2015-06-04
|
||||
*/
|
||||
|
||||
#include <easyflash.h>
|
||||
|
||||
#ifdef EF_USING_LOG
|
||||
|
||||
#if defined(EF_USING_LOG) && !defined(LOG_AREA_SIZE)
|
||||
#error "Please configure log area size (in ef_cfg.h)"
|
||||
#endif
|
||||
|
||||
/* magic code on every sector header. 'EF' is 0xEF30EF30 */
|
||||
#define LOG_SECTOR_MAGIC 0xEF30EF30
|
||||
/* sector header size, includes the sector magic code and status magic code */
|
||||
#define LOG_SECTOR_HEADER_SIZE 12
|
||||
/* sector header word size,what is equivalent to the total number of sectors header index */
|
||||
#define LOG_SECTOR_HEADER_WORD_SIZE 3
|
||||
|
||||
/**
|
||||
* Sector status magic code
|
||||
* The sector status is 8B after LOG_SECTOR_MAGIC at every sector header.
|
||||
* ==============================================
|
||||
* | header(12B) | status |
|
||||
* ----------------------------------------------
|
||||
* | 0xEF30EF30 0xFFFFFFFF 0xFFFFFFFF | empty |
|
||||
* | 0xEF30EF30 0xFEFEFEFE 0xFFFFFFFF | using |
|
||||
* | 0xEF30EF30 0xFEFEFEFE 0xFCFCFCFC | full |
|
||||
* ==============================================
|
||||
*
|
||||
* State transition relationship: empty->using->full
|
||||
* The FULL status will change to EMPTY after sector clean.
|
||||
*/
|
||||
#define SECTOR_STATUS_MAGIC_EMPUT 0xFFFFFFFF
|
||||
#define SECTOR_STATUS_MAGIC_USING 0xFEFEFEFE
|
||||
#define SECTOR_STATUS_MAGIC_FULL 0xFCFCFCFC
|
||||
|
||||
typedef enum {
|
||||
SECTOR_STATUS_EMPUT,
|
||||
SECTOR_STATUS_USING,
|
||||
SECTOR_STATUS_FULL,
|
||||
SECTOR_STATUS_HEADER_ERROR,
|
||||
} SectorStatus;
|
||||
|
||||
typedef enum {
|
||||
SECTOR_HEADER_MAGIC_INDEX,
|
||||
SECTOR_HEADER_USING_INDEX,
|
||||
SECTOR_HEADER_FULL_INDEX,
|
||||
} SectorHeaderIndex;
|
||||
|
||||
/* the stored logs start address and end address. It's like a ring buffer implemented on flash. */
|
||||
static uint32_t log_start_addr = 0, log_end_addr = 0;
|
||||
/* saved log area address for flash */
|
||||
static uint32_t log_area_start_addr = 0;
|
||||
/* initialize OK flag */
|
||||
static bool init_ok = false;
|
||||
|
||||
static void find_start_and_end_addr(void);
|
||||
static uint32_t get_next_flash_sec_addr(uint32_t cur_addr);
|
||||
|
||||
/**
|
||||
* The flash save log function initialize.
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_log_init(void) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
|
||||
EF_ASSERT(LOG_AREA_SIZE);
|
||||
EF_ASSERT(EF_ERASE_MIN_SIZE);
|
||||
/* the log area size must be an integral multiple of erase minimum size. */
|
||||
EF_ASSERT(LOG_AREA_SIZE % EF_ERASE_MIN_SIZE == 0);
|
||||
/* the log area size must be more than twice of EF_ERASE_MIN_SIZE */
|
||||
EF_ASSERT(LOG_AREA_SIZE / EF_ERASE_MIN_SIZE >= 2);
|
||||
|
||||
#ifdef EF_USING_ENV
|
||||
log_area_start_addr = EF_START_ADDR + ENV_AREA_SIZE;
|
||||
#else
|
||||
log_area_start_addr = EF_START_ADDR;
|
||||
#endif
|
||||
|
||||
/* find the log store start address and end address */
|
||||
find_start_and_end_addr();
|
||||
/* initialize OK */
|
||||
init_ok = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get flash sector current status.
|
||||
*
|
||||
* @param addr sector address, this function will auto calculate the sector header address by this address.
|
||||
*
|
||||
* @return the flash sector current status
|
||||
*/
|
||||
static SectorStatus get_sector_status(uint32_t addr) {
|
||||
uint32_t header_buf[LOG_SECTOR_HEADER_WORD_SIZE] = {0}, header_addr = 0;
|
||||
uint32_t sector_header_magic = 0;
|
||||
uint32_t status_full_magic = 0, status_use_magic = 0;
|
||||
|
||||
/* calculate the sector header address */
|
||||
header_addr = addr & (~(EF_ERASE_MIN_SIZE - 1));
|
||||
|
||||
if (ef_port_read(header_addr, header_buf, sizeof(header_buf)) == EF_NO_ERR) {
|
||||
sector_header_magic = header_buf[SECTOR_HEADER_MAGIC_INDEX];
|
||||
status_use_magic = header_buf[SECTOR_HEADER_USING_INDEX];
|
||||
status_full_magic = header_buf[SECTOR_HEADER_FULL_INDEX];
|
||||
} else {
|
||||
EF_DEBUG("Error: Read sector header data error.\n");
|
||||
return SECTOR_STATUS_HEADER_ERROR;
|
||||
}
|
||||
|
||||
/* compare header magic code */
|
||||
if(sector_header_magic == LOG_SECTOR_MAGIC){
|
||||
if((status_use_magic == SECTOR_STATUS_MAGIC_EMPUT) && (status_full_magic == SECTOR_STATUS_MAGIC_EMPUT)) {
|
||||
return SECTOR_STATUS_EMPUT;
|
||||
} else if((status_use_magic == SECTOR_STATUS_MAGIC_USING) && (status_full_magic == SECTOR_STATUS_MAGIC_EMPUT)) {
|
||||
return SECTOR_STATUS_USING;
|
||||
} else if((status_use_magic == SECTOR_STATUS_MAGIC_USING) && (status_full_magic == SECTOR_STATUS_MAGIC_FULL)) {
|
||||
return SECTOR_STATUS_FULL;
|
||||
} else {
|
||||
return SECTOR_STATUS_HEADER_ERROR;
|
||||
}
|
||||
} else {
|
||||
return SECTOR_STATUS_HEADER_ERROR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Write flash sector current status.
|
||||
*
|
||||
* @param addr sector address, this function will auto calculate the sector header address by this address.
|
||||
* @param status sector cur status
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
static EfErrCode write_sector_status(uint32_t addr, SectorStatus status) {
|
||||
uint32_t header, header_addr = 0;
|
||||
|
||||
/* calculate the sector header address */
|
||||
header_addr = addr & (~(EF_ERASE_MIN_SIZE - 1));
|
||||
|
||||
/* calculate the sector staus magic */
|
||||
switch (status) {
|
||||
case SECTOR_STATUS_EMPUT: {
|
||||
header = LOG_SECTOR_MAGIC;
|
||||
return ef_port_write(header_addr, &header, sizeof(header));
|
||||
}
|
||||
case SECTOR_STATUS_USING: {
|
||||
header = SECTOR_STATUS_MAGIC_USING;
|
||||
return ef_port_write(header_addr + sizeof(header), &header, sizeof(header));
|
||||
}
|
||||
case SECTOR_STATUS_FULL: {
|
||||
header = SECTOR_STATUS_MAGIC_FULL;
|
||||
return ef_port_write(header_addr + sizeof(header) * 2, &header, sizeof(header));
|
||||
}
|
||||
default:
|
||||
return EF_WRITE_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the current flash sector using end address by continuous 0xFF.
|
||||
*
|
||||
* @param addr sector address
|
||||
*
|
||||
* @return current flash sector using end address
|
||||
*/
|
||||
static uint32_t find_sec_using_end_addr(uint32_t addr) {
|
||||
/* read section data buffer size */
|
||||
#define READ_BUF_SIZE 32
|
||||
|
||||
uint32_t sector_start = addr, data_start = addr, continue_ff = 0, read_buf_size = 0, i;
|
||||
uint8_t buf[READ_BUF_SIZE];
|
||||
|
||||
EF_ASSERT(READ_BUF_SIZE % 4 == 0);
|
||||
/* calculate the sector start and data start address */
|
||||
sector_start = addr & (~(EF_ERASE_MIN_SIZE - 1));
|
||||
data_start = sector_start + LOG_SECTOR_HEADER_SIZE;
|
||||
|
||||
/* counts continuous 0xFF which is end of sector */
|
||||
while (data_start < sector_start + EF_ERASE_MIN_SIZE) {
|
||||
if (data_start + READ_BUF_SIZE < sector_start + EF_ERASE_MIN_SIZE) {
|
||||
read_buf_size = READ_BUF_SIZE;
|
||||
} else {
|
||||
read_buf_size = sector_start + EF_ERASE_MIN_SIZE - data_start;
|
||||
}
|
||||
ef_port_read(data_start, (uint32_t *)buf, read_buf_size);
|
||||
for (i = 0; i < read_buf_size; i++) {
|
||||
if (buf[i] == 0xFF) {
|
||||
continue_ff++;
|
||||
} else {
|
||||
continue_ff = 0;
|
||||
}
|
||||
}
|
||||
data_start += read_buf_size;
|
||||
}
|
||||
/* calculate current flash sector using end address */
|
||||
if (continue_ff >= EF_ERASE_MIN_SIZE - LOG_SECTOR_HEADER_SIZE) {
|
||||
/* from 0 to sec_size all sector is 0xFF, so the sector is empty */
|
||||
return sector_start + LOG_SECTOR_HEADER_SIZE;
|
||||
} else if (continue_ff >= 4) {
|
||||
/* form end_addr - 4 to sec_size length all area is 0xFF, so it's used part of the sector.
|
||||
* the address must be word alignment. */
|
||||
if (continue_ff % 4 != 0) {
|
||||
continue_ff = (continue_ff / 4 + 1) * 4;
|
||||
}
|
||||
return sector_start + EF_ERASE_MIN_SIZE - continue_ff;
|
||||
} else {
|
||||
/* all sector not has continuous 0xFF, so the sector is full */
|
||||
return sector_start + EF_ERASE_MIN_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the log store start address and end address.
|
||||
* It's like a ring buffer implemented on flash.
|
||||
* The flash log area can be in two states depending on start address and end address:
|
||||
* state 1 state 2
|
||||
* |============| |============|
|
||||
* log area start--> |############| <-- start address |############| <-- end address
|
||||
* |############| | empty |
|
||||
* |------------| |------------|
|
||||
* |############| |############| <-- start address
|
||||
* |############| |############|
|
||||
* |------------| |------------|
|
||||
* | . | | . |
|
||||
* | . | | . |
|
||||
* | . | | . |
|
||||
* |------------| |------------|
|
||||
* |############| <-- end address |############|
|
||||
* | empty | |############|
|
||||
* log area end --> |============| |============|
|
||||
*
|
||||
* LOG_AREA_SIZE = log area end - log area star
|
||||
*
|
||||
*/
|
||||
static void find_start_and_end_addr(void) {
|
||||
size_t cur_size = 0;
|
||||
SectorStatus cur_sec_status, last_sec_status;
|
||||
uint32_t cur_using_sec_addr = 0;
|
||||
/* all status sector counts */
|
||||
size_t empty_sec_counts = 0, using_sec_counts = 0, full_sector_counts = 0;
|
||||
/* total sector number */
|
||||
size_t total_sec_num = LOG_AREA_SIZE / EF_ERASE_MIN_SIZE;
|
||||
/* see comment of find_start_and_end_addr function */
|
||||
uint8_t cur_log_sec_state = 0;
|
||||
|
||||
/* get the first sector status */
|
||||
cur_sec_status = get_sector_status(log_area_start_addr);
|
||||
last_sec_status = cur_sec_status;
|
||||
|
||||
for (cur_size = EF_ERASE_MIN_SIZE; cur_size < LOG_AREA_SIZE; cur_size += EF_ERASE_MIN_SIZE) {
|
||||
/* get current sector status */
|
||||
cur_sec_status = get_sector_status(log_area_start_addr + cur_size);
|
||||
/* compare last and current status */
|
||||
switch (last_sec_status) {
|
||||
case SECTOR_STATUS_EMPUT: {
|
||||
switch (cur_sec_status) {
|
||||
case SECTOR_STATUS_EMPUT:
|
||||
break;
|
||||
case SECTOR_STATUS_USING:
|
||||
EF_DEBUG("Error: Log area error! Now will clean all log area.\n");
|
||||
ef_log_clean();
|
||||
return;
|
||||
case SECTOR_STATUS_FULL:
|
||||
EF_DEBUG("Error: Log area error! Now will clean all log area.\n");
|
||||
ef_log_clean();
|
||||
return;
|
||||
}
|
||||
empty_sec_counts++;
|
||||
break;
|
||||
}
|
||||
case SECTOR_STATUS_USING: {
|
||||
switch (cur_sec_status) {
|
||||
case SECTOR_STATUS_EMPUT:
|
||||
/* like state 1 */
|
||||
cur_log_sec_state = 1;
|
||||
log_start_addr = log_area_start_addr;
|
||||
cur_using_sec_addr = log_area_start_addr + cur_size - EF_ERASE_MIN_SIZE;
|
||||
break;
|
||||
case SECTOR_STATUS_USING:
|
||||
EF_DEBUG("Error: Log area error! Now will clean all log area.\n");
|
||||
ef_log_clean();
|
||||
return;
|
||||
case SECTOR_STATUS_FULL:
|
||||
/* like state 2 */
|
||||
cur_log_sec_state = 2;
|
||||
log_start_addr = log_area_start_addr + cur_size;
|
||||
cur_using_sec_addr = log_area_start_addr + cur_size - EF_ERASE_MIN_SIZE;
|
||||
break;
|
||||
}
|
||||
using_sec_counts++;
|
||||
break;
|
||||
}
|
||||
case SECTOR_STATUS_FULL: {
|
||||
switch (cur_sec_status) {
|
||||
case SECTOR_STATUS_EMPUT:
|
||||
/* like state 1 */
|
||||
if (cur_log_sec_state == 2) {
|
||||
EF_DEBUG("Error: Log area error! Now will clean all log area.\n");
|
||||
ef_log_clean();
|
||||
return;
|
||||
} else {
|
||||
cur_log_sec_state = 1;
|
||||
log_start_addr = log_area_start_addr;
|
||||
log_end_addr = log_area_start_addr + cur_size;
|
||||
cur_using_sec_addr = log_area_start_addr + cur_size - EF_ERASE_MIN_SIZE;
|
||||
}
|
||||
break;
|
||||
case SECTOR_STATUS_USING:
|
||||
if(total_sec_num <= 2) {
|
||||
/* like state 1 */
|
||||
cur_log_sec_state = 1;
|
||||
log_start_addr = log_area_start_addr;
|
||||
cur_using_sec_addr = log_area_start_addr + cur_size;
|
||||
} else {
|
||||
/* like state 2 when the sector is the last one */
|
||||
if (cur_size + EF_ERASE_MIN_SIZE >= LOG_AREA_SIZE) {
|
||||
cur_log_sec_state = 2;
|
||||
log_start_addr = get_next_flash_sec_addr(log_area_start_addr + cur_size);
|
||||
cur_using_sec_addr = log_area_start_addr + cur_size;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SECTOR_STATUS_FULL:
|
||||
break;
|
||||
}
|
||||
full_sector_counts++;
|
||||
break;
|
||||
}
|
||||
case SECTOR_STATUS_HEADER_ERROR:
|
||||
EF_DEBUG("Error: Log sector header error! Now will clean all log area.\n");
|
||||
ef_log_clean();
|
||||
return;
|
||||
}
|
||||
last_sec_status = cur_sec_status;
|
||||
}
|
||||
|
||||
/* the last sector status counts */
|
||||
if (cur_sec_status == SECTOR_STATUS_EMPUT) {
|
||||
empty_sec_counts++;
|
||||
} else if (cur_sec_status == SECTOR_STATUS_USING) {
|
||||
using_sec_counts++;
|
||||
} else if (cur_sec_status == SECTOR_STATUS_FULL) {
|
||||
full_sector_counts++;
|
||||
} else if (cur_sec_status == SECTOR_STATUS_HEADER_ERROR) {
|
||||
EF_DEBUG("Error: Log sector header error! Now will clean all log area.\n");
|
||||
ef_log_clean();
|
||||
return;
|
||||
}
|
||||
|
||||
if (using_sec_counts != 1) {
|
||||
/* this state is almost impossible */
|
||||
EF_DEBUG("Error: There must be only one sector status is USING! Now will clean all log area.\n");
|
||||
ef_log_clean();
|
||||
} else {
|
||||
/* find the end address */
|
||||
log_end_addr = find_sec_using_end_addr(cur_using_sec_addr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get log used flash total size.
|
||||
*
|
||||
* @return log used flash total size. @note NOT contain sector headers
|
||||
*/
|
||||
size_t ef_log_get_used_size(void) {
|
||||
size_t header_total_num = 0, physical_size = 0;
|
||||
/* must be call this function after initialize OK */
|
||||
if (!init_ok) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (log_start_addr < log_end_addr) {
|
||||
physical_size = log_end_addr - log_start_addr;
|
||||
} else {
|
||||
physical_size = LOG_AREA_SIZE - (log_start_addr - log_end_addr);
|
||||
}
|
||||
|
||||
header_total_num = physical_size / EF_ERASE_MIN_SIZE + 1;
|
||||
|
||||
return physical_size - header_total_num * LOG_SECTOR_HEADER_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sequential reading log data. It will ignore sector headers.
|
||||
*
|
||||
* @param addr address
|
||||
* @param log log buffer
|
||||
* @param size log size, not contain sector headers.
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
static EfErrCode log_seq_read(uint32_t addr, uint32_t *log, size_t size) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
size_t read_size = 0, read_size_temp = 0;
|
||||
|
||||
while (size) {
|
||||
/* move to sector data address */
|
||||
if ((addr + read_size) % EF_ERASE_MIN_SIZE == 0) {
|
||||
addr += LOG_SECTOR_HEADER_SIZE;
|
||||
}
|
||||
/* calculate current sector last data size */
|
||||
read_size_temp = EF_ERASE_MIN_SIZE - (addr % EF_ERASE_MIN_SIZE);
|
||||
if (size < read_size_temp) {
|
||||
read_size_temp = size;
|
||||
}
|
||||
result = ef_port_read(addr + read_size, log + read_size / 4, read_size_temp);
|
||||
if (result != EF_NO_ERR) {
|
||||
return result;
|
||||
}
|
||||
read_size += read_size_temp;
|
||||
size -= read_size_temp;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate flash physical address by log index.
|
||||
*
|
||||
* @param index log index
|
||||
*
|
||||
* @return flash physical address
|
||||
*/
|
||||
static uint32_t log_index2addr(size_t index) {
|
||||
size_t header_total_offset = 0;
|
||||
/* total include sector number */
|
||||
size_t sector_num = index / (EF_ERASE_MIN_SIZE - LOG_SECTOR_HEADER_SIZE) + 1;
|
||||
|
||||
header_total_offset = sector_num * LOG_SECTOR_HEADER_SIZE;
|
||||
if (log_start_addr < log_end_addr) {
|
||||
return log_start_addr + index + header_total_offset;
|
||||
} else {
|
||||
if (log_start_addr + index + header_total_offset < log_area_start_addr + LOG_AREA_SIZE) {
|
||||
return log_start_addr + index + header_total_offset;
|
||||
} else {
|
||||
return log_start_addr + index + header_total_offset - LOG_AREA_SIZE;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read log from flash.
|
||||
*
|
||||
* @param index index for saved log.
|
||||
* Minimum index is 0.
|
||||
* Maximum index is ef_log_get_used_size() - 1.
|
||||
* @param log the log which will read from flash
|
||||
* @param size read bytes size
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_log_read(size_t index, uint32_t *log, size_t size) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
size_t cur_using_size = ef_log_get_used_size();
|
||||
size_t read_size_temp = 0;
|
||||
size_t header_total_num = 0;
|
||||
|
||||
if (!size) {
|
||||
return result;
|
||||
}
|
||||
|
||||
EF_ASSERT(size % 4 == 0);
|
||||
EF_ASSERT(index < cur_using_size);
|
||||
|
||||
if (index + size > cur_using_size) {
|
||||
EF_DEBUG("Warning: Log read size out of bound. Cut read size.\n");
|
||||
size = cur_using_size - index;
|
||||
}
|
||||
/* must be call this function after initialize OK */
|
||||
if (!init_ok) {
|
||||
return EF_ENV_INIT_FAILED;
|
||||
}
|
||||
|
||||
if (log_start_addr < log_end_addr) {
|
||||
log_seq_read(log_index2addr(index), log, size);
|
||||
} else {
|
||||
if (log_index2addr(index) + size <= log_area_start_addr + LOG_AREA_SIZE) {
|
||||
/* Flash log area
|
||||
* |--------------|
|
||||
* log_area_start_addr --> |##############|
|
||||
* |##############|
|
||||
* |##############|
|
||||
* |--------------|
|
||||
* |##############|
|
||||
* |##############|
|
||||
* |##############| <-- log_end_addr
|
||||
* |--------------|
|
||||
* log_start_addr --> |##############|
|
||||
* read start --> |**************| <-- read end
|
||||
* |##############|
|
||||
* |--------------|
|
||||
*
|
||||
* read from (log_start_addr + log_index2addr(index)) to (log_start_addr + index + log_index2addr(index))
|
||||
*/
|
||||
result = log_seq_read(log_index2addr(index), log, size);
|
||||
} else if (log_index2addr(index) < log_area_start_addr + LOG_AREA_SIZE) {
|
||||
/* Flash log area
|
||||
* |--------------|
|
||||
* log_area_start_addr --> |**************| <-- read end
|
||||
* |##############|
|
||||
* |##############|
|
||||
* |--------------|
|
||||
* |##############|
|
||||
* |##############|
|
||||
* |##############| <-- log_end_addr
|
||||
* |--------------|
|
||||
* log_start_addr --> |##############|
|
||||
* read start --> |**************|
|
||||
* |**************|
|
||||
* |--------------|
|
||||
* read will by 2 steps
|
||||
* step1: read from (log_start_addr + log_index2addr(index)) to flash log area end address
|
||||
* step2: read from flash log area start address to read size's end address
|
||||
*/
|
||||
read_size_temp = (log_area_start_addr + LOG_AREA_SIZE) - log_index2addr(index);
|
||||
header_total_num = read_size_temp / EF_ERASE_MIN_SIZE;
|
||||
/* Minus some ignored bytes */
|
||||
read_size_temp -= header_total_num * LOG_SECTOR_HEADER_SIZE;
|
||||
result = log_seq_read(log_index2addr(index), log, read_size_temp);
|
||||
if (result == EF_NO_ERR) {
|
||||
result = log_seq_read(log_area_start_addr, log + read_size_temp / 4, size - read_size_temp);
|
||||
}
|
||||
} else {
|
||||
/* Flash log area
|
||||
* |--------------|
|
||||
* log_area_start_addr --> |##############|
|
||||
* read start --> |**************|
|
||||
* |**************| <-- read end
|
||||
* |--------------|
|
||||
* |##############|
|
||||
* |##############|
|
||||
* |##############| <-- log_end_addr
|
||||
* |--------------|
|
||||
* log_start_addr --> |##############|
|
||||
* |##############|
|
||||
* |##############|
|
||||
* |--------------|
|
||||
* read from (log_start_addr + log_index2addr(index) - LOG_AREA_SIZE) to read size's end address
|
||||
*/
|
||||
result = log_seq_read(log_index2addr(index) - LOG_AREA_SIZE, log, size);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write log to flash.
|
||||
*
|
||||
* @param log the log which will be write to flash
|
||||
* @param size write bytes size
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_log_write(const uint32_t *log, size_t size) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
size_t write_size = 0, writable_size = 0;
|
||||
uint32_t write_addr = log_end_addr, erase_addr;
|
||||
SectorStatus sector_status;
|
||||
|
||||
EF_ASSERT(size % 4 == 0);
|
||||
/* must be call this function after initialize OK */
|
||||
if (!init_ok) {
|
||||
return EF_ENV_INIT_FAILED;
|
||||
}
|
||||
|
||||
if ((sector_status = get_sector_status(write_addr)) == SECTOR_STATUS_HEADER_ERROR) {
|
||||
return EF_WRITE_ERR;
|
||||
}
|
||||
/* write some log when current sector status is USING and EMPTY */
|
||||
if ((sector_status == SECTOR_STATUS_USING) || (sector_status == SECTOR_STATUS_EMPUT)) {
|
||||
/* write the already erased but not used area */
|
||||
writable_size = EF_ERASE_MIN_SIZE - ((write_addr - log_area_start_addr) % EF_ERASE_MIN_SIZE);
|
||||
if (size >= writable_size) {
|
||||
result = ef_port_write(write_addr, log, writable_size);
|
||||
if (result != EF_NO_ERR) {
|
||||
goto exit;
|
||||
}
|
||||
/* change the current sector status to FULL */
|
||||
result = write_sector_status(write_addr, SECTOR_STATUS_FULL);
|
||||
if (result != EF_NO_ERR) {
|
||||
goto exit;
|
||||
}
|
||||
write_size += writable_size;
|
||||
} else {
|
||||
result = ef_port_write(write_addr, log, size);
|
||||
log_end_addr = write_addr + size;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
/* erase and write remain log */
|
||||
while (true) {
|
||||
/* calculate next available sector address */
|
||||
erase_addr = write_addr = get_next_flash_sec_addr(write_addr - 4);
|
||||
/* move the flash log start address to next available sector address */
|
||||
if (log_start_addr == erase_addr) {
|
||||
log_start_addr = get_next_flash_sec_addr(log_start_addr);
|
||||
}
|
||||
/* erase sector */
|
||||
result = ef_port_erase(erase_addr, EF_ERASE_MIN_SIZE);
|
||||
if (result != EF_NO_ERR) {
|
||||
goto exit;
|
||||
}
|
||||
/* change the sector status to EMPTY and USING when write begin sector start address */
|
||||
result = write_sector_status(write_addr, SECTOR_STATUS_EMPUT);
|
||||
result = write_sector_status(write_addr, SECTOR_STATUS_USING);
|
||||
if (result == EF_NO_ERR) {
|
||||
write_addr += LOG_SECTOR_HEADER_SIZE;
|
||||
} else {
|
||||
goto exit;
|
||||
}
|
||||
/* calculate current sector writable data size */
|
||||
writable_size = EF_ERASE_MIN_SIZE - LOG_SECTOR_HEADER_SIZE;
|
||||
if (size - write_size >= writable_size) {
|
||||
result = ef_port_write(write_addr, log + write_size / 4, writable_size);
|
||||
if (result != EF_NO_ERR) {
|
||||
goto exit;
|
||||
}
|
||||
/* change the current sector status to FULL */
|
||||
result = write_sector_status(write_addr, SECTOR_STATUS_FULL);
|
||||
if (result != EF_NO_ERR) {
|
||||
goto exit;
|
||||
}
|
||||
log_end_addr = write_addr + writable_size;
|
||||
write_size += writable_size;
|
||||
write_addr += writable_size;
|
||||
} else {
|
||||
result = ef_port_write(write_addr, log + write_size / 4, size - write_size);
|
||||
if (result != EF_NO_ERR) {
|
||||
goto exit;
|
||||
}
|
||||
log_end_addr = write_addr + (size - write_size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next flash sector address.The log total sector like ring buffer which implement by flash.
|
||||
*
|
||||
* @param cur_addr cur flash address
|
||||
*
|
||||
* @return next flash sector address
|
||||
*/
|
||||
static uint32_t get_next_flash_sec_addr(uint32_t cur_addr) {
|
||||
size_t cur_sec_id = (cur_addr - log_area_start_addr) / EF_ERASE_MIN_SIZE;
|
||||
size_t sec_total_num = LOG_AREA_SIZE / EF_ERASE_MIN_SIZE;
|
||||
|
||||
if (cur_sec_id + 1 >= sec_total_num) {
|
||||
/* return to ring head */
|
||||
return log_area_start_addr;
|
||||
} else {
|
||||
return log_area_start_addr + (cur_sec_id + 1) * EF_ERASE_MIN_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean all log which in flash.
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
EfErrCode ef_log_clean(void) {
|
||||
EfErrCode result = EF_NO_ERR;
|
||||
uint32_t write_addr = log_area_start_addr;
|
||||
|
||||
/* clean address */
|
||||
log_start_addr = log_area_start_addr;
|
||||
log_end_addr = log_start_addr + LOG_SECTOR_HEADER_SIZE;
|
||||
/* erase log flash area */
|
||||
result = ef_port_erase(log_area_start_addr, LOG_AREA_SIZE);
|
||||
if (result != EF_NO_ERR) {
|
||||
goto exit;
|
||||
}
|
||||
/* setting first sector is EMPTY to USING */
|
||||
write_sector_status(write_addr, SECTOR_STATUS_EMPUT);
|
||||
write_sector_status(write_addr, SECTOR_STATUS_USING);
|
||||
if (result != EF_NO_ERR) {
|
||||
goto exit;
|
||||
}
|
||||
write_addr += EF_ERASE_MIN_SIZE;
|
||||
/* add sector header */
|
||||
while (true) {
|
||||
write_sector_status(write_addr, SECTOR_STATUS_EMPUT);
|
||||
if (result != EF_NO_ERR) {
|
||||
goto exit;
|
||||
}
|
||||
write_addr += EF_ERASE_MIN_SIZE;
|
||||
if (write_addr >= log_area_start_addr + LOG_AREA_SIZE) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* EF_USING_LOG */
|
||||
99
1.主程序源代码/User/easydb/src/ef_utils.c
Normal file
99
1.主程序源代码/User/easydb/src/ef_utils.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* This file is part of the EasyFlash Library.
|
||||
*
|
||||
* Copyright (c) 2015-2017, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: Some utils for this library.
|
||||
* Created on: 2015-01-14
|
||||
*/
|
||||
|
||||
#include <easyflash.h>
|
||||
|
||||
static const uint32_t crc32_table[] =
|
||||
{
|
||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
|
||||
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
||||
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
|
||||
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
||||
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
|
||||
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
||||
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
|
||||
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
||||
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
|
||||
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
||||
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
|
||||
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
||||
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
|
||||
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
|
||||
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
|
||||
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
|
||||
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
|
||||
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
|
||||
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
|
||||
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
||||
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
|
||||
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
|
||||
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
|
||||
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
||||
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
|
||||
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
|
||||
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
|
||||
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
|
||||
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
|
||||
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
||||
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
|
||||
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
|
||||
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
|
||||
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
|
||||
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
|
||||
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
||||
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
|
||||
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
|
||||
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
|
||||
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
||||
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
|
||||
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate the CRC32 value of a memory buffer.
|
||||
*
|
||||
* @param crc accumulated CRC32 value, must be 0 on first call
|
||||
* @param buf buffer to calculate CRC32 value for
|
||||
* @param size bytes in buffer
|
||||
*
|
||||
* @return calculated CRC32 value
|
||||
*/
|
||||
uint32_t ef_calc_crc32(uint32_t crc, const void *buf, size_t size)
|
||||
{
|
||||
const uint8_t *p;
|
||||
|
||||
p = (const uint8_t *)buf;
|
||||
crc = crc ^ ~0U;
|
||||
|
||||
while (size--) {
|
||||
crc = crc32_table[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
|
||||
}
|
||||
|
||||
return crc ^ ~0U;
|
||||
}
|
||||
Reference in New Issue
Block a user