vboot: Record whether the slot switched away from failed

When both firmware slots fail and the new slot needs an extra reboot
(e.g. for EC or CSE sync), vb2_select_fw_slot() copies the new slot's
own result into FW_PREV_TRIED/FW_PREV_RESULT on that reboot. fail_impl()
then no longer sees that the other slot failed, so it switches back to
it instead of requesting recovery, and the device boots back and forth
between the two slots forever.

Add a new nvdata bit, VB2_NV_FW_OTHER_SLOT_FAILED, which
vb2_select_fw_slot() sets on every slot switch to whether the slot being
left had failed. Extra reboots on the same slot leave it alone. In
fail_impl(), decide whether to try the other slot from this bit instead
of FW_PREV_*. When RO includes this change, the bit is set whenever the
old check would be true, and with extra reboots the loop now ends in
recovery. Like FW_PREV_* with the formal RO fix discussed on the bug,
the record doesn't expire while the device stays on a slot, so a later
single failure on that slot goes straight to recovery.

FW_PREV_TRIED/FW_PREV_RESULT become vestigial for this decision, but
vb2_select_fw_slot() still sets them as before, and crossystem still
reports them (fw_prev_tried/fw_prev_result). Adding a new field instead
of changing what FW_PREV_* mean also makes it easier to build a
backport for existing firmware branches: there, RW firmware can write
the new bit without touching fields that frozen RO also writes.

The bit is MISC 0x80 rather than 0x20, which is unused on ToT, because
0x20 is still VB2_NV_MISC_DISABLE_ALT_OS on several supported firmware
branches (e.g. octopus, grunt, rammus, atlas, nocturne), whose firmware
reads and writes it. Those branches will get an equivalent backport
that writes the same bit from RW firmware, so the bit must be free
there too. 0x80 has never been assigned on any firmware branch, and old
RO, old RW and crossystem preserve it.

WARNING: Do not cherry-pick this change to existing firmware branches.
Only RO sets the new bit, and RO is already frozen on devices using
those branches. With old RO, RW built with this change never sees the
bit set, so fail_impl() never requests recovery even when both slots
fail. Existing firmware branches need the separate backport, which
writes the bit from RW firmware (e.g. CL:8842975 for
firmware-bluey-16702.B).

BRANCH=none
BUG=b:184304285
TEST=make run2tests USE_FLASHROM=0
TAG=agy
CONV=22503f2d-2161-4a37-9f9d-fe851ee04c47

Change-Id: I19641e70fdaf9f3d9441b919767958d16a6a6964
Signed-off-by: Yu-Ping Wu <yupingso@google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/8811902
Tested-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
Commit-Queue: Yu-Ping Wu <yupingso@chromium.org>
6 files changed