]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
c609719b | 2 | /* |
ea882baf | 3 | * (C) Copyright 2000-2010 |
c609719b WD |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
5 | * | |
6 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
7 | * Andreas Heppel <[email protected]> | |
c609719b WD |
8 | */ |
9 | ||
10 | /* | |
11 | * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <[email protected]> | |
12 | * | |
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 | |
6d0f6bcf | 21 | * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are |
c609719b WD |
22 | * strongly dependent on the used HW, and must be redefined for each |
23 | * board that wants to use them. | |
24 | */ | |
25 | ||
26 | #include <common.h> | |
c609719b | 27 | #include <command.h> |
7b51b576 | 28 | #include <env.h> |
f3998fdc | 29 | #include <env_internal.h> |
401d1c4f | 30 | #include <asm/global_data.h> |
c609719b | 31 | #include <linux/stddef.h> |
ea882baf WD |
32 | #include <search.h> |
33 | #include <errno.h> | |
3db71108 | 34 | #include <u-boot/crc.h> |
c609719b | 35 | |
957a0e69 JCPV |
36 | DECLARE_GLOBAL_DATA_PTR; |
37 | ||
6d0f6bcf | 38 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
c609719b WD |
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); | |
c609719b | 41 | #else |
46d9d1c3 | 42 | static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
c609719b WD |
43 | #endif |
44 | ||
c5951991 | 45 | static int env_nvram_load(void) |
c609719b | 46 | { |
cd0f4fa1 | 47 | char buf[CONFIG_ENV_SIZE]; |
ea882baf | 48 | |
6d0f6bcf | 49 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
ea882baf | 50 | nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
c609719b | 51 | #else |
cd0f4fa1 | 52 | memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
c609719b | 53 | #endif |
890feeca | 54 | return env_import(buf, 1, H_EXTERNAL); |
c609719b WD |
55 | } |
56 | ||
e5bce247 | 57 | static int env_nvram_save(void) |
c609719b | 58 | { |
cd0f4fa1 | 59 | env_t env_new; |
ea882baf WD |
60 | int rcode = 0; |
61 | ||
7ce1526e MV |
62 | rcode = env_export(&env_new); |
63 | if (rcode) | |
64 | return rcode; | |
ea882baf | 65 | |
6d0f6bcf | 66 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
cd0f4fa1 TR |
67 | nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE); |
68 | #else | |
69 | if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL) | |
70 | rcode = 1; | |
c609719b WD |
71 | #endif |
72 | return rcode; | |
73 | } | |
74 | ||
ea882baf | 75 | /* |
c609719b WD |
76 | * Initialize Environment use |
77 | * | |
78 | * We are still running from ROM, so data use is limited | |
79 | */ | |
e5bce247 | 80 | static int env_nvram_init(void) |
c609719b | 81 | { |
6d0f6bcf | 82 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
c609719b | 83 | ulong crc; |
cd0f4fa1 | 84 | uchar data[ENV_SIZE]; |
ea882baf WD |
85 | |
86 | nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong)); | |
91494ca6 | 87 | nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE); |
c609719b WD |
88 | |
89 | if (crc32(0, data, ENV_SIZE) == crc) { | |
91494ca6 | 90 | gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long); |
c609719b WD |
91 | #else |
92 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { | |
91494ca6 | 93 | gd->env_addr = (ulong)&env_ptr->data; |
c609719b | 94 | #endif |
203e94f6 | 95 | gd->env_valid = ENV_VALID; |
c609719b | 96 | } else { |
91494ca6 | 97 | gd->env_addr = (ulong)&default_environment[0]; |
2d7cb5b4 | 98 | gd->env_valid = ENV_INVALID; |
c609719b | 99 | } |
91494ca6 IG |
100 | |
101 | return 0; | |
c609719b | 102 | } |
4415f1d1 SG |
103 | |
104 | U_BOOT_ENV_LOCATION(nvram) = { | |
105 | .location = ENVL_NVRAM, | |
ac358beb | 106 | ENV_NAME("NVRAM") |
e5bce247 SG |
107 | .load = env_nvram_load, |
108 | .save = env_save_ptr(env_nvram_save), | |
109 | .init = env_nvram_init, | |
4415f1d1 | 110 | }; |