1 /* $NetBSD: installboot.c,v 1.5 2002/04/12 06:50:41 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.5 2002/04/12 06:50:41 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 <limits.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 55 #include "installboot.h" 56 57 int main(int, char *[]); 58 static int getmachine(ib_params *, const char *, const char *); 59 static void usage(void); 60 61 static ib_params installboot_params; 62 63 int 64 main(int argc, char *argv[]) 65 { 66 struct utsname utsname; 67 ib_params *params; 68 long lval; 69 int ch, rv, mode; 70 char *p; 71 const char *op; 72 73 setprogname(argv[0]); 74 params = &installboot_params; 75 memset(params, 0, sizeof(*params)); 76 if ((p = getenv("MACHINE")) != NULL) 77 if (! getmachine(params, p, "$MACHINE")) 78 exit(1); 79 80 while ((ch = getopt(argc, argv, "b:cm:no:t:v")) != -1) { 81 switch (ch) { 82 83 case 'b': 84 if (*optarg == '\0') 85 goto badblock; 86 lval = strtol(optarg, &p, 0); 87 if (lval < 0 || lval >= LONG_MAX || *p != '\0') { 88 badblock: 89 errx(1, "Invalid block number `%s'", optarg); 90 } 91 params->startblock = lval; 92 params->flags |= IB_STARTBLOCK; 93 break; 94 95 case 'c': 96 params->flags |= IB_CLEAR; 97 break; 98 99 case 'm': 100 if (! getmachine(params, optarg, "-m")) 101 exit(1); 102 break; 103 104 case 'n': 105 params->flags |= IB_NOWRITE; 106 break; 107 108 case 'o': 109 if (params->machine == NULL) 110 errx(1, 111 "Machine needs to be specified before -o"); 112 while ((p = strsep(&optarg, ",")) != NULL) { 113 if (*p == '\0') 114 errx(1, "Empty `-o' option"); 115 if (! params->machine->parseopt(params, p)) 116 exit(1); 117 } 118 break; 119 120 case 't': 121 params->fstype = optarg; // XXX: validate? 122 break; 123 124 case 'v': 125 params->flags |= IB_VERBOSE; 126 break; 127 128 case '?': 129 default: 130 usage(); 131 /* NOTREACHED */ 132 133 } 134 } 135 argc -= optind; 136 argv += optind; 137 138 if (((params->flags & IB_CLEAR) != 0 && argc != 1) || 139 ((params->flags & IB_CLEAR) == 0 && (argc < 2 || argc > 3))) 140 usage(); 141 142 /* set missing defaults */ 143 if (params->machine == NULL) { 144 if (uname(&utsname) == -1) 145 err(1, "Determine uname"); 146 if (! getmachine(params, utsname.machine, "uname()")) 147 exit(1); 148 } 149 // XXX: set default params->fstype 150 151 params->filesystem = argv[0]; 152 if (params->flags & IB_NOWRITE) { 153 op = "only"; 154 mode = O_RDONLY; 155 } else { 156 op = "write"; 157 mode = O_RDWR; 158 } 159 if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1) 160 err(1, "Opening file system `%s' read-%s", 161 params->filesystem, op); 162 163 if (argc >= 2) { 164 params->stage1 = argv[1]; 165 if ((params->s1fd = open(params->stage1, O_RDONLY, 0600)) 166 == -1) 167 err(1, "Opening primary bootstrap `%s'", 168 params->stage1); 169 } 170 if (argc == 3) { 171 params->stage2 = argv[2]; 172 } 173 assert(params->machine != NULL); 174 175 if (params->flags & IB_VERBOSE) { 176 printf("File system: %s\n", params->filesystem); 177 printf("Primary bootstrap: %s\n", 178 (params->flags & IB_CLEAR) ? "(to be cleared)" 179 : params->stage1); 180 if (params->stage2 != NULL) 181 printf("Secondary bootstrap: %s\n", params->stage2); 182 } 183 184 if (params->flags & IB_CLEAR) { 185 op = "Clear"; 186 rv = params->machine->clearboot(params); 187 } else { 188 op = "Set"; 189 rv = params->machine->setboot(params); 190 } 191 if (rv == 0) 192 errx(1, "%s bootstrap operation failed", op); 193 194 if (fsync(params->fsfd) == -1) 195 err(1, "Synchronising file system `%s'", params->filesystem); 196 if (close(params->fsfd) == -1) 197 err(1, "Closing file system `%s'", params->filesystem); 198 if (argc == 2) 199 if (close(params->s1fd) == -1) 200 err(1, "Closing primary bootstrap `%s'", 201 params->stage1); 202 203 exit(0); 204 /* NOTREACHED */ 205 } 206 207 int 208 parseoptionflag(ib_params *params, const char *option, ib_flags wantflags) 209 { 210 struct { 211 const char *name; 212 ib_flags flag; 213 } flags[] = { 214 { "alphasum", IB_ALPHASUM }, 215 { "append", IB_APPEND }, 216 { "sunsum", IB_SUNSUM }, 217 { NULL, 0 }, 218 }; 219 220 int i; 221 222 assert(params != NULL); 223 assert(option != NULL); 224 225 for (i = 0; flags[i].name != NULL; i++) { 226 if ((strcmp(flags[i].name, option) == 0) && 227 (wantflags & flags[i].flag)) { 228 params->flags |= flags[i].flag; 229 return (1); 230 } 231 } 232 return (0); 233 } 234 235 int 236 no_parseopt(ib_params *params, const char *option) 237 { 238 239 /* all options are unsupported */ 240 warnx("Unsupported -o option `%s'", option); 241 return (0); 242 } 243 244 int 245 no_setboot(ib_params *params) 246 { 247 248 /* bootstrap installation is not supported */ 249 warnx("%s: bootstrap installation is not supported", 250 params->machine->name); 251 return (0); 252 } 253 254 int 255 no_clearboot(ib_params *params) 256 { 257 258 /* bootstrap removal is not supported */ 259 warnx("%s: bootstrap removal is not supported", 260 params->machine->name); 261 return (0); 262 } 263 264 265 static int 266 getmachine(ib_params *param, const char *mach, const char *provider) 267 { 268 int i; 269 270 assert(param != NULL); 271 assert(mach != NULL); 272 273 for (i = 0; machines[i].name != NULL; i++) { 274 if (strcmp(machines[i].name, mach) == 0) { 275 param->machine = &machines[i]; 276 return (1); 277 } 278 } 279 warnx("Invalid machine `%s' from %s", mach, provider); 280 warnx("Supported machines are:"); 281 #define MACHS_PER_LINE 10 282 for (i = 0; machines[i].name != NULL; i++) { 283 fputs((i % MACHS_PER_LINE) ? ", " : "\t", stderr); 284 fputs(machines[i].name, stderr); 285 if ((i % MACHS_PER_LINE) == (MACHS_PER_LINE - 1)) 286 fputs("\n", stderr); 287 } 288 if ((i % MACHS_PER_LINE) != 0) 289 fputs("\n", stderr); 290 return (0); 291 } 292 293 static void 294 usage(void) 295 { 296 const char *prog; 297 298 prog = getprogname(); 299 fprintf(stderr, 300 "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n" 301 "\t\t [-b block] filesystem primary [secondary]\n" 302 "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n", 303 prog, prog); 304 exit(1); 305 } 306