blob: fb265235afc71c0497a8d4539fc2b430e0ab4190 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02003 * (C) Copyright 2000-2010
wdenkc6097192002-11-03 00:24:07 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
wdenkc6097192002-11-03 00:24:07 +00008 */
9
10/*
11 * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020021 * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
wdenkc6097192002-11-03 00:24:07 +000022 * 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>
wdenkc6097192002-11-03 00:24:07 +000027#include <command.h>
Simon Glass7b51b572019-08-01 09:46:52 -060028#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060029#include <env_internal.h>
Simon Glass401d1c42020-10-30 21:38:53 -060030#include <asm/global_data.h>
wdenkc6097192002-11-03 00:24:07 +000031#include <linux/stddef.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020032#include <search.h>
33#include <errno.h>
Simon Glass3db71102019-11-14 12:57:16 -070034#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000035
Jean-Christophe PLAGNIOL-VILLARD957a0e62008-09-10 22:47:59 +020036DECLARE_GLOBAL_DATA_PTR;
37
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenkc6097192002-11-03 00:24:07 +000039extern void *nvram_read(void *dest, const long src, size_t count);
40extern void nvram_write(long dest, const void *src, size_t count);
wdenkc6097192002-11-03 00:24:07 +000041#else
Rasmus Villemoes46d9d1c2020-02-18 08:54:09 +000042static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000043#endif
44
Simon Glassc5951992017-08-03 12:22:17 -060045static int env_nvram_load(void)
wdenkc6097192002-11-03 00:24:07 +000046{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040047 char buf[CONFIG_ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020048
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020049#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +020050 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000051#else
Tom Rinicd0f4fa2013-04-05 14:55:21 -040052 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000053#endif
Marek Vasut890feec2020-07-07 20:51:35 +020054 return env_import(buf, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +000055}
56
Simon Glasse5bce242017-08-03 12:22:01 -060057static int env_nvram_save(void)
wdenkc6097192002-11-03 00:24:07 +000058{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040059 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020060 int rcode = 0;
61
Marek Vasut7ce15262014-03-05 19:59:50 +010062 rcode = env_export(&env_new);
63 if (rcode)
64 return rcode;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020065
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020066#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Tom Rinicd0f4fa2013-04-05 14:55:21 -040067 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;
wdenkc6097192002-11-03 00:24:07 +000071#endif
72 return rcode;
73}
74
Wolfgang Denkea882ba2010-06-20 23:33:59 +020075/*
wdenkc6097192002-11-03 00:24:07 +000076 * Initialize Environment use
77 *
78 * We are still running from ROM, so data use is limited
79 */
Simon Glasse5bce242017-08-03 12:22:01 -060080static int env_nvram_init(void)
wdenkc6097192002-11-03 00:24:07 +000081{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020082#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
wdenkc6097192002-11-03 00:24:07 +000083 ulong crc;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040084 uchar data[ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020085
86 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
Igor Grinberg91494ca2011-11-07 01:14:04 +000087 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000088
89 if (crc32(0, data, ENV_SIZE) == crc) {
Marek Behúna73c1f02021-10-22 15:47:17 +020090 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
wdenkc6097192002-11-03 00:24:07 +000091#else
92 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Marek Behúna73c1f02021-10-22 15:47:17 +020093 gd->env_addr = (ulong)&env_ptr->data;
wdenkc6097192002-11-03 00:24:07 +000094#endif
Simon Glass203e94f2017-08-03 12:21:56 -060095 gd->env_valid = ENV_VALID;
wdenkc6097192002-11-03 00:24:07 +000096 } else {
Marek Behún4735de42021-10-22 15:47:16 +020097 gd->env_valid = ENV_INVALID;
wdenkc6097192002-11-03 00:24:07 +000098 }
Igor Grinberg91494ca2011-11-07 01:14:04 +000099
100 return 0;
wdenkc6097192002-11-03 00:24:07 +0000101}
Simon Glass4415f1d2017-08-03 12:21:58 -0600102
103U_BOOT_ENV_LOCATION(nvram) = {
104 .location = ENVL_NVRAM,
Simon Glassac358be2017-08-03 12:22:03 -0600105 ENV_NAME("NVRAM")
Simon Glasse5bce242017-08-03 12:22:01 -0600106 .load = env_nvram_load,
107 .save = env_save_ptr(env_nvram_save),
108 .init = env_nvram_init,
Simon Glass4415f1d2017-08-03 12:21:58 -0600109};