static void fu_provider_chug_get_firmware_version (FuProviderChugItem *item) { FuProviderChugPrivate *priv = GET_PRIVATE (item->provider_chug); guint16 major; guint16 micro; guint16 minor; guint8 idx; g_autoptr(GError) error = NULL; g_autofree gchar *version = NULL; /* try to get the version without claiming interface */ if (!g_usb_device_open (item->usb_device, &error)) { g_debug ("Failed to open, polling: %s", error->message); return; } idx = g_usb_device_get_custom_index (item->usb_device, G_USB_DEVICE_CLASS_VENDOR_SPECIFIC, 'F', 'W', NULL); if (idx != 0x00) { g_autofree gchar *tmp = NULL; tmp = g_usb_device_get_string_descriptor (item->usb_device, idx, NULL); if (tmp != NULL) { item->got_version = TRUE; g_debug ("obtained fwver using extension '%s'", tmp); fu_device_set_version (item->device, tmp); goto out; } } g_usb_device_close (item->usb_device, NULL); /* attempt to open the device and get the serial number */ if (!ch_device_open (item->usb_device, &error)) { g_debug ("Failed to claim interface, polling: %s", error->message); return; } ch_device_queue_get_firmware_ver (priv->device_queue, item->usb_device, &major, &minor, µ); if (!ch_device_queue_process (priv->device_queue, CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, NULL, &error)) { g_warning ("Failed to get serial: %s", error->message); goto out; } /* got things the old fashioned way */ item->got_version = TRUE; version = g_strdup_printf ("%i.%i.%i", major, minor, micro); g_debug ("obtained fwver using API '%s'", version); fu_device_set_version (item->device, version); out: /* we're done here */ g_clear_error (&error); if (!g_usb_device_close (item->usb_device, &error)) g_debug ("Failed to close: %s", error->message); }
/** * fu_provider_get_results: **/ gboolean fu_provider_get_results (FuProvider *provider, FuDevice *device, GError **error) { FuProviderClass *klass = FU_PROVIDER_GET_CLASS (provider); FwupdUpdateState update_state; const gchar *tmp; g_autoptr(GError) error_local = NULL; g_autoptr(FwupdResult) res_pending = NULL; g_autoptr(FuPending) pending = NULL; /* handled by the provider */ if (klass->get_results != NULL) return klass->get_results (provider, device, error); /* handled using the database */ pending = fu_pending_new (); res_pending = fu_pending_get_device (pending, fu_device_get_id (device), &error_local); if (res_pending == NULL) { g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO, "Failed to find %s in pending database: %s", fu_device_get_id (device), error_local->message); return FALSE; } /* copy the important parts from the pending device to the real one */ update_state = fwupd_result_get_update_state (res_pending); if (update_state == FWUPD_UPDATE_STATE_UNKNOWN || update_state == FWUPD_UPDATE_STATE_PENDING) { g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO, "Device %s has not been updated offline yet", fu_device_get_id (device)); return FALSE; } /* copy */ fu_device_set_update_state (device, update_state); tmp = fwupd_result_get_update_error (res_pending); if (tmp != NULL) fu_device_set_update_error (device, tmp); tmp = fwupd_result_get_device_version (res_pending); if (tmp != NULL) fu_device_set_version (device, tmp); tmp = fwupd_result_get_update_version (res_pending); if (tmp != NULL) fu_device_set_update_version (device, tmp); return TRUE; }
/** * fu_provider_udev_unlock: **/ static gboolean fu_provider_udev_unlock (FuProvider *provider, FuDevice *device, GError **error) { const gchar *rom_fn; g_autoptr(FuRom) rom = NULL; g_autoptr(GFile) file = NULL; /* get the FW version from the rom */ g_debug ("unlocking UDev device %s", fu_device_get_id (device)); rom_fn = fu_device_get_metadata (device, "RomFilename"); if (rom_fn == NULL) { g_set_error_literal (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "Unable to read firmware from device"); return FALSE; } file = g_file_new_for_path (rom_fn); rom = fu_rom_new (); if (!fu_rom_load_file (rom, file, FU_ROM_LOAD_FLAG_BLANK_PPID, NULL, error)) return FALSE; /* update version */ if (g_strcmp0 (fu_device_get_version (device), fu_rom_get_version (rom)) != 0) { g_debug ("changing version of %s from %s to %s", fu_device_get_id (device), fu_device_get_version (device), fu_rom_get_version (rom)); fu_device_set_version (device, fu_rom_get_version (rom)); } /* prefer the GUID from the firmware rather than the * hardware as the firmware may be more generic, which * also allows us to match the GUID when doing 'verify' * on a device with a different PID to the firmware */ if (g_strcmp0 (fu_device_get_guid (device), fu_rom_get_guid (rom)) != 0) { fu_device_set_guid (device, fu_rom_get_guid (rom)); g_debug ("changing GUID of %s from %s to %s", fu_device_get_id (device), fu_device_get_guid (device), fu_rom_get_guid (rom)); } return TRUE; }
static gboolean fu_altos_device_probe (FuDevice *device, GError **error) { FuAltosDevice *self = FU_ALTOS_DEVICE (device); GUsbDevice *usb_device = fu_usb_device_get_dev (FU_USB_DEVICE (self)); /* bootloader uses tty */ if (self->kind == FU_ALTOS_DEVICE_KIND_BOOTLOADER) return fu_altos_device_probe_bootloader (self, error); /* get version */ if (self->kind == FU_ALTOS_DEVICE_KIND_CHAOSKEY) { const gchar *version_prefix = "ChaosKey-hw-1.0-sw-"; guint8 version_idx; g_autofree gchar *version = NULL; g_autoptr(FuDeviceLocker) locker = NULL; /* open */ locker = fu_device_locker_new (usb_device, error); if (locker == NULL) return FALSE; /* get string */ version_idx = g_usb_device_get_product_index (usb_device); version = g_usb_device_get_string_descriptor (usb_device, version_idx, error); if (version == NULL) return FALSE; if (!g_str_has_prefix (version, version_prefix)) { g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, "not a ChaosKey v1.0 device: %s", version); return FALSE; } fu_device_set_version (FU_DEVICE (self), version + 19); } /* success */ return TRUE; }
static gboolean fu_usb_device_probe (FuDevice *device, GError **error) { FuUsbDevice *self = FU_USB_DEVICE (device); FuUsbDeviceClass *klass = FU_USB_DEVICE_GET_CLASS (device); FuUsbDevicePrivate *priv = GET_PRIVATE (self); guint16 release; g_autofree gchar *devid0 = NULL; g_autofree gchar *devid1 = NULL; g_autofree gchar *devid2 = NULL; g_autofree gchar *vendor_id = NULL; g_autoptr(GPtrArray) intfs = NULL; /* set vendor ID */ vendor_id = g_strdup_printf ("USB:0x%04X", g_usb_device_get_vid (priv->usb_device)); fu_device_set_vendor_id (device, vendor_id); /* set the version if the release has been set */ release = g_usb_device_get_release (priv->usb_device); if (release != 0x0) { g_autofree gchar *version = NULL; version = fu_common_version_from_uint16 (release, FU_VERSION_FORMAT_BCD); fu_device_set_version (device, version); } /* add GUIDs in order of priority */ devid2 = g_strdup_printf ("USB\\VID_%04X&PID_%04X&REV_%04X", g_usb_device_get_vid (priv->usb_device), g_usb_device_get_pid (priv->usb_device), release); fu_device_add_instance_id (device, devid2); devid1 = g_strdup_printf ("USB\\VID_%04X&PID_%04X", g_usb_device_get_vid (priv->usb_device), g_usb_device_get_pid (priv->usb_device)); fu_device_add_instance_id (device, devid1); devid0 = g_strdup_printf ("USB\\VID_%04X", g_usb_device_get_vid (priv->usb_device)); fu_device_add_instance_id (device, devid0); /* add the interface GUIDs */ intfs = g_usb_device_get_interfaces (priv->usb_device, error); if (intfs == NULL) return FALSE; for (guint i = 0; i < intfs->len; i++) { GUsbInterface *intf = g_ptr_array_index (intfs, i); g_autofree gchar *intid1 = NULL; g_autofree gchar *intid2 = NULL; g_autofree gchar *intid3 = NULL; intid1 = g_strdup_printf ("USB\\CLASS_%02X&SUBCLASS_%02X&PROT_%02X", g_usb_interface_get_class (intf), g_usb_interface_get_subclass (intf), g_usb_interface_get_protocol (intf)); fu_device_add_instance_id (device, intid1); intid2 = g_strdup_printf ("USB\\CLASS_%02X&SUBCLASS_%02X", g_usb_interface_get_class (intf), g_usb_interface_get_subclass (intf)); fu_device_add_instance_id (device, intid2); intid3 = g_strdup_printf ("USB\\CLASS_%02X", g_usb_interface_get_class (intf)); fu_device_add_instance_id (device, intid3); } /* subclassed */ if (klass->probe != NULL) { if (!klass->probe (self, error)) return FALSE; } /* success */ return TRUE; }
static gboolean 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; g_return_val_if_fail (FU_IS_USB_DEVICE (self), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); /* already open */ if (priv->usb_device_locker != NULL) return TRUE; /* open */ locker = fu_device_locker_new (priv->usb_device, error); if (locker == NULL) return FALSE; /* get vendor */ if (fu_device_get_vendor (device) == NULL) { idx = g_usb_device_get_manufacturer_index (priv->usb_device); if (idx != 0x00) { g_autofree gchar *tmp = NULL; tmp = g_usb_device_get_string_descriptor (priv->usb_device, idx, error); if (tmp == NULL) return FALSE; fu_device_set_vendor (device, tmp); } } /* get product */ if (fu_device_get_name (device) == NULL) { idx = g_usb_device_get_product_index (priv->usb_device); if (idx != 0x00) { g_autofree gchar *tmp = NULL; tmp = g_usb_device_get_string_descriptor (priv->usb_device, idx, error); if (tmp == NULL) return FALSE; fu_device_set_name (device, tmp); } } /* get serial number */ if (fu_device_get_serial (device) == NULL) { idx = g_usb_device_get_serial_number_index (priv->usb_device); if (idx != 0x00) { g_autofree gchar *tmp = NULL; tmp = g_usb_device_get_string_descriptor (priv->usb_device, idx, error); if (tmp == NULL) return FALSE; fu_device_set_serial (device, tmp); } } /* get version number, falling back to the USB device release */ idx = g_usb_device_get_custom_index (priv->usb_device, G_USB_DEVICE_CLASS_VENDOR_SPECIFIC, 'F', 'W', NULL); if (idx != 0x00) { g_autofree gchar *tmp = NULL; tmp = g_usb_device_get_string_descriptor (priv->usb_device, idx, NULL); fu_device_set_version (device, tmp); } /* get GUID from the descriptor if set */ idx = g_usb_device_get_custom_index (priv->usb_device, G_USB_DEVICE_CLASS_VENDOR_SPECIFIC, 'G', 'U', NULL); if (idx != 0x00) { g_autofree gchar *tmp = NULL; tmp = g_usb_device_get_string_descriptor (priv->usb_device, idx, NULL); fu_device_add_guid (device, tmp); } /* subclassed */ if (klass->open != NULL) { if (!klass->open (self, error)) return FALSE; } /* success */ priv->usb_device_locker = g_steal_pointer (&locker); return TRUE; }
static gboolean fu_altos_device_probe_bootloader (FuAltosDevice *self, GError **error) { g_autoptr(FuDeviceLocker) locker = NULL; g_auto(GStrv) lines = NULL; g_autoptr(GString) str = NULL; /* get tty for upload */ if (!fu_altos_device_find_tty (self, error)) return FALSE; locker = fu_device_locker_new_full (self, (FuDeviceLockerFunc) fu_altos_device_tty_open, (FuDeviceLockerFunc) fu_altos_device_tty_close, error); if (locker == NULL) return FALSE; /* get the version information */ if (!fu_altos_device_tty_write (self, "v\n", -1, error)) return FALSE; str = fu_altos_device_tty_read (self, 100, -1, error); if (str == NULL) return FALSE; /* parse each line */ lines = g_strsplit_set (str->str, "\n\r", -1); for (guint i = 0; lines[i] != NULL; i++) { /* ignore */ if (lines[i][0] == '\0') continue; if (g_str_has_prefix (lines[i], "manufacturer ")) continue; if (g_str_has_prefix (lines[i], "product ")) continue; /* we can flash firmware */ if (g_strcmp0 (lines[i], "altos-loader") == 0) { fu_device_remove_flag (FU_DEVICE (self), FWUPD_DEVICE_FLAG_NEEDS_BOOTLOADER); continue; } /* version number */ if (g_str_has_prefix (lines[i], "software-version ")) { fu_device_set_version (FU_DEVICE (self), lines[i] + 17); continue; } /* address base and bound */ if (g_str_has_prefix (lines[i], "flash-range ")) { g_auto(GStrv) addrs = g_strsplit (lines[i] + 17, " ", -1); self->addr_base = g_ascii_strtoull (addrs[0], NULL, 16); self->addr_bound = g_ascii_strtoull (addrs[1], NULL, 16); g_debug ("base: %x, bound: %x", (guint) self->addr_base, (guint) self->addr_bound); continue; } /* unknown line */ g_debug ("unknown data: '%s'", lines[i]); } return TRUE; }
/** * fu_provider_udev_client_add: **/ static void fu_provider_udev_client_add (FuProviderUdev *provider_udev, GUdevDevice *device) { FuProviderUdevPrivate *priv = GET_PRIVATE (provider_udev); FuDevice *dev; const gchar *display_name; const gchar *guid; const gchar *product; const gchar *vendor; g_autofree gchar *guid_new = NULL; g_autofree gchar *id = NULL; g_autofree gchar *rom_fn = NULL; g_autofree gchar *version = NULL; g_auto(GStrv) split = NULL; g_autoptr(AsProfile) profile = as_profile_new (); g_autoptr(AsProfileTask) ptask = NULL; /* interesting device? */ guid = g_udev_device_get_property (device, "FWUPD_GUID"); if (guid == NULL) return; /* get data */ ptask = as_profile_start (profile, "FuProviderUdev:client-add{%s}", guid); g_debug ("adding udev device: %s", g_udev_device_get_sysfs_path (device)); /* is already in database */ id = fu_provider_udev_get_id (device); dev = g_hash_table_lookup (priv->devices, id); if (dev != NULL) { g_debug ("ignoring duplicate %s", id); return; } /* get the FW version from the BCD device revision */ product = g_udev_device_get_property (device, "PRODUCT"); if (product != NULL) { split = g_strsplit (product, "/", -1); if (g_strv_length (split) != 3) { g_warning ("env{PRODUCT} is invalid: %s", product); return; } version = g_strdup (split[2]); } /* no GUID from the ROM, so fix up the VID:PID */ if (!as_utils_guid_is_valid (guid)) { guid_new = as_utils_guid_from_string (guid); g_debug ("fixing GUID %s->%s", guid, guid_new); } else { guid_new = g_strdup (guid); } /* did we get enough data */ dev = fu_device_new (); fu_device_add_flag (dev, FU_DEVICE_FLAG_INTERNAL); fu_device_set_id (dev, id); fu_device_set_guid (dev, guid_new); display_name = g_udev_device_get_property (device, "FWUPD_MODEL"); if (display_name == NULL) display_name = g_udev_device_get_property (device, "ID_MODEL_FROM_DATABASE"); if (display_name != NULL) fu_device_set_name (dev, display_name); vendor = g_udev_device_get_property (device, "FWUPD_VENDOR"); if (vendor == NULL) vendor = g_udev_device_get_property (device, "ID_VENDOR_FROM_DATABASE"); if (vendor != NULL) fu_device_set_vendor (dev, vendor); if (version != NULL) fu_device_set_version (dev, version); /* get the FW version from the rom when unlocked */ rom_fn = g_build_filename (g_udev_device_get_sysfs_path (device), "rom", NULL); if (g_file_test (rom_fn, G_FILE_TEST_EXISTS)) { fu_device_set_metadata (dev, "RomFilename", rom_fn); fu_device_add_flag (dev, FU_DEVICE_FLAG_LOCKED); } /* insert to hash */ g_hash_table_insert (priv->devices, g_strdup (id), dev); fu_provider_device_add (FU_PROVIDER (provider_udev), dev); }
static gboolean fu_provider_uefi_coldplug (FuProvider *provider, GError **error) { AsVersionParseFlag parse_flags; g_autofree gchar *display_name = NULL; fwup_resource *re; gint supported; g_autofree gchar *guid = NULL; g_autoptr(FuDevice) dev = NULL; g_autoptr(fwup_resource_iter) iter = NULL; /* supported = 0 : ESRT unspported supported = 1 : unlocked, ESRT supported supported = 2 : it is locked but can be unlocked to support ESRT supported = 3 : it is locked, has been marked to be unlocked on next boot calling unlock again is OK. */ supported = fwup_supported (); if (supported == 0) { g_set_error_literal (error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, "UEFI firmware updating not supported"); return FALSE; } if (supported >= 2) { dev = fu_device_new (); fu_device_set_id (dev, "UEFI-dummy-dev0"); fu_device_add_guid (dev, "2d47f29b-83a2-4f31-a2e8-63474f4d4c2e"); fu_device_set_version (dev, "0"); fu_device_add_flag (dev, FU_DEVICE_FLAG_ALLOW_ONLINE); fu_device_add_flag (dev, FU_DEVICE_FLAG_LOCKED); fu_provider_device_add (provider, dev); return TRUE; } /* this can fail if we have no permissions */ if (fwup_resource_iter_create (&iter) < 0) { g_set_error_literal (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "Cannot create fwup iter"); return FALSE; } /* set Display Name to the system for all capsules */ g_file_get_contents ("/sys/class/dmi/id/product_name", &display_name, NULL, NULL); if (display_name != NULL) g_strchomp (display_name); /* add each device */ guid = g_strdup ("00000000-0000-0000-0000-000000000000"); parse_flags = fu_provider_uefi_get_version_format (); while (fwup_resource_iter_next (iter, &re) > 0) { efi_guid_t *guid_raw; guint32 version_raw; guint64 hardware_instance = 0; /* FIXME */ g_autofree gchar *id = NULL; g_autofree gchar *version = NULL; g_autofree gchar *version_lowest = NULL; /* convert to strings */ fwup_get_guid (re, &guid_raw); if (efi_guid_to_str (guid_raw, &guid) < 0) { g_warning ("failed to convert guid to string"); continue; } fwup_get_fw_version(re, &version_raw); version = as_utils_version_from_uint32 (version_raw, parse_flags); id = g_strdup_printf ("UEFI-%s-dev%" G_GUINT64_FORMAT, guid, hardware_instance); dev = fu_device_new (); fu_device_set_id (dev, id); fu_device_add_guid (dev, guid); fu_device_set_version (dev, version); if (display_name != NULL) fu_device_set_name(dev, display_name); fwup_get_lowest_supported_fw_version (re, &version_raw); if (version_raw != 0) { version_lowest = as_utils_version_from_uint32 (version_raw, parse_flags); fu_device_set_version_lowest (dev, version_lowest); } fu_device_add_flag (dev, FU_DEVICE_FLAG_INTERNAL); fu_device_add_flag (dev, FU_DEVICE_FLAG_ALLOW_OFFLINE); fu_device_add_flag (dev, FU_DEVICE_FLAG_REQUIRE_AC); fu_provider_device_add (provider, dev); } return TRUE; }