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.
17 #include <crt0.h> /* for __dos_argv0 */
24 /* Force usage of ANSI (char*) variant of GetModuleFileName() function */
26 #ifdef GetModuleFileName
27 #undef GetModuleFileName
29 #define GetModuleFileName GetModuleFileNameA
32 /* Define __ImageBase for all linkers */
34 /* GNU LD provides __ImageBase symbol since 2.19, in previous versions it is
35 * under name _image_base__, so add weak alias for compatibility. */
37 asm(".weak\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "\n\t"
38 ".set\t" PCI_STRINGIFY(__MINGW_USYMBOL(__ImageBase)) "," PCI_STRINGIFY(__MINGW_USYMBOL(_image_base__)));
41 * MSVC link.exe provides __ImageBase symbol since 12.00 (MSVC 6.0), for
42 * previous versions resolve it at runtime via GetModuleHandleA() which
43 * returns base for main executable or via VirtualQuery() for DLL builds.
45 #if defined(_MSC_VER) && _MSC_VER < 1200
47 get_current_module_handle(void)
50 MEMORY_BASIC_INFORMATION info;
51 size_t len = VirtualQuery(&get_current_module_handle, &info, sizeof(info));
52 if (len != sizeof(info))
54 return (HMODULE)info.AllocationBase;
56 return GetModuleHandleA(NULL);
59 #define __ImageBase (*(IMAGE_DOS_HEADER *)get_current_module_handle())
61 extern IMAGE_DOS_HEADER __ImageBase;
66 extern HINSTANCE _hModule;
67 #elif defined(_WINDOWS)
68 extern HINSTANCE _hInstance;
73 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
75 #ifdef PCI_HAVE_PM_LINUX_SYSFS
80 #ifdef PCI_HAVE_PM_LINUX_PROC
85 #ifdef PCI_HAVE_PM_INTEL_CONF
92 #ifdef PCI_HAVE_PM_FBSD_DEVICE
97 #ifdef PCI_HAVE_PM_AIX_DEVICE
102 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
107 #ifdef PCI_HAVE_PM_OBSD_DEVICE
112 #ifdef PCI_HAVE_PM_DUMP
117 #ifdef PCI_HAVE_PM_DARWIN_DEVICE
122 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE
127 #ifdef PCI_HAVE_PM_HURD_CONF
132 #ifdef PCI_HAVE_PM_WIN32_CFGMGR32
137 #ifdef PCI_HAVE_PM_WIN32_KLDBG
142 #ifdef PCI_HAVE_PM_WIN32_SYSDBG
147 #ifdef PCI_HAVE_PM_MMIO_CONF
156 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
157 static int probe_sequence[] = {
158 // System-specific methods
159 PCI_ACCESS_SYS_BUS_PCI,
160 PCI_ACCESS_PROC_BUS_PCI,
161 PCI_ACCESS_FBSD_DEVICE,
162 PCI_ACCESS_AIX_DEVICE,
163 PCI_ACCESS_NBSD_LIBPCI,
164 PCI_ACCESS_OBSD_DEVICE,
166 PCI_ACCESS_SYLIXOS_DEVICE,
168 PCI_ACCESS_WIN32_CFGMGR32,
169 PCI_ACCESS_WIN32_KLDBG,
170 PCI_ACCESS_WIN32_SYSDBG,
171 // Low-level methods poking the hardware directly
172 PCI_ACCESS_I386_TYPE1,
173 PCI_ACCESS_I386_TYPE2,
174 PCI_ACCESS_MMIO_TYPE1_EXT,
175 PCI_ACCESS_MMIO_TYPE1,
179 static void PCI_NONRET
180 pci_generic_error(char *msg, ...)
185 fputs("pcilib: ", stderr);
186 vfprintf(stderr, msg, args);
193 pci_generic_warn(char *msg, ...)
198 fputs("pcilib: ", stderr);
199 vfprintf(stderr, msg, args);
205 pci_generic_debug(char *msg, ...)
210 vfprintf(stdout, msg, args);
215 pci_null_debug(char *msg UNUSED, ...)
219 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL
222 pci_malloc(struct pci_access *a, int size)
224 void *x = malloc(size);
227 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size);
239 pci_strdup(struct pci_access *a, const char *s)
241 int len = strlen(s) + 1;
242 char *t = pci_malloc(a, len);
248 pci_lookup_method(char *name)
252 for (i=0; i<PCI_ACCESS_MAX; i++)
253 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
259 pci_get_method_name(int index)
261 if (index < 0 || index >= PCI_ACCESS_MAX)
263 else if (!pci_methods[index])
266 return pci_methods[index]->name;
269 #if defined(PCI_OS_WINDOWS) || defined(PCI_OS_DJGPP)
272 pci_init_name_list_path(struct pci_access *a)
274 if ((PCI_PATH_IDS_DIR)[0])
275 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0);
281 #if defined(PCI_OS_WINDOWS) && (defined(_WIN32) || defined(_WINDLL) || defined(_WINDOWS))
287 module = (HINSTANCE)&__ImageBase;
288 #elif defined(_WINDLL)
290 #elif defined(_WINDOWS)
295 * Module file name can have arbitrary length despite all MS examples say
296 * about MAX_PATH upper limit. This limit does not apply for example when
297 * executable is running from network disk with very long UNC paths or
298 * when using "\\??\\" prefix for specifying executable binary path.
299 * Function GetModuleFileName() returns passed size argument when passed
300 * buffer is too small and does not signal any error. In this case retry
301 * again with larger buffer.
303 size = 256; /* initial buffer size (more than sizeof(PCI_IDS)-4) */
305 path = pci_malloc(a, size);
306 len = GetModuleFileName(module, path, size-sizeof(PCI_IDS)-4); /* 4 for "\\\\?\\" */
307 if (len >= size-sizeof(PCI_IDS)-4)
317 * GetModuleFileName() has bugs. On Windows 10 it prepends current drive
318 * letter if path is just pure NT namespace (with "\\??\\" prefix). Such
319 * extra drive letter makes path fully invalid and unusable. So remove
320 * extra drive letter to make path valid again.
321 * Reproduce: CreateProcessW("\\??\\C:\\lspci.exe", ...)
323 if (((path[0] >= 'a' && path[0] <= 'z') ||
324 (path[0] >= 'A' && path[0] <= 'Z')) &&
325 strncmp(path+1, ":\\??\\", 5) == 0)
327 memmove(path, path+2, len-2);
333 * GetModuleFileName() has bugs. On Windows 10 it does not add "\\\\?\\"
334 * prefix when path is in native NT UNC namespace. Such path is treated by
335 * WinAPI/DOS functions as standard DOS path relative to the current
336 * directory, hence something completely different. So prepend missing
337 * "\\\\?\\" prefix to make path valid again.
338 * Reproduce: CreateProcessW("\\??\\UNC\\10.0.2.4\\qemu\\lspci.exe", ...)
340 * If path starts with DOS drive letter and with appended PCI_IDS is
341 * longer than 260 bytes and is without "\\\\?\\" prefix then append it.
342 * This prefix is required for paths and file names with DOS drive letter
343 * longer than 260 bytes.
345 if (strncmp(path, "\\UNC\\", 5) == 0 ||
346 strncmp(path, "UNC\\", 4) == 0 ||
347 (((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z')) &&
348 len + sizeof(PCI_IDS) >= 260))
350 memmove(path+4, path, len);
351 memcpy(path, "\\\\?\\", 4);
356 #elif defined(PCI_OS_DJGPP) || defined(PCI_OS_WINDOWS)
358 const char *exe_path;
361 exe_path = __dos_argv0;
366 len = strlen(exe_path);
367 path = pci_malloc(a, len+sizeof(PCI_IDS));
368 memcpy(path, exe_path, len+1);
372 sep = strrchr(path, '\\');
376 * If current module path (current executable for static builds or
377 * current DLL library for shared build) cannot be determined then
378 * fallback to the current directory.
381 pci_set_name_list_path(a, PCI_IDS, 0);
385 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS));
386 pci_set_name_list_path(a, path, 1);
394 pci_init_name_list_path(struct pci_access *a)
396 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
404 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access));
407 memset(a, 0, sizeof(*a));
408 pci_init_name_list_path(a);
410 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
411 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
412 a->id_lookup_mode = PCI_LOOKUP_CACHE;
415 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
417 for (i=0; i<PCI_ACCESS_MAX; i++)
418 if (pci_methods[i] && pci_methods[i]->config)
419 pci_methods[i]->config(a);
424 pci_init_v35(struct pci_access *a)
427 a->error = pci_generic_error;
429 a->warning = pci_generic_warn;
431 a->debug = pci_generic_debug;
433 a->debug = pci_null_debug;
437 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
438 a->error("This access method is not supported.");
439 a->methods = pci_methods[a->method];
444 for (i=0; probe_sequence[i] >= 0; i++)
446 struct pci_methods *m = pci_methods[probe_sequence[i]];
449 a->debug("Trying method %s...", m->name);
454 a->method = probe_sequence[i];
457 a->debug("...No.\n");
460 a->error("Cannot find any working access method.");
462 a->debug("Decided to use %s\n", a->methods->name);
466 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
467 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
468 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
469 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
472 pci_cleanup(struct pci_access *a)
474 struct pci_dev *d, *e;
476 for (d=a->devices; d; d=e)
482 a->methods->cleanup(a);
483 pci_free_name_list(a);
485 pci_set_name_list_path(a, NULL, 0);