/src/fwupd/libfwupdplugin/fu-context.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2021 Richard Hughes <richard@hughsie.com> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | 0 | #define G_LOG_DOMAIN "FuContext" |
8 | | |
9 | | #include "config.h" |
10 | | |
11 | | #ifdef HAVE_CPUID_H |
12 | | #include <cpuid.h> |
13 | | #endif |
14 | | |
15 | | #include "fu-common-private.h" |
16 | | #include "fu-config-private.h" |
17 | | #include "fu-context-helper.h" |
18 | | #include "fu-context-private.h" |
19 | | #include "fu-dummy-efivars.h" |
20 | | #include "fu-efi-device-path-list.h" |
21 | | #include "fu-efi-file-path-device-path.h" |
22 | | #include "fu-efi-hard-drive-device-path.h" |
23 | | #include "fu-fdt-firmware.h" |
24 | | #include "fu-hwids-private.h" |
25 | | #include "fu-path-store.h" |
26 | | #include "fu-path.h" |
27 | | #include "fu-pefile-firmware.h" |
28 | | #include "fu-string.h" |
29 | | #include "fu-volume-locker.h" |
30 | | #include "fu-volume-private.h" |
31 | | |
32 | | /** |
33 | | * FuContext: |
34 | | * |
35 | | * A context that represents the shared system state. This object is shared |
36 | | * between the engine, the plugins and the devices. |
37 | | */ |
38 | | |
39 | | typedef struct { |
40 | | GMainContext *main_ctx; |
41 | | FuContextFlags flags; |
42 | | FuPathStore *pstore; |
43 | | FuHwids *hwids; |
44 | | FuConfig *config; |
45 | | FuSmbios *smbios; |
46 | | FuSmbiosChassisKind chassis_kind; |
47 | | FuQuirks *quirks; |
48 | | FuEfivars *efivars; |
49 | | GPtrArray *backends; |
50 | | GPtrArray *host_bkcs; /* utf8 */ |
51 | | GHashTable *runtime_versions; |
52 | | GHashTable *compile_versions; |
53 | | GHashTable *udev_subsystems; /* utf8:GPtrArray */ |
54 | | GPtrArray *esp_volumes; |
55 | | GHashTable *firmware_gtypes; /* utf8:GType */ |
56 | | GHashTable *hwid_flags; /* str: */ |
57 | | FuPowerState power_state; |
58 | | FuLidState lid_state; |
59 | | FuDisplayState display_state; |
60 | | guint battery_level; |
61 | | guint battery_threshold; |
62 | | GPtrArray *bios_settings; |
63 | | FuFirmware *fdt; /* optional */ |
64 | | gchar *esp_location; |
65 | | FuCpuVendor cpu_vendor; |
66 | | } FuContextPrivate; |
67 | | |
68 | | enum { |
69 | | SIGNAL_SECURITY_CHANGED, |
70 | | SIGNAL_HOUSEKEEPING, |
71 | | SIGNAL_ESP_WRITE, |
72 | | SIGNAL_LAST, |
73 | | }; |
74 | | |
75 | | enum { |
76 | | PROP_0, |
77 | | PROP_POWER_STATE, |
78 | | PROP_LID_STATE, |
79 | | PROP_DISPLAY_STATE, |
80 | | PROP_BATTERY_LEVEL, |
81 | | PROP_BATTERY_THRESHOLD, |
82 | | PROP_FLAGS, |
83 | | PROP_LAST |
84 | | }; |
85 | | |
86 | | static guint signals[SIGNAL_LAST] = {0}; |
87 | | |
88 | 803 | G_DEFINE_TYPE_WITH_PRIVATE(FuContext, fu_context, G_TYPE_OBJECT) |
89 | 803 | |
90 | 803 | #define GET_PRIVATE(o) (fu_context_get_instance_private(o)) |
91 | | |
92 | 0 | #define FU_CONTEXT_HOST_BKCS_MAX 100 |
93 | | |
94 | | static gboolean |
95 | | fu_context_ensure_smbios_uefi_enabled(FuContext *self, GError **error) |
96 | 0 | { |
97 | 0 | GBytes *bios_blob; |
98 | 0 | g_autoptr(FuStructSmbiosBiosInformation) st = NULL; |
99 | 0 | g_autoptr(GPtrArray) bios_tables = NULL; |
100 | |
|
101 | 0 | bios_tables = fu_context_get_smbios_data(self, 0, FU_SMBIOS_STRUCTURE_LENGTH_ANY, NULL); |
102 | 0 | if (bios_tables == NULL) { |
103 | 0 | const gchar *tmp = g_getenv("FWUPD_DELL_FAKE_SMBIOS"); |
104 | 0 | if (tmp != NULL) |
105 | 0 | return TRUE; |
106 | 0 | g_set_error_literal(error, |
107 | 0 | FWUPD_ERROR, |
108 | 0 | FWUPD_ERROR_NOT_SUPPORTED, |
109 | 0 | "SMBIOS not supported"); |
110 | 0 | return FALSE; |
111 | 0 | } |
112 | 0 | bios_blob = g_ptr_array_index(bios_tables, 0); |
113 | |
|
114 | 0 | st = fu_struct_smbios_bios_information_parse_bytes(bios_blob, 0x0, error); |
115 | 0 | if (st == NULL) |
116 | 0 | return FALSE; |
117 | 0 | if (fu_struct_smbios_bios_information_get_length(st) < 0x14) { |
118 | 0 | g_set_error_literal(error, |
119 | 0 | FWUPD_ERROR, |
120 | 0 | FWUPD_ERROR_NOT_SUPPORTED, |
121 | 0 | "SMBIOS 2.3 not supported"); |
122 | 0 | return FALSE; |
123 | 0 | } |
124 | 0 | if ((fu_struct_smbios_bios_information_get_characteristics_ext(st) & |
125 | 0 | FU_SMBIOS_BIOS_CHARACTERISTICS_EXT_UEFI_SPECIFICATION_SUPPORTED) == 0) { |
126 | 0 | g_set_error_literal(error, |
127 | 0 | FWUPD_ERROR, |
128 | 0 | FWUPD_ERROR_NOT_SUPPORTED, |
129 | 0 | "System does not support UEFI mode"); |
130 | 0 | return FALSE; |
131 | 0 | } |
132 | | |
133 | | /* success */ |
134 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_SMBIOS_UEFI_ENABLED); |
135 | 0 | return TRUE; |
136 | 0 | } |
137 | | |
138 | | static GFile * |
139 | | fu_context_get_fdt_file(FuContext *self, GError **error) |
140 | 0 | { |
141 | 0 | g_autofree gchar *fdtfn_local = NULL; |
142 | 0 | g_autofree gchar *fdtfn_sys = NULL; |
143 | | |
144 | | /* look for override first, fall back to system value */ |
145 | 0 | fdtfn_local = fu_context_build_filename(self, |
146 | 0 | error, |
147 | 0 | FU_PATH_KIND_LOCALSTATEDIR_PKG, |
148 | 0 | "system.dtb", |
149 | 0 | NULL); |
150 | 0 | if (fdtfn_local == NULL) |
151 | 0 | return NULL; |
152 | 0 | if (g_file_test(fdtfn_local, G_FILE_TEST_EXISTS)) |
153 | 0 | return g_file_new_for_path(fdtfn_local); |
154 | | |
155 | | /* actual hardware value */ |
156 | 0 | fdtfn_sys = fu_context_build_filename(self, error, FU_PATH_KIND_SYSFSDIR_FW, "fdt", NULL); |
157 | 0 | if (fdtfn_sys == NULL) |
158 | 0 | return NULL; |
159 | 0 | if (g_file_test(fdtfn_sys, G_FILE_TEST_EXISTS)) |
160 | 0 | return g_file_new_for_path(fdtfn_sys); |
161 | | |
162 | | /* failed */ |
163 | 0 | g_set_error(error, |
164 | 0 | FWUPD_ERROR, |
165 | 0 | FWUPD_ERROR_NOT_SUPPORTED, |
166 | 0 | "cannot find %s or override %s", |
167 | 0 | fdtfn_sys, |
168 | 0 | fdtfn_local); |
169 | 0 | return NULL; |
170 | 0 | } |
171 | | |
172 | | /** |
173 | | * fu_context_get_fdt: |
174 | | * @self: a #FuContext |
175 | | * @error: (nullable): optional return location for an error |
176 | | * |
177 | | * Gets and parses the system FDT, aka. the Flat Device Tree. |
178 | | * |
179 | | * The results are cached internally to the context, and subsequent calls to this function |
180 | | * returns the pre-parsed object. |
181 | | * |
182 | | * Returns: (transfer full): a #FuFdtFirmware, or %NULL |
183 | | **/ |
184 | | FuFirmware * |
185 | | fu_context_get_fdt(FuContext *self, GError **error) |
186 | 0 | { |
187 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
188 | |
|
189 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
190 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
191 | | |
192 | | /* load if not already parsed */ |
193 | 0 | if (priv->fdt == NULL) { |
194 | 0 | g_autoptr(FuFirmware) fdt_tmp = fu_fdt_firmware_new(); |
195 | 0 | g_autoptr(GFile) file = fu_context_get_fdt_file(self, error); |
196 | 0 | if (file == NULL) |
197 | 0 | return NULL; |
198 | 0 | if (!fu_firmware_parse_file(fdt_tmp, |
199 | 0 | file, |
200 | 0 | FU_FIRMWARE_PARSE_FLAG_NO_SEARCH, |
201 | 0 | error)) { |
202 | 0 | g_prefix_error_literal(error, "failed to parse FDT: "); |
203 | 0 | return NULL; |
204 | 0 | } |
205 | 0 | priv->fdt = g_steal_pointer(&fdt_tmp); |
206 | 0 | } |
207 | | |
208 | | /* success */ |
209 | 0 | return g_object_ref(priv->fdt); |
210 | 0 | } |
211 | | |
212 | | /** |
213 | | * fu_context_get_efivars: |
214 | | * @self: a #FuContext |
215 | | * |
216 | | * Gets the EFI variable store. |
217 | | * |
218 | | * Returns: (transfer none): a #FuEfivars |
219 | | **/ |
220 | | FuEfivars * |
221 | | fu_context_get_efivars(FuContext *self) |
222 | 0 | { |
223 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
224 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
225 | 0 | return priv->efivars; |
226 | 0 | } |
227 | | |
228 | | /** |
229 | | * fu_context_efivars_check_free_space: |
230 | | * @self: a #FuContext |
231 | | * @count: size in bytes |
232 | | * @error: (nullable): optional return location for an error |
233 | | * |
234 | | * Checks for a given amount of free space in the EFI NVRAM variable store. |
235 | | * |
236 | | * Returns: %TRUE for success |
237 | | **/ |
238 | | gboolean |
239 | | fu_context_efivars_check_free_space(FuContext *self, gsize count, GError **error) |
240 | 0 | { |
241 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
242 | 0 | guint64 total; |
243 | |
|
244 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
245 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
246 | | |
247 | | /* escape hatch */ |
248 | 0 | if (fu_context_has_flag(self, FU_CONTEXT_FLAG_IGNORE_EFIVARS_FREE_SPACE)) |
249 | 0 | return TRUE; |
250 | | |
251 | 0 | total = fu_efivars_space_free(priv->efivars, error); |
252 | 0 | if (total == G_MAXUINT64) |
253 | 0 | return FALSE; |
254 | 0 | if (total < count) { |
255 | 0 | g_autofree gchar *countstr = g_format_size(count); |
256 | 0 | g_autofree gchar *totalstr = g_format_size(total); |
257 | 0 | g_set_error(error, |
258 | 0 | FWUPD_ERROR, |
259 | 0 | FWUPD_ERROR_BROKEN_SYSTEM, |
260 | 0 | "Not enough efivarfs space, requested %s and got %s", |
261 | 0 | countstr, |
262 | 0 | totalstr); |
263 | 0 | return FALSE; |
264 | 0 | } |
265 | 0 | return TRUE; |
266 | 0 | } |
267 | | |
268 | | /** |
269 | | * fu_context_get_smbios: |
270 | | * @self: a #FuContext |
271 | | * |
272 | | * Gets the SMBIOS store. |
273 | | * |
274 | | * Returns: (transfer none): a #FuSmbios |
275 | | **/ |
276 | | FuSmbios * |
277 | | fu_context_get_smbios(FuContext *self) |
278 | 0 | { |
279 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
280 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
281 | 0 | return priv->smbios; |
282 | 0 | } |
283 | | |
284 | | /** |
285 | | * fu_context_set_smbios: |
286 | | * @self: a #FuContext |
287 | | * @smbios: a #FuSmbios |
288 | | * |
289 | | * Sets the SMBIOS store. This is only required by self test code. |
290 | | **/ |
291 | | void |
292 | | fu_context_set_smbios(FuContext *self, FuSmbios *smbios) |
293 | 0 | { |
294 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
295 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
296 | 0 | g_return_if_fail(FU_IS_SMBIOS(smbios)); |
297 | 0 | g_set_object(&priv->smbios, smbios); |
298 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_LOADED_HWINFO); |
299 | 0 | } |
300 | | |
301 | | /** |
302 | | * fu_context_get_main_context: |
303 | | * @self: a #FuContext |
304 | | * |
305 | | * Gets the main context. |
306 | | * |
307 | | * Returns: (transfer none): a #GMainContextm or %NULL if unset |
308 | | **/ |
309 | | GMainContext * |
310 | | fu_context_get_main_context(FuContext *self) |
311 | 0 | { |
312 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
313 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
314 | 0 | return priv->main_ctx; |
315 | 0 | } |
316 | | |
317 | | /** |
318 | | * fu_context_add_timeout_seconds: |
319 | | * @self: a #FuContext |
320 | | * @interval: timeout interval in seconds |
321 | | * @function: (scope call): function to call |
322 | | * @data: data to pass to @function |
323 | | * |
324 | | * Creates a new timeout source and attaches it to the main context. |
325 | | * |
326 | | * Returns: (transfer full): the #GSource, which must be removed with g_source_unref() |
327 | | **/ |
328 | | GSource * |
329 | | fu_context_add_timeout_seconds(FuContext *self, guint interval, GSourceFunc function, gpointer data) |
330 | 0 | { |
331 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
332 | 0 | g_autoptr(GSource) source = g_timeout_source_new_seconds(interval); |
333 | |
|
334 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
335 | 0 | g_return_val_if_fail(interval != 0, NULL); |
336 | 0 | g_return_val_if_fail(function != NULL, NULL); |
337 | | |
338 | 0 | g_source_set_callback(source, function, data, NULL); |
339 | 0 | g_source_attach(source, priv->main_ctx); |
340 | 0 | return g_steal_pointer(&source); |
341 | 0 | } |
342 | | |
343 | | /** |
344 | | * fu_context_add_timeout: |
345 | | * @self: a #FuContext |
346 | | * @interval_ms: timeout interval in ms |
347 | | * @function: (scope call): function to call |
348 | | * @data: data to pass to @function |
349 | | * |
350 | | * Creates a new timeout source and attaches it to the main context. |
351 | | * |
352 | | * Returns: (transfer full): the #GSource, which must be removed with g_source_unref() |
353 | | **/ |
354 | | GSource * |
355 | | fu_context_add_timeout(FuContext *self, guint interval_ms, GSourceFunc function, gpointer data) |
356 | 0 | { |
357 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
358 | 0 | g_autoptr(GSource) source = g_timeout_source_new(interval_ms); |
359 | |
|
360 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
361 | 0 | g_return_val_if_fail(interval_ms != 0, NULL); |
362 | 0 | g_return_val_if_fail(function != NULL, NULL); |
363 | | |
364 | 0 | g_source_set_callback(source, function, data, NULL); |
365 | 0 | g_source_attach(source, priv->main_ctx); |
366 | 0 | return g_steal_pointer(&source); |
367 | 0 | } |
368 | | |
369 | | /** |
370 | | * fu_context_set_main_context: |
371 | | * @self: a #FuContext |
372 | | * @main_ctx: (nullable): a #GMainContext |
373 | | * |
374 | | * Sets or clears the main context. This is only required by fwupdtool. |
375 | | **/ |
376 | | void |
377 | | fu_context_set_main_context(FuContext *self, GMainContext *main_ctx) |
378 | 0 | { |
379 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
380 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
381 | 0 | if (main_ctx == priv->main_ctx) |
382 | 0 | return; |
383 | 0 | g_clear_pointer(&priv->main_ctx, g_main_context_unref); |
384 | 0 | if (main_ctx != NULL) |
385 | 0 | priv->main_ctx = g_main_context_ref(main_ctx); |
386 | 0 | } |
387 | | |
388 | | /** |
389 | | * fu_context_get_hwids: |
390 | | * @self: a #FuContext |
391 | | * |
392 | | * Gets the HWIDs store. |
393 | | * |
394 | | * Returns: (transfer none): a #FuHwids |
395 | | **/ |
396 | | FuHwids * |
397 | | fu_context_get_hwids(FuContext *self) |
398 | 0 | { |
399 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
400 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
401 | 0 | return priv->hwids; |
402 | 0 | } |
403 | | |
404 | | /** |
405 | | * fu_context_get_config: |
406 | | * @self: a #FuContext |
407 | | * |
408 | | * Gets the system config. |
409 | | * |
410 | | * Returns: (transfer none): a #FuHwids |
411 | | **/ |
412 | | FuConfig * |
413 | | fu_context_get_config(FuContext *self) |
414 | 0 | { |
415 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
416 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
417 | 0 | return priv->config; |
418 | 0 | } |
419 | | |
420 | | /** |
421 | | * fu_context_get_config_str: |
422 | | * @self: a #FuContext |
423 | | * @key: (not nullable): a settings key |
424 | | * |
425 | | * Return the value of a config key, falling back to the default value if missing. |
426 | | * |
427 | | * Returns: (transfer full) (nullable): string value |
428 | | **/ |
429 | | gchar * |
430 | | fu_context_get_config_str(FuContext *self, const gchar *key) |
431 | 0 | { |
432 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
433 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
434 | 0 | g_return_val_if_fail(key != NULL, NULL); |
435 | 0 | return fu_config_get_value(priv->config, "fwupd", key); |
436 | 0 | } |
437 | | |
438 | | /** |
439 | | * fu_context_get_config_strv: |
440 | | * @self: a #FuContext |
441 | | * @key: (not nullable): a settings key |
442 | | * |
443 | | * Return the value of a config key, falling back to the default value if missing. |
444 | | * |
445 | | * Returns: (transfer full) (nullable): string array value |
446 | | **/ |
447 | | gchar ** |
448 | | fu_context_get_config_strv(FuContext *self, const gchar *key) |
449 | 0 | { |
450 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
451 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
452 | 0 | g_return_val_if_fail(key != NULL, NULL); |
453 | 0 | return fu_config_get_value_strv(priv->config, "fwupd", key); |
454 | 0 | } |
455 | | |
456 | | /** |
457 | | * fu_context_get_config_bool: |
458 | | * @self: a #FuContext |
459 | | * @key: (not nullable): a settings key |
460 | | * |
461 | | * Return the value of a config key, falling back to the default value if missing. |
462 | | * |
463 | | * Returns: boolean value |
464 | | **/ |
465 | | gboolean |
466 | | fu_context_get_config_bool(FuContext *self, const gchar *key) |
467 | 0 | { |
468 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
469 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
470 | 0 | g_return_val_if_fail(key != NULL, FALSE); |
471 | 0 | return fu_config_get_value_bool(priv->config, "fwupd", key); |
472 | 0 | } |
473 | | |
474 | | /** |
475 | | * fu_context_get_config_u64: |
476 | | * @self: a #FuContext |
477 | | * @key: a settings key |
478 | | * |
479 | | * Return the value of a config key, falling back to the default value if missing. |
480 | | * |
481 | | * Returns: integer key value |
482 | | **/ |
483 | | guint64 |
484 | | fu_context_get_config_u64(FuContext *self, const gchar *key) |
485 | 0 | { |
486 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
487 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), G_MAXUINT64); |
488 | 0 | g_return_val_if_fail(key != NULL, G_MAXUINT64); |
489 | 0 | return fu_config_get_value_u64(priv->config, "fwupd", key); |
490 | 0 | } |
491 | | |
492 | | /** |
493 | | * fu_context_get_smbios_string: |
494 | | * @self: a #FuContext |
495 | | * @type: a SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS |
496 | | * @length: expected length of the structure, or %FU_SMBIOS_STRUCTURE_LENGTH_ANY |
497 | | * @offset: a SMBIOS offset |
498 | | * @error: (nullable): optional return location for an error |
499 | | * |
500 | | * Gets a hardware SMBIOS string. |
501 | | * |
502 | | * The @type and @offset can be referenced from the DMTF SMBIOS specification: |
503 | | * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf |
504 | | * |
505 | | * Returns: a string, or %NULL |
506 | | **/ |
507 | | const gchar * |
508 | | fu_context_get_smbios_string(FuContext *self, |
509 | | guint8 type, |
510 | | guint8 length, |
511 | | guint8 offset, |
512 | | GError **error) |
513 | 0 | { |
514 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
515 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
516 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
517 | 0 | if (!fu_context_has_flag(self, FU_CONTEXT_FLAG_LOADED_HWINFO)) { |
518 | 0 | g_critical("cannot use SMBIOS before calling ->load_hwinfo()"); |
519 | 0 | return NULL; |
520 | 0 | } |
521 | 0 | return fu_smbios_get_string(priv->smbios, type, length, offset, error); |
522 | 0 | } |
523 | | |
524 | | /** |
525 | | * fu_context_get_smbios_data: |
526 | | * @self: a #FuContext |
527 | | * @type: a SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS |
528 | | * @length: expected length of the structure, or %FU_SMBIOS_STRUCTURE_LENGTH_ANY |
529 | | * @error: (nullable): optional return location for an error |
530 | | * |
531 | | * Gets all hardware SMBIOS data for a specific type. |
532 | | * |
533 | | * Returns: (transfer container) (element-type GBytes): a #GBytes, or %NULL if not found |
534 | | **/ |
535 | | GPtrArray * |
536 | | fu_context_get_smbios_data(FuContext *self, guint8 type, guint8 length, GError **error) |
537 | 0 | { |
538 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
539 | |
|
540 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
541 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
542 | | |
543 | | /* must be valid and non-zero length */ |
544 | 0 | if (!fu_context_has_flag(self, FU_CONTEXT_FLAG_LOADED_HWINFO)) { |
545 | 0 | g_critical("cannot use SMBIOS before calling ->load_hwinfo()"); |
546 | 0 | g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "no data"); |
547 | 0 | return NULL; |
548 | 0 | } |
549 | 0 | return fu_smbios_get_data(priv->smbios, type, length, error); |
550 | 0 | } |
551 | | |
552 | | /** |
553 | | * fu_context_get_smbios_integer: |
554 | | * @self: a #FuContext |
555 | | * @type: a structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS |
556 | | * @length: expected length of the structure, or %FU_SMBIOS_STRUCTURE_LENGTH_ANY |
557 | | * @offset: a structure offset |
558 | | * @error: (nullable): optional return location for an error |
559 | | * |
560 | | * Reads an integer value from the SMBIOS string table of a specific structure. |
561 | | * |
562 | | * The @type and @offset can be referenced from the DMTF SMBIOS specification: |
563 | | * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf |
564 | | * |
565 | | * Returns: an integer, or %G_MAXUINT if invalid or not found |
566 | | **/ |
567 | | guint |
568 | | fu_context_get_smbios_integer(FuContext *self, |
569 | | guint8 type, |
570 | | guint8 length, |
571 | | guint8 offset, |
572 | | GError **error) |
573 | 0 | { |
574 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
575 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), G_MAXUINT); |
576 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, G_MAXUINT); |
577 | 0 | if (!fu_context_has_flag(self, FU_CONTEXT_FLAG_LOADED_HWINFO)) { |
578 | 0 | g_critical("cannot use SMBIOS before calling ->load_hwinfo()"); |
579 | 0 | return G_MAXUINT; |
580 | 0 | } |
581 | 0 | return fu_smbios_get_integer(priv->smbios, type, length, offset, error); |
582 | 0 | } |
583 | | |
584 | | /** |
585 | | * fu_context_get_bios_settings: |
586 | | * @self: a #FuContext |
587 | | * |
588 | | * Returns all the firmware attributes defined in the system. |
589 | | * |
590 | | * Returns: (transfer full) (element-type FuBiosSetting): an array |
591 | | **/ |
592 | | GPtrArray * |
593 | | fu_context_get_bios_settings(FuContext *self) |
594 | 0 | { |
595 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
596 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
597 | 0 | return g_ptr_array_ref(priv->bios_settings); |
598 | 0 | } |
599 | | |
600 | | /** |
601 | | * fu_context_get_bios_setting: |
602 | | * @self: a #FuContext |
603 | | * @name: a BIOS setting title, e.g. `BootOrderLock` |
604 | | * |
605 | | * Finds out if a system supports a given BIOS setting. |
606 | | * |
607 | | * Returns: (transfer none): #FuBiosSetting if the attr exists. |
608 | | **/ |
609 | | FuBiosSetting * |
610 | | fu_context_get_bios_setting(FuContext *self, const gchar *name) |
611 | 0 | { |
612 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
613 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
614 | 0 | g_return_val_if_fail(name != NULL, NULL); |
615 | | |
616 | 0 | for (guint i = 0; i < priv->bios_settings->len; i++) { |
617 | 0 | FuBiosSetting *attr = g_ptr_array_index(priv->bios_settings, i); |
618 | 0 | if (g_strcmp0(name, fu_bios_setting_get_id(attr)) == 0 || |
619 | 0 | g_strcmp0(name, fu_bios_setting_get_name(attr)) == 0 || |
620 | 0 | g_strcmp0(name, fu_bios_setting_get_appstream_id(attr)) == 0) |
621 | 0 | return attr; |
622 | 0 | } |
623 | 0 | return NULL; |
624 | 0 | } |
625 | | |
626 | | /** |
627 | | * fu_context_add_bios_setting: |
628 | | * @self: a #FuContext |
629 | | * @attr: a #FuBiosSetting |
630 | | * @error: (nullable): optional return location for an error |
631 | | * |
632 | | * Registers a BIOS setting that was created by a plugin. |
633 | | * This is a public API for plugins to register custom BIOS settings. |
634 | | * |
635 | | * Returns: TRUE if the setting was registered successfully |
636 | | **/ |
637 | | gboolean |
638 | | fu_context_add_bios_setting(FuContext *self, FuBiosSetting *attr, GError **error) |
639 | 0 | { |
640 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
641 | |
|
642 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
643 | 0 | g_return_val_if_fail(FU_IS_BIOS_SETTING(attr), FALSE); |
644 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
645 | | |
646 | 0 | if (fu_bios_setting_get_id(attr) == NULL) { |
647 | 0 | g_set_error_literal(error, |
648 | 0 | FWUPD_ERROR, |
649 | 0 | FWUPD_ERROR_INVALID_DATA, |
650 | 0 | "BIOS setting missing required id property"); |
651 | 0 | return FALSE; |
652 | 0 | } |
653 | | |
654 | 0 | if (fu_context_get_bios_setting(self, fu_bios_setting_get_id(attr)) != NULL) { |
655 | 0 | g_set_error(error, |
656 | 0 | FWUPD_ERROR, |
657 | 0 | FWUPD_ERROR_INVALID_DATA, |
658 | 0 | "BIOS setting with id %s already exists", |
659 | 0 | fu_bios_setting_get_id(attr)); |
660 | 0 | return FALSE; |
661 | 0 | } |
662 | | |
663 | 0 | g_ptr_array_add(priv->bios_settings, g_object_ref(attr)); |
664 | 0 | return TRUE; |
665 | 0 | } |
666 | | |
667 | | /** |
668 | | * fu_context_get_chassis_kind: |
669 | | * @self: a #FuContext |
670 | | * |
671 | | * Gets the chassis kind, if known. |
672 | | * |
673 | | * Returns: a #FuSmbiosChassisKind, e.g. %FU_SMBIOS_CHASSIS_KIND_LAPTOP |
674 | | **/ |
675 | | FuSmbiosChassisKind |
676 | | fu_context_get_chassis_kind(FuContext *self) |
677 | 0 | { |
678 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
679 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
680 | 0 | return priv->chassis_kind; |
681 | 0 | } |
682 | | |
683 | | /** |
684 | | * fu_context_set_chassis_kind: |
685 | | * @self: a #FuContext |
686 | | * @chassis_kind: a #FuSmbiosChassisKind, e.g. %FU_SMBIOS_CHASSIS_KIND_TABLET |
687 | | * |
688 | | * Sets the chassis kind. |
689 | | **/ |
690 | | void |
691 | | fu_context_set_chassis_kind(FuContext *self, FuSmbiosChassisKind chassis_kind) |
692 | 0 | { |
693 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
694 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
695 | 0 | priv->chassis_kind = chassis_kind; |
696 | 0 | } |
697 | | |
698 | | /** |
699 | | * fu_context_has_hwid_guid: |
700 | | * @self: a #FuContext |
701 | | * @guid: a GUID, e.g. `059eb22d-6dc7-59af-abd3-94bbe017f67c` |
702 | | * |
703 | | * Finds out if a hardware GUID exists. |
704 | | * |
705 | | * Returns: %TRUE if the GUID exists |
706 | | **/ |
707 | | gboolean |
708 | | fu_context_has_hwid_guid(FuContext *self, const gchar *guid) |
709 | 0 | { |
710 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
711 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
712 | 0 | if (!fu_context_has_flag(self, FU_CONTEXT_FLAG_LOADED_HWINFO)) { |
713 | 0 | g_critical("cannot use HWIDs before calling ->load_hwinfo()"); |
714 | 0 | return FALSE; |
715 | 0 | } |
716 | 0 | return fu_hwids_has_guid(priv->hwids, guid); |
717 | 0 | } |
718 | | |
719 | | /** |
720 | | * fu_context_get_hwid_guids: |
721 | | * @self: a #FuContext |
722 | | * |
723 | | * Returns all the HWIDs defined in the system. All hardware IDs on a |
724 | | * specific system can be shown using the `fwupdmgr hwids` command. |
725 | | * |
726 | | * Returns: (transfer none) (element-type utf8): an array of GUIDs |
727 | | **/ |
728 | | GPtrArray * |
729 | | fu_context_get_hwid_guids(FuContext *self) |
730 | 0 | { |
731 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
732 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
733 | 0 | if (!fu_context_has_flag(self, FU_CONTEXT_FLAG_LOADED_HWINFO)) { |
734 | 0 | g_critical("cannot use HWIDs before calling ->load_hwinfo()"); |
735 | 0 | return NULL; |
736 | 0 | } |
737 | 0 | return fu_hwids_get_guids(priv->hwids); |
738 | 0 | } |
739 | | |
740 | | /** |
741 | | * fu_context_get_hwid_value: |
742 | | * @self: a #FuContext |
743 | | * @key: a DMI ID, e.g. `BiosVersion` |
744 | | * |
745 | | * Gets the cached value for one specific key that is valid ASCII and suitable |
746 | | * for display. |
747 | | * |
748 | | * Returns: the string, e.g. `1.2.3`, or %NULL if not found |
749 | | **/ |
750 | | const gchar * |
751 | | fu_context_get_hwid_value(FuContext *self, const gchar *key) |
752 | 0 | { |
753 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
754 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
755 | 0 | g_return_val_if_fail(key != NULL, NULL); |
756 | 0 | if (!fu_context_has_flag(self, FU_CONTEXT_FLAG_LOADED_HWINFO)) { |
757 | 0 | g_critical("cannot use HWIDs before calling ->load_hwinfo()"); |
758 | 0 | return NULL; |
759 | 0 | } |
760 | 0 | return fu_hwids_get_value(priv->hwids, key); |
761 | 0 | } |
762 | | |
763 | | /** |
764 | | * fu_context_get_hwid_replace_value: |
765 | | * @self: a #FuContext |
766 | | * @keys: a key, e.g. `HardwareID-3` or %FU_HWIDS_KEY_PRODUCT_SKU |
767 | | * @error: (nullable): optional return location for an error |
768 | | * |
769 | | * Gets the replacement value for a specific key. All hardware IDs on a |
770 | | * specific system can be shown using the `fwupdmgr hwids` command. |
771 | | * |
772 | | * Returns: (transfer full): a string, or %NULL for error. |
773 | | **/ |
774 | | gchar * |
775 | | fu_context_get_hwid_replace_value(FuContext *self, const gchar *keys, GError **error) |
776 | 0 | { |
777 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
778 | |
|
779 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
780 | 0 | g_return_val_if_fail(keys != NULL, NULL); |
781 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
782 | | |
783 | 0 | if (!fu_context_has_flag(self, FU_CONTEXT_FLAG_LOADED_HWINFO)) { |
784 | 0 | g_critical("cannot use HWIDs before calling ->load_hwinfo()"); |
785 | 0 | g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "no data"); |
786 | 0 | return NULL; |
787 | 0 | } |
788 | 0 | return fu_hwids_get_replace_values(priv->hwids, keys, error); |
789 | 0 | } |
790 | | |
791 | | /** |
792 | | * fu_context_add_runtime_version: |
793 | | * @self: a #FuContext |
794 | | * @component_id: an AppStream component id, e.g. `org.gnome.Software` |
795 | | * @version: a version string, e.g. `1.2.3` |
796 | | * |
797 | | * Sets a runtime version of a specific dependency. |
798 | | **/ |
799 | | void |
800 | | fu_context_add_runtime_version(FuContext *self, const gchar *component_id, const gchar *version) |
801 | 0 | { |
802 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
803 | |
|
804 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
805 | 0 | g_return_if_fail(component_id != NULL); |
806 | 0 | g_return_if_fail(version != NULL); |
807 | | |
808 | 0 | if (priv->runtime_versions == NULL) |
809 | 0 | return; |
810 | 0 | g_hash_table_insert(priv->runtime_versions, g_strdup(component_id), g_strdup(version)); |
811 | 0 | } |
812 | | |
813 | | /** |
814 | | * fu_context_get_runtime_version: |
815 | | * @self: a #FuContext |
816 | | * @component_id: an AppStream component id, e.g. `org.gnome.Software` |
817 | | * |
818 | | * Sets a runtime version of a specific dependency. |
819 | | * |
820 | | * Returns: a version string, e.g. `1.2.3`, or %NULL |
821 | | **/ |
822 | | const gchar * |
823 | | fu_context_get_runtime_version(FuContext *self, const gchar *component_id) |
824 | 0 | { |
825 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
826 | |
|
827 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
828 | 0 | g_return_val_if_fail(component_id != NULL, NULL); |
829 | | |
830 | 0 | if (priv->runtime_versions == NULL) |
831 | 0 | return NULL; |
832 | 0 | return g_hash_table_lookup(priv->runtime_versions, component_id); |
833 | 0 | } |
834 | | |
835 | | /** |
836 | | * fu_context_get_runtime_versions: |
837 | | * @self: a #FuContext |
838 | | * |
839 | | * Gets the runtime versions for the context. |
840 | | * |
841 | | * Returns: (transfer none) (element-type utf8 utf8): dictionary of versions |
842 | | **/ |
843 | | GHashTable * |
844 | | fu_context_get_runtime_versions(FuContext *self) |
845 | 0 | { |
846 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
847 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
848 | 0 | return priv->runtime_versions; |
849 | 0 | } |
850 | | |
851 | | /** |
852 | | * fu_context_add_compile_version: |
853 | | * @self: a #FuContext |
854 | | * @component_id: an AppStream component id, e.g. `org.gnome.Software` |
855 | | * @version: a version string, e.g. `1.2.3` |
856 | | * |
857 | | * Sets a compile-time version of a specific dependency. |
858 | | **/ |
859 | | void |
860 | | fu_context_add_compile_version(FuContext *self, const gchar *component_id, const gchar *version) |
861 | 0 | { |
862 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
863 | |
|
864 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
865 | 0 | g_return_if_fail(component_id != NULL); |
866 | 0 | g_return_if_fail(version != NULL); |
867 | | |
868 | 0 | if (priv->compile_versions == NULL) |
869 | 0 | return; |
870 | 0 | g_hash_table_insert(priv->compile_versions, g_strdup(component_id), g_strdup(version)); |
871 | 0 | } |
872 | | |
873 | | /** |
874 | | * fu_context_get_compile_versions: |
875 | | * @self: a #FuContext |
876 | | * |
877 | | * Gets the compile time versions for the context. |
878 | | * |
879 | | * Returns: (transfer none) (element-type utf8 utf8): dictionary of versions |
880 | | **/ |
881 | | GHashTable * |
882 | | fu_context_get_compile_versions(FuContext *self) |
883 | 0 | { |
884 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
885 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
886 | 0 | return priv->compile_versions; |
887 | 0 | } |
888 | | |
889 | | static gint |
890 | | fu_context_udev_plugin_names_sort_cb(gconstpointer a, gconstpointer b) |
891 | 0 | { |
892 | 0 | const gchar *str_a = *((const gchar **)a); |
893 | 0 | const gchar *str_b = *((const gchar **)b); |
894 | 0 | return g_strcmp0(str_a, str_b); |
895 | 0 | } |
896 | | |
897 | | /** |
898 | | * fu_context_add_udev_subsystem: |
899 | | * @self: a #FuContext |
900 | | * @subsystem: a subsystem name, e.g. `pciport`, or `block:partition` |
901 | | * @plugin_name: (nullable): a plugin name, e.g. `iommu` |
902 | | * |
903 | | * Registers the udev subsystem to be watched by the daemon. |
904 | | * |
905 | | * Plugins can use this method only in fu_plugin_init() |
906 | | **/ |
907 | | void |
908 | | fu_context_add_udev_subsystem(FuContext *self, const gchar *subsystem, const gchar *plugin_name) |
909 | 0 | { |
910 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
911 | 0 | GPtrArray *plugin_names; |
912 | 0 | g_auto(GStrv) subsystem_devtype = NULL; |
913 | |
|
914 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
915 | 0 | g_return_if_fail(subsystem != NULL); |
916 | | |
917 | | /* add the base subsystem watch if passed a subsystem:devtype */ |
918 | 0 | subsystem_devtype = g_strsplit(subsystem, ":", 2); |
919 | 0 | if (g_strv_length(subsystem_devtype) > 1) |
920 | 0 | fu_context_add_udev_subsystem(self, subsystem_devtype[0], NULL); |
921 | | |
922 | | /* already exists */ |
923 | 0 | plugin_names = g_hash_table_lookup(priv->udev_subsystems, subsystem); |
924 | 0 | if (plugin_names != NULL) { |
925 | 0 | if (plugin_name != NULL) { |
926 | 0 | for (guint i = 0; i < plugin_names->len; i++) { |
927 | 0 | const gchar *tmp = g_ptr_array_index(plugin_names, i); |
928 | 0 | if (g_strcmp0(tmp, plugin_name) == 0) |
929 | 0 | return; |
930 | 0 | } |
931 | 0 | g_ptr_array_add(plugin_names, g_strdup(plugin_name)); |
932 | 0 | g_ptr_array_sort(plugin_names, fu_context_udev_plugin_names_sort_cb); |
933 | 0 | } |
934 | 0 | return; |
935 | 0 | } |
936 | | |
937 | | /* add */ |
938 | 0 | plugin_names = g_ptr_array_new_with_free_func(g_free); |
939 | 0 | if (plugin_name != NULL) |
940 | 0 | g_ptr_array_add(plugin_names, g_strdup(plugin_name)); |
941 | 0 | g_hash_table_insert(priv->udev_subsystems, |
942 | 0 | g_strdup(subsystem), |
943 | 0 | g_steal_pointer(&plugin_names)); |
944 | 0 | if (plugin_name != NULL) |
945 | 0 | g_info("added udev subsystem watch of %s for plugin %s", subsystem, plugin_name); |
946 | 0 | else |
947 | 0 | g_info("added udev subsystem watch of %s", subsystem); |
948 | 0 | } |
949 | | |
950 | | /** |
951 | | * fu_context_get_plugin_names_for_udev_subsystem: |
952 | | * @self: a #FuContext |
953 | | * @subsystem: a subsystem name, e.g. `pciport`, or `block:partition` |
954 | | * @error: (nullable): optional return location for an error |
955 | | * |
956 | | * Gets the plugins which registered for a specific subsystem. |
957 | | * |
958 | | * Returns: (transfer container) (element-type utf8): List of plugin names |
959 | | **/ |
960 | | GPtrArray * |
961 | | fu_context_get_plugin_names_for_udev_subsystem(FuContext *self, |
962 | | const gchar *subsystem, |
963 | | GError **error) |
964 | 0 | { |
965 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
966 | 0 | GPtrArray *plugin_names_tmp; |
967 | 0 | g_auto(GStrv) subsystem_devtype = NULL; |
968 | 0 | g_autoptr(GPtrArray) plugin_names = g_ptr_array_new_with_free_func(g_free); |
969 | |
|
970 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
971 | 0 | g_return_val_if_fail(subsystem != NULL, NULL); |
972 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
973 | | |
974 | | /* add the base subsystem first */ |
975 | 0 | subsystem_devtype = g_strsplit(subsystem, ":", 2); |
976 | 0 | if (g_strv_length(subsystem_devtype) > 1) { |
977 | 0 | plugin_names_tmp = g_hash_table_lookup(priv->udev_subsystems, subsystem_devtype[0]); |
978 | 0 | if (plugin_names_tmp != NULL) |
979 | 0 | g_ptr_array_extend(plugin_names, |
980 | 0 | plugin_names_tmp, |
981 | 0 | (GCopyFunc)g_strdup, |
982 | 0 | NULL); |
983 | 0 | } |
984 | | |
985 | | /* add the exact match */ |
986 | 0 | plugin_names_tmp = g_hash_table_lookup(priv->udev_subsystems, subsystem); |
987 | 0 | if (plugin_names_tmp != NULL) |
988 | 0 | g_ptr_array_extend(plugin_names, plugin_names_tmp, (GCopyFunc)g_strdup, NULL); |
989 | | |
990 | | /* no matches */ |
991 | 0 | if (plugin_names->len == 0) { |
992 | 0 | g_set_error(error, |
993 | 0 | FWUPD_ERROR, |
994 | 0 | FWUPD_ERROR_NOT_FOUND, |
995 | 0 | "no plugins registered for %s", |
996 | 0 | subsystem); |
997 | 0 | return NULL; |
998 | 0 | } |
999 | | |
1000 | | /* success */ |
1001 | 0 | return g_steal_pointer(&plugin_names); |
1002 | 0 | } |
1003 | | |
1004 | | /** |
1005 | | * fu_context_get_udev_subsystems: |
1006 | | * @self: a #FuContext |
1007 | | * |
1008 | | * Gets the udev subsystems required by all plugins. |
1009 | | * |
1010 | | * Returns: (transfer container) (element-type utf8): List of subsystems |
1011 | | **/ |
1012 | | GPtrArray * |
1013 | | fu_context_get_udev_subsystems(FuContext *self) |
1014 | 0 | { |
1015 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1016 | 0 | g_autoptr(GList) keys = g_hash_table_get_keys(priv->udev_subsystems); |
1017 | 0 | g_autoptr(GPtrArray) subsystems = g_ptr_array_new_with_free_func(g_free); |
1018 | |
|
1019 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
1020 | | |
1021 | 0 | for (GList *l = keys; l != NULL; l = l->next) { |
1022 | 0 | const gchar *subsystem = (const gchar *)l->data; |
1023 | 0 | g_ptr_array_add(subsystems, g_strdup(subsystem)); |
1024 | 0 | } |
1025 | 0 | return g_steal_pointer(&subsystems); |
1026 | 0 | } |
1027 | | |
1028 | | /** |
1029 | | * fu_context_has_udev_subsystem: |
1030 | | * @self: a #FuContext |
1031 | | * @subsystem: (not nullable): a subsystem name, e.g. `pciport`, or `block:partition` |
1032 | | * |
1033 | | * Finds if the udev subsystem is being watched by the daemon. |
1034 | | * |
1035 | | * Returns: %TRUE if the udev subsystem was found |
1036 | | **/ |
1037 | | gboolean |
1038 | | fu_context_has_udev_subsystem(FuContext *self, const gchar *subsystem) |
1039 | 0 | { |
1040 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1041 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
1042 | 0 | g_return_val_if_fail(subsystem != NULL, FALSE); |
1043 | 0 | return g_hash_table_contains(priv->udev_subsystems, subsystem); |
1044 | 0 | } |
1045 | | |
1046 | | static gchar * |
1047 | | fu_context_convert_firmware_gtype_to_id(GType gtype) |
1048 | 0 | { |
1049 | 0 | const gchar *gtype_name = g_type_name(gtype); |
1050 | 0 | gsize len = strlen(gtype_name); |
1051 | 0 | g_autoptr(GString) str = g_string_new(NULL); |
1052 | | |
1053 | | /* no format */ |
1054 | 0 | if (g_strcmp0(gtype_name, "FuFirmware") == 0) |
1055 | 0 | return g_strdup("raw"); |
1056 | | |
1057 | | /* not useful */ |
1058 | 0 | if (g_str_has_suffix(gtype_name, "Firmware")) |
1059 | 0 | len -= 8; |
1060 | | |
1061 | | /* normal plugins */ |
1062 | 0 | for (guint j = 2; j < len; j++) { |
1063 | 0 | gchar tmp = gtype_name[j]; |
1064 | 0 | if (g_ascii_isupper(tmp)) { |
1065 | 0 | if (str->len > 0) |
1066 | 0 | g_string_append_c(str, '-'); |
1067 | 0 | g_string_append_c(str, g_ascii_tolower(tmp)); |
1068 | 0 | } else { |
1069 | 0 | g_string_append_c(str, tmp); |
1070 | 0 | } |
1071 | 0 | } |
1072 | 0 | if (str->len == 0) |
1073 | 0 | return NULL; |
1074 | 0 | return g_string_free_and_steal(g_steal_pointer(&str)); |
1075 | 0 | } |
1076 | | |
1077 | | /** |
1078 | | * fu_context_add_firmware_gtype: |
1079 | | * @self: a #FuContext |
1080 | | * @gtype: a #GType e.g. `FU_TYPE_FOO_FIRMWARE` |
1081 | | * |
1082 | | * Adds a firmware #GType which is used when creating devices. |
1083 | | * |
1084 | | * Plugins can use this method only in fu_plugin_init() |
1085 | | **/ |
1086 | | void |
1087 | | fu_context_add_firmware_gtype(FuContext *self, GType gtype) |
1088 | 0 | { |
1089 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1090 | |
|
1091 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1092 | 0 | g_return_if_fail(gtype != G_TYPE_INVALID); |
1093 | | |
1094 | 0 | g_type_ensure(gtype); |
1095 | 0 | g_hash_table_insert(priv->firmware_gtypes, |
1096 | 0 | fu_context_convert_firmware_gtype_to_id(gtype), |
1097 | 0 | GSIZE_TO_POINTER(gtype)); |
1098 | 0 | } |
1099 | | |
1100 | | /** |
1101 | | * fu_context_get_firmware_gtype_by_id: |
1102 | | * @self: a #FuContext |
1103 | | * @id: an string describing the type, e.g. `ihex` |
1104 | | * |
1105 | | * Returns the #GType using the firmware @id. |
1106 | | * |
1107 | | * Returns: a #GType, or %G_TYPE_INVALID |
1108 | | **/ |
1109 | | GType |
1110 | | fu_context_get_firmware_gtype_by_id(FuContext *self, const gchar *id) |
1111 | 0 | { |
1112 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1113 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), G_TYPE_INVALID); |
1114 | 0 | g_return_val_if_fail(id != NULL, G_TYPE_INVALID); |
1115 | 0 | return GPOINTER_TO_SIZE(g_hash_table_lookup(priv->firmware_gtypes, id)); |
1116 | 0 | } |
1117 | | |
1118 | | static gint |
1119 | | fu_context_gtypes_sort_cb(gconstpointer a, gconstpointer b) |
1120 | 0 | { |
1121 | 0 | const gchar *stra = *((const gchar **)a); |
1122 | 0 | const gchar *strb = *((const gchar **)b); |
1123 | 0 | return g_strcmp0(stra, strb); |
1124 | 0 | } |
1125 | | |
1126 | | /** |
1127 | | * fu_context_get_firmware_gtype_ids: |
1128 | | * @self: a #FuContext |
1129 | | * |
1130 | | * Returns all the firmware #GType IDs. |
1131 | | * |
1132 | | * Returns: (transfer container) (element-type utf8): firmware IDs |
1133 | | **/ |
1134 | | GPtrArray * |
1135 | | fu_context_get_firmware_gtype_ids(FuContext *self) |
1136 | 0 | { |
1137 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1138 | 0 | GPtrArray *firmware_gtypes = g_ptr_array_new_with_free_func(g_free); |
1139 | 0 | g_autoptr(GList) keys = g_hash_table_get_keys(priv->firmware_gtypes); |
1140 | |
|
1141 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
1142 | | |
1143 | 0 | for (GList *l = keys; l != NULL; l = l->next) { |
1144 | 0 | const gchar *id = l->data; |
1145 | 0 | g_ptr_array_add(firmware_gtypes, g_strdup(id)); |
1146 | 0 | } |
1147 | 0 | g_ptr_array_sort(firmware_gtypes, fu_context_gtypes_sort_cb); |
1148 | 0 | return firmware_gtypes; |
1149 | 0 | } |
1150 | | |
1151 | | /** |
1152 | | * fu_context_get_firmware_gtypes: |
1153 | | * @self: a #FuContext |
1154 | | * |
1155 | | * Returns all the firmware #GType's. |
1156 | | * |
1157 | | * Returns: (transfer container) (element-type GType): Firmware types |
1158 | | **/ |
1159 | | GArray * |
1160 | | fu_context_get_firmware_gtypes(FuContext *self) |
1161 | 0 | { |
1162 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1163 | 0 | GArray *firmware_gtypes = g_array_new(FALSE, FALSE, sizeof(GType)); |
1164 | 0 | g_autoptr(GList) values = g_hash_table_get_values(priv->firmware_gtypes); |
1165 | |
|
1166 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
1167 | | |
1168 | 0 | for (GList *l = values; l != NULL; l = l->next) { |
1169 | 0 | GType gtype = GPOINTER_TO_SIZE(l->data); |
1170 | 0 | g_array_append_val(firmware_gtypes, gtype); |
1171 | 0 | } |
1172 | 0 | return firmware_gtypes; |
1173 | 0 | } |
1174 | | |
1175 | | /** |
1176 | | * fu_context_add_quirk_key: |
1177 | | * @self: a #FuContext |
1178 | | * @key: a quirk string, e.g. `DfuVersion` |
1179 | | * |
1180 | | * Adds a possible quirk key. If added by a plugin it should be namespaced |
1181 | | * using the plugin name, where possible. |
1182 | | * |
1183 | | * Plugins can use this method only in fu_plugin_init() |
1184 | | **/ |
1185 | | void |
1186 | | fu_context_add_quirk_key(FuContext *self, const gchar *key) |
1187 | 0 | { |
1188 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1189 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1190 | 0 | g_return_if_fail(key != NULL); |
1191 | 0 | if (priv->quirks == NULL) |
1192 | 0 | return; |
1193 | 0 | fu_quirks_add_possible_key(priv->quirks, key); |
1194 | 0 | } |
1195 | | |
1196 | | /** |
1197 | | * fu_context_lookup_quirk_by_id: |
1198 | | * @self: a #FuContext |
1199 | | * @guid: GUID to lookup |
1200 | | * @key: an ID to match the entry, e.g. `Summary` |
1201 | | * |
1202 | | * Looks up an entry in the hardware database using a string value. |
1203 | | * |
1204 | | * Returns: (transfer none): values from the database, or %NULL if not found |
1205 | | **/ |
1206 | | const gchar * |
1207 | | fu_context_lookup_quirk_by_id(FuContext *self, const gchar *guid, const gchar *key) |
1208 | 0 | { |
1209 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1210 | |
|
1211 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
1212 | 0 | g_return_val_if_fail(guid != NULL, NULL); |
1213 | 0 | g_return_val_if_fail(key != NULL, NULL); |
1214 | | |
1215 | 0 | if (priv->flags & FU_CONTEXT_FLAG_NO_QUIRKS) |
1216 | 0 | return NULL; |
1217 | | |
1218 | | /* exact ID */ |
1219 | 0 | return fu_quirks_lookup_by_id(priv->quirks, guid, key); |
1220 | 0 | } |
1221 | | |
1222 | | typedef struct { |
1223 | | FuContext *self; /* noref */ |
1224 | | FuContextLookupIter iter_cb; |
1225 | | gpointer user_data; |
1226 | | } FuContextQuirkLookupHelper; |
1227 | | |
1228 | | static void |
1229 | | fu_context_lookup_quirk_by_id_iter_cb(FuQuirks *self, |
1230 | | const gchar *key, |
1231 | | const gchar *value, |
1232 | | FuContextQuirkSource source, |
1233 | | gpointer user_data) |
1234 | 0 | { |
1235 | 0 | FuContextQuirkLookupHelper *helper = (FuContextQuirkLookupHelper *)user_data; |
1236 | 0 | helper->iter_cb(helper->self, key, value, source, helper->user_data); |
1237 | 0 | } |
1238 | | |
1239 | | /** |
1240 | | * fu_context_lookup_quirk_by_id_iter: |
1241 | | * @self: a #FuContext |
1242 | | * @guid: GUID to lookup |
1243 | | * @key: (nullable): an ID to match the entry, e.g. `Name`, or %NULL for all keys |
1244 | | * @iter_cb: (scope call) (closure user_data): a function to call for each result |
1245 | | * @flags: some #FuContextLookupFlags, e.g. #FU_CONTEXT_LOOKUP_FLAG_ALLOW_CACHE |
1246 | | * @user_data: user data passed to @iter_cb |
1247 | | * |
1248 | | * Looks up all entries in the hardware database using a GUID value. |
1249 | | * |
1250 | | * Returns: %TRUE if the ID was found, and @iter was called |
1251 | | **/ |
1252 | | gboolean |
1253 | | fu_context_lookup_quirk_by_id_iter(FuContext *self, |
1254 | | const gchar *guid, |
1255 | | const gchar *key, |
1256 | | FuContextLookupIter iter_cb, |
1257 | | FuContextLookupFlags flags, |
1258 | | gpointer user_data) |
1259 | 0 | { |
1260 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1261 | 0 | FuQuirksLookupFlags lookup_flags = FU_QUIRKS_LOOKUP_FLAG_NONE; |
1262 | 0 | FuContextQuirkLookupHelper helper = { |
1263 | 0 | .self = self, |
1264 | 0 | .iter_cb = iter_cb, |
1265 | 0 | .user_data = user_data, |
1266 | 0 | }; |
1267 | |
|
1268 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
1269 | 0 | g_return_val_if_fail(guid != NULL, FALSE); |
1270 | 0 | g_return_val_if_fail(iter_cb != NULL, FALSE); |
1271 | 0 | if (priv->flags & FU_CONTEXT_FLAG_NO_QUIRKS) |
1272 | 0 | return TRUE; |
1273 | 0 | if (flags & FU_CONTEXT_LOOKUP_FLAG_ALLOW_CACHE) |
1274 | 0 | lookup_flags |= FU_QUIRKS_LOOKUP_FLAG_ALLOW_CACHE; |
1275 | 0 | return fu_quirks_lookup_by_id_iter(priv->quirks, |
1276 | 0 | guid, |
1277 | 0 | key, |
1278 | 0 | fu_context_lookup_quirk_by_id_iter_cb, |
1279 | 0 | lookup_flags, |
1280 | 0 | &helper); |
1281 | 0 | } |
1282 | | |
1283 | | /** |
1284 | | * fu_context_security_changed: |
1285 | | * @self: a #FuContext |
1286 | | * |
1287 | | * Informs the daemon that the HSI state may have changed. |
1288 | | **/ |
1289 | | void |
1290 | | fu_context_security_changed(FuContext *self) |
1291 | 0 | { |
1292 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1293 | 0 | g_signal_emit(self, signals[SIGNAL_SECURITY_CHANGED], 0); |
1294 | 0 | } |
1295 | | |
1296 | | /** |
1297 | | * fu_context_housekeeping: |
1298 | | * @self: a #FuContext |
1299 | | * |
1300 | | * Performs any housekeeping maintenance when the daemon is idle. |
1301 | | **/ |
1302 | | void |
1303 | | fu_context_housekeeping(FuContext *self) |
1304 | 0 | { |
1305 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1306 | 0 | g_signal_emit(self, signals[SIGNAL_HOUSEKEEPING], 0); |
1307 | 0 | } |
1308 | | |
1309 | | typedef gboolean (*FuContextHwidsSetupFunc)(FuContext *self, FuHwids *hwids, GError **error); |
1310 | | |
1311 | | static void |
1312 | | fu_context_detect_full_disk_encryption(FuContext *self) |
1313 | 0 | { |
1314 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1315 | 0 | g_autoptr(GPtrArray) devices = NULL; |
1316 | 0 | g_autoptr(GError) error_local = NULL; |
1317 | |
|
1318 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1319 | | |
1320 | 0 | if (g_file_test("/var/lib/systemd/pcrlock.json", G_FILE_TEST_EXISTS)) |
1321 | 0 | priv->flags |= FU_CONTEXT_FLAG_FDE_SYSTEMD_PCRLOCK; |
1322 | |
|
1323 | 0 | devices = fu_common_get_block_devices(&error_local); |
1324 | 0 | if (devices == NULL) { |
1325 | 0 | g_info("Failed to get block devices: %s", error_local->message); |
1326 | 0 | return; |
1327 | 0 | } |
1328 | | |
1329 | 0 | for (guint i = 0; i < devices->len; i++) { |
1330 | 0 | GDBusProxy *proxy = g_ptr_array_index(devices, i); |
1331 | 0 | g_autoptr(GVariant) id_type = g_dbus_proxy_get_cached_property(proxy, "IdType"); |
1332 | 0 | g_autoptr(GVariant) device = g_dbus_proxy_get_cached_property(proxy, "Device"); |
1333 | 0 | g_autoptr(GVariant) id_label = g_dbus_proxy_get_cached_property(proxy, "IdLabel"); |
1334 | 0 | if (id_type != NULL && device != NULL && |
1335 | 0 | g_strcmp0(fwupd_variant_get_string(id_type), "BitLocker") == 0) |
1336 | 0 | priv->flags |= FU_CONTEXT_FLAG_FDE_BITLOCKER; |
1337 | |
|
1338 | 0 | if (id_type != NULL && id_label != NULL && |
1339 | 0 | g_strcmp0(fwupd_variant_get_string(id_label), "ubuntu-data-enc") == 0 && |
1340 | 0 | g_strcmp0(fwupd_variant_get_string(id_type), "crypto_LUKS") == 0) { |
1341 | 0 | priv->flags |= FU_CONTEXT_FLAG_FDE_SNAPD; |
1342 | 0 | } |
1343 | 0 | } |
1344 | 0 | } |
1345 | | |
1346 | | static gboolean |
1347 | | fu_context_flag_is_quirk_controlled(FuContextFlags flag) |
1348 | 0 | { |
1349 | 0 | return (flag & |
1350 | 0 | (FU_CONTEXT_FLAG_SYSTEM_INHIBIT | FU_CONTEXT_FLAG_INHIBIT_VOLUME_MOUNT | |
1351 | 0 | FU_CONTEXT_FLAG_FDE_BITLOCKER | FU_CONTEXT_FLAG_FDE_SNAPD | |
1352 | 0 | FU_CONTEXT_FLAG_FDE_SYSTEMD_PCRLOCK | FU_CONTEXT_FLAG_IGNORE_EFIVARS_FREE_SPACE | |
1353 | 0 | FU_CONTEXT_FLAG_INSECURE_UEFI | FU_CONTEXT_FLAG_IS_HYPERVISOR | |
1354 | 0 | FU_CONTEXT_FLAG_IS_HYPERVISOR_PRIVILEGED | FU_CONTEXT_FLAG_IS_CONTAINER | |
1355 | 0 | FU_CONTEXT_FLAG_SMBIOS_UEFI_ENABLED | FU_CONTEXT_FLAG_IS_SERVER)) > 0; |
1356 | 0 | } |
1357 | | |
1358 | | static void |
1359 | | fu_context_hwid_quirk_cb(FuContext *self, |
1360 | | const gchar *key, |
1361 | | const gchar *value, |
1362 | | FuContextQuirkSource source, |
1363 | | gpointer user_data) |
1364 | 0 | { |
1365 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1366 | 0 | if (value != NULL) { |
1367 | 0 | g_auto(GStrv) values = g_strsplit(value, ",", -1); |
1368 | 0 | for (guint i = 0; values[i] != NULL; i++) { |
1369 | 0 | const gchar *value_tmp = values[i]; |
1370 | 0 | if (g_str_has_prefix(value_tmp, "~")) { |
1371 | 0 | if (g_strcmp0(key, FU_QUIRKS_FLAGS) == 0) { |
1372 | 0 | FuContextFlags flag = |
1373 | 0 | fu_context_flag_from_string(value_tmp + 1); |
1374 | 0 | if (fu_context_flag_is_quirk_controlled(flag)) |
1375 | 0 | fu_context_remove_flag(self, flag); |
1376 | 0 | } |
1377 | 0 | g_hash_table_remove(priv->hwid_flags, value_tmp + 1); |
1378 | 0 | continue; |
1379 | 0 | } |
1380 | 0 | if (g_strcmp0(key, FU_QUIRKS_FLAGS) == 0) { |
1381 | 0 | FuContextFlags flag = fu_context_flag_from_string(value_tmp); |
1382 | 0 | if (fu_context_flag_is_quirk_controlled(flag)) |
1383 | 0 | fu_context_add_flag(self, flag); |
1384 | 0 | } |
1385 | 0 | g_hash_table_add(priv->hwid_flags, g_strdup(value_tmp)); |
1386 | 0 | } |
1387 | 0 | } |
1388 | 0 | } |
1389 | | |
1390 | | static void |
1391 | | fu_context_detect_container(FuContext *self) |
1392 | 0 | { |
1393 | 0 | #ifndef __ANDROID__ |
1394 | 0 | gsize bufsz = 0; |
1395 | 0 | g_autofree gchar *buf = NULL; |
1396 | |
|
1397 | 0 | if (!g_file_get_contents("/proc/1/cgroup", &buf, &bufsz, NULL)) |
1398 | 0 | return; |
1399 | 0 | if (g_strstr_len(buf, (gssize)bufsz, "docker") != NULL) { |
1400 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_IS_CONTAINER); |
1401 | 0 | return; |
1402 | 0 | } |
1403 | 0 | if (g_strstr_len(buf, (gssize)bufsz, "lxc") != NULL) { |
1404 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_IS_CONTAINER); |
1405 | 0 | return; |
1406 | 0 | } |
1407 | 0 | #endif |
1408 | 0 | } |
1409 | | |
1410 | | static void |
1411 | | fu_context_detect_hypervisor_privileged(FuContext *self) |
1412 | 0 | { |
1413 | 0 | g_autofree gchar *xen_privileged_fn = NULL; |
1414 | | |
1415 | | /* privileged xen can access most hardware */ |
1416 | 0 | xen_privileged_fn = fu_context_build_filename(self, |
1417 | 0 | NULL, |
1418 | 0 | FU_PATH_KIND_SYSFSDIR_FW_ATTRIB, |
1419 | 0 | "hypervisor", |
1420 | 0 | "start_flags", |
1421 | 0 | "privileged", |
1422 | 0 | NULL); |
1423 | 0 | if (xen_privileged_fn == NULL) |
1424 | 0 | return; |
1425 | 0 | if (!g_file_test(xen_privileged_fn, G_FILE_TEST_EXISTS)) { |
1426 | 0 | g_autofree gchar *contents = NULL; |
1427 | 0 | if (g_file_get_contents(xen_privileged_fn, &contents, NULL, NULL)) { |
1428 | 0 | if (g_strcmp0(contents, "1") == 0) { |
1429 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_IS_HYPERVISOR_PRIVILEGED); |
1430 | 0 | return; |
1431 | 0 | } |
1432 | 0 | } |
1433 | 0 | } |
1434 | 0 | } |
1435 | | |
1436 | | static void |
1437 | | fu_context_detect_cpu_vendor(FuContext *self) |
1438 | 0 | { |
1439 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1440 | 0 | guint ebx = 0; |
1441 | 0 | guint ecx = 0; |
1442 | 0 | guint edx = 0; |
1443 | |
|
1444 | 0 | if (priv->cpu_vendor != FU_CPU_VENDOR_UNKNOWN) |
1445 | 0 | return; |
1446 | | |
1447 | 0 | if (!fu_cpuid(0x0, NULL, &ebx, &ecx, &edx, NULL)) |
1448 | 0 | return; |
1449 | | #ifdef HAVE_CPUID_H |
1450 | | if (ebx == signature_INTEL_ebx && edx == signature_INTEL_edx && |
1451 | | ecx == signature_INTEL_ecx) { |
1452 | | priv->cpu_vendor = FU_CPU_VENDOR_INTEL; |
1453 | | } else if (ebx == signature_AMD_ebx && edx == signature_AMD_edx && |
1454 | | ecx == signature_AMD_ecx) { |
1455 | | priv->cpu_vendor = FU_CPU_VENDOR_AMD; |
1456 | | } |
1457 | | #endif |
1458 | 0 | } |
1459 | | |
1460 | | static void |
1461 | | fu_context_detect_hypervisor(FuContext *self) |
1462 | 0 | { |
1463 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1464 | 0 | const gchar *flags; |
1465 | 0 | g_autoptr(GHashTable) cpu_attrs = NULL; |
1466 | |
|
1467 | 0 | cpu_attrs = fu_cpu_get_attrs(priv->pstore, NULL); |
1468 | 0 | if (cpu_attrs == NULL) |
1469 | 0 | return; |
1470 | 0 | flags = g_hash_table_lookup(cpu_attrs, "flags"); |
1471 | 0 | if (flags == NULL) |
1472 | 0 | return; |
1473 | 0 | if (g_strstr_len(flags, -1, "hypervisor") != NULL) { |
1474 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_IS_HYPERVISOR); |
1475 | 0 | fu_context_detect_hypervisor_privileged(self); |
1476 | 0 | return; |
1477 | 0 | } |
1478 | 0 | } |
1479 | | |
1480 | | static gboolean |
1481 | | fu_context_load_boot_entries(FuContext *self, GError **error) |
1482 | 0 | { |
1483 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1484 | 0 | g_autoptr(GPtrArray) entries = NULL; |
1485 | | |
1486 | | /* skip unless UEFI is enabled in SMBIOS */ |
1487 | 0 | if (!fu_context_has_flag(self, FU_CONTEXT_FLAG_SMBIOS_UEFI_ENABLED)) |
1488 | 0 | return TRUE; |
1489 | | |
1490 | 0 | entries = fu_efivars_get_boot_entries(priv->efivars, error); |
1491 | 0 | if (entries == NULL) |
1492 | 0 | return FALSE; |
1493 | 0 | for (guint i = 0; i < entries->len; i++) { |
1494 | 0 | FuEfiLoadOption *entry = g_ptr_array_index(entries, i); |
1495 | 0 | const gchar *path; |
1496 | |
|
1497 | 0 | path = |
1498 | 0 | fu_efi_load_option_get_metadata(entry, FU_EFI_LOAD_OPTION_METADATA_PATH, NULL); |
1499 | 0 | if (path != NULL) { |
1500 | 0 | g_autofree gchar *path_lower = g_ascii_strdown(path, -1); |
1501 | 0 | if (g_strstr_len(path_lower, -1, "bootmgfw.efi") != NULL) { |
1502 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_DUAL_BOOT_WINDOWS); |
1503 | 0 | break; |
1504 | 0 | } |
1505 | 0 | } |
1506 | 0 | } |
1507 | | |
1508 | | /* success */ |
1509 | 0 | return TRUE; |
1510 | 0 | } |
1511 | | |
1512 | | /** |
1513 | | * fu_context_get_host_bkcs: |
1514 | | * @self: a #FuContext |
1515 | | * |
1516 | | * Gets the list of host BKCs. |
1517 | | * |
1518 | | * Returns: (transfer none) (element-type utf8): a #GPtrArray |
1519 | | **/ |
1520 | | GPtrArray * |
1521 | | fu_context_get_host_bkcs(FuContext *self) |
1522 | 0 | { |
1523 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1524 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
1525 | 0 | return priv->host_bkcs; |
1526 | 0 | } |
1527 | | |
1528 | | /** |
1529 | | * fu_context_get_host_bkcs_as_str: |
1530 | | * @self: a #FuContext |
1531 | | * |
1532 | | * Gets the list of host BKCs. |
1533 | | * |
1534 | | * Returns: a string representing the host BKCs. |
1535 | | **/ |
1536 | | gchar * |
1537 | | fu_context_get_host_bkcs_as_str(FuContext *self) |
1538 | 0 | { |
1539 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1540 | |
|
1541 | 0 | g_autoptr(GString) str = NULL; |
1542 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
1543 | | |
1544 | 0 | if (priv->host_bkcs->len == 0) |
1545 | 0 | return NULL; |
1546 | 0 | return fu_strjoin(",", priv->host_bkcs); |
1547 | 0 | } |
1548 | | |
1549 | | /** |
1550 | | * fu_context_add_host_bkc: |
1551 | | * @self: a #FuContext |
1552 | | * @host_bkc: a host BKC, e.g. `uefi-secure-boot` |
1553 | | * |
1554 | | * Adds a host BKC if it does not already exist. |
1555 | | **/ |
1556 | | void |
1557 | | fu_context_add_host_bkc(FuContext *self, const gchar *host_bkc) |
1558 | 0 | { |
1559 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1560 | |
|
1561 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1562 | 0 | g_return_if_fail(host_bkc != NULL); |
1563 | | |
1564 | 0 | if (g_strcmp0(host_bkc, "") == 0) |
1565 | 0 | return; |
1566 | 0 | if (g_ptr_array_find_with_equal_func(priv->host_bkcs, host_bkc, g_str_equal, NULL)) |
1567 | 0 | return; |
1568 | 0 | g_ptr_array_add(priv->host_bkcs, g_strdup(host_bkc)); |
1569 | 0 | } |
1570 | | |
1571 | | static gboolean |
1572 | | fu_context_add_host_bkc_safe(FuContext *self, const gchar *host_bkc, GError **error) |
1573 | 0 | { |
1574 | 0 | g_auto(GStrv) host_bkcs = NULL; |
1575 | |
|
1576 | 0 | host_bkcs = g_strsplit(host_bkc, ",", FU_CONTEXT_HOST_BKCS_MAX); |
1577 | 0 | if (g_strv_length(host_bkcs) == FU_CONTEXT_HOST_BKCS_MAX) { |
1578 | 0 | g_set_error(error, |
1579 | 0 | FWUPD_ERROR, |
1580 | 0 | FWUPD_ERROR_INVALID_DATA, |
1581 | 0 | "host has unreasonable BKC count: %u", |
1582 | 0 | (guint)FU_CONTEXT_HOST_BKCS_MAX); |
1583 | 0 | return FALSE; |
1584 | 0 | } |
1585 | 0 | for (guint i = 0; host_bkcs[i] != NULL; i++) |
1586 | 0 | fu_context_add_host_bkc(self, host_bkcs[i]); |
1587 | | |
1588 | | /* success */ |
1589 | 0 | return TRUE; |
1590 | 0 | } |
1591 | | |
1592 | | static void |
1593 | | fu_context_host_bkc_quirk_cb(FuContext *self, |
1594 | | const gchar *key, |
1595 | | const gchar *value, |
1596 | | FuContextQuirkSource source, |
1597 | | gpointer user_data) |
1598 | 0 | { |
1599 | 0 | g_auto(GStrv) host_bkcs = NULL; |
1600 | |
|
1601 | 0 | if (value == NULL) |
1602 | 0 | return; |
1603 | 0 | host_bkcs = g_strsplit(value, ",", -1); |
1604 | 0 | for (guint i = 0; host_bkcs[i] != NULL; i++) |
1605 | 0 | fu_context_add_host_bkc(self, host_bkcs[i]); |
1606 | 0 | } |
1607 | | |
1608 | | static gboolean |
1609 | | fu_context_load_host_bkcs(FuContext *self, GError **error) |
1610 | 0 | { |
1611 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1612 | 0 | GPtrArray *guids = fu_context_get_hwid_guids(self); |
1613 | 0 | g_autofree gchar *host_bkc = NULL; |
1614 | | |
1615 | | /* unload all previous entries */ |
1616 | 0 | g_ptr_array_set_size(priv->host_bkcs, 0); |
1617 | | |
1618 | | /* add from config then quirk files */ |
1619 | 0 | host_bkc = fu_context_get_config_str(self, "HostBkc"); |
1620 | 0 | if (host_bkc != NULL) { |
1621 | 0 | if (!fu_context_add_host_bkc_safe(self, host_bkc, error)) |
1622 | 0 | return FALSE; |
1623 | 0 | } |
1624 | 0 | for (guint i = 0; i < guids->len; i++) { |
1625 | 0 | const gchar *guid = g_ptr_array_index(guids, i); |
1626 | 0 | fu_context_lookup_quirk_by_id_iter(self, |
1627 | 0 | guid, |
1628 | 0 | FU_QUIRKS_HOST_BKC, |
1629 | 0 | fu_context_host_bkc_quirk_cb, |
1630 | 0 | FU_CONTEXT_LOOKUP_FLAG_NONE, |
1631 | 0 | NULL); |
1632 | 0 | } |
1633 | | |
1634 | | /* success */ |
1635 | 0 | return TRUE; |
1636 | 0 | } |
1637 | | |
1638 | | static void |
1639 | | fu_context_config_changed_cb(FuConfig *config, FuContext *self) |
1640 | 0 | { |
1641 | 0 | fu_context_load_host_bkcs(self, NULL); |
1642 | 0 | } |
1643 | | |
1644 | | /** |
1645 | | * fu_context_load: |
1646 | | * @self: a #FuContext |
1647 | | * @progress: a #FuProgress |
1648 | | * @flags: a #FuContextLoadFlags, e.g. %FU_CONTEXT_LOAD_FLAG_HWID_SMBIOS |
1649 | | * @error: (nullable): optional return location for an error |
1650 | | * |
1651 | | * Loads all hardware information and quirks into the context. |
1652 | | * |
1653 | | * Returns: %TRUE for success |
1654 | | **/ |
1655 | | gboolean |
1656 | | fu_context_load(FuContext *self, FuProgress *progress, FuContextLoadFlags flags, GError **error) |
1657 | 0 | { |
1658 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1659 | 0 | FuConfigLoadFlags config_load_flags = FU_CONFIG_LOAD_FLAG_NONE; |
1660 | 0 | GPtrArray *guids; |
1661 | 0 | const gchar *machine_kind = g_getenv("FWUPD_MACHINE_KIND"); |
1662 | 0 | g_autoptr(GError) error_quirks = NULL; |
1663 | 0 | g_autoptr(GError) error_hwids = NULL; |
1664 | 0 | g_autoptr(GError) error_smbios = NULL; |
1665 | 0 | g_autoptr(GError) error_bios_settings = NULL; |
1666 | 0 | g_autoptr(GError) error_efivars = NULL; |
1667 | 0 | struct { |
1668 | 0 | const gchar *name; |
1669 | 0 | FuContextLoadFlags flag; |
1670 | 0 | FuContextHwidsSetupFunc func; |
1671 | 0 | } hwids_setup_map[] = { |
1672 | 0 | {"config", FU_CONTEXT_LOAD_FLAG_HWID_CONFIG, fu_hwids_config_setup}, |
1673 | 0 | {"smbios", FU_CONTEXT_LOAD_FLAG_HWID_SMBIOS, fu_hwids_smbios_setup}, |
1674 | 0 | {"fdt", FU_CONTEXT_LOAD_FLAG_HWID_FDT, fu_hwids_fdt_setup}, |
1675 | 0 | {"kenv", FU_CONTEXT_LOAD_FLAG_HWID_KENV, fu_hwids_kenv_setup}, |
1676 | 0 | {"dmi", FU_CONTEXT_LOAD_FLAG_HWID_DMI, fu_hwids_dmi_setup}, |
1677 | 0 | {"darwin", FU_CONTEXT_LOAD_FLAG_HWID_DARWIN, fu_hwids_darwin_setup}, |
1678 | 0 | }; |
1679 | |
|
1680 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
1681 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
1682 | | |
1683 | | /* progress */ |
1684 | 0 | fu_progress_set_id(progress, G_STRLOC); |
1685 | 0 | fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 1, "quirks"); |
1686 | 0 | fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 1, "hwids-setup-funcs"); |
1687 | 0 | fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 1, "hwids-setup"); |
1688 | 0 | fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 3, "set-flags"); |
1689 | 0 | fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 1, "detect-fde"); |
1690 | 0 | fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 1, "detect-hypervisor-container"); |
1691 | | |
1692 | | /* load paths */ |
1693 | 0 | if (flags & FU_CONTEXT_LOAD_FLAG_PATH_STORE_DEFAULTS) |
1694 | 0 | fu_path_store_load_defaults(priv->pstore); |
1695 | 0 | if (flags & FU_CONTEXT_LOAD_FLAG_PATH_STORE_ENV) |
1696 | 0 | fu_path_store_load_from_env(priv->pstore); |
1697 | | |
1698 | | /* rebuild silo if required */ |
1699 | 0 | if (!fu_quirks_load(priv->quirks, &error_quirks)) { |
1700 | 0 | if (!g_error_matches(error_quirks, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED)) |
1701 | 0 | g_warning("failed to load quirks: %s", error_quirks->message); |
1702 | 0 | } |
1703 | 0 | fu_progress_step_done(progress); |
1704 | | |
1705 | | /* required always */ |
1706 | 0 | if (flags & FU_CONTEXT_LOAD_FLAG_WATCH_FILES) |
1707 | 0 | config_load_flags |= FU_CONFIG_LOAD_FLAG_WATCH_FILES; |
1708 | 0 | if (flags & FU_CONTEXT_LOAD_FLAG_FIX_PERMISSIONS) |
1709 | 0 | config_load_flags |= FU_CONFIG_LOAD_FLAG_FIX_PERMISSIONS; |
1710 | 0 | if (!fu_config_load(priv->config, config_load_flags, error)) |
1711 | 0 | return FALSE; |
1712 | 0 | g_signal_connect(priv->config, "changed", G_CALLBACK(fu_context_config_changed_cb), self); |
1713 | | |
1714 | | /* run all the HWID setup funcs */ |
1715 | 0 | for (guint i = 0; i < G_N_ELEMENTS(hwids_setup_map); i++) { |
1716 | 0 | if ((flags & hwids_setup_map[i].flag) > 0) { |
1717 | 0 | g_autoptr(GError) error_local = NULL; |
1718 | 0 | if (!hwids_setup_map[i].func(self, priv->hwids, &error_local)) { |
1719 | 0 | g_info("failed to load %s: %s", |
1720 | 0 | hwids_setup_map[i].name, |
1721 | 0 | error_local->message); |
1722 | 0 | continue; |
1723 | 0 | } |
1724 | 0 | } |
1725 | 0 | } |
1726 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_LOADED_HWINFO); |
1727 | 0 | fu_progress_step_done(progress); |
1728 | |
|
1729 | 0 | if (!fu_hwids_setup(priv->hwids, &error_hwids)) |
1730 | 0 | g_warning("Failed to load HWIDs: %s", error_hwids->message); |
1731 | 0 | fu_progress_step_done(progress); |
1732 | | |
1733 | | /* does the system support UEFI mode? */ |
1734 | 0 | if (!fu_context_ensure_smbios_uefi_enabled(self, &error_smbios)) |
1735 | 0 | g_debug("%s", error_smbios->message); |
1736 | | |
1737 | | /* load per-host BKC from config */ |
1738 | 0 | if (!fu_context_load_host_bkcs(self, error)) |
1739 | 0 | return FALSE; |
1740 | | |
1741 | | /* set the hwid flags */ |
1742 | 0 | guids = fu_context_get_hwid_guids(self); |
1743 | 0 | for (guint i = 0; i < guids->len; i++) { |
1744 | 0 | const gchar *guid = g_ptr_array_index(guids, i); |
1745 | 0 | fu_context_lookup_quirk_by_id_iter(self, |
1746 | 0 | guid, |
1747 | 0 | FU_QUIRKS_FLAGS, |
1748 | 0 | fu_context_hwid_quirk_cb, |
1749 | 0 | FU_CONTEXT_LOOKUP_FLAG_NONE, |
1750 | 0 | NULL); |
1751 | 0 | } |
1752 | 0 | fu_progress_step_done(progress); |
1753 | |
|
1754 | 0 | fu_context_detect_full_disk_encryption(self); |
1755 | 0 | fu_progress_step_done(progress); |
1756 | | |
1757 | | /* allow overriding for development */ |
1758 | 0 | if (machine_kind != NULL) { |
1759 | 0 | if (g_strcmp0(machine_kind, "physical") == 0) { |
1760 | | /* nothing to do */ |
1761 | 0 | } else if (g_strcmp0(machine_kind, "container") == 0) { |
1762 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_IS_CONTAINER); |
1763 | 0 | } else if (g_strcmp0(machine_kind, "virtual") == 0) { |
1764 | 0 | fu_context_add_flag(self, FU_CONTEXT_FLAG_IS_HYPERVISOR); |
1765 | 0 | } else { |
1766 | 0 | g_set_error(error, |
1767 | 0 | FWUPD_ERROR, |
1768 | 0 | FWUPD_ERROR_INVALID_DATA, |
1769 | 0 | "invalid machine kind specified: %s", |
1770 | 0 | machine_kind); |
1771 | 0 | return FALSE; |
1772 | 0 | } |
1773 | 0 | } else { |
1774 | 0 | fu_context_detect_hypervisor(self); |
1775 | 0 | fu_context_detect_container(self); |
1776 | 0 | } |
1777 | 0 | fu_context_detect_cpu_vendor(self); |
1778 | 0 | fu_progress_step_done(progress); |
1779 | | |
1780 | | /* is this dual booted with Windows */ |
1781 | 0 | if (!fu_context_load_boot_entries(self, &error_efivars)) { |
1782 | 0 | if (!g_error_matches(error_efivars, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED) && |
1783 | 0 | !g_error_matches(error_efivars, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND)) |
1784 | 0 | g_warning("failed to check boot entries: %s", error_efivars->message); |
1785 | 0 | } |
1786 | | |
1787 | | /* always */ |
1788 | 0 | return TRUE; |
1789 | 0 | } |
1790 | | |
1791 | | /** |
1792 | | * fu_context_has_hwid_flag: |
1793 | | * @self: a #FuContext |
1794 | | * @flag: flag, e.g. `use-legacy-bootmgr-desc` |
1795 | | * |
1796 | | * Returns if a HwId custom flag exists, typically added from a DMI quirk. |
1797 | | * |
1798 | | * Returns: %TRUE if the flag exists |
1799 | | **/ |
1800 | | gboolean |
1801 | | fu_context_has_hwid_flag(FuContext *self, const gchar *flag) |
1802 | 0 | { |
1803 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1804 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
1805 | 0 | g_return_val_if_fail(flag != NULL, FALSE); |
1806 | 0 | return g_hash_table_lookup(priv->hwid_flags, flag) != NULL; |
1807 | 0 | } |
1808 | | |
1809 | | /** |
1810 | | * fu_context_get_power_state: |
1811 | | * @self: a #FuContext |
1812 | | * |
1813 | | * Gets if the system is on battery power, e.g. UPS or laptop battery. |
1814 | | * |
1815 | | * Returns: a power state, e.g. %FU_POWER_STATE_BATTERY_DISCHARGING |
1816 | | **/ |
1817 | | FuPowerState |
1818 | | fu_context_get_power_state(FuContext *self) |
1819 | 0 | { |
1820 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1821 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
1822 | 0 | return priv->power_state; |
1823 | 0 | } |
1824 | | |
1825 | | /** |
1826 | | * fu_context_set_power_state: |
1827 | | * @self: a #FuContext |
1828 | | * @power_state: a power state, e.g. %FU_POWER_STATE_BATTERY_DISCHARGING |
1829 | | * |
1830 | | * Sets if the system is on battery power, e.g. UPS or laptop battery. |
1831 | | **/ |
1832 | | void |
1833 | | fu_context_set_power_state(FuContext *self, FuPowerState power_state) |
1834 | 0 | { |
1835 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1836 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1837 | 0 | if (priv->power_state == power_state) |
1838 | 0 | return; |
1839 | 0 | priv->power_state = power_state; |
1840 | 0 | g_info("power state now %s", fu_power_state_to_string(power_state)); |
1841 | 0 | g_object_notify(G_OBJECT(self), "power-state"); |
1842 | 0 | } |
1843 | | |
1844 | | /** |
1845 | | * fu_context_get_lid_state: |
1846 | | * @self: a #FuContext |
1847 | | * |
1848 | | * Gets the laptop lid state, if applicable. |
1849 | | * |
1850 | | * Returns: a lid state, e.g. %FU_LID_STATE_CLOSED |
1851 | | **/ |
1852 | | FuLidState |
1853 | | fu_context_get_lid_state(FuContext *self) |
1854 | 0 | { |
1855 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1856 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
1857 | 0 | return priv->lid_state; |
1858 | 0 | } |
1859 | | |
1860 | | /** |
1861 | | * fu_context_set_lid_state: |
1862 | | * @self: a #FuContext |
1863 | | * @lid_state: a lid state, e.g. %FU_LID_STATE_CLOSED |
1864 | | * |
1865 | | * Sets the laptop lid state, if applicable. |
1866 | | **/ |
1867 | | void |
1868 | | fu_context_set_lid_state(FuContext *self, FuLidState lid_state) |
1869 | 0 | { |
1870 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1871 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1872 | 0 | if (priv->lid_state == lid_state) |
1873 | 0 | return; |
1874 | 0 | priv->lid_state = lid_state; |
1875 | 0 | g_info("lid state now %s", fu_lid_state_to_string(lid_state)); |
1876 | 0 | g_object_notify(G_OBJECT(self), "lid-state"); |
1877 | 0 | } |
1878 | | |
1879 | | /** |
1880 | | * fu_context_get_display_state: |
1881 | | * @self: a #FuContext |
1882 | | * |
1883 | | * Gets the display state, if applicable. |
1884 | | * |
1885 | | * Returns: a display state, e.g. %FU_DISPLAY_STATE_CONNECTED |
1886 | | **/ |
1887 | | FuDisplayState |
1888 | | fu_context_get_display_state(FuContext *self) |
1889 | 0 | { |
1890 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1891 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
1892 | 0 | return priv->display_state; |
1893 | 0 | } |
1894 | | |
1895 | | /** |
1896 | | * fu_context_set_display_state: |
1897 | | * @self: a #FuContext |
1898 | | * @display_state: a display state, e.g. %FU_DISPLAY_STATE_CONNECTED |
1899 | | * |
1900 | | * Sets the display state, if applicable. |
1901 | | **/ |
1902 | | void |
1903 | | fu_context_set_display_state(FuContext *self, FuDisplayState display_state) |
1904 | 0 | { |
1905 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1906 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1907 | 0 | if (priv->display_state == display_state) |
1908 | 0 | return; |
1909 | 0 | priv->display_state = display_state; |
1910 | 0 | g_info("display-state now %s", fu_display_state_to_string(display_state)); |
1911 | 0 | g_object_notify(G_OBJECT(self), "display-state"); |
1912 | 0 | } |
1913 | | |
1914 | | /** |
1915 | | * fu_context_get_battery_level: |
1916 | | * @self: a #FuContext |
1917 | | * |
1918 | | * Gets the system battery level in percent. |
1919 | | * |
1920 | | * Returns: percentage value, or %FWUPD_BATTERY_LEVEL_INVALID for unknown |
1921 | | **/ |
1922 | | guint |
1923 | | fu_context_get_battery_level(FuContext *self) |
1924 | 0 | { |
1925 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1926 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), G_MAXUINT); |
1927 | 0 | return priv->battery_level; |
1928 | 0 | } |
1929 | | |
1930 | | /** |
1931 | | * fu_context_set_battery_level: |
1932 | | * @self: a #FuContext |
1933 | | * @battery_level: value |
1934 | | * |
1935 | | * Sets the system battery level in percent. |
1936 | | **/ |
1937 | | void |
1938 | | fu_context_set_battery_level(FuContext *self, guint battery_level) |
1939 | 0 | { |
1940 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1941 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1942 | 0 | g_return_if_fail(battery_level <= FWUPD_BATTERY_LEVEL_INVALID); |
1943 | 0 | if (priv->battery_level == battery_level) |
1944 | 0 | return; |
1945 | 0 | priv->battery_level = battery_level; |
1946 | 0 | g_info("battery level now %u", battery_level); |
1947 | 0 | g_object_notify(G_OBJECT(self), "battery-level"); |
1948 | 0 | } |
1949 | | |
1950 | | /** |
1951 | | * fu_context_get_battery_threshold: |
1952 | | * @self: a #FuContext |
1953 | | * |
1954 | | * Gets the system battery threshold in percent. |
1955 | | * |
1956 | | * Returns: percentage value, or %FWUPD_BATTERY_LEVEL_INVALID for unknown |
1957 | | **/ |
1958 | | guint |
1959 | | fu_context_get_battery_threshold(FuContext *self) |
1960 | 0 | { |
1961 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1962 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), G_MAXUINT); |
1963 | 0 | return priv->battery_threshold; |
1964 | 0 | } |
1965 | | |
1966 | | /** |
1967 | | * fu_context_set_battery_threshold: |
1968 | | * @self: a #FuContext |
1969 | | * @battery_threshold: value |
1970 | | * |
1971 | | * Sets the system battery threshold in percent. |
1972 | | **/ |
1973 | | void |
1974 | | fu_context_set_battery_threshold(FuContext *self, guint battery_threshold) |
1975 | 0 | { |
1976 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
1977 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
1978 | 0 | g_return_if_fail(battery_threshold <= FWUPD_BATTERY_LEVEL_INVALID); |
1979 | 0 | if (priv->battery_threshold == battery_threshold) |
1980 | 0 | return; |
1981 | 0 | priv->battery_threshold = battery_threshold; |
1982 | 0 | g_info("battery threshold now %u", battery_threshold); |
1983 | 0 | g_object_notify(G_OBJECT(self), "battery-threshold"); |
1984 | 0 | } |
1985 | | |
1986 | | /** |
1987 | | * fu_context_add_flag: |
1988 | | * @context: a #FuContext |
1989 | | * @flag: the context flag |
1990 | | * |
1991 | | * Adds a specific flag to the context. |
1992 | | **/ |
1993 | | void |
1994 | | fu_context_add_flag(FuContext *context, FuContextFlags flag) |
1995 | 0 | { |
1996 | 0 | FuContextPrivate *priv = GET_PRIVATE(context); |
1997 | 0 | g_return_if_fail(FU_IS_CONTEXT(context)); |
1998 | 0 | if (priv->flags & flag) |
1999 | 0 | return; |
2000 | 0 | priv->flags |= flag; |
2001 | 0 | g_object_notify(G_OBJECT(context), "flags"); |
2002 | 0 | } |
2003 | | |
2004 | | /** |
2005 | | * fu_context_remove_flag: |
2006 | | * @context: a #FuContext |
2007 | | * @flag: the context flag |
2008 | | * |
2009 | | * Removes a specific flag from the context. |
2010 | | **/ |
2011 | | void |
2012 | | fu_context_remove_flag(FuContext *context, FuContextFlags flag) |
2013 | 0 | { |
2014 | 0 | FuContextPrivate *priv = GET_PRIVATE(context); |
2015 | 0 | g_return_if_fail(FU_IS_CONTEXT(context)); |
2016 | 0 | if ((priv->flags & flag) == 0) |
2017 | 0 | return; |
2018 | 0 | priv->flags &= ~flag; |
2019 | 0 | g_object_notify(G_OBJECT(context), "flags"); |
2020 | 0 | } |
2021 | | |
2022 | | /** |
2023 | | * fu_context_has_flag: |
2024 | | * @context: a #FuContext |
2025 | | * @flag: the context flag |
2026 | | * |
2027 | | * Finds if the context has a specific flag. |
2028 | | * |
2029 | | * Returns: %TRUE if the flag is set |
2030 | | **/ |
2031 | | gboolean |
2032 | | fu_context_has_flag(FuContext *context, FuContextFlags flag) |
2033 | 0 | { |
2034 | 0 | FuContextPrivate *priv = GET_PRIVATE(context); |
2035 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(context), FALSE); |
2036 | 0 | return (priv->flags & flag) > 0; |
2037 | 0 | } |
2038 | | |
2039 | | static void |
2040 | | fu_context_esp_write_file_cb(FuVolume *volume, const gchar *filename, FuContext *self) |
2041 | 0 | { |
2042 | 0 | g_signal_emit(self, signals[SIGNAL_ESP_WRITE], 0, volume, filename); |
2043 | 0 | } |
2044 | | |
2045 | | /** |
2046 | | * fu_context_add_esp_volume: |
2047 | | * @self: a #FuContext |
2048 | | * @volume: a #FuVolume |
2049 | | * |
2050 | | * Adds an ESP volume location. |
2051 | | **/ |
2052 | | void |
2053 | | fu_context_add_esp_volume(FuContext *self, FuVolume *volume) |
2054 | 0 | { |
2055 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2056 | |
|
2057 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
2058 | 0 | g_return_if_fail(FU_IS_VOLUME(volume)); |
2059 | | |
2060 | | /* check for dupes */ |
2061 | 0 | for (guint i = 0; i < priv->esp_volumes->len; i++) { |
2062 | 0 | FuVolume *volume_tmp = g_ptr_array_index(priv->esp_volumes, i); |
2063 | 0 | if (g_strcmp0(fu_volume_get_id(volume_tmp), fu_volume_get_id(volume)) == 0) { |
2064 | 0 | g_debug("not adding duplicate volume %s", fu_volume_get_id(volume)); |
2065 | 0 | return; |
2066 | 0 | } |
2067 | 0 | } |
2068 | | |
2069 | | /* add */ |
2070 | 0 | g_signal_connect_object(FU_VOLUME(volume), |
2071 | 0 | "write-file", |
2072 | 0 | G_CALLBACK(fu_context_esp_write_file_cb), |
2073 | 0 | self, |
2074 | 0 | G_CONNECT_DEFAULT); |
2075 | 0 | g_ptr_array_add(priv->esp_volumes, g_object_ref(volume)); |
2076 | 0 | } |
2077 | | |
2078 | | /** |
2079 | | * fu_context_set_esp_location: |
2080 | | * @self: A #FuContext object. |
2081 | | * @location: (nullable): The path to the preferred ESP, or %NULL for automatic selection. |
2082 | | * |
2083 | | * Sets the user's desired ESP (EFI System Partition) location for the given #FuContext. |
2084 | | */ |
2085 | | void |
2086 | | fu_context_set_esp_location(FuContext *self, const gchar *location) |
2087 | 0 | { |
2088 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2089 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
2090 | 0 | g_set_str(&priv->esp_location, location); |
2091 | 0 | } |
2092 | | |
2093 | | /** |
2094 | | * fu_context_get_esp_location: |
2095 | | * @self: The FuContext object. |
2096 | | * |
2097 | | * Retrieves the user's desired ESP (EFI System Partition) location for the given #FuContext |
2098 | | * |
2099 | | * Return: The preferred ESP location as a string. |
2100 | | */ |
2101 | | const gchar * |
2102 | | fu_context_get_esp_location(FuContext *self) |
2103 | 0 | { |
2104 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2105 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2106 | 0 | return priv->esp_location; |
2107 | 0 | } |
2108 | | |
2109 | | /** |
2110 | | * fu_context_get_esp_volumes: |
2111 | | * @self: a #FuContext |
2112 | | * @error: (nullable): optional return location for an error |
2113 | | * |
2114 | | * Finds all volumes that could be an ESP. |
2115 | | * |
2116 | | * The volumes are cached and so subsequent calls to this function will be much faster. |
2117 | | * |
2118 | | * Returns: (transfer container) (element-type FuVolume): a #GPtrArray, or %NULL if no ESP was found |
2119 | | **/ |
2120 | | GPtrArray * |
2121 | | fu_context_get_esp_volumes(FuContext *self, GError **error) |
2122 | 0 | { |
2123 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2124 | 0 | const gchar *path_tmp; |
2125 | 0 | g_autoptr(GError) error_bdp = NULL; |
2126 | 0 | g_autoptr(GError) error_esp = NULL; |
2127 | 0 | g_autoptr(GPtrArray) volumes_bdp = NULL; |
2128 | 0 | g_autoptr(GPtrArray) volumes_esp = NULL; |
2129 | |
|
2130 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2131 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
2132 | | |
2133 | | /* cached result */ |
2134 | 0 | if (priv->esp_volumes->len > 0) |
2135 | 0 | return g_ptr_array_ref(priv->esp_volumes); |
2136 | | |
2137 | | /* for the test suite use local directory for ESP */ |
2138 | 0 | path_tmp = fu_context_get_path(self, FU_PATH_KIND_UEFI_ESP, NULL); |
2139 | 0 | if (path_tmp != NULL) { |
2140 | 0 | g_autoptr(FuVolume) vol = fu_volume_new_from_mount_path(path_tmp); |
2141 | 0 | fu_volume_set_partition_kind(vol, FU_VOLUME_KIND_ESP); |
2142 | 0 | fu_volume_set_partition_uuid(vol, "00000000-0000-0000-0000-000000000000"); |
2143 | 0 | fu_context_add_esp_volume(self, vol); |
2144 | 0 | return g_ptr_array_ref(priv->esp_volumes); |
2145 | 0 | } |
2146 | | |
2147 | | /* ESP */ |
2148 | 0 | volumes_esp = fu_volume_new_by_kind(FU_VOLUME_KIND_ESP, &error_esp); |
2149 | 0 | if (volumes_esp == NULL) { |
2150 | 0 | g_debug("%s", error_esp->message); |
2151 | 0 | } else { |
2152 | 0 | for (guint i = 0; i < volumes_esp->len; i++) { |
2153 | 0 | FuVolume *vol = g_ptr_array_index(volumes_esp, i); |
2154 | 0 | g_autofree gchar *type = fu_volume_get_id_type(vol); |
2155 | 0 | if (g_strcmp0(type, "vfat") != 0) |
2156 | 0 | continue; |
2157 | 0 | fu_context_add_esp_volume(self, vol); |
2158 | 0 | } |
2159 | 0 | } |
2160 | | |
2161 | | /* BDP */ |
2162 | 0 | volumes_bdp = fu_volume_new_by_kind(FU_VOLUME_KIND_BDP, &error_bdp); |
2163 | 0 | if (volumes_bdp == NULL) { |
2164 | 0 | g_debug("%s", error_bdp->message); |
2165 | 0 | } else { |
2166 | 0 | for (guint i = 0; i < volumes_bdp->len; i++) { |
2167 | 0 | FuVolume *vol = g_ptr_array_index(volumes_bdp, i); |
2168 | 0 | g_autofree gchar *type = fu_volume_get_id_type(vol); |
2169 | 0 | if (g_strcmp0(type, "vfat") != 0) |
2170 | 0 | continue; |
2171 | 0 | if (!fu_volume_is_internal(vol)) |
2172 | 0 | continue; |
2173 | 0 | fu_context_add_esp_volume(self, vol); |
2174 | 0 | } |
2175 | 0 | } |
2176 | | |
2177 | | /* nothing found */ |
2178 | 0 | if (priv->esp_volumes->len == 0) { |
2179 | 0 | g_autoptr(GPtrArray) devices = NULL; |
2180 | | |
2181 | | /* check if udisks2 is working */ |
2182 | 0 | devices = fu_common_get_block_devices(error); |
2183 | 0 | if (devices == NULL) |
2184 | 0 | return NULL; |
2185 | 0 | g_set_error_literal(error, |
2186 | 0 | FWUPD_ERROR, |
2187 | 0 | FWUPD_ERROR_NOT_FOUND, |
2188 | 0 | "No ESP or BDP found"); |
2189 | 0 | return NULL; |
2190 | 0 | } |
2191 | | |
2192 | | /* success */ |
2193 | 0 | return g_ptr_array_ref(priv->esp_volumes); |
2194 | 0 | } |
2195 | | |
2196 | | static gboolean |
2197 | | fu_context_is_esp(FuVolume *esp) |
2198 | 0 | { |
2199 | 0 | g_autofree gchar *mount_point = fu_volume_get_mount_point(esp); |
2200 | 0 | g_autofree gchar *fn = NULL; |
2201 | 0 | g_autofree gchar *fn2 = NULL; |
2202 | |
|
2203 | 0 | if (mount_point == NULL) |
2204 | 0 | return FALSE; |
2205 | | |
2206 | 0 | fn = g_build_filename(mount_point, "EFI", NULL); |
2207 | 0 | fn2 = g_build_filename(mount_point, "efi", NULL); |
2208 | |
|
2209 | 0 | return g_file_test(fn, G_FILE_TEST_IS_DIR) || g_file_test(fn2, G_FILE_TEST_IS_DIR); |
2210 | 0 | } |
2211 | | |
2212 | | static gboolean |
2213 | | fu_context_is_esp_linux(FuVolume *esp, GError **error) |
2214 | 0 | { |
2215 | 0 | const gchar *prefixes[] = {"grub", "shim", "systemd-boot", "zfsbootmenu", NULL}; |
2216 | 0 | g_autofree gchar *prefixes_str = NULL; |
2217 | 0 | g_autofree gchar *mount_point = fu_volume_get_mount_point(esp); |
2218 | 0 | g_autoptr(GPtrArray) files = NULL; |
2219 | | |
2220 | | /* look for any likely basenames */ |
2221 | 0 | if (mount_point == NULL) { |
2222 | 0 | g_set_error_literal(error, |
2223 | 0 | FWUPD_ERROR, |
2224 | 0 | FWUPD_ERROR_NOT_SUPPORTED, |
2225 | 0 | "no mountpoint for ESP"); |
2226 | 0 | return FALSE; |
2227 | 0 | } |
2228 | 0 | files = fu_path_get_files(mount_point, error); |
2229 | 0 | if (files == NULL) |
2230 | 0 | return FALSE; |
2231 | 0 | for (guint i = 0; i < files->len; i++) { |
2232 | 0 | const gchar *fn = g_ptr_array_index(files, i); |
2233 | 0 | g_autofree gchar *basename = g_path_get_basename(fn); |
2234 | 0 | g_autofree gchar *basename_lower = g_utf8_strdown(basename, -1); |
2235 | |
|
2236 | 0 | for (guint j = 0; prefixes[j] != NULL; j++) { |
2237 | 0 | if (!g_str_has_prefix(basename_lower, prefixes[j])) |
2238 | 0 | continue; |
2239 | 0 | if (!g_str_has_suffix(basename_lower, ".efi")) |
2240 | 0 | continue; |
2241 | 0 | g_info("found %s which indicates a Linux ESP, using %s", fn, mount_point); |
2242 | 0 | return TRUE; |
2243 | 0 | } |
2244 | 0 | } |
2245 | | |
2246 | | /* failed */ |
2247 | 0 | prefixes_str = g_strjoinv("|", (gchar **)prefixes); |
2248 | 0 | g_set_error(error, |
2249 | 0 | FWUPD_ERROR, |
2250 | 0 | FWUPD_ERROR_NOT_FOUND, |
2251 | 0 | "did not any files with prefix %s in %s", |
2252 | 0 | prefixes_str, |
2253 | 0 | mount_point); |
2254 | 0 | return FALSE; |
2255 | 0 | } |
2256 | | |
2257 | | static gint |
2258 | | fu_context_sort_esp_score_cb(gconstpointer a, gconstpointer b, gpointer user_data) |
2259 | 0 | { |
2260 | 0 | GHashTable *esp_scores = (GHashTable *)user_data; |
2261 | 0 | guint esp1_score = GPOINTER_TO_UINT(g_hash_table_lookup(esp_scores, *((FuVolume **)a))); |
2262 | 0 | guint esp2_score = GPOINTER_TO_UINT(g_hash_table_lookup(esp_scores, *((FuVolume **)b))); |
2263 | 0 | if (esp1_score < esp2_score) |
2264 | 0 | return 1; |
2265 | 0 | if (esp1_score > esp2_score) |
2266 | 0 | return -1; |
2267 | 0 | return 0; |
2268 | 0 | } |
2269 | | |
2270 | | /** |
2271 | | * fu_context_get_default_esp: |
2272 | | * @self: a #FuContext |
2273 | | * @error: (nullable): optional return location for an error |
2274 | | * |
2275 | | * Finds the volume that represents the ESP that plugins should nominally |
2276 | | * use for accessing storing data. |
2277 | | * |
2278 | | * Returns: (transfer full): a volume, or %NULL if no ESP was found |
2279 | | **/ |
2280 | | FuVolume * |
2281 | | fu_context_get_default_esp(FuContext *self, GError **error) |
2282 | 0 | { |
2283 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2284 | 0 | g_autoptr(GPtrArray) esp_volumes = NULL; |
2285 | 0 | const gchar *user_esp_location = fu_context_get_esp_location(self); |
2286 | |
|
2287 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2288 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
2289 | | |
2290 | | /* show which volumes we're choosing from */ |
2291 | 0 | esp_volumes = fu_context_get_esp_volumes(self, error); |
2292 | 0 | if (esp_volumes == NULL) |
2293 | 0 | return NULL; |
2294 | | |
2295 | | /* no mounting */ |
2296 | 0 | if (priv->flags & FU_CONTEXT_FLAG_INHIBIT_VOLUME_MOUNT) { |
2297 | 0 | g_set_error_literal(error, |
2298 | 0 | FWUPD_ERROR, |
2299 | 0 | FWUPD_ERROR_NOT_SUPPORTED, |
2300 | 0 | "cannot mount volume by policy"); |
2301 | 0 | return NULL; |
2302 | 0 | } |
2303 | | |
2304 | | /* we found more than one: lets look for the best one */ |
2305 | 0 | if (esp_volumes->len > 1) { |
2306 | 0 | g_autoptr(GString) str = g_string_new("more than one ESP possible:"); |
2307 | 0 | g_autoptr(GHashTable) esp_scores = g_hash_table_new(g_direct_hash, g_direct_equal); |
2308 | 0 | for (guint i = 0; i < esp_volumes->len; i++) { |
2309 | 0 | FuVolume *esp = g_ptr_array_index(esp_volumes, i); |
2310 | 0 | guint score = 0; |
2311 | 0 | g_autofree gchar *kind = NULL; |
2312 | 0 | g_autoptr(FuVolumeLocker) locker = NULL; |
2313 | 0 | g_autoptr(GError) error_local = NULL; |
2314 | | |
2315 | | /* ignore the volume completely if we cannot mount it */ |
2316 | 0 | locker = fu_volume_locker_new(esp, &error_local); |
2317 | 0 | if (locker == NULL) { |
2318 | 0 | g_warning("failed to mount ESP: %s", error_local->message); |
2319 | 0 | continue; |
2320 | 0 | } |
2321 | | |
2322 | | /* if user specified, make sure that it matches */ |
2323 | 0 | if (user_esp_location != NULL) { |
2324 | 0 | g_autofree gchar *mount = fu_volume_get_mount_point(esp); |
2325 | 0 | if (g_strcmp0(mount, user_esp_location) != 0) { |
2326 | 0 | g_debug("skipping %s as it's not the user " |
2327 | 0 | "specified ESP", |
2328 | 0 | mount); |
2329 | 0 | continue; |
2330 | 0 | } |
2331 | 0 | } |
2332 | | |
2333 | 0 | if (!fu_context_is_esp(esp)) { |
2334 | 0 | g_debug("not an ESP: %s", fu_volume_get_id(esp)); |
2335 | 0 | continue; |
2336 | 0 | } |
2337 | | |
2338 | | /* big partitions are better than small partitions */ |
2339 | 0 | score += fu_volume_get_size(esp) / FU_MB; |
2340 | | |
2341 | | /* prefer partitions with the ESP flag set over msftdata */ |
2342 | 0 | kind = fu_volume_get_partition_kind(esp); |
2343 | 0 | if (g_strcmp0(kind, FU_VOLUME_KIND_ESP) == 0) |
2344 | 0 | score += 0x20000; |
2345 | | |
2346 | | /* prefer linux ESP */ |
2347 | 0 | if (!fu_context_is_esp_linux(esp, &error_local)) { |
2348 | 0 | g_debug("not a Linux ESP: %s", error_local->message); |
2349 | 0 | } else { |
2350 | 0 | score += 64 * FU_KB; |
2351 | 0 | } |
2352 | 0 | g_hash_table_insert(esp_scores, (gpointer)esp, GUINT_TO_POINTER(score)); |
2353 | 0 | } |
2354 | |
|
2355 | 0 | if (g_hash_table_size(esp_scores) == 0) { |
2356 | 0 | g_set_error_literal(error, |
2357 | 0 | FWUPD_ERROR, |
2358 | 0 | FWUPD_ERROR_NOT_SUPPORTED, |
2359 | 0 | "no EFI system partition found"); |
2360 | 0 | return NULL; |
2361 | 0 | } |
2362 | | |
2363 | 0 | g_ptr_array_sort_with_data(esp_volumes, fu_context_sort_esp_score_cb, esp_scores); |
2364 | 0 | for (guint i = 0; i < esp_volumes->len; i++) { |
2365 | 0 | FuVolume *esp = g_ptr_array_index(esp_volumes, i); |
2366 | 0 | guint score = GPOINTER_TO_UINT(g_hash_table_lookup(esp_scores, esp)); |
2367 | 0 | g_string_append_printf(str, "\n - 0x%x:\t%s", score, fu_volume_get_id(esp)); |
2368 | 0 | } |
2369 | 0 | g_debug("%s", str->str); |
2370 | 0 | } |
2371 | | |
2372 | 0 | if (esp_volumes->len == 1) { |
2373 | 0 | FuVolume *esp = g_ptr_array_index(esp_volumes, 0); |
2374 | 0 | g_autoptr(FuVolumeLocker) locker = NULL; |
2375 | | |
2376 | | /* ensure it can be mounted */ |
2377 | 0 | locker = fu_volume_locker_new(esp, error); |
2378 | 0 | if (locker == NULL) |
2379 | 0 | return NULL; |
2380 | | |
2381 | | /* if user specified, does it match mountpoints ? */ |
2382 | 0 | if (user_esp_location != NULL) { |
2383 | 0 | g_autofree gchar *mount = fu_volume_get_mount_point(esp); |
2384 | |
|
2385 | 0 | if (g_strcmp0(mount, user_esp_location) != 0) { |
2386 | 0 | g_set_error(error, |
2387 | 0 | FWUPD_ERROR, |
2388 | 0 | FWUPD_ERROR_NOT_SUPPORTED, |
2389 | 0 | "user specified ESP %s not found", |
2390 | 0 | user_esp_location); |
2391 | 0 | return NULL; |
2392 | 0 | } |
2393 | 0 | } |
2394 | 0 | } |
2395 | | |
2396 | | /* "success" */ |
2397 | 0 | return g_object_ref(g_ptr_array_index(esp_volumes, 0)); |
2398 | 0 | } |
2399 | | |
2400 | | /** |
2401 | | * fu_context_get_esp_volume_by_hard_drive_device_path: |
2402 | | * @self: a #FuContext |
2403 | | * @dp: a #FuEfiHardDriveDevicePath |
2404 | | * @error: (nullable): optional return location for an error |
2405 | | * |
2406 | | * Gets a volume that matches the EFI device path |
2407 | | * |
2408 | | * Returns: (transfer full): a volume, or %NULL if it was not found |
2409 | | **/ |
2410 | | FuVolume * |
2411 | | fu_context_get_esp_volume_by_hard_drive_device_path(FuContext *self, |
2412 | | FuEfiHardDriveDevicePath *dp, |
2413 | | GError **error) |
2414 | 0 | { |
2415 | 0 | g_autoptr(GPtrArray) volumes = NULL; |
2416 | |
|
2417 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2418 | 0 | g_return_val_if_fail(FU_IS_EFI_HARD_DRIVE_DEVICE_PATH(dp), NULL); |
2419 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
2420 | | |
2421 | 0 | volumes = fu_context_get_esp_volumes(self, error); |
2422 | 0 | if (volumes == NULL) |
2423 | 0 | return NULL; |
2424 | 0 | for (guint i = 0; i < volumes->len; i++) { |
2425 | 0 | FuVolume *volume = g_ptr_array_index(volumes, i); |
2426 | 0 | g_autoptr(GError) error_local = NULL; |
2427 | 0 | g_autoptr(FuEfiHardDriveDevicePath) dp_tmp = NULL; |
2428 | |
|
2429 | 0 | dp_tmp = fu_efi_hard_drive_device_path_new_from_volume(volume, &error_local); |
2430 | 0 | if (dp_tmp == NULL) { |
2431 | 0 | g_debug("%s", error_local->message); |
2432 | 0 | continue; |
2433 | 0 | } |
2434 | 0 | if (!fu_efi_hard_drive_device_path_compare(dp, dp_tmp)) |
2435 | 0 | continue; |
2436 | 0 | return g_object_ref(volume); |
2437 | 0 | } |
2438 | | |
2439 | | /* failed */ |
2440 | 0 | g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, "could not find EFI DP"); |
2441 | 0 | return NULL; |
2442 | 0 | } |
2443 | | |
2444 | | static FuFirmware * |
2445 | | fu_context_esp_load_pe_file(const gchar *filename, GError **error) |
2446 | 0 | { |
2447 | 0 | g_autoptr(FuFirmware) firmware = fu_pefile_firmware_new(); |
2448 | 0 | g_autoptr(GFile) file = g_file_new_for_path(filename); |
2449 | 0 | fu_firmware_set_filename(firmware, filename); |
2450 | 0 | if (!fu_firmware_parse_file(firmware, file, FU_FIRMWARE_PARSE_FLAG_NONE, error)) { |
2451 | 0 | g_prefix_error(error, "failed to load %s: ", filename); |
2452 | 0 | return NULL; |
2453 | 0 | } |
2454 | 0 | return g_steal_pointer(&firmware); |
2455 | 0 | } |
2456 | | |
2457 | | static gchar * |
2458 | | fu_context_build_uefi_basename_for_arch(const gchar *app_name) |
2459 | 0 | { |
2460 | 0 | #ifdef __x86_64__ |
2461 | 0 | return g_strdup_printf("%sx64.efi", app_name); |
2462 | 0 | #endif |
2463 | | #ifdef __aarch64__ |
2464 | | return g_strdup_printf("%saa64.efi", app_name); |
2465 | | #endif |
2466 | | #ifdef __loongarch_lp64 |
2467 | | return g_strdup_printf("%sloongarch64.efi", app_name); |
2468 | | #endif |
2469 | | #if (defined(__riscv) && __riscv_xlen == 64) |
2470 | | return g_strdup_printf("%sriscv64.efi", app_name); |
2471 | | #endif |
2472 | | #if defined(__i386__) || defined(__i686__) |
2473 | | return g_strdup_printf("%sia32.efi", app_name); |
2474 | | #endif |
2475 | | #ifdef __arm__ |
2476 | | return g_strdup_printf("%sarm.efi", app_name); |
2477 | | #endif |
2478 | 0 | return NULL; |
2479 | 0 | } |
2480 | | |
2481 | | static gboolean |
2482 | | fu_context_get_esp_files_for_entry(FuContext *self, |
2483 | | FuEfiLoadOption *entry, |
2484 | | GPtrArray *files, |
2485 | | FuContextEspFileFlags flags, |
2486 | | GError **error) |
2487 | 0 | { |
2488 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2489 | 0 | g_autofree gchar *dp_filename = NULL; |
2490 | 0 | g_autofree gchar *filename = NULL; |
2491 | 0 | g_autofree gchar *mount_point = NULL; |
2492 | 0 | g_autofree gchar *shim_name = fu_context_build_uefi_basename_for_arch("shim"); |
2493 | 0 | g_autoptr(FuVolumeLocker) volume_locker = NULL; |
2494 | 0 | g_autoptr(FuEfiFilePathDevicePath) dp_path = NULL; |
2495 | 0 | g_autoptr(FuEfiHardDriveDevicePath) dp_hdd = NULL; |
2496 | 0 | g_autoptr(FuFirmware) dp_list = NULL; |
2497 | 0 | g_autoptr(FuVolume) volume = NULL; |
2498 | | |
2499 | | /* all entries should have a list */ |
2500 | 0 | dp_list = |
2501 | 0 | fu_firmware_get_image_by_gtype(FU_FIRMWARE(entry), FU_TYPE_EFI_DEVICE_PATH_LIST, NULL); |
2502 | 0 | if (dp_list == NULL) |
2503 | 0 | return TRUE; |
2504 | | |
2505 | | /* HDD */ |
2506 | 0 | dp_hdd = FU_EFI_HARD_DRIVE_DEVICE_PATH( |
2507 | 0 | fu_firmware_get_image_by_gtype(FU_FIRMWARE(dp_list), |
2508 | 0 | FU_TYPE_EFI_HARD_DRIVE_DEVICE_PATH, |
2509 | 0 | NULL)); |
2510 | 0 | if (dp_hdd == NULL) |
2511 | 0 | return TRUE; |
2512 | | |
2513 | | /* FILE */ |
2514 | 0 | dp_path = FU_EFI_FILE_PATH_DEVICE_PATH( |
2515 | 0 | fu_firmware_get_image_by_gtype(FU_FIRMWARE(dp_list), |
2516 | 0 | FU_TYPE_EFI_FILE_PATH_DEVICE_PATH, |
2517 | 0 | NULL)); |
2518 | 0 | if (dp_path == NULL) |
2519 | 0 | return TRUE; |
2520 | | |
2521 | | /* can we match the volume? */ |
2522 | 0 | volume = fu_context_get_esp_volume_by_hard_drive_device_path(self, dp_hdd, error); |
2523 | 0 | if (volume == NULL) |
2524 | 0 | return FALSE; |
2525 | 0 | if (priv->flags & FU_CONTEXT_FLAG_INHIBIT_VOLUME_MOUNT) { |
2526 | 0 | g_set_error_literal(error, |
2527 | 0 | FWUPD_ERROR, |
2528 | 0 | FWUPD_ERROR_NOT_SUPPORTED, |
2529 | 0 | "cannot mount volume by policy"); |
2530 | 0 | return FALSE; |
2531 | 0 | } |
2532 | 0 | volume_locker = fu_volume_locker_new(volume, error); |
2533 | 0 | if (volume_locker == NULL) |
2534 | 0 | return FALSE; |
2535 | 0 | dp_filename = fu_efi_file_path_device_path_get_name(dp_path, error); |
2536 | 0 | if (dp_filename == NULL) |
2537 | 0 | return FALSE; |
2538 | 0 | if (!fu_path_verify_safe(dp_filename, error)) |
2539 | 0 | return FALSE; |
2540 | | |
2541 | | /* the file itself */ |
2542 | 0 | mount_point = fu_volume_get_mount_point(volume); |
2543 | 0 | filename = g_build_filename(mount_point, dp_filename, NULL); |
2544 | 0 | g_debug("check for 1st stage bootloader: %s", filename); |
2545 | 0 | if (flags & FU_CONTEXT_ESP_FILE_FLAG_INCLUDE_FIRST_STAGE) { |
2546 | 0 | g_autoptr(FuFirmware) firmware = NULL; |
2547 | 0 | g_autoptr(GError) error_local = NULL; |
2548 | | |
2549 | | /* ignore if the file cannot be loaded as a PE file */ |
2550 | 0 | firmware = fu_context_esp_load_pe_file(filename, &error_local); |
2551 | 0 | if (firmware == NULL) { |
2552 | 0 | if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED) || |
2553 | 0 | g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_INVALID_FILE)) { |
2554 | 0 | g_debug("ignoring: %s", error_local->message); |
2555 | 0 | } else { |
2556 | 0 | g_propagate_error(error, g_steal_pointer(&error_local)); |
2557 | 0 | return FALSE; |
2558 | 0 | } |
2559 | 0 | } else { |
2560 | 0 | fu_firmware_set_idx(firmware, fu_firmware_get_idx(FU_FIRMWARE(entry))); |
2561 | 0 | g_ptr_array_add(files, g_steal_pointer(&firmware)); |
2562 | 0 | } |
2563 | 0 | } |
2564 | | |
2565 | | /* the 2nd stage bootloader, typically grub */ |
2566 | 0 | if (flags & FU_CONTEXT_ESP_FILE_FLAG_INCLUDE_SECOND_STAGE && |
2567 | 0 | g_str_has_suffix(filename, shim_name)) { |
2568 | 0 | g_autoptr(FuFirmware) firmware = NULL; |
2569 | 0 | g_autoptr(GError) error_local = NULL; |
2570 | 0 | g_autoptr(GString) filename2 = g_string_new(filename); |
2571 | 0 | const gchar *path; |
2572 | |
|
2573 | 0 | path = |
2574 | 0 | fu_efi_load_option_get_metadata(entry, FU_EFI_LOAD_OPTION_METADATA_PATH, NULL); |
2575 | 0 | if (path != NULL) { |
2576 | 0 | g_string_replace(filename2, shim_name, path, 1); |
2577 | 0 | } else { |
2578 | 0 | g_autofree gchar *grub_name = |
2579 | 0 | fu_context_build_uefi_basename_for_arch("grub"); |
2580 | 0 | g_string_replace(filename2, shim_name, grub_name, 1); |
2581 | 0 | } |
2582 | 0 | g_debug("check for 2nd stage bootloader: %s", filename2->str); |
2583 | | |
2584 | | /* ignore if the file cannot be loaded as a PE file */ |
2585 | 0 | firmware = fu_context_esp_load_pe_file(filename2->str, &error_local); |
2586 | 0 | if (firmware == NULL) { |
2587 | 0 | if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED) || |
2588 | 0 | g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_INVALID_FILE)) { |
2589 | 0 | g_debug("ignoring: %s", error_local->message); |
2590 | 0 | } else { |
2591 | 0 | g_propagate_error(error, g_steal_pointer(&error_local)); |
2592 | 0 | return FALSE; |
2593 | 0 | } |
2594 | 0 | } else { |
2595 | 0 | fu_firmware_set_idx(firmware, fu_firmware_get_idx(FU_FIRMWARE(entry))); |
2596 | 0 | g_ptr_array_add(files, g_steal_pointer(&firmware)); |
2597 | 0 | } |
2598 | 0 | } |
2599 | | |
2600 | | /* revocations, typically for SBAT */ |
2601 | 0 | if (flags & FU_CONTEXT_ESP_FILE_FLAG_INCLUDE_REVOCATIONS && |
2602 | 0 | g_str_has_suffix(filename, shim_name)) { |
2603 | 0 | g_autoptr(GString) filename2 = g_string_new(filename); |
2604 | 0 | g_autoptr(FuFirmware) firmware = NULL; |
2605 | 0 | g_autoptr(GError) error_local = NULL; |
2606 | |
|
2607 | 0 | g_string_replace(filename2, shim_name, "revocations.efi", 1); |
2608 | 0 | g_debug("check for revocation: %s", filename2->str); |
2609 | | |
2610 | | /* ignore if the file cannot be loaded as a PE file */ |
2611 | 0 | firmware = fu_context_esp_load_pe_file(filename2->str, &error_local); |
2612 | 0 | if (firmware == NULL) { |
2613 | 0 | if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED) || |
2614 | 0 | g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_INVALID_FILE)) { |
2615 | 0 | g_debug("ignoring: %s", error_local->message); |
2616 | 0 | } else { |
2617 | 0 | g_propagate_error(error, g_steal_pointer(&error_local)); |
2618 | 0 | return FALSE; |
2619 | 0 | } |
2620 | 0 | } else { |
2621 | 0 | fu_firmware_set_idx(firmware, fu_firmware_get_idx(FU_FIRMWARE(entry))); |
2622 | 0 | g_ptr_array_add(files, g_steal_pointer(&firmware)); |
2623 | 0 | } |
2624 | 0 | } |
2625 | | |
2626 | | /* success */ |
2627 | 0 | return TRUE; |
2628 | 0 | } |
2629 | | |
2630 | | /** |
2631 | | * fu_context_get_esp_files: |
2632 | | * @self: a #FuContext |
2633 | | * @flags: some #FuContextEspFileFlags, e.g. #FU_CONTEXT_ESP_FILE_FLAG_INCLUDE_FIRST_STAGE |
2634 | | * @error: #GError |
2635 | | * |
2636 | | * Gets the PE files for all the entries listed in `BootOrder`. |
2637 | | * |
2638 | | * Returns: (transfer full) (element-type FuPefileFirmware): PE firmware data |
2639 | | **/ |
2640 | | GPtrArray * |
2641 | | fu_context_get_esp_files(FuContext *self, FuContextEspFileFlags flags, GError **error) |
2642 | 0 | { |
2643 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2644 | 0 | g_autoptr(GPtrArray) entries = NULL; |
2645 | 0 | g_autoptr(GPtrArray) files = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref); |
2646 | |
|
2647 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2648 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
2649 | | |
2650 | 0 | entries = fu_efivars_get_boot_entries(priv->efivars, error); |
2651 | 0 | if (entries == NULL) |
2652 | 0 | return NULL; |
2653 | 0 | for (guint i = 0; i < entries->len; i++) { |
2654 | 0 | FuEfiLoadOption *entry = g_ptr_array_index(entries, i); |
2655 | 0 | g_autoptr(GError) error_local = NULL; |
2656 | 0 | if (!fu_context_get_esp_files_for_entry(self, entry, files, flags, &error_local)) { |
2657 | 0 | if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND) || |
2658 | 0 | g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED) || |
2659 | 0 | g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_INVALID_FILE)) { |
2660 | 0 | g_debug("ignoring %s: %s", |
2661 | 0 | fu_firmware_get_id(FU_FIRMWARE(entry)), |
2662 | 0 | error_local->message); |
2663 | 0 | continue; |
2664 | 0 | } |
2665 | 0 | g_propagate_error(error, g_steal_pointer(&error_local)); |
2666 | 0 | return NULL; |
2667 | 0 | } |
2668 | 0 | } |
2669 | | |
2670 | | /* success */ |
2671 | 0 | return g_steal_pointer(&files); |
2672 | 0 | } |
2673 | | |
2674 | | /** |
2675 | | * fu_context_get_backends: |
2676 | | * @self: a #FuContext |
2677 | | * |
2678 | | * Gets all the possible backends used by all plugins. |
2679 | | * |
2680 | | * Returns: (transfer none) (element-type FuBackend): List of backends |
2681 | | **/ |
2682 | | GPtrArray * |
2683 | | fu_context_get_backends(FuContext *self) |
2684 | 0 | { |
2685 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2686 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2687 | 0 | return priv->backends; |
2688 | 0 | } |
2689 | | |
2690 | | /** |
2691 | | * fu_context_add_backend: |
2692 | | * @self: a #FuContext |
2693 | | * @backend: a #FuBackend |
2694 | | * |
2695 | | * Adds a backend to the context. |
2696 | | * |
2697 | | * Returns: (transfer full): a #FuBackend, or %NULL on error |
2698 | | **/ |
2699 | | void |
2700 | | fu_context_add_backend(FuContext *self, FuBackend *backend) |
2701 | 0 | { |
2702 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2703 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
2704 | 0 | g_return_if_fail(FU_IS_BACKEND(backend)); |
2705 | 0 | g_ptr_array_add(priv->backends, g_object_ref(backend)); |
2706 | 0 | } |
2707 | | |
2708 | | /** |
2709 | | * fu_context_get_backend_by_name: |
2710 | | * @self: a #FuContext |
2711 | | * @name: backend name, e.g. `udev` or `bluez` |
2712 | | * @error: (nullable): optional return location for an error |
2713 | | * |
2714 | | * Gets a specific backend added to the context. |
2715 | | * |
2716 | | * Returns: (transfer full): a #FuBackend, or %NULL on error |
2717 | | **/ |
2718 | | FuBackend * |
2719 | | fu_context_get_backend_by_name(FuContext *self, const gchar *name, GError **error) |
2720 | 0 | { |
2721 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2722 | |
|
2723 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2724 | 0 | g_return_val_if_fail(name != NULL, NULL); |
2725 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
2726 | | |
2727 | 0 | for (guint i = 0; i < priv->backends->len; i++) { |
2728 | 0 | FuBackend *backend = g_ptr_array_index(priv->backends, i); |
2729 | 0 | if (g_strcmp0(fu_backend_get_name(backend), name) == 0) |
2730 | 0 | return g_object_ref(backend); |
2731 | 0 | } |
2732 | 0 | g_set_error(error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, "no backend with name %s", name); |
2733 | 0 | return NULL; |
2734 | 0 | } |
2735 | | |
2736 | | /* private */ |
2737 | | gboolean |
2738 | | fu_context_has_backend(FuContext *self, const gchar *name) |
2739 | 0 | { |
2740 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2741 | |
|
2742 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FALSE); |
2743 | 0 | g_return_val_if_fail(name != NULL, FALSE); |
2744 | | |
2745 | 0 | for (guint i = 0; i < priv->backends->len; i++) { |
2746 | 0 | FuBackend *backend = g_ptr_array_index(priv->backends, i); |
2747 | 0 | if (g_strcmp0(fu_backend_get_name(backend), name) == 0) |
2748 | 0 | return TRUE; |
2749 | 0 | } |
2750 | 0 | return FALSE; |
2751 | 0 | } |
2752 | | |
2753 | | /* private */ |
2754 | | gpointer |
2755 | | fu_context_get_data(FuContext *self, const gchar *key) |
2756 | 0 | { |
2757 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2758 | 0 | g_return_val_if_fail(key != NULL, NULL); |
2759 | 0 | return g_object_get_data(G_OBJECT(self), key); |
2760 | 0 | } |
2761 | | |
2762 | | /* private */ |
2763 | | void |
2764 | | fu_context_set_data(FuContext *self, const gchar *key, gpointer data) |
2765 | 0 | { |
2766 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
2767 | 0 | g_return_if_fail(key != NULL); |
2768 | 0 | g_object_set_data(G_OBJECT(self), key, data); |
2769 | 0 | } |
2770 | | |
2771 | | /** |
2772 | | * fu_context_get_path: |
2773 | | * @self: a #FuContext |
2774 | | * @kind: a #FuPathKind |
2775 | | * @error: (nullable): optional return location for an error |
2776 | | * |
2777 | | * Gets a well-known path. |
2778 | | * |
2779 | | * Returns: (transfer none): a path, or %NULL on error |
2780 | | **/ |
2781 | | const gchar * |
2782 | | fu_context_get_path(FuContext *self, FuPathKind kind, GError **error) |
2783 | 0 | { |
2784 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2785 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2786 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
2787 | 0 | return fu_path_store_get_path(priv->pstore, kind, error); |
2788 | 0 | } |
2789 | | |
2790 | | /** |
2791 | | * fu_context_build_filename: |
2792 | | * @self: a #FuContext |
2793 | | * @error: (nullable): optional return location for an error |
2794 | | * @kind: a #FuPathKind e.g. %FU_PATH_KIND_DATADIR_PKG |
2795 | | * @...: pairs of string key values, ending with %NULL |
2796 | | * |
2797 | | * Gets a fwupd-specific system path. These can be overridden with various |
2798 | | * environment variables, for instance %FWUPD_DATADIR. |
2799 | | * |
2800 | | * Returns: a system path, or %NULL if invalid |
2801 | | **/ |
2802 | | gchar * |
2803 | | fu_context_build_filename(FuContext *self, GError **error, FuPathKind kind, ...) |
2804 | 0 | { |
2805 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2806 | 0 | va_list args; |
2807 | 0 | gchar *path; |
2808 | 0 | const gchar *path_prefix = NULL; |
2809 | |
|
2810 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2811 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
2812 | | |
2813 | 0 | path_prefix = fu_path_store_get_path(priv->pstore, kind, error); |
2814 | 0 | if (path_prefix == NULL) |
2815 | 0 | return NULL; |
2816 | | |
2817 | 0 | va_start(args, kind); |
2818 | 0 | path = g_build_filename_valist(path_prefix, &args); |
2819 | 0 | va_end(args); |
2820 | |
|
2821 | 0 | return path; |
2822 | 0 | } |
2823 | | |
2824 | | /** |
2825 | | * fu_context_set_path: |
2826 | | * @self: a #FuContext |
2827 | | * @kind: a #FuPathKind |
2828 | | * |
2829 | | * Sets a well-known path. |
2830 | | **/ |
2831 | | void |
2832 | | fu_context_set_path(FuContext *self, FuPathKind kind, const gchar *dirname) |
2833 | 0 | { |
2834 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2835 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
2836 | 0 | fu_path_store_set_path(priv->pstore, kind, dirname); |
2837 | 0 | } |
2838 | | |
2839 | | /** |
2840 | | * fu_context_set_tmpdir: |
2841 | | * @self: a #FuContext |
2842 | | * @tmpdir: (not nullable): a #FuTemporaryDirectory |
2843 | | * |
2844 | | * Sets the @kind to a temporary path. |
2845 | | **/ |
2846 | | void |
2847 | | fu_context_set_tmpdir(FuContext *self, FuPathKind kind, FuTemporaryDirectory *tmpdir) |
2848 | 0 | { |
2849 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2850 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
2851 | 0 | g_return_if_fail(kind < FU_PATH_KIND_LAST); |
2852 | 0 | g_return_if_fail(FU_IS_TEMPORARY_DIRECTORY(tmpdir)); |
2853 | 0 | fu_path_store_set_tmpdir(priv->pstore, kind, tmpdir); |
2854 | 0 | } |
2855 | | |
2856 | | /** |
2857 | | * fu_context_get_path_store: |
2858 | | * @self: a #FuContext |
2859 | | * |
2860 | | * Gets the well-known path store. |
2861 | | * |
2862 | | * Returns: (transfer none): a #FuPathStore, or %NULL if not set |
2863 | | **/ |
2864 | | FuPathStore * |
2865 | | fu_context_get_path_store(FuContext *self) |
2866 | 0 | { |
2867 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2868 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), NULL); |
2869 | 0 | return priv->pstore; |
2870 | 0 | } |
2871 | | |
2872 | | /** |
2873 | | * fu_context_get_cpu_vendor: |
2874 | | * @self: a #FuContext |
2875 | | * |
2876 | | * Uses CPUID to discover the CPU vendor. |
2877 | | * |
2878 | | * Returns: a CPU vendor, e.g. %FU_CPU_VENDOR_AMD if the vendor was AMD. |
2879 | | **/ |
2880 | | FuCpuVendor |
2881 | | fu_context_get_cpu_vendor(FuContext *self) |
2882 | 0 | { |
2883 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2884 | 0 | g_return_val_if_fail(FU_IS_CONTEXT(self), FU_CPU_VENDOR_UNKNOWN); |
2885 | 0 | return priv->cpu_vendor; |
2886 | 0 | } |
2887 | | |
2888 | | /* private */ |
2889 | | void |
2890 | | fu_context_set_cpu_vendor(FuContext *self, FuCpuVendor cpu_vendor) |
2891 | 0 | { |
2892 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2893 | 0 | g_return_if_fail(FU_IS_CONTEXT(self)); |
2894 | 0 | priv->cpu_vendor = cpu_vendor; |
2895 | 0 | } |
2896 | | |
2897 | | static void |
2898 | | fu_context_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) |
2899 | 0 | { |
2900 | 0 | FuContext *self = FU_CONTEXT(object); |
2901 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2902 | 0 | switch (prop_id) { |
2903 | 0 | case PROP_POWER_STATE: |
2904 | 0 | g_value_set_uint(value, priv->power_state); |
2905 | 0 | break; |
2906 | 0 | case PROP_LID_STATE: |
2907 | 0 | g_value_set_uint(value, priv->lid_state); |
2908 | 0 | break; |
2909 | 0 | case PROP_DISPLAY_STATE: |
2910 | 0 | g_value_set_uint(value, priv->display_state); |
2911 | 0 | break; |
2912 | 0 | case PROP_BATTERY_LEVEL: |
2913 | 0 | g_value_set_uint(value, priv->battery_level); |
2914 | 0 | break; |
2915 | 0 | case PROP_BATTERY_THRESHOLD: |
2916 | 0 | g_value_set_uint(value, priv->battery_threshold); |
2917 | 0 | break; |
2918 | 0 | case PROP_FLAGS: |
2919 | 0 | g_value_set_uint64(value, priv->flags); |
2920 | 0 | break; |
2921 | 0 | default: |
2922 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
2923 | 0 | break; |
2924 | 0 | } |
2925 | 0 | } |
2926 | | |
2927 | | static void |
2928 | | fu_context_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) |
2929 | 0 | { |
2930 | 0 | FuContext *self = FU_CONTEXT(object); |
2931 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2932 | 0 | switch (prop_id) { |
2933 | 0 | case PROP_POWER_STATE: |
2934 | 0 | fu_context_set_power_state(self, g_value_get_uint(value)); |
2935 | 0 | break; |
2936 | 0 | case PROP_LID_STATE: |
2937 | 0 | fu_context_set_lid_state(self, g_value_get_uint(value)); |
2938 | 0 | break; |
2939 | 0 | case PROP_DISPLAY_STATE: |
2940 | 0 | fu_context_set_display_state(self, g_value_get_uint(value)); |
2941 | 0 | break; |
2942 | 0 | case PROP_BATTERY_LEVEL: |
2943 | 0 | fu_context_set_battery_level(self, g_value_get_uint(value)); |
2944 | 0 | break; |
2945 | 0 | case PROP_BATTERY_THRESHOLD: |
2946 | 0 | fu_context_set_battery_threshold(self, g_value_get_uint(value)); |
2947 | 0 | break; |
2948 | 0 | case PROP_FLAGS: |
2949 | 0 | priv->flags = g_value_get_uint64(value); |
2950 | 0 | break; |
2951 | 0 | default: |
2952 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
2953 | 0 | break; |
2954 | 0 | } |
2955 | 0 | } |
2956 | | |
2957 | | static void |
2958 | | fu_context_dispose(GObject *object) |
2959 | 0 | { |
2960 | 0 | FuContext *self = FU_CONTEXT(object); |
2961 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2962 | 0 | g_ptr_array_set_size(priv->backends, 0); |
2963 | 0 | G_OBJECT_CLASS(fu_context_parent_class)->dispose(object); |
2964 | 0 | } |
2965 | | |
2966 | | static void |
2967 | | fu_context_finalize(GObject *object) |
2968 | 0 | { |
2969 | 0 | FuContext *self = FU_CONTEXT(object); |
2970 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
2971 | |
|
2972 | 0 | g_clear_pointer(&priv->main_ctx, g_main_context_unref); |
2973 | 0 | if (priv->fdt != NULL) |
2974 | 0 | g_object_unref(priv->fdt); |
2975 | 0 | if (priv->efivars != NULL) |
2976 | 0 | g_object_unref(priv->efivars); |
2977 | 0 | g_free(priv->esp_location); |
2978 | 0 | g_hash_table_unref(priv->runtime_versions); |
2979 | 0 | g_hash_table_unref(priv->compile_versions); |
2980 | 0 | g_object_unref(priv->pstore); |
2981 | 0 | g_object_unref(priv->hwids); |
2982 | 0 | g_object_unref(priv->config); |
2983 | 0 | g_hash_table_unref(priv->hwid_flags); |
2984 | 0 | g_object_unref(priv->quirks); |
2985 | 0 | g_object_unref(priv->smbios); |
2986 | 0 | g_ptr_array_unref(priv->bios_settings); |
2987 | 0 | g_hash_table_unref(priv->firmware_gtypes); |
2988 | 0 | g_hash_table_unref(priv->udev_subsystems); |
2989 | 0 | g_ptr_array_unref(priv->esp_volumes); |
2990 | 0 | g_ptr_array_unref(priv->backends); |
2991 | 0 | g_ptr_array_unref(priv->host_bkcs); |
2992 | |
|
2993 | 0 | G_OBJECT_CLASS(fu_context_parent_class)->finalize(object); |
2994 | 0 | } |
2995 | | |
2996 | | static void |
2997 | | fu_context_constructed(GObject *obj) |
2998 | 0 | { |
2999 | 0 | FuContext *self = FU_CONTEXT(obj); |
3000 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
3001 | 0 | priv->efivars = (priv->flags & FU_CONTEXT_FLAG_DUMMY_EFIVARS) > 0 |
3002 | 0 | ? fu_dummy_efivars_new() |
3003 | 0 | : fu_efivars_new(priv->pstore); |
3004 | | |
3005 | | /* chain up to parent */ |
3006 | 0 | G_OBJECT_CLASS(fu_context_parent_class)->constructed(obj); |
3007 | 0 | } |
3008 | | |
3009 | | static void |
3010 | | fu_context_class_init(FuContextClass *klass) |
3011 | 0 | { |
3012 | 0 | GObjectClass *object_class = G_OBJECT_CLASS(klass); |
3013 | 0 | GParamSpec *pspec; |
3014 | |
|
3015 | 0 | object_class->dispose = fu_context_dispose; |
3016 | 0 | object_class->get_property = fu_context_get_property; |
3017 | 0 | object_class->set_property = fu_context_set_property; |
3018 | 0 | object_class->constructed = fu_context_constructed; |
3019 | | |
3020 | | /** |
3021 | | * FuContext:power-state: |
3022 | | * |
3023 | | * The system power state. |
3024 | | */ |
3025 | 0 | pspec = g_param_spec_uint("power-state", |
3026 | 0 | NULL, |
3027 | 0 | NULL, |
3028 | 0 | FU_POWER_STATE_UNKNOWN, |
3029 | 0 | FU_POWER_STATE_LAST, |
3030 | 0 | FU_POWER_STATE_UNKNOWN, |
3031 | 0 | G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
3032 | 0 | g_object_class_install_property(object_class, PROP_POWER_STATE, pspec); |
3033 | | |
3034 | | /** |
3035 | | * FuContext:lid-state: |
3036 | | * |
3037 | | * The system lid state. |
3038 | | */ |
3039 | 0 | pspec = g_param_spec_uint("lid-state", |
3040 | 0 | NULL, |
3041 | 0 | NULL, |
3042 | 0 | FU_LID_STATE_UNKNOWN, |
3043 | 0 | FU_LID_STATE_LAST, |
3044 | 0 | FU_LID_STATE_UNKNOWN, |
3045 | 0 | G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
3046 | 0 | g_object_class_install_property(object_class, PROP_LID_STATE, pspec); |
3047 | | |
3048 | | /** |
3049 | | * FuContext:display-state: |
3050 | | * |
3051 | | * The display state. |
3052 | | */ |
3053 | 0 | pspec = g_param_spec_uint("display-state", |
3054 | 0 | NULL, |
3055 | 0 | NULL, |
3056 | 0 | FU_DISPLAY_STATE_UNKNOWN, |
3057 | 0 | FU_DISPLAY_STATE_LAST, |
3058 | 0 | FU_DISPLAY_STATE_UNKNOWN, |
3059 | 0 | G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
3060 | 0 | g_object_class_install_property(object_class, PROP_DISPLAY_STATE, pspec); |
3061 | | |
3062 | | /** |
3063 | | * FuContext:battery-level: |
3064 | | * |
3065 | | * The system battery level in percent. |
3066 | | */ |
3067 | 0 | pspec = g_param_spec_uint("battery-level", |
3068 | 0 | NULL, |
3069 | 0 | NULL, |
3070 | 0 | 0, |
3071 | 0 | FWUPD_BATTERY_LEVEL_INVALID, |
3072 | 0 | FWUPD_BATTERY_LEVEL_INVALID, |
3073 | 0 | G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
3074 | 0 | g_object_class_install_property(object_class, PROP_BATTERY_LEVEL, pspec); |
3075 | | |
3076 | | /** |
3077 | | * FuContext:battery-threshold: |
3078 | | * |
3079 | | * The system battery threshold in percent. |
3080 | | */ |
3081 | 0 | pspec = g_param_spec_uint("battery-threshold", |
3082 | 0 | NULL, |
3083 | 0 | NULL, |
3084 | 0 | 0, |
3085 | 0 | FWUPD_BATTERY_LEVEL_INVALID, |
3086 | 0 | FWUPD_BATTERY_LEVEL_INVALID, |
3087 | 0 | G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
3088 | 0 | g_object_class_install_property(object_class, PROP_BATTERY_THRESHOLD, pspec); |
3089 | | |
3090 | | /** |
3091 | | * FuContext:flags: |
3092 | | * |
3093 | | * The context flags. |
3094 | | */ |
3095 | 0 | pspec = g_param_spec_uint64("flags", |
3096 | 0 | NULL, |
3097 | 0 | NULL, |
3098 | 0 | FU_CONTEXT_FLAG_NONE, |
3099 | 0 | G_MAXUINT64, |
3100 | 0 | FU_CONTEXT_FLAG_NONE, |
3101 | 0 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_NAME); |
3102 | 0 | g_object_class_install_property(object_class, PROP_FLAGS, pspec); |
3103 | | |
3104 | | /** |
3105 | | * FuContext::security-changed: |
3106 | | * @self: the #FuContext instance that emitted the signal |
3107 | | * |
3108 | | * The ::security-changed signal is emitted when some system state has changed that could |
3109 | | * have affected the security level. |
3110 | | **/ |
3111 | 0 | signals[SIGNAL_SECURITY_CHANGED] = |
3112 | 0 | g_signal_new("security-changed", |
3113 | 0 | G_TYPE_FROM_CLASS(object_class), |
3114 | 0 | G_SIGNAL_RUN_LAST, |
3115 | 0 | G_STRUCT_OFFSET(FuContextClass, security_changed), |
3116 | 0 | NULL, |
3117 | 0 | NULL, |
3118 | 0 | g_cclosure_marshal_VOID__VOID, |
3119 | 0 | G_TYPE_NONE, |
3120 | 0 | 0); |
3121 | | /** |
3122 | | * FuContext::housekeeping: |
3123 | | * @self: the #FuContext instance that emitted the signal |
3124 | | * |
3125 | | * The ::housekeeping signal is emitted when helper objects should do house-keeping actions |
3126 | | * when the daemon is idle. |
3127 | | **/ |
3128 | 0 | signals[SIGNAL_HOUSEKEEPING] = g_signal_new("housekeeping", |
3129 | 0 | G_TYPE_FROM_CLASS(object_class), |
3130 | 0 | G_SIGNAL_RUN_LAST, |
3131 | 0 | G_STRUCT_OFFSET(FuContextClass, housekeeping), |
3132 | 0 | NULL, |
3133 | 0 | NULL, |
3134 | 0 | g_cclosure_marshal_VOID__VOID, |
3135 | 0 | G_TYPE_NONE, |
3136 | 0 | 0); |
3137 | | /** |
3138 | | * FuContext::esp-write: |
3139 | | * @self: the #FuContext instance that emitted the signal |
3140 | | * @volume: the #FuVolume being written to |
3141 | | * @filename: the filename being written |
3142 | | * |
3143 | | * The ::esp-write signal is emitted when plugins should be notified of a write to a |
3144 | | * specific ESP volume. |
3145 | | **/ |
3146 | 0 | signals[SIGNAL_ESP_WRITE] = g_signal_new("esp-write", |
3147 | 0 | G_TYPE_FROM_CLASS(object_class), |
3148 | 0 | G_SIGNAL_RUN_LAST, |
3149 | 0 | G_STRUCT_OFFSET(FuContextClass, volume_write), |
3150 | 0 | NULL, |
3151 | 0 | NULL, |
3152 | 0 | g_cclosure_marshal_generic, |
3153 | 0 | G_TYPE_NONE, |
3154 | 0 | 2, |
3155 | 0 | FU_TYPE_VOLUME, |
3156 | 0 | G_TYPE_STRING); |
3157 | |
|
3158 | 0 | object_class->finalize = fu_context_finalize; |
3159 | 0 | } |
3160 | | |
3161 | | static void |
3162 | | fu_context_init(FuContext *self) |
3163 | 0 | { |
3164 | 0 | FuContextPrivate *priv = GET_PRIVATE(self); |
3165 | 0 | priv->chassis_kind = FU_SMBIOS_CHASSIS_KIND_UNKNOWN; |
3166 | 0 | priv->battery_level = FWUPD_BATTERY_LEVEL_INVALID; |
3167 | 0 | priv->battery_threshold = FWUPD_BATTERY_LEVEL_INVALID; |
3168 | 0 | priv->pstore = fu_path_store_new(); |
3169 | 0 | priv->smbios = fu_smbios_new(priv->pstore); |
3170 | 0 | priv->hwids = fu_hwids_new(); |
3171 | 0 | priv->config = fu_config_new(priv->pstore); |
3172 | 0 | priv->hwid_flags = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); |
3173 | 0 | priv->udev_subsystems = g_hash_table_new_full(g_str_hash, |
3174 | 0 | g_str_equal, |
3175 | 0 | g_free, |
3176 | 0 | (GDestroyNotify)g_ptr_array_unref); |
3177 | 0 | priv->firmware_gtypes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); |
3178 | 0 | priv->quirks = fu_quirks_new(self); |
3179 | 0 | priv->bios_settings = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref); |
3180 | 0 | priv->esp_volumes = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref); |
3181 | 0 | priv->runtime_versions = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); |
3182 | 0 | priv->compile_versions = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); |
3183 | 0 | priv->backends = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref); |
3184 | 0 | priv->host_bkcs = g_ptr_array_new_with_free_func(g_free); |
3185 | 0 | } |
3186 | | |
3187 | | /* private */ |
3188 | | FuContext * |
3189 | | fu_context_new_full(FuContextFlags flags) |
3190 | 0 | { |
3191 | 0 | return FU_CONTEXT(g_object_new(FU_TYPE_CONTEXT, "flags", (guint64)flags, NULL)); |
3192 | 0 | } |
3193 | | |
3194 | | /** |
3195 | | * fu_context_new: |
3196 | | * |
3197 | | * Creates a new #FuContext |
3198 | | * |
3199 | | * Returns: (transfer full): the object |
3200 | | **/ |
3201 | | FuContext * |
3202 | | fu_context_new(void) |
3203 | 0 | { |
3204 | 0 | return fu_context_new_full(FU_CONTEXT_FLAG_NONE); |
3205 | 0 | } |