No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / alpha / stand / setnetbootinfo / setnetbootinfo.c
blob174f98b16d4dce10493e9ef6f6ddf9a5b5594058
1 /* $NetBSD: setnetbootinfo.c,v 1.12 2002/09/22 05:38:30 mycroft Exp $ */
3 /*
4 * Copyright (c) 1997 Christopher G. Demetriou
5 * All rights reserved.
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
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.
34 #include <sys/fcntl.h>
35 #include <sys/stat.h>
36 #include <sys/socket.h> /* XXX */
37 #include <net/if.h> /* XXX */
38 #include <net/if_ether.h>
39 #include <err.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include "stand/common/bbinfo.h"
47 static void usage(void);
48 int main(int argc, char *argv[]);
50 int verbose, force, unset;
51 char *netboot, *outfile, *addr, *host;
53 char *outfilename;
55 struct ether_addr *ether_addr, _ether_addr;
57 static void
58 usage(void)
61 fprintf(stderr, "usage:\n");
62 fprintf(stderr, "\tsetnetboot [-v] [-f] [-o outfile] \\\n");
63 fprintf(stderr, "\t [-a ether-address | -h ether-host] infile\n");
64 fprintf(stderr, "\tsetnetboot [-v] -u -o outfile infile\n");
65 exit(1);
68 int
69 main(int argc, char *argv[])
71 struct netbbinfo *netbbinfop;
72 struct stat sb;
73 u_int64_t *qp, csum;
74 char *netbb;
75 int c, fd, i;
77 while ((c = getopt(argc, argv, "a:fh:o:uv")) != -1) {
78 switch (c) {
79 case 'a':
80 /* use the argument as an ethernet address */
81 addr = optarg;
82 break;
83 case 'f':
84 /* set force flag in network boot block */
85 force = 1;
86 break;
87 case 'h':
88 /* use the argument as a host to find in /etc/ethers */
89 host = optarg;
90 break;
91 case 'o':
92 /* use the argument as the output file name */
93 outfile = optarg;
94 break;
95 case 'u':
96 /* remove configuration information */
97 unset = 1;
98 break;
99 case 'v':
100 /* Chat */
101 verbose = 1;
102 break;
103 default:
104 usage();
108 if ((argc - optind) != 1)
109 usage();
110 netboot = argv[optind];
112 if (unset && (force || host != NULL || addr != NULL))
113 errx(1, "-u can't be used with -f, -h, or -a");
115 if (unset) {
116 if (force || host != NULL || addr != NULL)
117 errx(1, "-u can't be used with -f, -h, or -a");
118 if (outfile == NULL)
119 errx(1, "-u cannot be used without -o");
120 } else {
121 if ((host == NULL && addr == NULL) ||
122 (host != NULL && addr != NULL))
123 usage();
125 if (host != NULL) {
126 if (ether_hostton(host, &_ether_addr) == -1)
127 errx(1, "ethernet address couldn't be found for \"%s\"",
128 host);
129 ether_addr = &_ether_addr;
130 } else { /* addr != NULL */
131 ether_addr = ether_aton(addr);
132 if (ether_addr == NULL)
133 errx(1, "ethernet address \"%s\" is invalid",
134 addr);
138 if (outfile != NULL)
139 outfilename = outfile;
140 else {
141 /* name + 12 for enet addr + '.' before enet addr + NUL */
142 outfilename = malloc(strlen(netboot) + 14);
143 if (outfilename == NULL)
144 err(1, "malloc of output file name failed");
145 sprintf(outfilename, "%s.%02x%02x%02x%02x%02x%02x", netboot,
146 ether_addr->ether_addr_octet[0],
147 ether_addr->ether_addr_octet[1],
148 ether_addr->ether_addr_octet[2],
149 ether_addr->ether_addr_octet[3],
150 ether_addr->ether_addr_octet[4],
151 ether_addr->ether_addr_octet[5]);
154 if (verbose) {
155 printf("netboot: %s\n", netboot);
156 if (unset)
157 printf("unsetting configuration\n");
158 else
159 printf("ethernet address: %s (%s), force = %d\n",
160 ether_ntoa(ether_addr), host ? host : addr, force);
161 printf("output netboot: %s\n", outfilename);
165 if (verbose)
166 printf("opening %s...\n", netboot);
167 if ((fd = open(netboot, O_RDONLY, 0)) == -1)
168 err(1, "open: %s", netboot);
169 if (fstat(fd, &sb) == -1)
170 err(1, "fstat: %s", netboot);
171 if (!S_ISREG(sb.st_mode))
172 errx(1, "%s must be a regular file", netboot);
174 if (verbose)
175 printf("reading %s...\n", netboot);
176 netbb = malloc(sb.st_size);
177 if (netbb == NULL)
178 err(1, "malloc of %lu for %s failed",
179 (unsigned long)sb.st_size, netboot);
180 if (read(fd, netbb, sb.st_size) != sb.st_size)
181 err(1, "read of %lu from %s failed",
182 (unsigned long)sb.st_size, netboot);
184 if (verbose)
185 printf("closing %s...\n", netboot);
186 close(fd);
188 if (verbose)
189 printf("looking for netbbinfo...\n");
190 netbbinfop = NULL;
191 for (qp = (u_int64_t *)netbb; qp < (u_int64_t *)(netbb + sb.st_size);
192 qp++) {
193 if (((struct netbbinfo *)qp)->magic1 == 0xfeedbabedeadbeefLL &&
194 ((struct netbbinfo *)qp)->magic2 == 0xfeedbeefdeadbabeLL) {
195 netbbinfop = (struct netbbinfo *)qp;
196 break;
199 if (netbbinfop == NULL)
200 errx(1, "netboot information structure not found in %s",
201 netboot);
202 if (verbose)
203 printf("found netbbinfo structure at offset 0x%lx.\n",
204 (unsigned long)((char *)netbbinfop - netbb));
206 if (verbose)
207 printf("setting netbbinfo structure...\n");
208 memset(netbbinfop, 0, sizeof *netbbinfop);
209 netbbinfop->magic1 = 0xfeedbabedeadbeefLL;
210 netbbinfop->magic2 = 0xfeedbeefdeadbabeLL;
211 netbbinfop->set = unset ? 0 : 1;
212 if (netbbinfop->set) {
213 for (i = 0; i < 6; i++)
214 netbbinfop->ether_addr[i] =
215 ether_addr->ether_addr_octet[i];
216 netbbinfop->force = force;
218 netbbinfop->cksum = 0;
220 if (verbose)
221 printf("setting netbbinfo checksum...\n");
222 csum = 0;
223 for (i = 0, qp = (u_int64_t *)netbbinfop;
224 i < (sizeof *netbbinfop / sizeof (u_int64_t)); i++, qp++)
225 csum += *qp;
226 netbbinfop->cksum = -csum;
228 if (verbose)
229 printf("opening %s...\n", outfilename);
230 if ((fd = open(outfilename, O_WRONLY | O_CREAT, 0666)) == -1)
231 err(1, "open: %s", outfilename);
233 if (verbose)
234 printf("writing %s...\n", outfilename);
235 if (write(fd, netbb, sb.st_size) != sb.st_size)
236 err(1, "write of %lu to %s failed",
237 (unsigned long)sb.st_size, outfilename);
239 if (verbose)
240 printf("closing %s...\n", outfilename);
241 close(fd);
243 free(netbb);
244 if (outfile == NULL)
245 free(outfilename);
247 exit (0);