Richard Hughes | 02c90d8 | 2018-08-09 12:13:03 +0100 | [diff] [blame] | 1 | /* |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 2 | * Copyright (C) 2015-2018 Richard Hughes <richard@hughsie.com> |
| 3 | * |
Mario Limonciello | 51308e6 | 2018-05-28 20:05:46 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: LGPL-2.1+ |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Richard Hughes | b08e7bc | 2018-09-11 10:51:13 +0100 | [diff] [blame] | 7 | #define G_LOG_DOMAIN "FuMain" |
| 8 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 9 | #include "config.h" |
| 10 | |
| 11 | #include <fwupd.h> |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 12 | #include <glib/gstdio.h> |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 13 | #include <glib/gi18n.h> |
| 14 | #include <glib-unix.h> |
| 15 | #include <locale.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <unistd.h> |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 18 | #include <libsoup/soup.h> |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 19 | |
Mario Limonciello | 7a3df4b | 2019-01-31 10:27:22 -0600 | [diff] [blame] | 20 | #include "fu-device-private.h" |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 21 | #include "fu-engine.h" |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 22 | #include "fu-history.h" |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 23 | #include "fu-plugin-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 24 | #include "fu-progressbar.h" |
| 25 | #include "fu-smbios.h" |
| 26 | #include "fu-util-common.h" |
Mario Limonciello | fde4773 | 2018-09-11 12:20:58 -0500 | [diff] [blame] | 27 | #include "fu-debug.h" |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 28 | #include "fwupd-common-private.h" |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 29 | #include "fwupd-device-private.h" |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 30 | |
Richard Hughes | 3d00522 | 2019-05-17 14:02:41 +0100 | [diff] [blame] | 31 | #ifdef HAVE_SYSTEMD |
| 32 | #include "fu-systemd.h" |
| 33 | #endif |
| 34 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 35 | /* custom return code */ |
| 36 | #define EXIT_NOTHING_TO_DO 2 |
| 37 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 38 | typedef enum { |
| 39 | FU_UTIL_OPERATION_UNKNOWN, |
| 40 | FU_UTIL_OPERATION_UPDATE, |
| 41 | FU_UTIL_OPERATION_INSTALL, |
| 42 | FU_UTIL_OPERATION_LAST |
| 43 | } FuUtilOperation; |
| 44 | |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 45 | struct FuUtilPrivate { |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 46 | GCancellable *cancellable; |
| 47 | GMainLoop *loop; |
| 48 | GOptionContext *context; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 49 | FuEngine *engine; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 50 | FuProgressbar *progressbar; |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 51 | gboolean no_reboot_check; |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 52 | gboolean prepare_blob; |
| 53 | gboolean cleanup_blob; |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 54 | gboolean enable_json_state; |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 55 | FwupdInstallFlags flags; |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 56 | gboolean show_all_devices; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 57 | /* only valid in update and downgrade */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 58 | FuUtilOperation current_operation; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 59 | FwupdDevice *current_device; |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 60 | gchar *current_message; |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 61 | FwupdDeviceFlags completion_flags; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 62 | FwupdDeviceFlags filter_include; |
| 63 | FwupdDeviceFlags filter_exclude; |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 64 | }; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 65 | |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 66 | static gboolean |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 67 | fu_util_save_current_state (FuUtilPrivate *priv, GError **error) |
| 68 | { |
| 69 | g_autoptr(JsonBuilder) builder = NULL; |
| 70 | g_autoptr(JsonGenerator) json_generator = NULL; |
| 71 | g_autoptr(JsonNode) json_root = NULL; |
| 72 | g_autoptr(GPtrArray) devices = NULL; |
| 73 | g_autofree gchar *state = NULL; |
| 74 | g_autofree gchar *dirname = NULL; |
| 75 | g_autofree gchar *filename = NULL; |
| 76 | |
| 77 | if (!priv->enable_json_state) |
| 78 | return TRUE; |
| 79 | |
| 80 | devices = fu_engine_get_devices (priv->engine, error); |
| 81 | if (devices == NULL) |
| 82 | return FALSE; |
| 83 | |
| 84 | /* create header */ |
| 85 | builder = json_builder_new (); |
| 86 | json_builder_begin_object (builder); |
| 87 | |
| 88 | /* add each device */ |
| 89 | json_builder_set_member_name (builder, "Devices"); |
| 90 | json_builder_begin_array (builder); |
| 91 | for (guint i = 0; i < devices->len; i++) { |
| 92 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 93 | json_builder_begin_object (builder); |
| 94 | fwupd_device_to_json (dev, builder); |
| 95 | json_builder_end_object (builder); |
| 96 | } |
| 97 | json_builder_end_array (builder); |
| 98 | json_builder_end_object (builder); |
| 99 | |
| 100 | /* export as a string */ |
| 101 | json_root = json_builder_get_root (builder); |
| 102 | json_generator = json_generator_new (); |
| 103 | json_generator_set_pretty (json_generator, TRUE); |
| 104 | json_generator_set_root (json_generator, json_root); |
| 105 | state = json_generator_to_data (json_generator, NULL); |
| 106 | if (state == NULL) |
| 107 | return FALSE; |
| 108 | dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
| 109 | filename = g_build_filename (dirname, "state.json", NULL); |
| 110 | return g_file_set_contents (filename, state, -1, error); |
| 111 | } |
| 112 | |
| 113 | static gboolean |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 114 | fu_util_start_engine (FuUtilPrivate *priv, FuEngineLoadFlags flags, GError **error) |
Mario Limonciello | e8dd4d7 | 2019-02-26 15:28:04 -0600 | [diff] [blame] | 115 | { |
| 116 | g_autoptr(GError) error_local = NULL; |
| 117 | |
Richard Hughes | d92ccca | 2019-05-20 11:28:31 +0100 | [diff] [blame] | 118 | #ifdef HAVE_SYSTEMD |
Richard Hughes | 3d00522 | 2019-05-17 14:02:41 +0100 | [diff] [blame] | 119 | if (!fu_systemd_unit_stop (fu_util_get_systemd_unit (), &error_local)) |
Mario Limonciello | e8dd4d7 | 2019-02-26 15:28:04 -0600 | [diff] [blame] | 120 | g_debug ("Failed top stop daemon: %s", error_local->message); |
Richard Hughes | d92ccca | 2019-05-20 11:28:31 +0100 | [diff] [blame] | 121 | #endif |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 122 | if (!fu_engine_load (priv->engine, flags, error)) |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 123 | return FALSE; |
| 124 | if (fu_engine_get_tainted (priv->engine)) { |
| 125 | g_printerr ("WARNING: This tool has loaded 3rd party code and " |
| 126 | "is no longer supported by the upstream developers!\n"); |
| 127 | } |
| 128 | return TRUE; |
Mario Limonciello | e61c94d | 2018-10-11 10:49:55 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 131 | static void |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 132 | fu_util_maybe_prefix_sandbox_error (const gchar *value, GError **error) |
| 133 | { |
| 134 | g_autofree gchar *path = g_path_get_dirname (value); |
| 135 | if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { |
| 136 | g_prefix_error (error, |
| 137 | "Unable to access %s. You may need to copy %s to %s: ", |
| 138 | path, value, g_getenv ("HOME")); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 143 | fu_util_cancelled_cb (GCancellable *cancellable, gpointer user_data) |
| 144 | { |
| 145 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 146 | /* TRANSLATORS: this is when a device ctrl+c's a watch */ |
| 147 | g_print ("%s\n", _("Cancelled")); |
| 148 | g_main_loop_quit (priv->loop); |
| 149 | } |
| 150 | |
| 151 | static gboolean |
| 152 | fu_util_smbios_dump (FuUtilPrivate *priv, gchar **values, GError **error) |
| 153 | { |
| 154 | g_autofree gchar *tmp = NULL; |
| 155 | g_autoptr(FuSmbios) smbios = NULL; |
| 156 | if (g_strv_length (values) < 1) { |
| 157 | g_set_error_literal (error, |
| 158 | FWUPD_ERROR, |
| 159 | FWUPD_ERROR_INVALID_ARGS, |
| 160 | "Invalid arguments"); |
| 161 | return FALSE; |
| 162 | } |
| 163 | smbios = fu_smbios_new (); |
| 164 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 165 | return FALSE; |
| 166 | tmp = fu_smbios_to_string (smbios); |
| 167 | g_print ("%s\n", tmp); |
| 168 | return TRUE; |
| 169 | } |
| 170 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 171 | static gboolean |
| 172 | fu_util_sigint_cb (gpointer user_data) |
| 173 | { |
| 174 | FuUtilPrivate *priv = (FuUtilPrivate *) user_data; |
| 175 | g_debug ("Handling SIGINT"); |
| 176 | g_cancellable_cancel (priv->cancellable); |
| 177 | return FALSE; |
| 178 | } |
| 179 | |
| 180 | static void |
| 181 | fu_util_private_free (FuUtilPrivate *priv) |
| 182 | { |
Mario Limonciello | cc50e1a | 2018-08-14 17:45:24 -0500 | [diff] [blame] | 183 | if (priv->current_device != NULL) |
| 184 | g_object_unref (priv->current_device); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 185 | if (priv->engine != NULL) |
| 186 | g_object_unref (priv->engine); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 187 | if (priv->loop != NULL) |
| 188 | g_main_loop_unref (priv->loop); |
| 189 | if (priv->cancellable != NULL) |
| 190 | g_object_unref (priv->cancellable); |
| 191 | if (priv->progressbar != NULL) |
| 192 | g_object_unref (priv->progressbar); |
| 193 | if (priv->context != NULL) |
| 194 | g_option_context_free (priv->context); |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 195 | g_free (priv->current_message); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 196 | g_free (priv); |
| 197 | } |
| 198 | |
| 199 | #pragma clang diagnostic push |
| 200 | #pragma clang diagnostic ignored "-Wunused-function" |
| 201 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuUtilPrivate, fu_util_private_free) |
| 202 | #pragma clang diagnostic pop |
| 203 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 204 | |
| 205 | static void |
| 206 | fu_main_engine_device_added_cb (FuEngine *engine, |
| 207 | FuDevice *device, |
| 208 | FuUtilPrivate *priv) |
| 209 | { |
| 210 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 211 | g_debug ("ADDED:\n%s", tmp); |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | fu_main_engine_device_removed_cb (FuEngine *engine, |
| 216 | FuDevice *device, |
| 217 | FuUtilPrivate *priv) |
| 218 | { |
| 219 | g_autofree gchar *tmp = fu_device_to_string (device); |
| 220 | g_debug ("REMOVED:\n%s", tmp); |
| 221 | } |
| 222 | |
| 223 | static void |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 224 | fu_main_engine_status_changed_cb (FuEngine *engine, |
| 225 | FwupdStatus status, |
| 226 | FuUtilPrivate *priv) |
| 227 | { |
| 228 | fu_progressbar_update (priv->progressbar, status, 0); |
| 229 | } |
| 230 | |
| 231 | static void |
| 232 | fu_main_engine_percentage_changed_cb (FuEngine *engine, |
| 233 | guint percentage, |
| 234 | FuUtilPrivate *priv) |
| 235 | { |
| 236 | fu_progressbar_update (priv->progressbar, FWUPD_STATUS_UNKNOWN, percentage); |
| 237 | } |
| 238 | |
| 239 | static gboolean |
| 240 | fu_util_watch (FuUtilPrivate *priv, gchar **values, GError **error) |
| 241 | { |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 242 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 243 | return FALSE; |
| 244 | g_main_loop_run (priv->loop); |
| 245 | return TRUE; |
| 246 | } |
| 247 | |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 248 | static gint |
| 249 | fu_util_plugin_name_sort_cb (FuPlugin **item1, FuPlugin **item2) |
| 250 | { |
| 251 | return fu_plugin_name_compare (*item1, *item2); |
| 252 | } |
| 253 | |
| 254 | static gboolean |
| 255 | fu_util_get_plugins (FuUtilPrivate *priv, gchar **values, GError **error) |
| 256 | { |
| 257 | GPtrArray *plugins; |
| 258 | guint cnt = 0; |
| 259 | |
| 260 | /* load engine */ |
| 261 | if (!fu_engine_load_plugins (priv->engine, error)) |
| 262 | return FALSE; |
| 263 | |
| 264 | /* print */ |
| 265 | plugins = fu_engine_get_plugins (priv->engine); |
| 266 | g_ptr_array_sort (plugins, (GCompareFunc) fu_util_plugin_name_sort_cb); |
| 267 | for (guint i = 0; i < plugins->len; i++) { |
| 268 | FuPlugin *plugin = g_ptr_array_index (plugins, i); |
| 269 | if (!fu_plugin_get_enabled (plugin)) |
| 270 | continue; |
| 271 | g_print ("%s\n", fu_plugin_get_name (plugin)); |
| 272 | cnt++; |
| 273 | } |
| 274 | if (cnt == 0) { |
| 275 | /* TRANSLATORS: nothing found */ |
| 276 | g_print ("%s\n", _("No plugins found")); |
| 277 | return TRUE; |
| 278 | } |
| 279 | |
| 280 | return TRUE; |
| 281 | } |
| 282 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 283 | static gboolean |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 284 | fu_util_filter_device (FuUtilPrivate *priv, FwupdDevice *dev) |
| 285 | { |
| 286 | if (priv->filter_include != FWUPD_DEVICE_FLAG_NONE) { |
| 287 | if (!fwupd_device_has_flag (dev, priv->filter_include)) |
| 288 | return FALSE; |
| 289 | } |
| 290 | if (priv->filter_exclude != FWUPD_DEVICE_FLAG_NONE) { |
| 291 | if (fwupd_device_has_flag (dev, priv->filter_exclude)) |
| 292 | return FALSE; |
| 293 | } |
| 294 | return TRUE; |
| 295 | } |
| 296 | |
| 297 | static gboolean |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 298 | fu_util_get_updates (FuUtilPrivate *priv, gchar **values, GError **error) |
| 299 | { |
| 300 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 301 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 302 | |
| 303 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 304 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 305 | return FALSE; |
| 306 | |
| 307 | /* get devices from daemon */ |
| 308 | devices = fu_engine_get_devices (priv->engine, error); |
| 309 | if (devices == NULL) |
| 310 | return FALSE; |
| 311 | for (guint i = 0; i < devices->len; i++) { |
| 312 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 313 | g_autoptr(GPtrArray) rels = NULL; |
| 314 | g_autoptr(GError) error_local = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 315 | GNode *child; |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 316 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 317 | /* not going to have results, so save a engine round-trip */ |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 318 | if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) |
| 319 | continue; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 320 | if (!fu_util_filter_device (priv, dev)) |
| 321 | continue; |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 322 | |
| 323 | /* get the releases for this device and filter for validity */ |
| 324 | rels = fu_engine_get_upgrades (priv->engine, |
| 325 | fwupd_device_get_id (dev), |
| 326 | &error_local); |
| 327 | if (rels == NULL) { |
| 328 | g_printerr ("%s\n", error_local->message); |
| 329 | continue; |
| 330 | } |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 331 | child = g_node_append_data (root, dev); |
| 332 | |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 333 | for (guint j = 0; j < rels->len; j++) { |
| 334 | FwupdRelease *rel = g_ptr_array_index (rels, j); |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 335 | g_node_append_data (child, g_object_ref (rel)); |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 336 | } |
| 337 | } |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 338 | if (g_node_n_nodes (root, G_TRAVERSE_ALL) > 1) |
| 339 | fu_util_print_tree (root, priv); |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 340 | /* save the device state for other applications to see */ |
| 341 | if (!fu_util_save_current_state (priv, error)) |
| 342 | return FALSE; |
| 343 | |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 344 | /* success */ |
| 345 | return TRUE; |
| 346 | } |
| 347 | |
| 348 | static gboolean |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 349 | fu_util_get_details (FuUtilPrivate *priv, gchar **values, GError **error) |
| 350 | { |
| 351 | g_autoptr(GPtrArray) array = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 352 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 353 | gint fd; |
| 354 | |
| 355 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 356 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 357 | return FALSE; |
| 358 | |
| 359 | /* check args */ |
| 360 | if (g_strv_length (values) != 1) { |
| 361 | g_set_error_literal (error, |
| 362 | FWUPD_ERROR, |
| 363 | FWUPD_ERROR_INVALID_ARGS, |
| 364 | "Invalid arguments"); |
| 365 | return FALSE; |
| 366 | } |
| 367 | |
| 368 | /* open file */ |
| 369 | fd = open (values[0], O_RDONLY); |
| 370 | if (fd < 0) { |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 371 | fu_util_maybe_prefix_sandbox_error (values[0], error); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 372 | g_set_error (error, |
| 373 | FWUPD_ERROR, |
| 374 | FWUPD_ERROR_INVALID_FILE, |
| 375 | "failed to open %s", |
| 376 | values[0]); |
| 377 | return FALSE; |
| 378 | } |
| 379 | array = fu_engine_get_details (priv->engine, fd, error); |
| 380 | close (fd); |
| 381 | |
| 382 | if (array == NULL) |
| 383 | return FALSE; |
| 384 | for (guint i = 0; i < array->len; i++) { |
| 385 | FwupdDevice *dev = g_ptr_array_index (array, i); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 386 | if (!fu_util_filter_device (priv, dev)) |
| 387 | continue; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 388 | g_node_append_data (root, dev); |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 389 | } |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 390 | fu_util_print_tree (root, priv); |
| 391 | |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 392 | return TRUE; |
| 393 | } |
| 394 | |
| 395 | static gboolean |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 396 | fu_util_get_device_flags (FuUtilPrivate *priv, gchar **values, GError **error) |
| 397 | { |
| 398 | g_autoptr(GString) str = g_string_new (NULL); |
| 399 | |
| 400 | for (FwupdDeviceFlags i = FWUPD_DEVICE_FLAG_INTERNAL; i < FWUPD_DEVICE_FLAG_UNKNOWN; i<<=1) { |
| 401 | const gchar *tmp = fwupd_device_flag_to_string (i); |
| 402 | if (tmp == NULL) |
| 403 | break; |
| 404 | if (i != FWUPD_DEVICE_FLAG_INTERNAL) |
| 405 | g_string_append (str, " "); |
| 406 | g_string_append (str, tmp); |
| 407 | g_string_append (str, " ~"); |
| 408 | g_string_append (str, tmp); |
| 409 | } |
| 410 | g_print ("%s\n", str->str); |
| 411 | |
| 412 | return TRUE; |
| 413 | } |
| 414 | |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 415 | static void |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 416 | fu_util_build_device_tree (FuUtilPrivate *priv, GNode *root, GPtrArray *devs, FuDevice *dev) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 417 | { |
| 418 | for (guint i = 0; i < devs->len; i++) { |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 419 | FuDevice *dev_tmp = g_ptr_array_index (devs, i); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 420 | if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev_tmp))) |
| 421 | continue; |
Mario Limonciello | d1775bc | 2018-07-17 00:28:52 -0500 | [diff] [blame] | 422 | if (!priv->show_all_devices && |
| 423 | !fu_util_is_interesting_device (FWUPD_DEVICE (dev_tmp))) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 424 | continue; |
Richard Hughes | 0d1577e | 2018-05-29 13:59:06 +0100 | [diff] [blame] | 425 | if (fu_device_get_parent (dev_tmp) == dev) { |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 426 | GNode *child = g_node_append_data (root, dev_tmp); |
| 427 | fu_util_build_device_tree (priv, child, devs, dev_tmp); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static gboolean |
Mario Limonciello | 1a9127d | 2019-08-27 11:32:51 +0100 | [diff] [blame] | 433 | fu_util_get_devices (FuUtilPrivate *priv, gchar **values, GError **error) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 434 | { |
| 435 | g_autoptr(GNode) root = g_node_new (NULL); |
| 436 | g_autoptr(GPtrArray) devs = NULL; |
| 437 | |
| 438 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 439 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 440 | return FALSE; |
| 441 | |
| 442 | /* print */ |
| 443 | devs = fu_engine_get_devices (priv->engine, error); |
| 444 | if (devs == NULL) |
| 445 | return FALSE; |
| 446 | |
| 447 | /* print */ |
| 448 | if (devs->len == 0) { |
| 449 | /* TRANSLATORS: nothing attached that can be upgraded */ |
| 450 | g_print ("%s\n", _("No hardware detected with firmware update capability")); |
| 451 | return TRUE; |
| 452 | } |
| 453 | fu_util_build_device_tree (priv, root, devs, NULL); |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 454 | fu_util_print_tree (root, priv); |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 455 | |
Mario Limonciello | 1a9127d | 2019-08-27 11:32:51 +0100 | [diff] [blame] | 456 | /* save the device state for other applications to see */ |
| 457 | return fu_util_save_current_state (priv, error); |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 458 | } |
| 459 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 460 | static FuDevice * |
| 461 | fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error) |
| 462 | { |
| 463 | FuDevice *dev; |
| 464 | guint idx; |
| 465 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 466 | g_autoptr(GPtrArray) devices_filtered = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 467 | |
| 468 | /* get devices from daemon */ |
| 469 | devices = fu_engine_get_devices (priv->engine, error); |
| 470 | if (devices == NULL) |
| 471 | return NULL; |
| 472 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 473 | /* filter results */ |
| 474 | devices_filtered = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 475 | for (guint i = 0; i < devices->len; i++) { |
| 476 | dev = g_ptr_array_index (devices, i); |
| 477 | if (!fu_util_filter_device (priv, FWUPD_DEVICE (dev))) |
| 478 | continue; |
| 479 | if (!fwupd_device_has_flag (FWUPD_DEVICE (dev), FWUPD_DEVICE_FLAG_SUPPORTED)) |
| 480 | continue; |
| 481 | g_ptr_array_add (devices_filtered, g_object_ref (dev)); |
| 482 | } |
| 483 | |
| 484 | /* nothing */ |
| 485 | if (devices_filtered->len == 0) { |
| 486 | g_set_error_literal (error, |
| 487 | FWUPD_ERROR, |
| 488 | FWUPD_ERROR_NOTHING_TO_DO, |
| 489 | "No supported devices"); |
| 490 | return NULL; |
| 491 | } |
| 492 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 493 | /* exactly one */ |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 494 | if (devices_filtered->len == 1) { |
| 495 | dev = g_ptr_array_index (devices_filtered, 0); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 496 | return g_object_ref (dev); |
| 497 | } |
| 498 | |
| 499 | /* TRANSLATORS: get interactive prompt */ |
| 500 | g_print ("%s\n", _("Choose a device:")); |
| 501 | /* TRANSLATORS: this is to abort the interactive prompt */ |
| 502 | g_print ("0.\t%s\n", _("Cancel")); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 503 | for (guint i = 0; i < devices_filtered->len; i++) { |
| 504 | dev = g_ptr_array_index (devices_filtered, i); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 505 | g_print ("%u.\t%s (%s)\n", |
| 506 | i + 1, |
| 507 | fu_device_get_id (dev), |
| 508 | fu_device_get_name (dev)); |
| 509 | } |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 510 | idx = fu_util_prompt_for_number (devices_filtered->len); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 511 | if (idx == 0) { |
| 512 | g_set_error_literal (error, |
| 513 | FWUPD_ERROR, |
| 514 | FWUPD_ERROR_NOTHING_TO_DO, |
| 515 | "Request canceled"); |
| 516 | return NULL; |
| 517 | } |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 518 | dev = g_ptr_array_index (devices_filtered, idx - 1); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 519 | return g_object_ref (dev); |
| 520 | } |
| 521 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 522 | static void |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 523 | fu_util_update_device_changed_cb (FwupdClient *client, |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 524 | FwupdDevice *device, |
| 525 | FuUtilPrivate *priv) |
| 526 | { |
| 527 | g_autofree gchar *str = NULL; |
| 528 | |
Richard Hughes | 809abea | 2019-03-23 11:06:18 +0000 | [diff] [blame] | 529 | /* allowed to set whenever the device has changed */ |
| 530 | if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) |
| 531 | priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN; |
| 532 | if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT)) |
| 533 | priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT; |
| 534 | |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 535 | /* same as last time, so ignore */ |
| 536 | if (priv->current_device != NULL && |
| 537 | fwupd_device_compare (priv->current_device, device) == 0) |
| 538 | return; |
| 539 | |
| 540 | /* show message in progressbar */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 541 | if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) { |
| 542 | /* TRANSLATORS: %1 is a device name */ |
| 543 | str = g_strdup_printf (_("Updating %s…"), |
| 544 | fwupd_device_get_name (device)); |
| 545 | fu_progressbar_set_title (priv->progressbar, str); |
| 546 | } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) { |
| 547 | /* TRANSLATORS: %1 is a device name */ |
| 548 | str = g_strdup_printf (_("Installing on %s…"), |
| 549 | fwupd_device_get_name (device)); |
| 550 | fu_progressbar_set_title (priv->progressbar, str); |
| 551 | } else { |
| 552 | g_warning ("no FuUtilOperation set"); |
| 553 | } |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 554 | g_set_object (&priv->current_device, device); |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 555 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 556 | if (priv->current_message == NULL) { |
| 557 | const gchar *tmp = fwupd_device_get_update_message (priv->current_device); |
| 558 | if (tmp != NULL) |
| 559 | priv->current_message = g_strdup (tmp); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | static void |
| 564 | fu_util_display_current_message (FuUtilPrivate *priv) |
| 565 | { |
| 566 | if (priv->current_message == NULL) |
| 567 | return; |
| 568 | g_print ("%s\n", priv->current_message); |
| 569 | g_clear_pointer (&priv->current_message, g_free); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 570 | } |
| 571 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 572 | static gboolean |
| 573 | fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error) |
| 574 | { |
| 575 | g_autoptr(FuDevice) device = NULL; |
| 576 | g_autoptr(GBytes) blob_fw = NULL; |
| 577 | |
| 578 | /* invalid args */ |
| 579 | if (g_strv_length (values) == 0) { |
| 580 | g_set_error_literal (error, |
| 581 | FWUPD_ERROR, |
| 582 | FWUPD_ERROR_INVALID_ARGS, |
| 583 | "Invalid arguments"); |
| 584 | return FALSE; |
| 585 | } |
| 586 | |
| 587 | /* parse blob */ |
| 588 | blob_fw = fu_common_get_contents_bytes (values[0], error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 589 | if (blob_fw == NULL) { |
| 590 | fu_util_maybe_prefix_sandbox_error (values[0], error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 591 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 592 | } |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 593 | |
| 594 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 595 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 596 | return FALSE; |
| 597 | |
| 598 | /* get device */ |
| 599 | if (g_strv_length (values) >= 2) { |
| 600 | device = fu_engine_get_device (priv->engine, values[1], error); |
| 601 | if (device == NULL) |
| 602 | return FALSE; |
| 603 | } else { |
| 604 | device = fu_util_prompt_for_device (priv, error); |
| 605 | if (device == NULL) |
| 606 | return FALSE; |
| 607 | } |
| 608 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 609 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 610 | g_signal_connect (priv->engine, "device-changed", |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 611 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 612 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 613 | /* write bare firmware */ |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 614 | if (priv->prepare_blob) { |
| 615 | g_autoptr(GPtrArray) devices = NULL; |
| 616 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 617 | g_ptr_array_add (devices, g_object_ref (device)); |
| 618 | if (!fu_engine_composite_prepare (priv->engine, devices, error)) { |
| 619 | g_prefix_error (error, "failed to prepare composite action: "); |
| 620 | return FALSE; |
| 621 | } |
| 622 | } |
Mario Limonciello | 035818b | 2019-03-26 11:12:16 -0500 | [diff] [blame] | 623 | priv->flags = FWUPD_INSTALL_FLAG_NO_HISTORY; |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 624 | if (!fu_engine_install_blob (priv->engine, device, blob_fw, priv->flags, error)) |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 625 | return FALSE; |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 626 | if (priv->cleanup_blob) { |
| 627 | g_autoptr(FuDevice) device_new = NULL; |
| 628 | g_autoptr(GError) error_local = NULL; |
| 629 | |
| 630 | /* get the possibly new device from the old ID */ |
| 631 | device_new = fu_engine_get_device (priv->engine, |
| 632 | fu_device_get_id (device), |
| 633 | &error_local); |
| 634 | if (device_new == NULL) { |
| 635 | g_debug ("failed to find new device: %s", |
| 636 | error_local->message); |
| 637 | } else { |
Mario Limonciello | bb3fa5e | 2019-02-07 21:13:02 -0600 | [diff] [blame] | 638 | g_autoptr(GPtrArray) devices_new = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 639 | g_ptr_array_add (devices_new, g_steal_pointer (&device_new)); |
| 640 | if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) { |
| 641 | g_prefix_error (error, "failed to cleanup composite action: "); |
| 642 | return FALSE; |
| 643 | } |
| 644 | } |
| 645 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 646 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 647 | fu_util_display_current_message (priv); |
| 648 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 649 | /* success */ |
| 650 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 653 | static gint |
| 654 | fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b) |
| 655 | { |
| 656 | FuInstallTask *task1 = *((FuInstallTask **) a); |
| 657 | FuInstallTask *task2 = *((FuInstallTask **) b); |
| 658 | return fu_install_task_compare (task1, task2); |
| 659 | } |
| 660 | |
| 661 | static gboolean |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 662 | fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error) |
| 663 | { |
| 664 | const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL }, |
| 665 | { "curl", uri, "--output", fn, NULL }, |
| 666 | { NULL } }; |
| 667 | for (guint i = 0; argv[i][0] != NULL; i++) { |
| 668 | g_autoptr(GError) error_local = NULL; |
| 669 | if (!fu_common_find_program_in_path (argv[i][0], &error_local)) { |
| 670 | g_debug ("%s", error_local->message); |
| 671 | continue; |
| 672 | } |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 673 | return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error); |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 674 | } |
| 675 | g_set_error_literal (error, |
| 676 | FWUPD_ERROR, |
| 677 | FWUPD_ERROR_NOT_FOUND, |
| 678 | "no supported out-of-process downloaders found"); |
| 679 | return FALSE; |
| 680 | } |
| 681 | |
| 682 | static gchar * |
| 683 | fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error) |
| 684 | { |
| 685 | g_autofree gchar *filename = NULL; |
| 686 | g_autoptr(SoupURI) uri = NULL; |
| 687 | |
| 688 | /* a local file */ |
| 689 | uri = soup_uri_new (perhapsfn); |
| 690 | if (uri == NULL) |
| 691 | return g_strdup (perhapsfn); |
| 692 | |
| 693 | /* download the firmware to a cachedir */ |
| 694 | filename = fu_util_get_user_cache_path (perhapsfn); |
| 695 | if (!fu_common_mkdir_parent (filename, error)) |
| 696 | return NULL; |
| 697 | if (!fu_util_download_out_of_process (perhapsfn, filename, error)) |
| 698 | return NULL; |
| 699 | return g_steal_pointer (&filename); |
| 700 | } |
| 701 | |
| 702 | static gboolean |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 703 | fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error) |
| 704 | { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 705 | g_autofree gchar *filename = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 706 | g_autoptr(GBytes) blob_cab = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 707 | g_autoptr(GPtrArray) components = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 708 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 709 | g_autoptr(GPtrArray) errors = NULL; |
| 710 | g_autoptr(GPtrArray) install_tasks = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 711 | g_autoptr(XbSilo) silo = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 712 | |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 713 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 714 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 715 | return FALSE; |
| 716 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 717 | /* handle both forms */ |
| 718 | if (g_strv_length (values) == 1) { |
| 719 | devices_possible = fu_engine_get_devices (priv->engine, error); |
| 720 | if (devices_possible == NULL) |
| 721 | return FALSE; |
| 722 | } else if (g_strv_length (values) == 2) { |
| 723 | FuDevice *device = fu_engine_get_device (priv->engine, |
| 724 | values[1], |
| 725 | error); |
| 726 | if (device == NULL) |
| 727 | return FALSE; |
| 728 | devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 729 | g_ptr_array_add (devices_possible, device); |
| 730 | } else { |
| 731 | g_set_error_literal (error, |
| 732 | FWUPD_ERROR, |
| 733 | FWUPD_ERROR_INVALID_ARGS, |
| 734 | "Invalid arguments"); |
| 735 | return FALSE; |
| 736 | } |
| 737 | |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 738 | /* download if required */ |
| 739 | filename = fu_util_download_if_required (priv, values[0], error); |
| 740 | if (filename == NULL) |
| 741 | return FALSE; |
| 742 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 743 | /* parse silo */ |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 744 | blob_cab = fu_common_get_contents_bytes (filename, error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 745 | if (blob_cab == NULL) { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 746 | fu_util_maybe_prefix_sandbox_error (filename, error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 747 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 748 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 749 | silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error); |
| 750 | if (silo == NULL) |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 751 | return FALSE; |
Mario Limonciello | 51ddf18 | 2019-01-26 00:31:58 -0600 | [diff] [blame] | 752 | components = xb_silo_query (silo, "components/component", 0, error); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 753 | if (components == NULL) |
| 754 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 755 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 756 | /* for each component in the silo */ |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 757 | errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free); |
| 758 | install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 759 | for (guint i = 0; i < components->len; i++) { |
| 760 | XbNode *component = g_ptr_array_index (components, i); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 761 | |
| 762 | /* do any devices pass the requirements */ |
| 763 | for (guint j = 0; j < devices_possible->len; j++) { |
| 764 | FuDevice *device = g_ptr_array_index (devices_possible, j); |
| 765 | g_autoptr(FuInstallTask) task = NULL; |
| 766 | g_autoptr(GError) error_local = NULL; |
| 767 | |
| 768 | /* is this component valid for the device */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 769 | task = fu_install_task_new (device, component); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 770 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 771 | task, priv->flags, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 772 | &error_local)) { |
| 773 | g_debug ("requirement on %s:%s failed: %s", |
| 774 | fu_device_get_id (device), |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 775 | xb_node_query_text (component, "id", NULL), |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 776 | error_local->message); |
| 777 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 778 | continue; |
| 779 | } |
| 780 | |
Mario Limonciello | 7a3df4b | 2019-01-31 10:27:22 -0600 | [diff] [blame] | 781 | /* if component should have an update message from CAB */ |
| 782 | fu_device_incorporate_from_component (device, component); |
| 783 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 784 | /* success */ |
| 785 | g_ptr_array_add (install_tasks, g_steal_pointer (&task)); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /* order the install tasks by the device priority */ |
| 790 | g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb); |
| 791 | |
| 792 | /* nothing suitable */ |
| 793 | if (install_tasks->len == 0) { |
| 794 | GError *error_tmp = fu_common_error_array_get_best (errors); |
| 795 | g_propagate_error (error, error_tmp); |
| 796 | return FALSE; |
| 797 | } |
| 798 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 799 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 800 | g_signal_connect (priv->engine, "device-changed", |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 801 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 802 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 803 | /* install all the tasks */ |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 804 | if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error)) |
| 805 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 806 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 807 | fu_util_display_current_message (priv); |
| 808 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 809 | /* we don't want to ask anything */ |
| 810 | if (priv->no_reboot_check) { |
| 811 | g_debug ("skipping reboot check"); |
| 812 | return TRUE; |
| 813 | } |
| 814 | |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 815 | /* save the device state for other applications to see */ |
| 816 | if (!fu_util_save_current_state (priv, error)) |
| 817 | return FALSE; |
| 818 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 819 | /* success */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 820 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 823 | static gboolean |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 824 | fu_util_install_release (FuUtilPrivate *priv, FwupdRelease *rel, GError **error) |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 825 | { |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 826 | FwupdRemote *remote; |
| 827 | const gchar *remote_id; |
| 828 | const gchar *uri_tmp; |
| 829 | g_auto(GStrv) argv = NULL; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 830 | |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 831 | uri_tmp = fwupd_release_get_uri (rel); |
| 832 | if (uri_tmp == NULL) { |
| 833 | g_set_error_literal (error, |
| 834 | FWUPD_ERROR, |
| 835 | FWUPD_ERROR_INVALID_FILE, |
| 836 | "release missing URI"); |
| 837 | return FALSE; |
| 838 | } |
| 839 | remote_id = fwupd_release_get_remote_id (rel); |
| 840 | if (remote_id == NULL) { |
| 841 | g_set_error (error, |
| 842 | FWUPD_ERROR, |
| 843 | FWUPD_ERROR_INVALID_FILE, |
| 844 | "failed to find remote for %s", |
| 845 | uri_tmp); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 846 | return FALSE; |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | remote = fu_engine_get_remote_by_id (priv->engine, |
| 850 | remote_id, |
| 851 | error); |
| 852 | if (remote == NULL) |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 853 | return FALSE; |
| 854 | |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 855 | argv = g_new0 (gchar *, 2); |
| 856 | /* local remotes have the firmware already */ |
| 857 | if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) { |
| 858 | const gchar *fn_cache = fwupd_remote_get_filename_cache (remote); |
| 859 | g_autofree gchar *path = g_path_get_dirname (fn_cache); |
| 860 | argv[0] = g_build_filename (path, uri_tmp, NULL); |
| 861 | } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) { |
| 862 | argv[0] = g_strdup (uri_tmp + 7); |
| 863 | /* web remote, fu_util_install will download file */ |
| 864 | } else { |
| 865 | argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error); |
| 866 | } |
| 867 | return fu_util_install (priv, argv, error); |
| 868 | } |
| 869 | |
| 870 | static gboolean |
| 871 | fu_util_update_all (FuUtilPrivate *priv, GError **error) |
| 872 | { |
| 873 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 874 | |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 875 | devices = fu_engine_get_devices (priv->engine, error); |
Mario Limonciello | 387bda4 | 2019-02-07 14:20:22 +0000 | [diff] [blame] | 876 | if (devices == NULL) |
| 877 | return FALSE; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 878 | for (guint i = 0; i < devices->len; i++) { |
| 879 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 880 | FwupdRelease *rel; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 881 | const gchar *device_id; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 882 | g_autoptr(GPtrArray) rels = NULL; |
| 883 | g_autoptr(GError) error_local = NULL; |
| 884 | |
| 885 | if (!fu_util_is_interesting_device (dev)) |
| 886 | continue; |
| 887 | /* only show stuff that has metadata available */ |
| 888 | if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) |
| 889 | continue; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 890 | if (!fu_util_filter_device (priv, dev)) |
| 891 | continue; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 892 | |
| 893 | device_id = fu_device_get_id (dev); |
| 894 | rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local); |
| 895 | if (rels == NULL) { |
| 896 | g_printerr ("%s\n", error_local->message); |
| 897 | continue; |
| 898 | } |
| 899 | |
| 900 | rel = g_ptr_array_index (rels, 0); |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 901 | if (!fu_util_install_release (priv, rel, &error_local)) { |
| 902 | g_printerr ("%s\n", error_local->message); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 903 | continue; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 904 | } |
Mario Limonciello | fd73485 | 2019-08-01 16:41:42 -0500 | [diff] [blame] | 905 | fu_util_display_current_message (priv); |
| 906 | } |
| 907 | return TRUE; |
| 908 | } |
| 909 | |
| 910 | static gboolean |
| 911 | fu_util_update_by_id (FuUtilPrivate *priv, const gchar *device_id, GError **error) |
| 912 | { |
| 913 | FwupdRelease *rel; |
| 914 | g_autoptr(FuDevice) dev = NULL; |
| 915 | g_autoptr(GPtrArray) rels = NULL; |
| 916 | |
| 917 | /* do not allow a partial device-id */ |
| 918 | dev = fu_engine_get_device (priv->engine, device_id, error); |
| 919 | if (dev == NULL) |
| 920 | return FALSE; |
| 921 | |
| 922 | /* get the releases for this device and filter for validity */ |
| 923 | rels = fu_engine_get_upgrades (priv->engine, device_id, error); |
| 924 | if (rels == NULL) |
| 925 | return FALSE; |
| 926 | rel = g_ptr_array_index (rels, 0); |
| 927 | if (!fu_util_install_release (priv, rel, error)) |
| 928 | return FALSE; |
| 929 | fu_util_display_current_message (priv); |
| 930 | |
| 931 | return TRUE; |
| 932 | } |
| 933 | |
| 934 | static gboolean |
| 935 | fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error) |
| 936 | { |
| 937 | if (g_strv_length (values) > 1) { |
| 938 | g_set_error_literal (error, |
| 939 | FWUPD_ERROR, |
| 940 | FWUPD_ERROR_INVALID_ARGS, |
| 941 | "Invalid arguments"); |
| 942 | return FALSE; |
| 943 | } |
| 944 | |
| 945 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 946 | return FALSE; |
| 947 | |
| 948 | priv->current_operation = FU_UTIL_OPERATION_UPDATE; |
| 949 | g_signal_connect (priv->engine, "device-changed", |
| 950 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
| 951 | |
| 952 | if (g_strv_length (values) == 1) { |
| 953 | if (!fu_util_update_by_id (priv, values[0], error)) |
| 954 | return FALSE; |
| 955 | } else { |
| 956 | if (!fu_util_update_all (priv, error)) |
| 957 | return FALSE; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 958 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 959 | |
| 960 | /* we don't want to ask anything */ |
| 961 | if (priv->no_reboot_check) { |
| 962 | g_debug ("skipping reboot check"); |
| 963 | return TRUE; |
| 964 | } |
| 965 | |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 966 | /* save the device state for other applications to see */ |
| 967 | if (!fu_util_save_current_state (priv, error)) |
| 968 | return FALSE; |
| 969 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 970 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | static gboolean |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 974 | fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 975 | { |
| 976 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 977 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 978 | |
| 979 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 980 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 981 | return FALSE; |
| 982 | |
| 983 | /* invalid args */ |
| 984 | if (g_strv_length (values) == 0) { |
| 985 | g_set_error_literal (error, |
| 986 | FWUPD_ERROR, |
| 987 | FWUPD_ERROR_INVALID_ARGS, |
| 988 | "Invalid arguments"); |
| 989 | return FALSE; |
| 990 | } |
| 991 | |
| 992 | /* get device */ |
| 993 | if (g_strv_length (values) >= 1) { |
| 994 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 995 | if (device == NULL) |
| 996 | return FALSE; |
| 997 | } else { |
| 998 | device = fu_util_prompt_for_device (priv, error); |
| 999 | if (device == NULL) |
| 1000 | return FALSE; |
| 1001 | } |
| 1002 | |
| 1003 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1004 | locker = fu_device_locker_new (device, error); |
| 1005 | if (locker == NULL) |
| 1006 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1007 | return fu_device_detach (device, error); |
| 1008 | } |
| 1009 | |
| 1010 | static gboolean |
| 1011 | fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1012 | { |
| 1013 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1014 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1015 | |
| 1016 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 1017 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1018 | return FALSE; |
| 1019 | |
| 1020 | /* invalid args */ |
| 1021 | if (g_strv_length (values) == 0) { |
| 1022 | g_set_error_literal (error, |
| 1023 | FWUPD_ERROR, |
| 1024 | FWUPD_ERROR_INVALID_ARGS, |
| 1025 | "Invalid arguments"); |
| 1026 | return FALSE; |
| 1027 | } |
| 1028 | |
| 1029 | /* get device */ |
| 1030 | if (g_strv_length (values) >= 1) { |
| 1031 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 1032 | if (device == NULL) |
| 1033 | return FALSE; |
| 1034 | } else { |
| 1035 | device = fu_util_prompt_for_device (priv, error); |
| 1036 | if (device == NULL) |
| 1037 | return FALSE; |
| 1038 | } |
| 1039 | |
| 1040 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 1041 | locker = fu_device_locker_new (device, error); |
| 1042 | if (locker == NULL) |
| 1043 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1044 | return fu_device_attach (device, error); |
| 1045 | } |
| 1046 | |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1047 | static gboolean |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1048 | fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1049 | { |
| 1050 | gboolean has_pending = FALSE; |
| 1051 | g_autoptr(FuHistory) history = fu_history_new (); |
| 1052 | g_autoptr(GPtrArray) devices = NULL; |
| 1053 | |
| 1054 | /* check the history database before starting the daemon */ |
| 1055 | if (g_strv_length (values) == 0) { |
| 1056 | devices = fu_history_get_devices (history, error); |
| 1057 | if (devices == NULL) |
| 1058 | return FALSE; |
| 1059 | } else if (g_strv_length (values) == 1) { |
| 1060 | FuDevice *device; |
| 1061 | device = fu_history_get_device_by_id (history, values[0], error); |
| 1062 | if (device == NULL) |
| 1063 | return FALSE; |
| 1064 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 1065 | g_ptr_array_add (devices, device); |
| 1066 | } else { |
| 1067 | g_set_error_literal (error, |
| 1068 | FWUPD_ERROR, |
| 1069 | FWUPD_ERROR_INVALID_ARGS, |
| 1070 | "Invalid arguments"); |
| 1071 | return FALSE; |
| 1072 | } |
| 1073 | |
| 1074 | /* nothing to do */ |
| 1075 | for (guint i = 0; i < devices->len; i++) { |
| 1076 | FuDevice *dev = g_ptr_array_index (devices, i); |
| 1077 | if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) { |
| 1078 | fu_engine_add_plugin_filter (priv->engine, |
| 1079 | fu_device_get_plugin (dev)); |
| 1080 | has_pending = TRUE; |
| 1081 | } |
| 1082 | } |
| 1083 | if (!has_pending) { |
| 1084 | g_set_error_literal (error, |
| 1085 | FWUPD_ERROR, |
| 1086 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1087 | "No firmware to activate"); |
| 1088 | return FALSE; |
| 1089 | |
| 1090 | } |
| 1091 | |
| 1092 | /* load engine */ |
Richard Hughes | 88dc0f4 | 2019-03-07 11:58:11 +0000 | [diff] [blame] | 1093 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_READONLY_FS, error)) |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1094 | return FALSE; |
| 1095 | |
| 1096 | /* activate anything with _NEEDS_ACTIVATION */ |
| 1097 | for (guint i = 0; i < devices->len; i++) { |
| 1098 | FuDevice *device = g_ptr_array_index (devices, i); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1099 | if (!fu_util_filter_device (priv, FWUPD_DEVICE (device))) |
| 1100 | continue; |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1101 | if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) |
| 1102 | continue; |
| 1103 | /* TRANSLATORS: shown when shutting down to switch to the new version */ |
| 1104 | g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device)); |
| 1105 | if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error)) |
| 1106 | return FALSE; |
| 1107 | } |
| 1108 | |
| 1109 | return TRUE; |
| 1110 | } |
| 1111 | |
| 1112 | static gboolean |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1113 | fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1114 | { |
| 1115 | g_autoptr(FuSmbios) smbios = fu_smbios_new (); |
| 1116 | g_autoptr(FuHwids) hwids = fu_hwids_new (); |
| 1117 | const gchar *hwid_keys[] = { |
| 1118 | FU_HWIDS_KEY_BIOS_VENDOR, |
| 1119 | FU_HWIDS_KEY_BIOS_VERSION, |
| 1120 | FU_HWIDS_KEY_BIOS_MAJOR_RELEASE, |
| 1121 | FU_HWIDS_KEY_BIOS_MINOR_RELEASE, |
| 1122 | FU_HWIDS_KEY_MANUFACTURER, |
| 1123 | FU_HWIDS_KEY_FAMILY, |
| 1124 | FU_HWIDS_KEY_PRODUCT_NAME, |
| 1125 | FU_HWIDS_KEY_PRODUCT_SKU, |
| 1126 | FU_HWIDS_KEY_ENCLOSURE_KIND, |
| 1127 | FU_HWIDS_KEY_BASEBOARD_MANUFACTURER, |
| 1128 | FU_HWIDS_KEY_BASEBOARD_PRODUCT, |
| 1129 | NULL }; |
| 1130 | |
| 1131 | /* read DMI data */ |
| 1132 | if (g_strv_length (values) == 0) { |
| 1133 | if (!fu_smbios_setup (smbios, error)) |
| 1134 | return FALSE; |
| 1135 | } else if (g_strv_length (values) == 1) { |
| 1136 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 1137 | return FALSE; |
| 1138 | } else { |
| 1139 | g_set_error_literal (error, |
| 1140 | FWUPD_ERROR, |
| 1141 | FWUPD_ERROR_INVALID_ARGS, |
| 1142 | "Invalid arguments"); |
| 1143 | return FALSE; |
| 1144 | } |
| 1145 | if (!fu_hwids_setup (hwids, smbios, error)) |
| 1146 | return FALSE; |
| 1147 | |
| 1148 | /* show debug output */ |
| 1149 | g_print ("Computer Information\n"); |
| 1150 | g_print ("--------------------\n"); |
| 1151 | for (guint i = 0; hwid_keys[i] != NULL; i++) { |
| 1152 | const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]); |
| 1153 | if (tmp == NULL) |
| 1154 | continue; |
| 1155 | if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 || |
| 1156 | g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) { |
| 1157 | guint64 val = g_ascii_strtoull (tmp, NULL, 16); |
| 1158 | g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val); |
| 1159 | } else { |
| 1160 | g_print ("%s: %s\n", hwid_keys[i], tmp); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /* show GUIDs */ |
| 1165 | g_print ("\nHardware IDs\n"); |
| 1166 | g_print ("------------\n"); |
| 1167 | for (guint i = 0; i < 15; i++) { |
| 1168 | const gchar *keys = NULL; |
| 1169 | g_autofree gchar *guid = NULL; |
| 1170 | g_autofree gchar *key = NULL; |
| 1171 | g_autofree gchar *keys_str = NULL; |
| 1172 | g_auto(GStrv) keysv = NULL; |
| 1173 | g_autoptr(GError) error_local = NULL; |
| 1174 | |
| 1175 | /* get the GUID */ |
| 1176 | key = g_strdup_printf ("HardwareID-%u", i); |
| 1177 | keys = fu_hwids_get_replace_keys (hwids, key); |
| 1178 | guid = fu_hwids_get_guid (hwids, key, &error_local); |
| 1179 | if (guid == NULL) { |
| 1180 | g_print ("%s\n", error_local->message); |
| 1181 | continue; |
| 1182 | } |
| 1183 | |
| 1184 | /* show what makes up the GUID */ |
| 1185 | keysv = g_strsplit (keys, "&", -1); |
| 1186 | keys_str = g_strjoinv (" + ", keysv); |
| 1187 | g_print ("{%s} <- %s\n", guid, keys_str); |
| 1188 | } |
| 1189 | |
| 1190 | return TRUE; |
| 1191 | } |
| 1192 | |
Mario Limonciello | f6d01b1 | 2018-10-18 12:57:10 -0500 | [diff] [blame] | 1193 | static gboolean |
| 1194 | fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1195 | { |
| 1196 | const gchar *script_fn = "startup.sh"; |
| 1197 | const gchar *output_fn = "firmware.bin"; |
| 1198 | g_autoptr(GBytes) archive_blob = NULL; |
| 1199 | g_autoptr(GBytes) firmware_blob = NULL; |
| 1200 | if (g_strv_length (values) < 2) { |
| 1201 | g_set_error_literal (error, |
| 1202 | FWUPD_ERROR, |
| 1203 | FWUPD_ERROR_INVALID_ARGS, |
| 1204 | "Invalid arguments"); |
| 1205 | return FALSE; |
| 1206 | } |
| 1207 | archive_blob = fu_common_get_contents_bytes (values[0], error); |
| 1208 | if (archive_blob == NULL) |
| 1209 | return FALSE; |
| 1210 | if (g_strv_length (values) > 2) |
| 1211 | script_fn = values[2]; |
| 1212 | if (g_strv_length (values) > 3) |
| 1213 | output_fn = values[3]; |
| 1214 | firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error); |
| 1215 | if (firmware_blob == NULL) |
| 1216 | return FALSE; |
| 1217 | return fu_common_set_contents_bytes (values[1], firmware_blob, error); |
| 1218 | } |
| 1219 | |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 1220 | static gboolean |
| 1221 | fu_util_self_sign (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1222 | { |
| 1223 | g_autofree gchar *sig = NULL; |
| 1224 | |
| 1225 | /* check args */ |
| 1226 | if (g_strv_length (values) != 1) { |
| 1227 | g_set_error_literal (error, |
| 1228 | FWUPD_ERROR, |
| 1229 | FWUPD_ERROR_INVALID_ARGS, |
| 1230 | "Invalid arguments: value expected"); |
| 1231 | return FALSE; |
| 1232 | } |
| 1233 | |
| 1234 | /* start engine */ |
| 1235 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 1236 | return FALSE; |
| 1237 | sig = fu_engine_self_sign (priv->engine, values[0], |
| 1238 | FU_KEYRING_SIGN_FLAG_ADD_TIMESTAMP | |
| 1239 | FU_KEYRING_SIGN_FLAG_ADD_CERT, error); |
| 1240 | if (sig == NULL) |
| 1241 | return FALSE; |
| 1242 | g_print ("%s\n", sig); |
| 1243 | return TRUE; |
| 1244 | } |
| 1245 | |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1246 | static void |
| 1247 | fu_util_device_added_cb (FwupdClient *client, |
| 1248 | FwupdDevice *device, |
| 1249 | gpointer user_data) |
| 1250 | { |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1251 | g_autofree gchar *tmp = fu_util_device_to_string (device, 0); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1252 | /* TRANSLATORS: this is when a device is hotplugged */ |
| 1253 | g_print ("%s\n%s", _("Device added:"), tmp); |
| 1254 | } |
| 1255 | |
| 1256 | static void |
| 1257 | fu_util_device_removed_cb (FwupdClient *client, |
| 1258 | FwupdDevice *device, |
| 1259 | gpointer user_data) |
| 1260 | { |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1261 | g_autofree gchar *tmp = fu_util_device_to_string (device, 0); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1262 | /* TRANSLATORS: this is when a device is hotplugged */ |
| 1263 | g_print ("%s\n%s", _("Device removed:"), tmp); |
| 1264 | } |
| 1265 | |
| 1266 | static void |
| 1267 | fu_util_device_changed_cb (FwupdClient *client, |
| 1268 | FwupdDevice *device, |
| 1269 | gpointer user_data) |
| 1270 | { |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1271 | g_autofree gchar *tmp = fu_util_device_to_string (device, 0); |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1272 | /* TRANSLATORS: this is when a device has been updated */ |
| 1273 | g_print ("%s\n%s", _("Device changed:"), tmp); |
| 1274 | } |
| 1275 | |
| 1276 | static void |
| 1277 | fu_util_changed_cb (FwupdClient *client, gpointer user_data) |
| 1278 | { |
| 1279 | /* TRANSLATORS: this is when the daemon state changes */ |
| 1280 | g_print ("%s\n", _("Changed")); |
| 1281 | } |
| 1282 | |
| 1283 | static gboolean |
| 1284 | fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1285 | { |
| 1286 | g_autoptr(FwupdClient) client = fwupd_client_new (); |
| 1287 | |
| 1288 | /* get all the devices */ |
| 1289 | if (!fwupd_client_connect (client, priv->cancellable, error)) |
| 1290 | return FALSE; |
| 1291 | |
| 1292 | /* watch for any hotplugged device */ |
| 1293 | g_signal_connect (client, "changed", |
| 1294 | G_CALLBACK (fu_util_changed_cb), priv); |
| 1295 | g_signal_connect (client, "device-added", |
| 1296 | G_CALLBACK (fu_util_device_added_cb), priv); |
| 1297 | g_signal_connect (client, "device-removed", |
| 1298 | G_CALLBACK (fu_util_device_removed_cb), priv); |
| 1299 | g_signal_connect (client, "device-changed", |
| 1300 | G_CALLBACK (fu_util_device_changed_cb), priv); |
| 1301 | g_signal_connect (priv->cancellable, "cancelled", |
| 1302 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 1303 | g_main_loop_run (priv->loop); |
| 1304 | return TRUE; |
| 1305 | } |
| 1306 | |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 1307 | static gboolean |
| 1308 | fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1309 | { |
| 1310 | g_autofree gchar *str = NULL; |
| 1311 | g_autoptr(FuDevice) dev = NULL; |
| 1312 | |
| 1313 | /* load engine */ |
| 1314 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 1315 | return FALSE; |
| 1316 | |
| 1317 | /* get device */ |
| 1318 | if (g_strv_length (values) == 1) { |
| 1319 | dev = fu_engine_get_device (priv->engine, values[1], error); |
| 1320 | if (dev == NULL) |
| 1321 | return FALSE; |
| 1322 | } else { |
| 1323 | dev = fu_util_prompt_for_device (priv, error); |
| 1324 | if (dev == NULL) |
| 1325 | return FALSE; |
| 1326 | } |
| 1327 | |
| 1328 | /* add checksums */ |
| 1329 | if (!fu_engine_verify_update (priv->engine, fu_device_get_id (dev), error)) |
| 1330 | return FALSE; |
| 1331 | |
| 1332 | /* show checksums */ |
| 1333 | str = fu_device_to_string (dev); |
| 1334 | g_print ("%s\n", str); |
| 1335 | return TRUE; |
| 1336 | } |
| 1337 | |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1338 | static gboolean |
| 1339 | fu_util_get_history (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1340 | { |
| 1341 | g_autoptr(GPtrArray) devices = NULL; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 1342 | g_autoptr(GNode) root = g_node_new (NULL); |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1343 | |
| 1344 | /* load engine */ |
| 1345 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
| 1346 | return FALSE; |
| 1347 | |
| 1348 | /* get all devices from the history database */ |
| 1349 | devices = fu_engine_get_history (priv->engine, error); |
| 1350 | if (devices == NULL) |
| 1351 | return FALSE; |
| 1352 | |
| 1353 | /* show each device */ |
| 1354 | for (guint i = 0; i < devices->len; i++) { |
| 1355 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1356 | if (!fu_util_filter_device (priv, dev)) |
| 1357 | continue; |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 1358 | g_node_append_data (root, dev); |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1359 | } |
Mario Limonciello | 4250d9d | 2019-08-29 09:53:44 -0500 | [diff] [blame] | 1360 | fu_util_print_tree (root, priv); |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1361 | |
| 1362 | return TRUE; |
| 1363 | } |
| 1364 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1365 | int |
| 1366 | main (int argc, char *argv[]) |
| 1367 | { |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1368 | gboolean allow_older = FALSE; |
| 1369 | gboolean allow_reinstall = FALSE; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1370 | gboolean force = FALSE; |
| 1371 | gboolean ret; |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1372 | gboolean version = FALSE; |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 1373 | gboolean interactive = isatty (fileno (stdout)) != 0; |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1374 | g_auto(GStrv) plugin_glob = NULL; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1375 | g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1); |
| 1376 | g_autoptr(GError) error = NULL; |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1377 | g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new (); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1378 | g_autofree gchar *cmd_descriptions = NULL; |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1379 | g_autofree gchar *filter = NULL; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1380 | const GOptionEntry options[] = { |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1381 | { "version", '\0', 0, G_OPTION_ARG_NONE, &version, |
| 1382 | /* TRANSLATORS: command line option */ |
| 1383 | _("Show client and daemon versions"), NULL }, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1384 | { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall, |
| 1385 | /* TRANSLATORS: command line option */ |
| 1386 | _("Allow re-installing existing firmware versions"), NULL }, |
| 1387 | { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older, |
| 1388 | /* TRANSLATORS: command line option */ |
| 1389 | _("Allow downgrading firmware versions"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1390 | { "force", '\0', 0, G_OPTION_ARG_NONE, &force, |
| 1391 | /* TRANSLATORS: command line option */ |
| 1392 | _("Override plugin warning"), NULL }, |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1393 | { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check, |
| 1394 | /* TRANSLATORS: command line option */ |
| 1395 | _("Do not check for reboot after update"), NULL }, |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 1396 | { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices, |
| 1397 | /* TRANSLATORS: command line option */ |
| 1398 | _("Show devices that are not updatable"), NULL }, |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1399 | { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob, |
| 1400 | /* TRANSLATORS: command line option */ |
| 1401 | _("Manually whitelist specific plugins"), NULL }, |
Mario Limonciello | 8402cea | 2019-02-07 20:25:31 -0600 | [diff] [blame] | 1402 | { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob, |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 1403 | /* TRANSLATORS: command line option */ |
| 1404 | _("Run the plugin composite prepare routine when using install-blob"), NULL }, |
Mario Limonciello | 8402cea | 2019-02-07 20:25:31 -0600 | [diff] [blame] | 1405 | { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob, |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 1406 | /* TRANSLATORS: command line option */ |
| 1407 | _("Run the plugin composite cleanup routine when using install-blob"), NULL }, |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 1408 | { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state, |
| 1409 | /* TRANSLATORS: command line option */ |
| 1410 | _("Save device state into a JSON file between executions"), NULL }, |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1411 | { "filter", '\0', 0, G_OPTION_ARG_STRING, &filter, |
| 1412 | /* TRANSLATORS: command line option */ |
| 1413 | _("Filter with a set of device flags using a ~ prefix to " |
| 1414 | "exclude, e.g. 'internal,~needs-reboot'"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1415 | { NULL} |
| 1416 | }; |
| 1417 | |
| 1418 | setlocale (LC_ALL, ""); |
| 1419 | |
| 1420 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 1421 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 1422 | textdomain (GETTEXT_PACKAGE); |
| 1423 | |
| 1424 | /* ensure root user */ |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 1425 | if (interactive && (getuid () != 0 || geteuid () != 0)) |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1426 | /* TRANSLATORS: we're poking around as a power user */ |
Mario Limonciello | b900c09 | 2018-05-22 14:22:21 -0500 | [diff] [blame] | 1427 | g_printerr ("%s\n", _("This program may only work correctly as root")); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1428 | |
| 1429 | /* create helper object */ |
| 1430 | priv->loop = g_main_loop_new (NULL, FALSE); |
| 1431 | priv->progressbar = fu_progressbar_new (); |
| 1432 | |
| 1433 | /* add commands */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1434 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | f6d01b1 | 2018-10-18 12:57:10 -0500 | [diff] [blame] | 1435 | "build-firmware", |
| 1436 | "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]", |
| 1437 | /* TRANSLATORS: command description */ |
| 1438 | _("Build firmware using a sandbox"), |
| 1439 | fu_util_firmware_builder); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1440 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1441 | "smbios-dump", |
| 1442 | "FILE", |
| 1443 | /* TRANSLATORS: command description */ |
| 1444 | _("Dump SMBIOS data from a file"), |
| 1445 | fu_util_smbios_dump); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1446 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 1447 | "get-plugins", |
| 1448 | NULL, |
| 1449 | /* TRANSLATORS: command description */ |
| 1450 | _("Get all enabled plugins registered with the system"), |
| 1451 | fu_util_get_plugins); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1452 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 1453 | "get-details", |
| 1454 | NULL, |
| 1455 | /* TRANSLATORS: command description */ |
| 1456 | _("Gets details about a firmware file"), |
| 1457 | fu_util_get_details); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1458 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | fe59394 | 2019-04-03 13:48:52 -0500 | [diff] [blame] | 1459 | "get-history", |
| 1460 | NULL, |
| 1461 | /* TRANSLATORS: command description */ |
| 1462 | _("Show history of firmware updates"), |
| 1463 | fu_util_get_history); |
| 1464 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | dfff18e | 2019-08-29 11:51:41 -0500 | [diff] [blame] | 1465 | "get-updates,get-upgrades", |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 1466 | NULL, |
| 1467 | /* TRANSLATORS: command description */ |
| 1468 | _("Gets the list of updates for connected hardware"), |
| 1469 | fu_util_get_updates); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1470 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 1a9127d | 2019-08-27 11:32:51 +0100 | [diff] [blame] | 1471 | "get-devices,get-topology", |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1472 | NULL, |
| 1473 | /* TRANSLATORS: command description */ |
| 1474 | _("Get all devices that support firmware updates"), |
| 1475 | fu_util_get_devices); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1476 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1477 | "get-device-flags", |
| 1478 | NULL, |
| 1479 | /* TRANSLATORS: command description */ |
| 1480 | _("Get all device flags supported by fwupd"), |
| 1481 | fu_util_get_device_flags); |
| 1482 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1483 | "watch", |
| 1484 | NULL, |
| 1485 | /* TRANSLATORS: command description */ |
Piotr DrÄ…g | 472fa59 | 2018-06-06 14:53:48 +0200 | [diff] [blame] | 1486 | _("Watch for hardware changes"), |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1487 | fu_util_watch); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1488 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1489 | "install-blob", |
| 1490 | "FILENAME DEVICE-ID", |
| 1491 | /* TRANSLATORS: command description */ |
| 1492 | _("Install a firmware blob on a device"), |
| 1493 | fu_util_install_blob); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1494 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1495 | "install", |
| 1496 | "FILE [ID]", |
| 1497 | /* TRANSLATORS: command description */ |
| 1498 | _("Install a firmware file on this hardware"), |
| 1499 | fu_util_install); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1500 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1501 | "attach", |
| 1502 | "DEVICE-ID", |
| 1503 | /* TRANSLATORS: command description */ |
| 1504 | _("Attach to firmware mode"), |
| 1505 | fu_util_attach); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1506 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1507 | "detach", |
| 1508 | "DEVICE-ID", |
| 1509 | /* TRANSLATORS: command description */ |
| 1510 | _("Detach to bootloader mode"), |
| 1511 | fu_util_detach); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1512 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1513 | "activate", |
| 1514 | "[DEVICE-ID]", |
| 1515 | /* TRANSLATORS: command description */ |
| 1516 | _("Activate pending devices"), |
| 1517 | fu_util_activate); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1518 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1519 | "hwids", |
| 1520 | "[FILE]", |
| 1521 | /* TRANSLATORS: command description */ |
| 1522 | _("Return all the hardware IDs for the machine"), |
| 1523 | fu_util_hwids); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1524 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1525 | "monitor", |
| 1526 | NULL, |
| 1527 | /* TRANSLATORS: command description */ |
| 1528 | _("Monitor the daemon for events"), |
| 1529 | fu_util_monitor); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1530 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | dfff18e | 2019-08-29 11:51:41 -0500 | [diff] [blame] | 1531 | "update,upgrade", |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1532 | NULL, |
| 1533 | /* TRANSLATORS: command description */ |
| 1534 | _("Update all devices that match local metadata"), |
| 1535 | fu_util_update); |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 1536 | fu_util_cmd_array_add (cmd_array, |
| 1537 | "self-sign", |
| 1538 | "TEXT", |
| 1539 | /* TRANSLATORS: command description */ |
Richard Hughes | 12a021d | 2019-03-27 09:23:24 +0000 | [diff] [blame] | 1540 | C_("command-description", |
| 1541 | "Sign data using the client certificate"), |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 1542 | fu_util_self_sign); |
Richard Hughes | 1568449 | 2019-03-15 16:27:50 +0000 | [diff] [blame] | 1543 | fu_util_cmd_array_add (cmd_array, |
| 1544 | "verify-update", |
| 1545 | "[DEVICE_ID]", |
| 1546 | /* TRANSLATORS: command description */ |
| 1547 | _("Update the stored metadata with current contents"), |
| 1548 | fu_util_verify_update); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1549 | |
| 1550 | /* do stuff on ctrl+c */ |
| 1551 | priv->cancellable = g_cancellable_new (); |
| 1552 | g_unix_signal_add_full (G_PRIORITY_DEFAULT, |
| 1553 | SIGINT, fu_util_sigint_cb, |
| 1554 | priv, NULL); |
| 1555 | g_signal_connect (priv->cancellable, "cancelled", |
| 1556 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 1557 | |
| 1558 | /* sort by command name */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1559 | fu_util_cmd_array_sort (cmd_array); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1560 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1561 | /* non-TTY consoles cannot answer questions */ |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 1562 | if (!interactive) { |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1563 | priv->no_reboot_check = TRUE; |
Mario Limonciello | 9b31e6f | 2019-01-31 15:42:19 -0600 | [diff] [blame] | 1564 | fu_progressbar_set_interactive (priv->progressbar, FALSE); |
| 1565 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1566 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1567 | /* get a list of the commands */ |
| 1568 | priv->context = g_option_context_new (NULL); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1569 | cmd_descriptions = fu_util_cmd_array_to_string (cmd_array); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1570 | g_option_context_set_summary (priv->context, cmd_descriptions); |
| 1571 | g_option_context_set_description (priv->context, |
| 1572 | "This tool allows an administrator to use the fwupd plugins " |
| 1573 | "without being installed on the host system."); |
| 1574 | |
| 1575 | /* TRANSLATORS: program name */ |
| 1576 | g_set_application_name (_("Firmware Utility")); |
| 1577 | g_option_context_add_main_entries (priv->context, options, NULL); |
Mario Limonciello | fde4773 | 2018-09-11 12:20:58 -0500 | [diff] [blame] | 1578 | g_option_context_add_group (priv->context, fu_debug_get_option_group ()); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1579 | ret = g_option_context_parse (priv->context, &argc, &argv, &error); |
| 1580 | if (!ret) { |
| 1581 | /* TRANSLATORS: the user didn't read the man page */ |
| 1582 | g_print ("%s: %s\n", _("Failed to parse arguments"), |
| 1583 | error->message); |
| 1584 | return EXIT_FAILURE; |
| 1585 | } |
| 1586 | |
Richard Hughes | 747f570 | 2019-08-06 14:27:26 +0100 | [diff] [blame] | 1587 | /* parse filter flags */ |
| 1588 | if (filter != NULL) { |
| 1589 | if (!fu_util_parse_filter_flags (filter, |
| 1590 | &priv->filter_include, |
| 1591 | &priv->filter_exclude, |
| 1592 | &error)) { |
| 1593 | /* TRANSLATORS: the user didn't read the man page */ |
| 1594 | g_print ("%s: %s\n", _("Failed to parse flags for --filter"), |
| 1595 | error->message); |
| 1596 | return EXIT_FAILURE; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1601 | /* set flags */ |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1602 | if (allow_reinstall) |
| 1603 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
| 1604 | if (allow_older) |
| 1605 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
| 1606 | if (force) |
| 1607 | priv->flags |= FWUPD_INSTALL_FLAG_FORCE; |
| 1608 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1609 | /* load engine */ |
| 1610 | priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES); |
| 1611 | g_signal_connect (priv->engine, "device-added", |
| 1612 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 1613 | priv); |
| 1614 | g_signal_connect (priv->engine, "device-removed", |
| 1615 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 1616 | priv); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1617 | g_signal_connect (priv->engine, "status-changed", |
| 1618 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 1619 | priv); |
| 1620 | g_signal_connect (priv->engine, "percentage-changed", |
| 1621 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 1622 | priv); |
| 1623 | |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1624 | /* just show versions and exit */ |
| 1625 | if (version) { |
| 1626 | g_autofree gchar *version_str = fu_util_get_versions (); |
| 1627 | g_print ("%s\n", version_str); |
| 1628 | return EXIT_SUCCESS; |
| 1629 | } |
| 1630 | |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1631 | /* any plugin whitelist specified */ |
| 1632 | for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++) |
| 1633 | fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]); |
| 1634 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1635 | /* run the specified command */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1636 | ret = fu_util_cmd_array_run (cmd_array, priv, argv[1], (gchar**) &argv[2], &error); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1637 | if (!ret) { |
| 1638 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) { |
| 1639 | g_autofree gchar *tmp = NULL; |
| 1640 | tmp = g_option_context_get_help (priv->context, TRUE, NULL); |
| 1641 | g_print ("%s\n\n%s", error->message, tmp); |
| 1642 | return EXIT_FAILURE; |
| 1643 | } |
| 1644 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) { |
| 1645 | g_print ("%s\n", error->message); |
| 1646 | return EXIT_NOTHING_TO_DO; |
| 1647 | } |
| 1648 | g_print ("%s\n", error->message); |
| 1649 | return EXIT_FAILURE; |
| 1650 | } |
| 1651 | |
| 1652 | /* success */ |
| 1653 | return EXIT_SUCCESS; |
| 1654 | } |