Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (c) Copyright 2012 by National Instruments, |
| 4 | * Joe Hershberger <joe.hershberger@ni.com> |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 7 | #include <asm/global_data.h> |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 8 | |
| 9 | #include <command.h> |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 11 | #include <env_internal.h> |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 12 | #include <errno.h> |
| 13 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 14 | #include <memalign.h> |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 15 | #include <search.h> |
| 16 | #include <ubi_uboot.h> |
| 17 | #undef crc32 |
| 18 | |
Hamish Guthrie | 674ab24 | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 19 | #define _QUOTE(x) #x |
| 20 | #define QUOTE(x) _QUOTE(x) |
| 21 | |
| 22 | #if (CONFIG_ENV_UBI_VID_OFFSET == 0) |
| 23 | #define UBI_VID_OFFSET NULL |
| 24 | #else |
| 25 | #define UBI_VID_OFFSET QUOTE(CONFIG_ENV_UBI_VID_OFFSET) |
| 26 | #endif |
| 27 | |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Patrick Delaunay | 86d13f6 | 2022-12-14 16:51:31 +0100 | [diff] [blame] | 30 | #if CONFIG_SYS_REDUNDAND_ENVIRONMENT |
| 31 | #define ENV_UBI_VOLUME_REDUND CONFIG_ENV_UBI_VOLUME_REDUND |
| 32 | #else |
| 33 | #define ENV_UBI_VOLUME_REDUND "invalid" |
| 34 | #endif |
| 35 | |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 36 | #ifdef CONFIG_CMD_SAVEENV |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 37 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 38 | static int env_ubi_save(void) |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 39 | { |
| 40 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 41 | int ret; |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 42 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 43 | ret = env_export(env_new); |
| 44 | if (ret) |
| 45 | return ret; |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 46 | |
Hamish Guthrie | 674ab24 | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 47 | if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) { |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 48 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 49 | CONFIG_ENV_UBI_PART); |
| 50 | return 1; |
| 51 | } |
| 52 | |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 53 | if (gd->env_valid == ENV_VALID) { |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 54 | puts("Writing to redundant UBI... "); |
| 55 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND, |
Alexey Romanov | de417e9 | 2024-07-18 08:45:24 +0300 | [diff] [blame] | 56 | (void *)env_new, 0, CONFIG_ENV_SIZE)) { |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 57 | printf("\n** Unable to write env to %s:%s **\n", |
| 58 | CONFIG_ENV_UBI_PART, |
| 59 | CONFIG_ENV_UBI_VOLUME_REDUND); |
| 60 | return 1; |
| 61 | } |
| 62 | } else { |
| 63 | puts("Writing to UBI... "); |
| 64 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, |
Alexey Romanov | de417e9 | 2024-07-18 08:45:24 +0300 | [diff] [blame] | 65 | (void *)env_new, 0, CONFIG_ENV_SIZE)) { |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 66 | printf("\n** Unable to write env to %s:%s **\n", |
| 67 | CONFIG_ENV_UBI_PART, |
| 68 | CONFIG_ENV_UBI_VOLUME); |
| 69 | return 1; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | puts("done\n"); |
| 74 | |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 75 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 80 | static int env_ubi_save(void) |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 81 | { |
| 82 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 83 | int ret; |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 84 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 85 | ret = env_export(env_new); |
| 86 | if (ret) |
| 87 | return ret; |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 88 | |
Hamish Guthrie | 674ab24 | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 89 | if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) { |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 90 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 91 | CONFIG_ENV_UBI_PART); |
| 92 | return 1; |
| 93 | } |
| 94 | |
Alexey Romanov | de417e9 | 2024-07-18 08:45:24 +0300 | [diff] [blame] | 95 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, 0, |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 96 | CONFIG_ENV_SIZE)) { |
| 97 | printf("\n** Unable to write env to %s:%s **\n", |
| 98 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | puts("done\n"); |
| 103 | return 0; |
| 104 | } |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 105 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 106 | #endif /* CONFIG_CMD_SAVEENV */ |
| 107 | |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 108 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 109 | static int env_ubi_load(void) |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 110 | { |
| 111 | ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE); |
| 112 | ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE); |
Simon Goldschmidt | e07096e | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 113 | int read1_fail, read2_fail; |
Fiach Antaw | 0a2e176 | 2017-01-25 18:53:12 +1000 | [diff] [blame] | 114 | env_t *tmp_env1, *tmp_env2; |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 115 | |
Marcin Niestroj | bd04127 | 2016-05-24 14:59:55 +0200 | [diff] [blame] | 116 | /* |
| 117 | * In case we have restarted u-boot there is a chance that buffer |
| 118 | * contains old environment (from the previous boot). |
| 119 | * If UBI volume is zero size, ubi_volume_read() doesn't modify the |
| 120 | * buffer. |
| 121 | * We need to clear buffer manually here, so the invalid CRC will |
| 122 | * cause setting default environment as expected. |
| 123 | */ |
| 124 | memset(env1_buf, 0x0, CONFIG_ENV_SIZE); |
| 125 | memset(env2_buf, 0x0, CONFIG_ENV_SIZE); |
| 126 | |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 127 | tmp_env1 = (env_t *)env1_buf; |
| 128 | tmp_env2 = (env_t *)env2_buf; |
| 129 | |
Hamish Guthrie | 674ab24 | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 130 | if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) { |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 131 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 132 | CONFIG_ENV_UBI_PART); |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 133 | env_set_default(NULL, 0); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 134 | return -EIO; |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Alexey Romanov | 0043a97 | 2024-07-18 08:45:23 +0300 | [diff] [blame] | 137 | read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1, 0, |
Simon Goldschmidt | e07096e | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 138 | CONFIG_ENV_SIZE); |
| 139 | if (read1_fail) |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 140 | printf("\n** Unable to read env from %s:%s **\n", |
| 141 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 142 | |
Simon Goldschmidt | e07096e | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 143 | read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, |
Alexey Romanov | 0043a97 | 2024-07-18 08:45:23 +0300 | [diff] [blame] | 144 | (void *)tmp_env2, 0, CONFIG_ENV_SIZE); |
Simon Goldschmidt | e07096e | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 145 | if (read2_fail) |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 146 | printf("\n** Unable to read redundant env from %s:%s **\n", |
| 147 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 148 | |
Simon Goldschmidt | e07096e | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 149 | return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, |
Marek Vasut | dfe4b7e | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 150 | read2_fail, H_EXTERNAL); |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 151 | } |
| 152 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 153 | static int env_ubi_load(void) |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 154 | { |
| 155 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
| 156 | |
Marcin Niestroj | bd04127 | 2016-05-24 14:59:55 +0200 | [diff] [blame] | 157 | /* |
| 158 | * In case we have restarted u-boot there is a chance that buffer |
| 159 | * contains old environment (from the previous boot). |
| 160 | * If UBI volume is zero size, ubi_volume_read() doesn't modify the |
| 161 | * buffer. |
| 162 | * We need to clear buffer manually here, so the invalid CRC will |
| 163 | * cause setting default environment as expected. |
| 164 | */ |
| 165 | memset(buf, 0x0, CONFIG_ENV_SIZE); |
| 166 | |
Hamish Guthrie | 674ab24 | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 167 | if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) { |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 168 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 169 | CONFIG_ENV_UBI_PART); |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 170 | env_set_default(NULL, 0); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 171 | return -EIO; |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Alexey Romanov | 0043a97 | 2024-07-18 08:45:23 +0300 | [diff] [blame] | 174 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, 0, CONFIG_ENV_SIZE)) { |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 175 | printf("\n** Unable to read env from %s:%s **\n", |
| 176 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
Simon Glass | 9738586 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 177 | env_set_default(NULL, 0); |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 178 | return -EIO; |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Marek Vasut | dfe4b7e | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 181 | return env_import(buf, 1, H_EXTERNAL); |
Joe Hershberger | 0c5faa8 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 182 | } |
Joe Hershberger | db14e86 | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 183 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 184 | |
Patrick Delaunay | 86d13f6 | 2022-12-14 16:51:31 +0100 | [diff] [blame] | 185 | static int env_ubi_erase(void) |
| 186 | { |
| 187 | ALLOC_CACHE_ALIGN_BUFFER(char, env_buf, CONFIG_ENV_SIZE); |
| 188 | int ret = 0; |
| 189 | |
| 190 | if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) { |
| 191 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 192 | CONFIG_ENV_UBI_PART); |
| 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | memset(env_buf, 0x0, CONFIG_ENV_SIZE); |
| 197 | |
| 198 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, |
Alexey Romanov | de417e9 | 2024-07-18 08:45:24 +0300 | [diff] [blame] | 199 | (void *)env_buf, 0, CONFIG_ENV_SIZE)) { |
Patrick Delaunay | 86d13f6 | 2022-12-14 16:51:31 +0100 | [diff] [blame] | 200 | printf("\n** Unable to erase env to %s:%s **\n", |
| 201 | CONFIG_ENV_UBI_PART, |
| 202 | CONFIG_ENV_UBI_VOLUME); |
| 203 | ret = 1; |
| 204 | } |
| 205 | if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) { |
| 206 | if (ubi_volume_write(ENV_UBI_VOLUME_REDUND, |
Alexey Romanov | de417e9 | 2024-07-18 08:45:24 +0300 | [diff] [blame] | 207 | (void *)env_buf, 0, CONFIG_ENV_SIZE)) { |
Patrick Delaunay | 86d13f6 | 2022-12-14 16:51:31 +0100 | [diff] [blame] | 208 | printf("\n** Unable to erase env to %s:%s **\n", |
| 209 | CONFIG_ENV_UBI_PART, |
| 210 | ENV_UBI_VOLUME_REDUND); |
| 211 | ret = 1; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 218 | U_BOOT_ENV_LOCATION(ubi) = { |
| 219 | .location = ENVL_UBI, |
Marek Vasut | adeec30 | 2018-08-21 15:53:33 +0200 | [diff] [blame] | 220 | ENV_NAME("UBI") |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 221 | .load = env_ubi_load, |
| 222 | .save = env_save_ptr(env_ubi_save), |
Patrick Delaunay | 86d13f6 | 2022-12-14 16:51:31 +0100 | [diff] [blame] | 223 | .erase = ENV_ERASE_PTR(env_ubi_erase), |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 224 | }; |