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