]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EmbeddedPkg/Application/AndroidFastboot/AndroidFastbootApp.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / Application / AndroidFastboot / AndroidFastbootApp.c
index 9ddc34f57cf4448a86926dc9270d95c7d8a83520..b16478bea04b74c4cc139dff17c8db2d33d0abff 100644 (file)
@@ -2,13 +2,7 @@
 \r
   Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>\r
 \r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
  * FASTBOOT_PLATFORM_PROTOCOL to implement the Android Fastboot protocol.\r
  */\r
 \r
-STATIC FASTBOOT_TRANSPORT_PROTOCOL *mTransport;\r
-STATIC FASTBOOT_PLATFORM_PROTOCOL  *mPlatform;\r
+STATIC FASTBOOT_TRANSPORT_PROTOCOL  *mTransport;\r
+STATIC FASTBOOT_PLATFORM_PROTOCOL   *mPlatform;\r
 \r
-STATIC EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *mTextOut;\r
+STATIC EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *mTextOut;\r
 \r
 typedef enum {\r
   ExpectCmdState,\r
@@ -42,45 +36,45 @@ typedef enum {
   FastbootStateMax\r
 } ANDROID_FASTBOOT_STATE;\r
 \r
-STATIC ANDROID_FASTBOOT_STATE mState = ExpectCmdState;\r
+STATIC ANDROID_FASTBOOT_STATE  mState = ExpectCmdState;\r
 \r
 // When in ExpectDataState, the number of bytes of data to expect:\r
-STATIC UINT64 mNumDataBytes;\r
+STATIC UINT64  mNumDataBytes;\r
 // .. and the number of bytes so far received this data phase\r
-STATIC UINT64 mBytesReceivedSoFar;\r
+STATIC UINT64  mBytesReceivedSoFar;\r
 // .. and the buffer to save data into\r
-STATIC UINT8 *mDataBuffer = NULL;\r
+STATIC UINT8  *mDataBuffer = NULL;\r
 \r
 // Event notify functions, from which gBS->Exit shouldn't be called, can signal\r
 // this event when the application should exit\r
-STATIC EFI_EVENT mFinishedEvent;\r
+STATIC EFI_EVENT  mFinishedEvent;\r
 \r
-STATIC EFI_EVENT mFatalSendErrorEvent;\r
+STATIC EFI_EVENT  mFatalSendErrorEvent;\r
 \r
 // This macro uses sizeof - only use it on arrays (i.e. string literals)\r
-#define SEND_LITERAL(Str) mTransport->Send (                  \\r
+#define SEND_LITERAL(Str)            mTransport->Send (       \\r
                                         sizeof (Str) - 1,     \\r
                                         Str,                  \\r
                                         &mFatalSendErrorEvent \\r
                                         )\r
-#define MATCH_CMD_LITERAL(Cmd, Buf) !AsciiStrnCmp (Cmd, Buf, sizeof (Cmd) - 1)\r
+#define MATCH_CMD_LITERAL(Cmd, Buf)  !AsciiStrnCmp (Cmd, Buf, sizeof (Cmd) - 1)\r
 \r
-#define IS_LOWERCASE_ASCII(Char) (Char >= 'a' && Char <= 'z')\r
+#define IS_LOWERCASE_ASCII(Char)  (Char >= 'a' && Char <= 'z')\r
 \r
-#define FASTBOOT_STRING_MAX_LENGTH  256\r
-#define FASTBOOT_COMMAND_MAX_LENGTH 64\r
+#define FASTBOOT_STRING_MAX_LENGTH   256\r
+#define FASTBOOT_COMMAND_MAX_LENGTH  64\r
 \r
 STATIC\r
 VOID\r
 HandleGetVar (\r
-  IN CHAR8 *CmdArg\r
+  IN CHAR8  *CmdArg\r
   )\r
 {\r
-  CHAR8      Response[FASTBOOT_COMMAND_MAX_LENGTH + 1] = "OKAY";\r
-  EFI_STATUS Status;\r
+  CHAR8       Response[FASTBOOT_COMMAND_MAX_LENGTH + 1] = "OKAY";\r
+  EFI_STATUS  Status;\r
 \r
   // Respond to getvar:version with 0.4 (version of Fastboot protocol)\r
-  if (!AsciiStrnCmp ("version", CmdArg, sizeof ("version") - 1 )) {\r
+  if (!AsciiStrnCmp ("version", CmdArg, sizeof ("version") - 1)) {\r
     SEND_LITERAL ("OKAY" ANDROID_FASTBOOT_VERSION);\r
   } else {\r
     // All other variables are assumed to be platform specific\r
@@ -96,11 +90,11 @@ HandleGetVar (
 STATIC\r
 VOID\r
 HandleDownload (\r
-  IN CHAR8 *NumBytesString\r
+  IN CHAR8  *NumBytesString\r
   )\r
 {\r
-  CHAR8       Response[12] = "DATA";\r
-  CHAR16      OutputString[FASTBOOT_STRING_MAX_LENGTH];\r
+  CHAR8   Response[13];\r
+  CHAR16  OutputString[FASTBOOT_STRING_MAX_LENGTH];\r
 \r
   // Argument is 8-character ASCII string hex representation of number of bytes\r
   // that will be sent in the data phase.\r
@@ -127,10 +121,16 @@ HandleDownload (
   if (mDataBuffer == NULL) {\r
     SEND_LITERAL ("FAILNot enough memory");\r
   } else {\r
-    AsciiStrnCpy (Response + 4, NumBytesString, 8);\r
-    mTransport->Send (sizeof(Response), Response, &mFatalSendErrorEvent);\r
+    ZeroMem (Response, sizeof Response);\r
+    AsciiSPrint (\r
+      Response,\r
+      sizeof Response,\r
+      "DATA%x",\r
+      (UINT32)mNumDataBytes\r
+      );\r
+    mTransport->Send (sizeof Response - 1, Response, &mFatalSendErrorEvent);\r
 \r
-    mState = ExpectDataState;\r
+    mState              = ExpectDataState;\r
     mBytesReceivedSoFar = 0;\r
   }\r
 }\r
@@ -138,7 +138,7 @@ HandleDownload (
 STATIC\r
 VOID\r
 HandleFlash (\r
-  IN CHAR8 *PartitionName\r
+  IN CHAR8  *PartitionName\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -165,7 +165,7 @@ HandleFlash (
   } else if (EFI_ERROR (Status)) {\r
     SEND_LITERAL ("FAILError flashing partition.");\r
     mTextOut->OutputString (mTextOut, L"Error flashing partition.\r\n");\r
-    DEBUG ((EFI_D_ERROR, "Couldn't flash image:  %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Couldn't flash image:  %r\n", Status));\r
   } else {\r
     mTextOut->OutputString (mTextOut, L"Done.\r\n");\r
     SEND_LITERAL ("OKAY");\r
@@ -175,7 +175,7 @@ HandleFlash (
 STATIC\r
 VOID\r
 HandleErase (\r
-  IN CHAR8 *PartitionName\r
+  IN CHAR8  *PartitionName\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -188,7 +188,7 @@ HandleErase (
   Status = mPlatform->ErasePartition (PartitionName);\r
   if (EFI_ERROR (Status)) {\r
     SEND_LITERAL ("FAILCheck device console.");\r
-    DEBUG ((EFI_D_ERROR, "Couldn't erase image:  %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Couldn't erase image:  %r\n", Status));\r
   } else {\r
     SEND_LITERAL ("OKAY");\r
   }\r
@@ -200,7 +200,7 @@ HandleBoot (
   VOID\r
   )\r
 {\r
-  EFI_STATUS Status;\r
+  EFI_STATUS  Status;\r
 \r
   mTextOut->OutputString (mTextOut, L"Booting downloaded image\r\n");\r
 \r
@@ -216,15 +216,16 @@ HandleBoot (
 \r
   Status = BootAndroidBootImg (mNumDataBytes, mDataBuffer);\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "Failed to boot downloaded image: %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Failed to boot downloaded image: %r\n", Status));\r
   }\r
+\r
   // We shouldn't get here\r
 }\r
 \r
 STATIC\r
 VOID\r
 HandleOemCommand (\r
-  IN CHAR8 *Command\r
+  IN CHAR8  *Command\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -245,10 +246,10 @@ STATIC
 VOID\r
 AcceptCmd (\r
   IN        UINTN  Size,\r
-  IN  CONST CHAR8 *Data\r
+  IN  CONST CHAR8  *Data\r
   )\r
 {\r
-  CHAR8       Command[FASTBOOT_COMMAND_MAX_LENGTH + 1];\r
+  CHAR8  Command[FASTBOOT_COMMAND_MAX_LENGTH + 1];\r
 \r
   // Max command size is 64 bytes\r
   if (Size > FASTBOOT_COMMAND_MAX_LENGTH) {\r
@@ -257,8 +258,7 @@ AcceptCmd (
   }\r
 \r
   // Commands aren't null-terminated. Let's get a null-terminated version.\r
-  AsciiStrnCpy (Command, Data, Size);\r
-  Command[Size] = '\0';\r
+  AsciiStrnCpyS (Command, sizeof Command, Data, Size);\r
 \r
   // Parse command\r
   if (MATCH_CMD_LITERAL ("getvar", Command)) {\r
@@ -287,17 +287,18 @@ AcceptCmd (
       // Here we just reboot normally.\r
       SEND_LITERAL ("INFOreboot-bootloader not supported, rebooting normally.");\r
     }\r
+\r
     SEND_LITERAL ("OKAY");\r
     gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
 \r
     // Shouldn't get here\r
-    DEBUG ((EFI_D_ERROR, "Fastboot: gRT->ResetSystem didn't work\n"));\r
+    DEBUG ((DEBUG_ERROR, "Fastboot: gRT->ResetSystem didn't work\n"));\r
   } else if (MATCH_CMD_LITERAL ("powerdown", Command)) {\r
     SEND_LITERAL ("OKAY");\r
     gRT->ResetSystem (EfiResetShutdown, EFI_SUCCESS, 0, NULL);\r
 \r
     // Shouldn't get here\r
-    DEBUG ((EFI_D_ERROR, "Fastboot: gRT->ResetSystem didn't work\n"));\r
+    DEBUG ((DEBUG_ERROR, "Fastboot: gRT->ResetSystem didn't work\n"));\r
   } else if (MATCH_CMD_LITERAL ("oem", Command)) {\r
     // The "oem" command isn't in the specification, but it was observed in the\r
     // wild, followed by a space, followed by the actual command.\r
@@ -305,7 +306,7 @@ AcceptCmd (
   } else if (IS_LOWERCASE_ASCII (Command[0])) {\r
     // Commands starting with lowercase ASCII characters are reserved for the\r
     // Fastboot protocol. If we don't recognise it, it's probably the future\r
-    // and there are new commmands in the protocol.\r
+    // and there are new commands in the protocol.\r
     // (By the way, the "oem" command mentioned above makes this reservation\r
     //  redundant, but we handle it here to be spec-compliant)\r
     SEND_LITERAL ("FAILCommand not recognised. Check Fastboot version.");\r
@@ -318,12 +319,12 @@ STATIC
 VOID\r
 AcceptData (\r
   IN  UINTN  Size,\r
-  IN  VOID  *Data\r
+  IN  VOID   *Data\r
   )\r
 {\r
-  UINT32 RemainingBytes = mNumDataBytes - mBytesReceivedSoFar;\r
-  CHAR16 OutputString[FASTBOOT_STRING_MAX_LENGTH];\r
-  STATIC UINTN Count = 0;\r
+  UINT32        RemainingBytes = mNumDataBytes - mBytesReceivedSoFar;\r
+  CHAR16        OutputString[FASTBOOT_STRING_MAX_LENGTH];\r
+  STATIC UINTN  Count = 0;\r
 \r
   // Protocol doesn't say anything about sending extra data so just ignore it.\r
   if (Size > RemainingBytes) {\r
@@ -336,7 +337,7 @@ AcceptData (
 \r
   // Show download progress. Don't do it for every packet  as outputting text\r
   // might be time consuming - do it on the last packet and on every 32nd packet\r
-  if ((Count++ % 32) == 0 || Size == RemainingBytes) {\r
+  if (((Count++ % 32) == 0) || (Size == RemainingBytes)) {\r
     // (Note no newline in format string - it will overwrite the line each time)\r
     UnicodeSPrint (\r
       OutputString,\r
@@ -368,23 +369,24 @@ STATIC
 VOID\r
 DataReady (\r
   IN EFI_EVENT  Event,\r
-  IN VOID      *Context\r
+  IN VOID       *Context\r
   )\r
 {\r
   UINTN       Size;\r
-  VOID       *Data;\r
+  VOID        *Data;\r
   EFI_STATUS  Status;\r
 \r
   do {\r
     Status = mTransport->Receive (&Size, &Data);\r
     if (!EFI_ERROR (Status)) {\r
       if (mState == ExpectCmdState) {\r
-        AcceptCmd (Size, (CHAR8 *) Data);\r
+        AcceptCmd (Size, (CHAR8 *)Data);\r
       } else if (mState == ExpectDataState) {\r
         AcceptData (Size, Data);\r
       } else {\r
         ASSERT (FALSE);\r
       }\r
+\r
       FreePool (Data);\r
     }\r
   } while (!EFI_ERROR (Status));\r
@@ -406,7 +408,7 @@ STATIC
 VOID\r
 FatalErrorNotify (\r
   IN EFI_EVENT  Event,\r
-  IN VOID      *Context\r
+  IN VOID       *Context\r
   )\r
 {\r
   mTextOut->OutputString (mTextOut, L"Fatal error sending command response. Exiting.\r\n");\r
@@ -416,58 +418,61 @@ FatalErrorNotify (
 EFI_STATUS\r
 EFIAPI\r
 FastbootAppEntryPoint (\r
-  IN EFI_HANDLE                            ImageHandle,\r
-  IN EFI_SYSTEM_TABLE                      *SystemTable\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
   EFI_STATUS                      Status;\r
   EFI_EVENT                       ReceiveEvent;\r
   EFI_EVENT                       WaitEventArray[2];\r
   UINTN                           EventIndex;\r
-  EFI_SIMPLE_TEXT_INPUT_PROTOCOL *TextIn;\r
+  EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *TextIn;\r
+  EFI_INPUT_KEY                   Key;\r
 \r
   mDataBuffer = NULL;\r
 \r
   Status = gBS->LocateProtocol (\r
-    &gAndroidFastbootTransportProtocolGuid,\r
-    NULL,\r
-    (VOID **) &mTransport\r
-    );\r
+                  &gAndroidFastbootTransportProtocolGuid,\r
+                  NULL,\r
+                  (VOID **)&mTransport\r
+                  );\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "Fastboot: Couldn't open Fastboot Transport Protocol: %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Fastboot: Couldn't open Fastboot Transport Protocol: %r\n", Status));\r
     return Status;\r
   }\r
 \r
-  Status = gBS->LocateProtocol (&gAndroidFastbootPlatformProtocolGuid, NULL, (VOID **) &mPlatform);\r
+  Status = gBS->LocateProtocol (&gAndroidFastbootPlatformProtocolGuid, NULL, (VOID **)&mPlatform);\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "Fastboot: Couldn't open Fastboot Platform Protocol: %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Fastboot: Couldn't open Fastboot Platform Protocol: %r\n", Status));\r
     return Status;\r
   }\r
 \r
   Status = mPlatform->Init ();\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "Fastboot: Couldn't initialise Fastboot Platform Protocol: %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Fastboot: Couldn't initialise Fastboot Platform Protocol: %r\n", Status));\r
     return Status;\r
   }\r
 \r
-  Status = gBS->LocateProtocol (&gEfiSimpleTextOutProtocolGuid, NULL, (VOID **) &mTextOut);\r
+  Status = gBS->LocateProtocol (&gEfiSimpleTextOutProtocolGuid, NULL, (VOID **)&mTextOut);\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR,\r
-      "Fastboot: Couldn't open Text Output Protocol: %r\n", Status\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "Fastboot: Couldn't open Text Output Protocol: %r\n",\r
+      Status\r
       ));\r
     return Status;\r
   }\r
 \r
-  Status = gBS->LocateProtocol (&gEfiSimpleTextInProtocolGuid, NULL, (VOID **) &TextIn);\r
+  Status = gBS->LocateProtocol (&gEfiSimpleTextInProtocolGuid, NULL, (VOID **)&TextIn);\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "Fastboot: Couldn't open Text Input Protocol: %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Fastboot: Couldn't open Text Input Protocol: %r\n", Status));\r
     return Status;\r
   }\r
 \r
   // Disable watchdog\r
   Status = gBS->SetWatchdogTimer (0, 0x10000, 0, NULL);\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "Fastboot: Couldn't disable watchdog timer: %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Fastboot: Couldn't disable watchdog timer: %r\n", Status));\r
   }\r
 \r
   // Create event for receipt of data from the host\r
@@ -487,37 +492,49 @@ FastbootAppEntryPoint (
   // Create event to pass to FASTBOOT_TRANSPORT_PROTOCOL.Send, signalling a\r
   // fatal error\r
   Status = gBS->CreateEvent (\r
-                 EVT_NOTIFY_SIGNAL,\r
-                 TPL_CALLBACK,\r
-                 FatalErrorNotify,\r
-                 NULL,\r
-                 &mFatalSendErrorEvent\r
-                 );\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  FatalErrorNotify,\r
+                  NULL,\r
+                  &mFatalSendErrorEvent\r
+                  );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-\r
   // Start listening for data\r
   Status = mTransport->Start (\r
-    ReceiveEvent\r
-    );\r
+                         ReceiveEvent\r
+                         );\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "Fastboot: Couldn't start transport: %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Fastboot: Couldn't start transport: %r\n", Status));\r
     return Status;\r
   }\r
 \r
   // Talk to the user\r
-  mTextOut->OutputString (mTextOut,\r
-      L"Android Fastboot mode - version " ANDROID_FASTBOOT_VERSION ". Press any key to quit.\r\n");\r
+  mTextOut->OutputString (\r
+              mTextOut,\r
+              L"Android Fastboot mode - version " ANDROID_FASTBOOT_VERSION ". Press RETURN or SPACE key to quit.\r\n"\r
+              );\r
 \r
   // Quit when the user presses any key, or mFinishedEvent is signalled\r
   WaitEventArray[0] = mFinishedEvent;\r
   WaitEventArray[1] = TextIn->WaitForKey;\r
-  gBS->WaitForEvent (2, WaitEventArray, &EventIndex);\r
+  while (1) {\r
+    gBS->WaitForEvent (2, WaitEventArray, &EventIndex);\r
+    Status = TextIn->ReadKeyStroke (gST->ConIn, &Key);\r
+    if (Key.ScanCode == SCAN_NULL) {\r
+      if ((Key.UnicodeChar == CHAR_CARRIAGE_RETURN) ||\r
+          (Key.UnicodeChar == L' '))\r
+      {\r
+        break;\r
+      }\r
+    }\r
+  }\r
 \r
   mTransport->Stop ();\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "Warning: Fastboot Transport Stop: %r\n", Status));\r
+    DEBUG ((DEBUG_ERROR, "Warning: Fastboot Transport Stop: %r\n", Status));\r
   }\r
+\r
   mPlatform->UnInit ();\r
 \r
   return EFI_SUCCESS;\r