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
This commit is contained in:
Warner Losh 2022-09-16 09:08:57 -06:00
parent 2e6ed47a46
commit fc352701ff
12 changed files with 45 additions and 130 deletions

View File

@ -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.
*

View File

@ -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);
}

View File

@ -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 */

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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,

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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);