Allow net_cleanup for loader.efi

While netbooting with loader.efi on at least one arm64 platform
which uses u-boot emulating UEFI, the kernel gets corrupted, we
suspected the u-boot ethernet driver was still running.

Use netdev.dv_cleanup for efinet_dev to address this.

This in turn requires calling dev_cleanup() before bi_load() to avoid
a loader crash since bi_load() calls ExitBootServices.

Reviewed by:	imp
Sponsored by:   Juniper Networks, Inc.
Differential Revision:	https://reviews.freebsd.org/D51186
This commit is contained in:
Simon J. Gerraty 2025-07-10 11:14:38 -07:00
parent 3fde39073c
commit d1f0ee548c
6 changed files with 33 additions and 17 deletions

View file

@ -256,6 +256,7 @@ efi_env_net_params(struct iodesc *desc)
rootip.s_addr = rootaddr;
#ifdef EFINET_DEBUG
printf("%s: proto=%d\n", __func__, netproto);
printf("%s: ip=%s\n", __func__, inet_ntoa(myip));
printf("%s: mask=%s\n", __func__, intoa(netmask));
printf("%s: gateway=%s\n", __func__, inet_ntoa(gateip));
@ -427,6 +428,7 @@ efinet_dev_init(void)
dif->dif_private = handles2[i];
}
efinet_dev.dv_cleanup = netdev.dv_cleanup;
efinet_dev.dv_open = netdev.dv_open;
efinet_dev.dv_close = netdev.dv_close;
efinet_dev.dv_strategy = netdev.dv_strategy;

View file

@ -209,6 +209,12 @@ elf64_exec(struct preloaded_file *fp)
trampoline, PT4);
printf("Start @ 0x%lx ...\n", ehdr->e_entry);
/*
* we have to cleanup here because net_cleanup() doesn't work after
* we call ExitBootServices
*/
dev_cleanup();
efi_time_fini();
err = bi_load(fp->f_args, &modulep, &kernend, true);
if (err != 0) {
@ -218,8 +224,6 @@ elf64_exec(struct preloaded_file *fp)
return (err);
}
dev_cleanup();
trampoline(trampstack, copy_staging == COPY_STAGING_ENABLE ?
efi_copy_finish : efi_copy_finish_nop, kernend, modulep,
PT4, ehdr->e_entry);

View file

@ -74,16 +74,17 @@ __elfN(arm_exec)(struct preloaded_file *fp)
printf("Kernel entry at %p...\n", entry);
printf("Kernel args: %s\n", fp->f_args);
/*
* we have to cleanup here because net_cleanup() doesn't work after
* we call ExitBootServices
*/
dev_cleanup();
if ((error = bi_load(fp->f_args, &modulep, &kernend, true)) != 0) {
efi_time_init();
return (error);
}
/* At this point we've called ExitBootServices, so we can't call
* printf or any other function that uses Boot Services */
dev_cleanup();
(*entry)((void *)modulep);
panic("exec returned");
}

View file

@ -69,6 +69,12 @@ elf64_exec(struct preloaded_file *fp)
ehdr = (Elf_Ehdr *)&(md->md_data);
entry = efi_translate(ehdr->e_entry);
/*
* we have to cleanup here because net_cleanup() doesn't work after
* we call ExitBootServices
*/
dev_cleanup();
efi_time_fini();
err = bi_load(fp->f_args, &modulep, &kernendp, true);
if (err != 0) {
@ -76,8 +82,6 @@ elf64_exec(struct preloaded_file *fp)
return (err);
}
dev_cleanup();
/* Clean D-cache under kernel area and invalidate whole I-cache */
clean_addr = (vm_offset_t)efi_translate(fp->f_addr);
clean_size = (vm_offset_t)efi_translate(kernendp) - clean_addr;

View file

@ -252,6 +252,13 @@ elf64_exec(struct preloaded_file *fp)
ehdr->e_entry
);
/*
* we have to cleanup here because net_cleanup() doesn't work after
* we call ExitBootServices
*/
dev_cleanup();
efi_time_fini();
err = bi_load(fp->f_args, &modulep, &kernend, true);
if (err != 0) {
@ -259,8 +266,6 @@ elf64_exec(struct preloaded_file *fp)
return (err);
}
dev_cleanup();
trampoline(trampstack, type == AllocateMaxAddress ? efi_copy_finish :
efi_copy_finish_nop, kernend, modulep, PT4, gdtr, ehdr->e_entry);

View file

@ -86,17 +86,17 @@ __elfN(exec)(struct preloaded_file *fp)
printf("Kernel entry at %p...\n", entry);
printf("Kernel args: %s\n", fp->f_args);
/*
* we have to cleanup here because net_cleanup() doesn't work after
* we call ExitBootServices
*/
dev_cleanup();
if ((error = bi_load(fp->f_args, &modulep, &kernend, true)) != 0) {
efi_time_init();
return (error);
}
/*
* At this point we've called ExitBootServices, so we can't call
* printf or any other function that uses Boot Services
*/
dev_cleanup();
(*entry)((void *)modulep);
panic("exec returned");
}