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