1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright (c) 2000 by Sun Microsystems, Inc. 24*0Sstevel@tonic-gate * All rights reserved. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <unistd.h> 30*0Sstevel@tonic-gate #include <stdlib.h> 31*0Sstevel@tonic-gate #include <stdio.h> 32*0Sstevel@tonic-gate #include <sys/stat.h> 33*0Sstevel@tonic-gate #include <fcntl.h> 34*0Sstevel@tonic-gate #include <string.h> 35*0Sstevel@tonic-gate #include <fcntl.h> 36*0Sstevel@tonic-gate #include <sys/shm.h> 37*0Sstevel@tonic-gate #include <dlfcn.h> 38*0Sstevel@tonic-gate #include <sys/systeminfo.h> 39*0Sstevel@tonic-gate #include <sys/utsname.h> 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #include <fcode/private.h> 42*0Sstevel@tonic-gate #include <fcode/log.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #include <fcdriver/fcdriver.h> 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate static char *default_search_path; 47*0Sstevel@tonic-gate static char search_proto[] = 48*0Sstevel@tonic-gate "/usr/platform/%s/lib/efcode%s:" 49*0Sstevel@tonic-gate "/usr/platform/%s/lib/efcode%s:" 50*0Sstevel@tonic-gate "/usr/lib/efcode%s"; 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate /* 53*0Sstevel@tonic-gate * Build the library/drop-in fcode search path. If there's no architecture 54*0Sstevel@tonic-gate * passed, we build the search path (per the PSARC decision): 55*0Sstevel@tonic-gate * /usr/platform/`uname -i`/lib/efcode 56*0Sstevel@tonic-gate * /usr/platform/`uname -m`/lib/efcode 57*0Sstevel@tonic-gate * /usr/lib/efcode 58*0Sstevel@tonic-gate * If there is an architecture passed, we prepend the following search path to 59*0Sstevel@tonic-gate * the above: 60*0Sstevel@tonic-gate * /usr/platform/`uname -i`/lib/efcode/{architecture} 61*0Sstevel@tonic-gate * /usr/platform/`uname -m`/lib/efcode/{architecture} 62*0Sstevel@tonic-gate * /usr/lib/efcode/{architecture} 63*0Sstevel@tonic-gate * This allows FCode drop-in searches to find FCode in the non-architecture 64*0Sstevel@tonic-gate * directories. 65*0Sstevel@tonic-gate */ 66*0Sstevel@tonic-gate static void 67*0Sstevel@tonic-gate build_default_search_path(char *arch) 68*0Sstevel@tonic-gate { 69*0Sstevel@tonic-gate char platform[100], *p; 70*0Sstevel@tonic-gate struct stat statb; 71*0Sstevel@tonic-gate struct utsname utsname; 72*0Sstevel@tonic-gate int len; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate sysinfo(SI_PLATFORM, platform, sizeof (platform)); 75*0Sstevel@tonic-gate uname(&utsname); 76*0Sstevel@tonic-gate len = strlen(search_proto) + strlen(platform) + strlen(utsname.machine); 77*0Sstevel@tonic-gate if (*arch != '\0') { 78*0Sstevel@tonic-gate len += len + (3 * strlen(arch)) + 1; 79*0Sstevel@tonic-gate } 80*0Sstevel@tonic-gate default_search_path = MALLOC(len); 81*0Sstevel@tonic-gate if (*arch != '\0') { 82*0Sstevel@tonic-gate sprintf(default_search_path, search_proto, platform, arch, 83*0Sstevel@tonic-gate utsname.machine, arch, arch); 84*0Sstevel@tonic-gate p = default_search_path + strlen(default_search_path); 85*0Sstevel@tonic-gate *p++ = ':'; 86*0Sstevel@tonic-gate } else 87*0Sstevel@tonic-gate p = default_search_path; 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate sprintf(p, search_proto, platform, "", utsname.machine, "", ""); 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate static void 93*0Sstevel@tonic-gate set_default_search_path(fcode_env_t *env) 94*0Sstevel@tonic-gate { 95*0Sstevel@tonic-gate if (default_search_path) 96*0Sstevel@tonic-gate FREE(default_search_path); 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate parse_word(env); 99*0Sstevel@tonic-gate default_search_path = pop_a_duped_string(env, NULL); 100*0Sstevel@tonic-gate } 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate static void 103*0Sstevel@tonic-gate get_default_search_path(fcode_env_t *env) 104*0Sstevel@tonic-gate { 105*0Sstevel@tonic-gate push_a_string(env, default_search_path); 106*0Sstevel@tonic-gate } 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /* 109*0Sstevel@tonic-gate * Complicated by fact that a library (e.g. a 32-bit library) can match the 110*0Sstevel@tonic-gate * file name. But if we're running as 64-bit, dlopen on that library will 111*0Sstevel@tonic-gate * fail. 112*0Sstevel@tonic-gate */ 113*0Sstevel@tonic-gate static char * 114*0Sstevel@tonic-gate search_path(char *name, char *search, int (*fn)(char *)) 115*0Sstevel@tonic-gate { 116*0Sstevel@tonic-gate char *p, *next_p; 117*0Sstevel@tonic-gate char *tpath, *fpath; 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate fpath = STRDUP(search); 120*0Sstevel@tonic-gate for (p = fpath; p != NULL; p = next_p) { 121*0Sstevel@tonic-gate if ((next_p = strchr(p, ':')) != NULL) 122*0Sstevel@tonic-gate *next_p++ = '\0'; 123*0Sstevel@tonic-gate tpath = MALLOC(strlen(p) + strlen(name) + 2); 124*0Sstevel@tonic-gate sprintf(tpath, "%s/%s", p, name); 125*0Sstevel@tonic-gate if ((*fn)(tpath)) { 126*0Sstevel@tonic-gate FREE(fpath); 127*0Sstevel@tonic-gate return (tpath); 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate FREE(tpath); 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate FREE(fpath); 132*0Sstevel@tonic-gate return (NULL); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate static int 136*0Sstevel@tonic-gate load_lib_file(char *path) 137*0Sstevel@tonic-gate { 138*0Sstevel@tonic-gate struct stat buf; 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "load_lib_file: '%s' -> ", path); 141*0Sstevel@tonic-gate if (stat(path, &buf)) { 142*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "stat failed\n"); 143*0Sstevel@tonic-gate return (0); 144*0Sstevel@tonic-gate } 145*0Sstevel@tonic-gate if (dlopen(path, RTLD_NOW) != NULL) { 146*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "OK\n"); 147*0Sstevel@tonic-gate return (1); 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "dlopen failed\n"); 150*0Sstevel@tonic-gate return (0); 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate static int 154*0Sstevel@tonic-gate is_fcode_file(char *path) 155*0Sstevel@tonic-gate { 156*0Sstevel@tonic-gate struct stat statb; 157*0Sstevel@tonic-gate int fd; 158*0Sstevel@tonic-gate uchar_t header[8]; 159*0Sstevel@tonic-gate int status; 160*0Sstevel@tonic-gate static char func_name[] = "is_fcode_file"; 161*0Sstevel@tonic-gate extern int check_fcode_header(char *, uchar_t *, int); 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' -> ", func_name, path); 164*0Sstevel@tonic-gate if ((fd = open(path, 0)) < 0) { 165*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' can't open\n", func_name, 166*0Sstevel@tonic-gate path); 167*0Sstevel@tonic-gate return (0); 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate if (fstat(fd, &statb) != 0 || read(fd, header, sizeof (header)) < 0) { 170*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' can't fstat/read\n", 171*0Sstevel@tonic-gate func_name, path); 172*0Sstevel@tonic-gate close(fd); 173*0Sstevel@tonic-gate return (0); 174*0Sstevel@tonic-gate } 175*0Sstevel@tonic-gate status = check_fcode_header(path, header, statb.st_size); 176*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: '%s' format %s\n", func_name, path, 177*0Sstevel@tonic-gate status ? "OK" : "NOT OK"); 178*0Sstevel@tonic-gate close(fd); 179*0Sstevel@tonic-gate return (status); 180*0Sstevel@tonic-gate } 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate static char * 183*0Sstevel@tonic-gate find_lib_file(fcode_env_t *env, char *prefix, char *name, char *suffix, 184*0Sstevel@tonic-gate int (*fn)(char *)) 185*0Sstevel@tonic-gate { 186*0Sstevel@tonic-gate char *search, *fname; 187*0Sstevel@tonic-gate char *lib_name; 188*0Sstevel@tonic-gate common_data_t *cdp = env->private; 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate if ((search = cdp->search_path) == NULL && 191*0Sstevel@tonic-gate (search = default_search_path) == NULL) { 192*0Sstevel@tonic-gate log_message(MSG_ERROR, "find_lib_file: no search path\n"); 193*0Sstevel@tonic-gate return (NULL); 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate lib_name = MALLOC(strlen(name) + strlen(prefix) + strlen(suffix) + 1); 197*0Sstevel@tonic-gate sprintf(lib_name, "%s%s%s", prefix, name, suffix); 198*0Sstevel@tonic-gate fname = search_path(lib_name, search, fn); 199*0Sstevel@tonic-gate FREE(lib_name); 200*0Sstevel@tonic-gate return (fname); 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate char * 204*0Sstevel@tonic-gate search_for_fcode_file(fcode_env_t *env, char *basename) 205*0Sstevel@tonic-gate { 206*0Sstevel@tonic-gate return (find_lib_file(env, "", basename, ".fc", is_fcode_file)); 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate static void 210*0Sstevel@tonic-gate load_appropriate_file(fcode_env_t *env, char *name, device_t *d) 211*0Sstevel@tonic-gate { 212*0Sstevel@tonic-gate char *fname; 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate if ((fname = find_lib_file(env, "lfc_", name, ".so", load_lib_file)) 215*0Sstevel@tonic-gate != NULL) { 216*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "Loading Library: %s\n", fname); 217*0Sstevel@tonic-gate FREE(fname); 218*0Sstevel@tonic-gate } else if ((fname = search_for_fcode_file(env, name)) != NULL) { 219*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "Loading Fcode: %s\n", fname); 220*0Sstevel@tonic-gate run_fcode_from_file(env, fname, 0); 221*0Sstevel@tonic-gate FREE(fname); 222*0Sstevel@tonic-gate } else { 223*0Sstevel@tonic-gate throw_from_fclib(env, 1, 224*0Sstevel@tonic-gate "Can't find 'lfc_%s.so' or '%s.fc'\n", name, name); 225*0Sstevel@tonic-gate } 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate void 229*0Sstevel@tonic-gate install_node_data(fcode_env_t *env, device_t *d) 230*0Sstevel@tonic-gate { 231*0Sstevel@tonic-gate prop_t *p; 232*0Sstevel@tonic-gate device_t *cd; 233*0Sstevel@tonic-gate char libname[512]; 234*0Sstevel@tonic-gate static char func_name[] = "install_node_data"; 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate if (d->parent) { 237*0Sstevel@tonic-gate if ((p = lookup_package_property(env, "device_type", 238*0Sstevel@tonic-gate d->parent)) == NULL) { 239*0Sstevel@tonic-gate log_message(MSG_ERROR, "%s: no 'device_type' property" 240*0Sstevel@tonic-gate " for '%s'\n", func_name, get_path(env, d->parent)); 241*0Sstevel@tonic-gate return; 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate /* 244*0Sstevel@tonic-gate * Warning: lookup_package_property uses a static data area to 245*0Sstevel@tonic-gate * build the property node returned, so we have to grab a copy 246*0Sstevel@tonic-gate * of the data. 247*0Sstevel@tonic-gate */ 248*0Sstevel@tonic-gate strcpy(libname, (char *)p->data); 249*0Sstevel@tonic-gate strcat(libname, "_"); 250*0Sstevel@tonic-gate } else 251*0Sstevel@tonic-gate libname[0] = '\0'; 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate if ((p = lookup_package_property(env, "device_type", d)) == NULL) { 254*0Sstevel@tonic-gate log_message(MSG_ERROR, "%s: no 'device_type' property for" 255*0Sstevel@tonic-gate " '%s'\n", func_name, get_path(env, d)); 256*0Sstevel@tonic-gate return; 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate /* 260*0Sstevel@tonic-gate * Warning: lookup_package_property uses a static data area to build 261*0Sstevel@tonic-gate * the property node returned, so we have to grab a copy of the 262*0Sstevel@tonic-gate * data. 263*0Sstevel@tonic-gate */ 264*0Sstevel@tonic-gate strcat(libname, (char *)p->data); 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate debug_msg(DEBUG_FIND_FCODE, "%s: `%s` lname: '%s'\n", func_name, 267*0Sstevel@tonic-gate get_path(env, d), libname); 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate load_appropriate_file(env, libname, d); 270*0Sstevel@tonic-gate } 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate #pragma init(_init) 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate static void 275*0Sstevel@tonic-gate _init(void) 276*0Sstevel@tonic-gate { 277*0Sstevel@tonic-gate fcode_env_t *env = initial_env; 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate ASSERT(env); 280*0Sstevel@tonic-gate NOTICE; 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate #if defined(__sparcv9) 283*0Sstevel@tonic-gate build_default_search_path("/sparcv9"); 284*0Sstevel@tonic-gate #else 285*0Sstevel@tonic-gate build_default_search_path(""); 286*0Sstevel@tonic-gate #endif 287*0Sstevel@tonic-gate FORTH(0, "set-default-search-path", set_default_search_path); 288*0Sstevel@tonic-gate FORTH(0, "get-default-search-path", get_default_search_path); 289*0Sstevel@tonic-gate } 290