Tom Rini | 83d290c | 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 | ea882ba | 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 | |
| 10 | /* #define DEBUG */ |
| 11 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 12 | #include <command.h> |
Simon Glass | db19701 | 2019-08-01 09:47:04 -0600 | [diff] [blame] | 13 | #include <env.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 14 | #include <env_internal.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 17 | #include <linux/stddef.h> |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 18 | #include <malloc.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 19 | #include <search.h> |
| 20 | #include <errno.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 21 | #include <u-boot/crc.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 22 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 25 | #ifndef CONFIG_SPL_BUILD |
| 26 | # if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH) |
Tom Rini | 17ead04 | 2022-07-23 13:05:03 -0400 | [diff] [blame] | 27 | # include <flash.h> |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 28 | # define CMD_SAVEENV |
| 29 | # elif defined(CONFIG_ENV_ADDR_REDUND) |
| 30 | # error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH |
| 31 | # endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 34 | /* TODO(sjg@chromium.org): Figure out all these special cases */ |
| 35 | #if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \ |
| 36 | !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \ |
| 37 | !defined(CONFIG_TARGET_EDMINIV2)) || \ |
| 38 | !defined(CONFIG_SPL_BUILD) |
| 39 | #define LOADENV |
| 40 | #endif |
| 41 | |
| 42 | #if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD) |
| 43 | #define INITENV |
| 44 | #endif |
| 45 | |
Rajesh Bhagat | b6cba29 | 2018-11-05 18:01:10 +0000 | [diff] [blame] | 46 | #if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \ |
| 47 | !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 48 | #ifdef ENV_IS_EMBEDDED |
Simon Glass | be54ec1 | 2019-08-01 09:47:03 -0600 | [diff] [blame] | 49 | static env_t *env_ptr = &embedded_environment; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | #else /* ! ENV_IS_EMBEDDED */ |
| 51 | |
Rajesh Bhagat | b6cba29 | 2018-11-05 18:01:10 +0000 | [diff] [blame] | 52 | static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 53 | #endif /* ENV_IS_EMBEDDED */ |
Rajesh Bhagat | b6cba29 | 2018-11-05 18:01:10 +0000 | [diff] [blame] | 54 | #endif |
| 55 | static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 57 | /* CONFIG_ENV_ADDR is supposed to be on sector boundary */ |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 58 | static ulong __maybe_unused end_addr = |
| 59 | CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 60 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 61 | #ifdef CONFIG_ENV_ADDR_REDUND |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 62 | |
| 63 | static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 64 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 65 | /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */ |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 66 | static ulong __maybe_unused end_addr_new = |
| 67 | CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 68 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 69 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 70 | #ifdef CONFIG_ENV_ADDR_REDUND |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 71 | #ifdef INITENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 72 | static int env_flash_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 73 | { |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 74 | int crc1_ok = 0, crc2_ok = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 75 | |
| 76 | uchar flag1 = flash_addr->flags; |
| 77 | uchar flag2 = flash_addr_new->flags; |
| 78 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 79 | ulong addr1 = (ulong)&(flash_addr->data); |
| 80 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 81 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 82 | crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc; |
| 83 | crc2_ok = |
| 84 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 85 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 86 | if (crc1_ok && !crc2_ok) { |
| 87 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 88 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 89 | } else if (!crc1_ok && crc2_ok) { |
| 90 | gd->env_addr = addr2; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 91 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 92 | } else if (!crc1_ok && !crc2_ok) { |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 93 | gd->env_valid = ENV_INVALID; |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 94 | } else if (flag1 == ENV_REDUND_ACTIVE && |
| 95 | flag2 == ENV_REDUND_OBSOLETE) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 96 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 97 | gd->env_valid = ENV_VALID; |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 98 | } else if (flag1 == ENV_REDUND_OBSOLETE && |
| 99 | flag2 == ENV_REDUND_ACTIVE) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 100 | gd->env_addr = addr2; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 101 | gd->env_valid = ENV_VALID; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 102 | } else if (flag1 == flag2) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 103 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 104 | gd->env_valid = ENV_REDUND; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 105 | } else if (flag1 == 0xFF) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 106 | gd->env_addr = addr1; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 107 | gd->env_valid = ENV_REDUND; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 108 | } else if (flag2 == 0xFF) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 109 | gd->env_addr = addr2; |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 110 | gd->env_valid = ENV_REDUND; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 113 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 115 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | |
| 117 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 118 | static int env_flash_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 119 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 120 | env_t env_new; |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 121 | char *saved_data = NULL; |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 122 | char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 123 | int rc = 1; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 124 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 125 | ulong up_data = 0; |
wdenk | 500545c | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 126 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 127 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 128 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 130 | if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 131 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 132 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 133 | debug("Protect off %08lX ... %08lX\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 134 | (ulong)flash_addr_new, end_addr_new); |
| 135 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 136 | if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 137 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 138 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 139 | rc = env_export(&env_new); |
| 140 | if (rc) |
| 141 | return rc; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 142 | env_new.flags = new_flag; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 143 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 144 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 145 | up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 146 | debug("Data to save 0x%lX\n", up_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 147 | if (up_data) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 148 | saved_data = malloc(up_data); |
| 149 | if (saved_data == NULL) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 150 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 151 | up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 152 | goto done; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 153 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 154 | memcpy(saved_data, |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 155 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), |
| 156 | up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 157 | debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n", |
| 158 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
| 159 | up_data, saved_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 160 | } |
| 161 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 162 | puts("Erasing Flash..."); |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 163 | debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 164 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 165 | if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 166 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 167 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 168 | puts("Writing to Flash... "); |
| 169 | debug(" %08lX ... %08lX ...", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 170 | (ulong)&(flash_addr_new->data), |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 171 | sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data)); |
| 172 | rc = flash_write((char *)&env_new, (ulong)flash_addr_new, |
| 173 | sizeof(env_new)); |
| 174 | if (rc) |
| 175 | goto perror; |
| 176 | |
| 177 | rc = flash_write(&flag, (ulong)&(flash_addr->flags), |
| 178 | sizeof(flash_addr->flags)); |
| 179 | if (rc) |
| 180 | goto perror; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 181 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 182 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 183 | if (up_data) { /* restore the rest of sector */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 184 | debug("Restoring the rest of data to 0x%lX len 0x%lX\n", |
| 185 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 186 | if (flash_write(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 187 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 188 | up_data)) |
| 189 | goto perror; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 190 | } |
| 191 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 192 | puts("done\n"); |
| 193 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 194 | { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 195 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 196 | ulong ltmp = end_addr; |
| 197 | |
| 198 | flash_addr = flash_addr_new; |
| 199 | flash_addr_new = etmp; |
| 200 | |
| 201 | end_addr = end_addr_new; |
| 202 | end_addr_new = ltmp; |
| 203 | } |
| 204 | |
| 205 | rc = 0; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 206 | goto done; |
| 207 | perror: |
| 208 | flash_perror(rc); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 209 | done: |
Heinrich Schuchardt | 28ab12a | 2021-10-26 00:41:59 +0200 | [diff] [blame] | 210 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 211 | /* try to re-protect */ |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 212 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
| 213 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 214 | |
| 215 | return rc; |
| 216 | } |
| 217 | #endif /* CMD_SAVEENV */ |
| 218 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 219 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 220 | |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 221 | #ifdef INITENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 222 | static int env_flash_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 223 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 224 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 225 | gd->env_addr = (ulong)&(env_ptr->data); |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 226 | gd->env_valid = ENV_VALID; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 227 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 228 | } |
Dirk Behme | 9bb8b20 | 2007-08-20 07:09:05 +0200 | [diff] [blame] | 229 | |
Marek Behún | ee48390 | 2021-10-22 15:47:21 +0200 | [diff] [blame] | 230 | gd->env_valid = ENV_INVALID; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 231 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 232 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 233 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 234 | |
| 235 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 236 | static int env_flash_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 237 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 238 | env_t env_new; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 239 | int rc = 1; |
Tom Rini | 3fa1981 | 2014-04-17 16:20:39 -0400 | [diff] [blame] | 240 | char *saved_data = NULL; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 241 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 242 | ulong up_data = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 243 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 244 | up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 245 | debug("Data to save 0x%lx\n", up_data); |
| 246 | if (up_data) { |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 247 | saved_data = malloc(up_data); |
| 248 | if (saved_data == NULL) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 249 | printf("Unable to save the rest of sector (%ld)\n", |
| 250 | up_data); |
| 251 | goto done; |
| 252 | } |
| 253 | memcpy(saved_data, |
| 254 | (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data); |
| 255 | debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n", |
| 256 | (ulong)flash_addr + CONFIG_ENV_SIZE, |
| 257 | up_data, |
| 258 | (ulong)saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 259 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 260 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 261 | |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 262 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 263 | |
| 264 | if (flash_sect_protect(0, (long)flash_addr, end_addr)) |
| 265 | goto done; |
| 266 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 267 | rc = env_export(&env_new); |
| 268 | if (rc) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 269 | goto done; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 270 | |
| 271 | puts("Erasing Flash..."); |
| 272 | if (flash_sect_erase((long)flash_addr, end_addr)) |
| 273 | goto done; |
| 274 | |
| 275 | puts("Writing to Flash... "); |
| 276 | rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE); |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 277 | if (rc != 0) |
| 278 | goto perror; |
| 279 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 280 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 281 | if (up_data) { /* restore the rest of sector */ |
| 282 | debug("Restoring the rest of data to 0x%lx len 0x%lx\n", |
| 283 | (ulong)flash_addr + CONFIG_ENV_SIZE, up_data); |
| 284 | if (flash_write(saved_data, |
| 285 | (long)flash_addr + CONFIG_ENV_SIZE, |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 286 | up_data)) |
| 287 | goto perror; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 288 | } |
| 289 | #endif |
| 290 | puts("done\n"); |
| 291 | rc = 0; |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 292 | goto done; |
| 293 | perror: |
| 294 | flash_perror(rc); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 295 | done: |
Heinrich Schuchardt | 28ab12a | 2021-10-26 00:41:59 +0200 | [diff] [blame] | 296 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 297 | /* try to re-protect */ |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 298 | flash_sect_protect(1, (long)flash_addr, end_addr); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 299 | return rc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 300 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 301 | #endif /* CMD_SAVEENV */ |
| 302 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 303 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 304 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 305 | #ifdef LOADENV |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 306 | static int env_flash_load(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 307 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 308 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 309 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 310 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 311 | ulong ltmp = end_addr; |
| 312 | |
| 313 | flash_addr = flash_addr_new; |
| 314 | flash_addr_new = etmp; |
| 315 | |
| 316 | end_addr = end_addr_new; |
| 317 | end_addr_new = ltmp; |
| 318 | } |
| 319 | |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 320 | if (flash_addr_new->flags != ENV_REDUND_OBSOLETE && |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 321 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) { |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 322 | char flag = ENV_REDUND_OBSOLETE; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 323 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 324 | gd->env_valid = ENV_REDUND; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 325 | flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 326 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 327 | (ulong)&(flash_addr_new->flags), |
| 328 | sizeof(flash_addr_new->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 329 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Simon Glass | d3716dd | 2019-08-01 09:47:08 -0600 | [diff] [blame] | 332 | if (flash_addr->flags != ENV_REDUND_ACTIVE && |
| 333 | (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) { |
| 334 | char flag = ENV_REDUND_ACTIVE; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 335 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 336 | gd->env_valid = ENV_REDUND; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 337 | flash_sect_protect(0, (ulong)flash_addr, end_addr); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 338 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 339 | (ulong)&(flash_addr->flags), |
| 340 | sizeof(flash_addr->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 341 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 344 | if (gd->env_valid == ENV_REDUND) |
Igor Grinberg | 82b54b9 | 2011-11-07 01:14:09 +0000 | [diff] [blame] | 345 | puts("*** Warning - some problems detected " |
| 346 | "reading environment; recovered successfully\n\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 347 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 348 | |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 349 | return env_import((char *)flash_addr, 1, H_EXTERNAL); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 350 | } |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 351 | #endif /* LOADENV */ |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 352 | |
| 353 | U_BOOT_ENV_LOCATION(flash) = { |
| 354 | .location = ENVL_FLASH, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 355 | ENV_NAME("Flash") |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 356 | #ifdef LOADENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 357 | .load = env_flash_load, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 358 | #endif |
| 359 | #ifdef CMD_SAVEENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 360 | .save = env_save_ptr(env_flash_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 361 | #endif |
| 362 | #ifdef INITENV |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 363 | .init = env_flash_init, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 364 | #endif |
| 365 | }; |