xref: /openbsd-src/usr.sbin/installboot/efi_softraid.c (revision a129f1317d7010208a741d8c4ee87d77103a5a15)
1 /*	$OpenBSD: efi_softraid.c,v 1.2 2022/08/29 18:54: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 	if (strlen(bd.bd_vendor) < 1)
58 		errx(1, "invalid disk name");
59 	part = bd.bd_vendor[strlen(bd.bd_vendor) - 1];
60 	if (part < 'a' || part >= 'a' + MAXPARTITIONS)
61 		errx(1, "invalid partition %c\n", part);
62 	bd.bd_vendor[strlen(bd.bd_vendor) - 1] = '\0';
63 
64 	/* Open device. */
65 	if ((diskfd = opendev(bd.bd_vendor, (nowrite ? O_RDONLY : O_RDWR),
66 	    OPENDEV_PART, &realdev)) == -1)
67 		err(1, "open: %s", realdev);
68 
69 	if (verbose)
70 		fprintf(stderr, "%s%c: %s boot blocks on %s\n", bd.bd_vendor,
71 		    part, (nowrite ? "would install" : "installing"), realdev);
72 
73 	/* Write boot blocks to device. */
74 	md_installboot(diskfd, realdev);
75 
76 	close(diskfd);
77 }
78 
79 void
80 sr_install_bootldr(int devfd, char *dev)
81 {
82 	/*
83 	 * EFI platforms have a single stage bootstrap.
84 	 * sr_install_bootblk() installs it on each softraid chunk.
85 	 * The softraid volume does not require any bootstrap code.
86 	 */
87 }
88