git: 6046a35b71c2 - main - loader: Read the partition table of md images

From: Olivier Cochard <olivier_at_FreeBSD.org>
Date: Thu, 12 Nov 2026 09:27:54 UTC
The branch main has been updated by olivier:

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

commit 6046a35b71c297ebe64a766c0d5c04fb62fc0f5f
Author:     Olivier Cochard <olivier@FreeBSD.org>
AuthorDate: 2026-11-08 21:44:38 +0000
Commit:     Olivier Cochard <olivier@FreeBSD.org>
CommitDate: 2026-11-12 09:22:04 +0000

    loader: Read the partition table of md images
    
    md is a DEVT_DISK device but never called disk_open(), so reads always started
    at offset 0 of the image. A partitioned image, such as a memstick image booted
    memdisk=, has its partition table there, not a filesystem, and the loader stops
    with "MD not present".
    
    Reviewed by:    imp
    Approved by:    imp
    Sponsored by:   Netflix
    Differential Revision:  https://ron-dev.freebsd.org/FreeBSD/src/pulls/99
---
 stand/common/md.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/stand/common/md.c b/stand/common/md.c
index 93301e39a2f5..9da336600e6a 100644
--- a/stand/common/md.c
+++ b/stand/common/md.c
@@ -31,6 +31,7 @@
 #include <sys/stdarg.h>
 
 #include "bootstrap.h"
+#include "disk.h"
 
 #define	MD_FLAG_MASK	MD_FLAG_KERNEL
 
@@ -61,6 +62,8 @@ struct devsw md_dev = {
 	.dv_ioctl = noioctl,
 	.dv_print = md_print,
 	.dv_cleanup = nullsys,
+	.dv_fmtdev = disk_fmtdev,
+	.dv_parsedev = disk_parsedev,
 };
 
 /*
@@ -172,14 +175,16 @@ static int
 md_strategy(void *devdata, int rw, daddr_t blk, size_t size,
     char *buf, size_t *rsize)
 {
-	struct devdesc *dev = (struct devdesc *)devdata;
+	struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
 	struct md_info *md;
 	size_t ofs;
 
-	md = md_get_info(dev->d_unit);
+	md = md_get_info(dev->dd.d_unit);
 	if (md == NULL)
 		return (ENXIO);
 
+	blk += dev->d_offset;
+
 	if (blk < 0 || (uintmax_t)blk >= md->md_size / MD_BLOCK_SIZE)
 		return (EIO);
 
@@ -209,25 +214,35 @@ static int
 md_open(struct open_file *f, ...)
 {
 	va_list ap;
-	struct devdesc *dev;
+	struct disk_devdesc *dev;
+	struct md_info *md;
 
 	va_start(ap, f);
-	dev = va_arg(ap, struct devdesc *);
+	dev = va_arg(ap, struct disk_devdesc *);
 	va_end(ap);
 
-	if (md_get_info(dev->d_unit) == NULL)
+	md = md_get_info(dev->dd.d_unit);
+	if (md == NULL)
 		return (ENXIO);
 
-	return (0);
+	/*
+	 * The image may be partitioned, as the memstick images used with
+	 * memdisk= are. Without this only offset 0 is reachable, where
+	 * there is no filesystem.
+	 */
+	return (disk_open(dev, md->md_size, MD_BLOCK_SIZE));
 }
 
 static int
 md_close(struct open_file *f)
 {
-	struct devdesc *dev;
+	struct disk_devdesc *dev;
+
+	dev = (struct disk_devdesc *)(f->f_devdata);
+	if (md_get_info(dev->dd.d_unit) == NULL)
+		return (ENXIO);
 
-	dev = (struct devdesc *)(f->f_devdata);
-	return (md_get_info(dev->d_unit) == NULL ? ENXIO : 0);
+	return (disk_close(dev));
 }
 
 static int