Files

83 lines
2.6 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* SPDX-License-Identifier: GPL-2.0+ */
#ifndef __FW_ENV_H__
#define __FW_ENV_H__
#include <stdint.h>
#include <stddef.h>
#include <sys/types.h> // 引入标准类型头文件
// 基础类型定义
typedef uint32_t u32;
typedef uint8_t u8;
typedef int32_t s32;
typedef unsigned long ulong; // 关键:显式定义ulong类型
// 环境变量核心宏
#define MAX_REDUND 2
#define ENV_CRC_SIZE sizeof(uint32_t)
#define ENV_HEADER_SIZE ENV_CRC_SIZE
#define CONFIG_ENV_SIZE 0x10000
// 环境变量操作选项
struct env_opts {
#ifdef CONFIG_FILE
char *config_file;
#endif
char *lockname;
char *part;
ulong offset;
ulong size;
ulong esize;
};
// 环境变量设备配置
struct env_device {
char *devname; // /dev/mtdX
ulong offset; // 偏移
ulong size; // 环境变量总大小
ulong erasesize; // 擦除块大小
int type; // 设备类型(MTD
int ubi_volume; // UBI卷标识
};
// 环境变量上下文
struct environment {
void *image; // 环境变量缓冲区
uint32_t *crc; // CRC指针(前4字节)
uint8_t *flags; // 冗余标志
char *data; // 数据区指针
ulong usable_envsize; // 数据区有效长度
int dirty; // 是否需要刷写
int dev_current; // 当前激活设备
int flag_scheme; // 标志方案
};
// 快捷宏
#define DEVNAME(idx) envdevices[idx].devname
#define DEVOFFSET(idx) envdevices[idx].offset
#define ENVSIZE(idx) envdevices[idx].size
#define DEVESIZE(idx) envdevices[idx].erasesize
#define DEVTYPE(idx) envdevices[idx].type
#define IS_UBI(idx) envdevices[idx].ubi_volume
#define CUR_DEVNAME DEVNAME(environment.dev_current)
#define CUR_DEVOFFSET DEVOFFSET(environment.dev_current)
#define CUR_ENVSIZE ENVSIZE(environment.dev_current)
#define CUR_DEVESIZE DEVESIZE(environment.dev_current)
// 全局变量声明
extern struct env_device envdevices[MAX_REDUND];
extern struct environment environment;
extern int have_redund_env;
// 函数声明
int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts);
int fw_env_set(int argc, char *argv[], struct env_opts *opts);
int fw_parse_script(char *fname, struct env_opts *opts);
int fw_env_open(struct env_opts *opts);
char *fw_getenv(char *name);
int fw_env_write(char *name, char *value);
int fw_env_flush(struct env_opts *opts);
int fw_env_close(struct env_opts *opts);
char *fw_env_version(void);
#endif