]> git.ipfire.org Git - people/ms/u-boot.git/blame - env/nvram.c
arc: Fix final linkage with Elf32 tools
[people/ms/u-boot.git] / 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
3765b3e7 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);
c609719b 39#else
0e8d1586 40env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
c609719b
WD
41#endif
42
bf95df44 43#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
21f63944 44static int env_nvram_get_char(int index)
c609719b 45{
c609719b
WD
46 uchar c;
47
91494ca6 48 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
c609719b
WD
49
50 return c;
c609719b 51}
bf95df44 52#endif
c609719b 53
c5951991 54static int env_nvram_load(void)
c609719b 55{
cd0f4fa1 56 char buf[CONFIG_ENV_SIZE];
ea882baf 57
6d0f6bcf 58#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
ea882baf 59 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
c609719b 60#else
cd0f4fa1 61 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
c609719b 62#endif
2166ebf7 63 return env_import(buf, 1);
c609719b
WD
64}
65
e5bce247 66static int env_nvram_save(void)
c609719b 67{
cd0f4fa1 68 env_t env_new;
ea882baf
WD
69 int rcode = 0;
70
7ce1526e
MV
71 rcode = env_export(&env_new);
72 if (rcode)
73 return rcode;
ea882baf 74
6d0f6bcf 75#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
cd0f4fa1
TR
76 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
77#else
78 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
79 rcode = 1;
c609719b
WD
80#endif
81 return rcode;
82}
83
ea882baf 84/*
c609719b
WD
85 * Initialize Environment use
86 *
87 * We are still running from ROM, so data use is limited
88 */
e5bce247 89static int env_nvram_init(void)
c609719b 90{
6d0f6bcf 91#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
c609719b 92 ulong crc;
cd0f4fa1 93 uchar data[ENV_SIZE];
ea882baf
WD
94
95 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
91494ca6 96 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
c609719b
WD
97
98 if (crc32(0, data, ENV_SIZE) == crc) {
91494ca6 99 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
c609719b
WD
100#else
101 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
91494ca6 102 gd->env_addr = (ulong)&env_ptr->data;
c609719b 103#endif
203e94f6 104 gd->env_valid = ENV_VALID;
c609719b 105 } else {
91494ca6 106 gd->env_addr = (ulong)&default_environment[0];
2d7cb5b4 107 gd->env_valid = ENV_INVALID;
c609719b 108 }
91494ca6
IG
109
110 return 0;
c609719b 111}
4415f1d1
SG
112
113U_BOOT_ENV_LOCATION(nvram) = {
114 .location = ENVL_NVRAM,
ac358beb 115 ENV_NAME("NVRAM")
4415f1d1 116#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
e5bce247 117 .get_char = env_nvram_get_char,
4415f1d1 118#endif
e5bce247
SG
119 .load = env_nvram_load,
120 .save = env_save_ptr(env_nvram_save),
121 .init = env_nvram_init,
4415f1d1 122};