xref: /openbsd-src/usr.sbin/installboot/efi_softraid.c (revision 7510f1853da61ff94685428118a05a01b8842f76)
1 /*	$OpenBSD: efi_softraid.c,v 1.3 2022/10/05 09:58:43 kn Exp $	*/
2 /*
3  * Copyright (c) 2012 Joel Sing <jsing@openbsd.org>
4  * Copyright (c) 2022 Klemens Nanni <kn@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/disklabel.h>
21 #include <sys/ioctl.h>
22 
23 #include <dev/biovar.h>
24 #include <dev/softraidvar.h>
25 
26 #include <err.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <util.h>
31 #include <unistd.h>
32 
33 #include "installboot.h"
34 
35 void
36 sr_install_bootblk(int devfd, int vol, int disk)
37 {
38 	struct bioc_disk bd;
39 	char *realdev;
40 	int diskfd;
41 	char part;
42 
43 	/* Get device name for this disk/chunk. */
44 	memset(&bd, 0, sizeof(bd));
45 	bd.bd_volid = vol;
46 	bd.bd_diskid = disk;
47 	if (ioctl(devfd, BIOCDISK, &bd) == -1)
48 		err(1, "BIOCDISK");
49 
50 	/* Check disk status. */
51 	if (bd.bd_status != BIOC_SDONLINE && bd.bd_status != BIOC_SDREBUILD) {
52 		fprintf(stderr, "softraid chunk %u not online - skipping...\n",
53 		    disk);
54 		return;
55 	}
56 
57 	/* Keydisks always have a size of zero. */
58 	if (bd.bd_size == 0) {
59 		fprintf(stderr, "softraid chunk %u is keydisk - skipping...\n",
60 		    disk);
61 		return;
62 	}
63 
64 	if (strlen(bd.bd_vendor) < 1)
65 		errx(1, "invalid disk name");
66 	part = bd.bd_vendor[strlen(bd.bd_vendor) - 1];
67 	if (part < 'a' || part >= 'a' + MAXPARTITIONS)
68 		errx(1, "invalid partition %c\n", part);
69 	bd.bd_vendor[strlen(bd.bd_vendor) - 1] = '\0';
70 
71 	/* Open device. */
72 	if ((diskfd = opendev(bd.bd_vendor, (nowrite ? O_RDONLY : O_RDWR),
73 	    OPENDEV_PART, &realdev)) == -1)
74 		err(1, "open: %s", realdev);
75 
76 	if (verbose)
77 		fprintf(stderr, "%s%c: %s boot blocks on %s\n", bd.bd_vendor,
78 		    part, (nowrite ? "would install" : "installing"), realdev);
79 
80 	/* Write boot blocks to device. */
81 	md_installboot(diskfd, realdev);
82 
83 	close(diskfd);
84 }
85 
86 void
87 sr_install_bootldr(int devfd, char *dev)
88 {
89 	/*
90 	 * EFI platforms have a single stage bootstrap.
91 	 * sr_install_bootblk() installs it on each softraid chunk.
92 	 * The softraid volume does not require any bootstrap code.
93 	 */
94 }
95