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