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