mdsetimage.c revision 1.20.28.1 1 1.20.28.1 pgoyette /* $NetBSD: mdsetimage.c,v 1.20.28.1 2016/11/04 14:49:27 pgoyette Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.20.28.1 pgoyette * Copyright (c) 1996, 2002 Christopher G. Demetriou
5 1.10 cgd * All rights reserved.
6 1.10 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.16 cgd * 3. The name of the author may not be used to endorse or promote products
16 1.10 cgd * derived from this software without specific prior written permission.
17 1.10 cgd *
18 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.10 cgd *
29 1.16 cgd * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>>
30 1.1 cgd */
31 1.1 cgd
32 1.20.28.1 pgoyette #if HAVE_NBTOOL_CONFIG_H
33 1.20.28.1 pgoyette #include "nbtool_config.h"
34 1.20.28.1 pgoyette #endif
35 1.20.28.1 pgoyette
36 1.6 lukem #include <sys/cdefs.h>
37 1.20.28.1 pgoyette #if !defined(lint)
38 1.18 lukem __COPYRIGHT("@(#) Copyright (c) 1996\
39 1.18 lukem Christopher G. Demetriou. All rights reserved.");
40 1.20.28.1 pgoyette __RCSID("$NetBSD: mdsetimage.c,v 1.20.28.1 2016/11/04 14:49:27 pgoyette Exp $");
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #include <sys/types.h>
44 1.1 cgd #include <sys/mman.h>
45 1.1 cgd #include <sys/stat.h>
46 1.1 cgd
47 1.1 cgd #include <err.h>
48 1.1 cgd #include <fcntl.h>
49 1.1 cgd #include <limits.h>
50 1.1 cgd #include <stdio.h>
51 1.6 lukem #include <stdlib.h>
52 1.1 cgd #include <unistd.h>
53 1.20.28.1 pgoyette #include <string.h>
54 1.1 cgd
55 1.20.28.1 pgoyette #include "bin.h"
56 1.1 cgd
57 1.20.28.1 pgoyette #define CHUNKSIZE (64 * 1024)
58 1.1 cgd
59 1.20.28.1 pgoyette static void usage(void) __attribute__((noreturn));
60 1.1 cgd
61 1.1 cgd int verbose;
62 1.20.28.1 pgoyette int extract;
63 1.20.28.1 pgoyette int setsize;
64 1.20.28.1 pgoyette
65 1.20.28.1 pgoyette static const char *progname;
66 1.20.28.1 pgoyette #undef setprogname
67 1.20.28.1 pgoyette #define setprogname(x) (void)(progname = (x))
68 1.20.28.1 pgoyette #undef getprogname
69 1.20.28.1 pgoyette #define getprogname() (progname)
70 1.1 cgd
71 1.1 cgd int
72 1.20.28.1 pgoyette main(int argc, char *argv[])
73 1.1 cgd {
74 1.20.28.1 pgoyette int ch, kfd, fsfd, rv;
75 1.1 cgd struct stat ksb, fssb;
76 1.20.28.1 pgoyette size_t md_root_image_offset, md_root_size_offset;
77 1.20.28.1 pgoyette u_int32_t md_root_size_value;
78 1.1 cgd const char *kfile, *fsfile;
79 1.1 cgd char *mappedkfile;
80 1.20.28.1 pgoyette char *bfdname = NULL;
81 1.20.28.1 pgoyette void *bin;
82 1.20.28.1 pgoyette ssize_t left_to_copy;
83 1.20.28.1 pgoyette const char *md_root_image = "_md_root_image";
84 1.20.28.1 pgoyette const char *md_root_size = "_md_root_size";
85 1.20.28.1 pgoyette unsigned long text_start = ~0;
86 1.13 cgd
87 1.13 cgd setprogname(argv[0]);
88 1.1 cgd
89 1.20.28.1 pgoyette while ((ch = getopt(argc, argv, "I:S:b:svx")) != -1)
90 1.1 cgd switch (ch) {
91 1.20 uebayasi case 'I':
92 1.20.28.1 pgoyette md_root_image = optarg;
93 1.20 uebayasi break;
94 1.20 uebayasi case 'S':
95 1.20.28.1 pgoyette md_root_size = optarg;
96 1.20 uebayasi break;
97 1.5 scottr case 'T':
98 1.5 scottr text_start = strtoul(optarg, NULL, 0);
99 1.5 scottr break;
100 1.20.28.1 pgoyette case 'b':
101 1.20.28.1 pgoyette bfdname = optarg;
102 1.20.28.1 pgoyette break;
103 1.20.28.1 pgoyette case 's':
104 1.20.28.1 pgoyette setsize = 1;
105 1.20.28.1 pgoyette break;
106 1.20.28.1 pgoyette case 'v':
107 1.20.28.1 pgoyette verbose = 1;
108 1.20.28.1 pgoyette break;
109 1.20.28.1 pgoyette case 'x':
110 1.20.28.1 pgoyette extract = 1;
111 1.20.28.1 pgoyette break;
112 1.1 cgd case '?':
113 1.1 cgd default:
114 1.1 cgd usage();
115 1.1 cgd }
116 1.1 cgd argc -= optind;
117 1.1 cgd argv += optind;
118 1.1 cgd
119 1.1 cgd if (argc != 2)
120 1.1 cgd usage();
121 1.1 cgd kfile = argv[0];
122 1.1 cgd fsfile = argv[1];
123 1.1 cgd
124 1.20.28.1 pgoyette if (extract) {
125 1.20.28.1 pgoyette if ((kfd = open(kfile, O_RDONLY, 0)) == -1)
126 1.20.28.1 pgoyette err(1, "open %s", kfile);
127 1.20.28.1 pgoyette } else {
128 1.20.28.1 pgoyette if ((kfd = open(kfile, O_RDWR, 0)) == -1)
129 1.20.28.1 pgoyette err(1, "open %s", kfile);
130 1.20.28.1 pgoyette }
131 1.1 cgd
132 1.1 cgd if (fstat(kfd, &ksb) == -1)
133 1.1 cgd err(1, "fstat %s", kfile);
134 1.20.28.1 pgoyette if ((uintmax_t)ksb.st_size != (size_t)ksb.st_size)
135 1.1 cgd errx(1, "%s too big to map", kfile);
136 1.1 cgd
137 1.20.28.1 pgoyette if ((mappedkfile = mmap(NULL, ksb.st_size, PROT_READ,
138 1.20.28.1 pgoyette MAP_FILE | MAP_PRIVATE, kfd, 0)) == (caddr_t)-1)
139 1.1 cgd err(1, "mmap %s", kfile);
140 1.1 cgd if (verbose)
141 1.1 cgd fprintf(stderr, "mapped %s\n", kfile);
142 1.1 cgd
143 1.20.28.1 pgoyette bin = bin_open(kfd, kfile, bfdname);
144 1.20.28.1 pgoyette
145 1.20.28.1 pgoyette if (bin_find_md_root(bin, mappedkfile, ksb.st_size, text_start,
146 1.20.28.1 pgoyette md_root_image, md_root_size, &md_root_image_offset,
147 1.20.28.1 pgoyette &md_root_size_offset, &md_root_size_value, verbose) != 0)
148 1.20.28.1 pgoyette errx(1, "could not find symbols in %s", kfile);
149 1.20.28.1 pgoyette if (verbose)
150 1.20.28.1 pgoyette fprintf(stderr, "got symbols from %s\n", kfile);
151 1.1 cgd
152 1.1 cgd if (verbose)
153 1.20.28.1 pgoyette fprintf(stderr, "root @ %#zx/%u\n",
154 1.20.28.1 pgoyette md_root_image_offset, md_root_size_value);
155 1.20.28.1 pgoyette
156 1.20.28.1 pgoyette munmap(mappedkfile, ksb.st_size);
157 1.20.28.1 pgoyette
158 1.20.28.1 pgoyette if (extract) {
159 1.20.28.1 pgoyette if ((fsfd = open(fsfile, O_WRONLY|O_CREAT, 0777)) == -1)
160 1.20.28.1 pgoyette err(1, "open %s", fsfile);
161 1.20.28.1 pgoyette left_to_copy = md_root_size_value;
162 1.20.28.1 pgoyette } else {
163 1.20.28.1 pgoyette if ((fsfd = open(fsfile, O_RDONLY, 0)) == -1)
164 1.20.28.1 pgoyette err(1, "open %s", fsfile);
165 1.20.28.1 pgoyette if (fstat(fsfd, &fssb) == -1)
166 1.20.28.1 pgoyette err(1, "fstat %s", fsfile);
167 1.20.28.1 pgoyette if ((uintmax_t)fssb.st_size != (size_t)fssb.st_size)
168 1.20.28.1 pgoyette errx(1, "fs image is too big");
169 1.20.28.1 pgoyette if (fssb.st_size > md_root_size_value)
170 1.20.28.1 pgoyette errx(1, "fs image (%jd bytes) too big for buffer"
171 1.20.28.1 pgoyette " (%u bytes)", (intmax_t) fssb.st_size,
172 1.20.28.1 pgoyette md_root_size_value);
173 1.20.28.1 pgoyette left_to_copy = fssb.st_size;
174 1.20.28.1 pgoyette }
175 1.20.28.1 pgoyette
176 1.20.28.1 pgoyette if (verbose)
177 1.20.28.1 pgoyette fprintf(stderr, "copying image %s %s %s (%zd bytes)\n", fsfile,
178 1.20.28.1 pgoyette (extract ? "from" : "into"), kfile, left_to_copy);
179 1.20.28.1 pgoyette
180 1.20.28.1 pgoyette if (lseek(kfd, md_root_image_offset, SEEK_SET) !=
181 1.20.28.1 pgoyette (off_t)md_root_image_offset)
182 1.20.28.1 pgoyette err(1, "seek %s", kfile);
183 1.20.28.1 pgoyette while (left_to_copy > 0) {
184 1.20.28.1 pgoyette char buf[CHUNKSIZE];
185 1.20.28.1 pgoyette ssize_t todo;
186 1.20.28.1 pgoyette int rfd;
187 1.20.28.1 pgoyette int wfd;
188 1.20.28.1 pgoyette const char *rfile;
189 1.20.28.1 pgoyette const char *wfile;
190 1.20.28.1 pgoyette if (extract) {
191 1.20.28.1 pgoyette rfd = kfd;
192 1.20.28.1 pgoyette rfile = kfile;
193 1.20.28.1 pgoyette wfd = fsfd;
194 1.20.28.1 pgoyette wfile = fsfile;
195 1.20.28.1 pgoyette } else {
196 1.20.28.1 pgoyette rfd = fsfd;
197 1.20.28.1 pgoyette rfile = fsfile;
198 1.20.28.1 pgoyette wfd = kfd;
199 1.20.28.1 pgoyette wfile = kfile;
200 1.20.28.1 pgoyette }
201 1.20.28.1 pgoyette
202 1.20.28.1 pgoyette todo = (left_to_copy > CHUNKSIZE) ? CHUNKSIZE : left_to_copy;
203 1.20.28.1 pgoyette if ((rv = read(rfd, buf, todo)) != todo) {
204 1.20.28.1 pgoyette if (rv == -1)
205 1.20.28.1 pgoyette err(1, "read %s", rfile);
206 1.20.28.1 pgoyette else
207 1.20.28.1 pgoyette errx(1, "unexpected EOF reading %s", rfile);
208 1.20.28.1 pgoyette }
209 1.20.28.1 pgoyette if ((rv = write(wfd, buf, todo)) != todo) {
210 1.20.28.1 pgoyette if (rv == -1)
211 1.20.28.1 pgoyette err(1, "write %s", wfile);
212 1.20.28.1 pgoyette else
213 1.20.28.1 pgoyette errx(1, "short write writing %s", wfile);
214 1.20.28.1 pgoyette }
215 1.20.28.1 pgoyette left_to_copy -= todo;
216 1.7 ross }
217 1.1 cgd if (verbose)
218 1.1 cgd fprintf(stderr, "done copying image\n");
219 1.20.28.1 pgoyette if (setsize && !extract) {
220 1.20.28.1 pgoyette char buf[sizeof(uint32_t)];
221 1.1 cgd
222 1.20.28.1 pgoyette if (verbose)
223 1.20.28.1 pgoyette fprintf(stderr, "setting md_root_size to %jd\n",
224 1.20.28.1 pgoyette (intmax_t) fssb.st_size);
225 1.20.28.1 pgoyette if (lseek(kfd, md_root_size_offset, SEEK_SET) !=
226 1.20.28.1 pgoyette (off_t)md_root_size_offset)
227 1.20.28.1 pgoyette err(1, "seek %s", kfile);
228 1.20.28.1 pgoyette bin_put_32(bin, fssb.st_size, buf);
229 1.20.28.1 pgoyette if (write(kfd, buf, sizeof(buf)) != sizeof(buf))
230 1.20.28.1 pgoyette err(1, "write %s", kfile);
231 1.20.28.1 pgoyette }
232 1.20.28.1 pgoyette
233 1.20.28.1 pgoyette close(fsfd);
234 1.1 cgd close(kfd);
235 1.1 cgd
236 1.1 cgd if (verbose)
237 1.1 cgd fprintf(stderr, "exiting\n");
238 1.20.28.1 pgoyette
239 1.20.28.1 pgoyette bin_close(bin);
240 1.20.28.1 pgoyette return 0;
241 1.1 cgd }
242 1.1 cgd
243 1.1 cgd static void
244 1.20.28.1 pgoyette usage(void)
245 1.1 cgd {
246 1.20.28.1 pgoyette const char **list;
247 1.1 cgd
248 1.20.28.1 pgoyette fprintf(stderr, "Usage: %s [-svx] [-b bfdname] [-I image_symbol] "
249 1.20.28.1 pgoyette "[-S size_symbol] [-T address] kernel image\n", getprogname());
250 1.20.28.1 pgoyette fprintf(stderr, "Supported targets:");
251 1.20.28.1 pgoyette for (list = bin_supported_targets(); *list != NULL; list++)
252 1.20.28.1 pgoyette fprintf(stderr, " %s", *list);
253 1.20.28.1 pgoyette fprintf(stderr, "\n");
254 1.1 cgd exit(1);
255 1.1 cgd }
256