File: fu-system76-launch-device.c

package info (click to toggle)
fwupd 2.0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,504 kB
  • sloc: ansic: 277,388; python: 11,485; xml: 9,493; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (277 lines) | stat: -rw-r--r-- 8,264 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright 2019 Richard Hughes <richard@hughsie.com>
 * Copyright 2021 Jeremy Soller <jeremy@system76.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "fu-system76-launch-device.h"
#include "fu-system76-launch-struct.h"

#define SYSTEM76_LAUNCH_CMD_VERSION	 3
#define SYSTEM76_LAUNCH_CMD_RESET	 6
#define SYSTEM76_LAUNCH_CMD_SECURITY_SET 21
#define SYSTEM76_LAUNCH_TIMEOUT		 1000

struct _FuSystem76LaunchDevice {
	FuUsbDevice parent_instance;
};

G_DEFINE_TYPE(FuSystem76LaunchDevice, fu_system76_launch_device, FU_TYPE_USB_DEVICE)

typedef struct {
	guint8 *data;
	gsize len;
} FuSystem76LaunchDeviceHelper;

static gboolean
fu_system76_launch_device_response_cb(FuDevice *device, gpointer user_data, GError **error)
{
	const guint8 ep_in = 0x82;
	gsize actual_len = 0;
	FuSystem76LaunchDeviceHelper *helper = (FuSystem76LaunchDeviceHelper *)user_data;

	/* receive response */
	if (!fu_usb_device_interrupt_transfer(FU_USB_DEVICE(device),
					      ep_in,
					      helper->data,
					      helper->len,
					      &actual_len,
					      SYSTEM76_LAUNCH_TIMEOUT,
					      NULL,
					      error)) {
		g_prefix_error_literal(error, "failed to read response: ");
		return FALSE;
	}
	if (actual_len < helper->len) {
		g_set_error(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_INVALID_DATA,
			    "response truncated: received %" G_GSIZE_FORMAT " bytes",
			    actual_len);
		return FALSE;
	}

	/* success */
	return TRUE;
}

static gboolean
fu_system76_launch_device_command(FuSystem76LaunchDevice *self,
				  guint8 *data,
				  gsize len,
				  GError **error)
{
	const guint8 ep_out = 0x03;
	gsize actual_len = 0;
	FuSystem76LaunchDeviceHelper helper = {.data = data, .len = len};

	/* send command */
	if (!fu_usb_device_interrupt_transfer(FU_USB_DEVICE(self),
					      ep_out,
					      data,
					      len,
					      &actual_len,
					      SYSTEM76_LAUNCH_TIMEOUT,
					      NULL,
					      error)) {
		g_prefix_error_literal(error, "failed to send command: ");
		return FALSE;
	}
	if (actual_len < len) {
		g_set_error(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_INVALID_DATA,
			    "command truncated: sent %" G_GSIZE_FORMAT " bytes",
			    actual_len);
		return FALSE;
	}
	return fu_device_retry(FU_DEVICE(self),
			       fu_system76_launch_device_response_cb,
			       5,
			       &helper,
			       error);
}

static gboolean
fu_system76_launch_device_version_cb(FuDevice *device, gpointer user_data, GError **error)
{
	FuSystem76LaunchDevice *self = FU_SYSTEM76_LAUNCH_DEVICE(device);
	guint8 data[32] = {SYSTEM76_LAUNCH_CMD_VERSION, 0};
	g_autofree gchar *version = NULL;

	/* execute version command */
	if (!fu_system76_launch_device_command(self, data, sizeof(data), error)) {
		g_prefix_error_literal(error, "failed to execute version command: ");
		return FALSE;
	}

	version = g_strdup_printf("%s", &data[2]);
	fu_device_set_version(device, version);

	return TRUE;
}

static gboolean
fu_system76_launch_device_setup(FuDevice *device, GError **error)
{
	/* FuUsbDevice->setup */
	if (!FU_DEVICE_CLASS(fu_system76_launch_device_parent_class)->setup(device, error))
		return FALSE;

	/* set version */
	return fu_device_retry_full(device,
				    fu_system76_launch_device_version_cb,
				    5,
				    500,
				    NULL,
				    error);
}

static gboolean
fu_system76_launch_device_reset(FuSystem76LaunchDevice *self, guint8 *rc, GError **error)
{
	guint8 data[32] = {SYSTEM76_LAUNCH_CMD_RESET, 0};

	/* execute reset command */
	if (!fu_system76_launch_device_command(self, data, sizeof(data), error)) {
		g_prefix_error_literal(error, "failed to execute reset command: ");
		return FALSE;
	}

	*rc = data[1];
	return TRUE;
}

static gboolean
fu_system76_launch_device_security_set(FuSystem76LaunchDevice *self,
				       FuSystem76LaunchSecurityState state,
				       guint8 *rc,
				       GError **error)
{
	guint8 data[32] = {SYSTEM76_LAUNCH_CMD_SECURITY_SET, 0, state, 0};

	/* execute security set command */
	if (!fu_system76_launch_device_command(self, data, sizeof(data), error)) {
		g_prefix_error_literal(error, "failed to execute security set command: ");
		return FALSE;
	}

	*rc = data[1];
	return TRUE;
}

static gboolean
fu_system76_launch_device_detach(FuDevice *device, FuProgress *progress, GError **error)
{
	FuSystem76LaunchDevice *self = FU_SYSTEM76_LAUNCH_DEVICE(device);
	guint8 rc = 0x0;
	g_autoptr(FwupdRequest) request = fwupd_request_new();
	g_autoptr(GTimer) timer = g_timer_new();

	/* prompt for unlock if reset was blocked */
	if (!fu_system76_launch_device_reset(self, &rc, error))
		return FALSE;

	/* unlikely, but already unlocked */
	if (rc == 0) {
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG);
		return TRUE;
	}

	/* notify device of desire to unlock */
	if (!fu_system76_launch_device_security_set(
		self,
		FU_SYSTEM76_LAUNCH_SECURITY_STATE_PREPARE_UNLOCK,
		&rc,
		error))
		return FALSE;

	/* generate a message if not already set */
	if (fu_device_get_update_message(device) == NULL) {
		g_autofree gchar *msg = NULL;
		const gchar *unlock_keys;
		switch (fu_device_get_pid(device)) {
		case 0x0001: /* launch_1 */
			unlock_keys = "Fn+Esc";
			break;
		case 0x000B: /* thelio_io_2 */
			unlock_keys = "the Power button";
			break;
		default:
			unlock_keys = "Left Ctrl+Right Ctrl+Esc";
			break;
		}
		msg = g_strdup_printf(
		    "To ensure you have physical access, %s needs to be manually unlocked. "
		    "Please press %s to unlock and re-run the update.",
		    fu_device_get_name(device),
		    unlock_keys);
		fu_device_set_update_message(device, msg);
	}

	/* the user has to do something */
	fwupd_request_set_kind(request, FWUPD_REQUEST_KIND_IMMEDIATE);
	fwupd_request_set_id(request, FWUPD_REQUEST_ID_PRESS_UNLOCK);
	fwupd_request_set_message(request, fu_device_get_update_message(device));
	if (!fu_device_emit_request(device, request, progress, error))
		return FALSE;

	/* poll for the user-unlock */
	do {
		fu_device_sleep(device, 1000); /* ms */
		if (!fu_system76_launch_device_reset(self, &rc, error))
			return FALSE;
	} while (rc != 0 &&
		 g_timer_elapsed(timer, NULL) * 1000.f < FU_DEVICE_REMOVE_DELAY_USER_REPLUG);
	if (rc != 0) {
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_NEEDS_USER_ACTION,
				    fu_device_get_update_message(device));
		return FALSE;
	}

	/* success */
	fu_device_add_flag(device, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG);
	return TRUE;
}

static void
fu_system76_launch_device_set_progress(FuDevice *device, FuProgress *progress)
{
	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_add_step(progress, FWUPD_STATUS_DECOMPRESSING, 0, "prepare-fw");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 30, "detach");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_WRITE, 40, "write");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 5, "attach");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 25, "reload");
}

static void
fu_system76_launch_device_init(FuSystem76LaunchDevice *self)
{
	fu_device_set_remove_delay(FU_DEVICE(self), FU_DEVICE_REMOVE_DELAY_RE_ENUMERATE);
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_UPDATABLE);
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_UNSIGNED_PAYLOAD);
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_ADD_COUNTERPART_GUIDS);
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_REPLUG_MATCH_GUID);
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_ADD_INSTANCE_ID_REV);
	fu_device_add_request_flag(FU_DEVICE(self), FWUPD_REQUEST_FLAG_NON_GENERIC_MESSAGE);
	fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_PLAIN);
	fu_device_add_protocol(FU_DEVICE(self), "com.microsoft.uf2");
	fu_device_add_protocol(FU_DEVICE(self), "org.usb.dfu");
	fu_device_retry_set_delay(FU_DEVICE(self), 100);
	fu_usb_device_add_interface(FU_USB_DEVICE(self), 0x01);
}

static void
fu_system76_launch_device_class_init(FuSystem76LaunchDeviceClass *klass)
{
	FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
	device_class->setup = fu_system76_launch_device_setup;
	device_class->detach = fu_system76_launch_device_detach;
	device_class->set_progress = fu_system76_launch_device_set_progress;
}