git: 7055aa7a070f - main - loader: Move ramdisk export to md.c

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 11 Sep 2026 05:04:17 UTC
The branch main has been updated by imp:

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

commit 7055aa7a070fa98fa3a7e1efaab0b07d68a08975
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2026-09-11 04:13:09 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2026-09-11 04:15:27 +0000

    loader: Move ramdisk export to md.c
    
    Move the common routine to md.c and use it where we do memdisks today.
    
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D59417
---
 stand/common/bootstrap.h         |  3 +++
 stand/common/md.c                | 19 +++++++++++++++++++
 stand/efi/libefi/efipart.c       |  8 +-------
 stand/i386/libi386/biosmemdisk.c | 11 ++---------
 stand/i386/loader/Makefile       |  1 +
 5 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/stand/common/bootstrap.h b/stand/common/bootstrap.h
index 7be1d6897a9a..ba56f2e3c149 100644
--- a/stand/common/bootstrap.h
+++ b/stand/common/bootstrap.h
@@ -381,4 +381,7 @@ int gen_setcurrdev(struct env_var *ev, int flags, const void *value);
 int mount_currdev(struct env_var *, int, const void *);
 void set_currdev(const char *devname);
 
+/* md.c */
+void md_export_to_kernel(uint64_t start, uint64_t len);
+
 #endif /* !_BOOTSTRAP_H_ */
diff --git a/stand/common/md.c b/stand/common/md.c
index b1179254e3c0..604bf2ae64a8 100644
--- a/stand/common/md.c
+++ b/stand/common/md.c
@@ -34,6 +34,25 @@
 
 #define	MD_BLOCK_SIZE	512
 
+/*
+ * The kernel can accept a MD inline in the metadata, or out-of-line via
+ * hints, like when memory disks are passed to us in, for example, UEFI.
+ */
+void
+md_export_to_kernel(uint64_t start, uint64_t len)
+{
+	char key[32], value[32];
+	static int unit = 0;	/* Note: loader unit and kernel unit may differ */
+
+	snprintf(key, sizeof(key), "hint.md.%d.physaddr", unit);
+	snprintf(value, sizeof(value), "0x%016jx", (uintmax_t)start);
+	setenv(key, value, 1);
+	snprintf(key, sizeof(key), "hint.md.%d.len", unit);
+	snprintf(value, sizeof(value), "%jd", (uintmax_t)len);
+	setenv(key, value, 1);
+	unit++;
+}
+
 #ifdef MD_IMAGE_SIZE
 
 #if (MD_IMAGE_SIZE == 0 || MD_IMAGE_SIZE % MD_BLOCK_SIZE)
diff --git a/stand/efi/libefi/efipart.c b/stand/efi/libefi/efipart.c
index c80a3b966376..1b1795ccdd40 100644
--- a/stand/efi/libefi/efipart.c
+++ b/stand/efi/libefi/efipart.c
@@ -1278,7 +1278,6 @@ efiblk_memdisk_preload(void)
 	MEDIA_RAM_DISK_DEVICE_PATH *ram;
 	uint64_t start, end;
 	pdinfo_t *md;
-	char key[32], value[32];
 	int i;
 
 	STAILQ_FOREACH(md, &pdinfo, pd_link) {
@@ -1288,11 +1287,6 @@ efiblk_memdisk_preload(void)
 		i = ram->Instance;
 		start = ((uint64_t)ram->StartingAddr[1] << 32) | ram->StartingAddr[0];
 		end = ((uint64_t)ram->EndingAddr[1] << 32) | ram->EndingAddr[0];
-		snprintf(key, sizeof(key), "hint.md.%d.physaddr", i);
-		snprintf(value, sizeof(value), "0x%016x", start);
-		setenv(key, value, 1);
-		snprintf(key, sizeof(key), "hint.md.%d.len", i);
-		snprintf(value, sizeof(value), "%d", end - start + 1);
-		setenv(key, value, 1);
+		md_export_to_kernel(start, end - start + 1);
 	}
 }
diff --git a/stand/i386/libi386/biosmemdisk.c b/stand/i386/libi386/biosmemdisk.c
index 208ae289950a..89199563cbef 100644
--- a/stand/i386/libi386/biosmemdisk.c
+++ b/stand/i386/libi386/biosmemdisk.c
@@ -66,8 +66,7 @@ struct safe_13h_hook {
 void
 biosmemdisk_detect(void)
 {
-	char line[80], scratch[80];
-	int hook = 0, count = 0, sector_size;
+	int hook = 0, sector_size;
 	uint16_t segment, offset;
 	struct safe_13h_hook *probe;
 	ACPI_TABLE_HEADER *mbft;
@@ -125,13 +124,7 @@ biosmemdisk_detect(void)
 		    mdi->mdi_major, mdi->mdi_minor, mdi->mdi_disk_ptr,
 		    mdi->mdi_disk_sectors, mdi->mdi_disk_sectors * sector_size);
 
-		snprintf(line, sizeof(line), "hint.md.%d.physaddr", count);
-		snprintf(scratch, sizeof(scratch), "0x%08x", mdi->mdi_disk_ptr);
-		setenv(line, scratch, 1);
-		snprintf(line, sizeof(line), "hint.md.%d.len", count);
-		snprintf(scratch, sizeof(scratch), "%d", mdi->mdi_disk_sectors * sector_size);
-		setenv(line, scratch, 1);
-		count++;
+		md_export_to_kernel(mdi->mdi_disk_ptr, mdi->mdi_disk_sectors * sector_size);
 end_of_loop:
 		hook++;
 		offset = probe->sh_next_offset;
diff --git a/stand/i386/loader/Makefile b/stand/i386/loader/Makefile
index 8b9163aa69c2..043853f80360 100644
--- a/stand/i386/loader/Makefile
+++ b/stand/i386/loader/Makefile
@@ -10,6 +10,7 @@ LOADER_UFS_SUPPORT?=	yes
 LOADER_GZIP_SUPPORT?=	yes
 LOADER_BZIP2_SUPPORT?=	yes
 LOADER_XZ_SUPPORT?=	yes
+LOADER_MD_SUPPORT?=	yes
 
 .include <bsd.init.mk>