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