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