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