env: Fix saving environment to "bad CRC" location
[oweals/u-boot.git] / env / remote.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2011-2012 Freescale Semiconductor, Inc.
4  */
5
6 /* #define DEBUG */
7
8 #include <common.h>
9 #include <command.h>
10 #include <environment.h>
11 #include <linux/stddef.h>
12
13 #ifdef ENV_IS_EMBEDDED
14 env_t *env_ptr = &environment;
15 #else /* ! ENV_IS_EMBEDDED */
16 env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
17 #endif /* ENV_IS_EMBEDDED */
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 #if !defined(CONFIG_ENV_OFFSET)
22 #define CONFIG_ENV_OFFSET 0
23 #endif
24
25 static int env_remote_init(void)
26 {
27         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
28                 gd->env_addr = (ulong)&(env_ptr->data);
29                 gd->env_valid = ENV_VALID;
30                 return 0;
31         }
32
33         return -ENOENT;
34 }
35
36 #ifdef CONFIG_CMD_SAVEENV
37 static int env_remote_save(void)
38 {
39 #ifdef CONFIG_SRIO_PCIE_BOOT_SLAVE
40         printf("Can not support the 'saveenv' when boot from SRIO or PCIE!\n");
41         return 1;
42 #else
43         return 0;
44 #endif
45 }
46 #endif /* CONFIG_CMD_SAVEENV */
47
48 static int env_remote_load(void)
49 {
50 #ifndef ENV_IS_EMBEDDED
51         return env_import((char *)env_ptr, 1);
52 #endif
53
54         return 0;
55 }
56
57 U_BOOT_ENV_LOCATION(remote) = {
58         .location       = ENVL_REMOTE,
59         ENV_NAME("Remote")
60         .load           = env_remote_load,
61         .save           = env_save_ptr(env_remote_save),
62         .init           = env_remote_init,
63 };