uboot/common/env_common.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2000-2002
   3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   4 *
   5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
   6 * Andreas Heppel <aheppel@sysgo.de>
   7
   8 * See file CREDITS for list of people who contributed to this
   9 * project.
  10 *
  11 * This program is free software; you can redistribute it and/or
  12 * modify it under the terms of the GNU General Public License as
  13 * published by the Free Software Foundation; either version 2 of
  14 * the License, or (at your option) any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 * GNU General Public License for more details.
  20 *
  21 * You should have received a copy of the GNU General Public License
  22 * along with this program; if not, write to the Free Software
  23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24 * MA 02111-1307 USA
  25 */
  26
  27#include <common.h>
  28#include <command.h>
  29#include <environment.h>
  30#include <linux/stddef.h>
  31#include <malloc.h>
  32
  33DECLARE_GLOBAL_DATA_PTR;
  34
  35#ifdef CONFIG_AMIGAONEG3SE
  36        extern void enable_nvram(void);
  37        extern void disable_nvram(void);
  38#endif
  39
  40#undef DEBUG_ENV
  41#ifdef DEBUG_ENV
  42#define DEBUGF(fmt,args...) printf(fmt ,##args)
  43#else
  44#define DEBUGF(fmt,args...)
  45#endif
  46
  47extern env_t *env_ptr;
  48
  49extern void env_relocate_spec (void);
  50extern uchar env_get_char_spec(int);
  51
  52static uchar env_get_char_init (int index);
  53
  54/************************************************************************
  55 * Default settings to be used when no valid environment is found
  56 */
  57#define XMK_STR(x)      #x
  58#define MK_STR(x)       XMK_STR(x)
  59
  60uchar default_environment[] = {
  61#ifdef  CONFIG_BOOTARGS
  62        "bootargs="     CONFIG_BOOTARGS                 "\0"
  63#endif
  64#ifdef  CONFIG_BOOTCOMMAND
  65        "bootcmd="      CONFIG_BOOTCOMMAND              "\0"
  66#endif
  67#ifdef  CONFIG_RAMBOOTCOMMAND
  68        "ramboot="      CONFIG_RAMBOOTCOMMAND           "\0"
  69#endif
  70#ifdef  CONFIG_NFSBOOTCOMMAND
  71        "nfsboot="      CONFIG_NFSBOOTCOMMAND           "\0"
  72#endif
  73#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  74        "bootdelay="    MK_STR(CONFIG_BOOTDELAY)        "\0"
  75#endif
  76#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
  77        "baudrate="     MK_STR(CONFIG_BAUDRATE)         "\0"
  78#endif
  79#ifdef  CONFIG_LOADS_ECHO
  80        "loads_echo="   MK_STR(CONFIG_LOADS_ECHO)       "\0"
  81#endif
  82#ifdef  CONFIG_ETHADDR
  83        "ethaddr="      MK_STR(CONFIG_ETHADDR)          "\0"
  84#endif
  85#ifdef  CONFIG_ETH1ADDR
  86        "eth1addr="     MK_STR(CONFIG_ETH1ADDR)         "\0"
  87#endif
  88#ifdef  CONFIG_ETH2ADDR
  89        "eth2addr="     MK_STR(CONFIG_ETH2ADDR)         "\0"
  90#endif
  91#ifdef  CONFIG_ETH3ADDR
  92        "eth3addr="     MK_STR(CONFIG_ETH3ADDR)         "\0"
  93#endif
  94#ifdef  CONFIG_ETH4ADDR
  95        "eth4addr="     MK_STR(CONFIG_ETH4ADDR)         "\0"
  96#endif
  97#ifdef  CONFIG_ETH5ADDR
  98        "eth5addr="     MK_STR(CONFIG_ETH5ADDR)         "\0"
  99#endif
 100#ifdef  CONFIG_IPADDR
 101        "ipaddr="       MK_STR(CONFIG_IPADDR)           "\0"
 102#endif
 103#ifdef  CONFIG_SERVERIP
 104        "serverip="     MK_STR(CONFIG_SERVERIP)         "\0"
 105#endif
 106#ifdef  CONFIG_SYS_AUTOLOAD
 107        "autoload="     CONFIG_SYS_AUTOLOAD                     "\0"
 108#endif
 109#ifdef  CONFIG_PREBOOT
 110        "preboot="      CONFIG_PREBOOT                  "\0"
 111#endif
 112#ifdef  CONFIG_ROOTPATH
 113        "rootpath="     MK_STR(CONFIG_ROOTPATH)         "\0"
 114#endif
 115#ifdef  CONFIG_GATEWAYIP
 116        "gatewayip="    MK_STR(CONFIG_GATEWAYIP)        "\0"
 117#endif
 118#ifdef  CONFIG_NETMASK
 119        "netmask="      MK_STR(CONFIG_NETMASK)          "\0"
 120#endif
 121#ifdef  CONFIG_HOSTNAME
 122        "hostname="     MK_STR(CONFIG_HOSTNAME)         "\0"
 123#endif
 124#ifdef  CONFIG_BOOTFILE
 125        "bootfile="     MK_STR(CONFIG_BOOTFILE)         "\0"
 126#endif
 127#ifdef  CONFIG_LOADADDR
 128        "loadaddr="     MK_STR(CONFIG_LOADADDR)         "\0"
 129#endif
 130#ifdef  CONFIG_CLOCKS_IN_MHZ
 131        "clocks_in_mhz=1\0"
 132#endif
 133#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
 134        "pcidelay="     MK_STR(CONFIG_PCI_BOOTDELAY)    "\0"
 135#endif
 136#ifdef  CONFIG_EXTRA_ENV_SETTINGS
 137        CONFIG_EXTRA_ENV_SETTINGS
 138#endif
 139        "\0"
 140};
 141
 142void env_crc_update (void)
 143{
 144        env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
 145}
 146
 147static uchar env_get_char_init (int index)
 148{
 149        uchar c;
 150
 151        /* if crc was bad, use the default environment */
 152        if (gd->env_valid)
 153        {
 154                c = env_get_char_spec(index);
 155        } else {
 156                c = default_environment[index];
 157        }
 158
 159        return (c);
 160}
 161
 162#ifdef CONFIG_AMIGAONEG3SE
 163uchar env_get_char_memory (int index)
 164{
 165        uchar retval;
 166        enable_nvram();
 167        if (gd->env_valid) {
 168                retval = ( *((uchar *)(gd->env_addr + index)) );
 169        } else {
 170                retval = ( default_environment[index] );
 171        }
 172        disable_nvram();
 173        return retval;
 174}
 175#else
 176uchar env_get_char_memory (int index)
 177{
 178        if (gd->env_valid) {
 179                return ( *((uchar *)(gd->env_addr + index)) );
 180        } else {
 181                return ( default_environment[index] );
 182        }
 183}
 184#endif
 185
 186uchar env_get_char (int index)
 187{
 188        uchar c;
 189
 190        /* if relocated to RAM */
 191        if (gd->flags & GD_FLG_RELOC)
 192                c = env_get_char_memory(index);
 193        else
 194                c = env_get_char_init(index);
 195
 196        return (c);
 197}
 198
 199uchar *env_get_addr (int index)
 200{
 201        if (gd->env_valid) {
 202                return ( ((uchar *)(gd->env_addr + index)) );
 203        } else {
 204                return (&default_environment[index]);
 205        }
 206}
 207
 208void set_default_env(void)
 209{
 210        if (sizeof(default_environment) > ENV_SIZE) {
 211                puts ("*** Error - default environment is too large\n\n");
 212                return;
 213        }
 214
 215        memset(env_ptr, 0, sizeof(env_t));
 216        memcpy(env_ptr->data, default_environment,
 217               sizeof(default_environment));
 218#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
 219        env_ptr->flags = 0xFF;
 220#endif
 221        env_crc_update ();
 222        gd->env_valid = 1;
 223}
 224
 225void env_relocate (void)
 226{
 227#ifndef CONFIG_RELOC_FIXUP_WORKS
 228        DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
 229                gd->reloc_off);
 230#endif
 231
 232#ifdef CONFIG_AMIGAONEG3SE
 233        enable_nvram();
 234#endif
 235
 236#ifdef ENV_IS_EMBEDDED
 237        /*
 238         * The environment buffer is embedded with the text segment,
 239         * just relocate the environment pointer
 240         */
 241#ifndef CONFIG_RELOC_FIXUP_WORKS
 242        env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
 243#endif
 244        DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
 245#else
 246        /*
 247         * We must allocate a buffer for the environment
 248         */
 249        env_ptr = (env_t *)malloc (CONFIG_ENV_SIZE);
 250        DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
 251#endif
 252
 253        if (gd->env_valid == 0) {
 254#if defined(CONFIG_GTH) || defined(CONFIG_ENV_IS_NOWHERE)       /* Environment not changable */
 255                puts ("Using default environment\n\n");
 256#else
 257                puts ("*** Warning - bad CRC, using default environment\n\n");
 258                show_boot_progress (-60);
 259#endif
 260                set_default_env();
 261        }
 262        else {
 263                env_relocate_spec ();
 264        }
 265        gd->env_addr = (ulong)&(env_ptr->data);
 266
 267#ifdef CONFIG_AMIGAONEG3SE
 268        disable_nvram();
 269#endif
 270}
 271
 272#ifdef CONFIG_AUTO_COMPLETE
 273int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
 274{
 275        int i, nxt, len, vallen, found;
 276        const char *lval, *rval;
 277
 278        found = 0;
 279        cmdv[0] = NULL;
 280
 281        len = strlen(var);
 282        /* now iterate over the variables and select those that match */
 283        for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
 284
 285                for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
 286                        ;
 287
 288                lval = (char *)env_get_addr(i);
 289                rval = strchr(lval, '=');
 290                if (rval != NULL) {
 291                        vallen = rval - lval;
 292                        rval++;
 293                } else
 294                        vallen = strlen(lval);
 295
 296                if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
 297                        continue;
 298
 299                if (found >= maxv - 2 || bufsz < vallen + 1) {
 300                        cmdv[found++] = "...";
 301                        break;
 302                }
 303                cmdv[found++] = buf;
 304                memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
 305                *buf++ = '\0'; bufsz--;
 306        }
 307
 308        cmdv[found] = NULL;
 309        return found;
 310}
 311#endif
 312