git: 91ac713b646d - main - stand/boot1.efi: Allow modules to add env variables

From: Warner Losh <imp_at_FreeBSD.org>
Date: Mon, 01 May 2023 21:04:11 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=91ac713b646de667ac149be7dfe06adba4ce55cc

commit 91ac713b646de667ac149be7dfe06adba4ce55cc
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2023-05-01 15:26:31 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-05-01 21:02:52 +0000

    stand/boot1.efi: Allow modules to add env variables
    
    Sometimes filesystem modules need to pass details of the state of the
    filesystem to later stages of a boot. Provide a generic method to do
    so. We'll add them after any env variables set in our config files.
    
    Sponsored by:           Netflix
    Reviewed by:            tsoome, kevans
    Differential Revision:  https://reviews.freebsd.org/D39407
---
 stand/efi/boot1/boot1.c       | 20 ++++++++++++++++++++
 stand/efi/boot1/boot_module.h |  3 +++
 2 files changed, 23 insertions(+)

diff --git a/stand/efi/boot1/boot1.c b/stand/efi/boot1/boot1.c
index 088821ecd1f8..2f9801c8ea92 100644
--- a/stand/efi/boot1/boot1.c
+++ b/stand/efi/boot1/boot1.c
@@ -96,6 +96,26 @@ try_boot(const boot_module_t *mod, dev_info_t *dev, void *loaderbuf, size_t load
 		buf = NULL;
 	}
 
+	/*
+	 * See if there's any env variables the module wants to set. If so,
+	 * append it to any config present.
+	 */
+	if (mod->extra_env != NULL) {
+		const char *env = mod->extra_env();
+		if (env != NULL) {
+			size_t newlen = cmdsize + strlen(env) + 1;
+
+			cmd = realloc(cmd, newlen);
+			if (cmd == NULL)
+				goto errout;
+			if (cmdsize > 0)
+				strlcat(cmd, " ", newlen);
+			strlcat(cmd, env, newlen);
+			cmdsize = strlen(cmd);
+			free(__DECONST(char *, env));
+		}
+	}
+
 	if ((status = BS->LoadImage(TRUE, IH, efi_devpath_last_node(dev->devpath),
 	    loaderbuf, loadersize, &loaderhandle)) != EFI_SUCCESS) {
 		printf("Failed to load image provided by %s, size: %zu, (%lu)\n",
diff --git a/stand/efi/boot1/boot_module.h b/stand/efi/boot1/boot_module.h
index 99046fb53577..4ecfa4c72a83 100644
--- a/stand/efi/boot1/boot_module.h
+++ b/stand/efi/boot1/boot_module.h
@@ -94,6 +94,9 @@ typedef struct boot_module_t
 
 	/* valid devices as found by probe. */
 	dev_info_t *(*devices)(void);
+
+	/* return any environment variables to pass to next stage */
+	const char *(*extra_env)(void);
 } boot_module_t;
 
 extern const boot_module_t *boot_modules[];