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