1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2010-2016 Freescale Semiconductor, Inc.
11 #include <environment.h>
12 #include <linux/stddef.h>
18 #if defined(CONFIG_ENV_SIZE_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
19 #error ENV REDUND not supported
22 #if !defined(CONFIG_ENV_OFFSET) || !defined(CONFIG_ENV_SIZE)
23 #error CONFIG_ENV_OFFSET or CONFIG_ENV_SIZE not defined
26 __weak int sata_get_env_dev(void)
28 return CONFIG_SYS_SATA_ENV_DEV;
31 #ifdef CONFIG_CMD_SAVEENV
32 static inline int write_env(struct blk_desc *sata, unsigned long size,
33 unsigned long offset, void *buffer)
35 uint blk_start, blk_cnt, n;
37 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
38 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
40 n = blk_dwrite(sata, blk_start, blk_cnt, buffer);
42 return (n == blk_cnt) ? 0 : -1;
45 static int env_sata_save(void)
47 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
48 struct blk_desc *sata = NULL;
51 if (sata_initialize())
54 env_sata = sata_get_env_dev();
56 sata = sata_get_dev(env_sata);
58 printf("Unknown SATA(%d) device for environment!\n",
63 ret = env_export(env_new);
67 printf("Writing to SATA(%d)...", env_sata);
68 if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, &env_new)) {
76 #endif /* CONFIG_CMD_SAVEENV */
78 static inline int read_env(struct blk_desc *sata, unsigned long size,
79 unsigned long offset, void *buffer)
81 uint blk_start, blk_cnt, n;
83 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
84 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
86 n = blk_dread(sata, blk_start, blk_cnt, buffer);
88 return (n == blk_cnt) ? 0 : -1;
91 static void env_sata_load(void)
93 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
94 struct blk_desc *sata = NULL;
97 if (sata_initialize())
100 env_sata = sata_get_env_dev();
102 sata = sata_get_dev(env_sata);
104 printf("Unknown SATA(%d) device for environment!\n", env_sata);
108 if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) {
109 set_default_env(NULL, 0);
113 return env_import(buf, 1);
116 U_BOOT_ENV_LOCATION(sata) = {
117 .location = ENVL_ESATA,
119 .load = env_sata_load,
120 .save = env_save_ptr(env_sata_save),