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