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