loadfile_ecoff.c revision 1.8.14.1 1 1.8.14.1 mjf /* $NetBSD: loadfile_ecoff.c,v 1.8.14.1 2007/12/08 18:20:53 mjf Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center and by Christos Zoulas.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.1 thorpej * must display the following acknowledgement:
21 1.1 thorpej * This product includes software developed by the NetBSD
22 1.1 thorpej * Foundation, Inc. and its contributors.
23 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 thorpej * contributors may be used to endorse or promote products derived
25 1.1 thorpej * from this software without specific prior written permission.
26 1.1 thorpej *
27 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.1 thorpej */
39 1.1 thorpej
40 1.2 thorpej #ifdef _STANDALONE
41 1.2 thorpej #include <lib/libsa/stand.h>
42 1.2 thorpej #include <lib/libkern/libkern.h>
43 1.5 perry #else
44 1.2 thorpej #include <stdio.h>
45 1.2 thorpej #include <string.h>
46 1.2 thorpej #include <errno.h>
47 1.2 thorpej #include <stdlib.h>
48 1.5 perry #include <unistd.h>
49 1.2 thorpej #include <fcntl.h>
50 1.2 thorpej #include <err.h>
51 1.5 perry #endif
52 1.2 thorpej
53 1.5 perry #include <sys/param.h>
54 1.2 thorpej #include <sys/exec.h>
55 1.2 thorpej
56 1.2 thorpej #include "loadfile.h"
57 1.2 thorpej
58 1.2 thorpej #ifdef BOOT_ECOFF
59 1.2 thorpej
60 1.2 thorpej int
61 1.2 thorpej loadfile_coff(fd, coff, marks, flags)
62 1.1 thorpej int fd;
63 1.1 thorpej struct ecoff_exechdr *coff;
64 1.1 thorpej u_long *marks;
65 1.1 thorpej int flags;
66 1.1 thorpej {
67 1.1 thorpej paddr_t offset = marks[MARK_START];
68 1.1 thorpej paddr_t minp = ~0, maxp = 0, pos;
69 1.8.14.1 mjf ssize_t nr;
70 1.4 reinoud
71 1.4 reinoud /* some ports dont use the offset */
72 1.4 reinoud offset = offset;
73 1.1 thorpej
74 1.1 thorpej /* Read in text. */
75 1.1 thorpej if (lseek(fd, ECOFF_TXTOFF(coff), SEEK_SET) == -1) {
76 1.1 thorpej WARN(("lseek text"));
77 1.1 thorpej return 1;
78 1.1 thorpej }
79 1.1 thorpej
80 1.1 thorpej if (coff->a.tsize != 0) {
81 1.1 thorpej if (flags & LOAD_TEXT) {
82 1.1 thorpej PROGRESS(("%lu", coff->a.tsize));
83 1.8.14.1 mjf nr = READ(fd, coff->a.text_start, coff->a.tsize);
84 1.8.14.1 mjf if (nr == -1) {
85 1.8.14.1 mjf return 1;
86 1.8.14.1 mjf }
87 1.8.14.1 mjf if (nr != coff->a.tsize) {
88 1.8.14.1 mjf errno = EIO;
89 1.1 thorpej return 1;
90 1.1 thorpej }
91 1.1 thorpej }
92 1.1 thorpej else {
93 1.1 thorpej if (lseek(fd, coff->a.tsize, SEEK_CUR) == -1) {
94 1.1 thorpej WARN(("read text"));
95 1.1 thorpej return 1;
96 1.1 thorpej }
97 1.1 thorpej }
98 1.1 thorpej if (flags & (COUNT_TEXT|LOAD_TEXT)) {
99 1.1 thorpej pos = coff->a.text_start;
100 1.1 thorpej if (minp > pos)
101 1.1 thorpej minp = pos;
102 1.1 thorpej pos += coff->a.tsize;
103 1.1 thorpej if (maxp < pos)
104 1.1 thorpej maxp = pos;
105 1.1 thorpej }
106 1.1 thorpej }
107 1.1 thorpej
108 1.1 thorpej /* Read in data. */
109 1.1 thorpej if (coff->a.dsize != 0) {
110 1.1 thorpej if (flags & LOAD_DATA) {
111 1.1 thorpej PROGRESS(("+%lu", coff->a.dsize));
112 1.8.14.1 mjf nr = READ(fd, coff->a.data_start, coff->a.dsize);
113 1.8.14.1 mjf if (nr == -1) {
114 1.8.14.1 mjf WARN(("read data"));
115 1.8.14.1 mjf return 1;
116 1.8.14.1 mjf }
117 1.8.14.1 mjf if (nr != coff->a.dsize) {
118 1.8.14.1 mjf errno = EIO;
119 1.1 thorpej WARN(("read data"));
120 1.1 thorpej return 1;
121 1.1 thorpej }
122 1.1 thorpej }
123 1.1 thorpej if (flags & (COUNT_DATA|LOAD_DATA)) {
124 1.1 thorpej pos = coff->a.data_start;
125 1.1 thorpej if (minp > pos)
126 1.1 thorpej minp = pos;
127 1.1 thorpej pos += coff->a.dsize;
128 1.1 thorpej if (maxp < pos)
129 1.1 thorpej maxp = pos;
130 1.1 thorpej }
131 1.1 thorpej }
132 1.1 thorpej
133 1.1 thorpej /* Zero out bss. */
134 1.1 thorpej if (coff->a.bsize != 0) {
135 1.1 thorpej if (flags & LOAD_BSS) {
136 1.1 thorpej PROGRESS(("+%lu", coff->a.bsize));
137 1.1 thorpej BZERO(coff->a.bss_start, coff->a.bsize);
138 1.1 thorpej }
139 1.1 thorpej if (flags & (COUNT_BSS|LOAD_BSS)) {
140 1.1 thorpej pos = coff->a.bss_start;
141 1.1 thorpej if (minp > pos)
142 1.1 thorpej minp = pos;
143 1.1 thorpej pos = coff->a.bsize;
144 1.1 thorpej if (maxp < pos)
145 1.1 thorpej maxp = pos;
146 1.1 thorpej }
147 1.1 thorpej }
148 1.1 thorpej
149 1.1 thorpej marks[MARK_START] = LOADADDR(minp);
150 1.1 thorpej marks[MARK_ENTRY] = LOADADDR(coff->a.entry);
151 1.1 thorpej marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */
152 1.1 thorpej marks[MARK_SYM] = LOADADDR(maxp);
153 1.1 thorpej marks[MARK_END] = LOADADDR(maxp);
154 1.8 martin marks[MARK_DATA] = LOADADDR(coff->a.data_start);
155 1.1 thorpej return 0;
156 1.1 thorpej }
157 1.2 thorpej
158 1.2 thorpej #endif /* BOOT_ECOFF */
159