2002-12-17 01:51:00 +00:00
|
|
|
/*
|
2008-05-07 03:16:15 +00:00
|
|
|
* (C) Copyright 2000-2008
|
2002-12-17 01:51:00 +00:00
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2003-04-12 23:38:12 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2002-12-17 01:51:00 +00:00
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.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>
|
|
|
|
|
2007-11-27 09:23:20 +00:00
|
|
|
#ifdef MTD_OLD
|
|
|
|
# include <linux/mtd/mtd.h>
|
|
|
|
#else
|
2008-01-08 21:58:27 +00:00
|
|
|
# define __user /* nothing */
|
2007-11-27 09:23:20 +00:00
|
|
|
# include <mtd/mtd-user.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "fw_env.h"
|
2002-12-17 01:51:00 +00:00
|
|
|
|
|
|
|
#define CMD_GETENV "fw_printenv"
|
|
|
|
#define CMD_SETENV "fw_setenv"
|
|
|
|
|
|
|
|
typedef struct envdev_s {
|
2007-11-27 09:23:20 +00:00
|
|
|
char devname[16]; /* Device name */
|
2003-04-12 23:38:12 +00:00
|
|
|
ulong devoff; /* Device offset */
|
|
|
|
ulong env_size; /* environment size */
|
|
|
|
ulong erase_size; /* device erase size */
|
2002-12-17 01:51:00 +00:00
|
|
|
} envdev_t;
|
|
|
|
|
|
|
|
static envdev_t envdevices[2];
|
|
|
|
static int curdev;
|
|
|
|
|
|
|
|
#define DEVNAME(i) envdevices[(i)].devname
|
2003-01-11 09:48:40 +00:00
|
|
|
#define DEVOFFSET(i) envdevices[(i)].devoff
|
2002-12-17 01:51:00 +00:00
|
|
|
#define ENVSIZE(i) envdevices[(i)].env_size
|
|
|
|
#define DEVESIZE(i) envdevices[(i)].erase_size
|
|
|
|
|
|
|
|
#define CFG_ENV_SIZE ENVSIZE(curdev)
|
|
|
|
|
2003-01-11 09:48:40 +00:00
|
|
|
#define ENV_SIZE getenvsize()
|
2002-12-17 01:51:00 +00:00
|
|
|
|
|
|
|
typedef struct environment_s {
|
2003-04-12 23:38:12 +00:00
|
|
|
ulong crc; /* CRC32 over data bytes */
|
2007-11-27 09:23:20 +00:00
|
|
|
unsigned char flags; /* active or obsolete */
|
|
|
|
char *data;
|
2002-12-17 01:51:00 +00:00
|
|
|
} env_t;
|
|
|
|
|
|
|
|
static env_t environment;
|
|
|
|
|
2003-01-11 09:48:40 +00:00
|
|
|
static int HaveRedundEnv = 0;
|
|
|
|
|
2007-11-27 09:23:20 +00:00
|
|
|
static unsigned char active_flag = 1;
|
|
|
|
static unsigned char obsolete_flag = 0;
|
2003-01-11 09:48:40 +00:00
|
|
|
|
2002-12-17 01:51:00 +00:00
|
|
|
|
|
|
|
#define XMK_STR(x) #x
|
|
|
|
#define MK_STR(x) XMK_STR(x)
|
|
|
|
|
2007-11-27 09:23:20 +00:00
|
|
|
static char default_environment[] = {
|
2003-01-11 09:48:40 +00:00
|
|
|
#if defined(CONFIG_BOOTARGS)
|
2003-04-12 23:38:12 +00:00
|
|
|
"bootargs=" CONFIG_BOOTARGS "\0"
|
2002-12-17 01:51:00 +00:00
|
|
|
#endif
|
2003-01-11 09:48:40 +00:00
|
|
|
#if defined(CONFIG_BOOTCOMMAND)
|
2003-04-12 23:38:12 +00:00
|
|
|
"bootcmd=" CONFIG_BOOTCOMMAND "\0"
|
2002-12-17 01:51:00 +00:00
|
|
|
#endif
|
2003-01-11 09:48:40 +00:00
|
|
|
#if defined(CONFIG_RAMBOOTCOMMAND)
|
2003-04-12 23:38:12 +00:00
|
|
|
"ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_NFSBOOTCOMMAND)
|
2003-04-12 23:38:12 +00:00
|
|
|
"nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
|
2003-04-12 23:38:12 +00:00
|
|
|
"bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0"
|
2002-12-17 01:51:00 +00:00
|
|
|
#endif
|
2003-01-11 09:48:40 +00:00
|
|
|
#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
|
2003-04-12 23:38:12 +00:00
|
|
|
"baudrate=" MK_STR (CONFIG_BAUDRATE) "\0"
|
2002-12-17 01:51:00 +00:00
|
|
|
#endif
|
2003-01-11 09:48:40 +00:00
|
|
|
#ifdef CONFIG_LOADS_ECHO
|
2003-04-12 23:38:12 +00:00
|
|
|
"loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
2002-12-17 01:51:00 +00:00
|
|
|
#ifdef CONFIG_ETHADDR
|
2003-04-12 23:38:12 +00:00
|
|
|
"ethaddr=" MK_STR (CONFIG_ETHADDR) "\0"
|
2002-12-17 01:51:00 +00:00
|
|
|
#endif
|
2003-01-11 09:48:40 +00:00
|
|
|
#ifdef CONFIG_ETH1ADDR
|
2003-04-12 23:38:12 +00:00
|
|
|
"eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_ETH2ADDR
|
2003-04-12 23:38:12 +00:00
|
|
|
"eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
2004-12-31 09:32:47 +00:00
|
|
|
#ifdef CONFIG_ETH3ADDR
|
|
|
|
"eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0"
|
|
|
|
#endif
|
2003-01-11 09:48:40 +00:00
|
|
|
#ifdef CONFIG_ETHPRIME
|
2003-04-12 23:38:12 +00:00
|
|
|
"ethprime=" CONFIG_ETHPRIME "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
2002-12-17 01:51:00 +00:00
|
|
|
#ifdef CONFIG_IPADDR
|
2003-04-12 23:38:12 +00:00
|
|
|
"ipaddr=" MK_STR (CONFIG_IPADDR) "\0"
|
2002-12-17 01:51:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SERVERIP
|
2003-04-12 23:38:12 +00:00
|
|
|
"serverip=" MK_STR (CONFIG_SERVERIP) "\0"
|
2002-12-17 01:51:00 +00:00
|
|
|
#endif
|
2003-01-11 09:48:40 +00:00
|
|
|
#ifdef CFG_AUTOLOAD
|
2003-04-12 23:38:12 +00:00
|
|
|
"autoload=" CFG_AUTOLOAD "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_ROOTPATH
|
2003-04-12 23:38:12 +00:00
|
|
|
"rootpath=" MK_STR (CONFIG_ROOTPATH) "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_GATEWAYIP
|
2003-04-12 23:38:12 +00:00
|
|
|
"gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NETMASK
|
2003-04-12 23:38:12 +00:00
|
|
|
"netmask=" MK_STR (CONFIG_NETMASK) "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_HOSTNAME
|
2003-04-12 23:38:12 +00:00
|
|
|
"hostname=" MK_STR (CONFIG_HOSTNAME) "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_BOOTFILE
|
2003-04-12 23:38:12 +00:00
|
|
|
"bootfile=" MK_STR (CONFIG_BOOTFILE) "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_LOADADDR
|
2003-04-12 23:38:12 +00:00
|
|
|
"loadaddr=" MK_STR (CONFIG_LOADADDR) "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_PREBOOT
|
2003-04-12 23:38:12 +00:00
|
|
|
"preboot=" CONFIG_PREBOOT "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_CLOCKS_IN_MHZ
|
2003-04-12 23:38:12 +00:00
|
|
|
"clocks_in_mhz=" "1" "\0"
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
2003-02-14 11:21:23 +00:00
|
|
|
#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
|
2003-04-12 23:38:12 +00:00
|
|
|
"pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0"
|
2003-02-14 11:21:23 +00:00
|
|
|
#endif
|
2003-01-11 09:48:40 +00:00
|
|
|
#ifdef CONFIG_EXTRA_ENV_SETTINGS
|
|
|
|
CONFIG_EXTRA_ENV_SETTINGS
|
|
|
|
#endif
|
2003-04-12 23:38:12 +00:00
|
|
|
"\0" /* Termimate env_t data with 2 NULs */
|
2002-12-17 01:51:00 +00:00
|
|
|
};
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
static int flash_io (int mode);
|
2007-11-27 09:23:20 +00:00
|
|
|
static char *envmatch (char * s1, char * s2);
|
2003-04-12 23:38:12 +00:00
|
|
|
static int env_init (void);
|
|
|
|
static int parse_config (void);
|
|
|
|
|
2003-01-11 09:48:40 +00:00
|
|
|
#if defined(CONFIG_FILE)
|
2003-04-12 23:38:12 +00:00
|
|
|
static int get_config (char *);
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
2003-04-12 23:38:12 +00:00
|
|
|
static inline ulong getenvsize (void)
|
2003-01-11 09:48:40 +00:00
|
|
|
{
|
2003-04-12 23:38:12 +00:00
|
|
|
ulong rc = CFG_ENV_SIZE - sizeof (long);
|
|
|
|
|
2003-01-11 09:48:40 +00:00
|
|
|
if (HaveRedundEnv)
|
2003-04-12 23:38:12 +00:00
|
|
|
rc -= sizeof (char);
|
2003-01-11 09:48:40 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2002-12-17 01:51:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Search the environment for a variable.
|
|
|
|
* Return the value, if found, or NULL, if not found.
|
|
|
|
*/
|
2007-11-27 09:23:20 +00:00
|
|
|
char *fw_getenv (char *name)
|
2002-12-17 01:51:00 +00:00
|
|
|
{
|
2007-11-27 09:23:20 +00:00
|
|
|
char *env, *nxt;
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if (env_init ())
|
2002-12-17 01:51:00 +00:00
|
|
|
return (NULL);
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
for (env = environment.data; *env; env = nxt + 1) {
|
2007-11-27 09:23:20 +00:00
|
|
|
char *val;
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
for (nxt = env; *nxt; ++nxt) {
|
2002-12-17 01:51:00 +00:00
|
|
|
if (nxt >= &environment.data[ENV_SIZE]) {
|
|
|
|
fprintf (stderr, "## Error: "
|
|
|
|
"environment not terminated\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
val = envmatch (name, env);
|
2002-12-17 01:51:00 +00:00
|
|
|
if (!val)
|
|
|
|
continue;
|
|
|
|
return (val);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print the current definition of one, or more, or all
|
|
|
|
* environment variables
|
|
|
|
*/
|
2008-05-07 03:16:15 +00:00
|
|
|
int fw_printenv (int argc, char *argv[])
|
2002-12-17 01:51:00 +00:00
|
|
|
{
|
2007-11-27 09:23:20 +00:00
|
|
|
char *env, *nxt;
|
2002-12-17 01:51:00 +00:00
|
|
|
int i, n_flag;
|
2008-05-07 03:16:15 +00:00
|
|
|
int rc = 0;
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if (env_init ())
|
2008-05-07 03:16:15 +00:00
|
|
|
return (-1);
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if (argc == 1) { /* Print all env variables */
|
|
|
|
for (env = environment.data; *env; env = nxt + 1) {
|
|
|
|
for (nxt = env; *nxt; ++nxt) {
|
2002-12-17 01:51:00 +00:00
|
|
|
if (nxt >= &environment.data[ENV_SIZE]) {
|
|
|
|
fprintf (stderr, "## Error: "
|
|
|
|
"environment not terminated\n");
|
2008-05-07 03:16:15 +00:00
|
|
|
return (-1);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
printf ("%s\n", env);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
2008-05-07 03:16:15 +00:00
|
|
|
return (0);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if (strcmp (argv[1], "-n") == 0) {
|
2002-12-17 01:51:00 +00:00
|
|
|
n_flag = 1;
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf (stderr, "## Error: "
|
|
|
|
"`-n' option requires exactly one argument\n");
|
2008-05-07 03:16:15 +00:00
|
|
|
return (-1);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n_flag = 0;
|
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
for (i = 1; i < argc; ++i) { /* print single env variables */
|
2007-11-27 09:23:20 +00:00
|
|
|
char *name = argv[i];
|
|
|
|
char *val = NULL;
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
for (env = environment.data; *env; env = nxt + 1) {
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
for (nxt = env; *nxt; ++nxt) {
|
2002-12-17 01:51:00 +00:00
|
|
|
if (nxt >= &environment.data[ENV_SIZE]) {
|
|
|
|
fprintf (stderr, "## Error: "
|
|
|
|
"environment not terminated\n");
|
2008-05-07 03:16:15 +00:00
|
|
|
return (-1);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
val = envmatch (name, env);
|
2002-12-17 01:51:00 +00:00
|
|
|
if (val) {
|
|
|
|
if (!n_flag) {
|
|
|
|
fputs (name, stdout);
|
2003-04-12 23:38:12 +00:00
|
|
|
putc ('=', stdout);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
puts (val);
|
2002-12-17 01:51:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-07 03:16:15 +00:00
|
|
|
if (!val) {
|
2003-04-12 23:38:12 +00:00
|
|
|
fprintf (stderr, "## Error: \"%s\" not defined\n", name);
|
2008-05-07 03:16:15 +00:00
|
|
|
rc = -1;
|
|
|
|
}
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
2008-05-07 03:16:15 +00:00
|
|
|
|
|
|
|
return (rc);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deletes or sets environment variables. Returns errno style error codes:
|
|
|
|
* 0 - OK
|
|
|
|
* EINVAL - need at least 1 argument
|
|
|
|
* EROFS - certain variables ("ethaddr", "serial#") cannot be
|
|
|
|
* modified or deleted
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int fw_setenv (int argc, char *argv[])
|
|
|
|
{
|
2003-04-12 23:38:12 +00:00
|
|
|
int i, len;
|
2007-11-27 09:23:20 +00:00
|
|
|
char *env, *nxt;
|
|
|
|
char *oldval = NULL;
|
|
|
|
char *name;
|
2002-12-17 01:51:00 +00:00
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if (env_init ())
|
2002-12-17 01:51:00 +00:00
|
|
|
return (errno);
|
|
|
|
|
|
|
|
name = argv[1];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* search if variable with this name already exists
|
|
|
|
*/
|
2003-04-12 23:38:12 +00:00
|
|
|
for (nxt = env = environment.data; *env; env = nxt + 1) {
|
|
|
|
for (nxt = env; *nxt; ++nxt) {
|
2002-12-17 01:51:00 +00:00
|
|
|
if (nxt >= &environment.data[ENV_SIZE]) {
|
|
|
|
fprintf (stderr, "## Error: "
|
|
|
|
"environment not terminated\n");
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
if ((oldval = envmatch (name, env)) != NULL)
|
2002-12-17 01:51:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete any existing definition
|
|
|
|
*/
|
|
|
|
if (oldval) {
|
|
|
|
/*
|
|
|
|
* Ethernet Address and serial# can be set only once
|
|
|
|
*/
|
|
|
|
if ((strcmp (name, "ethaddr") == 0) ||
|
2003-04-12 23:38:12 +00:00
|
|
|
(strcmp (name, "serial#") == 0)) {
|
2002-12-17 01:51:00 +00:00
|
|
|
fprintf (stderr, "Can't overwrite \"%s\"\n", name);
|
|
|
|
return (EROFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*++nxt == '\0') {
|
|
|
|
*env = '\0';
|
|
|
|
} else {
|
|
|
|
for (;;) {
|
|
|
|
*env = *nxt++;
|
|
|
|
if ((*env == '\0') && (*nxt == '\0'))
|
|
|
|
break;
|
|
|
|
++env;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*++env = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete only ? */
|
|
|
|
if (argc < 3)
|
|
|
|
goto WRITE_FLASH;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append new definition at the end
|
|
|
|
*/
|
2003-04-12 23:38:12 +00:00
|
|
|
for (env = environment.data; *env || *(env + 1); ++env);
|
2002-12-17 01:51:00 +00:00
|
|
|
if (env > environment.data)
|
|
|
|
++env;
|
|
|
|
/*
|
|
|
|
* Overflow when:
|
|
|
|
* "name" + "=" + "val" +"\0\0" > CFG_ENV_SIZE - (env-environment)
|
|
|
|
*/
|
2003-04-12 23:38:12 +00:00
|
|
|
len = strlen (name) + 2;
|
2002-12-17 01:51:00 +00:00
|
|
|
/* add '=' for first arg, ' ' for all others */
|
2003-04-12 23:38:12 +00:00
|
|
|
for (i = 2; i < argc; ++i) {
|
|
|
|
len += strlen (argv[i]) + 1;
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
if (len > (&environment.data[ENV_SIZE] - env)) {
|
2002-12-17 01:51:00 +00:00
|
|
|
fprintf (stderr,
|
|
|
|
"Error: environment overflow, \"%s\" deleted\n",
|
|
|
|
name);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
while ((*env = *name++) != '\0')
|
|
|
|
env++;
|
2003-04-12 23:38:12 +00:00
|
|
|
for (i = 2; i < argc; ++i) {
|
2007-11-27 09:23:20 +00:00
|
|
|
char *val = argv[i];
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
*env = (i == 2) ? '=' : ' ';
|
|
|
|
while ((*++env = *val++) != '\0');
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* end is marked with double '\0' */
|
|
|
|
*++env = '\0';
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
WRITE_FLASH:
|
2002-12-17 01:51:00 +00:00
|
|
|
|
|
|
|
/* Update CRC */
|
2007-11-27 09:23:20 +00:00
|
|
|
environment.crc = crc32 (0, (uint8_t*) environment.data, ENV_SIZE);
|
2002-12-17 01:51:00 +00:00
|
|
|
|
|
|
|
/* write environment back to flash */
|
|
|
|
if (flash_io (O_RDWR)) {
|
2003-04-12 23:38:12 +00:00
|
|
|
fprintf (stderr, "Error: can't write fw_env to flash\n");
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int flash_io (int mode)
|
|
|
|
{
|
|
|
|
int fd, fdr, rc, otherdev, len, resid;
|
|
|
|
erase_info_t erase;
|
2003-06-21 00:17:24 +00:00
|
|
|
char *data = NULL;
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if ((fd = open (DEVNAME (curdev), mode)) < 0) {
|
|
|
|
fprintf (stderr,
|
|
|
|
"Can't open %s: %s\n",
|
|
|
|
DEVNAME (curdev), strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
len = sizeof (environment.crc);
|
2003-01-11 09:48:40 +00:00
|
|
|
if (HaveRedundEnv) {
|
2003-04-12 23:38:12 +00:00
|
|
|
len += sizeof (environment.flags);
|
2003-01-11 09:48:40 +00:00
|
|
|
}
|
2002-12-17 01:51:00 +00:00
|
|
|
|
|
|
|
if (mode == O_RDWR) {
|
2003-01-11 09:48:40 +00:00
|
|
|
if (HaveRedundEnv) {
|
|
|
|
/* switch to next partition for writing */
|
|
|
|
otherdev = !curdev;
|
2003-04-12 23:38:12 +00:00
|
|
|
if ((fdr = open (DEVNAME (otherdev), mode)) < 0) {
|
|
|
|
fprintf (stderr,
|
|
|
|
"Can't open %s: %s\n",
|
|
|
|
DEVNAME (otherdev),
|
|
|
|
strerror (errno));
|
2003-01-11 09:48:40 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
otherdev = curdev;
|
|
|
|
fdr = fd;
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
printf ("Unlocking flash...\n");
|
|
|
|
erase.length = DEVESIZE (otherdev);
|
|
|
|
erase.start = DEVOFFSET (otherdev);
|
2002-12-17 01:51:00 +00:00
|
|
|
ioctl (fdr, MEMUNLOCK, &erase);
|
|
|
|
|
2003-01-11 09:48:40 +00:00
|
|
|
if (HaveRedundEnv) {
|
2003-04-12 23:38:12 +00:00
|
|
|
erase.length = DEVESIZE (curdev);
|
|
|
|
erase.start = DEVOFFSET (curdev);
|
2003-01-11 09:48:40 +00:00
|
|
|
ioctl (fd, MEMUNLOCK, &erase);
|
|
|
|
environment.flags = active_flag;
|
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
printf ("Done\n");
|
|
|
|
resid = DEVESIZE (otherdev) - CFG_ENV_SIZE;
|
2002-12-17 01:51:00 +00:00
|
|
|
if (resid) {
|
2003-04-12 23:38:12 +00:00
|
|
|
if ((data = malloc (resid)) == NULL) {
|
|
|
|
fprintf (stderr,
|
|
|
|
"Cannot malloc %d bytes: %s\n",
|
|
|
|
resid,
|
|
|
|
strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
if (lseek (fdr, DEVOFFSET (otherdev) + CFG_ENV_SIZE, SEEK_SET)
|
|
|
|
== -1) {
|
|
|
|
fprintf (stderr, "seek error on %s: %s\n",
|
|
|
|
DEVNAME (otherdev),
|
|
|
|
strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if ((rc = read (fdr, data, resid)) != resid) {
|
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"read error on %s: %s\n",
|
|
|
|
DEVNAME (otherdev),
|
|
|
|
strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
printf ("Erasing old environment...\n");
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
erase.length = DEVESIZE (otherdev);
|
|
|
|
erase.start = DEVOFFSET (otherdev);
|
2002-12-17 01:51:00 +00:00
|
|
|
if (ioctl (fdr, MEMERASE, &erase) != 0) {
|
|
|
|
fprintf (stderr, "MTD erase error on %s: %s\n",
|
2003-04-12 23:38:12 +00:00
|
|
|
DEVNAME (otherdev),
|
|
|
|
strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
printf ("Done\n");
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
printf ("Writing environment to %s...\n", DEVNAME (otherdev));
|
|
|
|
if (lseek (fdr, DEVOFFSET (otherdev), SEEK_SET) == -1) {
|
2003-01-11 09:48:40 +00:00
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"seek error on %s: %s\n",
|
|
|
|
DEVNAME (otherdev), strerror (errno));
|
2003-01-11 09:48:40 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
if (write (fdr, &environment, len) != len) {
|
2002-12-17 01:51:00 +00:00
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"CRC write error on %s: %s\n",
|
|
|
|
DEVNAME (otherdev), strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
if (write (fdr, environment.data, ENV_SIZE) != ENV_SIZE) {
|
2002-12-17 01:51:00 +00:00
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"Write error on %s: %s\n",
|
|
|
|
DEVNAME (otherdev), strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (resid) {
|
|
|
|
if (write (fdr, data, resid) != resid) {
|
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"write error on %s: %s\n",
|
|
|
|
DEVNAME (curdev), strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
free (data);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
2003-01-11 09:48:40 +00:00
|
|
|
if (HaveRedundEnv) {
|
|
|
|
/* change flag on current active env partition */
|
2003-04-12 23:38:12 +00:00
|
|
|
if (lseek (fd, DEVOFFSET (curdev) + sizeof (ulong), SEEK_SET)
|
|
|
|
== -1) {
|
|
|
|
fprintf (stderr, "seek error on %s: %s\n",
|
|
|
|
DEVNAME (curdev), strerror (errno));
|
2003-01-11 09:48:40 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
if (write (fd, &obsolete_flag, sizeof (obsolete_flag)) !=
|
|
|
|
sizeof (obsolete_flag)) {
|
2003-01-11 09:48:40 +00:00
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"Write error on %s: %s\n",
|
|
|
|
DEVNAME (curdev), strerror (errno));
|
2003-01-11 09:48:40 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
printf ("Done\n");
|
|
|
|
printf ("Locking ...\n");
|
|
|
|
erase.length = DEVESIZE (otherdev);
|
|
|
|
erase.start = DEVOFFSET (otherdev);
|
2002-12-17 01:51:00 +00:00
|
|
|
ioctl (fdr, MEMLOCK, &erase);
|
2003-01-11 09:48:40 +00:00
|
|
|
if (HaveRedundEnv) {
|
2003-04-12 23:38:12 +00:00
|
|
|
erase.length = DEVESIZE (curdev);
|
|
|
|
erase.start = DEVOFFSET (curdev);
|
2003-01-11 09:48:40 +00:00
|
|
|
ioctl (fd, MEMLOCK, &erase);
|
2003-04-12 23:38:12 +00:00
|
|
|
if (close (fdr)) {
|
2003-01-11 09:48:40 +00:00
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"I/O error on %s: %s\n",
|
|
|
|
DEVNAME (otherdev),
|
|
|
|
strerror (errno));
|
2003-01-11 09:48:40 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
printf ("Done\n");
|
2002-12-17 01:51:00 +00:00
|
|
|
} else {
|
2003-01-11 09:48:40 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if (lseek (fd, DEVOFFSET (curdev), SEEK_SET) == -1) {
|
2003-01-11 09:48:40 +00:00
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"seek error on %s: %s\n",
|
|
|
|
DEVNAME (curdev), strerror (errno));
|
2003-01-11 09:48:40 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2002-12-17 01:51:00 +00:00
|
|
|
if (read (fd, &environment, len) != len) {
|
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"CRC read error on %s: %s\n",
|
|
|
|
DEVNAME (curdev), strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if ((rc = read (fd, environment.data, ENV_SIZE)) != ENV_SIZE) {
|
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"Read error on %s: %s\n",
|
|
|
|
DEVNAME (curdev), strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if (close (fd)) {
|
2002-12-17 01:51:00 +00:00
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"I/O error on %s: %s\n",
|
|
|
|
DEVNAME (curdev), strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* everything ok */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-11-27 09:23:20 +00:00
|
|
|
static char *envmatch (char * s1, char * s2)
|
2002-12-17 01:51:00 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
while (*s1 == *s2++)
|
|
|
|
if (*s1++ == '=')
|
2003-04-12 23:38:12 +00:00
|
|
|
return (s2);
|
|
|
|
if (*s1 == '\0' && *(s2 - 1) == '=')
|
|
|
|
return (s2);
|
|
|
|
return (NULL);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prevent confusion if running from erased flash memory
|
|
|
|
*/
|
2003-04-12 23:38:12 +00:00
|
|
|
static int env_init (void)
|
2002-12-17 01:51:00 +00:00
|
|
|
{
|
|
|
|
int crc1, crc1_ok;
|
2007-11-27 09:23:20 +00:00
|
|
|
char *addr1;
|
2003-01-11 09:48:40 +00:00
|
|
|
|
2002-12-17 01:51:00 +00:00
|
|
|
int crc2, crc2_ok;
|
2007-11-27 09:23:20 +00:00
|
|
|
char flag1, flag2, *addr2;
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if (parse_config ()) /* should fill envdevices */
|
2003-01-11 09:48:40 +00:00
|
|
|
return 1;
|
2003-04-12 23:38:12 +00:00
|
|
|
|
2003-01-11 09:48:40 +00:00
|
|
|
if ((addr1 = calloc (1, ENV_SIZE)) == NULL) {
|
2003-04-12 23:38:12 +00:00
|
|
|
fprintf (stderr,
|
|
|
|
"Not enough memory for environment (%ld bytes)\n",
|
|
|
|
ENV_SIZE);
|
2003-01-11 09:48:40 +00:00
|
|
|
return (errno);
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
|
2003-01-11 09:48:40 +00:00
|
|
|
/* read environment from FLASH to local buffer */
|
|
|
|
environment.data = addr1;
|
|
|
|
curdev = 0;
|
|
|
|
if (flash_io (O_RDONLY)) {
|
|
|
|
return (errno);
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
|
2007-11-27 09:23:20 +00:00
|
|
|
crc1_ok = ((crc1 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE))
|
2003-01-11 09:48:40 +00:00
|
|
|
== environment.crc);
|
|
|
|
if (!HaveRedundEnv) {
|
2002-12-17 01:51:00 +00:00
|
|
|
if (!crc1_ok) {
|
2003-04-12 23:38:12 +00:00
|
|
|
fprintf (stderr,
|
|
|
|
"Warning: Bad CRC, using default environment\n");
|
2006-03-12 22:27:46 +00:00
|
|
|
memcpy(environment.data, default_environment, sizeof default_environment);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
2003-01-11 09:48:40 +00:00
|
|
|
} else {
|
2002-12-17 01:51:00 +00:00
|
|
|
flag1 = environment.flags;
|
2003-04-12 23:38:12 +00:00
|
|
|
|
2002-12-17 01:51:00 +00:00
|
|
|
curdev = 1;
|
|
|
|
if ((addr2 = calloc (1, ENV_SIZE)) == NULL) {
|
2003-04-12 23:38:12 +00:00
|
|
|
fprintf (stderr,
|
|
|
|
"Not enough memory for environment (%ld bytes)\n",
|
|
|
|
ENV_SIZE);
|
2002-12-17 01:51:00 +00:00
|
|
|
return (errno);
|
2003-04-12 23:38:12 +00:00
|
|
|
}
|
2002-12-17 01:51:00 +00:00
|
|
|
environment.data = addr2;
|
2003-04-12 23:38:12 +00:00
|
|
|
|
2002-12-17 01:51:00 +00:00
|
|
|
if (flash_io (O_RDONLY)) {
|
|
|
|
return (errno);
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
|
2007-11-27 09:23:20 +00:00
|
|
|
crc2_ok = ((crc2 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE))
|
2002-12-17 01:51:00 +00:00
|
|
|
== environment.crc);
|
|
|
|
flag2 = environment.flags;
|
2003-04-12 23:38:12 +00:00
|
|
|
|
|
|
|
if (crc1_ok && !crc2_ok) {
|
|
|
|
environment.data = addr1;
|
2002-12-17 01:51:00 +00:00
|
|
|
environment.flags = flag1;
|
|
|
|
environment.crc = crc1;
|
|
|
|
curdev = 0;
|
2003-04-12 23:38:12 +00:00
|
|
|
free (addr2);
|
|
|
|
} else if (!crc1_ok && crc2_ok) {
|
|
|
|
environment.data = addr2;
|
2002-12-17 01:51:00 +00:00
|
|
|
environment.flags = flag2;
|
|
|
|
environment.crc = crc2;
|
|
|
|
curdev = 1;
|
2003-04-12 23:38:12 +00:00
|
|
|
free (addr1);
|
|
|
|
} else if (!crc1_ok && !crc2_ok) {
|
|
|
|
fprintf (stderr,
|
|
|
|
"Warning: Bad CRC, using default environment\n");
|
2006-03-12 22:27:46 +00:00
|
|
|
memcpy(environment.data, default_environment, sizeof default_environment);
|
2002-12-17 01:51:00 +00:00
|
|
|
curdev = 0;
|
2003-04-12 23:38:12 +00:00
|
|
|
free (addr1);
|
|
|
|
} else if (flag1 == active_flag && flag2 == obsolete_flag) {
|
|
|
|
environment.data = addr1;
|
2002-12-17 01:51:00 +00:00
|
|
|
environment.flags = flag1;
|
|
|
|
environment.crc = crc1;
|
|
|
|
curdev = 0;
|
2003-04-12 23:38:12 +00:00
|
|
|
free (addr2);
|
|
|
|
} else if (flag1 == obsolete_flag && flag2 == active_flag) {
|
|
|
|
environment.data = addr2;
|
2002-12-17 01:51:00 +00:00
|
|
|
environment.flags = flag2;
|
|
|
|
environment.crc = crc2;
|
|
|
|
curdev = 1;
|
2003-04-12 23:38:12 +00:00
|
|
|
free (addr1);
|
|
|
|
} else if (flag1 == flag2) {
|
|
|
|
environment.data = addr1;
|
2002-12-17 01:51:00 +00:00
|
|
|
environment.flags = flag1;
|
|
|
|
environment.crc = crc1;
|
|
|
|
curdev = 0;
|
2003-04-12 23:38:12 +00:00
|
|
|
free (addr2);
|
|
|
|
} else if (flag1 == 0xFF) {
|
|
|
|
environment.data = addr1;
|
2002-12-17 01:51:00 +00:00
|
|
|
environment.flags = flag1;
|
|
|
|
environment.crc = crc1;
|
|
|
|
curdev = 0;
|
2003-04-12 23:38:12 +00:00
|
|
|
free (addr2);
|
|
|
|
} else if (flag2 == 0xFF) {
|
|
|
|
environment.data = addr2;
|
2002-12-17 01:51:00 +00:00
|
|
|
environment.flags = flag2;
|
|
|
|
environment.crc = crc2;
|
|
|
|
curdev = 1;
|
2003-04-12 23:38:12 +00:00
|
|
|
free (addr1);
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
static int parse_config ()
|
2002-12-17 01:51:00 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2003-01-11 09:48:40 +00:00
|
|
|
#if defined(CONFIG_FILE)
|
|
|
|
/* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
|
2003-04-12 23:38:12 +00:00
|
|
|
if (get_config (CONFIG_FILE)) {
|
2003-01-11 09:48:40 +00:00
|
|
|
fprintf (stderr,
|
2003-04-12 23:38:12 +00:00
|
|
|
"Cannot parse config file: %s\n", strerror (errno));
|
2002-12-17 01:51:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2003-01-11 09:48:40 +00:00
|
|
|
#else
|
2003-04-12 23:38:12 +00:00
|
|
|
strcpy (DEVNAME (0), DEVICE1_NAME);
|
|
|
|
DEVOFFSET (0) = DEVICE1_OFFSET;
|
|
|
|
ENVSIZE (0) = ENV1_SIZE;
|
|
|
|
DEVESIZE (0) = DEVICE1_ESIZE;
|
2002-12-17 01:51:00 +00:00
|
|
|
#ifdef HAVE_REDUND
|
2003-04-12 23:38:12 +00:00
|
|
|
strcpy (DEVNAME (1), DEVICE2_NAME);
|
|
|
|
DEVOFFSET (1) = DEVICE2_OFFSET;
|
|
|
|
ENVSIZE (1) = ENV2_SIZE;
|
|
|
|
DEVESIZE (1) = DEVICE2_ESIZE;
|
2003-01-11 09:48:40 +00:00
|
|
|
HaveRedundEnv = 1;
|
2002-12-17 01:51:00 +00:00
|
|
|
#endif
|
2003-01-11 09:48:40 +00:00
|
|
|
#endif
|
2003-04-12 23:38:12 +00:00
|
|
|
if (stat (DEVNAME (0), &st)) {
|
|
|
|
fprintf (stderr,
|
|
|
|
"Cannot access MTD device %s: %s\n",
|
|
|
|
DEVNAME (0), strerror (errno));
|
2003-01-11 09:48:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
|
|
|
|
if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
|
|
|
|
fprintf (stderr,
|
|
|
|
"Cannot access MTD device %s: %s\n",
|
2005-09-24 23:20:38 +00:00
|
|
|
DEVNAME (1), strerror (errno));
|
2003-01-11 09:48:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-12-17 01:51:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-01-11 09:48:40 +00:00
|
|
|
|
|
|
|
#if defined(CONFIG_FILE)
|
|
|
|
static int get_config (char *fname)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int i = 0;
|
|
|
|
int rc;
|
|
|
|
char dump[128];
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
if ((fp = fopen (fname, "r")) == NULL) {
|
2003-01-11 09:48:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-04-12 23:38:12 +00:00
|
|
|
while ((i < 2) && ((rc = fscanf (fp, "%s %lx %lx %lx",
|
|
|
|
DEVNAME (i),
|
|
|
|
&DEVOFFSET (i),
|
|
|
|
&ENVSIZE (i),
|
|
|
|
&DEVESIZE (i) )) != EOF)) {
|
2003-01-11 09:48:40 +00:00
|
|
|
|
|
|
|
/* Skip incomplete conversions and comment strings */
|
2003-04-12 23:38:12 +00:00
|
|
|
if ((rc < 3) || (*DEVNAME (i) == '#')) {
|
|
|
|
fgets (dump, sizeof (dump), fp); /* Consume till end */
|
2003-01-11 09:48:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2003-04-12 23:38:12 +00:00
|
|
|
fclose (fp);
|
|
|
|
|
2003-01-11 09:48:40 +00:00
|
|
|
HaveRedundEnv = i - 1;
|
2003-04-12 23:38:12 +00:00
|
|
|
if (!i) { /* No valid entries found */
|
2003-01-11 09:48:40 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|