mdsetimage.c revision 1.7 1 /* $NetBSD: mdsetimage.c,v 1.7 1998/08/27 18:03:44 ross 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.7 1998/08/27 18:03:44 ross 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 }
161 if (verbose)
162 fprintf(stderr, "done copying image\n");
163
164 close(fsfd);
165
166 munmap(mappedkfile, ksb.st_size);
167 close(kfd);
168
169 if (verbose)
170 fprintf(stderr, "exiting\n");
171 exit(0);
172 }
173
174 static void
175 usage()
176 {
177 extern const char *__progname;
178
179 fprintf(stderr, "usage: %s kernel_file fsimage_file\n", __progname);
180 exit(1);
181 }
182
183
184 struct {
185 const char *name;
186 int (*check) __P((const char *, size_t));
187 int (*findoff) __P((const char *, size_t, u_long, size_t *));
188 } exec_formats[] = {
189 #ifdef NLIST_AOUT
190 { "a.out", check_aout, findoff_aout, },
191 #endif
192 #ifdef NLIST_ECOFF
193 { "ECOFF", check_ecoff, findoff_ecoff, },
194 #endif
195 #ifdef NLIST_ELF32
196 { "ELF32", check_elf32, findoff_elf32, },
197 #endif
198 #ifdef NLIST_ELF64
199 { "ELF64", check_elf64, findoff_elf64, },
200 #endif
201 };
202
203 static int
204 find_md_root(fname, mappedfile, mappedsize, nl, rootoffp, rootsizep)
205 const char *fname, *mappedfile;
206 size_t mappedsize;
207 const struct nlist *nl;
208 size_t *rootoffp;
209 u_int32_t *rootsizep;
210 {
211 int i, n;
212 size_t rootsizeoff;
213
214 n = sizeof exec_formats / sizeof exec_formats[0];
215 for (i = 0; i < n; i++) {
216 if ((*exec_formats[i].check)(mappedfile, mappedsize) == 0)
217 break;
218 }
219 if (i == n) {
220 warnx("%s: unknown executable format", fname);
221 return (1);
222 }
223
224 if (verbose) {
225 fprintf(stderr, "%s is an %s binary\n", fname,
226 exec_formats[i].name);
227 #ifdef NLIST_AOUT
228 if (T_flag_specified)
229 fprintf(stderr, "kernel text loads at 0x%lx\n",
230 text_start);
231 #endif
232 }
233
234 if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
235 nl[X_MD_ROOT_SIZE].n_value, &rootsizeoff) != 0) {
236 warnx("couldn't find offset for %s in %s",
237 nl[X_MD_ROOT_SIZE].n_name, fname);
238 return (1);
239 }
240 if (verbose)
241 fprintf(stderr, "%s is at offset %#lx in %s\n",
242 nl[X_MD_ROOT_SIZE].n_name,
243 (unsigned long)rootsizeoff, fname);
244 *rootsizep = *(u_int32_t *)&mappedfile[rootsizeoff];
245 if (verbose)
246 fprintf(stderr, "%s has value %#x\n",
247 nl[X_MD_ROOT_SIZE].n_name, *rootsizep);
248
249 if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
250 nl[X_MD_ROOT_IMAGE].n_value, rootoffp) != 0) {
251 warnx("couldn't find offset for %s in %s",
252 nl[X_MD_ROOT_IMAGE].n_name, fname);
253 return (1);
254 }
255 if (verbose)
256 fprintf(stderr, "%s is at offset %#lx in %s\n",
257 nl[X_MD_ROOT_IMAGE].n_name,
258 (unsigned long)(*rootoffp), fname);
259
260 return (0);
261 }
262