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