1 /* $NetBSD: setnetbootinfo.c,v 1.12 2002/09/22 05:38:30 mycroft Exp $ */
4 * Copyright (c) 1997 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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>
36 #include <sys/socket.h> /* XXX */
37 #include <net/if.h> /* XXX */
38 #include <net/if_ether.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
;
55 struct ether_addr
*ether_addr
, _ether_addr
;
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");
69 main(int argc
, char *argv
[])
71 struct netbbinfo
*netbbinfop
;
77 while ((c
= getopt(argc
, argv
, "a:fh:o:uv")) != -1) {
80 /* use the argument as an ethernet address */
84 /* set force flag in network boot block */
88 /* use the argument as a host to find in /etc/ethers */
92 /* use the argument as the output file name */
96 /* remove configuration information */
108 if ((argc
- optind
) != 1)
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");
116 if (force
|| host
!= NULL
|| addr
!= NULL
)
117 errx(1, "-u can't be used with -f, -h, or -a");
119 errx(1, "-u cannot be used without -o");
121 if ((host
== NULL
&& addr
== NULL
) ||
122 (host
!= NULL
&& addr
!= NULL
))
126 if (ether_hostton(host
, &_ether_addr
) == -1)
127 errx(1, "ethernet address couldn't be found for \"%s\"",
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",
139 outfilename
= outfile
;
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]);
155 printf("netboot: %s\n", netboot
);
157 printf("unsetting configuration\n");
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
);
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
);
175 printf("reading %s...\n", netboot
);
176 netbb
= malloc(sb
.st_size
);
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
);
185 printf("closing %s...\n", netboot
);
189 printf("looking for netbbinfo...\n");
191 for (qp
= (u_int64_t
*)netbb
; qp
< (u_int64_t
*)(netbb
+ sb
.st_size
);
193 if (((struct netbbinfo
*)qp
)->magic1
== 0xfeedbabedeadbeefLL
&&
194 ((struct netbbinfo
*)qp
)->magic2
== 0xfeedbeefdeadbabeLL
) {
195 netbbinfop
= (struct netbbinfo
*)qp
;
199 if (netbbinfop
== NULL
)
200 errx(1, "netboot information structure not found in %s",
203 printf("found netbbinfo structure at offset 0x%lx.\n",
204 (unsigned long)((char *)netbbinfop
- netbb
));
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;
221 printf("setting netbbinfo checksum...\n");
223 for (i
= 0, qp
= (u_int64_t
*)netbbinfop
;
224 i
< (sizeof *netbbinfop
/ sizeof (u_int64_t
)); i
++, qp
++)
226 netbbinfop
->cksum
= -csum
;
229 printf("opening %s...\n", outfilename
);
230 if ((fd
= open(outfilename
, O_WRONLY
| O_CREAT
, 0666)) == -1)
231 err(1, "open: %s", outfilename
);
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
);
240 printf("closing %s...\n", outfilename
);