1 /* $NetBSD: platid.c,v 1.3 2001/09/27 16:31:23 uch Exp $ */ 2 3 /*- 4 * Copyright (c) 1999-2001 5 * Shin Takemura and PocketBSD Project. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the PocketBSD project 18 * and its contributors. 19 * 4. Neither the name of the project nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 */ 36 37 #include <sys/types.h> 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 41 #include <machine/platid.h> 42 43 platid_t platid_unknown = {{ PLATID_UNKNOWN, PLATID_UNKNOWN }}; 44 platid_t platid_wild = {{ PLATID_WILD, PLATID_WILD }}; 45 platid_t platid = {{ PLATID_UNKNOWN, PLATID_UNKNOWN }}; 46 47 void 48 platid_ntoh(platid_t *pid) 49 { 50 51 pid->dw.dw0 = ntohl(pid->dw.dw0); 52 pid->dw.dw1 = ntohl(pid->dw.dw1); 53 } 54 55 void 56 platid_hton(platid_t *pid) 57 { 58 59 pid->dw.dw0 = htonl(pid->dw.dw0); 60 pid->dw.dw1 = htonl(pid->dw.dw1); 61 } 62 63 void 64 platid_dump(char *name, void* pxx) 65 { 66 int i; 67 unsigned char* p = (unsigned char*)pxx; 68 69 printf("%14s: ", name); 70 71 for (i = 0; i < 8; i++) { 72 printf("%02x", p[i]); 73 } 74 printf("\n"); 75 } 76 77 int 78 platid_match(platid_t *platid, platid_mask_t *mask) 79 { 80 81 return (platid_match_sub(platid, mask, 0)); 82 } 83 84 int 85 platid_match_sub(platid_t *platid, platid_mask_t *mask, int unknown_is_match) 86 { 87 int match_count; 88 89 #define PLATID_MATCH(mbr) \ 90 if (platid->s.mbr != mask->s.mbr && \ 91 mask->s.mbr != platid_wild.s.mbr && \ 92 !(platid->s.mbr == platid_unknown.s.mbr && \ 93 unknown_is_match)) { \ 94 return (0); \ 95 } else if (platid->s.mbr == mask->s.mbr) { \ 96 match_count++; \ 97 } 98 99 match_count = 1; 100 PLATID_MATCH(cpu_submodel); 101 PLATID_MATCH(cpu_model); 102 PLATID_MATCH(cpu_series); 103 PLATID_MATCH(cpu_arch); 104 105 PLATID_MATCH(submodel); 106 PLATID_MATCH(model); 107 PLATID_MATCH(series); 108 PLATID_MATCH(vendor); 109 110 return (match_count); 111 112 #undef PLATID_MATCH 113 } 114 115 tchar* 116 platid_name(platid_t *platid) 117 { 118 struct platid_name *match; 119 120 match = platid_search(platid, 121 platid_name_table, platid_name_table_size, 122 sizeof(struct platid_name)); 123 124 return ((match != NULL) ? match->name : TEXT("UNKNOWN")); 125 } 126 127 struct platid_data * 128 platid_search_data(platid_t *platid, struct platid_data *datap) 129 { 130 131 while (datap->mask != NULL && !platid_match(platid, datap->mask)) 132 datap++; 133 if (datap->mask == NULL && datap->data == NULL) 134 return (NULL); 135 136 return (datap); 137 } 138 139 void * 140 platid_search(platid_t *platid, void *base, int nmemb, int size) 141 { 142 int i, match_level, res; 143 void *match; 144 145 match_level = 0; 146 match = NULL; 147 for (i = 0; i < nmemb; i++) { 148 res = platid_match(platid, *(platid_mask_t**)base); 149 if (match_level < res) { 150 match_level = res; 151 match = base; 152 } 153 (char *)base += size; 154 } 155 156 return (match); 157 } 158