vboot: Publicize check_reboot_for_display()

Publicize check_reboot_for_display() and rename it to
vb2api_need_reboot_for_display() for depthcharge to use.

BRANCH=none
BUG=chromium:1030608
TEST=FEATURES=test emerge-nami vboot_reference

Change-Id: Ie6c734850b67b29c8a098c3f232888f3ab47fddf
Signed-off-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/1957726
Reviewed-by: Joel Kitching <kitching@chromium.org>
diff --git a/firmware/2lib/2auxfw_sync.c b/firmware/2lib/2auxfw_sync.c
index 7c6c2ba..e90ee7f 100644
--- a/firmware/2lib/2auxfw_sync.c
+++ b/firmware/2lib/2auxfw_sync.c
@@ -5,6 +5,7 @@
  * Auxiliary firmware sync routines for vboot
  */
 
+#include "2api.h"
 #include "2common.h"
 #include "2misc.h"
 #include "2nvstorage.h"
@@ -14,19 +15,6 @@
 #include "vboot_display.h"
 
 /**
- * If no display is available, set DISPLAY_REQUEST in NV space
- */
-static int check_reboot_for_display(struct vb2_context *ctx)
-{
-	if (!(vb2_get_sd(ctx)->flags & VB2_SD_FLAG_DISPLAY_AVAILABLE)) {
-		VB2_DEBUG("Reboot to initialize display\n");
-		vb2_nv_set(ctx, VB2_NV_DISPLAY_REQUEST, 1);
-		return 1;
-	}
-	return 0;
-}
-
-/**
  * Display the WAIT screen
  */
 static void display_wait_screen(struct vb2_context *ctx)
@@ -126,7 +114,7 @@
 	/* If AUX FW update is slow display the wait screen */
 	if (fw_update == VB_AUX_FW_SLOW_UPDATE) {
 		/* Display should be available, but better check again */
-		if (check_reboot_for_display(ctx))
+		if (vb2api_need_reboot_for_display(ctx))
 			return VBERROR_REBOOT_REQUIRED;
 		display_wait_screen(ctx);
 	}
diff --git a/firmware/2lib/2ec_sync.c b/firmware/2lib/2ec_sync.c
index aa06303..03948ea 100644
--- a/firmware/2lib/2ec_sync.c
+++ b/firmware/2lib/2ec_sync.c
@@ -19,19 +19,6 @@
 	 VB2_SD_FLAG_ECSYNC_EC_RO : VB2_SD_FLAG_ECSYNC_EC_RW)
 
 /**
- * If no display is available, set DISPLAY_REQUEST in NV space
- */
-static int check_reboot_for_display(struct vb2_context *ctx)
-{
-	if (!(vb2_get_sd(ctx)->flags & VB2_SD_FLAG_DISPLAY_AVAILABLE)) {
-		VB2_DEBUG("Reboot to initialize display\n");
-		vb2_nv_set(ctx, VB2_NV_DISPLAY_REQUEST, 1);
-		return 1;
-	}
-	return 0;
-}
-
-/**
  * Display the WAIT screen
  */
 static void display_wait_screen(struct vb2_context *ctx)
@@ -474,7 +461,7 @@
 	vb2_error_t phase1_rv = ec_sync_phase1(ctx);
 	int need_wait_screen = ec_will_update_slowly(ctx);
 
-	if (need_wait_screen && check_reboot_for_display(ctx))
+	if (need_wait_screen && vb2api_need_reboot_for_display(ctx))
 		return VBERROR_REBOOT_REQUIRED;
 
 	if (phase1_rv)
diff --git a/firmware/2lib/2misc.c b/firmware/2lib/2misc.c
index 8df75d6..76c780b 100644
--- a/firmware/2lib/2misc.c
+++ b/firmware/2lib/2misc.c
@@ -12,6 +12,7 @@
 #include "2rsa.h"
 #include "2secdata.h"
 #include "2sha.h"
+#include "2struct.h"
 #include "2sysincludes.h"
 #include "vboot_api.h"
 #include "vboot_struct.h"
@@ -406,3 +407,13 @@
 	/* Now we confidently check the recovery switch state at boot */
 	return !!(vb2_get_sd(ctx)->vbsd->flags & VBSD_BOOT_REC_SWITCH_ON);
 }
+
+int vb2api_need_reboot_for_display(struct vb2_context *ctx)
+{
+	if (!(vb2_get_sd(ctx)->flags & VB2_SD_FLAG_DISPLAY_AVAILABLE)) {
+		VB2_DEBUG("Need reboot to initialize display\n");
+		vb2_nv_set(ctx, VB2_NV_DISPLAY_REQUEST, 1);
+		return 1;
+	}
+	return 0;
+}
diff --git a/firmware/2lib/include/2api.h b/firmware/2lib/include/2api.h
index 0c56e64..fda1fa7 100644
--- a/firmware/2lib/include/2api.h
+++ b/firmware/2lib/include/2api.h
@@ -788,6 +788,14 @@
  */
 vb2_error_t vb2api_auxfw_sync(struct vb2_context *ctx);
 
+/**
+ * If no display is available, set DISPLAY_REQUEST in nvdata.
+ *
+ * @param ctx           Vboot2 context
+ * @return 1 if DISPLAY_REQUEST is set and a reboot is required, or 0 otherwise.
+ */
+int vb2api_need_reboot_for_display(struct vb2_context *ctx);
+
 /*****************************************************************************/
 /* APIs provided by the caller to verified boot */
 
diff --git a/tests/vb2_misc_tests.c b/tests/vb2_misc_tests.c
index 38806ea..22de3ae 100644
--- a/tests/vb2_misc_tests.c
+++ b/tests/vb2_misc_tests.c
@@ -724,6 +724,24 @@
 		"prev failure");
 }
 
+static void need_reboot_for_display_tests(void)
+{
+	/* Display not available, reboot required */
+	reset_common_data();
+	TEST_EQ(vb2api_need_reboot_for_display(ctx), 1,
+		"need_reboot_for_display: need reboot");
+	TEST_EQ(vb2_nv_get(ctx, VB2_NV_DISPLAY_REQUEST), 1,
+		"  set display request");
+
+	/* Display available, don't need reboot */
+	reset_common_data();
+	sd->flags |= VB2_SD_FLAG_DISPLAY_AVAILABLE;
+	TEST_EQ(vb2api_need_reboot_for_display(ctx), 0,
+		"need_reboot_for_display: don't need reboot");
+	TEST_EQ(vb2_nv_get(ctx, VB2_NV_DISPLAY_REQUEST), 0,
+		"  not set display request");
+}
+
 int main(int argc, char* argv[])
 {
 	init_workbuf_tests();
@@ -734,6 +752,7 @@
 	dev_switch_tests();
 	tpm_clear_tests();
 	select_slot_tests();
+	need_reboot_for_display_tests();
 
 	return gTestSuccess ? 0 : 255;
 }