Home | History | Annotate | Line # | Download | only in mdsetimage
mdsetimage.c revision 1.20.28.2
      1  1.20.28.2  pgoyette /*	$NetBSD: mdsetimage.c,v 1.20.28.2 2017/01/07 08:57:00 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.2  pgoyette __RCSID("$NetBSD: mdsetimage.c,v 1.20.28.2 2017/01/07 08:57:00 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.20.28.2  pgoyette #include <stdint.h>
     51        1.1       cgd #include <stdio.h>
     52        1.6     lukem #include <stdlib.h>
     53        1.1       cgd #include <unistd.h>
     54  1.20.28.1  pgoyette #include <string.h>
     55        1.1       cgd 
     56  1.20.28.1  pgoyette #include "bin.h"
     57        1.1       cgd 
     58  1.20.28.1  pgoyette #define	CHUNKSIZE	(64 * 1024)
     59        1.1       cgd 
     60  1.20.28.1  pgoyette static void	usage(void) __attribute__((noreturn));
     61        1.1       cgd 
     62        1.1       cgd int	verbose;
     63  1.20.28.1  pgoyette int	extract;
     64  1.20.28.1  pgoyette int	setsize;
     65  1.20.28.1  pgoyette 
     66  1.20.28.1  pgoyette static const char *progname;
     67  1.20.28.1  pgoyette #undef setprogname
     68  1.20.28.1  pgoyette #define	setprogname(x)	(void)(progname = (x))
     69  1.20.28.1  pgoyette #undef getprogname
     70  1.20.28.1  pgoyette #define	getprogname()	(progname)
     71        1.1       cgd 
     72        1.1       cgd int
     73  1.20.28.1  pgoyette main(int argc, char *argv[])
     74        1.1       cgd {
     75  1.20.28.1  pgoyette 	int ch, kfd, fsfd, rv;
     76        1.1       cgd 	struct stat ksb, fssb;
     77  1.20.28.1  pgoyette 	size_t md_root_image_offset, md_root_size_offset;
     78  1.20.28.1  pgoyette 	u_int32_t md_root_size_value;
     79        1.1       cgd 	const char *kfile, *fsfile;
     80        1.1       cgd 	char *mappedkfile;
     81  1.20.28.1  pgoyette 	char *bfdname = NULL;
     82  1.20.28.1  pgoyette 	void *bin;
     83  1.20.28.1  pgoyette 	ssize_t left_to_copy;
     84  1.20.28.1  pgoyette 	const char *md_root_image = "_md_root_image";
     85  1.20.28.1  pgoyette 	const char *md_root_size = "_md_root_size";
     86  1.20.28.1  pgoyette 	unsigned long text_start = ~0;
     87       1.13       cgd 
     88       1.13       cgd 	setprogname(argv[0]);
     89        1.1       cgd 
     90  1.20.28.1  pgoyette 	while ((ch = getopt(argc, argv, "I:S:b:svx")) != -1)
     91        1.1       cgd 		switch (ch) {
     92       1.20  uebayasi 		case 'I':
     93  1.20.28.1  pgoyette 			md_root_image = optarg;
     94       1.20  uebayasi 			break;
     95       1.20  uebayasi 		case 'S':
     96  1.20.28.1  pgoyette 			md_root_size = optarg;
     97       1.20  uebayasi 			break;
     98        1.5    scottr 		case 'T':
     99        1.5    scottr 			text_start = strtoul(optarg, NULL, 0);
    100        1.5    scottr 			break;
    101  1.20.28.1  pgoyette 		case 'b':
    102  1.20.28.1  pgoyette 			bfdname = optarg;
    103  1.20.28.1  pgoyette 			break;
    104  1.20.28.1  pgoyette 		case 's':
    105  1.20.28.1  pgoyette 			setsize = 1;
    106  1.20.28.1  pgoyette 			break;
    107  1.20.28.1  pgoyette 		case 'v':
    108  1.20.28.1  pgoyette 			verbose = 1;
    109  1.20.28.1  pgoyette 			break;
    110  1.20.28.1  pgoyette 		case 'x':
    111  1.20.28.1  pgoyette 			extract = 1;
    112  1.20.28.1  pgoyette 			break;
    113        1.1       cgd 		case '?':
    114        1.1       cgd 		default:
    115        1.1       cgd 			usage();
    116        1.1       cgd 	}
    117        1.1       cgd 	argc -= optind;
    118        1.1       cgd 	argv += optind;
    119        1.1       cgd 
    120        1.1       cgd 	if (argc != 2)
    121        1.1       cgd 		usage();
    122        1.1       cgd 	kfile = argv[0];
    123        1.1       cgd 	fsfile = argv[1];
    124        1.1       cgd 
    125  1.20.28.1  pgoyette 	if (extract) {
    126  1.20.28.1  pgoyette 		if ((kfd = open(kfile, O_RDONLY, 0))  == -1)
    127  1.20.28.1  pgoyette 			err(1, "open %s", kfile);
    128  1.20.28.1  pgoyette 	} else {
    129  1.20.28.1  pgoyette 		if ((kfd = open(kfile, O_RDWR, 0))  == -1)
    130  1.20.28.1  pgoyette 			err(1, "open %s", kfile);
    131  1.20.28.1  pgoyette 	}
    132        1.1       cgd 
    133        1.1       cgd 	if (fstat(kfd, &ksb) == -1)
    134        1.1       cgd 		err(1, "fstat %s", kfile);
    135  1.20.28.1  pgoyette 	if ((uintmax_t)ksb.st_size != (size_t)ksb.st_size)
    136        1.1       cgd 		errx(1, "%s too big to map", kfile);
    137        1.1       cgd 
    138  1.20.28.1  pgoyette 	if ((mappedkfile = mmap(NULL, ksb.st_size, PROT_READ,
    139  1.20.28.1  pgoyette 	    MAP_FILE | MAP_PRIVATE, kfd, 0)) == (caddr_t)-1)
    140        1.1       cgd 		err(1, "mmap %s", kfile);
    141        1.1       cgd 	if (verbose)
    142        1.1       cgd 		fprintf(stderr, "mapped %s\n", kfile);
    143        1.1       cgd 
    144  1.20.28.1  pgoyette 	bin = bin_open(kfd, kfile, bfdname);
    145  1.20.28.1  pgoyette 
    146  1.20.28.1  pgoyette 	if (bin_find_md_root(bin, mappedkfile, ksb.st_size, text_start,
    147  1.20.28.1  pgoyette 	    md_root_image, md_root_size, &md_root_image_offset,
    148  1.20.28.1  pgoyette 	    &md_root_size_offset, &md_root_size_value, verbose) != 0)
    149  1.20.28.1  pgoyette 		errx(1, "could not find symbols in %s", kfile);
    150  1.20.28.1  pgoyette 	if (verbose)
    151  1.20.28.1  pgoyette 		fprintf(stderr, "got symbols from %s\n", kfile);
    152        1.1       cgd 
    153        1.1       cgd 	if (verbose)
    154  1.20.28.1  pgoyette 		fprintf(stderr, "root @ %#zx/%u\n",
    155  1.20.28.1  pgoyette 		    md_root_image_offset, md_root_size_value);
    156  1.20.28.1  pgoyette 
    157  1.20.28.1  pgoyette 	munmap(mappedkfile, ksb.st_size);
    158  1.20.28.1  pgoyette 
    159  1.20.28.1  pgoyette 	if (extract) {
    160  1.20.28.1  pgoyette 		if ((fsfd = open(fsfile, O_WRONLY|O_CREAT, 0777)) == -1)
    161  1.20.28.1  pgoyette 			err(1, "open %s", fsfile);
    162  1.20.28.1  pgoyette 		left_to_copy = md_root_size_value;
    163  1.20.28.1  pgoyette 	} else {
    164  1.20.28.1  pgoyette 		if ((fsfd = open(fsfile, O_RDONLY, 0)) == -1)
    165  1.20.28.1  pgoyette 			err(1, "open %s", fsfile);
    166  1.20.28.1  pgoyette 		if (fstat(fsfd, &fssb) == -1)
    167  1.20.28.1  pgoyette 			err(1, "fstat %s", fsfile);
    168  1.20.28.1  pgoyette 		if ((uintmax_t)fssb.st_size != (size_t)fssb.st_size)
    169  1.20.28.1  pgoyette 			errx(1, "fs image is too big");
    170  1.20.28.1  pgoyette 		if (fssb.st_size > md_root_size_value)
    171  1.20.28.1  pgoyette 			errx(1, "fs image (%jd bytes) too big for buffer"
    172  1.20.28.1  pgoyette 			    " (%u bytes)", (intmax_t) fssb.st_size,
    173  1.20.28.1  pgoyette 			    md_root_size_value);
    174  1.20.28.1  pgoyette 		left_to_copy = fssb.st_size;
    175  1.20.28.1  pgoyette 	}
    176  1.20.28.1  pgoyette 
    177  1.20.28.1  pgoyette 	if (verbose)
    178  1.20.28.1  pgoyette 		fprintf(stderr, "copying image %s %s %s (%zd bytes)\n", fsfile,
    179  1.20.28.1  pgoyette 		    (extract ? "from" : "into"), kfile, left_to_copy);
    180  1.20.28.1  pgoyette 
    181  1.20.28.1  pgoyette 	if (lseek(kfd, md_root_image_offset, SEEK_SET) !=
    182  1.20.28.1  pgoyette 	    (off_t)md_root_image_offset)
    183  1.20.28.1  pgoyette 		err(1, "seek %s", kfile);
    184  1.20.28.1  pgoyette 	while (left_to_copy > 0) {
    185  1.20.28.1  pgoyette 		char buf[CHUNKSIZE];
    186  1.20.28.1  pgoyette 		ssize_t todo;
    187  1.20.28.1  pgoyette 		int rfd;
    188  1.20.28.1  pgoyette 		int wfd;
    189  1.20.28.1  pgoyette 		const char *rfile;
    190  1.20.28.1  pgoyette 		const char *wfile;
    191  1.20.28.1  pgoyette 		if (extract) {
    192  1.20.28.1  pgoyette 			rfd = kfd;
    193  1.20.28.1  pgoyette 			rfile = kfile;
    194  1.20.28.1  pgoyette 			wfd = fsfd;
    195  1.20.28.1  pgoyette 			wfile = fsfile;
    196  1.20.28.1  pgoyette 		} else {
    197  1.20.28.1  pgoyette 			rfd = fsfd;
    198  1.20.28.1  pgoyette 			rfile = fsfile;
    199  1.20.28.1  pgoyette 			wfd = kfd;
    200  1.20.28.1  pgoyette 			wfile = kfile;
    201  1.20.28.1  pgoyette 		}
    202  1.20.28.1  pgoyette 
    203  1.20.28.1  pgoyette 		todo = (left_to_copy > CHUNKSIZE) ? CHUNKSIZE : left_to_copy;
    204  1.20.28.1  pgoyette 		if ((rv = read(rfd, buf, todo)) != todo) {
    205  1.20.28.1  pgoyette 			if (rv == -1)
    206  1.20.28.1  pgoyette 				err(1, "read %s", rfile);
    207  1.20.28.1  pgoyette 			else
    208  1.20.28.1  pgoyette 				errx(1, "unexpected EOF reading %s", rfile);
    209  1.20.28.1  pgoyette 		}
    210  1.20.28.1  pgoyette 		if ((rv = write(wfd, buf, todo)) != todo) {
    211  1.20.28.1  pgoyette 			if (rv == -1)
    212  1.20.28.1  pgoyette 				err(1, "write %s", wfile);
    213  1.20.28.1  pgoyette 			else
    214  1.20.28.1  pgoyette 				errx(1, "short write writing %s", wfile);
    215  1.20.28.1  pgoyette 		}
    216  1.20.28.1  pgoyette 		left_to_copy -= todo;
    217        1.7      ross 	}
    218        1.1       cgd 	if (verbose)
    219        1.1       cgd 		fprintf(stderr, "done copying image\n");
    220  1.20.28.1  pgoyette 	if (setsize && !extract) {
    221  1.20.28.1  pgoyette 		char buf[sizeof(uint32_t)];
    222        1.1       cgd 
    223  1.20.28.1  pgoyette 		if (verbose)
    224  1.20.28.1  pgoyette 			fprintf(stderr, "setting md_root_size to %jd\n",
    225  1.20.28.1  pgoyette 			    (intmax_t) fssb.st_size);
    226  1.20.28.1  pgoyette 		if (lseek(kfd, md_root_size_offset, SEEK_SET) !=
    227  1.20.28.1  pgoyette 		    (off_t)md_root_size_offset)
    228  1.20.28.1  pgoyette 			err(1, "seek %s", kfile);
    229  1.20.28.1  pgoyette 		bin_put_32(bin, fssb.st_size, buf);
    230  1.20.28.1  pgoyette 		if (write(kfd, buf, sizeof(buf)) != sizeof(buf))
    231  1.20.28.1  pgoyette 			err(1, "write %s", kfile);
    232  1.20.28.1  pgoyette 	}
    233  1.20.28.1  pgoyette 
    234  1.20.28.1  pgoyette 	close(fsfd);
    235        1.1       cgd 	close(kfd);
    236        1.1       cgd 
    237        1.1       cgd 	if (verbose)
    238        1.1       cgd 		fprintf(stderr, "exiting\n");
    239  1.20.28.1  pgoyette 
    240  1.20.28.1  pgoyette 	bin_close(bin);
    241  1.20.28.1  pgoyette 	return 0;
    242        1.1       cgd }
    243        1.1       cgd 
    244        1.1       cgd static void
    245  1.20.28.1  pgoyette usage(void)
    246        1.1       cgd {
    247  1.20.28.1  pgoyette 	const char **list;
    248        1.1       cgd 
    249  1.20.28.1  pgoyette 	fprintf(stderr, "Usage: %s [-svx] [-b bfdname] [-I image_symbol] "
    250  1.20.28.1  pgoyette 	    "[-S size_symbol] [-T address] kernel image\n", getprogname());
    251  1.20.28.1  pgoyette 	fprintf(stderr, "Supported targets:");
    252  1.20.28.1  pgoyette 	for (list = bin_supported_targets(); *list != NULL; list++)
    253  1.20.28.1  pgoyette 		fprintf(stderr, " %s", *list);
    254  1.20.28.1  pgoyette 	fprintf(stderr, "\n");
    255        1.1       cgd 	exit(1);
    256        1.1       cgd }
    257