summaryrefslogtreecommitdiff
path: root/usr.sbin/installboot/installboot.c
blob: 47f321e202e6b62eb4d116205e4e2823e1578837 (plain)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*	$NetBSD: installboot.c,v 1.10 2002/05/20 14:38:38 lukem Exp $	*/

/*-
 * Copyright (c) 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Luke Mewburn of Wasabi Systems.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the NetBSD
 *	Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 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/cdefs.h>
#if defined(__RCSID) && !defined(__lint)
__RCSID("$NetBSD: installboot.c,v 1.10 2002/05/20 14:38:38 lukem Exp $");
#endif	/* !__lint */

#include <sys/utsname.h>

#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "installboot.h"

int		main(int, char *[]);
static	int	getmachine(ib_params *, const char *, const char *);
static	int	getfstype(ib_params *, const char *, const char *);
static	void	usage(void);

static	ib_params	installboot_params;

int
main(int argc, char *argv[])
{
	struct utsname	utsname;
	ib_params	*params;
	unsigned long	lval;
	int		ch, rv, mode;
	char 		*p;
	const char	*op;

	setprogname(argv[0]);
	params = &installboot_params;
	memset(params, 0, sizeof(*params));
	params->fsfd = -1;
	params->s1fd = -1;
	if ((p = getenv("MACHINE")) != NULL)
		if (! getmachine(params, p, "$MACHINE"))
			exit(1);

	while ((ch = getopt(argc, argv, "b:B:cm:no:t:v")) != -1) {
		switch (ch) {

		case 'b':
		case 'B':
			if (*optarg == '\0')
				goto badblock;
			lval = strtoul(optarg, &p, 0);
			if (lval > UINT32_MAX || *p != '\0') {
 badblock:
				errx(1, "Invalid block number `%s'", optarg);
			}
			if (ch == 'b') {
				params->s1start = (uint32_t)lval;
				params->flags |= IB_STAGE1START;
			} else {
				params->s2start = (uint32_t)lval;
				params->flags |= IB_STAGE2START;
			}
			break;

		case 'c':
			params->flags |= IB_CLEAR;
			break;

		case 'm':
			if (! getmachine(params, optarg, "-m"))
				exit(1);
			break;

		case 'n':
			params->flags |= IB_NOWRITE;
			break;

		case 'o':
			if (params->machine == NULL)
				errx(1,
				    "Machine needs to be specified before -o");
			while ((p = strsep(&optarg, ",")) != NULL) {
				if (*p == '\0')
					errx(1, "Empty `-o' option");
				if (! params->machine->parseopt(params, p))
					exit(1);
			}
			break;

		case 't':
			if (! getfstype(params, optarg, "-t"))
				exit(1);
			break;

		case 'v':
			params->flags |= IB_VERBOSE;
			break;

		case '?':
		default:
			usage();
			/* NOTREACHED */

		}
	}
	argc -= optind;
	argv += optind;

	if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
	    ((params->flags & IB_CLEAR) == 0 && (argc < 2 || argc > 3)))
		usage();

		/* set missing defaults */
	if (params->machine == NULL) {
		if (uname(&utsname) == -1)
			err(1, "Determine uname");
		if (! getmachine(params, utsname.machine, "uname()"))
			exit(1);
	}

	params->filesystem = argv[0];
	if (params->flags & IB_NOWRITE) {
		op = "only";
		mode = O_RDONLY;
	} else {
		op = "write";
		mode = O_RDWR;
	}
	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
		err(1, "Opening file system `%s' read-%s",
		    params->filesystem, op);
	if (fstat(params->fsfd, &params->fsstat) == -1)
		err(1, "Examining file system `%s'", params->filesystem);
	if (params->fstype != NULL) {
		if (! params->fstype->match(params))
			err(1, "File system `%s' is not of type %s",
			    params->filesystem, params->fstype->name);
	} else {
		params->fstype = &fstypes[0];
		while (params->fstype->name != NULL &&
			! params->fstype->match(params))
			params->fstype++;
		if (params->fstype->name == NULL)
			errx(1, "File system `%s' is of an unknown type",
			    params->filesystem);
	}

	if (argc >= 2) {
		params->stage1 = argv[1];
		if ((params->s1fd = open(params->stage1, O_RDONLY, 0600))
		    == -1)
			err(1, "Opening primary bootstrap `%s'",
			    params->stage1);
		if (fstat(params->s1fd, &params->s1stat) == -1)
			err(1, "Examining primary bootstrap `%s'",
			    params->stage1);
		if (!S_ISREG(params->s1stat.st_mode))
			err(1, "`%s' must be a regular file", params->stage1);
	}
	if (argc == 3) {
		params->stage2 = argv[2];
	}
	assert(params->machine != NULL);

	if (params->flags & IB_VERBOSE) {
		printf("File system:         %s\n", params->filesystem);
		printf("File system type:    %s (blocksize %u, needswap %d)\n",
		    params->fstype->name,
		    params->fstype->blocksize, params->fstype->needswap);
		printf("Primary bootstrap:   %s\n",
		    (params->flags & IB_CLEAR) ? "(to be cleared)"
		    : params->stage1);
		if (params->stage2 != NULL)
			printf("Secondary bootstrap: %s\n", params->stage2);
	}

	if (params->flags & IB_CLEAR) {
		op = "Clear";
		rv = params->machine->clearboot(params);
	} else {
		op = "Set";
		rv = params->machine->setboot(params);
	}
	if (rv == 0)
		errx(1, "%s bootstrap operation failed", op);

	if (S_ISREG(params->fsstat.st_mode)) {
		if (fsync(params->fsfd) == -1)
			err(1, "Synchronising file system `%s'",
			    params->filesystem);
	} else {
		/* Sync filesystems (to clean in-memory superblock?) */
		sync();
	}
	if (close(params->fsfd) == -1)
		err(1, "Closing file system `%s'", params->filesystem);
	if (argc == 2)
		if (close(params->s1fd) == -1)
			err(1, "Closing primary bootstrap `%s'",
			    params->stage1);

	exit(0);
	/* NOTREACHED */
}

int
parseoptionflag(ib_params *params, const char *option, ib_flags wantflags)
{
	struct {
		const char	*name;
		ib_flags	flag;
	} flags[] = {
		{ "alphasum",	IB_ALPHASUM },
		{ "append",	IB_APPEND },
		{ "sunsum",	IB_SUNSUM },
		{ NULL,		0 },
	};

	int	i;

	assert(params != NULL);
	assert(option != NULL);

	for (i = 0; flags[i].name != NULL; i++) {
		if ((strcmp(flags[i].name, option) == 0) &&
		    (wantflags & flags[i].flag)) {
			params->flags |= flags[i].flag;
			return (1);
		}
	}
	return (0);
}

int
no_parseopt(ib_params *params, const char *option)
{

	assert(params != NULL);
	assert(option != NULL);

		/* all options are unsupported */
	warnx("Unsupported -o option `%s'", option);
	return (0);
}

int
no_setboot(ib_params *params)
{

	assert(params != NULL);

		/* bootstrap installation is not supported */
	warnx("%s: bootstrap installation is not supported",
	    params->machine->name);
	return (0);
}

int
no_clearboot(ib_params *params)
{

	assert(params != NULL);

		/* bootstrap removal is not supported */
	warnx("%s: bootstrap removal is not supported",
	    params->machine->name);
	return (0);
}


static int
getmachine(ib_params *param, const char *mach, const char *provider)
{
	const char *prefix;
	int	i;

	if (param != NULL && mach != NULL && provider != NULL) {
		for (i = 0; machines[i].name != NULL; i++) {
			if (strcmp(machines[i].name, mach) == 0) {
				param->machine = &machines[i];
				return (1);
			}
		}
		warnx("Invalid machine `%s' from %s", mach, provider);
	}
	warnx("Supported machines are:");
#define MACHS_PER_LINE	9
	prefix="";
	for (i = 0; machines[i].name != NULL; i++) {
		if (i == 0)
			prefix="\t";
		else if (i % MACHS_PER_LINE)
			prefix=", ";
		else
			prefix=",\n\t";
		fprintf(stderr, "%s%s", prefix, machines[i].name);
	}
	fputs("\n", stderr);
	return (0);
}

static int
getfstype(ib_params *param, const char *fstype, const char *provider)
{
	const char *prefix;
	int	i;

	if (param != NULL && fstype != NULL && provider != NULL) {
		for (i = 0; fstypes[i].name != NULL; i++) {
			if (strcmp(fstypes[i].name, fstype) == 0) {
				param->fstype = &fstypes[i];
				return (1);
			}
		}
		warnx("Invalid file system type `%s' from %s",
		    fstype, provider);
	}
	warnx("Supported file system types are:");
#define FSTYPES_PER_LINE	9
	prefix="";
	for (i = 0; fstypes[i].name != NULL; i++) {
		if (i == 0)
			prefix="\t";
		else if (i % FSTYPES_PER_LINE)
			prefix=", ";
		else
			prefix=",\n\t";
		fprintf(stderr, "%s%s", prefix, fstypes[i].name);
	}
	fputs("\n", stderr);
	return (0);
}

static void
usage(void)
{
	const char	*prog;

	prog = getprogname();
	fprintf(stderr,
"Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
"\t\t   [-b s1start] [-B s2start] filesystem primary [secondary]\n"
"Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
	    prog, prog);
	getmachine(NULL, NULL, NULL);
	getfstype(NULL, NULL, NULL);
	exit(1);
}