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