git: 31cf842a96da - stable/13 - stand: create devinit

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:13:23 UTC
The branch stable/13 has been updated by imp:

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

commit 31cf842a96da02e198590d93f7577c1621035d7b
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-11-30 22:09:29 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:39 +0000

    stand: create devinit
    
    devinit() marches through all the devices, calling the inint routines if
    any exist. Replace all the identical copies of this code.
    
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D37349
    
    (cherry picked from commit 66012c8fc4f92b80a61405dc7e206617e9f08920)
---
 stand/efi/loader/main.c        |  4 +---
 stand/i386/loader/main.c       |  9 +--------
 stand/i386/zfsboot/zfsboot.c   |  4 +---
 stand/libsa/dev.c              | 18 ++++++++++++++++++
 stand/libsa/libsa.3            |  8 ++++++++
 stand/libsa/stand.h            |  1 +
 stand/powerpc/ofw/main.c       |  7 +------
 stand/userboot/userboot/main.c |  8 +-------
 8 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/stand/efi/loader/main.c b/stand/efi/loader/main.c
index 5edc06c9e0d5..85c545e0e72f 100644
--- a/stand/efi/loader/main.c
+++ b/stand/efi/loader/main.c
@@ -991,9 +991,7 @@ main(int argc, CHAR16 *argv[])
 		    "failures\n", i);
 	}
 
-	for (i = 0; devsw[i] != NULL; i++)
-		if (devsw[i]->dv_init != NULL)
-			(devsw[i]->dv_init)();
+	devinit();
 
 	/*
 	 * Detect console settings two different ways: one via the command
diff --git a/stand/i386/loader/main.c b/stand/i386/loader/main.c
index 15d1c312b278..861e08e7569c 100644
--- a/stand/i386/loader/main.c
+++ b/stand/i386/loader/main.c
@@ -96,8 +96,6 @@ ptov(uintptr_t x)
 int
 main(void)
 {
-	int	i;
-
 	/* Pick up arguments */
 	kargs = (void *)__args;
 	initial_howto = kargs->howto;
@@ -244,12 +242,7 @@ main(void)
 		import_geli_boot_data(gbdata);
 #endif /* LOADER_GELI_SUPPORT */
 
-	/*
-	 * March through the device switch probing for things.
-	 */
-	for (i = 0; devsw[i] != NULL; i++)
-		if (devsw[i]->dv_init != NULL)
-			(devsw[i]->dv_init)();
+	devinit();
 
 	printf("BIOS %dkB/%dkB available memory\n", bios_basemem / 1024,
 	    bios_extmem / 1024);
diff --git a/stand/i386/zfsboot/zfsboot.c b/stand/i386/zfsboot/zfsboot.c
index ea390b6ea7a6..1d64ace063e0 100644
--- a/stand/i386/zfsboot/zfsboot.c
+++ b/stand/i386/zfsboot/zfsboot.c
@@ -211,9 +211,7 @@ main(void)
 	env_setenv("currdev", EV_VOLATILE, "", i386_setcurrdev,
 	    env_nounset);
 
-	for (i = 0; devsw[i] != NULL; i++)
-		if (devsw[i]->dv_init != NULL)
-			(devsw[i]->dv_init)();
+	devinit();
 
 	/* XXX assumes this will be a disk, but it looks likely give above */
 	disk_parsedev((struct devdesc **)&devdesc, boot_devname + 4, NULL);
diff --git a/stand/libsa/dev.c b/stand/libsa/dev.c
index c0bcce07718b..158b4d69381b 100644
--- a/stand/libsa/dev.c
+++ b/stand/libsa/dev.c
@@ -149,3 +149,21 @@ devparse(struct devdesc **dev, const char *devspec, const char **path)
 		free(idev);
 	return (0);
 }
+
+int
+devinit(void)
+{
+	int err = 0;
+
+	/*
+	 * March through the device switch probing for things.
+	 */
+	for (int i = 0; devsw[i] != NULL; i++) {
+		if (devsw[i]->dv_init != NULL) {
+			if ((devsw[i]->dv_init)() != 0) {
+				err++;
+			}
+		}
+	}
+	return (err);
+}
diff --git a/stand/libsa/libsa.3 b/stand/libsa/libsa.3
index 210f54a5aa6a..fcfd22cbf0d0 100644
--- a/stand/libsa/libsa.3
+++ b/stand/libsa/libsa.3
@@ -537,6 +537,14 @@ is non-NULL, then a pointer to the remainder of the
 .Dv devdesc
 string after the device specification is written.
 .It Xo
+.Ft int
+.Fn devinit void
+Calls all the
+.Fa dv_init
+routines in the
+.Dv devsw
+array, returning the number of routines that returned an error.
+.It Xo
 .Ft void
 .Fn twiddle void
 .Xc
diff --git a/stand/libsa/stand.h b/stand/libsa/stand.h
index 6e52325ec166..660337db6980 100644
--- a/stand/libsa/stand.h
+++ b/stand/libsa/stand.h
@@ -188,6 +188,7 @@ struct devdesc {
 
 char *devformat(struct devdesc *d);
 int devparse(struct devdesc **, const char *, const char **);
+int devinit(void);
 
 struct open_file {
     int			f_flags;	/* see F_* below */
diff --git a/stand/powerpc/ofw/main.c b/stand/powerpc/ofw/main.c
index 81195d3f2444..177e665a4dc7 100644
--- a/stand/powerpc/ofw/main.c
+++ b/stand/powerpc/ofw/main.c
@@ -182,12 +182,7 @@ main(int (*openfirm)(void *))
 	/* Set up currdev variable to have hooks in place. */
 	env_setenv("currdev", EV_VOLATILE, "", ofw_setcurrdev, env_nounset);
 
-	/*
-	 * March through the device switch probing for things.
-	 */
-	for (i = 0; devsw[i] != NULL; i++)
-		if (devsw[i]->dv_init != NULL)
-			(devsw[i]->dv_init)();
+	devinit();
 
 	printf("\n%s", bootprog_info);
 	printf("Memory: %lldKB\n", memsize() / 1024);
diff --git a/stand/userboot/userboot/main.c b/stand/userboot/userboot/main.c
index 9ede0cd360e9..6ec5c5ddbbb6 100644
--- a/stand/userboot/userboot/main.c
+++ b/stand/userboot/userboot/main.c
@@ -206,13 +206,7 @@ loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
 	 * Initialise the block cache. Set the upper limit.
 	 */
 	bcache_init(32768, 512);
-	/*
-	 * March through the device switch probing for things.
-	 */
-	for (i = 0; devsw[i] != NULL; i++)
-		if (devsw[i]->dv_init != NULL)
-			(devsw[i]->dv_init)();
-
+	devinit();
 	extract_currdev();
 
 	/*