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
154 #if defined(PCI_HAVE_PM_ECAM)
161 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
162 static int probe_sequence[] = {
163 // System-specific methods
164 PCI_ACCESS_SYS_BUS_PCI,
165 PCI_ACCESS_PROC_BUS_PCI,
166 PCI_ACCESS_FBSD_DEVICE,
167 PCI_ACCESS_AIX_DEVICE,
168 PCI_ACCESS_NBSD_LIBPCI,
169 PCI_ACCESS_OBSD_DEVICE,
171 PCI_ACCESS_SYLIXOS_DEVICE,
173 PCI_ACCESS_WIN32_CFGMGR32,
174 PCI_ACCESS_WIN32_KLDBG,
175 PCI_ACCESS_WIN32_SYSDBG,
176 // Low-level methods poking the hardware directly
178 PCI_ACCESS_I386_TYPE1,
179 PCI_ACCESS_I386_TYPE2,
180 PCI_ACCESS_MMIO_TYPE1_EXT,
181 PCI_ACCESS_MMIO_TYPE1,
185 static void PCI_NONRET
186 pci_generic_error(char *msg, ...)
191 fputs("pcilib: ", stderr);
192 vfprintf(stderr, msg, args);
199 pci_generic_warn(char *msg, ...)
204 fputs("pcilib: ", stderr);
205 vfprintf(stderr, msg, args);
211 pci_generic_debug(char *msg, ...)
216 vfprintf(stdout, msg, args);
221 pci_null_debug(char *msg UNUSED, ...)
225 // Memory allocation functions are safe to call if pci_access is not fully initalized or even NULL
228 pci_malloc(struct pci_access *a, int size)
230 void *x = malloc(size);
233 (a && a->error ? a->error : pci_generic_error)("Out of memory (allocation of %d bytes failed)", size);
245 pci_strdup(struct pci_access *a, const char *s)
247 int len = strlen(s) + 1;
248 char *t = pci_malloc(a, len);
254 pci_lookup_method(char *name)
258 for (i=0; i<PCI_ACCESS_MAX; i++)
259 if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
265 pci_get_method_name(int index)
267 if (index < 0 || index >= PCI_ACCESS_MAX)
269 else if (!pci_methods[index])
272 return pci_methods[index]->name;
275 #if defined(PCI_OS_WINDOWS) || defined(PCI_OS_DJGPP)
278 pci_init_name_list_path(struct pci_access *a)
280 if ((PCI_PATH_IDS_DIR)[0])
281 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0);
287 #if defined(PCI_OS_WINDOWS) && (defined(_WIN32) || defined(_WINDLL) || defined(_WINDOWS))
293 module = (HINSTANCE)&__ImageBase;
294 #elif defined(_WINDLL)
296 #elif defined(_WINDOWS)
301 * Module file name can have arbitrary length despite all MS examples say
302 * about MAX_PATH upper limit. This limit does not apply for example when
303 * executable is running from network disk with very long UNC paths or
304 * when using "\\??\\" prefix for specifying executable binary path.
305 * Function GetModuleFileName() returns passed size argument when passed
306 * buffer is too small and does not signal any error. In this case retry
307 * again with larger buffer.
309 size = 256; /* initial buffer size (more than sizeof(PCI_IDS)-4) */
311 path = pci_malloc(a, size);
312 len = GetModuleFileName(module, path, size-sizeof(PCI_IDS)-4); /* 4 for "\\\\?\\" */
313 if (len >= size-sizeof(PCI_IDS)-4)
323 * GetModuleFileName() has bugs. On Windows 10 it prepends current drive
324 * letter if path is just pure NT namespace (with "\\??\\" prefix). Such
325 * extra drive letter makes path fully invalid and unusable. So remove
326 * extra drive letter to make path valid again.
327 * Reproduce: CreateProcessW("\\??\\C:\\lspci.exe", ...)
329 if (((path[0] >= 'a' && path[0] <= 'z') ||
330 (path[0] >= 'A' && path[0] <= 'Z')) &&
331 strncmp(path+1, ":\\??\\", 5) == 0)
333 memmove(path, path+2, len-2);
339 * GetModuleFileName() has bugs. On Windows 10 it does not add "\\\\?\\"
340 * prefix when path is in native NT UNC namespace. Such path is treated by
341 * WinAPI/DOS functions as standard DOS path relative to the current
342 * directory, hence something completely different. So prepend missing
343 * "\\\\?\\" prefix to make path valid again.
344 * Reproduce: CreateProcessW("\\??\\UNC\\10.0.2.4\\qemu\\lspci.exe", ...)
346 * If path starts with DOS drive letter and with appended PCI_IDS is
347 * longer than 260 bytes and is without "\\\\?\\" prefix then append it.
348 * This prefix is required for paths and file names with DOS drive letter
349 * longer than 260 bytes.
351 if (strncmp(path, "\\UNC\\", 5) == 0 ||
352 strncmp(path, "UNC\\", 4) == 0 ||
353 (((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z')) &&
354 len + sizeof(PCI_IDS) >= 260))
356 memmove(path+4, path, len);
357 memcpy(path, "\\\\?\\", 4);
362 #elif defined(PCI_OS_DJGPP) || defined(PCI_OS_WINDOWS)
364 const char *exe_path;
367 exe_path = __dos_argv0;
372 len = strlen(exe_path);
373 path = pci_malloc(a, len+sizeof(PCI_IDS));
374 memcpy(path, exe_path, len+1);
378 sep = strrchr(path, '\\');
382 * If current module path (current executable for static builds or
383 * current DLL library for shared build) cannot be determined then
384 * fallback to the current directory.
387 pci_set_name_list_path(a, PCI_IDS, 0);
391 memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS));
392 pci_set_name_list_path(a, path, 1);
400 pci_init_name_list_path(struct pci_access *a)
402 pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
410 struct pci_access *a = pci_malloc(NULL, sizeof(struct pci_access));
413 memset(a, 0, sizeof(*a));
414 pci_init_name_list_path(a);
416 pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
417 pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
418 a->id_lookup_mode = PCI_LOOKUP_CACHE;
421 pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
423 for (i=0; i<PCI_ACCESS_MAX; i++)
424 if (pci_methods[i] && pci_methods[i]->config)
425 pci_methods[i]->config(a);
430 pci_init_internal(struct pci_access *a, int skip_method)
433 a->error = pci_generic_error;
435 a->warning = pci_generic_warn;
437 a->debug = pci_generic_debug;
439 a->debug = pci_null_debug;
441 if (a->method != PCI_ACCESS_AUTO)
443 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
444 a->error("This access method is not supported.");
445 a->methods = pci_methods[a->method];
450 for (i=0; probe_sequence[i] >= 0; i++)
452 struct pci_methods *m = pci_methods[probe_sequence[i]];
455 if (skip_method == probe_sequence[i])
457 a->debug("Trying method %s...", m->name);
462 a->method = probe_sequence[i];
465 a->debug("...No.\n");
470 a->debug("Decided to use %s\n", a->methods->name);
476 pci_init_v35(struct pci_access *a)
478 if (!pci_init_internal(a, -1))
479 a->error("Cannot find any working access method.");
482 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
483 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
484 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
485 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
488 pci_clone_access(struct pci_access *a)
490 struct pci_access *b = pci_alloc();
492 b->writeable = a->writeable;
493 b->buscentric = a->buscentric;
494 b->debugging = a->debugging;
496 b->warning = a->warning;
503 pci_cleanup(struct pci_access *a)
505 struct pci_dev *d, *e;
507 for (d=a->devices; d; d=e)
513 a->methods->cleanup(a);
514 pci_free_name_list(a);
516 pci_set_name_list_path(a, NULL, 0);