[U-Boot] [PATCH V3] common: env: support sata device

Introduce env support for sata device. 1. Implement write_env/read_env/env_relocate_spec/saveenv/sata_get_env_dev 2. If want to enable this feature, define CONFIG_ENV_IS_IN_SATA, and define CONFIG_SYS_SATA_ENV_DEV or implement your own sata_get_ev_dev.
Signed-off-by: Peng Fan van.freenix@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Joe Hershberger joe.hershberger@ni.com Cc: Bin Meng bmeng.cn@gmail.com Cc: Stefan Roese sr@denx.de Cc: Heiko Schocher hs@denx.de Cc: Stuart Longland stuartl@vrt.com.au Cc: Maxime Ripard maxime.ripard@free-electrons.com Cc: Tom Rini trini@konsulko.com ---
V3: Discard the ENV_IS_EMBEDDED test which was missed to removed in V2.
V2: Add error msg when ENV REDUND, since not supported now. Discard ent_ptr Use blk_dread/blk_dwrite
cmd/nvedit.c | 3 +- common/Makefile | 1 + common/env_sata.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 common/env_sata.c
diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 9cf884e..b67563b 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -49,12 +49,13 @@ DECLARE_GLOBAL_DATA_PTR; !defined(CONFIG_ENV_IS_IN_NAND) && \ !defined(CONFIG_ENV_IS_IN_NVRAM) && \ !defined(CONFIG_ENV_IS_IN_ONENAND) && \ + !defined(CONFIG_ENV_IS_IN_SATA) && \ !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \ !defined(CONFIG_ENV_IS_IN_REMOTE) && \ !defined(CONFIG_ENV_IS_IN_UBI) && \ !defined(CONFIG_ENV_IS_NOWHERE) # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\ -SPI_FLASH|NVRAM|MMC|FAT|EXT4|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE +SATA|SPI_FLASH|NVRAM|MMC|FAT|EXT4|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE #endif
/* diff --git a/common/Makefile b/common/Makefile index 9a4b817..b23f312 100644 --- a/common/Makefile +++ b/common/Makefile @@ -50,6 +50,7 @@ obj-$(CONFIG_ENV_IS_IN_EXT4) += env_ext4.o obj-$(CONFIG_ENV_IS_IN_NAND) += env_nand.o obj-$(CONFIG_ENV_IS_IN_NVRAM) += env_nvram.o obj-$(CONFIG_ENV_IS_IN_ONENAND) += env_onenand.o +obj-$(CONFIG_ENV_IS_IN_SATA) += env_sata.o obj-$(CONFIG_ENV_IS_IN_SPI_FLASH) += env_sf.o obj-$(CONFIG_ENV_IS_IN_REMOTE) += env_remote.o obj-$(CONFIG_ENV_IS_IN_UBI) += env_ubi.o diff --git a/common/env_sata.c b/common/env_sata.c new file mode 100644 index 0000000..b0cee35 --- /dev/null +++ b/common/env_sata.c @@ -0,0 +1,127 @@ +/* + * (C) Copyright 2010-2016 Freescale Semiconductor, Inc. + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +/* #define DEBUG */ + +#include <common.h> + +#include <command.h> +#include <environment.h> +#include <linux/stddef.h> +#include <errno.h> +#include <memalign.h> +#include <sata.h> +#include <search.h> + +#if defined(CONFIG_ENV_SIZE_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND) +#error ENV REDUND not supported +#endif + +#if !defined(CONFIG_ENV_OFFSET) || !defined(CONFIG_ENV_SIZE) +#error CONFIG_ENV_OFFSET or CONFIG_ENV_SIZE not defined +#endif + +char *env_name_spec = "SATA"; + +DECLARE_GLOBAL_DATA_PTR; + +__weak int sata_get_env_dev(void) +{ + return CONFIG_SYS_SATA_ENV_DEV; +} + +int env_init(void) +{ + /* use default */ + gd->env_addr = (ulong)&default_environment[0]; + gd->env_valid = 1; + + return 0; +} + +#ifdef CONFIG_CMD_SAVEENV +static inline int write_env(struct blk_desc *sata, unsigned long size, + unsigned long offset, void *buffer) +{ + uint blk_start, blk_cnt, n; + + blk_start = ALIGN(offset, sata->blksz) / sata->blksz; + blk_cnt = ALIGN(size, sata->blksz) / sata->blksz; + + n = blk_dwrite(sata, blk_start, blk_cnt, buffer); + + return (n == blk_cnt) ? 0 : -1; +} + +int saveenv(void) +{ + ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); + struct blk_desc *sata = NULL; + int env_sata, ret; + + if (sata_initialize()) + return 1; + + env_sata = sata_get_env_dev(); + + sata = sata_get_dev(env_sata); + if (sata == NULL) { + printf("Unknown SATA(%d) device for environment!\n", + env_sata); + return 1; + } + + ret = env_export(env_new); + if (ret) + return 1; + + printf("Writing to SATA(%d)...", env_sata); + if (write_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, &env_new)) { + puts("failed\n"); + return 1; + } + + puts("done\n"); + return 0; +} +#endif /* CONFIG_CMD_SAVEENV */ + +static inline int read_env(struct blk_desc *sata, unsigned long size, + unsigned long offset, void *buffer) +{ + uint blk_start, blk_cnt, n; + + blk_start = ALIGN(offset, sata->blksz) / sata->blksz; + blk_cnt = ALIGN(size, sata->blksz) / sata->blksz; + + n = blk_dread(sata, blk_start, blk_cnt, buffer); + + return (n == blk_cnt) ? 0 : -1; +} + +void env_relocate_spec(void) +{ + ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); + struct blk_desc *sata = NULL; + int env_sata; + + if (sata_initialize()) + return; + + env_sata = sata_get_env_dev(); + + sata = sata_get_dev(env_sata); + if (sata == NULL) { + printf("Unknown SATA(%d) device for environment!\n", + env_sata); + return; + } + + if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) + return set_default_env(NULL); + + env_import(buf, 1); +}

On Sun, Apr 03, 2016 at 09:52:13PM +0800, Peng Fan wrote:
Introduce env support for sata device.
- Implement write_env/read_env/env_relocate_spec/saveenv/sata_get_env_dev
- If want to enable this feature, define CONFIG_ENV_IS_IN_SATA, and define CONFIG_SYS_SATA_ENV_DEV or implement your own sata_get_ev_dev.
Signed-off-by: Peng Fan van.freenix@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Joe Hershberger joe.hershberger@ni.com Cc: Bin Meng bmeng.cn@gmail.com Cc: Stefan Roese sr@denx.de Cc: Heiko Schocher hs@denx.de Cc: Stuart Longland stuartl@vrt.com.au Cc: Maxime Ripard maxime.ripard@free-electrons.com Cc: Tom Rini trini@konsulko.com
Reviewed-by: Tom Rini trini@konsulko.com

On 8 April 2016 at 13:45, Tom Rini trini@konsulko.com wrote:
On Sun, Apr 03, 2016 at 09:52:13PM +0800, Peng Fan wrote:
Introduce env support for sata device.
- Implement write_env/read_env/env_relocate_spec/saveenv/sata_get_env_dev
- If want to enable this feature, define CONFIG_ENV_IS_IN_SATA, and define CONFIG_SYS_SATA_ENV_DEV or implement your own sata_get_ev_dev.
Signed-off-by: Peng Fan van.freenix@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Joe Hershberger joe.hershberger@ni.com Cc: Bin Meng bmeng.cn@gmail.com Cc: Stefan Roese sr@denx.de Cc: Heiko Schocher hs@denx.de Cc: Stuart Longland stuartl@vrt.com.au Cc: Maxime Ripard maxime.ripard@free-electrons.com Cc: Tom Rini trini@konsulko.com
Reviewed-by: Tom Rini trini@konsulko.com
-- Tom
Ugh, we really should move this to linker lists or driver model. Do we really want to make that conversion even harder by adding one more option?
Regards, Simon

On Sat, Apr 09, 2016 at 12:35:12PM -0600, Simon Glass wrote:
On 8 April 2016 at 13:45, Tom Rini trini@konsulko.com wrote:
On Sun, Apr 03, 2016 at 09:52:13PM +0800, Peng Fan wrote:
Introduce env support for sata device.
- Implement write_env/read_env/env_relocate_spec/saveenv/sata_get_env_dev
- If want to enable this feature, define CONFIG_ENV_IS_IN_SATA, and define CONFIG_SYS_SATA_ENV_DEV or implement your own sata_get_ev_dev.
Signed-off-by: Peng Fan van.freenix@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Joe Hershberger joe.hershberger@ni.com Cc: Bin Meng bmeng.cn@gmail.com Cc: Stefan Roese sr@denx.de Cc: Heiko Schocher hs@denx.de Cc: Stuart Longland stuartl@vrt.com.au Cc: Maxime Ripard maxime.ripard@free-electrons.com Cc: Tom Rini trini@konsulko.com
Reviewed-by: Tom Rini trini@konsulko.com
-- Tom
Ugh, we really should move this to linker lists or driver model. Do we really want to make that conversion even harder by adding one more option?
linker lists for env? But I'd put Kconfig'ing env at the top of my wish list for env changes before DM. This should be an easy conversion too :)

On Sun, Apr 03, 2016 at 09:52:13PM +0800, Peng Fan wrote:
Introduce env support for sata device.
- Implement write_env/read_env/env_relocate_spec/saveenv/sata_get_env_dev
- If want to enable this feature, define CONFIG_ENV_IS_IN_SATA, and define CONFIG_SYS_SATA_ENV_DEV or implement your own sata_get_ev_dev.
Signed-off-by: Peng Fan van.freenix@gmail.com Cc: Simon Glass sjg@chromium.org Cc: Joe Hershberger joe.hershberger@ni.com Cc: Bin Meng bmeng.cn@gmail.com Cc: Stefan Roese sr@denx.de Cc: Heiko Schocher hs@denx.de Cc: Stuart Longland stuartl@vrt.com.au Cc: Maxime Ripard maxime.ripard@free-electrons.com Cc: Tom Rini trini@konsulko.com Reviewed-by: Tom Rini trini@konsulko.com
Applied to u-boot/master, thanks!
participants (3)
-
Peng Fan
-
Simon Glass
-
Tom Rini