blob: 229c34f5367c636cb295526e1e5069f9c2746868 [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 <common.h>
wdenkc6097192002-11-03 00:24:07 +000011#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060012#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060013#include <env_internal.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <linux/stddef.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020016#include <search.h>
17#include <errno.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070018#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000019
Jean-Christophe PLAGNIOL-VILLARDd13dd352008-09-10 22:47:59 +020020DECLARE_GLOBAL_DATA_PTR;
21
Rasmus Villemoes5d18fc12020-02-18 08:54:09 +000022static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000023
Simon Glass99778492017-08-03 12:22:17 -060024static int env_nvram_load(void)
wdenkc6097192002-11-03 00:24:07 +000025{
Tom Rinia840b8e2013-04-05 14:55:21 -040026 char buf[CONFIG_ENV_SIZE];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020027
Tom Rinia840b8e2013-04-05 14:55:21 -040028 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
Tom Rini8e27b262022-12-02 16:42:23 -050029
Marek Vasutdfe4b7e2020-07-07 20:51:35 +020030 return env_import(buf, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +000031}
32
Simon Glass082af922017-08-03 12:22:01 -060033static int env_nvram_save(void)
wdenkc6097192002-11-03 00:24:07 +000034{
Tom Rinia840b8e2013-04-05 14:55:21 -040035 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020036 int rcode = 0;
37
Marek Vasutd73c1292014-03-05 19:59:50 +010038 rcode = env_export(&env_new);
39 if (rcode)
40 return rcode;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020041
Tom Rinia840b8e2013-04-05 14:55:21 -040042 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
43 rcode = 1;
Tom Rini8e27b262022-12-02 16:42:23 -050044
wdenkc6097192002-11-03 00:24:07 +000045 return rcode;
46}
47
Wolfgang Denk460a9ff2010-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 Glass082af922017-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Ășnef480b42021-10-22 15:47:17 +020056 gd->env_addr = (ulong)&env_ptr->data;
Simon Glass4bc2ad22017-08-03 12:21:56 -060057 gd->env_valid = ENV_VALID;
wdenkc6097192002-11-03 00:24:07 +000058 } else {
Marek BehĂșn6d7653c2021-10-22 15:47:16 +020059 gd->env_valid = ENV_INVALID;
wdenkc6097192002-11-03 00:24:07 +000060 }
Igor Grinberg04482862011-11-07 01:14:04 +000061
62 return 0;
wdenkc6097192002-11-03 00:24:07 +000063}
Simon Glassc10a88e2017-08-03 12:21:58 -060064
65U_BOOT_ENV_LOCATION(nvram) = {
66 .location = ENVL_NVRAM,
Simon Glassd8273ed2017-08-03 12:22:03 -060067 ENV_NAME("NVRAM")
Simon Glass082af922017-08-03 12:22:01 -060068 .load = env_nvram_load,
69 .save = env_save_ptr(env_nvram_save),
70 .init = env_nvram_init,
Simon Glassc10a88e2017-08-03 12:21:58 -060071};