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