cmiot_fal.c 源码
2026/7/20大约 2 分钟附录源码附录OTAFAL
cmiot_fal.c — OTA Flash 抽象层实现
路径: components/ota/cmiot/port/oneos/cmiot_fal.c
功能: OTA 远程升级组件的 Flash 抽象层(FAL)实现,将 OTA 核心的 Flash 操作请求转发到 OneOS FAL 框架。这是 OTA 组件与硬件 Flash 之间的桥梁。
引用章节: 7.8 OTA 远程升级组件 | 5.17 FAL Flash抽象层
完整源码
/**
***********************************************************************************************************************
* Copyright (c) 2021, China Mobile Communications Group Co.,Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @file ota_fal.c
*
* @brief Interface related to OS.
*
* @revision
* Date Author Notes
* 2020-10-13 OneOS Team First Version
***********************************************************************************************************************
*/
#include "cmiot_fal.h"
#include "cmiot_cfg.h"
#include "cmiot_log.h"
#include <fal_part.h>
void *cmiot_ota_fal_part_find(cmiot_char *name)
{
return (void *)fal_part_find(name);
}
cmiot_int32 cmiot_ota_fal_part_read(void *part, cmiot_uint32 addr, cmiot_char *buf, cmiot_uint32 size)
{
return fal_part_read((fal_part_t *)part, (uint32_t)addr, (uint8_t*)buf, size);
}
cmiot_int32 cmiot_ota_fal_part_write(void *part, cmiot_uint32 addr, cmiot_char *buf, cmiot_uint32 size)
{
return fal_part_write((fal_part_t *)part, (uint32_t)addr, (uint8_t*)buf, size);
}
cmiot_int32 cmiot_ota_fal_part_erase(void *part, cmiot_uint32 addr, size_t size)
{
return fal_part_erase((fal_part_t *)part, (uint32_t)addr, size);
}
void *cmiot_hal_get_device(cmiot_uint8 type)
{
cmiot_char *device = NULL;
if ((type == CMIOT_FILETYPE_PATCH) || (type == CMIOT_FILETYPE_PATCH_INFO))
{
device = cmiot_download_name();
}
if (device != NULL)
{
return cmiot_ota_fal_part_find(device);
}
else
{
return NULL;
}
}
cmiot_uint32 cmiot_hal_get_true_blocksize(cmiot_uint8 type)
{
fal_part_t *part = (fal_part_t *)cmiot_hal_get_device(type);
if (part && part->flash)
{
return (part->flash->block_size < 1024 ? 1024 : part->flash->block_size);
}
CMIOT_INFO("[CMIOT] ", "Get flash block size failed\r\n");
return 0;
}
cmiot_uint32 cmiot_hal_get_true_pagesize(cmiot_uint8 type)
{
fal_part_t *part = (fal_part_t *)cmiot_hal_get_device(type);
if (part && part->flash)
{
return part->flash->page_size;
}
CMIOT_INFO("[CMIOT] ", "Get flash page size failed\r\n");
return 0;
}
cmiot_uint32 cmiot_hal_get_delta_addr(void)
{
fal_part_t *part = (fal_part_t *)cmiot_hal_get_device(CMIOT_FILETYPE_PATCH);
if (part && part->info)
{
return part->info->offset;
}
CMIOT_INFO("[CMIOT] ", "Get download addr failed\r\n");
return 0;
}
cmiot_uint32 cmiot_hal_get_download_size()
{
fal_part_t *part = (fal_part_t *)cmiot_hal_get_device(CMIOT_FILETYPE_PATCH);
if (part && part->info)
{
return part->info->size;
}
CMIOT_INFO("[CMIOT] ", "Get download size failed\r\n");
return 0;
}关键说明
| 模块 | 说明 |
|---|---|
| 分区适配 | 将 OTA 的 cmiot_ota_fal_part_* 调用直接转发到 fal_part_* API,实现零开销适配 |
| 设备获取 | cmiot_hal_get_device 根据补丁文件类型查找对应的 Flash 分区名 |
| 块大小 | 最小块大小为 1024 字节,确保与差分算法的最小擦除单位兼容 |
| 错误处理 | 每个 cmiot_hal_get_* 函数在失败时通过 CMIOT_INFO 输出错误日志 |