File: fu-android-boot-device.c

package info (click to toggle)
fwupd 2.0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,340 kB
  • sloc: ansic: 274,440; python: 11,468; xml: 9,432; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (397 lines) | stat: -rw-r--r-- 12,248 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright 2022 Dylan Van Assche <me@dylanvanassche.be>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <fcntl.h>
#include <string.h>

#include "fu-android-boot-device.h"

#define ANDROID_BOOT_UNKNOWN_VERSION "0.0.0"
#define ANDROID_BOOT_SECTOR_SIZE     512

struct _FuAndroidBootDevice {
	FuBlockPartition parent_instance;
	gchar *boot_slot;
	guint64 max_size;
};

G_DEFINE_TYPE(FuAndroidBootDevice, fu_android_boot_device, FU_TYPE_BLOCK_PARTITION)

static void
fu_android_boot_device_to_string(FuDevice *device, guint idt, GString *str)
{
	FuAndroidBootDevice *self = FU_ANDROID_BOOT_DEVICE(device);

	fwupd_codec_string_append(str, idt, "BootSlot", self->boot_slot);
	fwupd_codec_string_append_hex(str, idt, "MaxSize", self->max_size);
}

static gboolean
fu_android_boot_device_probe(FuDevice *device, GError **error)
{
	FuAndroidBootDevice *self = FU_ANDROID_BOOT_DEVICE(device);
	guint64 sectors = 0;
	guint64 size = 0;
	g_autofree gchar *prop_size = NULL;
	g_autoptr(GHashTable) cmdline = NULL;

	/* FuUdevDevice->probe */
	if (!FU_DEVICE_CLASS(fu_android_boot_device_parent_class)->probe(device, error))
		return FALSE;

	/* get kernel cmdline */
	cmdline = fu_kernel_get_cmdline(error);
	if (cmdline == NULL)
		return FALSE;

	/* set the physical ID */
	if (!fu_udev_device_set_physical_id(FU_UDEV_DEVICE(device), "block", error))
		return FALSE;

	/* extract boot slot if available */
	self->boot_slot = g_strdup(g_hash_table_lookup(cmdline, "androidboot.slot_suffix"));

	/* set max firmware size, required to avoid writing firmware bigger than partition */
	prop_size = fu_udev_device_read_sysfs(FU_UDEV_DEVICE(device),
					      "size",
					      FU_UDEV_DEVICE_ATTR_READ_TIMEOUT_DEFAULT,
					      NULL);
	if (prop_size == NULL) {
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_NOT_SUPPORTED,
				    "device does not expose its size");
		return FALSE;
	}
	if (!fu_strtoull(prop_size, &sectors, 0x0, G_MAXUINT64, FU_INTEGER_BASE_AUTO, error))
		return FALSE;
	size = sectors * ANDROID_BOOT_SECTOR_SIZE;
	self->max_size = size;

	/* extract serial number and set it */
	fu_device_set_serial(device, g_hash_table_lookup(cmdline, "androidboot.serialno"));

	/* set the firmware maximum size based on partition size or from quirk */
	fu_device_set_firmware_size_max(device, self->max_size);

	return TRUE;
}

static gboolean
fu_android_boot_device_setup(FuDevice *device, GError **error)
{
	FuAndroidBootDevice *self = FU_ANDROID_BOOT_DEVICE(device);
	const gchar *fs_label;

	/* FuBlockPartition->setup() */
	if (!FU_DEVICE_CLASS(fu_android_boot_device_parent_class)->setup(device, error))
		return FALSE;

	/* extract label and check if it matches boot slot */
	fs_label = fu_block_partition_get_fs_label(FU_BLOCK_PARTITION(self));
	if (fs_label != NULL) {
		fu_device_set_name(device, fs_label);

		/* if device has A/B partitioning, compare slot to only expose partitions in-use */
		if (self->boot_slot != NULL && !g_str_has_suffix(fs_label, self->boot_slot)) {
			g_set_error_literal(error,
					    FWUPD_ERROR,
					    FWUPD_ERROR_NOT_SUPPORTED,
					    "device is on a different bootslot");
			return FALSE;
		}
	}

	/*
	 * Some devices don't have unique TYPE UUIDs, add the partition label to make them truly
	 * unique Devices have a fixed partition scheme anyway because they originally have Android
	 * which has such requirements.
	 */
	if (fu_block_partition_get_fs_uuid(FU_BLOCK_PARTITION(self)) == NULL) {
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_NOT_SUPPORTED,
				    "no partition UUID");
		return FALSE;
	}
	fu_device_add_instance_strsafe(device,
				       "UUID",
				       fu_block_partition_get_fs_uuid(FU_BLOCK_PARTITION(self)));
	fu_device_add_instance_strsafe(device, "LABEL", fs_label);
	fu_device_add_instance_strsafe(device, "SLOT", self->boot_slot);

	/* GUID based on UUID / UUID, label / UUID, label, slot */
	if (!fu_device_build_instance_id(device, error, "DRIVE", "UUID", NULL))
		return FALSE;
	fu_device_build_instance_id(device, NULL, "DRIVE", "UUID", "LABEL", NULL);
	fu_device_build_instance_id(device, NULL, "DRIVE", "UUID", "LABEL", "SLOT", NULL);

	/* quirks will have matched now */
	if (!fu_device_has_flag(device, FWUPD_DEVICE_FLAG_UPDATABLE)) {
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_NOT_SUPPORTED,
				    "device is not updatable");
		return FALSE;
	}

	/* success */
	return TRUE;
}

static gboolean
fu_android_boot_device_open(FuDevice *device, GError **error)
{
	g_autoptr(GError) error_local = NULL;

	/* FuUdevDevice->open */
	if (!FU_DEVICE_CLASS(fu_android_boot_device_parent_class)->open(device, &error_local)) {
		if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_PERMISSION_DENIED)) {
			g_set_error_literal(error,
					    FWUPD_ERROR,
					    FWUPD_ERROR_NOT_SUPPORTED,
					    error_local->message);
			return FALSE;
		}
		g_propagate_error(error, g_steal_pointer(&error_local));
		return FALSE;
	}

	return TRUE;
}

static gboolean
fu_android_boot_device_write(FuAndroidBootDevice *self,
			     FuChunkArray *chunks,
			     FuProgress *progress,
			     GError **error)
{
	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_set_steps(progress, fu_chunk_array_length(chunks));

	/* rewind */
	if (!fu_udev_device_seek(FU_UDEV_DEVICE(self), 0x0, error)) {
		g_prefix_error_literal(error, "failed to rewind: ");
		return FALSE;
	}

	/* write each chunk */
	for (guint i = 0; i < fu_chunk_array_length(chunks); i++) {
		g_autoptr(FuChunk) chk = NULL;

		/* prepare chunk */
		chk = fu_chunk_array_index(chunks, i, error);
		if (chk == NULL)
			return FALSE;
		if (!fu_udev_device_pwrite(FU_UDEV_DEVICE(self),
					   fu_chunk_get_address(chk),
					   fu_chunk_get_data(chk),
					   fu_chunk_get_data_sz(chk),
					   error)) {
			g_prefix_error(error,
				       "failed to write @0x%x: ",
				       (guint)fu_chunk_get_address(chk));
			return FALSE;
		}
		fu_progress_step_done(progress);
	}

	return TRUE;
}

static gboolean
fu_android_boot_device_erase(FuAndroidBootDevice *self, FuProgress *progress, GError **error)
{
	g_autoptr(FuChunkArray) chunks = NULL;
	gsize bufsz = fu_device_get_firmware_size_max(FU_DEVICE(self));
	g_autofree guint8 *buf = g_malloc0(bufsz);
	g_autoptr(GBytes) fw = g_bytes_new_take(g_steal_pointer(&buf), bufsz);

	chunks = fu_chunk_array_new_from_bytes(fw,
					       FU_CHUNK_ADDR_OFFSET_NONE,
					       FU_CHUNK_PAGESZ_NONE,
					       10 * 1024);
	return fu_android_boot_device_write(self, chunks, progress, error);
}

static gboolean
fu_android_boot_device_verify(FuAndroidBootDevice *self,
			      FuChunkArray *chunks,
			      FuProgress *progress,
			      GError **error)
{
	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_set_steps(progress, fu_chunk_array_length(chunks));

	/* verify each chunk */
	for (guint i = 0; i < fu_chunk_array_length(chunks); i++) {
		g_autofree guint8 *buf = NULL;
		g_autoptr(FuChunk) chk = NULL;
		g_autoptr(GBytes) blob1 = NULL;
		g_autoptr(GBytes) blob2 = NULL;

		/* prepare chunk */
		chk = fu_chunk_array_index(chunks, i, error);
		if (chk == NULL)
			return FALSE;

		buf = g_malloc0(fu_chunk_get_data_sz(chk));
		if (!fu_udev_device_pread(FU_UDEV_DEVICE(self),
					  fu_chunk_get_address(chk),
					  buf,
					  fu_chunk_get_data_sz(chk),
					  error)) {
			g_prefix_error(error,
				       "failed to read @0x%x: ",
				       (guint)fu_chunk_get_address(chk));
			return FALSE;
		}
		blob1 = fu_chunk_get_bytes(chk);
		blob2 = g_bytes_new_static(buf, fu_chunk_get_data_sz(chk));
		if (!fu_bytes_compare(blob1, blob2, error)) {
			g_prefix_error(error,
				       "failed to verify @0x%x: ",
				       (guint)fu_chunk_get_address(chk));
			return FALSE;
		}
		fu_progress_step_done(progress);
	}

	return TRUE;
}

static gboolean
fu_android_boot_device_write_firmware(FuDevice *device,
				      FuFirmware *firmware,
				      FuProgress *progress,
				      FwupdInstallFlags flags,
				      GError **error)
{
	FuAndroidBootDevice *self = FU_ANDROID_BOOT_DEVICE(device);
	g_autoptr(GInputStream) stream = NULL;
	g_autoptr(FuChunkArray) chunks = NULL;

	/* get data to write */
	stream = fu_firmware_get_stream(firmware, error);
	if (stream == NULL)
		return FALSE;
	chunks = fu_chunk_array_new_from_stream(stream,
						FU_CHUNK_ADDR_OFFSET_NONE,
						FU_CHUNK_PAGESZ_NONE,
						10 * 1024,
						error);
	if (chunks == NULL)
		return FALSE;

	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_ERASE, 72, NULL);
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_WRITE, 20, NULL);
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_VERIFY, 7, NULL);

	/* erase, write, verify */
	if (!fu_android_boot_device_erase(self, fu_progress_get_child(progress), error))
		return FALSE;
	fu_progress_step_done(progress);

	if (!fu_android_boot_device_write(self, chunks, fu_progress_get_child(progress), error))
		return FALSE;
	fu_progress_step_done(progress);

	if (!fu_android_boot_device_verify(self, chunks, fu_progress_get_child(progress), error))
		return FALSE;
	fu_progress_step_done(progress);

	return TRUE;
}

static gboolean
fu_android_boot_device_set_quirk_kv(FuDevice *device,
				    const gchar *key,
				    const gchar *value,
				    GError **error)
{
	FuAndroidBootDevice *self = FU_ANDROID_BOOT_DEVICE(device);

	/* load from quirks */
	if (g_strcmp0(key, "AndroidBootVersionProperty") == 0) {
		g_autoptr(GHashTable) cmdline = NULL;
		const gchar *version = NULL;

		cmdline = fu_kernel_get_cmdline(error);
		if (cmdline == NULL)
			return FALSE;

		version = g_hash_table_lookup(cmdline, value);
		if (version != NULL)
			fu_device_set_version(device, version);
		return TRUE;
	}

	if (g_strcmp0(key, "AndroidBootPartitionMaxSize") == 0) {
		guint64 size = 0;

		if (!fu_strtoull(value, &size, 0, G_MAXUINT32, FU_INTEGER_BASE_AUTO, error))
			return FALSE;
		self->max_size = size;
		return TRUE;
	}

	/* failed */
	g_set_error_literal(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_NOT_SUPPORTED,
			    "quirk key not supported");
	return FALSE;
}

static void
fu_android_boot_device_finalize(GObject *obj)
{
	FuAndroidBootDevice *self = FU_ANDROID_BOOT_DEVICE(obj);
	G_OBJECT_CLASS(fu_android_boot_device_parent_class)->finalize(obj);
	g_free(self->boot_slot);
}

static void
fu_android_boot_device_init(FuAndroidBootDevice *self)
{
	fu_device_set_summary(FU_DEVICE(self), "Android Bootloader");
	fu_device_add_protocol(FU_DEVICE(self), "com.google.android_boot");
	fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_PLAIN);
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_REQUIRE_AC);
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_INTERNAL);
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_NEEDS_REBOOT);
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_CAN_VERIFY_IMAGE);
	fu_udev_device_add_open_flag(FU_UDEV_DEVICE(self), FU_IO_CHANNEL_OPEN_FLAG_READ);
	fu_udev_device_add_open_flag(FU_UDEV_DEVICE(self), FU_IO_CHANNEL_OPEN_FLAG_WRITE);
	fu_udev_device_add_open_flag(FU_UDEV_DEVICE(self), FU_IO_CHANNEL_OPEN_FLAG_SYNC);
	fu_device_add_icon(FU_DEVICE(self), FU_DEVICE_ICON_COMPUTER);

	/*
	 * Fallback for ABL without version reporting, fwupd will always provide an upgrade in this
	 * case. Once upgraded, the version reporting will be available and the update notification
	 * will disappear. If version reporting is available, the reported version is set.
	 */
	fu_device_set_version(FU_DEVICE(self), ANDROID_BOOT_UNKNOWN_VERSION);
	fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_TRIPLET);
}

static void
fu_android_boot_device_class_init(FuAndroidBootDeviceClass *klass)
{
	FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
	GObjectClass *object_class = G_OBJECT_CLASS(klass);

	object_class->finalize = fu_android_boot_device_finalize;
	device_class->probe = fu_android_boot_device_probe;
	device_class->setup = fu_android_boot_device_setup;
	device_class->open = fu_android_boot_device_open;
	device_class->write_firmware = fu_android_boot_device_write_firmware;
	device_class->to_string = fu_android_boot_device_to_string;
	device_class->set_quirk_kv = fu_android_boot_device_set_quirk_kv;
}