代码拉取完成,页面将自动刷新
// 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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。