1 Star 3 Fork 0

lyfan/fwenv_tool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fw_env.c 13.80 KB
一键复制 编辑 原始数据 按行查看 历史
lyfan 提交于 2021-08-21 17:16 . 初始提交
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
// SPDX-License-Identifier: GPL-2.0+
/*
* (C) Copyright 2000-2010
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
*
* (C) Copyright 2008
* Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <env_flags.h>
#include <fcntl.h>
#include <libgen.h>
#include <linux/fs.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include "fw_env_private.h"
#include "fw_env.h"
#define min(x, y) ({ \
typeof(x) _min1 = (x); \
typeof(y) _min2 = (y); \
(void) (&_min1 == &_min2); \
_min1 < _min2 ? _min1 : _min2; })
#define ENV_SIZE (ENV_SIZE_BYTE - sizeof(uint32_t))
struct env_image_single {
uint32_t crc; /* CRC32 over data bytes */
char data[];
};
enum flag_scheme {
FLAG_NONE,
FLAG_BOOLEAN,
FLAG_INCREMENTAL,
};
struct environment {
void *image;
uint32_t *crc;
unsigned char *flags;
char *data;
enum flag_scheme flag_scheme;
};
static struct environment environment = {
.flag_scheme = FLAG_NONE,
};
#include <env_default.h>
static int flash_io(int mode);
static char *skip_chars(char *s)
{
for (; *s != '\0'; s++) {
if (isblank(*s) || *s == '=')
return s;
}
return NULL;
}
static char *skip_blanks(char *s)
{
for (; *s != '\0'; s++) {
if (!isblank(*s))
return s;
}
return NULL;
}
/*
* s1 is either a simple 'name', or a 'name=value' pair.
* s2 is a 'name=value' pair.
* If the names match, return the value of s2, else NULL.
*/
static char *envmatch(char *s1, char *s2)
{
if (s1 == NULL || s2 == NULL)
return NULL;
while (*s1 == *s2++)
if (*s1++ == '=')
return s2;
if (*s1 == '\0' && *(s2 - 1) == '=')
return s2;
return NULL;
}
/**
* Search the environment for a variable.
* Return the value, if found, or NULL, if not found.
*/
char *fw_getenv(char *name)
{
char *env, *nxt;
for (env = environment.data; *env; env = nxt + 1) {
char *val;
for (nxt = env; *nxt; ++nxt) {
if (nxt >= &environment.data[ENV_SIZE]) {
fprintf(stderr, "## Error: "
"environment not terminated\n");
return NULL;
}
}
val = envmatch(name, env);
if (!val)
continue;
return val;
}
return NULL;
}
/*
* Search the default environment for a variable.
* Return the value, if found, or NULL, if not found.
*/
char *fw_getdefenv(char *name)
{
char *env, *nxt;
for (env = default_environment; *env; env = nxt + 1) {
char *val;
for (nxt = env; *nxt; ++nxt) {
if (nxt >= &default_environment[ENV_SIZE]) {
fprintf(stderr, "## Error: "
"default environment not terminated\n");
return NULL;
}
}
val = envmatch(name, env);
if (!val)
continue;
return val;
}
return NULL;
}
/*
* Print the current definition of one, or more, or all
* environment variables
*/
int fw_printenv(int argc, char *argv[], int value_only)
{
int i, rc = 0;
if (value_only && argc != 1) {
fprintf(stderr,
"## Error: `-n'/`--noheader' option requires exactly one argument\n");
return -1;
}
if (fw_env_init())
return -1;
if(flash_io(O_RDONLY) != 0){
perror("fw_printenv flash_io error\n");
return -1;
}
if (argc == 0) { /* Print all env variables */
char *env, *nxt;
for (env = environment.data; *env; env = nxt + 1) {
for (nxt = env; *nxt; ++nxt) {
if (nxt >= &environment.data[ENV_SIZE]) {
fprintf(stderr, "## Error: "
"environment not terminated\n");
return -1;
}
}
printf("%s\n", env);
}
fw_env_close();
return 0;
}
for (i = 0; i < argc; ++i) { /* print a subset of env variables */
char *name = argv[i];
char *val = NULL;
val = fw_getenv(name);
if (!val) {
fprintf(stderr, "## Error: \"%s\" not defined\n", name);
rc = -1;
continue;
}
if (value_only) {
puts(val);
break;
}
printf("%s=%s\n", name, val);
}
fw_env_close();
return rc;
}
int fw_env_flush(void)
{
/*
* Update CRC
*/
*environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
/* write environment back to flash */
if (flash_io(O_RDWR)) {
fprintf(stderr, "Error: can't write fw_env to binary file\n");
return -1;
}
return 0;
}
/*
* Set/Clear a single variable in the environment.
* This is called in sequence to update the environment
* in RAM without updating the copy in flash after each set
*/
int fw_env_write(char *name, char *value)
{
int len;
char *env, *nxt;
char *oldval = NULL;
int deleting, creating, overwriting;
/*
* search if variable with this name already exists
*/
for (nxt = env = environment.data; *env; env = nxt + 1) {
for (nxt = env; *nxt; ++nxt) {
if (nxt >= &environment.data[ENV_SIZE]) {
fprintf(stderr, "## Error: "
"environment not terminated\n");
errno = EINVAL;
return -1;
}
}
oldval = envmatch(name, env);
if (oldval)
break;
}
deleting = (oldval && !(value && strlen(value)));
creating = (!oldval && (value && strlen(value)));
overwriting = (oldval && (value && strlen(value)));
/* check for permission */
if (deleting) {
if (env_flags_validate_varaccess(name,
ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
printf("Can't delete \"%s\"\n", name);
errno = EROFS;
return -1;
}
} else if (overwriting) {
if (env_flags_validate_varaccess(name,
ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
printf("Can't overwrite \"%s\"\n", name);
errno = EROFS;
return -1;
} else if (env_flags_validate_varaccess(name,
ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
const char *defval = fw_getdefenv(name);
if (defval == NULL)
defval = "";
if (strcmp(oldval, defval)
!= 0) {
printf("Can't overwrite \"%s\"\n", name);
errno = EROFS;
return -1;
}
}
} else if (creating) {
if (env_flags_validate_varaccess(name,
ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
printf("Can't create \"%s\"\n", name);
errno = EROFS;
return -1;
}
} else
/* Nothing to do */
return 0;
if (deleting || overwriting) {
if (*++nxt == '\0') {
*env = '\0';
} else {
for (;;) {
*env = *nxt++;
if ((*env == '\0') && (*nxt == '\0'))
break;
++env;
}
}
*++env = '\0';
}
/* Delete only ? */
if (!value || !strlen(value))
return 0;
/*
* Append new definition at the end
*/
for (env = environment.data; *env || *(env + 1); ++env)
;
if (env > environment.data)
++env;
/*
* Overflow when:
* "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment)
*/
len = strlen(name) + 2;
/* add '=' for first arg, ' ' for all others */
len += strlen(value) + 1;
if (len > (&environment.data[ENV_SIZE] - env)) {
fprintf(stderr,
"Error: environment overflow, \"%s\" deleted\n", name);
return -1;
}
while ((*env = *name++) != '\0')
env++;
*env = '=';
while ((*++env = *value++) != '\0')
;
/* end is marked with double '\0' */
*++env = '\0';
return 0;
}
/*
* Deletes or sets environment variables. Returns -1 and sets errno error codes:
* 0 - OK
* EINVAL - need at least 1 argument
* EROFS - certain variables ("ethaddr", "serial#") cannot be
* modified or deleted
*
*/
int fw_env_set(int argc, char *argv[])
{
int i;
size_t len;
char *name, **valv;
char *oldval;
char *value = NULL;
int valc;
int ret;
if (argc < 1) {
fprintf(stderr, "## Error: variable name missing\n");
errno = EINVAL;
return -1;
}
if (fw_env_init()) {
fprintf(stderr, "Error: environment not initialized\n");
return -1;
}
if(flash_io(O_RDONLY) != 0){
perror("fw_env_set flash_io error\n");
return -1;
}
name = argv[0];
valv = argv + 1;
valc = argc - 1;
if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
fw_env_close();
return -1;
}
len = 0;
for (i = 0; i < valc; ++i) {
char *val = valv[i];
size_t val_len = strlen(val);
if (value)
value[len - 1] = ' ';
oldval = value;
value = realloc(value, len + val_len + 1);
if (!value) {
fprintf(stderr,
"Cannot malloc %zu bytes: %s\n",
len, strerror(errno));
free(oldval);
return -1;
}
memcpy(value + len, val, val_len);
len += val_len;
value[len++] = '\0';
}
fw_env_write(name, value);
free(value);
ret = fw_env_flush();
fw_env_close();
return ret;
}
/*
* Parse a file and configure the u-boot variables.
* The script file has a very simple format, as follows:
*
* Each line has a couple with name, value:
* <white spaces>variable_name<white spaces>variable_value
*
* Both variable_name and variable_value are interpreted as strings.
* Any character after <white spaces> and before ending \r\n is interpreted
* as variable's value (no comment allowed on these lines !)
*
* Comments are allowed if the first character in the line is #
*
* Returns -1 and sets errno error codes:
* 0 - OK
* -1 - Error
*/
int fw_parse_script(char *fname)
{
FILE *fp;
char *line = NULL;
size_t linesize = 0;
char *name;
char *val;
int lineno = 0;
int len;
int ret = 0;
if (fw_env_init()) {
fprintf(stderr, "Error: environment not initialized\n");
return -1;
}
if (strcmp(fname, "-") == 0)
fp = stdin;
else {
fp = fopen(fname, "r");
if (fp == NULL) {
fprintf(stderr, "I cannot open %s for reading\n",
fname);
return -1;
}
}
while ((len = getline(&line, &linesize, fp)) != -1) {
lineno++;
/*
* Read a whole line from the file. If the line is not
* terminated, reports an error and exit.
*/
if (line[len - 1] != '\n') {
fprintf(stderr,
"Line %d not correctly terminated\n",
lineno);
ret = -1;
break;
}
/* Drop ending line feed / carriage return */
line[--len] = '\0';
if (len && line[len - 1] == '\r')
line[--len] = '\0';
/* Skip comment or empty lines */
if (len == 0 || line[0] == '#')
continue;
/*
* Search for variable's name remove leading whitespaces
*/
name = skip_blanks(line);
if (!name)
continue;
/* The first white space is the end of variable name */
val = skip_chars(name);
len = strlen(name);
if (val) {
*val++ = '\0';
if ((val - name) < len)
val = skip_blanks(val);
else
val = NULL;
}
#ifdef DEBUG
fprintf(stderr, "Setting %s : %s\n",
name, val ? val : " removed");
#endif
if (env_flags_validate_type(name, val) < 0) {
ret = -1;
break;
}
/*
* If there is an error setting a variable,
* try to save the environment and returns an error
*/
if (fw_env_write(name, val)) {
fprintf(stderr,
"fw_env_write returns with error : %s\n",
strerror(errno));
ret = -1;
break;
}
}
free(line);
/* Close file if not stdin */
if (strcmp(fname, "-") != 0)
fclose(fp);
ret |= fw_env_flush();
fw_env_close();
return ret;
}
static int flash_io_read(int fd_current)
{
void *data = environment.image;
struct stat sb;
off_t rsectlen = 0;
if(fstat(fd_current, &sb) != 0){
fprintf(stderr, "fstat file %s failed: %s\n", ENV_IMG_FILE_NAME, strerror(errno));
return -1;
}
for(rsectlen = 0; rsectlen < sb.st_size; data += 4096){
if (sb.st_size - rsectlen >= 4096){
if(read(fd_current, data, 4096) != 4096){
fprintf(stderr, "Write error on %s: %s\n", ENV_IMG_FILE_NAME, strerror(errno));
return -1;
}
rsectlen += 4096;
} else{
if(read(fd_current, data, sb.st_size - rsectlen) != sb.st_size - rsectlen){
fprintf(stderr, "Write error on %s: %s\n", ENV_IMG_FILE_NAME, strerror(errno));
return -1;
}
rsectlen = sb.st_size;
}
}
return 0;
}
static int flash_io_write(int fd_current)
{
void *data = environment.image;
for(int wsectlen = 0; wsectlen < ENV_SIZE_BYTE; data += 4096){
if (ENV_SIZE_BYTE - wsectlen >= 4096){
if(write(fd_current, data, 4096) != 4096){
fprintf(stderr, "Write error on %s: %s\n", ENV_IMG_FILE_NAME, strerror(errno));
return -1;
}
wsectlen += 4096;
} else{
if(write(fd_current, data, ENV_SIZE_BYTE - wsectlen) != ENV_SIZE_BYTE - wsectlen){
fprintf(stderr, "Write error on %s: %s\n", ENV_IMG_FILE_NAME, strerror(errno));
return -1;
}
wsectlen = ENV_SIZE_BYTE;
}
}
return 0;
}
static int flash_io(int mode)
{
int fd_current, rc;
if(mode == O_RDONLY){ // fw_printenv
if(access(ENV_IMG_FILE_NAME, F_OK | R_OK) == 0){ // if ENV_IMG_FILE_NAME exist, we parse it!
fd_current = open(ENV_IMG_FILE_NAME, mode);
if (fd_current < 0) {
fprintf(stderr, "Can't open %s: %s\n", ENV_IMG_FILE_NAME, strerror(errno));
return -1;
}
rc = flash_io_read(fd_current);
}else{ // else we use default environment, and write it to %ENV_IMG_FILE_NAME
fprintf(stdout, "%s isn't exist or cannot be read, use default environment", ENV_IMG_FILE_NAME);
memcpy(environment.data, default_environment, sizeof(default_environment));
fw_env_flush();
return 0;
}
}else{ // fw_setenv or fw_printenv write to %ENV_IMG_FILE_NAME
fd_current = open(ENV_IMG_FILE_NAME, mode | O_CREAT | O_TRUNC, 0666);
if (fd_current < 0) {
fprintf(stderr, "Can't open %s: %s\n", ENV_IMG_FILE_NAME, strerror(errno));
return -1;
}
rc = flash_io_write(fd_current);
}
if (close(fd_current)) {
fprintf(stderr, "I/O error on %s: %s\n", ENV_IMG_FILE_NAME, strerror(errno));
return -1;
}
return rc;
}
/*
* Prevent confusion if running from erased flash memory
*/
int fw_env_init(void)
{
void *addr0 = NULL;
struct env_image_single *single;
int ret;
addr0 = calloc(1, ENV_SIZE_BYTE);
if (addr0 == NULL) {
fprintf(stderr, "Not enough memory for environment (%ld bytes)\n", (long)ENV_SIZE_BYTE);
ret = -ENOMEM;
goto open_cleanup;
}
environment.image = addr0;
single = addr0;
environment.crc = &single->crc;
environment.flags = NULL;
environment.data = single->data;
// don't use default environment defaultly
// memcpy(environment.data, default_environment, sizeof(default_environment));
return 0;
open_cleanup:
if (addr0)
free(addr0);
return ret;
}
/*
* Simply free allocated buffer with environment
*/
int fw_env_close(void)
{
if (environment.image)
free(environment.image);
environment.image = NULL;
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/liangyuf/fwenv_tool.git
git@gitee.com:liangyuf/fwenv_tool.git
liangyuf
fwenv_tool
fwenv_tool
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385