blob: 726eaac55efd3ceb1ee341d6f1efbbe9dcd41540 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenkc6097192002-11-03 00:24:07 +00003 * 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/*
28 * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
29 *
30 * It might not be possible in all cases to use 'memcpy()' to copy
31 * the environment to NVRAM, as the NVRAM might not be mapped into
32 * the memory space. (I.e. this is the case for the BAB750). In those
33 * cases it might be possible to access the NVRAM using a different
34 * method. For example, the RTC on the BAB750 is accessible in IO
35 * space using its address and data registers. To enable usage of
36 * NVRAM in those cases I invented the functions 'nvram_read()' and
37 * 'nvram_write()', which will be activated upon the configuration
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038 * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
wdenkc6097192002-11-03 00:24:07 +000039 * strongly dependent on the used HW, and must be redefined for each
40 * board that wants to use them.
41 */
42
43#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000044#include <command.h>
45#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000046#include <linux/stddef.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020047#include <search.h>
48#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000049
Jean-Christophe PLAGNIOL-VILLARD957a0e62008-09-10 22:47:59 +020050DECLARE_GLOBAL_DATA_PTR;
51
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenkc6097192002-11-03 00:24:07 +000053extern void *nvram_read(void *dest, const long src, size_t count);
54extern void nvram_write(long dest, const void *src, size_t count);
Igor Grinberg91494ca2011-11-07 01:14:04 +000055env_t *env_ptr;
wdenkc6097192002-11-03 00:24:07 +000056#else
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020057env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000058#endif
59
Igor Grinberg91494ca2011-11-07 01:14:04 +000060char *env_name_spec = "NVRAM";
wdenkc6097192002-11-03 00:24:07 +000061
Wolfgang Denkea882ba2010-06-20 23:33:59 +020062uchar env_get_char_spec(int index)
wdenkc6097192002-11-03 00:24:07 +000063{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020064#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenkc6097192002-11-03 00:24:07 +000065 uchar c;
66
Igor Grinberg91494ca2011-11-07 01:14:04 +000067 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
wdenkc6097192002-11-03 00:24:07 +000068
69 return c;
70#else
wdenkc6097192002-11-03 00:24:07 +000071 return *((uchar *)(gd->env_addr + index));
72#endif
73}
74
Wolfgang Denkea882ba2010-06-20 23:33:59 +020075void env_relocate_spec(void)
wdenkc6097192002-11-03 00:24:07 +000076{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020077 char buf[CONFIG_ENV_SIZE];
78
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020079#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +020080 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000081#else
Igor Grinberg91494ca2011-11-07 01:14:04 +000082 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000083#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020084 env_import(buf, 1);
wdenkc6097192002-11-03 00:24:07 +000085}
86
Wolfgang Denkea882ba2010-06-20 23:33:59 +020087int saveenv(void)
wdenkc6097192002-11-03 00:24:07 +000088{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020089 env_t env_new;
90 ssize_t len;
91 char *res;
92 int rcode = 0;
93
94 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +010095 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020096 if (len < 0) {
97 error("Cannot export environment: errno = %d\n", errno);
98 return 1;
99 }
100 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
101
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200102#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200103 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000104#else
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200105 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
106 rcode = 1;
wdenkc6097192002-11-03 00:24:07 +0000107#endif
108 return rcode;
109}
110
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200111/*
wdenkc6097192002-11-03 00:24:07 +0000112 * Initialize Environment use
113 *
114 * We are still running from ROM, so data use is limited
115 */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200116int env_init(void)
wdenkc6097192002-11-03 00:24:07 +0000117{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200118#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
wdenkc6097192002-11-03 00:24:07 +0000119 ulong crc;
120 uchar data[ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200121
122 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
Igor Grinberg91494ca2011-11-07 01:14:04 +0000123 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000124
125 if (crc32(0, data, ENV_SIZE) == crc) {
Igor Grinberg91494ca2011-11-07 01:14:04 +0000126 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
wdenkc6097192002-11-03 00:24:07 +0000127#else
128 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg91494ca2011-11-07 01:14:04 +0000129 gd->env_addr = (ulong)&env_ptr->data;
wdenkc6097192002-11-03 00:24:07 +0000130#endif
Igor Grinberg91494ca2011-11-07 01:14:04 +0000131 gd->env_valid = 1;
wdenkc6097192002-11-03 00:24:07 +0000132 } else {
Igor Grinberg91494ca2011-11-07 01:14:04 +0000133 gd->env_addr = (ulong)&default_environment[0];
134 gd->env_valid = 0;
wdenkc6097192002-11-03 00:24:07 +0000135 }
Igor Grinberg91494ca2011-11-07 01:14:04 +0000136
137 return 0;
wdenkc6097192002-11-03 00:24:07 +0000138}