All of lore.kernel.org
 help / color / mirror / Atom feed
From: AKASHI Takahiro <takahiro.akashi@linaro.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v5 05/19] env: fat: support multiple env contexts
Date: Thu,  5 Sep 2019 17:21:19 +0900	[thread overview]
Message-ID: <20190905082133.18996-6-takahiro.akashi@linaro.org> (raw)
In-Reply-To: <20190905082133.18996-1-takahiro.akashi@linaro.org>

To support multiple env contexts in this backing store driver,
all the necessary parameters to load/save context data with will
be contained in dedicated structure, env_fat_context.

All the contexts that want to support their own context must
provide its own Kconfig to fill in the parameters.

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
---
 env/fat.c     | 102 +++++++++++++++++++++++++++++++++++++-------------
 include/env.h |   2 +
 2 files changed, 78 insertions(+), 26 deletions(-)

diff --git a/env/fat.c b/env/fat.c
index 1836556f361d..0236c82849b3 100644
--- a/env/fat.c
+++ b/env/fat.c
@@ -31,25 +31,57 @@
 # endif
 #endif
 
+struct env_fat_context {
+	const char *interface;
+	const char *dev_and_part;
+	const char *file;
+};
+
+int env_fat_init_params(struct env_context *ctx, const char *interface,
+			const char *dev_part, const char *file)
+{
+	struct env_fat_context *params;
+
+	params = calloc(sizeof(*params), 1);
+	if (!params)
+		return -1;
+
+	params->interface = interface;
+	params->dev_and_part = dev_part;
+	params->file = file;
+	ctx->drv_params[ENVL_FAT] = params;
+
+	return 0;
+}
+
 #ifdef CMD_SAVEENV
-static int env_fat_save(void)
+static int env_fat_save(struct env_context *ctx)
 {
-	env_t __aligned(ARCH_DMA_MINALIGN) env_new;
+	env_hdr_t *env_new;
 	struct blk_desc *dev_desc = NULL;
 	disk_partition_t info;
 	int dev, part;
+	struct env_fat_context *params = ctx->drv_params[ENVL_FAT];
 	int err;
 	loff_t size;
 
-	err = env_export(&env_new);
+	if (!params)
+		return 1;
+
+	env_new = malloc_cache_aligned(sizeof(env_hdr_t) + ctx->env_size);
+	if (!env_new)
+		return 1;
+
+	err = env_export(ctx, env_new);
 	if (err)
-		return err;
+		goto out;
+	err = 1;
 
-	part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
-					CONFIG_ENV_FAT_DEVICE_AND_PART,
-					&dev_desc, &info, 1);
+	part = blk_get_device_part_str(params->interface,
+				       params->dev_and_part,
+				       &dev_desc, &info, 1);
 	if (part < 0)
-		return 1;
+		goto out;
 
 	dev = dev_desc->devnum;
 	if (fat_set_blk_dev(dev_desc, &info) != 0) {
@@ -58,43 +90,51 @@ static int env_fat_save(void)
 		 * will calling it. The missing \n is intentional.
 		 */
 		printf("Unable to use %s %d:%d... ",
-		       CONFIG_ENV_FAT_INTERFACE, dev, part);
-		return 1;
+		       params->interface, dev, part);
+		goto out;
 	}
 
-	err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
-			     &size);
+	err = file_fat_write(params->file, (void *)env_new, 0,
+			     sizeof(env_hdr_t) + ctx->env_size, &size);
 	if (err == -1) {
 		/*
 		 * This printf is embedded in the messages from env_save that
 		 * will calling it. The missing \n is intentional.
 		 */
 		printf("Unable to write \"%s\" from %s%d:%d... ",
-			CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
-		return 1;
+			params->file, params->interface, dev, part);
+		err = 1;
+		goto out;
 	}
+	err = 0;
+out:
+	free(env_new);
 
-	return 0;
+	return err;
 }
 #endif /* CMD_SAVEENV */
 
 #ifdef LOADENV
-static int env_fat_load(void)
+static int env_fat_load(struct env_context *ctx)
 {
-	ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
+	struct env_fat_context *params = ctx->drv_params[ENVL_FAT];
+	ALLOC_CACHE_ALIGN_BUFFER(char, buf, sizeof(env_hdr_t) + ctx->env_size);
 	struct blk_desc *dev_desc = NULL;
 	disk_partition_t info;
 	int dev, part;
 	int err;
 
+	if (!params)
+		return -ENODEV;
+
 #ifdef CONFIG_MMC
-	if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
+	if (!strcmp(params->interface, "mmc"))
 		mmc_initialize(NULL);
 #endif
 
-	part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
-					CONFIG_ENV_FAT_DEVICE_AND_PART,
-					&dev_desc, &info, 1);
+	part = blk_get_device_part_str(params->interface,
+				       params->dev_and_part,
+				       &dev_desc, &info, 1);
 	if (part < 0)
 		goto err_env_relocate;
 
@@ -105,30 +145,39 @@ static int env_fat_load(void)
 		 * will calling it. The missing \n is intentional.
 		 */
 		printf("Unable to use %s %d:%d... ",
-		       CONFIG_ENV_FAT_INTERFACE, dev, part);
+		       params->interface, dev, part);
 		goto err_env_relocate;
 	}
 
-	err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
+	err = file_fat_read(params->file, buf,
+			    sizeof(env_hdr_t) + ctx->env_size);
 	if (err == -1) {
 		/*
 		 * This printf is embedded in the messages from env_save that
 		 * will calling it. The missing \n is intentional.
 		 */
 		printf("Unable to read \"%s\" from %s%d:%d... ",
-			CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
+			params->file, params->interface, dev, part);
 		goto err_env_relocate;
 	}
 
-	return env_import(buf, 1);
+	return env_import(ctx, buf, 1);
 
 err_env_relocate:
-	env_set_default(NULL, 0);
+	env_set_default(ctx, NULL, 0);
 
 	return -EIO;
 }
 #endif /* LOADENV */
 
+static int env_fat_init(struct env_context *ctx)
+{
+	if (ctx->drv_init)
+		return ctx->drv_init(ctx, ENVL_FAT);
+
+	return -ENOENT;
+}
+
 U_BOOT_ENV_LOCATION(fat) = {
 	.location	= ENVL_FAT,
 	ENV_NAME("FAT")
@@ -138,4 +187,5 @@ U_BOOT_ENV_LOCATION(fat) = {
 #ifdef CMD_SAVEENV
 	.save		= env_save_ptr(env_fat_save),
 #endif
+	.init		= env_fat_init,
 };
diff --git a/include/env.h b/include/env.h
index 9b4b9debc36d..8045dda9f811 100644
--- a/include/env.h
+++ b/include/env.h
@@ -398,5 +398,7 @@ int env_flash_init_params(struct env_context *ctx,
 			  struct environment_hdr *flash_addr_new,
 			  ulong end_addr_new,
 			  ulong default_env_addr);
+int env_fat_init_params(struct env_context *ctx, const char *interface,
+			const char *dev_part, const char *file);
 
 #endif
-- 
2.21.0

  parent reply	other threads:[~2019-09-05  8:21 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-05  8:21 [U-Boot] [PATCH v5 00/19] efi_loader: non-volatile variables support AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 01/19] env: extend interfaces allowing for env contexts AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 02/19] env: define env context for U-Boot environment AKASHI Takahiro
2019-09-05 19:43   ` Heinrich Schuchardt
2019-09-06  0:41     ` AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 03/19] env: nowhere: rework with new env interfaces AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 04/19] env: flash: support multiple env contexts AKASHI Takahiro
2019-09-05  8:21 ` AKASHI Takahiro [this message]
2019-09-05  8:21 ` [U-Boot] [PATCH v5 06/19] hashtable: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 07/19] api: converted with new env interfaces AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 08/19] arch: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 09/19] board: " AKASHI Takahiro
2019-09-05 12:02   ` Lukasz Majewski
2019-09-06  0:34     ` AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 10/19] cmd: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 11/19] common: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 12/19] disk: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 13/19] drivers: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 14/19] fs: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 15/19] lib: converted with new env interfaces (except efi_loader) AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 16/19] net: converted with new env interfaces AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 17/19] post: " AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 18/19] env, efi_loader: define env context for UEFI variables AKASHI Takahiro
2019-09-05 19:37   ` Heinrich Schuchardt
2019-09-06  0:54     ` AKASHI Takahiro
2019-09-05  8:21 ` [U-Boot] [PATCH v5 19/19] efi_loader: variable: rework with new env interfaces AKASHI Takahiro
2019-09-05  8:31 ` [U-Boot] [PATCH v5 00/19] efi_loader: non-volatile variables support AKASHI Takahiro
2019-10-01  6:28 ` AKASHI Takahiro
2019-10-23  6:53   ` AKASHI Takahiro
2019-10-25  7:06     ` Wolfgang Denk
2019-10-25  7:56       ` AKASHI Takahiro
2019-10-25 13:25         ` Wolfgang Denk
2019-10-28  1:14           ` AKASHI Takahiro
2019-10-29 13:28             ` Wolfgang Denk
2019-11-01  6:04               ` AKASHI Takahiro
2019-11-04 16:00                 ` Wolfgang Denk
2019-11-04 16:16                   ` Tom Rini
2019-11-05  5:18                   ` AKASHI Takahiro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190905082133.18996-6-takahiro.akashi@linaro.org \
    --to=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.