Commit 82b54b972b0f114db03e0e3067108609ccbf517c

Authored by Igor Grinberg
Committed by Stefano Babic
1 parent eb58a7fc77

env: clean env_flash.c checkpatch and code style

Cleanup the env_flash.c checkpatch warnings, errors and coding style.

Signed-off-by: Igor Grinberg <grinberg@compulab.co.il>

Showing 1 changed file with 84 additions and 91 deletions Side-by-side Diff

... ... @@ -39,14 +39,15 @@
39 39 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
40 40 #define CMD_SAVEENV
41 41 #elif defined(CONFIG_ENV_ADDR_REDUND)
42   -#error Cannot use CONFIG_ENV_ADDR_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
  42 +#error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
43 43 #endif
44 44  
45   -#if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
  45 +#if defined(CONFIG_ENV_SIZE_REDUND) && \
  46 + (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
46 47 #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
47 48 #endif
48 49  
49   -char * env_name_spec = "Flash";
  50 +char *env_name_spec = "Flash";
50 51  
51 52 #ifdef ENV_IS_EMBEDDED
52 53 env_t *env_ptr = &environment;
... ... @@ -57,7 +58,6 @@
57 58  
58 59 env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
59 60 static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
60   -
61 61 #endif /* ENV_IS_EMBEDDED */
62 62  
63 63 #if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND)
64 64  
... ... @@ -75,12 +75,11 @@
75 75  
76 76 uchar env_get_char_spec(int index)
77 77 {
78   - return (*((uchar *)(gd->env_addr + index)));
  78 + return *((uchar *)(gd->env_addr + index));
79 79 }
80 80  
81 81 #ifdef CONFIG_ENV_ADDR_REDUND
82   -
83   -int env_init(void)
  82 +int env_init(void)
84 83 {
85 84 int crc1_ok = 0, crc2_ok = 0;
86 85  
87 86  
88 87  
89 88  
90 89  
91 90  
92 91  
... ... @@ -91,33 +90,34 @@
91 90 ulong addr1 = (ulong)&(flash_addr->data);
92 91 ulong addr2 = (ulong)&(flash_addr_new->data);
93 92  
94   - crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
95   - crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
  93 + crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
  94 + crc2_ok =
  95 + crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
96 96  
97   - if (crc1_ok && ! crc2_ok) {
98   - gd->env_addr = addr1;
99   - gd->env_valid = 1;
100   - } else if (! crc1_ok && crc2_ok) {
101   - gd->env_addr = addr2;
102   - gd->env_valid = 1;
103   - } else if (! crc1_ok && ! crc2_ok) {
104   - gd->env_addr = addr_default;
105   - gd->env_valid = 0;
  97 + if (crc1_ok && !crc2_ok) {
  98 + gd->env_addr = addr1;
  99 + gd->env_valid = 1;
  100 + } else if (!crc1_ok && crc2_ok) {
  101 + gd->env_addr = addr2;
  102 + gd->env_valid = 1;
  103 + } else if (!crc1_ok && !crc2_ok) {
  104 + gd->env_addr = addr_default;
  105 + gd->env_valid = 0;
106 106 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
107   - gd->env_addr = addr1;
108   - gd->env_valid = 1;
  107 + gd->env_addr = addr1;
  108 + gd->env_valid = 1;
109 109 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
110   - gd->env_addr = addr2;
111   - gd->env_valid = 1;
  110 + gd->env_addr = addr2;
  111 + gd->env_valid = 1;
112 112 } else if (flag1 == flag2) {
113   - gd->env_addr = addr1;
114   - gd->env_valid = 2;
  113 + gd->env_addr = addr1;
  114 + gd->env_valid = 2;
115 115 } else if (flag1 == 0xFF) {
116   - gd->env_addr = addr1;
117   - gd->env_valid = 2;
  116 + gd->env_addr = addr1;
  117 + gd->env_valid = 2;
118 118 } else if (flag2 == 0xFF) {
119   - gd->env_addr = addr2;
120   - gd->env_valid = 2;
  119 + gd->env_addr = addr2;
  120 + gd->env_valid = 2;
121 121 }
122 122  
123 123 return 0;
124 124  
125 125  
126 126  
127 127  
128 128  
129 129  
... ... @@ -128,27 +128,23 @@
128 128 {
129 129 env_t env_new;
130 130 ssize_t len;
131   - char *saved_data = NULL;
132   - char *res;
133   - int rc = 1;
  131 + char *res, *saved_data = NULL;
134 132 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
  133 + int rc = 1;
135 134 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
136 135 ulong up_data = 0;
137 136 #endif
138 137  
139   - debug("Protect off %08lX ... %08lX\n",
140   - (ulong)flash_addr, end_addr);
  138 + debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
141 139  
142   - if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) {
  140 + if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
143 141 goto done;
144   - }
145 142  
146 143 debug("Protect off %08lX ... %08lX\n",
147 144 (ulong)flash_addr_new, end_addr_new);
148 145  
149   - if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) {
  146 + if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
150 147 goto done;
151   - }
152 148  
153 149 res = (char *)&env_new.data;
154 150 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
155 151  
156 152  
157 153  
158 154  
159 155  
160 156  
161 157  
162 158  
163 159  
164 160  
... ... @@ -156,63 +152,61 @@
156 152 error("Cannot export environment: errno = %d\n", errno);
157 153 goto done;
158 154 }
159   - env_new.crc = crc32(0, env_new.data, ENV_SIZE);
160   - env_new.flags = new_flag;
  155 + env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  156 + env_new.flags = new_flag;
161 157  
162 158 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
163   - up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE));
  159 + up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
164 160 debug("Data to save 0x%lX\n", up_data);
165 161 if (up_data) {
166   - if ((saved_data = malloc(up_data)) == NULL) {
  162 + saved_data = malloc(up_data);
  163 + if (saved_data == NULL) {
167 164 printf("Unable to save the rest of sector (%ld)\n",
168 165 up_data);
169 166 goto done;
170 167 }
171 168 memcpy(saved_data,
172   - (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data);
  169 + (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
  170 + up_data);
173 171 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
174 172 (long)flash_addr_new + CONFIG_ENV_SIZE,
175 173 up_data, saved_data);
176 174 }
177 175 #endif
178 176 puts("Erasing Flash...");
179   - debug(" %08lX ... %08lX ...",
180   - (ulong)flash_addr_new, end_addr_new);
  177 + debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
181 178  
182   - if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) {
  179 + if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
183 180 goto done;
184   - }
185 181  
186 182 puts("Writing to Flash... ");
187 183 debug(" %08lX ... %08lX ...",
188 184 (ulong)&(flash_addr_new->data),
189   - sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
190   - if ((rc = flash_write((char *)&env_new,
191   - (ulong)flash_addr_new,
192   - sizeof(env_new))) ||
193   - (rc = flash_write(&flag,
194   - (ulong)&(flash_addr->flags),
195   - sizeof(flash_addr->flags))) ) {
196   - flash_perror(rc);
197   - goto done;
198   - }
  185 + sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
  186 + rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
  187 + sizeof(env_new));
  188 + if (rc)
  189 + goto perror;
199 190  
  191 + rc = flash_write(&flag, (ulong)&(flash_addr->flags),
  192 + sizeof(flash_addr->flags));
  193 + if (rc)
  194 + goto perror;
  195 +
200 196 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
201 197 if (up_data) { /* restore the rest of sector */
202 198 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
203 199 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
204 200 if (flash_write(saved_data,
205 201 (long)flash_addr_new + CONFIG_ENV_SIZE,
206   - up_data)) {
207   - flash_perror(rc);
208   - goto done;
209   - }
  202 + up_data))
  203 + goto perror;
210 204 }
211 205 #endif
212 206 puts("done\n");
213 207  
214 208 {
215   - env_t * etmp = flash_addr;
  209 + env_t *etmp = flash_addr;
216 210 ulong ltmp = end_addr;
217 211  
218 212 flash_addr = flash_addr_new;
219 213  
... ... @@ -223,12 +217,15 @@
223 217 }
224 218  
225 219 rc = 0;
  220 + goto done;
  221 +perror:
  222 + flash_perror(rc);
226 223 done:
227 224 if (saved_data)
228 225 free(saved_data);
229 226 /* try to re-protect */
230   - (void) flash_sect_protect(1, (ulong)flash_addr, end_addr);
231   - (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
  227 + flash_sect_protect(1, (ulong)flash_addr, end_addr);
  228 + flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
232 229  
233 230 return rc;
234 231 }
235 232  
236 233  
237 234  
238 235  
239 236  
240 237  
... ... @@ -236,35 +233,34 @@
236 233  
237 234 #else /* ! CONFIG_ENV_ADDR_REDUND */
238 235  
239   -int env_init(void)
  236 +int env_init(void)
240 237 {
241 238 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
242   - gd->env_addr = (ulong)&(env_ptr->data);
243   - gd->env_valid = 1;
244   - return(0);
  239 + gd->env_addr = (ulong)&(env_ptr->data);
  240 + gd->env_valid = 1;
  241 + return 0;
245 242 }
246 243  
247   - gd->env_addr = (ulong)&default_environment[0];
248   - gd->env_valid = 0;
  244 + gd->env_addr = (ulong)&default_environment[0];
  245 + gd->env_valid = 0;
249 246 return 0;
250 247 }
251 248  
252 249 #ifdef CMD_SAVEENV
253   -
254 250 int saveenv(void)
255 251 {
256 252 env_t env_new;
257 253 ssize_t len;
258 254 int rc = 1;
259   - char *res;
260   - char *saved_data = NULL;
  255 + char *res, *saved_data = NULL;
261 256 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
262 257 ulong up_data = 0;
263 258  
264   - up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE));
  259 + up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
265 260 debug("Data to save 0x%lx\n", up_data);
266 261 if (up_data) {
267   - if ((saved_data = malloc(up_data)) == NULL) {
  262 + saved_data = malloc(up_data);
  263 + if (saved_data == NULL) {
268 264 printf("Unable to save the rest of sector (%ld)\n",
269 265 up_data);
270 266 goto done;
... ... @@ -278,8 +274,7 @@
278 274 }
279 275 #endif /* CONFIG_ENV_SECT_SIZE */
280 276  
281   - debug("Protect off %08lX ... %08lX\n",
282   - (ulong)flash_addr, end_addr);
  277 + debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
283 278  
284 279 if (flash_sect_protect(0, (long)flash_addr, end_addr))
285 280 goto done;
286 281  
287 282  
288 283  
289 284  
... ... @@ -298,32 +293,31 @@
298 293  
299 294 puts("Writing to Flash... ");
300 295 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
301   - if (rc != 0) {
302   - flash_perror(rc);
303   - goto done;
304   - }
  296 + if (rc != 0)
  297 + goto perror;
  298 +
305 299 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
306 300 if (up_data) { /* restore the rest of sector */
307 301 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
308 302 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
309 303 if (flash_write(saved_data,
310 304 (long)flash_addr + CONFIG_ENV_SIZE,
311   - up_data)) {
312   - flash_perror(rc);
313   - goto done;
314   - }
  305 + up_data))
  306 + goto perror;
315 307 }
316 308 #endif
317 309 puts("done\n");
318 310 rc = 0;
  311 + goto done;
  312 +perror:
  313 + flash_perror(rc);
319 314 done:
320 315 if (saved_data)
321 316 free(saved_data);
322 317 /* try to re-protect */
323   - (void) flash_sect_protect(1, (long)flash_addr, end_addr);
  318 + flash_sect_protect(1, (long)flash_addr, end_addr);
324 319 return rc;
325 320 }
326   -
327 321 #endif /* CMD_SAVEENV */
328 322  
329 323 #endif /* CONFIG_ENV_ADDR_REDUND */
... ... @@ -343,8 +337,7 @@
343 337 }
344 338  
345 339 if (flash_addr_new->flags != OBSOLETE_FLAG &&
346   - crc32(0, flash_addr_new->data, ENV_SIZE) ==
347   - flash_addr_new->crc) {
  340 + crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
348 341 char flag = OBSOLETE_FLAG;
349 342  
350 343 gd->env_valid = 2;
... ... @@ -368,8 +361,8 @@
368 361 }
369 362  
370 363 if (gd->env_valid == 2)
371   - puts ("*** Warning - some problems detected "
372   - "reading environment; recovered successfully\n\n");
  364 + puts("*** Warning - some problems detected "
  365 + "reading environment; recovered successfully\n\n");
373 366 #endif /* CONFIG_ENV_ADDR_REDUND */
374 367  
375 368 env_import((char *)flash_addr, 1);