1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13 * It might not be possible in all cases to use 'memcpy()' to copy
14 * the environment to NVRAM, as the NVRAM might not be mapped into
15 * the memory space. (I.e. this is the case for the BAB750). In those
16 * cases it might be possible to access the NVRAM using a different
17 * method. For example, the RTC on the BAB750 is accessible in IO
18 * space using its address and data registers. To enable usage of
19 * NVRAM in those cases I invented the functions 'nvram_read()' and
20 * 'nvram_write()', which will be activated upon the configuration
21 * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
22 * strongly dependent on the used HW, and must be redefined for each
23 * board that wants to use them.
29 #include <env_internal.h>
30 #include <asm/global_data.h>
31 #include <linux/stddef.h>
34 #include <u-boot/crc.h>
36 DECLARE_GLOBAL_DATA_PTR;
38 #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
39 extern void *nvram_read(void *dest, const long src, size_t count);
40 extern void nvram_write(long dest, const void *src, size_t count);
42 static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
45 #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
46 /** Call this function from overridden env_get_char_spec() if you need
49 int env_nvram_get_char(int index)
53 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
59 static int env_nvram_load(void)
61 char buf[CONFIG_ENV_SIZE];
63 #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
64 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
66 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
68 return env_import(buf, 1, H_EXTERNAL);
71 static int env_nvram_save(void)
76 rcode = env_export(&env_new);
80 #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
81 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
83 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
90 * Initialize Environment use
92 * We are still running from ROM, so data use is limited
94 static int env_nvram_init(void)
96 #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
100 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
101 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
103 if (crc32(0, data, ENV_SIZE) == crc) {
104 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
106 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
107 gd->env_addr = (ulong)&env_ptr->data;
109 gd->env_valid = ENV_VALID;
111 gd->env_addr = (ulong)&default_environment[0];
112 gd->env_valid = ENV_INVALID;
118 U_BOOT_ENV_LOCATION(nvram) = {
119 .location = ENVL_NVRAM,
121 .load = env_nvram_load,
122 .save = env_save_ptr(env_nvram_save),
123 .init = env_nvram_init,