1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/*
* Copyright 2017 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#define G_LOG_DOMAIN "FuCommon"
#include "config.h"
#ifdef HAVE_CPUID_H
#include <cpuid.h>
#endif
#include "fu-common-private.h"
#include "fu-firmware.h"
#include "fu-path.h"
#include "fu-string.h"
/**
* fu_size_checked_add:
* @a: The #gsize left operand
* @b: The #gsize right operand
*
* Performs a checked addition of a and b, ensuring the result does not overflow.
*
* Returns: @a+@b, or %G_MAXSIZE on overflow
*
* Since: 2.0.19
**/
gsize
fu_size_checked_add(gsize a, gsize b)
{
gsize tmp = 0;
if (!g_size_checked_add(&tmp, a, b))
return G_MAXSIZE;
return tmp;
}
/**
* fu_error_map_entry_to_gerror:
* @value: the value to look up
* @entries: the #FuErrorMapEntry map
* @n_entries: number of @entries
* @error: (nullable): optional return location for an error
*
* Sets the #GError from the integer value and the error map.
*
* Any entries with a error_code of `FWUPD_ERROR_LAST` will return success.
*
* Returns: boolean success
*
* Since: 2.0.13
**/
gboolean
fu_error_map_entry_to_gerror(guint value,
const FuErrorMapEntry entries[],
guint n_entries,
GError **error)
{
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
for (guint i = 0; i < n_entries; i++) {
const FuErrorMapEntry entry = entries[i];
if (entry.value != value)
continue;
if (entry.code == FWUPD_ERROR_LAST)
return TRUE;
g_set_error(error,
FWUPD_ERROR,
entry.code,
"%s [0x%x]",
entry.message != NULL ? entry.message
: fwupd_error_to_string(entry.value),
entry.value);
return FALSE;
}
g_set_error(error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "generic failure [0x%x]", value);
return FALSE;
}
/**
* fu_cpuid:
* @leaf: the CPUID level, now called the 'leaf' by Intel
* @eax: (out) (nullable): EAX register
* @ebx: (out) (nullable): EBX register
* @ecx: (out) (nullable): ECX register
* @edx: (out) (nullable): EDX register
* @error: (nullable): optional return location for an error
*
* Calls CPUID and returns the registers for the given leaf.
*
* Returns: %TRUE if the registers are set.
*
* Since: 1.8.2
**/
gboolean
fu_cpuid(guint32 leaf, guint32 *eax, guint32 *ebx, guint32 *ecx, guint32 *edx, GError **error)
{
#ifdef HAVE_CPUID_H
guint eax_tmp = 0;
guint ebx_tmp = 0;
guint ecx_tmp = 0;
guint edx_tmp = 0;
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* get vendor */
__get_cpuid_count(leaf, 0x0, &eax_tmp, &ebx_tmp, &ecx_tmp, &edx_tmp);
if (eax != NULL)
*eax = eax_tmp;
if (ebx != NULL)
*ebx = ebx_tmp;
if (ecx != NULL)
*ecx = ecx_tmp;
if (edx != NULL)
*edx = edx_tmp;
return TRUE;
#else
g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, "no <cpuid.h> support");
return FALSE;
#endif
}
/**
* fu_cpu_get_attrs:
* @error: (nullable): optional return location for an error
*
* Gets attributes for the first CPU listed in `/proc/cpuinfo`.
*
* Returns: (element-type utf8 utf8) (transfer full): CPU attributes
*
* Since: 2.0.7
**/
GHashTable *
fu_cpu_get_attrs(GError **error)
{
gsize bufsz = 0;
g_autofree gchar *buf = NULL;
g_autofree gchar *fn = fu_path_build(FU_PATH_KIND_PROCFS, "cpuinfo", NULL);
g_autoptr(GHashTable) hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
if (!g_file_get_contents(fn, &buf, &bufsz, error))
return NULL;
if (bufsz > 0) {
g_auto(GStrv) lines = fu_strsplit(buf, bufsz, "\n", -1);
for (guint i = 0; lines[i] != NULL; i++) {
g_auto(GStrv) tokens = NULL;
if (lines[i][0] == '\0')
break;
tokens = g_strsplit(lines[i], ": ", 2);
for (guint j = 0; tokens[j] != NULL; j++) {
g_hash_table_insert(hash,
fu_strstrip(tokens[0]),
g_strdup(tokens[1]));
}
}
}
/* success */
return g_steal_pointer(&hash);
}
/**
* fu_cpu_get_vendor:
*
* Uses CPUID to discover the CPU vendor.
*
* Returns: a CPU vendor, e.g. %FU_CPU_VENDOR_AMD if the vendor was AMD.
*
* Since: 1.8.2
**/
FuCpuVendor
fu_cpu_get_vendor(void)
{
#ifdef HAVE_CPUID_H
guint ebx = 0;
guint ecx = 0;
guint edx = 0;
if (fu_cpuid(0x0, NULL, &ebx, &ecx, &edx, NULL)) {
if (ebx == signature_INTEL_ebx && edx == signature_INTEL_edx &&
ecx == signature_INTEL_ecx) {
return FU_CPU_VENDOR_INTEL;
}
if (ebx == signature_AMD_ebx && edx == signature_AMD_edx &&
ecx == signature_AMD_ecx) {
return FU_CPU_VENDOR_AMD;
}
}
#endif
/* failed */
return FU_CPU_VENDOR_UNKNOWN;
}
/**
* fu_common_get_memory_size:
*
* Returns the size of physical memory.
*
* Returns: bytes
*
* Since: 1.5.6
**/
guint64
fu_common_get_memory_size(void)
{
return fu_common_get_memory_size_impl();
}
/**
* fu_common_get_kernel_cmdline:
* @error: (nullable): optional return location for an error
*
* Returns the current kernel command line options.
*
* Returns: options as a string, or %NULL on error
*
* Since: 1.5.6
**/
gchar *
fu_common_get_kernel_cmdline(GError **error)
{
return fu_common_get_kernel_cmdline_impl(error);
}
/**
* fu_common_get_olson_timezone_id:
* @error: (nullable): optional return location for an error
*
* Gets the system Olson timezone ID, as used in the CLDR and ICU specifications.
*
* Returns: timezone string, e.g. `Europe/London` or %NULL on error
*
* Since: 1.9.7
**/
gchar *
fu_common_get_olson_timezone_id(GError **error)
{
return fu_common_get_olson_timezone_id_impl(error);
}
/**
* fu_common_align_up:
* @value: value to align
* @alignment: align to this power of 2, where 0x1F is the maximum value of 2GB
*
* Align a value to a power of 2 boundary, where @alignment is the bit position
* to align to. If @alignment is zero then @value is always returned unchanged.
*
* Returns: aligned value, which will be the same as @value if already aligned,
* or %G_MAXSIZE if the value would overflow
*
* Since: 1.6.0
**/
gsize
fu_common_align_up(gsize value, guint8 alignment)
{
gsize value_new;
gsize mask = (gsize)1 << alignment;
g_return_val_if_fail(alignment <= FU_FIRMWARE_ALIGNMENT_2G, G_MAXSIZE);
/* no alignment required */
if ((value & (mask - 1)) == 0)
return value;
/* increment up to the next alignment value */
value_new = value + mask;
value_new &= ~(mask - 1);
/* overflow */
if (value_new < value)
return G_MAXSIZE;
/* success */
return value_new;
}
/**
* fu_power_state_is_ac:
* @power_state: a power state, e.g. %FU_POWER_STATE_AC_FULLY_CHARGED
*
* Determines if the power state can be considered "on AC power".
*
* Returns: %TRUE for success
*
* Since: 1.8.11
**/
gboolean
fu_power_state_is_ac(FuPowerState power_state)
{
return power_state != FU_POWER_STATE_BATTERY;
}
/**
* fu_error_convert:
* @entries: the #FuErrorConvertEntry map
* @n_entries: number of @entries
* @perror: (nullable): A #GError, perhaps with domain #GIOError
*
* Convert the error to a #FwupdError, if required.
*
* Since: 2.0.14
**/
gboolean
fu_error_convert(const FuErrorConvertEntry entries[], guint n_entries, GError **perror)
{
GError *error = (perror != NULL) ? *perror : NULL;
/* sanity check */
if (error == NULL)
return TRUE;
/* convert GIOError and GFileError */
fwupd_error_convert(perror);
if (error->domain == FWUPD_ERROR)
return FALSE;
for (guint i = 0; i < n_entries; i++) {
if (g_error_matches(error, entries[i].domain, entries[i].code)) {
error->domain = FWUPD_ERROR;
error->code = entries[i].error;
return FALSE;
}
}
#ifndef SUPPORTED_BUILD
/* fallback */
g_critical("GError %s:%i was not converted to FwupdError",
g_quark_to_string(error->domain),
error->code);
#endif
error->domain = FWUPD_ERROR;
error->code = FWUPD_ERROR_INTERNAL;
return FALSE;
}
/**
* fu_xmlb_builder_insert_kv:
* @bn: #XbBuilderNode
* @key: string key
* @value: string value
*
* Convenience function to add an XML node with a string value. If @value is %NULL
* then no member is added.
*
* Since: 1.6.0
**/
void
fu_xmlb_builder_insert_kv(XbBuilderNode *bn, const gchar *key, const gchar *value)
{
if (value == NULL)
return;
xb_builder_node_insert_text(bn, key, value, NULL);
}
/**
* fu_xmlb_builder_insert_kx:
* @bn: #XbBuilderNode
* @key: string key
* @value: integer value
*
* Convenience function to add an XML node with an integer value. If @value is 0
* then no member is added.
*
* Since: 1.6.0
**/
void
fu_xmlb_builder_insert_kx(XbBuilderNode *bn, const gchar *key, guint64 value)
{
g_autofree gchar *value_hex = NULL;
if (value == 0)
return;
value_hex = g_strdup_printf("0x%lx", (gulong)value);
xb_builder_node_insert_text(bn, key, value_hex, NULL);
}
/**
* fu_xmlb_builder_insert_kb:
* @bn: #XbBuilderNode
* @key: string key
* @value: boolean value
*
* Convenience function to add an XML node with a boolean value.
*
* Since: 1.6.0
**/
void
fu_xmlb_builder_insert_kb(XbBuilderNode *bn, const gchar *key, gboolean value)
{
xb_builder_node_insert_text(bn, key, value ? "true" : "false", NULL);
}
|