installboot.c revision 1.7 1 1.7 lukem /* $NetBSD: installboot.c,v 1.7 2002/04/30 14:21:17 lukem Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.1 lukem * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.1 lukem * by Luke Mewburn of Wasabi Systems.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted provided that the following conditions
12 1.1 lukem * are met:
13 1.1 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer.
15 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1 lukem * documentation and/or other materials provided with the distribution.
18 1.1 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1 lukem * must display the following acknowledgement:
20 1.1 lukem * This product includes software developed by the NetBSD
21 1.1 lukem * Foundation, Inc. and its contributors.
22 1.1 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 lukem * contributors may be used to endorse or promote products derived
24 1.1 lukem * from this software without specific prior written permission.
25 1.1 lukem *
26 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
37 1.1 lukem */
38 1.1 lukem
39 1.1 lukem #include <sys/cdefs.h>
40 1.1 lukem #if defined(__RCSID) && !defined(__lint)
41 1.7 lukem __RCSID("$NetBSD: installboot.c,v 1.7 2002/04/30 14:21:17 lukem Exp $");
42 1.1 lukem #endif /* !__lint */
43 1.1 lukem
44 1.1 lukem #include <sys/utsname.h>
45 1.1 lukem
46 1.1 lukem #include <assert.h>
47 1.1 lukem #include <err.h>
48 1.1 lukem #include <fcntl.h>
49 1.2 simonb #include <limits.h>
50 1.1 lukem #include <stdio.h>
51 1.6 lukem #include <stdint.h>
52 1.1 lukem #include <stdlib.h>
53 1.1 lukem #include <string.h>
54 1.1 lukem #include <unistd.h>
55 1.1 lukem
56 1.1 lukem #include "installboot.h"
57 1.1 lukem
58 1.1 lukem int main(int, char *[]);
59 1.1 lukem static int getmachine(ib_params *, const char *, const char *);
60 1.6 lukem static int getfstype(ib_params *, const char *, const char *);
61 1.1 lukem static void usage(void);
62 1.1 lukem
63 1.1 lukem static ib_params installboot_params;
64 1.1 lukem
65 1.1 lukem int
66 1.1 lukem main(int argc, char *argv[])
67 1.1 lukem {
68 1.1 lukem struct utsname utsname;
69 1.1 lukem ib_params *params;
70 1.6 lukem unsigned long lval;
71 1.1 lukem int ch, rv, mode;
72 1.1 lukem char *p;
73 1.1 lukem const char *op;
74 1.1 lukem
75 1.1 lukem setprogname(argv[0]);
76 1.1 lukem params = &installboot_params;
77 1.1 lukem memset(params, 0, sizeof(*params));
78 1.1 lukem if ((p = getenv("MACHINE")) != NULL)
79 1.1 lukem if (! getmachine(params, p, "$MACHINE"))
80 1.1 lukem exit(1);
81 1.1 lukem
82 1.1 lukem while ((ch = getopt(argc, argv, "b:cm:no:t:v")) != -1) {
83 1.1 lukem switch (ch) {
84 1.1 lukem
85 1.1 lukem case 'b':
86 1.1 lukem if (*optarg == '\0')
87 1.1 lukem goto badblock;
88 1.6 lukem lval = strtoul(optarg, &p, 0);
89 1.6 lukem if (lval > UINT32_MAX || *p != '\0') {
90 1.1 lukem badblock:
91 1.1 lukem errx(1, "Invalid block number `%s'", optarg);
92 1.1 lukem }
93 1.6 lukem params->startblock = (uint32_t)lval;
94 1.1 lukem params->flags |= IB_STARTBLOCK;
95 1.1 lukem break;
96 1.1 lukem
97 1.1 lukem case 'c':
98 1.1 lukem params->flags |= IB_CLEAR;
99 1.1 lukem break;
100 1.1 lukem
101 1.1 lukem case 'm':
102 1.1 lukem if (! getmachine(params, optarg, "-m"))
103 1.1 lukem exit(1);
104 1.1 lukem break;
105 1.1 lukem
106 1.1 lukem case 'n':
107 1.1 lukem params->flags |= IB_NOWRITE;
108 1.1 lukem break;
109 1.1 lukem
110 1.1 lukem case 'o':
111 1.1 lukem if (params->machine == NULL)
112 1.1 lukem errx(1,
113 1.1 lukem "Machine needs to be specified before -o");
114 1.1 lukem while ((p = strsep(&optarg, ",")) != NULL) {
115 1.1 lukem if (*p == '\0')
116 1.1 lukem errx(1, "Empty `-o' option");
117 1.1 lukem if (! params->machine->parseopt(params, p))
118 1.1 lukem exit(1);
119 1.1 lukem }
120 1.1 lukem break;
121 1.1 lukem
122 1.1 lukem case 't':
123 1.6 lukem if (! getfstype(params, optarg, "-t"))
124 1.6 lukem exit(1);
125 1.1 lukem break;
126 1.1 lukem
127 1.1 lukem case 'v':
128 1.1 lukem params->flags |= IB_VERBOSE;
129 1.1 lukem break;
130 1.1 lukem
131 1.1 lukem case '?':
132 1.1 lukem default:
133 1.1 lukem usage();
134 1.1 lukem /* NOTREACHED */
135 1.1 lukem
136 1.1 lukem }
137 1.1 lukem }
138 1.1 lukem argc -= optind;
139 1.1 lukem argv += optind;
140 1.1 lukem
141 1.1 lukem if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
142 1.5 lukem ((params->flags & IB_CLEAR) == 0 && (argc < 2 || argc > 3)))
143 1.1 lukem usage();
144 1.1 lukem
145 1.1 lukem /* set missing defaults */
146 1.1 lukem if (params->machine == NULL) {
147 1.1 lukem if (uname(&utsname) == -1)
148 1.1 lukem err(1, "Determine uname");
149 1.1 lukem if (! getmachine(params, utsname.machine, "uname()"))
150 1.1 lukem exit(1);
151 1.1 lukem }
152 1.1 lukem
153 1.1 lukem params->filesystem = argv[0];
154 1.1 lukem if (params->flags & IB_NOWRITE) {
155 1.1 lukem op = "only";
156 1.1 lukem mode = O_RDONLY;
157 1.1 lukem } else {
158 1.1 lukem op = "write";
159 1.1 lukem mode = O_RDWR;
160 1.1 lukem }
161 1.1 lukem if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
162 1.1 lukem err(1, "Opening file system `%s' read-%s",
163 1.1 lukem params->filesystem, op);
164 1.6 lukem if (params->fstype != NULL) {
165 1.6 lukem if (! params->fstype->match(params))
166 1.6 lukem err(1, "File system `%s' is not of type %s",
167 1.6 lukem params->filesystem, params->fstype->name);
168 1.6 lukem } else {
169 1.6 lukem params->fstype = &fstypes[0];
170 1.6 lukem while (params->fstype->name != NULL &&
171 1.6 lukem ! params->fstype->match(params))
172 1.6 lukem params->fstype++;
173 1.6 lukem if (params->fstype->name == NULL)
174 1.7 lukem errx(1, "File system `%s' is of an unknown type",
175 1.6 lukem params->filesystem);
176 1.6 lukem }
177 1.1 lukem
178 1.5 lukem if (argc >= 2) {
179 1.5 lukem params->stage1 = argv[1];
180 1.5 lukem if ((params->s1fd = open(params->stage1, O_RDONLY, 0600))
181 1.1 lukem == -1)
182 1.5 lukem err(1, "Opening primary bootstrap `%s'",
183 1.5 lukem params->stage1);
184 1.5 lukem }
185 1.5 lukem if (argc == 3) {
186 1.5 lukem params->stage2 = argv[2];
187 1.1 lukem }
188 1.1 lukem assert(params->machine != NULL);
189 1.1 lukem
190 1.1 lukem if (params->flags & IB_VERBOSE) {
191 1.5 lukem printf("File system: %s\n", params->filesystem);
192 1.5 lukem printf("Primary bootstrap: %s\n",
193 1.1 lukem (params->flags & IB_CLEAR) ? "(to be cleared)"
194 1.5 lukem : params->stage1);
195 1.5 lukem if (params->stage2 != NULL)
196 1.5 lukem printf("Secondary bootstrap: %s\n", params->stage2);
197 1.1 lukem }
198 1.1 lukem
199 1.1 lukem if (params->flags & IB_CLEAR) {
200 1.1 lukem op = "Clear";
201 1.1 lukem rv = params->machine->clearboot(params);
202 1.1 lukem } else {
203 1.1 lukem op = "Set";
204 1.1 lukem rv = params->machine->setboot(params);
205 1.1 lukem }
206 1.1 lukem if (rv == 0)
207 1.5 lukem errx(1, "%s bootstrap operation failed", op);
208 1.1 lukem
209 1.4 lukem if (fsync(params->fsfd) == -1)
210 1.4 lukem err(1, "Synchronising file system `%s'", params->filesystem);
211 1.1 lukem if (close(params->fsfd) == -1)
212 1.1 lukem err(1, "Closing file system `%s'", params->filesystem);
213 1.1 lukem if (argc == 2)
214 1.5 lukem if (close(params->s1fd) == -1)
215 1.5 lukem err(1, "Closing primary bootstrap `%s'",
216 1.5 lukem params->stage1);
217 1.1 lukem
218 1.1 lukem exit(0);
219 1.1 lukem /* NOTREACHED */
220 1.1 lukem }
221 1.1 lukem
222 1.1 lukem int
223 1.1 lukem parseoptionflag(ib_params *params, const char *option, ib_flags wantflags)
224 1.1 lukem {
225 1.1 lukem struct {
226 1.1 lukem const char *name;
227 1.1 lukem ib_flags flag;
228 1.1 lukem } flags[] = {
229 1.1 lukem { "alphasum", IB_ALPHASUM },
230 1.1 lukem { "append", IB_APPEND },
231 1.1 lukem { "sunsum", IB_SUNSUM },
232 1.1 lukem { NULL, 0 },
233 1.1 lukem };
234 1.1 lukem
235 1.1 lukem int i;
236 1.1 lukem
237 1.1 lukem assert(params != NULL);
238 1.1 lukem assert(option != NULL);
239 1.1 lukem
240 1.1 lukem for (i = 0; flags[i].name != NULL; i++) {
241 1.1 lukem if ((strcmp(flags[i].name, option) == 0) &&
242 1.1 lukem (wantflags & flags[i].flag)) {
243 1.1 lukem params->flags |= flags[i].flag;
244 1.1 lukem return (1);
245 1.1 lukem }
246 1.1 lukem }
247 1.3 lukem return (0);
248 1.3 lukem }
249 1.3 lukem
250 1.3 lukem int
251 1.3 lukem no_parseopt(ib_params *params, const char *option)
252 1.3 lukem {
253 1.3 lukem
254 1.7 lukem assert(params != NULL);
255 1.7 lukem assert(option != NULL);
256 1.7 lukem
257 1.3 lukem /* all options are unsupported */
258 1.3 lukem warnx("Unsupported -o option `%s'", option);
259 1.3 lukem return (0);
260 1.3 lukem }
261 1.3 lukem
262 1.3 lukem int
263 1.3 lukem no_setboot(ib_params *params)
264 1.3 lukem {
265 1.3 lukem
266 1.7 lukem assert(params != NULL);
267 1.7 lukem
268 1.5 lukem /* bootstrap installation is not supported */
269 1.5 lukem warnx("%s: bootstrap installation is not supported",
270 1.3 lukem params->machine->name);
271 1.3 lukem return (0);
272 1.3 lukem }
273 1.3 lukem
274 1.3 lukem int
275 1.3 lukem no_clearboot(ib_params *params)
276 1.3 lukem {
277 1.3 lukem
278 1.7 lukem assert(params != NULL);
279 1.7 lukem
280 1.5 lukem /* bootstrap removal is not supported */
281 1.5 lukem warnx("%s: bootstrap removal is not supported",
282 1.3 lukem params->machine->name);
283 1.1 lukem return (0);
284 1.1 lukem }
285 1.1 lukem
286 1.1 lukem
287 1.1 lukem static int
288 1.1 lukem getmachine(ib_params *param, const char *mach, const char *provider)
289 1.1 lukem {
290 1.1 lukem int i;
291 1.1 lukem
292 1.1 lukem assert(param != NULL);
293 1.1 lukem assert(mach != NULL);
294 1.7 lukem assert(provider != NULL);
295 1.1 lukem
296 1.1 lukem for (i = 0; machines[i].name != NULL; i++) {
297 1.1 lukem if (strcmp(machines[i].name, mach) == 0) {
298 1.1 lukem param->machine = &machines[i];
299 1.1 lukem return (1);
300 1.1 lukem }
301 1.1 lukem }
302 1.1 lukem warnx("Invalid machine `%s' from %s", mach, provider);
303 1.1 lukem warnx("Supported machines are:");
304 1.1 lukem #define MACHS_PER_LINE 10
305 1.1 lukem for (i = 0; machines[i].name != NULL; i++) {
306 1.1 lukem fputs((i % MACHS_PER_LINE) ? ", " : "\t", stderr);
307 1.1 lukem fputs(machines[i].name, stderr);
308 1.1 lukem if ((i % MACHS_PER_LINE) == (MACHS_PER_LINE - 1))
309 1.1 lukem fputs("\n", stderr);
310 1.1 lukem }
311 1.1 lukem if ((i % MACHS_PER_LINE) != 0)
312 1.6 lukem fputs("\n", stderr);
313 1.6 lukem return (0);
314 1.6 lukem }
315 1.6 lukem
316 1.6 lukem static int
317 1.6 lukem getfstype(ib_params *param, const char *fstype, const char *provider)
318 1.6 lukem {
319 1.6 lukem int i;
320 1.6 lukem
321 1.6 lukem assert(param != NULL);
322 1.6 lukem assert(fstype != NULL);
323 1.7 lukem assert(provider != NULL);
324 1.6 lukem
325 1.6 lukem for (i = 0; fstypes[i].name != NULL; i++) {
326 1.6 lukem if (strcmp(fstypes[i].name, fstype) == 0) {
327 1.6 lukem param->fstype = &fstypes[i];
328 1.6 lukem return (1);
329 1.6 lukem }
330 1.6 lukem }
331 1.6 lukem warnx("Invalid file system type `%s' from %s", fstype, provider);
332 1.6 lukem warnx("Supported file system types are:");
333 1.6 lukem #define FSTYPES_PER_LINE 10
334 1.6 lukem for (i = 0; fstypes[i].name != NULL; i++) {
335 1.6 lukem fputs((i % FSTYPES_PER_LINE) ? ", " : "\t", stderr);
336 1.6 lukem fputs(fstypes[i].name, stderr);
337 1.6 lukem if ((i % FSTYPES_PER_LINE) == (FSTYPES_PER_LINE - 1))
338 1.6 lukem fputs("\n", stderr);
339 1.6 lukem }
340 1.6 lukem if ((i % FSTYPES_PER_LINE) != 0)
341 1.1 lukem fputs("\n", stderr);
342 1.1 lukem return (0);
343 1.1 lukem }
344 1.1 lukem
345 1.1 lukem static void
346 1.1 lukem usage(void)
347 1.1 lukem {
348 1.1 lukem const char *prog;
349 1.1 lukem
350 1.1 lukem prog = getprogname();
351 1.1 lukem fprintf(stderr,
352 1.1 lukem "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
353 1.5 lukem "\t\t [-b block] filesystem primary [secondary]\n"
354 1.1 lukem "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
355 1.1 lukem prog, prog);
356 1.1 lukem exit(1);
357 1.1 lukem }
358