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