From 092d89168f10fe60c196d3252fccc8f13df20433 Mon Sep 17 00:00:00 2001 From: kn Date: Mon, 7 Nov 2022 15:56:09 +0000 Subject: [PATCH] Merge duplicate MD code into MI sr_open_chunk() It does not have the prettiest signature, but nicely folds identical copies into softraid.c, which then allows us to reuse sr_open_chunk() yet again in an upcoming diff to make -p softraid aware (fixes arm64 installations). Regress keeps passing. Works fine on amd64, arm64 and sparc64. "looks fine" mlarkin for whom this unbreaks a fresh arm64 softraid install --- usr.sbin/installboot/efi_softraid.c | 44 ++-------------------- usr.sbin/installboot/i386_softraid.c | 36 ++---------------- usr.sbin/installboot/installboot.h | 3 +- usr.sbin/installboot/softraid.c | 49 ++++++++++++++++++++++++- usr.sbin/installboot/sparc64_softraid.c | 35 ++---------------- 5 files changed, 59 insertions(+), 108 deletions(-) diff --git a/usr.sbin/installboot/efi_softraid.c b/usr.sbin/installboot/efi_softraid.c index ed1c6423387..7d0ce5823f7 100644 --- a/usr.sbin/installboot/efi_softraid.c +++ b/usr.sbin/installboot/efi_softraid.c @@ -1,4 +1,4 @@ -/* $OpenBSD: efi_softraid.c,v 1.3 2022/10/05 09:58:43 kn Exp $ */ +/* $OpenBSD: efi_softraid.c,v 1.4 2022/11/07 15:56:09 kn Exp $ */ /* * Copyright (c) 2012 Joel Sing * Copyright (c) 2022 Klemens Nanni @@ -16,18 +16,9 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include -#include - #include -#include -#include -#include #include -#include -#include #include #include "installboot.h" @@ -40,38 +31,9 @@ sr_install_bootblk(int devfd, int vol, int disk) 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); - return; - } - - /* Keydisks always have a size of zero. */ - if (bd.bd_size == 0) { - fprintf(stderr, "softraid chunk %u is keydisk - 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, diff --git a/usr.sbin/installboot/i386_softraid.c b/usr.sbin/installboot/i386_softraid.c index 16ae394b146..9d64858331b 100644 --- a/usr.sbin/installboot/i386_softraid.c +++ b/usr.sbin/installboot/i386_softraid.c @@ -1,4 +1,4 @@ -/* $OpenBSD: i386_softraid.c,v 1.20 2022/10/05 09:58:43 kn Exp $ */ +/* $OpenBSD: i386_softraid.c,v 1.21 2022/11/07 15:56:09 kn Exp $ */ /* * Copyright (c) 2012 Joel Sing * Copyright (c) 2010 Otto Moerbeek @@ -32,7 +32,6 @@ #include #include #include -#include #include "installboot.h" #include "i386_installboot.h" @@ -51,38 +50,9 @@ sr_install_bootblk(int devfd, int vol, int disk) 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; - } - - /* Keydisks always have a size of zero. */ - if (bd.bd_size == 0) { - fprintf(stderr, "softraid chunk %u is keydisk - skipping...\n", - disk); - 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) diff --git a/usr.sbin/installboot/installboot.h b/usr.sbin/installboot/installboot.h index 0665592cb92..d70005d2496 100644 --- a/usr.sbin/installboot/installboot.h +++ b/usr.sbin/installboot/installboot.h @@ -1,4 +1,4 @@ -/* $OpenBSD: installboot.h,v 1.14 2022/02/03 10:25:14 visa Exp $ */ +/* $OpenBSD: installboot.h,v 1.15 2022/11/07 15:56:09 kn Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing * @@ -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 *); diff --git a/usr.sbin/installboot/softraid.c b/usr.sbin/installboot/softraid.c index c197164526a..1bb6254631f 100644 --- a/usr.sbin/installboot/softraid.c +++ b/usr.sbin/installboot/softraid.c @@ -1,4 +1,4 @@ -/* $OpenBSD: softraid.c,v 1.5 2020/06/08 19:17:12 kn Exp $ */ +/* $OpenBSD: softraid.c,v 1.6 2022/11/07 15:56:09 kn Exp $ */ /* * Copyright (c) 2012 Joel Sing * @@ -15,14 +15,18 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include +#include #include #include #include #include +#include #include #include +#include #include "installboot.h" @@ -108,3 +112,46 @@ sr_status(struct bio_status *bs) 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; + } + + /* Keydisks always have a size of zero. */ + if (bd->bd_size == 0) { + fprintf(stderr, "softraid chunk %u is keydisk - 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; +} diff --git a/usr.sbin/installboot/sparc64_softraid.c b/usr.sbin/installboot/sparc64_softraid.c index 81fb199326a..c0bf9a834c2 100644 --- a/usr.sbin/installboot/sparc64_softraid.c +++ b/usr.sbin/installboot/sparc64_softraid.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sparc64_softraid.c,v 1.7 2022/10/05 09:58:43 kn Exp $ */ +/* $OpenBSD: sparc64_softraid.c,v 1.8 2022/11/07 15:56:09 kn Exp $ */ /* * Copyright (c) 2012 Joel Sing * @@ -41,38 +41,9 @@ sr_install_bootblk(int devfd, int vol, int disk) 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); - return; - } - - /* Keydisks always have a size of zero. */ - if (bd.bd_size == 0) { - fprintf(stderr, "softraid chunk %u is keydisk - 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, -- 2.20.1