blob: d49cd0f337a05d5700f20e6de54971f29f799683 [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
wdenkc6097192002-11-03 00:24:07 +000010#include <command.h>
Simon Glass7b51b572019-08-01 09:46:52 -060011#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060012#include <env_internal.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
wdenkc6097192002-11-03 00:24:07 +000014#include <linux/stddef.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020015#include <search.h>
16#include <errno.h>
Simon Glass3db71102019-11-14 12:57:16 -070017#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000018
Jean-Christophe PLAGNIOL-VILLARD957a0e62008-09-10 22:47:59 +020019DECLARE_GLOBAL_DATA_PTR;
20
Rasmus Villemoes46d9d1c2020-02-18 08:54:09 +000021static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000022
Simon Glassc5951992017-08-03 12:22:17 -060023static int env_nvram_load(void)
wdenkc6097192002-11-03 00:24:07 +000024{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040025 char buf[CONFIG_ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020026
Tom Rinicd0f4fa2013-04-05 14:55:21 -040027 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
Tom Riniade03b52022-12-02 16:42:23 -050028
Marek Vasut890feec2020-07-07 20:51:35 +020029 return env_import(buf, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +000030}
31
Simon Glasse5bce242017-08-03 12:22:01 -060032static int env_nvram_save(void)
wdenkc6097192002-11-03 00:24:07 +000033{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040034 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020035 int rcode = 0;
36
Marek Vasut7ce15262014-03-05 19:59:50 +010037 rcode = env_export(&env_new);
38 if (rcode)
39 return rcode;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020040
Tom Rinicd0f4fa2013-04-05 14:55:21 -040041 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
42 rcode = 1;
Tom Riniade03b52022-12-02 16:42:23 -050043
wdenkc6097192002-11-03 00:24:07 +000044 return rcode;
45}
46
Wolfgang Denkea882ba2010-06-20 23:33:59 +020047/*
wdenkc6097192002-11-03 00:24:07 +000048 * Initialize Environment use
49 *
50 * We are still running from ROM, so data use is limited
51 */
Simon Glasse5bce242017-08-03 12:22:01 -060052static int env_nvram_init(void)
wdenkc6097192002-11-03 00:24:07 +000053{
wdenkc6097192002-11-03 00:24:07 +000054 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Marek BehĂșna73c1f02021-10-22 15:47:17 +020055 gd->env_addr = (ulong)&env_ptr->data;
Simon Glass203e94f2017-08-03 12:21:56 -060056 gd->env_valid = ENV_VALID;
wdenkc6097192002-11-03 00:24:07 +000057 } else {
Marek BehĂșn4735de42021-10-22 15:47:16 +020058 gd->env_valid = ENV_INVALID;
wdenkc6097192002-11-03 00:24:07 +000059 }
Igor Grinberg91494ca2011-11-07 01:14:04 +000060
61 return 0;
wdenkc6097192002-11-03 00:24:07 +000062}
Simon Glass4415f1d2017-08-03 12:21:58 -060063
64U_BOOT_ENV_LOCATION(nvram) = {
65 .location = ENVL_NVRAM,
Simon Glassac358be2017-08-03 12:22:03 -060066 ENV_NAME("NVRAM")
Simon Glasse5bce242017-08-03 12:22:01 -060067 .load = env_nvram_load,
68 .save = env_save_ptr(env_nvram_save),
69 .init = env_nvram_init,
Simon Glass4415f1d2017-08-03 12:21:58 -060070};