1 /* $NetBSD: installboot.c,v 1.3 2002/04/11 07:56:13 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.3 2002/04/11 07:56:13 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)) 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->bootblock = argv[1]; 165 if ((params->bbfd = open(params->bootblock, O_RDONLY, 0600)) 166 == -1) 167 err(1, "Opening boot block `%s'", params->bootblock); 168 } 169 assert(params->machine != NULL); 170 171 if (params->flags & IB_VERBOSE) { 172 printf("File system: %s\n", params->filesystem); 173 printf("Boot block: %s\n", 174 (params->flags & IB_CLEAR) ? "(to be cleared)" 175 : params->bootblock); 176 } 177 178 if (params->flags & IB_CLEAR) { 179 op = "Clear"; 180 rv = params->machine->clearboot(params); 181 } else { 182 op = "Set"; 183 rv = params->machine->setboot(params); 184 } 185 if (rv == 0) 186 errx(1, "%s boot block operation failed", op); 187 188 if (close(params->fsfd) == -1) 189 err(1, "Closing file system `%s'", params->filesystem); 190 if (argc == 2) 191 if (close(params->bbfd) == -1) 192 err(1, "Closing boot block `%s'", params->bootblock); 193 194 exit(0); 195 /* NOTREACHED */ 196 } 197 198 int 199 parseoptionflag(ib_params *params, const char *option, ib_flags wantflags) 200 { 201 struct { 202 const char *name; 203 ib_flags flag; 204 } flags[] = { 205 { "alphasum", IB_ALPHASUM }, 206 { "append", IB_APPEND }, 207 { "sunsum", IB_SUNSUM }, 208 { NULL, 0 }, 209 }; 210 211 int i; 212 213 assert(params != NULL); 214 assert(option != NULL); 215 216 for (i = 0; flags[i].name != NULL; i++) { 217 if ((strcmp(flags[i].name, option) == 0) && 218 (wantflags & flags[i].flag)) { 219 params->flags |= flags[i].flag; 220 return (1); 221 } 222 } 223 return (0); 224 } 225 226 int 227 no_parseopt(ib_params *params, const char *option) 228 { 229 230 /* all options are unsupported */ 231 warnx("Unsupported -o option `%s'", option); 232 return (0); 233 } 234 235 int 236 no_setboot(ib_params *params) 237 { 238 239 /* boot block installation is not supported */ 240 warnx("%s: boot block installation is not supported", 241 params->machine->name); 242 return (0); 243 } 244 245 int 246 no_clearboot(ib_params *params) 247 { 248 249 /* boot block removal is not supported */ 250 warnx("%s: boot block removal is not supported", 251 params->machine->name); 252 return (0); 253 } 254 255 256 static int 257 getmachine(ib_params *param, const char *mach, const char *provider) 258 { 259 int i; 260 261 assert(param != NULL); 262 assert(mach != NULL); 263 264 for (i = 0; machines[i].name != NULL; i++) { 265 if (strcmp(machines[i].name, mach) == 0) { 266 param->machine = &machines[i]; 267 return (1); 268 } 269 } 270 warnx("Invalid machine `%s' from %s", mach, provider); 271 warnx("Supported machines are:"); 272 #define MACHS_PER_LINE 10 273 for (i = 0; machines[i].name != NULL; i++) { 274 fputs((i % MACHS_PER_LINE) ? ", " : "\t", stderr); 275 fputs(machines[i].name, stderr); 276 if ((i % MACHS_PER_LINE) == (MACHS_PER_LINE - 1)) 277 fputs("\n", stderr); 278 } 279 if ((i % MACHS_PER_LINE) != 0) 280 fputs("\n", stderr); 281 return (0); 282 } 283 284 static void 285 usage(void) 286 { 287 const char *prog; 288 289 prog = getprogname(); 290 fprintf(stderr, 291 "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n" 292 "\t\t [-b block] filesystem bootstrap\n" 293 "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n", 294 prog, prog); 295 exit(1); 296 } 297