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 | |
Philip Withnall | bc339aa | 2016-11-22 16:13:22 +0000 | [diff] [blame] | 62 | #ifndef HAVE_POLKIT_0_114 |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 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 | 4ced466 | 2016-08-26 11:02:31 +0100 | [diff] [blame] | 471 | if (helper->blob_fws != NULL) |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 472 | g_ptr_array_unref (helper->blob_fws); |
Richard Hughes | 4ced466 | 2016-08-26 11:02:31 +0100 | [diff] [blame] | 473 | if (helper->blob_cab != NULL) |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 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 | |
Mario Limonciello | 3ef952f | 2016-09-20 12:28:59 -0500 | [diff] [blame] | 552 | /* Called with online update, test if device is supposed to allow this */ |
| 553 | if (!(helper->flags & FWUPD_INSTALL_FLAG_OFFLINE) && |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 554 | !fu_device_has_flag (item->device, FWUPD_DEVICE_FLAG_ALLOW_ONLINE)) { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 555 | g_set_error(error, |
| 556 | FWUPD_ERROR, |
Mario Limonciello | 3ef952f | 2016-09-20 12:28:59 -0500 | [diff] [blame] | 557 | FWUPD_ERROR_NOT_SUPPORTED, |
| 558 | "Device %s does not allow online updates", |
| 559 | fu_device_get_id (device)); |
| 560 | return FALSE; |
| 561 | } |
| 562 | /* Called with offline update, test if device is supposed to allow this */ |
| 563 | if (helper->flags & FWUPD_INSTALL_FLAG_OFFLINE && |
| 564 | !fu_device_has_flag (item->device, FWUPD_DEVICE_FLAG_ALLOW_OFFLINE)) { |
| 565 | g_set_error(error, |
| 566 | FWUPD_ERROR, |
| 567 | FWUPD_ERROR_NOT_SUPPORTED, |
| 568 | "Device %s does not allow offline updates", |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 569 | fu_device_get_id (device)); |
| 570 | return FALSE; |
| 571 | } |
| 572 | |
| 573 | /* can we only do this on AC power */ |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 574 | if (fu_device_has_flag (item->device, FWUPD_DEVICE_FLAG_REQUIRE_AC)) { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 575 | if (fu_main_on_battery (helper->priv)) { |
| 576 | g_set_error_literal (error, |
| 577 | FWUPD_ERROR, |
| 578 | FWUPD_ERROR_NOT_SUPPORTED, |
| 579 | "Cannot install update " |
| 580 | "when not on AC power"); |
| 581 | return FALSE; |
| 582 | } |
| 583 | } |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 586 | /* run the correct providers for each device */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 587 | for (guint i = 0; i < helper->devices->len; i ++) { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 588 | FuDevice *device = g_ptr_array_index (helper->devices, i); |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 589 | GBytes *blob_fw = g_ptr_array_index (helper->blob_fws, i); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 590 | item = fu_main_get_item_by_id (helper->priv, |
| 591 | fu_device_get_id (device)); |
| 592 | plugin = fu_main_get_plugin_for_device (helper->priv->plugins, |
| 593 | item->device); |
| 594 | if (!fu_provider_update (item->provider, |
| 595 | item->device, |
| 596 | helper->blob_cab, |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 597 | blob_fw, |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 598 | plugin, |
| 599 | helper->flags, |
| 600 | error)) |
| 601 | return FALSE; |
| 602 | |
| 603 | /* make the UI update */ |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 604 | 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] | 605 | fu_main_emit_device_changed (helper->priv, item->device); |
| 606 | } |
Richard Hughes | a3a8f50 | 2015-11-24 12:31:59 +0000 | [diff] [blame] | 607 | |
| 608 | /* make the UI update */ |
| 609 | fu_main_emit_changed (helper->priv); |
| 610 | return TRUE; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 613 | static void |
| 614 | fu_main_check_authorization_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 615 | { |
| 616 | FuMainAuthHelper *helper = (FuMainAuthHelper *) user_data; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 617 | g_autoptr(GError) error = NULL; |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 618 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 619 | |
| 620 | /* get result */ |
| 621 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 622 | res, &error); |
| 623 | if (auth == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 624 | g_set_error (&error, |
| 625 | FWUPD_ERROR, |
| 626 | FWUPD_ERROR_AUTH_FAILED, |
| 627 | "could not check for auth: %s", error->message); |
| 628 | fu_main_invocation_return_error (helper->priv, helper->invocation, error); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 629 | fu_main_helper_free (helper); |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | /* did not auth */ |
| 634 | if (!polkit_authorization_result_get_is_authorized (auth)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 635 | g_set_error_literal (&error, |
| 636 | FWUPD_ERROR, |
| 637 | FWUPD_ERROR_AUTH_FAILED, |
| 638 | "failed to obtain auth"); |
| 639 | fu_main_invocation_return_error (helper->priv, helper->invocation, error); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 640 | fu_main_helper_free (helper); |
| 641 | return; |
| 642 | } |
| 643 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 644 | /* we're good to go */ |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 645 | if (helper->auth_kind == FU_MAIN_AUTH_KIND_INSTALL) { |
| 646 | if (!fu_main_provider_update_authenticated (helper, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 647 | fu_main_invocation_return_error (helper->priv, |
| 648 | helper->invocation, |
| 649 | error); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 650 | fu_main_helper_free (helper); |
| 651 | return; |
| 652 | } |
| 653 | } else if (helper->auth_kind == FU_MAIN_AUTH_KIND_UNLOCK) { |
| 654 | if (!fu_main_provider_unlock_authenticated (helper, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 655 | fu_main_invocation_return_error (helper->priv, |
| 656 | helper->invocation, |
| 657 | error); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 658 | fu_main_helper_free (helper); |
| 659 | return; |
| 660 | } |
| 661 | } else { |
| 662 | g_assert_not_reached (); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /* success */ |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 666 | fu_main_invocation_return_value (helper->priv, helper->invocation, NULL); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 667 | fu_main_helper_free (helper); |
| 668 | } |
| 669 | |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 670 | static gchar * |
| 671 | fu_main_get_guids_from_store (AsStore *store) |
| 672 | { |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 673 | AsProvide *prov; |
| 674 | GPtrArray *provides; |
| 675 | GPtrArray *apps; |
| 676 | GString *str = g_string_new (""); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 677 | |
| 678 | /* return a string with all the firmware apps in the store */ |
| 679 | apps = as_store_get_apps (store); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 680 | for (guint i = 0; i < apps->len; i++) { |
| 681 | AsApp *app = AS_APP (g_ptr_array_index (apps, i)); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 682 | provides = as_app_get_provides (app); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 683 | for (guint j = 0; j < provides->len; j++) { |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 684 | prov = AS_PROVIDE (g_ptr_array_index (provides, j)); |
| 685 | if (as_provide_get_kind (prov) != AS_PROVIDE_KIND_FIRMWARE_FLASHED) |
| 686 | continue; |
| 687 | g_string_append_printf (str, "%s,", as_provide_get_value (prov)); |
| 688 | } |
| 689 | } |
| 690 | if (str->len == 0) |
| 691 | return NULL; |
| 692 | g_string_truncate (str, str->len - 1); |
| 693 | return g_string_free (str, FALSE); |
| 694 | } |
| 695 | |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 696 | static void |
| 697 | fu_main_vendor_quirk_release_version (AsApp *app) |
| 698 | { |
| 699 | AsVersionParseFlag flags = AS_VERSION_PARSE_FLAG_USE_TRIPLET; |
| 700 | GPtrArray *releases; |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 701 | |
| 702 | /* no quirk required */ |
Richard Hughes | 65dfbfe | 2016-03-02 13:42:53 +0000 | [diff] [blame] | 703 | if (as_app_get_kind (app) != AS_APP_KIND_FIRMWARE) |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 704 | return; |
| 705 | |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 706 | for (guint i = 0; quirk_table[i].identifier != NULL; i++) { |
Mario Limonciello | 918f393 | 2016-02-03 12:47:23 -0600 | [diff] [blame] | 707 | if (g_str_has_prefix (as_app_get_id(app), quirk_table[i].identifier)) |
| 708 | flags = quirk_table[i].flags; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 709 | } |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 710 | |
| 711 | /* fix each release */ |
| 712 | releases = as_app_get_releases (app); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 713 | for (guint i = 0; i < releases->len; i++) { |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 714 | AsRelease *rel; |
| 715 | const gchar *version; |
| 716 | guint64 ver_uint32; |
| 717 | g_autofree gchar *version_new = NULL; |
| 718 | |
| 719 | rel = g_ptr_array_index (releases, i); |
| 720 | version = as_release_get_version (rel); |
| 721 | if (version == NULL) |
| 722 | continue; |
| 723 | if (g_strstr_len (version, -1, ".") != NULL) |
| 724 | continue; |
| 725 | |
| 726 | /* metainfo files use hex and the LVFS uses decimal */ |
| 727 | if (g_str_has_prefix (version, "0x")) { |
| 728 | ver_uint32 = g_ascii_strtoull (version + 2, NULL, 16); |
| 729 | } else { |
| 730 | ver_uint32 = g_ascii_strtoull (version, NULL, 10); |
| 731 | } |
| 732 | if (ver_uint32 == 0) |
| 733 | continue; |
| 734 | |
| 735 | /* convert to dotted decimal */ |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 736 | version_new = as_utils_version_from_uint32 ((guint32) ver_uint32, flags); |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 737 | as_release_set_version (rel, version_new); |
| 738 | } |
| 739 | } |
| 740 | |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 741 | static AsApp * |
| 742 | fu_main_store_get_app_by_guids (AsStore *store, FuDevice *device) |
| 743 | { |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 744 | GPtrArray *guids = fu_device_get_guids (device); |
| 745 | for (guint i = 0; i < guids->len; i++) { |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 746 | AsApp *app = NULL; |
| 747 | app = as_store_get_app_by_provide (store, |
| 748 | AS_PROVIDE_KIND_FIRMWARE_FLASHED, |
| 749 | g_ptr_array_index (guids, i)); |
| 750 | if (app != NULL) |
| 751 | return app; |
| 752 | } |
| 753 | return NULL; |
| 754 | } |
| 755 | |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 756 | static AsScreenshot * |
| 757 | _as_app_get_screenshot_default (AsApp *app) |
| 758 | { |
| 759 | GPtrArray *array = as_app_get_screenshots (app); |
| 760 | if (array->len == 0) |
| 761 | return NULL; |
| 762 | return g_ptr_array_index (array, 0); |
| 763 | } |
| 764 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 765 | static gboolean |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 766 | fu_main_update_helper_for_device (FuMainAuthHelper *helper, |
| 767 | FuDevice *device, |
| 768 | GError **error) |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 769 | { |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 770 | AsApp *app; |
| 771 | AsChecksum *csum_tmp; |
| 772 | AsRelease *rel; |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 773 | GBytes *blob_fw; |
Richard Hughes | def3175 | 2015-03-04 19:26:54 +0000 | [diff] [blame] | 774 | const gchar *tmp; |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 775 | const gchar *version; |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 776 | gboolean is_downgrade; |
Richard Hughes | db468ee | 2016-06-29 10:10:47 +0100 | [diff] [blame] | 777 | gint vercmp; |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 778 | |
| 779 | /* find from guid */ |
| 780 | app = fu_main_store_get_app_by_guids (helper->store, device); |
| 781 | if (app == NULL) { |
| 782 | g_autofree gchar *guid = NULL; |
| 783 | guid = fu_main_get_guids_from_store (helper->store); |
| 784 | g_set_error (error, |
| 785 | FWUPD_ERROR, |
| 786 | FWUPD_ERROR_INVALID_FILE, |
| 787 | "firmware is not for this hw: required %s got %s", |
| 788 | fu_device_get_guid_default (device), guid); |
| 789 | return FALSE; |
| 790 | } |
| 791 | |
| 792 | /* parse the DriverVer */ |
| 793 | rel = as_app_get_release_default (app); |
| 794 | if (rel == NULL) { |
| 795 | g_set_error_literal (error, |
| 796 | FWUPD_ERROR, |
| 797 | FWUPD_ERROR_INVALID_FILE, |
| 798 | "no releases in the firmware component"); |
| 799 | return FALSE; |
| 800 | } |
| 801 | |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 802 | /* no update abilities */ |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 803 | if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_ALLOW_OFFLINE) && |
| 804 | !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_ALLOW_ONLINE)) { |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 805 | g_set_error (error, |
| 806 | FWUPD_ERROR, |
| 807 | FWUPD_ERROR_INTERNAL, |
| 808 | "Device %s does not currently allow updates", |
| 809 | fu_device_get_id (device)); |
| 810 | return FALSE; |
| 811 | } |
| 812 | |
| 813 | /* not in bootloader mode */ |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 814 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_BOOTLOADER)) { |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 815 | const gchar *caption = NULL; |
| 816 | AsScreenshot *ss = _as_app_get_screenshot_default (app); |
| 817 | if (ss != NULL) |
| 818 | caption = as_screenshot_get_caption (ss, NULL); |
| 819 | if (caption != NULL) { |
| 820 | g_set_error (error, |
| 821 | FWUPD_ERROR, |
| 822 | FWUPD_ERROR_INTERNAL, |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 823 | "Device %s needs to manually be put in update mode: %s", |
| 824 | fu_device_get_name (device), caption); |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 825 | } else { |
| 826 | g_set_error (error, |
| 827 | FWUPD_ERROR, |
| 828 | FWUPD_ERROR_INTERNAL, |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 829 | "Device %s needs to manually be put in update mode", |
| 830 | fu_device_get_name (device)); |
Richard Hughes | 01b9a83 | 2016-08-16 17:59:32 +0100 | [diff] [blame] | 831 | } |
| 832 | return FALSE; |
| 833 | } |
| 834 | |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 835 | /* get the blob */ |
| 836 | csum_tmp = as_release_get_checksum_by_target (rel, AS_CHECKSUM_TARGET_CONTENT); |
| 837 | tmp = as_checksum_get_filename (csum_tmp); |
| 838 | if (tmp == NULL) { |
| 839 | g_set_error_literal (error, |
| 840 | FWUPD_ERROR, |
| 841 | FWUPD_ERROR_INVALID_FILE, |
| 842 | "no checksum filename"); |
| 843 | return FALSE; |
| 844 | } |
| 845 | |
| 846 | /* not all devices have to use the same blob */ |
| 847 | blob_fw = as_release_get_blob (rel, tmp); |
| 848 | if (blob_fw == NULL) { |
| 849 | g_set_error_literal (error, |
| 850 | FWUPD_ERROR, |
| 851 | FWUPD_ERROR_READ, |
| 852 | "failed to get firmware blob"); |
| 853 | return FALSE; |
| 854 | } |
| 855 | |
| 856 | /* possibly convert the version from 0x to dotted */ |
| 857 | fu_main_vendor_quirk_release_version (app); |
| 858 | |
| 859 | version = as_release_get_version (rel); |
| 860 | fu_device_set_update_version (device, version); |
| 861 | |
| 862 | /* compare to the lowest supported version, if it exists */ |
| 863 | tmp = fu_device_get_version_lowest (device); |
| 864 | if (tmp != NULL && as_utils_vercmp (tmp, version) > 0) { |
| 865 | g_set_error (error, |
| 866 | FWUPD_ERROR, |
| 867 | FWUPD_ERROR_VERSION_NEWER, |
| 868 | "Specified firmware is older than the minimum " |
| 869 | "required version '%s < %s'", tmp, version); |
| 870 | return FALSE; |
| 871 | } |
| 872 | |
| 873 | /* check the device is locked */ |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 874 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_LOCKED)) { |
Richard Hughes | 3ab17e6 | 2016-07-04 12:37:22 +0100 | [diff] [blame] | 875 | g_set_error (error, |
| 876 | FWUPD_ERROR, |
| 877 | FWUPD_ERROR_INTERNAL, |
| 878 | "Device %s is locked", |
| 879 | fu_device_get_id (device)); |
| 880 | return FALSE; |
| 881 | } |
| 882 | |
| 883 | /* compare the versions of what we have installed */ |
| 884 | tmp = fu_device_get_version (device); |
| 885 | if (tmp == NULL) { |
| 886 | g_set_error (error, |
| 887 | FWUPD_ERROR, |
| 888 | FWUPD_ERROR_INTERNAL, |
| 889 | "Device %s does not yet have a current version", |
| 890 | fu_device_get_id (device)); |
| 891 | return FALSE; |
| 892 | } |
| 893 | vercmp = as_utils_vercmp (tmp, version); |
| 894 | if (vercmp == 0 && (helper->flags & FWUPD_INSTALL_FLAG_ALLOW_REINSTALL) == 0) { |
| 895 | g_set_error (error, |
| 896 | FWUPD_ERROR, |
| 897 | FWUPD_ERROR_VERSION_SAME, |
| 898 | "Specified firmware is already installed '%s'", |
| 899 | tmp); |
| 900 | return FALSE; |
| 901 | } |
| 902 | is_downgrade = vercmp > 0; |
| 903 | if (is_downgrade && (helper->flags & FWUPD_INSTALL_FLAG_ALLOW_OLDER) == 0) { |
| 904 | g_set_error (error, |
| 905 | FWUPD_ERROR, |
| 906 | FWUPD_ERROR_VERSION_NEWER, |
| 907 | "Specified firmware is older than installed '%s < %s'", |
| 908 | tmp, version); |
| 909 | return FALSE; |
| 910 | } |
| 911 | |
| 912 | /* if any downgrade, we want the global to be true */ |
| 913 | if (is_downgrade) |
| 914 | helper->is_downgrade = is_downgrade; |
| 915 | |
| 916 | /* verify */ |
| 917 | if (!fu_main_get_release_trust_flags (rel, &helper->trust_flags, error)) |
| 918 | return FALSE; |
| 919 | |
| 920 | /* success */ |
| 921 | g_ptr_array_add (helper->blob_fws, g_bytes_ref (blob_fw)); |
| 922 | return TRUE; |
| 923 | } |
| 924 | |
| 925 | static gboolean |
| 926 | fu_main_update_helper (FuMainAuthHelper *helper, GError **error) |
| 927 | { |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 928 | g_autoptr(GError) error_first = NULL; |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 929 | |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 930 | /* load store file which also decompresses firmware */ |
| 931 | fu_main_set_status (helper->priv, FWUPD_STATUS_DECOMPRESSING); |
| 932 | if (!as_store_from_bytes (helper->store, helper->blob_cab, NULL, error)) |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 933 | return FALSE; |
Richard Hughes | d079b1a | 2015-03-06 10:09:55 +0000 | [diff] [blame] | 934 | |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 935 | /* we've specified a specific device; failure is critical */ |
| 936 | if (helper->devices->len > 0) { |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 937 | for (guint i = 0; i < helper->devices->len; i ++) { |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 938 | FuDevice *device = g_ptr_array_index (helper->devices, i); |
| 939 | if (!fu_main_update_helper_for_device (helper, device, error)) |
| 940 | return FALSE; |
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 | return TRUE; |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 943 | } |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 944 | |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 945 | /* if we've not chosen a device, try and find anything in the |
| 946 | * cabinet 'store' that matches any installed device and is updatable */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 947 | for (guint i = 0; i < helper->priv->devices->len; i++) { |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 948 | AsApp *app; |
| 949 | FuDeviceItem *item; |
| 950 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 951 | |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 952 | /* guid found */ |
| 953 | item = g_ptr_array_index (helper->priv->devices, i); |
| 954 | app = fu_main_store_get_app_by_guids (helper->store, item->device); |
| 955 | if (app == NULL) |
| 956 | continue; |
| 957 | |
| 958 | /* try this device, error not fatal */ |
| 959 | if (!fu_main_update_helper_for_device (helper, |
| 960 | item->device, |
| 961 | &error_local)) { |
| 962 | g_debug ("failed to add %s: %s", |
| 963 | fu_device_get_id (item->device), |
| 964 | error_local->message); |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 965 | |
| 966 | /* save this for later */ |
| 967 | if (error_first == NULL) |
| 968 | error_first = g_error_copy (error_local); |
Richard Hughes | bd40528 | 2016-07-04 13:00:54 +0100 | [diff] [blame] | 969 | continue; |
| 970 | } |
| 971 | |
| 972 | /* success */ |
| 973 | g_ptr_array_add (helper->devices, g_object_ref (item->device)); |
| 974 | } |
Richard Hughes | 346ea88 | 2016-07-04 12:31:06 +0100 | [diff] [blame] | 975 | if (helper->devices->len == 0) { |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 976 | if (error_first != NULL) { |
Richard Hughes | 4fd38c8 | 2016-08-16 17:57:30 +0100 | [diff] [blame] | 977 | g_set_error_literal (error, |
| 978 | FWUPD_ERROR, |
| 979 | FWUPD_ERROR_INVALID_FILE, |
| 980 | error_first->message); |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 981 | } else { |
Richard Hughes | 4fd38c8 | 2016-08-16 17:57:30 +0100 | [diff] [blame] | 982 | g_autofree gchar *guid = NULL; |
| 983 | guid = fu_main_get_guids_from_store (helper->store); |
Richard Hughes | c8646af | 2016-07-04 13:04:27 +0100 | [diff] [blame] | 984 | g_set_error (error, |
| 985 | FWUPD_ERROR, |
| 986 | FWUPD_ERROR_INVALID_FILE, |
| 987 | "no attached hardware matched %s", |
| 988 | guid); |
| 989 | } |
Richard Hughes | 346ea88 | 2016-07-04 12:31:06 +0100 | [diff] [blame] | 990 | return FALSE; |
| 991 | } |
| 992 | |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 993 | /* sanity check */ |
| 994 | if (helper->devices->len != helper->blob_fws->len) { |
| 995 | g_set_error (error, |
| 996 | FWUPD_ERROR, |
| 997 | FWUPD_ERROR_INTERNAL, |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 998 | "not enough firmware blobs (%u) for devices (%u)", |
Richard Hughes | a4a2c18 | 2016-06-29 10:37:05 +0100 | [diff] [blame] | 999 | helper->blob_fws->len, |
| 1000 | helper->devices->len); |
| 1001 | return FALSE; |
| 1002 | } |
| 1003 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 1004 | return TRUE; |
| 1005 | } |
| 1006 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1007 | static guint |
| 1008 | fu_main_dbus_get_uid (FuMainPrivate *priv, const gchar *sender) |
| 1009 | { |
| 1010 | guint uid; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1011 | g_autoptr(GError) error = NULL; |
| 1012 | g_autoptr(GVariant) value = NULL; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1013 | |
| 1014 | if (priv->proxy_uid == NULL) |
| 1015 | return G_MAXUINT; |
| 1016 | value = g_dbus_proxy_call_sync (priv->proxy_uid, |
| 1017 | "GetConnectionUnixUser", |
| 1018 | g_variant_new ("(s)", sender), |
| 1019 | G_DBUS_CALL_FLAGS_NONE, |
| 1020 | -1, |
| 1021 | NULL, |
| 1022 | &error); |
| 1023 | if (value == NULL) { |
| 1024 | g_warning ("Failed to get uid for %s: %s", |
| 1025 | sender, error->message); |
| 1026 | return G_MAXUINT; |
| 1027 | } |
| 1028 | g_variant_get (value, "(u)", &uid); |
| 1029 | return uid; |
| 1030 | } |
| 1031 | |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1032 | static FuDeviceItem * |
| 1033 | fu_main_get_item_by_id_fallback_pending (FuMainPrivate *priv, const gchar *id, GError **error) |
| 1034 | { |
| 1035 | FuDevice *dev; |
| 1036 | FuProvider *provider; |
| 1037 | FuDeviceItem *item = NULL; |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1038 | FwupdUpdateState update_state; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1039 | const gchar *tmp; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1040 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1041 | |
| 1042 | /* not a wildcard */ |
| 1043 | if (g_strcmp0 (id, FWUPD_DEVICE_ID_ANY) != 0) { |
| 1044 | item = fu_main_get_item_by_id (priv, id); |
| 1045 | if (item == NULL) { |
| 1046 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 1047 | FWUPD_ERROR, |
| 1048 | FWUPD_ERROR_NOT_FOUND, |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1049 | "no suitable device found for %s", id); |
| 1050 | } |
| 1051 | return item; |
| 1052 | } |
| 1053 | |
| 1054 | /* allow '*' for any */ |
| 1055 | devices = fu_pending_get_devices (priv->pending, error); |
| 1056 | if (devices == NULL) |
| 1057 | return NULL; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1058 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1059 | dev = g_ptr_array_index (devices, i); |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1060 | update_state = fu_device_get_update_state (dev); |
| 1061 | if (update_state == FWUPD_UPDATE_STATE_UNKNOWN) |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1062 | continue; |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1063 | if (update_state == FWUPD_UPDATE_STATE_PENDING) |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1064 | continue; |
| 1065 | |
| 1066 | /* if the device is not still connected, fake a FuDeviceItem */ |
| 1067 | item = fu_main_get_item_by_id (priv, fu_device_get_id (dev)); |
| 1068 | if (item == NULL) { |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1069 | tmp = fu_device_get_provider (dev); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1070 | provider = fu_main_get_provider_by_name (priv, tmp); |
| 1071 | if (provider == NULL) { |
| 1072 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 1073 | FWUPD_ERROR, |
| 1074 | FWUPD_ERROR_NOT_FOUND, |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1075 | "no provider %s found", tmp); |
| 1076 | } |
| 1077 | item = g_new0 (FuDeviceItem, 1); |
| 1078 | item->device = g_object_ref (dev); |
| 1079 | item->provider = g_object_ref (provider); |
| 1080 | g_ptr_array_add (priv->devices, item); |
| 1081 | |
| 1082 | /* FIXME: just a boolean on FuDeviceItem? */ |
| 1083 | fu_device_set_metadata (dev, "FakeDevice", "TRUE"); |
| 1084 | } |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | /* no device found */ |
| 1089 | if (item == NULL) { |
| 1090 | g_set_error_literal (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 1091 | FWUPD_ERROR, |
| 1092 | FWUPD_ERROR_NOT_FOUND, |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1093 | "no suitable devices found"); |
| 1094 | } |
| 1095 | return item; |
| 1096 | } |
| 1097 | |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1098 | static const gchar * |
| 1099 | fu_main_get_action_id_for_device (FuMainAuthHelper *helper) |
| 1100 | { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1101 | gboolean all_removable = TRUE; |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1102 | gboolean is_trusted; |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1103 | |
| 1104 | /* only test the payload */ |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1105 | is_trusted = (helper->trust_flags & FWUPD_TRUST_FLAG_PAYLOAD) > 0; |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1106 | |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1107 | /* any non-removable means false */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1108 | for (guint i = 0; i < helper->devices->len; i ++) { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1109 | FuDevice *device = g_ptr_array_index (helper->devices, i); |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 1110 | if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_INTERNAL)) { |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1111 | all_removable = FALSE; |
| 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1116 | /* relax authentication checks for removable devices */ |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1117 | if (all_removable) { |
Richard Hughes | db468ee | 2016-06-29 10:10:47 +0100 | [diff] [blame] | 1118 | if (helper->is_downgrade) |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1119 | return "org.freedesktop.fwupd.downgrade-hotplug"; |
| 1120 | if (is_trusted) |
| 1121 | return "org.freedesktop.fwupd.update-hotplug-trusted"; |
| 1122 | return "org.freedesktop.fwupd.update-hotplug"; |
| 1123 | } |
| 1124 | |
| 1125 | /* internal device */ |
Richard Hughes | db468ee | 2016-06-29 10:10:47 +0100 | [diff] [blame] | 1126 | if (helper->is_downgrade) |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 1127 | return "org.freedesktop.fwupd.downgrade-internal"; |
| 1128 | if (is_trusted) |
| 1129 | return "org.freedesktop.fwupd.update-internal-trusted"; |
| 1130 | return "org.freedesktop.fwupd.update-internal"; |
| 1131 | } |
| 1132 | |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1133 | static gboolean |
Mario Limonciello | 3ed5447 | 2015-07-23 13:19:39 -0500 | [diff] [blame] | 1134 | 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] | 1135 | { |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1136 | const guint8 *data; |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1137 | gsize size; |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1138 | GPtrArray *apps; |
Richard Hughes | bb840ce | 2015-10-30 08:47:24 +0000 | [diff] [blame] | 1139 | g_autofree gchar *xml = NULL; |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1140 | g_autoptr(AsStore) store = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1141 | g_autoptr(GBytes) bytes = NULL; |
| 1142 | g_autoptr(GBytes) bytes_raw = NULL; |
| 1143 | g_autoptr(GBytes) bytes_sig = NULL; |
| 1144 | g_autoptr(FuKeyring) kr = NULL; |
| 1145 | g_autoptr(GConverter) converter = NULL; |
| 1146 | g_autoptr(GFile) file = NULL; |
| 1147 | g_autoptr(GInputStream) stream_buf = NULL; |
| 1148 | g_autoptr(GInputStream) stream_fd = NULL; |
| 1149 | g_autoptr(GInputStream) stream = NULL; |
| 1150 | g_autoptr(GInputStream) stream_sig = NULL; |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1151 | |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1152 | /* read the entire file into memory */ |
| 1153 | stream_fd = g_unix_input_stream_new (fd, TRUE); |
| 1154 | bytes_raw = g_input_stream_read_bytes (stream_fd, 0x100000, NULL, error); |
| 1155 | if (bytes_raw == NULL) |
| 1156 | return FALSE; |
| 1157 | stream_buf = g_memory_input_stream_new (); |
| 1158 | g_memory_input_stream_add_bytes (G_MEMORY_INPUT_STREAM (stream_buf), bytes_raw); |
| 1159 | |
| 1160 | /* peek the file type and get data */ |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1161 | data = g_bytes_get_data (bytes_raw, &size); |
| 1162 | if (size < 2) { |
| 1163 | g_set_error_literal (error, |
| 1164 | FWUPD_ERROR, |
| 1165 | FWUPD_ERROR_INVALID_FILE, |
| 1166 | "file is too small"); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1167 | return FALSE; |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1168 | } |
| 1169 | if (data[0] == 0x1f && data[1] == 0x8b) { |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1170 | g_debug ("using GZip decompressor for data"); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1171 | converter = G_CONVERTER (g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP)); |
| 1172 | stream = g_converter_input_stream_new (stream_buf, converter); |
| 1173 | bytes = g_input_stream_read_bytes (stream, 0x100000, NULL, error); |
| 1174 | if (bytes == NULL) |
| 1175 | return FALSE; |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1176 | } else if (data[0] == '<' && data[1] == '?') { |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1177 | g_debug ("using no decompressor for data"); |
| 1178 | bytes = g_bytes_ref (bytes_raw); |
| 1179 | } else { |
| 1180 | g_set_error (error, |
| 1181 | FWUPD_ERROR, |
| 1182 | FWUPD_ERROR_INVALID_FILE, |
| 1183 | "file type '0x%02x,0x%02x' not supported", |
Richard Hughes | f2fca01 | 2015-10-30 08:44:44 +0000 | [diff] [blame] | 1184 | data[0], data[1]); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1185 | return FALSE; |
| 1186 | } |
| 1187 | |
| 1188 | /* read signature */ |
| 1189 | stream_sig = g_unix_input_stream_new (fd_sig, TRUE); |
| 1190 | bytes_sig = g_input_stream_read_bytes (stream_sig, 0x800, NULL, error); |
| 1191 | if (bytes_sig == NULL) |
| 1192 | return FALSE; |
| 1193 | |
| 1194 | /* verify file */ |
| 1195 | kr = fu_keyring_new (); |
| 1196 | if (!fu_keyring_add_public_keys (kr, "/etc/pki/fwupd-metadata", error)) |
| 1197 | return FALSE; |
| 1198 | if (!fu_keyring_verify_data (kr, bytes_raw, bytes_sig, error)) |
| 1199 | return FALSE; |
| 1200 | |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1201 | /* load the store locally until we know it is valid */ |
| 1202 | store = as_store_new (); |
Richard Hughes | bb840ce | 2015-10-30 08:47:24 +0000 | [diff] [blame] | 1203 | data = g_bytes_get_data (bytes, &size); |
| 1204 | xml = g_strndup ((const gchar *) data, size); |
| 1205 | if (!as_store_from_xml (store, xml, NULL, error)) |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1206 | return FALSE; |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1207 | |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1208 | /* add the new application from the store */ |
| 1209 | as_store_remove_all (priv->store); |
| 1210 | apps = as_store_get_apps (store); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1211 | for (guint i = 0; i < apps->len; i++) { |
Richard Hughes | 727664f | 2015-10-27 09:56:04 +0000 | [diff] [blame] | 1212 | AsApp *app = g_ptr_array_index (apps, i); |
| 1213 | as_store_add_app (priv->store, app); |
| 1214 | } |
| 1215 | |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1216 | /* save the new file */ |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1217 | as_store_set_api_version (priv->store, 0.9); |
| 1218 | file = g_file_new_for_path ("/var/cache/app-info/xmls/fwupd.xml"); |
| 1219 | if (!as_store_to_file (priv->store, file, |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1220 | AS_NODE_TO_XML_FLAG_ADD_HEADER | |
| 1221 | AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE | |
| 1222 | AS_NODE_TO_XML_FLAG_FORMAT_INDENT, |
| 1223 | NULL, error)) { |
| 1224 | return FALSE; |
| 1225 | } |
| 1226 | |
| 1227 | return TRUE; |
| 1228 | } |
| 1229 | |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1230 | static gboolean |
| 1231 | fu_main_store_delay_cb (gpointer user_data) |
| 1232 | { |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1233 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1234 | GPtrArray *apps; |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1235 | |
Richard Hughes | dde7a2f | 2016-04-28 15:06:52 +0100 | [diff] [blame] | 1236 | /* print what we've got */ |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1237 | apps = as_store_get_apps (priv->store); |
Richard Hughes | 0a36f44 | 2016-01-06 14:57:20 +0000 | [diff] [blame] | 1238 | if (apps->len == 0) { |
| 1239 | g_debug ("no devices in store"); |
| 1240 | } else { |
| 1241 | g_debug ("devices now in store:"); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1242 | for (guint i = 0; i < apps->len; i++) { |
| 1243 | AsApp *app = g_ptr_array_index (apps, i); |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 1244 | g_debug ("%u\t%s\t%s", i + 1, |
Richard Hughes | 0a36f44 | 2016-01-06 14:57:20 +0000 | [diff] [blame] | 1245 | as_app_get_id (app), |
| 1246 | as_app_get_name (app, NULL)); |
| 1247 | } |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1248 | } |
Richard Hughes | dde7a2f | 2016-04-28 15:06:52 +0100 | [diff] [blame] | 1249 | |
| 1250 | /* are any devices now supported? */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1251 | for (guint i = 0; i < priv->devices->len; i++) { |
| 1252 | FuDeviceItem *item = g_ptr_array_index (priv->devices, i); |
Richard Hughes | dde7a2f | 2016-04-28 15:06:52 +0100 | [diff] [blame] | 1253 | if (fu_main_get_updates_item_update (priv, item)) |
| 1254 | fu_main_emit_device_changed (priv, item->device); |
| 1255 | } |
| 1256 | |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1257 | priv->store_changed_id = 0; |
| 1258 | return G_SOURCE_REMOVE; |
| 1259 | } |
| 1260 | |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 1261 | static void |
| 1262 | fu_main_store_changed_cb (AsStore *store, FuMainPrivate *priv) |
| 1263 | { |
| 1264 | if (priv->store_changed_id != 0) |
| 1265 | return; |
| 1266 | priv->store_changed_id = g_timeout_add (200, fu_main_store_delay_cb, priv); |
| 1267 | } |
| 1268 | |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1269 | static gboolean |
| 1270 | fu_main_get_updates_item_update (FuMainPrivate *priv, FuDeviceItem *item) |
| 1271 | { |
| 1272 | AsApp *app; |
| 1273 | AsChecksum *csum; |
| 1274 | AsRelease *rel; |
| 1275 | GPtrArray *releases; |
| 1276 | const gchar *tmp; |
| 1277 | const gchar *version; |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1278 | g_autoptr(GPtrArray) updates_list = NULL; |
| 1279 | |
| 1280 | /* get device version */ |
| 1281 | version = fu_device_get_version (item->device); |
| 1282 | if (version == NULL) |
| 1283 | return FALSE; |
| 1284 | |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 1285 | /* match the GUIDs in the XML */ |
| 1286 | app = fu_main_store_get_app_by_guids (priv->store, item->device); |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1287 | if (app == NULL) |
| 1288 | return FALSE; |
| 1289 | |
| 1290 | /* possibly convert the version from 0x to dotted */ |
| 1291 | fu_main_vendor_quirk_release_version (app); |
| 1292 | |
| 1293 | /* get latest release */ |
| 1294 | rel = as_app_get_release_default (app); |
| 1295 | if (rel == NULL) { |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 1296 | g_debug ("%s [%s] has no firmware update metadata", |
| 1297 | fu_device_get_id (item->device), |
| 1298 | fu_device_get_name (item->device)); |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1299 | return FALSE; |
| 1300 | } |
| 1301 | |
Richard Hughes | 4f4e1f3 | 2016-04-28 14:29:29 +0100 | [diff] [blame] | 1302 | /* supported in metadata */ |
| 1303 | fwupd_result_add_device_flag (FWUPD_RESULT (item->device), |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 1304 | FWUPD_DEVICE_FLAG_SUPPORTED); |
Richard Hughes | 4f4e1f3 | 2016-04-28 14:29:29 +0100 | [diff] [blame] | 1305 | |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1306 | /* check if actually newer than what we have installed */ |
| 1307 | if (as_utils_vercmp (as_release_get_version (rel), version) <= 0) { |
| 1308 | g_debug ("%s has no firmware updates", |
| 1309 | fu_device_get_id (item->device)); |
| 1310 | return FALSE; |
| 1311 | } |
| 1312 | |
Richard Hughes | 6561c8d | 2016-05-17 16:24:09 +0100 | [diff] [blame] | 1313 | /* only show devices that can be updated */ |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 1314 | if (!fu_device_has_flag (item->device, FWUPD_DEVICE_FLAG_ALLOW_OFFLINE) && |
| 1315 | !fu_device_has_flag (item->device, FWUPD_DEVICE_FLAG_ALLOW_ONLINE)) { |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 1316 | g_debug ("ignoring %s [%s] as not updatable live or offline", |
| 1317 | fu_device_get_id (item->device), |
| 1318 | fu_device_get_name (item->device)); |
Richard Hughes | 6561c8d | 2016-05-17 16:24:09 +0100 | [diff] [blame] | 1319 | return FALSE; |
| 1320 | } |
| 1321 | |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1322 | /* can we only do this on AC power */ |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 1323 | if (fu_device_has_flag (item->device, FWUPD_DEVICE_FLAG_REQUIRE_AC) && |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1324 | fu_main_on_battery (priv)) { |
| 1325 | g_debug ("ignoring update for %s as not on AC power", |
| 1326 | fu_device_get_id (item->device)); |
| 1327 | return FALSE; |
| 1328 | } |
| 1329 | |
| 1330 | /* add application metadata */ |
| 1331 | fu_device_set_update_id (item->device, as_app_get_id (app)); |
| 1332 | tmp = as_app_get_developer_name (app, NULL); |
| 1333 | if (tmp != NULL) |
| 1334 | fu_device_set_update_vendor (item->device, tmp); |
| 1335 | tmp = as_app_get_name (app, NULL); |
| 1336 | if (tmp != NULL) |
| 1337 | fu_device_set_update_name (item->device, tmp); |
| 1338 | tmp = as_app_get_comment (app, NULL); |
| 1339 | if (tmp != NULL) |
| 1340 | fu_device_set_update_summary (item->device, tmp); |
| 1341 | tmp = as_app_get_description (app, NULL); |
| 1342 | if (tmp != NULL) |
| 1343 | fu_device_set_description (item->device, tmp); |
| 1344 | tmp = as_app_get_url_item (app, AS_URL_KIND_HOMEPAGE); |
| 1345 | if (tmp != NULL) |
| 1346 | fu_device_set_update_homepage (item->device, tmp); |
| 1347 | tmp = as_app_get_project_license (app); |
| 1348 | if (tmp != NULL) |
| 1349 | fu_device_set_update_license (item->device, tmp); |
Richard Hughes | fa782a3 | 2016-08-18 18:34:59 +0100 | [diff] [blame] | 1350 | #if AS_CHECK_VERSION(0,6,1) |
| 1351 | tmp = as_app_get_unique_id (app); |
| 1352 | if (tmp != NULL) |
| 1353 | fu_device_set_unique_id (item->device, tmp); |
| 1354 | #else |
| 1355 | fu_device_set_unique_id (item->device, as_app_get_id (app)); |
| 1356 | #endif |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1357 | |
| 1358 | /* add release information */ |
| 1359 | tmp = as_release_get_version (rel); |
| 1360 | if (tmp != NULL) |
| 1361 | fu_device_set_update_version (item->device, tmp); |
| 1362 | csum = as_release_get_checksum_by_target (rel, AS_CHECKSUM_TARGET_CONTAINER); |
| 1363 | if (csum != NULL) { |
| 1364 | fu_device_set_update_checksum (item->device, |
| 1365 | as_checksum_get_value (csum)); |
| 1366 | } |
| 1367 | tmp = as_release_get_location_default (rel); |
| 1368 | if (tmp != NULL) |
| 1369 | fu_device_set_update_uri (item->device, tmp); |
| 1370 | |
| 1371 | /* get the list of releases newer than the one installed */ |
| 1372 | updates_list = g_ptr_array_new (); |
| 1373 | releases = as_app_get_releases (app); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1374 | for (guint i = 0; i < releases->len; i++) { |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1375 | rel = g_ptr_array_index (releases, i); |
Richard Hughes | ae87438 | 2016-08-17 14:23:05 +0100 | [diff] [blame] | 1376 | if (as_utils_vercmp (as_release_get_version (rel), version) <= 0) |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1377 | continue; |
| 1378 | tmp = as_release_get_description (rel, NULL); |
| 1379 | if (tmp == NULL) |
| 1380 | continue; |
| 1381 | g_ptr_array_add (updates_list, rel); |
| 1382 | } |
| 1383 | |
| 1384 | /* no prefix on each release */ |
| 1385 | if (updates_list->len == 1) { |
| 1386 | rel = g_ptr_array_index (updates_list, 0); |
| 1387 | fu_device_set_update_description (item->device, |
| 1388 | as_release_get_description (rel, NULL)); |
| 1389 | } else { |
| 1390 | g_autoptr(GString) update_desc = NULL; |
| 1391 | update_desc = g_string_new (""); |
| 1392 | |
| 1393 | /* get the descriptions with a version prefix */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1394 | for (guint i = 0; i < updates_list->len; i++) { |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1395 | rel = g_ptr_array_index (updates_list, i); |
| 1396 | g_string_append_printf (update_desc, |
| 1397 | "<p>%s:</p>%s", |
| 1398 | as_release_get_version (rel), |
| 1399 | as_release_get_description (rel, NULL)); |
| 1400 | } |
| 1401 | if (update_desc->len > 0) |
| 1402 | fu_device_set_update_description (item->device, update_desc->str); |
| 1403 | } |
| 1404 | |
| 1405 | /* success */ |
| 1406 | return TRUE; |
| 1407 | } |
| 1408 | |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1409 | /* find any updates using the AppStream metadata */ |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1410 | static GPtrArray * |
| 1411 | fu_main_get_updates (FuMainPrivate *priv, GError **error) |
| 1412 | { |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1413 | GPtrArray *updates = g_ptr_array_new (); |
| 1414 | for (guint i = 0; i < priv->devices->len; i++) { |
| 1415 | FuDeviceItem *item = g_ptr_array_index (priv->devices, i); |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 1416 | if (fu_main_get_updates_item_update (priv, item)) |
| 1417 | g_ptr_array_add (updates, item); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1418 | } |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1419 | return updates; |
| 1420 | } |
| 1421 | |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1422 | static AsStore * |
| 1423 | fu_main_get_store_from_fd (FuMainPrivate *priv, gint fd, GError **error) |
| 1424 | { |
Richard Hughes | 5c066ad | 2016-08-18 18:38:06 +0100 | [diff] [blame] | 1425 | g_autofree gchar *checksum = NULL; |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1426 | g_autoptr(AsStore) store = NULL; |
| 1427 | g_autoptr(GBytes) blob_cab = NULL; |
| 1428 | g_autoptr(GError) error_local = NULL; |
| 1429 | g_autoptr(GInputStream) stream = NULL; |
| 1430 | |
| 1431 | /* read the entire fd to a data blob */ |
| 1432 | stream = g_unix_input_stream_new (fd, TRUE); |
| 1433 | blob_cab = g_input_stream_read_bytes (stream, |
| 1434 | FU_MAIN_FIRMWARE_SIZE_MAX, |
| 1435 | NULL, &error_local); |
| 1436 | if (blob_cab == NULL){ |
| 1437 | g_set_error_literal (error, |
| 1438 | FWUPD_ERROR, |
| 1439 | FWUPD_ERROR_INVALID_FILE, |
| 1440 | error_local->message); |
| 1441 | return NULL; |
| 1442 | } |
| 1443 | |
| 1444 | /* load file */ |
| 1445 | store = as_store_new (); |
| 1446 | if (!as_store_from_bytes (store, blob_cab, NULL, &error_local)) { |
| 1447 | g_set_error_literal (error, |
| 1448 | FWUPD_ERROR, |
| 1449 | FWUPD_ERROR_INVALID_FILE, |
| 1450 | error_local->message); |
| 1451 | return NULL; |
| 1452 | } |
Richard Hughes | 5c066ad | 2016-08-18 18:38:06 +0100 | [diff] [blame] | 1453 | |
| 1454 | /* get a checksum of the file and use it as the origin */ |
| 1455 | checksum = g_compute_checksum_for_data (G_CHECKSUM_SHA1, |
| 1456 | g_bytes_get_data (blob_cab, NULL), |
| 1457 | g_bytes_get_size (blob_cab)); |
| 1458 | as_store_set_origin (store, checksum); |
| 1459 | |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1460 | return g_steal_pointer (&store); |
| 1461 | } |
| 1462 | |
| 1463 | static FwupdResult * |
| 1464 | fu_main_get_result_from_app (FuMainPrivate *priv, AsApp *app, GError **error) |
| 1465 | { |
| 1466 | FwupdTrustFlags trust_flags = FWUPD_TRUST_FLAG_NONE; |
| 1467 | AsRelease *rel; |
Mario Limonciello | 8eaadd0 | 2016-06-17 16:23:44 -0500 | [diff] [blame] | 1468 | AsChecksum * csum_tmp; |
| 1469 | const gchar *fn; |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1470 | GPtrArray *provides; |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1471 | g_autoptr(FwupdResult) res = NULL; |
| 1472 | |
| 1473 | res = fwupd_result_new (); |
| 1474 | provides = as_app_get_provides (app); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1475 | for (guint i = 0; i < provides->len; i++) { |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1476 | AsProvide *prov = AS_PROVIDE (g_ptr_array_index (provides, i)); |
| 1477 | FuDeviceItem *item; |
| 1478 | const gchar *guid; |
| 1479 | |
| 1480 | /* not firmware */ |
| 1481 | if (as_provide_get_kind (prov) != AS_PROVIDE_KIND_FIRMWARE_FLASHED) |
| 1482 | continue; |
| 1483 | |
| 1484 | /* is a online or offline update appropriate */ |
| 1485 | guid = as_provide_get_value (prov); |
| 1486 | if (guid == NULL) |
| 1487 | continue; |
| 1488 | item = fu_main_get_item_by_guid (priv, guid); |
| 1489 | if (item != NULL) { |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1490 | fwupd_result_set_device_flags (res, fu_device_get_flags (item->device)); |
| 1491 | fwupd_result_set_device_id (res, fu_device_get_id (item->device)); |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /* add GUID */ |
| 1495 | fwupd_result_add_guid (res, guid); |
| 1496 | } |
| 1497 | if (fwupd_result_get_guids(res)->len == 0) { |
| 1498 | g_set_error_literal (error, |
| 1499 | FWUPD_ERROR, |
| 1500 | FWUPD_ERROR_INTERNAL, |
| 1501 | "component has no GUIDs"); |
| 1502 | return NULL; |
| 1503 | } |
| 1504 | |
| 1505 | /* verify trust */ |
| 1506 | rel = as_app_get_release_default (app); |
| 1507 | if (!fu_main_get_release_trust_flags (rel, &trust_flags, error)) |
| 1508 | return NULL; |
| 1509 | |
| 1510 | /* possibly convert the version from 0x to dotted */ |
| 1511 | fu_main_vendor_quirk_release_version (app); |
| 1512 | |
| 1513 | /* create a result with all the metadata in */ |
| 1514 | fwupd_result_set_device_description (res, as_app_get_description (app, NULL)); |
Richard Hughes | 4921bd9 | 2016-06-27 11:17:43 +0100 | [diff] [blame] | 1515 | fwupd_result_set_update_id (res, as_app_get_id (app)); |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1516 | fwupd_result_set_update_description (res, as_release_get_description (rel, NULL)); |
| 1517 | fwupd_result_set_update_homepage (res, as_app_get_url_item (app, AS_URL_KIND_HOMEPAGE)); |
| 1518 | fwupd_result_set_update_license (res, as_app_get_project_license (app)); |
| 1519 | fwupd_result_set_update_name (res, as_app_get_name (app, NULL)); |
| 1520 | fwupd_result_set_update_size (res, as_release_get_size (rel, AS_SIZE_KIND_INSTALLED)); |
| 1521 | fwupd_result_set_update_summary (res, as_app_get_comment (app, NULL)); |
| 1522 | fwupd_result_set_update_trust_flags (res, trust_flags); |
| 1523 | fwupd_result_set_update_vendor (res, as_app_get_developer_name (app, NULL)); |
| 1524 | fwupd_result_set_update_version (res, as_release_get_version (rel)); |
Richard Hughes | fa782a3 | 2016-08-18 18:34:59 +0100 | [diff] [blame] | 1525 | #if AS_CHECK_VERSION(0,6,1) |
| 1526 | fwupd_result_set_unique_id (res, as_app_get_unique_id (app)); |
| 1527 | #else |
| 1528 | fwupd_result_set_unique_id (res, as_app_get_id (app)); |
| 1529 | #endif |
| 1530 | |
Mario Limonciello | 8eaadd0 | 2016-06-17 16:23:44 -0500 | [diff] [blame] | 1531 | csum_tmp = as_release_get_checksum_by_target (rel, |
| 1532 | AS_CHECKSUM_TARGET_CONTENT); |
| 1533 | fn = as_checksum_get_filename (csum_tmp); |
| 1534 | if (fn != NULL) |
| 1535 | fwupd_result_set_update_filename (res, fn); |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1536 | return g_steal_pointer (&res); |
| 1537 | } |
| 1538 | |
| 1539 | static GVariant * |
| 1540 | fu_main_get_details_from_fd (FuMainPrivate *priv, gint fd, GError **error) |
| 1541 | { |
| 1542 | AsApp *app = NULL; |
| 1543 | GPtrArray *apps; |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1544 | g_autoptr(AsStore) store = NULL; |
| 1545 | g_autoptr(FwupdResult) res = NULL; |
| 1546 | |
| 1547 | store = fu_main_get_store_from_fd (priv, fd, error); |
| 1548 | if (store == NULL) |
| 1549 | return NULL; |
| 1550 | |
| 1551 | /* get all apps */ |
| 1552 | apps = as_store_get_apps (store); |
| 1553 | if (apps->len == 0) { |
| 1554 | g_set_error_literal (error, |
| 1555 | FWUPD_ERROR, |
| 1556 | FWUPD_ERROR_INVALID_FILE, |
| 1557 | "no components"); |
| 1558 | return NULL; |
| 1559 | } |
| 1560 | if (apps->len > 1) { |
| 1561 | /* we've got a .cab file with multiple components, |
| 1562 | * so try to find the first thing that's installed */ |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1563 | for (guint i = 0; i < priv->devices->len; i++) { |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1564 | FuDeviceItem *item = g_ptr_array_index (priv->devices, i); |
| 1565 | app = fu_main_store_get_app_by_guids (store, item->device); |
| 1566 | if (app != NULL) |
| 1567 | break; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | /* well, we've tried our best, just show the first entry */ |
| 1572 | if (app == NULL) |
| 1573 | app = AS_APP (g_ptr_array_index (apps, 0)); |
| 1574 | |
| 1575 | /* create a result with all the metadata in */ |
Richard Hughes | 5c066ad | 2016-08-18 18:38:06 +0100 | [diff] [blame] | 1576 | as_app_set_origin (app, as_store_get_origin (store)); |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 1577 | res = fu_main_get_result_from_app (priv, app, error); |
| 1578 | if (res == NULL) |
| 1579 | return NULL; |
| 1580 | return fwupd_result_to_data (res, "(a{sv})"); |
| 1581 | } |
| 1582 | |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1583 | static GVariant * |
| 1584 | fu_main_get_details_local_from_fd (FuMainPrivate *priv, gint fd, GError **error) |
| 1585 | { |
| 1586 | GPtrArray *apps; |
| 1587 | GVariantBuilder builder; |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1588 | g_autoptr(AsStore) store = NULL; |
| 1589 | |
| 1590 | store = fu_main_get_store_from_fd (priv, fd, error); |
| 1591 | if (store == NULL) |
| 1592 | return NULL; |
| 1593 | |
| 1594 | /* get all apps */ |
| 1595 | apps = as_store_get_apps (store); |
| 1596 | if (apps->len == 0) { |
| 1597 | g_set_error_literal (error, |
| 1598 | FWUPD_ERROR, |
| 1599 | FWUPD_ERROR_INVALID_FILE, |
| 1600 | "no components"); |
| 1601 | return NULL; |
| 1602 | } |
| 1603 | |
| 1604 | /* create results with all the metadata in */ |
| 1605 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 1606 | for (guint i = 0; i < apps->len; i++) { |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1607 | g_autoptr(FwupdResult) res = NULL; |
| 1608 | AsApp *app = g_ptr_array_index (apps, i); |
| 1609 | GVariant *tmp; |
Richard Hughes | 5c066ad | 2016-08-18 18:38:06 +0100 | [diff] [blame] | 1610 | as_app_set_origin (app, as_store_get_origin (store)); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1611 | res = fu_main_get_result_from_app (priv, app, error); |
| 1612 | if (res == NULL) |
| 1613 | return NULL; |
| 1614 | tmp = fwupd_result_to_data (res, "{sa{sv}}"); |
| 1615 | g_variant_builder_add_value (&builder, tmp); |
| 1616 | } |
| 1617 | return g_variant_new ("(a{sa{sv}})", &builder); |
| 1618 | } |
| 1619 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1620 | static void |
| 1621 | fu_main_daemon_method_call (GDBusConnection *connection, const gchar *sender, |
| 1622 | const gchar *object_path, const gchar *interface_name, |
| 1623 | const gchar *method_name, GVariant *parameters, |
| 1624 | GDBusMethodInvocation *invocation, gpointer user_data) |
| 1625 | { |
| 1626 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1627 | GVariant *val; |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1628 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1629 | |
| 1630 | /* return 'as' */ |
| 1631 | if (g_strcmp0 (method_name, "GetDevices") == 0) { |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1632 | g_debug ("Called %s()", method_name); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1633 | val = fu_main_device_array_to_variant (priv->devices, &error); |
| 1634 | if (val == NULL) { |
Richard Hughes | 9d76a87 | 2015-09-17 12:49:07 +0100 | [diff] [blame] | 1635 | if (g_error_matches (error, |
| 1636 | FWUPD_ERROR, |
| 1637 | FWUPD_ERROR_NOTHING_TO_DO)) { |
| 1638 | g_prefix_error (&error, "No detected devices: "); |
| 1639 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1640 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1641 | return; |
| 1642 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1643 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1644 | return; |
| 1645 | } |
| 1646 | |
| 1647 | /* return 'as' */ |
| 1648 | if (g_strcmp0 (method_name, "GetUpdates") == 0) { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1649 | g_autoptr(GPtrArray) updates = NULL; |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1650 | g_debug ("Called %s()", method_name); |
| 1651 | updates = fu_main_get_updates (priv, &error); |
| 1652 | if (updates == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1653 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 1654 | return; |
| 1655 | } |
| 1656 | val = fu_main_device_array_to_variant (updates, &error); |
Richard Hughes | 9a38c03 | 2015-03-17 20:58:46 +0000 | [diff] [blame] | 1657 | if (val == NULL) { |
Richard Hughes | 9d76a87 | 2015-09-17 12:49:07 +0100 | [diff] [blame] | 1658 | if (g_error_matches (error, |
| 1659 | FWUPD_ERROR, |
| 1660 | FWUPD_ERROR_NOTHING_TO_DO)) { |
Mario Limonciello | 54a5a21 | 2016-10-19 14:29:53 -0500 | [diff] [blame] | 1661 | if (fu_main_on_battery (priv)) |
| 1662 | g_prefix_error (&error, "No devices can be updated while operating on battery power: "); |
| 1663 | else |
| 1664 | g_prefix_error (&error, "No devices can be updated: "); |
Richard Hughes | 9d76a87 | 2015-09-17 12:49:07 +0100 | [diff] [blame] | 1665 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1666 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 9a38c03 | 2015-03-17 20:58:46 +0000 | [diff] [blame] | 1667 | return; |
| 1668 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1669 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1670 | return; |
| 1671 | } |
| 1672 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1673 | /* return '' */ |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1674 | if (g_strcmp0 (method_name, "ClearResults") == 0) { |
| 1675 | FuDeviceItem *item = NULL; |
| 1676 | const gchar *id = NULL; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1677 | |
| 1678 | g_variant_get (parameters, "(&s)", &id); |
| 1679 | g_debug ("Called %s(%s)", method_name, id); |
| 1680 | |
| 1681 | /* find device */ |
| 1682 | item = fu_main_get_item_by_id_fallback_pending (priv, id, &error); |
| 1683 | if (item == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1684 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1685 | return; |
| 1686 | } |
| 1687 | |
| 1688 | /* call into the provider */ |
| 1689 | if (!fu_provider_clear_results (item->provider, item->device, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1690 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1691 | return; |
| 1692 | } |
| 1693 | |
| 1694 | /* success */ |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1695 | fu_main_invocation_return_value (priv, invocation, NULL); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1696 | return; |
| 1697 | } |
| 1698 | |
| 1699 | /* return 'a{sv}' */ |
| 1700 | if (g_strcmp0 (method_name, "GetResults") == 0) { |
| 1701 | FuDeviceItem *item = NULL; |
| 1702 | const gchar *id = NULL; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1703 | |
| 1704 | g_variant_get (parameters, "(&s)", &id); |
| 1705 | g_debug ("Called %s(%s)", method_name, id); |
| 1706 | |
| 1707 | /* find device */ |
| 1708 | item = fu_main_get_item_by_id_fallback_pending (priv, id, &error); |
| 1709 | if (item == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1710 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1711 | return; |
| 1712 | } |
| 1713 | |
| 1714 | /* call into the provider */ |
| 1715 | if (!fu_provider_get_results (item->provider, item->device, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1716 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1717 | return; |
| 1718 | } |
| 1719 | |
Richard Hughes | 2db526d | 2016-08-25 15:07:23 +0100 | [diff] [blame] | 1720 | /* ensure the unique ID is set */ |
| 1721 | if (fwupd_result_get_unique_id (FWUPD_RESULT (item->device)) == NULL) { |
Richard Hughes | 4ced466 | 2016-08-26 11:02:31 +0100 | [diff] [blame] | 1722 | g_autofree gchar *id2 = NULL; |
Richard Hughes | 2db526d | 2016-08-25 15:07:23 +0100 | [diff] [blame] | 1723 | FwupdResult *res = FWUPD_RESULT (item->device); |
Richard Hughes | 27aad5a | 2016-08-29 16:12:23 +0100 | [diff] [blame] | 1724 | #if AS_CHECK_VERSION(0,6,1) |
Richard Hughes | 4ced466 | 2016-08-26 11:02:31 +0100 | [diff] [blame] | 1725 | id2 = as_utils_unique_id_build (AS_APP_SCOPE_SYSTEM, |
| 1726 | AS_BUNDLE_KIND_UNKNOWN, |
| 1727 | NULL, |
| 1728 | AS_APP_KIND_FIRMWARE, |
| 1729 | fwupd_result_get_device_name (res), |
| 1730 | fwupd_result_get_device_version (res)); |
Richard Hughes | 27aad5a | 2016-08-29 16:12:23 +0100 | [diff] [blame] | 1731 | #else |
| 1732 | id2 = g_strdup_printf ("system/*/*/firmware/%s/%s", |
| 1733 | fwupd_result_get_device_name (res), |
| 1734 | fwupd_result_get_device_version (res)); |
| 1735 | #endif |
Richard Hughes | 4ced466 | 2016-08-26 11:02:31 +0100 | [diff] [blame] | 1736 | fwupd_result_set_unique_id (res, id2); |
Richard Hughes | 2db526d | 2016-08-25 15:07:23 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1739 | /* success */ |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1740 | val = fwupd_result_to_data (FWUPD_RESULT (item->device), "(a{sv})"); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1741 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1742 | return; |
| 1743 | } |
| 1744 | |
| 1745 | /* return '' */ |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1746 | if (g_strcmp0 (method_name, "UpdateMetadata") == 0) { |
| 1747 | GDBusMessage *message; |
| 1748 | GUnixFDList *fd_list; |
| 1749 | gint fd_data; |
| 1750 | gint fd_sig; |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1751 | |
| 1752 | message = g_dbus_method_invocation_get_message (invocation); |
| 1753 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1754 | 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] | 1755 | g_set_error (&error, |
| 1756 | FWUPD_ERROR, |
| 1757 | FWUPD_ERROR_INTERNAL, |
| 1758 | "invalid handle"); |
| 1759 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1760 | return; |
| 1761 | } |
| 1762 | fd_data = g_unix_fd_list_get (fd_list, 0, &error); |
| 1763 | if (fd_data < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1764 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1765 | return; |
| 1766 | } |
| 1767 | fd_sig = g_unix_fd_list_get (fd_list, 1, &error); |
| 1768 | if (fd_sig < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1769 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1770 | return; |
| 1771 | } |
Mario Limonciello | 3ed5447 | 2015-07-23 13:19:39 -0500 | [diff] [blame] | 1772 | if (!fu_main_daemon_update_metadata (priv, fd_data, fd_sig, &error)) { |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1773 | g_prefix_error (&error, "failed to update metadata: "); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1774 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1775 | return; |
| 1776 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1777 | fu_main_invocation_return_value (priv, invocation, NULL); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1778 | return; |
| 1779 | } |
| 1780 | |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1781 | /* return 's' */ |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1782 | if (g_strcmp0 (method_name, "Unlock") == 0) { |
| 1783 | FuDeviceItem *item = NULL; |
| 1784 | FuMainAuthHelper *helper; |
| 1785 | const gchar *id = NULL; |
| 1786 | g_autoptr(PolkitSubject) subject = NULL; |
| 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 | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1798 | return; |
| 1799 | } |
| 1800 | |
| 1801 | /* check the device is locked */ |
Richard Hughes | 644562e | 2016-08-22 10:30:24 +0100 | [diff] [blame] | 1802 | if (!fu_device_has_flag (item->device, FWUPD_DEVICE_FLAG_LOCKED)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1803 | g_set_error (&error, |
| 1804 | FWUPD_ERROR, |
| 1805 | FWUPD_ERROR_NOT_FOUND, |
| 1806 | "Device %s is not locked", id); |
| 1807 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1808 | return; |
| 1809 | } |
| 1810 | |
| 1811 | /* process the firmware */ |
| 1812 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1813 | helper->auth_kind = FU_MAIN_AUTH_KIND_UNLOCK; |
| 1814 | helper->invocation = g_object_ref (invocation); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1815 | 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] | 1816 | helper->priv = priv; |
| 1817 | |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1818 | /* FIXME: do we want to support "*"? */ |
| 1819 | g_ptr_array_add (helper->devices, g_object_ref (item->device)); |
| 1820 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1821 | /* authenticate */ |
| 1822 | subject = polkit_system_bus_name_new (sender); |
| 1823 | polkit_authority_check_authorization (helper->priv->authority, subject, |
| 1824 | "org.freedesktop.fwupd.device-unlock", |
| 1825 | NULL, |
| 1826 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1827 | NULL, |
| 1828 | fu_main_check_authorization_cb, |
| 1829 | helper); |
| 1830 | return; |
| 1831 | } |
| 1832 | |
| 1833 | /* return 's' */ |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1834 | if (g_strcmp0 (method_name, "Verify") == 0) { |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1835 | AsApp *app; |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1836 | AsChecksum *csum; |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1837 | AsRelease *release; |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1838 | FuDeviceItem *item = NULL; |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1839 | const gchar *hash = NULL; |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1840 | const gchar *id = NULL; |
| 1841 | const gchar *version = NULL; |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1842 | |
| 1843 | /* check the id exists */ |
| 1844 | g_variant_get (parameters, "(&s)", &id); |
| 1845 | g_debug ("Called %s(%s)", method_name, id); |
| 1846 | item = fu_main_get_item_by_id (priv, id); |
| 1847 | if (item == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1848 | g_set_error (&error, |
| 1849 | FWUPD_ERROR, |
| 1850 | FWUPD_ERROR_NOT_FOUND, |
| 1851 | "No such device %s", id); |
| 1852 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1853 | return; |
| 1854 | } |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1855 | |
| 1856 | /* set the device firmware hash */ |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1857 | if (!fu_provider_verify (item->provider, item->device, |
| 1858 | FU_PROVIDER_VERIFY_FLAG_NONE, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1859 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1860 | return; |
| 1861 | } |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1862 | |
| 1863 | /* find component in metadata */ |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 1864 | app = fu_main_store_get_app_by_guids (priv->store, item->device); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1865 | if (app == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1866 | g_set_error_literal (&error, |
| 1867 | FWUPD_ERROR, |
| 1868 | FWUPD_ERROR_NOT_FOUND, |
| 1869 | "No metadata"); |
| 1870 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1871 | return; |
| 1872 | } |
| 1873 | |
| 1874 | /* find version in metadata */ |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1875 | version = fu_device_get_version (item->device); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1876 | release = as_app_get_release (app, version); |
| 1877 | if (release == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1878 | g_set_error (&error, |
| 1879 | FWUPD_ERROR, |
| 1880 | FWUPD_ERROR_NOT_FOUND, |
| 1881 | "No version %s", version); |
| 1882 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1883 | return; |
| 1884 | } |
| 1885 | |
| 1886 | /* find checksum */ |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1887 | csum = as_release_get_checksum_by_target (release, AS_CHECKSUM_TARGET_CONTENT); |
| 1888 | if (csum == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1889 | g_set_error (&error, |
| 1890 | FWUPD_ERROR, |
| 1891 | FWUPD_ERROR_NOT_FOUND, |
| 1892 | "No content checksum for %s", version); |
| 1893 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1894 | return; |
| 1895 | } |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 1896 | hash = fu_device_get_checksum (item->device); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1897 | if (g_strcmp0 (as_checksum_get_value (csum), hash) != 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1898 | g_set_error (&error, |
| 1899 | FWUPD_ERROR, |
| 1900 | FWUPD_ERROR_NOT_FOUND, |
| 1901 | "For v%s expected %s, got %s", |
| 1902 | version, |
| 1903 | as_checksum_get_value (csum), |
| 1904 | hash); |
| 1905 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | c6ff8fa | 2015-08-03 18:00:51 +0100 | [diff] [blame] | 1906 | return; |
| 1907 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1908 | fu_main_invocation_return_value (priv, invocation, NULL); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1909 | return; |
| 1910 | } |
| 1911 | |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1912 | /* return '' */ |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 1913 | if (g_strcmp0 (method_name, "Install") == 0) { |
Richard Hughes | d079b1a | 2015-03-06 10:09:55 +0000 | [diff] [blame] | 1914 | FuDeviceItem *item = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1915 | FuMainAuthHelper *helper; |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 1916 | FwupdInstallFlags flags = FWUPD_INSTALL_FLAG_NONE; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1917 | GDBusMessage *message; |
| 1918 | GUnixFDList *fd_list; |
| 1919 | GVariant *prop_value; |
Richard Hughes | a8e8394 | 2015-03-09 17:19:35 +0000 | [diff] [blame] | 1920 | const gchar *action_id; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1921 | const gchar *id = NULL; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1922 | gchar *prop_key; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1923 | gint32 fd_handle = 0; |
| 1924 | gint fd; |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 1925 | g_autoptr(PolkitSubject) subject = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1926 | g_autoptr(GVariantIter) iter = NULL; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1927 | g_autoptr(GBytes) blob_cab = NULL; |
| 1928 | g_autoptr(GInputStream) stream = NULL; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1929 | |
| 1930 | /* check the id exists */ |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1931 | g_variant_get (parameters, "(&sha{sv})", &id, &fd_handle, &iter); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1932 | g_debug ("Called %s(%s,%i)", method_name, id, fd_handle); |
Richard Hughes | d079b1a | 2015-03-06 10:09:55 +0000 | [diff] [blame] | 1933 | if (g_strcmp0 (id, FWUPD_DEVICE_ID_ANY) != 0) { |
| 1934 | item = fu_main_get_item_by_id (priv, id); |
| 1935 | if (item == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1936 | g_set_error (&error, |
| 1937 | FWUPD_ERROR, |
| 1938 | FWUPD_ERROR_NOT_FOUND, |
| 1939 | "no such device %s", id); |
| 1940 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | d079b1a | 2015-03-06 10:09:55 +0000 | [diff] [blame] | 1941 | return; |
| 1942 | } |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1945 | /* get options */ |
| 1946 | while (g_variant_iter_next (iter, "{&sv}", |
| 1947 | &prop_key, &prop_value)) { |
| 1948 | g_debug ("got option %s", prop_key); |
| 1949 | if (g_strcmp0 (prop_key, "offline") == 0 && |
| 1950 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 1951 | flags |= FWUPD_INSTALL_FLAG_OFFLINE; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1952 | if (g_strcmp0 (prop_key, "allow-older") == 0 && |
| 1953 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 1954 | flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1955 | if (g_strcmp0 (prop_key, "allow-reinstall") == 0 && |
| 1956 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 1957 | flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
Mario Limonciello | 71a5b98 | 2016-05-10 15:38:53 -0500 | [diff] [blame] | 1958 | if (g_strcmp0 (prop_key, "force") == 0 && |
| 1959 | g_variant_get_boolean (prop_value) == TRUE) |
| 1960 | flags |= FWUPD_INSTALL_FLAG_FORCE; |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 1961 | g_variant_unref (prop_value); |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1964 | /* get the fd */ |
| 1965 | message = g_dbus_method_invocation_get_message (invocation); |
| 1966 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1967 | 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] | 1968 | g_set_error (&error, |
| 1969 | FWUPD_ERROR, |
| 1970 | FWUPD_ERROR_INTERNAL, |
| 1971 | "invalid handle"); |
| 1972 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1973 | return; |
| 1974 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame^] | 1975 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1976 | if (fd < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1977 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1978 | return; |
| 1979 | } |
| 1980 | |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1981 | /* read the entire fd to a data blob */ |
| 1982 | stream = g_unix_input_stream_new (fd, TRUE); |
| 1983 | blob_cab = g_input_stream_read_bytes (stream, |
| 1984 | FU_MAIN_FIRMWARE_SIZE_MAX, |
| 1985 | NULL, &error); |
| 1986 | if (blob_cab == NULL){ |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1987 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1988 | return; |
| 1989 | } |
| 1990 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 1991 | /* process the firmware */ |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1992 | helper = g_new0 (FuMainAuthHelper, 1); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1993 | helper->auth_kind = FU_MAIN_AUTH_KIND_INSTALL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1994 | helper->invocation = g_object_ref (invocation); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1995 | helper->trust_flags = FWUPD_TRUST_FLAG_NONE; |
| 1996 | helper->blob_cab = g_bytes_ref (blob_cab); |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1997 | helper->flags = flags; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1998 | helper->priv = priv; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1999 | helper->store = as_store_new (); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 2000 | 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] | 2001 | 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] | 2002 | if (item != NULL) |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 2003 | g_ptr_array_add (helper->devices, g_object_ref (item->device)); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 2004 | if (!fu_main_update_helper (helper, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2005 | fu_main_invocation_return_error (helper->priv, helper->invocation, error); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 2006 | fu_main_helper_free (helper); |
| 2007 | return; |
| 2008 | } |
| 2009 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 2010 | /* is root */ |
| 2011 | if (fu_main_dbus_get_uid (priv, sender) == 0) { |
| 2012 | if (!fu_main_provider_update_authenticated (helper, &error)) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2013 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 2014 | } else { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2015 | fu_main_invocation_return_value (priv, invocation, NULL); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 2016 | } |
| 2017 | fu_main_helper_free (helper); |
| 2018 | return; |
| 2019 | } |
| 2020 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 2021 | /* authenticate */ |
Richard Hughes | 63bbbf5 | 2015-04-14 16:12:16 +0100 | [diff] [blame] | 2022 | action_id = fu_main_get_action_id_for_device (helper); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2023 | subject = polkit_system_bus_name_new (sender); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 2024 | polkit_authority_check_authorization (helper->priv->authority, subject, |
Richard Hughes | a8e8394 | 2015-03-09 17:19:35 +0000 | [diff] [blame] | 2025 | action_id, |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2026 | NULL, |
| 2027 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 2028 | NULL, |
| 2029 | fu_main_check_authorization_cb, |
| 2030 | helper); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2031 | return; |
| 2032 | } |
| 2033 | |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 2034 | /* get a single result object from a local file */ |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2035 | if (g_strcmp0 (method_name, "GetDetails") == 0) { |
| 2036 | GDBusMessage *message; |
| 2037 | GUnixFDList *fd_list; |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2038 | gint32 fd_handle = 0; |
| 2039 | gint fd; |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2040 | |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 2041 | /* get parameters */ |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2042 | g_variant_get (parameters, "(h)", &fd_handle); |
| 2043 | g_debug ("Called %s(%i)", method_name, fd_handle); |
| 2044 | |
| 2045 | /* get the fd */ |
| 2046 | message = g_dbus_method_invocation_get_message (invocation); |
| 2047 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 2048 | 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] | 2049 | g_set_error (&error, |
| 2050 | FWUPD_ERROR, |
| 2051 | FWUPD_ERROR_INTERNAL, |
| 2052 | "invalid handle"); |
| 2053 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2054 | return; |
| 2055 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame^] | 2056 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2057 | if (fd < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2058 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2059 | return; |
| 2060 | } |
| 2061 | |
Richard Hughes | 8ac07dc | 2016-05-29 08:32:55 +0100 | [diff] [blame] | 2062 | /* get details about the file */ |
| 2063 | val = fu_main_get_details_from_fd (priv, fd, &error); |
| 2064 | if (val == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2065 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2066 | return; |
| 2067 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2068 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | cccc775 | 2015-03-06 11:13:19 +0000 | [diff] [blame] | 2069 | return; |
| 2070 | } |
| 2071 | |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2072 | /* get multiple result objects from a local file */ |
| 2073 | if (g_strcmp0 (method_name, "GetDetailsLocal") == 0) { |
| 2074 | GDBusMessage *message; |
| 2075 | GUnixFDList *fd_list; |
| 2076 | gint32 fd_handle = 0; |
| 2077 | gint fd; |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2078 | |
| 2079 | /* get parameters */ |
| 2080 | g_variant_get (parameters, "(h)", &fd_handle); |
| 2081 | g_debug ("Called %s(%i)", method_name, fd_handle); |
| 2082 | |
| 2083 | /* get the fd */ |
| 2084 | message = g_dbus_method_invocation_get_message (invocation); |
| 2085 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 2086 | 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] | 2087 | g_set_error (&error, |
| 2088 | FWUPD_ERROR, |
| 2089 | FWUPD_ERROR_INTERNAL, |
| 2090 | "invalid handle"); |
| 2091 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2092 | return; |
| 2093 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame^] | 2094 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2095 | if (fd < 0) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2096 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2097 | return; |
| 2098 | } |
| 2099 | |
| 2100 | /* get details about the file */ |
| 2101 | val = fu_main_get_details_local_from_fd (priv, fd, &error); |
| 2102 | if (val == NULL) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2103 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2104 | return; |
| 2105 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2106 | fu_main_invocation_return_value (priv, invocation, val); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 2107 | return; |
| 2108 | } |
| 2109 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2110 | /* we suck */ |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 2111 | g_set_error (&error, |
| 2112 | G_DBUS_ERROR, |
| 2113 | G_DBUS_ERROR_UNKNOWN_METHOD, |
| 2114 | "no such method %s", method_name); |
| 2115 | fu_main_invocation_return_error (priv, invocation, error); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2118 | static GVariant * |
| 2119 | fu_main_daemon_get_property (GDBusConnection *connection_, const gchar *sender, |
| 2120 | const gchar *object_path, const gchar *interface_name, |
| 2121 | const gchar *property_name, GError **error, |
| 2122 | gpointer user_data) |
| 2123 | { |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2124 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2125 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2126 | if (g_strcmp0 (property_name, "DaemonVersion") == 0) |
| 2127 | return g_variant_new_string (VERSION); |
| 2128 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2129 | if (g_strcmp0 (property_name, "Status") == 0) |
Richard Hughes | 37e9d77 | 2016-04-28 14:23:24 +0100 | [diff] [blame] | 2130 | return g_variant_new_uint32 (priv->status); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2131 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2132 | /* return an error */ |
| 2133 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 2134 | G_DBUS_ERROR, |
| 2135 | G_DBUS_ERROR_UNKNOWN_PROPERTY, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2136 | "failed to get daemon property %s", |
| 2137 | property_name); |
| 2138 | return NULL; |
| 2139 | } |
| 2140 | |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 2141 | static void |
| 2142 | fu_main_providers_coldplug (FuMainPrivate *priv) |
| 2143 | { |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2144 | g_autoptr(AsProfileTask) ptask = NULL; |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 2145 | |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2146 | ptask = as_profile_start_literal (priv->profile, "FuMain:coldplug"); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 2147 | for (guint i = 0; i < priv->providers->len; i++) { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 2148 | g_autoptr(GError) error = NULL; |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2149 | g_autoptr(AsProfileTask) ptask2 = NULL; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 2150 | FuProvider *provider = g_ptr_array_index (priv->providers, i); |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2151 | ptask2 = as_profile_start (priv->profile, |
| 2152 | "FuMain:coldplug{%s}", |
| 2153 | fu_provider_get_name (provider)); |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 2154 | if (!fu_provider_coldplug (FU_PROVIDER (provider), &error)) |
| 2155 | g_warning ("Failed to coldplug: %s", error->message); |
| 2156 | } |
| 2157 | } |
| 2158 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2159 | static void |
| 2160 | fu_main_on_bus_acquired_cb (GDBusConnection *connection, |
| 2161 | const gchar *name, |
| 2162 | gpointer user_data) |
| 2163 | { |
| 2164 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2165 | guint registration_id; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 2166 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2167 | static const GDBusInterfaceVTable interface_vtable = { |
| 2168 | fu_main_daemon_method_call, |
| 2169 | fu_main_daemon_get_property, |
| 2170 | NULL |
| 2171 | }; |
| 2172 | |
| 2173 | priv->connection = g_object_ref (connection); |
| 2174 | registration_id = g_dbus_connection_register_object (connection, |
| 2175 | FWUPD_DBUS_PATH, |
| 2176 | priv->introspection_daemon->interfaces[0], |
| 2177 | &interface_vtable, |
| 2178 | priv, /* user_data */ |
| 2179 | NULL, /* user_data_free_func */ |
| 2180 | NULL); /* GError** */ |
| 2181 | g_assert (registration_id > 0); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 2182 | |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 2183 | /* add devices */ |
| 2184 | fu_main_providers_coldplug (priv); |
| 2185 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 2186 | /* connect to D-Bus directly */ |
| 2187 | priv->proxy_uid = |
| 2188 | g_dbus_proxy_new_sync (priv->connection, |
| 2189 | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 2190 | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 2191 | NULL, |
| 2192 | "org.freedesktop.DBus", |
| 2193 | "/org/freedesktop/DBus", |
| 2194 | "org.freedesktop.DBus", |
| 2195 | NULL, |
| 2196 | &error); |
| 2197 | if (priv->proxy_uid == NULL) { |
| 2198 | g_warning ("cannot connect to DBus: %s", error->message); |
| 2199 | return; |
| 2200 | } |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2201 | |
Richard Hughes | 9559bbe | 2016-03-29 18:54:20 +0100 | [diff] [blame] | 2202 | /* connect to UPower */ |
| 2203 | priv->proxy_upower = |
| 2204 | g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, |
| 2205 | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 2206 | NULL, |
| 2207 | "org.freedesktop.UPower", |
| 2208 | "/org/freedesktop/UPower", |
| 2209 | "org.freedesktop.UPower", |
| 2210 | NULL, |
| 2211 | &error); |
| 2212 | if (priv->proxy_upower == NULL) { |
| 2213 | g_warning ("Failed to conect UPower: %s", error->message); |
| 2214 | return; |
| 2215 | } |
| 2216 | |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2217 | /* dump startup profile data */ |
Richard Hughes | 2a1e75d | 2015-12-18 17:42:53 +0000 | [diff] [blame] | 2218 | if (fu_debug_is_verbose ()) |
| 2219 | as_profile_dump (priv->profile); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2222 | static void |
| 2223 | fu_main_on_name_acquired_cb (GDBusConnection *connection, |
| 2224 | const gchar *name, |
| 2225 | gpointer user_data) |
| 2226 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2227 | g_debug ("FuMain: acquired name: %s", name); |
| 2228 | } |
| 2229 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2230 | static void |
| 2231 | fu_main_on_name_lost_cb (GDBusConnection *connection, |
| 2232 | const gchar *name, |
| 2233 | gpointer user_data) |
| 2234 | { |
| 2235 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2236 | g_debug ("FuMain: lost name: %s", name); |
| 2237 | g_main_loop_quit (priv->loop); |
| 2238 | } |
| 2239 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2240 | static gboolean |
| 2241 | fu_main_timed_exit_cb (gpointer user_data) |
| 2242 | { |
| 2243 | GMainLoop *loop = (GMainLoop *) user_data; |
| 2244 | g_main_loop_quit (loop); |
| 2245 | return G_SOURCE_REMOVE; |
| 2246 | } |
| 2247 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2248 | static GDBusNodeInfo * |
| 2249 | fu_main_load_introspection (const gchar *filename, GError **error) |
| 2250 | { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 2251 | g_autoptr(GBytes) data = NULL; |
| 2252 | g_autofree gchar *path = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2253 | |
| 2254 | /* lookup data */ |
| 2255 | path = g_build_filename ("/org/freedesktop/fwupd", filename, NULL); |
| 2256 | data = g_resource_lookup_data (fu_get_resource (), |
| 2257 | path, |
| 2258 | G_RESOURCE_LOOKUP_FLAGS_NONE, |
| 2259 | error); |
| 2260 | if (data == NULL) |
| 2261 | return NULL; |
| 2262 | |
| 2263 | /* build introspection from XML */ |
| 2264 | return g_dbus_node_info_new_for_xml (g_bytes_get_data (data, NULL), error); |
| 2265 | } |
| 2266 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2267 | static void |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2268 | fu_main_provider_device_added_cb (FuProvider *provider, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2269 | FuDevice *device, |
| 2270 | gpointer user_data) |
| 2271 | { |
| 2272 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2273 | FuDeviceItem *item; |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 2274 | AsApp *app; |
| 2275 | FuPlugin *plugin; |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 2276 | g_auto(GStrv) guids = NULL; |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 2277 | g_autoptr(GError) error = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2278 | |
Richard Hughes | fdf4616 | 2016-08-25 13:05:18 +0100 | [diff] [blame] | 2279 | /* device has no GUIDs set! */ |
| 2280 | if (fu_device_get_guid_default (device) == NULL) { |
| 2281 | g_warning ("no GUIDs for device %s [%s]", |
| 2282 | fu_device_get_id (device), |
| 2283 | fu_device_get_name (device)); |
| 2284 | return; |
| 2285 | } |
| 2286 | |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 2287 | /* is this GUID blacklisted */ |
| 2288 | guids = g_key_file_get_string_list (priv->config, |
| 2289 | "fwupd", |
| 2290 | "BlacklistDevices", |
| 2291 | NULL, /* length */ |
| 2292 | NULL); |
| 2293 | if (guids != NULL && |
| 2294 | g_strv_contains ((const gchar * const *) guids, |
| 2295 | fu_device_get_guid_default (device))) { |
| 2296 | g_debug ("%s is blacklisted [%s], ignoring from %s", |
| 2297 | fu_device_get_id (device), |
| 2298 | fu_device_get_guid_default (device), |
| 2299 | fu_provider_get_name (provider)); |
| 2300 | return; |
| 2301 | } |
| 2302 | |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 2303 | /* remove any fake device */ |
| 2304 | 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] | 2305 | if (item != NULL) { |
| 2306 | g_debug ("already added %s by %s, ignoring same device from %s", |
| 2307 | fu_device_get_id (item->device), |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 2308 | fu_device_get_provider (item->device), |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2309 | fu_provider_get_name (provider)); |
| 2310 | return; |
| 2311 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 2312 | |
| 2313 | /* create new device */ |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2314 | item = g_new0 (FuDeviceItem, 1); |
| 2315 | item->device = g_object_ref (device); |
| 2316 | item->provider = g_object_ref (provider); |
| 2317 | g_ptr_array_add (priv->devices, item); |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 2318 | |
| 2319 | /* does this match anything in the AppStream data */ |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 2320 | app = fu_main_store_get_app_by_guids (priv->store, item->device); |
Richard Hughes | dad1e19 | 2016-03-13 09:56:54 +0000 | [diff] [blame] | 2321 | if (app != NULL) { |
| 2322 | const gchar *tmp; |
| 2323 | tmp = as_app_get_metadata_item (app, FU_DEVICE_KEY_FWUPD_PLUGIN); |
| 2324 | if (tmp != NULL) { |
| 2325 | g_debug ("setting plugin: %s", tmp); |
| 2326 | fu_device_set_metadata (item->device, |
| 2327 | FU_DEVICE_KEY_FWUPD_PLUGIN, |
| 2328 | tmp); |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | /* run any plugins */ |
| 2333 | plugin = fu_main_get_plugin_for_device (priv->plugins, device); |
| 2334 | if (plugin != NULL) { |
| 2335 | if (!fu_plugin_run_device_probe (plugin, device, &error)) { |
| 2336 | g_warning ("failed to probe %s: %s", |
| 2337 | fu_device_get_id (item->device), |
| 2338 | error->message); |
| 2339 | } |
| 2340 | } |
| 2341 | |
Richard Hughes | 422e866 | 2016-04-28 15:05:33 +0100 | [diff] [blame] | 2342 | /* match the metadata at this point so clients can tell if the |
| 2343 | * device is worthy */ |
| 2344 | fu_main_get_updates_item_update (priv, item); |
| 2345 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 2346 | /* notify clients */ |
| 2347 | fu_main_emit_device_added (priv, item->device); |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 2348 | fu_main_emit_changed (priv); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2351 | static void |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2352 | fu_main_provider_device_removed_cb (FuProvider *provider, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2353 | FuDevice *device, |
| 2354 | gpointer user_data) |
| 2355 | { |
| 2356 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2357 | FuDeviceItem *item; |
| 2358 | |
| 2359 | item = fu_main_get_item_by_id (priv, fu_device_get_id (device)); |
| 2360 | if (item == NULL) { |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2361 | g_debug ("no device to remove %s", fu_device_get_id (device)); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2362 | return; |
| 2363 | } |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2364 | |
| 2365 | /* check this came from the same provider */ |
| 2366 | if (g_strcmp0 (fu_provider_get_name (provider), |
| 2367 | fu_provider_get_name (item->provider)) != 0) { |
| 2368 | g_debug ("ignoring duplicate removal from %s", |
| 2369 | fu_provider_get_name (provider)); |
| 2370 | return; |
| 2371 | } |
| 2372 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 2373 | /* make the UI update */ |
| 2374 | fu_main_emit_device_removed (priv, device); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2375 | g_ptr_array_remove (priv->devices, item); |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 2376 | fu_main_emit_changed (priv); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2379 | static void |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2380 | fu_main_provider_status_changed_cb (FuProvider *provider, |
Richard Hughes | f910ac9 | 2015-03-19 10:43:42 +0000 | [diff] [blame] | 2381 | FwupdStatus status, |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2382 | gpointer user_data) |
| 2383 | { |
| 2384 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2385 | fu_main_set_status (priv, status); |
| 2386 | } |
| 2387 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2388 | static void |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 2389 | fu_main_provider_percentage_changed_cb (FuProvider *provider, |
| 2390 | guint percentage, |
| 2391 | gpointer user_data) |
| 2392 | { |
| 2393 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 2394 | fu_main_set_percentage (priv, percentage); |
| 2395 | } |
| 2396 | |
| 2397 | static void |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2398 | fu_main_add_provider (FuMainPrivate *priv, FuProvider *provider) |
| 2399 | { |
| 2400 | g_signal_connect (provider, "device-added", |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2401 | G_CALLBACK (fu_main_provider_device_added_cb), |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2402 | priv); |
| 2403 | g_signal_connect (provider, "device-removed", |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2404 | G_CALLBACK (fu_main_provider_device_removed_cb), |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2405 | priv); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2406 | g_signal_connect (provider, "status-changed", |
Mario Limonciello | c2cbd1a | 2016-06-22 14:59:29 -0500 | [diff] [blame] | 2407 | G_CALLBACK (fu_main_provider_status_changed_cb), |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 2408 | priv); |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 2409 | g_signal_connect (provider, "percentage-changed", |
| 2410 | G_CALLBACK (fu_main_provider_percentage_changed_cb), |
| 2411 | priv); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2412 | g_ptr_array_add (priv->providers, provider); |
| 2413 | } |
| 2414 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2415 | int |
| 2416 | main (int argc, char *argv[]) |
| 2417 | { |
| 2418 | FuMainPrivate *priv = NULL; |
| 2419 | gboolean immediate_exit = FALSE; |
| 2420 | gboolean ret; |
| 2421 | gboolean timed_exit = FALSE; |
| 2422 | GOptionContext *context; |
| 2423 | guint owner_id = 0; |
Richard Hughes | 33a518a | 2016-07-27 15:22:53 +0100 | [diff] [blame] | 2424 | gint retval = 1; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2425 | const GOptionEntry options[] = { |
| 2426 | { "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit, |
| 2427 | /* TRANSLATORS: exit after we've started up, used for user profiling */ |
| 2428 | _("Exit after a small delay"), NULL }, |
| 2429 | { "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit, |
| 2430 | /* TRANSLATORS: exit straight away, used for automatic profiling */ |
| 2431 | _("Exit after the engine has loaded"), NULL }, |
| 2432 | { NULL} |
| 2433 | }; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 2434 | g_autoptr(GError) error = NULL; |
| 2435 | g_autofree gchar *config_file = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2436 | |
| 2437 | setlocale (LC_ALL, ""); |
| 2438 | |
| 2439 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 2440 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 2441 | textdomain (GETTEXT_PACKAGE); |
| 2442 | |
| 2443 | /* TRANSLATORS: program name */ |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 2444 | g_set_application_name (_("Firmware Update Daemon")); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2445 | context = g_option_context_new (NULL); |
| 2446 | g_option_context_add_main_entries (context, options, NULL); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2447 | g_option_context_add_group (context, fu_debug_get_option_group ()); |
Richard Hughes | 8ded6ca | 2015-03-16 12:51:36 +0000 | [diff] [blame] | 2448 | /* TRANSLATORS: program summary */ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2449 | g_option_context_set_summary (context, _("Firmware Update D-Bus Service")); |
| 2450 | ret = g_option_context_parse (context, &argc, &argv, &error); |
| 2451 | if (!ret) { |
| 2452 | g_warning ("FuMain: failed to parse command line arguments: %s", |
| 2453 | error->message); |
| 2454 | goto out; |
| 2455 | } |
| 2456 | |
| 2457 | /* create new objects */ |
| 2458 | priv = g_new0 (FuMainPrivate, 1); |
Richard Hughes | f910ac9 | 2015-03-19 10:43:42 +0000 | [diff] [blame] | 2459 | priv->status = FWUPD_STATUS_IDLE; |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 2460 | priv->percentage = 0; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2461 | 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] | 2462 | priv->loop = g_main_loop_new (NULL, FALSE); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 2463 | priv->pending = fu_pending_new (); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2464 | priv->store = as_store_new (); |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2465 | priv->profile = as_profile_new (); |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 2466 | g_signal_connect (priv->store, "changed", |
| 2467 | G_CALLBACK (fu_main_store_changed_cb), priv); |
| 2468 | as_store_set_watch_flags (priv->store, AS_STORE_WATCH_FLAG_ADDED | |
| 2469 | AS_STORE_WATCH_FLAG_REMOVED); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2470 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 2471 | /* load plugin */ |
| 2472 | priv->plugins = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 2473 | g_free, (GDestroyNotify) fu_plugin_free); |
| 2474 | if (!fu_main_load_plugins (priv->plugins, &error)) { |
| 2475 | g_print ("failed to load plugins: %s\n", error->message); |
| 2476 | retval = EXIT_FAILURE; |
| 2477 | goto out; |
| 2478 | } |
| 2479 | |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2480 | /* load AppStream */ |
Richard Hughes | 65dfbfe | 2016-03-02 13:42:53 +0000 | [diff] [blame] | 2481 | as_store_add_filter (priv->store, AS_APP_KIND_FIRMWARE); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2482 | if (!as_store_load (priv->store, |
| 2483 | AS_STORE_LOAD_FLAG_APP_INFO_SYSTEM, |
| 2484 | NULL, &error)){ |
| 2485 | g_warning ("FuMain: failed to load AppStream data: %s", |
| 2486 | error->message); |
| 2487 | return FALSE; |
| 2488 | } |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2489 | |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2490 | /* read config file */ |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2491 | config_file = g_build_filename (SYSCONFDIR, "fwupd.conf", NULL); |
| 2492 | g_debug ("Loading fallback values from %s", config_file); |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 2493 | priv->config = g_key_file_new (); |
| 2494 | if (!g_key_file_load_from_file (priv->config, config_file, |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2495 | G_KEY_FILE_NONE, &error)) { |
| 2496 | g_print ("failed to load config file %s: %s\n", |
| 2497 | config_file, error->message); |
| 2498 | retval = EXIT_FAILURE; |
| 2499 | goto out; |
| 2500 | } |
| 2501 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2502 | /* add providers */ |
| 2503 | 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] | 2504 | if (g_key_file_get_boolean (priv->config, "fwupd", "EnableOptionROM", NULL)) |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2505 | fu_main_add_provider (priv, fu_provider_udev_new ()); |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2506 | fu_main_add_provider (priv, fu_provider_dfu_new ()); |
Richard Hughes | 25cf6ab | 2015-08-04 21:34:12 +0100 | [diff] [blame] | 2507 | fu_main_add_provider (priv, fu_provider_rpi_new ()); |
Richard Hughes | 14d1764 | 2016-08-17 12:03:03 +0100 | [diff] [blame] | 2508 | fu_main_add_provider (priv, fu_provider_ebitdo_new ()); |
Richard Hughes | 3c99ba4 | 2015-03-05 12:17:48 +0000 | [diff] [blame] | 2509 | #ifdef HAVE_COLORHUG |
Richard Hughes | 72dff81 | 2015-03-03 15:13:25 +0000 | [diff] [blame] | 2510 | fu_main_add_provider (priv, fu_provider_chug_new ()); |
Richard Hughes | 3c99ba4 | 2015-03-05 12:17:48 +0000 | [diff] [blame] | 2511 | #endif |
| 2512 | #ifdef HAVE_UEFI |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 2513 | fu_main_add_provider (priv, fu_provider_uefi_new ()); |
Richard Hughes | 3c99ba4 | 2015-03-05 12:17:48 +0000 | [diff] [blame] | 2514 | #endif |
Mario Limonciello | 958ead6 | 2016-05-14 00:10:25 -0500 | [diff] [blame] | 2515 | #ifdef HAVE_DELL |
| 2516 | fu_main_add_provider (priv, fu_provider_dell_new ()); |
| 2517 | #endif |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2518 | |
Richard Hughes | 5d057a8 | 2015-11-24 18:09:57 +0000 | [diff] [blame] | 2519 | /* last as least priority */ |
| 2520 | fu_main_add_provider (priv, fu_provider_usb_new ()); |
| 2521 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2522 | /* load introspection from file */ |
| 2523 | priv->introspection_daemon = fu_main_load_introspection (FWUPD_DBUS_INTERFACE ".xml", |
| 2524 | &error); |
| 2525 | if (priv->introspection_daemon == NULL) { |
| 2526 | g_warning ("FuMain: failed to load daemon introspection: %s", |
| 2527 | error->message); |
| 2528 | goto out; |
| 2529 | } |
| 2530 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2531 | /* get authority */ |
| 2532 | priv->authority = polkit_authority_get_sync (NULL, &error); |
| 2533 | if (priv->authority == NULL) { |
| 2534 | g_warning ("FuMain: failed to load polkit authority: %s", |
| 2535 | error->message); |
| 2536 | goto out; |
| 2537 | } |
| 2538 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2539 | /* own the object */ |
| 2540 | owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, |
| 2541 | FWUPD_DBUS_SERVICE, |
| 2542 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 2543 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 2544 | fu_main_on_bus_acquired_cb, |
| 2545 | fu_main_on_name_acquired_cb, |
| 2546 | fu_main_on_name_lost_cb, |
| 2547 | priv, NULL); |
| 2548 | |
| 2549 | /* Only timeout and close the mainloop if we have specified it |
| 2550 | * on the command line */ |
| 2551 | if (immediate_exit) |
| 2552 | g_idle_add (fu_main_timed_exit_cb, priv->loop); |
| 2553 | else if (timed_exit) |
| 2554 | g_timeout_add_seconds (5, fu_main_timed_exit_cb, priv->loop); |
| 2555 | |
| 2556 | /* wait */ |
| 2557 | g_info ("Daemon ready for requests"); |
| 2558 | g_main_loop_run (priv->loop); |
| 2559 | |
| 2560 | /* success */ |
| 2561 | retval = 0; |
| 2562 | out: |
| 2563 | g_option_context_free (context); |
| 2564 | if (owner_id > 0) |
| 2565 | g_bus_unown_name (owner_id); |
| 2566 | if (priv != NULL) { |
| 2567 | if (priv->loop != NULL) |
| 2568 | g_main_loop_unref (priv->loop); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 2569 | if (priv->proxy_uid != NULL) |
| 2570 | g_object_unref (priv->proxy_uid); |
Richard Hughes | 9559bbe | 2016-03-29 18:54:20 +0100 | [diff] [blame] | 2571 | if (priv->proxy_upower != NULL) |
| 2572 | g_object_unref (priv->proxy_upower); |
Richard Hughes | 9a52c5e | 2016-07-18 09:17:02 +0100 | [diff] [blame] | 2573 | if (priv->config != NULL) |
Richard Hughes | fec2480 | 2016-11-04 09:30:14 +0000 | [diff] [blame] | 2574 | g_key_file_unref (priv->config); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2575 | if (priv->connection != NULL) |
| 2576 | g_object_unref (priv->connection); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 2577 | if (priv->authority != NULL) |
| 2578 | g_object_unref (priv->authority); |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 2579 | if (priv->profile != NULL) |
| 2580 | g_object_unref (priv->profile); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 2581 | if (priv->store != NULL) |
| 2582 | g_object_unref (priv->store); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2583 | if (priv->introspection_daemon != NULL) |
| 2584 | g_dbus_node_info_unref (priv->introspection_daemon); |
Richard Hughes | 033ccba | 2015-09-10 14:51:28 +0100 | [diff] [blame] | 2585 | if (priv->store_changed_id != 0) |
| 2586 | g_source_remove (priv->store_changed_id); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 2587 | g_object_unref (priv->pending); |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 2588 | if (priv->providers != NULL) |
| 2589 | g_ptr_array_unref (priv->providers); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 2590 | if (priv->plugins != NULL) |
| 2591 | g_hash_table_unref (priv->plugins); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 2592 | g_ptr_array_unref (priv->devices); |
| 2593 | g_free (priv); |
| 2594 | } |
| 2595 | return retval; |
| 2596 | } |
| 2597 | |