blob: df1b37913d57d8f0a03edbd6cb5ac97e75aa6676 [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>
28#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000029#include <linux/stddef.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020030#include <search.h>
31#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000032
Jean-Christophe PLAGNIOL-VILLARD957a0e62008-09-10 22:47:59 +020033DECLARE_GLOBAL_DATA_PTR;
34
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020035#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenkc6097192002-11-03 00:24:07 +000036extern void *nvram_read(void *dest, const long src, size_t count);
37extern void nvram_write(long dest, const void *src, size_t count);
wdenkc6097192002-11-03 00:24:07 +000038#else
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020039env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000040#endif
41
Igor Grinbergbf95df42011-12-26 03:33:10 +000042#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +000043/** Call this function from overridden env_get_char_spec() if you need
44 * this functionality.
45 */
46int env_nvram_get_char(int index)
wdenkc6097192002-11-03 00:24:07 +000047{
wdenkc6097192002-11-03 00:24:07 +000048 uchar c;
49
Igor Grinberg91494ca2011-11-07 01:14:04 +000050 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
wdenkc6097192002-11-03 00:24:07 +000051
52 return c;
wdenkc6097192002-11-03 00:24:07 +000053}
Igor Grinbergbf95df42011-12-26 03:33:10 +000054#endif
wdenkc6097192002-11-03 00:24:07 +000055
Simon Glassc5951992017-08-03 12:22:17 -060056static int env_nvram_load(void)
wdenkc6097192002-11-03 00:24:07 +000057{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040058 char buf[CONFIG_ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020059
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020060#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +020061 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000062#else
Tom Rinicd0f4fa2013-04-05 14:55:21 -040063 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000064#endif
Simon Goldschmidt2166ebf2018-01-31 14:47:12 +010065 return env_import(buf, 1);
wdenkc6097192002-11-03 00:24:07 +000066}
67
Simon Glasse5bce242017-08-03 12:22:01 -060068static int env_nvram_save(void)
wdenkc6097192002-11-03 00:24:07 +000069{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040070 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020071 int rcode = 0;
72
Marek Vasut7ce15262014-03-05 19:59:50 +010073 rcode = env_export(&env_new);
74 if (rcode)
75 return rcode;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020076
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020077#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Tom Rinicd0f4fa2013-04-05 14:55:21 -040078 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
79#else
80 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
81 rcode = 1;
wdenkc6097192002-11-03 00:24:07 +000082#endif
83 return rcode;
84}
85
Wolfgang Denkea882ba2010-06-20 23:33:59 +020086/*
wdenkc6097192002-11-03 00:24:07 +000087 * Initialize Environment use
88 *
89 * We are still running from ROM, so data use is limited
90 */
Simon Glasse5bce242017-08-03 12:22:01 -060091static int env_nvram_init(void)
wdenkc6097192002-11-03 00:24:07 +000092{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020093#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
wdenkc6097192002-11-03 00:24:07 +000094 ulong crc;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040095 uchar data[ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020096
97 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
Igor Grinberg91494ca2011-11-07 01:14:04 +000098 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000099
100 if (crc32(0, data, ENV_SIZE) == crc) {
Igor Grinberg91494ca2011-11-07 01:14:04 +0000101 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
wdenkc6097192002-11-03 00:24:07 +0000102#else
103 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg91494ca2011-11-07 01:14:04 +0000104 gd->env_addr = (ulong)&env_ptr->data;
wdenkc6097192002-11-03 00:24:07 +0000105#endif
Simon Glass203e94f2017-08-03 12:21:56 -0600106 gd->env_valid = ENV_VALID;
wdenkc6097192002-11-03 00:24:07 +0000107 } else {
Igor Grinberg91494ca2011-11-07 01:14:04 +0000108 gd->env_addr = (ulong)&default_environment[0];
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600109 gd->env_valid = ENV_INVALID;
wdenkc6097192002-11-03 00:24:07 +0000110 }
Igor Grinberg91494ca2011-11-07 01:14:04 +0000111
112 return 0;
wdenkc6097192002-11-03 00:24:07 +0000113}
Simon Glass4415f1d2017-08-03 12:21:58 -0600114
115U_BOOT_ENV_LOCATION(nvram) = {
116 .location = ENVL_NVRAM,
Simon Glassac358be2017-08-03 12:22:03 -0600117 ENV_NAME("NVRAM")
Simon Glasse5bce242017-08-03 12:22:01 -0600118 .load = env_nvram_load,
119 .save = env_save_ptr(env_nvram_save),
120 .init = env_nvram_init,
Simon Glass4415f1d2017-08-03 12:21:58 -0600121};