]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkPkg/Library/FrameworkUefiLib/UefiLib.c
MdeModulePkg/FaultTolerantWriteDxe: implement standalone MM version
[mirror_edk2.git] / IntelFrameworkPkg / Library / FrameworkUefiLib / UefiLib.c
index 3eeb12110a90466192097e186575c18c60f3765c..45bf4b17c5c3bb951cf8a2eec0e0db0348dce0b6 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
-  The UEFI Library provides functions and macros that simplify the development of \r
-  UEFI Drivers and UEFI Applications.  These functions and macros help manage EFI \r
-  events, build simple locks utilizing EFI Task Priority Levels (TPLs), install \r
-  EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers, \r
+  The UEFI Library provides functions and macros that simplify the development of\r
+  UEFI Drivers and UEFI Applications.  These functions and macros help manage EFI\r
+  events, build simple locks utilizing EFI Task Priority Levels (TPLs), install\r
+  EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers,\r
   and print messages on the console output and standard error devices.\r
 \r
   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
@@ -47,7 +47,7 @@ CompareIso639LanguageCode (
 /**\r
   Retrieves a pointer to the system configuration table from the EFI System Table\r
   based on a specified GUID.\r
-  \r
+\r
   This function searches the list of configuration tables stored in the EFI System Table\r
   for a table with a GUID that matches TableGuid.  If a match is found, then a pointer to\r
   the configuration table is returned in Table., and EFI_SUCCESS is returned. If a matching GUID\r
@@ -96,7 +96,7 @@ EfiGetSystemConfigurationTable (
   no instances of ProtocolGuid in the handle database at the time this function is invoked,\r
   then the notification function is still executed one time. In addition, every time a protocol\r
   of type ProtocolGuid instance is installed or reinstalled, the notification function is also\r
-  executed. This function returns the notification event that was created. \r
+  executed. This function returns the notification event that was created.\r
   If ProtocolGuid is NULL, then ASSERT().\r
   If NotifyTpl is not a legal TPL value, then ASSERT().\r
   If NotifyFunction is NULL, then ASSERT().\r
@@ -109,7 +109,7 @@ EfiGetSystemConfigurationTable (
   @param  NotifyContext   The context parameter to pass to NotifyFunction.\r
   @param  Registration    A pointer to a memory location to receive the registration value.\r
                           This value is passed to LocateHandle() to obtain new handles that\r
-                          have been added that support the ProtocolGuid-specified protocol. \r
+                          have been added that support the ProtocolGuid-specified protocol.\r
 \r
   @return The notification event that was created.\r
 \r
@@ -170,7 +170,7 @@ EfiCreateProtocolNotifyEvent(
 \r
   This function creates an event using NotifyTpl, NoifyFunction, and NotifyContext.\r
   This event is signaled with EfiNamedEventSignal(). This provides the ability for one or more\r
-  listeners on the same event named by the GUID specified by Name. \r
+  listeners on the same event named by the GUID specified by Name.\r
   If Name is NULL, then ASSERT().\r
   If NotifyTpl is not a legal TPL value, then ASSERT().\r
   If NotifyFunction is NULL, then ASSERT().\r
@@ -178,7 +178,7 @@ EfiCreateProtocolNotifyEvent(
   @param  Name                  Supplies GUID name of the event.\r
   @param  NotifyTpl             Supplies the task priority level of the event notifications.\r
   @param  NotifyFunction        Supplies the function to notify when the event is signaled.\r
-  @param  NotifyContext         The context parameter to pass to NotifyFunction. \r
+  @param  NotifyContext         The context parameter to pass to NotifyFunction.\r
   @param  Registration          A pointer to a memory location to receive the registration value.\r
 \r
   @retval EFI_SUCCESS           A named event was created.\r
@@ -202,7 +202,7 @@ EfiNamedEventListen (
   ASSERT (Name != NULL);\r
   ASSERT (NotifyFunction != NULL);\r
   ASSERT (NotifyTpl <= TPL_HIGH_LEVEL);\r
-  \r
+\r
   //\r
   // Create event\r
   //\r
@@ -343,13 +343,13 @@ EfiEventEmptyFunction (
 {\r
 }\r
 \r
-/** \r
+/**\r
   Returns the current TPL.\r
 \r
-  This function returns the current TPL.  There is no EFI service to directly \r
-  retrieve the current TPL. Instead, the RaiseTPL() function is used to raise \r
-  the TPL to TPL_HIGH_LEVEL.  This will return the current TPL.  The TPL level \r
-  can then immediately be restored back to the current TPL level with a call \r
+  This function returns the current TPL.  There is no EFI service to directly\r
+  retrieve the current TPL. Instead, the RaiseTPL() function is used to raise\r
+  the TPL to TPL_HIGH_LEVEL.  This will return the current TPL.  The TPL level\r
+  can then immediately be restored back to the current TPL level with a call\r
   to RestoreTPL().\r
 \r
   @return The current TPL.\r
@@ -373,8 +373,8 @@ EfiGetCurrentTpl (
 /**\r
   Initializes a basic mutual exclusion lock.\r
 \r
-  This function initializes a basic mutual exclusion lock to the released state \r
-  and returns the lock.  Each lock provides mutual exclusion access at its task \r
+  This function initializes a basic mutual exclusion lock to the released state\r
+  and returns the lock.  Each lock provides mutual exclusion access at its task\r
   priority level.  Since there is no preemption or multiprocessor support in EFI,\r
   acquiring the lock only consists of raising to the locks TPL.\r
   If Lock is NULL, then ASSERT().\r
@@ -405,8 +405,8 @@ EfiInitializeLock (
 /**\r
   Acquires ownership of a lock.\r
 \r
-  This function raises the system's current task priority level to the task \r
-  priority level of the mutual exclusion lock.  Then, it places the lock in the \r
+  This function raises the system's current task priority level to the task\r
+  priority level of the mutual exclusion lock.  Then, it places the lock in the\r
   acquired state.\r
   If Lock is NULL, then ASSERT().\r
   If Lock is not initialized, then ASSERT().\r
@@ -471,8 +471,8 @@ EfiAcquireLockOrFail (
 /**\r
   Releases ownership of a lock.\r
 \r
-  This function transitions a mutual exclusion lock from the acquired state to \r
-  the released state, and restores the system's task priority level to its \r
+  This function transitions a mutual exclusion lock from the acquired state to\r
+  the released state, and restores the system's task priority level to its\r
   previous level.\r
   If Lock is NULL, then ASSERT().\r
   If Lock is not initialized, then ASSERT().\r
@@ -506,7 +506,7 @@ EfiReleaseLock (
   currently managing the controller specified by ControllerHandle.  This test\r
   is performed by evaluating if the the protocol specified by ProtocolGuid is\r
   present on ControllerHandle and is was opened by DriverBindingHandle with an\r
-  attribute of EFI_OPEN_PROTOCOL_BY_DRIVER. \r
+  attribute of EFI_OPEN_PROTOCOL_BY_DRIVER.\r
   If ProtocolGuid is NULL, then ASSERT().\r
 \r
   @param  ControllerHandle     A handle for a controller to test.\r
@@ -569,10 +569,10 @@ EfiTestManagedDevice (
   ChildHandle with an attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
   If ProtocolGuid is NULL, then ASSERT().\r
 \r
-  @param  ControllerHandle     A handle for a (parent) controller to test. \r
+  @param  ControllerHandle     A handle for a (parent) controller to test.\r
   @param  ChildHandle          A child handle to test.\r
   @param  ProtocolGuid         Supplies the protocol that the child controller\r
-                               opens on its parent controller. \r
+                               opens on its parent controller.\r
 \r
   @retval EFI_SUCCESS          ChildHandle is a child of the ControllerHandle.\r
   @retval EFI_UNSUPPORTED      ChildHandle is not a child of the\r
@@ -631,26 +631,26 @@ EfiTestChildHandle (
   UnicodeStringTable that matches the language code specified by Language, then it\r
   is returned in UnicodeString.\r
 \r
-  @param  Language                A pointer to the ISO 639-2 language code for the \r
+  @param  Language                A pointer to the ISO 639-2 language code for the\r
                                   Unicode string to look up and return.\r
-  @param  SupportedLanguages      A pointer to the set of ISO 639-2 language codes \r
-                                  that the Unicode string table supports.  Language \r
+  @param  SupportedLanguages      A pointer to the set of ISO 639-2 language codes\r
+                                  that the Unicode string table supports.  Language\r
                                   must be a member of this set.\r
   @param  UnicodeStringTable      A pointer to the table of Unicode strings.\r
   @param  UnicodeString           A pointer to the Unicode string from UnicodeStringTable\r
                                   that matches the language specified by Language.\r
 \r
-  @retval EFI_SUCCESS             The Unicode string that matches the language \r
+  @retval EFI_SUCCESS             The Unicode string that matches the language\r
                                   specified by Language was found\r
-                                  in the table of Unicode strings UnicodeStringTable, \r
+                                  in the table of Unicode strings UnicodeStringTable,\r
                                   and it was returned in UnicodeString.\r
   @retval EFI_INVALID_PARAMETER   Language is NULL.\r
   @retval EFI_INVALID_PARAMETER   UnicodeString is NULL.\r
   @retval EFI_UNSUPPORTED         SupportedLanguages is NULL.\r
   @retval EFI_UNSUPPORTED         UnicodeStringTable is NULL.\r
-  @retval EFI_UNSUPPORTED         The language specified by Language is not a \r
+  @retval EFI_UNSUPPORTED         The language specified by Language is not a\r
                                   member of SupportedLanguages.\r
-  @retval EFI_UNSUPPORTED         The language specified by Language is not \r
+  @retval EFI_UNSUPPORTED         The language specified by Language is not\r
                                   supported by UnicodeStringTable.\r
 \r
 **/\r
@@ -723,7 +723,7 @@ LookupUnicodeString (
                                return. If Iso639Language is TRUE, then this ASCII string is\r
                                not assumed to be Null-terminated, and only the first three\r
                                characters are used. If Iso639Language is FALSE, then this ASCII\r
-                               string must be Null-terminated. \r
+                               string must be Null-terminated.\r
   @param  SupportedLanguages   A pointer to a Null-terminated ASCII string that contains a\r
                                set of ISO 639-2 or RFC 4646 language codes that the Unicode\r
                                string table supports.  Language must be a member of this set.\r
@@ -743,11 +743,11 @@ LookupUnicodeString (
   @retval  EFI_SUCCESS            The Unicode string that matches the language specified by Language\r
                                   was found in the table of Unicode strings UnicodeStringTable, and\r
                                   it was returned in UnicodeString.\r
-  @retval  EFI_INVALID_PARAMETER  Language is NULL.  \r
-  @retval  EFI_INVALID_PARAMETER  UnicodeString is NULL.  \r
-  @retval  EFI_UNSUPPORTED        SupportedLanguages is NULL.  \r
-  @retval  EFI_UNSUPPORTED        UnicodeStringTable is NULL.  \r
-  @retval  EFI_UNSUPPORTED        The language specified by Language is not a member of SupportedLanguages.  \r
+  @retval  EFI_INVALID_PARAMETER  Language is NULL.\r
+  @retval  EFI_INVALID_PARAMETER  UnicodeString is NULL.\r
+  @retval  EFI_UNSUPPORTED        SupportedLanguages is NULL.\r
+  @retval  EFI_UNSUPPORTED        UnicodeStringTable is NULL.\r
+  @retval  EFI_UNSUPPORTED        The language specified by Language is not a member of SupportedLanguages.\r
   @retval  EFI_UNSUPPORTED        The language specified by Language is not supported by UnicodeStringTable.\r
 \r
 **/\r
@@ -833,13 +833,13 @@ LookupUnicodeString2 (
 /**\r
   This function adds a Unicode string to UnicodeStringTable.\r
 \r
-  If Language is a member of SupportedLanguages then UnicodeString is added to \r
-  UnicodeStringTable.  New buffers are allocated for both Language and \r
-  UnicodeString.  The contents of Language and UnicodeString are copied into \r
-  these new buffers.  These buffers are automatically freed when \r
+  If Language is a member of SupportedLanguages then UnicodeString is added to\r
+  UnicodeStringTable.  New buffers are allocated for both Language and\r
+  UnicodeString.  The contents of Language and UnicodeString are copied into\r
+  these new buffers.  These buffers are automatically freed when\r
   FreeUnicodeStringTable() is called.\r
 \r
-  @param  Language                A pointer to the ISO 639-2 language code for the Unicode \r
+  @param  Language                A pointer to the ISO 639-2 language code for the Unicode\r
                                   string to add.\r
   @param  SupportedLanguages      A pointer to the set of ISO 639-2 language codes\r
                                   that the Unicode string table supports.\r
@@ -847,19 +847,19 @@ LookupUnicodeString2 (
   @param  UnicodeStringTable      A pointer to the table of Unicode strings.\r
   @param  UnicodeString           A pointer to the Unicode string to add.\r
 \r
-  @retval EFI_SUCCESS             The Unicode string that matches the language \r
-                                  specified by Language was found in the table of \r
-                                  Unicode strings UnicodeStringTable, and it was \r
+  @retval EFI_SUCCESS             The Unicode string that matches the language\r
+                                  specified by Language was found in the table of\r
+                                  Unicode strings UnicodeStringTable, and it was\r
                                   returned in UnicodeString.\r
   @retval EFI_INVALID_PARAMETER   Language is NULL.\r
   @retval EFI_INVALID_PARAMETER   UnicodeString is NULL.\r
   @retval EFI_INVALID_PARAMETER   UnicodeString is an empty string.\r
   @retval EFI_UNSUPPORTED         SupportedLanguages is NULL.\r
-  @retval EFI_ALREADY_STARTED     A Unicode string with language Language is \r
+  @retval EFI_ALREADY_STARTED     A Unicode string with language Language is\r
                                   already present in UnicodeStringTable.\r
-  @retval EFI_OUT_OF_RESOURCES    There is not enough memory to add another \r
+  @retval EFI_OUT_OF_RESOURCES    There is not enough memory to add another\r
                                   Unicode string to UnicodeStringTable.\r
-  @retval EFI_UNSUPPORTED         The language specified by Language is not a \r
+  @retval EFI_UNSUPPORTED         The language specified by Language is not a\r
                                   member of SupportedLanguages.\r
 \r
 **/\r
@@ -1022,21 +1022,21 @@ AddUnicodeString (
                               RFC 4646 language codes separated by ';'.\r
   @param  UnicodeStringTable  A pointer to the table of Unicode strings. Type EFI_UNICODE_STRING_TABLE\r
                               is defined in "Related Definitions".\r
-  @param  UnicodeString       A pointer to the Unicode string to add.  \r
+  @param  UnicodeString       A pointer to the Unicode string to add.\r
   @param  Iso639Language      Specifies the supported language code format. If it is TRUE,\r
                               then Language and SupportedLanguages follow ISO 639-2 language code format.\r
                               Otherwise, they follow RFC 4646 language code format.\r
 \r
   @retval EFI_SUCCESS            The Unicode string that matches the language specified by\r
                                  Language was found in the table of Unicode strings UnicodeStringTable,\r
-                                 and it was returned in UnicodeString.  \r
-  @retval EFI_INVALID_PARAMETER  Language is NULL.  \r
-  @retval EFI_INVALID_PARAMETER  UnicodeString is NULL.  \r
-  @retval EFI_INVALID_PARAMETER  UnicodeString is an empty string.  \r
-  @retval EFI_UNSUPPORTED        SupportedLanguages is NULL.  \r
+                                 and it was returned in UnicodeString.\r
+  @retval EFI_INVALID_PARAMETER  Language is NULL.\r
+  @retval EFI_INVALID_PARAMETER  UnicodeString is NULL.\r
+  @retval EFI_INVALID_PARAMETER  UnicodeString is an empty string.\r
+  @retval EFI_UNSUPPORTED        SupportedLanguages is NULL.\r
   @retval EFI_ALREADY_STARTED    A Unicode string with language Language is already present in\r
-                                 UnicodeStringTable.  \r
-  @retval EFI_OUT_OF_RESOURCES   There is not enough memory to add another Unicode string UnicodeStringTable.  \r
+                                 UnicodeStringTable.\r
+  @retval EFI_OUT_OF_RESOURCES   There is not enough memory to add another Unicode string UnicodeStringTable.\r
   @retval EFI_UNSUPPORTED        The language specified by Language is not a member of SupportedLanguages.\r
 \r
 **/\r
@@ -1120,7 +1120,7 @@ AddUnicodeString2 (
       while (*LanguageString != 0) {\r
         for (Index = 0; LanguageString[Index] != 0 && LanguageString[Index] != ';'; Index++);\r
 \r
-        if (AsciiStrnCmp (Language, LanguageString, Index) == 0) { \r
+        if (AsciiStrnCmp (Language, LanguageString, Index) == 0) {\r
           return EFI_ALREADY_STARTED;\r
         }\r
         LanguageString += Index;\r
@@ -1202,7 +1202,7 @@ AddUnicodeString2 (
   This function frees the table of Unicode strings in UnicodeStringTable.\r
 \r
   If UnicodeStringTable is NULL, then EFI_SUCCESS is returned.\r
-  Otherwise, each language code, and each Unicode string in the Unicode string \r
+  Otherwise, each language code, and each Unicode string in the Unicode string\r
   table are freed, and EFI_SUCCESS is returned.\r
 \r
   @param  UnicodeStringTable  A pointer to the table of Unicode strings.\r
@@ -1252,8 +1252,8 @@ FreeUnicodeStringTable (
 }\r
 \r
 /**\r
-  Returns a pointer to an allocated buffer that contains the contents of a \r
-  variable retrieved through the UEFI Runtime Service GetVariable().  The \r
+  Returns a pointer to an allocated buffer that contains the contents of a\r
+  variable retrieved through the UEFI Runtime Service GetVariable().  The\r
   returned buffer is allocated using AllocatePool().  The caller is responsible\r
   for freeing this buffer with FreePool().\r
 \r
@@ -1314,10 +1314,10 @@ GetVariable (
 \r
 \r
 /**\r
-  Returns a pointer to an allocated buffer that contains the contents of a \r
-  variable retrieved through the UEFI Runtime Service GetVariable().  This \r
+  Returns a pointer to an allocated buffer that contains the contents of a\r
+  variable retrieved through the UEFI Runtime Service GetVariable().  This\r
   function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
-  The returned buffer is allocated using AllocatePool().  The caller is \r
+  The returned buffer is allocated using AllocatePool().  The caller is\r
   responsible for freeing this buffer with FreePool().\r
 \r
   If Name is NULL, then ASSERT().\r
@@ -1442,50 +1442,50 @@ GetEfiGlobalVariable2 (
 }\r
 \r
 /**\r
-  Returns a pointer to an allocated buffer that contains the best matching language \r
-  from a set of supported languages.  \r
-  \r
-  This function supports both ISO 639-2 and RFC 4646 language codes, but language \r
-  code types may not be mixed in a single call to this function.  The language \r
-  code returned is allocated using AllocatePool().  The caller is responsible for \r
+  Returns a pointer to an allocated buffer that contains the best matching language\r
+  from a set of supported languages.\r
+\r
+  This function supports both ISO 639-2 and RFC 4646 language codes, but language\r
+  code types may not be mixed in a single call to this function.  The language\r
+  code returned is allocated using AllocatePool().  The caller is responsible for\r
   freeing the allocated buffer using FreePool().  This function supports a variable\r
-  argument list that allows the caller to pass in a prioritized list of language \r
-  codes to test against all the language codes in SupportedLanguages. \r
+  argument list that allows the caller to pass in a prioritized list of language\r
+  codes to test against all the language codes in SupportedLanguages.\r
 \r
   If SupportedLanguages is NULL, then ASSERT().\r
 \r
   @param[in]  SupportedLanguages  A pointer to a Null-terminated ASCII string that\r
-                                  contains a set of language codes in the format \r
+                                  contains a set of language codes in the format\r
                                   specified by Iso639Language.\r
-  @param[in]  Iso639Language      If TRUE, then all language codes are assumed to be\r
-                                  in ISO 639-2 format.  If FALSE, then all language\r
+  @param[in]  Iso639Language      If not zero, then all language codes are assumed to be\r
+                                  in ISO 639-2 format.  If zero, then all language\r
                                   codes are assumed to be in RFC 4646 language format\r
-  @param[in]  ...                 A variable argument list that contains pointers to \r
+  @param[in]  ...                 A variable argument list that contains pointers to\r
                                   Null-terminated ASCII strings that contain one or more\r
                                   language codes in the format specified by Iso639Language.\r
                                   The first language code from each of these language\r
                                   code lists is used to determine if it is an exact or\r
-                                  close match to any of the language codes in \r
+                                  close match to any of the language codes in\r
                                   SupportedLanguages.  Close matches only apply to RFC 4646\r
                                   language codes, and the matching algorithm from RFC 4647\r
-                                  is used to determine if a close match is present.  If \r
+                                  is used to determine if a close match is present.  If\r
                                   an exact or close match is found, then the matching\r
                                   language code from SupportedLanguages is returned.  If\r
                                   no matches are found, then the next variable argument\r
-                                  parameter is evaluated.  The variable argument list \r
+                                  parameter is evaluated.  The variable argument list\r
                                   is terminated by a NULL.\r
 \r
   @retval NULL   The best matching language could not be found in SupportedLanguages.\r
-  @retval NULL   There are not enough resources available to return the best matching \r
+  @retval NULL   There are not enough resources available to return the best matching\r
                  language.\r
-  @retval Other  A pointer to a Null-terminated ASCII string that is the best matching \r
+  @retval Other  A pointer to a Null-terminated ASCII string that is the best matching\r
                  language in SupportedLanguages.\r
 \r
 **/\r
 CHAR8 *\r
 EFIAPI\r
 GetBestLanguage (\r
-  IN CONST CHAR8  *SupportedLanguages, \r
+  IN CONST CHAR8  *SupportedLanguages,\r
   IN UINTN        Iso639Language,\r
   ...\r
   )\r
@@ -1510,7 +1510,7 @@ GetBestLanguage (
     //\r
     // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r
     //\r
-    if (!Iso639Language) {\r
+    if (Iso639Language == 0) {\r
       for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r
     }\r
 \r
@@ -1525,7 +1525,7 @@ GetBestLanguage (
         //\r
         // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r
         //\r
-        if (!Iso639Language) {\r
+        if (Iso639Language == 0) {\r
           //\r
           // Skip ';' characters in Supported\r
           //\r
@@ -1557,14 +1557,14 @@ GetBestLanguage (
         }\r
       }\r
 \r
-      if (Iso639Language) {\r
+      if (Iso639Language != 0) {\r
         //\r
         // If ISO 639 mode, then each language can only be tested once\r
         //\r
         LanguageLength = 0;\r
       } else {\r
         //\r
-        // If RFC 4646 mode, then trim Language from the right to the next '-' character \r
+        // If RFC 4646 mode, then trim Language from the right to the next '-' character\r
         //\r
         for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r
       }\r
@@ -1573,7 +1573,7 @@ GetBestLanguage (
   VA_END (Args);\r
 \r
   //\r
-  // No matches were found \r
+  // No matches were found\r
   //\r
   return NULL;\r
 }\r
@@ -1691,3 +1691,202 @@ EfiLocateProtocolBuffer (
 \r
   return EFI_SUCCESS;\r
 }\r
+\r
+/**\r
+  Open or create a file or directory, possibly creating the chain of\r
+  directories leading up to the directory.\r
+\r
+  EfiOpenFileByDevicePath() first locates EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on\r
+  FilePath, and opens the root directory of that filesystem with\r
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume().\r
+\r
+  On the remaining device path, the longest initial sequence of\r
+  FILEPATH_DEVICE_PATH nodes is node-wise traversed with\r
+  EFI_FILE_PROTOCOL.Open().\r
+\r
+  (As a consequence, if OpenMode includes EFI_FILE_MODE_CREATE, and Attributes\r
+  includes EFI_FILE_DIRECTORY, and each FILEPATH_DEVICE_PATH specifies a single\r
+  pathname component, then EfiOpenFileByDevicePath() ensures that the specified\r
+  series of subdirectories exist on return.)\r
+\r
+  The EFI_FILE_PROTOCOL identified by the last FILEPATH_DEVICE_PATH node is\r
+  output to the caller; intermediate EFI_FILE_PROTOCOL instances are closed. If\r
+  there are no FILEPATH_DEVICE_PATH nodes past the node that identifies the\r
+  filesystem, then the EFI_FILE_PROTOCOL of the root directory of the\r
+  filesystem is output to the caller. If a device path node that is different\r
+  from FILEPATH_DEVICE_PATH is encountered relative to the filesystem, the\r
+  traversal is stopped with an error, and a NULL EFI_FILE_PROTOCOL is output.\r
+\r
+  @param[in,out] FilePath  On input, the device path to the file or directory\r
+                           to open or create. The caller is responsible for\r
+                           ensuring that the device path pointed-to by FilePath\r
+                           is well-formed. On output, FilePath points one past\r
+                           the last node in the original device path that has\r
+                           been successfully processed. FilePath is set on\r
+                           output even if EfiOpenFileByDevicePath() returns an\r
+                           error.\r
+\r
+  @param[out] File         On error, File is set to NULL. On success, File is\r
+                           set to the EFI_FILE_PROTOCOL of the root directory\r
+                           of the filesystem, if there are no\r
+                           FILEPATH_DEVICE_PATH nodes in FilePath; otherwise,\r
+                           File is set to the EFI_FILE_PROTOCOL identified by\r
+                           the last node in FilePath.\r
+\r
+  @param[in] OpenMode      The OpenMode parameter to pass to\r
+                           EFI_FILE_PROTOCOL.Open().\r
+\r
+  @param[in] Attributes    The Attributes parameter to pass to\r
+                           EFI_FILE_PROTOCOL.Open().\r
+\r
+  @retval EFI_SUCCESS            The file or directory has been opened or\r
+                                 created.\r
+\r
+  @retval EFI_INVALID_PARAMETER  FilePath is NULL; or File is NULL; or FilePath\r
+                                 contains a device path node, past the node\r
+                                 that identifies\r
+                                 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, that is not a\r
+                                 FILEPATH_DEVICE_PATH node.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   Memory allocation failed.\r
+\r
+  @return                        Error codes propagated from the\r
+                                 LocateDevicePath() and OpenProtocol() boot\r
+                                 services, and from the\r
+                                 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume()\r
+                                 and EFI_FILE_PROTOCOL.Open() member functions.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EfiOpenFileByDevicePath (\r
+  IN OUT EFI_DEVICE_PATH_PROTOCOL  **FilePath,\r
+  OUT    EFI_FILE_PROTOCOL         **File,\r
+  IN     UINT64                    OpenMode,\r
+  IN     UINT64                    Attributes\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  EFI_HANDLE                      FileSystemHandle;\r
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;\r
+  EFI_FILE_PROTOCOL               *LastFile;\r
+  FILEPATH_DEVICE_PATH            *FilePathNode;\r
+  CHAR16                          *AlignedPathName;\r
+  CHAR16                          *PathName;\r
+  EFI_FILE_PROTOCOL               *NextFile;\r
+\r
+  if (File == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  *File = NULL;\r
+\r
+  if (FilePath == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Look up the filesystem.\r
+  //\r
+  Status = gBS->LocateDevicePath (\r
+                  &gEfiSimpleFileSystemProtocolGuid,\r
+                  FilePath,\r
+                  &FileSystemHandle\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  Status = gBS->OpenProtocol (\r
+                  FileSystemHandle,\r
+                  &gEfiSimpleFileSystemProtocolGuid,\r
+                  (VOID **)&FileSystem,\r
+                  gImageHandle,\r
+                  NULL,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Open the root directory of the filesystem. After this operation succeeds,\r
+  // we have to release LastFile on error.\r
+  //\r
+  Status = FileSystem->OpenVolume (FileSystem, &LastFile);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Traverse the device path nodes relative to the filesystem.\r
+  //\r
+  while (!IsDevicePathEnd (*FilePath)) {\r
+    if (DevicePathType (*FilePath) != MEDIA_DEVICE_PATH ||\r
+        DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto CloseLastFile;\r
+    }\r
+    FilePathNode = (FILEPATH_DEVICE_PATH *)*FilePath;\r
+\r
+    //\r
+    // FilePathNode->PathName may be unaligned, and the UEFI specification\r
+    // requires pointers that are passed to protocol member functions to be\r
+    // aligned. Create an aligned copy of the pathname if necessary.\r
+    //\r
+    if ((UINTN)FilePathNode->PathName % sizeof *FilePathNode->PathName == 0) {\r
+      AlignedPathName = NULL;\r
+      PathName = FilePathNode->PathName;\r
+    } else {\r
+      AlignedPathName = AllocateCopyPool (\r
+                          (DevicePathNodeLength (FilePathNode) -\r
+                           SIZE_OF_FILEPATH_DEVICE_PATH),\r
+                          FilePathNode->PathName\r
+                          );\r
+      if (AlignedPathName == NULL) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto CloseLastFile;\r
+      }\r
+      PathName = AlignedPathName;\r
+    }\r
+\r
+    //\r
+    // Open or create the file corresponding to the next pathname fragment.\r
+    //\r
+    Status = LastFile->Open (\r
+                         LastFile,\r
+                         &NextFile,\r
+                         PathName,\r
+                         OpenMode,\r
+                         Attributes\r
+                         );\r
+\r
+    //\r
+    // Release any AlignedPathName on both error and success paths; PathName is\r
+    // no longer needed.\r
+    //\r
+    if (AlignedPathName != NULL) {\r
+      FreePool (AlignedPathName);\r
+    }\r
+    if (EFI_ERROR (Status)) {\r
+      goto CloseLastFile;\r
+    }\r
+\r
+    //\r
+    // Advance to the next device path node.\r
+    //\r
+    LastFile->Close (LastFile);\r
+    LastFile = NextFile;\r
+    *FilePath = NextDevicePathNode (FilePathNode);\r
+  }\r
+\r
+  *File = LastFile;\r
+  return EFI_SUCCESS;\r
+\r
+CloseLastFile:\r
+  LastFile->Close (LastFile);\r
+\r
+  //\r
+  // We are on the error path; we must have set an error Status for returning\r
+  // to the caller.\r
+  //\r
+  ASSERT (EFI_ERROR (Status));\r
+  return Status;\r
+}\r