Richard Hughes | 02c90d8 | 2018-08-09 12:13:03 +0100 | [diff] [blame] | 1 | /* |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 2 | * Copyright (C) 2017 Richard Hughes <richard@hughsie.com> |
| 3 | * |
Mario Limonciello | 51308e6 | 2018-05-28 20:05:46 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: LGPL-2.1+ |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Richard Hughes | b08e7bc | 2018-09-11 10:51:13 +0100 | [diff] [blame] | 7 | #define G_LOG_DOMAIN "FuCommon" |
| 8 | |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 9 | #include <config.h> |
| 10 | |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 11 | #ifdef HAVE_GIO_UNIX |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 12 | #include <gio/gunixinputstream.h> |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 13 | #endif |
Richard Hughes | 954dd9f | 2017-08-08 13:36:25 +0100 | [diff] [blame] | 14 | #include <glib/gstdio.h> |
| 15 | |
Richard Hughes | 5c508de | 2019-11-22 09:57:34 +0000 | [diff] [blame] | 16 | #ifdef HAVE_FNMATCH_H |
| 17 | #include <fnmatch.h> |
Richard Hughes | 45a0073 | 2019-11-22 16:57:14 +0000 | [diff] [blame] | 18 | #elif _WIN32 |
| 19 | #include <shlwapi.h> |
Richard Hughes | 5c508de | 2019-11-22 09:57:34 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
Richard Hughes | 68175e9 | 2021-01-14 09:43:33 +0000 | [diff] [blame] | 22 | #ifdef _WIN32 |
| 23 | #include <sysinfoapi.h> |
| 24 | #endif |
| 25 | |
Richard Hughes | bd44432 | 2020-05-21 12:05:03 +0100 | [diff] [blame] | 26 | #ifdef HAVE_CPUID_H |
Richard Hughes | 9223c89 | 2020-05-09 20:32:08 +0100 | [diff] [blame] | 27 | #include <cpuid.h> |
Richard Hughes | bd44432 | 2020-05-21 12:05:03 +0100 | [diff] [blame] | 28 | #endif |
Richard Hughes | 9223c89 | 2020-05-09 20:32:08 +0100 | [diff] [blame] | 29 | |
Richard Hughes | 5add3a7 | 2021-01-13 19:25:10 +0000 | [diff] [blame] | 30 | #ifdef HAVE_LIBARCHIVE |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 31 | #include <archive_entry.h> |
| 32 | #include <archive.h> |
Richard Hughes | 5add3a7 | 2021-01-13 19:25:10 +0000 | [diff] [blame] | 33 | #endif |
Richard Hughes | 7ee42fe | 2017-08-15 14:06:21 +0100 | [diff] [blame] | 34 | #include <errno.h> |
Richard Hughes | 484ee29 | 2019-03-22 16:10:50 +0000 | [diff] [blame] | 35 | #include <limits.h> |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 36 | #include <string.h> |
Richard Hughes | 484ee29 | 2019-03-22 16:10:50 +0000 | [diff] [blame] | 37 | #include <stdlib.h> |
Richard Hughes | 68175e9 | 2021-01-14 09:43:33 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 39 | |
| 40 | #include "fwupd-error.h" |
| 41 | |
| 42 | #include "fu-common.h" |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 43 | #include "fu-volume-private.h" |
| 44 | |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 45 | #define UDISKS_DBUS_SERVICE "org.freedesktop.UDisks2" |
| 46 | #define UDISKS_DBUS_PATH "/org/freedesktop/UDisks2/Manager" |
| 47 | #define UDISKS_DBUS_MANAGER_INTERFACE "org.freedesktop.UDisks2.Manager" |
| 48 | #define UDISKS_DBUS_INTERFACE_PARTITION "org.freedesktop.UDisks2.Partition" |
| 49 | #define UDISKS_DBUS_INTERFACE_FILESYSTEM "org.freedesktop.UDisks2.Filesystem" |
| 50 | #define UDISKS_DBUS_INTERFACE_BLOCK "org.freedesktop.UDisks2.Block" |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 51 | |
| 52 | /** |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 53 | * SECTION:fu-common |
| 54 | * @short_description: common functionality for plugins to use |
| 55 | * |
| 56 | * Helper functions that can be used by the daemon and plugins. |
| 57 | * |
| 58 | * See also: #FuPlugin |
| 59 | */ |
| 60 | |
| 61 | /** |
Richard Hughes | 954dd9f | 2017-08-08 13:36:25 +0100 | [diff] [blame] | 62 | * fu_common_rmtree: |
| 63 | * @directory: a directory name |
| 64 | * @error: A #GError or %NULL |
| 65 | * |
| 66 | * Recursively removes a directory. |
| 67 | * |
| 68 | * Returns: %TRUE for success, %FALSE otherwise |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 69 | * |
| 70 | * Since: 0.9.7 |
Richard Hughes | 954dd9f | 2017-08-08 13:36:25 +0100 | [diff] [blame] | 71 | **/ |
| 72 | gboolean |
| 73 | fu_common_rmtree (const gchar *directory, GError **error) |
| 74 | { |
| 75 | const gchar *filename; |
| 76 | g_autoptr(GDir) dir = NULL; |
| 77 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 78 | g_return_val_if_fail (directory != NULL, FALSE); |
| 79 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 80 | |
Richard Hughes | 954dd9f | 2017-08-08 13:36:25 +0100 | [diff] [blame] | 81 | /* try to open */ |
Richard Hughes | 455fdd3 | 2017-08-16 12:26:44 +0100 | [diff] [blame] | 82 | g_debug ("removing %s", directory); |
Richard Hughes | 954dd9f | 2017-08-08 13:36:25 +0100 | [diff] [blame] | 83 | dir = g_dir_open (directory, 0, error); |
| 84 | if (dir == NULL) |
| 85 | return FALSE; |
| 86 | |
| 87 | /* find each */ |
| 88 | while ((filename = g_dir_read_name (dir))) { |
| 89 | g_autofree gchar *src = NULL; |
| 90 | src = g_build_filename (directory, filename, NULL); |
| 91 | if (g_file_test (src, G_FILE_TEST_IS_DIR)) { |
| 92 | if (!fu_common_rmtree (src, error)) |
| 93 | return FALSE; |
| 94 | } else { |
| 95 | if (g_unlink (src) != 0) { |
| 96 | g_set_error (error, |
| 97 | FWUPD_ERROR, |
| 98 | FWUPD_ERROR_INTERNAL, |
| 99 | "Failed to delete: %s", src); |
| 100 | return FALSE; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | if (g_remove (directory) != 0) { |
| 105 | g_set_error (error, |
| 106 | FWUPD_ERROR, |
| 107 | FWUPD_ERROR_INTERNAL, |
| 108 | "Failed to delete: %s", directory); |
| 109 | return FALSE; |
| 110 | } |
| 111 | return TRUE; |
| 112 | } |
| 113 | |
Richard Hughes | 89e968b | 2018-03-07 10:01:08 +0000 | [diff] [blame] | 114 | static gboolean |
| 115 | fu_common_get_file_list_internal (GPtrArray *files, const gchar *directory, GError **error) |
| 116 | { |
| 117 | const gchar *filename; |
| 118 | g_autoptr(GDir) dir = NULL; |
| 119 | |
| 120 | /* try to open */ |
| 121 | dir = g_dir_open (directory, 0, error); |
| 122 | if (dir == NULL) |
| 123 | return FALSE; |
| 124 | |
| 125 | /* find each */ |
| 126 | while ((filename = g_dir_read_name (dir))) { |
| 127 | g_autofree gchar *src = g_build_filename (directory, filename, NULL); |
| 128 | if (g_file_test (src, G_FILE_TEST_IS_DIR)) { |
| 129 | if (!fu_common_get_file_list_internal (files, src, error)) |
| 130 | return FALSE; |
| 131 | } else { |
| 132 | g_ptr_array_add (files, g_steal_pointer (&src)); |
| 133 | } |
| 134 | } |
| 135 | return TRUE; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * fu_common_get_files_recursive: |
Richard Hughes | 8aa7239 | 2018-05-02 08:38:43 +0100 | [diff] [blame] | 141 | * @path: a directory name |
Richard Hughes | 89e968b | 2018-03-07 10:01:08 +0000 | [diff] [blame] | 142 | * @error: A #GError or %NULL |
| 143 | * |
| 144 | * Returns every file found under @directory, and any subdirectory. |
| 145 | * If any path under @directory cannot be accessed due to permissions an error |
| 146 | * will be returned. |
| 147 | * |
Richard Hughes | a0d81c7 | 2019-11-27 11:41:54 +0000 | [diff] [blame] | 148 | * Returns: (transfer container) (element-type utf8): array of files, or %NULL for error |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 149 | * |
| 150 | * Since: 1.0.6 |
Richard Hughes | 89e968b | 2018-03-07 10:01:08 +0000 | [diff] [blame] | 151 | **/ |
| 152 | GPtrArray * |
| 153 | fu_common_get_files_recursive (const gchar *path, GError **error) |
| 154 | { |
| 155 | g_autoptr(GPtrArray) files = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 156 | |
| 157 | g_return_val_if_fail (path != NULL, NULL); |
| 158 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 159 | |
Richard Hughes | 89e968b | 2018-03-07 10:01:08 +0000 | [diff] [blame] | 160 | if (!fu_common_get_file_list_internal (files, path, error)) |
| 161 | return NULL; |
| 162 | return g_steal_pointer (&files); |
| 163 | } |
Richard Hughes | 954dd9f | 2017-08-08 13:36:25 +0100 | [diff] [blame] | 164 | /** |
Richard Hughes | 7ee42fe | 2017-08-15 14:06:21 +0100 | [diff] [blame] | 165 | * fu_common_mkdir_parent: |
| 166 | * @filename: A full pathname |
| 167 | * @error: A #GError, or %NULL |
| 168 | * |
| 169 | * Creates any required directories, including any parent directories. |
| 170 | * |
| 171 | * Returns: %TRUE for success |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 172 | * |
| 173 | * Since: 0.9.7 |
Richard Hughes | 7ee42fe | 2017-08-15 14:06:21 +0100 | [diff] [blame] | 174 | **/ |
| 175 | gboolean |
| 176 | fu_common_mkdir_parent (const gchar *filename, GError **error) |
| 177 | { |
| 178 | g_autofree gchar *parent = NULL; |
Richard Hughes | 455fdd3 | 2017-08-16 12:26:44 +0100 | [diff] [blame] | 179 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 180 | g_return_val_if_fail (filename != NULL, FALSE); |
| 181 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 182 | |
Richard Hughes | 7ee42fe | 2017-08-15 14:06:21 +0100 | [diff] [blame] | 183 | parent = g_path_get_dirname (filename); |
Mario Limonciello | d4155ff | 2020-09-28 15:20:07 -0500 | [diff] [blame] | 184 | if (!g_file_test (parent, G_FILE_TEST_IS_DIR)) |
| 185 | g_debug ("creating path %s", parent); |
Richard Hughes | 7ee42fe | 2017-08-15 14:06:21 +0100 | [diff] [blame] | 186 | if (g_mkdir_with_parents (parent, 0755) == -1) { |
| 187 | g_set_error (error, |
| 188 | FWUPD_ERROR, |
| 189 | FWUPD_ERROR_INTERNAL, |
| 190 | "Failed to create '%s': %s", |
| 191 | parent, g_strerror (errno)); |
| 192 | return FALSE; |
| 193 | } |
| 194 | return TRUE; |
| 195 | } |
| 196 | |
| 197 | /** |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 198 | * fu_common_set_contents_bytes: |
| 199 | * @filename: A filename |
| 200 | * @bytes: The data to write |
| 201 | * @error: A #GError, or %NULL |
| 202 | * |
| 203 | * Writes a blob of data to a filename, creating the parent directories as |
| 204 | * required. |
| 205 | * |
| 206 | * Returns: %TRUE for success |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 207 | * |
| 208 | * Since: 0.9.5 |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 209 | **/ |
| 210 | gboolean |
| 211 | fu_common_set_contents_bytes (const gchar *filename, GBytes *bytes, GError **error) |
| 212 | { |
| 213 | const gchar *data; |
| 214 | gsize size; |
| 215 | g_autoptr(GFile) file = NULL; |
| 216 | g_autoptr(GFile) file_parent = NULL; |
| 217 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 218 | g_return_val_if_fail (filename != NULL, FALSE); |
| 219 | g_return_val_if_fail (bytes != NULL, FALSE); |
| 220 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 221 | |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 222 | file = g_file_new_for_path (filename); |
| 223 | file_parent = g_file_get_parent (file); |
| 224 | if (!g_file_query_exists (file_parent, NULL)) { |
| 225 | if (!g_file_make_directory_with_parents (file_parent, NULL, error)) |
| 226 | return FALSE; |
| 227 | } |
| 228 | data = g_bytes_get_data (bytes, &size); |
Richard Hughes | 455fdd3 | 2017-08-16 12:26:44 +0100 | [diff] [blame] | 229 | g_debug ("writing %s with %" G_GSIZE_FORMAT " bytes", filename, size); |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 230 | return g_file_set_contents (filename, data, size, error); |
| 231 | } |
| 232 | |
Richard Hughes | d0d2ae6 | 2017-08-08 12:22:30 +0100 | [diff] [blame] | 233 | /** |
| 234 | * fu_common_get_contents_bytes: |
| 235 | * @filename: A filename |
| 236 | * @error: A #GError, or %NULL |
| 237 | * |
| 238 | * Reads a blob of data from a file. |
| 239 | * |
| 240 | * Returns: a #GBytes, or %NULL for failure |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 241 | * |
| 242 | * Since: 0.9.7 |
Richard Hughes | d0d2ae6 | 2017-08-08 12:22:30 +0100 | [diff] [blame] | 243 | **/ |
| 244 | GBytes * |
| 245 | fu_common_get_contents_bytes (const gchar *filename, GError **error) |
| 246 | { |
| 247 | gchar *data = NULL; |
| 248 | gsize len = 0; |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 249 | |
| 250 | g_return_val_if_fail (filename != NULL, NULL); |
| 251 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 252 | |
Richard Hughes | d0d2ae6 | 2017-08-08 12:22:30 +0100 | [diff] [blame] | 253 | if (!g_file_get_contents (filename, &data, &len, error)) |
| 254 | return NULL; |
Richard Hughes | 455fdd3 | 2017-08-16 12:26:44 +0100 | [diff] [blame] | 255 | g_debug ("reading %s with %" G_GSIZE_FORMAT " bytes", filename, len); |
Richard Hughes | d0d2ae6 | 2017-08-08 12:22:30 +0100 | [diff] [blame] | 256 | return g_bytes_new_take (data, len); |
| 257 | } |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * fu_common_get_contents_fd: |
| 261 | * @fd: A file descriptor |
| 262 | * @count: The maximum number of bytes to read |
| 263 | * @error: A #GError, or %NULL |
| 264 | * |
| 265 | * Reads a blob from a specific file descriptor. |
| 266 | * |
| 267 | * Note: this will close the fd when done |
| 268 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 269 | * Returns: (transfer full): a #GBytes, or %NULL |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 270 | * |
| 271 | * Since: 0.9.5 |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 272 | **/ |
| 273 | GBytes * |
| 274 | fu_common_get_contents_fd (gint fd, gsize count, GError **error) |
| 275 | { |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 276 | #ifdef HAVE_GIO_UNIX |
Richard Hughes | 97ad335 | 2021-01-14 20:07:47 +0000 | [diff] [blame] | 277 | guint8 tmp[0x8000] = { 0x0 }; |
| 278 | g_autoptr(GByteArray) buf = g_byte_array_new (); |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 279 | g_autoptr(GError) error_local = NULL; |
| 280 | g_autoptr(GInputStream) stream = NULL; |
| 281 | |
| 282 | g_return_val_if_fail (fd > 0, NULL); |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 283 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 284 | |
Richard Hughes | 919f8ab | 2018-02-14 10:24:56 +0000 | [diff] [blame] | 285 | /* this is invalid */ |
| 286 | if (count == 0) { |
| 287 | g_set_error_literal (error, |
| 288 | FWUPD_ERROR, |
| 289 | FWUPD_ERROR_NOT_SUPPORTED, |
| 290 | "A maximum read size must be specified"); |
| 291 | return NULL; |
| 292 | } |
| 293 | |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 294 | /* read the entire fd to a data blob */ |
| 295 | stream = g_unix_input_stream_new (fd, TRUE); |
Richard Hughes | 97ad335 | 2021-01-14 20:07:47 +0000 | [diff] [blame] | 296 | |
| 297 | /* read from stream in 32kB chunks */ |
| 298 | while (TRUE) { |
| 299 | gssize sz; |
| 300 | sz = g_input_stream_read (stream, tmp, sizeof(tmp), NULL, &error_local); |
| 301 | if (sz == 0) |
| 302 | break; |
| 303 | if (sz < 0) { |
| 304 | g_set_error_literal (error, |
| 305 | FWUPD_ERROR, |
| 306 | FWUPD_ERROR_INVALID_FILE, |
| 307 | error_local->message); |
| 308 | return NULL; |
| 309 | } |
| 310 | g_byte_array_append (buf, tmp, sz); |
| 311 | if (buf->len > count) { |
| 312 | g_set_error (error, |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 313 | FWUPD_ERROR, |
| 314 | FWUPD_ERROR_INVALID_FILE, |
Richard Hughes | 97ad335 | 2021-01-14 20:07:47 +0000 | [diff] [blame] | 315 | "cannot read from fd: 0x%x > 0x%x", |
| 316 | buf->len, (guint) count); |
| 317 | return NULL; |
| 318 | } |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 319 | } |
Richard Hughes | 97ad335 | 2021-01-14 20:07:47 +0000 | [diff] [blame] | 320 | return g_byte_array_free_to_bytes (g_steal_pointer (&buf)); |
Richard Hughes | 9e5675e | 2019-11-22 09:35:03 +0000 | [diff] [blame] | 321 | #else |
| 322 | g_set_error_literal (error, |
| 323 | FWUPD_ERROR, |
| 324 | FWUPD_ERROR_NOT_SUPPORTED, |
| 325 | "Not supported as <glib-unix.h> is unavailable"); |
| 326 | return NULL; |
| 327 | #endif |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 328 | } |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 329 | |
Richard Hughes | 5add3a7 | 2021-01-13 19:25:10 +0000 | [diff] [blame] | 330 | #ifdef HAVE_LIBARCHIVE |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 331 | static gboolean |
| 332 | fu_common_extract_archive_entry (struct archive_entry *entry, const gchar *dir) |
| 333 | { |
| 334 | const gchar *tmp; |
| 335 | g_autofree gchar *buf = NULL; |
| 336 | |
| 337 | /* no output file */ |
| 338 | if (archive_entry_pathname (entry) == NULL) |
| 339 | return FALSE; |
| 340 | |
| 341 | /* update output path */ |
| 342 | tmp = archive_entry_pathname (entry); |
| 343 | buf = g_build_filename (dir, tmp, NULL); |
| 344 | archive_entry_update_pathname_utf8 (entry, buf); |
| 345 | return TRUE; |
| 346 | } |
Richard Hughes | 5add3a7 | 2021-01-13 19:25:10 +0000 | [diff] [blame] | 347 | #endif |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 348 | |
| 349 | /** |
| 350 | * fu_common_extract_archive: |
| 351 | * @blob: a #GBytes archive as a blob |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 352 | * @dir: a directory name to extract to |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 353 | * @error: A #GError, or %NULL |
| 354 | * |
Richard Hughes | 21eaeef | 2020-01-14 12:10:01 +0000 | [diff] [blame] | 355 | * Extracts an archive to a directory. |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 356 | * |
| 357 | * Returns: %TRUE for success |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 358 | * |
| 359 | * Since: 0.9.7 |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 360 | **/ |
| 361 | gboolean |
| 362 | fu_common_extract_archive (GBytes *blob, const gchar *dir, GError **error) |
| 363 | { |
Richard Hughes | 5add3a7 | 2021-01-13 19:25:10 +0000 | [diff] [blame] | 364 | #ifdef HAVE_LIBARCHIVE |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 365 | gboolean ret = TRUE; |
| 366 | int r; |
| 367 | struct archive *arch = NULL; |
| 368 | struct archive_entry *entry; |
| 369 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 370 | g_return_val_if_fail (blob != NULL, FALSE); |
| 371 | g_return_val_if_fail (dir != NULL, FALSE); |
| 372 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 373 | |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 374 | /* decompress anything matching either glob */ |
Richard Hughes | 455fdd3 | 2017-08-16 12:26:44 +0100 | [diff] [blame] | 375 | g_debug ("decompressing into %s", dir); |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 376 | arch = archive_read_new (); |
| 377 | archive_read_support_format_all (arch); |
| 378 | archive_read_support_filter_all (arch); |
| 379 | r = archive_read_open_memory (arch, |
| 380 | (void *) g_bytes_get_data (blob, NULL), |
| 381 | (size_t) g_bytes_get_size (blob)); |
| 382 | if (r != 0) { |
| 383 | ret = FALSE; |
| 384 | g_set_error (error, |
| 385 | FWUPD_ERROR, |
| 386 | FWUPD_ERROR_INTERNAL, |
| 387 | "Cannot open: %s", |
| 388 | archive_error_string (arch)); |
| 389 | goto out; |
| 390 | } |
| 391 | for (;;) { |
| 392 | gboolean valid; |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 393 | r = archive_read_next_header (arch, &entry); |
| 394 | if (r == ARCHIVE_EOF) |
| 395 | break; |
| 396 | if (r != ARCHIVE_OK) { |
| 397 | ret = FALSE; |
| 398 | g_set_error (error, |
| 399 | FWUPD_ERROR, |
| 400 | FWUPD_ERROR_INTERNAL, |
| 401 | "Cannot read header: %s", |
| 402 | archive_error_string (arch)); |
| 403 | goto out; |
| 404 | } |
| 405 | |
| 406 | /* only extract if valid */ |
| 407 | valid = fu_common_extract_archive_entry (entry, dir); |
| 408 | if (!valid) |
| 409 | continue; |
| 410 | r = archive_read_extract (arch, entry, 0); |
| 411 | if (r != ARCHIVE_OK) { |
| 412 | ret = FALSE; |
| 413 | g_set_error (error, |
| 414 | FWUPD_ERROR, |
| 415 | FWUPD_ERROR_INTERNAL, |
| 416 | "Cannot extract: %s", |
| 417 | archive_error_string (arch)); |
| 418 | goto out; |
| 419 | } |
| 420 | } |
| 421 | out: |
| 422 | if (arch != NULL) { |
| 423 | archive_read_close (arch); |
| 424 | archive_read_free (arch); |
| 425 | } |
| 426 | return ret; |
Richard Hughes | 5add3a7 | 2021-01-13 19:25:10 +0000 | [diff] [blame] | 427 | #else |
| 428 | g_set_error_literal (error, |
| 429 | FWUPD_ERROR, |
| 430 | FWUPD_ERROR_NOT_SUPPORTED, |
| 431 | "missing libarchive support"); |
| 432 | return FALSE; |
| 433 | #endif |
Richard Hughes | 94f939a | 2017-08-08 12:21:39 +0100 | [diff] [blame] | 434 | } |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 435 | |
| 436 | static void |
Yehezkel Bernat | e43f7fb | 2017-08-30 12:09:34 +0300 | [diff] [blame] | 437 | fu_common_add_argv (GPtrArray *argv, const gchar *fmt, ...) G_GNUC_PRINTF (2, 3); |
| 438 | |
| 439 | static void |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 440 | fu_common_add_argv (GPtrArray *argv, const gchar *fmt, ...) |
| 441 | { |
| 442 | va_list args; |
| 443 | g_autofree gchar *tmp = NULL; |
| 444 | g_auto(GStrv) split = NULL; |
| 445 | |
| 446 | va_start (args, fmt); |
| 447 | tmp = g_strdup_vprintf (fmt, args); |
| 448 | va_end (args); |
| 449 | |
| 450 | split = g_strsplit (tmp, " ", -1); |
| 451 | for (guint i = 0; split[i] != NULL; i++) |
| 452 | g_ptr_array_add (argv, g_strdup (split[i])); |
| 453 | } |
| 454 | |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 455 | /** |
| 456 | * fu_common_find_program_in_path: |
| 457 | * @basename: The program to search |
| 458 | * @error: A #GError, or %NULL |
| 459 | * |
| 460 | * Looks for a program in the PATH variable |
| 461 | * |
| 462 | * Returns: a new #gchar, or %NULL for error |
| 463 | * |
| 464 | * Since: 1.1.2 |
| 465 | **/ |
Richard Hughes | 22367e7 | 2018-08-30 10:24:04 +0100 | [diff] [blame] | 466 | gchar * |
| 467 | fu_common_find_program_in_path (const gchar *basename, GError **error) |
| 468 | { |
| 469 | gchar *fn = g_find_program_in_path (basename); |
| 470 | if (fn == NULL) { |
| 471 | g_set_error (error, |
| 472 | FWUPD_ERROR, |
| 473 | FWUPD_ERROR_NOT_SUPPORTED, |
| 474 | "missing executable %s in PATH", |
| 475 | basename); |
| 476 | return NULL; |
| 477 | } |
| 478 | return fn; |
| 479 | } |
| 480 | |
| 481 | static gboolean |
| 482 | fu_common_test_namespace_support (GError **error) |
| 483 | { |
| 484 | /* test if CONFIG_USER_NS is valid */ |
| 485 | if (!g_file_test ("/proc/self/ns/user", G_FILE_TEST_IS_SYMLINK)) { |
| 486 | g_set_error (error, |
| 487 | FWUPD_ERROR, |
| 488 | FWUPD_ERROR_NOT_SUPPORTED, |
| 489 | "missing CONFIG_USER_NS in kernel"); |
| 490 | return FALSE; |
| 491 | } |
| 492 | if (g_file_test ("/proc/sys/kernel/unprivileged_userns_clone", G_FILE_TEST_EXISTS)) { |
| 493 | g_autofree gchar *clone = NULL; |
| 494 | if (!g_file_get_contents ("/proc/sys/kernel/unprivileged_userns_clone", &clone, NULL, error)) |
| 495 | return FALSE; |
| 496 | if (g_ascii_strtoll (clone, NULL, 10) == 0) { |
| 497 | g_set_error (error, |
| 498 | FWUPD_ERROR, |
| 499 | FWUPD_ERROR_NOT_SUPPORTED, |
| 500 | "unprivileged user namespace clones disabled by distro"); |
| 501 | return FALSE; |
| 502 | } |
| 503 | } |
| 504 | return TRUE; |
| 505 | } |
| 506 | |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 507 | /** |
| 508 | * fu_common_firmware_builder: |
| 509 | * @bytes: The data to use |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 510 | * @script_fn: Name of the script to run in the tarball, e.g. `startup.sh` |
| 511 | * @output_fn: Name of the generated firmware, e.g. `firmware.bin` |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 512 | * @error: A #GError, or %NULL |
| 513 | * |
| 514 | * Builds a firmware file using tools from the host session in a bubblewrap |
| 515 | * jail. Several things happen during build: |
| 516 | * |
| 517 | * 1. The @bytes data is untarred to a temporary location |
| 518 | * 2. A bubblewrap container is set up |
| 519 | * 3. The startup.sh script is run inside the container |
| 520 | * 4. The firmware.bin is extracted from the container |
| 521 | * 5. The temporary location is deleted |
| 522 | * |
| 523 | * Returns: a new #GBytes, or %NULL for error |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 524 | * |
| 525 | * Since: 0.9.7 |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 526 | **/ |
| 527 | GBytes * |
| 528 | fu_common_firmware_builder (GBytes *bytes, |
| 529 | const gchar *script_fn, |
| 530 | const gchar *output_fn, |
| 531 | GError **error) |
| 532 | { |
| 533 | gint rc = 0; |
| 534 | g_autofree gchar *argv_str = NULL; |
Mario Limonciello | 37b5958 | 2018-08-13 08:38:01 -0500 | [diff] [blame] | 535 | g_autofree gchar *bwrap_fn = NULL; |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 536 | g_autofree gchar *localstatebuilderdir = NULL; |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 537 | g_autofree gchar *localstatedir = NULL; |
| 538 | g_autofree gchar *output2_fn = NULL; |
| 539 | g_autofree gchar *standard_error = NULL; |
| 540 | g_autofree gchar *standard_output = NULL; |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 541 | g_autofree gchar *tmpdir = NULL; |
| 542 | g_autoptr(GBytes) firmware_blob = NULL; |
| 543 | g_autoptr(GPtrArray) argv = g_ptr_array_new_with_free_func (g_free); |
| 544 | |
| 545 | g_return_val_if_fail (bytes != NULL, NULL); |
| 546 | g_return_val_if_fail (script_fn != NULL, NULL); |
| 547 | g_return_val_if_fail (output_fn != NULL, NULL); |
| 548 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 549 | |
Mario Limonciello | 37b5958 | 2018-08-13 08:38:01 -0500 | [diff] [blame] | 550 | /* find bwrap in the path */ |
Richard Hughes | 22367e7 | 2018-08-30 10:24:04 +0100 | [diff] [blame] | 551 | bwrap_fn = fu_common_find_program_in_path ("bwrap", error); |
| 552 | if (bwrap_fn == NULL) |
Richard Hughes | ddb3e20 | 2018-08-23 11:29:57 +0100 | [diff] [blame] | 553 | return NULL; |
Mario Limonciello | 37b5958 | 2018-08-13 08:38:01 -0500 | [diff] [blame] | 554 | |
| 555 | /* test if CONFIG_USER_NS is valid */ |
Richard Hughes | 22367e7 | 2018-08-30 10:24:04 +0100 | [diff] [blame] | 556 | if (!fu_common_test_namespace_support (error)) |
Richard Hughes | ddb3e20 | 2018-08-23 11:29:57 +0100 | [diff] [blame] | 557 | return NULL; |
Mario Limonciello | 37b5958 | 2018-08-13 08:38:01 -0500 | [diff] [blame] | 558 | |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 559 | /* untar file to temp location */ |
| 560 | tmpdir = g_dir_make_tmp ("fwupd-gen-XXXXXX", error); |
| 561 | if (tmpdir == NULL) |
| 562 | return NULL; |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 563 | if (!fu_common_extract_archive (bytes, tmpdir, error)) |
| 564 | return NULL; |
| 565 | |
| 566 | /* this is shared with the plugins */ |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 567 | localstatedir = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
| 568 | localstatebuilderdir = g_build_filename (localstatedir, "builder", NULL); |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 569 | |
| 570 | /* launch bubblewrap and generate firmware */ |
Mario Limonciello | 37b5958 | 2018-08-13 08:38:01 -0500 | [diff] [blame] | 571 | g_ptr_array_add (argv, g_steal_pointer (&bwrap_fn)); |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 572 | fu_common_add_argv (argv, "--die-with-parent"); |
| 573 | fu_common_add_argv (argv, "--ro-bind /usr /usr"); |
Mario Limonciello | b821557 | 2018-07-13 09:49:55 -0500 | [diff] [blame] | 574 | fu_common_add_argv (argv, "--ro-bind /lib /lib"); |
Mario Limonciello | ed4e912 | 2020-12-15 20:26:50 -0600 | [diff] [blame] | 575 | fu_common_add_argv (argv, "--ro-bind-try /lib64 /lib64"); |
Mario Limonciello | b821557 | 2018-07-13 09:49:55 -0500 | [diff] [blame] | 576 | fu_common_add_argv (argv, "--ro-bind /bin /bin"); |
| 577 | fu_common_add_argv (argv, "--ro-bind /sbin /sbin"); |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 578 | fu_common_add_argv (argv, "--dir /tmp"); |
| 579 | fu_common_add_argv (argv, "--dir /var"); |
| 580 | fu_common_add_argv (argv, "--bind %s /tmp", tmpdir); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 581 | if (g_file_test (localstatebuilderdir, G_FILE_TEST_EXISTS)) |
| 582 | fu_common_add_argv (argv, "--ro-bind %s /boot", localstatebuilderdir); |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 583 | fu_common_add_argv (argv, "--dev /dev"); |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 584 | fu_common_add_argv (argv, "--chdir /tmp"); |
| 585 | fu_common_add_argv (argv, "--unshare-all"); |
Richard Hughes | 443e409 | 2017-08-09 16:07:31 +0100 | [diff] [blame] | 586 | fu_common_add_argv (argv, "/tmp/%s", script_fn); |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 587 | g_ptr_array_add (argv, NULL); |
| 588 | argv_str = g_strjoinv (" ", (gchar **) argv->pdata); |
| 589 | g_debug ("running '%s' in %s", argv_str, tmpdir); |
| 590 | if (!g_spawn_sync ("/tmp", |
| 591 | (gchar **) argv->pdata, |
| 592 | NULL, |
Richard Hughes | f6f72a4 | 2017-08-09 16:25:25 +0100 | [diff] [blame] | 593 | G_SPAWN_SEARCH_PATH, |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 594 | NULL, NULL, /* child_setup */ |
| 595 | &standard_output, |
| 596 | &standard_error, |
| 597 | &rc, |
| 598 | error)) { |
| 599 | g_prefix_error (error, "failed to run '%s': ", argv_str); |
| 600 | return NULL; |
| 601 | } |
| 602 | if (standard_output != NULL && standard_output[0] != '\0') |
| 603 | g_debug ("console output was: %s", standard_output); |
| 604 | if (rc != 0) { |
Mario Limonciello | 37b5958 | 2018-08-13 08:38:01 -0500 | [diff] [blame] | 605 | FwupdError code = FWUPD_ERROR_INTERNAL; |
| 606 | if (errno == ENOTTY) |
| 607 | code = FWUPD_ERROR_PERMISSION_DENIED; |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 608 | g_set_error (error, |
| 609 | FWUPD_ERROR, |
Mario Limonciello | 37b5958 | 2018-08-13 08:38:01 -0500 | [diff] [blame] | 610 | code, |
Richard Hughes | 41cbe2a | 2017-08-08 14:13:35 +0100 | [diff] [blame] | 611 | "failed to build firmware: %s", |
| 612 | standard_error); |
| 613 | return NULL; |
| 614 | } |
| 615 | |
| 616 | /* get generated file */ |
| 617 | output2_fn = g_build_filename (tmpdir, output_fn, NULL); |
| 618 | firmware_blob = fu_common_get_contents_bytes (output2_fn, error); |
| 619 | if (firmware_blob == NULL) |
| 620 | return NULL; |
| 621 | |
| 622 | /* cleanup temp directory */ |
| 623 | if (!fu_common_rmtree (tmpdir, error)) |
| 624 | return NULL; |
| 625 | |
| 626 | /* success */ |
| 627 | return g_steal_pointer (&firmware_blob); |
| 628 | } |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 629 | |
| 630 | typedef struct { |
| 631 | FuOutputHandler handler_cb; |
| 632 | gpointer handler_user_data; |
| 633 | GMainLoop *loop; |
| 634 | GSource *source; |
| 635 | GInputStream *stream; |
| 636 | GCancellable *cancellable; |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 637 | guint timeout_id; |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 638 | } FuCommonSpawnHelper; |
| 639 | |
| 640 | static void fu_common_spawn_create_pollable_source (FuCommonSpawnHelper *helper); |
| 641 | |
| 642 | static gboolean |
| 643 | fu_common_spawn_source_pollable_cb (GObject *stream, gpointer user_data) |
| 644 | { |
| 645 | FuCommonSpawnHelper *helper = (FuCommonSpawnHelper *) user_data; |
| 646 | gchar buffer[1024]; |
| 647 | gssize sz; |
| 648 | g_auto(GStrv) split = NULL; |
| 649 | g_autoptr(GError) error = NULL; |
| 650 | |
| 651 | /* read from stream */ |
| 652 | sz = g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (stream), |
| 653 | buffer, |
| 654 | sizeof(buffer) - 1, |
| 655 | NULL, |
| 656 | &error); |
| 657 | if (sz < 0) { |
Richard Hughes | 67cbe64 | 2017-08-16 12:26:14 +0100 | [diff] [blame] | 658 | if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) { |
| 659 | g_warning ("failed to get read from nonblocking fd: %s", |
| 660 | error->message); |
| 661 | } |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 662 | return G_SOURCE_REMOVE; |
| 663 | } |
| 664 | |
| 665 | /* no read possible */ |
| 666 | if (sz == 0) |
| 667 | g_main_loop_quit (helper->loop); |
| 668 | |
| 669 | /* emit lines */ |
| 670 | if (helper->handler_cb != NULL) { |
| 671 | buffer[sz] = '\0'; |
| 672 | split = g_strsplit (buffer, "\n", -1); |
| 673 | for (guint i = 0; split[i] != NULL; i++) { |
| 674 | if (split[i][0] == '\0') |
| 675 | continue; |
| 676 | helper->handler_cb (split[i], helper->handler_user_data); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /* set up the source for the next read */ |
| 681 | fu_common_spawn_create_pollable_source (helper); |
| 682 | return G_SOURCE_REMOVE; |
| 683 | } |
| 684 | |
| 685 | static void |
| 686 | fu_common_spawn_create_pollable_source (FuCommonSpawnHelper *helper) |
| 687 | { |
| 688 | if (helper->source != NULL) |
| 689 | g_source_destroy (helper->source); |
| 690 | helper->source = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (helper->stream), |
| 691 | helper->cancellable); |
| 692 | g_source_attach (helper->source, NULL); |
| 693 | g_source_set_callback (helper->source, (GSourceFunc) fu_common_spawn_source_pollable_cb, helper, NULL); |
| 694 | } |
| 695 | |
| 696 | static void |
| 697 | fu_common_spawn_helper_free (FuCommonSpawnHelper *helper) |
| 698 | { |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 699 | g_object_unref (helper->cancellable); |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 700 | if (helper->stream != NULL) |
| 701 | g_object_unref (helper->stream); |
| 702 | if (helper->source != NULL) |
| 703 | g_source_destroy (helper->source); |
| 704 | if (helper->loop != NULL) |
| 705 | g_main_loop_unref (helper->loop); |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 706 | if (helper->timeout_id != 0) |
| 707 | g_source_remove (helper->timeout_id); |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 708 | g_free (helper); |
| 709 | } |
| 710 | |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 711 | #pragma clang diagnostic push |
| 712 | #pragma clang diagnostic ignored "-Wunused-function" |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 713 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuCommonSpawnHelper, fu_common_spawn_helper_free) |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 714 | #pragma clang diagnostic pop |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 715 | |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 716 | static gboolean |
| 717 | fu_common_spawn_timeout_cb (gpointer user_data) |
| 718 | { |
| 719 | FuCommonSpawnHelper *helper = (FuCommonSpawnHelper *) user_data; |
| 720 | g_cancellable_cancel (helper->cancellable); |
| 721 | g_main_loop_quit (helper->loop); |
| 722 | helper->timeout_id = 0; |
| 723 | return G_SOURCE_REMOVE; |
| 724 | } |
| 725 | |
| 726 | static void |
| 727 | fu_common_spawn_cancelled_cb (GCancellable *cancellable, FuCommonSpawnHelper *helper) |
| 728 | { |
| 729 | /* just propagate */ |
| 730 | g_cancellable_cancel (helper->cancellable); |
| 731 | } |
| 732 | |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 733 | /** |
| 734 | * fu_common_spawn_sync: |
| 735 | * @argv: The argument list to run |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 736 | * @handler_cb: (scope call): A #FuOutputHandler or %NULL |
| 737 | * @handler_user_data: the user data to pass to @handler_cb |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 738 | * @timeout_ms: a timeout in ms, or 0 for no limit |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 739 | * @cancellable: a #GCancellable, or %NULL |
| 740 | * @error: A #GError or %NULL |
| 741 | * |
| 742 | * Runs a subprocess and waits for it to exit. Any output on standard out or |
| 743 | * standard error will be forwarded to @handler_cb as whole lines. |
| 744 | * |
| 745 | * Returns: %TRUE for success |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 746 | * |
| 747 | * Since: 0.9.7 |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 748 | **/ |
| 749 | gboolean |
| 750 | fu_common_spawn_sync (const gchar * const * argv, |
| 751 | FuOutputHandler handler_cb, |
| 752 | gpointer handler_user_data, |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 753 | guint timeout_ms, |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 754 | GCancellable *cancellable, GError **error) |
| 755 | { |
| 756 | g_autoptr(FuCommonSpawnHelper) helper = NULL; |
| 757 | g_autoptr(GSubprocess) subprocess = NULL; |
Richard Hughes | 455fdd3 | 2017-08-16 12:26:44 +0100 | [diff] [blame] | 758 | g_autofree gchar *argv_str = NULL; |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 759 | gulong cancellable_id = 0; |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 760 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 761 | g_return_val_if_fail (argv != NULL, FALSE); |
| 762 | g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE); |
| 763 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 764 | |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 765 | /* create subprocess */ |
Richard Hughes | 455fdd3 | 2017-08-16 12:26:44 +0100 | [diff] [blame] | 766 | argv_str = g_strjoinv (" ", (gchar **) argv); |
| 767 | g_debug ("running '%s'", argv_str); |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 768 | subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_PIPE | |
| 769 | G_SUBPROCESS_FLAGS_STDERR_MERGE, error); |
| 770 | if (subprocess == NULL) |
| 771 | return FALSE; |
| 772 | |
| 773 | /* watch for process to exit */ |
| 774 | helper = g_new0 (FuCommonSpawnHelper, 1); |
| 775 | helper->handler_cb = handler_cb; |
| 776 | helper->handler_user_data = handler_user_data; |
| 777 | helper->loop = g_main_loop_new (NULL, FALSE); |
| 778 | helper->stream = g_subprocess_get_stdout_pipe (subprocess); |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 779 | |
| 780 | /* always create a cancellable, and connect up the parent */ |
| 781 | helper->cancellable = g_cancellable_new (); |
| 782 | if (cancellable != NULL) { |
| 783 | cancellable_id = g_cancellable_connect (cancellable, |
| 784 | G_CALLBACK (fu_common_spawn_cancelled_cb), |
| 785 | helper, NULL); |
| 786 | } |
| 787 | |
| 788 | /* allow timeout */ |
| 789 | if (timeout_ms > 0) { |
| 790 | helper->timeout_id = g_timeout_add (timeout_ms, |
| 791 | fu_common_spawn_timeout_cb, |
| 792 | helper); |
| 793 | } |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 794 | fu_common_spawn_create_pollable_source (helper); |
| 795 | g_main_loop_run (helper->loop); |
Richard Hughes | b768e4d | 2019-02-26 13:55:18 +0000 | [diff] [blame] | 796 | g_cancellable_disconnect (cancellable, cancellable_id); |
| 797 | if (g_cancellable_set_error_if_cancelled (helper->cancellable, error)) |
| 798 | return FALSE; |
Richard Hughes | 049ccc8 | 2017-08-09 15:26:56 +0100 | [diff] [blame] | 799 | return g_subprocess_wait_check (subprocess, cancellable, error); |
| 800 | } |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 801 | |
| 802 | /** |
| 803 | * fu_common_write_uint16: |
| 804 | * @buf: A writable buffer |
| 805 | * @val_native: a value in host byte-order |
Richard Hughes | 8aa7239 | 2018-05-02 08:38:43 +0100 | [diff] [blame] | 806 | * @endian: A #FuEndianType, e.g. %G_LITTLE_ENDIAN |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 807 | * |
| 808 | * Writes a value to a buffer using a specified endian. |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 809 | * |
| 810 | * Since: 1.0.3 |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 811 | **/ |
| 812 | void |
| 813 | fu_common_write_uint16 (guint8 *buf, guint16 val_native, FuEndianType endian) |
| 814 | { |
| 815 | guint16 val_hw; |
| 816 | switch (endian) { |
| 817 | case G_BIG_ENDIAN: |
| 818 | val_hw = GUINT16_TO_BE(val_native); |
| 819 | break; |
| 820 | case G_LITTLE_ENDIAN: |
| 821 | val_hw = GUINT16_TO_LE(val_native); |
| 822 | break; |
| 823 | default: |
| 824 | g_assert_not_reached (); |
| 825 | } |
| 826 | memcpy (buf, &val_hw, sizeof(val_hw)); |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * fu_common_write_uint32: |
| 831 | * @buf: A writable buffer |
| 832 | * @val_native: a value in host byte-order |
Richard Hughes | 8aa7239 | 2018-05-02 08:38:43 +0100 | [diff] [blame] | 833 | * @endian: A #FuEndianType, e.g. %G_LITTLE_ENDIAN |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 834 | * |
| 835 | * Writes a value to a buffer using a specified endian. |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 836 | * |
| 837 | * Since: 1.0.3 |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 838 | **/ |
| 839 | void |
| 840 | fu_common_write_uint32 (guint8 *buf, guint32 val_native, FuEndianType endian) |
| 841 | { |
| 842 | guint32 val_hw; |
| 843 | switch (endian) { |
| 844 | case G_BIG_ENDIAN: |
| 845 | val_hw = GUINT32_TO_BE(val_native); |
| 846 | break; |
| 847 | case G_LITTLE_ENDIAN: |
| 848 | val_hw = GUINT32_TO_LE(val_native); |
| 849 | break; |
| 850 | default: |
| 851 | g_assert_not_reached (); |
| 852 | } |
| 853 | memcpy (buf, &val_hw, sizeof(val_hw)); |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * fu_common_read_uint16: |
| 858 | * @buf: A readable buffer |
Richard Hughes | 8aa7239 | 2018-05-02 08:38:43 +0100 | [diff] [blame] | 859 | * @endian: A #FuEndianType, e.g. %G_LITTLE_ENDIAN |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 860 | * |
| 861 | * Read a value from a buffer using a specified endian. |
| 862 | * |
| 863 | * Returns: a value in host byte-order |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 864 | * |
| 865 | * Since: 1.0.3 |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 866 | **/ |
| 867 | guint16 |
| 868 | fu_common_read_uint16 (const guint8 *buf, FuEndianType endian) |
| 869 | { |
| 870 | guint16 val_hw, val_native; |
| 871 | memcpy (&val_hw, buf, sizeof(val_hw)); |
| 872 | switch (endian) { |
| 873 | case G_BIG_ENDIAN: |
| 874 | val_native = GUINT16_FROM_BE(val_hw); |
| 875 | break; |
| 876 | case G_LITTLE_ENDIAN: |
| 877 | val_native = GUINT16_FROM_LE(val_hw); |
| 878 | break; |
| 879 | default: |
| 880 | g_assert_not_reached (); |
| 881 | } |
| 882 | return val_native; |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * fu_common_read_uint32: |
| 887 | * @buf: A readable buffer |
Richard Hughes | 8aa7239 | 2018-05-02 08:38:43 +0100 | [diff] [blame] | 888 | * @endian: A #FuEndianType, e.g. %G_LITTLE_ENDIAN |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 889 | * |
| 890 | * Read a value from a buffer using a specified endian. |
| 891 | * |
| 892 | * Returns: a value in host byte-order |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 893 | * |
| 894 | * Since: 1.0.3 |
Richard Hughes | ae252cd | 2017-12-08 10:48:15 +0000 | [diff] [blame] | 895 | **/ |
| 896 | guint32 |
| 897 | fu_common_read_uint32 (const guint8 *buf, FuEndianType endian) |
| 898 | { |
| 899 | guint32 val_hw, val_native; |
| 900 | memcpy (&val_hw, buf, sizeof(val_hw)); |
| 901 | switch (endian) { |
| 902 | case G_BIG_ENDIAN: |
| 903 | val_native = GUINT32_FROM_BE(val_hw); |
| 904 | break; |
| 905 | case G_LITTLE_ENDIAN: |
| 906 | val_native = GUINT32_FROM_LE(val_hw); |
| 907 | break; |
| 908 | default: |
| 909 | g_assert_not_reached (); |
| 910 | } |
| 911 | return val_native; |
| 912 | } |
Richard Hughes | e82eef3 | 2018-05-20 10:41:26 +0100 | [diff] [blame] | 913 | |
Richard Hughes | 73bf233 | 2018-08-28 09:38:09 +0100 | [diff] [blame] | 914 | /** |
| 915 | * fu_common_strtoull: |
| 916 | * @str: A string, e.g. "0x1234" |
| 917 | * |
| 918 | * Converts a string value to an integer. Values are assumed base 10, unless |
| 919 | * prefixed with "0x" where they are parsed as base 16. |
| 920 | * |
| 921 | * Returns: integer value, or 0x0 for error |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 922 | * |
| 923 | * Since: 1.1.2 |
Richard Hughes | 73bf233 | 2018-08-28 09:38:09 +0100 | [diff] [blame] | 924 | **/ |
| 925 | guint64 |
| 926 | fu_common_strtoull (const gchar *str) |
| 927 | { |
| 928 | guint base = 10; |
| 929 | if (str == NULL) |
| 930 | return 0x0; |
| 931 | if (g_str_has_prefix (str, "0x")) { |
| 932 | str += 2; |
| 933 | base = 16; |
| 934 | } |
| 935 | return g_ascii_strtoull (str, NULL, base); |
| 936 | } |
| 937 | |
Richard Hughes | a574a75 | 2018-08-31 13:31:03 +0100 | [diff] [blame] | 938 | /** |
| 939 | * fu_common_strstrip: |
| 940 | * @str: A string, e.g. " test " |
| 941 | * |
| 942 | * Removes leading and trailing whitespace from a constant string. |
| 943 | * |
| 944 | * Returns: newly allocated string |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 945 | * |
| 946 | * Since: 1.1.2 |
Richard Hughes | a574a75 | 2018-08-31 13:31:03 +0100 | [diff] [blame] | 947 | **/ |
| 948 | gchar * |
| 949 | fu_common_strstrip (const gchar *str) |
| 950 | { |
| 951 | guint head = G_MAXUINT; |
| 952 | guint tail = 0; |
| 953 | |
| 954 | g_return_val_if_fail (str != NULL, NULL); |
| 955 | |
| 956 | /* find first non-space char */ |
| 957 | for (guint i = 0; str[i] != '\0'; i++) { |
| 958 | if (str[i] != ' ') { |
| 959 | head = i; |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | if (head == G_MAXUINT) |
| 964 | return g_strdup (""); |
| 965 | |
| 966 | /* find last non-space char */ |
| 967 | for (guint i = head; str[i] != '\0'; i++) { |
Mario Limonciello | ef3c766 | 2019-09-04 23:37:59 -0500 | [diff] [blame] | 968 | if (!g_ascii_isspace (str[i])) |
Richard Hughes | a574a75 | 2018-08-31 13:31:03 +0100 | [diff] [blame] | 969 | tail = i; |
| 970 | } |
| 971 | return g_strndup (str + head, tail - head + 1); |
| 972 | } |
| 973 | |
Richard Hughes | e82eef3 | 2018-05-20 10:41:26 +0100 | [diff] [blame] | 974 | static const GError * |
| 975 | fu_common_error_array_find (GPtrArray *errors, FwupdError error_code) |
| 976 | { |
| 977 | for (guint j = 0; j < errors->len; j++) { |
| 978 | const GError *error = g_ptr_array_index (errors, j); |
| 979 | if (g_error_matches (error, FWUPD_ERROR, error_code)) |
| 980 | return error; |
| 981 | } |
| 982 | return NULL; |
| 983 | } |
| 984 | |
| 985 | static guint |
| 986 | fu_common_error_array_count (GPtrArray *errors, FwupdError error_code) |
| 987 | { |
| 988 | guint cnt = 0; |
| 989 | for (guint j = 0; j < errors->len; j++) { |
| 990 | const GError *error = g_ptr_array_index (errors, j); |
| 991 | if (g_error_matches (error, FWUPD_ERROR, error_code)) |
| 992 | cnt++; |
| 993 | } |
| 994 | return cnt; |
| 995 | } |
| 996 | |
| 997 | static gboolean |
| 998 | fu_common_error_array_matches_any (GPtrArray *errors, FwupdError *error_codes) |
| 999 | { |
| 1000 | for (guint j = 0; j < errors->len; j++) { |
| 1001 | const GError *error = g_ptr_array_index (errors, j); |
| 1002 | gboolean matches_any = FALSE; |
| 1003 | for (guint i = 0; error_codes[i] != FWUPD_ERROR_LAST; i++) { |
| 1004 | if (g_error_matches (error, FWUPD_ERROR, error_codes[i])) { |
| 1005 | matches_any = TRUE; |
| 1006 | break; |
| 1007 | } |
| 1008 | } |
| 1009 | if (!matches_any) |
| 1010 | return FALSE; |
| 1011 | } |
| 1012 | return TRUE; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * fu_common_error_array_get_best: |
| 1017 | * @errors: (element-type GError): array of errors |
| 1018 | * |
| 1019 | * Finds the 'best' error to show the user from a array of errors, creating a |
| 1020 | * completely bespoke error where required. |
| 1021 | * |
| 1022 | * Returns: (transfer full): a #GError, never %NULL |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1023 | * |
| 1024 | * Since: 1.0.8 |
Richard Hughes | e82eef3 | 2018-05-20 10:41:26 +0100 | [diff] [blame] | 1025 | **/ |
| 1026 | GError * |
| 1027 | fu_common_error_array_get_best (GPtrArray *errors) |
| 1028 | { |
| 1029 | FwupdError err_prio[] = { FWUPD_ERROR_INVALID_FILE, |
| 1030 | FWUPD_ERROR_VERSION_SAME, |
| 1031 | FWUPD_ERROR_VERSION_NEWER, |
| 1032 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1033 | FWUPD_ERROR_INTERNAL, |
| 1034 | FWUPD_ERROR_NOT_FOUND, |
| 1035 | FWUPD_ERROR_LAST }; |
| 1036 | FwupdError err_all_uptodate[] = { FWUPD_ERROR_VERSION_SAME, |
| 1037 | FWUPD_ERROR_NOT_FOUND, |
| 1038 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1039 | FWUPD_ERROR_LAST }; |
| 1040 | FwupdError err_all_newer[] = { FWUPD_ERROR_VERSION_NEWER, |
| 1041 | FWUPD_ERROR_VERSION_SAME, |
| 1042 | FWUPD_ERROR_NOT_FOUND, |
| 1043 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1044 | FWUPD_ERROR_LAST }; |
| 1045 | |
| 1046 | /* are all the errors either GUID-not-matched or version-same? */ |
| 1047 | if (fu_common_error_array_count (errors, FWUPD_ERROR_VERSION_SAME) > 1 && |
| 1048 | fu_common_error_array_matches_any (errors, err_all_uptodate)) { |
| 1049 | return g_error_new (FWUPD_ERROR, |
| 1050 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1051 | "All updatable firmware is already installed"); |
| 1052 | } |
| 1053 | |
| 1054 | /* are all the errors either GUID-not-matched or version same or newer? */ |
| 1055 | if (fu_common_error_array_count (errors, FWUPD_ERROR_VERSION_NEWER) > 1 && |
| 1056 | fu_common_error_array_matches_any (errors, err_all_newer)) { |
| 1057 | return g_error_new (FWUPD_ERROR, |
| 1058 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1059 | "All updatable devices already have newer versions"); |
| 1060 | } |
| 1061 | |
| 1062 | /* get the most important single error */ |
| 1063 | for (guint i = 0; err_prio[i] != FWUPD_ERROR_LAST; i++) { |
| 1064 | const GError *error_tmp = fu_common_error_array_find (errors, err_prio[i]); |
| 1065 | if (error_tmp != NULL) |
| 1066 | return g_error_copy (error_tmp); |
| 1067 | } |
| 1068 | |
| 1069 | /* fall back to something */ |
| 1070 | return g_error_new (FWUPD_ERROR, |
| 1071 | FWUPD_ERROR_NOT_FOUND, |
| 1072 | "No supported devices found"); |
| 1073 | } |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1074 | |
| 1075 | /** |
| 1076 | * fu_common_get_path: |
| 1077 | * @path_kind: A #FuPathKind e.g. %FU_PATH_KIND_DATADIR_PKG |
| 1078 | * |
| 1079 | * Gets a fwupd-specific system path. These can be overridden with various |
| 1080 | * environment variables, for instance %FWUPD_DATADIR. |
| 1081 | * |
| 1082 | * Returns: a system path, or %NULL if invalid |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1083 | * |
| 1084 | * Since: 1.0.8 |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1085 | **/ |
| 1086 | gchar * |
| 1087 | fu_common_get_path (FuPathKind path_kind) |
| 1088 | { |
| 1089 | const gchar *tmp; |
| 1090 | g_autofree gchar *basedir = NULL; |
| 1091 | |
| 1092 | switch (path_kind) { |
| 1093 | /* /var */ |
| 1094 | case FU_PATH_KIND_LOCALSTATEDIR: |
| 1095 | tmp = g_getenv ("FWUPD_LOCALSTATEDIR"); |
| 1096 | if (tmp != NULL) |
| 1097 | return g_strdup (tmp); |
| 1098 | tmp = g_getenv ("SNAP_USER_DATA"); |
| 1099 | if (tmp != NULL) |
Richard Hughes | 668ee21 | 2019-11-22 09:17:46 +0000 | [diff] [blame] | 1100 | return g_build_filename (tmp, FWUPD_LOCALSTATEDIR, NULL); |
| 1101 | return g_build_filename (FWUPD_LOCALSTATEDIR, NULL); |
Richard Hughes | c368958 | 2020-05-06 12:35:20 +0100 | [diff] [blame] | 1102 | /* /proc */ |
| 1103 | case FU_PATH_KIND_PROCFS: |
| 1104 | tmp = g_getenv ("FWUPD_PROCFS"); |
| 1105 | if (tmp != NULL) |
| 1106 | return g_strdup (tmp); |
| 1107 | return g_strdup ("/proc"); |
Richard Hughes | 282b10d | 2018-06-22 14:48:00 +0100 | [diff] [blame] | 1108 | /* /sys/firmware */ |
| 1109 | case FU_PATH_KIND_SYSFSDIR_FW: |
| 1110 | tmp = g_getenv ("FWUPD_SYSFSFWDIR"); |
| 1111 | if (tmp != NULL) |
| 1112 | return g_strdup (tmp); |
| 1113 | return g_strdup ("/sys/firmware"); |
Mario Limonciello | 3960265 | 2019-04-29 21:08:58 -0500 | [diff] [blame] | 1114 | /* /sys/class/tpm */ |
Richard Hughes | b56015e | 2018-12-12 09:25:32 +0000 | [diff] [blame] | 1115 | case FU_PATH_KIND_SYSFSDIR_TPM: |
| 1116 | tmp = g_getenv ("FWUPD_SYSFSTPMDIR"); |
| 1117 | if (tmp != NULL) |
| 1118 | return g_strdup (tmp); |
| 1119 | return g_strdup ("/sys/class/tpm"); |
Richard Hughes | 83390f6 | 2018-06-22 20:36:46 +0100 | [diff] [blame] | 1120 | /* /sys/bus/platform/drivers */ |
| 1121 | case FU_PATH_KIND_SYSFSDIR_DRIVERS: |
| 1122 | tmp = g_getenv ("FWUPD_SYSFSDRIVERDIR"); |
| 1123 | if (tmp != NULL) |
| 1124 | return g_strdup (tmp); |
| 1125 | return g_strdup ("/sys/bus/platform/drivers"); |
Mario Limonciello | 9dce1f7 | 2020-02-04 09:12:52 -0600 | [diff] [blame] | 1126 | /* /sys/kernel/security */ |
| 1127 | case FU_PATH_KIND_SYSFSDIR_SECURITY: |
| 1128 | tmp = g_getenv ("FWUPD_SYSFSSECURITYDIR"); |
| 1129 | if (tmp != NULL) |
| 1130 | return g_strdup (tmp); |
| 1131 | return g_strdup ("/sys/kernel/security"); |
Richard Hughes | a715791 | 2020-05-11 17:14:05 +0100 | [diff] [blame] | 1132 | /* /sys/firmware/acpi/tables */ |
| 1133 | case FU_PATH_KIND_ACPI_TABLES: |
| 1134 | tmp = g_getenv ("FWUPD_ACPITABLESDIR"); |
| 1135 | if (tmp != NULL) |
| 1136 | return g_strdup (tmp); |
| 1137 | return g_strdup ("/sys/firmware/acpi/tables"); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1138 | /* /etc */ |
| 1139 | case FU_PATH_KIND_SYSCONFDIR: |
| 1140 | tmp = g_getenv ("FWUPD_SYSCONFDIR"); |
| 1141 | if (tmp != NULL) |
| 1142 | return g_strdup (tmp); |
| 1143 | tmp = g_getenv ("SNAP_USER_DATA"); |
| 1144 | if (tmp != NULL) |
Richard Hughes | 668ee21 | 2019-11-22 09:17:46 +0000 | [diff] [blame] | 1145 | return g_build_filename (tmp, FWUPD_SYSCONFDIR, NULL); |
| 1146 | return g_strdup (FWUPD_SYSCONFDIR); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1147 | /* /usr/lib/<triplet>/fwupd-plugins-3 */ |
| 1148 | case FU_PATH_KIND_PLUGINDIR_PKG: |
| 1149 | tmp = g_getenv ("FWUPD_PLUGINDIR"); |
| 1150 | if (tmp != NULL) |
| 1151 | return g_strdup (tmp); |
| 1152 | tmp = g_getenv ("SNAP"); |
| 1153 | if (tmp != NULL) |
Richard Hughes | 668ee21 | 2019-11-22 09:17:46 +0000 | [diff] [blame] | 1154 | return g_build_filename (tmp, FWUPD_PLUGINDIR, NULL); |
| 1155 | return g_build_filename (FWUPD_PLUGINDIR, NULL); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1156 | /* /usr/share/fwupd */ |
| 1157 | case FU_PATH_KIND_DATADIR_PKG: |
| 1158 | tmp = g_getenv ("FWUPD_DATADIR"); |
| 1159 | if (tmp != NULL) |
| 1160 | return g_strdup (tmp); |
| 1161 | tmp = g_getenv ("SNAP"); |
| 1162 | if (tmp != NULL) |
Richard Hughes | 668ee21 | 2019-11-22 09:17:46 +0000 | [diff] [blame] | 1163 | return g_build_filename (tmp, FWUPD_DATADIR, PACKAGE_NAME, NULL); |
| 1164 | return g_build_filename (FWUPD_DATADIR, PACKAGE_NAME, NULL); |
Mario Limonciello | e6e2bf9 | 2018-07-10 12:11:25 -0500 | [diff] [blame] | 1165 | /* /usr/libexec/fwupd/efi */ |
| 1166 | case FU_PATH_KIND_EFIAPPDIR: |
| 1167 | tmp = g_getenv ("FWUPD_EFIAPPDIR"); |
| 1168 | if (tmp != NULL) |
| 1169 | return g_strdup (tmp); |
| 1170 | #ifdef EFI_APP_LOCATION |
| 1171 | tmp = g_getenv ("SNAP"); |
| 1172 | if (tmp != NULL) |
| 1173 | return g_build_filename (tmp, EFI_APP_LOCATION, NULL); |
| 1174 | return g_strdup (EFI_APP_LOCATION); |
| 1175 | #else |
| 1176 | return NULL; |
| 1177 | #endif |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1178 | /* /etc/fwupd */ |
| 1179 | case FU_PATH_KIND_SYSCONFDIR_PKG: |
Mario Limonciello | 277c196 | 2019-08-26 23:42:23 -0500 | [diff] [blame] | 1180 | tmp = g_getenv ("CONFIGURATION_DIRECTORY"); |
Mario Limonciello | 695cb58 | 2019-12-12 10:45:42 -0600 | [diff] [blame] | 1181 | if (tmp != NULL && g_file_test (tmp, G_FILE_TEST_EXISTS)) |
Mario Limonciello | 277c196 | 2019-08-26 23:42:23 -0500 | [diff] [blame] | 1182 | return g_build_filename (tmp, NULL); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1183 | basedir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR); |
| 1184 | return g_build_filename (basedir, PACKAGE_NAME, NULL); |
| 1185 | /* /var/lib/fwupd */ |
| 1186 | case FU_PATH_KIND_LOCALSTATEDIR_PKG: |
Mario Limonciello | 277c196 | 2019-08-26 23:42:23 -0500 | [diff] [blame] | 1187 | tmp = g_getenv ("STATE_DIRECTORY"); |
Mario Limonciello | 695cb58 | 2019-12-12 10:45:42 -0600 | [diff] [blame] | 1188 | if (tmp != NULL && g_file_test (tmp, G_FILE_TEST_EXISTS)) |
Mario Limonciello | 277c196 | 2019-08-26 23:42:23 -0500 | [diff] [blame] | 1189 | return g_build_filename (tmp, NULL); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1190 | basedir = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR); |
| 1191 | return g_build_filename (basedir, "lib", PACKAGE_NAME, NULL); |
| 1192 | /* /var/cache/fwupd */ |
| 1193 | case FU_PATH_KIND_CACHEDIR_PKG: |
Mario Limonciello | 277c196 | 2019-08-26 23:42:23 -0500 | [diff] [blame] | 1194 | tmp = g_getenv ("CACHE_DIRECTORY"); |
Mario Limonciello | 695cb58 | 2019-12-12 10:45:42 -0600 | [diff] [blame] | 1195 | if (tmp != NULL && g_file_test (tmp, G_FILE_TEST_EXISTS)) |
Mario Limonciello | 277c196 | 2019-08-26 23:42:23 -0500 | [diff] [blame] | 1196 | return g_build_filename (tmp, NULL); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1197 | basedir = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR); |
| 1198 | return g_build_filename (basedir, "cache", PACKAGE_NAME, NULL); |
Richard Hughes | afdba37 | 2019-11-23 12:57:35 +0000 | [diff] [blame] | 1199 | case FU_PATH_KIND_OFFLINE_TRIGGER: |
| 1200 | tmp = g_getenv ("FWUPD_OFFLINE_TRIGGER"); |
| 1201 | if (tmp != NULL) |
| 1202 | return g_strdup (tmp); |
| 1203 | return g_strdup ("/system-update"); |
Mario Limonciello | 057c67a | 2019-05-23 10:44:19 -0500 | [diff] [blame] | 1204 | case FU_PATH_KIND_POLKIT_ACTIONS: |
| 1205 | #ifdef POLKIT_ACTIONDIR |
| 1206 | return g_strdup (POLKIT_ACTIONDIR); |
| 1207 | #else |
| 1208 | return NULL; |
| 1209 | #endif |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1210 | /* this shouldn't happen */ |
| 1211 | default: |
Richard Hughes | beb47a8 | 2018-09-11 18:28:53 +0100 | [diff] [blame] | 1212 | g_warning ("cannot build path for unknown kind %u", path_kind); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | return NULL; |
| 1216 | } |
Richard Hughes | 83e56c1 | 2018-10-10 20:24:41 +0100 | [diff] [blame] | 1217 | |
| 1218 | /** |
| 1219 | * fu_common_string_replace: |
| 1220 | * @string: The #GString to operate on |
| 1221 | * @search: The text to search for |
| 1222 | * @replace: The text to use for substitutions |
| 1223 | * |
| 1224 | * Performs multiple search and replace operations on the given string. |
| 1225 | * |
| 1226 | * Returns: the number of replacements done, or 0 if @search is not found. |
| 1227 | * |
| 1228 | * Since: 1.2.0 |
| 1229 | **/ |
| 1230 | guint |
| 1231 | fu_common_string_replace (GString *string, const gchar *search, const gchar *replace) |
| 1232 | { |
| 1233 | gchar *tmp; |
| 1234 | guint count = 0; |
| 1235 | gsize search_idx = 0; |
| 1236 | gsize replace_len; |
| 1237 | gsize search_len; |
| 1238 | |
| 1239 | g_return_val_if_fail (string != NULL, 0); |
| 1240 | g_return_val_if_fail (search != NULL, 0); |
| 1241 | g_return_val_if_fail (replace != NULL, 0); |
| 1242 | |
| 1243 | /* nothing to do */ |
| 1244 | if (string->len == 0) |
| 1245 | return 0; |
| 1246 | |
| 1247 | search_len = strlen (search); |
| 1248 | replace_len = strlen (replace); |
| 1249 | |
| 1250 | do { |
| 1251 | tmp = g_strstr_len (string->str + search_idx, -1, search); |
| 1252 | if (tmp == NULL) |
| 1253 | break; |
| 1254 | |
| 1255 | /* advance the counter in case @replace contains @search */ |
| 1256 | search_idx = (gsize) (tmp - string->str); |
| 1257 | |
| 1258 | /* reallocate the string if required */ |
| 1259 | if (search_len > replace_len) { |
| 1260 | g_string_erase (string, |
| 1261 | (gssize) search_idx, |
| 1262 | (gssize) (search_len - replace_len)); |
| 1263 | memcpy (tmp, replace, replace_len); |
| 1264 | } else if (search_len < replace_len) { |
| 1265 | g_string_insert_len (string, |
| 1266 | (gssize) search_idx, |
| 1267 | replace, |
| 1268 | (gssize) (replace_len - search_len)); |
| 1269 | /* we have to treat this specially as it could have |
| 1270 | * been reallocated when the insertion happened */ |
| 1271 | memcpy (string->str + search_idx, replace, replace_len); |
| 1272 | } else { |
| 1273 | /* just memcmp in the new string */ |
| 1274 | memcpy (tmp, replace, replace_len); |
| 1275 | } |
| 1276 | search_idx += replace_len; |
| 1277 | count++; |
| 1278 | } while (TRUE); |
| 1279 | |
| 1280 | return count; |
| 1281 | } |
Richard Hughes | e59cb9a | 2018-12-05 14:37:40 +0000 | [diff] [blame] | 1282 | |
Richard Hughes | ae96a1f | 2019-09-23 11:16:36 +0100 | [diff] [blame] | 1283 | /** |
| 1284 | * fu_common_strwidth: |
| 1285 | * @text: The string to operate on |
| 1286 | * |
| 1287 | * Returns the width of the string in displayed characters on the console. |
| 1288 | * |
| 1289 | * Returns: width of text |
| 1290 | * |
| 1291 | * Since: 1.3.2 |
| 1292 | **/ |
| 1293 | gsize |
| 1294 | fu_common_strwidth (const gchar *text) |
| 1295 | { |
| 1296 | const gchar *p = text; |
| 1297 | gsize width = 0; |
Richard Hughes | 4d2c0f8 | 2020-07-07 12:02:30 +0100 | [diff] [blame] | 1298 | |
| 1299 | g_return_val_if_fail (text != NULL, 0); |
| 1300 | |
Richard Hughes | ae96a1f | 2019-09-23 11:16:36 +0100 | [diff] [blame] | 1301 | while (*p) { |
| 1302 | gunichar c = g_utf8_get_char (p); |
| 1303 | if (g_unichar_iswide (c)) |
| 1304 | width += 2; |
| 1305 | else if (!g_unichar_iszerowidth (c)) |
| 1306 | width += 1; |
| 1307 | p = g_utf8_next_char (p); |
| 1308 | } |
| 1309 | return width; |
| 1310 | } |
| 1311 | |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1312 | /** |
| 1313 | * fu_common_string_append_kv: |
| 1314 | * @str: A #GString |
| 1315 | * @idt: The indent |
| 1316 | * @key: A string to append |
| 1317 | * @value: a string to append |
| 1318 | * |
| 1319 | * Appends a key and string value to a string |
| 1320 | * |
| 1321 | * Since: 1.2.4 |
| 1322 | */ |
Richard Hughes | cea28de | 2019-08-09 11:16:40 +0100 | [diff] [blame] | 1323 | void |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1324 | fu_common_string_append_kv (GString *str, guint idt, const gchar *key, const gchar *value) |
Richard Hughes | cea28de | 2019-08-09 11:16:40 +0100 | [diff] [blame] | 1325 | { |
Richard Hughes | 2506dbf | 2020-09-03 10:04:19 +0100 | [diff] [blame] | 1326 | const guint align = 24; |
Richard Hughes | 847cae8 | 2019-08-27 11:22:23 +0100 | [diff] [blame] | 1327 | gsize keysz; |
Richard Hughes | cea28de | 2019-08-09 11:16:40 +0100 | [diff] [blame] | 1328 | |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1329 | g_return_if_fail (idt * 2 < align); |
Richard Hughes | cea28de | 2019-08-09 11:16:40 +0100 | [diff] [blame] | 1330 | |
| 1331 | /* ignore */ |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1332 | if (key == NULL) |
Richard Hughes | cea28de | 2019-08-09 11:16:40 +0100 | [diff] [blame] | 1333 | return; |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1334 | for (gsize i = 0; i < idt; i++) |
| 1335 | g_string_append (str, " "); |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1336 | if (key[0] != '\0') { |
| 1337 | g_string_append_printf (str, "%s:", key); |
Richard Hughes | ae96a1f | 2019-09-23 11:16:36 +0100 | [diff] [blame] | 1338 | keysz = (idt * 2) + fu_common_strwidth (key) + 1; |
Richard Hughes | 847cae8 | 2019-08-27 11:22:23 +0100 | [diff] [blame] | 1339 | } else { |
| 1340 | keysz = idt * 2; |
Mario Limonciello | fee8f49 | 2019-08-18 12:16:07 -0500 | [diff] [blame] | 1341 | } |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1342 | if (value != NULL) { |
Mario Limonciello | 1dbb82d | 2019-09-20 14:22:14 -0500 | [diff] [blame] | 1343 | g_auto(GStrv) split = NULL; |
| 1344 | split = g_strsplit (value, "\n", -1); |
| 1345 | for (guint i = 0; split[i] != NULL; i++) { |
| 1346 | if (i == 0) { |
| 1347 | for (gsize j = keysz; j < align; j++) |
| 1348 | g_string_append (str, " "); |
| 1349 | } else { |
| 1350 | for (gsize j = 0; j < idt; j++) |
| 1351 | g_string_append (str, " "); |
| 1352 | } |
| 1353 | g_string_append (str, split[i]); |
| 1354 | g_string_append (str, "\n"); |
| 1355 | } |
| 1356 | } else { |
| 1357 | g_string_append (str, "\n"); |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1358 | } |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1359 | } |
| 1360 | |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1361 | /** |
| 1362 | * fu_common_string_append_ku: |
| 1363 | * @str: A #GString |
| 1364 | * @idt: The indent |
| 1365 | * @key: A string to append |
| 1366 | * @value: guint64 |
| 1367 | * |
| 1368 | * Appends a key and unsigned integer to a string |
| 1369 | * |
| 1370 | * Since: 1.2.4 |
| 1371 | */ |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1372 | void |
| 1373 | fu_common_string_append_ku (GString *str, guint idt, const gchar *key, guint64 value) |
| 1374 | { |
| 1375 | g_autofree gchar *tmp = g_strdup_printf ("%" G_GUINT64_FORMAT, value); |
| 1376 | fu_common_string_append_kv (str, idt, key, tmp); |
| 1377 | } |
| 1378 | |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1379 | /** |
| 1380 | * fu_common_string_append_kx: |
| 1381 | * @str: A #GString |
| 1382 | * @idt: The indent |
| 1383 | * @key: A string to append |
| 1384 | * @value: guint64 |
| 1385 | * |
| 1386 | * Appends a key and hex integer to a string |
| 1387 | * |
| 1388 | * Since: 1.2.4 |
| 1389 | */ |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1390 | void |
| 1391 | fu_common_string_append_kx (GString *str, guint idt, const gchar *key, guint64 value) |
| 1392 | { |
| 1393 | g_autofree gchar *tmp = g_strdup_printf ("0x%x", (guint) value); |
| 1394 | fu_common_string_append_kv (str, idt, key, tmp); |
| 1395 | } |
| 1396 | |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1397 | /** |
| 1398 | * fu_common_string_append_kb: |
| 1399 | * @str: A #GString |
| 1400 | * @idt: The indent |
| 1401 | * @key: A string to append |
| 1402 | * @value: Boolean |
| 1403 | * |
| 1404 | * Appends a key and boolean value to a string |
| 1405 | * |
| 1406 | * Since: 1.2.4 |
| 1407 | */ |
Richard Hughes | 6e3e62b | 2019-08-14 10:43:08 +0100 | [diff] [blame] | 1408 | void |
| 1409 | fu_common_string_append_kb (GString *str, guint idt, const gchar *key, gboolean value) |
| 1410 | { |
| 1411 | fu_common_string_append_kv (str, idt, key, value ? "true" : "false"); |
Richard Hughes | cea28de | 2019-08-09 11:16:40 +0100 | [diff] [blame] | 1412 | } |
| 1413 | |
Richard Hughes | e59cb9a | 2018-12-05 14:37:40 +0000 | [diff] [blame] | 1414 | /** |
Richard Hughes | 3548186 | 2019-01-06 12:01:58 +0000 | [diff] [blame] | 1415 | * fu_common_dump_full: |
| 1416 | * @log_domain: log domain, typically %G_LOG_DOMAIN or %NULL |
| 1417 | * @title: prefix title, or %NULL |
| 1418 | * @data: buffer to print |
| 1419 | * @len: the size of @data |
| 1420 | * @columns: break new lines after this many bytes |
| 1421 | * @flags: some #FuDumpFlags, e.g. %FU_DUMP_FLAGS_SHOW_ASCII |
| 1422 | * |
| 1423 | * Dumps a raw buffer to the screen. |
| 1424 | * |
| 1425 | * Since: 1.2.4 |
| 1426 | **/ |
| 1427 | void |
| 1428 | fu_common_dump_full (const gchar *log_domain, |
| 1429 | const gchar *title, |
| 1430 | const guint8 *data, |
| 1431 | gsize len, |
| 1432 | guint columns, |
| 1433 | FuDumpFlags flags) |
| 1434 | { |
| 1435 | g_autoptr(GString) str = g_string_new (NULL); |
| 1436 | |
| 1437 | /* optional */ |
| 1438 | if (title != NULL) |
| 1439 | g_string_append_printf (str, "%s:", title); |
| 1440 | |
| 1441 | /* if more than can fit on one line then start afresh */ |
| 1442 | if (len > columns || flags & FU_DUMP_FLAGS_SHOW_ADDRESSES) { |
| 1443 | g_string_append (str, "\n"); |
| 1444 | } else { |
| 1445 | for (gsize i = str->len; i < 16; i++) |
| 1446 | g_string_append (str, " "); |
| 1447 | } |
| 1448 | |
| 1449 | /* offset line */ |
| 1450 | if (flags & FU_DUMP_FLAGS_SHOW_ADDRESSES) { |
| 1451 | g_string_append (str, " │ "); |
| 1452 | for (gsize i = 0; i < columns; i++) |
| 1453 | g_string_append_printf (str, "%02x ", (guint) i); |
| 1454 | g_string_append (str, "\n───────┼"); |
| 1455 | for (gsize i = 0; i < columns; i++) |
| 1456 | g_string_append (str, "───"); |
| 1457 | g_string_append_printf (str, "\n0x%04x │ ", (guint) 0); |
| 1458 | } |
| 1459 | |
| 1460 | /* print each row */ |
| 1461 | for (gsize i = 0; i < len; i++) { |
| 1462 | g_string_append_printf (str, "%02x ", data[i]); |
| 1463 | |
| 1464 | /* optionally print ASCII char */ |
| 1465 | if (flags & FU_DUMP_FLAGS_SHOW_ASCII) { |
| 1466 | if (g_ascii_isprint (data[i])) |
| 1467 | g_string_append_printf (str, "[%c] ", data[i]); |
| 1468 | else |
| 1469 | g_string_append (str, "[?] "); |
| 1470 | } |
| 1471 | |
| 1472 | /* new row required */ |
| 1473 | if (i > 0 && i != len - 1 && (i + 1) % columns == 0) { |
| 1474 | g_string_append (str, "\n"); |
| 1475 | if (flags & FU_DUMP_FLAGS_SHOW_ADDRESSES) |
| 1476 | g_string_append_printf (str, "0x%04x │ ", (guint) i + 1); |
| 1477 | } |
| 1478 | } |
| 1479 | g_log (log_domain, G_LOG_LEVEL_DEBUG, "%s", str->str); |
| 1480 | } |
| 1481 | |
| 1482 | /** |
Richard Hughes | e59cb9a | 2018-12-05 14:37:40 +0000 | [diff] [blame] | 1483 | * fu_common_dump_raw: |
| 1484 | * @log_domain: log domain, typically %G_LOG_DOMAIN or %NULL |
| 1485 | * @title: prefix title, or %NULL |
| 1486 | * @data: buffer to print |
| 1487 | * @len: the size of @data |
| 1488 | * |
| 1489 | * Dumps a raw buffer to the screen. |
| 1490 | * |
| 1491 | * Since: 1.2.2 |
| 1492 | **/ |
| 1493 | void |
| 1494 | fu_common_dump_raw (const gchar *log_domain, |
| 1495 | const gchar *title, |
| 1496 | const guint8 *data, |
| 1497 | gsize len) |
| 1498 | { |
Richard Hughes | 3548186 | 2019-01-06 12:01:58 +0000 | [diff] [blame] | 1499 | FuDumpFlags flags = FU_DUMP_FLAGS_NONE; |
| 1500 | if (len > 64) |
| 1501 | flags |= FU_DUMP_FLAGS_SHOW_ADDRESSES; |
| 1502 | fu_common_dump_full (log_domain, title, data, len, 32, flags); |
Richard Hughes | e59cb9a | 2018-12-05 14:37:40 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /** |
Mario Limonciello | 3960265 | 2019-04-29 21:08:58 -0500 | [diff] [blame] | 1506 | * fu_common_dump_bytes: |
Richard Hughes | e59cb9a | 2018-12-05 14:37:40 +0000 | [diff] [blame] | 1507 | * @log_domain: log domain, typically %G_LOG_DOMAIN or %NULL |
| 1508 | * @title: prefix title, or %NULL |
| 1509 | * @bytes: a #GBytes |
| 1510 | * |
| 1511 | * Dumps a byte buffer to the screen. |
| 1512 | * |
| 1513 | * Since: 1.2.2 |
| 1514 | **/ |
| 1515 | void |
| 1516 | fu_common_dump_bytes (const gchar *log_domain, |
| 1517 | const gchar *title, |
| 1518 | GBytes *bytes) |
| 1519 | { |
| 1520 | gsize len = 0; |
| 1521 | const guint8 *data = g_bytes_get_data (bytes, &len); |
| 1522 | fu_common_dump_raw (log_domain, title, data, len); |
| 1523 | } |
Richard Hughes | fc90f39 | 2019-01-15 21:21:16 +0000 | [diff] [blame] | 1524 | |
| 1525 | /** |
| 1526 | * fu_common_bytes_align: |
| 1527 | * @bytes: a #GBytes |
| 1528 | * @blksz: block size in bytes |
| 1529 | * @padval: the byte used to pad the byte buffer |
| 1530 | * |
| 1531 | * Aligns a block of memory to @blksize using the @padval value; if |
| 1532 | * the block is already aligned then the original @bytes is returned. |
| 1533 | * |
| 1534 | * Returns: (transfer full): a #GBytes, possibly @bytes |
| 1535 | * |
| 1536 | * Since: 1.2.4 |
| 1537 | **/ |
| 1538 | GBytes * |
| 1539 | fu_common_bytes_align (GBytes *bytes, gsize blksz, gchar padval) |
| 1540 | { |
| 1541 | const guint8 *data; |
| 1542 | gsize sz; |
| 1543 | |
| 1544 | g_return_val_if_fail (bytes != NULL, NULL); |
| 1545 | g_return_val_if_fail (blksz > 0, NULL); |
| 1546 | |
| 1547 | /* pad */ |
| 1548 | data = g_bytes_get_data (bytes, &sz); |
| 1549 | if (sz % blksz != 0) { |
| 1550 | gsize sz_align = ((sz / blksz) + 1) * blksz; |
| 1551 | guint8 *data_align = g_malloc (sz_align); |
| 1552 | memcpy (data_align, data, sz); |
| 1553 | memset (data_align + sz, padval, sz_align - sz); |
| 1554 | g_debug ("aligning 0x%x bytes to 0x%x", |
| 1555 | (guint) sz, (guint) sz_align); |
| 1556 | return g_bytes_new_take (data_align, sz_align); |
| 1557 | } |
| 1558 | |
| 1559 | /* perfectly aligned */ |
| 1560 | return g_bytes_ref (bytes); |
| 1561 | } |
Richard Hughes | 3699946 | 2019-03-19 20:23:29 +0000 | [diff] [blame] | 1562 | |
| 1563 | /** |
| 1564 | * fu_common_bytes_is_empty: |
| 1565 | * @bytes: a #GBytes |
| 1566 | * |
| 1567 | * Checks if a byte array are just empty (0xff) bytes. |
| 1568 | * |
| 1569 | * Return value: %TRUE if @bytes is empty |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1570 | * |
| 1571 | * Since: 1.2.6 |
Richard Hughes | 3699946 | 2019-03-19 20:23:29 +0000 | [diff] [blame] | 1572 | **/ |
| 1573 | gboolean |
| 1574 | fu_common_bytes_is_empty (GBytes *bytes) |
| 1575 | { |
| 1576 | gsize sz = 0; |
| 1577 | const guint8 *buf = g_bytes_get_data (bytes, &sz); |
| 1578 | for (gsize i = 0; i < sz; i++) { |
| 1579 | if (buf[i] != 0xff) |
| 1580 | return FALSE; |
| 1581 | } |
| 1582 | return TRUE; |
| 1583 | } |
Richard Hughes | 2aad104 | 2019-03-21 09:03:32 +0000 | [diff] [blame] | 1584 | |
| 1585 | /** |
Richard Hughes | 38245ff | 2019-09-18 14:46:09 +0100 | [diff] [blame] | 1586 | * fu_common_bytes_compare_raw: |
| 1587 | * @buf1: a buffer |
| 1588 | * @bufsz1: sizeof @buf1 |
| 1589 | * @buf2: another buffer |
| 1590 | * @bufsz2: sizeof @buf2 |
Richard Hughes | 2aad104 | 2019-03-21 09:03:32 +0000 | [diff] [blame] | 1591 | * @error: A #GError or %NULL |
| 1592 | * |
Richard Hughes | 38245ff | 2019-09-18 14:46:09 +0100 | [diff] [blame] | 1593 | * Compares the buffers for equality. |
Richard Hughes | 2aad104 | 2019-03-21 09:03:32 +0000 | [diff] [blame] | 1594 | * |
Richard Hughes | 38245ff | 2019-09-18 14:46:09 +0100 | [diff] [blame] | 1595 | * Return value: %TRUE if @buf1 and @buf2 are identical |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1596 | * |
| 1597 | * Since: 1.3.2 |
Richard Hughes | 2aad104 | 2019-03-21 09:03:32 +0000 | [diff] [blame] | 1598 | **/ |
| 1599 | gboolean |
Richard Hughes | 38245ff | 2019-09-18 14:46:09 +0100 | [diff] [blame] | 1600 | fu_common_bytes_compare_raw (const guint8 *buf1, gsize bufsz1, |
| 1601 | const guint8 *buf2, gsize bufsz2, |
| 1602 | GError **error) |
Richard Hughes | 2aad104 | 2019-03-21 09:03:32 +0000 | [diff] [blame] | 1603 | { |
Richard Hughes | 38245ff | 2019-09-18 14:46:09 +0100 | [diff] [blame] | 1604 | g_return_val_if_fail (buf1 != NULL, FALSE); |
| 1605 | g_return_val_if_fail (buf2 != NULL, FALSE); |
Richard Hughes | 2aad104 | 2019-03-21 09:03:32 +0000 | [diff] [blame] | 1606 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 1607 | |
| 1608 | /* not the same length */ |
Richard Hughes | 2aad104 | 2019-03-21 09:03:32 +0000 | [diff] [blame] | 1609 | if (bufsz1 != bufsz2) { |
| 1610 | g_set_error (error, |
| 1611 | G_IO_ERROR, |
| 1612 | G_IO_ERROR_INVALID_DATA, |
| 1613 | "got %" G_GSIZE_FORMAT " bytes, expected " |
| 1614 | "%" G_GSIZE_FORMAT, bufsz1, bufsz2); |
| 1615 | return FALSE; |
| 1616 | } |
| 1617 | |
| 1618 | /* check matches */ |
| 1619 | for (guint i = 0x0; i < bufsz1; i++) { |
| 1620 | if (buf1[i] != buf2[i]) { |
| 1621 | g_set_error (error, |
| 1622 | G_IO_ERROR, |
| 1623 | G_IO_ERROR_INVALID_DATA, |
| 1624 | "got 0x%02x, expected 0x%02x @ 0x%04x", |
| 1625 | buf1[i], buf2[i], i); |
| 1626 | return FALSE; |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | /* success */ |
| 1631 | return TRUE; |
| 1632 | } |
Richard Hughes | 484ee29 | 2019-03-22 16:10:50 +0000 | [diff] [blame] | 1633 | |
| 1634 | /** |
Richard Hughes | 38245ff | 2019-09-18 14:46:09 +0100 | [diff] [blame] | 1635 | * fu_common_bytes_compare: |
| 1636 | * @bytes1: a #GBytes |
| 1637 | * @bytes2: another #GBytes |
| 1638 | * @error: A #GError or %NULL |
| 1639 | * |
| 1640 | * Compares the buffers for equality. |
| 1641 | * |
| 1642 | * Return value: %TRUE if @bytes1 and @bytes2 are identical |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1643 | * |
| 1644 | * Since: 1.2.6 |
Richard Hughes | 38245ff | 2019-09-18 14:46:09 +0100 | [diff] [blame] | 1645 | **/ |
| 1646 | gboolean |
| 1647 | fu_common_bytes_compare (GBytes *bytes1, GBytes *bytes2, GError **error) |
| 1648 | { |
| 1649 | const guint8 *buf1; |
| 1650 | const guint8 *buf2; |
| 1651 | gsize bufsz1; |
| 1652 | gsize bufsz2; |
| 1653 | |
| 1654 | g_return_val_if_fail (bytes1 != NULL, FALSE); |
| 1655 | g_return_val_if_fail (bytes2 != NULL, FALSE); |
| 1656 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 1657 | |
| 1658 | buf1 = g_bytes_get_data (bytes1, &bufsz1); |
| 1659 | buf2 = g_bytes_get_data (bytes2, &bufsz2); |
| 1660 | return fu_common_bytes_compare_raw (buf1, bufsz1, buf2, bufsz2, error); |
| 1661 | } |
| 1662 | |
| 1663 | /** |
Richard Hughes | 7afd7cb | 2019-08-07 11:42:42 +0100 | [diff] [blame] | 1664 | * fu_common_bytes_pad: |
| 1665 | * @bytes: a #GBytes |
| 1666 | * @sz: the desired size in bytes |
| 1667 | * |
Richard Hughes | 2ad99bb | 2021-03-03 10:19:13 +0000 | [diff] [blame^] | 1668 | * Pads a GBytes to a minimum @sz with `0xff`. |
Richard Hughes | 7afd7cb | 2019-08-07 11:42:42 +0100 | [diff] [blame] | 1669 | * |
| 1670 | * Return value: (transfer full): a #GBytes |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1671 | * |
| 1672 | * Since: 1.3.1 |
Richard Hughes | 7afd7cb | 2019-08-07 11:42:42 +0100 | [diff] [blame] | 1673 | **/ |
| 1674 | GBytes * |
| 1675 | fu_common_bytes_pad (GBytes *bytes, gsize sz) |
| 1676 | { |
| 1677 | gsize bytes_sz; |
| 1678 | |
Richard Hughes | 2ad99bb | 2021-03-03 10:19:13 +0000 | [diff] [blame^] | 1679 | g_return_val_if_fail (bytes != NULL, NULL); |
| 1680 | g_return_val_if_fail (sz != 0, NULL); |
Richard Hughes | 7afd7cb | 2019-08-07 11:42:42 +0100 | [diff] [blame] | 1681 | |
| 1682 | /* pad */ |
| 1683 | bytes_sz = g_bytes_get_size (bytes); |
| 1684 | if (bytes_sz < sz) { |
| 1685 | const guint8 *data = g_bytes_get_data (bytes, NULL); |
| 1686 | guint8 *data_new = g_malloc (sz); |
| 1687 | memcpy (data_new, data, bytes_sz); |
| 1688 | memset (data_new + bytes_sz, 0xff, sz - bytes_sz); |
| 1689 | return g_bytes_new_take (data_new, sz); |
| 1690 | } |
| 1691 | |
Richard Hughes | 2ad99bb | 2021-03-03 10:19:13 +0000 | [diff] [blame^] | 1692 | /* not required */ |
Richard Hughes | 7afd7cb | 2019-08-07 11:42:42 +0100 | [diff] [blame] | 1693 | return g_bytes_ref (bytes); |
| 1694 | } |
| 1695 | |
| 1696 | /** |
Richard Hughes | 05e3377 | 2020-12-08 18:37:02 +0000 | [diff] [blame] | 1697 | * fu_common_bytes_new_offset: |
| 1698 | * @bytes: a #GBytes |
| 1699 | * @offset: where subsection starts at |
| 1700 | * @length: length of subsection |
| 1701 | * @error: A #GError or %NULL |
| 1702 | * |
| 1703 | * Creates a #GBytes which is a subsection of another #GBytes. |
| 1704 | * |
| 1705 | * Return value: (transfer full): a #GBytes, or #NULL if range is invalid |
| 1706 | * |
| 1707 | * Since: 1.5.4 |
| 1708 | **/ |
| 1709 | GBytes * |
| 1710 | fu_common_bytes_new_offset (GBytes *bytes, |
| 1711 | gsize offset, |
| 1712 | gsize length, |
| 1713 | GError **error) |
| 1714 | { |
| 1715 | g_return_val_if_fail (bytes != NULL, NULL); |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 1716 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
Richard Hughes | 05e3377 | 2020-12-08 18:37:02 +0000 | [diff] [blame] | 1717 | |
| 1718 | /* sanity check */ |
| 1719 | if (offset + length > g_bytes_get_size (bytes)) { |
| 1720 | g_set_error (error, |
| 1721 | G_IO_ERROR, |
| 1722 | G_IO_ERROR_INVALID_DATA, |
| 1723 | "cannot create bytes @0x%02x for 0x%02x " |
| 1724 | "as buffer only 0x%04x bytes in size", |
| 1725 | (guint) offset, |
| 1726 | (guint) length, |
| 1727 | (guint) g_bytes_get_size (bytes)); |
| 1728 | return NULL; |
| 1729 | } |
| 1730 | return g_bytes_new_from_bytes (bytes, offset, length); |
| 1731 | } |
| 1732 | |
| 1733 | /** |
Richard Hughes | 484ee29 | 2019-03-22 16:10:50 +0000 | [diff] [blame] | 1734 | * fu_common_realpath: |
| 1735 | * @filename: a filename |
| 1736 | * @error: A #GError or %NULL |
| 1737 | * |
| 1738 | * Finds the canonicalized absolute filename for a path. |
| 1739 | * |
| 1740 | * Return value: A filename, or %NULL if invalid or not found |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1741 | * |
| 1742 | * Since: 1.2.6 |
Richard Hughes | 484ee29 | 2019-03-22 16:10:50 +0000 | [diff] [blame] | 1743 | **/ |
| 1744 | gchar * |
| 1745 | fu_common_realpath (const gchar *filename, GError **error) |
| 1746 | { |
| 1747 | char full_tmp[PATH_MAX]; |
| 1748 | |
| 1749 | g_return_val_if_fail (filename != NULL, NULL); |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 1750 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
Richard Hughes | 484ee29 | 2019-03-22 16:10:50 +0000 | [diff] [blame] | 1751 | |
Richard Hughes | 8694dee | 2019-11-22 09:16:34 +0000 | [diff] [blame] | 1752 | #ifdef HAVE_REALPATH |
Richard Hughes | 484ee29 | 2019-03-22 16:10:50 +0000 | [diff] [blame] | 1753 | if (realpath (filename, full_tmp) == NULL) { |
Richard Hughes | 8694dee | 2019-11-22 09:16:34 +0000 | [diff] [blame] | 1754 | #else |
| 1755 | if (_fullpath (full_tmp, filename, sizeof(full_tmp)) == NULL) { |
| 1756 | #endif |
Richard Hughes | 484ee29 | 2019-03-22 16:10:50 +0000 | [diff] [blame] | 1757 | g_set_error (error, |
| 1758 | G_IO_ERROR, |
| 1759 | G_IO_ERROR_INVALID_DATA, |
| 1760 | "cannot resolve path: %s", |
| 1761 | strerror (errno)); |
| 1762 | return NULL; |
| 1763 | } |
Richard Hughes | 8694dee | 2019-11-22 09:16:34 +0000 | [diff] [blame] | 1764 | if (!g_file_test (full_tmp, G_FILE_TEST_EXISTS)) { |
| 1765 | g_set_error (error, |
| 1766 | G_IO_ERROR, |
| 1767 | G_IO_ERROR_INVALID_DATA, |
| 1768 | "cannot find path: %s", |
| 1769 | full_tmp); |
| 1770 | return NULL; |
| 1771 | } |
Richard Hughes | 484ee29 | 2019-03-22 16:10:50 +0000 | [diff] [blame] | 1772 | return g_strdup (full_tmp); |
| 1773 | } |
Richard Hughes | 7afd7cb | 2019-08-07 11:42:42 +0100 | [diff] [blame] | 1774 | |
| 1775 | /** |
Richard Hughes | 5c508de | 2019-11-22 09:57:34 +0000 | [diff] [blame] | 1776 | * fu_common_fnmatch: |
| 1777 | * @pattern: a glob pattern, e.g. `*foo*` |
| 1778 | * @str: a string to match against the pattern, e.g. `bazfoobar` |
| 1779 | * |
| 1780 | * Matches a string against a glob pattern. |
| 1781 | * |
| 1782 | * Return value: %TRUE if the string matched |
| 1783 | * |
| 1784 | * Since: 1.3.5 |
| 1785 | **/ |
| 1786 | gboolean |
| 1787 | fu_common_fnmatch (const gchar *pattern, const gchar *str) |
| 1788 | { |
| 1789 | g_return_val_if_fail (pattern != NULL, FALSE); |
| 1790 | g_return_val_if_fail (str != NULL, FALSE); |
| 1791 | #ifdef HAVE_FNMATCH_H |
| 1792 | return fnmatch (pattern, str, FNM_NOESCAPE) == 0; |
Richard Hughes | 45a0073 | 2019-11-22 16:57:14 +0000 | [diff] [blame] | 1793 | #elif _WIN32 |
| 1794 | g_return_val_if_fail (strlen (pattern) < MAX_PATH, FALSE); |
| 1795 | g_return_val_if_fail (strlen (str) < MAX_PATH, FALSE); |
| 1796 | return PathMatchSpecA (str, pattern); |
Richard Hughes | 5c508de | 2019-11-22 09:57:34 +0000 | [diff] [blame] | 1797 | #else |
| 1798 | return g_strcmp0 (pattern, str) == 0; |
| 1799 | #endif |
| 1800 | } |
| 1801 | |
Richard Hughes | a84d7a7 | 2020-05-06 12:11:51 +0100 | [diff] [blame] | 1802 | static gint |
| 1803 | fu_common_filename_glob_sort_cb (gconstpointer a, gconstpointer b) |
| 1804 | { |
| 1805 | return g_strcmp0 (*(const gchar **)a, *(const gchar **)b); |
| 1806 | } |
| 1807 | |
| 1808 | /** |
| 1809 | * fu_common_filename_glob: |
| 1810 | * @directory: a directory path |
| 1811 | * @pattern: a glob pattern, e.g. `*foo*` |
| 1812 | * @error: A #GError or %NULL |
| 1813 | * |
| 1814 | * Returns all the filenames that match a specific glob pattern. |
| 1815 | * Any results are sorted. No matching files will set @error. |
| 1816 | * |
| 1817 | * Return value: (element-type utf8) (transfer container): matching files, or %NULL |
| 1818 | * |
| 1819 | * Since: 1.5.0 |
| 1820 | **/ |
| 1821 | GPtrArray * |
| 1822 | fu_common_filename_glob (const gchar *directory, const gchar *pattern, GError **error) |
| 1823 | { |
| 1824 | const gchar *basename; |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 1825 | g_autoptr(GDir) dir = NULL; |
Richard Hughes | a84d7a7 | 2020-05-06 12:11:51 +0100 | [diff] [blame] | 1826 | g_autoptr(GPtrArray) files = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 1827 | |
| 1828 | g_return_val_if_fail (directory != NULL, NULL); |
| 1829 | g_return_val_if_fail (pattern != NULL, NULL); |
| 1830 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 1831 | |
| 1832 | dir = g_dir_open (directory, 0, error); |
Richard Hughes | a84d7a7 | 2020-05-06 12:11:51 +0100 | [diff] [blame] | 1833 | if (dir == NULL) |
| 1834 | return NULL; |
| 1835 | while ((basename = g_dir_read_name (dir)) != NULL) { |
| 1836 | if (!fu_common_fnmatch (pattern, basename)) |
| 1837 | continue; |
| 1838 | g_ptr_array_add (files, g_build_filename (directory, basename, NULL)); |
| 1839 | } |
| 1840 | if (files->len == 0) { |
| 1841 | g_set_error_literal (error, |
| 1842 | G_IO_ERROR, |
| 1843 | G_IO_ERROR_NOT_FOUND, |
| 1844 | "no files matched pattern"); |
| 1845 | return NULL; |
| 1846 | } |
| 1847 | g_ptr_array_sort (files, fu_common_filename_glob_sort_cb); |
| 1848 | return g_steal_pointer (&files); |
| 1849 | } |
| 1850 | |
Richard Hughes | 5c508de | 2019-11-22 09:57:34 +0000 | [diff] [blame] | 1851 | /** |
Richard Hughes | 7afd7cb | 2019-08-07 11:42:42 +0100 | [diff] [blame] | 1852 | * fu_common_strnsplit: |
| 1853 | * @str: a string to split |
| 1854 | * @sz: size of @str |
| 1855 | * @delimiter: a string which specifies the places at which to split the string |
| 1856 | * @max_tokens: the maximum number of pieces to split @str into |
| 1857 | * |
| 1858 | * Splits a string into a maximum of @max_tokens pieces, using the given |
| 1859 | * delimiter. If @max_tokens is reached, the remainder of string is appended |
| 1860 | * to the last token. |
| 1861 | * |
Richard Hughes | a0d81c7 | 2019-11-27 11:41:54 +0000 | [diff] [blame] | 1862 | * Return value: (transfer full): a newly-allocated NULL-terminated array of strings |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1863 | * |
| 1864 | * Since: 1.3.1 |
Richard Hughes | 7afd7cb | 2019-08-07 11:42:42 +0100 | [diff] [blame] | 1865 | **/ |
| 1866 | gchar ** |
| 1867 | fu_common_strnsplit (const gchar *str, gsize sz, |
| 1868 | const gchar *delimiter, gint max_tokens) |
| 1869 | { |
| 1870 | if (str[sz - 1] != '\0') { |
| 1871 | g_autofree gchar *str2 = g_strndup (str, sz); |
| 1872 | return g_strsplit (str2, delimiter, max_tokens); |
| 1873 | } |
| 1874 | return g_strsplit (str, delimiter, max_tokens); |
| 1875 | } |
Richard Hughes | 5308ea4 | 2019-08-09 12:25:13 +0100 | [diff] [blame] | 1876 | |
| 1877 | /** |
Richard Hughes | 364e268 | 2021-01-05 14:38:15 +0000 | [diff] [blame] | 1878 | * fu_common_strsafe: |
| 1879 | * @str: (nullable): a string to make safe for printing |
| 1880 | * @maxsz: maximum size of returned string |
| 1881 | * |
| 1882 | * Converts a string into something that can be safely printed. |
| 1883 | * |
| 1884 | * Return value: (transfer full): safe string, or %NULL if there was nothing valid |
| 1885 | * |
| 1886 | * Since: 1.5.5 |
| 1887 | **/ |
| 1888 | gchar * |
| 1889 | fu_common_strsafe (const gchar *str, gsize maxsz) |
| 1890 | { |
| 1891 | gboolean valid = FALSE; |
| 1892 | g_autoptr(GString) tmp = NULL; |
| 1893 | |
Richard Hughes | 364e268 | 2021-01-05 14:38:15 +0000 | [diff] [blame] | 1894 | /* sanity check */ |
Richard Hughes | c123bee | 2021-02-08 16:47:05 +0000 | [diff] [blame] | 1895 | if (str == NULL || maxsz == 0) |
Richard Hughes | 364e268 | 2021-01-05 14:38:15 +0000 | [diff] [blame] | 1896 | return NULL; |
| 1897 | |
| 1898 | /* replace non-printable chars with '.' */ |
Richard Hughes | c123bee | 2021-02-08 16:47:05 +0000 | [diff] [blame] | 1899 | tmp = g_string_sized_new (maxsz); |
Richard Hughes | a4e0de4 | 2021-02-14 20:33:47 +0000 | [diff] [blame] | 1900 | for (gsize i = 0; i < maxsz && str[i] != '\0'; i++) { |
Richard Hughes | 364e268 | 2021-01-05 14:38:15 +0000 | [diff] [blame] | 1901 | if (!g_ascii_isprint (str[i])) { |
| 1902 | g_string_append_c (tmp, '.'); |
| 1903 | continue; |
| 1904 | } |
| 1905 | g_string_append_c (tmp, str[i]); |
Richard Hughes | ffbb117 | 2021-02-14 20:34:31 +0000 | [diff] [blame] | 1906 | if (!g_ascii_isspace (str[i])) |
| 1907 | valid = TRUE; |
Richard Hughes | 364e268 | 2021-01-05 14:38:15 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | /* if just junk, don't return 'all dots' */ |
| 1911 | if (tmp->len == 0 || !valid) |
| 1912 | return NULL; |
| 1913 | return g_string_free (g_steal_pointer (&tmp), FALSE); |
| 1914 | } |
| 1915 | |
Richard Hughes | e52e1b4 | 2021-01-26 09:59:15 +0000 | [diff] [blame] | 1916 | |
| 1917 | /** |
| 1918 | * fu_common_strjoin_array: |
| 1919 | * @separator: (nullable): string to insert between each of the strings, or %NULL |
| 1920 | * @array: (element-type utf8): A #GPtrArray |
| 1921 | * |
| 1922 | * Joins an array of strings together to form one long string, with the optional |
| 1923 | * separator inserted between each of them. |
| 1924 | * |
| 1925 | * If @array has no items, the return value will be an empty string. |
| 1926 | * If @array contains a single item, separator will not appear in the resulting |
| 1927 | * string. |
| 1928 | * |
| 1929 | * Returns: a string |
| 1930 | * |
| 1931 | * Since: 1.5.6 |
| 1932 | **/ |
| 1933 | gchar * |
| 1934 | fu_common_strjoin_array (const gchar *separator, GPtrArray *array) |
| 1935 | { |
| 1936 | g_autofree const gchar **strv = NULL; |
| 1937 | |
| 1938 | g_return_val_if_fail (array != NULL, NULL); |
| 1939 | |
| 1940 | strv = g_new0 (const gchar *, array->len + 1); |
| 1941 | for (guint i = 0; i < array->len; i++) |
| 1942 | strv[i] = g_ptr_array_index (array, i); |
| 1943 | return g_strjoinv (separator, (gchar **) strv); |
| 1944 | } |
| 1945 | |
Richard Hughes | 364e268 | 2021-01-05 14:38:15 +0000 | [diff] [blame] | 1946 | /** |
Richard Hughes | 5308ea4 | 2019-08-09 12:25:13 +0100 | [diff] [blame] | 1947 | * fu_memcpy_safe: |
| 1948 | * @dst: destination buffer |
| 1949 | * @dst_sz: maximum size of @dst, typically `sizeof(dst)` |
| 1950 | * @dst_offset: offset in bytes into @dst to copy to |
| 1951 | * @src: source buffer |
| 1952 | * @src_sz: maximum size of @dst, typically `sizeof(src)` |
| 1953 | * @src_offset: offset in bytes into @src to copy from |
| 1954 | * @n: number of bytes to copy from @src+@offset from |
| 1955 | * @error: A #GError or %NULL |
| 1956 | * |
| 1957 | * Copies some memory using memcpy in a safe way. Providing the buffer sizes |
| 1958 | * of both the destination and the source allows us to check for buffer overflow. |
| 1959 | * |
| 1960 | * Providing the buffer offsets also allows us to check reading past the end of |
| 1961 | * the source buffer. For this reason the caller should NEVER add an offset to |
| 1962 | * @src or @dst. |
| 1963 | * |
| 1964 | * You don't need to use this function in "obviously correct" cases, nor should |
| 1965 | * you use it when performance is a concern. Only us it when you're not sure if |
| 1966 | * malicious data from a device or firmware could cause memory corruption. |
| 1967 | * |
| 1968 | * Return value: %TRUE if the bytes were copied, %FALSE otherwise |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 1969 | * |
| 1970 | * Since: 1.3.1 |
Richard Hughes | 5308ea4 | 2019-08-09 12:25:13 +0100 | [diff] [blame] | 1971 | **/ |
| 1972 | gboolean |
| 1973 | fu_memcpy_safe (guint8 *dst, gsize dst_sz, gsize dst_offset, |
| 1974 | const guint8 *src, gsize src_sz, gsize src_offset, |
| 1975 | gsize n, GError **error) |
| 1976 | { |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 1977 | g_return_val_if_fail (dst != NULL, FALSE); |
| 1978 | g_return_val_if_fail (src != NULL, FALSE); |
| 1979 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 1980 | |
Richard Hughes | 5308ea4 | 2019-08-09 12:25:13 +0100 | [diff] [blame] | 1981 | if (n == 0) |
| 1982 | return TRUE; |
| 1983 | |
| 1984 | if (n > src_sz) { |
| 1985 | g_set_error (error, |
| 1986 | FWUPD_ERROR, |
| 1987 | FWUPD_ERROR_READ, |
| 1988 | "attempted to read 0x%02x bytes from buffer of 0x%02x", |
| 1989 | (guint) n, (guint) src_sz); |
| 1990 | return FALSE; |
| 1991 | } |
| 1992 | if (n + src_offset > src_sz) { |
| 1993 | g_set_error (error, |
| 1994 | FWUPD_ERROR, |
| 1995 | FWUPD_ERROR_READ, |
| 1996 | "attempted to read 0x%02x bytes at offset 0x%02x from buffer of 0x%02x", |
| 1997 | (guint) n, (guint) src_offset, (guint) src_sz); |
| 1998 | return FALSE; |
| 1999 | } |
| 2000 | if (n > dst_sz) { |
| 2001 | g_set_error (error, |
| 2002 | FWUPD_ERROR, |
| 2003 | FWUPD_ERROR_WRITE, |
| 2004 | "attempted to write 0x%02x bytes to buffer of 0x%02x", |
| 2005 | (guint) n, (guint) dst_sz); |
| 2006 | return FALSE; |
| 2007 | } |
| 2008 | if (n + dst_offset > dst_sz) { |
| 2009 | g_set_error (error, |
| 2010 | FWUPD_ERROR, |
| 2011 | FWUPD_ERROR_WRITE, |
| 2012 | "attempted to write 0x%02x bytes at offset 0x%02x to buffer of 0x%02x", |
| 2013 | (guint) n, (guint) dst_offset, (guint) dst_sz); |
| 2014 | return FALSE; |
| 2015 | } |
| 2016 | |
| 2017 | /* phew! */ |
| 2018 | memcpy (dst + dst_offset, src + src_offset, n); |
| 2019 | return TRUE; |
| 2020 | } |
Richard Hughes | 37c6a7b | 2019-08-14 21:57:43 +0100 | [diff] [blame] | 2021 | |
Richard Hughes | 80768f5 | 2019-10-22 07:19:14 +0100 | [diff] [blame] | 2022 | /** |
Richard Hughes | 9b11af9 | 2021-02-04 20:28:49 +0000 | [diff] [blame] | 2023 | * fu_memdup_safe: |
| 2024 | * @src: source buffer |
| 2025 | * @n: number of bytes to copy from @src |
| 2026 | * @error: A #GError or %NULL |
| 2027 | * |
| 2028 | * Duplicates some memory using memdup in a safe way. |
| 2029 | * |
| 2030 | * You don't need to use this function in "obviously correct" cases, nor should |
| 2031 | * you use it when performance is a concern. Only us it when you're not sure if |
| 2032 | * malicious data from a device or firmware could cause memory corruption. |
| 2033 | * |
| 2034 | * NOTE: This function intentionally limits allocation size to 1GB. |
| 2035 | * |
| 2036 | * Return value: (transfer full): block of allocated memory, or %NULL for an error. |
| 2037 | * |
| 2038 | * Since: 1.5.6 |
| 2039 | **/ |
| 2040 | guint8 * |
| 2041 | fu_memdup_safe (const guint8 *src, gsize n, GError **error) |
| 2042 | { |
Richard Hughes | 9b11af9 | 2021-02-04 20:28:49 +0000 | [diff] [blame] | 2043 | /* sanity check */ |
| 2044 | if (n > 0x40000000) { |
| 2045 | g_set_error (error, |
| 2046 | G_IO_ERROR, |
| 2047 | G_IO_ERROR_NOT_SUPPORTED, |
| 2048 | "cannot allocate %uGB of memory", |
| 2049 | (guint) (n / 0x40000000)); |
| 2050 | return NULL; |
| 2051 | } |
| 2052 | |
Richard Hughes | 5294d0c | 2021-02-05 09:50:02 +0000 | [diff] [blame] | 2053 | #if GLIB_CHECK_VERSION(2,67,3) |
Richard Hughes | 9b11af9 | 2021-02-04 20:28:49 +0000 | [diff] [blame] | 2054 | /* linear block of memory */ |
Richard Hughes | 5294d0c | 2021-02-05 09:50:02 +0000 | [diff] [blame] | 2055 | return g_memdup2 (src, n); |
| 2056 | #else |
| 2057 | return g_memdup (src, (guint) n); |
| 2058 | #endif |
Richard Hughes | 9b11af9 | 2021-02-04 20:28:49 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /** |
Richard Hughes | c21a0b9 | 2019-10-24 12:24:37 +0100 | [diff] [blame] | 2062 | * fu_common_read_uint8_safe: |
| 2063 | * @buf: source buffer |
| 2064 | * @bufsz: maximum size of @buf, typically `sizeof(buf)` |
| 2065 | * @offset: offset in bytes into @buf to copy from |
| 2066 | * @value: (out) (allow-none): the parsed value |
| 2067 | * @error: A #GError or %NULL |
| 2068 | * |
| 2069 | * Read a value from a buffer in a safe way. |
| 2070 | * |
| 2071 | * You don't need to use this function in "obviously correct" cases, nor should |
| 2072 | * you use it when performance is a concern. Only us it when you're not sure if |
| 2073 | * malicious data from a device or firmware could cause memory corruption. |
| 2074 | * |
| 2075 | * Return value: %TRUE if @value was set, %FALSE otherwise |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 2076 | * |
| 2077 | * Since: 1.3.3 |
Richard Hughes | c21a0b9 | 2019-10-24 12:24:37 +0100 | [diff] [blame] | 2078 | **/ |
| 2079 | gboolean |
| 2080 | fu_common_read_uint8_safe (const guint8 *buf, |
| 2081 | gsize bufsz, |
| 2082 | gsize offset, |
| 2083 | guint8 *value, |
| 2084 | GError **error) |
| 2085 | { |
| 2086 | guint8 tmp; |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2087 | |
| 2088 | g_return_val_if_fail (buf != NULL, FALSE); |
| 2089 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 2090 | |
Richard Hughes | c21a0b9 | 2019-10-24 12:24:37 +0100 | [diff] [blame] | 2091 | if (!fu_memcpy_safe (&tmp, sizeof(tmp), 0x0, /* dst */ |
| 2092 | buf, bufsz, offset, /* src */ |
| 2093 | sizeof(tmp), error)) |
| 2094 | return FALSE; |
| 2095 | if (value != NULL) |
| 2096 | *value = tmp; |
| 2097 | return TRUE; |
| 2098 | } |
| 2099 | |
| 2100 | /** |
Richard Hughes | 80768f5 | 2019-10-22 07:19:14 +0100 | [diff] [blame] | 2101 | * fu_common_read_uint16_safe: |
| 2102 | * @buf: source buffer |
| 2103 | * @bufsz: maximum size of @buf, typically `sizeof(buf)` |
| 2104 | * @offset: offset in bytes into @buf to copy from |
| 2105 | * @value: (out) (allow-none): the parsed value |
| 2106 | * @endian: A #FuEndianType, e.g. %G_LITTLE_ENDIAN |
| 2107 | * @error: A #GError or %NULL |
| 2108 | * |
| 2109 | * Read a value from a buffer using a specified endian in a safe way. |
| 2110 | * |
| 2111 | * You don't need to use this function in "obviously correct" cases, nor should |
| 2112 | * you use it when performance is a concern. Only us it when you're not sure if |
| 2113 | * malicious data from a device or firmware could cause memory corruption. |
| 2114 | * |
| 2115 | * Return value: %TRUE if @value was set, %FALSE otherwise |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 2116 | * |
| 2117 | * Since: 1.3.3 |
Richard Hughes | 80768f5 | 2019-10-22 07:19:14 +0100 | [diff] [blame] | 2118 | **/ |
| 2119 | gboolean |
| 2120 | fu_common_read_uint16_safe (const guint8 *buf, |
| 2121 | gsize bufsz, |
| 2122 | gsize offset, |
| 2123 | guint16 *value, |
| 2124 | FuEndianType endian, |
| 2125 | GError **error) |
| 2126 | { |
| 2127 | guint8 dst[2] = { 0x0 }; |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2128 | |
| 2129 | g_return_val_if_fail (buf != NULL, FALSE); |
| 2130 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 2131 | |
Richard Hughes | 7d01ac9 | 2019-10-23 14:31:46 +0100 | [diff] [blame] | 2132 | if (!fu_memcpy_safe (dst, sizeof(dst), 0x0, /* dst */ |
Richard Hughes | 80768f5 | 2019-10-22 07:19:14 +0100 | [diff] [blame] | 2133 | buf, bufsz, offset, /* src */ |
Richard Hughes | 7d01ac9 | 2019-10-23 14:31:46 +0100 | [diff] [blame] | 2134 | sizeof(dst), error)) |
Richard Hughes | 80768f5 | 2019-10-22 07:19:14 +0100 | [diff] [blame] | 2135 | return FALSE; |
| 2136 | if (value != NULL) |
| 2137 | *value = fu_common_read_uint16 (dst, endian); |
| 2138 | return TRUE; |
| 2139 | } |
| 2140 | |
| 2141 | /** |
| 2142 | * fu_common_read_uint32_safe: |
| 2143 | * @buf: source buffer |
| 2144 | * @bufsz: maximum size of @buf, typically `sizeof(buf)` |
| 2145 | * @offset: offset in bytes into @buf to copy from |
| 2146 | * @value: (out) (allow-none): the parsed value |
| 2147 | * @endian: A #FuEndianType, e.g. %G_LITTLE_ENDIAN |
| 2148 | * @error: A #GError or %NULL |
| 2149 | * |
| 2150 | * Read a value from a buffer using a specified endian in a safe way. |
| 2151 | * |
| 2152 | * You don't need to use this function in "obviously correct" cases, nor should |
| 2153 | * you use it when performance is a concern. Only us it when you're not sure if |
| 2154 | * malicious data from a device or firmware could cause memory corruption. |
| 2155 | * |
| 2156 | * Return value: %TRUE if @value was set, %FALSE otherwise |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 2157 | * |
| 2158 | * Since: 1.3.3 |
Richard Hughes | 80768f5 | 2019-10-22 07:19:14 +0100 | [diff] [blame] | 2159 | **/ |
| 2160 | gboolean |
| 2161 | fu_common_read_uint32_safe (const guint8 *buf, |
| 2162 | gsize bufsz, |
| 2163 | gsize offset, |
| 2164 | guint32 *value, |
| 2165 | FuEndianType endian, |
| 2166 | GError **error) |
| 2167 | { |
| 2168 | guint8 dst[4] = { 0x0 }; |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2169 | |
| 2170 | g_return_val_if_fail (buf != NULL, FALSE); |
| 2171 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 2172 | |
Richard Hughes | 7d01ac9 | 2019-10-23 14:31:46 +0100 | [diff] [blame] | 2173 | if (!fu_memcpy_safe (dst, sizeof(dst), 0x0, /* dst */ |
Richard Hughes | 80768f5 | 2019-10-22 07:19:14 +0100 | [diff] [blame] | 2174 | buf, bufsz, offset, /* src */ |
Richard Hughes | 7d01ac9 | 2019-10-23 14:31:46 +0100 | [diff] [blame] | 2175 | sizeof(dst), error)) |
Richard Hughes | 80768f5 | 2019-10-22 07:19:14 +0100 | [diff] [blame] | 2176 | return FALSE; |
| 2177 | if (value != NULL) |
| 2178 | *value = fu_common_read_uint32 (dst, endian); |
| 2179 | return TRUE; |
| 2180 | } |
| 2181 | |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 2182 | /** |
| 2183 | * fu_byte_array_append_uint8: |
| 2184 | * @array: A #GByteArray |
| 2185 | * @data: #guint8 |
| 2186 | * |
| 2187 | * Adds a 8 bit integer to a byte array |
| 2188 | * |
| 2189 | * Since: 1.3.1 |
| 2190 | **/ |
Richard Hughes | 37c6a7b | 2019-08-14 21:57:43 +0100 | [diff] [blame] | 2191 | void |
| 2192 | fu_byte_array_append_uint8 (GByteArray *array, guint8 data) |
| 2193 | { |
| 2194 | g_byte_array_append (array, &data, sizeof(data)); |
| 2195 | } |
| 2196 | |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 2197 | /** |
| 2198 | * fu_byte_array_append_uint16: |
| 2199 | * @array: A #GByteArray |
| 2200 | * @data: #guint16 |
| 2201 | * @endian: #FuEndianType |
| 2202 | * |
| 2203 | * Adds a 16 bit integer to a byte array |
| 2204 | * |
| 2205 | * Since: 1.3.1 |
| 2206 | **/ |
Richard Hughes | 37c6a7b | 2019-08-14 21:57:43 +0100 | [diff] [blame] | 2207 | void |
| 2208 | fu_byte_array_append_uint16 (GByteArray *array, guint16 data, FuEndianType endian) |
| 2209 | { |
| 2210 | guint8 buf[2]; |
| 2211 | fu_common_write_uint16 (buf, data, endian); |
| 2212 | g_byte_array_append (array, buf, sizeof(buf)); |
| 2213 | } |
| 2214 | |
Mario Limonciello | 1a680f3 | 2019-11-25 19:44:53 -0600 | [diff] [blame] | 2215 | /** |
| 2216 | * fu_byte_array_append_uint32: |
| 2217 | * @array: A #GByteArray |
| 2218 | * @data: #guint32 |
| 2219 | * @endian: #FuEndianType |
| 2220 | * |
| 2221 | * Adds a 32 bit integer to a byte array |
| 2222 | * |
| 2223 | * Since: 1.3.1 |
| 2224 | **/ |
Richard Hughes | 37c6a7b | 2019-08-14 21:57:43 +0100 | [diff] [blame] | 2225 | void |
| 2226 | fu_byte_array_append_uint32 (GByteArray *array, guint32 data, FuEndianType endian) |
| 2227 | { |
| 2228 | guint8 buf[4]; |
| 2229 | fu_common_write_uint32 (buf, data, endian); |
| 2230 | g_byte_array_append (array, buf, sizeof(buf)); |
| 2231 | } |
Mario Limonciello | 9dce1f7 | 2020-02-04 09:12:52 -0600 | [diff] [blame] | 2232 | |
| 2233 | /** |
Richard Hughes | a2a8f8e | 2020-10-20 09:27:26 +0100 | [diff] [blame] | 2234 | * fu_byte_array_set_size: |
| 2235 | * @array: a #GByteArray |
| 2236 | * @length: the new size of the GByteArray |
| 2237 | * |
| 2238 | * Sets the size of the GByteArray, expanding it with NULs if necessary. |
| 2239 | * |
| 2240 | * Since: 1.5.0 |
| 2241 | **/ |
| 2242 | void |
| 2243 | fu_byte_array_set_size (GByteArray *array, guint length) |
| 2244 | { |
| 2245 | guint oldlength = array->len; |
| 2246 | g_byte_array_set_size (array, length); |
| 2247 | if (length > oldlength) |
| 2248 | memset (array->data + oldlength, 0x0, length - oldlength); |
| 2249 | } |
| 2250 | |
| 2251 | /** |
Mario Limonciello | 9dce1f7 | 2020-02-04 09:12:52 -0600 | [diff] [blame] | 2252 | * fu_common_kernel_locked_down: |
| 2253 | * |
| 2254 | * Determines if kernel lockdown in effect |
| 2255 | * |
| 2256 | * Since: 1.3.8 |
| 2257 | **/ |
| 2258 | gboolean |
| 2259 | fu_common_kernel_locked_down (void) |
| 2260 | { |
| 2261 | #ifndef _WIN32 |
| 2262 | gsize len = 0; |
| 2263 | g_autofree gchar *dir = fu_common_get_path (FU_PATH_KIND_SYSFSDIR_SECURITY); |
| 2264 | g_autofree gchar *fname = g_build_filename (dir, "lockdown", NULL); |
| 2265 | g_autofree gchar *data = NULL; |
| 2266 | g_auto(GStrv) options = NULL; |
| 2267 | |
| 2268 | if (!g_file_test (fname, G_FILE_TEST_EXISTS)) |
| 2269 | return FALSE; |
| 2270 | if (!g_file_get_contents (fname, &data, &len, NULL)) |
| 2271 | return FALSE; |
| 2272 | if (len < 1) |
| 2273 | return FALSE; |
| 2274 | options = g_strsplit (data, " ", -1); |
| 2275 | for (guint i = 0; options[i] != NULL; i++) { |
| 2276 | if (g_strcmp0 (options[i], "[none]") == 0) |
| 2277 | return FALSE; |
| 2278 | } |
| 2279 | return TRUE; |
| 2280 | #else |
| 2281 | return FALSE; |
| 2282 | #endif |
| 2283 | } |
Richard Hughes | 9223c89 | 2020-05-09 20:32:08 +0100 | [diff] [blame] | 2284 | |
| 2285 | /** |
Richard Hughes | bd1dc2a | 2020-08-20 15:35:28 +0100 | [diff] [blame] | 2286 | * fu_common_cpuid: |
| 2287 | * @leaf: The CPUID level, now called the 'leaf' by Intel |
| 2288 | * @eax: (out) (nullable): EAX register |
| 2289 | * @ebx: (out) (nullable): EBX register |
| 2290 | * @ecx: (out) (nullable): ECX register |
| 2291 | * @edx: (out) (nullable): EDX register |
| 2292 | * @error: A #GError or NULL |
| 2293 | * |
| 2294 | * Calls CPUID and returns the registers for the given leaf. |
| 2295 | * |
| 2296 | * Return value: %TRUE if the registers are set. |
| 2297 | * |
| 2298 | * Since: 1.5.0 |
| 2299 | **/ |
| 2300 | gboolean |
| 2301 | fu_common_cpuid (guint32 leaf, |
| 2302 | guint32 *eax, |
| 2303 | guint32 *ebx, |
| 2304 | guint32 *ecx, |
| 2305 | guint32 *edx, |
| 2306 | GError **error) |
| 2307 | { |
| 2308 | #ifdef HAVE_CPUID_H |
| 2309 | guint eax_tmp = 0; |
| 2310 | guint ebx_tmp = 0; |
| 2311 | guint ecx_tmp = 0; |
| 2312 | guint edx_tmp = 0; |
| 2313 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2314 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 2315 | |
Richard Hughes | bd1dc2a | 2020-08-20 15:35:28 +0100 | [diff] [blame] | 2316 | /* get vendor */ |
Richard Hughes | 7c4a64b | 2020-08-23 21:20:58 +0100 | [diff] [blame] | 2317 | __get_cpuid_count (leaf, 0x0, &eax_tmp, &ebx_tmp, &ecx_tmp, &edx_tmp); |
Richard Hughes | bd1dc2a | 2020-08-20 15:35:28 +0100 | [diff] [blame] | 2318 | if (eax != NULL) |
| 2319 | *eax = eax_tmp; |
| 2320 | if (ebx != NULL) |
| 2321 | *ebx = ebx_tmp; |
| 2322 | if (ecx != NULL) |
| 2323 | *ecx = ecx_tmp; |
| 2324 | if (edx != NULL) |
| 2325 | *edx = edx_tmp; |
| 2326 | return TRUE; |
| 2327 | #else |
| 2328 | g_set_error_literal (error, |
| 2329 | G_IO_ERROR, |
| 2330 | G_IO_ERROR_NOT_SUPPORTED, |
| 2331 | "no <cpuid.h> support"); |
| 2332 | return FALSE; |
| 2333 | #endif |
| 2334 | } |
| 2335 | |
| 2336 | /** |
Richard Hughes | 9223c89 | 2020-05-09 20:32:08 +0100 | [diff] [blame] | 2337 | * fu_common_is_cpu_intel: |
| 2338 | * |
| 2339 | * Uses CPUID to discover the CPU vendor and check if it is Intel. |
| 2340 | * |
| 2341 | * Return value: %TRUE if the vendor was Intel. |
Richard Hughes | b63cfa9 | 2021-01-05 22:57:12 +0000 | [diff] [blame] | 2342 | * Deprecated: 1.5.5: Use fu_common_get_cpu_vendor() instead. |
Richard Hughes | 9223c89 | 2020-05-09 20:32:08 +0100 | [diff] [blame] | 2343 | * |
| 2344 | * Since: 1.5.0 |
| 2345 | **/ |
| 2346 | gboolean |
| 2347 | fu_common_is_cpu_intel (void) |
| 2348 | { |
Richard Hughes | b63cfa9 | 2021-01-05 22:57:12 +0000 | [diff] [blame] | 2349 | return fu_common_get_cpu_vendor () == FU_CPU_VENDOR_INTEL; |
| 2350 | } |
| 2351 | |
| 2352 | /** |
| 2353 | * fu_common_get_cpu_vendor: |
| 2354 | * |
| 2355 | * Uses CPUID to discover the CPU vendor. |
| 2356 | * |
| 2357 | * Return value: a #FuCpuVendor, e.g. %FU_CPU_VENDOR_AMD if the vendor was AMD. |
| 2358 | * |
| 2359 | * Since: 1.5.5 |
| 2360 | **/ |
| 2361 | FuCpuVendor |
| 2362 | fu_common_get_cpu_vendor (void) |
| 2363 | { |
| 2364 | #ifdef HAVE_CPUID_H |
Richard Hughes | 9223c89 | 2020-05-09 20:32:08 +0100 | [diff] [blame] | 2365 | guint ebx = 0; |
| 2366 | guint ecx = 0; |
| 2367 | guint edx = 0; |
Richard Hughes | 9223c89 | 2020-05-09 20:32:08 +0100 | [diff] [blame] | 2368 | |
Richard Hughes | b63cfa9 | 2021-01-05 22:57:12 +0000 | [diff] [blame] | 2369 | if (fu_common_cpuid (0x0, NULL, &ebx, &ecx, &edx, NULL)) { |
| 2370 | if (ebx == signature_INTEL_ebx && |
| 2371 | edx == signature_INTEL_edx && |
| 2372 | ecx == signature_INTEL_ecx) { |
| 2373 | return FU_CPU_VENDOR_INTEL; |
| 2374 | } |
| 2375 | if (ebx == signature_AMD_ebx && |
| 2376 | edx == signature_AMD_edx && |
| 2377 | ecx == signature_AMD_ecx) { |
| 2378 | return FU_CPU_VENDOR_AMD; |
| 2379 | } |
Richard Hughes | 9223c89 | 2020-05-09 20:32:08 +0100 | [diff] [blame] | 2380 | } |
Richard Hughes | bd44432 | 2020-05-21 12:05:03 +0100 | [diff] [blame] | 2381 | #endif |
Richard Hughes | b63cfa9 | 2021-01-05 22:57:12 +0000 | [diff] [blame] | 2382 | |
| 2383 | /* failed */ |
| 2384 | return FU_CPU_VENDOR_UNKNOWN; |
Richard Hughes | 9223c89 | 2020-05-09 20:32:08 +0100 | [diff] [blame] | 2385 | } |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2386 | |
Richard Hughes | 3611147 | 2020-08-12 15:04:24 +0100 | [diff] [blame] | 2387 | /** |
| 2388 | * fu_common_is_live_media: |
| 2389 | * |
| 2390 | * Checks if the user is running from a live media using various heuristics. |
| 2391 | * |
| 2392 | * Returns: %TRUE if live |
| 2393 | * |
| 2394 | * Since: 1.4.6 |
| 2395 | **/ |
| 2396 | gboolean |
| 2397 | fu_common_is_live_media (void) |
| 2398 | { |
| 2399 | gsize bufsz = 0; |
| 2400 | g_autofree gchar *buf = NULL; |
| 2401 | g_auto(GStrv) tokens = NULL; |
| 2402 | const gchar *args[] = { |
| 2403 | "rd.live.image", |
| 2404 | "boot=live", |
| 2405 | NULL, /* last entry */ |
| 2406 | }; |
| 2407 | if (g_file_test ("/cdrom/.disk/info", G_FILE_TEST_EXISTS)) |
| 2408 | return TRUE; |
| 2409 | if (!g_file_get_contents ("/proc/cmdline", &buf, &bufsz, NULL)) |
| 2410 | return FALSE; |
| 2411 | if (bufsz == 0) |
| 2412 | return FALSE; |
| 2413 | tokens = fu_common_strnsplit (buf, bufsz - 1, " ", -1); |
| 2414 | for (guint i = 0; args[i] != NULL; i++) { |
| 2415 | if (g_strv_contains ((const gchar * const *) tokens, args[i])) |
| 2416 | return TRUE; |
| 2417 | } |
| 2418 | return FALSE; |
| 2419 | } |
| 2420 | |
Richard Hughes | 68175e9 | 2021-01-14 09:43:33 +0000 | [diff] [blame] | 2421 | /** |
| 2422 | * fu_common_get_memory_size: |
| 2423 | * |
| 2424 | * Returns the size of physical memory. |
| 2425 | * |
| 2426 | * Returns: bytes |
| 2427 | * |
| 2428 | * Since: 1.5.6 |
| 2429 | **/ |
| 2430 | guint64 |
| 2431 | fu_common_get_memory_size (void) |
| 2432 | { |
| 2433 | #ifdef _WIN32 |
| 2434 | MEMORYSTATUSEX status; |
| 2435 | status.dwLength = sizeof(status); |
| 2436 | GlobalMemoryStatusEx (&status); |
| 2437 | return (guint64) status.ullTotalPhys; |
| 2438 | #else |
| 2439 | return sysconf (_SC_PHYS_PAGES) * sysconf (_SC_PAGE_SIZE); |
| 2440 | #endif |
| 2441 | } |
| 2442 | |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2443 | static GPtrArray * |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2444 | fu_common_get_block_devices (GError **error) |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2445 | { |
| 2446 | GVariantBuilder builder; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2447 | const gchar *obj; |
| 2448 | g_autoptr(GVariant) output = NULL; |
| 2449 | g_autoptr(GDBusProxy) proxy = NULL; |
| 2450 | g_autoptr(GPtrArray) devices = NULL; |
| 2451 | g_autoptr(GVariantIter) iter = NULL; |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2452 | g_autoptr(GDBusConnection) connection = NULL; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2453 | |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2454 | connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error); |
| 2455 | if (connection == NULL) { |
| 2456 | g_prefix_error (error, "failed to get system bus: "); |
| 2457 | return NULL; |
| 2458 | } |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2459 | proxy = g_dbus_proxy_new_sync (connection, |
| 2460 | G_DBUS_PROXY_FLAGS_NONE, NULL, |
| 2461 | UDISKS_DBUS_SERVICE, |
| 2462 | UDISKS_DBUS_PATH, |
| 2463 | UDISKS_DBUS_MANAGER_INTERFACE, |
| 2464 | NULL, error); |
| 2465 | if (proxy == NULL) { |
| 2466 | g_prefix_error (error, "failed to find %s: ", UDISKS_DBUS_SERVICE); |
| 2467 | return NULL; |
| 2468 | } |
| 2469 | g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2470 | output = g_dbus_proxy_call_sync (proxy, |
Richard Hughes | db344d5 | 2020-09-09 19:42:27 +0100 | [diff] [blame] | 2471 | "GetBlockDevices", |
| 2472 | g_variant_new ("(a{sv})", &builder), |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2473 | G_DBUS_CALL_FLAGS_NONE, |
| 2474 | -1, NULL, error); |
Richard Hughes | 0bdf561 | 2020-10-30 14:56:22 +0000 | [diff] [blame] | 2475 | if (output == NULL) { |
| 2476 | if (error != NULL) |
| 2477 | g_dbus_error_strip_remote_error (*error); |
| 2478 | g_prefix_error (error, "failed to call %s.%s(): ", |
| 2479 | UDISKS_DBUS_SERVICE, |
| 2480 | "GetBlockDevices"); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2481 | return NULL; |
Richard Hughes | 0bdf561 | 2020-10-30 14:56:22 +0000 | [diff] [blame] | 2482 | } |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2483 | devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2484 | g_variant_get (output, "(ao)", &iter); |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2485 | while (g_variant_iter_next (iter, "&o", &obj)) { |
| 2486 | g_autoptr(GDBusProxy) proxy_blk = NULL; |
| 2487 | proxy_blk = g_dbus_proxy_new_sync (connection, |
| 2488 | G_DBUS_PROXY_FLAGS_NONE, NULL, |
| 2489 | UDISKS_DBUS_SERVICE, |
| 2490 | obj, |
| 2491 | UDISKS_DBUS_INTERFACE_BLOCK, |
| 2492 | NULL, error); |
| 2493 | if (proxy_blk == NULL) { |
| 2494 | g_prefix_error (error, "failed to initialize d-bus proxy for %s: ", obj); |
| 2495 | return NULL; |
| 2496 | } |
| 2497 | g_ptr_array_add (devices, g_steal_pointer (&proxy_blk)); |
| 2498 | } |
| 2499 | |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2500 | |
| 2501 | return g_steal_pointer (&devices); |
| 2502 | } |
| 2503 | |
Richard Hughes | ad3cfc0 | 2021-02-22 10:02:54 +0000 | [diff] [blame] | 2504 | static const gchar * |
| 2505 | fu_common_convert_to_gpt_type (const gchar *type) |
| 2506 | { |
| 2507 | struct { |
| 2508 | const gchar *mbr; |
| 2509 | const gchar *gpt; |
| 2510 | } typeguids[] = { |
| 2511 | { "0xef", "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" }, /* esp */ |
| 2512 | { "0x0b", "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7" }, /* fat32 */ |
| 2513 | { NULL, NULL } |
| 2514 | }; |
| 2515 | for (guint i = 0; typeguids[i].mbr != NULL; i++) { |
| 2516 | if (g_strcmp0 (type, typeguids[i].mbr) == 0) |
| 2517 | return typeguids[i].gpt; |
| 2518 | } |
| 2519 | return type; |
| 2520 | } |
| 2521 | |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2522 | /** |
| 2523 | * fu_common_get_volumes_by_kind: |
| 2524 | * @kind: A volume kind, typically a GUID |
| 2525 | * @error: A #GError or NULL |
| 2526 | * |
Richard Hughes | c57a8f5 | 2020-10-30 14:42:42 +0000 | [diff] [blame] | 2527 | * Finds all volumes of a specific partition type |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2528 | * |
| 2529 | * Returns: (transfer container) (element-type FuVolume): a #GPtrArray, or %NULL if the kind was not found |
| 2530 | * |
| 2531 | * Since: 1.4.6 |
| 2532 | **/ |
| 2533 | GPtrArray * |
| 2534 | fu_common_get_volumes_by_kind (const gchar *kind, GError **error) |
| 2535 | { |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2536 | g_autoptr(GPtrArray) devices = NULL; |
| 2537 | g_autoptr(GPtrArray) volumes = NULL; |
| 2538 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2539 | g_return_val_if_fail (kind != NULL, NULL); |
| 2540 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 2541 | |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2542 | devices = fu_common_get_block_devices (error); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2543 | if (devices == NULL) |
Richard Hughes | b81140d | 2020-08-17 14:47:17 +0100 | [diff] [blame] | 2544 | return NULL; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2545 | volumes = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 2546 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2547 | GDBusProxy *proxy_blk = g_ptr_array_index (devices, i); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2548 | const gchar *type_str; |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2549 | g_autoptr(FuVolume) vol = NULL; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2550 | g_autoptr(GDBusProxy) proxy_part = NULL; |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2551 | g_autoptr(GDBusProxy) proxy_fs = NULL; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2552 | g_autoptr(GVariant) val = NULL; |
| 2553 | |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2554 | proxy_part = g_dbus_proxy_new_sync (g_dbus_proxy_get_connection (proxy_blk), |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2555 | G_DBUS_PROXY_FLAGS_NONE, NULL, |
| 2556 | UDISKS_DBUS_SERVICE, |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2557 | g_dbus_proxy_get_object_path (proxy_blk), |
| 2558 | UDISKS_DBUS_INTERFACE_PARTITION, |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2559 | NULL, error); |
| 2560 | if (proxy_part == NULL) { |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2561 | g_prefix_error (error, "failed to initialize d-bus proxy %s: ", |
| 2562 | g_dbus_proxy_get_object_path (proxy_blk)); |
Richard Hughes | b81140d | 2020-08-17 14:47:17 +0100 | [diff] [blame] | 2563 | return NULL; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2564 | } |
| 2565 | val = g_dbus_proxy_get_cached_property (proxy_part, "Type"); |
| 2566 | if (val == NULL) |
| 2567 | continue; |
| 2568 | |
Richard Hughes | db344d5 | 2020-09-09 19:42:27 +0100 | [diff] [blame] | 2569 | g_variant_get (val, "&s", &type_str); |
Richard Hughes | 43417b2 | 2020-10-30 14:46:16 +0000 | [diff] [blame] | 2570 | proxy_fs = g_dbus_proxy_new_sync (g_dbus_proxy_get_connection (proxy_blk), |
| 2571 | G_DBUS_PROXY_FLAGS_NONE, NULL, |
| 2572 | UDISKS_DBUS_SERVICE, |
| 2573 | g_dbus_proxy_get_object_path (proxy_blk), |
| 2574 | UDISKS_DBUS_INTERFACE_FILESYSTEM, |
| 2575 | NULL, error); |
| 2576 | if (proxy_fs == NULL) { |
| 2577 | g_prefix_error (error, "failed to initialize d-bus proxy %s: ", |
| 2578 | g_dbus_proxy_get_object_path (proxy_blk)); |
Richard Hughes | b81140d | 2020-08-17 14:47:17 +0100 | [diff] [blame] | 2579 | return NULL; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2580 | } |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2581 | vol = g_object_new (FU_TYPE_VOLUME, |
| 2582 | "proxy-block", proxy_blk, |
| 2583 | "proxy-filesystem", proxy_fs, |
| 2584 | NULL); |
Richard Hughes | ad3cfc0 | 2021-02-22 10:02:54 +0000 | [diff] [blame] | 2585 | |
| 2586 | /* convert MBR type to GPT type */ |
| 2587 | type_str = fu_common_convert_to_gpt_type (type_str); |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2588 | g_debug ("device %s, type: %s, internal: %d, fs: %s", |
| 2589 | g_dbus_proxy_get_object_path (proxy_blk), type_str, |
| 2590 | fu_volume_is_internal (vol), |
| 2591 | fu_volume_get_id_type (vol)); |
| 2592 | if (g_strcmp0 (type_str, kind) != 0) |
| 2593 | continue; |
| 2594 | g_ptr_array_add (volumes, g_steal_pointer (&vol)); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2595 | } |
| 2596 | if (volumes->len == 0) { |
| 2597 | g_set_error (error, |
| 2598 | G_IO_ERROR, |
| 2599 | G_IO_ERROR_NOT_FOUND, |
| 2600 | "no volumes of type %s", kind); |
| 2601 | return NULL; |
| 2602 | } |
| 2603 | return g_steal_pointer (&volumes); |
| 2604 | } |
| 2605 | |
| 2606 | /** |
Richard Hughes | 0bdf561 | 2020-10-30 14:56:22 +0000 | [diff] [blame] | 2607 | * fu_common_get_volume_by_device: |
| 2608 | * @device: A device string, typcically starting with `/dev/` |
| 2609 | * @error: A #GError or NULL |
| 2610 | * |
| 2611 | * Finds the first volume from the specified device. |
| 2612 | * |
| 2613 | * Returns: (transfer full): a #GPtrArray, or %NULL if the kind was not found |
| 2614 | * |
| 2615 | * Since: 1.5.1 |
| 2616 | **/ |
| 2617 | FuVolume * |
| 2618 | fu_common_get_volume_by_device (const gchar *device, GError **error) |
| 2619 | { |
| 2620 | g_autoptr(GPtrArray) devices = NULL; |
| 2621 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2622 | g_return_val_if_fail (device != NULL, NULL); |
| 2623 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 2624 | |
Richard Hughes | 0bdf561 | 2020-10-30 14:56:22 +0000 | [diff] [blame] | 2625 | /* find matching block device */ |
| 2626 | devices = fu_common_get_block_devices (error); |
| 2627 | if (devices == NULL) |
| 2628 | return NULL; |
| 2629 | for (guint i = 0; i < devices->len; i++) { |
| 2630 | GDBusProxy *proxy_blk = g_ptr_array_index (devices, i); |
| 2631 | g_autoptr(GVariant) val = NULL; |
| 2632 | val = g_dbus_proxy_get_cached_property (proxy_blk, "Device"); |
| 2633 | if (val == NULL) |
| 2634 | continue; |
| 2635 | if (g_strcmp0 (g_variant_get_bytestring (val), device) == 0) { |
| 2636 | return g_object_new (FU_TYPE_VOLUME, |
| 2637 | "proxy-block", proxy_blk, |
| 2638 | NULL); |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | /* failed */ |
| 2643 | g_set_error (error, |
| 2644 | G_IO_ERROR, |
| 2645 | G_IO_ERROR_NOT_FOUND, |
| 2646 | "no volumes for device %s", |
| 2647 | device); |
| 2648 | return NULL; |
| 2649 | } |
| 2650 | |
| 2651 | /** |
| 2652 | * fu_common_get_volume_by_devnum: |
Richard Hughes | e0f9207 | 2020-11-06 09:50:29 +0000 | [diff] [blame] | 2653 | * @devnum: A device number |
Richard Hughes | 0bdf561 | 2020-10-30 14:56:22 +0000 | [diff] [blame] | 2654 | * @error: A #GError or NULL |
| 2655 | * |
| 2656 | * Finds the first volume from the specified device. |
| 2657 | * |
| 2658 | * Returns: (transfer full): a #GPtrArray, or %NULL if the kind was not found |
| 2659 | * |
| 2660 | * Since: 1.5.1 |
| 2661 | **/ |
| 2662 | FuVolume * |
| 2663 | fu_common_get_volume_by_devnum (guint32 devnum, GError **error) |
| 2664 | { |
| 2665 | g_autoptr(GPtrArray) devices = NULL; |
| 2666 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2667 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 2668 | |
Richard Hughes | 0bdf561 | 2020-10-30 14:56:22 +0000 | [diff] [blame] | 2669 | /* find matching block device */ |
| 2670 | devices = fu_common_get_block_devices (error); |
| 2671 | if (devices == NULL) |
| 2672 | return NULL; |
| 2673 | for (guint i = 0; i < devices->len; i++) { |
| 2674 | GDBusProxy *proxy_blk = g_ptr_array_index (devices, i); |
| 2675 | g_autoptr(GVariant) val = NULL; |
| 2676 | val = g_dbus_proxy_get_cached_property (proxy_blk, "DeviceNumber"); |
| 2677 | if (val == NULL) |
| 2678 | continue; |
| 2679 | if (devnum == g_variant_get_uint64 (val)) { |
| 2680 | return g_object_new (FU_TYPE_VOLUME, |
| 2681 | "proxy-block", proxy_blk, |
| 2682 | NULL); |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | /* failed */ |
| 2687 | g_set_error (error, |
| 2688 | G_IO_ERROR, |
| 2689 | G_IO_ERROR_NOT_FOUND, |
| 2690 | "no volumes for devnum %u", |
| 2691 | devnum); |
| 2692 | return NULL; |
| 2693 | } |
| 2694 | |
| 2695 | /** |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2696 | * fu_common_get_esp_default: |
| 2697 | * @error: A #GError or NULL |
| 2698 | * |
| 2699 | * Gets the platform default ESP |
| 2700 | * |
| 2701 | * Returns: (transfer full): a #FuVolume, or %NULL if the ESP was not found |
| 2702 | * |
| 2703 | * Since: 1.4.6 |
| 2704 | **/ |
| 2705 | FuVolume * |
| 2706 | fu_common_get_esp_default (GError **error) |
| 2707 | { |
| 2708 | const gchar *path_tmp; |
Richard Hughes | 9d20bf9 | 2020-12-14 09:36:46 +0000 | [diff] [blame] | 2709 | gboolean has_internal = FALSE; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2710 | g_autoptr(GPtrArray) volumes_fstab = g_ptr_array_new (); |
| 2711 | g_autoptr(GPtrArray) volumes_mtab = g_ptr_array_new (); |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2712 | g_autoptr(GPtrArray) volumes_vfat = g_ptr_array_new (); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2713 | g_autoptr(GPtrArray) volumes = NULL; |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2714 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2715 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2716 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 2717 | |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2718 | /* for the test suite use local directory for ESP */ |
| 2719 | path_tmp = g_getenv ("FWUPD_UEFI_ESP_PATH"); |
| 2720 | if (path_tmp != NULL) |
| 2721 | return fu_volume_new_from_mount_path (path_tmp); |
| 2722 | |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2723 | volumes = fu_common_get_volumes_by_kind (FU_VOLUME_KIND_ESP, &error_local); |
| 2724 | if (volumes == NULL) { |
| 2725 | g_debug ("%s, falling back to %s", error_local->message, FU_VOLUME_KIND_BDP); |
| 2726 | volumes = fu_common_get_volumes_by_kind (FU_VOLUME_KIND_BDP, error); |
| 2727 | if (volumes == NULL) { |
| 2728 | g_prefix_error (error, "%s: ", error_local->message); |
| 2729 | return NULL; |
| 2730 | } |
| 2731 | } |
Richard Hughes | 9d20bf9 | 2020-12-14 09:36:46 +0000 | [diff] [blame] | 2732 | |
| 2733 | /* are there _any_ internal vfat partitions? |
| 2734 | * remember HintSystem is just that -- a hint! */ |
| 2735 | for (guint i = 0; i < volumes->len; i++) { |
| 2736 | FuVolume *vol = g_ptr_array_index (volumes, i); |
| 2737 | g_autofree gchar *type = fu_volume_get_id_type (vol); |
| 2738 | if (g_strcmp0 (type, "vfat") == 0 && |
| 2739 | fu_volume_is_internal (vol)) { |
| 2740 | has_internal = TRUE; |
| 2741 | break; |
| 2742 | } |
| 2743 | } |
| 2744 | |
| 2745 | /* filter to vfat partitions */ |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2746 | for (guint i = 0; i < volumes->len; i++) { |
| 2747 | FuVolume *vol = g_ptr_array_index (volumes, i); |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2748 | g_autofree gchar *type = fu_volume_get_id_type (vol); |
| 2749 | if (type == NULL) |
| 2750 | continue; |
Richard Hughes | 9d20bf9 | 2020-12-14 09:36:46 +0000 | [diff] [blame] | 2751 | if (has_internal && !fu_volume_is_internal (vol)) |
Mario Limonciello | 56d816a | 2020-11-11 16:59:30 -0600 | [diff] [blame] | 2752 | continue; |
| 2753 | if (g_strcmp0 (type, "vfat") == 0) |
| 2754 | g_ptr_array_add (volumes_vfat, vol); |
| 2755 | } |
| 2756 | if (volumes_vfat->len == 0) { |
| 2757 | g_set_error (error, |
| 2758 | G_IO_ERROR, |
| 2759 | G_IO_ERROR_INVALID_FILENAME, |
| 2760 | "No ESP found"); |
| 2761 | return NULL; |
| 2762 | } |
| 2763 | for (guint i = 0; i < volumes_vfat->len; i++) { |
| 2764 | FuVolume *vol = g_ptr_array_index (volumes_vfat, i); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2765 | g_ptr_array_add (fu_volume_is_mounted (vol) ? volumes_mtab : volumes_fstab, vol); |
| 2766 | } |
| 2767 | if (volumes_mtab->len == 1) { |
| 2768 | FuVolume *vol = g_ptr_array_index (volumes_mtab, 0); |
| 2769 | return g_object_ref (vol); |
| 2770 | } |
| 2771 | if (volumes_mtab->len == 0 && volumes_fstab->len == 1) { |
| 2772 | FuVolume *vol = g_ptr_array_index (volumes_fstab, 0); |
| 2773 | return g_object_ref (vol); |
| 2774 | } |
| 2775 | g_set_error (error, |
| 2776 | G_IO_ERROR, |
| 2777 | G_IO_ERROR_INVALID_FILENAME, |
| 2778 | "More than one available ESP"); |
| 2779 | return NULL; |
| 2780 | } |
| 2781 | |
| 2782 | /** |
| 2783 | * fu_common_get_esp_for_path: |
| 2784 | * @esp_path: A path to the ESP |
| 2785 | * @error: A #GError or NULL |
| 2786 | * |
| 2787 | * Gets the platform ESP using a UNIX or UDisks path |
| 2788 | * |
| 2789 | * Returns: (transfer full): a #FuVolume, or %NULL if the ESP was not found |
| 2790 | * |
| 2791 | * Since: 1.4.6 |
| 2792 | **/ |
| 2793 | FuVolume * |
| 2794 | fu_common_get_esp_for_path (const gchar *esp_path, GError **error) |
| 2795 | { |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2796 | g_autofree gchar *basename = NULL; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2797 | g_autoptr(GPtrArray) volumes = NULL; |
Mario Limonciello | be220a4 | 2021-02-10 10:39:58 -0600 | [diff] [blame] | 2798 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2799 | |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2800 | g_return_val_if_fail (esp_path != NULL, NULL); |
| 2801 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 2802 | |
Mario Limonciello | be220a4 | 2021-02-10 10:39:58 -0600 | [diff] [blame] | 2803 | volumes = fu_common_get_volumes_by_kind (FU_VOLUME_KIND_ESP, &error_local); |
| 2804 | if (volumes == NULL) { |
| 2805 | /* check if it's a valid directory already */ |
| 2806 | if (g_file_test (esp_path, G_FILE_TEST_IS_DIR)) |
| 2807 | return fu_volume_new_from_mount_path (esp_path); |
| 2808 | g_propagate_error (error, g_steal_pointer (&error_local)); |
Richard Hughes | b81140d | 2020-08-17 14:47:17 +0100 | [diff] [blame] | 2809 | return NULL; |
Mario Limonciello | be220a4 | 2021-02-10 10:39:58 -0600 | [diff] [blame] | 2810 | } |
Richard Hughes | 6a489a9 | 2020-12-22 10:32:06 +0000 | [diff] [blame] | 2811 | basename = g_path_get_basename (esp_path); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2812 | for (guint i = 0; i < volumes->len; i++) { |
| 2813 | FuVolume *vol = g_ptr_array_index (volumes, i); |
Mario Limonciello | 5a83563 | 2020-10-07 14:22:08 -0500 | [diff] [blame] | 2814 | g_autofree gchar *vol_basename = g_path_get_basename (fu_volume_get_mount_point (vol)); |
Richard Hughes | 8f0b2d1 | 2020-08-12 12:41:53 +0100 | [diff] [blame] | 2815 | if (g_strcmp0 (basename, vol_basename) == 0) |
| 2816 | return g_object_ref (vol); |
| 2817 | } |
| 2818 | g_set_error (error, |
| 2819 | G_IO_ERROR, |
| 2820 | G_IO_ERROR_INVALID_FILENAME, |
| 2821 | "No ESP with path %s", |
| 2822 | esp_path); |
| 2823 | return NULL; |
| 2824 | } |
Richard Hughes | 6f5e35a | 2020-09-25 14:14:52 +0100 | [diff] [blame] | 2825 | |
| 2826 | /** |
Richard Hughes | 44ae2a7 | 2020-09-25 18:00:21 +0100 | [diff] [blame] | 2827 | * fu_common_crc8: |
| 2828 | * @buf: memory buffer |
| 2829 | * @bufsz: sizeof buf |
| 2830 | * |
| 2831 | * Returns the cyclic redundancy check value for the given memory buffer. |
| 2832 | * |
| 2833 | * Returns: CRC value |
| 2834 | * |
| 2835 | * Since: 1.5.0 |
| 2836 | **/ |
| 2837 | guint8 |
| 2838 | fu_common_crc8 (const guint8 *buf, gsize bufsz) |
| 2839 | { |
| 2840 | guint32 crc = 0; |
| 2841 | for (gsize j = bufsz; j > 0; j--) { |
| 2842 | crc ^= (*(buf++) << 8); |
| 2843 | for (guint32 i = 8; i; i--) { |
| 2844 | if (crc & 0x8000) |
| 2845 | crc ^= (0x1070 << 3); |
| 2846 | crc <<= 1; |
| 2847 | } |
| 2848 | } |
| 2849 | return ~((guint8) (crc >> 8)); |
| 2850 | } |
| 2851 | |
| 2852 | /** |
Richard Hughes | 6f5e35a | 2020-09-25 14:14:52 +0100 | [diff] [blame] | 2853 | * fu_common_crc16: |
| 2854 | * @buf: memory buffer |
| 2855 | * @bufsz: sizeof buf |
| 2856 | * |
| 2857 | * Returns the cyclic redundancy check value for the given memory buffer. |
| 2858 | * |
| 2859 | * Returns: CRC value |
| 2860 | * |
| 2861 | * Since: 1.5.0 |
| 2862 | **/ |
| 2863 | guint16 |
| 2864 | fu_common_crc16 (const guint8 *buf, gsize bufsz) |
| 2865 | { |
| 2866 | guint16 crc = 0xffff; |
| 2867 | for (gsize len = bufsz; len > 0; len--) { |
| 2868 | crc = (guint16) (crc ^ (*buf++)); |
| 2869 | for (guint8 i = 0; i < 8; i++) { |
| 2870 | if (crc & 0x1) { |
| 2871 | crc = (crc >> 1) ^ 0xa001; |
| 2872 | } else { |
| 2873 | crc >>= 1; |
| 2874 | } |
| 2875 | } |
| 2876 | } |
| 2877 | return ~crc; |
| 2878 | } |
| 2879 | |
| 2880 | /** |
| 2881 | * fu_common_crc32_full: |
| 2882 | * @buf: memory buffer |
| 2883 | * @bufsz: sizeof buf |
| 2884 | * @crc: initial CRC value, typically 0xFFFFFFFF |
| 2885 | * @polynomial: CRC polynomial, typically 0xEDB88320 |
| 2886 | * |
| 2887 | * Returns the cyclic redundancy check value for the given memory buffer. |
| 2888 | * |
| 2889 | * Returns: CRC value |
| 2890 | * |
| 2891 | * Since: 1.5.0 |
| 2892 | **/ |
| 2893 | guint32 |
| 2894 | fu_common_crc32_full (const guint8 *buf, gsize bufsz, guint32 crc, guint32 polynomial) |
| 2895 | { |
| 2896 | for (guint32 idx = 0; idx < bufsz; idx++) { |
| 2897 | guint8 data = *buf++; |
| 2898 | crc = crc ^ data; |
| 2899 | for (guint32 bit = 0; bit < 8; bit++) { |
| 2900 | guint32 mask = -(crc & 1); |
| 2901 | crc = (crc >> 1) ^ (polynomial & mask); |
| 2902 | } |
| 2903 | } |
| 2904 | return ~crc; |
| 2905 | } |
| 2906 | |
| 2907 | /** |
| 2908 | * fu_common_crc32: |
| 2909 | * @buf: memory buffer |
| 2910 | * @bufsz: sizeof buf |
| 2911 | * |
| 2912 | * Returns the cyclic redundancy check value for the given memory buffer. |
| 2913 | * |
| 2914 | * Returns: CRC value |
| 2915 | * |
| 2916 | * Since: 1.5.0 |
| 2917 | **/ |
| 2918 | guint32 |
| 2919 | fu_common_crc32 (const guint8 *buf, gsize bufsz) |
| 2920 | { |
| 2921 | return fu_common_crc32_full (buf, bufsz, 0xFFFFFFFF, 0xEDB88320); |
| 2922 | } |
Richard Hughes | c754667 | 2021-01-28 10:53:30 +0000 | [diff] [blame] | 2923 | |
| 2924 | /** |
| 2925 | * fu_common_uri_get_scheme: |
| 2926 | * @uri: valid URI, e.g. `https://foo.bar/baz` |
| 2927 | * |
| 2928 | * Returns the USI scheme for the given URI. |
| 2929 | * |
| 2930 | * Returns: scheme value, or %NULL if invalid, e.g. `https` |
| 2931 | * |
| 2932 | * Since: 1.5.6 |
| 2933 | **/ |
| 2934 | gchar * |
| 2935 | fu_common_uri_get_scheme (const gchar *uri) |
| 2936 | { |
| 2937 | gchar *tmp; |
| 2938 | |
| 2939 | g_return_val_if_fail (uri != NULL, NULL); |
| 2940 | |
| 2941 | tmp = g_strstr_len (uri, -1, ":"); |
| 2942 | if (tmp == NULL || tmp[0] == '\0') |
| 2943 | return NULL; |
| 2944 | return g_utf8_strdown (uri, tmp - uri); |
| 2945 | } |