1 /*	$OpenBSD: exec.c,v 1.6 2003/08/16 14:45:46 henning Exp $ */
2 
3 /*
4  * Copyright (c) 1999 Mats O Jansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <err.h>
28 #include <sys/types.h>
29 #include <stdio.h>
30 #include "exec.h"
31 
32 __RCSID("$MirOS: src/usr.sbin/config/exec.c,v 1.4 2010/07/25 16:31:08 tg Exp $");
33 
34 #ifdef AOUT_SUPPORT
35 int	aout_check(char *);
36 void	aout_loadkernel(const char *);
37 void	aout_savekernel(const char *);
38 caddr_t	aout_adjust(caddr_t);
39 caddr_t	aout_readjust(caddr_t);
40 #endif
41 
42 #ifdef ECOFF_SUPPORT
43 int	ecoff_check(char *);
44 void	ecoff_loadkernel(const char *);
45 void	ecoff_savekernel(const char *);
46 caddr_t	ecoff_adjust(caddr_t);
47 caddr_t	ecoff_readjust(caddr_t);
48 #endif
49 
50 #ifdef ELF_SUPPORT
51 int	elf_check(const char *);
52 void	elf_loadkernel(const char *);
53 void	elf_savekernel(const char *);
54 caddr_t	elf_adjust(caddr_t);
55 caddr_t	elf_readjust(caddr_t);
56 #endif
57 
58 #define DO_AOUT	0
59 #define DO_ECOFF 1
60 #define	DO_ELF 2
61 
62 int current_exec = -1;
63 
64 caddr_t
adjust(caddr_t x)65 adjust(caddr_t x)
66 {
67 	switch (current_exec) {
68 #ifdef AOUT_SUPPORT
69 	case DO_AOUT:
70 		return(aout_adjust(x));
71 		break;
72 #endif
73 #ifdef ECOFF_SUPPORT
74 	case DO_ECOFF:
75 		return(ecoff_adjust(x));
76 		break;
77 #endif
78 #ifdef ELF_SUPPORT
79 	case DO_ELF:
80 		return(elf_adjust(x));
81 		break;
82 #endif
83 	default:
84 		errx(1, "no supported exec type");
85 	}
86 }
87 
88 caddr_t
readjust(caddr_t x)89 readjust(caddr_t x)
90 {
91 	switch (current_exec) {
92 #ifdef AOUT_SUPPORT
93 	case DO_AOUT:
94 		return(aout_readjust(x));
95 		break;
96 #endif
97 #ifdef ECOFF_SUPPORT
98 	case DO_ECOFF:
99 		return(ecoff_readjust(x));
100 		break;
101 #endif
102 #ifdef ELF_SUPPORT
103 	case DO_ELF:
104 		return(elf_readjust(x));
105 		break;
106 #endif
107 	default:
108 		errx(1, "no supported exec type");
109 	}
110 }
111 
112 void
loadkernel(const char * file)113 loadkernel(const char *file)
114 {
115 	current_exec = -1;
116 
117 #ifdef AOUT_SUPPORT
118 	if (aout_check(file)) {
119 		current_exec = DO_AOUT;
120 	}
121 #endif
122 
123 #ifdef ECOFF_SUPPORT
124 	if (ecoff_check(file)) {
125 		current_exec = DO_ECOFF;
126 	}
127 #endif
128 
129 #ifdef ELF_SUPPORT
130 	if (elf_check(file)) {
131 		current_exec = DO_ELF;
132 	}
133 #endif
134 
135 	switch (current_exec) {
136 #ifdef AOUT_SUPPORT
137 	case DO_AOUT:
138 		aout_loadkernel(file);
139 		break;
140 #endif
141 #ifdef ECOFF_SUPPORT
142 	case DO_ECOFF:
143 		ecoff_loadkernel(file);
144 		break;
145 #endif
146 #ifdef ELF_SUPPORT
147 	case DO_ELF:
148 		elf_loadkernel(file);
149 		break;
150 #endif
151 	default:
152 		errx(1, "no supported exec type");
153 	}
154 }
155 
156 void
savekernel(const char * outfile)157 savekernel(const char *outfile)
158 {
159 	switch (current_exec) {
160 #ifdef AOUT_SUPPORT
161 	case DO_AOUT:
162 		aout_savekernel(outfile);
163 		break;
164 #endif
165 #ifdef ECOFF_SUPPORT
166 	case DO_ECOFF:
167 		ecoff_savekernel(outfile);
168 		break;
169 #endif
170 #ifdef ELF_SUPPORT
171 	case DO_ELF:
172 		elf_savekernel(outfile);
173 		break;
174 #endif
175 	default:
176 		errx(1, "no supported exec type");
177 	}
178 }
179