blob: 7c8ea26f968200f80359a7f915bdbfd570eabd02 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
Wolfgang Denk460a9ff2010-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-VILLARD03836942008-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 Glass0af6e2d2019-08-01 09:46:52 -060028#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060029#include <env_internal.h>
wdenkc6097192002-11-03 00:24:07 +000030#include <linux/stddef.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020031#include <search.h>
32#include <errno.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070033#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000034
Jean-Christophe PLAGNIOL-VILLARDd13dd352008-09-10 22:47:59 +020035DECLARE_GLOBAL_DATA_PTR;
36
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020037#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenkc6097192002-11-03 00:24:07 +000038extern void *nvram_read(void *dest, const long src, size_t count);
39extern void nvram_write(long dest, const void *src, size_t count);
wdenkc6097192002-11-03 00:24:07 +000040#else
Rasmus Villemoes5d18fc12020-02-18 08:54:09 +000041static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000042#endif
43
Igor Grinberg76c02dc2011-12-26 03:33:10 +000044#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Goldschmidt Simon9b0d66b2018-02-09 20:23:17 +000045/** Call this function from overridden env_get_char_spec() if you need
46 * this functionality.
47 */
48int env_nvram_get_char(int index)
wdenkbb444c92002-12-07 00:20:59 +000049{
wdenkbb444c92002-12-07 00:20:59 +000050 uchar c;
51
Igor Grinberg04482862011-11-07 01:14:04 +000052 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
wdenkc6097192002-11-03 00:24:07 +000053
wdenkbb444c92002-12-07 00:20:59 +000054 return c;
wdenkc6097192002-11-03 00:24:07 +000055}
Igor Grinberg76c02dc2011-12-26 03:33:10 +000056#endif
wdenkc6097192002-11-03 00:24:07 +000057
Simon Glass99778492017-08-03 12:22:17 -060058static int env_nvram_load(void)
wdenkc6097192002-11-03 00:24:07 +000059{
Tom Rinia840b8e2013-04-05 14:55:21 -040060 char buf[CONFIG_ENV_SIZE];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020061
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020062#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020063 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000064#else
Tom Rinia840b8e2013-04-05 14:55:21 -040065 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000066#endif
Marek Vasutdfe4b7e2020-07-07 20:51:35 +020067 return env_import(buf, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +000068}
69
Simon Glass082af922017-08-03 12:22:01 -060070static int env_nvram_save(void)
wdenkc6097192002-11-03 00:24:07 +000071{
Tom Rinia840b8e2013-04-05 14:55:21 -040072 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020073 int rcode = 0;
74
Marek Vasutd73c1292014-03-05 19:59:50 +010075 rcode = env_export(&env_new);
76 if (rcode)
77 return rcode;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020078
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020079#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Tom Rinia840b8e2013-04-05 14:55:21 -040080 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
81#else
82 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
83 rcode = 1;
wdenkc6097192002-11-03 00:24:07 +000084#endif
85 return rcode;
86}
87
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020088/*
wdenkc6097192002-11-03 00:24:07 +000089 * Initialize Environment use
90 *
91 * We are still running from ROM, so data use is limited
92 */
Simon Glass082af922017-08-03 12:22:01 -060093static int env_nvram_init(void)
wdenkc6097192002-11-03 00:24:07 +000094{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020095#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
wdenkc6097192002-11-03 00:24:07 +000096 ulong crc;
Tom Rinia840b8e2013-04-05 14:55:21 -040097 uchar data[ENV_SIZE];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020098
99 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
Igor Grinberg04482862011-11-07 01:14:04 +0000100 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000101
102 if (crc32(0, data, ENV_SIZE) == crc) {
Igor Grinberg04482862011-11-07 01:14:04 +0000103 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
wdenkc6097192002-11-03 00:24:07 +0000104#else
105 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg04482862011-11-07 01:14:04 +0000106 gd->env_addr = (ulong)&env_ptr->data;
wdenkc6097192002-11-03 00:24:07 +0000107#endif
Simon Glass4bc2ad22017-08-03 12:21:56 -0600108 gd->env_valid = ENV_VALID;
wdenkc6097192002-11-03 00:24:07 +0000109 } else {
Igor Grinberg04482862011-11-07 01:14:04 +0000110 gd->env_addr = (ulong)&default_environment[0];
Simon Glass1f39d0b2017-08-20 04:45:15 -0600111 gd->env_valid = ENV_INVALID;
wdenkc6097192002-11-03 00:24:07 +0000112 }
Igor Grinberg04482862011-11-07 01:14:04 +0000113
114 return 0;
wdenkc6097192002-11-03 00:24:07 +0000115}
Simon Glassc10a88e2017-08-03 12:21:58 -0600116
117U_BOOT_ENV_LOCATION(nvram) = {
118 .location = ENVL_NVRAM,
Simon Glassd8273ed2017-08-03 12:22:03 -0600119 ENV_NAME("NVRAM")
Simon Glass082af922017-08-03 12:22:01 -0600120 .load = env_nvram_load,
121 .save = env_save_ptr(env_nvram_save),
122 .init = env_nvram_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600123};