Home | History | Annotate | Line # | Download | only in setnetbootinfo
setnetbootinfo.c revision 1.5.10.1
      1 /* $NetBSD: setnetbootinfo.c,v 1.5.10.1 1997/11/13 21:08:19 mellon 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 <stdio.h>
     35 #include <stdlib.h>
     36 #include <string.h>
     37 #include <err.h>
     38 #include <sys/fcntl.h>
     39 #include <sys/stat.h>
     40 #include <sys/socket.h>						/* XXX */
     41 #include <net/if.h>						/* XXX */
     42 #include <net/if_ether.h>
     43 
     44 #include "stand/common/bbinfo.h"
     45 
     46 int	verbose, force, unset;
     47 char	*netboot, *outfile, *addr, *host;
     48 
     49 char	*outfilename;
     50 
     51 extern char *ether_ntoa __P((struct ether_addr *s));		/* XXX */
     52 struct ether_addr *ether_addr, _ether_addr;
     53 
     54 static void
     55 usage()
     56 {
     57 
     58 	fprintf(stderr, "usage:\n");
     59 	fprintf(stderr, "\tsetnetboot [-v] [-f] [-o outfile] \\\n");
     60 	fprintf(stderr, "\t    [-a ether-address | -h ether-host] infile\n");
     61 	fprintf(stderr, "\tsetnetboot [-v] -u -o outfile infile\n");
     62 	exit(1);
     63 }
     64 
     65 int
     66 main(argc, argv)
     67 	int argc;
     68 	char *argv[];
     69 {
     70 	struct netbbinfo *netbbinfop;
     71 	struct stat sb;
     72 	u_int64_t *qp, csum;
     73 	char *netbb;
     74 	int c, fd, i;
     75 
     76 	while ((c = getopt(argc, argv, "a:fh:o:uv")) != -1) {
     77 		switch (c) {
     78 		case 'a':
     79 			/* use the argument as an ethernet address */
     80 			addr = optarg;
     81 			break;
     82 		case 'f':
     83 			/* set force flag in network boot block */
     84 			force = 1;
     85 			break;
     86 		case 'h':
     87 			/* use the argument as a host to find in /etc/ethers */
     88 			host = optarg;
     89 			break;
     90 		case 'o':
     91 			/* use the argument as the output file name */
     92 			outfile = optarg;
     93 			break;
     94 		case 'u':
     95 			/* remove configuration information */
     96 			unset = 1;
     97 			break;
     98 		case 'v':
     99 			/* Chat */
    100 			verbose = 1;
    101 			break;
    102 		default:
    103 			usage();
    104 		}
    105 	}
    106 
    107 	if ((argc - optind) != 1)
    108 		usage();
    109 	netboot = argv[optind];
    110 
    111 	if (unset && (force || host != NULL || addr != NULL))
    112 		errx(1, "-u can't be used with -f, -h, or -a");
    113 
    114 	if (unset) {
    115 		if (force || host != NULL || addr != NULL)
    116 			errx(1, "-u can't be used with -f, -h, or -a");
    117 		if (outfile == NULL)
    118 			errx(1, "-u cannot be used without -o");
    119 	} else {
    120 		if ((host == NULL && addr == NULL) ||
    121 		    (host != NULL && addr != NULL))
    122 			usage();
    123 
    124 		if (host != NULL) {
    125 			if (ether_hostton(host, &_ether_addr) == -1)
    126 				errx(1, "ethernet address couldn't be found for \"%s\"",
    127 				    host);
    128 			ether_addr = &_ether_addr;
    129 		} else { /* addr != NULL */
    130 			ether_addr = ether_aton(addr);
    131 			if (ether_addr == NULL)
    132 				errx(1, "ethernet address \"%s\" is invalid",
    133 				    addr);
    134 		}
    135 	}
    136 
    137 	if (outfile != NULL)
    138 		outfilename = outfile;
    139 	else {
    140 		/* name + 12 for enet addr + '.' before enet addr + NUL */
    141 		outfilename = malloc(strlen(netboot) + 14);
    142 		if (outfilename == NULL)
    143 			err(1, "malloc of output file name failed");
    144 		sprintf(outfilename, "%s.%02x%02x%02x%02x%02x%02x", netboot,
    145 		    ether_addr->ether_addr_octet[0],
    146 		    ether_addr->ether_addr_octet[1],
    147 		    ether_addr->ether_addr_octet[2],
    148 		    ether_addr->ether_addr_octet[3],
    149 		    ether_addr->ether_addr_octet[4],
    150 		    ether_addr->ether_addr_octet[5]);
    151 	}
    152 
    153 	if (verbose) {
    154 		printf("netboot: %s\n", netboot);
    155 		if (unset)
    156 			printf("unsetting configuration\n");
    157 		else
    158 			printf("ethernet address: %s (%s), force = %d\n",
    159 			    ether_ntoa(ether_addr), host ? host : addr, force);
    160 		printf("output netboot: %s\n", outfilename);
    161 	}
    162 
    163 
    164 	if (verbose)
    165 		printf("opening %s...\n", netboot);
    166 	if ((fd = open(netboot, O_RDONLY, 0)) == -1)
    167 		err(1, "open: %s", netboot);
    168 	if (fstat(fd, &sb) == -1)
    169 		err(1, "fstat: %s", netboot);
    170 	if (!S_ISREG(sb.st_mode))
    171 		errx(1, "%s must be a regular file", netboot);
    172 
    173 	if (verbose)
    174 		printf("reading %s...\n", netboot);
    175 	netbb = malloc(sb.st_size);
    176 	if (netbb == NULL)
    177 		err(1, "malloc of %lu for %s failed",
    178 		    (unsigned long)sb.st_size, netboot);
    179 	if (read(fd, netbb, sb.st_size) != sb.st_size)
    180 		err(1, "read of %lu from %s failed",
    181 		    (unsigned long)sb.st_size, netboot);
    182 
    183 	if (verbose)
    184 		printf("closing %s...\n", netboot);
    185 	close(fd);
    186 
    187 	if (verbose)
    188 		printf("looking for netbbinfo...\n");
    189 	netbbinfop = NULL;
    190 	for (qp = (u_int64_t *)netbb; qp < (u_int64_t *)(netbb + sb.st_size);
    191 	    qp++) {
    192 		if (((struct netbbinfo *)qp)->magic1 == 0xfeedbabedeadbeef &&
    193 		    ((struct netbbinfo *)qp)->magic2 == 0xfeedbeefdeadbabe) {
    194 			netbbinfop = (struct netbbinfo *)qp;
    195 			break;
    196 		}
    197 	}
    198 	if (netbbinfop == NULL)
    199 		errx(1, "netboot information structure not found in %s",
    200 		    netboot);
    201 	if (verbose)
    202 		printf("found netbbinfo structure at offset 0x%lx.\n",
    203 		    (unsigned long)((char *)netbbinfop - netbb));
    204 
    205 	if (verbose)
    206 		printf("setting netbbinfo structure...\n");
    207 	bzero(netbbinfop, sizeof *netbbinfop);
    208 	netbbinfop->magic1 = 0xfeedbabedeadbeef;
    209 	netbbinfop->magic2 = 0xfeedbeefdeadbabe;
    210 	netbbinfop->set = unset ? 0 : 1;
    211 	if (netbbinfop->set) {
    212 		for (i = 0; i < 6; i++)
    213 			netbbinfop->ether_addr[i] =
    214 			    ether_addr->ether_addr_octet[i];
    215 		netbbinfop->force = force;
    216 	}
    217 	netbbinfop->cksum = 0;
    218 
    219 	if (verbose)
    220 		printf("setting netbbinfo checksum...\n");
    221 	csum = 0;
    222 	for (i = 0, qp = (u_int64_t *)netbbinfop;
    223 	    i < (sizeof *netbbinfop / sizeof (u_int64_t)); i++, qp++)
    224 		csum += *qp;
    225 	netbbinfop->cksum = -csum;
    226 
    227 	if (verbose)
    228 		printf("opening %s...\n", outfilename);
    229 	if ((fd = open(outfilename, O_WRONLY | O_CREAT, 0666)) == -1)
    230 		err(1, "open: %s", outfilename);
    231 
    232 	if (verbose)
    233 		printf("writing %s...\n", outfilename);
    234 	if (write(fd, netbb, sb.st_size) != sb.st_size)
    235 		err(1, "write of %lu to %s failed",
    236 		    (unsigned long)sb.st_size, outfilename);
    237 
    238 	if (verbose)
    239 		printf("closing %s...\n", outfilename);
    240 	close(fd);
    241 
    242 	free(netbb);
    243 	if (outfile == NULL)
    244 		free(outfilename);
    245 
    246 	exit (0);
    247 }
    248