2 * Copyright (c) 1998 Michael Smith
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/kern/subr_module.c,v 1.6 1999/10/11 15:19:10 peter Exp $
27 * $DragonFly: src/sys/kern/subr_module.c,v 1.3 2003/11/20 22:07:33 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/linker.h>
35 * Preloaded module support
38 caddr_t preload_metadata;
41 * Search for the preloaded module (name)
44 preload_search_by_name(const char *name)
52 if (preload_metadata == NULL)
55 curp = preload_metadata;
57 hdr = (u_int32_t *)curp;
58 if (hdr[0] == 0 && hdr[1] == 0)
62 * Search for a MODINFO_NAME field. the boot loader really
63 * ought to strip the path names
65 if (hdr[0] == MODINFO_NAME) {
66 scanname = curp + sizeof(u_int32_t) * 2;
68 while (i > 0 && scanname[i-1] != '/')
70 if (strcmp(name, scanname) == 0)
72 if (strcmp(name, scanname + i) == 0)
75 /* skip to next field */
76 next = sizeof(u_int32_t) * 2 + hdr[1];
77 next = roundup(next, sizeof(u_long));
84 * Search for the first preloaded module of (type)
87 preload_search_by_type(const char *type)
93 if (preload_metadata != NULL) {
95 curp = preload_metadata;
98 hdr = (u_int32_t *)curp;
99 if (hdr[0] == 0 && hdr[1] == 0)
102 /* remember the start of each record */
103 if (hdr[0] == MODINFO_NAME)
106 /* Search for a MODINFO_TYPE field */
107 if ((hdr[0] == MODINFO_TYPE) &&
108 !strcmp(type, curp + sizeof(u_int32_t) * 2))
111 /* skip to next field */
112 next = sizeof(u_int32_t) * 2 + hdr[1];
113 next = roundup(next, sizeof(u_long));
121 * Walk through the preloaded module list
124 preload_search_next_name(caddr_t base)
130 if (preload_metadata != NULL) {
132 /* Pick up where we left off last time */
134 /* skip to next field */
136 hdr = (u_int32_t *)curp;
137 next = sizeof(u_int32_t) * 2 + hdr[1];
138 next = roundup(next, sizeof(u_long));
141 curp = preload_metadata;
144 hdr = (u_int32_t *)curp;
145 if (hdr[0] == 0 && hdr[1] == 0)
148 /* Found a new record? */
149 if (hdr[0] == MODINFO_NAME)
152 /* skip to next field */
153 next = sizeof(u_int32_t) * 2 + hdr[1];
154 next = roundup(next, sizeof(u_long));
162 * Given a preloaded module handle (mod), return a pointer
163 * to the data for the attribute (inf).
166 preload_search_info(caddr_t mod, int inf)
175 hdr = (u_int32_t *)curp;
176 /* end of module data? */
177 if (hdr[0] == 0 && hdr[1] == 0)
180 * We give up once we've looped back to what we were looking at
181 * first - this should normally be a MODINFO_NAME field.
191 * Attribute match? Return pointer to data.
192 * Consumer may safely assume that size value preceeds
196 return(curp + (sizeof(u_int32_t) * 2));
198 /* skip to next field */
199 next = sizeof(u_int32_t) * 2 + hdr[1];
200 next = roundup(next, sizeof(u_long));
207 * Delete a preload record by name.
210 preload_delete_name(const char *name)
217 if (preload_metadata != NULL) {
220 curp = preload_metadata;
222 hdr = (u_int32_t *)curp;
223 if (hdr[0] == 0 && hdr[1] == 0)
226 /* Search for a MODINFO_NAME field */
227 if (hdr[0] == MODINFO_NAME) {
228 if (!strcmp(name, curp + sizeof(u_int32_t) * 2))
229 clearing = 1; /* got it, start clearing */
231 clearing = 0; /* at next one now.. better stop */
234 hdr[0] = MODINFO_EMPTY;
236 /* skip to next field */
237 next = sizeof(u_int32_t) * 2 + hdr[1];
238 next = roundup(next, sizeof(u_long));
244 /* Called from locore on i386. Convert physical pointers to kvm. Sigh. */
246 preload_bootstrap_relocate(vm_offset_t offset)
253 if (preload_metadata != NULL) {
255 curp = preload_metadata;
257 hdr = (u_int32_t *)curp;
258 if (hdr[0] == 0 && hdr[1] == 0)
261 /* Deal with the ones that we know we have to fix */
264 case MODINFO_METADATA|MODINFOMD_SSYM:
265 case MODINFO_METADATA|MODINFOMD_ESYM:
266 ptr = (vm_offset_t *)(curp + (sizeof(u_int32_t) * 2));
270 /* The rest is beyond us for now */
272 /* skip to next field */
273 next = sizeof(u_int32_t) * 2 + hdr[1];
274 next = roundup(next, sizeof(u_long));