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