]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Add runtime checking for EdkFvbServiceLib.
authorklu2 <klu2@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 20 Dec 2006 03:58:26 +0000 (03:58 +0000)
committerklu2 <klu2@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 20 Dec 2006 03:58:26 +0000 (03:58 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@2121 6f19259b-4bc3-4df7-8a09-765794883524

EdkModulePkg/Library/EdkFvbServiceLib/Fvb.h
EdkModulePkg/Library/EdkFvbServiceLib/Ia32/Fvb.c
EdkModulePkg/Library/EdkFvbServiceLib/x64/Fvb.c

index c6d1eb71932a15f8417ca62e89e078dcd7f493e7..3a0cd507d95ec9ca64e06c7d4bd67e8f769094e5 100644 (file)
@@ -1,21 +1,13 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
+/**\r
+  The internal header file for EdkFvbServiceLib.\r
+  \r
+Copyright (c) 2006 Intel Corporation. <BR>\r
 All rights reserved. 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
 All rights reserved. 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
-\r
-Module Name:\r
-\r
-  Fvb.h\r
-\r
-Abstract:\r
-\r
---*/\r
+**/\r
 \r
 #ifndef __FVB_H__\r
 #define __FVB_H__\r
 \r
 #ifndef __FVB_H__\r
 #define __FVB_H__\r
@@ -26,6 +18,7 @@ typedef struct {
   EFI_HANDLE                          Handle;\r
   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
   EFI_FVB_EXTENSION_PROTOCOL          *FvbExtension;\r
   EFI_HANDLE                          Handle;\r
   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;\r
   EFI_FVB_EXTENSION_PROTOCOL          *FvbExtension;\r
+  BOOLEAN                             IsRuntimeAccess;\r
 } FVB_ENTRY;\r
 \r
 #endif\r
 } FVB_ENTRY;\r
 \r
 #endif\r
index 04693eb6322b184c3d448c2ee2275cd89144be6c..c94c8242ac1419bb8666ce582c7888eecf76dd1d 100644 (file)
@@ -1,19 +1,4 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. 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
-\r
-Module Name:\r
-\r
-    Fvb.c \r
-\r
-Abstract:\r
+/**@file\r
 \r
   Firmware Volume Block Protocol Runtime Abstraction\r
 \r
 \r
   Firmware Volume Block Protocol Runtime Abstraction\r
 \r
@@ -23,11 +8,17 @@ Abstract:
   the protocol has been reinstalled and it needs updateing.\r
 \r
   If you are using any of these lib functions.you must first call FvbInitialize ().\r
   the protocol has been reinstalled and it needs updateing.\r
 \r
   If you are using any of these lib functions.you must first call FvbInitialize ().\r
+  \r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. 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
 \r
 \r
-Key:\r
-  FVB - Firmware Volume Block\r
-\r
---*/\r
+**/\r
 \r
 #include "Fvb.h"\r
 \r
 \r
 #include "Fvb.h"\r
 \r
@@ -39,6 +30,105 @@ STATIC EFI_EVENT          mFvbRegistration;
 STATIC BOOLEAN            mEfiFvbInitialized        = FALSE;\r
 STATIC UINTN              mFvbCount;\r
 \r
 STATIC BOOLEAN            mEfiFvbInitialized        = FALSE;\r
 STATIC UINTN              mFvbCount;\r
 \r
+/**\r
+  Check whether an address is runtime memory or not.\r
\r
+  @param    Address   The Address being checked.\r
+  \r
+  @retval   TRUE      The address is runtime memory.\r
+  @retval   FALSE     The address is not runtime memory.\r
+**/\r
+BOOLEAN\r
+IsRuntimeMemory (\r
+  IN VOID   *Address\r
+  )\r
+{\r
+  EFI_STATUS                           Status;\r
+  UINT8                                TmpMemoryMap[1];\r
+  UINTN                                MapKey;\r
+  UINTN                                DescriptorSize;\r
+  UINT32                               DescriptorVersion;\r
+  UINTN                                MemoryMapSize;\r
+  EFI_MEMORY_DESCRIPTOR                *MemoryMap;\r
+  EFI_MEMORY_DESCRIPTOR                *MemoryMapPtr;\r
+  BOOLEAN                              IsRuntime;\r
+  UINTN                                Index;\r
+\r
+  IsRuntime = FALSE;\r
+\r
+  //\r
+  // Get System MemoryMapSize\r
+  //\r
+  MemoryMapSize = 1;\r
+  Status = gBS->GetMemoryMap (\r
+                  &MemoryMapSize,\r
+                  (EFI_MEMORY_DESCRIPTOR *)TmpMemoryMap,\r
+                  &MapKey,\r
+                  &DescriptorSize,\r
+                  &DescriptorVersion\r
+                  );\r
+  ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
+  //\r
+  // Enlarge space here, because we will allocate pool now.\r
+  //\r
+  MemoryMapSize += EFI_PAGE_SIZE;\r
+  Status = gBS->AllocatePool (\r
+                  EfiBootServicesData,\r
+                  MemoryMapSize,\r
+                  (VOID**)&MemoryMap\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Get System MemoryMap\r
+  //\r
+  Status = gBS->GetMemoryMap (\r
+                  &MemoryMapSize,\r
+                  MemoryMap,\r
+                  &MapKey,\r
+                  &DescriptorSize,\r
+                  &DescriptorVersion\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  MemoryMapPtr = MemoryMap;\r
+  //\r
+  // Search the request Address\r
+  //\r
+  for (Index = 0; Index < (MemoryMapSize / DescriptorSize); Index++) {\r
+    if (((EFI_PHYSICAL_ADDRESS)(UINTN)Address >= MemoryMap->PhysicalStart) &&\r
+        ((EFI_PHYSICAL_ADDRESS)(UINTN)Address < MemoryMap->PhysicalStart\r
+                                              + LShiftU64 (MemoryMap->NumberOfPages, EFI_PAGE_SHIFT))) {\r
+      //\r
+      // Found it\r
+      //\r
+      if (MemoryMap->Attribute & EFI_MEMORY_RUNTIME) {\r
+        IsRuntime = TRUE;\r
+      }\r
+      break;\r
+    }\r
+    //\r
+    // Get next item\r
+    //\r
+    MemoryMap = (EFI_MEMORY_DESCRIPTOR *)((UINTN)MemoryMap + DescriptorSize);\r
+  }\r
+\r
+  //\r
+  // Done\r
+  //\r
+  gBS->FreePool (MemoryMapPtr);\r
+\r
+  return IsRuntime;\r
+}\r
+\r
+/**\r
+  Update mFvbEntry. Add new entry, or update existing entry if Fvb protocol is\r
+  reinstalled.\r
\r
+  @param Event      The Event that is being processed\r
+  @param Context    Event Context\r
+  \r
+**/\r
 STATIC\r
 VOID\r
 EFIAPI\r
 STATIC\r
 VOID\r
 EFIAPI\r
@@ -46,22 +136,6 @@ FvbNotificationEvent (
   IN  EFI_EVENT       Event,\r
   IN  VOID            *Context\r
   )\r
   IN  EFI_EVENT       Event,\r
   IN  VOID            *Context\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Update mFvbEntry. Add new entry, or update existing entry if Fvb protocol is\r
-  reinstalled.\r
-\r
-Arguments:\r
-\r
-  Event   - The Event that is being processed\r
-  \r
-  Context - Event Context\r
-\r
-Returns: \r
-  None\r
-\r
---*/\r
 {\r
   EFI_STATUS  Status;\r
   UINTN       BufferSize;\r
 {\r
   EFI_STATUS  Status;\r
   UINTN       BufferSize;\r
@@ -115,43 +189,59 @@ Returns:
     //\r
     //  Get the interface pointer and if it's ours, skip it\r
     //\r
     //\r
     //  Get the interface pointer and if it's ours, skip it\r
     //\r
-    Status = gBS->HandleProtocol (Handle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **) &mFvbEntry[UpdateIndex].Fvb);\r
+    Status = gBS->HandleProtocol (\r
+                    Handle, \r
+                    &gEfiFirmwareVolumeBlockProtocolGuid, \r
+                    (VOID **) &mFvbEntry[UpdateIndex].Fvb\r
+                    );\r
     ASSERT_EFI_ERROR (Status);\r
 \r
     ASSERT_EFI_ERROR (Status);\r
 \r
-    Status = gBS->HandleProtocol (Handle, &gEfiFvbExtensionProtocolGuid, (VOID **) &mFvbEntry[UpdateIndex].FvbExtension);\r
+    Status = gBS->HandleProtocol (\r
+                    Handle, \r
+                    &gEfiFvbExtensionProtocolGuid, \r
+                    (VOID **) &mFvbEntry[UpdateIndex].FvbExtension\r
+                    );\r
     if (Status != EFI_SUCCESS) {\r
       mFvbEntry[UpdateIndex].FvbExtension = NULL;\r
     }\r
     if (Status != EFI_SUCCESS) {\r
       mFvbEntry[UpdateIndex].FvbExtension = NULL;\r
     }\r
+\r
+    //\r
+    // Check the FVB can be accessed in RUNTIME, The FVBs in FVB handle list comes\r
+    // from two way:\r
+    // 1) Dxe Core. (FVB information is transferred from FV HOB).\r
+    // 2) FVB driver. \r
+    // The FVB produced Dxe core is used for discoverying DXE driver and dispatch. These\r
+    // FVBs can only be accessed in boot time. \r
+    // FVB driver will discovery all FV in FLASH and these FVBs can be accessed in runtime.\r
+    // The FVB itself produced by FVB driver is allocated in runtime memory. So we can \r
+    // determine the what FVB can be accessed in RUNTIME by judging whether FVB itself is allocated\r
+    // in RUNTIME memory.\r
+    // \r
+    mFvbEntry[UpdateIndex].IsRuntimeAccess = IsRuntimeMemory (mFvbEntry[UpdateIndex].Fvb);\r
   }\r
 }\r
 \r
   }\r
 }\r
 \r
+/**\r
+  Convert all pointers in mFvbEntry after ExitBootServices.\r
+\r
+  @param Event      The Event that is being processed\r
+  @param Context    Event Context\r
+  \r
+**/\r
 VOID\r
 EFIAPI\r
 FvbVirtualAddressChangeNotifyEvent (\r
   IN EFI_EVENT        Event,\r
   IN VOID             *Context\r
   )\r
 VOID\r
 EFIAPI\r
 FvbVirtualAddressChangeNotifyEvent (\r
   IN EFI_EVENT        Event,\r
   IN VOID             *Context\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert all pointers in mFvbEntry after ExitBootServices.\r
-\r
-Arguments:\r
-\r
-  Event   - The Event that is being processed\r
-  \r
-  Context - Event Context\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
 {\r
   UINTN Index;\r
   if (mFvbEntry != NULL) {\r
     for (Index = 0; Index < MAX_FVB_COUNT; Index++) {\r
 {\r
   UINTN Index;\r
   if (mFvbEntry != NULL) {\r
     for (Index = 0; Index < MAX_FVB_COUNT; Index++) {\r
+      if (!mFvbEntry[Index].IsRuntimeAccess) {\r
+        continue;\r
+      }\r
+\r
       if (NULL != mFvbEntry[Index].Fvb) {\r
         EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetBlockSize);\r
         EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetPhysicalAddress);\r
       if (NULL != mFvbEntry[Index].Fvb) {\r
         EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetBlockSize);\r
         EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetPhysicalAddress);\r
@@ -173,24 +263,21 @@ Returns:
   }\r
 }\r
 \r
   }\r
 }\r
 \r
+/**\r
+  Library constructor function entry.\r
\r
+  @param ImageHandle    The handle of image who call this libary.\r
+  @param SystemTable    The point of System Table.\r
+  \r
+  @retval EFI_SUCESS    Sucess construct this library.\r
+  @retval Others        Fail to contruct this libary.\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FvbLibInitialize (\r
   IN EFI_HANDLE         ImageHandle,\r
   IN EFI_SYSTEM_TABLE   *SystemTable\r
   )\r
 EFI_STATUS\r
 EFIAPI\r
 FvbLibInitialize (\r
   IN EFI_HANDLE         ImageHandle,\r
   IN EFI_SYSTEM_TABLE   *SystemTable\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Initialize globals and register Fvb Protocol notification function.\r
-\r
-Arguments:\r
-  None \r
-\r
-Returns: \r
-  EFI_SUCCESS\r
-\r
---*/\r
 {\r
   UINTN Status;\r
   mFvbCount = 0;\r
 {\r
   UINTN Status;\r
   mFvbCount = 0;\r
@@ -238,15 +325,34 @@ Returns:
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
-//\r
+\r
+// \r
+// =============================================================================\r
 // The following functions wrap Fvb protocol in the Runtime Lib functions.\r
 // The Instance translates into Fvb instance. The Fvb order defined by HOBs and\r
 // thus the sequence of FVB protocol addition define Instance.\r
 //\r
 // EfiFvbInitialize () must be called before any of the following functions\r
 // must be called.\r
 // The following functions wrap Fvb protocol in the Runtime Lib functions.\r
 // The Instance translates into Fvb instance. The Fvb order defined by HOBs and\r
 // thus the sequence of FVB protocol addition define Instance.\r
 //\r
 // EfiFvbInitialize () must be called before any of the following functions\r
 // must be called.\r
-//\r
+// =============================================================================\r
+// \r
 \r
 \r
+/**\r
+  Reads specified number of bytes into a buffer from the specified block\r
+\r
+  @param Instance     The FV instance to be read from.\r
+  @param Lba          The logical block address to be read from\r
+  @param Offset       Offset into the block at which to begin reading\r
+  @param NumBytes     Pointer that on input contains the total size of\r
+                      the buffer. On output, it contains the total number\r
+                      of bytes read\r
+  @param Buffer       Pointer to a caller allocated buffer that will be\r
+                      used to hold the data read\r
+  \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to Read block\r
+  @retval Others                 Fail to read block\r
+**/\r
 EFI_STATUS\r
 EfiFvbReadBlock (\r
   IN UINTN                                        Instance,\r
 EFI_STATUS\r
 EfiFvbReadBlock (\r
   IN UINTN                                        Instance,\r
@@ -255,36 +361,34 @@ EfiFvbReadBlock (
   IN OUT UINTN                                    *NumBytes,\r
   IN UINT8                                        *Buffer\r
   )\r
   IN OUT UINTN                                    *NumBytes,\r
   IN UINT8                                        *Buffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Reads specified number of bytes into a buffer from the specified block\r
-\r
-Arguments:\r
-  Instance              - The FV instance to be read from\r
-  Lba                   - The logical block address to be read from\r
-  Offset                - Offset into the block at which to begin reading\r
-  NumBytes              - Pointer that on input contains the total size of\r
-                          the buffer. On output, it contains the total number\r
-                          of bytes read\r
-  Buffer                - Pointer to a caller allocated buffer that will be\r
-                          used to hold the data read\r
-\r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->Read (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->Read (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
 }\r
 \r
+/**\r
+   Writes specified number of bytes from the input buffer to the block\r
\r
+  @param Instance         The FV instance to be written to\r
+  @param Lba              The starting logical block index to write to\r
+  @param Offset           Offset into the block at which to begin writing\r
+  @param NumBytes         Pointer that on input contains the total size of\r
+                          the buffer. On output, it contains the total number\r
+                          of bytes actually written\r
+  @param Buffer           Pointer to a caller allocated buffer that contains\r
+                          the source for the write\r
+  \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to write block\r
+  @retval Others                 Fail to write block\r
+**/\r
 EFI_STATUS\r
 EfiFvbWriteBlock (\r
   IN UINTN                                        Instance,\r
 EFI_STATUS\r
 EfiFvbWriteBlock (\r
   IN UINTN                                        Instance,\r
@@ -293,160 +397,152 @@ EfiFvbWriteBlock (
   IN OUT UINTN                                    *NumBytes,\r
   IN UINT8                                        *Buffer\r
   )\r
   IN OUT UINTN                                    *NumBytes,\r
   IN UINT8                                        *Buffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Writes specified number of bytes from the input buffer to the block\r
-\r
-Arguments:\r
-  Instance              - The FV instance to be written to\r
-  Lba                   - The starting logical block index to write to\r
-  Offset                - Offset into the block at which to begin writing\r
-  NumBytes              - Pointer that on input contains the total size of\r
-                          the buffer. On output, it contains the total number\r
-                          of bytes actually written\r
-  Buffer                - Pointer to a caller allocated buffer that contains\r
-                          the source for the write\r
-\r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->Write (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->Write (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
 }\r
 \r
+/**\r
+   Erases and initializes a firmware volume block\r
+\r
+   @param Instance      The FV instance to be erased\r
+   @param Lba           The logical block index to be erased\r
+   \r
+   @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+   @retval EFI_SUCESS             Sucess to erase block\r
+   @retval Others                 Fail to erase block\r
+**/\r
 EFI_STATUS\r
 EfiFvbEraseBlock (\r
   IN UINTN                                Instance,\r
   IN EFI_LBA                              Lba\r
   )\r
 EFI_STATUS\r
 EfiFvbEraseBlock (\r
   IN UINTN                                Instance,\r
   IN EFI_LBA                              Lba\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Erases and initializes a firmware volume block\r
-\r
-Arguments:\r
-  Instance              - The FV instance to be erased\r
-  Lba                   - The logical block index to be erased\r
-  \r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->EraseBlocks (mFvbEntry[Instance].Fvb, Lba, -1);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->EraseBlocks (mFvbEntry[Instance].Fvb, Lba, -1);\r
 }\r
 \r
+/**\r
+  Retrieves attributes, insures positive polarity of attribute bits, returns\r
+  resulting attributes in output parameter\r
\r
+  @param Instance       The FV instance whose attributes is going to be returned\r
+  @param Attributes     Output buffer which contains attributes\r
+  \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to get Fv attribute\r
+  @retval Others                 Fail to get Fv attribute\r
+**/\r
 EFI_STATUS\r
 EfiFvbGetVolumeAttributes (\r
   IN UINTN                                Instance,\r
   OUT EFI_FVB_ATTRIBUTES                  *Attributes\r
   )\r
 EFI_STATUS\r
 EfiFvbGetVolumeAttributes (\r
   IN UINTN                                Instance,\r
   OUT EFI_FVB_ATTRIBUTES                  *Attributes\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Retrieves attributes, insures positive polarity of attribute bits, returns\r
-  resulting attributes in output parameter\r
-\r
-Arguments:\r
-  Instance              - The FV instance whose attributes is going to be \r
-                          returned\r
-  Attributes            - Output buffer which contains attributes\r
-\r
-Returns: \r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->GetVolumeAttributes (mFvbEntry[Instance].Fvb, Attributes);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->GetVolumeAttributes (mFvbEntry[Instance].Fvb, Attributes);\r
 }\r
 \r
-EFI_STATUS\r
-EfiFvbSetVolumeAttributes (\r
-  IN UINTN                                Instance,\r
-  IN EFI_FVB_ATTRIBUTES                   Attributes\r
-  )\r
-/*++\r
+/**\r
+   Modifies the current settings of the firmware volume according to the \r
+   input parameter, and returns the new setting of the volume\r
 \r
 \r
-Routine Description:\r
-  Modifies the current settings of the firmware volume according to the \r
-  input parameter, and returns the new setting of the volume\r
-\r
-Arguments:\r
-  Instance              - The FV instance whose attributes is going to be \r
+   @param Instance        The FV instance whose attributes is going to be \r
                           modified\r
                           modified\r
-  Attributes            - On input, it is a pointer to EFI_FVB_ATTRIBUTES \r
+   @param Attributes      On input, it is a pointer to EFI_FVB_ATTRIBUTES \r
                           containing the desired firmware volume settings.\r
                           On successful return, it contains the new settings\r
                           of the firmware volume\r
                           containing the desired firmware volume settings.\r
                           On successful return, it contains the new settings\r
                           of the firmware volume\r
-\r
-Returns: \r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
+   \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to set Fv attribute\r
+  @retval Others                 Fail to set Fv attribute\r
+**/\r
+EFI_STATUS\r
+EfiFvbSetVolumeAttributes (\r
+  IN UINTN                                Instance,\r
+  IN EFI_FVB_ATTRIBUTES                   Attributes\r
+  )\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->SetVolumeAttributes (mFvbEntry[Instance].Fvb, &Attributes);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->SetVolumeAttributes (mFvbEntry[Instance].Fvb, &Attributes);\r
 }\r
 \r
+/**\r
+   Retrieves the physical address of a memory mapped FV\r
+\r
+   @param Instance      The FV instance whose base address is going to be\r
+                        returned\r
+   @param BaseAddress   Pointer to a caller allocated EFI_PHYSICAL_ADDRESS \r
+                        that on successful return, contains the base address\r
+                        of the firmware volume.\r
+   \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to get physical address\r
+  @retval Others                 Fail to get physical address\r
+**/\r
 EFI_STATUS\r
 EfiFvbGetPhysicalAddress (\r
   IN UINTN                                Instance,\r
   OUT EFI_PHYSICAL_ADDRESS                *BaseAddress\r
   )\r
 EFI_STATUS\r
 EfiFvbGetPhysicalAddress (\r
   IN UINTN                                Instance,\r
   OUT EFI_PHYSICAL_ADDRESS                *BaseAddress\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Retrieves the physical address of a memory mapped FV\r
-\r
-Arguments:\r
-  Instance              - The FV instance whose base address is going to be\r
-                          returned\r
-  BaseAddress           - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS \r
-                          that on successful return, contains the base address\r
-                          of the firmware volume. \r
-\r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->GetPhysicalAddress (mFvbEntry[Instance].Fvb, BaseAddress);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->GetPhysicalAddress (mFvbEntry[Instance].Fvb, BaseAddress);\r
 }\r
 \r
+/**\r
+   Retrieve the size of a logical block\r
+\r
+   @param Instance            The FV instance whose block size is going to be\r
+                              returned\r
+   @param Lba                 Indicates which block to return the size for.\r
+   @param BlockSize           A pointer to a caller allocated UINTN in which\r
+                              the size of the block is returned\r
+   @param NumOfBlocks         a pointer to a caller allocated UINTN in which the\r
+                              number of consecutive blocks starting with Lba is\r
+                              returned. All blocks in this range have a size of\r
+                              BlockSize\r
+   \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to get block size\r
+  @retval Others                 Fail to get block size\r
+**/\r
 EFI_STATUS\r
 EfiFvbGetBlockSize (\r
   IN UINTN                                        Instance,\r
 EFI_STATUS\r
 EfiFvbGetBlockSize (\r
   IN UINTN                                        Instance,\r
@@ -454,37 +550,32 @@ EfiFvbGetBlockSize (
   OUT UINTN                                       *BlockSize,\r
   OUT UINTN                                       *NumOfBlocks\r
   )\r
   OUT UINTN                                       *BlockSize,\r
   OUT UINTN                                       *NumOfBlocks\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Retrieve the size of a logical block\r
-\r
-Arguments:\r
-  Instance              - The FV instance whose block size is going to be\r
-                          returned\r
-  Lba                   - Indicates which block to return the size for.\r
-  BlockSize             - A pointer to a caller allocated UINTN in which\r
-                          the size of the block is returned\r
-  NumOfBlocks           - a pointer to a caller allocated UINTN in which the\r
-                          number of consecutive blocks starting with Lba is\r
-                          returned. All blocks in this range have a size of\r
-                          BlockSize\r
-\r
-Returns: \r
-  EFI_SUCCESS           - The firmware volume was read successfully and \r
-                          contents are in Buffer\r
-                          \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->GetBlockSize (mFvbEntry[Instance].Fvb, Lba, BlockSize, NumOfBlocks);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->GetBlockSize (mFvbEntry[Instance].Fvb, Lba, BlockSize, NumOfBlocks);\r
 }\r
 \r
+/**\r
+   Erases and initializes a specified range of a firmware volume\r
+\r
+   @param Instance          The FV instance to be erased\r
+   @param StartLba          The starting logical block index to be erased\r
+   @param OffsetStartLba    Offset into the starting block at which to \r
+                            begin erasing\r
+   @param LastLba           The last logical block index to be erased\r
+   @param OffsetLastLba     Offset into the last block at which to end erasing\r
+   \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to erase custom block range\r
+  @retval Others                 Fail to erase custom block range\r
+**/\r
 EFI_STATUS\r
 EfiFvbEraseCustomBlockRange (\r
   IN UINTN                                Instance,\r
 EFI_STATUS\r
 EfiFvbEraseCustomBlockRange (\r
   IN UINTN                                Instance,\r
@@ -493,33 +584,15 @@ EfiFvbEraseCustomBlockRange (
   IN EFI_LBA                              LastLba,\r
   IN UINTN                                OffsetLastLba\r
   )\r
   IN EFI_LBA                              LastLba,\r
   IN UINTN                                OffsetLastLba\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Erases and initializes a specified range of a firmware volume\r
-\r
-Arguments:\r
-  Instance              - The FV instance to be erased\r
-  StartLba              - The starting logical block index to be erased\r
-  OffsetStartLba        - Offset into the starting block at which to \r
-                          begin erasing\r
-  LastLba               - The last logical block index to be erased\r
-  OffsetLastLba         - Offset into the last block at which to end erasing\r
-\r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-  \r
-  EFI_UNSUPPORTED       - not support\r
-  \r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   if (!(mFvbEntry[Instance].FvbExtension)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
   if (!(mFvbEntry[Instance].FvbExtension)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
index 4dcb81d2161c4b8b3a9776604db2d4c21abafcb4..c94c8242ac1419bb8666ce582c7888eecf76dd1d 100644 (file)
@@ -1,19 +1,4 @@
-/*++\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. 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
-\r
-Module Name:\r
-\r
-    Fvb.c \r
-\r
-Abstract:\r
+/**@file\r
 \r
   Firmware Volume Block Protocol Runtime Abstraction\r
 \r
 \r
   Firmware Volume Block Protocol Runtime Abstraction\r
 \r
@@ -23,11 +8,17 @@ Abstract:
   the protocol has been reinstalled and it needs updateing.\r
 \r
   If you are using any of these lib functions.you must first call FvbInitialize ().\r
   the protocol has been reinstalled and it needs updateing.\r
 \r
   If you are using any of these lib functions.you must first call FvbInitialize ().\r
+  \r
+Copyright (c) 2006, Intel Corporation                                                         \r
+All rights reserved. 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
 \r
 \r
-Key:\r
-  FVB - Firmware Volume Block\r
-\r
---*/\r
+**/\r
 \r
 #include "Fvb.h"\r
 \r
 \r
 #include "Fvb.h"\r
 \r
@@ -35,12 +26,109 @@ Key:
 // Lib will ASSERT if more FVB devices than this are added to the system.\r
 //\r
 STATIC FVB_ENTRY          *mFvbEntry;\r
 // Lib will ASSERT if more FVB devices than this are added to the system.\r
 //\r
 STATIC FVB_ENTRY          *mFvbEntry;\r
-// STATIC EFI_EVENT          mFvbVirtualNotifyEvent;\r
 STATIC EFI_EVENT          mFvbRegistration;\r
 STATIC EFI_EVENT          mFvbRegistration;\r
-// STATIC EFI_EVENT          mEfiFvbVirtualNotifyEvent;\r
 STATIC BOOLEAN            mEfiFvbInitialized        = FALSE;\r
 STATIC UINTN              mFvbCount;\r
 \r
 STATIC BOOLEAN            mEfiFvbInitialized        = FALSE;\r
 STATIC UINTN              mFvbCount;\r
 \r
+/**\r
+  Check whether an address is runtime memory or not.\r
\r
+  @param    Address   The Address being checked.\r
+  \r
+  @retval   TRUE      The address is runtime memory.\r
+  @retval   FALSE     The address is not runtime memory.\r
+**/\r
+BOOLEAN\r
+IsRuntimeMemory (\r
+  IN VOID   *Address\r
+  )\r
+{\r
+  EFI_STATUS                           Status;\r
+  UINT8                                TmpMemoryMap[1];\r
+  UINTN                                MapKey;\r
+  UINTN                                DescriptorSize;\r
+  UINT32                               DescriptorVersion;\r
+  UINTN                                MemoryMapSize;\r
+  EFI_MEMORY_DESCRIPTOR                *MemoryMap;\r
+  EFI_MEMORY_DESCRIPTOR                *MemoryMapPtr;\r
+  BOOLEAN                              IsRuntime;\r
+  UINTN                                Index;\r
+\r
+  IsRuntime = FALSE;\r
+\r
+  //\r
+  // Get System MemoryMapSize\r
+  //\r
+  MemoryMapSize = 1;\r
+  Status = gBS->GetMemoryMap (\r
+                  &MemoryMapSize,\r
+                  (EFI_MEMORY_DESCRIPTOR *)TmpMemoryMap,\r
+                  &MapKey,\r
+                  &DescriptorSize,\r
+                  &DescriptorVersion\r
+                  );\r
+  ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
+  //\r
+  // Enlarge space here, because we will allocate pool now.\r
+  //\r
+  MemoryMapSize += EFI_PAGE_SIZE;\r
+  Status = gBS->AllocatePool (\r
+                  EfiBootServicesData,\r
+                  MemoryMapSize,\r
+                  (VOID**)&MemoryMap\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Get System MemoryMap\r
+  //\r
+  Status = gBS->GetMemoryMap (\r
+                  &MemoryMapSize,\r
+                  MemoryMap,\r
+                  &MapKey,\r
+                  &DescriptorSize,\r
+                  &DescriptorVersion\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  MemoryMapPtr = MemoryMap;\r
+  //\r
+  // Search the request Address\r
+  //\r
+  for (Index = 0; Index < (MemoryMapSize / DescriptorSize); Index++) {\r
+    if (((EFI_PHYSICAL_ADDRESS)(UINTN)Address >= MemoryMap->PhysicalStart) &&\r
+        ((EFI_PHYSICAL_ADDRESS)(UINTN)Address < MemoryMap->PhysicalStart\r
+                                              + LShiftU64 (MemoryMap->NumberOfPages, EFI_PAGE_SHIFT))) {\r
+      //\r
+      // Found it\r
+      //\r
+      if (MemoryMap->Attribute & EFI_MEMORY_RUNTIME) {\r
+        IsRuntime = TRUE;\r
+      }\r
+      break;\r
+    }\r
+    //\r
+    // Get next item\r
+    //\r
+    MemoryMap = (EFI_MEMORY_DESCRIPTOR *)((UINTN)MemoryMap + DescriptorSize);\r
+  }\r
+\r
+  //\r
+  // Done\r
+  //\r
+  gBS->FreePool (MemoryMapPtr);\r
+\r
+  return IsRuntime;\r
+}\r
+\r
+/**\r
+  Update mFvbEntry. Add new entry, or update existing entry if Fvb protocol is\r
+  reinstalled.\r
\r
+  @param Event      The Event that is being processed\r
+  @param Context    Event Context\r
+  \r
+**/\r
 STATIC\r
 VOID\r
 EFIAPI\r
 STATIC\r
 VOID\r
 EFIAPI\r
@@ -48,22 +136,6 @@ FvbNotificationEvent (
   IN  EFI_EVENT       Event,\r
   IN  VOID            *Context\r
   )\r
   IN  EFI_EVENT       Event,\r
   IN  VOID            *Context\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Update mFvbEntry. Add new entry, or update existing entry if Fvb protocol is\r
-  reinstalled.\r
-\r
-Arguments:\r
-\r
-  Event   - The Event that is being processed\r
-  \r
-  Context - Event Context\r
-\r
-Returns: \r
-  None\r
-\r
---*/\r
 {\r
   EFI_STATUS  Status;\r
   UINTN       BufferSize;\r
 {\r
   EFI_STATUS  Status;\r
   UINTN       BufferSize;\r
@@ -103,7 +175,10 @@ Returns:
       // Use the next free slot for a new entry\r
       //\r
       UpdateIndex                   = mFvbCount++;\r
       // Use the next free slot for a new entry\r
       //\r
       UpdateIndex                   = mFvbCount++;\r
-                       ASSERT (UpdateIndex < MAX_FVB_COUNT);      \r
+      //\r
+      // Check the UpdateIndex whether exceed the maximum value.\r
+      //\r
+      ASSERT (UpdateIndex < MAX_FVB_COUNT);\r
       mFvbEntry[UpdateIndex].Handle = Handle;\r
     }\r
     //\r
       mFvbEntry[UpdateIndex].Handle = Handle;\r
     }\r
     //\r
@@ -114,43 +189,59 @@ Returns:
     //\r
     //  Get the interface pointer and if it's ours, skip it\r
     //\r
     //\r
     //  Get the interface pointer and if it's ours, skip it\r
     //\r
-    Status = gBS->HandleProtocol (Handle, &gEfiFirmwareVolumeBlockProtocolGuid, (VOID **)&mFvbEntry[UpdateIndex].Fvb);\r
+    Status = gBS->HandleProtocol (\r
+                    Handle, \r
+                    &gEfiFirmwareVolumeBlockProtocolGuid, \r
+                    (VOID **) &mFvbEntry[UpdateIndex].Fvb\r
+                    );\r
     ASSERT_EFI_ERROR (Status);\r
 \r
     ASSERT_EFI_ERROR (Status);\r
 \r
-    Status = gBS->HandleProtocol (Handle, &gEfiFvbExtensionProtocolGuid, (VOID **)&mFvbEntry[UpdateIndex].FvbExtension);\r
+    Status = gBS->HandleProtocol (\r
+                    Handle, \r
+                    &gEfiFvbExtensionProtocolGuid, \r
+                    (VOID **) &mFvbEntry[UpdateIndex].FvbExtension\r
+                    );\r
     if (Status != EFI_SUCCESS) {\r
       mFvbEntry[UpdateIndex].FvbExtension = NULL;\r
     }\r
     if (Status != EFI_SUCCESS) {\r
       mFvbEntry[UpdateIndex].FvbExtension = NULL;\r
     }\r
+\r
+    //\r
+    // Check the FVB can be accessed in RUNTIME, The FVBs in FVB handle list comes\r
+    // from two way:\r
+    // 1) Dxe Core. (FVB information is transferred from FV HOB).\r
+    // 2) FVB driver. \r
+    // The FVB produced Dxe core is used for discoverying DXE driver and dispatch. These\r
+    // FVBs can only be accessed in boot time. \r
+    // FVB driver will discovery all FV in FLASH and these FVBs can be accessed in runtime.\r
+    // The FVB itself produced by FVB driver is allocated in runtime memory. So we can \r
+    // determine the what FVB can be accessed in RUNTIME by judging whether FVB itself is allocated\r
+    // in RUNTIME memory.\r
+    // \r
+    mFvbEntry[UpdateIndex].IsRuntimeAccess = IsRuntimeMemory (mFvbEntry[UpdateIndex].Fvb);\r
   }\r
 }\r
 \r
   }\r
 }\r
 \r
+/**\r
+  Convert all pointers in mFvbEntry after ExitBootServices.\r
+\r
+  @param Event      The Event that is being processed\r
+  @param Context    Event Context\r
+  \r
+**/\r
 VOID\r
 EFIAPI\r
 FvbVirtualAddressChangeNotifyEvent (\r
   IN EFI_EVENT        Event,\r
   IN VOID             *Context\r
   )\r
 VOID\r
 EFIAPI\r
 FvbVirtualAddressChangeNotifyEvent (\r
   IN EFI_EVENT        Event,\r
   IN VOID             *Context\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Convert all pointers in mFvbEntry after ExitBootServices.\r
-\r
-Arguments:\r
-\r
-  Event   - The Event that is being processed\r
-  \r
-  Context - Event Context\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
 {\r
   UINTN Index;\r
   if (mFvbEntry != NULL) {\r
     for (Index = 0; Index < MAX_FVB_COUNT; Index++) {\r
 {\r
   UINTN Index;\r
   if (mFvbEntry != NULL) {\r
     for (Index = 0; Index < MAX_FVB_COUNT; Index++) {\r
+      if (!mFvbEntry[Index].IsRuntimeAccess) {\r
+        continue;\r
+      }\r
+\r
       if (NULL != mFvbEntry[Index].Fvb) {\r
         EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetBlockSize);\r
         EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetPhysicalAddress);\r
       if (NULL != mFvbEntry[Index].Fvb) {\r
         EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetBlockSize);\r
         EfiConvertPointer (0x0, (VOID **) &mFvbEntry[Index].Fvb->GetPhysicalAddress);\r
@@ -172,24 +263,21 @@ Returns:
   }\r
 }\r
 \r
   }\r
 }\r
 \r
+/**\r
+  Library constructor function entry.\r
\r
+  @param ImageHandle    The handle of image who call this libary.\r
+  @param SystemTable    The point of System Table.\r
+  \r
+  @retval EFI_SUCESS    Sucess construct this library.\r
+  @retval Others        Fail to contruct this libary.\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FvbLibInitialize (\r
   IN EFI_HANDLE         ImageHandle,\r
   IN EFI_SYSTEM_TABLE   *SystemTable\r
   )\r
 EFI_STATUS\r
 EFIAPI\r
 FvbLibInitialize (\r
   IN EFI_HANDLE         ImageHandle,\r
   IN EFI_SYSTEM_TABLE   *SystemTable\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Initialize globals and register Fvb Protocol notification function.\r
-\r
-Arguments:\r
-  None \r
-\r
-Returns: \r
-  EFI_SUCCESS\r
-\r
---*/\r
 {\r
   UINTN Status;\r
   mFvbCount = 0;\r
 {\r
   UINTN Status;\r
   mFvbCount = 0;\r
@@ -237,15 +325,34 @@ Returns:
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
-//\r
+\r
+// \r
+// =============================================================================\r
 // The following functions wrap Fvb protocol in the Runtime Lib functions.\r
 // The Instance translates into Fvb instance. The Fvb order defined by HOBs and\r
 // thus the sequence of FVB protocol addition define Instance.\r
 //\r
 // EfiFvbInitialize () must be called before any of the following functions\r
 // must be called.\r
 // The following functions wrap Fvb protocol in the Runtime Lib functions.\r
 // The Instance translates into Fvb instance. The Fvb order defined by HOBs and\r
 // thus the sequence of FVB protocol addition define Instance.\r
 //\r
 // EfiFvbInitialize () must be called before any of the following functions\r
 // must be called.\r
-//\r
+// =============================================================================\r
+// \r
+\r
+/**\r
+  Reads specified number of bytes into a buffer from the specified block\r
 \r
 \r
+  @param Instance     The FV instance to be read from.\r
+  @param Lba          The logical block address to be read from\r
+  @param Offset       Offset into the block at which to begin reading\r
+  @param NumBytes     Pointer that on input contains the total size of\r
+                      the buffer. On output, it contains the total number\r
+                      of bytes read\r
+  @param Buffer       Pointer to a caller allocated buffer that will be\r
+                      used to hold the data read\r
+  \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to Read block\r
+  @retval Others                 Fail to read block\r
+**/\r
 EFI_STATUS\r
 EfiFvbReadBlock (\r
   IN UINTN                                        Instance,\r
 EFI_STATUS\r
 EfiFvbReadBlock (\r
   IN UINTN                                        Instance,\r
@@ -254,36 +361,34 @@ EfiFvbReadBlock (
   IN OUT UINTN                                    *NumBytes,\r
   IN UINT8                                        *Buffer\r
   )\r
   IN OUT UINTN                                    *NumBytes,\r
   IN UINT8                                        *Buffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Reads specified number of bytes into a buffer from the specified block\r
-\r
-Arguments:\r
-  Instance              - The FV instance to be read from\r
-  Lba                   - The logical block address to be read from\r
-  Offset                - Offset into the block at which to begin reading\r
-  NumBytes              - Pointer that on input contains the total size of\r
-                          the buffer. On output, it contains the total number\r
-                          of bytes read\r
-  Buffer                - Pointer to a caller allocated buffer that will be\r
-                          used to hold the data read\r
-\r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->Read (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->Read (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
 }\r
 \r
+/**\r
+   Writes specified number of bytes from the input buffer to the block\r
\r
+  @param Instance         The FV instance to be written to\r
+  @param Lba              The starting logical block index to write to\r
+  @param Offset           Offset into the block at which to begin writing\r
+  @param NumBytes         Pointer that on input contains the total size of\r
+                          the buffer. On output, it contains the total number\r
+                          of bytes actually written\r
+  @param Buffer           Pointer to a caller allocated buffer that contains\r
+                          the source for the write\r
+  \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to write block\r
+  @retval Others                 Fail to write block\r
+**/\r
 EFI_STATUS\r
 EfiFvbWriteBlock (\r
   IN UINTN                                        Instance,\r
 EFI_STATUS\r
 EfiFvbWriteBlock (\r
   IN UINTN                                        Instance,\r
@@ -292,160 +397,152 @@ EfiFvbWriteBlock (
   IN OUT UINTN                                    *NumBytes,\r
   IN UINT8                                        *Buffer\r
   )\r
   IN OUT UINTN                                    *NumBytes,\r
   IN UINT8                                        *Buffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Writes specified number of bytes from the input buffer to the block\r
-\r
-Arguments:\r
-  Instance              - The FV instance to be written to\r
-  Lba                   - The starting logical block index to write to\r
-  Offset                - Offset into the block at which to begin writing\r
-  NumBytes              - Pointer that on input contains the total size of\r
-                          the buffer. On output, it contains the total number\r
-                          of bytes actually written\r
-  Buffer                - Pointer to a caller allocated buffer that contains\r
-                          the source for the write\r
-\r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->Write (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->Write (mFvbEntry[Instance].Fvb, Lba, Offset, NumBytes, Buffer);\r
 }\r
 \r
+/**\r
+   Erases and initializes a firmware volume block\r
+\r
+   @param Instance      The FV instance to be erased\r
+   @param Lba           The logical block index to be erased\r
+   \r
+   @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+   @retval EFI_SUCESS             Sucess to erase block\r
+   @retval Others                 Fail to erase block\r
+**/\r
 EFI_STATUS\r
 EfiFvbEraseBlock (\r
   IN UINTN                                Instance,\r
   IN EFI_LBA                              Lba\r
   )\r
 EFI_STATUS\r
 EfiFvbEraseBlock (\r
   IN UINTN                                Instance,\r
   IN EFI_LBA                              Lba\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Erases and initializes a firmware volume block\r
-\r
-Arguments:\r
-  Instance              - The FV instance to be erased\r
-  Lba                   - The logical block index to be erased\r
-  \r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->EraseBlocks (mFvbEntry[Instance].Fvb, Lba, -1);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->EraseBlocks (mFvbEntry[Instance].Fvb, Lba, -1);\r
 }\r
 \r
+/**\r
+  Retrieves attributes, insures positive polarity of attribute bits, returns\r
+  resulting attributes in output parameter\r
\r
+  @param Instance       The FV instance whose attributes is going to be returned\r
+  @param Attributes     Output buffer which contains attributes\r
+  \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to get Fv attribute\r
+  @retval Others                 Fail to get Fv attribute\r
+**/\r
 EFI_STATUS\r
 EfiFvbGetVolumeAttributes (\r
   IN UINTN                                Instance,\r
   OUT EFI_FVB_ATTRIBUTES                  *Attributes\r
   )\r
 EFI_STATUS\r
 EfiFvbGetVolumeAttributes (\r
   IN UINTN                                Instance,\r
   OUT EFI_FVB_ATTRIBUTES                  *Attributes\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Retrieves attributes, insures positive polarity of attribute bits, returns\r
-  resulting attributes in output parameter\r
-\r
-Arguments:\r
-  Instance              - The FV instance whose attributes is going to be \r
-                          returned\r
-  Attributes            - Output buffer which contains attributes\r
-\r
-Returns: \r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->GetVolumeAttributes (mFvbEntry[Instance].Fvb, Attributes);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->GetVolumeAttributes (mFvbEntry[Instance].Fvb, Attributes);\r
 }\r
 \r
-EFI_STATUS\r
-EfiFvbSetVolumeAttributes (\r
-  IN UINTN                                Instance,\r
-  IN EFI_FVB_ATTRIBUTES                   Attributes\r
-  )\r
-/*++\r
+/**\r
+   Modifies the current settings of the firmware volume according to the \r
+   input parameter, and returns the new setting of the volume\r
 \r
 \r
-Routine Description:\r
-  Modifies the current settings of the firmware volume according to the \r
-  input parameter, and returns the new setting of the volume\r
-\r
-Arguments:\r
-  Instance              - The FV instance whose attributes is going to be \r
+   @param Instance        The FV instance whose attributes is going to be \r
                           modified\r
                           modified\r
-  Attributes            - On input, it is a pointer to EFI_FVB_ATTRIBUTES \r
+   @param Attributes      On input, it is a pointer to EFI_FVB_ATTRIBUTES \r
                           containing the desired firmware volume settings.\r
                           On successful return, it contains the new settings\r
                           of the firmware volume\r
                           containing the desired firmware volume settings.\r
                           On successful return, it contains the new settings\r
                           of the firmware volume\r
-\r
-Returns: \r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
+   \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to set Fv attribute\r
+  @retval Others                 Fail to set Fv attribute\r
+**/\r
+EFI_STATUS\r
+EfiFvbSetVolumeAttributes (\r
+  IN UINTN                                Instance,\r
+  IN EFI_FVB_ATTRIBUTES                   Attributes\r
+  )\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->SetVolumeAttributes (mFvbEntry[Instance].Fvb, &Attributes);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->SetVolumeAttributes (mFvbEntry[Instance].Fvb, &Attributes);\r
 }\r
 \r
+/**\r
+   Retrieves the physical address of a memory mapped FV\r
+\r
+   @param Instance      The FV instance whose base address is going to be\r
+                        returned\r
+   @param BaseAddress   Pointer to a caller allocated EFI_PHYSICAL_ADDRESS \r
+                        that on successful return, contains the base address\r
+                        of the firmware volume.\r
+   \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to get physical address\r
+  @retval Others                 Fail to get physical address\r
+**/\r
 EFI_STATUS\r
 EfiFvbGetPhysicalAddress (\r
   IN UINTN                                Instance,\r
   OUT EFI_PHYSICAL_ADDRESS                *BaseAddress\r
   )\r
 EFI_STATUS\r
 EfiFvbGetPhysicalAddress (\r
   IN UINTN                                Instance,\r
   OUT EFI_PHYSICAL_ADDRESS                *BaseAddress\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Retrieves the physical address of a memory mapped FV\r
-\r
-Arguments:\r
-  Instance              - The FV instance whose base address is going to be\r
-                          returned\r
-  BaseAddress           - Pointer to a caller allocated EFI_PHYSICAL_ADDRESS \r
-                          that on successful return, contains the base address\r
-                          of the firmware volume. \r
-\r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->GetPhysicalAddress (mFvbEntry[Instance].Fvb, BaseAddress);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->GetPhysicalAddress (mFvbEntry[Instance].Fvb, BaseAddress);\r
 }\r
 \r
+/**\r
+   Retrieve the size of a logical block\r
+\r
+   @param Instance            The FV instance whose block size is going to be\r
+                              returned\r
+   @param Lba                 Indicates which block to return the size for.\r
+   @param BlockSize           A pointer to a caller allocated UINTN in which\r
+                              the size of the block is returned\r
+   @param NumOfBlocks         a pointer to a caller allocated UINTN in which the\r
+                              number of consecutive blocks starting with Lba is\r
+                              returned. All blocks in this range have a size of\r
+                              BlockSize\r
+   \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to get block size\r
+  @retval Others                 Fail to get block size\r
+**/\r
 EFI_STATUS\r
 EfiFvbGetBlockSize (\r
   IN UINTN                                        Instance,\r
 EFI_STATUS\r
 EfiFvbGetBlockSize (\r
   IN UINTN                                        Instance,\r
@@ -453,37 +550,32 @@ EfiFvbGetBlockSize (
   OUT UINTN                                       *BlockSize,\r
   OUT UINTN                                       *NumOfBlocks\r
   )\r
   OUT UINTN                                       *BlockSize,\r
   OUT UINTN                                       *NumOfBlocks\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Retrieve the size of a logical block\r
-\r
-Arguments:\r
-  Instance              - The FV instance whose block size is going to be\r
-                          returned\r
-  Lba                   - Indicates which block to return the size for.\r
-  BlockSize             - A pointer to a caller allocated UINTN in which\r
-                          the size of the block is returned\r
-  NumOfBlocks           - a pointer to a caller allocated UINTN in which the\r
-                          number of consecutive blocks starting with Lba is\r
-                          returned. All blocks in this range have a size of\r
-                          BlockSize\r
-\r
-Returns: \r
-  EFI_SUCCESS           - The firmware volume was read successfully and \r
-                          contents are in Buffer\r
-                          \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-\r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return mFvbEntry[Instance].Fvb->GetBlockSize (mFvbEntry[Instance].Fvb, Lba, BlockSize, NumOfBlocks);\r
 }\r
 \r
   return mFvbEntry[Instance].Fvb->GetBlockSize (mFvbEntry[Instance].Fvb, Lba, BlockSize, NumOfBlocks);\r
 }\r
 \r
+/**\r
+   Erases and initializes a specified range of a firmware volume\r
+\r
+   @param Instance          The FV instance to be erased\r
+   @param StartLba          The starting logical block index to be erased\r
+   @param OffsetStartLba    Offset into the starting block at which to \r
+                            begin erasing\r
+   @param LastLba           The last logical block index to be erased\r
+   @param OffsetLastLba     Offset into the last block at which to end erasing\r
+   \r
+  @retval EFI_INVALID_PARAMETER  Invalid parameter\r
+  @retval EFI_SUCESS             Sucess to erase custom block range\r
+  @retval Others                 Fail to erase custom block range\r
+**/\r
 EFI_STATUS\r
 EfiFvbEraseCustomBlockRange (\r
   IN UINTN                                Instance,\r
 EFI_STATUS\r
 EfiFvbEraseCustomBlockRange (\r
   IN UINTN                                Instance,\r
@@ -492,33 +584,15 @@ EfiFvbEraseCustomBlockRange (
   IN EFI_LBA                              LastLba,\r
   IN UINTN                                OffsetLastLba\r
   )\r
   IN EFI_LBA                              LastLba,\r
   IN UINTN                                OffsetLastLba\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-  Erases and initializes a specified range of a firmware volume\r
-\r
-Arguments:\r
-  Instance              - The FV instance to be erased\r
-  StartLba              - The starting logical block index to be erased\r
-  OffsetStartLba        - Offset into the starting block at which to \r
-                          begin erasing\r
-  LastLba               - The last logical block index to be erased\r
-  OffsetLastLba         - Offset into the last block at which to end erasing\r
-\r
-Returns: \r
-\r
-  Status code\r
-  \r
-  EFI_INVALID_PARAMETER - invalid parameter\r
-  \r
-  EFI_UNSUPPORTED       - not support\r
-  \r
---*/\r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 {\r
   if (Instance >= mFvbCount) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (EfiAtRuntime() && !mFvbEntry[Instance].IsRuntimeAccess) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   if (!(mFvbEntry[Instance].FvbExtension)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
   if (!(mFvbEntry[Instance].FvbExtension)) {\r
     return EFI_UNSUPPORTED;\r
   }\r