git: e40703a705e5 - stable/14 - stand/efi: Changes to efichar to allow it to be used in the kernel

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 16 Apr 2024 20:13:40 UTC
The branch stable/14 has been updated by imp:

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

commit e40703a705e598ab0faa8f43c0f75607a269c0db
Author:     Stephen J. Kiernan <stevek@FreeBSD.org>
AuthorDate: 2024-03-27 22:37:48 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-04-16 19:54:30 +0000

    stand/efi: Changes to efichar to allow it to be used in the kernel
    
    Replace malloc/free with EFICHAR_MALLOC and EFICHAR_FREEE macros.
    
    Obtained from:  Juniper Networks, Inc.
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D44541
    
    (cherry picked from commit fe429e6794d322636e7c1e520d50ec0cb711dd14)
---
 stand/efi/include/efichar.h |  8 ++++++++
 stand/efi/libefi/efichar.c  | 17 ++++++++++++-----
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/stand/efi/include/efichar.h b/stand/efi/include/efichar.h
index 4460f5efdcef..0a40906aeedf 100644
--- a/stand/efi/include/efichar.h
+++ b/stand/efi/include/efichar.h
@@ -37,4 +37,12 @@ int ucs2_to_utf8(const efi_char *, char **);
 int utf8_to_ucs2(const char *, efi_char **, size_t *);
 int ucs2len(const efi_char *);
 
+#ifdef _KERNEL
+#define	EFICHAR_MALLOC(sz)	malloc((sz), M_TEMP, M_WAITOK | M_ZERO)
+#define	EFICHAR_FREE(sz)	free((sz), M_TEMP)
+#else
+#define	EFICHAR_MALLOC(sz)	malloc(sz)
+#define	EFICHAR_FREE(sz)	free(sz)
+#endif
+
 #endif /* _BOOT_EFI_EFICHAR_H_ */
diff --git a/stand/efi/libefi/efichar.c b/stand/efi/libefi/efichar.c
index 86642d325754..659212799b6a 100644
--- a/stand/efi/libefi/efichar.c
+++ b/stand/efi/libefi/efichar.c
@@ -25,14 +25,21 @@
  */
 
 #include <sys/types.h>
+#ifndef _KERNEL
 #include <errno.h>
+#endif
 #ifdef _STANDALONE
 #include <stand.h>
 #else
+#ifdef _KERNEL
+#include <sys/malloc.h>
+#include <sys/systm.h>
+#else
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#endif
 #include <sys/efi.h>
 #include <machine/efi.h>
 #endif
@@ -87,7 +94,7 @@ ucs2_to_utf8(const efi_char *nm, char **name)
 	if (*name != NULL)
 		cp = *name;
 	else
-		cp = *name = malloc(sz);
+		cp = *name = EFICHAR_MALLOC(sz);
 	if (*name == NULL)
 		return (ENOMEM);
 
@@ -114,7 +121,7 @@ ucs2_to_utf8(const efi_char *nm, char **name)
 	if (len >= sz) {
 		/* Absent bugs, we'll never return EOVERFLOW */
 		if (freeit) {
-			free(*name);
+			EFICHAR_FREE(*name);
 			*name = NULL;
 		}
 		return (EOVERFLOW);
@@ -135,7 +142,7 @@ utf8_to_ucs2(const char *name, efi_char **nmp, size_t *len)
 
 	sz = strlen(name) * 2 + 2;
 	if (*nmp == NULL)
-		*nmp = malloc(sz);
+		*nmp = EFICHAR_MALLOC(sz);
 	if (*nmp == NULL)
 		return (ENOMEM);
 	nm = *nmp;
@@ -183,7 +190,7 @@ utf8_to_ucs2(const char *name, efi_char **nmp, size_t *len)
 	}
 	if (sz < 2) {
 		if (freeit) {
-			free(nm);
+			EFICHAR_FREE(nm);
 			*nmp = NULL;
 		}
 		return (EDOOFUS);
@@ -194,7 +201,7 @@ utf8_to_ucs2(const char *name, efi_char **nmp, size_t *len)
 	return (0);
 ilseq:
 	if (freeit) {
-		free(nm);
+		EFICHAR_FREE(nm);
 		*nmp = NULL;
 	}
 	return (EILSEQ);