It does not have the prettiest signature, but nicely folds identical
copies into MI softraid.c, which then allows us to
- avoid further diverging MD code
- implement the keydisk fix on tech@ once instead of thrice
- reuse sr_open_chunk() in an upcoming diff to make -p softraid aware

The last point is especially useful, otherwise I'd have to copy what
sr_open_chunk() does yet another three times (each *_softraid.c file),
turning it a much simpler and smaller diff.

This conflicts with the keydisk fix on tech@
"Softraid crypto with keydisk and installboot, skip on the same disk".

It does not really matter which one we land first as the actual keydisk
fix won't change, it just gets added either once or thrice.

I've successfully tested the diff below together with the keydisk fix
as well as the upcoming -p/softraid diff on amd64, arm64 and sparc64.

Feedback? OK?

Index: efi_softraid.c
===================================================================
RCS file: /cvs/src/usr.sbin/installboot/efi_softraid.c,v
retrieving revision 1.2
diff -u -p -r1.2 efi_softraid.c
--- efi_softraid.c      29 Aug 2022 18:54:43 -0000      1.2
+++ efi_softraid.c      10 Sep 2022 01:52:49 -0000
@@ -16,18 +16,9 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/types.h>
-#include <sys/disklabel.h>
-#include <sys/ioctl.h>
-
 #include <dev/biovar.h>
-#include <dev/softraidvar.h>
 
-#include <err.h>
-#include <fcntl.h>
 #include <stdio.h>
-#include <string.h>
-#include <util.h>
 #include <unistd.h>
 
 #include "installboot.h"
@@ -40,31 +31,9 @@ sr_install_bootblk(int devfd, int vol, i
        int diskfd;
        char part;
 
-       /* Get device name for this disk/chunk. */
-       memset(&bd, 0, sizeof(bd));
-       bd.bd_volid = vol;
-       bd.bd_diskid = disk;
-       if (ioctl(devfd, BIOCDISK, &bd) == -1)
-               err(1, "BIOCDISK");
-
-       /* Check disk status. */
-       if (bd.bd_status != BIOC_SDONLINE && bd.bd_status != BIOC_SDREBUILD) {
-               fprintf(stderr, "softraid chunk %u not online - skipping...\n",
-                   disk);
+       diskfd = sr_open_chunk(devfd, vol, disk, &bd, &realdev, &part);
+       if (diskfd == -1)
                return;
-       }
-
-       if (strlen(bd.bd_vendor) < 1)
-               errx(1, "invalid disk name");
-       part = bd.bd_vendor[strlen(bd.bd_vendor) - 1];
-       if (part < 'a' || part >= 'a' + MAXPARTITIONS)
-               errx(1, "invalid partition %c\n", part);
-       bd.bd_vendor[strlen(bd.bd_vendor) - 1] = '\0';
-
-       /* Open device. */
-       if ((diskfd = opendev(bd.bd_vendor, (nowrite ? O_RDONLY : O_RDWR),
-           OPENDEV_PART, &realdev)) == -1)
-               err(1, "open: %s", realdev);
 
        if (verbose)
                fprintf(stderr, "%s%c: %s boot blocks on %s\n", bd.bd_vendor,
Index: i386_softraid.c
===================================================================
RCS file: /cvs/src/usr.sbin/installboot/i386_softraid.c,v
retrieving revision 1.19
diff -u -p -r1.19 i386_softraid.c
--- i386_softraid.c     29 Aug 2022 18:54:43 -0000      1.19
+++ i386_softraid.c     10 Sep 2022 01:52:49 -0000
@@ -32,7 +32,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <util.h>
 
 #include "installboot.h"
 #include "i386_installboot.h"
@@ -51,31 +50,9 @@ sr_install_bootblk(int devfd, int vol, i
        char part, efipart;
        int diskfd;
 
-       /* Get device name for this disk/chunk. */
-       memset(&bd, 0, sizeof(bd));
-       bd.bd_volid = vol;
-       bd.bd_diskid = disk;
-       if (ioctl(devfd, BIOCDISK, &bd) == -1)
-               err(1, "BIOCDISK");
-
-       /* Check disk status. */
-       if (bd.bd_status != BIOC_SDONLINE && bd.bd_status != BIOC_SDREBUILD) {
-               fprintf(stderr, "softraid chunk %u not online - skipping...\n",
-                   disk);
+       diskfd = sr_open_chunk(devfd, vol, disk, &bd, &dev, &part);
+       if (diskfd == -1)
                return;
-       }
-
-       if (strlen(bd.bd_vendor) < 1)
-               errx(1, "invalid disk name");
-       part = bd.bd_vendor[strlen(bd.bd_vendor) - 1];
-       if (part < 'a' || part >= 'a' + MAXPARTITIONS)
-               errx(1, "invalid partition %c\n", part);
-       bd.bd_vendor[strlen(bd.bd_vendor) - 1] = '\0';
-
-       /* Open this device and check its disklabel. */
-       if ((diskfd = opendev(bd.bd_vendor, (nowrite? O_RDONLY:O_RDWR),
-           OPENDEV_PART, &dev)) == -1)
-               err(1, "open: %s", dev);
 
        /* Get and check disklabel. */
        if (ioctl(diskfd, DIOCGDINFO, &dl) == -1)
Index: installboot.h
===================================================================
RCS file: /cvs/src/usr.sbin/installboot/installboot.h,v
retrieving revision 1.14
diff -u -p -r1.14 installboot.h
--- installboot.h       3 Feb 2022 10:25:14 -0000       1.14
+++ installboot.h       10 Sep 2022 01:52:49 -0000
@@ -43,6 +43,7 @@ void  md_prepareboot(int, char *);
 void   md_installboot(int, char *);
 
 #ifdef SOFTRAID
+int    sr_open_chunk(int, int, int, struct bioc_disk *, char **, char *);
 void   sr_installboot(int, char *);
 void   sr_install_bootblk(int, int, int);
 void   sr_install_bootldr(int, char *);
Index: softraid.c
===================================================================
RCS file: /cvs/src/usr.sbin/installboot/softraid.c,v
retrieving revision 1.5
diff -u -p -r1.5 softraid.c
--- softraid.c  8 Jun 2020 19:17:12 -0000       1.5
+++ softraid.c  10 Sep 2022 01:53:01 -0000
@@ -15,14 +15,18 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <sys/types.h>
+#include <sys/disklabel.h>
 #include <sys/dkio.h>
 #include <sys/ioctl.h>
 
 #include <dev/biovar.h>
 
 #include <err.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <util.h>
 
 #include "installboot.h"
 
@@ -107,4 +111,40 @@ sr_status(struct bio_status *bs)
                else
                        exit(1);
        }
+}
+
+int
+sr_open_chunk(int devfd, int vol, int disk, struct bioc_disk *bd,
+    char **realdev, char *part)
+{
+       int diskfd;
+
+       /* Get device name for this disk/chunk. */
+       memset(bd, 0, sizeof(*bd));
+       bd->bd_volid = vol;
+       bd->bd_diskid = disk;
+       if (ioctl(devfd, BIOCDISK, bd) == -1)
+               err(1, "BIOCDISK");
+
+       /* Check disk status. */
+       if (bd->bd_status != BIOC_SDONLINE &&
+           bd->bd_status != BIOC_SDREBUILD) {
+               fprintf(stderr, "softraid chunk %u not online - skipping...\n",
+                   disk);
+               return -1;
+       }
+
+       if (strlen(bd->bd_vendor) < 1)
+               errx(1, "invalid disk name");
+       *part = bd->bd_vendor[strlen(bd->bd_vendor) - 1];
+       if (*part < 'a' || *part >= 'a' + MAXPARTITIONS)
+               errx(1, "invalid partition %c\n", *part);
+       bd->bd_vendor[strlen(bd->bd_vendor) - 1] = '\0';
+
+       /* Open device. */
+       if ((diskfd = opendev(bd->bd_vendor, (nowrite ? O_RDONLY : O_RDWR),
+           OPENDEV_PART, realdev)) == -1)
+               err(1, "open: %s", *realdev);
+
+       return diskfd;
 }
Index: sparc64_softraid.c
===================================================================
RCS file: /cvs/src/usr.sbin/installboot/sparc64_softraid.c,v
retrieving revision 1.6
diff -u -p -r1.6 sparc64_softraid.c
--- sparc64_softraid.c  29 Aug 2022 18:54:43 -0000      1.6
+++ sparc64_softraid.c  10 Sep 2022 01:52:50 -0000
@@ -15,19 +15,14 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/types.h>
-#include <sys/disklabel.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 
 #include <dev/biovar.h>
-#include <dev/softraidvar.h>
 
 #include <err.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
-#include <util.h>
 #include <unistd.h>
 
 #include "installboot.h"
@@ -41,31 +36,9 @@ sr_install_bootblk(int devfd, int vol, i
        int diskfd;
        char part;
 
-       /* Get device name for this disk/chunk. */
-       memset(&bd, 0, sizeof(bd));
-       bd.bd_volid = vol;
-       bd.bd_diskid = disk;
-       if (ioctl(devfd, BIOCDISK, &bd) == -1)
-               err(1, "BIOCDISK");
-
-       /* Check disk status. */
-       if (bd.bd_status != BIOC_SDONLINE && bd.bd_status != BIOC_SDREBUILD) {
-               fprintf(stderr, "softraid chunk %u not online - skipping...\n",
-                   disk);
+       diskfd = sr_open_chunk(devfd, vol, disk, &bd, &realdev, &part);
+       if (diskfd == -1)
                return;
-       }
-
-       if (strlen(bd.bd_vendor) < 1)
-               errx(1, "invalid disk name");
-       part = bd.bd_vendor[strlen(bd.bd_vendor) - 1];
-       if (part < 'a' || part >= 'a' + MAXPARTITIONS)
-               errx(1, "invalid partition %c\n", part);
-       bd.bd_vendor[strlen(bd.bd_vendor) - 1] = '\0';
-
-       /* Open device. */
-       if ((diskfd = opendev(bd.bd_vendor, (nowrite ? O_RDONLY : O_RDWR),
-           OPENDEV_PART, &realdev)) == -1)
-               err(1, "open: %s", realdev);
 
        if (verbose)
                fprintf(stderr, "%s%c: %s boot blocks on %s\n", bd.bd_vendor,

Reply via email to