vboot: rename VbExGetSwitches to vb2ex_physical_presence_pressed

The only purpose of this function is to get the physical presence
switch.  Rename it appropriately, and migrate function header
into 2api.h.

BUG=b:124141368, chromium:1035761
TEST=Build locally
BRANCH=none

Change-Id: Ice3e3a362ca2d2e3aa18c60a129f67d5139daf04
Cq-Depend: chromium:2069629
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2069628
Tested-by: Joel Kitching <kitching@chromium.org>
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
Commit-Queue: Joel Kitching <kitching@chromium.org>
diff --git a/firmware/2lib/include/2api.h b/firmware/2lib/include/2api.h
index 0ad10bd..cab5d88 100644
--- a/firmware/2lib/include/2api.h
+++ b/firmware/2lib/include/2api.h
@@ -884,6 +884,13 @@
  */
 vb2_error_t vb2ex_commit_data(struct vb2_context *ctx);
 
+/**
+ * Check that physical presence button is currently pressed by the user.
+ *
+ * @returns 1 for pressed, 0 for not.
+ */
+int vb2ex_physical_presence_pressed(void);
+
 /*****************************************************************************/
 /* Auxiliary firmware (auxfw) */
 
diff --git a/firmware/include/vboot_api.h b/firmware/include/vboot_api.h
index 7fc7939..8f21fe0 100644
--- a/firmware/include/vboot_api.h
+++ b/firmware/include/vboot_api.h
@@ -47,10 +47,6 @@
 #define VB_SHARED_DATA_MIN_SIZE 3072
 #define VB_SHARED_DATA_REC_SIZE 16384
 
-/* Flags for VbExGetSwitches() */
-/* Report if user is currently present (typically via power button) */
-#define VB_SWITCH_FLAG_PHYS_PRESENCE_PRESSED	0x00000004
-
 /*
  * We use disk handles rather than indices.  Using indices causes problems if
  * a disk is removed/inserted in the middle of processing.
@@ -585,11 +581,6 @@
  */
 uint32_t VbExKeyboardReadWithFlags(uint32_t *flags_ptr);
 
-/**
- * Return the current state of the switches specified in request_mask
- */
-uint32_t VbExGetSwitches(uint32_t request_mask);
-
 /*****************************************************************************/
 /* Misc */
 
diff --git a/firmware/lib/vboot_ui_legacy_clamshell.c b/firmware/lib/vboot_ui_legacy_clamshell.c
index 8efcfe9..959981d 100644
--- a/firmware/lib/vboot_ui_legacy_clamshell.c
+++ b/firmware/lib/vboot_ui_legacy_clamshell.c
@@ -42,7 +42,7 @@
 	uint32_t key;
 	uint32_t key_flags;
 	uint32_t btn;
-	int phys_presence_button_was_pressed = 0;
+	int button_pressed = 0;
 	int shutdown_requested = 0;
 
 	VB2_DEBUG("Entering(%x)\n", confirm_flags);
@@ -92,18 +92,18 @@
 			VB2_DEBUG("No (0)\n");
 			return 0;
 		default:
-			/* If the physical presence button is physical, and is
-			 * pressed, this is also a YES, but must wait for
-			 * release.
+			/*
+			 * If the physical presence button is separate from the
+			 * keyboard, and is pressed, this is also a YES, but
+			 * must wait for release.
 			 */
 			if (!PHYSICAL_PRESENCE_KEYBOARD) {
-				btn = VbExGetSwitches(
-					VB_SWITCH_FLAG_PHYS_PRESENCE_PRESSED);
+				btn = vb2ex_physical_presence_pressed();
 				if (btn) {
 					VB2_DEBUG("Presence button pressed, "
 						  "awaiting release\n");
-					phys_presence_button_was_pressed = 1;
-				} else if (phys_presence_button_was_pressed) {
+					button_pressed = 1;
+				} else if (button_pressed) {
 					VB2_DEBUG("Presence button released "
 						  "(1)\n");
 					return 1;
@@ -481,8 +481,7 @@
 		    !(sd->flags & VB2_SD_FLAG_DEV_MODE_ENABLED) &&
 		    (sd->flags & VB2_SD_FLAG_MANUAL_RECOVERY)) {
 			if (!PHYSICAL_PRESENCE_KEYBOARD &&
-			    VbExGetSwitches(
-					VB_SWITCH_FLAG_PHYS_PRESENCE_PRESSED)) {
+			    vb2ex_physical_presence_pressed()) {
 				/*
 				 * Is the presence button stuck?  In any case
 				 * we don't like this.  Beep and ignore.
diff --git a/firmware/lib/vboot_ui_legacy_wilco.c b/firmware/lib/vboot_ui_legacy_wilco.c
index b814c24..30a7786 100644
--- a/firmware/lib/vboot_ui_legacy_wilco.c
+++ b/firmware/lib/vboot_ui_legacy_wilco.c
@@ -260,8 +260,8 @@
 vb2_error_t vb2_diagnostics_ui(struct vb2_context *ctx)
 {
 	int active = 1;
-	int power_button_was_released = 0;
-	int power_button_was_pressed = 0;
+	int button_released = 0;
+	int button_pressed = 0;
 	vb2_error_t result = VBERROR_REBOOT_REQUIRED;
 	int action_confirmed = 0;
 	uint64_t start_time_us;
@@ -273,20 +273,15 @@
 	/* We'll loop until the user decides what to do */
 	do {
 		uint32_t key = VbExKeyboardRead();
-		/*
-		 * VbExIsShutdownRequested() is almost an adequate substitute
-		 * for adding a new flag to VbExGetSwitches().  The main
-		 * issue is that the former doesn't consult the power button
-		 * on detachables, and this function wants to see for itself
-		 * that the power button isn't currently pressed.
-		 */
-		if (VbExGetSwitches(VB_SWITCH_FLAG_PHYS_PRESENCE_PRESSED)) {
+		/* Note that we need to check that the physical presence button
+		   was pressed *and then* released. */
+		if (vb2ex_physical_presence_pressed()) {
 			/* Wait for a release before registering a press. */
-			if (power_button_was_released)
-				power_button_was_pressed = 1;
+			if (button_released)
+				button_pressed = 1;
 		} else {
-			power_button_was_released = 1;
-			if (power_button_was_pressed) {
+			button_released = 1;
+			if (button_pressed) {
 				VB2_DEBUG("vb2_diagnostics_ui() - power released\n");
 				action_confirmed = 1;
 				active = 0;
diff --git a/firmware/stub/vboot_api_stub.c b/firmware/stub/vboot_api_stub.c
index d00229c..a693944 100644
--- a/firmware/stub/vboot_api_stub.c
+++ b/firmware/stub/vboot_api_stub.c
@@ -54,7 +54,7 @@
 	return 0;
 }
 
-uint32_t VbExGetSwitches(uint32_t mask)
+int vb2ex_physical_presence_pressed(void)
 {
 	return 0;
 }
diff --git a/tests/vboot_api_kernel4_tests.c b/tests/vboot_api_kernel4_tests.c
index dd08c2f..b6807c2 100644
--- a/tests/vboot_api_kernel4_tests.c
+++ b/tests/vboot_api_kernel4_tests.c
@@ -42,9 +42,8 @@
 static uint32_t current_recovery_reason;
 static uint32_t expected_recovery_reason;
 
-static uint32_t mock_switches[8];
-static uint32_t mock_switches_count;
-static int mock_switches_are_stuck;
+static uint32_t mock_presence[8];
+static uint32_t mock_presence_count;
 
 static void reset_common_data(void)
 {
@@ -76,9 +75,8 @@
 	current_recovery_reason = 0;
 	expected_recovery_reason = 0;
 
-	memset(mock_switches, 0, sizeof(mock_switches));
-	mock_switches_count = 0;
-	mock_switches_are_stuck = 0;
+	memset(mock_presence, 0, sizeof(mock_presence));
+	mock_presence_count = 0;
 }
 
 static void test_slk(vb2_error_t retval, int recovery_reason, const char *desc)
@@ -168,12 +166,10 @@
 	return vbboot_retval;
 }
 
-uint32_t VbExGetSwitches(uint32_t request_mask)
+int vb2ex_physical_presence_pressed(void)
 {
-	if (mock_switches_are_stuck)
-		return mock_switches[0] & request_mask;
-	if (mock_switches_count < ARRAY_SIZE(mock_switches))
-		return mock_switches[mock_switches_count++] & request_mask;
+	if (mock_presence_count < ARRAY_SIZE(mock_presence))
+		return mock_presence[mock_presence_count++];
 	else
 		return 0;
 }
@@ -240,7 +236,7 @@
 	/* Check that NV_DIAG_REQUEST triggers diagnostic UI */
 	if (DIAGNOSTIC_UI) {
 		reset_common_data();
-		mock_switches[1] = VB_SWITCH_FLAG_PHYS_PRESENCE_PRESSED;
+		mock_presence[1] = 1;
 		vb2_nv_set(ctx, VB2_NV_DIAG_REQUEST, 1);
 		vbboot_retval = -4;
 		test_slk(VB2_ERROR_MOCK, 0,
diff --git a/tests/vboot_legacy_clamshell_tests.c b/tests/vboot_legacy_clamshell_tests.c
index f31adcd..9c7e0c7 100644
--- a/tests/vboot_legacy_clamshell_tests.c
+++ b/tests/vboot_legacy_clamshell_tests.c
@@ -166,19 +166,21 @@
 		return 0;
 }
 
-uint32_t VbExGetSwitches(uint32_t request_mask)
+int vb2ex_physical_presence_pressed(void)
 {
 	uint32_t result = 0;
+
 	if (mock_gpio_count >= ARRAY_SIZE(mock_gpio))
 		return 0;
-	if ((request_mask & VB_SWITCH_FLAG_PHYS_PRESENCE_PRESSED) &&
-	    (mock_gpio[mock_gpio_count].gpio_flags & GPIO_PRESENCE))
-		result |= VB_SWITCH_FLAG_PHYS_PRESENCE_PRESSED;
-	if (mock_gpio[mock_gpio_count].count > 0) {
+
+	if ((mock_gpio[mock_gpio_count].gpio_flags & GPIO_PRESENCE))
+		result = 1;
+
+	if (mock_gpio[mock_gpio_count].count > 0)
 		--mock_gpio[mock_gpio_count].count;
-	} else {
+	else
 		++mock_gpio_count;
-	}
+
 	return result;
 }
 
diff --git a/tests/vboot_legacy_menu_tests.c b/tests/vboot_legacy_menu_tests.c
index a6f1056..a9af7dd 100644
--- a/tests/vboot_legacy_menu_tests.c
+++ b/tests/vboot_legacy_menu_tests.c
@@ -46,9 +46,6 @@
 static uint32_t mock_keypress[64];
 static uint32_t mock_keyflags[64];
 static uint32_t mock_keypress_count;
-static uint32_t mock_switches[8];
-static uint32_t mock_switches_count;
-static int mock_switches_are_stuck;
 static uint32_t screens_displayed[64];
 static uint32_t screens_count = 0;
 static uint32_t beeps_played[64];
@@ -96,10 +93,6 @@
 	memset(mock_keyflags, 0, sizeof(mock_keyflags));
 	mock_keypress_count = 0;
 
-	memset(mock_switches, 0, sizeof(mock_switches));
-	mock_switches_count = 0;
-	mock_switches_are_stuck = 0;
-
 	mock_altfw_mask = 3 << 1;	/* This mask selects 1 and 2 */
 	vbexaltfwmask_called = 0;
 }
@@ -162,16 +155,6 @@
 		return 0;
 }
 
-uint32_t VbExGetSwitches(uint32_t request_mask)
-{
-	if (mock_switches_are_stuck)
-		return mock_switches[0] & request_mask;
-	if (mock_switches_count < ARRAY_SIZE(mock_switches))
-		return mock_switches[mock_switches_count++] & request_mask;
-	else
-		return 0;
-}
-
 vb2_error_t VbExLegacy(enum VbAltFwIndex_t _altfw_num)
 {
 	vbexlegacy_called++;