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;
36 #ifdef CONFIG_CMD_SAVEENV
37 static inline int write_env(struct blk_desc *sata, unsigned long size,
38 unsigned long offset, void *buffer)
40 uint blk_start, blk_cnt, n;
42 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
43 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
45 n = blk_dwrite(sata, blk_start, blk_cnt, buffer);
47 return (n == blk_cnt) ? 0 : -1;
50 static int env_sata_save(void)
52 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
53 struct blk_desc *sata = NULL;
56 if (sata_initialize())
59 env_sata = sata_get_env_dev();
61 sata = sata_get_dev(env_sata);
63 printf("Unknown SATA(%d) device for environment!\n",
68 ret = env_export(env_new);
72 printf("Writing to SATA(%d)...", env_sata);
73 if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, &env_new)) {
81 #endif /* CONFIG_CMD_SAVEENV */
83 static inline int read_env(struct blk_desc *sata, unsigned long size,
84 unsigned long offset, void *buffer)
86 uint blk_start, blk_cnt, n;
88 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
89 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
91 n = blk_dread(sata, blk_start, blk_cnt, buffer);
93 return (n == blk_cnt) ? 0 : -1;
96 static void env_sata_load(void)
98 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
99 struct blk_desc *sata = NULL;
102 if (sata_initialize())
105 env_sata = sata_get_env_dev();
107 sata = sata_get_dev(env_sata);
109 printf("Unknown SATA(%d) device for environment!\n",
114 if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf))
115 return set_default_env(NULL);
120 U_BOOT_ENV_LOCATION(sata) = {
121 .location = ENVL_ESATA,
122 .load = env_sata_load,
123 .save = env_save_ptr(env_sata_save),