Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | 2b74433 | 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 | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | |
| 9 | #include <command.h> |
| 10 | #include <environment.h> |
| 11 | #include <errno.h> |
| 12 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 13 | #include <memalign.h> |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 14 | #include <search.h> |
| 15 | #include <ubi_uboot.h> |
| 16 | #undef crc32 |
| 17 | |
Hamish Guthrie | 985186d | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 18 | #define _QUOTE(x) #x |
| 19 | #define QUOTE(x) _QUOTE(x) |
| 20 | |
| 21 | #if (CONFIG_ENV_UBI_VID_OFFSET == 0) |
| 22 | #define UBI_VID_OFFSET NULL |
| 23 | #else |
| 24 | #define UBI_VID_OFFSET QUOTE(CONFIG_ENV_UBI_VID_OFFSET) |
| 25 | #endif |
| 26 | |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 29 | #ifdef CONFIG_CMD_SAVEENV |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 30 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 31 | static int env_ubi_save(void) |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 32 | { |
| 33 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 34 | int ret; |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 35 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 36 | ret = env_export(env_new); |
| 37 | if (ret) |
| 38 | return ret; |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 39 | |
Hamish Guthrie | 985186d | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 40 | if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) { |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 41 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 42 | CONFIG_ENV_UBI_PART); |
| 43 | return 1; |
| 44 | } |
| 45 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 46 | if (gd->env_valid == ENV_VALID) { |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 47 | puts("Writing to redundant UBI... "); |
| 48 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND, |
| 49 | (void *)env_new, CONFIG_ENV_SIZE)) { |
| 50 | printf("\n** Unable to write env to %s:%s **\n", |
| 51 | CONFIG_ENV_UBI_PART, |
| 52 | CONFIG_ENV_UBI_VOLUME_REDUND); |
| 53 | return 1; |
| 54 | } |
| 55 | } else { |
| 56 | puts("Writing to UBI... "); |
| 57 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, |
| 58 | (void *)env_new, CONFIG_ENV_SIZE)) { |
| 59 | printf("\n** Unable to write env to %s:%s **\n", |
| 60 | CONFIG_ENV_UBI_PART, |
| 61 | CONFIG_ENV_UBI_VOLUME); |
| 62 | return 1; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | puts("done\n"); |
| 67 | |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 68 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 73 | static int env_ubi_save(void) |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 74 | { |
| 75 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 76 | int ret; |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 77 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 78 | ret = env_export(env_new); |
| 79 | if (ret) |
| 80 | return ret; |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 81 | |
Hamish Guthrie | 985186d | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 82 | if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) { |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 83 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 84 | CONFIG_ENV_UBI_PART); |
| 85 | return 1; |
| 86 | } |
| 87 | |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 88 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, |
| 89 | CONFIG_ENV_SIZE)) { |
| 90 | printf("\n** Unable to write env to %s:%s **\n", |
| 91 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | puts("done\n"); |
| 96 | return 0; |
| 97 | } |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 98 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 99 | #endif /* CONFIG_CMD_SAVEENV */ |
| 100 | |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 101 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 102 | static int env_ubi_load(void) |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 103 | { |
| 104 | ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE); |
| 105 | ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE); |
Simon Goldschmidt | 31f044b | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 106 | int read1_fail, read2_fail; |
Fiach Antaw | 9d364af | 2017-01-25 18:53:12 +1000 | [diff] [blame] | 107 | env_t *tmp_env1, *tmp_env2; |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 108 | |
Marcin Niestroj | c1f51e0 | 2016-05-24 14:59:55 +0200 | [diff] [blame] | 109 | /* |
| 110 | * In case we have restarted u-boot there is a chance that buffer |
| 111 | * contains old environment (from the previous boot). |
| 112 | * If UBI volume is zero size, ubi_volume_read() doesn't modify the |
| 113 | * buffer. |
| 114 | * We need to clear buffer manually here, so the invalid CRC will |
| 115 | * cause setting default environment as expected. |
| 116 | */ |
| 117 | memset(env1_buf, 0x0, CONFIG_ENV_SIZE); |
| 118 | memset(env2_buf, 0x0, CONFIG_ENV_SIZE); |
| 119 | |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 120 | tmp_env1 = (env_t *)env1_buf; |
| 121 | tmp_env2 = (env_t *)env2_buf; |
| 122 | |
Hamish Guthrie | 985186d | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 123 | if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) { |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 124 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 125 | CONFIG_ENV_UBI_PART); |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 126 | set_default_env(NULL, 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 127 | return -EIO; |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Simon Goldschmidt | 31f044b | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 130 | read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1, |
| 131 | CONFIG_ENV_SIZE); |
| 132 | if (read1_fail) |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 133 | printf("\n** Unable to read env from %s:%s **\n", |
| 134 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 135 | |
Simon Goldschmidt | 31f044b | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 136 | read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, |
| 137 | (void *)tmp_env2, CONFIG_ENV_SIZE); |
| 138 | if (read2_fail) |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 139 | printf("\n** Unable to read redundant env from %s:%s **\n", |
| 140 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND); |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 141 | |
Simon Goldschmidt | 31f044b | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 142 | return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, |
| 143 | read2_fail); |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 144 | } |
| 145 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 146 | static int env_ubi_load(void) |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 147 | { |
| 148 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
| 149 | |
Marcin Niestroj | c1f51e0 | 2016-05-24 14:59:55 +0200 | [diff] [blame] | 150 | /* |
| 151 | * In case we have restarted u-boot there is a chance that buffer |
| 152 | * contains old environment (from the previous boot). |
| 153 | * If UBI volume is zero size, ubi_volume_read() doesn't modify the |
| 154 | * buffer. |
| 155 | * We need to clear buffer manually here, so the invalid CRC will |
| 156 | * cause setting default environment as expected. |
| 157 | */ |
| 158 | memset(buf, 0x0, CONFIG_ENV_SIZE); |
| 159 | |
Hamish Guthrie | 985186d | 2019-05-15 15:15:55 +0200 | [diff] [blame] | 160 | if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) { |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 161 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 162 | CONFIG_ENV_UBI_PART); |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 163 | set_default_env(NULL, 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 164 | return -EIO; |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Kevin Smith | a7c06cd | 2015-10-23 17:51:47 +0000 | [diff] [blame] | 167 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) { |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 168 | printf("\n** Unable to read env from %s:%s **\n", |
| 169 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 170 | set_default_env(NULL, 0); |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 171 | return -EIO; |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Simon Goldschmidt | 2166ebf | 2018-01-31 14:47:12 +0100 | [diff] [blame] | 174 | return env_import(buf, 1); |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 175 | } |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 176 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 177 | |
| 178 | U_BOOT_ENV_LOCATION(ubi) = { |
| 179 | .location = ENVL_UBI, |
Marek Vasut | 344ca79 | 2018-08-21 15:53:33 +0200 | [diff] [blame] | 180 | ENV_NAME("UBI") |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 181 | .load = env_ubi_load, |
| 182 | .save = env_save_ptr(env_ubi_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 183 | }; |