2010-11-06 19:03:23 +03:00
|
|
|
/* $NetBSD: mdsetimage.c,v 1.20 2010/11/06 16:03:23 uebayasi Exp $ */
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
/*
|
2000-06-14 21:24:02 +04:00
|
|
|
* Copyright (c) 1996 Christopher G. Demetriou
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1996-10-04 04:18:55 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2001-10-02 03:32:34 +04:00
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2000-06-14 21:24:02 +04:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
1996-10-04 04:18:55 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2000-06-14 21:24:02 +04:00
|
|
|
*
|
2001-10-02 03:32:34 +04:00
|
|
|
* <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>>
|
1996-10-04 04:18:55 +04:00
|
|
|
*/
|
|
|
|
|
1997-10-17 14:28:39 +04:00
|
|
|
#include <sys/cdefs.h>
|
1996-10-04 04:18:55 +04:00
|
|
|
#ifndef lint
|
2008-07-21 17:36:57 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1996\
|
|
|
|
Christopher G. Demetriou. All rights reserved.");
|
1996-10-04 04:18:55 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
2010-11-06 19:03:23 +03:00
|
|
|
__RCSID("$NetBSD: mdsetimage.c,v 1.20 2010/11/06 16:03:23 uebayasi Exp $");
|
1996-10-04 04:18:55 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <stdio.h>
|
1997-10-17 14:28:39 +04:00
|
|
|
#include <stdlib.h>
|
2010-11-06 19:03:23 +03:00
|
|
|
#include <string.h>
|
1996-10-04 04:18:55 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
int main __P((int, char *[]));
|
2007-12-15 22:44:37 +03:00
|
|
|
static void usage __P((void)) __dead;
|
1997-01-02 03:33:34 +03:00
|
|
|
static int find_md_root __P((const char *, const char *, size_t,
|
1996-10-04 04:18:55 +04:00
|
|
|
const struct nlist *, size_t *, u_int32_t *));
|
|
|
|
|
1997-01-02 03:33:34 +03:00
|
|
|
#define X_MD_ROOT_IMAGE 0
|
|
|
|
#define X_MD_ROOT_SIZE 1
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
int verbose;
|
1997-09-30 10:20:16 +04:00
|
|
|
#ifdef NLIST_AOUT
|
|
|
|
/*
|
|
|
|
* Since we can't get the text address from an a.out executable, we
|
|
|
|
* need to be able to specify it. Note: there's no way to test to
|
|
|
|
* see if the user entered a valid address!
|
|
|
|
*/
|
|
|
|
int T_flag_specified; /* the -T flag was specified */
|
|
|
|
u_long text_start; /* Start of kernel text */
|
|
|
|
#endif /* NLIST_AOUT */
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
struct stat ksb, fssb;
|
1997-01-02 03:33:34 +03:00
|
|
|
size_t md_root_offset;
|
|
|
|
u_int32_t md_root_size;
|
1996-10-04 04:18:55 +04:00
|
|
|
const char *kfile, *fsfile;
|
|
|
|
char *mappedkfile;
|
|
|
|
int ch, kfd, fsfd, rv;
|
2010-11-06 19:03:23 +03:00
|
|
|
struct nlist md_root_nlist[3];
|
|
|
|
|
|
|
|
(void)memset(md_root_nlist, 0, sizeof(md_root_nlist));
|
|
|
|
N_NAME(&md_root_nlist[X_MD_ROOT_IMAGE]) = "_md_root_image";
|
|
|
|
N_NAME(&md_root_nlist[X_MD_ROOT_SIZE]) = "_md_root_size";
|
1996-10-04 04:18:55 +04:00
|
|
|
|
2001-02-21 03:06:22 +03:00
|
|
|
setprogname(argv[0]);
|
|
|
|
|
2010-11-06 19:03:23 +03:00
|
|
|
while ((ch = getopt(argc, argv, "I:S:T:v")) != -1)
|
1996-10-04 04:18:55 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
2010-11-06 19:03:23 +03:00
|
|
|
case 'I':
|
|
|
|
N_NAME(&md_root_nlist[X_MD_ROOT_IMAGE]) = optarg;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
N_NAME(&md_root_nlist[X_MD_ROOT_SIZE]) = optarg;
|
|
|
|
break;
|
1997-09-30 10:20:16 +04:00
|
|
|
case 'T':
|
|
|
|
#ifdef NLIST_AOUT
|
|
|
|
T_flag_specified = 1;
|
|
|
|
text_start = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
#endif /* NLIST_AOUT */
|
|
|
|
/* FALLTHROUGH */
|
1996-10-04 04:18:55 +04:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
kfile = argv[0];
|
|
|
|
fsfile = argv[1];
|
|
|
|
|
|
|
|
if ((kfd = open(kfile, O_RDWR, 0)) == -1)
|
|
|
|
err(1, "open %s", kfile);
|
|
|
|
|
1997-01-02 03:33:34 +03:00
|
|
|
if ((rv = __fdnlist(kfd, md_root_nlist)) != 0)
|
1996-10-04 04:18:55 +04:00
|
|
|
errx(1, "could not find symbols in %s", kfile);
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "got symbols from %s\n", kfile);
|
|
|
|
|
|
|
|
if (fstat(kfd, &ksb) == -1)
|
|
|
|
err(1, "fstat %s", kfile);
|
2001-03-22 02:27:33 +03:00
|
|
|
if (ksb.st_size != (size_t)ksb.st_size)
|
1996-10-04 04:18:55 +04:00
|
|
|
errx(1, "%s too big to map", kfile);
|
|
|
|
|
|
|
|
if ((mappedkfile = mmap(NULL, ksb.st_size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_FILE | MAP_SHARED, kfd, 0)) == (caddr_t)-1)
|
|
|
|
err(1, "mmap %s", kfile);
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "mapped %s\n", kfile);
|
|
|
|
|
1997-01-02 03:33:34 +03:00
|
|
|
if (find_md_root(kfile, mappedkfile, ksb.st_size, md_root_nlist,
|
|
|
|
&md_root_offset, &md_root_size) != 0)
|
|
|
|
errx(1, "could not find md root buffer in %s", kfile);
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
if ((fsfd = open(fsfile, O_RDONLY, 0)) == -1)
|
|
|
|
err(1, "open %s", fsfile);
|
|
|
|
if (fstat(fsfd, &fssb) == -1)
|
|
|
|
err(1, "fstat %s", fsfile);
|
2001-03-22 02:27:33 +03:00
|
|
|
if (fssb.st_size != (size_t)fssb.st_size)
|
1996-10-04 04:18:55 +04:00
|
|
|
errx(1, "fs image is too big");
|
1997-01-02 03:33:34 +03:00
|
|
|
if (fssb.st_size > md_root_size)
|
2001-01-05 06:27:26 +03:00
|
|
|
errx(1, "fs image (%lld bytes) too big for buffer (%lu bytes)",
|
2001-03-22 02:46:48 +03:00
|
|
|
(long long)fssb.st_size, (unsigned long)md_root_size);
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "copying image from %s into %s\n", fsfile,
|
|
|
|
kfile);
|
1997-01-02 03:33:34 +03:00
|
|
|
if ((rv = read(fsfd, mappedkfile + md_root_offset,
|
1998-08-27 22:03:42 +04:00
|
|
|
fssb.st_size)) != fssb.st_size) {
|
1996-10-04 04:18:55 +04:00
|
|
|
if (rv == -1)
|
|
|
|
err(1, "read %s", fsfile);
|
|
|
|
else
|
|
|
|
errx(1, "unexpected EOF reading %s", fsfile);
|
1998-08-27 22:03:42 +04:00
|
|
|
}
|
1996-10-04 04:18:55 +04:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "done copying image\n");
|
|
|
|
|
|
|
|
close(fsfd);
|
|
|
|
|
|
|
|
munmap(mappedkfile, ksb.st_size);
|
|
|
|
close(kfd);
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "exiting\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
|
2001-03-22 02:46:48 +03:00
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s kernel_file fsimage_file\n",
|
|
|
|
getprogname());
|
1996-10-04 04:18:55 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
int (*check) __P((const char *, size_t));
|
|
|
|
int (*findoff) __P((const char *, size_t, u_long, size_t *));
|
|
|
|
} exec_formats[] = {
|
|
|
|
#ifdef NLIST_AOUT
|
|
|
|
{ "a.out", check_aout, findoff_aout, },
|
|
|
|
#endif
|
|
|
|
#ifdef NLIST_ECOFF
|
|
|
|
{ "ECOFF", check_ecoff, findoff_ecoff, },
|
|
|
|
#endif
|
|
|
|
#ifdef NLIST_ELF32
|
|
|
|
{ "ELF32", check_elf32, findoff_elf32, },
|
|
|
|
#endif
|
|
|
|
#ifdef NLIST_ELF64
|
|
|
|
{ "ELF64", check_elf64, findoff_elf64, },
|
|
|
|
#endif
|
1999-09-12 20:08:13 +04:00
|
|
|
#ifdef NLIST_COFF
|
|
|
|
{ "COFF", check_coff, findoff_coff, },
|
|
|
|
#endif
|
1996-10-04 04:18:55 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
1997-01-02 03:33:34 +03:00
|
|
|
find_md_root(fname, mappedfile, mappedsize, nl, rootoffp, rootsizep)
|
1996-10-04 04:18:55 +04:00
|
|
|
const char *fname, *mappedfile;
|
|
|
|
size_t mappedsize;
|
|
|
|
const struct nlist *nl;
|
|
|
|
size_t *rootoffp;
|
|
|
|
u_int32_t *rootsizep;
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
size_t rootsizeoff;
|
|
|
|
|
|
|
|
n = sizeof exec_formats / sizeof exec_formats[0];
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if ((*exec_formats[i].check)(mappedfile, mappedsize) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == n) {
|
|
|
|
warnx("%s: unknown executable format", fname);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1997-09-30 10:20:16 +04:00
|
|
|
if (verbose) {
|
1996-10-04 04:18:55 +04:00
|
|
|
fprintf(stderr, "%s is an %s binary\n", fname,
|
|
|
|
exec_formats[i].name);
|
1997-09-30 10:20:16 +04:00
|
|
|
#ifdef NLIST_AOUT
|
|
|
|
if (T_flag_specified)
|
|
|
|
fprintf(stderr, "kernel text loads at 0x%lx\n",
|
|
|
|
text_start);
|
|
|
|
#endif
|
|
|
|
}
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
|
1997-01-02 03:33:34 +03:00
|
|
|
nl[X_MD_ROOT_SIZE].n_value, &rootsizeoff) != 0) {
|
1996-10-04 04:18:55 +04:00
|
|
|
warnx("couldn't find offset for %s in %s",
|
1997-01-02 03:33:34 +03:00
|
|
|
nl[X_MD_ROOT_SIZE].n_name, fname);
|
1996-10-04 04:18:55 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "%s is at offset %#lx in %s\n",
|
1997-02-12 01:26:14 +03:00
|
|
|
nl[X_MD_ROOT_SIZE].n_name,
|
|
|
|
(unsigned long)rootsizeoff, fname);
|
2009-07-30 19:16:37 +04:00
|
|
|
*rootsizep = *(const u_int32_t *)&mappedfile[rootsizeoff];
|
1996-10-04 04:18:55 +04:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "%s has value %#x\n",
|
1997-01-02 03:33:34 +03:00
|
|
|
nl[X_MD_ROOT_SIZE].n_name, *rootsizep);
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
if ((*exec_formats[i].findoff)(mappedfile, mappedsize,
|
1997-01-02 03:33:34 +03:00
|
|
|
nl[X_MD_ROOT_IMAGE].n_value, rootoffp) != 0) {
|
1996-10-04 04:18:55 +04:00
|
|
|
warnx("couldn't find offset for %s in %s",
|
1997-01-02 03:33:34 +03:00
|
|
|
nl[X_MD_ROOT_IMAGE].n_name, fname);
|
1996-10-04 04:18:55 +04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "%s is at offset %#lx in %s\n",
|
1997-02-12 01:26:14 +03:00
|
|
|
nl[X_MD_ROOT_IMAGE].n_name,
|
|
|
|
(unsigned long)(*rootoffp), fname);
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|