mdsetimage.c revision 1.4 1 /* $NetBSD: mdsetimage.c,v 1.4 1997/02/11 22:40:25 cgd 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 #ifndef lint
34 static char copyright[] =
35 "@(#) Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.\n";
36 #endif /* not lint */
37
38 #ifndef lint
39 static char *rcsid = "$NetBSD: mdsetimage.c,v 1.4 1997/02/11 22:40:25 cgd Exp $";
40 #endif /* not lint */
41
42 #include <sys/types.h>
43 #include <sys/mman.h>
44 #include <sys/stat.h>
45
46 #include <err.h>
47 #include <fcntl.h>
48 #include <limits.h>
49 #include <nlist.h>
50 #include <stdio.h>
51 #include <unistd.h>
52
53 #include "extern.h"
54
55 int main __P((int, char *[]));
56 static void usage __P((void)) __attribute__((noreturn));
57 static int find_md_root __P((const char *, const char *, size_t,
58 const struct nlist *, size_t *, u_int32_t *));
59
60 static struct nlist md_root_nlist[] = {
61 #define X_MD_ROOT_IMAGE 0
62 { "_md_root_image" },
63 #define X_MD_ROOT_SIZE 1
64 { "_md_root_size" },
65 { NULL }
66 };
67
68 int verbose;
69
70 int
71 main(argc, argv)
72 int argc;
73 char *argv[];
74 {
75 struct stat ksb, fssb;
76 size_t md_root_offset;
77 u_int32_t md_root_size;
78 const char *kfile, *fsfile;
79 char *mappedkfile;
80 int ch, kfd, fsfd, rv;
81
82 while ((ch = getopt(argc, argv, "v")) != -1)
83 switch (ch) {
84 case 'v':
85 verbose = 1;
86 break;
87 case '?':
88 default:
89 usage();
90 }
91 argc -= optind;
92 argv += optind;
93
94 if (argc != 2)
95 usage();
96 kfile = argv[0];
97 fsfile = argv[1];
98
99 if ((kfd = open(kfile, O_RDWR, 0)) == -1)
100 err(1, "open %s", kfile);
101
102 if ((rv = __fdnlist(kfd, md_root_nlist)) != 0)
103 errx(1, "could not find symbols in %s", kfile);
104 if (verbose)
105 fprintf(stderr, "got symbols from %s\n", kfile);
106
107 if (fstat(kfd, &ksb) == -1)
108 err(1, "fstat %s", kfile);
109 if (ksb.st_size > SIZE_T_MAX)
110 errx(1, "%s too big to map", kfile);
111
112 if ((mappedkfile = mmap(NULL, ksb.st_size, PROT_READ | PROT_WRITE,
113 MAP_FILE | MAP_SHARED, kfd, 0)) == (caddr_t)-1)
114 err(1, "mmap %s", kfile);
115 if (verbose)
116 fprintf(stderr, "mapped %s\n", kfile);
117
118 if (find_md_root(kfile, mappedkfile, ksb.st_size, md_root_nlist,
119 &md_root_offset, &md_root_size) != 0)
120 errx(1, "could not find md root buffer in %s", kfile);
121
122 if ((fsfd = open(fsfile, O_RDONLY, 0)) == -1)
123 err(1, "open %s", fsfile);
124 if (fstat(fsfd, &fssb) == -1)
125 err(1, "fstat %s", fsfile);
126 if (fssb.st_size > SIZE_T_MAX)
127 errx(1, "fs image is too big");
128 if (fssb.st_size > md_root_size)
129 errx(1, "fs image (%qd bytes) too big for buffer (%ld bytes)",
130 (long long)fssb.st_size, (unsigned long)md_root_size);
131
132 if (verbose)
133 fprintf(stderr, "copying image from %s into %s\n", fsfile,
134 kfile);
135 if ((rv = read(fsfd, mappedkfile + md_root_offset,
136 fssb.st_size)) != fssb.st_size)
137 if (rv == -1)
138 err(1, "read %s", fsfile);
139 else
140 errx(1, "unexpected EOF reading %s", fsfile);
141 if (verbose)
142 fprintf(stderr, "done copying image\n");
143
144 close(fsfd);
145
146 munmap(mappedkfile, ksb.st_size);
147 close(kfd);
148
149 if (verbose)
150 fprintf(stderr, "exiting\n");
151 exit(0);
152 }
153
154 static void
155 usage()
156 {
157 extern const char *__progname;
158
159 fprintf(stderr, "usage: %s kernel_file fsimage_file\n", __progname);
160 exit(1);
161 }
162
163
164 struct {
165 const char *name;
166 int (*check) __P((const char *, size_t));
167 int (*findoff) __P((const char *, size_t, u_long, size_t *));
168 } exec_formats[] = {
169 #ifdef NLIST_AOUT
170 { "a.out", check_aout, findoff_aout, },
171 #endif
172 #ifdef NLIST_ECOFF
173 { "ECOFF", check_ecoff, findoff_ecoff, },
174 #endif
175 #ifdef NLIST_ELF32
176 { "ELF32", check_elf32, findoff_elf32, },
177 #endif
178 #ifdef NLIST_ELF64
179 { "ELF64", check_elf64, findoff_elf64, },
180 #endif
181 };
182
183 static int
184 find_md_root(fname, mappedfile, mappedsize, nl, rootoffp, rootsizep)
185 const char *fname, *mappedfile;
186 size_t mappedsize;
187 const struct nlist *nl;
188 size_t *rootoffp;
189 u_int32_t *rootsizep;
190 {
191 int i, n;
192 size_t rootsizeoff;
193
194 n = sizeof exec_formats / sizeof exec_formats[0];
195 for (i = 0; i < n; i++) {
196 if ((*exec_formats[i].check)(mappedfile, mappedsize) == 0)
197 break;
198 }
199 if (i == n) {
200 warnx("%s: unknown executable format", fname);
201 return (1);
202 }
203
204 if (verbose)
205 fprintf(stderr, "%s is an %s binary\n", fname,
206 exec_formats[i].name);
207
208 if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
209 nl[X_MD_ROOT_SIZE].n_value, &rootsizeoff) != 0) {
210 warnx("couldn't find offset for %s in %s",
211 nl[X_MD_ROOT_SIZE].n_name, fname);
212 return (1);
213 }
214 if (verbose)
215 fprintf(stderr, "%s is at offset %#lx in %s\n",
216 nl[X_MD_ROOT_SIZE].n_name,
217 (unsigned long)rootsizeoff, fname);
218 *rootsizep = *(u_int32_t *)&mappedfile[rootsizeoff];
219 if (verbose)
220 fprintf(stderr, "%s has value %#x\n",
221 nl[X_MD_ROOT_SIZE].n_name, *rootsizep);
222
223 if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
224 nl[X_MD_ROOT_IMAGE].n_value, rootoffp) != 0) {
225 warnx("couldn't find offset for %s in %s",
226 nl[X_MD_ROOT_IMAGE].n_name, fname);
227 return (1);
228 }
229 if (verbose)
230 fprintf(stderr, "%s is at offset %#lx in %s\n",
231 nl[X_MD_ROOT_IMAGE].n_name,
232 (unsigned long)(*rootoffp), fname);
233
234 return (0);
235 }
236