xref: /openbsd-src/usr.sbin/installboot/i386_softraid.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /*	$OpenBSD: i386_softraid.c,v 1.2 2014/06/09 13:13:48 jsing Exp $	*/
2 /*
3  * Copyright (c) 2012 Joel Sing <jsing@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/disklabel.h>
20 #include <sys/dkio.h>
21 #include <sys/ioctl.h>
22 #include <sys/stat.h>
23 
24 #include <dev/biovar.h>
25 #include <dev/softraidvar.h>
26 #include <ufs/ufs/dinode.h>
27 
28 #include <err.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <util.h>
35 
36 #include "installboot.h"
37 #include "i386_installboot.h"
38 
39 void	sr_install_bootblk(int, int, int);
40 void	sr_install_bootldr(int, char *);
41 
42 void
43 sr_install_bootblk(int devfd, int vol, int disk)
44 {
45 	struct bioc_disk bd;
46 	struct disklabel dl;
47 	struct partition *pp;
48 	uint32_t poffset;
49 	char *dev;
50 	char part;
51 	int diskfd;
52 
53 	/* Get device name for this disk/chunk. */
54 	memset(&bd, 0, sizeof(bd));
55 	bd.bd_volid = vol;
56 	bd.bd_diskid = disk;
57 	if (ioctl(devfd, BIOCDISK, &bd) == -1)
58 		err(1, "BIOCDISK");
59 
60 	/* Check disk status. */
61 	if (bd.bd_status != BIOC_SDONLINE && bd.bd_status != BIOC_SDREBUILD) {
62 		fprintf(stderr, "softraid chunk %u not online - skipping...\n",
63 		    disk);
64 		return;
65 	}
66 
67 	if (strlen(bd.bd_vendor) < 1)
68 		errx(1, "invalid disk name");
69 	part = bd.bd_vendor[strlen(bd.bd_vendor) - 1];
70 	if (part < 'a' || part >= 'a' + MAXPARTITIONS)
71 		errx(1, "invalid partition %c\n", part);
72 	bd.bd_vendor[strlen(bd.bd_vendor) - 1] = '\0';
73 
74 	/* Open this device and check its disklabel. */
75 	if ((diskfd = opendev(bd.bd_vendor, (nowrite? O_RDONLY:O_RDWR),
76 	    OPENDEV_PART, &dev)) < 0)
77 		err(1, "open: %s", dev);
78 
79 	/* Get and check disklabel. */
80 	if (ioctl(diskfd, DIOCGDINFO, &dl) != 0)
81 		err(1, "disklabel: %s", dev);
82 	if (dl.d_magic != DISKMAGIC)
83 		err(1, "bad disklabel magic=0x%08x", dl.d_magic);
84 
85 	/* Warn on unknown disklabel types. */
86 	if (dl.d_type == 0)
87 		warnx("disklabel type unknown");
88 
89 	/* Determine poffset and set symbol value. */
90 	pp = &dl.d_partitions[part - 'a'];
91 	if (pp->p_offseth != 0)
92 		errx(1, "partition offset too high");
93 	poffset = pp->p_offset; 		/* Offset of RAID partition. */
94 	poffset += SR_BOOT_LOADER_OFFSET;	/* SR boot loader area. */
95 	sym_set_value(pbr_symbols, "_p_offset", poffset);
96 
97 	if (verbose)
98 		fprintf(stderr, "%s%c: installing boot blocks on %s, "
99 		    "part offset %u\n", bd.bd_vendor, part, dev, poffset);
100 
101 	/* Write boot blocks to device. */
102 	write_bootblocks(diskfd, dev, &dl);
103 
104 	close(diskfd);
105 }
106 
107 void
108 sr_install_bootldr(int devfd, char *dev)
109 {
110 	struct bioc_installboot bb;
111 	struct stat sb;
112 	struct ufs1_dinode *ino_p;
113 	uint32_t bootsize, inodeblk, inodedbl;
114 	uint16_t bsize = SR_FS_BLOCKSIZE;
115 	uint16_t nblocks;
116 	uint8_t bshift = 5;		/* fragsize == blocksize */
117 	int fd, i;
118 	u_char *p;
119 
120 	/*
121 	 * Install boot loader into softraid boot loader storage area.
122 	 *
123 	 * In order to allow us to reuse the existing biosboot we construct
124 	 * a fake FFS filesystem with a single inode, which points to the
125 	 * boot loader.
126 	 */
127 
128 	nblocks = howmany(SR_BOOT_LOADER_SIZE, SR_FS_BLOCKSIZE / DEV_BSIZE);
129 	inodeblk = nblocks - 1;
130 	bootsize = nblocks * SR_FS_BLOCKSIZE;
131 
132 	p = calloc(1, bootsize);
133 	if (p == NULL)
134 		err(1, NULL);
135 
136 	fd = open(stage2, O_RDONLY, 0);
137 	if (fd == -1)
138 		err(1, NULL);
139 
140 	if (fstat(fd, &sb) == -1)
141 		err(1, NULL);
142 
143 	nblocks = howmany(sb.st_blocks, SR_FS_BLOCKSIZE / DEV_BSIZE);
144 	if (sb.st_blocks * S_BLKSIZE > bootsize -
145 	    (int)(sizeof(struct ufs1_dinode)))
146 		errx(1, "boot code will not fit");
147 
148 	/* We only need to fill the direct block array. */
149 	ino_p = (struct ufs1_dinode *)&p[bootsize - sizeof(struct ufs1_dinode)];
150 
151 	ino_p->di_mode = sb.st_mode;
152 	ino_p->di_nlink = 1;
153 	ino_p->di_inumber = 0xfeebfaab;
154 	ino_p->di_size = read(fd, p, sb.st_blocks * S_BLKSIZE);
155 	ino_p->di_blocks = nblocks;
156 	for (i = 0; i < nblocks; i++)
157 		ino_p->di_db[i] = i;
158 
159 	inodedbl = ((u_char*)&ino_p->di_db[0] -
160 	    &p[bootsize - SR_FS_BLOCKSIZE]) + INODEOFF;
161 
162 	memset(&bb, 0, sizeof(bb));
163 	bb.bb_bootldr = p;
164 	bb.bb_bootldr_size = bootsize;
165 	bb.bb_bootblk = "XXX";
166 	bb.bb_bootblk_size = sizeof("XXX");
167 	strncpy(bb.bb_dev, dev, sizeof(bb.bb_dev));
168 	if (!nowrite) {
169 		if (verbose)
170 			fprintf(stderr, "%s: installing boot loader on "
171 			    "softraid volume\n", dev);
172 		if (ioctl(devfd, BIOCINSTALLBOOT, &bb) == -1)
173 			errx(1, "softraid installboot failed");
174 	}
175 
176 	/*
177 	 * Set the values that will need to go into biosboot
178 	 * (the partition boot record, a.k.a. the PBR).
179 	 */
180 	sym_set_value(pbr_symbols, "_fs_bsize_p", (bsize / 16));
181 	sym_set_value(pbr_symbols, "_fs_bsize_s", (bsize / 512));
182 	sym_set_value(pbr_symbols, "_fsbtodb", bshift);
183 	sym_set_value(pbr_symbols, "_inodeblk", inodeblk);
184 	sym_set_value(pbr_symbols, "_inodedbl", inodedbl);
185 	sym_set_value(pbr_symbols, "_nblocks", nblocks);
186 
187 	if (verbose)
188 		fprintf(stderr, "%s is %d blocks x %d bytes\n",
189 		    stage2, nblocks, bsize);
190 
191 	close(fd);
192 }
193