subr_module.c revision 40090
1/*- 2 * Copyright (c) 1998 Michael Smith 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $Id$ 27 */ 28 29#include <sys/param.h> 30#include <sys/kernel.h> 31#include <sys/systm.h> 32#include <sys/linker.h> 33 34/* 35 * Preloaded module support 36 */ 37 38caddr_t module_metadata; 39 40/* 41 * Search for the preloaded module (name) 42 */ 43caddr_t 44module_search_by_name(const char *name) 45{ 46 caddr_t curp; 47 u_int32_t *hdr; 48 49 if (module_metadata != NULL) { 50 51 curp = module_metadata; 52 for (;;) { 53 hdr = (u_int32_t *)curp; 54 if (hdr[0] == 0) 55 break; 56 57 /* Search for a MODINFO_NAME field */ 58 if ((hdr[0] == MODINFO_NAME) && 59 !strcmp(name, curp + sizeof(u_int32_t) * 2)) 60 return(curp); 61 62 /* skip to next field */ 63 curp += sizeof(u_int32_t) * 2 + hdr[1]; 64 } 65 } 66 return(NULL); 67} 68 69/* 70 * Search for the first preloaded module of (type) 71 */ 72caddr_t 73module_search_by_type(const char *type) 74{ 75 caddr_t curp, lname; 76 u_int32_t *hdr; 77 78 if (module_metadata != NULL) { 79 80 curp = module_metadata; 81 lname = NULL; 82 for (;;) { 83 hdr = (u_int32_t *)curp; 84 if (hdr[0] == 0) 85 break; 86 87 /* remember the start of each record */ 88 if (hdr[0] == MODINFO_NAME) 89 lname = curp; 90 91 /* Search for a MODINFO_TYPE field */ 92 if ((hdr[0] == MODINFO_TYPE) && 93 !strcmp(type, curp + sizeof(u_int32_t) * 2)) 94 return(lname); 95 96 /* skip to next field */ 97 curp += sizeof(u_int32_t) * 2 + hdr[1]; 98 } 99 } 100 return(NULL); 101} 102 103/* 104 * Given a preloaded module handle (mod), return a pointer 105 * to the data for the attribute (inf). 106 */ 107caddr_t 108module_search_info(caddr_t mod, int inf) 109{ 110 caddr_t curp; 111 u_int32_t *hdr; 112 u_int32_t type = 0; 113 114 curp = mod; 115 for (;;) { 116 hdr = (u_int32_t *)curp; 117 /* end of module data? */ 118 if (hdr[0] == 0) 119 break; 120 /* 121 * We give up once we've looped back to what we were looking at 122 * first - this should normally be a MODINFO_NAME field. 123 */ 124 if (type == 0) { 125 type = hdr[0]; 126 } else { 127 if (hdr[0] == type) 128 break; 129 } 130 131 /* 132 * Attribute match? Return pointer to data. 133 * Consumer may safely assume that size value preceeds 134 * data. 135 */ 136 if (hdr[0] == inf) 137 return(curp + (sizeof(u_int32_t) * 2)); 138 139 /* skip to next field */ 140 curp += sizeof(u_int32_t) * 2 + hdr[1]; 141 } 142 return(NULL); 143} 144 145