1 /*	$OpenBSD: i386_softraid.c,v 1.14 2020/03/07 15:11:50 otto Exp $	*/
2 /*
3  * Copyright (c) 2012 Joel Sing <jsing@openbsd.org>
4  * Copyright (c) 2010 Otto Moerbeek <otto@drijf.net>
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/param.h>	/* DEV_BSIZE */
20 #include <sys/disklabel.h>
21 #include <sys/dkio.h>
22 #include <sys/ioctl.h>
23 #include <sys/stat.h>
24 
25 #include <dev/biovar.h>
26 #include <dev/softraidvar.h>
27 #include <ufs/ufs/dinode.h>
28 
29 #include <err.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <util.h>
36 
37 #include "installboot.h"
38 #include "i386_installboot.h"
39 
40 void	sr_install_bootblk(int, int, int);
41 void	sr_install_bootldr(int, char *);
42 
43 void
44 sr_install_bootblk(int devfd, int vol, int disk)
45 {
46 	struct bioc_disk bd;
47 	struct disklabel dl;
48 	struct partition *pp;
49 	uint32_t poffset;
50 	char *dev;
51 	char part, efipart;
52 	int diskfd;
53 
54 	/* Get device name for this disk/chunk. */
55 	memset(&bd, 0, sizeof(bd));
56 	bd.bd_volid = vol;
57 	bd.bd_diskid = disk;
58 	if (ioctl(devfd, BIOCDISK, &bd) == -1)
59 		err(1, "BIOCDISK");
60 
61 	/* Check disk status. */
62 	if (bd.bd_status != BIOC_SDONLINE && bd.bd_status != BIOC_SDREBUILD) {
63 		fprintf(stderr, "softraid chunk %u not online - skipping...\n",
64 		    disk);
65 		return;
66 	}
67 
68 	if (strlen(bd.bd_vendor) < 1)
69 		errx(1, "invalid disk name");
70 	part = bd.bd_vendor[strlen(bd.bd_vendor) - 1];
71 	if (part < 'a' || part >= 'a' + MAXPARTITIONS)
72 		errx(1, "invalid partition %c\n", part);
73 	bd.bd_vendor[strlen(bd.bd_vendor) - 1] = '\0';
74 
75 	/* Open this device and check its disklabel. */
76 	if ((diskfd = opendev(bd.bd_vendor, (nowrite? O_RDONLY:O_RDWR),
77 	    OPENDEV_PART, &dev)) == -1)
78 		err(1, "open: %s", dev);
79 
80 	/* Get and check disklabel. */
81 	if (ioctl(diskfd, DIOCGDINFO, &dl) == -1)
82 		err(1, "disklabel: %s", dev);
83 	if (dl.d_magic != DISKMAGIC)
84 		err(1, "bad disklabel magic=0x%08x", dl.d_magic);
85 
86 	/* Warn on unknown disklabel types. */
87 	if (dl.d_type == 0)
88 		warnx("disklabel type unknown");
89 
90 	efipart = findgptefisys(diskfd, &dl);
91 	if (efipart != -1) {
92 		write_efisystem(&dl, (char)efipart);
93 		return;
94 	}
95 
96 	/* Determine poffset and set symbol value. */
97 	pp = &dl.d_partitions[part - 'a'];
98 	if (pp->p_offseth != 0)
99 		errx(1, "partition offset too high");
100 	poffset = pp->p_offset;			/* Offset of RAID partition. */
101 	poffset += SR_BOOT_LOADER_OFFSET;	/* SR boot loader area. */
102 	sym_set_value(pbr_symbols, "_p_offset", poffset);
103 
104 	if (verbose)
105 		fprintf(stderr, "%s%c: installing boot blocks on %s, "
106 		    "part offset %u\n", bd.bd_vendor, part, dev, poffset);
107 
108 	/* Write boot blocks to device. */
109 	write_bootblocks(diskfd, dev, &dl);
110 
111 	close(diskfd);
112 }
113 
114 void
115 sr_install_bootldr(int devfd, char *dev)
116 {
117 	struct bioc_installboot bb;
118 	struct stat sb;
119 	struct ufs1_dinode *ino_p;
120 	uint32_t bootsize, inodeblk, inodedbl;
121 	uint16_t bsize = SR_FS_BLOCKSIZE;
122 	uint16_t nblocks;
123 	uint8_t bshift = 5;		/* fragsize == blocksize */
124 	int fd, i;
125 	u_char *p;
126 
127 	/*
128 	 * Install boot loader into softraid boot loader storage area.
129 	 *
130 	 * In order to allow us to reuse the existing biosboot we construct
131 	 * a fake FFS filesystem with a single inode, which points to the
132 	 * boot loader.
133 	 */
134 
135 	nblocks = howmany(SR_BOOT_LOADER_SIZE, SR_FS_BLOCKSIZE / DEV_BSIZE);
136 	inodeblk = nblocks - 1;
137 	bootsize = nblocks * SR_FS_BLOCKSIZE;
138 
139 	p = calloc(1, bootsize);
140 	if (p == NULL)
141 		err(1, NULL);
142 
143 	fd = open(stage2, O_RDONLY, 0);
144 	if (fd == -1)
145 		err(1, NULL);
146 
147 	if (fstat(fd, &sb) == -1)
148 		err(1, NULL);
149 
150 	nblocks = howmany(sb.st_blocks, SR_FS_BLOCKSIZE / DEV_BSIZE);
151 	if (sb.st_blocks * S_BLKSIZE > bootsize -
152 	    (int)(sizeof(struct ufs1_dinode)))
153 		errx(1, "boot code will not fit");
154 
155 	/* We only need to fill the direct block array. */
156 	ino_p = (struct ufs1_dinode *)&p[bootsize - sizeof(struct ufs1_dinode)];
157 
158 	ino_p->di_mode = sb.st_mode;
159 	ino_p->di_nlink = 1;
160 	ino_p->di_inumber = 0xfeebfaab;
161 	ino_p->di_size = read(fd, p, sb.st_blocks * S_BLKSIZE);
162 	ino_p->di_blocks = nblocks;
163 	for (i = 0; i < nblocks; i++)
164 		ino_p->di_db[i] = i;
165 
166 	inodedbl = ((u_char*)&ino_p->di_db[0] -
167 	    &p[bootsize - SR_FS_BLOCKSIZE]) + INODEOFF;
168 
169 	memset(&bb, 0, sizeof(bb));
170 	bb.bb_bootldr = p;
171 	bb.bb_bootldr_size = bootsize;
172 	bb.bb_bootblk = "XXX";
173 	bb.bb_bootblk_size = sizeof("XXX");
174 	strncpy(bb.bb_dev, dev, sizeof(bb.bb_dev));
175 	if (!nowrite) {
176 		if (verbose)
177 			fprintf(stderr, "%s: installing boot loader on "
178 			    "softraid volume\n", dev);
179 		if (ioctl(devfd, BIOCINSTALLBOOT, &bb) == -1)
180 			errx(1, "softraid installboot failed");
181 	}
182 
183 	/*
184 	 * Set the values that will need to go into biosboot
185 	 * (the partition boot record, a.k.a. the PBR).
186 	 */
187 	sym_set_value(pbr_symbols, "_fs_bsize_p", (bsize / 16));
188 	sym_set_value(pbr_symbols, "_fs_bsize_s", (bsize / 512));
189 	sym_set_value(pbr_symbols, "_fsbtodb", bshift);
190 	sym_set_value(pbr_symbols, "_inodeblk", inodeblk);
191 	sym_set_value(pbr_symbols, "_inodedbl", inodedbl);
192 	sym_set_value(pbr_symbols, "_nblocks", nblocks);
193 
194 	if (verbose)
195 		fprintf(stderr, "%s is %d blocks x %d bytes\n",
196 		    stage2, nblocks, bsize);
197 
198 	free(p);
199 	close(fd);
200 }
201