git: bbc64cf66cdf - main - stand/boot1.efi: use the bootonce dataset as root dataset

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Thu, 08 Jun 2023 18:14:59 UTC
The branch main has been updated by glebius:

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

commit bbc64cf66cdf03bc0e45aecfbca13f7b6f025a65
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2023-06-08 18:14:45 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2023-06-08 18:14:45 +0000

    stand/boot1.efi: use the bootonce dataset as root dataset
    
    Before this change we would only pass the bootonce dataset name
    to the environment for the next loader, while actually reading
    the next stage loader from the 'bootfs' dataset, not the bootonce
    dataset.
    
    Another problem fixed by this change is a boot from a configuration
    when bootonce attribute is present, but 'bootfs' property is not set.
    
    Reviewed by:            imp
    Differential Revision:  https://reviews.freebsd.org/D40389
---
 stand/efi/boot1/zfs_module.c | 54 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 12 deletions(-)

diff --git a/stand/efi/boot1/zfs_module.c b/stand/efi/boot1/zfs_module.c
index 234b50084839..c9a1038d0e6a 100644
--- a/stand/efi/boot1/zfs_module.c
+++ b/stand/efi/boot1/zfs_module.c
@@ -144,6 +144,7 @@ load(const char *filepath, dev_info_t *devinfo, void **bufp, size_t *bufsize)
 	struct zfsmount zmount;
 	dnode_phys_t dn;
 	struct stat st;
+	uint64_t rootobj;
 	int err;
 	void *buf;
 
@@ -162,20 +163,49 @@ load(const char *filepath, dev_info_t *devinfo, void **bufp, size_t *bufsize)
 		return (EFI_NOT_FOUND);
 	}
 
-	if ((err = zfs_mount_impl(spa, 0, &zmount)) != 0) {
-		DPRINTF("Failed to mount pool '%s' (%d)\n", spa->spa_name, err);
-		return (EFI_NOT_FOUND);
+	if (zfs_get_bootonce_spa(spa, OS_BOOTONCE, zfs_bootonce,
+	    sizeof(zfs_bootonce)) == 0) {
+		/*
+		 * If bootonce attribute is present, use it as root dataset.
+		 * Any attempt to use it should clear the 'once' flag.  Prior
+		 * to now, we'd not be able to clear it anyway.  We don't care
+		 * if we can't find the files to boot, or if there's a problem
+		 * with it: we've tried to use it once we're able to mount the
+		 * ZFS dataset.
+		 *
+		 * Note: the attribute is prefixed with "zfs:" and suffixed
+		 * with ":".
+		 */
+		char *dname, *end;
+
+		if (zfs_bootonce[0] != 'z' || zfs_bootonce[1] != 'f' ||
+		    zfs_bootonce[2] != 's' || zfs_bootonce[3] != ':' ||
+		    (dname = strchr(&zfs_bootonce[4], '/')) == NULL ||
+		    (end = strrchr(&zfs_bootonce[4], ':')) == NULL) {
+			printf("INVALID zfs bootonce: %s\n", zfs_bootonce);
+			*zfs_bootonce = '\0';
+			rootobj = 0;
+		} else {
+			dname += 1;
+			*end = '\0';
+			if (zfs_lookup_dataset(spa, dname, &rootobj) != 0) {
+				printf("zfs bootonce dataset %s NOT FOUND\n",
+				    dname);
+				*zfs_bootonce = '\0';
+				rootobj = 0;
+			} else
+				printf("zfs bootonce: %s\n", zfs_bootonce);
+			*end = ':';
+		}
+	} else {
+		*zfs_bootonce = '\0';
+		rootobj = 0;
 	}
 
-	/*
-	 * OK. We've found a filesystem. Any attempt to use it should clear the
-	 * 'once' flag. Prior to now, we'd not be able to clear it anyway. We
-	 * don't care if we can't find the files to boot, or if there's a
-	 * problem with it: we've tried to use it once we're able to mount the
-	 * ZFS dataset.
-	 */
-	*zfs_bootonce = '\0';
-	zfs_get_bootonce_spa(spa, OS_BOOTONCE, zfs_bootonce, sizeof(zfs_bootonce));
+	if ((err = zfs_mount_impl(spa, rootobj, &zmount)) != 0) {
+		printf("Failed to mount pool '%s' (%d)\n", spa->spa_name, err);
+		return (EFI_NOT_FOUND);
+	}
 
 	if ((err = zfs_lookup(&zmount, filepath, &dn)) != 0) {
 		if (err == ENOENT) {