]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePeCoffLib/Arm/PeCoffLoaderEx.c
Update inappropriate comments.
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / Arm / PeCoffLoaderEx.c
index ce834d4df3bed14d47e4a6f824bf0081bcdf6d62..d6bf42738d2be7ed6be09ec7a24da3dfa999b4a8 100644 (file)
@@ -1,12 +1,12 @@
 /** @file\r
   Specific relocation fixups for ARM architecture.\r
 \r
-  Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
-  Portions copyright (c) 2008-2009 Apple Inc.<BR>\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
+  Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\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
+  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
 **/\r
 \r
 #include "BasePeCoffLibInternals.h"\r
+#include <Library/BaseLib.h>\r
 \r
 \r
+/**\r
+  Pass in a pointer to an ARM MOVT or MOVW immediate instruciton and \r
+  return the immediate data encoded in the instruction.\r
+\r
+  @param  Instruction   Pointer to ARM MOVT or MOVW immediate instruction\r
+\r
+  @return Immediate address encoded in the instruction\r
+\r
+**/\r
+UINT16\r
+ThumbMovtImmediateAddress (\r
+  IN UINT16 *Instruction\r
+  )\r
+{\r
+  UINT32  Movt;\r
+  UINT16  Address;\r
+  \r
+  // Thumb2 is two 16-bit instructions working together. Not a single 32-bit instruction\r
+  // Example MOVT R0, #0 is 0x0000f2c0 or 0xf2c0 0x0000\r
+  Movt = (*Instruction << 16) | (*(Instruction + 1)); \r
+\r
+  // imm16 = imm4:i:imm3:imm8\r
+  //         imm4 -> Bit19:Bit16\r
+  //         i    -> Bit26\r
+  //         imm3 -> Bit14:Bit12\r
+  //         imm8 -> Bit7:Bit0\r
+  Address  = (UINT16)(Movt & 0x000000ff);         // imm8\r
+  Address |= (UINT16)((Movt >> 4) &  0x0000f700); // imm4 imm3\r
+  Address |= (((Movt & BIT26) != 0) ? BIT11 : 0); // i\r
+  return Address;\r
+}\r
+\r
+\r
+/**\r
+  Update an ARM MOVT or MOVW immediate instruction immediate data.\r
+\r
+  @param  Instruction   Pointer to ARM MOVT or MOVW immediate instruction\r
+  @param  Address       New addres to patch into the instruction\r
+**/\r
+VOID\r
+ThumbMovtImmediatePatch (\r
+  IN OUT UINT16 *Instruction,\r
+  IN     UINT16 Address\r
+  )\r
+{\r
+  UINT16  Patch;\r
+\r
+  // First 16-bit chunk of instruciton\r
+  Patch  = ((Address >> 12) & 0x000f);            // imm4 \r
+  Patch |= (((Address & BIT11) != 0) ? BIT10 : 0); // i\r
+  // Mask out instruction bits and or in address\r
+  *(Instruction) = (*Instruction & ~0x040f) | Patch;\r
+\r
+  // Second 16-bit chunk of instruction\r
+  Patch  =  Address & 0x000000ff;          // imm8\r
+  Patch |=  ((Address << 4) & 0x00007000); // imm3\r
+  // Mask out instruction bits and or in address\r
+  Instruction++;\r
+  *Instruction = (*Instruction & ~0x70ff) | Patch;\r
+}\r
+\r
+\r
+\r
+/**\r
+  Pass in a pointer to an ARM MOVW/MOVT instruciton pair and \r
+  return the immediate data encoded in the two` instruction.\r
+\r
+  @param  Instructions  Pointer to ARM MOVW/MOVT insturction pair\r
+\r
+  @return Immediate address encoded in the instructions\r
+\r
+**/\r
+UINT32\r
+ThumbMovwMovtImmediateAddress (\r
+  IN UINT16 *Instructions\r
+  )\r
+{\r
+  UINT16  *Word;\r
+  UINT16  *Top;\r
+  \r
+  Word = Instructions;  // MOVW\r
+  Top  = Word + 2;      // MOVT\r
+  \r
+  return (ThumbMovtImmediateAddress (Top) << 16) + ThumbMovtImmediateAddress (Word);\r
+}\r
+\r
+\r
+/**\r
+  Update an ARM MOVW/MOVT immediate instruction instruction pair.\r
+\r
+  @param  Instructions  Pointer to ARM MOVW/MOVT instruction pair\r
+  @param  Address       New addres to patch into the instructions\r
+**/\r
+VOID\r
+ThumbMovwMovtImmediatePatch (\r
+  IN OUT UINT16 *Instructions,\r
+  IN     UINT32 Address\r
+  )\r
+{\r
+  UINT16  *Word;\r
+  UINT16  *Top;\r
+  \r
+  Word = Instructions;  // MOVW\r
+  Top  = Word + 2;      // MOVT\r
+\r
+  ThumbMovtImmediatePatch (Word, (UINT16)(Address & 0xffff));\r
+  ThumbMovtImmediatePatch (Top, (UINT16)(Address >> 16));\r
+}\r
+  \r
+  \r
+\r
 /**\r
   Performs an ARM-based specific relocation fixup and is a no-op on other\r
   instruction sets.\r
 \r
-  @param  Reloc       Pointer to the relocation record.\r
-  @param  Fixup       Pointer to the address to fix up.\r
-  @param  FixupData   Pointer to a buffer to log the fixups.\r
+  @param  Reloc       The pointer to the relocation record.\r
+  @param  Fixup       The pointer to the address to fix up.\r
+  @param  FixupData   The pointer to a buffer to log the fixups.\r
   @param  Adjust      The offset to adjust the fixup.\r
 \r
   @return Status code.\r
@@ -36,7 +148,33 @@ PeCoffLoaderRelocateImageEx (
   IN UINT64      Adjust\r
   )\r
 {\r
-  return RETURN_UNSUPPORTED;\r
+  UINT16      *Fixup16;\r
+  UINT32      FixupVal;\r
+\r
+  Fixup16   = (UINT16 *) Fixup;\r
+\r
+  switch ((*Reloc) >> 12) {\r
+  \r
+  case EFI_IMAGE_REL_BASED_ARM_MOV32T:\r
+    FixupVal = ThumbMovwMovtImmediateAddress (Fixup16) + (UINT32)Adjust;\r
+    ThumbMovwMovtImmediatePatch (Fixup16, FixupVal);\r
+\r
+    if (*FixupData != NULL) {\r
+      *FixupData = ALIGN_POINTER(*FixupData, sizeof(UINT64));\r
+      // Fixup16 is not aligned so we must copy it. Thumb instructions are streams of 16 bytes. \r
+      CopyMem (*FixupData, Fixup16, sizeof (UINT64));\r
+      *FixupData = *FixupData + sizeof(UINT64);\r
+    }\r
+    break;\r
+  \r
+  case EFI_IMAGE_REL_BASED_ARM_MOV32A:\r
+     ASSERT (FALSE);\r
+     // break omitted - ARM instruction encoding not implemented\r
+  default:\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -45,8 +183,6 @@ PeCoffLoaderRelocateImageEx (
   loading and relocating of the image type. It's up to the caller to support\r
   the entry point.\r
   \r
-  The IA32/X64 version PE/COFF loader/relocater both support IA32, X64 and EBC images.\r
-\r
   @param  Machine   Machine type from the PE Header.\r
 \r
   @return TRUE if this PE/COFF loader can load the image\r
@@ -69,9 +205,9 @@ PeCoffLoaderImageFormatSupported (
   instruction sets. This is used to re-relocated the image into the EFI virtual\r
   space for runtime calls.\r
 \r
-  @param  Reloc       Pointer to the relocation record.\r
-  @param  Fixup       Pointer to the address to fix up.\r
-  @param  FixupData   Pointer to a buffer to log the fixups.\r
+  @param  Reloc       The pointer to the relocation record.\r
+  @param  Fixup       The pointer to the address to fix up.\r
+  @param  FixupData   The pointer to a buffer to log the fixups.\r
   @param  Adjust      The offset to adjust the fixup.\r
 \r
   @return Status code.\r
@@ -85,6 +221,29 @@ PeHotRelocateImageEx (
   IN UINT64      Adjust\r
   )\r
 {\r
-  return RETURN_UNSUPPORTED;\r
+  UINT16  *Fixup16;\r
+  UINT32  FixupVal;\r
+\r
+  Fixup16 = (UINT16 *)Fixup;\r
+\r
+  switch ((*Reloc) >> 12) {\r
+\r
+  case EFI_IMAGE_REL_BASED_ARM_MOV32T:\r
+    *FixupData  = ALIGN_POINTER (*FixupData, sizeof (UINT64));\r
+    if (*(UINT64 *) (*FixupData) == ReadUnaligned64 ((UINT64 *)Fixup16)) {\r
+      FixupVal = ThumbMovwMovtImmediateAddress (Fixup16) + (UINT32)Adjust;\r
+      ThumbMovwMovtImmediatePatch (Fixup16, FixupVal);\r
+    }\r
+    break;\r
+  \r
+  case EFI_IMAGE_REL_BASED_ARM_MOV32A:\r
+    ASSERT (FALSE);\r
+    // break omitted - ARM instruction encoding not implemented\r
+  default:\r
+    DEBUG ((EFI_D_ERROR, "PeHotRelocateEx:unknown fixed type\n"));\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+  \r
+  return RETURN_SUCCESS;\r
 }\r
 \r