1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2010-2016 Freescale Semiconductor, Inc.
12 #include <env_internal.h>
13 #include <linux/stddef.h>
19 #if 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 __weak int sata_get_env_dev(void)
29 return CONFIG_SYS_SATA_ENV_DEV;
32 #ifdef CONFIG_CMD_SAVEENV
33 static inline int write_env(struct blk_desc *sata, unsigned long size,
34 unsigned long offset, void *buffer)
36 uint blk_start, blk_cnt, n;
38 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
39 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
41 n = blk_dwrite(sata, blk_start, blk_cnt, buffer);
43 return (n == blk_cnt) ? 0 : -1;
46 static int env_sata_save(void)
48 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
49 struct blk_desc *sata = NULL;
52 if (sata_initialize())
55 env_sata = sata_get_env_dev();
57 sata = sata_get_dev(env_sata);
59 printf("Unknown SATA(%d) device for environment!\n",
64 ret = env_export(env_new);
68 printf("Writing to SATA(%d)...", env_sata);
69 if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, (u_char *)env_new)) {
77 #endif /* CONFIG_CMD_SAVEENV */
79 static inline int read_env(struct blk_desc *sata, unsigned long size,
80 unsigned long offset, void *buffer)
82 uint blk_start, blk_cnt, n;
84 blk_start = ALIGN(offset, sata->blksz) / sata->blksz;
85 blk_cnt = ALIGN(size, sata->blksz) / sata->blksz;
87 n = blk_dread(sata, blk_start, blk_cnt, buffer);
89 return (n == blk_cnt) ? 0 : -1;
92 static void env_sata_load(void)
94 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
95 struct blk_desc *sata = NULL;
98 if (sata_initialize())
101 env_sata = sata_get_env_dev();
103 sata = sata_get_dev(env_sata);
105 printf("Unknown SATA(%d) device for environment!\n", env_sata);
109 if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) {
110 env_set_default(NULL, 0);
114 return env_import(buf, 1, H_EXTERNAL);
117 U_BOOT_ENV_LOCATION(sata) = {
118 .location = ENVL_ESATA,
120 .load = env_sata_load,
121 .save = env_save_ptr(env_sata_save),