summaryrefslogtreecommitdiff
path: root/sys/lib/libsa
diff options
context:
space:
mode:
authorpk <pk@NetBSD.org>2003-02-24 10:51:05 +0000
committerpk <pk@NetBSD.org>2003-02-24 10:51:05 +0000
commitd71686ab26fbb2b8331ee60e7b5a30d1ecd98e5a (patch)
tree6c2d1ef2ee47422f35e7ec8fa92f202b9264f823 /sys/lib/libsa
parentd82babe73c45226262f6d9d337990c8f92ff6717 (diff)
Introduce fdloadfile() to load an image from an open file descriptor;
implement loadfile() in terms of it. This allows clients to open a file once and "load" it multiple times (e.g. first with COUNT_KERNEL, then with LOAD_KERNEL) without the side-effects of multiple open calls.
Diffstat (limited to 'sys/lib/libsa')
-rw-r--r--sys/lib/libsa/loadfile.c47
-rw-r--r--sys/lib/libsa/loadfile.h3
2 files changed, 37 insertions, 13 deletions
diff --git a/sys/lib/libsa/loadfile.c b/sys/lib/libsa/loadfile.c
index 334b9ac17d3..1e547dcd6e6 100644
--- a/sys/lib/libsa/loadfile.c
+++ b/sys/lib/libsa/loadfile.c
@@ -1,4 +1,4 @@
-/* $NetBSD: loadfile.c,v 1.21 2002/12/11 09:55:20 pk Exp $ */
+/* $NetBSD: loadfile.c,v 1.22 2003/02/24 10:51:05 pk Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -104,6 +104,35 @@ loadfile(fname, marks, flags)
u_long *marks;
int flags;
{
+ int fd, error;
+
+ /* Open the file. */
+ if ((fd = open(fname, 0)) < 0) {
+ WARN(("open %s", fname ? fname : "<default>"));
+ return (-1);
+ }
+
+ /* Load it; save the value of errno across the close() call */
+ if ((error = fdloadfile(fd, marks, flags)) != 0) {
+ (void)close(fd);
+ errno = error;
+ return (-1);
+ }
+
+ return (fd);
+}
+
+/*
+ * Read in program from the given file descriptor.
+ * Return error code (0 on success).
+ * Fill in marks.
+ */
+int
+fdloadfile(fd, marks, flags)
+ int fd;
+ u_long *marks;
+ int flags;
+{
union {
#ifdef BOOT_ECOFF
struct ecoff_exechdr coff;
@@ -119,15 +148,11 @@ loadfile(fname, marks, flags)
#endif
} hdr;
ssize_t nr;
- int fd, rval;
-
- /* Open the file. */
- if ((fd = open(fname, 0)) < 0) {
- WARN(("open %s", fname ? fname : "<default>"));
- return -1;
- }
+ int rval;
/* Read the exec header. */
+ if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
+ goto err;
if ((nr = read(fd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
WARN(("read header"));
goto err;
@@ -162,16 +187,14 @@ loadfile(fname, marks, flags)
{
rval = 1;
errno = EFTYPE;
- WARN(("%s", fname ? fname : "<default>"));
}
if (rval == 0) {
if ((flags & LOAD_ALL) != 0)
PROGRESS(("=0x%lx\n",
marks[MARK_END] - marks[MARK_START]));
- return fd;
+ return (0);
}
err:
- (void)close(fd);
- return -1;
+ return (errno);
}
diff --git a/sys/lib/libsa/loadfile.h b/sys/lib/libsa/loadfile.h
index f9824de817b..b7ddb288962 100644
--- a/sys/lib/libsa/loadfile.h
+++ b/sys/lib/libsa/loadfile.h
@@ -1,4 +1,4 @@
-/* $NetBSD: loadfile.h,v 1.3 2001/10/31 17:20:50 thorpej Exp $ */
+/* $NetBSD: loadfile.h,v 1.4 2003/02/24 10:51:05 pk Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -66,6 +66,7 @@
#define COUNT_ALL 0x3f00
int loadfile(const char *, u_long *, int);
+int fdloadfile(int fd, u_long *, int);
#include "machine/loadfile_machdep.h"