From 969ce32e407f32671ab79444238b9ddf98c7d960 Mon Sep 17 00:00:00 2001 From: Nicolas Norvez Date: Tue, 9 Aug 2016 11:42:08 +0100 Subject: Detect and report VM environment in crossystem Add "inside_vm" command to crossystem. x86: If there is no HWID and mainfw_type is "nonchrome", report that the host is a VM. If HWID is present, it's not a VM. ARM: Detection not implemented and so far no ARM VMs exist, always report that the system is not a VM BUG=chromium:632303 TEST=emerge-cyan vboot_reference and test binary on cyan QEMU and HW BRANCH=none Change-Id: I18f5cb24b68e51f3097d9aafd9f0db0e610d322a Reviewed-on: https://chromium-review.googlesource.com/367240 Commit-Ready: Nicolas Norvez Tested-by: Nicolas Norvez Reviewed-by: Randall Spangler --- host/arch/arm/lib/crossystem_arch.c | 8 ++++++-- host/arch/mips/lib/crossystem_arch.c | 2 ++ host/arch/x86/lib/crossystem_arch.c | 17 +++++++++++++++++ utility/crossystem.c | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/host/arch/arm/lib/crossystem_arch.c b/host/arch/arm/lib/crossystem_arch.c index 309235dc..ae1a0299 100644 --- a/host/arch/arm/lib/crossystem_arch.c +++ b/host/arch/arm/lib/crossystem_arch.c @@ -401,11 +401,15 @@ int VbGetArchPropertyInt(const char* name) { if (value != -1) return value; return VbGetVarGpio("write-protect-switch"); - } else if (!strcasecmp(name, "recoverysw_ec_boot")) + } else if (!strcasecmp(name, "recoverysw_ec_boot")) { /* TODO: read correct value using ectool */ return 0; - else + } else if (!strcasecmp(name, "inside_vm")) { + /* No ARM VMs currently. */ + return 0; + } else { return -1; + } } const char* VbGetArchPropertyString(const char* name, char* dest, diff --git a/host/arch/mips/lib/crossystem_arch.c b/host/arch/mips/lib/crossystem_arch.c index 28a6b806..ed1c7ab8 100644 --- a/host/arch/mips/lib/crossystem_arch.c +++ b/host/arch/mips/lib/crossystem_arch.c @@ -42,6 +42,8 @@ int VbGetArchPropertyInt(const char* name) { return 0; } else if (!strcasecmp(name,"wpsw_boot")) { return 1; + } else if (!strcasecmp(name,"inside_vm")) { + return 0; } return -1; } diff --git a/host/arch/x86/lib/crossystem_arch.c b/host/arch/x86/lib/crossystem_arch.c index d7e89fe1..e1ee4f82 100644 --- a/host/arch/x86/lib/crossystem_arch.c +++ b/host/arch/x86/lib/crossystem_arch.c @@ -803,6 +803,23 @@ int VbGetArchPropertyInt(const char* name) { value = (int)fwupdate_value; } + /* Detect if the host is a VM. If there is no HWID and the firmware type + * is "nonchrome", then assume it is a VM. If HWID is present, it is a + * baremetal Chrome OS machine. Other cases are errors. */ + if (!strcasecmp(name,"inside_vm")) { + char hwid[VB_MAX_STRING_PROPERTY]; + if (!VbGetArchPropertyString("hwid", hwid, sizeof(hwid))) { + char fwtype_buf[VB_MAX_STRING_PROPERTY]; + const char *fwtype = VbGetArchPropertyString("mainfw_type", fwtype_buf, + sizeof(fwtype_buf)); + if (fwtype && !strcasecmp(fwtype,"nonchrome")) { + value = 1; + } + } else { + value = 0; + } + } + return value; } diff --git a/utility/crossystem.c b/utility/crossystem.c index c88a444d..6278e30f 100644 --- a/utility/crossystem.c +++ b/utility/crossystem.c @@ -69,6 +69,7 @@ const Param sys_param_list[] = { {"fw_prev_tried", IS_STRING, "Firmware tried on previous boot (vboot2)"}, {"fw_prev_result", IS_STRING, "Firmware result of previous boot (vboot2)"}, {"hwid", IS_STRING, "Hardware ID"}, + {"inside_vm", 0, "Running in a VM?"}, {"kern_nv", 0, "Non-volatile field for kernel use", "0x%08x"}, {"kernkey_vfy", IS_STRING, "Type of verification done on kernel key block"}, {"loc_idx", CAN_WRITE, "Localization index for firmware screens (writable)"}, -- cgit v1.2.1