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]; |
| 29 | gd->env_valid = 1; |
| 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 |
| 36 | static unsigned char env_flags; |
| 37 | |
| 38 | int saveenv(void) |
| 39 | { |
| 40 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 41 | int ret; |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 42 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 43 | ret = env_export(env_new); |
| 44 | if (ret) |
| 45 | return ret; |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 46 | |
| 47 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { |
| 48 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 49 | CONFIG_ENV_UBI_PART); |
| 50 | return 1; |
| 51 | } |
| 52 | |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 53 | env_new->flags = ++env_flags; /* increase the serial */ |
| 54 | |
| 55 | if (gd->env_valid == 1) { |
| 56 | puts("Writing to redundant UBI... "); |
| 57 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND, |
| 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_REDUND); |
| 62 | return 1; |
| 63 | } |
| 64 | } else { |
| 65 | puts("Writing to UBI... "); |
| 66 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, |
| 67 | (void *)env_new, CONFIG_ENV_SIZE)) { |
| 68 | printf("\n** Unable to write env to %s:%s **\n", |
| 69 | CONFIG_ENV_UBI_PART, |
| 70 | CONFIG_ENV_UBI_VOLUME); |
| 71 | return 1; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | puts("done\n"); |
| 76 | |
| 77 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 82 | int saveenv(void) |
| 83 | { |
| 84 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 85 | int ret; |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 86 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 87 | ret = env_export(env_new); |
| 88 | if (ret) |
| 89 | return ret; |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 90 | |
| 91 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { |
| 92 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 93 | CONFIG_ENV_UBI_PART); |
| 94 | return 1; |
| 95 | } |
| 96 | |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 97 | if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, |
| 98 | CONFIG_ENV_SIZE)) { |
| 99 | printf("\n** Unable to write env to %s:%s **\n", |
| 100 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | puts("done\n"); |
| 105 | return 0; |
| 106 | } |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 107 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 108 | #endif /* CONFIG_CMD_SAVEENV */ |
| 109 | |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 110 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
| 111 | void env_relocate_spec(void) |
| 112 | { |
| 113 | ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE); |
| 114 | ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE); |
| 115 | int crc1_ok = 0, crc2_ok = 0; |
| 116 | env_t *ep, *tmp_env1, *tmp_env2; |
| 117 | |
| 118 | tmp_env1 = (env_t *)env1_buf; |
| 119 | tmp_env2 = (env_t *)env2_buf; |
| 120 | |
| 121 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { |
| 122 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 123 | CONFIG_ENV_UBI_PART); |
| 124 | set_default_env(NULL); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1, |
| 129 | CONFIG_ENV_SIZE)) { |
| 130 | printf("\n** Unable to read env from %s:%s **\n", |
| 131 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
| 132 | } |
| 133 | |
| 134 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2, |
| 135 | CONFIG_ENV_SIZE)) { |
| 136 | printf("\n** Unable to read redundant env from %s:%s **\n", |
| 137 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND); |
| 138 | } |
| 139 | |
| 140 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc; |
| 141 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc; |
| 142 | |
| 143 | if (!crc1_ok && !crc2_ok) { |
| 144 | set_default_env("!bad CRC"); |
| 145 | return; |
| 146 | } else if (crc1_ok && !crc2_ok) { |
| 147 | gd->env_valid = 1; |
| 148 | } else if (!crc1_ok && crc2_ok) { |
| 149 | gd->env_valid = 2; |
| 150 | } else { |
| 151 | /* both ok - check serial */ |
| 152 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 153 | gd->env_valid = 2; |
| 154 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 155 | gd->env_valid = 1; |
| 156 | else if (tmp_env1->flags > tmp_env2->flags) |
| 157 | gd->env_valid = 1; |
| 158 | else if (tmp_env2->flags > tmp_env1->flags) |
| 159 | gd->env_valid = 2; |
| 160 | else /* flags are equal - almost impossible */ |
| 161 | gd->env_valid = 1; |
| 162 | } |
| 163 | |
| 164 | if (gd->env_valid == 1) |
| 165 | ep = tmp_env1; |
| 166 | else |
| 167 | ep = tmp_env2; |
| 168 | |
| 169 | env_flags = ep->flags; |
| 170 | env_import((char *)ep, 0); |
| 171 | } |
| 172 | #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
Joe Hershberger | 2b74433 | 2013-04-08 10:32:51 +0000 | [diff] [blame] | 173 | void env_relocate_spec(void) |
| 174 | { |
| 175 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
| 176 | |
| 177 | if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { |
| 178 | printf("\n** Cannot find mtd partition \"%s\"\n", |
| 179 | CONFIG_ENV_UBI_PART); |
| 180 | set_default_env(NULL); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)&buf, |
| 185 | CONFIG_ENV_SIZE)) { |
| 186 | printf("\n** Unable to read env from %s:%s **\n", |
| 187 | CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
| 188 | set_default_env(NULL); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | env_import(buf, 1); |
| 193 | } |
Joe Hershberger | 785881f | 2013-04-08 10:32:52 +0000 | [diff] [blame] | 194 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |