1 /* 2 * The PCI Library -- Initialization and related things 3 * 4 * Copyright (c) 1997--2018 Martin Mares <[email protected]> 5 * 6 * Can be freely distributed and used under the terms of the GNU GPL. 7 */ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <stdarg.h> 12 #include <string.h> 13 14 #include "internal.h" 15 16 #ifdef PCI_OS_WINDOWS 17 18 #include <windows.h> 19 20 /* Force usage of ANSI (char*) variant of GetModuleFileName() function */ 21 #ifdef _WIN32 22 #ifdef GetModuleFileName 23 #undef GetModuleFileName 24 #endif 25 #define GetModuleFileName GetModuleFileNameA 26 #endif 27 28 /* Define __ImageBase for all linkers */ 29 #ifdef _WIN32 30 /* GNU LD provides __ImageBase symbol since 2.19, in previous versions it is 31 * under name _image_base__, so add weak alias for compatibility. */ 32 #ifdef __GNUC__ 33 asm(".weak\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "\n\t" 34 ".set\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "," PCI_STRINGIFY(__MINGW_USYMBOL(_image_base__))); 35 #endif 36 /* 37 * MSVC link.exe provides __ImageBase symbol since 12.00 (MSVC 6.0), for 38 * previous versions resolve it at runtime via GetModuleHandleA() which 39 * returns base for main executable or via VirtualQuery() for DLL builds. 40 */ 41 #if defined(_MSC_VER) && _MSC_VER < 1200 42 static HMODULE 43 get_current_module_handle(void) 44 { 45 #ifdef PCI_SHARED_LIB 46 MEMORY_BASIC_INFORMATION info; 47 size_t len = VirtualQuery(&get_current_module_handle, &info, sizeof(info)); 48 if (len != sizeof(info)) 49 return NULL; 50 return (HMODULE)info.AllocationBase; 51 #else 52 return GetModuleHandleA(NULL); 53 #endif 54 } 55 #define __ImageBase (*(IMAGE_DOS_HEADER *)get_current_module_handle()) 56 #else 57 extern IMAGE_DOS_HEADER __ImageBase; 58 #endif 59 #endif 60 61 #if defined(_WINDLL) 62 extern HINSTANCE _hModule; 63 #elif defined(_WINDOWS) 64 extern HINSTANCE _hInstance; 65 #endif 66 67 #endif 68 69 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = { 70 NULL, 71 #ifdef PCI_HAVE_PM_LINUX_SYSFS 72 &pm_linux_sysfs, 73 #else 74 NULL, 75 #endif 76 #ifdef PCI_HAVE_PM_LINUX_PROC 77 &pm_linux_proc, 78 #else 79 NULL, 80 #endif 81 #ifdef PCI_HAVE_PM_INTEL_CONF 82 &pm_intel_conf1, 83 &pm_intel_conf2, 84 #else 85 NULL, 86 NULL, 87 #endif 88 #ifdef PCI_HAVE_PM_FBSD_DEVICE 89 &pm_fbsd_device, 90 #else 91 NULL, 92 #endif 93 #ifdef PCI_HAVE_PM_AIX_DEVICE 94 &pm_aix_device, 95 #else 96 NULL, 97 #endif 98 #ifdef PCI_HAVE_PM_NBSD_LIBPCI 99 &pm_nbsd_libpci, 100 #else 101 NULL, 102 #endif 103 #ifdef PCI_HAVE_PM_OBSD_DEVICE 104 &pm_obsd_device, 105 #else 106 NULL, 107 #endif 108 #ifdef PCI_HAVE_PM_DUMP 109 &pm_dump, 110 #else 111 NULL, 112 #endif 113 #ifdef PCI_HAVE_PM_DARWIN_DEVICE 114 &pm_darwin, 115 #else 116 NULL, 117 #endif 118 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE 119 &pm_sylixos_device, 120 #else 121 NULL, 122 #endif 123 #ifdef PCI_HAVE_PM_HURD_CONF 124 &pm_hurd, 125 #else 126 NULL, 127 #endif 128 #ifdef PCI_HAVE_PM_WIN32_CFGMGR32 129 &pm_win32_cfgmgr32, 130 #else 131 NULL, 132 #endif 133 #ifdef PCI_HAVE_PM_WIN32_KLDBG 134 &pm_win32_kldbg, 135 #else 136 NULL, 137 #endif 138 #ifdef PCI_HAVE_PM_WIN32_SYSDBG 139 &pm_win32_sysdbg, 140 #else 141 NULL, 142 #endif 143 #ifdef PCI_HAVE_PM_MMIO_CONF 144 &pm_mmio_conf1, 145 &pm_mmio_conf1_ext, 146 #else 147 NULL, 148 NULL, 149 #endif 150 }; 151 152 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order 153 static int probe_sequence[] = { 154 // System-specific methods 155 PCI_ACCESS_SYS_BUS_PCI, 156 PCI_ACCESS_PROC_BUS_PCI, 157 PCI_ACCESS_FBSD_DEVICE, 158 PCI_ACCESS_AIX_DEVICE, 159 PCI_ACCESS_NBSD_LIBPCI, 160 PCI_ACCESS_OBSD_DEVICE, 161 PCI_ACCESS_DARWIN, 162 PCI_ACCESS_SYLIXOS_DEVICE, 163 PCI_ACCESS_HURD, 164 PCI_ACCESS_WIN32_CFGMGR32, 165 PCI_ACCESS_WIN32_KLDBG, 166 PCI_ACCESS_WIN32_SYSDBG, 167 // Low-level methods poking the hardware directly 168 PCI_ACCESS_I386_TYPE1, 169 PCI_ACCESS_I386_TYPE2, 170 PCI_ACCESS_MMIO_TYPE1_EXT, 171 PCI_ACCESS_MMIO_TYPE1, 172 -1, 173 }; 174 175 static void PCI_NONRET 176 pci_generic_error(char *msg, ...) 177 { 178 va_list args; 179 180 va_start(args, msg); 181 fputs("pcilib: ", stderr); 182 vfprintf(stderr, msg, args); 183 va_end(args); 184 fputc('\n', stderr); 185 exit(1); 186 } 187 188 static void 189 pci_generic_warn(char *msg, ...) 190 { 191 va_list args; 192 193 va_start(args, msg); 194 fputs("pcilib: ", stderr); 195 vfprintf(stderr, msg, args); 196 va_end(args); 197 fputc('\n', stderr); 198 } 199 200 static void 201 pci_generic_debug(char *msg, ...) 202 { 203 va_list args; 204 205 va_start(args, msg); 206 vfprintf(stdout, msg, args); 207 va_end(args); 208 } 209 210 static void 211 pci_null_debug(char *msg UNUSED, ...) 212 { 213 } 214 215 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL 216 217 void * 218 pci_malloc(struct pci_access *a, int size) 219 { 220 void *x = malloc(size); 221 222 if (!x) 223 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size); 224 return x; 225 } 226 227 void 228 pci_mfree(void *x) 229 { 230 if (x) 231 free(x); 232 } 233 234 char * 235 pci_strdup(struct pci_access *a, const char *s) 236 { 237 int len = strlen(s) + 1; 238 char *t = pci_malloc(a, len); 239 memcpy(t, s, len); 240 return t; 241 } 242 243 int 244 pci_lookup_method(char *name) 245 { 246 int i; 247 248 for (i=0; i<PCI_ACCESS_MAX; i++) 249 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name)) 250 return i; 251 return -1; 252 } 253 254 char * 255 pci_get_method_name(int index) 256 { 257 if (index < 0 || index >= PCI_ACCESS_MAX) 258 return NULL; 259 else if (!pci_methods[index]) 260 return ""; 261 else 262 return pci_methods[index]->name; 263 } 264 265 #ifdef PCI_OS_WINDOWS 266 267 static void 268 pci_init_name_list_path(struct pci_access *a) 269 { 270 if ((PCI_PATH_IDS_DIR)[0]) 271 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0); 272 else 273 { 274 char *path, *sep; 275 size_t len; 276 277 #if defined(_WIN32) || defined(_WINDLL) || defined(_WINDOWS) 278 279 HMODULE module; 280 size_t size; 281 282 #if defined(_WIN32) 283 module = (HINSTANCE)&__ImageBase; 284 #elif defined(_WINDLL) 285 module = _hModule; 286 #elif defined(_WINDOWS) 287 module = _hInstance; 288 #endif 289 290 /* 291 * Module file name can have arbitrary length despite all MS examples say 292 * about MAX_PATH upper limit. This limit does not apply for example when 293 * executable is running from network disk with very long UNC paths or 294 * when using "\\??\\" prefix for specifying executable binary path. 295 * Function GetModuleFileName() returns passed size argument when passed 296 * buffer is too small and does not signal any error. In this case retry 297 * again with larger buffer. 298 */ 299 size = 256; /* initial buffer size (more than sizeof(PCI_IDS)) */ 300 retry: 301 path = pci_malloc(a, size); 302 len = GetModuleFileName(module, path, size-sizeof(PCI_IDS)); 303 if (len >= size-sizeof(PCI_IDS)) 304 { 305 free(path); 306 size *= 2; 307 goto retry; 308 } 309 else if (len == 0) 310 path[0] = '\0'; 311 312 #else 313 314 const char *exe_path; 315 316 exe_path = _pgmptr; 317 318 len = strlen(exe_path); 319 path = pci_malloc(a, len+sizeof(PCI_IDS)); 320 memcpy(path, exe_path, len+1); 321 322 #endif 323 324 sep = strrchr(path, '\\'); 325 if (!sep) 326 { 327 /* 328 * If current module path (current executable for static builds or 329 * current DLL library for shared build) cannot be determined then 330 * fallback to the current directory. 331 */ 332 free(path); 333 pci_set_name_list_path(a, PCI_IDS, 0); 334 } 335 else 336 { 337 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS)); 338 pci_set_name_list_path(a, path, 1); 339 } 340 } 341 } 342 343 #else 344 345 static void 346 pci_init_name_list_path(struct pci_access *a) 347 { 348 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0); 349 } 350 351 #endif 352 353 struct pci_access * 354 pci_alloc(void) 355 { 356 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access)); 357 int i; 358 359 memset(a, 0, sizeof(*a)); 360 pci_init_name_list_path(a); 361 #ifdef PCI_USE_DNS 362 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's"); 363 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file"); 364 a->id_lookup_mode = PCI_LOOKUP_CACHE; 365 #endif 366 #ifdef PCI_HAVE_HWDB 367 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero"); 368 #endif 369 for (i=0; i<PCI_ACCESS_MAX; i++) 370 if (pci_methods[i] && pci_methods[i]->config) 371 pci_methods[i]->config(a); 372 return a; 373 } 374 375 void 376 pci_init_v35(struct pci_access *a) 377 { 378 if (!a->error) 379 a->error = pci_generic_error; 380 if (!a->warning) 381 a->warning = pci_generic_warn; 382 if (!a->debug) 383 a->debug = pci_generic_debug; 384 if (!a->debugging) 385 a->debug = pci_null_debug; 386 387 if (a->method) 388 { 389 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method]) 390 a->error("This access method is not supported."); 391 a->methods = pci_methods[a->method]; 392 } 393 else 394 { 395 unsigned int i; 396 for (i=0; probe_sequence[i] >= 0; i++) 397 { 398 struct pci_methods *m = pci_methods[probe_sequence[i]]; 399 if (!m) 400 continue; 401 a->debug("Trying method %s...", m->name); 402 if (m->detect(a)) 403 { 404 a->debug("...OK\n"); 405 a->methods = m; 406 a->method = probe_sequence[i]; 407 break; 408 } 409 a->debug("...No.\n"); 410 } 411 if (!a->methods) 412 a->error("Cannot find any working access method."); 413 } 414 a->debug("Decided to use %s\n", a->methods->name); 415 a->methods->init(a); 416 } 417 418 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a)); 419 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35); 420 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0); 421 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5); 422 423 void 424 pci_cleanup(struct pci_access *a) 425 { 426 struct pci_dev *d, *e; 427 428 for (d=a->devices; d; d=e) 429 { 430 e = d->next; 431 pci_free_dev(d); 432 } 433 if (a->methods) 434 a->methods->cleanup(a); 435 pci_free_name_list(a); 436 pci_free_params(a); 437 pci_set_name_list_path(a, NULL, 0); 438 pci_mfree(a); 439 } 440