]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/env_nvram.c
Merge branch 'master' of git://www.denx.de/git/u-boot-cfi-flash
[people/ms/u-boot.git] / common / env_nvram.c
CommitLineData
c609719b 1/*
ea882baf 2 * (C) Copyright 2000-2010
c609719b
WD
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/*
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
6d0f6bcf 38 * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
c609719b
WD
39 * 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>
c609719b
WD
44#include <command.h>
45#include <environment.h>
c609719b 46#include <linux/stddef.h>
ea882baf
WD
47#include <search.h>
48#include <errno.h>
c609719b 49
957a0e69
JCPV
50DECLARE_GLOBAL_DATA_PTR;
51
6d0f6bcf 52#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
c609719b
WD
53extern void *nvram_read(void *dest, const long src, size_t count);
54extern void nvram_write(long dest, const void *src, size_t count);
91494ca6 55env_t *env_ptr;
c609719b 56#else
0e8d1586 57env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
c609719b
WD
58#endif
59
91494ca6 60char *env_name_spec = "NVRAM";
c609719b 61
60d7d5a6
RH
62#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
63static char env_buf[CONFIG_ENV_SIZE];
64#endif
65
bf95df44 66#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
ea882baf 67uchar env_get_char_spec(int index)
c609719b 68{
c609719b
WD
69 uchar c;
70
91494ca6 71 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
c609719b
WD
72
73 return c;
c609719b 74}
bf95df44 75#endif
c609719b 76
ea882baf 77void env_relocate_spec(void)
c609719b 78{
60d7d5a6 79 char *buf;
ea882baf 80
6d0f6bcf 81#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
60d7d5a6 82 buf = env_buf;
ea882baf 83 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
c609719b 84#else
60d7d5a6 85 buf = (void *)CONFIG_ENV_ADDR;
c609719b 86#endif
ea882baf 87 env_import(buf, 1);
c609719b
WD
88}
89
ea882baf 90int saveenv(void)
c609719b 91{
60d7d5a6
RH
92#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
93 env_t *env_new = (env_t *)env_buf;
94#else
95 env_t *env_new = (env_t *)CONFIG_ENV_ADDR;
96#endif
ea882baf
WD
97 ssize_t len;
98 char *res;
99 int rcode = 0;
100
60d7d5a6 101 res = (char *)env_new->data;
be11235a 102 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
ea882baf
WD
103 if (len < 0) {
104 error("Cannot export environment: errno = %d\n", errno);
105 return 1;
106 }
60d7d5a6 107 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
ea882baf 108
6d0f6bcf 109#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
60d7d5a6 110 nvram_write(CONFIG_ENV_ADDR, env_new, CONFIG_ENV_SIZE);
c609719b
WD
111#endif
112 return rcode;
113}
114
ea882baf 115/*
c609719b
WD
116 * Initialize Environment use
117 *
118 * We are still running from ROM, so data use is limited
119 */
ea882baf 120int env_init(void)
c609719b 121{
6d0f6bcf 122#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
c609719b 123 ulong crc;
60d7d5a6 124 uchar *data = env_buf;
ea882baf
WD
125
126 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
91494ca6 127 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
c609719b
WD
128
129 if (crc32(0, data, ENV_SIZE) == crc) {
91494ca6 130 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
c609719b
WD
131#else
132 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
91494ca6 133 gd->env_addr = (ulong)&env_ptr->data;
c609719b 134#endif
91494ca6 135 gd->env_valid = 1;
c609719b 136 } else {
91494ca6
IG
137 gd->env_addr = (ulong)&default_environment[0];
138 gd->env_valid = 0;
c609719b 139 }
91494ca6
IG
140
141 return 0;
c609719b 142}