2 * The PCI Library -- Initialization and related things
4 * Copyright (c) 1997--2018 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
19 #include <crt0.h> /* for __dos_argv0 */
26 /* Force usage of ANSI (char*) variant of GetModuleFileName() function */
28 #ifdef GetModuleFileName
29 #undef GetModuleFileName
31 #define GetModuleFileName GetModuleFileNameA
34 /* Define __ImageBase for all linkers */
36 /* GNU LD provides __ImageBase symbol since 2.19, in previous versions it is
37 * under name _image_base__, so add weak alias for compatibility. */
39 asm(".weak\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "\n\t"
40 ".set\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "," PCI_STRINGIFY(__MINGW_USYMBOL(_image_base__)));
43 * MSVC link.exe provides __ImageBase symbol since 12.00 (MSVC 6.0), for
44 * previous versions resolve it at runtime via GetModuleHandleA() which
45 * returns base for main executable or via VirtualQuery() for DLL builds.
47 #if defined(_MSC_VER) && _MSC_VER < 1200
49 get_current_module_handle(void)
52 MEMORY_BASIC_INFORMATION info;
53 size_t len = VirtualQuery(&get_current_module_handle, &info, sizeof(info));
54 if (len != sizeof(info))
56 return (HMODULE)info.AllocationBase;
58 return GetModuleHandleA(NULL);
61 #define __ImageBase (*(IMAGE_DOS_HEADER *)get_current_module_handle())
63 extern IMAGE_DOS_HEADER __ImageBase;
68 extern HINSTANCE _hModule;
69 #elif defined(_WINDOWS)
70 extern HINSTANCE _hInstance;
75 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
77 #ifdef PCI_HAVE_PM_LINUX_SYSFS
82 #ifdef PCI_HAVE_PM_LINUX_PROC
87 #ifdef PCI_HAVE_PM_INTEL_CONF
94 #ifdef PCI_HAVE_PM_FBSD_DEVICE
99 #ifdef PCI_HAVE_PM_AIX_DEVICE
104 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
109 #ifdef PCI_HAVE_PM_OBSD_DEVICE
114 #ifdef PCI_HAVE_PM_DUMP
119 #ifdef PCI_HAVE_PM_DARWIN_DEVICE
124 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE
129 #ifdef PCI_HAVE_PM_HURD_CONF
134 #ifdef PCI_HAVE_PM_WIN32_CFGMGR32
139 #ifdef PCI_HAVE_PM_WIN32_KLDBG
144 #ifdef PCI_HAVE_PM_WIN32_SYSDBG
149 #ifdef PCI_HAVE_PM_MMIO_CONF
156 #if defined(PCI_HAVE_PM_ECAM)
163 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
164 static int probe_sequence[] = {
165 // System-specific methods
166 PCI_ACCESS_SYS_BUS_PCI,
167 PCI_ACCESS_PROC_BUS_PCI,
168 PCI_ACCESS_FBSD_DEVICE,
169 PCI_ACCESS_AIX_DEVICE,
170 PCI_ACCESS_NBSD_LIBPCI,
171 PCI_ACCESS_OBSD_DEVICE,
173 PCI_ACCESS_SYLIXOS_DEVICE,
175 PCI_ACCESS_WIN32_CFGMGR32,
176 PCI_ACCESS_WIN32_KLDBG,
177 PCI_ACCESS_WIN32_SYSDBG,
178 // Low-level methods poking the hardware directly
180 PCI_ACCESS_I386_TYPE1,
181 PCI_ACCESS_I386_TYPE2,
182 PCI_ACCESS_MMIO_TYPE1_EXT,
183 PCI_ACCESS_MMIO_TYPE1,
187 static void PCI_NONRET
188 pci_generic_error(char *msg, ...)
193 fputs("pcilib: ", stderr);
194 vfprintf(stderr, msg, args);
201 pci_generic_warn(char *msg, ...)
206 fputs("pcilib: ", stderr);
207 vfprintf(stderr, msg, args);
213 pci_generic_debug(char *msg, ...)
218 vfprintf(stdout, msg, args);
223 pci_null_debug(char *msg UNUSED, ...)
227 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL
230 pci_malloc(struct pci_access *a, int size)
232 void *x = malloc(size);
235 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size);
247 pci_strdup(struct pci_access *a, const char *s)
249 int len = strlen(s) + 1;
250 char *t = pci_malloc(a, len);
256 pci_lookup_method(char *name)
260 for (i=0; i<PCI_ACCESS_MAX; i++)
261 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
267 pci_get_method_name(int index)
269 if (index < 0 || index >= PCI_ACCESS_MAX)
271 else if (!pci_methods[index])
274 return pci_methods[index]->name;
277 #if defined(PCI_OS_WINDOWS) || defined(PCI_OS_DJGPP)
280 pci_init_name_list_path(struct pci_access *a)
282 if ((PCI_PATH_IDS_DIR)[0])
283 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0);
289 #if defined(PCI_OS_WINDOWS) && (defined(_WIN32) || defined(_WINDLL) || defined(_WINDOWS))
295 module = (HINSTANCE)&__ImageBase;
296 #elif defined(_WINDLL)
298 #elif defined(_WINDOWS)
303 * Module file name can have arbitrary length despite all MS examples say
304 * about MAX_PATH upper limit. This limit does not apply for example when
305 * executable is running from network disk with very long UNC paths or
306 * when using "\\??\\" prefix for specifying executable binary path.
307 * Function GetModuleFileName() returns passed size argument when passed
308 * buffer is too small and does not signal any error. In this case retry
309 * again with larger buffer.
311 size = 256; /* initial buffer size (more than sizeof(PCI_IDS)-4) */
313 path = pci_malloc(a, size);
314 len = GetModuleFileName(module, path, size-sizeof(PCI_IDS)-4); /* 4 for "\\\\?\\" */
315 if (len >= size-sizeof(PCI_IDS)-4)
325 * GetModuleFileName() has bugs. On Windows 10 it prepends current drive
326 * letter if path is just pure NT namespace (with "\\??\\" prefix). Such
327 * extra drive letter makes path fully invalid and unusable. So remove
328 * extra drive letter to make path valid again.
329 * Reproduce: CreateProcessW("\\??\\C:\\lspci.exe", ...)
331 if (((path[0] >= 'a' && path[0] <= 'z') ||
332 (path[0] >= 'A' && path[0] <= 'Z')) &&
333 strncmp(path+1, ":\\??\\", 5) == 0)
335 memmove(path, path+2, len-2);
341 * GetModuleFileName() has bugs. On Windows 10 it does not add "\\\\?\\"
342 * prefix when path is in native NT UNC namespace. Such path is treated by
343 * WinAPI/DOS functions as standard DOS path relative to the current
344 * directory, hence something completely different. So prepend missing
345 * "\\\\?\\" prefix to make path valid again.
346 * Reproduce: CreateProcessW("\\??\\UNC\\10.0.2.4\\qemu\\lspci.exe", ...)
348 * If path starts with DOS drive letter and with appended PCI_IDS is
349 * longer than 260 bytes and is without "\\\\?\\" prefix then append it.
350 * This prefix is required for paths and file names with DOS drive letter
351 * longer than 260 bytes.
353 if (strncmp(path, "\\UNC\\", 5) == 0 ||
354 strncmp(path, "UNC\\", 4) == 0 ||
355 (((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z')) &&
356 len + sizeof(PCI_IDS) >= 260))
358 memmove(path+4, path, len);
359 memcpy(path, "\\\\?\\", 4);
364 #elif defined(PCI_OS_DJGPP) || defined(PCI_OS_WINDOWS)
366 const char *exe_path;
369 exe_path = __dos_argv0;
374 len = strlen(exe_path);
375 path = pci_malloc(a, len+sizeof(PCI_IDS));
376 memcpy(path, exe_path, len+1);
380 sep = strrchr(path, '\\');
384 * If current module path (current executable for static builds or
385 * current DLL library for shared build) cannot be determined then
386 * fallback to the current directory.
389 pci_set_name_list_path(a, PCI_IDS, 0);
393 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS));
394 pci_set_name_list_path(a, path, 1);
402 pci_init_name_list_path(struct pci_access *a)
404 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
412 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access));
415 memset(a, 0, sizeof(*a));
416 pci_init_name_list_path(a);
418 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
419 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
420 a->id_lookup_mode = PCI_LOOKUP_CACHE;
423 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
425 for (i=0; i<PCI_ACCESS_MAX; i++)
426 if (pci_methods[i] && pci_methods[i]->config)
427 pci_methods[i]->config(a);
432 pci_init_internal(struct pci_access *a, int skip_method)
435 a->error = pci_generic_error;
437 a->warning = pci_generic_warn;
439 a->debug = pci_generic_debug;
441 a->debug = pci_null_debug;
443 if (a->method != PCI_ACCESS_AUTO)
445 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
446 a->error("This access method is not supported.");
447 a->methods = pci_methods[a->method];
452 for (i=0; probe_sequence[i] >= 0; i++)
454 struct pci_methods *m = pci_methods[probe_sequence[i]];
457 if (skip_method == probe_sequence[i])
459 a->debug("Trying method %s...", m->name);
464 a->method = probe_sequence[i];
467 a->debug("...No.\n");
472 a->debug("Decided to use %s\n", a->methods->name);
478 pci_init_v35(struct pci_access *a)
480 if (!pci_init_internal(a, -1))
481 a->error("Cannot find any working access method.");
484 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
485 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
486 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
487 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
490 pci_clone_access(struct pci_access *a)
492 struct pci_access *b = pci_alloc();
494 b->writeable = a->writeable;
495 b->buscentric = a->buscentric;
496 b->debugging = a->debugging;
498 b->warning = a->warning;
505 pci_cleanup(struct pci_access *a)
507 struct pci_dev *d, *e;
509 for (d=a->devices; d; d=e)
515 a->methods->cleanup(a);
516 pci_free_name_list(a);
518 pci_set_name_list_path(a, NULL, 0);