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