Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 | * |
Richard Hughes | 53ec7ea | 2016-03-29 18:45:08 +0100 | [diff] [blame] | 3 | * Copyright (C) 2015-2016 Richard Hughes <richard@hughsie.com> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the GNU General Public License Version 2 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 24 | #include <fwupd.h> |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 25 | #include <appstream-glib.h> |
| 26 | #include <glib/gstdio.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 27 | #include <gio/gio.h> |
| 28 | #include <gio/gunixfdlist.h> |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 29 | #include <gio/gunixinputstream.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 30 | #include <glib/gi18n.h> |
| 31 | #include <locale.h> |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 32 | #include <polkit/polkit.h> |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #include <fcntl.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 35 | |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 36 | #include "fwupd-enums-private.h" |
| 37 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 38 | #include "fu-debug.h" |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 39 | #include "fu-device.h" |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 40 | #include "fu-plugin.h" |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 41 | #include "fu-keyring.h" |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 42 | #include "fu-pending.h" |
Richard Hughes | 3c99ba4 | 2015-03-05 12:17:48 +0000 | [diff] [blame] | 43 | #include "fu-provider.h" |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 44 | #include "fu-provider-dfu.h" |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 45 | #include "fu-provider-ebitdo.h" |
Richard Hughes | 25cf6ab | 2015-08-04 21:34:12 +0100 | [diff] [blame] | 46 | #include "fu-provider-rpi.h" |
Richard Hughes | ebb58a3 | 2015-05-29 15:35:37 +0100 | [diff] [blame] | 47 | #include "fu-provider-udev.h" |
Richard Hughes | c89c1b0 | 2015-05-05 15:21:18 +0100 | [diff] [blame] | 48 | #include "fu-provider-usb.h" |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 49 | #include "fu-resources.h" |
Mario Limonciello | 918f393 | 2016-02-03 12:47:23 -0600 | [diff] [blame] | 50 | #include "fu-quirks.h" |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 51 | |
Richard Hughes | 3c99ba4 | 2015-03-05 12:17:48 +0000 | [diff] [blame] | 52 | #ifdef HAVE_COLORHUG |
| 53 | #include "fu-provider-chug.h" |
| 54 | #endif |
| 55 | #ifdef HAVE_UEFI |
| 56 | #include "fu-provider-uefi.h" |
| 57 | #endif |
Mario Limonciello | 958ead6 | 2016-05-14 00:10:25 -0500 | [diff] [blame] | 58 | #ifdef HAVE_DELL |
| 59 | #include "fu-provider-dell.h" |
| 60 | #endif |
Richard Hughes | 3c99ba4 | 2015-03-05 12:17:48 +0000 | [diff] [blame] | 61 | |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 62 | #ifndef PolkitAuthorizationResult_autoptr |
| 63 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitAuthorizationResult, g_object_unref) |
| 64 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitSubject, g_object_unref) |
| 65 | #endif |
| 66 | |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 67 | #define FU_MAIN_FIRMWARE_SIZE_MAX (32 * 1024 * 1024) /* bytes */ |
| 68 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 69 | typedef struct { |
| 70 | GDBusConnection *connection; |
| 71 | GDBusNodeInfo *introspection_daemon; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 72 | GDBusProxy *proxy_uid; |
Richard Hughes | 9559bbe | 2016-03-29 18:54:20 +0100 | [diff] [blame] | 73 | GDBusProxy *proxy_upower; |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 74 | GKeyFile *config; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 75 | GMainLoop *loop; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 76 | GPtrArray *devices; /* of FuDeviceItem */ |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 77 | GPtrArray *providers; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 78 | PolkitAuthority *authority; |
Richard Hughes | f910ac9 | 2015-03-19 10:43:42 +0000 | [diff] [blame] | 79 | FwupdStatus status; |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 80 | guint percentage; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 81 | FuPending *pending; |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 82 | AsProfile *profile; |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 83 | AsStore *store; |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 84 | guint store_changed_id; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 85 | GHashTable *plugins; /* of name : FuPlugin */ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 86 | } FuMainPrivate; |
| 87 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 88 | typedef struct { |
| 89 | FuDevice *device; |
| 90 | FuProvider *provider; |
| 91 | } FuDeviceItem; |
| 92 | |
Richard Hughes | dde7a2f | 2016-04-28 15:06:52 +0100 | [diff] [blame] | 93 | static gboolean fu_main_get_updates_item_update (FuMainPrivate *priv, FuDeviceItem *item); |
| 94 | |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 95 | static void |
| 96 | fu_main_emit_changed (FuMainPrivate *priv) |
| 97 | { |
| 98 | /* not yet connected */ |
| 99 | if (priv->connection == NULL) |
| 100 | return; |
| 101 | g_dbus_connection_emit_signal (priv->connection, |
| 102 | NULL, |
| 103 | FWUPD_DBUS_PATH, |
| 104 | FWUPD_DBUS_INTERFACE, |
| 105 | "Changed", |
| 106 | NULL, NULL); |
| 107 | } |
| 108 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 109 | static void |
| 110 | fu_main_emit_device_added (FuMainPrivate *priv, FuDevice *device) |
| 111 | { |
| 112 | GVariant *val; |
| 113 | |
| 114 | /* not yet connected */ |
| 115 | if (priv->connection == NULL) |
| 116 | return; |
| 117 | val = fwupd_result_to_data (FWUPD_RESULT (device), "(a{sv})"); |
| 118 | g_dbus_connection_emit_signal (priv->connection, |
| 119 | NULL, |
| 120 | FWUPD_DBUS_PATH, |
| 121 | FWUPD_DBUS_INTERFACE, |
| 122 | "DeviceAdded", |
| 123 | val, NULL); |
| 124 | } |
| 125 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 126 | static void |
| 127 | fu_main_emit_device_removed (FuMainPrivate *priv, FuDevice *device) |
| 128 | { |
| 129 | GVariant *val; |
| 130 | |
| 131 | /* not yet connected */ |
| 132 | if (priv->connection == NULL) |
| 133 | return; |
| 134 | val = fwupd_result_to_data (FWUPD_RESULT (device), "(a{sv})"); |
| 135 | g_dbus_connection_emit_signal (priv->connection, |
| 136 | NULL, |
| 137 | FWUPD_DBUS_PATH, |
| 138 | FWUPD_DBUS_INTERFACE, |
| 139 | "DeviceRemoved", |
| 140 | val, NULL); |
| 141 | } |
| 142 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 143 | static void |
| 144 | fu_main_emit_device_changed (FuMainPrivate *priv, FuDevice *device) |
| 145 | { |
| 146 | GVariant *val; |
| 147 | |
| 148 | /* not yet connected */ |
| 149 | if (priv->connection == NULL) |
| 150 | return; |
| 151 | val = fwupd_result_to_data (FWUPD_RESULT (device), "(a{sv})"); |
| 152 | g_dbus_connection_emit_signal (priv->connection, |
| 153 | NULL, |
| 154 | FWUPD_DBUS_PATH, |
| 155 | FWUPD_DBUS_INTERFACE, |
| 156 | "DeviceChanged", |
| 157 | val, NULL); |
| 158 | } |
| 159 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 160 | static void |
| 161 | fu_main_emit_property_changed (FuMainPrivate *priv, |
| 162 | const gchar *property_name, |
| 163 | GVariant *property_value) |
| 164 | { |
| 165 | GVariantBuilder builder; |
| 166 | GVariantBuilder invalidated_builder; |
| 167 | |
| 168 | /* not yet connected */ |
| 169 | if (priv->connection == NULL) |
| 170 | return; |
| 171 | |
| 172 | /* build the dict */ |
| 173 | g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as")); |
| 174 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 175 | g_variant_builder_add (&builder, |
| 176 | "{sv}", |
| 177 | property_name, |
| 178 | property_value); |
| 179 | g_dbus_connection_emit_signal (priv->connection, |
| 180 | NULL, |
| 181 | FWUPD_DBUS_PATH, |
| 182 | "org.freedesktop.DBus.Properties", |
| 183 | "PropertiesChanged", |
| 184 | g_variant_new ("(sa{sv}as)", |
| 185 | FWUPD_DBUS_INTERFACE, |
| 186 | &builder, |
| 187 | &invalidated_builder), |
| 188 | NULL); |
| 189 | g_variant_builder_clear (&builder); |
| 190 | g_variant_builder_clear (&invalidated_builder); |
| 191 | } |
| 192 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 193 | static void |
Richard Hughes | f910ac9 | 2015-03-19 10:43:42 +0000 | [diff] [blame] | 194 | fu_main_set_status (FuMainPrivate *priv, FwupdStatus status) |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 195 | { |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 196 | if (priv->status == status) |
| 197 | return; |
| 198 | priv->status = status; |
| 199 | |
| 200 | /* emit changed */ |
Richard Hughes | 8818151 | 2015-03-19 10:57:44 +0000 | [diff] [blame] | 201 | g_debug ("Emitting PropertyChanged('Status'='%s')", |
| 202 | fwupd_status_to_string (status)); |
| 203 | fu_main_emit_property_changed (priv, "Status", g_variant_new_uint32 (status)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 206 | static void |
| 207 | fu_main_set_percentage (FuMainPrivate *priv, guint percentage) |
| 208 | { |
| 209 | if (priv->percentage == percentage) |
| 210 | return; |
| 211 | priv->percentage = percentage; |
| 212 | |
| 213 | /* emit changed */ |
| 214 | g_debug ("Emitting PropertyChanged('Percentage'='%u%%')", percentage); |
| 215 | fu_main_emit_property_changed (priv, "Percentage", |
| 216 | g_variant_new_uint32 (percentage)); |
| 217 | } |
| 218 | |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 219 | static GVariant * |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 220 | fu_main_device_array_to_variant (GPtrArray *devices, GError **error) |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 221 | { |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 222 | GVariantBuilder builder; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 223 | |
Richard Hughes | 9a38c03 | 2015-03-17 20:58:46 +0000 | [diff] [blame] | 224 | /* no devices */ |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 225 | if (devices->len == 0) { |
Richard Hughes | 9a38c03 | 2015-03-17 20:58:46 +0000 | [diff] [blame] | 226 | g_set_error_literal (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 227 | FWUPD_ERROR, |
| 228 | FWUPD_ERROR_NOTHING_TO_DO, |
Richard Hughes | 9d76a87 | 2015-09-17 12:49:07 +0100 | [diff] [blame] | 229 | "Nothing to do"); |
Richard Hughes | 9a38c03 | 2015-03-17 20:58:46 +0000 | [diff] [blame] | 230 | return NULL; |
| 231 | } |
| 232 | |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 233 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 234 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 235 | GVariant *tmp; |
| 236 | FuDeviceItem *item; |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 237 | item = g_ptr_array_index (devices, i); |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 238 | tmp = fwupd_result_to_data (FWUPD_RESULT (item->device), "{sa{sv}}"); |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 239 | g_variant_builder_add_value (&builder, tmp); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 240 | } |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 241 | return g_variant_new ("(a{sa{sv}})", &builder); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 244 | static void |
| 245 | fu_main_invocation_return_value (FuMainPrivate *priv, |
| 246 | GDBusMethodInvocation *invocation, |
| 247 | GVariant *parameters) |
| 248 | { |
| 249 | fu_main_set_status (priv, FWUPD_STATUS_IDLE); |
| 250 | g_dbus_method_invocation_return_value (invocation, parameters); |
| 251 | } |
| 252 | |
| 253 | static void |
| 254 | fu_main_invocation_return_error (FuMainPrivate *priv, |
| 255 | GDBusMethodInvocation *invocation, |
| 256 | const GError *error) |
| 257 | { |
| 258 | fu_main_set_status (priv, FWUPD_STATUS_IDLE); |
| 259 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 260 | } |
| 261 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 262 | static gboolean |
| 263 | fu_main_load_plugins (GHashTable *plugins, GError **error) |
| 264 | { |
| 265 | FuPlugin *plugin; |
| 266 | GModule *module; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 267 | const gchar *fn; |
| 268 | g_autofree gchar *plugin_dir = NULL; |
| 269 | g_autoptr(GDir) dir = NULL; |
| 270 | g_autoptr(GList) values = NULL; |
| 271 | |
| 272 | /* search */ |
| 273 | plugin_dir = g_build_filename (LIBDIR, "fwupd-plugins-1", NULL); |
| 274 | dir = g_dir_open (plugin_dir, 0, error); |
| 275 | if (dir == NULL) |
| 276 | return FALSE; |
| 277 | while ((fn = g_dir_read_name (dir)) != NULL) { |
| 278 | g_autofree gchar *filename = NULL; |
| 279 | |
| 280 | /* ignore non-plugins */ |
| 281 | if (!g_str_has_suffix (fn, ".so")) |
| 282 | continue; |
| 283 | |
| 284 | /* open module */ |
| 285 | filename = g_build_filename (plugin_dir, fn, NULL); |
| 286 | g_debug ("adding plugin %s", filename); |
| 287 | module = g_module_open (filename, 0); |
| 288 | if (module == NULL) { |
| 289 | g_warning ("failed to open plugin %s: %s", |
| 290 | filename, g_module_error ()); |
| 291 | continue; |
| 292 | } |
| 293 | plugin = fu_plugin_new (module); |
| 294 | if (plugin == NULL) { |
| 295 | g_module_close (module); |
| 296 | g_warning ("plugin %s requires name", filename); |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | /* add */ |
| 301 | g_hash_table_insert (plugins, g_strdup (plugin->name), plugin); |
| 302 | } |
| 303 | |
| 304 | /* start them all up */ |
| 305 | values = g_hash_table_get_values (plugins); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 306 | for (GList *l = values; l != NULL; l = l->next) { |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 307 | plugin = FU_PLUGIN (l->data); |
| 308 | if (!fu_plugin_run_startup (plugin, error)) |
| 309 | return FALSE; |
| 310 | } |
| 311 | |
| 312 | return TRUE; |
| 313 | } |
| 314 | |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 315 | static FuPlugin * |
| 316 | fu_main_get_plugin_for_device (GHashTable *plugins, FuDevice *device) |
| 317 | { |
| 318 | const gchar *tmp; |
Richard Hughes | 0bda054 | 2016-08-11 15:35:30 +0100 | [diff] [blame] | 319 | FuPlugin *plugin; |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 320 | |
| 321 | /* does a vendor plugin exist */ |
| 322 | tmp = fu_device_get_metadata (device, FU_DEVICE_KEY_FWUPD_PLUGIN); |
| 323 | if (tmp == NULL) |
| 324 | return NULL; |
Richard Hughes | 0bda054 | 2016-08-11 15:35:30 +0100 | [diff] [blame] | 325 | plugin = g_hash_table_lookup (plugins, tmp); |
| 326 | if (plugin == NULL) { |
| 327 | g_warning ("requested plugin %s for %s, but not found", |
| 328 | tmp, fu_device_get_id (device)); |
| 329 | } |
| 330 | return plugin; |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 333 | static void |
| 334 | fu_main_item_free (FuDeviceItem *item) |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 335 | { |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 336 | g_object_unref (item->device); |
| 337 | g_object_unref (item->provider); |
| 338 | g_free (item); |
| 339 | } |
| 340 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 341 | static FuDeviceItem * |
| 342 | fu_main_get_item_by_id (FuMainPrivate *priv, const gchar *id) |
| 343 | { |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 344 | for (guint i = 0; i < priv->devices->len; i++) { |
| 345 | FuDeviceItem *item = g_ptr_array_index (priv->devices, i); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 346 | if (g_strcmp0 (fu_device_get_id (item->device), id) == 0) |
| 347 | return item; |
Richard Hughes | b1b59d8 | 2016-01-06 13:20:53 +0000 | [diff] [blame] | 348 | if (g_strcmp0 (fu_device_get_equivalent_id (item->device), id) == 0) |
| 349 | return item; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 350 | } |
| 351 | return NULL; |
| 352 | } |
| 353 | |
Richard Hughes | 16d8f62 | 2016-03-15 16:34:20 +0000 | [diff] [blame] | 354 | static FuDeviceItem * |
| 355 | fu_main_get_item_by_guid (FuMainPrivate *priv, const gchar *guid) |
| 356 | { |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 357 | for (guint i = 0; i < priv->devices->len; i++) { |
| 358 | FuDeviceItem *item = g_ptr_array_index (priv->devices, i); |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 359 | if (fu_device_has_guid (item->device, guid)) |
Richard Hughes | 16d8f62 | 2016-03-15 16:34:20 +0000 | [diff] [blame] | 360 | return item; |
| 361 | } |
| 362 | return NULL; |
| 363 | } |
| 364 | |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 365 | static FuProvider * |
| 366 | fu_main_get_provider_by_name (FuMainPrivate *priv, const gchar *name) |
| 367 | { |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 368 | for (guint i = 0; i < priv->providers->len; i++) { |
| 369 | FuProvider *provider = g_ptr_array_index (priv->providers, i); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 370 | if (g_strcmp0 (fu_provider_get_name (provider), name) == 0) |
| 371 | return provider; |
| 372 | } |
| 373 | return NULL; |
| 374 | } |
| 375 | |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 376 | static gboolean |
| 377 | fu_main_get_release_trust_flags (AsRelease *release, |
| 378 | FwupdTrustFlags *trust_flags, |
| 379 | GError **error) |
| 380 | { |
| 381 | AsChecksum *csum_tmp; |
| 382 | GBytes *blob_payload; |
| 383 | GBytes *blob_signature; |
| 384 | const gchar *fn; |
| 385 | g_autofree gchar *pki_dir = NULL; |
| 386 | g_autofree gchar *fn_signature = NULL; |
| 387 | g_autoptr(GError) error_local = NULL; |
| 388 | g_autoptr(FuKeyring) kr = NULL; |
| 389 | |
| 390 | /* no filename? */ |
| 391 | csum_tmp = as_release_get_checksum_by_target (release, AS_CHECKSUM_TARGET_CONTENT); |
| 392 | fn = as_checksum_get_filename (csum_tmp); |
| 393 | if (fn == NULL) { |
| 394 | g_set_error_literal (error, |
| 395 | FWUPD_ERROR, |
| 396 | FWUPD_ERROR_INVALID_FILE, |
| 397 | "no filename"); |
| 398 | return FALSE; |
| 399 | } |
| 400 | |
| 401 | /* no signature == no trust */ |
| 402 | fn_signature = g_strdup_printf ("%s.asc", fn); |
| 403 | blob_signature = as_release_get_blob (release, fn_signature); |
| 404 | if (blob_signature == NULL) { |
| 405 | g_debug ("firmware archive contained no GPG signature"); |
| 406 | return TRUE; |
| 407 | } |
| 408 | |
| 409 | /* get payload */ |
| 410 | blob_payload = as_release_get_blob (release, fn); |
| 411 | if (blob_payload == NULL) { |
| 412 | g_set_error_literal (error, |
| 413 | FWUPD_ERROR, |
| 414 | FWUPD_ERROR_INVALID_FILE, |
| 415 | "no payload"); |
| 416 | return FALSE; |
| 417 | } |
| 418 | |
| 419 | /* check we were installed correctly */ |
| 420 | pki_dir = g_build_filename (SYSCONFDIR, "pki", "fwupd", NULL); |
| 421 | if (!g_file_test (pki_dir, G_FILE_TEST_EXISTS)) { |
| 422 | g_set_error (error, |
| 423 | FWUPD_ERROR, |
| 424 | FWUPD_ERROR_NOT_FOUND, |
| 425 | "PKI directory %s not found", pki_dir); |
| 426 | return FALSE; |
| 427 | } |
| 428 | |
| 429 | /* verify against the system trusted keys */ |
| 430 | kr = fu_keyring_new (); |
| 431 | if (!fu_keyring_add_public_keys (kr, pki_dir, error)) |
| 432 | return FALSE; |
| 433 | if (!fu_keyring_verify_data (kr, blob_payload, blob_signature, &error_local)) { |
| 434 | g_warning ("untrusted as failed to verify: %s", |
| 435 | error_local->message); |
| 436 | return TRUE; |
| 437 | } |
| 438 | |
| 439 | /* awesome! */ |
| 440 | g_debug ("marking payload as trusted"); |
| 441 | *trust_flags |= FWUPD_TRUST_FLAG_PAYLOAD; |
| 442 | return TRUE; |
| 443 | } |
| 444 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 445 | typedef enum { |
| 446 | FU_MAIN_AUTH_KIND_UNKNOWN, |
| 447 | FU_MAIN_AUTH_KIND_INSTALL, |
| 448 | FU_MAIN_AUTH_KIND_UNLOCK, |
| 449 | FU_MAIN_AUTH_KIND_LAST |
| 450 | } FuMainAuthKind; |
| 451 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 452 | typedef struct { |
| 453 | GDBusMethodInvocation *invocation; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 454 | AsStore *store; |
| 455 | FwupdTrustFlags trust_flags; |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 456 | GPtrArray *devices; /* of FuDevice */ |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 457 | GPtrArray *blob_fws; /* of GBytes */ |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 458 | FwupdInstallFlags flags; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 459 | GBytes *blob_cab; |
Richard Hughes | db468ee | 2016-06-29 10:10:47 +0100 | [diff] [blame] | 460 | gboolean is_downgrade; |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 461 | FuMainAuthKind auth_kind; |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 462 | FuMainPrivate *priv; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 463 | } FuMainAuthHelper; |
| 464 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 465 | static void |
| 466 | fu_main_helper_free (FuMainAuthHelper *helper) |
| 467 | { |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 468 | /* free */ |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 469 | if (helper->devices != NULL) |
| 470 | g_ptr_array_unref (helper->devices); |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 471 | if (helper->blob_fws > 0) |
| 472 | g_ptr_array_unref (helper->blob_fws); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 473 | if (helper->blob_cab > 0) |
| 474 | g_bytes_unref (helper->blob_cab); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 475 | if (helper->store != NULL) |
| 476 | g_object_unref (helper->store); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 477 | g_object_unref (helper->invocation); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 478 | g_free (helper); |
| 479 | } |
| 480 | |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 481 | static gboolean |
Richard Hughes | 9559bbe | 2016-03-29 18:54:20 +0100 | [diff] [blame] | 482 | fu_main_on_battery (FuMainPrivate *priv) |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 483 | { |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 484 | g_autoptr(GVariant) value = NULL; |
Richard Hughes | 9559bbe | 2016-03-29 18:54:20 +0100 | [diff] [blame] | 485 | if (priv->proxy_upower == NULL) { |
| 486 | g_warning ("Failed to get OnBattery property as no UPower"); |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 487 | return FALSE; |
| 488 | } |
Richard Hughes | 9559bbe | 2016-03-29 18:54:20 +0100 | [diff] [blame] | 489 | value = g_dbus_proxy_get_cached_property (priv->proxy_upower, "OnBattery"); |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 490 | if (value == NULL) { |
| 491 | g_warning ("Failed to get OnBattery property value"); |
| 492 | return FALSE; |
| 493 | } |
| 494 | return g_variant_get_boolean (value); |
| 495 | } |
| 496 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 497 | static gboolean |
| 498 | fu_main_provider_unlock_authenticated (FuMainAuthHelper *helper, GError **error) |
| 499 | { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 500 | /* check the devices still exists */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 501 | for (guint i = 0; i < helper->devices->len; i ++) { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 502 | FuDeviceItem *item; |
| 503 | FuDevice *device = g_ptr_array_index (helper->devices, i); |
| 504 | |
| 505 | item = fu_main_get_item_by_id (helper->priv, |
| 506 | fu_device_get_id (device)); |
| 507 | if (item == NULL) { |
| 508 | g_set_error (error, |
| 509 | FWUPD_ERROR, |
| 510 | FWUPD_ERROR_INVALID_FILE, |
| 511 | "device %s was removed", |
| 512 | fu_device_get_id (device)); |
| 513 | return FALSE; |
| 514 | } |
| 515 | |
| 516 | /* run the correct provider that added this */ |
| 517 | if (!fu_provider_unlock (item->provider, |
| 518 | item->device, |
| 519 | error)) |
| 520 | return FALSE; |
| 521 | |
| 522 | /* make the UI update */ |
| 523 | fu_main_emit_device_changed (helper->priv, item->device); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 526 | /* make the UI update */ |
| 527 | fu_main_emit_changed (helper->priv); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 528 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 529 | return TRUE; |
| 530 | } |
| 531 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 532 | static gboolean |
| 533 | fu_main_provider_update_authenticated (FuMainAuthHelper *helper, GError **error) |
| 534 | { |
| 535 | FuDeviceItem *item; |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 536 | FuPlugin *plugin; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 537 | |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 538 | /* check the devices still exists */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 539 | for (guint i = 0; i < helper->devices->len; i ++) { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 540 | FuDevice *device = g_ptr_array_index (helper->devices, i); |
| 541 | item = fu_main_get_item_by_id (helper->priv, |
| 542 | fu_device_get_id (device)); |
| 543 | if (item == NULL) { |
| 544 | g_set_error (error, |
| 545 | FWUPD_ERROR, |
| 546 | FWUPD_ERROR_INVALID_FILE, |
| 547 | "device %s was removed", |
| 548 | fu_device_get_id (device)); |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 549 | return FALSE; |
| 550 | } |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 551 | |
| 552 | /* The provider might have taken away update abilities */ |
| 553 | if (!fu_device_has_flag (item->device, FU_DEVICE_FLAG_ALLOW_OFFLINE) && |
| 554 | !fu_device_has_flag (item->device, FU_DEVICE_FLAG_ALLOW_ONLINE)) { |
| 555 | g_set_error(error, |
| 556 | FWUPD_ERROR, |
| 557 | FWUPD_ERROR_INTERNAL, |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 558 | "Device %s does not now allow updates", |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 559 | fu_device_get_id (device)); |
| 560 | return FALSE; |
| 561 | } |
| 562 | |
| 563 | /* can we only do this on AC power */ |
| 564 | if (fu_device_has_flag (item->device, FU_DEVICE_FLAG_REQUIRE_AC)) { |
| 565 | if (fu_main_on_battery (helper->priv)) { |
| 566 | g_set_error_literal (error, |
| 567 | FWUPD_ERROR, |
| 568 | FWUPD_ERROR_NOT_SUPPORTED, |
| 569 | "Cannot install update " |
| 570 | "when not on AC power"); |
| 571 | return FALSE; |
| 572 | } |
| 573 | } |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 576 | /* run the correct providers for each device */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 577 | for (guint i = 0; i < helper->devices->len; i ++) { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 578 | FuDevice *device = g_ptr_array_index (helper->devices, i); |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 579 | GBytes *blob_fw = g_ptr_array_index (helper->blob_fws, i); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 580 | item = fu_main_get_item_by_id (helper->priv, |
| 581 | fu_device_get_id (device)); |
| 582 | plugin = fu_main_get_plugin_for_device (helper->priv->plugins, |
| 583 | item->device); |
| 584 | if (!fu_provider_update (item->provider, |
| 585 | item->device, |
| 586 | helper->blob_cab, |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 587 | blob_fw, |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 588 | plugin, |
| 589 | helper->flags, |
| 590 | error)) |
| 591 | return FALSE; |
| 592 | |
| 593 | /* make the UI update */ |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 594 | fu_device_set_modified (item->device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 595 | fu_main_emit_device_changed (helper->priv, item->device); |
| 596 | } |
Richard Hughes | a3a8f50 | 2015-11-24 12:31:59 +0000 | [diff] [blame] | 597 | |
| 598 | /* make the UI update */ |
| 599 | fu_main_emit_changed (helper->priv); |
| 600 | return TRUE; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 603 | static void |
| 604 | fu_main_check_authorization_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 605 | { |
| 606 | FuMainAuthHelper *helper = (FuMainAuthHelper *) user_data; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 607 | g_autoptr(GError) error = NULL; |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 608 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 609 | |
| 610 | /* get result */ |
| 611 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 612 | res, &error); |
| 613 | if (auth == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 614 | g_set_error (&error, |
| 615 | FWUPD_ERROR, |
| 616 | FWUPD_ERROR_AUTH_FAILED, |
| 617 | "could not check for auth: %s", error->message); |
| 618 | fu_main_invocation_return_error (helper->priv, helper->invocation, error); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 619 | fu_main_helper_free (helper); |
| 620 | return; |
| 621 | } |
| 622 | |
| 623 | /* did not auth */ |
| 624 | if (!polkit_authorization_result_get_is_authorized (auth)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 625 | g_set_error_literal (&error, |
| 626 | FWUPD_ERROR, |
| 627 | FWUPD_ERROR_AUTH_FAILED, |
| 628 | "failed to obtain auth"); |
| 629 | fu_main_invocation_return_error (helper->priv, helper->invocation, error); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 630 | fu_main_helper_free (helper); |
| 631 | return; |
| 632 | } |
| 633 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 634 | /* we're good to go */ |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 635 | if (helper->auth_kind == FU_MAIN_AUTH_KIND_INSTALL) { |
| 636 | if (!fu_main_provider_update_authenticated (helper, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 637 | fu_main_invocation_return_error (helper->priv, |
| 638 | helper->invocation, |
| 639 | error); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 640 | fu_main_helper_free (helper); |
| 641 | return; |
| 642 | } |
| 643 | } else if (helper->auth_kind == FU_MAIN_AUTH_KIND_UNLOCK) { |
| 644 | if (!fu_main_provider_unlock_authenticated (helper, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 645 | fu_main_invocation_return_error (helper->priv, |
| 646 | helper->invocation, |
| 647 | error); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 648 | fu_main_helper_free (helper); |
| 649 | return; |
| 650 | } |
| 651 | } else { |
| 652 | g_assert_not_reached (); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /* success */ |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 656 | fu_main_invocation_return_value (helper->priv, helper->invocation, NULL); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 657 | fu_main_helper_free (helper); |
| 658 | } |
| 659 | |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 660 | static gchar * |
| 661 | fu_main_get_guids_from_store (AsStore *store) |
| 662 | { |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 663 | AsProvide *prov; |
| 664 | GPtrArray *provides; |
| 665 | GPtrArray *apps; |
| 666 | GString *str = g_string_new (""); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 667 | |
| 668 | /* return a string with all the firmware apps in the store */ |
| 669 | apps = as_store_get_apps (store); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 670 | for (guint i = 0; i < apps->len; i++) { |
| 671 | AsApp *app = AS_APP (g_ptr_array_index (apps, i)); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 672 | provides = as_app_get_provides (app); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 673 | for (guint j = 0; j < provides->len; j++) { |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 674 | prov = AS_PROVIDE (g_ptr_array_index (provides, j)); |
| 675 | if (as_provide_get_kind (prov) != AS_PROVIDE_KIND_FIRMWARE_FLASHED) |
| 676 | continue; |
| 677 | g_string_append_printf (str, "%s,", as_provide_get_value (prov)); |
| 678 | } |
| 679 | } |
| 680 | if (str->len == 0) |
| 681 | return NULL; |
| 682 | g_string_truncate (str, str->len - 1); |
| 683 | return g_string_free (str, FALSE); |
| 684 | } |
| 685 | |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 686 | static void |
| 687 | fu_main_vendor_quirk_release_version (AsApp *app) |
| 688 | { |
| 689 | AsVersionParseFlag flags = AS_VERSION_PARSE_FLAG_USE_TRIPLET; |
| 690 | GPtrArray *releases; |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 691 | |
| 692 | /* no quirk required */ |
Richard Hughes | 65dfbfe | 2016-03-02 13:42:53 +0000 | [diff] [blame] | 693 | if (as_app_get_kind (app) != AS_APP_KIND_FIRMWARE) |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 694 | return; |
| 695 | |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 696 | for (guint i = 0; quirk_table[i].identifier != NULL; i++) { |
Mario Limonciello | 918f393 | 2016-02-03 12:47:23 -0600 | [diff] [blame] | 697 | if (g_str_has_prefix (as_app_get_id(app), quirk_table[i].identifier)) |
| 698 | flags = quirk_table[i].flags; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 699 | } |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 700 | |
| 701 | /* fix each release */ |
| 702 | releases = as_app_get_releases (app); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 703 | for (guint i = 0; i < releases->len; i++) { |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 704 | AsRelease *rel; |
| 705 | const gchar *version; |
| 706 | guint64 ver_uint32; |
| 707 | g_autofree gchar *version_new = NULL; |
| 708 | |
| 709 | rel = g_ptr_array_index (releases, i); |
| 710 | version = as_release_get_version (rel); |
| 711 | if (version == NULL) |
| 712 | continue; |
| 713 | if (g_strstr_len (version, -1, ".") != NULL) |
| 714 | continue; |
| 715 | |
| 716 | /* metainfo files use hex and the LVFS uses decimal */ |
| 717 | if (g_str_has_prefix (version, "0x")) { |
| 718 | ver_uint32 = g_ascii_strtoull (version + 2, NULL, 16); |
| 719 | } else { |
| 720 | ver_uint32 = g_ascii_strtoull (version, NULL, 10); |
| 721 | } |
| 722 | if (ver_uint32 == 0) |
| 723 | continue; |
| 724 | |
| 725 | /* convert to dotted decimal */ |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 726 | version_new = as_utils_version_from_uint32 ((guint32) ver_uint32, flags); |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 727 | as_release_set_version (rel, version_new); |
| 728 | } |
| 729 | } |
| 730 | |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 731 | static AsApp * |
| 732 | fu_main_store_get_app_by_guids (AsStore *store, FuDevice *device) |
| 733 | { |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 734 | GPtrArray *guids = fu_device_get_guids (device); |
| 735 | for (guint i = 0; i < guids->len; i++) { |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 736 | AsApp *app = NULL; |
| 737 | app = as_store_get_app_by_provide (store, |
| 738 | AS_PROVIDE_KIND_FIRMWARE_FLASHED, |
| 739 | g_ptr_array_index (guids, i)); |
| 740 | if (app != NULL) |
| 741 | return app; |
| 742 | } |
| 743 | return NULL; |
| 744 | } |
| 745 | |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 746 | static AsScreenshot * |
| 747 | _as_app_get_screenshot_default (AsApp *app) |
| 748 | { |
| 749 | GPtrArray *array = as_app_get_screenshots (app); |
| 750 | if (array->len == 0) |
| 751 | return NULL; |
| 752 | return g_ptr_array_index (array, 0); |
| 753 | } |
| 754 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 755 | static gboolean |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 756 | fu_main_update_helper_for_device (FuMainAuthHelper *helper, |
| 757 | FuDevice *device, |
| 758 | GError **error) |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 759 | { |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 760 | AsApp *app; |
| 761 | AsChecksum *csum_tmp; |
| 762 | AsRelease *rel; |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 763 | GBytes *blob_fw; |
Richard Hughes | def3175 | 2015-03-04 19:26:54 +0000 | [diff] [blame] | 764 | const gchar *tmp; |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 765 | const gchar *version; |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 766 | gboolean is_downgrade; |
Richard Hughes | db468ee | 2016-06-29 10:10:47 +0100 | [diff] [blame] | 767 | gint vercmp; |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 768 | |
| 769 | /* find from guid */ |
| 770 | app = fu_main_store_get_app_by_guids (helper->store, device); |
| 771 | if (app == NULL) { |
| 772 | g_autofree gchar *guid = NULL; |
| 773 | guid = fu_main_get_guids_from_store (helper->store); |
| 774 | g_set_error (error, |
| 775 | FWUPD_ERROR, |
| 776 | FWUPD_ERROR_INVALID_FILE, |
| 777 | "firmware is not for this hw: required %s got %s", |
| 778 | fu_device_get_guid_default (device), guid); |
| 779 | return FALSE; |
| 780 | } |
| 781 | |
| 782 | /* parse the DriverVer */ |
| 783 | rel = as_app_get_release_default (app); |
| 784 | if (rel == NULL) { |
| 785 | g_set_error_literal (error, |
| 786 | FWUPD_ERROR, |
| 787 | FWUPD_ERROR_INVALID_FILE, |
| 788 | "no releases in the firmware component"); |
| 789 | return FALSE; |
| 790 | } |
| 791 | |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 792 | /* no update abilities */ |
| 793 | if (!fu_device_has_flag (device, FU_DEVICE_FLAG_ALLOW_OFFLINE) && |
| 794 | !fu_device_has_flag (device, FU_DEVICE_FLAG_ALLOW_ONLINE)) { |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 795 | g_set_error (error, |
| 796 | FWUPD_ERROR, |
| 797 | FWUPD_ERROR_INTERNAL, |
| 798 | "Device %s does not currently allow updates", |
| 799 | fu_device_get_id (device)); |
| 800 | return FALSE; |
| 801 | } |
| 802 | |
| 803 | /* not in bootloader mode */ |
| 804 | if (fu_device_has_flag (device, FU_DEVICE_FLAG_NEEDS_BOOTLOADER)) { |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 805 | const gchar *caption = NULL; |
| 806 | AsScreenshot *ss = _as_app_get_screenshot_default (app); |
| 807 | if (ss != NULL) |
| 808 | caption = as_screenshot_get_caption (ss, NULL); |
| 809 | if (caption != NULL) { |
| 810 | g_set_error (error, |
| 811 | FWUPD_ERROR, |
| 812 | FWUPD_ERROR_INTERNAL, |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 813 | "Device %s needs to manually be put in update mode: %s", |
| 814 | fu_device_get_name (device), caption); |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 815 | } else { |
| 816 | g_set_error (error, |
| 817 | FWUPD_ERROR, |
| 818 | FWUPD_ERROR_INTERNAL, |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 819 | "Device %s needs to manually be put in update mode", |
| 820 | fu_device_get_name (device)); |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 821 | } |
| 822 | return FALSE; |
| 823 | } |
| 824 | |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 825 | /* get the blob */ |
| 826 | csum_tmp = as_release_get_checksum_by_target (rel, AS_CHECKSUM_TARGET_CONTENT); |
| 827 | tmp = as_checksum_get_filename (csum_tmp); |
| 828 | if (tmp == NULL) { |
| 829 | g_set_error_literal (error, |
| 830 | FWUPD_ERROR, |
| 831 | FWUPD_ERROR_INVALID_FILE, |
| 832 | "no checksum filename"); |
| 833 | return FALSE; |
| 834 | } |
| 835 | |
| 836 | /* not all devices have to use the same blob */ |
| 837 | blob_fw = as_release_get_blob (rel, tmp); |
| 838 | if (blob_fw == NULL) { |
| 839 | g_set_error_literal (error, |
| 840 | FWUPD_ERROR, |
| 841 | FWUPD_ERROR_READ, |
| 842 | "failed to get firmware blob"); |
| 843 | return FALSE; |
| 844 | } |
| 845 | |
| 846 | /* possibly convert the version from 0x to dotted */ |
| 847 | fu_main_vendor_quirk_release_version (app); |
| 848 | |
| 849 | version = as_release_get_version (rel); |
| 850 | fu_device_set_update_version (device, version); |
| 851 | |
| 852 | /* compare to the lowest supported version, if it exists */ |
| 853 | tmp = fu_device_get_version_lowest (device); |
| 854 | if (tmp != NULL && as_utils_vercmp (tmp, version) > 0) { |
| 855 | g_set_error (error, |
| 856 | FWUPD_ERROR, |
| 857 | FWUPD_ERROR_VERSION_NEWER, |
| 858 | "Specified firmware is older than the minimum " |
| 859 | "required version '%s < %s'", tmp, version); |
| 860 | return FALSE; |
| 861 | } |
| 862 | |
| 863 | /* check the device is locked */ |
| 864 | if (fu_device_has_flag (device, FU_DEVICE_FLAG_LOCKED)) { |
| 865 | g_set_error (error, |
| 866 | FWUPD_ERROR, |
| 867 | FWUPD_ERROR_INTERNAL, |
| 868 | "Device %s is locked", |
| 869 | fu_device_get_id (device)); |
| 870 | return FALSE; |
| 871 | } |
| 872 | |
| 873 | /* compare the versions of what we have installed */ |
| 874 | tmp = fu_device_get_version (device); |
| 875 | if (tmp == NULL) { |
| 876 | g_set_error (error, |
| 877 | FWUPD_ERROR, |
| 878 | FWUPD_ERROR_INTERNAL, |
| 879 | "Device %s does not yet have a current version", |
| 880 | fu_device_get_id (device)); |
| 881 | return FALSE; |
| 882 | } |
| 883 | vercmp = as_utils_vercmp (tmp, version); |
| 884 | if (vercmp == 0 && (helper->flags & FWUPD_INSTALL_FLAG_ALLOW_REINSTALL) == 0) { |
| 885 | g_set_error (error, |
| 886 | FWUPD_ERROR, |
| 887 | FWUPD_ERROR_VERSION_SAME, |
| 888 | "Specified firmware is already installed '%s'", |
| 889 | tmp); |
| 890 | return FALSE; |
| 891 | } |
| 892 | is_downgrade = vercmp > 0; |
| 893 | if (is_downgrade && (helper->flags & FWUPD_INSTALL_FLAG_ALLOW_OLDER) == 0) { |
| 894 | g_set_error (error, |
| 895 | FWUPD_ERROR, |
| 896 | FWUPD_ERROR_VERSION_NEWER, |
| 897 | "Specified firmware is older than installed '%s < %s'", |
| 898 | tmp, version); |
| 899 | return FALSE; |
| 900 | } |
| 901 | |
| 902 | /* if any downgrade, we want the global to be true */ |
| 903 | if (is_downgrade) |
| 904 | helper->is_downgrade = is_downgrade; |
| 905 | |
| 906 | /* verify */ |
| 907 | if (!fu_main_get_release_trust_flags (rel, &helper->trust_flags, error)) |
| 908 | return FALSE; |
| 909 | |
| 910 | /* success */ |
| 911 | g_ptr_array_add (helper->blob_fws, g_bytes_ref (blob_fw)); |
| 912 | return TRUE; |
| 913 | } |
| 914 | |
| 915 | static gboolean |
| 916 | fu_main_update_helper (FuMainAuthHelper *helper, GError **error) |
| 917 | { |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 918 | g_autoptr(GError) error_first = NULL; |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 919 | |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 920 | /* load store file which also decompresses firmware */ |
| 921 | fu_main_set_status (helper->priv, FWUPD_STATUS_DECOMPRESSING); |
| 922 | if (!as_store_from_bytes (helper->store, helper->blob_cab, NULL, error)) |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 923 | return FALSE; |
Richard Hughes | d079b1a | 2015-03-06 10:09:55 +0000 | [diff] [blame] | 924 | |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 925 | /* we've specified a specific device; failure is critical */ |
| 926 | if (helper->devices->len > 0) { |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 927 | for (guint i = 0; i < helper->devices->len; i ++) { |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 928 | FuDevice *device = g_ptr_array_index (helper->devices, i); |
| 929 | if (!fu_main_update_helper_for_device (helper, device, error)) |
| 930 | return FALSE; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 931 | } |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 932 | return TRUE; |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 933 | } |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 934 | |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 935 | /* if we've not chosen a device, try and find anything in the |
| 936 | * cabinet 'store' that matches any installed device and is updatable */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 937 | for (guint i = 0; i < helper->priv->devices->len; i++) { |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 938 | AsApp *app; |
| 939 | FuDeviceItem *item; |
| 940 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 941 | |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 942 | /* guid found */ |
| 943 | item = g_ptr_array_index (helper->priv->devices, i); |
| 944 | app = fu_main_store_get_app_by_guids (helper->store, item->device); |
| 945 | if (app == NULL) |
| 946 | continue; |
| 947 | |
| 948 | /* try this device, error not fatal */ |
| 949 | if (!fu_main_update_helper_for_device (helper, |
| 950 | item->device, |
| 951 | &error_local)) { |
| 952 | g_debug ("failed to add %s: %s", |
| 953 | fu_device_get_id (item->device), |
| 954 | error_local->message); |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 955 | |
| 956 | /* save this for later */ |
| 957 | if (error_first == NULL) |
| 958 | error_first = g_error_copy (error_local); |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 959 | continue; |
| 960 | } |
| 961 | |
| 962 | /* success */ |
| 963 | g_ptr_array_add (helper->devices, g_object_ref (item->device)); |
| 964 | } |
Richard Hughes | 346ea88 | 2016-07-04 12:31:06 +0100 | [diff] [blame] | 965 | if (helper->devices->len == 0) { |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 966 | if (error_first != NULL) { |
Richard Hughes | 4fd38c8 | 2016-08-16 17:57:30 +0100 | [diff] [blame] | 967 | g_set_error_literal (error, |
| 968 | FWUPD_ERROR, |
| 969 | FWUPD_ERROR_INVALID_FILE, |
| 970 | error_first->message); |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 971 | } else { |
Richard Hughes | 4fd38c8 | 2016-08-16 17:57:30 +0100 | [diff] [blame] | 972 | g_autofree gchar *guid = NULL; |
| 973 | guid = fu_main_get_guids_from_store (helper->store); |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 974 | g_set_error (error, |
| 975 | FWUPD_ERROR, |
| 976 | FWUPD_ERROR_INVALID_FILE, |
| 977 | "no attached hardware matched %s", |
| 978 | guid); |
| 979 | } |
Richard Hughes | 346ea88 | 2016-07-04 12:31:06 +0100 | [diff] [blame] | 980 | return FALSE; |
| 981 | } |
| 982 | |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 983 | /* sanity check */ |
| 984 | if (helper->devices->len != helper->blob_fws->len) { |
| 985 | g_set_error (error, |
| 986 | FWUPD_ERROR, |
| 987 | FWUPD_ERROR_INTERNAL, |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 988 | "not enough firmware blobs (%u) for devices (%u)", |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 989 | helper->blob_fws->len, |
| 990 | helper->devices->len); |
| 991 | return FALSE; |
| 992 | } |
| 993 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 994 | return TRUE; |
| 995 | } |
| 996 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 997 | static guint |
| 998 | fu_main_dbus_get_uid (FuMainPrivate *priv, const gchar *sender) |
| 999 | { |
| 1000 | guint uid; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1001 | g_autoptr(GError) error = NULL; |
| 1002 | g_autoptr(GVariant) value = NULL; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1003 | |
| 1004 | if (priv->proxy_uid == NULL) |
| 1005 | return G_MAXUINT; |
| 1006 | value = g_dbus_proxy_call_sync (priv->proxy_uid, |
| 1007 | "GetConnectionUnixUser", |
| 1008 | g_variant_new ("(s)", sender), |
| 1009 | G_DBUS_CALL_FLAGS_NONE, |
| 1010 | -1, |
| 1011 | NULL, |
| 1012 | &error); |
| 1013 | if (value == NULL) { |
| 1014 | g_warning ("Failed to get uid for %s: %s", |
| 1015 | sender, error->message); |
| 1016 | return G_MAXUINT; |
| 1017 | } |
| 1018 | g_variant_get (value, "(u)", &uid); |
| 1019 | return uid; |
| 1020 | } |
| 1021 | |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1022 | static FuDeviceItem * |
| 1023 | fu_main_get_item_by_id_fallback_pending (FuMainPrivate *priv, const gchar *id, GError **error) |
| 1024 | { |
| 1025 | FuDevice *dev; |
| 1026 | FuProvider *provider; |
| 1027 | FuDeviceItem *item = NULL; |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1028 | FwupdUpdateState update_state; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1029 | const gchar *tmp; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1030 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1031 | |
| 1032 | /* not a wildcard */ |
| 1033 | if (g_strcmp0 (id, FWUPD_DEVICE_ID_ANY) != 0) { |
| 1034 | item = fu_main_get_item_by_id (priv, id); |
| 1035 | if (item == NULL) { |
| 1036 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 1037 | FWUPD_ERROR, |
| 1038 | FWUPD_ERROR_NOT_FOUND, |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1039 | "no suitable device found for %s", id); |
| 1040 | } |
| 1041 | return item; |
| 1042 | } |
| 1043 | |
| 1044 | /* allow '*' for any */ |
| 1045 | devices = fu_pending_get_devices (priv->pending, error); |
| 1046 | if (devices == NULL) |
| 1047 | return NULL; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1048 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1049 | dev = g_ptr_array_index (devices, i); |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1050 | update_state = fu_device_get_update_state (dev); |
| 1051 | if (update_state == FWUPD_UPDATE_STATE_UNKNOWN) |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1052 | continue; |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1053 | if (update_state == FWUPD_UPDATE_STATE_PENDING) |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1054 | continue; |
| 1055 | |
| 1056 | /* if the device is not still connected, fake a FuDeviceItem */ |
| 1057 | item = fu_main_get_item_by_id (priv, fu_device_get_id (dev)); |
| 1058 | if (item == NULL) { |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1059 | tmp = fu_device_get_provider (dev); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1060 | provider = fu_main_get_provider_by_name (priv, tmp); |
| 1061 | if (provider == NULL) { |
| 1062 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 1063 | FWUPD_ERROR, |
| 1064 | FWUPD_ERROR_NOT_FOUND, |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1065 | "no provider %s found", tmp); |
| 1066 | } |
| 1067 | item = g_new0 (FuDeviceItem, 1); |
| 1068 | item->device = g_object_ref (dev); |
| 1069 | item->provider = g_object_ref (provider); |
| 1070 | g_ptr_array_add (priv->devices, item); |
| 1071 | |
| 1072 | /* FIXME: just a boolean on FuDeviceItem? */ |
| 1073 | fu_device_set_metadata (dev, "FakeDevice", "TRUE"); |
| 1074 | } |
| 1075 | break; |
| 1076 | } |
| 1077 | |
| 1078 | /* no device found */ |
| 1079 | if (item == NULL) { |
| 1080 | g_set_error_literal (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 1081 | FWUPD_ERROR, |
| 1082 | FWUPD_ERROR_NOT_FOUND, |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1083 | "no suitable devices found"); |
| 1084 | } |
| 1085 | return item; |
| 1086 | } |
| 1087 | |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1088 | static const gchar * |
| 1089 | fu_main_get_action_id_for_device (FuMainAuthHelper *helper) |
| 1090 | { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1091 | gboolean all_removable = TRUE; |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1092 | gboolean is_trusted; |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1093 | |
| 1094 | /* only test the payload */ |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1095 | is_trusted = (helper->trust_flags & FWUPD_TRUST_FLAG_PAYLOAD) > 0; |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1096 | |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1097 | /* any non-removable means false */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1098 | for (guint i = 0; i < helper->devices->len; i ++) { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1099 | FuDevice *device = g_ptr_array_index (helper->devices, i); |
| 1100 | if (fu_device_has_flag (device, FU_DEVICE_FLAG_INTERNAL)) { |
| 1101 | all_removable = FALSE; |
| 1102 | break; |
| 1103 | } |
| 1104 | } |
| 1105 | |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1106 | /* relax authentication checks for removable devices */ |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1107 | if (all_removable) { |
Richard Hughes | db468ee | 2016-06-29 10:10:47 +0100 | [diff] [blame] | 1108 | if (helper->is_downgrade) |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1109 | return "org.freedesktop.fwupd.downgrade-hotplug"; |
| 1110 | if (is_trusted) |
| 1111 | return "org.freedesktop.fwupd.update-hotplug-trusted"; |
| 1112 | return "org.freedesktop.fwupd.update-hotplug"; |
| 1113 | } |
| 1114 | |
| 1115 | /* internal device */ |
Richard Hughes | db468ee | 2016-06-29 10:10:47 +0100 | [diff] [blame] | 1116 | if (helper->is_downgrade) |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1117 | return "org.freedesktop.fwupd.downgrade-internal"; |
| 1118 | if (is_trusted) |
| 1119 | return "org.freedesktop.fwupd.update-internal-trusted"; |
| 1120 | return "org.freedesktop.fwupd.update-internal"; |
| 1121 | } |
| 1122 | |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1123 | static gboolean |
Mario Limonciello | 3ed5447 | 2015-07-23 13:19:39 -0500 | [diff] [blame] | 1124 | fu_main_daemon_update_metadata (FuMainPrivate *priv, gint fd, gint fd_sig, GError **error) |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1125 | { |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1126 | const guint8 *data; |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1127 | gsize size; |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1128 | GPtrArray *apps; |
Richard Hughes | bb840ce | 2015-10-30 08:47:24 +0000 | [diff] [blame] | 1129 | g_autofree gchar *xml = NULL; |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1130 | g_autoptr(AsStore) store = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1131 | g_autoptr(GBytes) bytes = NULL; |
| 1132 | g_autoptr(GBytes) bytes_raw = NULL; |
| 1133 | g_autoptr(GBytes) bytes_sig = NULL; |
| 1134 | g_autoptr(FuKeyring) kr = NULL; |
| 1135 | g_autoptr(GConverter) converter = NULL; |
| 1136 | g_autoptr(GFile) file = NULL; |
| 1137 | g_autoptr(GInputStream) stream_buf = NULL; |
| 1138 | g_autoptr(GInputStream) stream_fd = NULL; |
| 1139 | g_autoptr(GInputStream) stream = NULL; |
| 1140 | g_autoptr(GInputStream) stream_sig = NULL; |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1141 | |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1142 | /* read the entire file into memory */ |
| 1143 | stream_fd = g_unix_input_stream_new (fd, TRUE); |
| 1144 | bytes_raw = g_input_stream_read_bytes (stream_fd, 0x100000, NULL, error); |
| 1145 | if (bytes_raw == NULL) |
| 1146 | return FALSE; |
| 1147 | stream_buf = g_memory_input_stream_new (); |
| 1148 | g_memory_input_stream_add_bytes (G_MEMORY_INPUT_STREAM (stream_buf), bytes_raw); |
| 1149 | |
| 1150 | /* peek the file type and get data */ |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1151 | data = g_bytes_get_data (bytes_raw, &size); |
| 1152 | if (size < 2) { |
| 1153 | g_set_error_literal (error, |
| 1154 | FWUPD_ERROR, |
| 1155 | FWUPD_ERROR_INVALID_FILE, |
| 1156 | "file is too small"); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1157 | return FALSE; |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1158 | } |
| 1159 | if (data[0] == 0x1f && data[1] == 0x8b) { |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1160 | g_debug ("using GZip decompressor for data"); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1161 | converter = G_CONVERTER (g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP)); |
| 1162 | stream = g_converter_input_stream_new (stream_buf, converter); |
| 1163 | bytes = g_input_stream_read_bytes (stream, 0x100000, NULL, error); |
| 1164 | if (bytes == NULL) |
| 1165 | return FALSE; |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1166 | } else if (data[0] == '<' && data[1] == '?') { |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1167 | g_debug ("using no decompressor for data"); |
| 1168 | bytes = g_bytes_ref (bytes_raw); |
| 1169 | } else { |
| 1170 | g_set_error (error, |
| 1171 | FWUPD_ERROR, |
| 1172 | FWUPD_ERROR_INVALID_FILE, |
| 1173 | "file type '0x%02x,0x%02x' not supported", |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1174 | data[0], data[1]); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1175 | return FALSE; |
| 1176 | } |
| 1177 | |
| 1178 | /* read signature */ |
| 1179 | stream_sig = g_unix_input_stream_new (fd_sig, TRUE); |
| 1180 | bytes_sig = g_input_stream_read_bytes (stream_sig, 0x800, NULL, error); |
| 1181 | if (bytes_sig == NULL) |
| 1182 | return FALSE; |
| 1183 | |
| 1184 | /* verify file */ |
| 1185 | kr = fu_keyring_new (); |
| 1186 | if (!fu_keyring_add_public_keys (kr, "/etc/pki/fwupd-metadata", error)) |
| 1187 | return FALSE; |
| 1188 | if (!fu_keyring_verify_data (kr, bytes_raw, bytes_sig, error)) |
| 1189 | return FALSE; |
| 1190 | |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1191 | /* load the store locally until we know it is valid */ |
| 1192 | store = as_store_new (); |
Richard Hughes | bb840ce | 2015-10-30 08:47:24 +0000 | [diff] [blame] | 1193 | data = g_bytes_get_data (bytes, &size); |
| 1194 | xml = g_strndup ((const gchar *) data, size); |
| 1195 | if (!as_store_from_xml (store, xml, NULL, error)) |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1196 | return FALSE; |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1197 | |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1198 | /* add the new application from the store */ |
| 1199 | as_store_remove_all (priv->store); |
| 1200 | apps = as_store_get_apps (store); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1201 | for (guint i = 0; i < apps->len; i++) { |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1202 | AsApp *app = g_ptr_array_index (apps, i); |
| 1203 | as_store_add_app (priv->store, app); |
| 1204 | } |
| 1205 | |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1206 | /* save the new file */ |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1207 | as_store_set_api_version (priv->store, 0.9); |
| 1208 | file = g_file_new_for_path ("/var/cache/app-info/xmls/fwupd.xml"); |
| 1209 | if (!as_store_to_file (priv->store, file, |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1210 | AS_NODE_TO_XML_FLAG_ADD_HEADER | |
| 1211 | AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE | |
| 1212 | AS_NODE_TO_XML_FLAG_FORMAT_INDENT, |
| 1213 | NULL, error)) { |
| 1214 | return FALSE; |
| 1215 | } |
| 1216 | |
| 1217 | return TRUE; |
| 1218 | } |
| 1219 | |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1220 | static gboolean |
| 1221 | fu_main_store_delay_cb (gpointer user_data) |
| 1222 | { |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1223 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1224 | GPtrArray *apps; |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1225 | |
Richard Hughes | dde7a2f | 2016-04-28 15:06:52 +0100 | [diff] [blame] | 1226 | /* print what we've got */ |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1227 | apps = as_store_get_apps (priv->store); |
Richard Hughes | 0a36f44 | 2016-01-06 14:57:20 +0000 | [diff] [blame] | 1228 | if (apps->len == 0) { |
| 1229 | g_debug ("no devices in store"); |
| 1230 | } else { |
| 1231 | g_debug ("devices now in store:"); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1232 | for (guint i = 0; i < apps->len; i++) { |
| 1233 | AsApp *app = g_ptr_array_index (apps, i); |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 1234 | g_debug ("%u\t%s\t%s", i + 1, |
Richard Hughes | 0a36f44 | 2016-01-06 14:57:20 +0000 | [diff] [blame] | 1235 | as_app_get_id (app), |
| 1236 | as_app_get_name (app, NULL)); |
| 1237 | } |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1238 | } |
Richard Hughes | dde7a2f | 2016-04-28 15:06:52 +0100 | [diff] [blame] | 1239 | |
| 1240 | /* are any devices now supported? */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1241 | for (guint i = 0; i < priv->devices->len; i++) { |
| 1242 | FuDeviceItem *item = g_ptr_array_index (priv->devices, i); |
Richard Hughes | dde7a2f | 2016-04-28 15:06:52 +0100 | [diff] [blame] | 1243 | if (fu_main_get_updates_item_update (priv, item)) |
| 1244 | fu_main_emit_device_changed (priv, item->device); |
| 1245 | } |
| 1246 | |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1247 | priv->store_changed_id = 0; |
| 1248 | return G_SOURCE_REMOVE; |
| 1249 | } |
| 1250 | |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1251 | static void |
| 1252 | fu_main_store_changed_cb (AsStore *store, FuMainPrivate *priv) |
| 1253 | { |
| 1254 | if (priv->store_changed_id != 0) |
| 1255 | return; |
| 1256 | priv->store_changed_id = g_timeout_add (200, fu_main_store_delay_cb, priv); |
| 1257 | } |
| 1258 | |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1259 | static gboolean |
| 1260 | fu_main_get_updates_item_update (FuMainPrivate *priv, FuDeviceItem *item) |
| 1261 | { |
| 1262 | AsApp *app; |
| 1263 | AsChecksum *csum; |
| 1264 | AsRelease *rel; |
| 1265 | GPtrArray *releases; |
| 1266 | const gchar *tmp; |
| 1267 | const gchar *version; |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1268 | g_autoptr(GPtrArray) updates_list = NULL; |
| 1269 | |
| 1270 | /* get device version */ |
| 1271 | version = fu_device_get_version (item->device); |
| 1272 | if (version == NULL) |
| 1273 | return FALSE; |
| 1274 | |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 1275 | /* match the GUIDs in the XML */ |
| 1276 | app = fu_main_store_get_app_by_guids (priv->store, item->device); |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1277 | if (app == NULL) |
| 1278 | return FALSE; |
| 1279 | |
| 1280 | /* possibly convert the version from 0x to dotted */ |
| 1281 | fu_main_vendor_quirk_release_version (app); |
| 1282 | |
| 1283 | /* get latest release */ |
| 1284 | rel = as_app_get_release_default (app); |
| 1285 | if (rel == NULL) { |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 1286 | g_debug ("%s [%s] has no firmware update metadata", |
| 1287 | fu_device_get_id (item->device), |
| 1288 | fu_device_get_name (item->device)); |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1289 | return FALSE; |
| 1290 | } |
| 1291 | |
Richard Hughes | 4f4e1f3 | 2016-04-28 14:29:29 +0100 | [diff] [blame] | 1292 | /* supported in metadata */ |
| 1293 | fwupd_result_add_device_flag (FWUPD_RESULT (item->device), |
| 1294 | FU_DEVICE_FLAG_SUPPORTED); |
| 1295 | |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1296 | /* check if actually newer than what we have installed */ |
| 1297 | if (as_utils_vercmp (as_release_get_version (rel), version) <= 0) { |
| 1298 | g_debug ("%s has no firmware updates", |
| 1299 | fu_device_get_id (item->device)); |
| 1300 | return FALSE; |
| 1301 | } |
| 1302 | |
Richard Hughes | 6561c8d | 2016-05-17 16:24:09 +0100 | [diff] [blame] | 1303 | /* only show devices that can be updated */ |
| 1304 | if (!fu_device_has_flag (item->device, FU_DEVICE_FLAG_ALLOW_OFFLINE) && |
| 1305 | !fu_device_has_flag (item->device, FU_DEVICE_FLAG_ALLOW_ONLINE)) { |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 1306 | g_debug ("ignoring %s [%s] as not updatable live or offline", |
| 1307 | fu_device_get_id (item->device), |
| 1308 | fu_device_get_name (item->device)); |
Richard Hughes | 6561c8d | 2016-05-17 16:24:09 +0100 | [diff] [blame] | 1309 | return FALSE; |
| 1310 | } |
| 1311 | |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1312 | /* can we only do this on AC power */ |
| 1313 | if (fu_device_has_flag (item->device, FU_DEVICE_FLAG_REQUIRE_AC) && |
| 1314 | fu_main_on_battery (priv)) { |
| 1315 | g_debug ("ignoring update for %s as not on AC power", |
| 1316 | fu_device_get_id (item->device)); |
| 1317 | return FALSE; |
| 1318 | } |
| 1319 | |
| 1320 | /* add application metadata */ |
| 1321 | fu_device_set_update_id (item->device, as_app_get_id (app)); |
| 1322 | tmp = as_app_get_developer_name (app, NULL); |
| 1323 | if (tmp != NULL) |
| 1324 | fu_device_set_update_vendor (item->device, tmp); |
| 1325 | tmp = as_app_get_name (app, NULL); |
| 1326 | if (tmp != NULL) |
| 1327 | fu_device_set_update_name (item->device, tmp); |
| 1328 | tmp = as_app_get_comment (app, NULL); |
| 1329 | if (tmp != NULL) |
| 1330 | fu_device_set_update_summary (item->device, tmp); |
| 1331 | tmp = as_app_get_description (app, NULL); |
| 1332 | if (tmp != NULL) |
| 1333 | fu_device_set_description (item->device, tmp); |
| 1334 | tmp = as_app_get_url_item (app, AS_URL_KIND_HOMEPAGE); |
| 1335 | if (tmp != NULL) |
| 1336 | fu_device_set_update_homepage (item->device, tmp); |
| 1337 | tmp = as_app_get_project_license (app); |
| 1338 | if (tmp != NULL) |
| 1339 | fu_device_set_update_license (item->device, tmp); |
| 1340 | |
| 1341 | /* add release information */ |
| 1342 | tmp = as_release_get_version (rel); |
| 1343 | if (tmp != NULL) |
| 1344 | fu_device_set_update_version (item->device, tmp); |
| 1345 | csum = as_release_get_checksum_by_target (rel, AS_CHECKSUM_TARGET_CONTAINER); |
| 1346 | if (csum != NULL) { |
| 1347 | fu_device_set_update_checksum (item->device, |
| 1348 | as_checksum_get_value (csum)); |
| 1349 | } |
| 1350 | tmp = as_release_get_location_default (rel); |
| 1351 | if (tmp != NULL) |
| 1352 | fu_device_set_update_uri (item->device, tmp); |
| 1353 | |
| 1354 | /* get the list of releases newer than the one installed */ |
| 1355 | updates_list = g_ptr_array_new (); |
| 1356 | releases = as_app_get_releases (app); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1357 | for (guint i = 0; i < releases->len; i++) { |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1358 | rel = g_ptr_array_index (releases, i); |
Richard Hughes | ae87438 | 2016-08-17 14:23:05 +0100 | [diff] [blame] | 1359 | if (as_utils_vercmp (as_release_get_version (rel), version) <= 0) |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1360 | continue; |
| 1361 | tmp = as_release_get_description (rel, NULL); |
| 1362 | if (tmp == NULL) |
| 1363 | continue; |
| 1364 | g_ptr_array_add (updates_list, rel); |
| 1365 | } |
| 1366 | |
| 1367 | /* no prefix on each release */ |
| 1368 | if (updates_list->len == 1) { |
| 1369 | rel = g_ptr_array_index (updates_list, 0); |
| 1370 | fu_device_set_update_description (item->device, |
| 1371 | as_release_get_description (rel, NULL)); |
| 1372 | } else { |
| 1373 | g_autoptr(GString) update_desc = NULL; |
| 1374 | update_desc = g_string_new (""); |
| 1375 | |
| 1376 | /* get the descriptions with a version prefix */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1377 | for (guint i = 0; i < updates_list->len; i++) { |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1378 | rel = g_ptr_array_index (updates_list, i); |
| 1379 | g_string_append_printf (update_desc, |
| 1380 | "<p>%s:</p>%s", |
| 1381 | as_release_get_version (rel), |
| 1382 | as_release_get_description (rel, NULL)); |
| 1383 | } |
| 1384 | if (update_desc->len > 0) |
| 1385 | fu_device_set_update_description (item->device, update_desc->str); |
| 1386 | } |
| 1387 | |
| 1388 | /* success */ |
| 1389 | return TRUE; |
| 1390 | } |
| 1391 | |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1392 | /* find any updates using the AppStream metadata */ |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1393 | static GPtrArray * |
| 1394 | fu_main_get_updates (FuMainPrivate *priv, GError **error) |
| 1395 | { |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1396 | GPtrArray *updates = g_ptr_array_new (); |
| 1397 | for (guint i = 0; i < priv->devices->len; i++) { |
| 1398 | FuDeviceItem *item = g_ptr_array_index (priv->devices, i); |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1399 | if (fu_main_get_updates_item_update (priv, item)) |
| 1400 | g_ptr_array_add (updates, item); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1401 | } |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1402 | return updates; |
| 1403 | } |
| 1404 | |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1405 | static AsStore * |
| 1406 | fu_main_get_store_from_fd (FuMainPrivate *priv, gint fd, GError **error) |
| 1407 | { |
| 1408 | g_autoptr(AsStore) store = NULL; |
| 1409 | g_autoptr(GBytes) blob_cab = NULL; |
| 1410 | g_autoptr(GError) error_local = NULL; |
| 1411 | g_autoptr(GInputStream) stream = NULL; |
| 1412 | |
| 1413 | /* read the entire fd to a data blob */ |
| 1414 | stream = g_unix_input_stream_new (fd, TRUE); |
| 1415 | blob_cab = g_input_stream_read_bytes (stream, |
| 1416 | FU_MAIN_FIRMWARE_SIZE_MAX, |
| 1417 | NULL, &error_local); |
| 1418 | if (blob_cab == NULL){ |
| 1419 | g_set_error_literal (error, |
| 1420 | FWUPD_ERROR, |
| 1421 | FWUPD_ERROR_INVALID_FILE, |
| 1422 | error_local->message); |
| 1423 | return NULL; |
| 1424 | } |
| 1425 | |
| 1426 | /* load file */ |
| 1427 | store = as_store_new (); |
| 1428 | if (!as_store_from_bytes (store, blob_cab, NULL, &error_local)) { |
| 1429 | g_set_error_literal (error, |
| 1430 | FWUPD_ERROR, |
| 1431 | FWUPD_ERROR_INVALID_FILE, |
| 1432 | error_local->message); |
| 1433 | return NULL; |
| 1434 | } |
| 1435 | return g_steal_pointer (&store); |
| 1436 | } |
| 1437 | |
| 1438 | static FwupdResult * |
| 1439 | fu_main_get_result_from_app (FuMainPrivate *priv, AsApp *app, GError **error) |
| 1440 | { |
| 1441 | FwupdTrustFlags trust_flags = FWUPD_TRUST_FLAG_NONE; |
| 1442 | AsRelease *rel; |
Mario Limonciello | 8eaadd0 | 2016-06-17 16:23:44 -0500 | [diff] [blame] | 1443 | AsChecksum * csum_tmp; |
| 1444 | const gchar *fn; |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1445 | GPtrArray *provides; |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1446 | g_autoptr(FwupdResult) res = NULL; |
| 1447 | |
| 1448 | res = fwupd_result_new (); |
| 1449 | provides = as_app_get_provides (app); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1450 | for (guint i = 0; i < provides->len; i++) { |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1451 | AsProvide *prov = AS_PROVIDE (g_ptr_array_index (provides, i)); |
| 1452 | FuDeviceItem *item; |
| 1453 | const gchar *guid; |
| 1454 | |
| 1455 | /* not firmware */ |
| 1456 | if (as_provide_get_kind (prov) != AS_PROVIDE_KIND_FIRMWARE_FLASHED) |
| 1457 | continue; |
| 1458 | |
| 1459 | /* is a online or offline update appropriate */ |
| 1460 | guid = as_provide_get_value (prov); |
| 1461 | if (guid == NULL) |
| 1462 | continue; |
| 1463 | item = fu_main_get_item_by_guid (priv, guid); |
| 1464 | if (item != NULL) { |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1465 | fwupd_result_set_device_flags (res, fu_device_get_flags (item->device)); |
| 1466 | fwupd_result_set_device_id (res, fu_device_get_id (item->device)); |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /* add GUID */ |
| 1470 | fwupd_result_add_guid (res, guid); |
| 1471 | } |
| 1472 | if (fwupd_result_get_guids(res)->len == 0) { |
| 1473 | g_set_error_literal (error, |
| 1474 | FWUPD_ERROR, |
| 1475 | FWUPD_ERROR_INTERNAL, |
| 1476 | "component has no GUIDs"); |
| 1477 | return NULL; |
| 1478 | } |
| 1479 | |
| 1480 | /* verify trust */ |
| 1481 | rel = as_app_get_release_default (app); |
| 1482 | if (!fu_main_get_release_trust_flags (rel, &trust_flags, error)) |
| 1483 | return NULL; |
| 1484 | |
| 1485 | /* possibly convert the version from 0x to dotted */ |
| 1486 | fu_main_vendor_quirk_release_version (app); |
| 1487 | |
| 1488 | /* create a result with all the metadata in */ |
| 1489 | fwupd_result_set_device_description (res, as_app_get_description (app, NULL)); |
Richard Hughes | 4921bd9 | 2016-06-27 11:17:43 +0100 | [diff] [blame] | 1490 | fwupd_result_set_update_id (res, as_app_get_id (app)); |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1491 | fwupd_result_set_update_description (res, as_release_get_description (rel, NULL)); |
| 1492 | fwupd_result_set_update_homepage (res, as_app_get_url_item (app, AS_URL_KIND_HOMEPAGE)); |
| 1493 | fwupd_result_set_update_license (res, as_app_get_project_license (app)); |
| 1494 | fwupd_result_set_update_name (res, as_app_get_name (app, NULL)); |
| 1495 | fwupd_result_set_update_size (res, as_release_get_size (rel, AS_SIZE_KIND_INSTALLED)); |
| 1496 | fwupd_result_set_update_summary (res, as_app_get_comment (app, NULL)); |
| 1497 | fwupd_result_set_update_trust_flags (res, trust_flags); |
| 1498 | fwupd_result_set_update_vendor (res, as_app_get_developer_name (app, NULL)); |
| 1499 | fwupd_result_set_update_version (res, as_release_get_version (rel)); |
Mario Limonciello | 8eaadd0 | 2016-06-17 16:23:44 -0500 | [diff] [blame] | 1500 | csum_tmp = as_release_get_checksum_by_target (rel, |
| 1501 | AS_CHECKSUM_TARGET_CONTENT); |
| 1502 | fn = as_checksum_get_filename (csum_tmp); |
| 1503 | if (fn != NULL) |
| 1504 | fwupd_result_set_update_filename (res, fn); |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1505 | return g_steal_pointer (&res); |
| 1506 | } |
| 1507 | |
| 1508 | static GVariant * |
| 1509 | fu_main_get_details_from_fd (FuMainPrivate *priv, gint fd, GError **error) |
| 1510 | { |
| 1511 | AsApp *app = NULL; |
| 1512 | GPtrArray *apps; |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1513 | g_autoptr(AsStore) store = NULL; |
| 1514 | g_autoptr(FwupdResult) res = NULL; |
| 1515 | |
| 1516 | store = fu_main_get_store_from_fd (priv, fd, error); |
| 1517 | if (store == NULL) |
| 1518 | return NULL; |
| 1519 | |
| 1520 | /* get all apps */ |
| 1521 | apps = as_store_get_apps (store); |
| 1522 | if (apps->len == 0) { |
| 1523 | g_set_error_literal (error, |
| 1524 | FWUPD_ERROR, |
| 1525 | FWUPD_ERROR_INVALID_FILE, |
| 1526 | "no components"); |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | if (apps->len > 1) { |
| 1530 | /* we've got a .cab file with multiple components, |
| 1531 | * so try to find the first thing that's installed */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1532 | for (guint i = 0; i < priv->devices->len; i++) { |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1533 | FuDeviceItem *item = g_ptr_array_index (priv->devices, i); |
| 1534 | app = fu_main_store_get_app_by_guids (store, item->device); |
| 1535 | if (app != NULL) |
| 1536 | break; |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | /* well, we've tried our best, just show the first entry */ |
| 1541 | if (app == NULL) |
| 1542 | app = AS_APP (g_ptr_array_index (apps, 0)); |
| 1543 | |
| 1544 | /* create a result with all the metadata in */ |
| 1545 | res = fu_main_get_result_from_app (priv, app, error); |
| 1546 | if (res == NULL) |
| 1547 | return NULL; |
| 1548 | return fwupd_result_to_data (res, "(a{sv})"); |
| 1549 | } |
| 1550 | |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1551 | static GVariant * |
| 1552 | fu_main_get_details_local_from_fd (FuMainPrivate *priv, gint fd, GError **error) |
| 1553 | { |
| 1554 | GPtrArray *apps; |
| 1555 | GVariantBuilder builder; |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1556 | g_autoptr(AsStore) store = NULL; |
| 1557 | |
| 1558 | store = fu_main_get_store_from_fd (priv, fd, error); |
| 1559 | if (store == NULL) |
| 1560 | return NULL; |
| 1561 | |
| 1562 | /* get all apps */ |
| 1563 | apps = as_store_get_apps (store); |
| 1564 | if (apps->len == 0) { |
| 1565 | g_set_error_literal (error, |
| 1566 | FWUPD_ERROR, |
| 1567 | FWUPD_ERROR_INVALID_FILE, |
| 1568 | "no components"); |
| 1569 | return NULL; |
| 1570 | } |
| 1571 | |
| 1572 | /* create results with all the metadata in */ |
| 1573 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1574 | for (guint i = 0; i < apps->len; i++) { |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1575 | g_autoptr(FwupdResult) res = NULL; |
| 1576 | AsApp *app = g_ptr_array_index (apps, i); |
| 1577 | GVariant *tmp; |
| 1578 | res = fu_main_get_result_from_app (priv, app, error); |
| 1579 | if (res == NULL) |
| 1580 | return NULL; |
| 1581 | tmp = fwupd_result_to_data (res, "{sa{sv}}"); |
| 1582 | g_variant_builder_add_value (&builder, tmp); |
| 1583 | } |
| 1584 | return g_variant_new ("(a{sa{sv}})", &builder); |
| 1585 | } |
| 1586 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1587 | static void |
| 1588 | fu_main_daemon_method_call (GDBusConnection *connection, const gchar *sender, |
| 1589 | const gchar *object_path, const gchar *interface_name, |
| 1590 | const gchar *method_name, GVariant *parameters, |
| 1591 | GDBusMethodInvocation *invocation, gpointer user_data) |
| 1592 | { |
| 1593 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1594 | GVariant *val; |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1595 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1596 | |
| 1597 | /* return 'as' */ |
| 1598 | if (g_strcmp0 (method_name, "GetDevices") == 0) { |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1599 | g_debug ("Called %s()", method_name); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1600 | val = fu_main_device_array_to_variant (priv->devices, &error); |
| 1601 | if (val == NULL) { |
Richard Hughes | 9d76a87 | 2015-09-17 12:49:07 +0100 | [diff] [blame] | 1602 | if (g_error_matches (error, |
| 1603 | FWUPD_ERROR, |
| 1604 | FWUPD_ERROR_NOTHING_TO_DO)) { |
| 1605 | g_prefix_error (&error, "No detected devices: "); |
| 1606 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1607 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1608 | return; |
| 1609 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1610 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1611 | return; |
| 1612 | } |
| 1613 | |
| 1614 | /* return 'as' */ |
| 1615 | if (g_strcmp0 (method_name, "GetUpdates") == 0) { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1616 | g_autoptr(GPtrArray) updates = NULL; |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1617 | g_debug ("Called %s()", method_name); |
| 1618 | updates = fu_main_get_updates (priv, &error); |
| 1619 | if (updates == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1620 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1621 | return; |
| 1622 | } |
| 1623 | val = fu_main_device_array_to_variant (updates, &error); |
Richard Hughes | 9a38c03 | 2015-03-17 20:58:46 +0000 | [diff] [blame] | 1624 | if (val == NULL) { |
Richard Hughes | 9d76a87 | 2015-09-17 12:49:07 +0100 | [diff] [blame] | 1625 | if (g_error_matches (error, |
| 1626 | FWUPD_ERROR, |
| 1627 | FWUPD_ERROR_NOTHING_TO_DO)) { |
| 1628 | g_prefix_error (&error, "No devices can be updated: "); |
| 1629 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1630 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 9a38c03 | 2015-03-17 20:58:46 +0000 | [diff] [blame] | 1631 | return; |
| 1632 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1633 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1634 | return; |
| 1635 | } |
| 1636 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1637 | /* return '' */ |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1638 | if (g_strcmp0 (method_name, "ClearResults") == 0) { |
| 1639 | FuDeviceItem *item = NULL; |
| 1640 | const gchar *id = NULL; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1641 | |
| 1642 | g_variant_get (parameters, "(&s)", &id); |
| 1643 | g_debug ("Called %s(%s)", method_name, id); |
| 1644 | |
| 1645 | /* find device */ |
| 1646 | item = fu_main_get_item_by_id_fallback_pending (priv, id, &error); |
| 1647 | if (item == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1648 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | /* call into the provider */ |
| 1653 | if (!fu_provider_clear_results (item->provider, item->device, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1654 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1655 | return; |
| 1656 | } |
| 1657 | |
| 1658 | /* success */ |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1659 | fu_main_invocation_return_value (priv, invocation, NULL); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | /* return 'a{sv}' */ |
| 1664 | if (g_strcmp0 (method_name, "GetResults") == 0) { |
| 1665 | FuDeviceItem *item = NULL; |
| 1666 | const gchar *id = NULL; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1667 | |
| 1668 | g_variant_get (parameters, "(&s)", &id); |
| 1669 | g_debug ("Called %s(%s)", method_name, id); |
| 1670 | |
| 1671 | /* find device */ |
| 1672 | item = fu_main_get_item_by_id_fallback_pending (priv, id, &error); |
| 1673 | if (item == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1674 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1675 | return; |
| 1676 | } |
| 1677 | |
| 1678 | /* call into the provider */ |
| 1679 | if (!fu_provider_get_results (item->provider, item->device, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1680 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1681 | return; |
| 1682 | } |
| 1683 | |
| 1684 | /* success */ |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1685 | val = fwupd_result_to_data (FWUPD_RESULT (item->device), "(a{sv})"); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1686 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1687 | return; |
| 1688 | } |
| 1689 | |
| 1690 | /* return '' */ |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1691 | if (g_strcmp0 (method_name, "UpdateMetadata") == 0) { |
| 1692 | GDBusMessage *message; |
| 1693 | GUnixFDList *fd_list; |
| 1694 | gint fd_data; |
| 1695 | gint fd_sig; |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1696 | |
| 1697 | message = g_dbus_method_invocation_get_message (invocation); |
| 1698 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1699 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 2) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1700 | g_set_error (&error, |
| 1701 | FWUPD_ERROR, |
| 1702 | FWUPD_ERROR_INTERNAL, |
| 1703 | "invalid handle"); |
| 1704 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1705 | return; |
| 1706 | } |
| 1707 | fd_data = g_unix_fd_list_get (fd_list, 0, &error); |
| 1708 | if (fd_data < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1709 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1710 | return; |
| 1711 | } |
| 1712 | fd_sig = g_unix_fd_list_get (fd_list, 1, &error); |
| 1713 | if (fd_sig < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1714 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1715 | return; |
| 1716 | } |
Mario Limonciello | 3ed5447 | 2015-07-23 13:19:39 -0500 | [diff] [blame] | 1717 | if (!fu_main_daemon_update_metadata (priv, fd_data, fd_sig, &error)) { |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1718 | g_prefix_error (&error, "failed to update metadata: "); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1719 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1720 | return; |
| 1721 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1722 | fu_main_invocation_return_value (priv, invocation, NULL); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1723 | return; |
| 1724 | } |
| 1725 | |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1726 | /* return 's' */ |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1727 | if (g_strcmp0 (method_name, "Unlock") == 0) { |
| 1728 | FuDeviceItem *item = NULL; |
| 1729 | FuMainAuthHelper *helper; |
| 1730 | const gchar *id = NULL; |
| 1731 | g_autoptr(PolkitSubject) subject = NULL; |
| 1732 | |
| 1733 | /* check the id exists */ |
| 1734 | g_variant_get (parameters, "(&s)", &id); |
| 1735 | g_debug ("Called %s(%s)", method_name, id); |
| 1736 | item = fu_main_get_item_by_id (priv, id); |
| 1737 | if (item == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1738 | g_set_error (&error, |
| 1739 | FWUPD_ERROR, |
| 1740 | FWUPD_ERROR_NOT_FOUND, |
| 1741 | "No such device %s", id); |
| 1742 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1743 | return; |
| 1744 | } |
| 1745 | |
| 1746 | /* check the device is locked */ |
Richard Hughes | 5bf2837 | 2016-03-29 18:43:54 +0100 | [diff] [blame] | 1747 | if (!fu_device_has_flag (item->device, FU_DEVICE_FLAG_LOCKED)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1748 | g_set_error (&error, |
| 1749 | FWUPD_ERROR, |
| 1750 | FWUPD_ERROR_NOT_FOUND, |
| 1751 | "Device %s is not locked", id); |
| 1752 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1753 | return; |
| 1754 | } |
| 1755 | |
| 1756 | /* process the firmware */ |
| 1757 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1758 | helper->auth_kind = FU_MAIN_AUTH_KIND_UNLOCK; |
| 1759 | helper->invocation = g_object_ref (invocation); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1760 | helper->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1761 | helper->priv = priv; |
| 1762 | |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1763 | /* FIXME: do we want to support "*"? */ |
| 1764 | g_ptr_array_add (helper->devices, g_object_ref (item->device)); |
| 1765 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1766 | /* authenticate */ |
| 1767 | subject = polkit_system_bus_name_new (sender); |
| 1768 | polkit_authority_check_authorization (helper->priv->authority, subject, |
| 1769 | "org.freedesktop.fwupd.device-unlock", |
| 1770 | NULL, |
| 1771 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1772 | NULL, |
| 1773 | fu_main_check_authorization_cb, |
| 1774 | helper); |
| 1775 | return; |
| 1776 | } |
| 1777 | |
| 1778 | /* return 's' */ |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1779 | if (g_strcmp0 (method_name, "Verify") == 0) { |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1780 | AsApp *app; |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1781 | AsChecksum *csum; |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1782 | AsRelease *release; |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1783 | FuDeviceItem *item = NULL; |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1784 | const gchar *hash = NULL; |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1785 | const gchar *id = NULL; |
| 1786 | const gchar *version = NULL; |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1787 | |
| 1788 | /* check the id exists */ |
| 1789 | g_variant_get (parameters, "(&s)", &id); |
| 1790 | g_debug ("Called %s(%s)", method_name, id); |
| 1791 | item = fu_main_get_item_by_id (priv, id); |
| 1792 | if (item == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1793 | g_set_error (&error, |
| 1794 | FWUPD_ERROR, |
| 1795 | FWUPD_ERROR_NOT_FOUND, |
| 1796 | "No such device %s", id); |
| 1797 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1798 | return; |
| 1799 | } |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1800 | |
| 1801 | /* set the device firmware hash */ |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1802 | if (!fu_provider_verify (item->provider, item->device, |
| 1803 | FU_PROVIDER_VERIFY_FLAG_NONE, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1804 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1805 | return; |
| 1806 | } |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1807 | |
| 1808 | /* find component in metadata */ |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 1809 | app = fu_main_store_get_app_by_guids (priv->store, item->device); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1810 | if (app == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1811 | g_set_error_literal (&error, |
| 1812 | FWUPD_ERROR, |
| 1813 | FWUPD_ERROR_NOT_FOUND, |
| 1814 | "No metadata"); |
| 1815 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1816 | return; |
| 1817 | } |
| 1818 | |
| 1819 | /* find version in metadata */ |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1820 | version = fu_device_get_version (item->device); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1821 | release = as_app_get_release (app, version); |
| 1822 | if (release == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1823 | g_set_error (&error, |
| 1824 | FWUPD_ERROR, |
| 1825 | FWUPD_ERROR_NOT_FOUND, |
| 1826 | "No version %s", version); |
| 1827 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1828 | return; |
| 1829 | } |
| 1830 | |
| 1831 | /* find checksum */ |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1832 | csum = as_release_get_checksum_by_target (release, AS_CHECKSUM_TARGET_CONTENT); |
| 1833 | if (csum == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1834 | g_set_error (&error, |
| 1835 | FWUPD_ERROR, |
| 1836 | FWUPD_ERROR_NOT_FOUND, |
| 1837 | "No content checksum for %s", version); |
| 1838 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1839 | return; |
| 1840 | } |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1841 | hash = fu_device_get_checksum (item->device); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1842 | if (g_strcmp0 (as_checksum_get_value (csum), hash) != 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1843 | g_set_error (&error, |
| 1844 | FWUPD_ERROR, |
| 1845 | FWUPD_ERROR_NOT_FOUND, |
| 1846 | "For v%s expected %s, got %s", |
| 1847 | version, |
| 1848 | as_checksum_get_value (csum), |
| 1849 | hash); |
| 1850 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1851 | return; |
| 1852 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1853 | fu_main_invocation_return_value (priv, invocation, NULL); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1854 | return; |
| 1855 | } |
| 1856 | |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1857 | /* return '' */ |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 1858 | if (g_strcmp0 (method_name, "Install") == 0) { |
Richard Hughes | d079b1a | 2015-03-06 10:09:55 +0000 | [diff] [blame] | 1859 | FuDeviceItem *item = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1860 | FuMainAuthHelper *helper; |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 1861 | FwupdInstallFlags flags = FWUPD_INSTALL_FLAG_NONE; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1862 | GDBusMessage *message; |
| 1863 | GUnixFDList *fd_list; |
| 1864 | GVariant *prop_value; |
Richard Hughes | a8e8394 | 2015-03-09 17:19:35 +0000 | [diff] [blame] | 1865 | const gchar *action_id; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1866 | const gchar *id = NULL; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1867 | gchar *prop_key; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1868 | gint32 fd_handle = 0; |
| 1869 | gint fd; |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 1870 | g_autoptr(PolkitSubject) subject = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1871 | g_autoptr(GVariantIter) iter = NULL; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1872 | g_autoptr(GBytes) blob_cab = NULL; |
| 1873 | g_autoptr(GInputStream) stream = NULL; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1874 | |
| 1875 | /* check the id exists */ |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1876 | g_variant_get (parameters, "(&sha{sv})", &id, &fd_handle, &iter); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1877 | g_debug ("Called %s(%s,%i)", method_name, id, fd_handle); |
Richard Hughes | d079b1a | 2015-03-06 10:09:55 +0000 | [diff] [blame] | 1878 | if (g_strcmp0 (id, FWUPD_DEVICE_ID_ANY) != 0) { |
| 1879 | item = fu_main_get_item_by_id (priv, id); |
| 1880 | if (item == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1881 | g_set_error (&error, |
| 1882 | FWUPD_ERROR, |
| 1883 | FWUPD_ERROR_NOT_FOUND, |
| 1884 | "no such device %s", id); |
| 1885 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | d079b1a | 2015-03-06 10:09:55 +0000 | [diff] [blame] | 1886 | return; |
| 1887 | } |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1890 | /* get options */ |
| 1891 | while (g_variant_iter_next (iter, "{&sv}", |
| 1892 | &prop_key, &prop_value)) { |
| 1893 | g_debug ("got option %s", prop_key); |
| 1894 | if (g_strcmp0 (prop_key, "offline") == 0 && |
| 1895 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 1896 | flags |= FWUPD_INSTALL_FLAG_OFFLINE; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1897 | if (g_strcmp0 (prop_key, "allow-older") == 0 && |
| 1898 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 1899 | flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1900 | if (g_strcmp0 (prop_key, "allow-reinstall") == 0 && |
| 1901 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 1902 | flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
Mario Limonciello | 71a5b98 | 2016-05-10 15:38:53 -0500 | [diff] [blame] | 1903 | if (g_strcmp0 (prop_key, "force") == 0 && |
| 1904 | g_variant_get_boolean (prop_value) == TRUE) |
| 1905 | flags |= FWUPD_INSTALL_FLAG_FORCE; |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 1906 | g_variant_unref (prop_value); |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1909 | /* get the fd */ |
| 1910 | message = g_dbus_method_invocation_get_message (invocation); |
| 1911 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1912 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 1) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1913 | g_set_error (&error, |
| 1914 | FWUPD_ERROR, |
| 1915 | FWUPD_ERROR_INTERNAL, |
| 1916 | "invalid handle"); |
| 1917 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1918 | return; |
| 1919 | } |
| 1920 | fd = g_unix_fd_list_get (fd_list, fd_handle, &error); |
| 1921 | if (fd < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1922 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1923 | return; |
| 1924 | } |
| 1925 | |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1926 | /* read the entire fd to a data blob */ |
| 1927 | stream = g_unix_input_stream_new (fd, TRUE); |
| 1928 | blob_cab = g_input_stream_read_bytes (stream, |
| 1929 | FU_MAIN_FIRMWARE_SIZE_MAX, |
| 1930 | NULL, &error); |
| 1931 | if (blob_cab == NULL){ |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1932 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1933 | return; |
| 1934 | } |
| 1935 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 1936 | /* process the firmware */ |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1937 | helper = g_new0 (FuMainAuthHelper, 1); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1938 | helper->auth_kind = FU_MAIN_AUTH_KIND_INSTALL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1939 | helper->invocation = g_object_ref (invocation); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1940 | helper->trust_flags = FWUPD_TRUST_FLAG_NONE; |
| 1941 | helper->blob_cab = g_bytes_ref (blob_cab); |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1942 | helper->flags = flags; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1943 | helper->priv = priv; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1944 | helper->store = as_store_new (); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1945 | helper->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 1946 | helper->blob_fws = g_ptr_array_new_with_free_func ((GDestroyNotify) g_bytes_unref); |
Richard Hughes | d079b1a | 2015-03-06 10:09:55 +0000 | [diff] [blame] | 1947 | if (item != NULL) |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1948 | g_ptr_array_add (helper->devices, g_object_ref (item->device)); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 1949 | if (!fu_main_update_helper (helper, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1950 | fu_main_invocation_return_error (helper->priv, helper->invocation, error); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 1951 | fu_main_helper_free (helper); |
| 1952 | return; |
| 1953 | } |
| 1954 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1955 | /* is root */ |
| 1956 | if (fu_main_dbus_get_uid (priv, sender) == 0) { |
| 1957 | if (!fu_main_provider_update_authenticated (helper, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1958 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1959 | } else { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1960 | fu_main_invocation_return_value (priv, invocation, NULL); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1961 | } |
| 1962 | fu_main_helper_free (helper); |
| 1963 | return; |
| 1964 | } |
| 1965 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 1966 | /* authenticate */ |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1967 | action_id = fu_main_get_action_id_for_device (helper); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1968 | subject = polkit_system_bus_name_new (sender); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 1969 | polkit_authority_check_authorization (helper->priv->authority, subject, |
Richard Hughes | a8e8394 | 2015-03-09 17:19:35 +0000 | [diff] [blame] | 1970 | action_id, |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1971 | NULL, |
| 1972 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1973 | NULL, |
| 1974 | fu_main_check_authorization_cb, |
| 1975 | helper); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1976 | return; |
| 1977 | } |
| 1978 | |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1979 | /* get a single result object from a local file */ |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 1980 | if (g_strcmp0 (method_name, "GetDetails") == 0) { |
| 1981 | GDBusMessage *message; |
| 1982 | GUnixFDList *fd_list; |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 1983 | gint32 fd_handle = 0; |
| 1984 | gint fd; |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 1985 | |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1986 | /* get parameters */ |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 1987 | g_variant_get (parameters, "(h)", &fd_handle); |
| 1988 | g_debug ("Called %s(%i)", method_name, fd_handle); |
| 1989 | |
| 1990 | /* get the fd */ |
| 1991 | message = g_dbus_method_invocation_get_message (invocation); |
| 1992 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1993 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 1) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 1994 | g_set_error (&error, |
| 1995 | FWUPD_ERROR, |
| 1996 | FWUPD_ERROR_INTERNAL, |
| 1997 | "invalid handle"); |
| 1998 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 1999 | return; |
| 2000 | } |
| 2001 | fd = g_unix_fd_list_get (fd_list, fd_handle, &error); |
| 2002 | if (fd < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 2003 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2004 | return; |
| 2005 | } |
| 2006 | |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 2007 | /* get details about the file */ |
| 2008 | val = fu_main_get_details_from_fd (priv, fd, &error); |
| 2009 | if (val == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 2010 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2011 | return; |
| 2012 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 2013 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2014 | return; |
| 2015 | } |
| 2016 | |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2017 | /* get multiple result objects from a local file */ |
| 2018 | if (g_strcmp0 (method_name, "GetDetailsLocal") == 0) { |
| 2019 | GDBusMessage *message; |
| 2020 | GUnixFDList *fd_list; |
| 2021 | gint32 fd_handle = 0; |
| 2022 | gint fd; |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2023 | |
| 2024 | /* get parameters */ |
| 2025 | g_variant_get (parameters, "(h)", &fd_handle); |
| 2026 | g_debug ("Called %s(%i)", method_name, fd_handle); |
| 2027 | |
| 2028 | /* get the fd */ |
| 2029 | message = g_dbus_method_invocation_get_message (invocation); |
| 2030 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 2031 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 1) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 2032 | g_set_error (&error, |
| 2033 | FWUPD_ERROR, |
| 2034 | FWUPD_ERROR_INTERNAL, |
| 2035 | "invalid handle"); |
| 2036 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2037 | return; |
| 2038 | } |
| 2039 | fd = g_unix_fd_list_get (fd_list, fd_handle, &error); |
| 2040 | if (fd < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 2041 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2042 | return; |
| 2043 | } |
| 2044 | |
| 2045 | /* get details about the file */ |
| 2046 | val = fu_main_get_details_local_from_fd (priv, fd, &error); |
| 2047 | if (val == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 2048 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2049 | return; |
| 2050 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 2051 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2052 | return; |
| 2053 | } |
| 2054 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2055 | /* we suck */ |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame^] | 2056 | g_set_error (&error, |
| 2057 | G_DBUS_ERROR, |
| 2058 | G_DBUS_ERROR_UNKNOWN_METHOD, |
| 2059 | "no such method %s", method_name); |
| 2060 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2063 | static GVariant * |
| 2064 | fu_main_daemon_get_property (GDBusConnection *connection_, const gchar *sender, |
| 2065 | const gchar *object_path, const gchar *interface_name, |
| 2066 | const gchar *property_name, GError **error, |
| 2067 | gpointer user_data) |
| 2068 | { |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2069 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2070 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2071 | if (g_strcmp0 (property_name, "DaemonVersion") == 0) |
| 2072 | return g_variant_new_string (VERSION); |
| 2073 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2074 | if (g_strcmp0 (property_name, "Status") == 0) |
Richard Hughes | 37e9d77 | 2016-04-28 14:23:24 +0100 | [diff] [blame] | 2075 | return g_variant_new_uint32 (priv->status); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2076 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2077 | /* return an error */ |
| 2078 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 2079 | G_DBUS_ERROR, |
| 2080 | G_DBUS_ERROR_UNKNOWN_PROPERTY, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2081 | "failed to get daemon property %s", |
| 2082 | property_name); |
| 2083 | return NULL; |
| 2084 | } |
| 2085 | |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 2086 | static void |
| 2087 | fu_main_providers_coldplug (FuMainPrivate *priv) |
| 2088 | { |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2089 | g_autoptr(AsProfileTask) ptask = NULL; |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 2090 | |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2091 | ptask = as_profile_start_literal (priv->profile, "FuMain:coldplug"); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 2092 | for (guint i = 0; i < priv->providers->len; i++) { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 2093 | g_autoptr(GError) error = NULL; |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2094 | g_autoptr(AsProfileTask) ptask2 = NULL; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 2095 | FuProvider *provider = g_ptr_array_index (priv->providers, i); |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2096 | ptask2 = as_profile_start (priv->profile, |
| 2097 | "FuMain:coldplug{%s}", |
| 2098 | fu_provider_get_name (provider)); |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 2099 | if (!fu_provider_coldplug (FU_PROVIDER (provider), &error)) |
| 2100 | g_warning ("Failed to coldplug: %s", error->message); |
| 2101 | } |
| 2102 | } |
| 2103 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2104 | static void |
| 2105 | fu_main_on_bus_acquired_cb (GDBusConnection *connection, |
| 2106 | const gchar *name, |
| 2107 | gpointer user_data) |
| 2108 | { |
| 2109 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2110 | guint registration_id; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 2111 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2112 | static const GDBusInterfaceVTable interface_vtable = { |
| 2113 | fu_main_daemon_method_call, |
| 2114 | fu_main_daemon_get_property, |
| 2115 | NULL |
| 2116 | }; |
| 2117 | |
| 2118 | priv->connection = g_object_ref (connection); |
| 2119 | registration_id = g_dbus_connection_register_object (connection, |
| 2120 | FWUPD_DBUS_PATH, |
| 2121 | priv->introspection_daemon->interfaces[0], |
| 2122 | &interface_vtable, |
| 2123 | priv, /* user_data */ |
| 2124 | NULL, /* user_data_free_func */ |
| 2125 | NULL); /* GError** */ |
| 2126 | g_assert (registration_id > 0); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 2127 | |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 2128 | /* add devices */ |
| 2129 | fu_main_providers_coldplug (priv); |
| 2130 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 2131 | /* connect to D-Bus directly */ |
| 2132 | priv->proxy_uid = |
| 2133 | g_dbus_proxy_new_sync (priv->connection, |
| 2134 | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 2135 | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 2136 | NULL, |
| 2137 | "org.freedesktop.DBus", |
| 2138 | "/org/freedesktop/DBus", |
| 2139 | "org.freedesktop.DBus", |
| 2140 | NULL, |
| 2141 | &error); |
| 2142 | if (priv->proxy_uid == NULL) { |
| 2143 | g_warning ("cannot connect to DBus: %s", error->message); |
| 2144 | return; |
| 2145 | } |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2146 | |
Richard Hughes | 9559bbe | 2016-03-29 18:54:20 +0100 | [diff] [blame] | 2147 | /* connect to UPower */ |
| 2148 | priv->proxy_upower = |
| 2149 | g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, |
| 2150 | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 2151 | NULL, |
| 2152 | "org.freedesktop.UPower", |
| 2153 | "/org/freedesktop/UPower", |
| 2154 | "org.freedesktop.UPower", |
| 2155 | NULL, |
| 2156 | &error); |
| 2157 | if (priv->proxy_upower == NULL) { |
| 2158 | g_warning ("Failed to conect UPower: %s", error->message); |
| 2159 | return; |
| 2160 | } |
| 2161 | |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2162 | /* dump startup profile data */ |
Richard Hughes | 2a1e75d | 2015-12-18 17:42:53 +0000 | [diff] [blame] | 2163 | if (fu_debug_is_verbose ()) |
| 2164 | as_profile_dump (priv->profile); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2167 | static void |
| 2168 | fu_main_on_name_acquired_cb (GDBusConnection *connection, |
| 2169 | const gchar *name, |
| 2170 | gpointer user_data) |
| 2171 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2172 | g_debug ("FuMain: acquired name: %s", name); |
| 2173 | } |
| 2174 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2175 | static void |
| 2176 | fu_main_on_name_lost_cb (GDBusConnection *connection, |
| 2177 | const gchar *name, |
| 2178 | gpointer user_data) |
| 2179 | { |
| 2180 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2181 | g_debug ("FuMain: lost name: %s", name); |
| 2182 | g_main_loop_quit (priv->loop); |
| 2183 | } |
| 2184 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2185 | static gboolean |
| 2186 | fu_main_timed_exit_cb (gpointer user_data) |
| 2187 | { |
| 2188 | GMainLoop *loop = (GMainLoop *) user_data; |
| 2189 | g_main_loop_quit (loop); |
| 2190 | return G_SOURCE_REMOVE; |
| 2191 | } |
| 2192 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2193 | static GDBusNodeInfo * |
| 2194 | fu_main_load_introspection (const gchar *filename, GError **error) |
| 2195 | { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 2196 | g_autoptr(GBytes) data = NULL; |
| 2197 | g_autofree gchar *path = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2198 | |
| 2199 | /* lookup data */ |
| 2200 | path = g_build_filename ("/org/freedesktop/fwupd", filename, NULL); |
| 2201 | data = g_resource_lookup_data (fu_get_resource (), |
| 2202 | path, |
| 2203 | G_RESOURCE_LOOKUP_FLAGS_NONE, |
| 2204 | error); |
| 2205 | if (data == NULL) |
| 2206 | return NULL; |
| 2207 | |
| 2208 | /* build introspection from XML */ |
| 2209 | return g_dbus_node_info_new_for_xml (g_bytes_get_data (data, NULL), error); |
| 2210 | } |
| 2211 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2212 | static void |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2213 | fu_main_provider_device_added_cb (FuProvider *provider, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2214 | FuDevice *device, |
| 2215 | gpointer user_data) |
| 2216 | { |
| 2217 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2218 | FuDeviceItem *item; |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 2219 | AsApp *app; |
| 2220 | FuPlugin *plugin; |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 2221 | g_auto(GStrv) guids = NULL; |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 2222 | g_autoptr(GError) error = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2223 | |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 2224 | /* is this GUID blacklisted */ |
| 2225 | guids = g_key_file_get_string_list (priv->config, |
| 2226 | "fwupd", |
| 2227 | "BlacklistDevices", |
| 2228 | NULL, /* length */ |
| 2229 | NULL); |
| 2230 | if (guids != NULL && |
| 2231 | g_strv_contains ((const gchar * const *) guids, |
| 2232 | fu_device_get_guid_default (device))) { |
| 2233 | g_debug ("%s is blacklisted [%s], ignoring from %s", |
| 2234 | fu_device_get_id (device), |
| 2235 | fu_device_get_guid_default (device), |
| 2236 | fu_provider_get_name (provider)); |
| 2237 | return; |
| 2238 | } |
| 2239 | |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 2240 | /* remove any fake device */ |
| 2241 | item = fu_main_get_item_by_id (priv, fu_device_get_id (device)); |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2242 | if (item != NULL) { |
| 2243 | g_debug ("already added %s by %s, ignoring same device from %s", |
| 2244 | fu_device_get_id (item->device), |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 2245 | fu_device_get_provider (item->device), |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2246 | fu_provider_get_name (provider)); |
| 2247 | return; |
| 2248 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* create new device */ |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2251 | item = g_new0 (FuDeviceItem, 1); |
| 2252 | item->device = g_object_ref (device); |
| 2253 | item->provider = g_object_ref (provider); |
| 2254 | g_ptr_array_add (priv->devices, item); |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 2255 | |
| 2256 | /* does this match anything in the AppStream data */ |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 2257 | app = fu_main_store_get_app_by_guids (priv->store, item->device); |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 2258 | if (app != NULL) { |
| 2259 | const gchar *tmp; |
| 2260 | tmp = as_app_get_metadata_item (app, FU_DEVICE_KEY_FWUPD_PLUGIN); |
| 2261 | if (tmp != NULL) { |
| 2262 | g_debug ("setting plugin: %s", tmp); |
| 2263 | fu_device_set_metadata (item->device, |
| 2264 | FU_DEVICE_KEY_FWUPD_PLUGIN, |
| 2265 | tmp); |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | /* run any plugins */ |
| 2270 | plugin = fu_main_get_plugin_for_device (priv->plugins, device); |
| 2271 | if (plugin != NULL) { |
| 2272 | if (!fu_plugin_run_device_probe (plugin, device, &error)) { |
| 2273 | g_warning ("failed to probe %s: %s", |
| 2274 | fu_device_get_id (item->device), |
| 2275 | error->message); |
| 2276 | } |
| 2277 | } |
| 2278 | |
Richard Hughes | 422e866 | 2016-04-28 15:05:33 +0100 | [diff] [blame] | 2279 | /* match the metadata at this point so clients can tell if the |
| 2280 | * device is worthy */ |
| 2281 | fu_main_get_updates_item_update (priv, item); |
| 2282 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 2283 | /* notify clients */ |
| 2284 | fu_main_emit_device_added (priv, item->device); |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 2285 | fu_main_emit_changed (priv); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2288 | static void |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2289 | fu_main_provider_device_removed_cb (FuProvider *provider, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2290 | FuDevice *device, |
| 2291 | gpointer user_data) |
| 2292 | { |
| 2293 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2294 | FuDeviceItem *item; |
| 2295 | |
| 2296 | item = fu_main_get_item_by_id (priv, fu_device_get_id (device)); |
| 2297 | if (item == NULL) { |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2298 | g_debug ("no device to remove %s", fu_device_get_id (device)); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2299 | return; |
| 2300 | } |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2301 | |
| 2302 | /* check this came from the same provider */ |
| 2303 | if (g_strcmp0 (fu_provider_get_name (provider), |
| 2304 | fu_provider_get_name (item->provider)) != 0) { |
| 2305 | g_debug ("ignoring duplicate removal from %s", |
| 2306 | fu_provider_get_name (provider)); |
| 2307 | return; |
| 2308 | } |
| 2309 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 2310 | /* make the UI update */ |
| 2311 | fu_main_emit_device_removed (priv, device); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2312 | g_ptr_array_remove (priv->devices, item); |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 2313 | fu_main_emit_changed (priv); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2316 | static void |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2317 | fu_main_provider_status_changed_cb (FuProvider *provider, |
Richard Hughes | f910ac9 | 2015-03-19 10:43:42 +0000 | [diff] [blame] | 2318 | FwupdStatus status, |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2319 | gpointer user_data) |
| 2320 | { |
| 2321 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2322 | fu_main_set_status (priv, status); |
| 2323 | } |
| 2324 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2325 | static void |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 2326 | fu_main_provider_percentage_changed_cb (FuProvider *provider, |
| 2327 | guint percentage, |
| 2328 | gpointer user_data) |
| 2329 | { |
| 2330 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2331 | fu_main_set_percentage (priv, percentage); |
| 2332 | } |
| 2333 | |
| 2334 | static void |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2335 | fu_main_add_provider (FuMainPrivate *priv, FuProvider *provider) |
| 2336 | { |
| 2337 | g_signal_connect (provider, "device-added", |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2338 | G_CALLBACK (fu_main_provider_device_added_cb), |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2339 | priv); |
| 2340 | g_signal_connect (provider, "device-removed", |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2341 | G_CALLBACK (fu_main_provider_device_removed_cb), |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2342 | priv); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2343 | g_signal_connect (provider, "status-changed", |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2344 | G_CALLBACK (fu_main_provider_status_changed_cb), |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2345 | priv); |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 2346 | g_signal_connect (provider, "percentage-changed", |
| 2347 | G_CALLBACK (fu_main_provider_percentage_changed_cb), |
| 2348 | priv); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2349 | g_ptr_array_add (priv->providers, provider); |
| 2350 | } |
| 2351 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2352 | int |
| 2353 | main (int argc, char *argv[]) |
| 2354 | { |
| 2355 | FuMainPrivate *priv = NULL; |
| 2356 | gboolean immediate_exit = FALSE; |
| 2357 | gboolean ret; |
| 2358 | gboolean timed_exit = FALSE; |
| 2359 | GOptionContext *context; |
| 2360 | guint owner_id = 0; |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 2361 | gint retval = 1; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2362 | const GOptionEntry options[] = { |
| 2363 | { "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit, |
| 2364 | /* TRANSLATORS: exit after we've started up, used for user profiling */ |
| 2365 | _("Exit after a small delay"), NULL }, |
| 2366 | { "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit, |
| 2367 | /* TRANSLATORS: exit straight away, used for automatic profiling */ |
| 2368 | _("Exit after the engine has loaded"), NULL }, |
| 2369 | { NULL} |
| 2370 | }; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 2371 | g_autoptr(GError) error = NULL; |
| 2372 | g_autofree gchar *config_file = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2373 | |
| 2374 | setlocale (LC_ALL, ""); |
| 2375 | |
| 2376 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 2377 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 2378 | textdomain (GETTEXT_PACKAGE); |
| 2379 | |
| 2380 | /* TRANSLATORS: program name */ |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 2381 | g_set_application_name (_("Firmware Update Daemon")); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2382 | context = g_option_context_new (NULL); |
| 2383 | g_option_context_add_main_entries (context, options, NULL); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2384 | g_option_context_add_group (context, fu_debug_get_option_group ()); |
Richard Hughes | 8ded6ca | 2015-03-16 12:51:36 +0000 | [diff] [blame] | 2385 | /* TRANSLATORS: program summary */ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2386 | g_option_context_set_summary (context, _("Firmware Update D-Bus Service")); |
| 2387 | ret = g_option_context_parse (context, &argc, &argv, &error); |
| 2388 | if (!ret) { |
| 2389 | g_warning ("FuMain: failed to parse command line arguments: %s", |
| 2390 | error->message); |
| 2391 | goto out; |
| 2392 | } |
| 2393 | |
| 2394 | /* create new objects */ |
| 2395 | priv = g_new0 (FuMainPrivate, 1); |
Richard Hughes | f910ac9 | 2015-03-19 10:43:42 +0000 | [diff] [blame] | 2396 | priv->status = FWUPD_STATUS_IDLE; |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 2397 | priv->percentage = 0; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2398 | priv->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) fu_main_item_free); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2399 | priv->loop = g_main_loop_new (NULL, FALSE); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 2400 | priv->pending = fu_pending_new (); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2401 | priv->store = as_store_new (); |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2402 | priv->profile = as_profile_new (); |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 2403 | g_signal_connect (priv->store, "changed", |
| 2404 | G_CALLBACK (fu_main_store_changed_cb), priv); |
| 2405 | as_store_set_watch_flags (priv->store, AS_STORE_WATCH_FLAG_ADDED | |
| 2406 | AS_STORE_WATCH_FLAG_REMOVED); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2407 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 2408 | /* load plugin */ |
| 2409 | priv->plugins = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 2410 | g_free, (GDestroyNotify) fu_plugin_free); |
| 2411 | if (!fu_main_load_plugins (priv->plugins, &error)) { |
| 2412 | g_print ("failed to load plugins: %s\n", error->message); |
| 2413 | retval = EXIT_FAILURE; |
| 2414 | goto out; |
| 2415 | } |
| 2416 | |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2417 | /* load AppStream */ |
Richard Hughes | 65dfbfe | 2016-03-02 13:42:53 +0000 | [diff] [blame] | 2418 | as_store_add_filter (priv->store, AS_APP_KIND_FIRMWARE); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2419 | if (!as_store_load (priv->store, |
| 2420 | AS_STORE_LOAD_FLAG_APP_INFO_SYSTEM, |
| 2421 | NULL, &error)){ |
| 2422 | g_warning ("FuMain: failed to load AppStream data: %s", |
| 2423 | error->message); |
| 2424 | return FALSE; |
| 2425 | } |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2426 | |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2427 | /* read config file */ |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2428 | config_file = g_build_filename (SYSCONFDIR, "fwupd.conf", NULL); |
| 2429 | g_debug ("Loading fallback values from %s", config_file); |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 2430 | priv->config = g_key_file_new (); |
| 2431 | if (!g_key_file_load_from_file (priv->config, config_file, |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2432 | G_KEY_FILE_NONE, &error)) { |
| 2433 | g_print ("failed to load config file %s: %s\n", |
| 2434 | config_file, error->message); |
| 2435 | retval = EXIT_FAILURE; |
| 2436 | goto out; |
| 2437 | } |
| 2438 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2439 | /* add providers */ |
| 2440 | priv->providers = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 2441 | if (g_key_file_get_boolean (priv->config, "fwupd", "EnableOptionROM", NULL)) |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2442 | fu_main_add_provider (priv, fu_provider_udev_new ()); |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2443 | fu_main_add_provider (priv, fu_provider_dfu_new ()); |
Richard Hughes | 25cf6ab | 2015-08-04 21:34:12 +0100 | [diff] [blame] | 2444 | fu_main_add_provider (priv, fu_provider_rpi_new ()); |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 2445 | fu_main_add_provider (priv, fu_provider_ebitdo_new ()); |
Richard Hughes | 3c99ba4 | 2015-03-05 12:17:48 +0000 | [diff] [blame] | 2446 | #ifdef HAVE_COLORHUG |
Richard Hughes | 72dff81 | 2015-03-03 15:13:25 +0000 | [diff] [blame] | 2447 | fu_main_add_provider (priv, fu_provider_chug_new ()); |
Richard Hughes | 3c99ba4 | 2015-03-05 12:17:48 +0000 | [diff] [blame] | 2448 | #endif |
| 2449 | #ifdef HAVE_UEFI |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2450 | fu_main_add_provider (priv, fu_provider_uefi_new ()); |
Richard Hughes | 3c99ba4 | 2015-03-05 12:17:48 +0000 | [diff] [blame] | 2451 | #endif |
Mario Limonciello | 958ead6 | 2016-05-14 00:10:25 -0500 | [diff] [blame] | 2452 | #ifdef HAVE_DELL |
| 2453 | fu_main_add_provider (priv, fu_provider_dell_new ()); |
| 2454 | #endif |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2455 | |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2456 | /* last as least priority */ |
| 2457 | fu_main_add_provider (priv, fu_provider_usb_new ()); |
| 2458 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2459 | /* load introspection from file */ |
| 2460 | priv->introspection_daemon = fu_main_load_introspection (FWUPD_DBUS_INTERFACE ".xml", |
| 2461 | &error); |
| 2462 | if (priv->introspection_daemon == NULL) { |
| 2463 | g_warning ("FuMain: failed to load daemon introspection: %s", |
| 2464 | error->message); |
| 2465 | goto out; |
| 2466 | } |
| 2467 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2468 | /* get authority */ |
| 2469 | priv->authority = polkit_authority_get_sync (NULL, &error); |
| 2470 | if (priv->authority == NULL) { |
| 2471 | g_warning ("FuMain: failed to load polkit authority: %s", |
| 2472 | error->message); |
| 2473 | goto out; |
| 2474 | } |
| 2475 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2476 | /* own the object */ |
| 2477 | owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, |
| 2478 | FWUPD_DBUS_SERVICE, |
| 2479 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 2480 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 2481 | fu_main_on_bus_acquired_cb, |
| 2482 | fu_main_on_name_acquired_cb, |
| 2483 | fu_main_on_name_lost_cb, |
| 2484 | priv, NULL); |
| 2485 | |
| 2486 | /* Only timeout and close the mainloop if we have specified it |
| 2487 | * on the command line */ |
| 2488 | if (immediate_exit) |
| 2489 | g_idle_add (fu_main_timed_exit_cb, priv->loop); |
| 2490 | else if (timed_exit) |
| 2491 | g_timeout_add_seconds (5, fu_main_timed_exit_cb, priv->loop); |
| 2492 | |
| 2493 | /* wait */ |
| 2494 | g_info ("Daemon ready for requests"); |
| 2495 | g_main_loop_run (priv->loop); |
| 2496 | |
| 2497 | /* success */ |
| 2498 | retval = 0; |
| 2499 | out: |
| 2500 | g_option_context_free (context); |
| 2501 | if (owner_id > 0) |
| 2502 | g_bus_unown_name (owner_id); |
| 2503 | if (priv != NULL) { |
| 2504 | if (priv->loop != NULL) |
| 2505 | g_main_loop_unref (priv->loop); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 2506 | if (priv->proxy_uid != NULL) |
| 2507 | g_object_unref (priv->proxy_uid); |
Richard Hughes | 9559bbe | 2016-03-29 18:54:20 +0100 | [diff] [blame] | 2508 | if (priv->proxy_upower != NULL) |
| 2509 | g_object_unref (priv->proxy_upower); |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 2510 | if (priv->config != NULL) |
| 2511 | g_object_unref (priv->config); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2512 | if (priv->connection != NULL) |
| 2513 | g_object_unref (priv->connection); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2514 | if (priv->authority != NULL) |
| 2515 | g_object_unref (priv->authority); |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2516 | if (priv->profile != NULL) |
| 2517 | g_object_unref (priv->profile); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2518 | if (priv->store != NULL) |
| 2519 | g_object_unref (priv->store); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2520 | if (priv->introspection_daemon != NULL) |
| 2521 | g_dbus_node_info_unref (priv->introspection_daemon); |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 2522 | if (priv->store_changed_id != 0) |
| 2523 | g_source_remove (priv->store_changed_id); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 2524 | g_object_unref (priv->pending); |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2525 | if (priv->providers != NULL) |
| 2526 | g_ptr_array_unref (priv->providers); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 2527 | if (priv->plugins != NULL) |
| 2528 | g_hash_table_unref (priv->plugins); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2529 | g_ptr_array_unref (priv->devices); |
| 2530 | g_free (priv); |
| 2531 | } |
| 2532 | return retval; |
| 2533 | } |
| 2534 | |