2002-10-10 05:57:10 +04:00
|
|
|
/* $NetBSD: modload.c,v 1.35 2002/10/10 01:57:10 simonb 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.
|
|
|
|
*/
|
|
|
|
|
1997-09-15 07:55:25 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2002-10-10 05:57:10 +04:00
|
|
|
__RCSID("$NetBSD: modload.c,v 1.35 2002/10/10 01:57:10 simonb Exp $");
|
1997-09-15 07:55:25 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1993-06-07 23:51:59 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/lkm.h>
|
1999-01-14 02:07:30 +03:00
|
|
|
#include <sys/stat.h>
|
1993-06-07 23:51:59 +04:00
|
|
|
#include <sys/file.h>
|
1997-09-15 07:55:25 +04:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1994-12-18 03:15:01 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1997-09-15 13:27:52 +04:00
|
|
|
#include <nlist.h>
|
1994-04-01 03:26:15 +04:00
|
|
|
#include "pathnames.h"
|
1993-06-07 23:51:59 +04:00
|
|
|
|
2002-10-06 17:23:00 +04:00
|
|
|
#define TRUE 1
|
|
|
|
#define FALSE 0
|
1999-01-14 02:07:30 +03:00
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
#include "modload.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 */
|
1997-09-15 13:27:52 +04:00
|
|
|
#ifndef DFLT_ENTRYEXT
|
|
|
|
#define DFLT_ENTRYEXT "_lkmentry"
|
|
|
|
#endif /* !DFLT_ENTRYEXT */
|
1994-04-01 03:26:15 +04:00
|
|
|
|
|
|
|
int debug = 0;
|
|
|
|
int verbose = 0;
|
1999-06-13 16:54:40 +04:00
|
|
|
char *out = NULL;
|
1999-04-30 22:32:01 +04:00
|
|
|
int symtab = 0;
|
1999-06-13 16:54:40 +04:00
|
|
|
int Sflag;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
2002-10-06 17:23:00 +04:00
|
|
|
static void cleanup(void);
|
1999-06-13 16:54:40 +04:00
|
|
|
|
|
|
|
/* prelink the module */
|
|
|
|
static int
|
2002-10-06 17:23:00 +04:00
|
|
|
prelink(const char *kernel,
|
|
|
|
const char *entry,
|
|
|
|
const char *outfile,
|
|
|
|
const void *address,
|
2002-10-10 05:57:10 +04:00
|
|
|
const char *object,
|
|
|
|
const char *ldscript)
|
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
|
|
|
|
2002-10-10 05:57:10 +04:00
|
|
|
linkcmd(cmdbuf, sizeof(cmdbuf), kernel, entry, outfile, address,
|
|
|
|
object, ldscript);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
if (debug)
|
1999-06-13 16:54:40 +04:00
|
|
|
fprintf(stderr, "%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
|
|
|
}
|
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
static void
|
|
|
|
usage(void)
|
1993-06-07 23:51:59 +04:00
|
|
|
{
|
1994-04-01 03:26:15 +04:00
|
|
|
|
|
|
|
fprintf(stderr, "usage:\n");
|
2001-09-17 05:40:08 +04:00
|
|
|
fprintf(stderr, "modload [-d] [-v] [-n] [-s] [-S] "
|
|
|
|
"[-A <kernel>] [-e <entry>]\n");
|
1994-04-01 03:26:15 +04:00
|
|
|
fprintf(stderr,
|
1999-06-13 16:54:40 +04:00
|
|
|
" [-p <postinstall>] [-o <output file>] <input file>\n");
|
1994-04-01 03:26:15 +04:00
|
|
|
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
|
1999-06-13 16:54:40 +04:00
|
|
|
#define OUTFILE_CREAT 0x08
|
|
|
|
|
1994-04-01 03:26:15 +04:00
|
|
|
int devfd, modfd;
|
|
|
|
struct lmc_resrv resrv;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
static void
|
|
|
|
cleanup(void)
|
1993-06-07 23:51:59 +04:00
|
|
|
{
|
1999-06-13 16:54:40 +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);
|
1999-06-13 16:54:40 +04:00
|
|
|
|
|
|
|
if (fileopen & OUTFILE_CREAT)
|
|
|
|
unlink(out);
|
1994-04-01 03:26:15 +04:00
|
|
|
}
|
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
static int
|
2001-09-30 01:15:11 +04:00
|
|
|
verify_entry(const char *entry, char *filename)
|
1997-09-15 13:27:52 +04:00
|
|
|
{
|
|
|
|
struct nlist names[2];
|
|
|
|
int n;
|
1998-07-26 21:53:10 +04:00
|
|
|
char *s;
|
1997-09-15 13:27:52 +04:00
|
|
|
|
1998-07-26 21:53:10 +04:00
|
|
|
memset(names, 0, sizeof(names));
|
|
|
|
s = malloc(strlen(entry) + 2);
|
2001-09-30 01:15:11 +04:00
|
|
|
sprintf(s, "_%s", entry); /* safe */
|
1997-09-15 13:27:52 +04:00
|
|
|
#ifdef _AOUT_INCLUDE_
|
1998-07-26 21:53:10 +04:00
|
|
|
names[0].n_un.n_name = s;
|
1997-09-15 13:27:52 +04:00
|
|
|
#else
|
1998-07-26 21:53:10 +04:00
|
|
|
names[0].n_name = s;
|
1997-09-15 13:27:52 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
n = nlist(filename, names);
|
|
|
|
if (n == -1)
|
|
|
|
err(1, "nlist %s", filename);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2002-10-06 17:23:00 +04:00
|
|
|
/*
|
|
|
|
* Transfer data to kernel memory in chunks
|
1999-06-13 16:54:40 +04:00
|
|
|
* of MODIOBUF size at a time.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
loadbuf(void *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct lmc_loadbuf ldbuf;
|
|
|
|
size_t n;
|
|
|
|
char *p = buf;
|
2002-10-06 17:23:00 +04:00
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
while(len) {
|
|
|
|
n = MIN(len, MODIOBUF);
|
|
|
|
ldbuf.cnt = n;
|
|
|
|
ldbuf.data = p;
|
2001-11-08 18:33:15 +03:00
|
|
|
if (ioctl(devfd, LMLOADBUF, &ldbuf) == -1)
|
1999-06-13 16:54:40 +04:00
|
|
|
err(11, "error loading buffer");
|
|
|
|
len -= n;
|
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer some empty space. */
|
|
|
|
void
|
|
|
|
loadspace(size_t len)
|
|
|
|
{
|
|
|
|
char buf[MODIOBUF];
|
|
|
|
size_t n;
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
while(len) {
|
|
|
|
n = MIN(len, sizeof(buf));
|
|
|
|
loadbuf(buf, n);
|
|
|
|
len -= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-06 17:23:00 +04:00
|
|
|
/*
|
|
|
|
* Transfer symbol table to kernel memory in chunks
|
2001-11-08 18:33:15 +03:00
|
|
|
* of MODIOBUF size at a time.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
loadsym(void *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct lmc_loadbuf ldbuf;
|
|
|
|
size_t n;
|
|
|
|
char *p = buf;
|
2002-10-06 17:23:00 +04:00
|
|
|
|
2001-11-08 18:33:15 +03:00
|
|
|
while(len) {
|
|
|
|
n = MIN(len, MODIOBUF);
|
|
|
|
ldbuf.cnt = n;
|
|
|
|
ldbuf.data = p;
|
|
|
|
if(ioctl(devfd, LMLOADSYMS, &ldbuf) == -1)
|
|
|
|
err(11, "error loading buffer");
|
|
|
|
len -= n;
|
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer some empty space. */
|
1994-04-01 03:26:15 +04:00
|
|
|
int
|
1999-06-13 16:54:40 +04:00
|
|
|
main(int argc, char **argv)
|
1994-04-01 03:26:15 +04:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
char *kname = _PATH_UNIX;
|
|
|
|
char *entry = DFLT_ENTRY;
|
|
|
|
char *post = NULL;
|
2002-10-10 05:57:10 +04:00
|
|
|
char *ldscript = NULL;
|
1994-04-01 03:26:15 +04:00
|
|
|
char *modobj;
|
|
|
|
char modout[80], *p;
|
1999-01-14 02:07:30 +03:00
|
|
|
struct stat stb;
|
1999-06-13 16:54:40 +04:00
|
|
|
int strtablen;
|
|
|
|
size_t modsize; /* XXX */
|
2002-10-07 06:33:55 +04:00
|
|
|
void *modentry; /* XXX */
|
1999-06-13 16:54:40 +04:00
|
|
|
int noready = 0, old = 0;
|
1994-04-01 03:26:15 +04:00
|
|
|
|
2002-10-10 05:57:10 +04:00
|
|
|
while ((c = getopt(argc, argv, "dnvse:p:o:A:ST:")) != -1) {
|
1994-04-01 03:26:15 +04:00
|
|
|
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 */
|
2002-10-10 05:57:10 +04:00
|
|
|
case 'T':
|
|
|
|
ldscript = optarg;
|
|
|
|
break; /* linker script */
|
1999-06-13 16:54:40 +04:00
|
|
|
case 'n':
|
|
|
|
noready = 1;
|
|
|
|
break;
|
1999-01-14 02:07:30 +03:00
|
|
|
case 's':
|
1999-04-30 22:32:01 +04:00
|
|
|
symtab = 1;
|
1999-01-14 02:07:30 +03:00
|
|
|
break;
|
1999-06-13 16:54:40 +04:00
|
|
|
case 'S':
|
|
|
|
Sflag = 1;
|
|
|
|
break;
|
1994-04-01 03:26:15 +04:00
|
|
|
case '?':
|
|
|
|
usage();
|
1999-06-13 16:54:40 +04:00
|
|
|
/* NOTREACHED */
|
1994-04-01 03:26:15 +04:00
|
|
|
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
|
|
|
|
2002-10-10 05:57:10 +04:00
|
|
|
if (ldscript == NULL && access(_PATH_LDSCRIPT, R_OK) == 0)
|
|
|
|
ldscript = _PATH_LDSCRIPT;
|
|
|
|
|
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
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
strncpy(modout, modobj, sizeof(modout) - 1);
|
|
|
|
modout[sizeof(modout) - 1] = '\0';
|
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");
|
1999-04-23 21:47:02 +04:00
|
|
|
*p = '\0';
|
|
|
|
if (out == NULL)
|
1993-06-07 23:51:59 +04:00
|
|
|
out = modout;
|
|
|
|
|
1997-09-15 13:27:52 +04:00
|
|
|
/*
|
|
|
|
* Verify that the entry point for the module exists.
|
|
|
|
*/
|
|
|
|
if (verify_entry(entry, modobj)) {
|
|
|
|
/*
|
|
|
|
* Try <modobj>_init if entry is DFLT_ENTRY.
|
|
|
|
*/
|
2001-09-30 01:15:11 +04:00
|
|
|
if (strcmp(entry, DFLT_ENTRY) == 0) {
|
1997-09-15 13:27:52 +04:00
|
|
|
if ((p = strrchr(modout, '/')))
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
p = modout;
|
2001-09-30 01:15:11 +04:00
|
|
|
entry = malloc(strlen(p) + strlen(DFLT_ENTRYEXT) + 1);
|
|
|
|
sprintf(entry, "%s%s", p, DFLT_ENTRYEXT); /* safe */
|
1997-09-15 13:27:52 +04:00
|
|
|
if (verify_entry(entry, modobj))
|
|
|
|
errx(1, "entry point _%s not found in %s",
|
|
|
|
entry, modobj);
|
|
|
|
} else
|
|
|
|
errx(1, "entry point _%s not found in %s", entry,
|
|
|
|
modobj);
|
|
|
|
}
|
|
|
|
|
1993-06-07 23:51:59 +04:00
|
|
|
/*
|
|
|
|
* Prelink to get file size
|
|
|
|
*/
|
2002-10-10 05:57:10 +04:00
|
|
|
if (prelink(kname, entry, out, 0, modobj, ldscript))
|
1994-04-01 03:26:15 +04:00
|
|
|
errx(1, "can't prelink `%s' creating `%s'", modobj, out);
|
1999-06-13 16:54:40 +04:00
|
|
|
if (Sflag == 0)
|
|
|
|
fileopen |= OUTFILE_CREAT;
|
2002-10-06 17:23:00 +04:00
|
|
|
|
1999-06-13 16:54:40 +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)
|
1999-06-13 16:54:40 +04:00
|
|
|
err(4, "%s", out);
|
1994-04-01 03:26:15 +04:00
|
|
|
fileopen |= MOD_OPEN;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1999-01-14 02:07:30 +03:00
|
|
|
/*
|
|
|
|
* stat for filesize to figure out string table size
|
|
|
|
*/
|
|
|
|
if (fstat(modfd, &stb) == -1)
|
|
|
|
err(3, "fstat `%s'", out);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
1999-06-13 16:54:40 +04:00
|
|
|
* work out various sizes and fill in resrv bits
|
1993-06-07 23:51:59 +04:00
|
|
|
*/
|
1999-06-13 16:54:40 +04:00
|
|
|
if (mod_sizes(modfd, &modsize, &strtablen, &resrv, &stb) != 0)
|
|
|
|
err(1, "can't get module sizes");
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
1999-06-13 16:54:40 +04:00
|
|
|
* Close the dummy module -- we have our sizing information.
|
1993-06-07 23:51:59 +04:00
|
|
|
*/
|
1999-06-13 16:54:40 +04:00
|
|
|
close(modfd);
|
|
|
|
fileopen &= ~MOD_OPEN;
|
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 */
|
1999-01-14 02:07:30 +03:00
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
if (verbose)
|
|
|
|
warnx("reserving %lu bytes of memory", (unsigned long)modsize);
|
1999-01-14 02:07:30 +03:00
|
|
|
if (ioctl(devfd, LMRESERV, &resrv) == -1) {
|
|
|
|
if (symtab)
|
|
|
|
warn("not loading symbols: kernel does not support symbol table loading");
|
|
|
|
doold:
|
|
|
|
symtab = 0;
|
|
|
|
if (ioctl(devfd, LMRESERV_O, &resrv) == -1)
|
1994-04-01 03:26:15 +04:00
|
|
|
err(9, "can't reserve memory");
|
1999-01-14 02:07:30 +03:00
|
|
|
old = TRUE;
|
|
|
|
}
|
1994-04-01 03:26:15 +04:00
|
|
|
fileopen |= PART_RESRV;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Relink at kernel load address
|
|
|
|
*/
|
2002-10-10 05:57:10 +04:00
|
|
|
if (prelink(kname, entry, out, (void *)resrv.addr, modobj, ldscript))
|
1999-06-13 16:54:40 +04:00
|
|
|
errx(1, "can't link `%s' creating `%s' bound to %p",
|
2002-10-07 06:33:55 +04:00
|
|
|
modobj, out, (void *)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)
|
1999-06-13 16:54:40 +04:00
|
|
|
err(4, "%s", out);
|
1994-04-01 03:26:15 +04:00
|
|
|
fileopen |= MOD_OPEN;
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
modentry = mod_load(modfd);
|
|
|
|
if (debug)
|
|
|
|
fprintf(stderr, "modentry = %p\n", modentry);
|
1993-06-07 23:51:59 +04:00
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
if (symtab)
|
|
|
|
mod_symload(strtablen);
|
1999-01-14 02:07:30 +03:00
|
|
|
|
1993-06-07 23:51:59 +04:00
|
|
|
/*
|
|
|
|
* Save ourselves before disaster (potentitally) strikes...
|
|
|
|
*/
|
|
|
|
sync();
|
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
if (noready)
|
|
|
|
return 0;
|
|
|
|
|
1993-06-07 23:51:59 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
1999-01-14 02:07:30 +03:00
|
|
|
if (ioctl(devfd, LMREADY, &modentry) == -1) {
|
|
|
|
if (errno == EINVAL && !old) {
|
|
|
|
if (fileopen & MOD_OPEN)
|
|
|
|
close(modfd);
|
|
|
|
/* PART_RESRV is not true since the kernel cleans up
|
|
|
|
after a failed LMREADY */
|
|
|
|
fileopen &= ~(MOD_OPEN|PART_RESRV);
|
|
|
|
/* try using oldstyle */
|
|
|
|
warn("module failed to load using new version; trying old version");
|
|
|
|
goto doold;
|
|
|
|
} else
|
|
|
|
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;
|
2002-09-13 19:30:36 +04:00
|
|
|
char id[16], type[16];
|
1994-09-18 23:53:44 +04:00
|
|
|
|
|
|
|
sbuf.id = resrv.slot;
|
|
|
|
if (ioctl(devfd, LMSTAT, &sbuf) == -1)
|
|
|
|
err(15, "error fetching module stats for post-install");
|
1998-07-28 23:22:54 +04:00
|
|
|
(void)snprintf(id, sizeof(id), "%d", sbuf.id);
|
|
|
|
(void)snprintf(type, sizeof(type), "0x%x", sbuf.type);
|
2002-09-13 19:30:36 +04:00
|
|
|
if (sbuf.type == LM_DEV) {
|
|
|
|
char arg3[16], arg4[16];
|
2002-09-19 02:59:36 +04:00
|
|
|
int bmajor = LKM_BLOCK_MAJOR(sbuf.offset);
|
|
|
|
int cmajor = LKM_CHAR_MAJOR(sbuf.offset);
|
2002-09-13 19:30:36 +04:00
|
|
|
(void)snprintf(arg3, sizeof(arg3), "%d", cmajor);
|
|
|
|
(void)snprintf(arg4, sizeof(arg4), "%d", bmajor);
|
|
|
|
execl(post, post, id, type, arg3, arg4, 0);
|
|
|
|
} else {
|
|
|
|
char arg3[16];
|
|
|
|
(void)snprintf(arg3, sizeof(arg3), "%ld",
|
|
|
|
(long)sbuf.offset);
|
|
|
|
execl(post, post, id, type, arg3, 0);
|
|
|
|
}
|
1994-09-18 23:53:44 +04:00
|
|
|
err(16, "can't exec `%s'", post);
|
|
|
|
}
|
|
|
|
|
1999-06-13 16:54:40 +04:00
|
|
|
exit (0);
|
1993-06-07 23:51:59 +04:00
|
|
|
}
|