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