git: fc352701ff3a - main - stand: collapse all copies of *copyenv into md_copyenv

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 16 Sep 2022 15:53:58 UTC
The branch main has been updated by imp:

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

commit fc352701ff3aeb0af22c0da17c4194cf1f8ad5d0
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-09-16 15:08:57 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-09-16 15:18:56 +0000

    stand: collapse all copies of *copyenv into md_copyenv
    
    Use the efi's bi_copyenv to md_copyenv and place it in modinfo.c. Remove
    all other nearly identical and efi's has the best error handling.
    
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D36574
---
 stand/common/metadata.c               | 28 ------------------------
 stand/common/modinfo.c                | 39 +++++++++++++++++++++++++++++++++
 stand/common/modinfo.h                |  1 +
 stand/efi/loader/bootinfo.c           | 41 +----------------------------------
 stand/i386/libi386/bootinfo.c         | 28 ------------------------
 stand/i386/libi386/bootinfo32.c       |  2 +-
 stand/i386/libi386/bootinfo64.c       |  2 +-
 stand/i386/libi386/libi386.h          |  1 -
 stand/userboot/userboot/bootinfo.c    | 28 ------------------------
 stand/userboot/userboot/bootinfo32.c  |  2 +-
 stand/userboot/userboot/bootinfo64.c  |  2 +-
 stand/userboot/userboot/libuserboot.h |  1 -
 12 files changed, 45 insertions(+), 130 deletions(-)

diff --git a/stand/common/metadata.c b/stand/common/metadata.c
index 37171ec3be99..fc8857ee089e 100644
--- a/stand/common/metadata.c
+++ b/stand/common/metadata.c
@@ -65,34 +65,6 @@ md_getboothowto(char *kargs)
     return(howto);
 }
 
-/*
- * Copy the environment into the load area starting at (addr).
- * Each variable is formatted as <name>=<value>, with a single nul
- * separating each variable, and a double nul terminating the environment.
- */
-static vm_offset_t
-md_copyenv(vm_offset_t addr)
-{
-    struct env_var	*ep;
-
-    /* traverse the environment */
-    for (ep = environ; ep != NULL; ep = ep->ev_next) {
-	archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
-	addr += strlen(ep->ev_name);
-	archsw.arch_copyin("=", addr, 1);
-	addr++;
-	if (ep->ev_value != NULL) {
-	    archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
-	    addr += strlen(ep->ev_value);
-	}
-	archsw.arch_copyin("", addr, 1);
-	addr++;
-    }
-    archsw.arch_copyin("", addr, 1);
-    addr++;
-    return(addr);
-}
-
 /*
  * Load the information expected by a kernel.
  *
diff --git a/stand/common/modinfo.c b/stand/common/modinfo.c
index 15116b3b9b78..0c00b2070274 100644
--- a/stand/common/modinfo.c
+++ b/stand/common/modinfo.c
@@ -148,3 +148,42 @@ md_copymodules(vm_offset_t addr, bool kern64)
 	MOD_END(addr, c);
 	return(addr);
 }
+
+/*
+ * Copy the environment into the load area starting at (addr).
+ * Each variable is formatted as <name>=<value>, with a single nul
+ * separating each variable, and a double nul terminating the environment.
+ */
+vm_offset_t
+md_copyenv(vm_offset_t start)
+{
+	struct env_var *ep;
+	vm_offset_t addr, last;
+	size_t len;
+
+	addr = last = start;
+
+	/* Traverse the environment. */
+	for (ep = environ; ep != NULL; ep = ep->ev_next) {
+		len = strlen(ep->ev_name);
+		if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
+			break;
+		addr += len;
+		if (archsw.arch_copyin("=", addr, 1) != 1)
+			break;
+		addr++;
+		if (ep->ev_value != NULL) {
+			len = strlen(ep->ev_value);
+			if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
+				break;
+			addr += len;
+		}
+		if (archsw.arch_copyin("", addr, 1) != 1)
+			break;
+		last = ++addr;
+	}
+
+	if (archsw.arch_copyin("", last++, 1) != 1)
+		last = start;
+	return(last);
+}
diff --git a/stand/common/modinfo.h b/stand/common/modinfo.h
index fc56173e1c51..e4739df0b68f 100644
--- a/stand/common/modinfo.h
+++ b/stand/common/modinfo.h
@@ -7,5 +7,6 @@
 #define COMMON_MODINFO_H
 
 vm_offset_t md_copymodules(vm_offset_t addr, bool kern64);
+vm_offset_t md_copyenv(vm_offset_t addr);
 
 #endif /* COMMON_MODINFO_H */
diff --git a/stand/efi/loader/bootinfo.c b/stand/efi/loader/bootinfo.c
index 9f56365536d1..558f7bdae91e 100644
--- a/stand/efi/loader/bootinfo.c
+++ b/stand/efi/loader/bootinfo.c
@@ -126,45 +126,6 @@ bi_getboothowto(char *kargs)
 	return (howto);
 }
 
-/*
- * Copy the environment into the load area starting at (addr).
- * Each variable is formatted as <name>=<value>, with a single nul
- * separating each variable, and a double nul terminating the environment.
- */
-static vm_offset_t
-bi_copyenv(vm_offset_t start)
-{
-	struct env_var *ep;
-	vm_offset_t addr, last;
-	size_t len;
-
-	addr = last = start;
-
-	/* Traverse the environment. */
-	for (ep = environ; ep != NULL; ep = ep->ev_next) {
-		len = strlen(ep->ev_name);
-		if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
-			break;
-		addr += len;
-		if (archsw.arch_copyin("=", addr, 1) != 1)
-			break;
-		addr++;
-		if (ep->ev_value != NULL) {
-			len = strlen(ep->ev_value);
-			if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
-				break;
-			addr += len;
-		}
-		if (archsw.arch_copyin("", addr, 1) != 1)
-			break;
-		last = ++addr;
-	}
-
-	if (archsw.arch_copyin("", last++, 1) != 1)
-		last = start;
-	return(last);
-}
-
 static EFI_STATUS
 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver)
 {
@@ -408,7 +369,7 @@ bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, bool exit_bs)
 
 	/* Copy our environment. */
 	envp = addr;
-	addr = bi_copyenv(addr);
+	addr = md_copyenv(addr);
 
 	/* Pad to a page boundary. */
 	addr = roundup(addr, PAGE_SIZE);
diff --git a/stand/i386/libi386/bootinfo.c b/stand/i386/libi386/bootinfo.c
index 84a0b3586a1c..92238d4fab44 100644
--- a/stand/i386/libi386/bootinfo.c
+++ b/stand/i386/libi386/bootinfo.c
@@ -103,31 +103,3 @@ bi_setboothowto(int howto)
 
     boot_howto_to_env(howto);
 }
-
-/*
- * Copy the environment into the load area starting at (addr).
- * Each variable is formatted as <name>=<value>, with a single nul
- * separating each variable, and a double nul terminating the environment.
- */
-vm_offset_t
-bi_copyenv(vm_offset_t addr)
-{
-    struct env_var	*ep;
-    
-    /* traverse the environment */
-    for (ep = environ; ep != NULL; ep = ep->ev_next) {
-	archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
-	addr += strlen(ep->ev_name);
-	archsw.arch_copyin("=", addr, 1);
-	addr++;
-	if (ep->ev_value != NULL) {
-	    archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
-	    addr += strlen(ep->ev_value);
-	}
-	archsw.arch_copyin("", addr, 1);
-	addr++;
-    }
-    archsw.arch_copyin("", addr, 1);
-    addr++;
-    return(addr);
-}
diff --git a/stand/i386/libi386/bootinfo32.c b/stand/i386/libi386/bootinfo32.c
index 965b62bbad81..9ca6a70d674e 100644
--- a/stand/i386/libi386/bootinfo32.c
+++ b/stand/i386/libi386/bootinfo32.c
@@ -126,7 +126,7 @@ bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t
 
     /* copy our environment */
     envp = addr;
-    addr = bi_copyenv(addr);
+    addr = md_copyenv(addr);
 
     /* pad to a page boundary */
     addr = roundup(addr, PAGE_SIZE);
diff --git a/stand/i386/libi386/bootinfo64.c b/stand/i386/libi386/bootinfo64.c
index 8b5eb8c1db93..bc8830d09098 100644
--- a/stand/i386/libi386/bootinfo64.c
+++ b/stand/i386/libi386/bootinfo64.c
@@ -167,7 +167,7 @@ bi_load64(char *args, vm_offset_t *modulep,
 
     /* copy our environment */
     envp = roundup(addr + size, PAGE_SIZE);
-    addr = bi_copyenv(envp);
+    addr = md_copyenv(envp);
 
     /* set kernend */
     kernend = roundup(addr, PAGE_SIZE);
diff --git a/stand/i386/libi386/libi386.h b/stand/i386/libi386/libi386.h
index 9c7a452a03ad..0115f30e31c7 100644
--- a/stand/i386/libi386/libi386.h
+++ b/stand/i386/libi386/libi386.h
@@ -146,7 +146,6 @@ int	i386_autoload(void);
 void	bi_load_vbe_data(struct preloaded_file *kfp);
 int	bi_getboothowto(char *kargs);
 void	bi_setboothowto(int howto);
-vm_offset_t	bi_copyenv(vm_offset_t addr);
 int	bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip,
 	    vm_offset_t *modulep, vm_offset_t *kernend);
 int	bi_load64(char *args, vm_offset_t *modulep,
diff --git a/stand/userboot/userboot/bootinfo.c b/stand/userboot/userboot/bootinfo.c
index 36a5e1982313..d77b2f3e51c7 100644
--- a/stand/userboot/userboot/bootinfo.c
+++ b/stand/userboot/userboot/bootinfo.c
@@ -83,31 +83,3 @@ bi_setboothowto(int howto)
 
     boot_howto_to_env(howto);
 }
-
-/*
- * Copy the environment into the load area starting at (addr).
- * Each variable is formatted as <name>=<value>, with a single nul
- * separating each variable, and a double nul terminating the environment.
- */
-vm_offset_t
-bi_copyenv(vm_offset_t addr)
-{
-    struct env_var	*ep;
-    
-    /* traverse the environment */
-    for (ep = environ; ep != NULL; ep = ep->ev_next) {
-        archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
-	addr += strlen(ep->ev_name);
-	archsw.arch_copyin("=", addr, 1);
-	addr++;
-	if (ep->ev_value != NULL) {
-            archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
-	    addr += strlen(ep->ev_value);
-	}
-	archsw.arch_copyin("", addr, 1);
-	addr++;
-    }
-    archsw.arch_copyin("", addr, 1);
-    addr++;
-    return(addr);
-}
diff --git a/stand/userboot/userboot/bootinfo32.c b/stand/userboot/userboot/bootinfo32.c
index 8203f90c98fe..08dde13baa30 100644
--- a/stand/userboot/userboot/bootinfo32.c
+++ b/stand/userboot/userboot/bootinfo32.c
@@ -106,7 +106,7 @@ bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t
 
     /* copy our environment */
     envp = addr;
-    addr = bi_copyenv(addr);
+    addr = md_copyenv(addr);
 
     /* pad to a page boundary */
     addr = roundup(addr, PAGE_SIZE);
diff --git a/stand/userboot/userboot/bootinfo64.c b/stand/userboot/userboot/bootinfo64.c
index 6d711cf51759..530ddaf768ed 100644
--- a/stand/userboot/userboot/bootinfo64.c
+++ b/stand/userboot/userboot/bootinfo64.c
@@ -138,7 +138,7 @@ bi_load64(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
 
     /* copy our environment */
     envp = addr;
-    addr = bi_copyenv(addr);
+    addr = md_copyenv(addr);
 
     /* pad to a page boundary */
     addr = roundup(addr, PAGE_SIZE);
diff --git a/stand/userboot/userboot/libuserboot.h b/stand/userboot/userboot/libuserboot.h
index eb3bada097e4..6d7dc0c3ff40 100644
--- a/stand/userboot/userboot/libuserboot.h
+++ b/stand/userboot/userboot/libuserboot.h
@@ -61,7 +61,6 @@ int	userboot_setcurrdev(struct env_var *ev, int flags, const void *value);
 
 int	bi_getboothowto(char *kargs);
 void	bi_setboothowto(int howto);
-vm_offset_t	bi_copyenv(vm_offset_t addr);
 int	bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip,
     vm_offset_t *modulep, vm_offset_t *kernend);
 int	bi_load64(char *args, vm_offset_t *modulep, vm_offset_t *kernend);