1 /* $NetBSD: setnetbootinfo.c,v 1.11 2001/07/22 15:17:32 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Christopher G. Demetriou 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Christopher G. Demetriou 18 * for the NetBSD Project. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/fcntl.h> 35 #include <sys/stat.h> 36 #include <sys/socket.h> /* XXX */ 37 #include <net/if.h> /* XXX */ 38 #include <net/if_ether.h> 39 #include <err.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #include "stand/common/bbinfo.h" 46 47 static void usage(void); 48 int main(int argc, char *argv[]); 49 50 int verbose, force, unset; 51 char *netboot, *outfile, *addr, *host; 52 53 char *outfilename; 54 55 extern char *ether_ntoa __P((struct ether_addr *s)); /* XXX */ 56 struct ether_addr *ether_addr, _ether_addr; 57 58 static void 59 usage(void) 60 { 61 62 fprintf(stderr, "usage:\n"); 63 fprintf(stderr, "\tsetnetboot [-v] [-f] [-o outfile] \\\n"); 64 fprintf(stderr, "\t [-a ether-address | -h ether-host] infile\n"); 65 fprintf(stderr, "\tsetnetboot [-v] -u -o outfile infile\n"); 66 exit(1); 67 } 68 69 int 70 main(argc, argv) 71 int argc; 72 char *argv[]; 73 { 74 struct netbbinfo *netbbinfop; 75 struct stat sb; 76 u_int64_t *qp, csum; 77 char *netbb; 78 int c, fd, i; 79 80 while ((c = getopt(argc, argv, "a:fh:o:uv")) != -1) { 81 switch (c) { 82 case 'a': 83 /* use the argument as an ethernet address */ 84 addr = optarg; 85 break; 86 case 'f': 87 /* set force flag in network boot block */ 88 force = 1; 89 break; 90 case 'h': 91 /* use the argument as a host to find in /etc/ethers */ 92 host = optarg; 93 break; 94 case 'o': 95 /* use the argument as the output file name */ 96 outfile = optarg; 97 break; 98 case 'u': 99 /* remove configuration information */ 100 unset = 1; 101 break; 102 case 'v': 103 /* Chat */ 104 verbose = 1; 105 break; 106 default: 107 usage(); 108 } 109 } 110 111 if ((argc - optind) != 1) 112 usage(); 113 netboot = argv[optind]; 114 115 if (unset && (force || host != NULL || addr != NULL)) 116 errx(1, "-u can't be used with -f, -h, or -a"); 117 118 if (unset) { 119 if (force || host != NULL || addr != NULL) 120 errx(1, "-u can't be used with -f, -h, or -a"); 121 if (outfile == NULL) 122 errx(1, "-u cannot be used without -o"); 123 } else { 124 if ((host == NULL && addr == NULL) || 125 (host != NULL && addr != NULL)) 126 usage(); 127 128 if (host != NULL) { 129 if (ether_hostton(host, &_ether_addr) == -1) 130 errx(1, "ethernet address couldn't be found for \"%s\"", 131 host); 132 ether_addr = &_ether_addr; 133 } else { /* addr != NULL */ 134 ether_addr = ether_aton(addr); 135 if (ether_addr == NULL) 136 errx(1, "ethernet address \"%s\" is invalid", 137 addr); 138 } 139 } 140 141 if (outfile != NULL) 142 outfilename = outfile; 143 else { 144 /* name + 12 for enet addr + '.' before enet addr + NUL */ 145 outfilename = malloc(strlen(netboot) + 14); 146 if (outfilename == NULL) 147 err(1, "malloc of output file name failed"); 148 sprintf(outfilename, "%s.%02x%02x%02x%02x%02x%02x", netboot, 149 ether_addr->ether_addr_octet[0], 150 ether_addr->ether_addr_octet[1], 151 ether_addr->ether_addr_octet[2], 152 ether_addr->ether_addr_octet[3], 153 ether_addr->ether_addr_octet[4], 154 ether_addr->ether_addr_octet[5]); 155 } 156 157 if (verbose) { 158 printf("netboot: %s\n", netboot); 159 if (unset) 160 printf("unsetting configuration\n"); 161 else 162 printf("ethernet address: %s (%s), force = %d\n", 163 ether_ntoa(ether_addr), host ? host : addr, force); 164 printf("output netboot: %s\n", outfilename); 165 } 166 167 168 if (verbose) 169 printf("opening %s...\n", netboot); 170 if ((fd = open(netboot, O_RDONLY, 0)) == -1) 171 err(1, "open: %s", netboot); 172 if (fstat(fd, &sb) == -1) 173 err(1, "fstat: %s", netboot); 174 if (!S_ISREG(sb.st_mode)) 175 errx(1, "%s must be a regular file", netboot); 176 177 if (verbose) 178 printf("reading %s...\n", netboot); 179 netbb = malloc(sb.st_size); 180 if (netbb == NULL) 181 err(1, "malloc of %lu for %s failed", 182 (unsigned long)sb.st_size, netboot); 183 if (read(fd, netbb, sb.st_size) != sb.st_size) 184 err(1, "read of %lu from %s failed", 185 (unsigned long)sb.st_size, netboot); 186 187 if (verbose) 188 printf("closing %s...\n", netboot); 189 close(fd); 190 191 if (verbose) 192 printf("looking for netbbinfo...\n"); 193 netbbinfop = NULL; 194 for (qp = (u_int64_t *)netbb; qp < (u_int64_t *)(netbb + sb.st_size); 195 qp++) { 196 if (((struct netbbinfo *)qp)->magic1 == 0xfeedbabedeadbeefLL && 197 ((struct netbbinfo *)qp)->magic2 == 0xfeedbeefdeadbabeLL) { 198 netbbinfop = (struct netbbinfo *)qp; 199 break; 200 } 201 } 202 if (netbbinfop == NULL) 203 errx(1, "netboot information structure not found in %s", 204 netboot); 205 if (verbose) 206 printf("found netbbinfo structure at offset 0x%lx.\n", 207 (unsigned long)((char *)netbbinfop - netbb)); 208 209 if (verbose) 210 printf("setting netbbinfo structure...\n"); 211 memset(netbbinfop, 0, sizeof *netbbinfop); 212 netbbinfop->magic1 = 0xfeedbabedeadbeefLL; 213 netbbinfop->magic2 = 0xfeedbeefdeadbabeLL; 214 netbbinfop->set = unset ? 0 : 1; 215 if (netbbinfop->set) { 216 for (i = 0; i < 6; i++) 217 netbbinfop->ether_addr[i] = 218 ether_addr->ether_addr_octet[i]; 219 netbbinfop->force = force; 220 } 221 netbbinfop->cksum = 0; 222 223 if (verbose) 224 printf("setting netbbinfo checksum...\n"); 225 csum = 0; 226 for (i = 0, qp = (u_int64_t *)netbbinfop; 227 i < (sizeof *netbbinfop / sizeof (u_int64_t)); i++, qp++) 228 csum += *qp; 229 netbbinfop->cksum = -csum; 230 231 if (verbose) 232 printf("opening %s...\n", outfilename); 233 if ((fd = open(outfilename, O_WRONLY | O_CREAT, 0666)) == -1) 234 err(1, "open: %s", outfilename); 235 236 if (verbose) 237 printf("writing %s...\n", outfilename); 238 if (write(fd, netbb, sb.st_size) != sb.st_size) 239 err(1, "write of %lu to %s failed", 240 (unsigned long)sb.st_size, outfilename); 241 242 if (verbose) 243 printf("closing %s...\n", outfilename); 244 close(fd); 245 246 free(netbb); 247 if (outfile == NULL) 248 free(outfilename); 249 250 exit (0); 251 } 252