]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/env_nvram.c
ARM: omap4-panda: Add MAC address creation for panda
[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
1a459660 8 * SPDX-License-Identifier: GPL-2.0+
c609719b
WD
9 */
10
11/*
12 * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
13 *
14 * It might not be possible in all cases to use 'memcpy()' to copy
15 * the environment to NVRAM, as the NVRAM might not be mapped into
16 * the memory space. (I.e. this is the case for the BAB750). In those
17 * cases it might be possible to access the NVRAM using a different
18 * method. For example, the RTC on the BAB750 is accessible in IO
19 * space using its address and data registers. To enable usage of
20 * NVRAM in those cases I invented the functions 'nvram_read()' and
21 * 'nvram_write()', which will be activated upon the configuration
6d0f6bcf 22 * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
c609719b
WD
23 * strongly dependent on the used HW, and must be redefined for each
24 * board that wants to use them.
25 */
26
27#include <common.h>
c609719b
WD
28#include <command.h>
29#include <environment.h>
c609719b 30#include <linux/stddef.h>
ea882baf
WD
31#include <search.h>
32#include <errno.h>
c609719b 33
957a0e69
JCPV
34DECLARE_GLOBAL_DATA_PTR;
35
6d0f6bcf 36#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
c609719b
WD
37extern void *nvram_read(void *dest, const long src, size_t count);
38extern void nvram_write(long dest, const void *src, size_t count);
91494ca6 39env_t *env_ptr;
c609719b 40#else
0e8d1586 41env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
c609719b
WD
42#endif
43
91494ca6 44char *env_name_spec = "NVRAM";
c609719b 45
bf95df44 46#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
ea882baf 47uchar env_get_char_spec(int index)
c609719b 48{
c609719b
WD
49 uchar c;
50
91494ca6 51 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
c609719b
WD
52
53 return c;
c609719b 54}
bf95df44 55#endif
c609719b 56
ea882baf 57void env_relocate_spec(void)
c609719b 58{
cd0f4fa1 59 char buf[CONFIG_ENV_SIZE];
ea882baf 60
6d0f6bcf 61#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
ea882baf 62 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
c609719b 63#else
cd0f4fa1 64 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
c609719b 65#endif
ea882baf 66 env_import(buf, 1);
c609719b
WD
67}
68
ea882baf 69int saveenv(void)
c609719b 70{
cd0f4fa1 71 env_t env_new;
ea882baf
WD
72 ssize_t len;
73 char *res;
74 int rcode = 0;
75
cd0f4fa1 76 res = (char *)&env_new.data;
be11235a 77 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
ea882baf
WD
78 if (len < 0) {
79 error("Cannot export environment: errno = %d\n", errno);
80 return 1;
81 }
cd0f4fa1 82 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
ea882baf 83
6d0f6bcf 84#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
cd0f4fa1
TR
85 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
86#else
87 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
88 rcode = 1;
c609719b
WD
89#endif
90 return rcode;
91}
92
ea882baf 93/*
c609719b
WD
94 * Initialize Environment use
95 *
96 * We are still running from ROM, so data use is limited
97 */
ea882baf 98int env_init(void)
c609719b 99{
6d0f6bcf 100#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
c609719b 101 ulong crc;
cd0f4fa1 102 uchar data[ENV_SIZE];
ea882baf
WD
103
104 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
91494ca6 105 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
c609719b
WD
106
107 if (crc32(0, data, ENV_SIZE) == crc) {
91494ca6 108 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
c609719b
WD
109#else
110 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
91494ca6 111 gd->env_addr = (ulong)&env_ptr->data;
c609719b 112#endif
91494ca6 113 gd->env_valid = 1;
c609719b 114 } else {
91494ca6
IG
115 gd->env_addr = (ulong)&default_environment[0];
116 gd->env_valid = 0;
c609719b 117 }
91494ca6
IG
118
119 return 0;
c609719b 120}