blob: 229c34f5367c636cb295526e1e5069f9c2746868 [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 <common.h>
wdenkc6097192002-11-03 00:24:07 +000011#include <command.h>
Simon Glass7b51b572019-08-01 09:46:52 -060012#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060013#include <env_internal.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <linux/stddef.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020016#include <search.h>
17#include <errno.h>
Simon Glass3db71102019-11-14 12:57:16 -070018#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000019
Jean-Christophe PLAGNIOL-VILLARD957a0e62008-09-10 22:47:59 +020020DECLARE_GLOBAL_DATA_PTR;
21
Rasmus Villemoes46d9d1c2020-02-18 08:54:09 +000022static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000023
Simon Glassc5951992017-08-03 12:22:17 -060024static int env_nvram_load(void)
wdenkc6097192002-11-03 00:24:07 +000025{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040026 char buf[CONFIG_ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020027
Tom Rinicd0f4fa2013-04-05 14:55:21 -040028 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
Tom Riniade03b52022-12-02 16:42:23 -050029
Marek Vasut890feec2020-07-07 20:51:35 +020030 return env_import(buf, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +000031}
32
Simon Glasse5bce242017-08-03 12:22:01 -060033static int env_nvram_save(void)
wdenkc6097192002-11-03 00:24:07 +000034{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040035 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020036 int rcode = 0;
37
Marek Vasut7ce15262014-03-05 19:59:50 +010038 rcode = env_export(&env_new);
39 if (rcode)
40 return rcode;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020041
Tom Rinicd0f4fa2013-04-05 14:55:21 -040042 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
43 rcode = 1;
Tom Riniade03b52022-12-02 16:42:23 -050044
wdenkc6097192002-11-03 00:24:07 +000045 return rcode;
46}
47
Wolfgang Denkea882ba2010-06-20 23:33:59 +020048/*
wdenkc6097192002-11-03 00:24:07 +000049 * Initialize Environment use
50 *
51 * We are still running from ROM, so data use is limited
52 */
Simon Glasse5bce242017-08-03 12:22:01 -060053static int env_nvram_init(void)
wdenkc6097192002-11-03 00:24:07 +000054{
wdenkc6097192002-11-03 00:24:07 +000055 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Marek BehĂșna73c1f02021-10-22 15:47:17 +020056 gd->env_addr = (ulong)&env_ptr->data;
Simon Glass203e94f2017-08-03 12:21:56 -060057 gd->env_valid = ENV_VALID;
wdenkc6097192002-11-03 00:24:07 +000058 } else {
Marek BehĂșn4735de42021-10-22 15:47:16 +020059 gd->env_valid = ENV_INVALID;
wdenkc6097192002-11-03 00:24:07 +000060 }
Igor Grinberg91494ca2011-11-07 01:14:04 +000061
62 return 0;
wdenkc6097192002-11-03 00:24:07 +000063}
Simon Glass4415f1d2017-08-03 12:21:58 -060064
65U_BOOT_ENV_LOCATION(nvram) = {
66 .location = ENVL_NVRAM,
Simon Glassac358be2017-08-03 12:22:03 -060067 ENV_NAME("NVRAM")
Simon Glasse5bce242017-08-03 12:22:01 -060068 .load = env_nvram_load,
69 .save = env_save_ptr(env_nvram_save),
70 .init = env_nvram_init,
Simon Glass4415f1d2017-08-03 12:21:58 -060071};