1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/*
* Copyright 2017 Richard Hughes <richard@hughsie.com>
* Copyright 2019 9elements Agency GmbH <patrick.rudolph@9elements.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <libflashrom.h>
#include <string.h>
#include "fu-flashrom-device.h"
#define SELFCHECK_TRUE 1
struct FuPluginData {
struct flashrom_flashctx *flashctx;
struct flashrom_programmer *flashprog;
gchar *guid; /* GUID from quirks that activated this plugin */
};
typedef FuPluginData FuFlashromPlugin;
#define FU_FLASHROM_PLUGIN(o) fu_plugin_get_data(FU_PLUGIN(o))
static void
fu_flashrom_plugin_to_string(FuPlugin *plugin, guint idt, GString *str)
{
FuFlashromPlugin *self = FU_FLASHROM_PLUGIN(plugin);
fwupd_codec_string_append(str, idt, "Guid", self->guid);
}
static int
fu_flashrom_plugin_debug_cb(enum flashrom_log_level lvl, const char *fmt, va_list args)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
g_autofree gchar *tmp = g_strdup_vprintf(fmt, args);
#pragma clang diagnostic pop
g_autofree gchar *str = fu_strstrip(tmp);
if (g_strcmp0(str, "OK.") == 0 || g_strcmp0(str, ".") == 0)
return 0;
switch (lvl) {
case FLASHROM_MSG_ERROR:
case FLASHROM_MSG_WARN:
g_warning("%s", str);
break;
case FLASHROM_MSG_INFO:
g_info("%s", str);
break;
case FLASHROM_MSG_DEBUG:
case FLASHROM_MSG_DEBUG2:
g_debug("%s", str);
break;
case FLASHROM_MSG_SPEW:
default:
break;
}
return 0;
}
static void
fu_flashrom_plugin_device_set_version(FuPlugin *plugin, FuDevice *device)
{
FuContext *ctx = fu_plugin_get_context(plugin);
const gchar *version;
const gchar *version_major;
const gchar *version_minor;
/* as-is */
version = fu_context_get_hwid_value(ctx, FU_HWIDS_KEY_BIOS_VERSION);
if (version != NULL) {
/* some Lenovo hardware requires a specific prefix for the EC,
* so strip it before we use ensure-semver */
if (strlen(version) > 9 && g_str_has_prefix(version, "CBET"))
version += 9;
/* this may not "stick" if there are no numeric chars */
fu_device_set_version(device, version);
if (fu_device_get_version(device) != NULL)
return;
}
/* component parts only */
version_major = fu_context_get_hwid_value(ctx, FU_HWIDS_KEY_BIOS_MAJOR_RELEASE);
version_minor = fu_context_get_hwid_value(ctx, FU_HWIDS_KEY_BIOS_MINOR_RELEASE);
if (version_major != NULL && version_minor != NULL) {
g_autofree gchar *tmp = g_strdup_printf("%s.%s.0", version_major, version_minor);
fu_device_set_version(device, tmp);
return;
}
}
static gboolean
fu_flashrom_plugin_device_set_bios_info(FuPlugin *plugin, FuDevice *device, GError **error)
{
FuContext *ctx = fu_plugin_get_context(plugin);
GBytes *bios_blob;
const guint8 *buf;
gsize bufsz;
guint32 bios_char = 0x0;
g_autoptr(GPtrArray) bios_tables = NULL;
/* get SMBIOS info */
bios_tables = fu_context_get_smbios_data(ctx,
FU_SMBIOS_STRUCTURE_TYPE_BIOS,
FU_SMBIOS_STRUCTURE_LENGTH_ANY,
error);
if (bios_tables == NULL)
return FALSE;
/* get SMBIOS data */
bios_blob = g_ptr_array_index(bios_tables, 0);
buf = g_bytes_get_data(bios_blob, &bufsz);
if (bufsz == 0)
return FALSE;
/* BIOS characteristics */
if (fu_memread_uint32_safe(buf, bufsz, 0xa, &bios_char, G_LITTLE_ENDIAN, NULL)) {
if ((bios_char & (1 << 11)) == 0) {
fu_device_inhibit(device,
"bios-characteristics",
"Not supported from SMBIOS");
}
}
/* ROM size if not already been quirked */
if (fu_device_get_firmware_size_max(device) == 0) {
guint8 bios_sz = 0x0;
guint64 firmware_size = 0x0;
if (!fu_memread_uint8_safe(buf, bufsz, 0x9, &bios_sz, NULL))
return FALSE;
/* need to read extended ROM size */
if (bios_sz == 0xff) {
guint16 bios_sz_ext = 0x0;
/* Bits 15-14 scale, 13-0 size
* 00 scale -> MiB
* 01 scale -> GiB
* others reserved
*/
if (!fu_memread_uint16_safe(buf,
bufsz,
0x18,
&bios_sz_ext,
G_LITTLE_ENDIAN,
error))
return FALSE;
firmware_size = (bios_sz_ext & 0x3ff) * (1024 * 1024);
if (bios_sz_ext & 0xc000)
firmware_size *= 1024;
} else {
firmware_size = (bios_sz + 1) * 64 * 1024;
}
fu_device_set_firmware_size_max(device, firmware_size);
}
return TRUE;
}
static void
fu_flashrom_plugin_device_set_hwids(FuPlugin *plugin, FuDevice *device)
{
FuContext *ctx = fu_plugin_get_context(plugin);
static const gchar *chids[] = {
"HardwareID-03",
"HardwareID-04",
"HardwareID-05",
"HardwareID-06",
"HardwareID-10",
"fwupd-04", /* for coreboot */
"fwupd-05", /* for coreboot */
};
/* don't include FU_HWIDS_KEY_BIOS_VERSION */
for (guint i = 0; i < G_N_ELEMENTS(chids); i++) {
g_autofree gchar *str = NULL;
str = fu_context_get_hwid_replace_value(ctx, chids[i], NULL);
if (str != NULL)
fu_device_add_instance_id(device, str);
}
}
static FuDevice *
fu_flashrom_plugin_add_device(FuPlugin *plugin,
const gchar *guid,
FuIfdRegion region,
GError **error)
{
FuContext *ctx = fu_plugin_get_context(plugin);
FuFlashromPlugin *self = FU_FLASHROM_PLUGIN(plugin);
const gchar *dmi_vendor;
const gchar *product = fu_context_get_hwid_value(ctx, FU_HWIDS_KEY_PRODUCT_NAME);
const gchar *vendor = fu_context_get_hwid_value(ctx, FU_HWIDS_KEY_MANUFACTURER);
const gchar *region_str = fu_ifd_region_to_string(region);
g_autofree gchar *name = g_strdup_printf("%s (%s)", product, region_str);
g_autoptr(FuDevice) device = fu_flashrom_device_new(ctx, self->flashctx, region);
g_autoptr(GError) error_local = NULL;
fu_device_set_name(device, name);
fu_device_set_vendor(device, vendor);
fu_device_add_instance_str(device, "VENDOR", vendor);
fu_device_add_instance_str(device, "PRODUCT", product);
fu_device_add_instance_strup(device, "REGION", region_str);
if (!fu_device_build_instance_id(device,
error,
"FLASHROM",
"VENDOR",
"PRODUCT",
"REGION",
NULL))
return NULL;
/* add this so we can attach board-specific quirks */
fu_device_add_instance_str(FU_DEVICE(device), "GUID", guid);
if (!fu_device_build_instance_id(FU_DEVICE(device), error, "FLASHROM", "GUID", NULL))
return NULL;
/* use same VendorID logic as with UEFI */
dmi_vendor = fu_context_get_hwid_value(ctx, FU_HWIDS_KEY_BIOS_VENDOR);
fu_device_build_vendor_id(FU_DEVICE(device), "DMI", dmi_vendor);
fu_flashrom_plugin_device_set_hwids(plugin, device);
fu_flashrom_plugin_device_set_version(plugin, device);
if (!fu_flashrom_plugin_device_set_bios_info(plugin, device, &error_local))
g_debug("failed to set bios info: %s", error_local->message);
if (!fu_device_setup(device, error))
return NULL;
/* BCR is almost-never used on coreboot because SMI is evil */
if (g_strcmp0(dmi_vendor, "coreboot") == 0 &&
fu_device_get_metadata(device, "PciBcrAddr") == NULL)
fu_device_set_metadata_integer(device, "PciBcrAddr", 0x0);
/* success */
fu_plugin_device_add(plugin, device);
return g_steal_pointer(&device);
}
static void
fu_flashrom_plugin_device_registered(FuPlugin *plugin, FuDevice *device)
{
g_autoptr(FuDevice) me_device = NULL;
FuFlashromPlugin *self = FU_FLASHROM_PLUGIN(plugin);
const gchar *me_region_str = fu_ifd_region_to_string(FU_IFD_REGION_ME);
/* we're only interested in a device from intel-spi plugin that corresponds to ME
* region of IFD */
if (g_strcmp0(fu_device_get_plugin(device), "intel_spi") != 0)
return;
if (g_strcmp0(fu_device_get_logical_id(device), me_region_str) != 0)
return;
me_device = fu_flashrom_plugin_add_device(plugin, self->guid, FU_IFD_REGION_ME, NULL);
if (me_device == NULL)
return;
/* unlock operation requires device to be locked */
if (fu_device_has_flag(device, FWUPD_DEVICE_FLAG_LOCKED))
fu_device_add_flag(me_device, FWUPD_DEVICE_FLAG_LOCKED);
}
static gboolean
fu_flashrom_plugin_coldplug(FuPlugin *plugin, FuProgress *progress, GError **error)
{
FuFlashromPlugin *self = FU_FLASHROM_PLUGIN(plugin);
g_autoptr(FuDevice) device =
fu_flashrom_plugin_add_device(plugin, self->guid, FU_IFD_REGION_BIOS, error);
return (device != NULL);
}
/* finds GUID that activated this plugin */
static const gchar *
fu_flashrom_plugin_find_guid(FuPlugin *plugin, GError **error)
{
FuContext *ctx = fu_plugin_get_context(plugin);
GPtrArray *hwids = fu_context_get_hwid_guids(ctx);
/* any coreboot */
if (g_strcmp0(fu_context_get_hwid_value(ctx, FU_HWIDS_KEY_BIOS_VENDOR), "coreboot") == 0)
return g_strdup(FWUPD_DEVICE_ID_ANY);
for (guint i = 0; i < hwids->len; i++) {
const gchar *guid = g_ptr_array_index(hwids, i);
const gchar *plugin_name =
fu_context_lookup_quirk_by_id(ctx, guid, FU_QUIRKS_PLUGIN);
if (g_strcmp0(plugin_name, "flashrom") == 0)
return guid;
}
g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, "no HwIDs found");
return NULL;
}
static gboolean
fu_flashrom_plugin_startup(FuPlugin *plugin, FuProgress *progress, GError **error)
{
const gchar *flashrom_args;
const gchar *flashrom_prog;
gint rc;
const gchar *guid;
FuContext *ctx = fu_plugin_get_context(plugin);
FuFlashromPlugin *self = FU_FLASHROM_PLUGIN(plugin);
/* progress */
fu_progress_set_id(progress, G_STRLOC);
fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 5, "find-guid");
fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 90, "init");
fu_progress_add_step(progress, FWUPD_STATUS_LOADING, 5, "probe");
guid = fu_flashrom_plugin_find_guid(plugin, error);
if (guid == NULL)
return FALSE;
fu_progress_step_done(progress);
/* if changed */
if (g_strcmp0(self->guid, guid) != 0) {
g_free(self->guid);
self->guid = g_strdup(guid);
}
if (flashrom_init(SELFCHECK_TRUE)) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"flashrom initialization error");
return FALSE;
}
flashrom_set_log_callback(fu_flashrom_plugin_debug_cb);
fu_progress_step_done(progress);
/* allow overriding from quirk file */
flashrom_prog = fu_context_lookup_quirk_by_id(ctx, guid, "FlashromProgrammer");
if (flashrom_prog == NULL)
flashrom_prog = "internal";
flashrom_args = fu_context_lookup_quirk_by_id(ctx, guid, "FlashromArgs");
g_debug("using programmer %s: %s", flashrom_prog, flashrom_args);
if (flashrom_programmer_init(&self->flashprog, flashrom_prog, flashrom_args)) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"programmer initialization failed");
return FALSE;
}
rc = flashrom_flash_probe(&self->flashctx, self->flashprog, NULL);
if (rc == 3) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"flash probe failed: multiple chips were found");
return FALSE;
}
if (rc == 2) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"flash probe failed: no chip was found");
return FALSE;
}
if (rc != 0) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"flash probe failed: unknown error");
return FALSE;
}
fu_progress_step_done(progress);
return TRUE;
}
static gboolean
fu_flashrom_plugin_unlock(FuPlugin *self, FuDevice *device, GError **error)
{
return fu_flashrom_device_unlock(FU_FLASHROM_DEVICE(device), error);
}
static void
fu_flashrom_plugin_constructed(GObject *obj)
{
FuPlugin *plugin = FU_PLUGIN(obj);
FuContext *ctx = fu_plugin_get_context(plugin);
fu_context_add_quirk_key(ctx, "FlashromFmapRegions");
(void)fu_plugin_alloc_data(plugin, sizeof(FuFlashromPlugin));
fu_plugin_add_rule(plugin, FU_PLUGIN_RULE_METADATA_SOURCE, "linux_lockdown");
fu_plugin_add_rule(plugin, FU_PLUGIN_RULE_CONFLICTS, "coreboot"); /* obsoleted */
fu_plugin_add_flag(plugin, FWUPD_PLUGIN_FLAG_REQUIRE_HWID);
fu_plugin_add_flag(plugin, FWUPD_PLUGIN_FLAG_MEASURE_SYSTEM_INTEGRITY);
fu_plugin_add_device_gtype(plugin, FU_TYPE_FLASHROM_DEVICE); /* coverage */
}
static void
fu_flashrom_plugin_finalize(GObject *obj)
{
FuPlugin *plugin = FU_PLUGIN(obj);
FuFlashromPlugin *self = FU_FLASHROM_PLUGIN(plugin);
if (self->flashctx != NULL)
flashrom_flash_release(self->flashctx);
if (self->flashprog != NULL)
flashrom_programmer_shutdown(self->flashprog);
g_free(self->guid);
/* nocheck:finalize */
/* G_OBJECT_CLASS(fu_flashrom_plugin_parent_class)->finalize() not required as modular */
}
void
fu_plugin_init_vfuncs(FuPluginVfuncs *vfuncs)
{
vfuncs->constructed = fu_flashrom_plugin_constructed;
vfuncs->finalize = fu_flashrom_plugin_finalize;
vfuncs->to_string = fu_flashrom_plugin_to_string;
vfuncs->device_registered = fu_flashrom_plugin_device_registered;
vfuncs->startup = fu_flashrom_plugin_startup;
vfuncs->coldplug = fu_flashrom_plugin_coldplug;
vfuncs->unlock = fu_flashrom_plugin_unlock;
}
|