git: fbeb08184407 - stable/12 - boot1.efi: use malloc family from libsa

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Fri, 08 Oct 2021 06:10:55 UTC
The branch stable/12 has been updated by kevans:

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

commit fbeb08184407c1d8824790fd51410f9e8f200522
Author:     Toomas Soome <tsoome@FreeBSD.org>
AuthorDate: 2020-06-30 21:48:58 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2021-10-08 05:24:26 +0000

    boot1.efi: use malloc family from libsa
    
    The zfs reader development did reach to the point where linking boot1,
    we will get errors about duplicate symbols Malloc, Free, Calloc.
    
    We can just use libsa version, just as loader.efi does. The only concern is,
    libsa zalloc is using fixed size heap region, I did pick 64MB as other
    stage instances are using, but this size is likely not optimal. In any case,
    with limited memory setups, we should boot loader.efi directly.
    
    (cherry picked from commit 12c470af750672c4847af20cc8e2a736aab7db78)
---
 stand/efi/boot1/boot1.c | 52 +++++++++++++++----------------------------------
 1 file changed, 16 insertions(+), 36 deletions(-)

diff --git a/stand/efi/boot1/boot1.c b/stand/efi/boot1/boot1.c
index 22bff7f10fba..088821ecd1f8 100644
--- a/stand/efi/boot1/boot1.c
+++ b/stand/efi/boot1/boot1.c
@@ -53,42 +53,8 @@ static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
 static EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
 static EFI_GUID ConsoleControlGUID = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
 
-/*
- * Provide Malloc / Free / Calloc backed by EFIs AllocatePool / FreePool which ensures
- * memory is correctly aligned avoiding EFI_INVALID_PARAMETER returns from
- * EFI methods.
- */
-
-void *
-Malloc(size_t len, const char *file __unused, int line __unused)
-{
-	void *out;
-
-	if (BS->AllocatePool(EfiLoaderData, len, &out) == EFI_SUCCESS)
-		return (out);
-
-	return (NULL);
-}
-
-void
-Free(void *buf, const char *file __unused, int line __unused)
-{
-	if (buf != NULL)
-		(void)BS->FreePool(buf);
-}
-
-void *
-Calloc(size_t n1, size_t n2, const char *file, int line)
-{
-	size_t bytes;
-	void *res;
-
-	bytes = n1 * n2;
-	if ((res = Malloc(bytes, file, line)) != NULL)
-		bzero(res, bytes);
-
-	return (res);
-}
+static EFI_PHYSICAL_ADDRESS heap;
+static UINTN heapsize;
 
 /*
  * try_boot only returns if it fails to load the loader. If it succeeds
@@ -201,6 +167,18 @@ efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE *Xsystab)
 	BS = ST->BootServices;
 	RS = ST->RuntimeServices;
 
+	heapsize = 64 * 1024 * 1024;
+	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
+	    EFI_SIZE_TO_PAGES(heapsize), &heap);
+	if (status != EFI_SUCCESS) {
+		ST->ConOut->OutputString(ST->ConOut,
+		    __DECONST(CHAR16 *,
+		    L"Failed to allocate memory for heap.\r\n"));
+		BS->Exit(IH, status, 0, NULL);
+	}
+
+	setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
+
 	/* Set up the console, so printf works. */
 	status = BS->LocateProtocol(&ConsoleControlGUID, NULL,
 	    (VOID **)&ConsoleControl);
@@ -296,6 +274,8 @@ add_device(dev_info_t **devinfop, dev_info_t *devinfo)
 void
 efi_exit(EFI_STATUS s)
 {
+
+	BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));
 	BS->Exit(IH, s, 0, NULL);
 }