blob: c8b34754efc692690ad3510eb9f790d708b2a72e [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00009 */
10
11/*
12 * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
13 *
14 * It might not be possible in all cases to use 'memcpy()' to copy
15 * the environment to NVRAM, as the NVRAM might not be mapped into
16 * the memory space. (I.e. this is the case for the BAB750). In those
17 * cases it might be possible to access the NVRAM using a different
18 * method. For example, the RTC on the BAB750 is accessible in IO
19 * space using its address and data registers. To enable usage of
20 * NVRAM in those cases I invented the functions 'nvram_read()' and
21 * 'nvram_write()', which will be activated upon the configuration
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020022 * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
wdenkc6097192002-11-03 00:24:07 +000023 * strongly dependent on the used HW, and must be redefined for each
24 * board that wants to use them.
25 */
26
27#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000028#include <command.h>
29#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000030#include <linux/stddef.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020031#include <search.h>
32#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000033
Jean-Christophe PLAGNIOL-VILLARDd13dd352008-09-10 22:47:59 +020034DECLARE_GLOBAL_DATA_PTR;
35
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020036#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenkc6097192002-11-03 00:24:07 +000037extern void *nvram_read(void *dest, const long src, size_t count);
38extern void nvram_write(long dest, const void *src, size_t count);
wdenkc6097192002-11-03 00:24:07 +000039#else
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020040env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000041#endif
42
Igor Grinberg76c02dc2011-12-26 03:33:10 +000043#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Simon Glass3fa4c222017-08-03 12:22:16 -060044static int env_nvram_get_char(int index)
wdenkbb444c92002-12-07 00:20:59 +000045{
wdenkbb444c92002-12-07 00:20:59 +000046 uchar c;
47
Igor Grinberg04482862011-11-07 01:14:04 +000048 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
wdenkc6097192002-11-03 00:24:07 +000049
wdenkbb444c92002-12-07 00:20:59 +000050 return c;
wdenkc6097192002-11-03 00:24:07 +000051}
Igor Grinberg76c02dc2011-12-26 03:33:10 +000052#endif
wdenkc6097192002-11-03 00:24:07 +000053
Simon Glass99778492017-08-03 12:22:17 -060054static int env_nvram_load(void)
wdenkc6097192002-11-03 00:24:07 +000055{
Tom Rinia840b8e2013-04-05 14:55:21 -040056 char buf[CONFIG_ENV_SIZE];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020057
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020058#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020059 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000060#else
Tom Rinia840b8e2013-04-05 14:55:21 -040061 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000062#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020063 env_import(buf, 1);
Simon Glass99778492017-08-03 12:22:17 -060064
65 return 0;
wdenkc6097192002-11-03 00:24:07 +000066}
67
Simon Glass082af922017-08-03 12:22:01 -060068static int env_nvram_save(void)
wdenkc6097192002-11-03 00:24:07 +000069{
Tom Rinia840b8e2013-04-05 14:55:21 -040070 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020071 int rcode = 0;
72
Marek Vasutd73c1292014-03-05 19:59:50 +010073 rcode = env_export(&env_new);
74 if (rcode)
75 return rcode;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020076
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020077#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Tom Rinia840b8e2013-04-05 14:55:21 -040078 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
79#else
80 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
81 rcode = 1;
wdenkc6097192002-11-03 00:24:07 +000082#endif
83 return rcode;
84}
85
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020086/*
wdenkc6097192002-11-03 00:24:07 +000087 * Initialize Environment use
88 *
89 * We are still running from ROM, so data use is limited
90 */
Simon Glass082af922017-08-03 12:22:01 -060091static int env_nvram_init(void)
wdenkc6097192002-11-03 00:24:07 +000092{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020093#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
wdenkc6097192002-11-03 00:24:07 +000094 ulong crc;
Tom Rinia840b8e2013-04-05 14:55:21 -040095 uchar data[ENV_SIZE];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020096
97 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
Igor Grinberg04482862011-11-07 01:14:04 +000098 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000099
100 if (crc32(0, data, ENV_SIZE) == crc) {
Igor Grinberg04482862011-11-07 01:14:04 +0000101 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
wdenkc6097192002-11-03 00:24:07 +0000102#else
103 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg04482862011-11-07 01:14:04 +0000104 gd->env_addr = (ulong)&env_ptr->data;
wdenkc6097192002-11-03 00:24:07 +0000105#endif
Simon Glass4bc2ad22017-08-03 12:21:56 -0600106 gd->env_valid = ENV_VALID;
wdenkc6097192002-11-03 00:24:07 +0000107 } else {
Igor Grinberg04482862011-11-07 01:14:04 +0000108 gd->env_addr = (ulong)&default_environment[0];
Simon Glass1f39d0b2017-08-20 04:45:15 -0600109 gd->env_valid = ENV_INVALID;
wdenkc6097192002-11-03 00:24:07 +0000110 }
Igor Grinberg04482862011-11-07 01:14:04 +0000111
112 return 0;
wdenkc6097192002-11-03 00:24:07 +0000113}
Simon Glassc10a88e2017-08-03 12:21:58 -0600114
115U_BOOT_ENV_LOCATION(nvram) = {
116 .location = ENVL_NVRAM,
Simon Glassd8273ed2017-08-03 12:22:03 -0600117 ENV_NAME("NVRAM")
Simon Glassc10a88e2017-08-03 12:21:58 -0600118#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Simon Glass082af922017-08-03 12:22:01 -0600119 .get_char = env_nvram_get_char,
Simon Glassc10a88e2017-08-03 12:21:58 -0600120#endif
Simon Glass082af922017-08-03 12:22:01 -0600121 .load = env_nvram_load,
122 .save = env_save_ptr(env_nvram_save),
123 .init = env_nvram_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600124};