| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 3 |
/* |
| 4 |
* mbimcli -- Command line interface to control MBIM devices |
| 5 |
* |
| 6 |
* Copyright 2018 Google LLC |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "config.h" |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <locale.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include <glib.h> |
| 17 |
#include <gio/gio.h> |
| 18 |
|
| 19 |
#include <libmbim-glib.h> |
| 20 |
|
| 21 |
#include "mbimcli.h" |
| 22 |
#include "mbimcli-helpers.h" |
| 23 |
|
| 24 |
/* Context */ |
| 25 |
typedef struct { |
| 26 |
MbimDevice *device; |
| 27 |
GCancellable *cancellable; |
| 28 |
} Context; |
| 29 |
static Context *ctx; |
| 30 |
|
| 31 |
/* Options */ |
| 32 |
static gboolean modem_reboot_set; |
| 33 |
static gchar *modem_reboot_str; |
| 34 |
|
| 35 |
static gboolean modem_reboot_arg_parse (const gchar *option_name, |
| 36 |
const gchar *value, |
| 37 |
gpointer user_data, |
| 38 |
GError **error); |
| 39 |
|
| 40 |
static GOptionEntry entries[] = { |
| 41 |
{ "intel-modem-reboot", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, G_CALLBACK (modem_reboot_arg_parse), |
| 42 |
"Reboot modem. Boot mode and timeout arguments only required if MBIMEx >= 2.0.", |
| 43 |
"[(Boot Mode),(Timeout)]" |
| 44 |
}, |
| 45 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 46 |
}; |
| 47 |
|
| 48 |
GOptionGroup * |
| 49 |
mbimcli_intel_firmware_update_get_option_group (void) |
| 50 |
{ |
| 51 |
GOptionGroup *group; |
| 52 |
|
| 53 |
group = g_option_group_new ("intel-firmware-update", |
| 54 |
"Intel Firmware Update Service options:", |
| 55 |
"Show Intel Firmware Update Service options", |
| 56 |
NULL, |
| 57 |
NULL); |
| 58 |
g_option_group_add_entries (group, entries); |
| 59 |
|
| 60 |
return group; |
| 61 |
} |
| 62 |
|
| 63 |
static gboolean |
| 64 |
modem_reboot_arg_parse (const gchar *option_name, |
| 65 |
const gchar *value, |
| 66 |
gpointer user_data, |
| 67 |
GError **error) |
| 68 |
{ |
| 69 |
modem_reboot_set = TRUE; |
| 70 |
modem_reboot_str = g_strdup (value); |
| 71 |
return TRUE; |
| 72 |
} |
| 73 |
|
| 74 |
gboolean |
| 75 |
mbimcli_intel_firmware_update_options_enabled (void) |
| 76 |
{ |
| 77 |
static guint n_actions = 0; |
| 78 |
static gboolean checked = FALSE; |
| 79 |
|
| 80 |
if (checked) |
| 81 |
return !!n_actions; |
| 82 |
|
| 83 |
n_actions = modem_reboot_set; |
| 84 |
|
| 85 |
if (n_actions > 1) { |
| 86 |
g_printerr ("error: too many Intel Firmware Update Service actions requested\n"); |
| 87 |
exit (EXIT_FAILURE); |
| 88 |
} |
| 89 |
|
| 90 |
checked = TRUE; |
| 91 |
return !!n_actions; |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
context_free (Context *context) |
| 96 |
{ |
| 97 |
if (!context) |
| 98 |
return; |
| 99 |
|
| 100 |
if (context->cancellable) |
| 101 |
g_object_unref (context->cancellable); |
| 102 |
if (context->device) |
| 103 |
g_object_unref (context->device); |
| 104 |
g_slice_free (Context, context); |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
shutdown (gboolean operation_status) |
| 109 |
{ |
| 110 |
/* Cleanup context and finish async operation */ |
| 111 |
context_free (ctx); |
| 112 |
mbimcli_async_operation_done (operation_status); |
| 113 |
} |
| 114 |
|
| 115 |
static void |
| 116 |
modem_reboot_ready (MbimDevice *device, |
| 117 |
GAsyncResult *res) |
| 118 |
{ |
| 119 |
g_autoptr(MbimMessage) response = NULL; |
| 120 |
g_autoptr(GError) error = NULL; |
| 121 |
|
| 122 |
response = mbim_device_command_finish (device, res, &error); |
| 123 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 124 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 125 |
shutdown (FALSE); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
g_print ("[%s] Successfully requested modem to reboot for firmware update\n\n", |
| 130 |
mbim_device_get_path_display (device)); |
| 131 |
|
| 132 |
shutdown (TRUE); |
| 133 |
} |
| 134 |
|
| 135 |
static gboolean |
| 136 |
modem_reboot_v2_input_parse (const gchar *str, |
| 137 |
MbimIntelBootMode *out_boot_mode, |
| 138 |
guint32 *out_timeout) |
| 139 |
{ |
| 140 |
g_auto(GStrv) split = NULL; |
| 141 |
g_autoptr(GError) error = NULL; |
| 142 |
|
| 143 |
split = g_strsplit (modem_reboot_str, ",", -1); |
| 144 |
|
| 145 |
if (g_strv_length (split) > 2) { |
| 146 |
g_printerr ("error: couldn't parse input string, too many arguments\n"); |
| 147 |
return FALSE; |
| 148 |
} |
| 149 |
|
| 150 |
if (g_strv_length (split) < 2) { |
| 151 |
g_printerr ("error: couldn't parse input string, missing arguments\n"); |
| 152 |
return FALSE; |
| 153 |
} |
| 154 |
|
| 155 |
if (!mbimcli_read_intel_boot_mode_from_string (split[0], out_boot_mode, &error)) { |
| 156 |
g_printerr ("error: couldn't read boot mode: %s\n", error->message); |
| 157 |
return FALSE; |
| 158 |
} |
| 159 |
|
| 160 |
if (!mbimcli_read_uint_from_string (split[1], out_timeout)) { |
| 161 |
g_printerr ("error: couldn't read timeout value\n"); |
| 162 |
return FALSE; |
| 163 |
} |
| 164 |
|
| 165 |
return TRUE; |
| 166 |
} |
| 167 |
|
| 168 |
void |
| 169 |
mbimcli_intel_firmware_update_run (MbimDevice *device, |
| 170 |
GCancellable *cancellable) |
| 171 |
{ |
| 172 |
g_autoptr(MbimMessage) request = NULL; |
| 173 |
|
| 174 |
/* Initialize context */ |
| 175 |
ctx = g_slice_new (Context); |
| 176 |
ctx->device = g_object_ref (device); |
| 177 |
ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; |
| 178 |
|
| 179 |
/* Request to reboot modem? */ |
| 180 |
if (modem_reboot_set) { |
| 181 |
if (mbim_device_check_ms_mbimex_version (device, 2, 0)) { |
| 182 |
MbimIntelBootMode boot_mode = MBIM_INTEL_BOOT_MODE_NORMAL_MODE; |
| 183 |
guint32 timeout = 0; |
| 184 |
|
| 185 |
if (!modem_reboot_v2_input_parse (modem_reboot_str, &boot_mode, &timeout)) { |
| 186 |
g_printerr ("error: couldn't parse input arguments\n"); |
| 187 |
g_printerr ("error: device in MBIMEx >= 2.0 requires boot mode and timeout arguments.\n"); |
| 188 |
shutdown (FALSE); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
request = mbim_message_intel_firmware_update_v2_modem_reboot_set_new (boot_mode, timeout, NULL); |
| 193 |
} else { |
| 194 |
if (modem_reboot_str) { |
| 195 |
g_printerr ("error: arguments are not expected in MBIMEx < 2.0\n"); |
| 196 |
shutdown (FALSE); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
g_debug ("Asynchronously rebooting modem..."); |
| 201 |
request = mbim_message_intel_firmware_update_modem_reboot_set_new (NULL); |
| 202 |
} |
| 203 |
|
| 204 |
mbim_device_command (ctx->device, |
| 205 |
request, |
| 206 |
10, |
| 207 |
ctx->cancellable, |
| 208 |
(GAsyncReadyCallback)modem_reboot_ready, |
| 209 |
NULL); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
g_warn_if_reached (); |
| 214 |
} |