Use the TPM to back up some of the nvram fields

We use a few bytes of battery-backed nvram to save some flags across
reboots. However if the battery discharges completely, these flags are lost.
There aren't any security issues with that since they reset to safe values,
but some of the flags are used to configure how the system boots in
dev-mode.

If a dev-mode user has completely replaced ChromeOS with some other OS, then
she often needs to set the dev_boot_usb and/or dev_boot_legacy flags as well
in order to boot it using Ctrl-U or Ctrl-L. If the battery dies, then those
flags are cleared, and the only way to make the Chromebook boot again is by
going through recovery, which wipes the disk.

This change uses a new NV space in the TPM to back up some of the nvram
flags. These nvram fields will be backed up:

  block_devmode
  dev_boot_legacy
  dev_boot_signed_only
  dev_boot_usb
  fwupdate_tries
  loc_idx

Because writing to the TPM space is slow and limited to an unspecified but
finite number of cycles, we only back up the fields when specifically
requested by the new backup_nvram_request flag. This flag will be set by
crossystem whenever it is used to change any of the fields listed above. The
backup will be attempted at the NEXT boot (because the TPM is locked after
booting), and the backup_nvram_request flag will be cleared if the backup
was successfull.

Note that this CL is for Top of Trunk only. The firmware will create the
required TPM spaces on systems that have never been booted, but we don't yet
have a secure or reliable method to update existing systems.

FYI, on Link, determining that the TPM's backup NV space doesn't exist adds
about 6ms to the boot time. If it does exist, the backup_nvram_request flag
is cleared automatically so it won't check until it's set again.

BUG=chromium:362105
BRANCH=ToT (only!)
TEST=manual

Testing this is a long and involved process. Read on...

First, there are host-side tests for it. In the chroot:

  cd src/platform/ec
  make runtests

Second, to test on a completely NEW system that was first booted with a BIOS
that contains this CL, do this:

  Enter dev-mode
  Use crossystem to set values for the fields listed above
  Confirm that "backup_nvram_request" is set to 1
  Reboot
  Use crossystem to confirm that "backup_nvram_request" is now 0
  Remove the battery and the AC
  Reattach either battery or AC so it will boot again
  Use crossystem to confirm that the backed up fields are still good, while
    the others have been reset to default values
  Switch to normal mode
  Remove the battery and the AC
  Reattach either battery or AC so it will boot again
  Look at the bios info in chrome://system to see what crossystem says
  Confirm that the dev_boot_* flags are all 0, while the others are restored

Third, to set things up to test this on an existing system (I used Link),
you have update the BIOS, delete both the Kernel and Firmware NV spaces in
the TPM, then reboot so that the BIOS will create the Backup, Kernel, and
Firmware spaces. It will only do that if they're all missing.

  Open it up, disable write-protect, attach a servo, etc.
  Switch to dev-mode, log in.
  Run make_dev_firmware.sh

  Reboot in recovery mode, and insert a USB stick with a test image on it.

  NOTE: In order to fiddle with the TPM, we'll *always* have to boot in
  recovery mode, since that's the only time the TPM is left unlocked. That's
  NOT the same as pressing Ctrl-U at the scary boot screen. The rest of
  these steps assume you've booted in recovery mode and are running from the
  test image on the USB stick.

  Run

    make_dev_ssd.sh --remove_rootfs_verification --recovery_key

  Reboot (recovery mode)

  Run

    mv /etc/init/tcsd.conf /etc/init/tcsd.conf.disabled

  Reboot (recovery mode).

  Run "tpmc getvf". It should say

    deactivated 0
    disableForceClear 0
    physicalPresence 1
    physicalPresenceLock 0
    bGlobalLock 0

  Run "tpmc geto". It should say

    Owned: no

  Now you'll need to build the "tpm-nvtool" utility. In the chroot:

    cd src/third_party/tpm/nvtool
    make

  Copy that to the DUT, in /usr/local/bin.

  Now run

    tcsd
    tpm-nvtool --list | grep Index

  You may see a number of spaces, but you should at least see these:

     # NV Index 0x00001007
     # NV Index 0x00001008

  Run

    tpm_takeownership

  It will prompt you for two passwords (and confirm each one). Respond with
  something you can remember like "google".

  Run

    tpm-nvtool  --release --index 0x1007 --owner_password "google"
    tpm-nvtool  --release --index 0x1008 --owner_password "google"

  Verify that it worked with

    tpm-nvtool --list | grep Index

  Power off.

  Using servo, flash the new BIOS that has this CL in it.

  Power on, normally this time (not recovery mode). If all goes well, it
  should create the correct NV spaces and boot into the SSD. Copy tpm-nvtool
  into this image too, and run

    tpm-nvtool --list | grep Index

  You should now see at least these spaces:

     # NV Index 0x00001007
     # NV Index 0x00001008
     # NV Index 0x00001009

  Now you're ready to test the backup/recover feature.

Change-Id: I00031fa0774720147327e2ae0f37e26b34b86341
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/202138
Reviewed-by: Luigi Semenzato <semenzato@chromium.org>
diff --git a/tests/vboot_api_init_tests.c b/tests/vboot_api_init_tests.c
index 9aa34b4..96ab9d3 100644
--- a/tests/vboot_api_init_tests.c
+++ b/tests/vboot_api_init_tests.c
@@ -32,6 +32,9 @@
 static uint32_t mock_rfs_retval;
 static int rfs_clear_tpm_request;
 static int rfs_disable_dev_request;
+static uint8_t backup_space[BACKUP_NV_SIZE];
+static int backup_write_called;
+static int backup_read_called;
 
 /* Reset mock data (for use before each test) */
 static void ResetMocks(void)
@@ -53,6 +56,10 @@
 	VbNvSetup(&vnc);
 	VbNvTeardown(&vnc);                   /* So CRC gets generated */
 
+	Memset(backup_space, 0, sizeof(backup_space));
+	backup_write_called = 0;
+	backup_read_called = 0;
+
 	Memset(&shared_data, 0, sizeof(shared_data));
 	VbSharedDataInit(shared, sizeof(shared_data));
 
@@ -79,11 +86,25 @@
 
 VbError_t VbExNvStorageWrite(const uint8_t *buf)
 {
-	nv_write_called = 1;
+	nv_write_called++;
 	Memcpy(vnc.raw, buf, sizeof(vnc.raw));
 	return VBERROR_SUCCESS;
 }
 
+uint32_t RollbackBackupRead(uint8_t *raw)
+{
+	backup_read_called++;
+	Memcpy(raw, backup_space, sizeof(backup_space));
+	return TPM_SUCCESS;
+}
+
+uint32_t RollbackBackupWrite(uint8_t *raw)
+{
+	backup_write_called++;
+	Memcpy(backup_space, raw, sizeof(backup_space));
+	return TPM_SUCCESS;
+}
+
 uint64_t VbExGetTimer(void)
 {
 	/*
@@ -514,10 +535,233 @@
 	TEST_EQ(rfs_clear_tpm_request, 1, "rfs tpm clear request");
 }
 
+static void VbInitTestBackup(void)
+{
+	VbNvContext tmp_vnc;
+	uint32_t u, nv_w, bu_r;
+
+	ResetMocks();
+	/* Normal mode call */
+	TestVbInit(0, 0, "normal mode, no backup");
+	TEST_EQ(shared->flags, 0, "  shared flags");
+	TEST_EQ(iparams.out_flags, 0, "  out flags");
+	TEST_EQ(nv_write_called, 0,
+		"  NV write not called since nothing changed");
+
+	ResetMocks();
+	/* Now set some params that should be backed up. */
+	VbNvSet(&vnc, VBNV_KERNEL_FIELD, 0xaabbccdd);
+	VbNvSet(&vnc, VBNV_LOCALIZATION_INDEX, 0xa5);
+	VbNvSet(&vnc, VBNV_DEV_BOOT_USB, 1);
+	VbNvSet(&vnc, VBNV_DEV_BOOT_LEGACY, 1);
+	VbNvSet(&vnc, VBNV_DEV_BOOT_SIGNED_ONLY, 1);
+	/* and some that don't */
+	VbNvSet(&vnc, VBNV_OPROM_NEEDED, 1);
+	VbNvSet(&vnc, VBNV_TRY_B_COUNT, 3);
+	/* Make sure they're clean */
+	VbNvTeardown(&vnc);
+	/* Normal mode call */
+	TestVbInit(0, 0, "normal mode, some backup");
+	TEST_EQ(shared->flags, 0, "  shared flags");
+	TEST_EQ(iparams.out_flags, 0, "  out flags");
+	TEST_EQ(nv_write_called, 1,
+		"  Write NV because things have changed");
+	/* Some fields should be unchanged */
+	VbNvGet(&vnc, VBNV_KERNEL_FIELD, &u);
+	TEST_EQ(u, 0xaabbccdd, "  NV kernel field");
+	VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &u);
+	TEST_EQ(u, 0xa5, "  NV localization index");
+	VbNvGet(&vnc, VBNV_OPROM_NEEDED, &u);
+	TEST_EQ(u, 1, "  NV oprom_needed");
+	VbNvGet(&vnc, VBNV_TRY_B_COUNT, &u);
+	TEST_EQ(u, 3, "  NV try_b_count");
+	/* But normal mode should have cleared the DEV_BOOT flags */
+	VbNvGet(&vnc, VBNV_DEV_BOOT_USB, &u);
+	TEST_EQ(u, 0, "  NV dev_boot_usb");
+	VbNvGet(&vnc, VBNV_DEV_BOOT_LEGACY, &u);
+	TEST_EQ(u, 0, "  NV dev_boot_legacy");
+	VbNvGet(&vnc, VBNV_DEV_BOOT_SIGNED_ONLY, &u);
+	TEST_EQ(u, 0, "  NV dev_boot_signed_only");
+	/* So we should have written the backup */
+	TEST_EQ(backup_write_called, 1, "  Backup written once");
+	/* And the backup should reflect the persisent flags. */
+	Memset(&tmp_vnc, 0, sizeof(tmp_vnc));
+	TEST_EQ(0, RestoreNvFromBackup(&tmp_vnc), "read from backup");
+	VbNvGet(&tmp_vnc, VBNV_KERNEL_FIELD, &u);
+	TEST_EQ(u, 0xaabbccdd, "  BU kernel field");
+	VbNvGet(&tmp_vnc, VBNV_LOCALIZATION_INDEX, &u);
+	TEST_EQ(u, 0xa5, "  BU localization index");
+	VbNvGet(&tmp_vnc, VBNV_DEV_BOOT_USB, &u);
+	TEST_EQ(u, 0, "  BU dev_boot_usb");
+	VbNvGet(&tmp_vnc, VBNV_DEV_BOOT_LEGACY, &u);
+	TEST_EQ(u, 0, "  BU dev_boot_legacy");
+	VbNvGet(&tmp_vnc, VBNV_DEV_BOOT_SIGNED_ONLY, &u);
+	TEST_EQ(u, 0, "  BU dev_boot_signed_only");
+	/* but not the others */
+	VbNvGet(&tmp_vnc, VBNV_OPROM_NEEDED, &u);
+	TEST_EQ(u, 0, "  BU oprom_needed");
+	VbNvGet(&tmp_vnc, VBNV_TRY_B_COUNT, &u);
+	TEST_EQ(u, 0, "  BU try_b_count");
+
+	/*
+	 * If we change one of the non-backed-up NVRAM params and try
+	 * again, we shouldn't need to backup again.
+	 */
+	VbNvSet(&vnc, VBNV_OPROM_NEEDED, 0);
+	VbNvSet(&vnc, VBNV_TRY_B_COUNT, 2);
+	/* Make sure they're clean */
+	VbNvTeardown(&vnc);
+	/* Normal mode call */
+	TestVbInit(0, 0, "normal mode, expect no backup");
+	TEST_EQ(shared->flags, 0, "  shared flags");
+	TEST_EQ(iparams.out_flags, 0, "  out flags");
+	TEST_EQ(backup_write_called, 1, "  Backup still only written once");
+
+	/* Now switch to dev-mode. */
+	iparams.flags = VB_INIT_FLAG_DEV_SWITCH_ON;
+	TestVbInit(0, 0, "Dev mode on");
+	TEST_EQ(shared->recovery_reason, 0, "  recovery reason");
+	TEST_EQ(iparams.out_flags,
+		VB_INIT_OUT_CLEAR_RAM |
+		VB_INIT_OUT_ENABLE_DISPLAY |
+		VB_INIT_OUT_ENABLE_USB_STORAGE |
+		VB_INIT_OUT_ENABLE_DEVELOPER |
+		VB_INIT_OUT_ENABLE_ALTERNATE_OS, "  out flags");
+	TEST_EQ(shared->flags, VBSD_BOOT_DEV_SWITCH_ON, "  shared flags");
+	TEST_EQ(backup_write_called, 1, "  Still only one backup");
+
+	/* Now change some params that should be backed up. */
+	VbNvSet(&vnc, VBNV_KERNEL_FIELD, 0xdeadbeef);
+	VbNvSet(&vnc, VBNV_LOCALIZATION_INDEX, 0x5a);
+	VbNvSet(&vnc, VBNV_DEV_BOOT_USB, 1);
+	VbNvSet(&vnc, VBNV_DEV_BOOT_LEGACY, 1);
+	VbNvSet(&vnc, VBNV_DEV_BOOT_SIGNED_ONLY, 1);
+	/* and some that don't */
+	VbNvSet(&vnc, VBNV_OPROM_NEEDED, 1);
+	VbNvSet(&vnc, VBNV_TRY_B_COUNT, 4);
+	/* Make sure they're clean */
+	VbNvTeardown(&vnc);
+	TestVbInit(0, 0, "Dev mode on");
+	TEST_EQ(shared->recovery_reason, 0, "  recovery reason");
+	TEST_EQ(iparams.out_flags,
+		VB_INIT_OUT_CLEAR_RAM |
+		VB_INIT_OUT_ENABLE_DISPLAY |
+		VB_INIT_OUT_ENABLE_USB_STORAGE |
+		VB_INIT_OUT_ENABLE_DEVELOPER, "  out flags");
+	TEST_EQ(shared->flags, VBSD_BOOT_DEV_SWITCH_ON, "  shared flags");
+	TEST_EQ(backup_write_called, 1, "  Once more, one backup");
+
+	/* But if we explictly request a backup, they'll get saved. */
+	VbNvSet(&vnc, VBNV_BACKUP_NVRAM_REQUEST, 1);
+	VbNvTeardown(&vnc);
+	TestVbInit(0, 0, "Dev mode on");
+	TEST_EQ(shared->recovery_reason, 0, "  recovery reason");
+	TEST_EQ(iparams.out_flags,
+		VB_INIT_OUT_CLEAR_RAM |
+		VB_INIT_OUT_ENABLE_DISPLAY |
+		VB_INIT_OUT_ENABLE_USB_STORAGE |
+		VB_INIT_OUT_ENABLE_DEVELOPER, "  out flags");
+	TEST_EQ(shared->flags, VBSD_BOOT_DEV_SWITCH_ON, "  shared flags");
+	TEST_EQ(backup_write_called, 2, "  Two backups now");
+	VbNvGet(&vnc, VBNV_BACKUP_NVRAM_REQUEST, &u);
+	TEST_EQ(u, 0, "  backup_request cleared");
+	/* Quick check that the non-backed-up stuff is still valid */
+	VbNvGet(&vnc, VBNV_OPROM_NEEDED, &u);
+	TEST_EQ(u, 1, "  NV oprom_needed");
+	VbNvGet(&vnc, VBNV_TRY_B_COUNT, &u);
+	TEST_EQ(u, 4, "  NV try_b_count");
+	/* But only the stuff we care about was backed up */
+	Memset(&tmp_vnc, 0, sizeof(tmp_vnc));
+	TEST_EQ(0, RestoreNvFromBackup(&tmp_vnc), "read from backup");
+	VbNvGet(&tmp_vnc, VBNV_KERNEL_FIELD, &u);
+	TEST_EQ(u, 0xdeadbeef, "  BU kernel field");
+	VbNvGet(&tmp_vnc, VBNV_LOCALIZATION_INDEX, &u);
+	TEST_EQ(u, 0x5a, "  BU localization index");
+	VbNvGet(&tmp_vnc, VBNV_DEV_BOOT_USB, &u);
+	TEST_EQ(u, 1, "  BU dev_boot_usb");
+	VbNvGet(&tmp_vnc, VBNV_DEV_BOOT_LEGACY, &u);
+	TEST_EQ(u, 1, "  BU dev_boot_legacy");
+	VbNvGet(&tmp_vnc, VBNV_DEV_BOOT_SIGNED_ONLY, &u);
+	TEST_EQ(u, 1, "  BU dev_boot_signed_only");
+	/* but not the others */
+	VbNvGet(&tmp_vnc, VBNV_OPROM_NEEDED, &u);
+	TEST_EQ(u, 0, "  BU oprom_needed");
+	VbNvGet(&tmp_vnc, VBNV_TRY_B_COUNT, &u);
+	TEST_EQ(u, 0, "  BU try_b_count");
+
+	/* If we lose the NV storage, the backup bits will be restored */
+	vnc.raw[0] = 0;
+	bu_r = backup_read_called;
+	nv_w = nv_write_called;
+	TestVbInit(0, 0, "Dev mode on");
+	TEST_EQ(shared->recovery_reason, 0, "  recovery reason");
+	TEST_EQ(iparams.out_flags,
+		VB_INIT_OUT_CLEAR_RAM |
+		VB_INIT_OUT_ENABLE_DISPLAY |
+		VB_INIT_OUT_ENABLE_USB_STORAGE |
+		VB_INIT_OUT_ENABLE_DEVELOPER, "  out flags");
+	TEST_EQ(shared->flags, VBSD_BOOT_DEV_SWITCH_ON, "  shared flags");
+	TEST_EQ(backup_write_called, 2, "  Still just two backups now");
+	TEST_EQ(backup_read_called, bu_r + 1, "  One more backup read");
+	TEST_EQ(nv_write_called, nv_w + 1, "  One more NV write");
+	/* The non-backed-up stuff is reset to defaults */
+	VbNvGet(&vnc, VBNV_OPROM_NEEDED, &u);
+	TEST_EQ(u, 0, "  NV oprom_needed");
+	VbNvGet(&vnc, VBNV_TRY_B_COUNT, &u);
+	TEST_EQ(u, 0, "  NV try_b_count");
+	/* And the backed up stuff is restored */
+	VbNvGet(&vnc, VBNV_KERNEL_FIELD, &u);
+	TEST_EQ(u, 0xdeadbeef, "  BU kernel field");
+	VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &u);
+	TEST_EQ(u, 0x5a, "  BU localization index");
+	VbNvGet(&vnc, VBNV_DEV_BOOT_USB, &u);
+	TEST_EQ(u, 1, "  BU dev_boot_usb");
+	VbNvGet(&vnc, VBNV_DEV_BOOT_LEGACY, &u);
+	TEST_EQ(u, 1, "  BU dev_boot_legacy");
+	VbNvGet(&vnc, VBNV_DEV_BOOT_SIGNED_ONLY, &u);
+	TEST_EQ(u, 1, "  BU dev_boot_signed_only");
+
+	/*
+	 * But if we lose the NV storage and go back to normal mode at the same
+	 * time, then the DEV_BOOT_* flags will be cleared.
+	 */
+	vnc.raw[0] = 0;
+	bu_r = backup_read_called;
+	nv_w = nv_write_called;
+	iparams.flags = 0;
+	TestVbInit(0, 0, "Back to normal mode");
+	TEST_EQ(shared->recovery_reason, 0, "  recovery reason");
+	TEST_EQ(iparams.out_flags, 0, "  out flags");
+	TEST_EQ(shared->flags, 0, "  shared flags");
+	/* We read twice: once to restore, once for read-prior-to-write */
+	TEST_EQ(backup_read_called, bu_r + 2, "  Two more backup reads");
+	TEST_EQ(backup_write_called, 3, "  Backup write due clearing DEV_*");
+	TEST_EQ(nv_write_called, nv_w + 1, "  One more NV write");
+	/* The non-backed-up stuff is reset to defaults */
+	VbNvGet(&vnc, VBNV_OPROM_NEEDED, &u);
+	TEST_EQ(u, 0, "  NV oprom_needed");
+	VbNvGet(&vnc, VBNV_TRY_B_COUNT, &u);
+	TEST_EQ(u, 0, "  NV try_b_count");
+	/* And the backed up stuff is restored */
+	VbNvGet(&vnc, VBNV_KERNEL_FIELD, &u);
+	TEST_EQ(u, 0xdeadbeef, "  BU kernel field");
+	VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &u);
+	TEST_EQ(u, 0x5a, "  BU localization index");
+	/* But not the DEV_BOOT_* flags */
+	VbNvGet(&vnc, VBNV_DEV_BOOT_USB, &u);
+	TEST_EQ(u, 0, "  BU dev_boot_usb");
+	VbNvGet(&vnc, VBNV_DEV_BOOT_LEGACY, &u);
+	TEST_EQ(u, 0, "  BU dev_boot_legacy");
+	VbNvGet(&vnc, VBNV_DEV_BOOT_SIGNED_ONLY, &u);
+	TEST_EQ(u, 0, "  BU dev_boot_signed_only");
+}
+
+
 int main(int argc, char *argv[])
 {
 	VbInitTest();
 	VbInitTestTPM();
+	VbInitTestBackup();
 
 	return gTestSuccess ? 0 : 255;
 }