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