mdsetimage.c revision 1.6 1 /* $NetBSD: mdsetimage.c,v 1.6 1997/10/17 10:28:44 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 #ifndef lint
35 __COPYRIGHT(
36 "@(#) Copyright (c) 1996 Christopher G. Demetriou.\
37 All rights reserved.\n");
38 #endif /* not lint */
39
40 #ifndef lint
41 __RCSID("$NetBSD: mdsetimage.c,v 1.6 1997/10/17 10:28:44 lukem Exp $");
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/mman.h>
46 #include <sys/stat.h>
47
48 #include <err.h>
49 #include <fcntl.h>
50 #include <limits.h>
51 #include <nlist.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55
56 #include "extern.h"
57
58 int main __P((int, char *[]));
59 static void usage __P((void)) __attribute__((noreturn));
60 static int find_md_root __P((const char *, const char *, size_t,
61 const struct nlist *, size_t *, u_int32_t *));
62
63 static struct nlist md_root_nlist[] = {
64 #define X_MD_ROOT_IMAGE 0
65 { "_md_root_image" },
66 #define X_MD_ROOT_SIZE 1
67 { "_md_root_size" },
68 { NULL }
69 };
70
71 int verbose;
72 #ifdef NLIST_AOUT
73 /*
74 * Since we can't get the text address from an a.out executable, we
75 * need to be able to specify it. Note: there's no way to test to
76 * see if the user entered a valid address!
77 */
78 int T_flag_specified; /* the -T flag was specified */
79 u_long text_start; /* Start of kernel text */
80 #endif /* NLIST_AOUT */
81
82 int
83 main(argc, argv)
84 int argc;
85 char *argv[];
86 {
87 struct stat ksb, fssb;
88 size_t md_root_offset;
89 u_int32_t md_root_size;
90 const char *kfile, *fsfile;
91 char *mappedkfile;
92 int ch, kfd, fsfd, rv;
93
94 while ((ch = getopt(argc, argv, "T:v")) != -1)
95 switch (ch) {
96 case 'v':
97 verbose = 1;
98 break;
99 case 'T':
100 #ifdef NLIST_AOUT
101 T_flag_specified = 1;
102 text_start = strtoul(optarg, NULL, 0);
103 break;
104 #endif /* NLIST_AOUT */
105 /* FALLTHROUGH */
106 case '?':
107 default:
108 usage();
109 }
110 argc -= optind;
111 argv += optind;
112
113 if (argc != 2)
114 usage();
115 kfile = argv[0];
116 fsfile = argv[1];
117
118 if ((kfd = open(kfile, O_RDWR, 0)) == -1)
119 err(1, "open %s", kfile);
120
121 if ((rv = __fdnlist(kfd, md_root_nlist)) != 0)
122 errx(1, "could not find symbols in %s", kfile);
123 if (verbose)
124 fprintf(stderr, "got symbols from %s\n", kfile);
125
126 if (fstat(kfd, &ksb) == -1)
127 err(1, "fstat %s", kfile);
128 if (ksb.st_size > SIZE_T_MAX)
129 errx(1, "%s too big to map", kfile);
130
131 if ((mappedkfile = mmap(NULL, ksb.st_size, PROT_READ | PROT_WRITE,
132 MAP_FILE | MAP_SHARED, kfd, 0)) == (caddr_t)-1)
133 err(1, "mmap %s", kfile);
134 if (verbose)
135 fprintf(stderr, "mapped %s\n", kfile);
136
137 if (find_md_root(kfile, mappedkfile, ksb.st_size, md_root_nlist,
138 &md_root_offset, &md_root_size) != 0)
139 errx(1, "could not find md root buffer in %s", kfile);
140
141 if ((fsfd = open(fsfile, O_RDONLY, 0)) == -1)
142 err(1, "open %s", fsfile);
143 if (fstat(fsfd, &fssb) == -1)
144 err(1, "fstat %s", fsfile);
145 if (fssb.st_size > SIZE_T_MAX)
146 errx(1, "fs image is too big");
147 if (fssb.st_size > md_root_size)
148 errx(1, "fs image (%qd bytes) too big for buffer (%ld bytes)",
149 (long long)fssb.st_size, (unsigned long)md_root_size);
150
151 if (verbose)
152 fprintf(stderr, "copying image from %s into %s\n", fsfile,
153 kfile);
154 if ((rv = read(fsfd, mappedkfile + md_root_offset,
155 fssb.st_size)) != fssb.st_size)
156 if (rv == -1)
157 err(1, "read %s", fsfile);
158 else
159 errx(1, "unexpected EOF reading %s", fsfile);
160 if (verbose)
161 fprintf(stderr, "done copying image\n");
162
163 close(fsfd);
164
165 munmap(mappedkfile, ksb.st_size);
166 close(kfd);
167
168 if (verbose)
169 fprintf(stderr, "exiting\n");
170 exit(0);
171 }
172
173 static void
174 usage()
175 {
176 extern const char *__progname;
177
178 fprintf(stderr, "usage: %s kernel_file fsimage_file\n", __progname);
179 exit(1);
180 }
181
182
183 struct {
184 const char *name;
185 int (*check) __P((const char *, size_t));
186 int (*findoff) __P((const char *, size_t, u_long, size_t *));
187 } exec_formats[] = {
188 #ifdef NLIST_AOUT
189 { "a.out", check_aout, findoff_aout, },
190 #endif
191 #ifdef NLIST_ECOFF
192 { "ECOFF", check_ecoff, findoff_ecoff, },
193 #endif
194 #ifdef NLIST_ELF32
195 { "ELF32", check_elf32, findoff_elf32, },
196 #endif
197 #ifdef NLIST_ELF64
198 { "ELF64", check_elf64, findoff_elf64, },
199 #endif
200 };
201
202 static int
203 find_md_root(fname, mappedfile, mappedsize, nl, rootoffp, rootsizep)
204 const char *fname, *mappedfile;
205 size_t mappedsize;
206 const struct nlist *nl;
207 size_t *rootoffp;
208 u_int32_t *rootsizep;
209 {
210 int i, n;
211 size_t rootsizeoff;
212
213 n = sizeof exec_formats / sizeof exec_formats[0];
214 for (i = 0; i < n; i++) {
215 if ((*exec_formats[i].check)(mappedfile, mappedsize) == 0)
216 break;
217 }
218 if (i == n) {
219 warnx("%s: unknown executable format", fname);
220 return (1);
221 }
222
223 if (verbose) {
224 fprintf(stderr, "%s is an %s binary\n", fname,
225 exec_formats[i].name);
226 #ifdef NLIST_AOUT
227 if (T_flag_specified)
228 fprintf(stderr, "kernel text loads at 0x%lx\n",
229 text_start);
230 #endif
231 }
232
233 if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
234 nl[X_MD_ROOT_SIZE].n_value, &rootsizeoff) != 0) {
235 warnx("couldn't find offset for %s in %s",
236 nl[X_MD_ROOT_SIZE].n_name, fname);
237 return (1);
238 }
239 if (verbose)
240 fprintf(stderr, "%s is at offset %#lx in %s\n",
241 nl[X_MD_ROOT_SIZE].n_name,
242 (unsigned long)rootsizeoff, fname);
243 *rootsizep = *(u_int32_t *)&mappedfile[rootsizeoff];
244 if (verbose)
245 fprintf(stderr, "%s has value %#x\n",
246 nl[X_MD_ROOT_SIZE].n_name, *rootsizep);
247
248 if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
249 nl[X_MD_ROOT_IMAGE].n_value, rootoffp) != 0) {
250 warnx("couldn't find offset for %s in %s",
251 nl[X_MD_ROOT_IMAGE].n_name, fname);
252 return (1);
253 }
254 if (verbose)
255 fprintf(stderr, "%s is at offset %#lx in %s\n",
256 nl[X_MD_ROOT_IMAGE].n_name,
257 (unsigned long)(*rootoffp), fname);
258
259 return (0);
260 }
261