Home | History | Annotate | Line # | Download | only in mdsetimage
mdsetimage.c revision 1.10
      1 /* $NetBSD: mdsetimage.c,v 1.10 2000/06/14 17:26:18 cgd Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *          This product includes software developed for the
     18  *          NetBSD Project.  See http://www.netbsd.org/ for
     19  *          information about NetBSD.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 __COPYRIGHT(
     40     "@(#) Copyright (c) 1996 Christopher G. Demetriou.\
     41   All rights reserved.\n");
     42 #endif /* not lint */
     43 
     44 #ifndef lint
     45 __RCSID("$NetBSD: mdsetimage.c,v 1.10 2000/06/14 17:26:18 cgd Exp $");
     46 #endif /* not lint */
     47 
     48 #include <sys/types.h>
     49 #include <sys/mman.h>
     50 #include <sys/stat.h>
     51 
     52 #include <err.h>
     53 #include <fcntl.h>
     54 #include <limits.h>
     55 #include <nlist.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <unistd.h>
     59 
     60 #include "extern.h"
     61 
     62 int		main __P((int, char *[]));
     63 static void	usage __P((void)) __attribute__((noreturn));
     64 static int	find_md_root __P((const char *, const char *, size_t,
     65 		    const struct nlist *, size_t *, u_int32_t *));
     66 
     67 static struct nlist md_root_nlist[] = {
     68 #define	X_MD_ROOT_IMAGE		0
     69 	{ "_md_root_image" },
     70 #define	X_MD_ROOT_SIZE		1
     71 	{ "_md_root_size" },
     72 	{ NULL }
     73 };
     74 
     75 int	verbose;
     76 #ifdef NLIST_AOUT
     77 /*
     78  * Since we can't get the text address from an a.out executable, we
     79  * need to be able to specify it.  Note: there's no way to test to
     80  * see if the user entered a valid address!
     81  */
     82 int	T_flag_specified;	/* the -T flag was specified */
     83 u_long	text_start;		/* Start of kernel text */
     84 #endif /* NLIST_AOUT */
     85 
     86 int
     87 main(argc, argv)
     88 	int argc;
     89 	char *argv[];
     90 {
     91 	struct stat ksb, fssb;
     92 	size_t md_root_offset;
     93 	u_int32_t md_root_size;
     94 	const char *kfile, *fsfile;
     95 	char *mappedkfile;
     96 	int ch, kfd, fsfd, rv;
     97 
     98 	while ((ch = getopt(argc, argv, "T:v")) != -1)
     99 		switch (ch) {
    100 		case 'v':
    101 			verbose = 1;
    102 			break;
    103 		case 'T':
    104 #ifdef NLIST_AOUT
    105 			T_flag_specified = 1;
    106 			text_start = strtoul(optarg, NULL, 0);
    107 			break;
    108 #endif /* NLIST_AOUT */
    109 			/* FALLTHROUGH */
    110 		case '?':
    111 		default:
    112 			usage();
    113 	}
    114 	argc -= optind;
    115 	argv += optind;
    116 
    117 	if (argc != 2)
    118 		usage();
    119 	kfile = argv[0];
    120 	fsfile = argv[1];
    121 
    122 	if ((kfd = open(kfile, O_RDWR, 0))  == -1)
    123 		err(1, "open %s", kfile);
    124 
    125 	if ((rv = __fdnlist(kfd, md_root_nlist)) != 0)
    126 		errx(1, "could not find symbols in %s", kfile);
    127 	if (verbose)
    128 		fprintf(stderr, "got symbols from %s\n", kfile);
    129 
    130 	if (fstat(kfd, &ksb) == -1)
    131 		err(1, "fstat %s", kfile);
    132 	if (ksb.st_size > SIZE_T_MAX)
    133 		errx(1, "%s too big to map", kfile);
    134 
    135 	if ((mappedkfile = mmap(NULL, ksb.st_size, PROT_READ | PROT_WRITE,
    136 	    MAP_FILE | MAP_SHARED, kfd, 0)) == (caddr_t)-1)
    137 		err(1, "mmap %s", kfile);
    138 	if (verbose)
    139 		fprintf(stderr, "mapped %s\n", kfile);
    140 
    141 	if (find_md_root(kfile, mappedkfile, ksb.st_size, md_root_nlist,
    142 	    &md_root_offset, &md_root_size) != 0)
    143 		errx(1, "could not find md root buffer in %s", kfile);
    144 
    145 	if ((fsfd = open(fsfile, O_RDONLY, 0)) == -1)
    146 		err(1, "open %s", fsfile);
    147 	if (fstat(fsfd, &fssb) == -1)
    148 		err(1, "fstat %s", fsfile);
    149 	if (fssb.st_size > SIZE_T_MAX)
    150 		errx(1, "fs image is too big");
    151 	if (fssb.st_size > md_root_size)
    152 		errx(1, "fs image (%qd bytes) too big for buffer (%ld bytes)",
    153 		     (long long)fssb.st_size, (unsigned long)md_root_size);
    154 
    155 	if (verbose)
    156 		fprintf(stderr, "copying image from %s into %s\n", fsfile,
    157 		    kfile);
    158 	if ((rv = read(fsfd, mappedkfile + md_root_offset,
    159 	    fssb.st_size)) != fssb.st_size) {
    160 		if (rv == -1)
    161 			err(1, "read %s", fsfile);
    162 		else
    163 			errx(1, "unexpected EOF reading %s", fsfile);
    164 	}
    165 	if (verbose)
    166 		fprintf(stderr, "done copying image\n");
    167 
    168 	close(fsfd);
    169 
    170 	munmap(mappedkfile, ksb.st_size);
    171 	close(kfd);
    172 
    173 	if (verbose)
    174 		fprintf(stderr, "exiting\n");
    175 	exit(0);
    176 }
    177 
    178 static void
    179 usage()
    180 {
    181 	extern const char *__progname;
    182 
    183 	fprintf(stderr, "usage: %s kernel_file fsimage_file\n", __progname);
    184 	exit(1);
    185 }
    186 
    187 
    188 struct {
    189 	const char *name;
    190 	int	(*check) __P((const char *, size_t));
    191 	int	(*findoff) __P((const char *, size_t, u_long, size_t *));
    192 } exec_formats[] = {
    193 #ifdef NLIST_AOUT
    194 	{	"a.out",	check_aout,	findoff_aout,	},
    195 #endif
    196 #ifdef NLIST_ECOFF
    197 	{	"ECOFF",	check_ecoff,	findoff_ecoff,	},
    198 #endif
    199 #ifdef NLIST_ELF32
    200 	{	"ELF32",	check_elf32,	findoff_elf32,	},
    201 #endif
    202 #ifdef NLIST_ELF64
    203 	{	"ELF64",	check_elf64,	findoff_elf64,	},
    204 #endif
    205 #ifdef NLIST_COFF
    206 	{	"COFF",		check_coff,	findoff_coff,	},
    207 #endif
    208 };
    209 
    210 static int
    211 find_md_root(fname, mappedfile, mappedsize, nl, rootoffp, rootsizep)
    212 	const char *fname, *mappedfile;
    213 	size_t mappedsize;
    214 	const struct nlist *nl;
    215 	size_t *rootoffp;
    216 	u_int32_t *rootsizep;
    217 {
    218 	int i, n;
    219 	size_t rootsizeoff;
    220 
    221 	n = sizeof exec_formats / sizeof exec_formats[0];
    222 	for (i = 0; i < n; i++) {
    223 		if ((*exec_formats[i].check)(mappedfile, mappedsize) == 0)
    224 			break;
    225 	}
    226 	if (i == n) {
    227 		warnx("%s: unknown executable format", fname);
    228 		return (1);
    229 	}
    230 
    231 	if (verbose) {
    232 		fprintf(stderr, "%s is an %s binary\n", fname,
    233 		    exec_formats[i].name);
    234 #ifdef NLIST_AOUT
    235 		if (T_flag_specified)
    236 			fprintf(stderr, "kernel text loads at 0x%lx\n",
    237 			    text_start);
    238 #endif
    239 	}
    240 
    241 	if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
    242 	    nl[X_MD_ROOT_SIZE].n_value, &rootsizeoff) != 0) {
    243 		warnx("couldn't find offset for %s in %s",
    244 		    nl[X_MD_ROOT_SIZE].n_name, fname);
    245 		return (1);
    246 	}
    247 	if (verbose)
    248 		fprintf(stderr, "%s is at offset %#lx in %s\n",
    249 			nl[X_MD_ROOT_SIZE].n_name,
    250 			(unsigned long)rootsizeoff, fname);
    251 	*rootsizep = *(u_int32_t *)&mappedfile[rootsizeoff];
    252 	if (verbose)
    253 		fprintf(stderr, "%s has value %#x\n",
    254 		    nl[X_MD_ROOT_SIZE].n_name, *rootsizep);
    255 
    256 	if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
    257 	    nl[X_MD_ROOT_IMAGE].n_value, rootoffp) != 0) {
    258 		warnx("couldn't find offset for %s in %s",
    259 		    nl[X_MD_ROOT_IMAGE].n_name, fname);
    260 		return (1);
    261 	}
    262 	if (verbose)
    263 		fprintf(stderr, "%s is at offset %#lx in %s\n",
    264 			nl[X_MD_ROOT_IMAGE].n_name,
    265 			(unsigned long)(*rootoffp), fname);
    266 
    267 	return (0);
    268 }
    269