From nobody Thu Nov 14 17:14:31 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1533298547891648.1685808317517; Fri, 3 Aug 2018 05:15:47 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 27CFE210D8507; Fri, 3 Aug 2018 05:15:46 -0700 (PDT) Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 16144210D8503 for ; Fri, 3 Aug 2018 05:15:45 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4B7CF402347F; Fri, 3 Aug 2018 12:15:44 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-120-198.rdu2.redhat.com [10.10.120.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id A0C1D7C38; Fri, 3 Aug 2018 12:15:41 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=66.187.233.73; helo=mx1.redhat.com; envelope-from=lersek@redhat.com; receiver=edk2-devel@lists.01.org From: Laszlo Ersek To: edk2-devel-01 Date: Fri, 3 Aug 2018 14:15:31 +0200 Message-Id: <20180803121537.32123-2-lersek@redhat.com> In-Reply-To: <20180803121537.32123-1-lersek@redhat.com> References: <20180803121537.32123-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 03 Aug 2018 12:15:44 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 03 Aug 2018 12:15:44 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'lersek@redhat.com' RCPT:'' Subject: [edk2] [PATCH v2 1/7] MdePkg/UefiLib: introduce EfiOpenFileByDevicePath() X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ruiyu Ni , Eric Dong , Liming Gao , Michael D Kinney , Jiaxin Wu , Jiewen Yao , Star Zeng , Jaben Carsey , Siyuan Fu , Chao Zhang MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RDMRC_1 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The EfiOpenFileByDevicePath() function centralizes functionality from - MdeModulePkg/Universal/Disk/RamDiskDxe - NetworkPkg/TlsAuthConfigDxe - SecurityPkg/VariableAuthenticated/SecureBootConfigDxe - ShellPkg/Library/UefiShellLib unifying the implementation and fixing various bugs. (Ray suggested that we eliminate the special handling of EFI_FILE_MODE_CREATE in the "OpenMode" input parameter as well. We plan to implement that separately, under .) Cc: Chao Zhang Cc: Eric Dong Cc: Jaben Carsey Cc: Jiaxin Wu Cc: Jiewen Yao Cc: Liming Gao Cc: Michael D Kinney Cc: Roman Bacik Cc: Ruiyu Ni Cc: Siyuan Fu Cc: Star Zeng Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1008 Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Laszlo Ersek Reviewed-by: Ruiyu Ni --- Notes: v2: =20 - add the following sentence to the FilePath parameter's documentation: "The caller is responsible for ensuring that the device path pointed-to by FilePath is well-formed." [Jiewen] =20 - lift the definition of the local variables that relate to the current device path node from the loop to the top of the function ("FilePathNode", "AlignedPathName", "PathName", "NextFile") [Ray] =20 - report new TianoCore BZ -- "don't distinguish EFI_FILE_MODE_CREATE in EfiOpenFileByDevicePath() / OpenMode" --, and reference it in the commit message, as future work [Ray] =20 - explain ASSERT (EFI_ERROR (Status)) -- as opposed to ASSERT_EFI_ERROR (Status) -- with a code comment [Jaben, Ray] =20 - pick up none of the received Reviewed-by tags (namely from Jaben, Liming, and Ray) due to the *sum* of changes above MdePkg/Library/UefiLib/UefiLib.inf | 1 + MdePkg/Include/Library/UefiLib.h | 88 ++++++++ MdePkg/Library/UefiLib/UefiLib.c | 227 ++++++++++++++++++++ 3 files changed, 316 insertions(+) diff --git a/MdePkg/Library/UefiLib/UefiLib.inf b/MdePkg/Library/UefiLib/Ue= fiLib.inf index f69f0a43b576..a6c739ef3d6d 100644 --- a/MdePkg/Library/UefiLib/UefiLib.inf +++ b/MdePkg/Library/UefiLib/UefiLib.inf @@ -68,6 +68,7 @@ [Protocols] gEfiSimpleTextOutProtocolGuid ## SOMETIMES_CONSUMES gEfiGraphicsOutputProtocolGuid ## SOMETIMES_CONSUMES gEfiHiiFontProtocolGuid ## SOMETIMES_CONSUMES + gEfiSimpleFileSystemProtocolGuid ## SOMETIMES_CONSUMES gEfiUgaDrawProtocolGuid | gEfiMdePkgTokenSpaceGuid.PcdUgaConsumeSupport = ## SOMETIMES_CONSUMES # Consumes if gEfiGraphicsOutputProto= colGuid uninstalled gEfiComponentNameProtocolGuid | NOT gEfiMdePkgTokenSpaceGuid.PcdCompone= ntNameDisable ## SOMETIMES_PRODUCES # User chooses to produce it gEfiComponentName2ProtocolGuid | NOT gEfiMdePkgTokenSpaceGuid.PcdCompone= ntName2Disable ## SOMETIMES_PRODUCES # User chooses to produce it diff --git a/MdePkg/Include/Library/UefiLib.h b/MdePkg/Include/Library/Uefi= Lib.h index 7c6fde620c74..f950f1c9c648 100644 --- a/MdePkg/Include/Library/UefiLib.h +++ b/MdePkg/Include/Library/UefiLib.h @@ -33,6 +33,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER= EXPRESS OR IMPLIED. #include #include #include +#include +#include =20 #include =20 @@ -1520,4 +1522,90 @@ EfiLocateProtocolBuffer ( OUT UINTN *NoProtocols, OUT VOID ***Buffer ); + +/** + Open or create a file or directory, possibly creating the chain of + directories leading up to the directory. + + EfiOpenFileByDevicePath() first locates EFI_SIMPLE_FILE_SYSTEM_PROTOCOL = on + FilePath, and opens the root directory of that filesystem with + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume(). + + On the remaining device path, the longest initial sequence of + FILEPATH_DEVICE_PATH nodes is node-wise traversed with + EFI_FILE_PROTOCOL.Open(). For the pathname fragment specified by each + traversed FILEPATH_DEVICE_PATH node, EfiOpenFileByDevicePath() first mas= ks + EFI_FILE_MODE_CREATE out of OpenMode, and passes 0 for Attributes. If + EFI_FILE_PROTOCOL.Open() fails, and OpenMode includes EFI_FILE_MODE_CREA= TE, + then the operation is retried with the caller's OpenMode and Attributes + unmodified. + + (As a consequence, if OpenMode includes EFI_FILE_MODE_CREATE, and Attrib= utes + includes EFI_FILE_DIRECTORY, and each FILEPATH_DEVICE_PATH specifies a s= ingle + pathname component, then EfiOpenFileByDevicePath() ensures that the spec= ified + series of subdirectories exist on return.) + + The EFI_FILE_PROTOCOL identified by the last FILEPATH_DEVICE_PATH node is + output to the caller; intermediate EFI_FILE_PROTOCOL instances are close= d. If + there are no FILEPATH_DEVICE_PATH nodes past the node that identifies the + filesystem, then the EFI_FILE_PROTOCOL of the root directory of the + filesystem is output to the caller. If a device path node that is differ= ent + from FILEPATH_DEVICE_PATH is encountered relative to the filesystem, the + traversal is stopped with an error, and a NULL EFI_FILE_PROTOCOL is outp= ut. + + @param[in,out] FilePath On input, the device path to the file or direct= ory + to open or create. The caller is responsible for + ensuring that the device path pointed-to by Fil= ePath + is well-formed. On output, FilePath points one = past + the last node in the original device path that = has + been successfully processed. FilePath is set on + output even if EfiOpenFileByDevicePath() return= s an + error. + + @param[out] File On error, File is set to NULL. On success, File= is + set to the EFI_FILE_PROTOCOL of the root direct= ory + of the filesystem, if there are no + FILEPATH_DEVICE_PATH nodes in FilePath; otherwi= se, + File is set to the EFI_FILE_PROTOCOL identified= by + the last node in FilePath. + + @param[in] OpenMode The OpenMode parameter to pass to + EFI_FILE_PROTOCOL.Open(). For each + FILEPATH_DEVICE_PATH node in FilePath, + EfiOpenFileByDevicePath() first opens the speci= fied + pathname fragment with EFI_FILE_MODE_CREATE mas= ked + out of OpenMode and with Attributes set to 0, a= nd + only retries the operation with EFI_FILE_MODE_C= REATE + unmasked and Attributes propagated if the first= open + attempt fails. + + @param[in] Attributes The Attributes parameter to pass to + EFI_FILE_PROTOCOL.Open(), when EFI_FILE_MODE_CR= EATE + is propagated unmasked in OpenMode. + + @retval EFI_SUCCESS The file or directory has been opened or + created. + + @retval EFI_INVALID_PARAMETER FilePath is NULL; or File is NULL; or Fil= ePath + contains a device path node, past the node + that identifies + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, that is = not a + FILEPATH_DEVICE_PATH node. + + @retval EFI_OUT_OF_RESOURCES Memory allocation failed. + + @return Error codes propagated from the + LocateDevicePath() and OpenProtocol() boot + services, and from the + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolum= e() + and EFI_FILE_PROTOCOL.Open() member funct= ions. +**/ +EFI_STATUS +EFIAPI +EfiOpenFileByDevicePath ( + IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath, + OUT EFI_FILE_PROTOCOL **File, + IN UINT64 OpenMode, + IN UINT64 Attributes + ); #endif diff --git a/MdePkg/Library/UefiLib/UefiLib.c b/MdePkg/Library/UefiLib/Uefi= Lib.c index 828a54ce7a97..7bcac5613768 100644 --- a/MdePkg/Library/UefiLib/UefiLib.c +++ b/MdePkg/Library/UefiLib/UefiLib.c @@ -1719,3 +1719,230 @@ EfiLocateProtocolBuffer ( =20 return EFI_SUCCESS; } + +/** + Open or create a file or directory, possibly creating the chain of + directories leading up to the directory. + + EfiOpenFileByDevicePath() first locates EFI_SIMPLE_FILE_SYSTEM_PROTOCOL = on + FilePath, and opens the root directory of that filesystem with + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume(). + + On the remaining device path, the longest initial sequence of + FILEPATH_DEVICE_PATH nodes is node-wise traversed with + EFI_FILE_PROTOCOL.Open(). For the pathname fragment specified by each + traversed FILEPATH_DEVICE_PATH node, EfiOpenFileByDevicePath() first mas= ks + EFI_FILE_MODE_CREATE out of OpenMode, and passes 0 for Attributes. If + EFI_FILE_PROTOCOL.Open() fails, and OpenMode includes EFI_FILE_MODE_CREA= TE, + then the operation is retried with the caller's OpenMode and Attributes + unmodified. + + (As a consequence, if OpenMode includes EFI_FILE_MODE_CREATE, and Attrib= utes + includes EFI_FILE_DIRECTORY, and each FILEPATH_DEVICE_PATH specifies a s= ingle + pathname component, then EfiOpenFileByDevicePath() ensures that the spec= ified + series of subdirectories exist on return.) + + The EFI_FILE_PROTOCOL identified by the last FILEPATH_DEVICE_PATH node is + output to the caller; intermediate EFI_FILE_PROTOCOL instances are close= d. If + there are no FILEPATH_DEVICE_PATH nodes past the node that identifies the + filesystem, then the EFI_FILE_PROTOCOL of the root directory of the + filesystem is output to the caller. If a device path node that is differ= ent + from FILEPATH_DEVICE_PATH is encountered relative to the filesystem, the + traversal is stopped with an error, and a NULL EFI_FILE_PROTOCOL is outp= ut. + + @param[in,out] FilePath On input, the device path to the file or direct= ory + to open or create. The caller is responsible for + ensuring that the device path pointed-to by Fil= ePath + is well-formed. On output, FilePath points one = past + the last node in the original device path that = has + been successfully processed. FilePath is set on + output even if EfiOpenFileByDevicePath() return= s an + error. + + @param[out] File On error, File is set to NULL. On success, File= is + set to the EFI_FILE_PROTOCOL of the root direct= ory + of the filesystem, if there are no + FILEPATH_DEVICE_PATH nodes in FilePath; otherwi= se, + File is set to the EFI_FILE_PROTOCOL identified= by + the last node in FilePath. + + @param[in] OpenMode The OpenMode parameter to pass to + EFI_FILE_PROTOCOL.Open(). For each + FILEPATH_DEVICE_PATH node in FilePath, + EfiOpenFileByDevicePath() first opens the speci= fied + pathname fragment with EFI_FILE_MODE_CREATE mas= ked + out of OpenMode and with Attributes set to 0, a= nd + only retries the operation with EFI_FILE_MODE_C= REATE + unmasked and Attributes propagated if the first= open + attempt fails. + + @param[in] Attributes The Attributes parameter to pass to + EFI_FILE_PROTOCOL.Open(), when EFI_FILE_MODE_CR= EATE + is propagated unmasked in OpenMode. + + @retval EFI_SUCCESS The file or directory has been opened or + created. + + @retval EFI_INVALID_PARAMETER FilePath is NULL; or File is NULL; or Fil= ePath + contains a device path node, past the node + that identifies + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, that is = not a + FILEPATH_DEVICE_PATH node. + + @retval EFI_OUT_OF_RESOURCES Memory allocation failed. + + @return Error codes propagated from the + LocateDevicePath() and OpenProtocol() boot + services, and from the + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolum= e() + and EFI_FILE_PROTOCOL.Open() member funct= ions. +**/ +EFI_STATUS +EFIAPI +EfiOpenFileByDevicePath ( + IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath, + OUT EFI_FILE_PROTOCOL **File, + IN UINT64 OpenMode, + IN UINT64 Attributes + ) +{ + EFI_STATUS Status; + EFI_HANDLE FileSystemHandle; + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem; + EFI_FILE_PROTOCOL *LastFile; + FILEPATH_DEVICE_PATH *FilePathNode; + CHAR16 *AlignedPathName; + CHAR16 *PathName; + EFI_FILE_PROTOCOL *NextFile; + + if (File =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + *File =3D NULL; + + if (FilePath =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Look up the filesystem. + // + Status =3D gBS->LocateDevicePath ( + &gEfiSimpleFileSystemProtocolGuid, + FilePath, + &FileSystemHandle + ); + if (EFI_ERROR (Status)) { + return Status; + } + Status =3D gBS->OpenProtocol ( + FileSystemHandle, + &gEfiSimpleFileSystemProtocolGuid, + (VOID **)&FileSystem, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Open the root directory of the filesystem. After this operation succe= eds, + // we have to release LastFile on error. + // + Status =3D FileSystem->OpenVolume (FileSystem, &LastFile); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Traverse the device path nodes relative to the filesystem. + // + while (!IsDevicePathEnd (*FilePath)) { + if (DevicePathType (*FilePath) !=3D MEDIA_DEVICE_PATH || + DevicePathSubType (*FilePath) !=3D MEDIA_FILEPATH_DP) { + Status =3D EFI_INVALID_PARAMETER; + goto CloseLastFile; + } + FilePathNode =3D (FILEPATH_DEVICE_PATH *)*FilePath; + + // + // FilePathNode->PathName may be unaligned, and the UEFI specification + // requires pointers that are passed to protocol member functions to be + // aligned. Create an aligned copy of the pathname if necessary. + // + if ((UINTN)FilePathNode->PathName % sizeof *FilePathNode->PathName =3D= =3D 0) { + AlignedPathName =3D NULL; + PathName =3D FilePathNode->PathName; + } else { + AlignedPathName =3D AllocateCopyPool ( + (DevicePathNodeLength (FilePathNode) - + SIZE_OF_FILEPATH_DEVICE_PATH), + FilePathNode->PathName + ); + if (AlignedPathName =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto CloseLastFile; + } + PathName =3D AlignedPathName; + } + + // + // Open the next pathname fragment with EFI_FILE_MODE_CREATE masked ou= t and + // with Attributes set to 0. + // + Status =3D LastFile->Open ( + LastFile, + &NextFile, + PathName, + OpenMode & ~(UINT64)EFI_FILE_MODE_CREATE, + 0 + ); + + // + // Retry with EFI_FILE_MODE_CREATE and the original Attributes if the = first + // attempt failed, and the caller specified EFI_FILE_MODE_CREATE. + // + if (EFI_ERROR (Status) && (OpenMode & EFI_FILE_MODE_CREATE) !=3D 0) { + Status =3D LastFile->Open ( + LastFile, + &NextFile, + PathName, + OpenMode, + Attributes + ); + } + + // + // Release any AlignedPathName on both error and success paths; PathNa= me is + // no longer needed. + // + if (AlignedPathName !=3D NULL) { + FreePool (AlignedPathName); + } + if (EFI_ERROR (Status)) { + goto CloseLastFile; + } + + // + // Advance to the next device path node. + // + LastFile->Close (LastFile); + LastFile =3D NextFile; + *FilePath =3D NextDevicePathNode (FilePathNode); + } + + *File =3D LastFile; + return EFI_SUCCESS; + +CloseLastFile: + LastFile->Close (LastFile); + + // + // We are on the error path; we must have set an error Status for return= ing + // to the caller. + // + ASSERT (EFI_ERROR (Status)); + return Status; +} --=20 2.14.1.3.gb7cf6e02401b _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel