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 | |
| 546 | /* same as last time, so ignore */ |
| 547 | if (priv->current_device != NULL && |
| 548 | fwupd_device_compare (priv->current_device, device) == 0) |
| 549 | return; |
| 550 | |
| 551 | /* show message in progressbar */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 552 | if (priv->current_operation == FU_UTIL_OPERATION_UPDATE) { |
| 553 | /* TRANSLATORS: %1 is a device name */ |
| 554 | str = g_strdup_printf (_("Updating %s…"), |
| 555 | fwupd_device_get_name (device)); |
| 556 | fu_progressbar_set_title (priv->progressbar, str); |
| 557 | } else if (priv->current_operation == FU_UTIL_OPERATION_INSTALL) { |
| 558 | /* TRANSLATORS: %1 is a device name */ |
| 559 | str = g_strdup_printf (_("Installing on %s…"), |
| 560 | fwupd_device_get_name (device)); |
| 561 | fu_progressbar_set_title (priv->progressbar, str); |
| 562 | } else { |
| 563 | g_warning ("no FuUtilOperation set"); |
| 564 | } |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 565 | g_set_object (&priv->current_device, device); |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 566 | |
| 567 | if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) |
| 568 | priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN; |
| 569 | else if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT)) |
| 570 | priv->completion_flags |= FWUPD_DEVICE_FLAG_NEEDS_REBOOT; |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 571 | |
| 572 | if (priv->current_message == NULL) { |
| 573 | const gchar *tmp = fwupd_device_get_update_message (priv->current_device); |
| 574 | if (tmp != NULL) |
| 575 | priv->current_message = g_strdup (tmp); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static void |
| 580 | fu_util_display_current_message (FuUtilPrivate *priv) |
| 581 | { |
| 582 | if (priv->current_message == NULL) |
| 583 | return; |
| 584 | g_print ("%s\n", priv->current_message); |
| 585 | g_clear_pointer (&priv->current_message, g_free); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 586 | } |
| 587 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 588 | static gboolean |
| 589 | fu_util_install_blob (FuUtilPrivate *priv, gchar **values, GError **error) |
| 590 | { |
| 591 | g_autoptr(FuDevice) device = NULL; |
| 592 | g_autoptr(GBytes) blob_fw = NULL; |
| 593 | |
| 594 | /* invalid args */ |
| 595 | if (g_strv_length (values) == 0) { |
| 596 | g_set_error_literal (error, |
| 597 | FWUPD_ERROR, |
| 598 | FWUPD_ERROR_INVALID_ARGS, |
| 599 | "Invalid arguments"); |
| 600 | return FALSE; |
| 601 | } |
| 602 | |
| 603 | /* parse blob */ |
| 604 | blob_fw = fu_common_get_contents_bytes (values[0], error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 605 | if (blob_fw == NULL) { |
| 606 | fu_util_maybe_prefix_sandbox_error (values[0], error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 607 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 608 | } |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 609 | |
| 610 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 611 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 612 | return FALSE; |
| 613 | |
| 614 | /* get device */ |
| 615 | if (g_strv_length (values) >= 2) { |
| 616 | device = fu_engine_get_device (priv->engine, values[1], error); |
| 617 | if (device == NULL) |
| 618 | return FALSE; |
| 619 | } else { |
| 620 | device = fu_util_prompt_for_device (priv, error); |
| 621 | if (device == NULL) |
| 622 | return FALSE; |
| 623 | } |
| 624 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 625 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 626 | g_signal_connect (priv->engine, "device-changed", |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 627 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 628 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 629 | /* write bare firmware */ |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 630 | if (priv->prepare_blob) { |
| 631 | g_autoptr(GPtrArray) devices = NULL; |
| 632 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 633 | g_ptr_array_add (devices, g_object_ref (device)); |
| 634 | if (!fu_engine_composite_prepare (priv->engine, devices, error)) { |
| 635 | g_prefix_error (error, "failed to prepare composite action: "); |
| 636 | return FALSE; |
| 637 | } |
| 638 | } |
Richard Hughes | 84af6e7 | 2019-02-01 18:19:41 +0000 | [diff] [blame] | 639 | 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] | 640 | return FALSE; |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 641 | if (priv->cleanup_blob) { |
| 642 | g_autoptr(FuDevice) device_new = NULL; |
| 643 | g_autoptr(GError) error_local = NULL; |
| 644 | |
| 645 | /* get the possibly new device from the old ID */ |
| 646 | device_new = fu_engine_get_device (priv->engine, |
| 647 | fu_device_get_id (device), |
| 648 | &error_local); |
| 649 | if (device_new == NULL) { |
| 650 | g_debug ("failed to find new device: %s", |
| 651 | error_local->message); |
| 652 | } else { |
Mario Limonciello | bb3fa5e | 2019-02-07 21:13:02 -0600 | [diff] [blame] | 653 | 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] | 654 | g_ptr_array_add (devices_new, g_steal_pointer (&device_new)); |
| 655 | if (!fu_engine_composite_cleanup (priv->engine, devices_new, error)) { |
| 656 | g_prefix_error (error, "failed to cleanup composite action: "); |
| 657 | return FALSE; |
| 658 | } |
| 659 | } |
| 660 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 661 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 662 | fu_util_display_current_message (priv); |
| 663 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 664 | /* success */ |
| 665 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 668 | static gint |
| 669 | fu_util_install_task_sort_cb (gconstpointer a, gconstpointer b) |
| 670 | { |
| 671 | FuInstallTask *task1 = *((FuInstallTask **) a); |
| 672 | FuInstallTask *task2 = *((FuInstallTask **) b); |
| 673 | return fu_install_task_compare (task1, task2); |
| 674 | } |
| 675 | |
| 676 | static gboolean |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 677 | fu_util_download_out_of_process (const gchar *uri, const gchar *fn, GError **error) |
| 678 | { |
| 679 | const gchar *argv[][5] = { { "wget", uri, "-o", fn, NULL }, |
| 680 | { "curl", uri, "--output", fn, NULL }, |
| 681 | { NULL } }; |
| 682 | for (guint i = 0; argv[i][0] != NULL; i++) { |
| 683 | g_autoptr(GError) error_local = NULL; |
| 684 | if (!fu_common_find_program_in_path (argv[i][0], &error_local)) { |
| 685 | g_debug ("%s", error_local->message); |
| 686 | continue; |
| 687 | } |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 688 | return fu_common_spawn_sync (argv[i], NULL, NULL, 0, NULL, error); |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 689 | } |
| 690 | g_set_error_literal (error, |
| 691 | FWUPD_ERROR, |
| 692 | FWUPD_ERROR_NOT_FOUND, |
| 693 | "no supported out-of-process downloaders found"); |
| 694 | return FALSE; |
| 695 | } |
| 696 | |
| 697 | static gchar * |
| 698 | fu_util_download_if_required (FuUtilPrivate *priv, const gchar *perhapsfn, GError **error) |
| 699 | { |
| 700 | g_autofree gchar *filename = NULL; |
| 701 | g_autoptr(SoupURI) uri = NULL; |
| 702 | |
| 703 | /* a local file */ |
| 704 | uri = soup_uri_new (perhapsfn); |
| 705 | if (uri == NULL) |
| 706 | return g_strdup (perhapsfn); |
| 707 | |
| 708 | /* download the firmware to a cachedir */ |
| 709 | filename = fu_util_get_user_cache_path (perhapsfn); |
| 710 | if (!fu_common_mkdir_parent (filename, error)) |
| 711 | return NULL; |
| 712 | if (!fu_util_download_out_of_process (perhapsfn, filename, error)) |
| 713 | return NULL; |
| 714 | return g_steal_pointer (&filename); |
| 715 | } |
| 716 | |
| 717 | static gboolean |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 718 | fu_util_install (FuUtilPrivate *priv, gchar **values, GError **error) |
| 719 | { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 720 | g_autofree gchar *filename = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 721 | g_autoptr(GBytes) blob_cab = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 722 | g_autoptr(GPtrArray) components = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 723 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 724 | g_autoptr(GPtrArray) errors = NULL; |
| 725 | g_autoptr(GPtrArray) install_tasks = NULL; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 726 | g_autoptr(XbSilo) silo = NULL; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 727 | |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 728 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 729 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | 8949e89 | 2018-05-25 08:03:06 -0500 | [diff] [blame] | 730 | return FALSE; |
| 731 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 732 | /* handle both forms */ |
| 733 | if (g_strv_length (values) == 1) { |
| 734 | devices_possible = fu_engine_get_devices (priv->engine, error); |
| 735 | if (devices_possible == NULL) |
| 736 | return FALSE; |
| 737 | } else if (g_strv_length (values) == 2) { |
| 738 | FuDevice *device = fu_engine_get_device (priv->engine, |
| 739 | values[1], |
| 740 | error); |
| 741 | if (device == NULL) |
| 742 | return FALSE; |
| 743 | devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 744 | g_ptr_array_add (devices_possible, device); |
| 745 | } else { |
| 746 | g_set_error_literal (error, |
| 747 | FWUPD_ERROR, |
| 748 | FWUPD_ERROR_INVALID_ARGS, |
| 749 | "Invalid arguments"); |
| 750 | return FALSE; |
| 751 | } |
| 752 | |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 753 | /* download if required */ |
| 754 | filename = fu_util_download_if_required (priv, values[0], error); |
| 755 | if (filename == NULL) |
| 756 | return FALSE; |
| 757 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 758 | /* parse silo */ |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 759 | blob_cab = fu_common_get_contents_bytes (filename, error); |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 760 | if (blob_cab == NULL) { |
Richard Hughes | 3d178be | 2018-08-30 11:14:24 +0100 | [diff] [blame] | 761 | fu_util_maybe_prefix_sandbox_error (filename, error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 762 | return FALSE; |
Mario Limonciello | b72aa8c | 2018-06-08 09:24:36 -0500 | [diff] [blame] | 763 | } |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 764 | silo = fu_engine_get_silo_from_blob (priv->engine, blob_cab, error); |
| 765 | if (silo == NULL) |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 766 | return FALSE; |
Mario Limonciello | 51ddf18 | 2019-01-26 00:31:58 -0600 | [diff] [blame] | 767 | components = xb_silo_query (silo, "components/component", 0, error); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 768 | if (components == NULL) |
| 769 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 770 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 771 | /* for each component in the silo */ |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 772 | errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free); |
| 773 | 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] | 774 | for (guint i = 0; i < components->len; i++) { |
| 775 | XbNode *component = g_ptr_array_index (components, i); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 776 | |
| 777 | /* do any devices pass the requirements */ |
| 778 | for (guint j = 0; j < devices_possible->len; j++) { |
| 779 | FuDevice *device = g_ptr_array_index (devices_possible, j); |
| 780 | g_autoptr(FuInstallTask) task = NULL; |
| 781 | g_autoptr(GError) error_local = NULL; |
| 782 | |
| 783 | /* is this component valid for the device */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 784 | task = fu_install_task_new (device, component); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 785 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 786 | task, priv->flags, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 787 | &error_local)) { |
| 788 | g_debug ("requirement on %s:%s failed: %s", |
| 789 | fu_device_get_id (device), |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 790 | xb_node_query_text (component, "id", NULL), |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 791 | error_local->message); |
| 792 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 793 | continue; |
| 794 | } |
| 795 | |
Mario Limonciello | 7a3df4b | 2019-01-31 10:27:22 -0600 | [diff] [blame] | 796 | /* if component should have an update message from CAB */ |
| 797 | fu_device_incorporate_from_component (device, component); |
| 798 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 799 | /* success */ |
| 800 | g_ptr_array_add (install_tasks, g_steal_pointer (&task)); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* order the install tasks by the device priority */ |
| 805 | g_ptr_array_sort (install_tasks, fu_util_install_task_sort_cb); |
| 806 | |
| 807 | /* nothing suitable */ |
| 808 | if (install_tasks->len == 0) { |
| 809 | GError *error_tmp = fu_common_error_array_get_best (errors); |
| 810 | g_propagate_error (error, error_tmp); |
| 811 | return FALSE; |
| 812 | } |
| 813 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 814 | priv->current_operation = FU_UTIL_OPERATION_INSTALL; |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 815 | g_signal_connect (priv->engine, "device-changed", |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 816 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
Mario Limonciello | 9eb66fe | 2018-08-10 11:32:44 -0500 | [diff] [blame] | 817 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 818 | /* install all the tasks */ |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 819 | if (!fu_engine_install_tasks (priv->engine, install_tasks, blob_cab, priv->flags, error)) |
| 820 | return FALSE; |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 821 | |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 822 | fu_util_display_current_message (priv); |
| 823 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 824 | /* we don't want to ask anything */ |
| 825 | if (priv->no_reboot_check) { |
| 826 | g_debug ("skipping reboot check"); |
| 827 | return TRUE; |
| 828 | } |
| 829 | |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 830 | /* save the device state for other applications to see */ |
| 831 | if (!fu_util_save_current_state (priv, error)) |
| 832 | return FALSE; |
| 833 | |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 834 | /* success */ |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 835 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 836 | } |
| 837 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 838 | static gboolean |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 839 | fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error) |
| 840 | { |
| 841 | g_autoptr(GPtrArray) devices = NULL; |
| 842 | |
| 843 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 844 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 845 | return FALSE; |
| 846 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 847 | priv->current_operation = FU_UTIL_OPERATION_UPDATE; |
| 848 | g_signal_connect (priv->engine, "device-changed", |
| 849 | G_CALLBACK (fu_util_update_device_changed_cb), priv); |
| 850 | |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 851 | devices = fu_engine_get_devices (priv->engine, error); |
Mario Limonciello | 387bda4 | 2019-02-07 14:20:22 +0000 | [diff] [blame] | 852 | if (devices == NULL) |
| 853 | return FALSE; |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 854 | for (guint i = 0; i < devices->len; i++) { |
| 855 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 856 | FwupdRelease *rel; |
| 857 | const gchar *remote_id; |
| 858 | const gchar *device_id; |
| 859 | const gchar *uri_tmp; |
| 860 | g_autoptr(GPtrArray) rels = NULL; |
| 861 | g_autoptr(GError) error_local = NULL; |
| 862 | |
| 863 | if (!fu_util_is_interesting_device (dev)) |
| 864 | continue; |
| 865 | /* only show stuff that has metadata available */ |
| 866 | if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_SUPPORTED)) |
| 867 | continue; |
| 868 | |
| 869 | device_id = fu_device_get_id (dev); |
| 870 | rels = fu_engine_get_upgrades (priv->engine, device_id, &error_local); |
| 871 | if (rels == NULL) { |
| 872 | g_printerr ("%s\n", error_local->message); |
| 873 | continue; |
| 874 | } |
| 875 | |
| 876 | rel = g_ptr_array_index (rels, 0); |
| 877 | uri_tmp = fwupd_release_get_uri (rel); |
| 878 | remote_id = fwupd_release_get_remote_id (rel); |
| 879 | if (remote_id != NULL) { |
| 880 | FwupdRemote *remote; |
| 881 | g_auto(GStrv) argv = NULL; |
| 882 | |
| 883 | remote = fu_engine_get_remote_by_id (priv->engine, |
| 884 | remote_id, |
| 885 | &error_local); |
| 886 | if (remote == NULL) { |
| 887 | g_printerr ("%s\n", error_local->message); |
| 888 | continue; |
| 889 | } |
| 890 | |
| 891 | argv = g_new0 (gchar *, 2); |
| 892 | /* local remotes have the firmware already */ |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 893 | if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) { |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 894 | const gchar *fn_cache = fwupd_remote_get_filename_cache (remote); |
| 895 | g_autofree gchar *path = g_path_get_dirname (fn_cache); |
| 896 | argv[0] = g_build_filename (path, uri_tmp, NULL); |
Mario Limonciello | 4f24d0b | 2019-01-26 01:19:59 -0600 | [diff] [blame] | 897 | } else if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DIRECTORY) { |
| 898 | argv[0] = g_strdup (uri_tmp + 7); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 899 | /* web remote, fu_util_install will download file */ |
| 900 | } else { |
| 901 | argv[0] = fwupd_remote_build_firmware_uri (remote, uri_tmp, error); |
| 902 | } |
| 903 | if (!fu_util_install (priv, argv, &error_local)) { |
| 904 | g_printerr ("%s\n", error_local->message); |
| 905 | continue; |
| 906 | } |
Mario Limonciello | 32241f4 | 2019-01-24 10:12:41 -0600 | [diff] [blame] | 907 | fu_util_display_current_message (priv); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 908 | } |
| 909 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 910 | |
| 911 | /* we don't want to ask anything */ |
| 912 | if (priv->no_reboot_check) { |
| 913 | g_debug ("skipping reboot check"); |
| 914 | return TRUE; |
| 915 | } |
| 916 | |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 917 | /* save the device state for other applications to see */ |
| 918 | if (!fu_util_save_current_state (priv, error)) |
| 919 | return FALSE; |
| 920 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 921 | return fu_util_prompt_complete (priv->completion_flags, TRUE, error); |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static gboolean |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 925 | fu_util_detach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 926 | { |
| 927 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 928 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 929 | |
| 930 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 931 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 932 | return FALSE; |
| 933 | |
| 934 | /* invalid args */ |
| 935 | if (g_strv_length (values) == 0) { |
| 936 | g_set_error_literal (error, |
| 937 | FWUPD_ERROR, |
| 938 | FWUPD_ERROR_INVALID_ARGS, |
| 939 | "Invalid arguments"); |
| 940 | return FALSE; |
| 941 | } |
| 942 | |
| 943 | /* get device */ |
| 944 | if (g_strv_length (values) >= 1) { |
| 945 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 946 | if (device == NULL) |
| 947 | return FALSE; |
| 948 | } else { |
| 949 | device = fu_util_prompt_for_device (priv, error); |
| 950 | if (device == NULL) |
| 951 | return FALSE; |
| 952 | } |
| 953 | |
| 954 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 955 | locker = fu_device_locker_new (device, error); |
| 956 | if (locker == NULL) |
| 957 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 958 | return fu_device_detach (device, error); |
| 959 | } |
| 960 | |
| 961 | static gboolean |
| 962 | fu_util_attach (FuUtilPrivate *priv, gchar **values, GError **error) |
| 963 | { |
| 964 | g_autoptr(FuDevice) device = NULL; |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 965 | g_autoptr(FuDeviceLocker) locker = NULL; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 966 | |
| 967 | /* load engine */ |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 968 | if (!fu_util_start_engine (priv, FU_ENGINE_LOAD_FLAG_NONE, error)) |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 969 | return FALSE; |
| 970 | |
| 971 | /* invalid args */ |
| 972 | if (g_strv_length (values) == 0) { |
| 973 | g_set_error_literal (error, |
| 974 | FWUPD_ERROR, |
| 975 | FWUPD_ERROR_INVALID_ARGS, |
| 976 | "Invalid arguments"); |
| 977 | return FALSE; |
| 978 | } |
| 979 | |
| 980 | /* get device */ |
| 981 | if (g_strv_length (values) >= 1) { |
| 982 | device = fu_engine_get_device (priv->engine, values[0], error); |
| 983 | if (device == NULL) |
| 984 | return FALSE; |
| 985 | } else { |
| 986 | device = fu_util_prompt_for_device (priv, error); |
| 987 | if (device == NULL) |
| 988 | return FALSE; |
| 989 | } |
| 990 | |
| 991 | /* run vfunc */ |
Richard Hughes | 2a679cd | 2018-09-04 21:13:23 +0100 | [diff] [blame] | 992 | locker = fu_device_locker_new (device, error); |
| 993 | if (locker == NULL) |
| 994 | return FALSE; |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 995 | return fu_device_attach (device, error); |
| 996 | } |
| 997 | |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 998 | static gboolean |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 999 | fu_util_activate (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1000 | { |
| 1001 | gboolean has_pending = FALSE; |
| 1002 | g_autoptr(FuHistory) history = fu_history_new (); |
| 1003 | g_autoptr(GPtrArray) devices = NULL; |
| 1004 | |
| 1005 | /* check the history database before starting the daemon */ |
| 1006 | if (g_strv_length (values) == 0) { |
| 1007 | devices = fu_history_get_devices (history, error); |
| 1008 | if (devices == NULL) |
| 1009 | return FALSE; |
| 1010 | } else if (g_strv_length (values) == 1) { |
| 1011 | FuDevice *device; |
| 1012 | device = fu_history_get_device_by_id (history, values[0], error); |
| 1013 | if (device == NULL) |
| 1014 | return FALSE; |
| 1015 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 1016 | g_ptr_array_add (devices, device); |
| 1017 | } else { |
| 1018 | g_set_error_literal (error, |
| 1019 | FWUPD_ERROR, |
| 1020 | FWUPD_ERROR_INVALID_ARGS, |
| 1021 | "Invalid arguments"); |
| 1022 | return FALSE; |
| 1023 | } |
| 1024 | |
| 1025 | /* nothing to do */ |
| 1026 | for (guint i = 0; i < devices->len; i++) { |
| 1027 | FuDevice *dev = g_ptr_array_index (devices, i); |
| 1028 | if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) { |
| 1029 | fu_engine_add_plugin_filter (priv->engine, |
| 1030 | fu_device_get_plugin (dev)); |
| 1031 | has_pending = TRUE; |
| 1032 | } |
| 1033 | } |
| 1034 | if (!has_pending) { |
| 1035 | g_set_error_literal (error, |
| 1036 | FWUPD_ERROR, |
| 1037 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1038 | "No firmware to activate"); |
| 1039 | return FALSE; |
| 1040 | |
| 1041 | } |
| 1042 | |
| 1043 | /* load engine */ |
Richard Hughes | 88dc0f4 | 2019-03-07 11:58:11 +0000 | [diff] [blame] | 1044 | 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] | 1045 | return FALSE; |
| 1046 | |
| 1047 | /* activate anything with _NEEDS_ACTIVATION */ |
| 1048 | for (guint i = 0; i < devices->len; i++) { |
| 1049 | FuDevice *device = g_ptr_array_index (devices, i); |
| 1050 | if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION)) |
| 1051 | continue; |
| 1052 | /* TRANSLATORS: shown when shutting down to switch to the new version */ |
| 1053 | g_print ("%s %s…\n", _("Activating firmware update"), fu_device_get_name (device)); |
| 1054 | if (!fu_engine_activate (priv->engine, fu_device_get_id (device), error)) |
| 1055 | return FALSE; |
| 1056 | } |
| 1057 | |
| 1058 | return TRUE; |
| 1059 | } |
| 1060 | |
| 1061 | static gboolean |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1062 | fu_util_hwids (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1063 | { |
| 1064 | g_autoptr(FuSmbios) smbios = fu_smbios_new (); |
| 1065 | g_autoptr(FuHwids) hwids = fu_hwids_new (); |
| 1066 | const gchar *hwid_keys[] = { |
| 1067 | FU_HWIDS_KEY_BIOS_VENDOR, |
| 1068 | FU_HWIDS_KEY_BIOS_VERSION, |
| 1069 | FU_HWIDS_KEY_BIOS_MAJOR_RELEASE, |
| 1070 | FU_HWIDS_KEY_BIOS_MINOR_RELEASE, |
| 1071 | FU_HWIDS_KEY_MANUFACTURER, |
| 1072 | FU_HWIDS_KEY_FAMILY, |
| 1073 | FU_HWIDS_KEY_PRODUCT_NAME, |
| 1074 | FU_HWIDS_KEY_PRODUCT_SKU, |
| 1075 | FU_HWIDS_KEY_ENCLOSURE_KIND, |
| 1076 | FU_HWIDS_KEY_BASEBOARD_MANUFACTURER, |
| 1077 | FU_HWIDS_KEY_BASEBOARD_PRODUCT, |
| 1078 | NULL }; |
| 1079 | |
| 1080 | /* read DMI data */ |
| 1081 | if (g_strv_length (values) == 0) { |
| 1082 | if (!fu_smbios_setup (smbios, error)) |
| 1083 | return FALSE; |
| 1084 | } else if (g_strv_length (values) == 1) { |
| 1085 | if (!fu_smbios_setup_from_file (smbios, values[0], error)) |
| 1086 | return FALSE; |
| 1087 | } else { |
| 1088 | g_set_error_literal (error, |
| 1089 | FWUPD_ERROR, |
| 1090 | FWUPD_ERROR_INVALID_ARGS, |
| 1091 | "Invalid arguments"); |
| 1092 | return FALSE; |
| 1093 | } |
| 1094 | if (!fu_hwids_setup (hwids, smbios, error)) |
| 1095 | return FALSE; |
| 1096 | |
| 1097 | /* show debug output */ |
| 1098 | g_print ("Computer Information\n"); |
| 1099 | g_print ("--------------------\n"); |
| 1100 | for (guint i = 0; hwid_keys[i] != NULL; i++) { |
| 1101 | const gchar *tmp = fu_hwids_get_value (hwids, hwid_keys[i]); |
| 1102 | if (tmp == NULL) |
| 1103 | continue; |
| 1104 | if (g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MAJOR_RELEASE) == 0 || |
| 1105 | g_strcmp0 (hwid_keys[i], FU_HWIDS_KEY_BIOS_MINOR_RELEASE) == 0) { |
| 1106 | guint64 val = g_ascii_strtoull (tmp, NULL, 16); |
| 1107 | g_print ("%s: %" G_GUINT64_FORMAT "\n", hwid_keys[i], val); |
| 1108 | } else { |
| 1109 | g_print ("%s: %s\n", hwid_keys[i], tmp); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | /* show GUIDs */ |
| 1114 | g_print ("\nHardware IDs\n"); |
| 1115 | g_print ("------------\n"); |
| 1116 | for (guint i = 0; i < 15; i++) { |
| 1117 | const gchar *keys = NULL; |
| 1118 | g_autofree gchar *guid = NULL; |
| 1119 | g_autofree gchar *key = NULL; |
| 1120 | g_autofree gchar *keys_str = NULL; |
| 1121 | g_auto(GStrv) keysv = NULL; |
| 1122 | g_autoptr(GError) error_local = NULL; |
| 1123 | |
| 1124 | /* get the GUID */ |
| 1125 | key = g_strdup_printf ("HardwareID-%u", i); |
| 1126 | keys = fu_hwids_get_replace_keys (hwids, key); |
| 1127 | guid = fu_hwids_get_guid (hwids, key, &error_local); |
| 1128 | if (guid == NULL) { |
| 1129 | g_print ("%s\n", error_local->message); |
| 1130 | continue; |
| 1131 | } |
| 1132 | |
| 1133 | /* show what makes up the GUID */ |
| 1134 | keysv = g_strsplit (keys, "&", -1); |
| 1135 | keys_str = g_strjoinv (" + ", keysv); |
| 1136 | g_print ("{%s} <- %s\n", guid, keys_str); |
| 1137 | } |
| 1138 | |
| 1139 | return TRUE; |
| 1140 | } |
| 1141 | |
Mario Limonciello | f6d01b1 | 2018-10-18 12:57:10 -0500 | [diff] [blame] | 1142 | static gboolean |
| 1143 | fu_util_firmware_builder (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1144 | { |
| 1145 | const gchar *script_fn = "startup.sh"; |
| 1146 | const gchar *output_fn = "firmware.bin"; |
| 1147 | g_autoptr(GBytes) archive_blob = NULL; |
| 1148 | g_autoptr(GBytes) firmware_blob = NULL; |
| 1149 | if (g_strv_length (values) < 2) { |
| 1150 | g_set_error_literal (error, |
| 1151 | FWUPD_ERROR, |
| 1152 | FWUPD_ERROR_INVALID_ARGS, |
| 1153 | "Invalid arguments"); |
| 1154 | return FALSE; |
| 1155 | } |
| 1156 | archive_blob = fu_common_get_contents_bytes (values[0], error); |
| 1157 | if (archive_blob == NULL) |
| 1158 | return FALSE; |
| 1159 | if (g_strv_length (values) > 2) |
| 1160 | script_fn = values[2]; |
| 1161 | if (g_strv_length (values) > 3) |
| 1162 | output_fn = values[3]; |
| 1163 | firmware_blob = fu_common_firmware_builder (archive_blob, script_fn, output_fn, error); |
| 1164 | if (firmware_blob == NULL) |
| 1165 | return FALSE; |
| 1166 | return fu_common_set_contents_bytes (values[1], firmware_blob, error); |
| 1167 | } |
| 1168 | |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1169 | static void |
| 1170 | fu_util_device_added_cb (FwupdClient *client, |
| 1171 | FwupdDevice *device, |
| 1172 | gpointer user_data) |
| 1173 | { |
| 1174 | g_autofree gchar *tmp = fwupd_device_to_string (device); |
| 1175 | /* TRANSLATORS: this is when a device is hotplugged */ |
| 1176 | g_print ("%s\n%s", _("Device added:"), tmp); |
| 1177 | } |
| 1178 | |
| 1179 | static void |
| 1180 | fu_util_device_removed_cb (FwupdClient *client, |
| 1181 | FwupdDevice *device, |
| 1182 | gpointer user_data) |
| 1183 | { |
| 1184 | g_autofree gchar *tmp = fwupd_device_to_string (device); |
| 1185 | /* TRANSLATORS: this is when a device is hotplugged */ |
| 1186 | g_print ("%s\n%s", _("Device removed:"), tmp); |
| 1187 | } |
| 1188 | |
| 1189 | static void |
| 1190 | fu_util_device_changed_cb (FwupdClient *client, |
| 1191 | FwupdDevice *device, |
| 1192 | gpointer user_data) |
| 1193 | { |
| 1194 | g_autofree gchar *tmp = fwupd_device_to_string (device); |
| 1195 | /* TRANSLATORS: this is when a device has been updated */ |
| 1196 | g_print ("%s\n%s", _("Device changed:"), tmp); |
| 1197 | } |
| 1198 | |
| 1199 | static void |
| 1200 | fu_util_changed_cb (FwupdClient *client, gpointer user_data) |
| 1201 | { |
| 1202 | /* TRANSLATORS: this is when the daemon state changes */ |
| 1203 | g_print ("%s\n", _("Changed")); |
| 1204 | } |
| 1205 | |
| 1206 | static gboolean |
| 1207 | fu_util_monitor (FuUtilPrivate *priv, gchar **values, GError **error) |
| 1208 | { |
| 1209 | g_autoptr(FwupdClient) client = fwupd_client_new (); |
| 1210 | |
| 1211 | /* get all the devices */ |
| 1212 | if (!fwupd_client_connect (client, priv->cancellable, error)) |
| 1213 | return FALSE; |
| 1214 | |
| 1215 | /* watch for any hotplugged device */ |
| 1216 | g_signal_connect (client, "changed", |
| 1217 | G_CALLBACK (fu_util_changed_cb), priv); |
| 1218 | g_signal_connect (client, "device-added", |
| 1219 | G_CALLBACK (fu_util_device_added_cb), priv); |
| 1220 | g_signal_connect (client, "device-removed", |
| 1221 | G_CALLBACK (fu_util_device_removed_cb), priv); |
| 1222 | g_signal_connect (client, "device-changed", |
| 1223 | G_CALLBACK (fu_util_device_changed_cb), priv); |
| 1224 | g_signal_connect (priv->cancellable, "cancelled", |
| 1225 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 1226 | g_main_loop_run (priv->loop); |
| 1227 | return TRUE; |
| 1228 | } |
| 1229 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1230 | int |
| 1231 | main (int argc, char *argv[]) |
| 1232 | { |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1233 | gboolean allow_older = FALSE; |
| 1234 | gboolean allow_reinstall = FALSE; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1235 | gboolean force = FALSE; |
| 1236 | gboolean ret; |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1237 | gboolean version = FALSE; |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 1238 | gboolean interactive = isatty (fileno (stdout)) != 0; |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1239 | g_auto(GStrv) plugin_glob = NULL; |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1240 | g_autoptr(FuUtilPrivate) priv = g_new0 (FuUtilPrivate, 1); |
| 1241 | g_autoptr(GError) error = NULL; |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1242 | g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new (); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1243 | g_autofree gchar *cmd_descriptions = NULL; |
| 1244 | const GOptionEntry options[] = { |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1245 | { "version", '\0', 0, G_OPTION_ARG_NONE, &version, |
| 1246 | /* TRANSLATORS: command line option */ |
| 1247 | _("Show client and daemon versions"), NULL }, |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1248 | { "allow-reinstall", '\0', 0, G_OPTION_ARG_NONE, &allow_reinstall, |
| 1249 | /* TRANSLATORS: command line option */ |
| 1250 | _("Allow re-installing existing firmware versions"), NULL }, |
| 1251 | { "allow-older", '\0', 0, G_OPTION_ARG_NONE, &allow_older, |
| 1252 | /* TRANSLATORS: command line option */ |
| 1253 | _("Allow downgrading firmware versions"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1254 | { "force", '\0', 0, G_OPTION_ARG_NONE, &force, |
| 1255 | /* TRANSLATORS: command line option */ |
| 1256 | _("Override plugin warning"), NULL }, |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1257 | { "no-reboot-check", '\0', 0, G_OPTION_ARG_NONE, &priv->no_reboot_check, |
| 1258 | /* TRANSLATORS: command line option */ |
| 1259 | _("Do not check for reboot after update"), NULL }, |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 1260 | { "show-all-devices", '\0', 0, G_OPTION_ARG_NONE, &priv->show_all_devices, |
| 1261 | /* TRANSLATORS: command line option */ |
| 1262 | _("Show devices that are not updatable"), NULL }, |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1263 | { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING_ARRAY, &plugin_glob, |
| 1264 | /* TRANSLATORS: command line option */ |
| 1265 | _("Manually whitelist specific plugins"), NULL }, |
Mario Limonciello | 8402cea | 2019-02-07 20:25:31 -0600 | [diff] [blame] | 1266 | { "prepare", '\0', 0, G_OPTION_ARG_NONE, &priv->prepare_blob, |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 1267 | /* TRANSLATORS: command line option */ |
| 1268 | _("Run the plugin composite prepare routine when using install-blob"), NULL }, |
Mario Limonciello | 8402cea | 2019-02-07 20:25:31 -0600 | [diff] [blame] | 1269 | { "cleanup", '\0', 0, G_OPTION_ARG_NONE, &priv->cleanup_blob, |
Mario Limonciello | 53ce25d | 2019-02-01 16:09:03 +0000 | [diff] [blame] | 1270 | /* TRANSLATORS: command line option */ |
| 1271 | _("Run the plugin composite cleanup routine when using install-blob"), NULL }, |
Mario Limonciello | 3143bad | 2019-02-27 07:31:00 -0600 | [diff] [blame] | 1272 | { "enable-json-state", '\0', 0, G_OPTION_ARG_NONE, &priv->enable_json_state, |
| 1273 | /* TRANSLATORS: command line option */ |
| 1274 | _("Save device state into a JSON file between executions"), NULL }, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1275 | { NULL} |
| 1276 | }; |
| 1277 | |
| 1278 | setlocale (LC_ALL, ""); |
| 1279 | |
| 1280 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 1281 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 1282 | textdomain (GETTEXT_PACKAGE); |
| 1283 | |
| 1284 | /* ensure root user */ |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 1285 | if (interactive && (getuid () != 0 || geteuid () != 0)) |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1286 | /* TRANSLATORS: we're poking around as a power user */ |
Mario Limonciello | b900c09 | 2018-05-22 14:22:21 -0500 | [diff] [blame] | 1287 | g_printerr ("%s\n", _("This program may only work correctly as root")); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1288 | |
| 1289 | /* create helper object */ |
| 1290 | priv->loop = g_main_loop_new (NULL, FALSE); |
| 1291 | priv->progressbar = fu_progressbar_new (); |
| 1292 | |
| 1293 | /* add commands */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1294 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | f6d01b1 | 2018-10-18 12:57:10 -0500 | [diff] [blame] | 1295 | "build-firmware", |
| 1296 | "FILE-IN FILE-OUT [SCRIPT] [OUTPUT]", |
| 1297 | /* TRANSLATORS: command description */ |
| 1298 | _("Build firmware using a sandbox"), |
| 1299 | fu_util_firmware_builder); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1300 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1301 | "smbios-dump", |
| 1302 | "FILE", |
| 1303 | /* TRANSLATORS: command description */ |
| 1304 | _("Dump SMBIOS data from a file"), |
| 1305 | fu_util_smbios_dump); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1306 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 1307 | "get-plugins", |
| 1308 | NULL, |
| 1309 | /* TRANSLATORS: command description */ |
| 1310 | _("Get all enabled plugins registered with the system"), |
| 1311 | fu_util_get_plugins); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1312 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 716ab27 | 2018-05-29 12:34:37 -0500 | [diff] [blame] | 1313 | "get-details", |
| 1314 | NULL, |
| 1315 | /* TRANSLATORS: command description */ |
| 1316 | _("Gets details about a firmware file"), |
| 1317 | fu_util_get_details); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1318 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 1e35e4c | 2019-01-28 11:13:02 -0600 | [diff] [blame] | 1319 | "get-updates", |
| 1320 | NULL, |
| 1321 | /* TRANSLATORS: command description */ |
| 1322 | _("Gets the list of updates for connected hardware"), |
| 1323 | fu_util_get_updates); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1324 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1325 | "get-devices", |
| 1326 | NULL, |
| 1327 | /* TRANSLATORS: command description */ |
| 1328 | _("Get all devices that support firmware updates"), |
| 1329 | fu_util_get_devices); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1330 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | ba9e5b9 | 2018-05-21 16:02:32 -0500 | [diff] [blame] | 1331 | "get-topology", |
| 1332 | NULL, |
| 1333 | /* TRANSLATORS: command description */ |
| 1334 | _("Get all devices according to the system topology"), |
| 1335 | fu_util_get_topology); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1336 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1337 | "watch", |
| 1338 | NULL, |
| 1339 | /* TRANSLATORS: command description */ |
Piotr DrÄ…g | 472fa59 | 2018-06-06 14:53:48 +0200 | [diff] [blame] | 1340 | _("Watch for hardware changes"), |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1341 | fu_util_watch); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1342 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1343 | "install-blob", |
| 1344 | "FILENAME DEVICE-ID", |
| 1345 | /* TRANSLATORS: command description */ |
| 1346 | _("Install a firmware blob on a device"), |
| 1347 | fu_util_install_blob); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1348 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | a36c9cf | 2018-05-20 10:41:44 +0100 | [diff] [blame] | 1349 | "install", |
| 1350 | "FILE [ID]", |
| 1351 | /* TRANSLATORS: command description */ |
| 1352 | _("Install a firmware file on this hardware"), |
| 1353 | fu_util_install); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1354 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1355 | "attach", |
| 1356 | "DEVICE-ID", |
| 1357 | /* TRANSLATORS: command description */ |
| 1358 | _("Attach to firmware mode"), |
| 1359 | fu_util_attach); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1360 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1361 | "detach", |
| 1362 | "DEVICE-ID", |
| 1363 | /* TRANSLATORS: command description */ |
| 1364 | _("Detach to bootloader mode"), |
| 1365 | fu_util_detach); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1366 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1367 | "activate", |
| 1368 | "[DEVICE-ID]", |
| 1369 | /* TRANSLATORS: command description */ |
| 1370 | _("Activate pending devices"), |
| 1371 | fu_util_activate); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1372 | fu_util_cmd_array_add (cmd_array, |
Richard Hughes | 1d894f1 | 2018-08-31 13:05:51 +0100 | [diff] [blame] | 1373 | "hwids", |
| 1374 | "[FILE]", |
| 1375 | /* TRANSLATORS: command description */ |
| 1376 | _("Return all the hardware IDs for the machine"), |
| 1377 | fu_util_hwids); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1378 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 62f8486 | 2018-10-18 13:15:23 -0500 | [diff] [blame] | 1379 | "monitor", |
| 1380 | NULL, |
| 1381 | /* TRANSLATORS: command description */ |
| 1382 | _("Monitor the daemon for events"), |
| 1383 | fu_util_monitor); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1384 | fu_util_cmd_array_add (cmd_array, |
Mario Limonciello | 46aaee8 | 2019-01-10 12:58:00 -0600 | [diff] [blame] | 1385 | "update", |
| 1386 | NULL, |
| 1387 | /* TRANSLATORS: command description */ |
| 1388 | _("Update all devices that match local metadata"), |
| 1389 | fu_util_update); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1390 | |
| 1391 | /* do stuff on ctrl+c */ |
| 1392 | priv->cancellable = g_cancellable_new (); |
| 1393 | g_unix_signal_add_full (G_PRIORITY_DEFAULT, |
| 1394 | SIGINT, fu_util_sigint_cb, |
| 1395 | priv, NULL); |
| 1396 | g_signal_connect (priv->cancellable, "cancelled", |
| 1397 | G_CALLBACK (fu_util_cancelled_cb), priv); |
| 1398 | |
| 1399 | /* sort by command name */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1400 | fu_util_cmd_array_sort (cmd_array); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1401 | |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1402 | /* non-TTY consoles cannot answer questions */ |
Mario Limonciello | 5d7aa40 | 2019-02-04 09:35:58 -0600 | [diff] [blame] | 1403 | if (!interactive) { |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1404 | priv->no_reboot_check = TRUE; |
Mario Limonciello | 9b31e6f | 2019-01-31 15:42:19 -0600 | [diff] [blame] | 1405 | fu_progressbar_set_interactive (priv->progressbar, FALSE); |
| 1406 | } |
Mario Limonciello | 3f243a9 | 2019-01-21 22:05:23 -0600 | [diff] [blame] | 1407 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1408 | /* get a list of the commands */ |
| 1409 | priv->context = g_option_context_new (NULL); |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1410 | cmd_descriptions = fu_util_cmd_array_to_string (cmd_array); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1411 | g_option_context_set_summary (priv->context, cmd_descriptions); |
| 1412 | g_option_context_set_description (priv->context, |
| 1413 | "This tool allows an administrator to use the fwupd plugins " |
| 1414 | "without being installed on the host system."); |
| 1415 | |
| 1416 | /* TRANSLATORS: program name */ |
| 1417 | g_set_application_name (_("Firmware Utility")); |
| 1418 | g_option_context_add_main_entries (priv->context, options, NULL); |
Mario Limonciello | fde4773 | 2018-09-11 12:20:58 -0500 | [diff] [blame] | 1419 | g_option_context_add_group (priv->context, fu_debug_get_option_group ()); |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1420 | ret = g_option_context_parse (priv->context, &argc, &argv, &error); |
| 1421 | if (!ret) { |
| 1422 | /* TRANSLATORS: the user didn't read the man page */ |
| 1423 | g_print ("%s: %s\n", _("Failed to parse arguments"), |
| 1424 | error->message); |
| 1425 | return EXIT_FAILURE; |
| 1426 | } |
| 1427 | |
Richard Hughes | 460226a | 2018-05-21 20:56:21 +0100 | [diff] [blame] | 1428 | /* set flags */ |
| 1429 | priv->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY; |
| 1430 | if (allow_reinstall) |
| 1431 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
| 1432 | if (allow_older) |
| 1433 | priv->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
| 1434 | if (force) |
| 1435 | priv->flags |= FWUPD_INSTALL_FLAG_FORCE; |
| 1436 | |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1437 | /* load engine */ |
| 1438 | priv->engine = fu_engine_new (FU_APP_FLAGS_NO_IDLE_SOURCES); |
| 1439 | g_signal_connect (priv->engine, "device-added", |
| 1440 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 1441 | priv); |
| 1442 | g_signal_connect (priv->engine, "device-removed", |
| 1443 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 1444 | priv); |
Richard Hughes | 98ca993 | 2018-05-18 10:24:07 +0100 | [diff] [blame] | 1445 | g_signal_connect (priv->engine, "status-changed", |
| 1446 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 1447 | priv); |
| 1448 | g_signal_connect (priv->engine, "percentage-changed", |
| 1449 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 1450 | priv); |
| 1451 | |
Mario Limonciello | 2d4b7a5 | 2018-09-12 22:08:04 -0500 | [diff] [blame] | 1452 | /* just show versions and exit */ |
| 1453 | if (version) { |
| 1454 | g_autofree gchar *version_str = fu_util_get_versions (); |
| 1455 | g_print ("%s\n", version_str); |
| 1456 | return EXIT_SUCCESS; |
| 1457 | } |
| 1458 | |
Richard Hughes | c02ee4d | 2018-05-22 15:46:03 +0100 | [diff] [blame] | 1459 | /* any plugin whitelist specified */ |
| 1460 | for (guint i = 0; plugin_glob != NULL && plugin_glob[i] != NULL; i++) |
| 1461 | fu_engine_add_plugin_filter (priv->engine, plugin_glob[i]); |
| 1462 | |
Richard Hughes | b597683 | 2018-05-18 10:02:09 +0100 | [diff] [blame] | 1463 | /* run the specified command */ |
Richard Hughes | c77e111 | 2019-03-01 10:16:26 +0000 | [diff] [blame] | 1464 | 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] | 1465 | if (!ret) { |
| 1466 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_ARGS)) { |
| 1467 | g_autofree gchar *tmp = NULL; |
| 1468 | tmp = g_option_context_get_help (priv->context, TRUE, NULL); |
| 1469 | g_print ("%s\n\n%s", error->message, tmp); |
| 1470 | return EXIT_FAILURE; |
| 1471 | } |
| 1472 | if (g_error_matches (error, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) { |
| 1473 | g_print ("%s\n", error->message); |
| 1474 | return EXIT_NOTHING_TO_DO; |
| 1475 | } |
| 1476 | g_print ("%s\n", error->message); |
| 1477 | return EXIT_FAILURE; |
| 1478 | } |
| 1479 | |
| 1480 | /* success */ |
| 1481 | return EXIT_SUCCESS; |
| 1482 | } |