Module Name:    src
Committed By:   martin
Date:           Thu Aug  1 13:22:49 UTC 2019

Modified Files:
        src/sys/arch/i386/stand/efiboot [netbsd-8]: Makefile.efiboot boot.c
            dev_net.c efiboot.c efiboot.h efimemory.c
        src/sys/arch/i386/stand/lib [netbsd-8]: exec.c libi386.h

Log Message:
Pull up following revision(s) (requested by nonaka in ticket #1309):

        sys/arch/i386/stand/lib/exec.c: revision 1.73
        sys/arch/i386/stand/efiboot/efiboot.c: revision 1.10
        sys/arch/i386/stand/lib/libi386.h: revision 1.44
        sys/arch/i386/stand/efiboot/efimemory.c: revision 1.6
        sys/arch/i386/stand/efiboot/efimemory.c: revision 1.7
        sys/arch/i386/stand/efiboot/efimemory.c: revision 1.8
        sys/arch/i386/stand/efiboot/efiboot.c: revision 1.9
        sys/arch/i386/stand/efiboot/boot.c: revision 1.12
        sys/arch/i386/stand/efiboot/boot.c: revision 1.13
        sys/arch/i386/stand/efiboot/efiboot.h: revision 1.9
        sys/arch/i386/stand/efiboot/Makefile.efiboot: revision 1.15
        sys/arch/i386/stand/efiboot/dev_net.c: revision 1.2

Added tftp support to x86 efiboot.

Pre-allocate memory for the kernel space at startup.

Added BTINFO_EFIMEMMAP compaction support to x86 efiboot.

Sync the output of memmap command to the output of stand/efiboot mem command.

Added missing efi_memory_probe() call.


To generate a diff of this commit:
cvs rdiff -u -r1.9.2.3 -r1.9.2.4 \
    src/sys/arch/i386/stand/efiboot/Makefile.efiboot
cvs rdiff -u -r1.5.2.5 -r1.5.2.6 src/sys/arch/i386/stand/efiboot/boot.c
cvs rdiff -u -r1.1.2.2 -r1.1.2.3 src/sys/arch/i386/stand/efiboot/dev_net.c
cvs rdiff -u -r1.4.10.3 -r1.4.10.4 src/sys/arch/i386/stand/efiboot/efiboot.c
cvs rdiff -u -r1.5.2.3 -r1.5.2.4 src/sys/arch/i386/stand/efiboot/efiboot.h
cvs rdiff -u -r1.4.10.1 -r1.4.10.2 \
    src/sys/arch/i386/stand/efiboot/efimemory.c
cvs rdiff -u -r1.68 -r1.68.6.1 src/sys/arch/i386/stand/lib/exec.c
cvs rdiff -u -r1.42 -r1.42.6.1 src/sys/arch/i386/stand/lib/libi386.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/stand/efiboot/Makefile.efiboot
diff -u src/sys/arch/i386/stand/efiboot/Makefile.efiboot:1.9.2.3 src/sys/arch/i386/stand/efiboot/Makefile.efiboot:1.9.2.4
--- src/sys/arch/i386/stand/efiboot/Makefile.efiboot:1.9.2.3	Wed Apr 11 14:51:43 2018
+++ src/sys/arch/i386/stand/efiboot/Makefile.efiboot	Thu Aug  1 13:22:48 2019
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile.efiboot,v 1.9.2.3 2018/04/11 14:51:43 martin Exp $
+# $NetBSD: Makefile.efiboot,v 1.9.2.4 2019/08/01 13:22:48 martin Exp $
 
 S=		${.CURDIR}/../../../../..
 
@@ -14,7 +14,9 @@ SOURCES+= efidisk.c efidisk_ll.c efigets
 SOURCES+= efinet.c efipxe.c
 LIBI386SRCS= biosdisk.c bootinfo.c bootinfo_biosgeom.c bootmenu.c
 LIBI386SRCS+= diskbuf.c exec.c menuutils.c parseutils.c pread.c
-SRCS= ${SOURCES} ${EXTRA_SOURCES} ${LIBI386SRCS}
+# use our own nfs implementation
+LIBSASRCS+= nfs.c
+SRCS= ${SOURCES} ${EXTRA_SOURCES} ${LIBI386SRCS} ${LIBSASRCS}
 
 PIE_CFLAGS=
 PIE_LDFLAGS=
@@ -35,6 +37,7 @@ BINMODE=444
 
 .PATH:	${.CURDIR} ${.CURDIR}/..
 .PATH:	${.CURDIR}/../../lib
+.PATH:	${.CURDIR}/../../libsa
 
 LDSCRIPT?= ${.CURDIR}/ldscript
 LDFLAGS+= -nostdlib -T${LDSCRIPT} -Bsymbolic -shared -nocombreloc
@@ -60,7 +63,7 @@ CPPFLAGS+= -DSUPPORT_EXT2FS
 CPPFLAGS+= -DSUPPORT_BOOTP
 CPPFLAGS+= -DSUPPORT_DHCP
 CPPFLAGS+= -DSUPPORT_NFS
-#CPPFLAGS+= -DSUPPORT_TFTP
+CPPFLAGS+= -DSUPPORT_TFTP
 CPPFLAGS+= -DPASS_BIOSGEOM
 CPPFLAGS+= -DBIOSDISK_DEFAULT_SECSIZE=2048	# for bootinfo_biosgeom.c
 CPPFLAGS+= -DLIBSA_ENABLE_LS_OP

Index: src/sys/arch/i386/stand/efiboot/boot.c
diff -u src/sys/arch/i386/stand/efiboot/boot.c:1.5.2.5 src/sys/arch/i386/stand/efiboot/boot.c:1.5.2.6
--- src/sys/arch/i386/stand/efiboot/boot.c:1.5.2.5	Wed Apr 11 14:51:43 2018
+++ src/sys/arch/i386/stand/efiboot/boot.c	Thu Aug  1 13:22:48 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: boot.c,v 1.5.2.5 2018/04/11 14:51:43 martin Exp $	*/
+/*	$NetBSD: boot.c,v 1.5.2.6 2019/08/01 13:22:48 martin Exp $	*/
 
 /*-
  * Copyright (c) 2016 Kimihiro Nonaka <non...@netbsd.org>
@@ -224,40 +224,16 @@ clearit(void)
 static void
 bootit(const char *filename, int howto)
 {
-	EFI_STATUS status;
-	EFI_PHYSICAL_ADDRESS bouncebuf;
-	UINTN npages;
-	u_long allocsz;
 
 	if (howto & AB_VERBOSE)
 		printf("booting %s (howto 0x%x)\n", sprint_bootsel(filename),
 		    howto);
 
-	if (count_netbsd(filename, &allocsz) < 0) {
-		printf("boot: %s: %s\n", sprint_bootsel(filename),
-		       strerror(errno));
-		return;
-	}
-
-	bouncebuf = EFI_ALLOCATE_MAX_ADDRESS;
-	npages = EFI_SIZE_TO_PAGES(allocsz);
-	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
-	    EfiLoaderData, npages, &bouncebuf);
-	if (EFI_ERROR(status)) {
-		printf("boot: %s: %s\n", sprint_bootsel(filename),
-		       strerror(ENOMEM));
-		return;
-	}
-
-	efi_loadaddr = bouncebuf;
-	if (exec_netbsd(filename, bouncebuf, howto, 0, efi_cleanup) < 0)
+	if (exec_netbsd(filename, efi_loadaddr, howto, 0, efi_cleanup) < 0)
 		printf("boot: %s: %s\n", sprint_bootsel(filename),
 		       strerror(errno));
 	else
 		printf("boot returned\n");
-
-	(void) uefi_call_wrapper(BS->FreePages, 2, bouncebuf, npages);
-	efi_loadaddr = 0;
 }
 
 void
@@ -372,7 +348,7 @@ command_help(char *arg)
 #if LIBSA_ENABLE_LS_OP
 	       "ls [path]\n"
 #endif
-	       "memmap [{sorted|unsorted}]\n"
+	       "memmap [{sorted|unsorted|compact}]\n"
 #ifndef SMALL
 	       "menu (reenters boot menu, if defined in boot.cfg)\n"
 #endif
@@ -625,18 +601,21 @@ void
 command_memmap(char *arg)
 {
 	bool sorted = true;
+	bool compact = false;
 
 	if (*arg == '\0' || strcmp(arg, "sorted") == 0)
 		/* Already sorted is true. */;
 	else if (strcmp(arg, "unsorted") == 0)
 		sorted = false;
+	else if (strcmp(arg, "compact") == 0)
+		compact = true;
 	else {
 		printf("invalid flag, "
-		    "must be 'sorted' or 'unsorted'.\n");
+		    "must be 'sorted', 'unsorted' or 'compact'.\n");
 		return;
 	}
 
-	efi_memory_show_map(sorted);
+	efi_memory_show_map(sorted, compact);
 }
 
 void

Index: src/sys/arch/i386/stand/efiboot/dev_net.c
diff -u src/sys/arch/i386/stand/efiboot/dev_net.c:1.1.2.2 src/sys/arch/i386/stand/efiboot/dev_net.c:1.1.2.3
--- src/sys/arch/i386/stand/efiboot/dev_net.c:1.1.2.2	Wed Apr 11 14:51:43 2018
+++ src/sys/arch/i386/stand/efiboot/dev_net.c	Thu Aug  1 13:22:48 2019
@@ -1,3 +1,256 @@
-/*	$NetBSD: dev_net.c,v 1.1.2.2 2018/04/11 14:51:43 martin Exp $	*/
+/*	$NetBSD: dev_net.c,v 1.1.2.3 2019/08/01 13:22:48 martin Exp $	*/
 
-#include <lib/libsa/dev_net.c>
+/*-
+ * Copyright (c) 1997 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Gordon W. Ross.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * This module implements a "raw device" interface suitable for
+ * use by the stand-alone I/O library NFS code.  This interface
+ * does not support any "block" access, and exists only for the
+ * purpose of initializing the network interface, getting boot
+ * parameters, and performing the NFS mount.
+ *
+ * At open time, this does:
+ *
+ * find interface      - netif_open()
+ * RARP for IP address - rarp_getipaddress()
+ * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
+ * RPC/mountd          - nfs_mount(sock, ip, path)
+ */
+
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
+
+#include <lib/libkern/libkern.h>
+
+#include "stand.h"
+#include "net.h"
+#include "netif.h"
+#include "nfs.h"
+#include "bootparam.h"
+#include "dev_net.h"
+#ifdef SUPPORT_BOOTP
+#include "bootp.h"
+#endif
+
+static int netdev_sock = -1;
+static int netdev_opens;
+
+static int net_getparams(int);
+
+/*
+ * Called by devopen after it sets f->f_dev to our devsw entry.
+ * This opens the low-level device and sets f->f_devdata.
+ * This is declared with variable arguments...
+ */
+int
+net_open(struct open_file *f, ...)
+{
+	va_list ap;
+	char *devname;		/* Device part of file name (or NULL). */
+	int error = 0;
+
+	va_start(ap, f);
+	devname = va_arg(ap, char *);
+	va_end(ap);
+
+#ifdef	NETIF_DEBUG
+	if (debug)
+		printf("%s\n", devname);
+#endif
+
+	/* On first open, do netif open, mount, etc. */
+	if (netdev_opens == 0) {
+		/* Find network interface. */
+		if (netdev_sock < 0) {
+			netdev_sock = netif_open(devname);
+			if (netdev_sock < 0) {
+				printf("netif_open() failed\n");
+				return ENXIO;
+			}
+#ifdef NETIF_DEBUG
+			if (debug)
+				printf("netif_open() succeeded\n");
+#endif
+		}
+		if (rootip.s_addr == 0) {
+			/* Get root IP address, and path, etc. */
+			error = net_getparams(netdev_sock);
+			if (error) {
+				/* getparams makes its own noise */
+				netif_close(netdev_sock);
+				netdev_sock = -1;
+				return error;
+			}
+		}
+	}
+	netdev_opens++;
+	f->f_devdata = &netdev_sock;
+	return error;
+}
+
+int
+net_close(struct open_file *f)
+{
+
+#ifdef	NETIF_DEBUG
+	if (debug)
+		printf("net_close: opens=%d\n", netdev_opens);
+#endif
+
+	/* On last close, do netif close, etc. */
+	f->f_devdata = NULL;
+	/* Extra close call? */
+	if (netdev_opens <= 0)
+		return 0;
+	netdev_opens--;
+	/* Not last close? */
+	if (netdev_opens > 0)
+		return 0;
+	rootip.s_addr = 0;
+	if (netdev_sock >= 0) {
+#ifdef NETIF_DEBUG
+		if (debug)
+			printf("%s: calling netif_close()\n", __func__);
+#endif
+		netif_close(netdev_sock);
+		netdev_sock = -1;
+	}
+	return 0;
+}
+
+int
+net_ioctl(struct open_file *f, u_long cmd, void *data)
+{
+
+	return EIO;
+}
+
+int
+net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
+	size_t *rsize)
+{
+
+	return EIO;
+}
+
+
+#ifdef	SUPPORT_BOOTP
+int try_bootp;
+#endif
+
+static int
+net_getparams(int sock)
+{
+	char buf[MAXHOSTNAMELEN];
+	n_long smask;
+
+#ifdef	SUPPORT_BOOTP
+	/*
+	 * Try to get boot info using BOOTP.  If we succeed, then
+	 * the server IP address, gateway, and root path will all
+	 * be initialized.  If any remain uninitialized, we will
+	 * use RARP and RPC/bootparam (the Sun way) to get them.
+	 */
+	if (try_bootp)
+		bootp(sock);
+	if (myip.s_addr != 0)
+		return 0;
+#ifdef NETIF_DEBUG
+	if (debug)
+		printf("BOOTP failed, trying RARP/RPC...\n");
+#endif
+#endif
+
+	/*
+	 * Use RARP to get our IP address.  This also sets our
+	 * netmask to the "natural" default for our address.
+	 */
+	if (rarp_getipaddress(sock)) {
+		printf("RARP failed\n");
+		return EIO;
+	}
+#ifdef NETIF_DEBUG
+	if (debug)
+		printf("client addr: %s\n", inet_ntoa(myip));
+#endif
+
+	/* Get our hostname, server IP address, gateway. */
+	if (bp_whoami(sock)) {
+		printf("bootparam/whoami RPC failed\n");
+		return EIO;
+	}
+#ifdef NETIF_DEBUG
+	if (debug)
+		printf("client name: %s\n", hostname);
+#endif
+
+	/*
+	 * Ignore the gateway from whoami (unreliable).
+	 * Use the "gateway" parameter instead.
+	 */
+	smask = 0;
+	gateip.s_addr = 0;
+	if (bp_getfile(sock, "gateway", &gateip, buf)) {
+		printf("%s: gateway bootparam missing\n", __func__);
+	} else {
+		/* Got it!  Parse the netmask. */
+		smask = inet_addr(buf);
+	}
+	if (smask) {
+		netmask = smask;
+#ifdef NETIF_DEBUG
+		if (debug)
+			printf("subnet mask: %s\n", intoa(netmask));
+#endif
+	}
+#ifdef NETIF_DEBUG
+	if (debug)
+		if (gateip.s_addr)
+			printf("net gateway: %s\n", inet_ntoa(gateip));
+#endif
+
+	/* Get the root server and pathname. */
+	if (bp_getfile(sock, "root", &rootip, rootpath)) {
+		printf("bootparam/getfile RPC failed\n");
+		return EIO;
+	}
+
+#ifdef NETIF_DEBUG
+	if (debug) {
+		printf("server addr: %s\n", inet_ntoa(rootip));
+		printf("server path: %s\n", rootpath);
+	}
+#endif
+
+	return 0;
+}

Index: src/sys/arch/i386/stand/efiboot/efiboot.c
diff -u src/sys/arch/i386/stand/efiboot/efiboot.c:1.4.10.3 src/sys/arch/i386/stand/efiboot/efiboot.c:1.4.10.4
--- src/sys/arch/i386/stand/efiboot/efiboot.c:1.4.10.3	Wed Apr 11 14:51:43 2018
+++ src/sys/arch/i386/stand/efiboot/efiboot.c	Thu Aug  1 13:22:48 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: efiboot.c,v 1.4.10.3 2018/04/11 14:51:43 martin Exp $	*/
+/*	$NetBSD: efiboot.c,v 1.4.10.4 2019/08/01 13:22:48 martin Exp $	*/
 
 /*-
  * Copyright (c) 2016 Kimihiro Nonaka <non...@netbsd.org>
@@ -89,6 +89,7 @@ efi_main(EFI_HANDLE imageHandle, EFI_SYS
 		}
 	}
 
+	efi_memory_probe();
 	efi_disk_probe();
 	efi_pxe_probe();
 	efi_net_probe();
@@ -135,6 +136,7 @@ efi_cleanup(void)
 	}
 	efi_cleanuped = true;
 
+	efi_memory_compact_map(desc, &NoEntries, DescriptorSize);
 	allocsz = sizeof(struct btinfo_efimemmap) - 1
 	    + NoEntries * DescriptorSize;
 	bim = alloc(allocsz);

Index: src/sys/arch/i386/stand/efiboot/efiboot.h
diff -u src/sys/arch/i386/stand/efiboot/efiboot.h:1.5.2.3 src/sys/arch/i386/stand/efiboot/efiboot.h:1.5.2.4
--- src/sys/arch/i386/stand/efiboot/efiboot.h:1.5.2.3	Wed Apr 11 14:51:43 2018
+++ src/sys/arch/i386/stand/efiboot/efiboot.h	Thu Aug  1 13:22:48 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: efiboot.h,v 1.5.2.3 2018/04/11 14:51:43 martin Exp $	*/
+/*	$NetBSD: efiboot.h,v 1.5.2.4 2019/08/01 13:22:48 martin Exp $	*/
 
 /*-
  * Copyright (c) 2016 Kimihiro Nonaka <non...@netbsd.org>
@@ -80,9 +80,11 @@ void efi_disk_show(void);
 
 /* efimemory.c */
 void efi_memory_probe(void);
-void efi_memory_show_map(bool);
+void efi_memory_show_map(bool, bool);
 EFI_MEMORY_DESCRIPTOR *efi_memory_get_map(UINTN *, UINTN *, UINTN *, UINT32 *,
     bool);
+EFI_MEMORY_DESCRIPTOR *efi_memory_compact_map(EFI_MEMORY_DESCRIPTOR *, UINTN *,
+    UINTN);
 
 /* efinet.c */
 void efi_net_probe(void);

Index: src/sys/arch/i386/stand/efiboot/efimemory.c
diff -u src/sys/arch/i386/stand/efiboot/efimemory.c:1.4.10.1 src/sys/arch/i386/stand/efiboot/efimemory.c:1.4.10.2
--- src/sys/arch/i386/stand/efiboot/efimemory.c:1.4.10.1	Mon Apr  2 08:50:33 2018
+++ src/sys/arch/i386/stand/efiboot/efimemory.c	Thu Aug  1 13:22:48 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: efimemory.c,v 1.4.10.1 2018/04/02 08:50:33 martin Exp $	*/
+/*	$NetBSD: efimemory.c,v 1.4.10.2 2019/08/01 13:22:48 martin Exp $	*/
 
 /*-
  * Copyright (c) 2016 Kimihiro Nonaka <non...@netbsd.org>
@@ -30,39 +30,27 @@
 
 #include <bootinfo.h>
 
-static const char *memtypes[] = {
-	"unknown",
-	"available",
-	"reserved",
-	"ACPI reclaimable",
-	"ACPI NVS",
-	"unusable",
-	"disabled",
-	"Persistent",
-	"undefined (8)",
-	"undefined (9)",
-	"undefined (10)",
-	"undefined (11)",
-	"Persistent (Legacy)"
+static const char *efi_memory_type[] = {
+	[EfiReservedMemoryType]		= "Reserved Memory Type",
+	[EfiLoaderCode]			= "Loader Code",
+	[EfiLoaderData]			= "Loader Data",
+	[EfiBootServicesCode]		= "Boot Services Code",
+	[EfiBootServicesData]		= "Boot Services Data",
+	[EfiRuntimeServicesCode]	= "Runtime Services Code",
+	[EfiRuntimeServicesData]	= "Runtime Services Data",
+	[EfiConventionalMemory]		= "Conventional Memory",
+	[EfiUnusableMemory]		= "Unusable Memory",
+	[EfiACPIReclaimMemory]		= "ACPI Reclaim Memory",
+	[EfiACPIMemoryNVS]		= "ACPI Memory NVS",
+	[EfiMemoryMappedIO]		= "MMIO",
+	[EfiMemoryMappedIOPortSpace]	= "MMIO (Port Space)",
+	[EfiPalCode]			= "Pal Code",
+	[EfiPersistentMemory]		= "Persistent Memory",
 };
 
-static const char *efimemtypes[] = {
-	"Reserved",
-	"LoaderCode",
-	"LoaderData",
-	"BootServicesCode",
-	"BootServicesData",
-	"RuntimeServicesCode",
-	"RuntimeServicesData",
-	"ConventionalMemory",
-	"UnusableMemory",
-	"ACPIReclaimMemory",
-	"ACPIMemoryNVS",
-	"MemoryMappedIO",
-	"MemoryMappedIOPortSpace",
-	"PalCode",
-	"PersistentMemory",
-};
+#ifndef KERN_LOADSPACE_SIZE
+#define KERN_LOADSPACE_SIZE	(128 * 1024 * 1024)	/* 128MiB */
+#endif
 
 static int
 getmemtype(EFI_MEMORY_DESCRIPTOR *md)
@@ -103,7 +91,7 @@ EFI_MEMORY_DESCRIPTOR *
 efi_memory_get_map(UINTN *NoEntries, UINTN *MapKey, UINTN *DescriptorSize,
     UINT32 *DescriptorVersion, bool sorted)
 {
-	EFI_MEMORY_DESCRIPTOR *desc, *md, *next, *target, tmp;
+	EFI_MEMORY_DESCRIPTOR *desc, *md, *next, *target, *tmp;
 	UINTN i, j;
 
 	*NoEntries = 0;
@@ -115,17 +103,93 @@ efi_memory_get_map(UINTN *NoEntries, UIN
 	if (!sorted)
 		return desc;
 
+	tmp = alloc(*DescriptorSize);
+	if (tmp == NULL)
+		return desc;
+
 	for (i = 0, md = desc; i < *NoEntries - 1; i++, md = next) {
 		target = next = NextMemoryDescriptor(md, *DescriptorSize);
 		for (j = i + 1; j < *NoEntries; j++) {
 			if (md->PhysicalStart > target->PhysicalStart) {
-				CopyMem(&tmp, md, sizeof(*md));
-				CopyMem(md, target, sizeof(*md));
-				CopyMem(target, &tmp, sizeof(*md));
+				CopyMem(tmp, md, *DescriptorSize);
+				CopyMem(md, target, *DescriptorSize);
+				CopyMem(target, tmp, *DescriptorSize);
 			}
 			target = NextMemoryDescriptor(target, *DescriptorSize);
 		}
 	}
+	dealloc(tmp, *DescriptorSize);
+
+	return desc;
+}
+
+EFI_MEMORY_DESCRIPTOR *
+efi_memory_compact_map(EFI_MEMORY_DESCRIPTOR *desc, UINTN *NoEntries,
+    UINTN DescriptorSize)
+{
+	EFI_MEMORY_DESCRIPTOR *md, *next, *target, *tmp;
+	UINTN i, j;
+	UINT32 type;
+	bool first = true, do_compact;
+
+	for (i = 0, md = target = desc; i < *NoEntries; i++, md = next) {
+		type = md->Type;
+		switch (type) {
+		case EfiLoaderCode:
+		case EfiLoaderData:
+		case EfiBootServicesCode:
+		case EfiBootServicesData:
+		case EfiConventionalMemory:
+			if ((md->Attribute & EFI_MEMORY_WB) != 0)
+				type = EfiConventionalMemory;
+			if (md->Attribute == target->Attribute) {
+				do_compact = true;
+				break;
+			}
+			/* FALLTHROUGH */
+		case EfiACPIReclaimMemory:
+		case EfiACPIMemoryNVS:
+		case EfiPersistentMemory:
+		case EfiReservedMemoryType:
+		case EfiRuntimeServicesCode:
+		case EfiRuntimeServicesData:
+		case EfiUnusableMemory:
+		case EfiMemoryMappedIO:
+		case EfiMemoryMappedIOPortSpace:
+		case EfiPalCode:
+		default:
+			do_compact = false;
+			break;
+		}
+
+		if (first) {
+			first = false;
+		} else if (do_compact &&
+		    type == target->Type &&
+		    md->Attribute == target->Attribute &&
+		    md->PhysicalStart == target->PhysicalStart + target->NumberOfPages * EFI_PAGE_SIZE) {
+			/* continuous region */
+			target->NumberOfPages += md->NumberOfPages;
+
+			tmp = md;
+			for (j = i + 1; j < *NoEntries; j++) {
+				next = NextMemoryDescriptor(md, DescriptorSize);
+				CopyMem(md, next, DescriptorSize);
+				md = next;
+			}
+			next = tmp;
+
+			i--;
+			(*NoEntries)--;
+			continue;
+		} else {
+			target = md;
+		}
+
+		target->Type = type;
+		next = NextMemoryDescriptor(md, DescriptorSize);
+	}
+
 	return desc;
 }
 
@@ -231,13 +295,21 @@ void
 efi_memory_probe(void)
 {
 	EFI_MEMORY_DESCRIPTOR *mdtop, *md, *next;
+	EFI_STATUS status;
+	EFI_PHYSICAL_ADDRESS bouncebuf;
 	UINTN i, n, NoEntries, MapKey, DescriptorSize, MappingSize;
 	UINT32 DescriptorVersion;
 	int memtype;
 
+	bouncebuf = EFI_ALLOCATE_MAX_ADDRESS;
+	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
+	    EfiLoaderData, EFI_SIZE_TO_PAGES(KERN_LOADSPACE_SIZE), &bouncebuf);
+	if (EFI_ERROR(status))
+		panic("couldn't allocate kernel space.");
+	efi_loadaddr = bouncebuf;
+
 	mdtop = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
 	    &DescriptorVersion, false);
-
 	printf(" mem[");
 	for (i = 0, n = 0, md = mdtop; i < NoEntries; i++, md = next) {
 		next = NextMemoryDescriptor(md, DescriptorSize);
@@ -261,15 +333,14 @@ efi_memory_probe(void)
 }
 
 void
-efi_memory_show_map(bool sorted)
+efi_memory_show_map(bool sorted, bool compact)
 {
 	EFI_STATUS status;
 	EFI_MEMORY_DESCRIPTOR *mdtop, *md, *next;
 	UINTN i, NoEntries, MapKey, DescriptorSize;
 	UINT32 DescriptorVersion;
-	char memstr[32], efimemstr[32];
-	int memtype;
-	UINTN cols, rows, row = 0;
+	char efimemstr[32];
+	UINTN cols, rows, row;
 
 	status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
 	    ST->ConOut->Mode->Mode, &cols, &rows);
@@ -280,25 +351,26 @@ efi_memory_show_map(bool sorted)
 
 	mdtop = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
 	    &DescriptorVersion, sorted);
+	if (compact)
+		efi_memory_compact_map(mdtop, &NoEntries, DescriptorSize);
+
+	printf("%-22s  %-16s  %-16s  %-16s\n", "Type", "Start", "End", "Attributes");
+	printf("----------------------  ----------------  ----------------  ----------------\n");
+	row = 2;
 
 	for (i = 0, md = mdtop; i < NoEntries; i++, md = next) {
 		next = NextMemoryDescriptor(md, DescriptorSize);
 
-		memtype = getmemtype(md);
-		if (memtype >= __arraycount(memtypes))
-			snprintf(memstr, sizeof(memstr), "unknown (%d)",
-			    memtype);
-		if (md->Type >= __arraycount(efimemtypes))
+		if (md->Type >= __arraycount(efi_memory_type))
 			snprintf(efimemstr, sizeof(efimemstr), "unknown (%d)",
 			    md->Type);
-		printf("%016" PRIxMAX "/%016" PRIxMAX ": %s [%s]\n",
+		printf("%-22s  %016" PRIxMAX "  %016" PRIxMAX "  %016" PRIxMAX "\n",
+		    md->Type >= __arraycount(efi_memory_type) ?
+		      efimemstr : efi_memory_type[md->Type],
 		    (uintmax_t)md->PhysicalStart,
 		    (uintmax_t)md->PhysicalStart +
 		      md->NumberOfPages * EFI_PAGE_SIZE - 1,
-		    memtype >= __arraycount(memtypes) ?
-		      memstr : memtypes[memtype],
-		    md->Type >= __arraycount(efimemtypes) ?
-		      efimemstr : efimemtypes[md->Type]);
+		    (uintmax_t)md->Attribute);
 
 		if (++row >= rows) {
 			row = 0;

Index: src/sys/arch/i386/stand/lib/exec.c
diff -u src/sys/arch/i386/stand/lib/exec.c:1.68 src/sys/arch/i386/stand/lib/exec.c:1.68.6.1
--- src/sys/arch/i386/stand/lib/exec.c:1.68	Fri Mar 24 08:50:17 2017
+++ src/sys/arch/i386/stand/lib/exec.c	Thu Aug  1 13:22:48 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec.c,v 1.68 2017/03/24 08:50:17 nonaka Exp $	 */
+/*	$NetBSD: exec.c,v 1.68.6.1 2019/08/01 13:22:48 martin Exp $	 */
 
 /*
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -464,55 +464,6 @@ out:
 	return -1;
 }
 
-int
-count_netbsd(const char *file, u_long *rsz)
-{
-	u_long marks[MARK_MAX];
-	char kdev[64];
-	char base_path[64] = "/";
-	struct stat st;
-	boot_module_t *bm;
-	u_long sz;
-	int err, fd;
-
-	howto = AB_SILENT;
-
-	memset(marks, 0, sizeof(marks));
-	if ((fd = loadfile(file, marks, COUNT_KERNEL | LOAD_NOTE)) == -1)
-		return -1;
-	close(fd);
-	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
-	    (-sizeof(int));
-	sz = marks[MARK_END];
-
-	/* The modules must be allocated after the kernel */
-	if (boot_modules_enabled) {
-		extract_device(file, kdev, sizeof(kdev));
-		module_base_path(base_path, sizeof(base_path));
-
-		/* If the root fs type is unusual, load its module. */
-		if (fsmod != NULL)
-			module_add_common(fsmod, BM_TYPE_KMOD);
-
-		for (bm = boot_modules; bm; bm = bm->bm_next) {
-			fd = module_open(bm, 0, kdev, base_path, false);
-			if (fd == -1)
-				continue;
-			sz = (sz + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
-			err = fstat(fd, &st);
-			if (err == -1 || st.st_size == -1) {
-				close(fd);
-				continue;
-			}
-			sz += st.st_size;
-			close(fd);
-		}
-	}
-
-	*rsz = sz;
-	return 0;
-}
-
 static void
 extract_device(const char *path, char *buf, size_t buflen)
 {

Index: src/sys/arch/i386/stand/lib/libi386.h
diff -u src/sys/arch/i386/stand/lib/libi386.h:1.42 src/sys/arch/i386/stand/lib/libi386.h:1.42.6.1
--- src/sys/arch/i386/stand/lib/libi386.h:1.42	Sun Mar 12 05:33:48 2017
+++ src/sys/arch/i386/stand/lib/libi386.h	Thu Aug  1 13:22:48 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: libi386.h,v 1.42 2017/03/12 05:33:48 nonaka Exp $	*/
+/*	$NetBSD: libi386.h,v 1.42.6.1 2019/08/01 13:22:48 martin Exp $	*/
 
 /*
  * Copyright (c) 1996
@@ -43,7 +43,6 @@ void multiboot(physaddr_t, physaddr_t, p
 
 int exec_netbsd(const char *, physaddr_t, int, int, void (*)(void));
 int exec_multiboot(const char *, char *);
-int count_netbsd(const char *, u_long *);
 
 void delay(int);
 int getbasemem(void);

Reply via email to