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