1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/* $OpenBSD: installboot.c,v 1.14 2012/01/11 16:15:02 jsing Exp $ */
/* $NetBSD: installboot.c,v 1.8 2001/02/19 22:48:59 cgd Exp $ */
/*-
* Copyright (c) 2012 Joel Sing <jsing@openbsd.org>
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Kranenburg.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/disklabel.h>
#include <sys/dkio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dev/biovar.h>
#include <dev/softraidvar.h>
#include <ufs/ffs/fs.h>
#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
int verbose, nowrite;
char *dev, *blkstore;
size_t blksize;
static void usage(void);
int main(int, char *[]);
static void write_bootblk(int);
static int sr_volume(int, int *, int *);
static void sr_installboot(int);
static void sr_install_bootblk(int, int, int);
static void
usage(void)
{
extern char *__progname;
fprintf(stderr, "Usage: %s [-nv] <bootblk> <device>\n", __progname);
exit(1);
}
int
main(int argc, char *argv[])
{
char *blkfile, *realdev;
int vol = -1, ndisks = 0, disk;
int c, devfd, blkfd;
struct stat sb;
while ((c = getopt(argc, argv, "nv")) != -1) {
switch (c) {
case 'n':
/* Do not actually write the bootblock to disk. */
nowrite = 1;
break;
case 'v':
/* Chat */
verbose = 1;
break;
default:
usage();
}
}
if (argc - optind < 2)
usage();
blkfile = argv[optind++];
dev = argv[optind];
if (verbose)
printf("bootblk: %s\n", blkfile);
if ((blkfd = open(blkfile, O_RDONLY)) < 0)
err(1, "open: %s", blkfile);
if (fstat(blkfd, &sb) == -1)
err(1, "fstat: %s", blkfile);
if (sb.st_size == 0)
errx(1, "%s is empty", blkfile);
blksize = howmany(sb.st_size, DEV_BSIZE) * DEV_BSIZE;
if (blksize > SBSIZE - DEV_BSIZE)
errx(1, "boot blocks too big");
if ((blkstore = malloc(blksize)) == NULL)
err(1, "malloc: %s", blkfile);
bzero(blkstore, blksize);
if (read(blkfd, blkstore, sb.st_size) != sb.st_size)
err(1, "read: %s", blkfile);
if ((devfd = opendev(dev, (nowrite ? O_RDONLY : O_RDWR),
OPENDEV_PART, &realdev)) < 0)
err(1, "open: %s", realdev);
if (verbose)
printf("device: %s\n", realdev);
if (sr_volume(devfd, &vol, &ndisks)) {
/* Install boot loader in softraid volume. */
sr_installboot(devfd);
/* Install bootblk on each disk that is part of this volume. */
for (disk = 0; disk < ndisks; disk++)
sr_install_bootblk(devfd, vol, disk);
} else {
/* Write boot blocks to device. */
write_bootblk(devfd);
}
close(devfd);
}
static void
write_bootblk(int devfd)
{
/*
* Write bootblock into the superblock.
*/
if (nowrite)
return;
if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE)
err(1, "lseek boot block");
/* Sync filesystems (to clean in-memory superblock?) */
sync();
if (write(devfd, blkstore, blksize) != blksize)
err(1, "write boot block");
}
static int
sr_volume(int devfd, int *vol, int *disks)
{
struct bioc_inq bi;
struct bioc_vol bv;
int rv, i;
/* Get volume information. */
memset(&bi, 0, sizeof(bi));
rv = ioctl(devfd, BIOCINQ, &bi);
if (rv == -1)
return 0;
/* XXX - softraid volumes will always have a "softraid0" controller. */
if (strncmp(bi.bi_dev, "softraid0", sizeof("softraid0")))
return 0;
/* Locate specific softraid volume. */
for (i = 0; i < bi.bi_novol; i++) {
memset(&bv, 0, sizeof(bv));
bv.bv_volid = i;
rv = ioctl(devfd, BIOCVOL, &bv);
if (rv == -1)
err(1, "BIOCVOL");
if (strncmp(dev, bv.bv_dev, sizeof(bv.bv_dev)) == 0) {
*vol = i;
*disks = bv.bv_nodisk;
break;
}
}
if (verbose)
fprintf(stderr, "%s: softraid volume with %i disk(s)\n",
dev, *disks);
return 1;
}
static void
sr_installboot(int devfd)
{
struct bioc_installboot bb;
struct stat sb;
int fd, i, rv;
u_char *p;
/*
* Install boot loader into softraid boot loader storage area.
*/
bb.bb_bootldr = "XXX";
bb.bb_bootldr_size = sizeof("XXX");
bb.bb_bootblk = blkstore;
bb.bb_bootblk_size = blksize;
strncpy(bb.bb_dev, dev, sizeof(bb.bb_dev));
if (!nowrite) {
if (verbose)
fprintf(stderr, "%s: installing boot loader on "
"softraid volume\n", dev);
rv = ioctl(devfd, BIOCINSTALLBOOT, &bb);
if (rv != 0)
errx(1, "softraid installboot failed");
}
}
static void
sr_install_bootblk(int devfd, int vol, int disk)
{
struct bioc_disk bd;
struct disklabel dl;
struct partition *pp;
uint32_t poffset;
char *realdev;
char part;
int diskfd;
int rv;
/* Get device name for this disk/chunk. */
memset(&bd, 0, sizeof(bd));
bd.bd_volid = vol;
bd.bd_diskid = disk;
rv = ioctl(devfd, BIOCDISK, &bd);
if (rv == -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;
}
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)) < 0)
err(1, "open: %s", realdev);
if (verbose)
fprintf(stderr, "%s%c: installing boot blocks on %s\n",
bd.bd_vendor, part, realdev);
/* Write boot blocks to device. */
write_bootblk(diskfd);
close(diskfd);
}
|