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