2 * (C) Copyright 2010-2016 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <environment.h>
13 #include <linux/stddef.h>
19 #if defined(CONFIG_ENV_SIZE_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
20 #error ENV REDUND not supported
23 #if !defined(CONFIG_ENV_OFFSET) || !defined(CONFIG_ENV_SIZE)
24 #error CONFIG_ENV_OFFSET or CONFIG_ENV_SIZE not defined
27 char *env_name_spec = "SATA";
29 DECLARE_GLOBAL_DATA_PTR;
31 __weak int sata_get_env_dev(void)
33 return CONFIG_SYS_SATA_ENV_DEV;
39 gd->env_addr = (ulong)&default_environment[0];
45 #ifdef CONFIG_CMD_SAVEENV
46 static inline int write_env(struct blk_desc *sata, unsigned long size,
47 unsigned long offset, void *buffer)
49 uint blk_start, blk_cnt, n;
51 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
52 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
54 n = blk_dwrite(sata, blk_start, blk_cnt, buffer);
56 return (n == blk_cnt) ? 0 : -1;
61 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
62 struct blk_desc *sata = NULL;
65 if (sata_initialize())
68 env_sata = sata_get_env_dev();
70 sata = sata_get_dev(env_sata);
72 printf("Unknown SATA(%d) device for environment!\n",
77 ret = env_export(env_new);
81 printf("Writing to SATA(%d)...", env_sata);
82 if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, &env_new)) {
90 #endif /* CONFIG_CMD_SAVEENV */
92 static inline int read_env(struct blk_desc *sata, unsigned long size,
93 unsigned long offset, void *buffer)
95 uint blk_start, blk_cnt, n;
97 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
98 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
100 n = blk_dread(sata, blk_start, blk_cnt, buffer);
102 return (n == blk_cnt) ? 0 : -1;
105 void env_relocate_spec(void)
107 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
108 struct blk_desc *sata = NULL;
111 if (sata_initialize())
114 env_sata = sata_get_env_dev();
116 sata = sata_get_dev(env_sata);
118 printf("Unknown SATA(%d) device for environment!\n",
123 if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf))
124 return set_default_env(NULL);