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