Make GUsb optional for fuzzing

This commit is contained in:
Richard Hughes 2021-01-13 18:43:44 +00:00
parent d1015cc2a2
commit 1a3d3b338e
13 changed files with 129 additions and 41 deletions

View File

@ -9,7 +9,9 @@
#include "config.h"
#include <gio/gio.h>
#ifdef HAVE_GUSB
#include <gusb.h>
#endif
#include "fu-device-locker.h"
#include "fu-usb-device.h"
@ -86,6 +88,7 @@ fu_device_locker_close (FuDeviceLocker *self, GError **error)
if (!self->device_open)
return TRUE;
if (!self->close_func (self->device, &error_local)) {
#ifdef HAVE_GUSB
if (G_USB_IS_DEVICE (self->device) &&
g_error_matches (error_local,
G_USB_DEVICE_ERROR,
@ -96,6 +99,10 @@ fu_device_locker_close (FuDeviceLocker *self, GError **error)
g_propagate_error (error, g_steal_pointer (&error_local));
return FALSE;
}
#else
g_propagate_error (error, g_steal_pointer (&error_local));
return FALSE;
#endif
}
self->device_open = FALSE;
return TRUE;
@ -131,6 +138,7 @@ fu_device_locker_new (gpointer device, GError **error)
g_return_val_if_fail (device != NULL, NULL);
g_return_val_if_fail (error != NULL, NULL);
#ifdef HAVE_GUSB
/* GUsbDevice */
if (G_USB_IS_DEVICE (device)) {
return fu_device_locker_new_full (device,
@ -138,6 +146,7 @@ fu_device_locker_new (gpointer device, GError **error)
(FuDeviceLockerFunc) g_usb_device_close,
error);
}
#endif
/* FuDevice */
if (FU_IS_DEVICE (device)) {

View File

@ -82,6 +82,7 @@ fu_hid_device_open (FuUsbDevice *device, GError **error)
{
FuHidDevice *self = FU_HID_DEVICE (device);
FuHidDeviceClass *klass = FU_HID_DEVICE_GET_CLASS (device);
#ifdef HAVE_GUSB
FuHidDevicePrivate *priv = GET_PRIVATE (self);
GUsbDeviceClaimInterfaceFlags flags = 0;
GUsbDevice *usb_device = fu_usb_device_get_dev (device);
@ -117,6 +118,7 @@ fu_hid_device_open (FuUsbDevice *device, GError **error)
g_prefix_error (error, "failed to claim HID interface: ");
return FALSE;
}
#endif
/* subclassed */
if (klass->open != NULL) {
@ -133,10 +135,12 @@ fu_hid_device_close (FuUsbDevice *device, GError **error)
{
FuHidDevice *self = FU_HID_DEVICE (device);
FuHidDeviceClass *klass = FU_HID_DEVICE_GET_CLASS (device);
#ifdef HAVE_GUSB
FuHidDevicePrivate *priv = GET_PRIVATE (self);
GUsbDeviceClaimInterfaceFlags flags = 0;
GUsbDevice *usb_device = fu_usb_device_get_dev (device);
g_autoptr(GError) error_local = NULL;
#endif
/* subclassed */
if (klass->close != NULL) {
@ -144,6 +148,7 @@ fu_hid_device_close (FuUsbDevice *device, GError **error)
return FALSE;
}
#ifdef HAVE_GUSB
/* release */
if ((priv->flags & FU_HID_DEVICE_FLAG_NO_KERNEL_REBIND) == 0)
flags |= G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER;
@ -162,6 +167,7 @@ fu_hid_device_close (FuUsbDevice *device, GError **error)
"failed to release HID interface: ");
return FALSE;
}
#endif
/* success */
return TRUE;
@ -237,6 +243,7 @@ fu_hid_device_set_report_internal (FuHidDevice *self,
FuHidDeviceRetryHelper *helper,
GError **error)
{
#ifdef HAVE_GUSB
FuHidDevicePrivate *priv = GET_PRIVATE (self);
GUsbDevice *usb_device;
gsize actual_len = 0;
@ -273,6 +280,7 @@ fu_hid_device_set_report_internal (FuHidDevice *self,
actual_len, helper->bufsz);
return FALSE;
}
#endif
return TRUE;
}
@ -342,6 +350,7 @@ fu_hid_device_get_report_internal (FuHidDevice *self,
FuHidDeviceRetryHelper *helper,
GError **error)
{
#ifdef HAVE_GUSB
FuHidDevicePrivate *priv = GET_PRIVATE (self);
GUsbDevice *usb_device;
gsize actual_len = 0;
@ -384,6 +393,7 @@ fu_hid_device_get_report_internal (FuHidDevice *self,
actual_len, helper->bufsz);
return FALSE;
}
#endif
return TRUE;
}

View File

@ -7,7 +7,9 @@
#pragma once
#include <gio/gio.h>
#ifdef HAVE_GUSB
#include <gusb.h>
#endif
#include "fu-common.h"
#include "fu-common-guid.h"

View File

@ -83,6 +83,7 @@ fu_usb_device_finalize (GObject *object)
static void
fu_usb_device_init (FuUsbDevice *device)
{
#ifdef HAVE_GUSB
fu_device_retry_add_recovery (FU_DEVICE (device),
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NO_DEVICE,
@ -91,6 +92,7 @@ fu_usb_device_init (FuUsbDevice *device)
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_PERMISSION_DENIED,
NULL);
#endif
}
/**
@ -111,6 +113,7 @@ fu_usb_device_is_open (FuUsbDevice *device)
return priv->usb_device_locker != NULL;
}
#ifdef HAVE_GUSB
static gboolean
fu_usb_device_query_hub (FuUsbDevice *self, GError **error)
{
@ -153,6 +156,7 @@ fu_usb_device_query_hub (FuUsbDevice *self, GError **error)
}
return TRUE;
}
#endif
static gboolean
fu_usb_device_open (FuDevice *device, GError **error)
@ -160,8 +164,9 @@ fu_usb_device_open (FuDevice *device, GError **error)
FuUsbDevice *self = FU_USB_DEVICE (device);
FuUsbDevicePrivate *priv = GET_PRIVATE (self);
FuUsbDeviceClass *klass = FU_USB_DEVICE_GET_CLASS (device);
guint idx;
g_autoptr(FuDeviceLocker) locker = NULL;
#ifdef HAVE_GUSB
guint idx;
g_return_val_if_fail (FU_IS_USB_DEVICE (self), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
@ -258,6 +263,7 @@ fu_usb_device_open (FuDevice *device, GError **error)
if (!fu_usb_device_query_hub (self, error))
return FALSE;
}
#endif
/* subclassed */
if (klass->open != NULL) {
@ -299,6 +305,7 @@ fu_usb_device_probe (FuDevice *device, GError **error)
{
FuUsbDevice *self = FU_USB_DEVICE (device);
FuUsbDeviceClass *klass = FU_USB_DEVICE_GET_CLASS (device);
#ifdef HAVE_GUSB
FuUsbDevicePrivate *priv = GET_PRIVATE (self);
guint16 release;
g_autofree gchar *devid0 = NULL;
@ -361,6 +368,7 @@ fu_usb_device_probe (FuDevice *device, GError **error)
fu_device_add_instance_id_full (device, intid3,
FU_DEVICE_INSTANCE_FLAG_ONLY_QUIRKS);
}
#endif
/* subclassed */
if (klass->probe != NULL) {
@ -385,11 +393,15 @@ fu_usb_device_probe (FuDevice *device, GError **error)
guint16
fu_usb_device_get_vid (FuUsbDevice *self)
{
#ifdef HAVE_GUSB
FuUsbDevicePrivate *priv = GET_PRIVATE (self);
g_return_val_if_fail (FU_IS_USB_DEVICE (self), 0x0000);
if (priv->usb_device == NULL)
return 0x0;
return g_usb_device_get_vid (priv->usb_device);
#else
return 0x0;
#endif
}
/**
@ -405,11 +417,15 @@ fu_usb_device_get_vid (FuUsbDevice *self)
guint16
fu_usb_device_get_pid (FuUsbDevice *self)
{
#ifdef HAVE_GUSB
FuUsbDevicePrivate *priv = GET_PRIVATE (self);
g_return_val_if_fail (FU_IS_USB_DEVICE (self), 0x0000);
if (priv->usb_device == NULL)
return 0x0;
return g_usb_device_get_pid (priv->usb_device);
#else
return 0x0;
#endif
}
/**
@ -425,11 +441,15 @@ fu_usb_device_get_pid (FuUsbDevice *self)
const gchar *
fu_usb_device_get_platform_id (FuUsbDevice *self)
{
#ifdef HAVE_GUSB
FuUsbDevicePrivate *priv = GET_PRIVATE (self);
g_return_val_if_fail (FU_IS_USB_DEVICE (self), NULL);
if (priv->usb_device == NULL)
return NULL;
return g_usb_device_get_platform_id (priv->usb_device);
#else
return NULL;
#endif
}
/**
@ -482,9 +502,11 @@ fu_usb_device_set_dev (FuUsbDevice *device, GUsbDevice *usb_device)
return;
}
#ifdef HAVE_GUSB
/* set device ID automatically */
fu_device_set_physical_id (FU_DEVICE (device),
g_usb_device_get_platform_id (usb_device));
#endif
}
/**
@ -501,7 +523,7 @@ fu_usb_device_set_dev (FuUsbDevice *device, GUsbDevice *usb_device)
GUdevDevice *
fu_usb_device_find_udev_device (FuUsbDevice *device, GError **error)
{
#ifdef HAVE_GUDEV
#if defined(HAVE_GUDEV) && defined(HAVE_GUSB)
FuUsbDevicePrivate *priv = GET_PRIVATE (device);
g_autoptr(GList) devices = NULL;
g_autoptr(GUdevClient) gudev_client = g_udev_client_new (NULL);
@ -643,7 +665,11 @@ fu_usb_device_class_init (FuUsbDeviceClass *klass)
device_class->unbind_driver = fu_udev_device_unbind_driver;
pspec = g_param_spec_object ("usb-device", NULL, NULL,
#ifdef HAVE_GUSB
G_USB_TYPE_DEVICE,
#else
G_TYPE_OBJECT,
#endif
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_NAME);

View File

@ -7,7 +7,13 @@
#pragma once
#include <glib-object.h>
#ifdef HAVE_GUSB
#include <gusb.h>
#else
typedef GObject GUsbContext;
typedef GObject GUsbDevice;
#define G_USB_CHECK_VERSION(a,c,b) 0
#endif
#include "fu-plugin.h"
#include "fu-udev-device.h"

View File

@ -92,9 +92,12 @@ introspection_deps = [
libxmlb,
libjcat,
giounix,
gusb,
]
if get_option('gusb')
introspection_deps += gusb
endif
if get_option('gudev')
fwupdplugin_headers_private += 'fu-udev-device-private.h'
introspection_deps += gudev
@ -160,7 +163,7 @@ fwupdplugin_pkgg.generate(
description : 'library for plugins to use to interact with fwupd daemon',
)
if get_option('introspection')
if get_option('introspection') and get_option('gusb')
gir_dep = declare_dependency(sources: fwupd_gir)
if gusb.type_name() == 'internal'
libgusb_girtarget = subproject('gusb').get_variable('libgusb_girtarget')[0]

View File

@ -212,7 +212,10 @@ else
gudev = dependency('', required : false)
endif
libxmlb = dependency('xmlb', version : '>= 0.1.13', fallback : ['libxmlb', 'libxmlb_dep'])
gusb = dependency('gusb', version : '>= 0.3.5', fallback : ['gusb', 'gusb_dep'])
if get_option('gusb')
gusb = dependency('gusb', version : '>= 0.3.5', fallback : ['gusb', 'gusb_dep'])
conf.set('HAVE_GUSB', '1')
endif
sqlite = dependency('sqlite3')
if get_option('libarchive')
libarchive = dependency('libarchive')
@ -462,6 +465,11 @@ endif
conf.set_quoted('FWUPD_PLUGINDIR', plugin_dir)
endif
# sanity check, otherwise there is not point building
if host_machine.system() == 'windows' and not get_option('gusb')
error('-Dgusb=true is required for Windows build')
endif
conf.set_quoted('GETTEXT_PACKAGE', meson.project_name())
conf.set_quoted('PACKAGE_NAME', meson.project_name())
conf.set_quoted('VERSION', meson.project_version())
@ -482,11 +490,14 @@ if build_standalone
plugin_deps += gio
plugin_deps += giounix
plugin_deps += gmodule
plugin_deps += gusb
plugin_deps += gudev
plugin_deps += libjsonglib
endif
if get_option('gusb')
plugin_deps += gusb
endif
if get_option('libarchive')
plugin_deps += libarchive
endif

View File

@ -8,6 +8,7 @@ option('lvfs', type : 'boolean', value : true, description : 'enable LVFS remote
option('man', type : 'boolean', value : true, description : 'enable man pages')
option('libarchive', type : 'boolean', value : true, description : 'enable libarchive support')
option('gudev', type : 'boolean', value : true, description : 'enable GUdev support')
option('gusb', type : 'boolean', value : true, description : 'enable GUsb support')
option('polkit', type: 'boolean', value : true, description : 'enable PolKit support in daemon')
option('plugin_altos', type : 'boolean', value : true, description : 'enable altos support')
option('plugin_amt', type : 'boolean', value : true, description : 'enable Intel AMT support')

View File

@ -1,37 +1,19 @@
subdir('acpi-dmar')
subdir('acpi-facp')
subdir('bcm57xx')
subdir('ccgx')
subdir('cros-ec')
subdir('cpu')
subdir('dfu')
subdir('colorhug')
subdir('ebitdo')
subdir('ep963x')
subdir('fastboot')
subdir('fresco-pd')
subdir('hailuck')
subdir('iommu')
subdir('jabra')
subdir('linux-lockdown')
subdir('linux-sleep')
subdir('linux-swap')
subdir('linux-tainted')
subdir('steelseries')
subdir('dell-dock')
subdir('nitrokey')
subdir('pci-bcr')
subdir('pci-mei')
subdir('rts54hid')
subdir('rts54hub')
subdir('solokey')
subdir('synaptics-cxaudio')
subdir('synaptics-prometheus')
subdir('test')
subdir('upower')
subdir('wacom-usb')
subdir('vli')
subdir('goodix-moc')
subdir('pixart-rf')
if get_option('plugin_msr')
@ -41,7 +23,6 @@ endif
if get_option('gudev')
subdir('ata')
subdir('elantp')
subdir('logitech-hidpp')
subdir('optionrom')
subdir('superio')
subdir('synaptics-rmi')
@ -49,12 +30,37 @@ subdir('thelio-io')
subdir('wacom-raw')
endif
if get_option('gudev') and get_option('gusb')
subdir('logitech-hidpp')
endif
if get_option('systemd')
subdir('logind')
endif
if get_option('gusb')
subdir('ccgx')
subdir('colorhug')
subdir('cros-ec')
subdir('dfu')
subdir('ebitdo')
subdir('fastboot')
subdir('fresco-pd')
subdir('goodix-moc')
subdir('hailuck')
subdir('jabra')
subdir('rts54hid')
subdir('rts54hub')
subdir('solokey')
subdir('steelseries')
subdir('synaptics-cxaudio')
subdir('synaptics-prometheus')
subdir('vli')
subdir('wacom-usb')
# depends on dfu
subdir('csr')
endif
if get_option('tpm')
if not get_option('gudev')
@ -83,8 +89,8 @@ subdir('modem-manager')
endif
if get_option('plugin_altos')
if not get_option('gudev')
error('gudev is required for plugin_altos')
if not get_option('gudev') or not get_option('gusb')
error('gudev and gusb is required for plugin_altos')
endif
subdir('altos')
endif

View File

@ -63,10 +63,7 @@ fwupdate = executable(
fwupdplugin_incdir,
],
dependencies : [
libxmlb,
giounix,
gusb,
gudev,
plugin_deps,
efiboot,
tpm2tss,
],

View File

@ -6065,6 +6065,7 @@ fu_engine_get_archive_size_max (FuEngine *self)
return fu_config_get_archive_size_max (self->config);
}
#ifdef HAVE_GUSB
static void
fu_engine_usb_device_removed_cb (GUsbContext *ctx,
GUsbDevice *usb_device,
@ -6145,6 +6146,7 @@ fu_engine_usb_device_added_cb (GUsbContext *ctx,
}
}
}
#endif
static void
fu_engine_load_quirks (FuEngine *self, FuQuirksLoadFlags quirks_flags)
@ -6449,7 +6451,11 @@ fu_engine_load (FuEngine *self, FuEngineLoadFlags flags, GError **error)
fu_engine_add_firmware_gtype (self, "smbios", FU_TYPE_SMBIOS);
/* set shared USB context */
#ifdef HAVE_GUSB
self->usb_ctx = g_usb_context_new (error);
#else
self->usb_ctx = g_object_new (G_TYPE_OBJECT, NULL);
#endif
if (self->usb_ctx == NULL) {
g_prefix_error (error, "Failed to get USB context: ");
return FALSE;
@ -6500,6 +6506,7 @@ fu_engine_load (FuEngine *self, FuEngineLoadFlags flags, GError **error)
fu_engine_plugins_coldplug (self, FALSE);
/* coldplug USB devices */
#ifdef HAVE_GUSB
g_signal_connect (self->usb_ctx, "device-added",
G_CALLBACK (fu_engine_usb_device_added_cb),
self);
@ -6508,6 +6515,7 @@ fu_engine_load (FuEngine *self, FuEngineLoadFlags flags, GError **error)
self);
if (flags & FU_ENGINE_LOAD_FLAG_COLDPLUG)
g_usb_context_enumerate (self->usb_ctx);
#endif
#ifdef HAVE_GUDEV
/* coldplug udev devices */
@ -6668,13 +6676,14 @@ fu_engine_init (FuEngine *self)
g_hash_table_insert (self->compile_versions,
g_strdup ("org.freedesktop.fwupd"),
g_strdup (VERSION));
#ifdef HAVE_GUSB
g_hash_table_insert (self->compile_versions,
g_strdup ("org.freedesktop.gusb"),
g_strdup_printf ("%i.%i.%i",
G_USB_MAJOR_VERSION,
G_USB_MINOR_VERSION,
G_USB_MICRO_VERSION));
#endif
}
static void

View File

@ -10,7 +10,9 @@
#include <stdio.h>
#include <glib/gi18n.h>
#ifdef HAVE_GUSB
#include <gusb.h>
#endif
#include <xmlb.h>
#include <fwupd.h>
#ifdef HAVE_LIBCURL
@ -286,11 +288,13 @@ fu_util_get_versions (void)
g_string_append_printf (string, "client version:\t%s\n", SOURCE_VERSION);
g_string_append_printf (string,
"compile-time dependency versions\n");
#ifdef HAVE_GUSB
g_string_append_printf (string,
"\tgusb:\t%d.%d.%d\n",
G_USB_MAJOR_VERSION,
G_USB_MINOR_VERSION,
G_USB_MICRO_VERSION);
#endif
#ifdef EFIVAR_LIBRARY_VERSION
g_string_append_printf (string,
"\tefivar:\t%s",

View File

@ -13,11 +13,19 @@ daemon_dep = [
giounix,
gmodule,
gudev,
gusb,
sqlite,
libjsonglib,
]
client_dep = [
gudev,
]
if get_option('gusb')
daemon_dep += gusb
client_dep += gusb
endif
if get_option('libarchive')
daemon_dep += libarchive
endif
@ -66,8 +74,7 @@ fwupdmgr = executable(
dependencies : [
libfwupd_deps,
libxmlb,
gudev,
gusb,
client_dep,
sqlite,
],
link_with : [
@ -96,8 +103,7 @@ fwupdagent = executable(
dependencies : [
libfwupd_deps,
libxmlb,
gudev,
gusb,
client_dep,
],
link_with : [
fwupd,
@ -125,8 +131,7 @@ fwupdoffline = executable(
],
dependencies : [
libfwupd_deps,
gudev,
gusb,
client_dep,
libxmlb,
sqlite,
],
@ -166,8 +171,7 @@ fwupdtool = executable(
libfwupd_deps,
libxmlb,
libgcab,
gudev,
gusb,
client_dep,
sqlite,
valgrind,
],