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