vboot: Introduce menu UI

According to crbug.com/1033815, add files for MENU_UI:
- 2ui.{c,h} for entry point functions called by kernel selection.
- vb2_ui_tests.c for tests (normal boot included).

There are three entry points: vb2_developer_menu(),
vb2_broken_recovery_menu(), and vb2_manual_recovery_menu().

Only infinite loop for this CL, others will be added in separate CLs.

BRANCH=none
BUG=b:146399181,chromium:1033815
TEST=USE="legacy_clamshell_ui" emerge-nami vboot_reference depthcharge
TEST=USE="legacy_menu_ui" emerge-nami vboot_reference depthcharge
TEST=USE="menu_ui" emerge-nami vboot_reference depthcharge
TEST=make runtests

Cq-Depend: chromium:2043102
Change-Id: I5a23eb006754b5ff08eb42bcd5021374995eab40
Signed-off-by: Hsuan Ting Chen <roccochen@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2087557
Reviewed-by: Joel Kitching <kitching@chromium.org>
Commit-Queue: Yu-Ping Wu <yupingso@chromium.org>
diff --git a/Makefile b/Makefile
index 6a610c8..5fd2ec6 100644
--- a/Makefile
+++ b/Makefile
@@ -192,6 +192,13 @@
 CFLAGS += -DMOCK_TPM
 endif
 
+# Enable the menu-based user interface.
+ifneq ($(filter-out 0,${MENU_UI}),)
+CFLAGS += -DMENU_UI=1
+else
+CFLAGS += -DMENU_UI=0
+endif
+
 # LEGACY_MENU_UI controls whether to enable legacy menu UI, which is used with
 # devices that don't have a keyboard (detachables).
 # Pass LEGACY_MENU_UI= (or =0) to make to disable feature.
@@ -369,6 +376,7 @@
 	firmware/2lib/2sha512.c \
 	firmware/2lib/2sha_utility.c \
 	firmware/2lib/2tpm_bootmode.c \
+	firmware/2lib/2ui.c \
 	firmware/lib/cgptlib/cgptlib.c \
 	firmware/lib/cgptlib/cgptlib_internal.c \
 	firmware/lib/cgptlib/crc32.c \
@@ -709,6 +717,7 @@
 	tests/vb2_secdata_kernel_tests \
 	tests/vb2_sha_api_tests \
 	tests/vb2_sha_tests \
+	tests/vb2_ui_tests \
 	tests/hmac_test
 
 TEST20_NAMES = \
@@ -1219,6 +1228,7 @@
 	${RUNTEST} ${BUILD_RUN}/tests/vb2_secdata_kernel_tests
 	${RUNTEST} ${BUILD_RUN}/tests/vb2_sha_api_tests
 	${RUNTEST} ${BUILD_RUN}/tests/vb2_sha_tests
+	${RUNTEST} ${BUILD_RUN}/tests/vb2_ui_tests
 	${RUNTEST} ${BUILD_RUN}/tests/vb20_api_kernel_tests
 	${RUNTEST} ${BUILD_RUN}/tests/vb20_kernel_tests
 	${RUNTEST} ${BUILD_RUN}/tests/vb20_misc_tests
diff --git a/firmware/2lib/2ui.c b/firmware/2lib/2ui.c
new file mode 100644
index 0000000..b6892cb
--- /dev/null
+++ b/firmware/2lib/2ui.c
@@ -0,0 +1,43 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * User interfaces for developer and recovery mode menus.
+ */
+
+#include "2api.h"
+#include "2common.h"
+#include "2ui.h"
+
+/*****************************************************************************/
+/* Entry points */
+
+vb2_error_t vb2_developer_menu(struct vb2_context *ctx)
+{
+	/* TODO(roccochen): Init, wait for user, and boot. */
+	vb2ex_display_ui(VB2_SCREEN_BLANK, 0);
+
+	while (1);
+
+	return VB2_SUCCESS;
+}
+
+vb2_error_t vb2_broken_recovery_menu(struct vb2_context *ctx)
+{
+	/* TODO(roccochen): Init and wait for user to reset or shutdown. */
+	vb2ex_display_ui(VB2_SCREEN_BLANK, 0);
+
+	while (1);
+
+	return VB2_SUCCESS;
+}
+
+vb2_error_t vb2_manual_recovery_menu(struct vb2_context *ctx)
+{
+	/* TODO(roccochen): Init and wait for user. */
+	vb2ex_display_ui(VB2_SCREEN_BLANK, 0);
+
+	while (1);
+
+	return VB2_SUCCESS;
+}
diff --git a/firmware/2lib/include/2api.h b/firmware/2lib/include/2api.h
index e19fcf7..6be8a8d 100644
--- a/firmware/2lib/include/2api.h
+++ b/firmware/2lib/include/2api.h
@@ -1106,4 +1106,22 @@
  */
 vb2_error_t vb2ex_ec_battery_cutoff(void);
 
+/*****************************************************************************/
+/* Functions for UI display. */
+
+/* Screens. */
+enum vb2_screen {
+	/* Blank screen */
+	VB2_SCREEN_BLANK = 0,
+};
+
+/**
+ * Display UI screen.
+ *
+ * @param screen		Screen to display.
+ * @param locale		Locale.
+ * @return VB2_SUCCESS, or error code on error.
+ */
+vb2_error_t vb2ex_display_ui(enum vb2_screen screen, uint32_t locale);
+
 #endif  /* VBOOT_REFERENCE_2API_H_ */
diff --git a/firmware/2lib/include/2ui.h b/firmware/2lib/include/2ui.h
new file mode 100644
index 0000000..c5fdc1c
--- /dev/null
+++ b/firmware/2lib/include/2ui.h
@@ -0,0 +1,47 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * User interfaces for developer and recovery mode menus.
+ */
+
+#ifndef VBOOT_REFERENCE_2UI_H_
+#define VBOOT_REFERENCE_2UI_H_
+
+/**
+ * UI for a developer-mode boot.
+ *
+ * Enter the developer menu, which provides options to switch out of developer
+ * mode, boot from external media, use legacy bootloader, or boot Chrome OS from
+ * disk.
+ *
+ * If a timeout occurs, take the default boot action.
+ *
+ * @param ctx		Vboot context
+ * @returns VB2_SUCCESS, or non-zero error code.
+ */
+vb2_error_t vb2_developer_menu(struct vb2_context *ctx);
+
+/**
+ * UI for a non-manual recovery ("BROKEN").
+ *
+ * Enter the recovery menu, which shows that an unrecoverable error was
+ * encountered last boot. Wait for the user to physically reset or shut down.
+ *
+ * @param ctx		Vboot context
+ * @returns VB2_SUCCESS, or non-zero error code.
+ */
+vb2_error_t vb2_broken_recovery_menu(struct vb2_context *ctx);
+
+/**
+ * UI for a manual recovery-mode boot.
+ *
+ * Enter the recovery menu, which prompts the user to insert recovery media,
+ * navigate the step-by-step recovery, or enter developer mode if allowed.
+ *
+ * @param ctx		Vboot context
+ * @returns VB2_SUCCESS, or non-zero error code.
+ */
+vb2_error_t vb2_manual_recovery_menu(struct vb2_context *ctx);
+
+#endif  /* VBOOT_REFERENCE_2UI_H_ */
diff --git a/firmware/lib/vboot_api_kernel.c b/firmware/lib/vboot_api_kernel.c
index 82b2b03..8b7aab9 100644
--- a/firmware/lib/vboot_api_kernel.c
+++ b/firmware/lib/vboot_api_kernel.c
@@ -13,6 +13,7 @@
 #include "2rsa.h"
 #include "2secdata.h"
 #include "2sysincludes.h"
+#include "2ui.h"
 #include "load_kernel_fw.h"
 #include "utility.h"
 #include "vb2_common.h"
@@ -241,10 +242,16 @@
 			VB2_DEBUG("NO_BOOT in RECOVERY mode\n");
 
 		/* Recovery boot.  This has UI. */
-		if (LEGACY_MENU_UI)
+		if (MENU_UI) {
+			if (vb2_allow_recovery(ctx))
+				rv = vb2_manual_recovery_menu(ctx);
+			else
+				rv = vb2_broken_recovery_menu(ctx);
+		} else if (LEGACY_MENU_UI) {
 			rv = VbBootRecoveryLegacyMenu(ctx);
-		else
+		} else {
 			rv = VbBootRecoveryLegacyClamshell(ctx);
+		}
 	} else if (DIAGNOSTIC_UI && vb2_nv_get(ctx, VB2_NV_DIAG_REQUEST)) {
 		vb2_nv_set(ctx, VB2_NV_DIAG_REQUEST, 0);
 
@@ -264,7 +271,9 @@
 			rv = VBERROR_REBOOT_REQUIRED;
 	} else if (ctx->flags & VB2_CONTEXT_DEVELOPER_MODE) {
 		/* Developer boot.  This has UI. */
-		if (LEGACY_MENU_UI)
+		if (MENU_UI)
+			rv = vb2_developer_menu(ctx);
+		else if (LEGACY_MENU_UI)
 			rv = VbBootDeveloperLegacyMenu(ctx);
 		else
 			rv = VbBootDeveloperLegacyClamshell(ctx);
diff --git a/tests/vb2_ui_tests.c b/tests/vb2_ui_tests.c
new file mode 100644
index 0000000..ffcb7d6
--- /dev/null
+++ b/tests/vb2_ui_tests.c
@@ -0,0 +1,34 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Tests for developer and recovery mode UIs.
+ */
+
+#include "test_common.h"
+
+/* Tests */
+
+static void developer_tests(void)
+{
+	/* TODO(roccochen) */
+}
+
+static void broken_recovery_tests(void)
+{
+	/* TODO(roccochen) */
+}
+
+static void manual_recovery_tests(void)
+{
+	/* TODO(roccochen) */
+}
+
+int main(void)
+{
+	developer_tests();
+	broken_recovery_tests();
+	manual_recovery_tests();
+
+	return gTestSuccess ? 0 : 255;
+}