FastbootCmds: add oem command to enable mainline support

In order to better support non-android operating systems, such
as postmarketOS (@postmarketOS, https://postmarketos.org/), add
a custom oem command {en,dis}able-mainline-support, which can be
used to toggle different behavior to improve UX when using such
systems.

Right now it does not do anything on its own except printing
whether mainline support is enabled or not when executing
`fastboot oem device-info`.

Change-Id: Icf4323e1402e97a9f81045a5f3225385876e0fce
Signed-off-by: Alexander Martinz <amartinz@shiftphones.com>
diff --git a/QcomModulePkg/Include/Library/DeviceInfo.h b/QcomModulePkg/Include/Library/DeviceInfo.h
index f07e81c..523055a 100644
--- a/QcomModulePkg/Include/Library/DeviceInfo.h
+++ b/QcomModulePkg/Include/Library/DeviceInfo.h
@@ -56,6 +56,7 @@
   CHAR8 user_public_key[MAX_USER_KEY_SIZE];
   UINT64 rollback_index[MAX_VB_PARTITIONS];
   CHAR8 Display_Cmdline[MAX_DISPLAY_CMDLINE_LEN];
+  BOOLEAN is_mainline_support_enabled;
 } DeviceInfo;
 
 struct verified_boot_verity_mode {
@@ -72,6 +73,7 @@
 BOOLEAN IsUnlockCritical (VOID);
 BOOLEAN IsEnforcing (VOID);
 BOOLEAN IsChargingScreenEnable (VOID);
+BOOLEAN IsMainlineSupportEnabled (VOID);
 BOOLEAN IsUserPublicKeySet (VOID);
 VOID
 GetBootloaderVersion (CHAR8 *BootloaderVersion, UINT32 Len);
@@ -80,6 +82,8 @@
 EFI_STATUS
 EnableChargingScreen (BOOLEAN IsEnabled);
 EFI_STATUS
+EnableMainlineSupport (BOOLEAN IsEnabled);
+EFI_STATUS
 EnableEnforcingMode (BOOLEAN IsEnabled);
 EFI_STATUS
 SetDeviceUnlockValue (UINT32 Type, BOOLEAN State);
diff --git a/QcomModulePkg/Library/BootLib/DeviceInfo.c b/QcomModulePkg/Library/BootLib/DeviceInfo.c
index da517d0..3e84b84 100644
--- a/QcomModulePkg/Library/BootLib/DeviceInfo.c
+++ b/QcomModulePkg/Library/BootLib/DeviceInfo.c
@@ -57,6 +57,11 @@
   return DevInfo.is_charger_screen_enabled;
 }
 
+BOOLEAN IsMainlineSupportEnabled (VOID)
+{
+  return DevInfo.is_mainline_support_enabled;
+}
+
 BOOLEAN IsUserPublicKeySet (VOID)
 {
   CHAR8 *UserKeyBuffer = NULL;
@@ -109,6 +114,24 @@
 }
 
 EFI_STATUS
+EnableMainlineSupport (BOOLEAN IsEnabled)
+{
+  EFI_STATUS Status = EFI_SUCCESS;
+
+  if (IsMainlineSupportEnabled () != IsEnabled) {
+    DevInfo.is_mainline_support_enabled = IsEnabled;
+    Status = ReadWriteDeviceInfo (WRITE_CONFIG, &DevInfo, sizeof (DevInfo));
+    if (Status != EFI_SUCCESS) {
+      DEBUG ((EFI_D_ERROR, "Error %a mainline support: %r\n",
+              (IsEnabled ? "Enabling" : "Disabling"), Status));
+      return Status;
+    }
+  }
+
+  return Status;
+}
+
+EFI_STATUS
 StoreDisplayCmdLine (CONST CHAR8 *CmdLine, UINT32 CmdLineLen)
 {
   EFI_STATUS Status = EFI_SUCCESS;
diff --git a/QcomModulePkg/Library/FastbootLib/FastbootCmds.c b/QcomModulePkg/Library/FastbootLib/FastbootCmds.c
index bbc4790..0bf6162 100644
--- a/QcomModulePkg/Library/FastbootLib/FastbootCmds.c
+++ b/QcomModulePkg/Library/FastbootLib/FastbootCmds.c
@@ -3011,6 +3011,34 @@
 }
 
 STATIC VOID
+CmdOemEnableMainlineSupport (CONST CHAR8 *Arg, VOID *Data, UINT32 Size)
+{
+  EFI_STATUS Status;
+  DEBUG ((EFI_D_INFO, "Enabling mainline support\n"));
+
+  Status = EnableMainlineSupport (TRUE);
+  if (Status != EFI_SUCCESS) {
+    FastbootFail ("Failed to enable mainline support");
+  } else {
+    FastbootOkay ("");
+  }
+}
+
+STATIC VOID
+CmdOemDisableMainlineSupport (CONST CHAR8 *Arg, VOID *Data, UINT32 Size)
+{
+  EFI_STATUS Status;
+  DEBUG ((EFI_D_INFO, "Disabling mainline support\n"));
+
+  Status = EnableMainlineSupport (FALSE);
+  if (Status != EFI_SUCCESS) {
+    FastbootFail ("Failed to disable mainline support");
+  } else {
+    FastbootOkay ("");
+  }
+}
+
+STATIC VOID
 CmdOemOffModeCharger (CONST CHAR8 *Arg, VOID *Data, UINT32 Size)
 {
   CHAR8 *Ptr = NULL;
@@ -3255,6 +3283,10 @@
                IsUserPublicKeySet () ? "true" : "false");
   FastbootInfo (DeviceInfo);
   WaitForTransferComplete ();
+  AsciiSPrint (DeviceInfo, sizeof (DeviceInfo), "Mainline support enabled: %a",
+               IsMainlineSupportEnabled () ? "true" : "false");
+  FastbootInfo (DeviceInfo);
+  WaitForTransferComplete ();
   FastbootOkay ("");
 }
 
@@ -3698,6 +3730,8 @@
 #endif
       {"oem enable-charger-screen", CmdOemEnableChargerScreen},
       {"oem disable-charger-screen", CmdOemDisableChargerScreen},
+      {"oem enable-mainline-support", CmdOemEnableMainlineSupport},
+      {"oem disable-mainline-support", CmdOemDisableMainlineSupport},
       {"oem off-mode-charge", CmdOemOffModeCharger},
       {"oem select-display-panel", CmdOemSelectDisplayPanel},
       {"oem device-info", CmdOemDevinfo},