UPSTREAM: cros-ec: adding stay-in-ro command if EC is in bootloader

(cherry picked from commit b48cd9fea6a045341ca4264479bf66543545605b)

BUG=b:238373742
TEST=emerge works; run the updater script on prism board manually

Change-Id: Ie508ff3169d63ca2685763b39f375a6e4b54b56c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/fwupd/+/3816713
Reviewed-by: Benson Leung <bleung@google.com>
Tested-by: Boris Mittelberg <bmbm@google.com>
Reviewed-by: Sean Paul <sean@poorly.run>
Commit-Queue: Boris Mittelberg <bmbm@google.com>
diff --git a/plugins/cros-ec/fu-cros-ec-usb-device.c b/plugins/cros-ec/fu-cros-ec-usb-device.c
index f973f27..9997ede 100644
--- a/plugins/cros-ec/fu-cros-ec-usb-device.c
+++ b/plugins/cros-ec/fu-cros-ec-usb-device.c
@@ -769,6 +769,29 @@
 }
 
 static gboolean
+fu_cros_ec_usb_device_stay_in_ro(FuDevice *device, GError **error)
+{
+	gsize response_size = 1;
+	guint8 response;
+	guint16 subcommand = UPDATE_EXTRA_CMD_STAY_IN_RO;
+	guint8 command_body[2]; /* Max command body size. */
+	gsize command_body_size = 0;
+
+	if (!fu_cros_ec_usb_device_send_subcommand(device,
+						   subcommand,
+						   command_body,
+						   command_body_size,
+						   &response,
+						   &response_size,
+						   FALSE,
+						   error)) {
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+static gboolean
 fu_cros_ec_usb_device_write_firmware(FuDevice *device,
 				     FuFirmware *firmware,
 				     FuProgress *progress,
@@ -782,22 +805,10 @@
 	fu_device_remove_private_flag(device, FU_CROS_EC_USB_DEVICE_FLAG_SPECIAL);
 
 	if (fu_device_has_private_flag(device, FU_CROS_EC_USB_DEVICE_FLAG_REBOOTING_TO_RO)) {
-		gsize response_size = 1;
-		guint8 response;
-		guint16 subcommand = UPDATE_EXTRA_CMD_STAY_IN_RO;
-		guint8 command_body[2]; /* Max command body size. */
-		gsize command_body_size = 0;
 		START_RESP start_resp;
 
 		fu_device_remove_private_flag(device, FU_CROS_EC_USB_DEVICE_FLAG_REBOOTING_TO_RO);
-		if (!fu_cros_ec_usb_device_send_subcommand(device,
-							   subcommand,
-							   command_body,
-							   command_body_size,
-							   &response,
-							   &response_size,
-							   FALSE,
-							   error)) {
+		if (!fu_cros_ec_usb_device_stay_in_ro(device, error)) {
 			g_prefix_error(error, "failed to send stay-in-ro subcommand: ");
 			return FALSE;
 		}
@@ -964,6 +975,8 @@
 		return TRUE;
 
 	if (self->in_bootloader) {
+		/* If EC just rebooted - prevent jumping to RW during the update */
+		fu_device_add_private_flag(device, FU_CROS_EC_USB_DEVICE_FLAG_REBOOTING_TO_RO);
 		g_debug("skipping immediate reboot in case of already in bootloader");
 		/* in RO so skip reboot */
 		return TRUE;