Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 2 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 3 | * (C) Copyright 2000-2010 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 4 | * 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> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 10 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 13 | #include <env_internal.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 15 | #include <linux/stddef.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 16 | #include <search.h> |
| 17 | #include <errno.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 18 | #include <u-boot/crc.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 19 | |
Jean-Christophe PLAGNIOL-VILLARD | d13dd35 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Rasmus Villemoes | 5d18fc1 | 2020-02-18 08:54:09 +0000 | [diff] [blame] | 22 | static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 23 | |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 24 | static int env_nvram_load(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 25 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 26 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 27 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 28 | memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
Tom Rini | 8e27b26 | 2022-12-02 16:42:23 -0500 | [diff] [blame] | 29 | |
Marek Vasut | dfe4b7e | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 30 | return env_import(buf, 1, H_EXTERNAL); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 33 | static int env_nvram_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 34 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 35 | env_t env_new; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 36 | int rcode = 0; |
| 37 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 38 | rcode = env_export(&env_new); |
| 39 | if (rcode) |
| 40 | return rcode; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 41 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 42 | if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL) |
| 43 | rcode = 1; |
Tom Rini | 8e27b26 | 2022-12-02 16:42:23 -0500 | [diff] [blame] | 44 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 45 | return rcode; |
| 46 | } |
| 47 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 48 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | * Initialize Environment use |
| 50 | * |
| 51 | * We are still running from ROM, so data use is limited |
| 52 | */ |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 53 | static int env_nvram_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 55 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Marek BehĂșn | ef480b4 | 2021-10-22 15:47:17 +0200 | [diff] [blame] | 56 | gd->env_addr = (ulong)&env_ptr->data; |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 57 | gd->env_valid = ENV_VALID; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 58 | } else { |
Marek BehĂșn | 6d7653c | 2021-10-22 15:47:16 +0200 | [diff] [blame] | 59 | gd->env_valid = ENV_INVALID; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 60 | } |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 61 | |
| 62 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | } |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 64 | |
| 65 | U_BOOT_ENV_LOCATION(nvram) = { |
| 66 | .location = ENVL_NVRAM, |
Simon Glass | d8273ed | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 67 | ENV_NAME("NVRAM") |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 68 | .load = env_nvram_load, |
| 69 | .save = env_save_ptr(env_nvram_save), |
| 70 | .init = env_nvram_init, |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 71 | }; |