1996-01-17 05:30:44 +03:00
|
|
|
/* $NetBSD: modload.c,v 1.14 1996/01/17 02:30:44 jtc Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1993-06-07 23:51:59 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1993 Terrence R. Lambert.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Terrence R. Lambert.
|
|
|
|
* 4. The name Terrence R. Lambert may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``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 TERRENCE R. LAMBERT 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/lkm.h>
|
|
|
|
#include <sys/file.h>
|
1995-05-28 09:21:58 +04:00
|
|
|
#include <errno.h>
|
1994-12-18 03:15:01 +03:00
|
|
|
#include <a.out.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <unistd.h>
|
1994-04-01 03:26:15 +04:00
|
|
|
#include "pathnames.h"
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
#ifndef DFLT_ENTRY
|
|
|
|
#define DFLT_ENTRY "xxxinit"
|
1994-04-01 03:26:15 +04:00
|
|
|
#endif /* !DFLT_ENTRY */
|
|
|
|
|
|
|
|
#define min(a, b) ((a) < (b) ? (a) : (b))
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Expected linker options:
|
|
|
|
*
|
|
|
|
* -A executable to link against
|
|
|
|
* -e entry point
|
|
|
|
* -o output file
|
|
|
|
* -T address to link to in hex (assumes it's a page boundry)
|
|
|
|
* <target> object file
|
|
|
|
*/
|
|
|
|
#define LINKCMD "ld -A %s -e _%s -o %s -T %x %s"
|
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
int debug = 0;
|
|
|
|
int verbose = 0;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
int
|
|
|
|
linkcmd(kernel, entry, outfile, address, object)
|
|
|
|
char *kernel, *entry, *outfile;
|
|
|
|
u_int address; /* XXX */
|
|
|
|
char *object;
|
1993-06-07 23:51:59 +04:00
|
|
|
{
|
1994-04-01 03:26:15 +04:00
|
|
|
char cmdbuf[1024];
|
|
|
|
int error = 0;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
sprintf(cmdbuf, LINKCMD, kernel, entry, outfile, address, object);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
if (debug)
|
|
|
|
printf("%s\n", cmdbuf);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
switch (system(cmdbuf)) {
|
|
|
|
case 0: /* SUCCESS! */
|
1993-06-07 23:51:59 +04:00
|
|
|
break;
|
1994-04-01 03:26:15 +04:00
|
|
|
case 1: /* uninformitive error */
|
1993-06-07 23:51:59 +04:00
|
|
|
/*
|
1993-11-23 07:43:06 +03:00
|
|
|
* Someone needs to fix the return values from the NetBSD
|
1993-06-07 23:51:59 +04:00
|
|
|
* ld program -- it's totally uninformative.
|
|
|
|
*
|
|
|
|
* No such file (4 on SunOS)
|
|
|
|
* Can't write output (2 on SunOS)
|
|
|
|
* Undefined symbol (1 on SunOS)
|
|
|
|
* etc.
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
case 127: /* can't load shell */
|
1993-06-07 23:51:59 +04:00
|
|
|
case 32512:
|
|
|
|
default:
|
1994-04-01 03:26:15 +04:00
|
|
|
error = 1;
|
1993-06-07 23:51:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
return error;
|
1993-06-07 23:51:59 +04:00
|
|
|
}
|
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
void
|
1993-06-07 23:51:59 +04:00
|
|
|
usage()
|
|
|
|
{
|
1994-04-01 03:26:15 +04:00
|
|
|
|
|
|
|
fprintf(stderr, "usage:\n");
|
|
|
|
fprintf(stderr, "modload [-d] [-v] [-A <kernel>] [-e <entry]\n");
|
|
|
|
fprintf(stderr,
|
|
|
|
"[-p <postinstall>] [-o <output file>] <input file>\n");
|
|
|
|
exit(1);
|
1993-06-07 23:51:59 +04:00
|
|
|
}
|
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
int fileopen = 0;
|
1993-06-07 23:51:59 +04:00
|
|
|
#define DEV_OPEN 0x01
|
|
|
|
#define MOD_OPEN 0x02
|
|
|
|
#define PART_RESRV 0x04
|
1994-04-01 03:26:15 +04:00
|
|
|
int devfd, modfd;
|
|
|
|
struct lmc_resrv resrv;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
void
|
|
|
|
cleanup()
|
1993-06-07 23:51:59 +04:00
|
|
|
{
|
1994-04-01 03:26:15 +04:00
|
|
|
if (fileopen & PART_RESRV) {
|
|
|
|
/*
|
|
|
|
* Free up kernel memory
|
|
|
|
*/
|
|
|
|
if (ioctl(devfd, LMUNRESRV, 0) == -1)
|
|
|
|
warn("can't release slot 0x%08x memory", resrv.slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileopen & DEV_OPEN)
|
|
|
|
close(devfd);
|
|
|
|
|
|
|
|
if (fileopen & MOD_OPEN)
|
|
|
|
close(modfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
char *kname = _PATH_UNIX;
|
|
|
|
char *entry = DFLT_ENTRY;
|
|
|
|
char *post = NULL;
|
|
|
|
char *out = NULL;
|
|
|
|
char *modobj;
|
|
|
|
char modout[80], *p;
|
|
|
|
struct exec info_buf;
|
|
|
|
u_int modsize; /* XXX */
|
|
|
|
u_int modentry; /* XXX */
|
|
|
|
|
|
|
|
struct lmc_loadbuf ldbuf;
|
|
|
|
int sz, bytesleft;
|
|
|
|
char buf[MODIOBUF];
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "dvA:e:p:o:")) != EOF) {
|
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
debug = 1;
|
|
|
|
break; /* debug */
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break; /* verbose */
|
|
|
|
case 'A':
|
|
|
|
kname = optarg;
|
|
|
|
break; /* kernel */
|
|
|
|
case 'e':
|
|
|
|
entry = optarg;
|
|
|
|
break; /* entry point */
|
|
|
|
case 'p':
|
|
|
|
post = optarg;
|
|
|
|
break; /* postinstall */
|
|
|
|
case 'o':
|
|
|
|
out = optarg;
|
|
|
|
break; /* output file */
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
default:
|
|
|
|
printf("default!\n");
|
|
|
|
break;
|
1993-06-07 23:51:59 +04:00
|
|
|
}
|
|
|
|
}
|
1994-04-01 03:26:15 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
if (argc != 1)
|
1993-06-07 23:51:59 +04:00
|
|
|
usage();
|
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
modobj = argv[0];
|
|
|
|
|
|
|
|
atexit(cleanup);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the virtual device device driver for exclusive use (needed
|
|
|
|
* to write the new module to it as our means of getting it in the
|
|
|
|
* kernel).
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if ((devfd = open(_PATH_LKM, O_RDWR, 0)) == -1)
|
|
|
|
err(3, _PATH_LKM);
|
|
|
|
fileopen |= DEV_OPEN;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
strcpy(modout, modobj);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1996-01-17 05:30:44 +03:00
|
|
|
p = strrchr(modout, '.');
|
1994-04-01 03:26:15 +04:00
|
|
|
if (!p || strcmp(p, ".o"))
|
|
|
|
errx(2, "module object must end in .o");
|
|
|
|
if (out == NULL) {
|
1993-06-07 23:51:59 +04:00
|
|
|
out = modout;
|
1994-09-18 23:53:44 +04:00
|
|
|
*p = '\0';
|
1993-06-07 23:51:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prelink to get file size
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if (linkcmd(kname, entry, out, 0, modobj))
|
|
|
|
errx(1, "can't prelink `%s' creating `%s'", modobj, out);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pre-open the 0-linked module to get the size information
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if ((modfd = open(out, O_RDONLY, 0)) == -1)
|
|
|
|
err(4, out);
|
|
|
|
fileopen |= MOD_OPEN;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the load module post load size... do this by reading the
|
|
|
|
* header and doing page counts.
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if (read(modfd, &info_buf, sizeof(struct exec)) == -1)
|
|
|
|
err(3, "read `%s'", out);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Close the dummy module -- we have our sizing information.
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
close(modfd);
|
|
|
|
fileopen &= ~MOD_OPEN;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Magic number...
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if (N_BADMAG(info_buf))
|
|
|
|
errx(4, "not an a.out format file");
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the size of the module
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
modsize = info_buf.a_text + info_buf.a_data + info_buf.a_bss;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reserve the required amount of kernel memory -- this may fail
|
|
|
|
* to be successful.
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
resrv.size = modsize; /* size in bytes */
|
|
|
|
resrv.name = modout; /* objname w/o ".o" */
|
|
|
|
resrv.slot = -1; /* returned */
|
|
|
|
resrv.addr = 0; /* returned */
|
|
|
|
if (ioctl(devfd, LMRESERV, &resrv) == -1)
|
|
|
|
err(9, "can't reserve memory");
|
|
|
|
fileopen |= PART_RESRV;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Relink at kernel load address
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if (linkcmd(kname, entry, out, resrv.addr, modobj))
|
|
|
|
errx(1, "can't link `%s' creating `%s' bound to 0x%08x",
|
|
|
|
modobj, out, resrv.addr);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the relinked module to load it...
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if ((modfd = open(out, O_RDONLY, 0)) == -1)
|
|
|
|
err(4, out);
|
|
|
|
fileopen |= MOD_OPEN;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reread the header to get the actual entry point *after* the
|
|
|
|
* relink.
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if (read(modfd, &info_buf, sizeof(struct exec)) == -1)
|
|
|
|
err(3, "read `%s'", out);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the entry point (for initialization)
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
modentry = info_buf.a_entry; /* place to call */
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Seek to the text offset to start loading...
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if (lseek(modfd, N_TXTOFF(info_buf), 0) == -1)
|
|
|
|
err(12, "lseek");
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Transfer the relinked module to kernel memory in chunks of
|
|
|
|
* MODIOBUF size at a time.
|
|
|
|
*/
|
1994-04-01 00:29:58 +04:00
|
|
|
for (bytesleft = info_buf.a_text + info_buf.a_data;
|
1994-04-01 03:26:15 +04:00
|
|
|
bytesleft > 0;
|
|
|
|
bytesleft -= sz) {
|
|
|
|
sz = min(bytesleft, MODIOBUF);
|
1994-04-01 00:29:58 +04:00
|
|
|
read(modfd, buf, sz);
|
1993-06-07 23:51:59 +04:00
|
|
|
ldbuf.cnt = sz;
|
|
|
|
ldbuf.data = buf;
|
1994-04-01 03:26:15 +04:00
|
|
|
if (ioctl(devfd, LMLOADBUF, &ldbuf) == -1)
|
|
|
|
err(11, "error transferring buffer");
|
1993-06-07 23:51:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save ourselves before disaster (potentitally) strikes...
|
|
|
|
*/
|
|
|
|
sync();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trigger the module as loaded by calling the entry procedure;
|
|
|
|
* this will do all necessary table fixup to ensure that state
|
|
|
|
* is maintained on success, or blow everything back to ground
|
|
|
|
* zero on failure.
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
if (ioctl(devfd, LMREADY, &modentry) == -1)
|
|
|
|
err(14, "error initializing module");
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Success!
|
|
|
|
*/
|
1994-04-01 03:26:15 +04:00
|
|
|
fileopen &= ~PART_RESRV; /* loaded */
|
|
|
|
printf("Module loaded as ID %d\n", resrv.slot);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1994-09-18 23:53:44 +04:00
|
|
|
/*
|
|
|
|
* Execute the post-install program, if specified.
|
|
|
|
*/
|
|
|
|
if (post) {
|
|
|
|
struct lmc_stat sbuf;
|
|
|
|
char id[16], type[16], offset[16];
|
|
|
|
|
|
|
|
sbuf.id = resrv.slot;
|
|
|
|
if (ioctl(devfd, LMSTAT, &sbuf) == -1)
|
|
|
|
err(15, "error fetching module stats for post-install");
|
|
|
|
sprintf(id, "%d", sbuf.id);
|
|
|
|
sprintf(type, "0x%x", sbuf.type);
|
|
|
|
sprintf(offset, "%d", sbuf.offset);
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* The modload docs say that drivers can install bdevsw &
|
|
|
|
* cdevsw, but the interface only supports one at a time.
|
|
|
|
*/
|
|
|
|
execl(post, post, id, type, offset, 0);
|
|
|
|
err(16, "can't exec `%s'", post);
|
|
|
|
}
|
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
return 0;
|
1993-06-07 23:51:59 +04:00
|
|
|
}
|