2016-12-18 21:32:24 +03:00
|
|
|
/* $NetBSD: mdsetimage.c,v 1.24 2016/12/18 18:32:24 riastradh Exp $ */
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
/*
|
2016-09-21 19:25:41 +03:00
|
|
|
* Copyright (c) 1996, 2002 Christopher G. Demetriou
|
2000-06-14 21:24:02 +04:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
1997-10-17 14:28:39 +04:00
|
|
|
#include <sys/cdefs.h>
|
2016-09-21 19:25:41 +03:00
|
|
|
#if !defined(lint)
|
2008-07-21 17:36:57 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1996\
|
|
|
|
Christopher G. Demetriou. All rights reserved.");
|
2016-12-18 21:32:24 +03:00
|
|
|
__RCSID("$NetBSD: mdsetimage.c,v 1.24 2016/12/18 18:32:24 riastradh 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>
|
2016-12-18 21:32:24 +03:00
|
|
|
#include <stdint.h>
|
1996-10-04 04:18:55 +04:00
|
|
|
#include <stdio.h>
|
1997-10-17 14:28:39 +04:00
|
|
|
#include <stdlib.h>
|
1996-10-04 04:18:55 +04:00
|
|
|
#include <unistd.h>
|
2016-09-21 19:25:41 +03:00
|
|
|
#include <string.h>
|
1996-10-04 04:18:55 +04:00
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
#include "bin.h"
|
1996-10-04 04:18:55 +04:00
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
#define CHUNKSIZE (64 * 1024)
|
1996-10-04 04:18:55 +04:00
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
static void usage(void) __attribute__((noreturn));
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
int verbose;
|
2016-09-21 19:25:41 +03:00
|
|
|
int extract;
|
|
|
|
int setsize;
|
|
|
|
|
|
|
|
static const char *progname;
|
|
|
|
#undef setprogname
|
|
|
|
#define setprogname(x) (void)(progname = (x))
|
|
|
|
#undef getprogname
|
|
|
|
#define getprogname() (progname)
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
int
|
2016-09-21 19:25:41 +03:00
|
|
|
main(int argc, char *argv[])
|
1996-10-04 04:18:55 +04:00
|
|
|
{
|
2016-09-21 19:25:41 +03:00
|
|
|
int ch, kfd, fsfd, rv;
|
1996-10-04 04:18:55 +04:00
|
|
|
struct stat ksb, fssb;
|
2016-09-21 19:25:41 +03:00
|
|
|
size_t md_root_image_offset, md_root_size_offset;
|
|
|
|
u_int32_t md_root_size_value;
|
1996-10-04 04:18:55 +04:00
|
|
|
const char *kfile, *fsfile;
|
|
|
|
char *mappedkfile;
|
2016-09-21 19:25:41 +03:00
|
|
|
char *bfdname = NULL;
|
|
|
|
void *bin;
|
|
|
|
ssize_t left_to_copy;
|
|
|
|
const char *md_root_image = "_md_root_image";
|
|
|
|
const char *md_root_size = "_md_root_size";
|
|
|
|
unsigned long text_start = ~0;
|
1996-10-04 04:18:55 +04:00
|
|
|
|
2001-02-21 03:06:22 +03:00
|
|
|
setprogname(argv[0]);
|
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
while ((ch = getopt(argc, argv, "I:S:b:svx")) != -1)
|
1996-10-04 04:18:55 +04:00
|
|
|
switch (ch) {
|
2010-11-06 19:03:23 +03:00
|
|
|
case 'I':
|
2016-09-21 19:25:41 +03:00
|
|
|
md_root_image = optarg;
|
2010-11-06 19:03:23 +03:00
|
|
|
break;
|
|
|
|
case 'S':
|
2016-09-21 19:25:41 +03:00
|
|
|
md_root_size = optarg;
|
2010-11-06 19:03:23 +03:00
|
|
|
break;
|
1997-09-30 10:20:16 +04:00
|
|
|
case 'T':
|
|
|
|
text_start = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
2016-09-21 19:25:41 +03:00
|
|
|
case 'b':
|
|
|
|
bfdname = optarg;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
setsize = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
extract = 1;
|
|
|
|
break;
|
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];
|
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
if (extract) {
|
|
|
|
if ((kfd = open(kfile, O_RDONLY, 0)) == -1)
|
|
|
|
err(1, "open %s", kfile);
|
|
|
|
} else {
|
|
|
|
if ((kfd = open(kfile, O_RDWR, 0)) == -1)
|
|
|
|
err(1, "open %s", kfile);
|
|
|
|
}
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
if (fstat(kfd, &ksb) == -1)
|
|
|
|
err(1, "fstat %s", kfile);
|
2016-09-21 19:25:41 +03:00
|
|
|
if ((uintmax_t)ksb.st_size != (size_t)ksb.st_size)
|
1996-10-04 04:18:55 +04:00
|
|
|
errx(1, "%s too big to map", kfile);
|
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
if ((mappedkfile = mmap(NULL, ksb.st_size, PROT_READ,
|
|
|
|
MAP_FILE | MAP_PRIVATE, kfd, 0)) == (caddr_t)-1)
|
1996-10-04 04:18:55 +04:00
|
|
|
err(1, "mmap %s", kfile);
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "mapped %s\n", kfile);
|
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
bin = bin_open(kfd, kfile, bfdname);
|
|
|
|
|
|
|
|
if (bin_find_md_root(bin, mappedkfile, ksb.st_size, text_start,
|
|
|
|
md_root_image, md_root_size, &md_root_image_offset,
|
|
|
|
&md_root_size_offset, &md_root_size_value, verbose) != 0)
|
|
|
|
errx(1, "could not find symbols in %s", kfile);
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "got symbols from %s\n", kfile);
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "root @ %#zx/%u\n",
|
|
|
|
md_root_image_offset, md_root_size_value);
|
|
|
|
|
|
|
|
munmap(mappedkfile, ksb.st_size);
|
1996-10-04 04:18:55 +04:00
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
if (extract) {
|
|
|
|
if ((fsfd = open(fsfile, O_WRONLY|O_CREAT, 0777)) == -1)
|
|
|
|
err(1, "open %s", fsfile);
|
|
|
|
left_to_copy = md_root_size_value;
|
|
|
|
} else {
|
|
|
|
if ((fsfd = open(fsfile, O_RDONLY, 0)) == -1)
|
|
|
|
err(1, "open %s", fsfile);
|
|
|
|
if (fstat(fsfd, &fssb) == -1)
|
|
|
|
err(1, "fstat %s", fsfile);
|
|
|
|
if ((uintmax_t)fssb.st_size != (size_t)fssb.st_size)
|
|
|
|
errx(1, "fs image is too big");
|
|
|
|
if (fssb.st_size > md_root_size_value)
|
2016-09-22 11:43:26 +03:00
|
|
|
errx(1, "fs image (%jd bytes) too big for buffer"
|
|
|
|
" (%u bytes)", (intmax_t) fssb.st_size,
|
|
|
|
md_root_size_value);
|
2016-09-21 19:25:41 +03:00
|
|
|
left_to_copy = fssb.st_size;
|
|
|
|
}
|
1996-10-04 04:18:55 +04:00
|
|
|
|
|
|
|
if (verbose)
|
2016-09-21 19:25:41 +03:00
|
|
|
fprintf(stderr, "copying image %s %s %s (%zd bytes)\n", fsfile,
|
|
|
|
(extract ? "from" : "into"), kfile, left_to_copy);
|
|
|
|
|
|
|
|
if (lseek(kfd, md_root_image_offset, SEEK_SET) !=
|
|
|
|
(off_t)md_root_image_offset)
|
|
|
|
err(1, "seek %s", kfile);
|
|
|
|
while (left_to_copy > 0) {
|
|
|
|
char buf[CHUNKSIZE];
|
|
|
|
ssize_t todo;
|
|
|
|
int rfd;
|
|
|
|
int wfd;
|
|
|
|
const char *rfile;
|
|
|
|
const char *wfile;
|
|
|
|
if (extract) {
|
|
|
|
rfd = kfd;
|
|
|
|
rfile = kfile;
|
|
|
|
wfd = fsfd;
|
|
|
|
wfile = fsfile;
|
|
|
|
} else {
|
|
|
|
rfd = fsfd;
|
|
|
|
rfile = fsfile;
|
|
|
|
wfd = kfd;
|
|
|
|
wfile = kfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
todo = (left_to_copy > CHUNKSIZE) ? CHUNKSIZE : left_to_copy;
|
|
|
|
if ((rv = read(rfd, buf, todo)) != todo) {
|
|
|
|
if (rv == -1)
|
|
|
|
err(1, "read %s", rfile);
|
|
|
|
else
|
|
|
|
errx(1, "unexpected EOF reading %s", rfile);
|
|
|
|
}
|
|
|
|
if ((rv = write(wfd, buf, todo)) != todo) {
|
|
|
|
if (rv == -1)
|
|
|
|
err(1, "write %s", wfile);
|
|
|
|
else
|
|
|
|
errx(1, "short write writing %s", wfile);
|
|
|
|
}
|
|
|
|
left_to_copy -= todo;
|
1998-08-27 22:03:42 +04:00
|
|
|
}
|
1996-10-04 04:18:55 +04:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "done copying image\n");
|
2016-09-21 19:25:41 +03:00
|
|
|
if (setsize && !extract) {
|
|
|
|
char buf[sizeof(uint32_t)];
|
|
|
|
|
|
|
|
if (verbose)
|
2016-09-22 11:43:26 +03:00
|
|
|
fprintf(stderr, "setting md_root_size to %jd\n",
|
|
|
|
(intmax_t) fssb.st_size);
|
2016-09-21 19:25:41 +03:00
|
|
|
if (lseek(kfd, md_root_size_offset, SEEK_SET) !=
|
|
|
|
(off_t)md_root_size_offset)
|
|
|
|
err(1, "seek %s", kfile);
|
|
|
|
bin_put_32(bin, fssb.st_size, buf);
|
|
|
|
if (write(kfd, buf, sizeof(buf)) != sizeof(buf))
|
|
|
|
err(1, "write %s", kfile);
|
|
|
|
}
|
1996-10-04 04:18:55 +04:00
|
|
|
|
2016-09-21 19:25:41 +03:00
|
|
|
close(fsfd);
|
1996-10-04 04:18:55 +04:00
|
|
|
close(kfd);
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "exiting\n");
|
2016-09-21 19:25:41 +03:00
|
|
|
|
|
|
|
bin_close(bin);
|
|
|
|
return 0;
|
1996-10-04 04:18:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-09-21 19:25:41 +03:00
|
|
|
usage(void)
|
1996-10-04 04:18:55 +04:00
|
|
|
{
|
2016-09-21 19:25:41 +03:00
|
|
|
const char **list;
|
|
|
|
|
|
|
|
fprintf(stderr, "Usage: %s [-svx] [-b bfdname] [-I image_symbol] "
|
2016-09-21 19:29:48 +03:00
|
|
|
"[-S size_symbol] [-T address] kernel image\n", getprogname());
|
|
|
|
fprintf(stderr, "Supported targets:");
|
2016-09-21 19:25:41 +03:00
|
|
|
for (list = bin_supported_targets(); *list != NULL; list++)
|
|
|
|
fprintf(stderr, " %s", *list);
|
|
|
|
fprintf(stderr, "\n");
|
1996-10-04 04:18:55 +04:00
|
|
|
exit(1);
|
|
|
|
}
|