2 * The PCI Library -- Initialization and related things
4 * Copyright (c) 1997--2024 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)
161 #if defined(PCI_HAVE_PM_AOS_EXPANSION)
168 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
169 static int probe_sequence[] = {
170 // System-specific methods
171 PCI_ACCESS_SYS_BUS_PCI,
172 PCI_ACCESS_PROC_BUS_PCI,
173 PCI_ACCESS_FBSD_DEVICE,
174 PCI_ACCESS_AIX_DEVICE,
175 PCI_ACCESS_NBSD_LIBPCI,
176 PCI_ACCESS_OBSD_DEVICE,
178 PCI_ACCESS_SYLIXOS_DEVICE,
180 PCI_ACCESS_WIN32_CFGMGR32,
181 PCI_ACCESS_WIN32_KLDBG,
182 PCI_ACCESS_WIN32_SYSDBG,
183 PCI_ACCESS_AOS_EXPANSION,
184 // Low-level methods poking the hardware directly
186 PCI_ACCESS_I386_TYPE1,
187 PCI_ACCESS_I386_TYPE2,
188 PCI_ACCESS_MMIO_TYPE1_EXT,
189 PCI_ACCESS_MMIO_TYPE1,
193 static void PCI_NONRET
194 pci_generic_error(char *msg, ...)
199 fputs("pcilib: ", stderr);
200 vfprintf(stderr, msg, args);
207 pci_generic_warn(char *msg, ...)
212 fputs("pcilib: ", stderr);
213 vfprintf(stderr, msg, args);
219 pci_generic_debug(char *msg, ...)
224 vfprintf(stdout, msg, args);
229 pci_null_debug(char *msg UNUSED, ...)
233 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL
236 pci_malloc(struct pci_access *a, int size)
238 void *x = malloc(size);
241 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size);
253 pci_strdup(struct pci_access *a, const char *s)
255 int len = strlen(s) + 1;
256 char *t = pci_malloc(a, len);
262 pci_lookup_method(char *name)
266 for (i=0; i<PCI_ACCESS_MAX; i++)
267 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
273 pci_get_method_name(int index)
275 if (index < 0 || index >= PCI_ACCESS_MAX)
277 else if (!pci_methods[index])
280 return pci_methods[index]->name;
283 #if defined(PCI_OS_WINDOWS) || defined(PCI_OS_DJGPP)
286 pci_init_name_list_path(struct pci_access *a)
288 if ((PCI_PATH_IDS_DIR)[0])
289 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0);
295 #if defined(PCI_OS_WINDOWS) && (defined(_WIN32) || defined(_WINDLL) || defined(_WINDOWS))
301 module = (HINSTANCE)&__ImageBase;
302 #elif defined(_WINDLL)
304 #elif defined(_WINDOWS)
309 * Module file name can have arbitrary length despite all MS examples say
310 * about MAX_PATH upper limit. This limit does not apply for example when
311 * executable is running from network disk with very long UNC paths or
312 * when using "\\??\\" prefix for specifying executable binary path.
313 * Function GetModuleFileName() returns passed size argument when passed
314 * buffer is too small and does not signal any error. In this case retry
315 * again with larger buffer.
317 size = 256; /* initial buffer size (more than sizeof(PCI_IDS)-4) */
319 path = pci_malloc(a, size);
320 len = GetModuleFileName(module, path, size-sizeof(PCI_IDS)-4); /* 4 for "\\\\?\\" */
321 if (len >= size-sizeof(PCI_IDS)-4)
331 * GetModuleFileName() has bugs. On Windows 10 it prepends current drive
332 * letter if path is just pure NT namespace (with "\\??\\" prefix). Such
333 * extra drive letter makes path fully invalid and unusable. So remove
334 * extra drive letter to make path valid again.
335 * Reproduce: CreateProcessW("\\??\\C:\\lspci.exe", ...)
337 if (((path[0] >= 'a' && path[0] <= 'z') ||
338 (path[0] >= 'A' && path[0] <= 'Z')) &&
339 strncmp(path+1, ":\\??\\", 5) == 0)
341 memmove(path, path+2, len-2);
347 * GetModuleFileName() has bugs. On Windows 10 it does not add "\\\\?\\"
348 * prefix when path is in native NT UNC namespace. Such path is treated by
349 * WinAPI/DOS functions as standard DOS path relative to the current
350 * directory, hence something completely different. So prepend missing
351 * "\\\\?\\" prefix to make path valid again.
352 * Reproduce: CreateProcessW("\\??\\UNC\\10.0.2.4\\qemu\\lspci.exe", ...)
354 * If path starts with DOS drive letter and with appended PCI_IDS is
355 * longer than 260 bytes and is without "\\\\?\\" prefix then append it.
356 * This prefix is required for paths and file names with DOS drive letter
357 * longer than 260 bytes.
359 if (strncmp(path, "\\UNC\\", 5) == 0 ||
360 strncmp(path, "UNC\\", 4) == 0 ||
361 (((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z')) &&
362 len + sizeof(PCI_IDS) >= 260))
364 memmove(path+4, path, len);
365 memcpy(path, "\\\\?\\", 4);
370 #elif defined(PCI_OS_DJGPP) || defined(PCI_OS_WINDOWS)
372 const char *exe_path;
375 exe_path = __dos_argv0;
380 len = strlen(exe_path);
381 path = pci_malloc(a, len+sizeof(PCI_IDS));
382 memcpy(path, exe_path, len+1);
386 sep = strrchr(path, '\\');
390 * If current module path (current executable for static builds or
391 * current DLL library for shared build) cannot be determined then
392 * fallback to the current directory.
395 pci_set_name_list_path(a, PCI_IDS, 0);
399 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS));
400 pci_set_name_list_path(a, path, 1);
405 #elif defined PCI_OS_AMIGAOS
408 pci_init_name_list_path(struct pci_access *a)
410 int len = strlen(PCI_PATH_IDS_DIR);
413 pci_set_name_list_path(a, PCI_IDS, 0);
416 char last_char = PCI_PATH_IDS_DIR[len - 1];
417 if (last_char == ':' || last_char == '/') // root or parent char
418 pci_set_name_list_path(a, PCI_PATH_IDS_DIR PCI_IDS, 0);
420 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
427 pci_init_name_list_path(struct pci_access *a)
429 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
437 pci_init_dns(struct pci_access *a)
439 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
440 a->id_lookup_mode = PCI_LOOKUP_CACHE;
442 char *cache_dir = getenv("XDG_CACHE_HOME");
444 cache_dir = "~/.cache";
446 int name_len = strlen(cache_dir) + 32;
447 char *cache_name = pci_malloc(NULL, name_len);
448 snprintf(cache_name, name_len, "%s/pci-ids", cache_dir);
449 struct pci_param *param = pci_define_param(a, "net.cache_name", cache_name, "Name of the ID cache file");
450 param->value_malloced = 1;
458 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access));
461 memset(a, 0, sizeof(*a));
462 pci_init_name_list_path(a);
467 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
469 for (i=0; i<PCI_ACCESS_MAX; i++)
470 if (pci_methods[i] && pci_methods[i]->config)
471 pci_methods[i]->config(a);
476 pci_init_internal(struct pci_access *a, int skip_method)
479 a->error = pci_generic_error;
481 a->warning = pci_generic_warn;
483 a->debug = pci_generic_debug;
485 a->debug = pci_null_debug;
487 if (a->method != PCI_ACCESS_AUTO)
489 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
490 a->error("This access method is not supported.");
491 a->methods = pci_methods[a->method];
496 for (i=0; probe_sequence[i] >= 0; i++)
498 struct pci_methods *m = pci_methods[probe_sequence[i]];
501 if (skip_method == probe_sequence[i])
503 a->debug("Trying method %s...", m->name);
508 a->method = probe_sequence[i];
511 a->debug("...No.\n");
516 a->debug("Decided to use %s\n", a->methods->name);
522 pci_init_v35(struct pci_access *a)
524 if (!pci_init_internal(a, -1))
525 a->error("Cannot find any working access method.");
528 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
529 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
530 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
531 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
534 pci_clone_access(struct pci_access *a)
536 struct pci_access *b = pci_alloc();
538 b->writeable = a->writeable;
539 b->buscentric = a->buscentric;
540 b->debugging = a->debugging;
542 b->warning = a->warning;
549 pci_cleanup(struct pci_access *a)
551 struct pci_dev *d, *e;
553 for (d=a->devices; d; d=e)
559 a->methods->cleanup(a);
560 pci_free_name_list(a);
562 pci_set_name_list_path(a, NULL, 0);