crossystem: Recover corrupted RW_NVRAM on flash writes

CL:4782351 changes the VBNV searching algorithm to binary search to
match firmware behavior. However, when the FMAP region RW_NVRAM is
corrupted, i.e., there are used entries occurring after blank ones, the
new appended entry is still not guaranteed to be found by the next
binary search (either by firmware or crossystem). Therefore, we need a
mechanism to recover the corrupted RW_NVRAM.

Modify crossystem to erase the entire region when seeing a corrupted
RW_NVRAM. Note that we only do that on flash writes because users may
not expect the flash to be modified by crossystem read operations.

Also change the vb2_write_nv_storage_flashrom() behavior for the blank
RW_NVRAM case. Instead of returning an error, now it will simply write
to entry 0 (beginning of RW_NVRAM). This change shouldn't have any
practical impact, because the previous vb2_read_nv_storage_flashrom()
call should have failed in this case.

BUG=b:172340350, b:295040078
TEST=make runtests
TEST=emerge-geralt vboot_reference
BRANCH=none

Disallow-Recycled-Builds: geralt-cq, trogdor-zephyr-cq
Change-Id: I6b754f204ab94c86e6d1baeb4d0b1811574b542b
Signed-off-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/4787788
Reviewed-by: Julius Werner <jwerner@chromium.org>
2 files changed