blob: d49cd0f337a05d5700f20e6de54971f29f799683 [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
wdenkc6097192002-11-03 00:24:07 +000010#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060012#include <env_internal.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
wdenkc6097192002-11-03 00:24:07 +000014#include <linux/stddef.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020015#include <search.h>
16#include <errno.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070017#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000018
Jean-Christophe PLAGNIOL-VILLARDd13dd352008-09-10 22:47:59 +020019DECLARE_GLOBAL_DATA_PTR;
20
Rasmus Villemoes5d18fc12020-02-18 08:54:09 +000021static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000022
Simon Glass99778492017-08-03 12:22:17 -060023static int env_nvram_load(void)
wdenkc6097192002-11-03 00:24:07 +000024{
Tom Rinia840b8e2013-04-05 14:55:21 -040025 char buf[CONFIG_ENV_SIZE];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020026
Tom Rinia840b8e2013-04-05 14:55:21 -040027 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
Tom Rini8e27b262022-12-02 16:42:23 -050028
Marek Vasutdfe4b7e2020-07-07 20:51:35 +020029 return env_import(buf, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +000030}
31
Simon Glass082af922017-08-03 12:22:01 -060032static int env_nvram_save(void)
wdenkc6097192002-11-03 00:24:07 +000033{
Tom Rinia840b8e2013-04-05 14:55:21 -040034 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020035 int rcode = 0;
36
Marek Vasutd73c1292014-03-05 19:59:50 +010037 rcode = env_export(&env_new);
38 if (rcode)
39 return rcode;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020040
Tom Rinia840b8e2013-04-05 14:55:21 -040041 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
42 rcode = 1;
Tom Rini8e27b262022-12-02 16:42:23 -050043
wdenkc6097192002-11-03 00:24:07 +000044 return rcode;
45}
46
Wolfgang Denk460a9ff2010-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 Glass082af922017-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Ășnef480b42021-10-22 15:47:17 +020055 gd->env_addr = (ulong)&env_ptr->data;
Simon Glass4bc2ad22017-08-03 12:21:56 -060056 gd->env_valid = ENV_VALID;
wdenkc6097192002-11-03 00:24:07 +000057 } else {
Marek BehĂșn6d7653c2021-10-22 15:47:16 +020058 gd->env_valid = ENV_INVALID;
wdenkc6097192002-11-03 00:24:07 +000059 }
Igor Grinberg04482862011-11-07 01:14:04 +000060
61 return 0;
wdenkc6097192002-11-03 00:24:07 +000062}
Simon Glassc10a88e2017-08-03 12:21:58 -060063
64U_BOOT_ENV_LOCATION(nvram) = {
65 .location = ENVL_NVRAM,
Simon Glassd8273ed2017-08-03 12:22:03 -060066 ENV_NAME("NVRAM")
Simon Glass082af922017-08-03 12:22:01 -060067 .load = env_nvram_load,
68 .save = env_save_ptr(env_nvram_save),
69 .init = env_nvram_init,
Simon Glassc10a88e2017-08-03 12:21:58 -060070};