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 DECLARE_GLOBAL_DATA_PTR;
29 __weak int sata_get_env_dev(void)
31 return CONFIG_SYS_SATA_ENV_DEV;
34 #ifdef CONFIG_CMD_SAVEENV
35 static inline int write_env(struct blk_desc *sata, unsigned long size,
36 unsigned long offset, void *buffer)
38 uint blk_start, blk_cnt, n;
40 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
41 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
43 n = blk_dwrite(sata, blk_start, blk_cnt, buffer);
45 return (n == blk_cnt) ? 0 : -1;
48 static int env_sata_save(void)
50 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
51 struct blk_desc *sata = NULL;
54 if (sata_initialize())
57 env_sata = sata_get_env_dev();
59 sata = sata_get_dev(env_sata);
61 printf("Unknown SATA(%d) device for environment!\n",
66 ret = env_export(env_new);
70 printf("Writing to SATA(%d)...", env_sata);
71 if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, &env_new)) {
79 #endif /* CONFIG_CMD_SAVEENV */
81 static inline int read_env(struct blk_desc *sata, unsigned long size,
82 unsigned long offset, void *buffer)
84 uint blk_start, blk_cnt, n;
86 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
87 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
89 n = blk_dread(sata, blk_start, blk_cnt, buffer);
91 return (n == blk_cnt) ? 0 : -1;
94 static void env_sata_load(void)
96 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
97 struct blk_desc *sata = NULL;
100 if (sata_initialize())
103 env_sata = sata_get_env_dev();
105 sata = sata_get_dev(env_sata);
107 printf("Unknown SATA(%d) device for environment!\n", env_sata);
111 if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) {
112 set_default_env(NULL);
121 U_BOOT_ENV_LOCATION(sata) = {
122 .location = ENVL_ESATA,
124 .load = env_sata_load,
125 .save = env_save_ptr(env_sata_save),