Blame view
env/ubi.c
4.49 KB
2b74433f3
|
1 2 3 4 |
/* * (c) Copyright 2012 by National Instruments, * Joe Hershberger <joe.hershberger@ni.com> * |
3765b3e7b
|
5 |
* SPDX-License-Identifier: GPL-2.0+ |
2b74433f3
|
6 7 8 9 10 11 12 13 |
*/ #include <common.h> #include <command.h> #include <environment.h> #include <errno.h> #include <malloc.h> |
cf92e05c0
|
14 |
#include <memalign.h> |
2b74433f3
|
15 16 17 |
#include <search.h> #include <ubi_uboot.h> #undef crc32 |
2b74433f3
|
18 |
DECLARE_GLOBAL_DATA_PTR; |
2b74433f3
|
19 |
#ifdef CONFIG_CMD_SAVEENV |
785881f77
|
20 |
#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
e5bce247b
|
21 |
static int env_ubi_save(void) |
785881f77
|
22 23 |
{ ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
7ce1526ed
|
24 |
int ret; |
785881f77
|
25 |
|
7ce1526ed
|
26 27 28 |
ret = env_export(env_new); if (ret) return ret; |
785881f77
|
29 30 31 32 33 34 35 36 |
if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { printf(" ** Cannot find mtd partition \"%s\" ", CONFIG_ENV_UBI_PART); return 1; } |
203e94f6c
|
37 |
if (gd->env_valid == ENV_VALID) { |
785881f77
|
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
puts("Writing to redundant UBI... "); if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)env_new, CONFIG_ENV_SIZE)) { printf(" ** Unable to write env to %s:%s ** ", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND); return 1; } } else { puts("Writing to UBI... "); if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, CONFIG_ENV_SIZE)) { printf(" ** Unable to write env to %s:%s ** ", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); return 1; } } puts("done "); |
203e94f6c
|
63 |
gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
785881f77
|
64 65 66 67 |
return 0; } #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
e5bce247b
|
68 |
static int env_ubi_save(void) |
2b74433f3
|
69 70 |
{ ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
7ce1526ed
|
71 |
int ret; |
2b74433f3
|
72 |
|
7ce1526ed
|
73 74 75 |
ret = env_export(env_new); if (ret) return ret; |
2b74433f3
|
76 77 78 79 80 81 82 83 |
if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { printf(" ** Cannot find mtd partition \"%s\" ", CONFIG_ENV_UBI_PART); return 1; } |
2b74433f3
|
84 85 86 87 88 89 90 91 92 93 94 95 96 |
if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, CONFIG_ENV_SIZE)) { printf(" ** Unable to write env to %s:%s ** ", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); return 1; } puts("done "); return 0; } |
785881f77
|
97 |
#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
2b74433f3
|
98 |
#endif /* CONFIG_CMD_SAVEENV */ |
785881f77
|
99 |
#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
c59519919
|
100 |
static int env_ubi_load(void) |
785881f77
|
101 102 103 |
{ ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE); ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE); |
31f044bd9
|
104 |
int read1_fail, read2_fail; |
9d364af23
|
105 |
env_t *tmp_env1, *tmp_env2; |
785881f77
|
106 |
|
c1f51e0f3
|
107 108 109 110 111 112 113 114 115 116 |
/* * In case we have restarted u-boot there is a chance that buffer * contains old environment (from the previous boot). * If UBI volume is zero size, ubi_volume_read() doesn't modify the * buffer. * We need to clear buffer manually here, so the invalid CRC will * cause setting default environment as expected. */ memset(env1_buf, 0x0, CONFIG_ENV_SIZE); memset(env2_buf, 0x0, CONFIG_ENV_SIZE); |
785881f77
|
117 118 119 120 121 122 123 124 125 |
tmp_env1 = (env_t *)env1_buf; tmp_env2 = (env_t *)env2_buf; if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { printf(" ** Cannot find mtd partition \"%s\" ", CONFIG_ENV_UBI_PART); set_default_env(NULL); |
c59519919
|
126 |
return -EIO; |
785881f77
|
127 |
} |
31f044bd9
|
128 129 130 |
read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1, CONFIG_ENV_SIZE); if (read1_fail) |
785881f77
|
131 132 133 134 |
printf(" ** Unable to read env from %s:%s ** ", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); |
785881f77
|
135 |
|
31f044bd9
|
136 137 138 |
read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2, CONFIG_ENV_SIZE); if (read2_fail) |
785881f77
|
139 140 141 142 |
printf(" ** Unable to read redundant env from %s:%s ** ", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND); |
785881f77
|
143 |
|
31f044bd9
|
144 145 |
return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, read2_fail); |
785881f77
|
146 147 |
} #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
c59519919
|
148 |
static int env_ubi_load(void) |
2b74433f3
|
149 150 |
{ ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
c1f51e0f3
|
151 152 153 154 155 156 157 158 159 |
/* * In case we have restarted u-boot there is a chance that buffer * contains old environment (from the previous boot). * If UBI volume is zero size, ubi_volume_read() doesn't modify the * buffer. * We need to clear buffer manually here, so the invalid CRC will * cause setting default environment as expected. */ memset(buf, 0x0, CONFIG_ENV_SIZE); |
2b74433f3
|
160 161 162 163 164 165 |
if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { printf(" ** Cannot find mtd partition \"%s\" ", CONFIG_ENV_UBI_PART); set_default_env(NULL); |
c59519919
|
166 |
return -EIO; |
2b74433f3
|
167 |
} |
a7c06cd3a
|
168 |
if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) { |
2b74433f3
|
169 170 171 172 173 |
printf(" ** Unable to read env from %s:%s ** ", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); set_default_env(NULL); |
c59519919
|
174 |
return -EIO; |
2b74433f3
|
175 |
} |
2166ebf78
|
176 |
return env_import(buf, 1); |
2b74433f3
|
177 |
} |
785881f77
|
178 |
#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
4415f1d1f
|
179 180 181 |
U_BOOT_ENV_LOCATION(ubi) = { .location = ENVL_UBI, |
e5bce247b
|
182 183 |
.load = env_ubi_load, .save = env_save_ptr(env_ubi_save), |
4415f1d1f
|
184 |
}; |