1 /* $NetBSD: setnetbootinfo.c,v 1.14 2014/03/26 08:09:06 christos 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 <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>
44 
45 #include "stand/common/bbinfo.h"
46 
47 static void usage(void);
48 int main(int argc, char *argv[]);
49 
50 int       verbose, force, unset;
51 char      *netboot, *outfile, *addr, *host;
52 
53 char      *outfilename;
54 
55 struct ether_addr *ether_addr, _ether_addr;
56 
57 static void
usage(void)58 usage(void)
59 {
60 
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);
66 }
67 
68 int
main(int argc,char * argv[])69 main(int argc, char *argv[])
70 {
71           struct netbbinfo *netbbinfop;
72           struct stat sb;
73           u_int64_t *qp, csum;
74           char *netbb;
75           int c, fd, i;
76 
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();
105                     }
106           }
107 
108           if ((argc - optind) != 1)
109                     usage();
110           netboot = argv[optind];
111 
112           if (unset && (force || host != NULL || addr != NULL))
113                     errx(1, "-u can't be used with -f, -h, or -a");
114 
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();
124 
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);
135                     }
136           }
137 
138           if (outfile != NULL)
139                     outfilename = outfile;
140           else {
141                     /* name + 12 for enet addr + '.' before enet addr + NUL */
142                     size_t len = strlen(netboot) + 14;
143                     outfilename = malloc(len);
144                     if (outfilename == NULL)
145                               err(1, "malloc of output file name failed");
146                     snprintf(outfilename, len,
147                         "%s.%02x%02x%02x%02x%02x%02x", netboot,
148                         ether_addr->ether_addr_octet[0],
149                         ether_addr->ether_addr_octet[1],
150                         ether_addr->ether_addr_octet[2],
151                         ether_addr->ether_addr_octet[3],
152                         ether_addr->ether_addr_octet[4],
153                         ether_addr->ether_addr_octet[5]);
154           }
155 
156           if (verbose) {
157                     printf("netboot: %s\n", netboot);
158                     if (unset)
159                               printf("unsetting configuration\n");
160                     else
161                               printf("ethernet address: %s (%s), force = %d\n",
162                                   ether_ntoa(ether_addr), host ? host : addr, force);
163                     printf("output netboot: %s\n", outfilename);
164           }
165 
166 
167           if (verbose)
168                     printf("opening %s...\n", netboot);
169           if ((fd = open(netboot, O_RDONLY, 0)) == -1)
170                     err(1, "open: %s", netboot);
171           if (fstat(fd, &sb) == -1)
172                     err(1, "fstat: %s", netboot);
173           if (!S_ISREG(sb.st_mode))
174                     errx(1, "%s must be a regular file", netboot);
175 
176           if (verbose)
177                     printf("reading %s...\n", netboot);
178           netbb = malloc(sb.st_size);
179           if (netbb == NULL)
180                     err(1, "malloc of %lu for %s failed",
181                         (unsigned long)sb.st_size, netboot);
182           if (read(fd, netbb, sb.st_size) != sb.st_size)
183                     err(1, "read of %lu from %s failed",
184                         (unsigned long)sb.st_size, netboot);
185 
186           if (verbose)
187                     printf("closing %s...\n", netboot);
188           close(fd);
189 
190           if (verbose)
191                     printf("looking for netbbinfo...\n");
192           netbbinfop = NULL;
193           for (qp = (u_int64_t *)netbb; qp < (u_int64_t *)(netbb + sb.st_size);
194               qp++) {
195                     if (((struct netbbinfo *)qp)->magic1 == 0xfeedbabedeadbeefLL &&
196                         ((struct netbbinfo *)qp)->magic2 == 0xfeedbeefdeadbabeLL) {
197                               netbbinfop = (struct netbbinfo *)qp;
198                               break;
199                     }
200           }
201           if (netbbinfop == NULL)
202                     errx(1, "netboot information structure not found in %s",
203                         netboot);
204           if (verbose)
205                     printf("found netbbinfo structure at offset 0x%lx.\n",
206                         (unsigned long)((char *)netbbinfop - netbb));
207 
208           if (verbose)
209                     printf("setting netbbinfo structure...\n");
210           memset(netbbinfop, 0, sizeof *netbbinfop);
211           netbbinfop->magic1 = 0xfeedbabedeadbeefLL;
212           netbbinfop->magic2 = 0xfeedbeefdeadbabeLL;
213           netbbinfop->set = unset ? 0 : 1;
214           if (netbbinfop->set) {
215                     for (i = 0; i < 6; i++)
216                               netbbinfop->ether_addr[i] =
217                                   ether_addr->ether_addr_octet[i];
218                     netbbinfop->force = force;
219           }
220           netbbinfop->cksum = 0;
221 
222           if (verbose)
223                     printf("setting netbbinfo checksum...\n");
224           csum = 0;
225           for (i = 0, qp = (u_int64_t *)netbbinfop;
226               i < (sizeof *netbbinfop / sizeof (u_int64_t)); i++, qp++)
227                     csum += *qp;
228           netbbinfop->cksum = -csum;
229 
230           if (verbose)
231                     printf("opening %s...\n", outfilename);
232           if ((fd = open(outfilename, O_WRONLY | O_CREAT, 0666)) == -1)
233                     err(1, "open: %s", outfilename);
234 
235           if (verbose)
236                     printf("writing %s...\n", outfilename);
237           if (write(fd, netbb, sb.st_size) != sb.st_size)
238                     err(1, "write of %lu to %s failed",
239                         (unsigned long)sb.st_size, outfilename);
240 
241           if (verbose)
242                     printf("closing %s...\n", outfilename);
243           close(fd);
244 
245           free(netbb);
246           if (outfile == NULL)
247                     free(outfilename);
248 
249           exit (0);
250 }
251