From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.24; helo=mga09.intel.com; envelope-from=jiewen.yao@intel.com; receiver=edk2-devel@lists.01.org Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) (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 D364D2097F553 for ; Wed, 18 Jul 2018 16:10:16 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Jul 2018 16:10:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,371,1526367600"; d="scan'208";a="217137593" Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by orsmga004.jf.intel.com with ESMTP; 18 Jul 2018 16:10:13 -0700 Received: from fmsmsx113.amr.corp.intel.com (10.18.116.7) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 18 Jul 2018 16:10:13 -0700 Received: from shsmsx152.ccr.corp.intel.com (10.239.6.52) by FMSMSX113.amr.corp.intel.com (10.18.116.7) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 18 Jul 2018 16:10:13 -0700 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.124]) by SHSMSX152.ccr.corp.intel.com ([169.254.6.173]) with mapi id 14.03.0319.002; Thu, 19 Jul 2018 07:10:11 +0800 From: "Yao, Jiewen" To: Laszlo Ersek CC: edk2-devel-01 , "Zhang, Chao B" , "Dong, Eric" , "Carsey, Jaben" , "Wu, Jiaxin" , "Gao, Liming" , "Kinney, Michael D" , Roman Bacik , "Ni, Ruiyu" , "Fu, Siyuan" , "Zeng, Star" , "Yao, Jiewen" Thread-Topic: [PATCH 1/6] MdePkg/UefiLib: introduce EfiOpenFileByDevicePath() Thread-Index: AQHUHtkHk9gu9//slUuamVfCosiu+6SVm0HR Date: Wed, 18 Jul 2018 23:10:10 +0000 Message-ID: <097C42DC-FC1C-4A85-BE9D-C826F9ADBADB@intel.com> References: <20180718205043.17574-1-lersek@redhat.com>, <20180718205043.17574-2-lersek@redhat.com> In-Reply-To: <20180718205043.17574-2-lersek@redhat.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: MIME-Version: 1.0 Subject: Re: [PATCH 1/6] 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: , X-List-Received-Date: Wed, 18 Jul 2018 23:10:17 -0000 Content-Language: zh-CN Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: quoted-printable Thanks Laszlo. Would you please add one line comment on the FilePath, to describe if the F= ilePath is internal input or external input? As such the API consumer can k= now if caller=1B$B!G=1B(Bs responsibility to verify it or callee=1B$B!G=1B(= Bs responsibility.=20 For example, if the caller gets path from a read write variable, and input = it directly, the this API need validate before use.=20 If the caller already does the verification, then this API can use it direc= tly.=20 Sanity check is just for the format, not the content.=20 The question I have is: Where should the sanity check be? thank you! Yao, Jiewen > =1B$B:_=1B(B 2018=1B$BG/=1B(B7=1B$B7n=1B(B19=1B$BF|!$>e8a=1B(B4:50=1B$B!$= =1B(BLaszlo Ersek =1B$B=20 > The EfiOpenFileByDevicePath() function centralizes functionality from >=20 > - MdeModulePkg/Universal/Disk/RamDiskDxe > - NetworkPkg/TlsAuthConfigDxe > - SecurityPkg/VariableAuthenticated/SecureBootConfigDxe > - ShellPkg/Library/UefiShellLib >=20 > unifying the implementation and fixing various bugs. >=20 > 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 > --- > MdePkg/Library/UefiLib/UefiLib.inf | 1 + > MdePkg/Include/Library/UefiLib.h | 86 ++++++++ > MdePkg/Library/UefiLib/UefiLib.c | 226 ++++++++++++++++++++ > 3 files changed, 313 insertions(+) >=20 > diff --git a/MdePkg/Library/UefiLib/UefiLib.inf b/MdePkg/Library/UefiLib/= UefiLib.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 gEfiGraphicsOutputProt= ocolGuid uninstalled > gEfiComponentNameProtocolGuid | NOT gEfiMdePkgTokenSpaceGuid.PcdCompon= entNameDisable ## SOMETIMES_PRODUCES # User chooses to produce it > gEfiComponentName2ProtocolGuid | NOT gEfiMdePkgTokenSpaceGuid.PcdCompon= entName2Disable ## SOMETIMES_PRODUCES # User chooses to produce it > diff --git a/MdePkg/Include/Library/UefiLib.h b/MdePkg/Include/Library/Ue= fiLib.h > index 7c6fde620c74..2468bf2aee80 100644 > --- a/MdePkg/Include/Library/UefiLib.h > +++ b/MdePkg/Include/Library/UefiLib.h > @@ -33,6 +33,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITH= ER EXPRESS OR IMPLIED. > #include > #include > #include > +#include > +#include >=20 > #include >=20 > @@ -1520,4 +1522,88 @@ 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_PROTOCO= L 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 m= asks > + EFI_FILE_MODE_CREATE out of OpenMode, and passes 0 for Attributes. If > + EFI_FILE_PROTOCOL.Open() fails, and OpenMode includes EFI_FILE_MODE_CR= EATE, > + then the operation is retried with the caller's OpenMode and Attribute= s > + unmodified. > + > + (As a consequence, if OpenMode includes EFI_FILE_MODE_CREATE, and Attr= ibutes > + includes EFI_FILE_DIRECTORY, and each FILEPATH_DEVICE_PATH specifies a= single > + pathname component, then EfiOpenFileByDevicePath() ensures that the sp= ecified > + 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 clo= sed. 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 diff= erent > + from FILEPATH_DEVICE_PATH is encountered relative to the filesystem, t= he > + traversal is stopped with an error, and a NULL EFI_FILE_PROTOCOL is ou= tput. > + > + @param[in,out] FilePath On input, the device path to the file or dire= ctory > + to open or create. On output, FilePath points= one > + past the last node in the original device pat= h that > + has been successfully processed. FilePath is = set on > + output even if EfiOpenFileByDevicePath() retu= rns an > + error. > + > + @param[out] File On error, File is set to NULL. On success, Fi= le is > + set to the EFI_FILE_PROTOCOL of the root dire= ctory > + of the filesystem, if there are no > + FILEPATH_DEVICE_PATH nodes in FilePath; other= wise, > + File is set to the EFI_FILE_PROTOCOL identifi= ed 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 spe= cified > + pathname fragment with EFI_FILE_MODE_CREATE m= asked > + out of OpenMode and with Attributes set to 0,= and > + only retries the operation with EFI_FILE_MODE= _CREATE > + unmasked and Attributes propagated if the fir= st open > + attempt fails. > + > + @param[in] Attributes The Attributes parameter to pass to > + EFI_FILE_PROTOCOL.Open(), when EFI_FILE_MODE_= CREATE > + is propagated unmasked in OpenMode. > + > + @retval EFI_SUCCESS The file or directory has been opened o= r > + created. > + > + @retval EFI_INVALID_PARAMETER FilePath is NULL; or File is NULL; or F= ilePath > + contains a device path node, past the n= ode > + that identifies > + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, that i= s not a > + FILEPATH_DEVICE_PATH node. > + > + @retval EFI_OUT_OF_RESOURCES Memory allocation failed. > + > + @return Error codes propagated from the > + LocateDevicePath() and OpenProtocol() b= oot > + services, and from the > + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVol= ume() > + and EFI_FILE_PROTOCOL.Open() member fun= ctions. > +**/ > +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/Ue= fiLib.c > index 828a54ce7a97..d3e290178cd9 100644 > --- a/MdePkg/Library/UefiLib/UefiLib.c > +++ b/MdePkg/Library/UefiLib/UefiLib.c > @@ -1719,3 +1719,229 @@ 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_PROTOCO= L 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 m= asks > + EFI_FILE_MODE_CREATE out of OpenMode, and passes 0 for Attributes. If > + EFI_FILE_PROTOCOL.Open() fails, and OpenMode includes EFI_FILE_MODE_CR= EATE, > + then the operation is retried with the caller's OpenMode and Attribute= s > + unmodified. > + > + (As a consequence, if OpenMode includes EFI_FILE_MODE_CREATE, and Attr= ibutes > + includes EFI_FILE_DIRECTORY, and each FILEPATH_DEVICE_PATH specifies a= single > + pathname component, then EfiOpenFileByDevicePath() ensures that the sp= ecified > + 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 clo= sed. 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 diff= erent > + from FILEPATH_DEVICE_PATH is encountered relative to the filesystem, t= he > + traversal is stopped with an error, and a NULL EFI_FILE_PROTOCOL is ou= tput. > + > + @param[in,out] FilePath On input, the device path to the file or dire= ctory > + to open or create. On output, FilePath points= one > + past the last node in the original device pat= h that > + has been successfully processed. FilePath is = set on > + output even if EfiOpenFileByDevicePath() retu= rns an > + error. > + > + @param[out] File On error, File is set to NULL. On success, Fi= le is > + set to the EFI_FILE_PROTOCOL of the root dire= ctory > + of the filesystem, if there are no > + FILEPATH_DEVICE_PATH nodes in FilePath; other= wise, > + File is set to the EFI_FILE_PROTOCOL identifi= ed 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 spe= cified > + pathname fragment with EFI_FILE_MODE_CREATE m= asked > + out of OpenMode and with Attributes set to 0,= and > + only retries the operation with EFI_FILE_MODE= _CREATE > + unmasked and Attributes propagated if the fir= st open > + attempt fails. > + > + @param[in] Attributes The Attributes parameter to pass to > + EFI_FILE_PROTOCOL.Open(), when EFI_FILE_MODE_= CREATE > + is propagated unmasked in OpenMode. > + > + @retval EFI_SUCCESS The file or directory has been opened o= r > + created. > + > + @retval EFI_INVALID_PARAMETER FilePath is NULL; or File is NULL; or F= ilePath > + contains a device path node, past the n= ode > + that identifies > + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, that i= s not a > + FILEPATH_DEVICE_PATH node. > + > + @retval EFI_OUT_OF_RESOURCES Memory allocation failed. > + > + @return Error codes propagated from the > + LocateDevicePath() and OpenProtocol() b= oot > + services, and from the > + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVol= ume() > + and EFI_FILE_PROTOCOL.Open() member fun= ctions. > +**/ > +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; > + > + 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 suc= ceeds, > + // 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)) { > + // > + // Keep local variables that relate to the current device path node = tightly > + // scoped. > + // > + FILEPATH_DEVICE_PATH *FilePathNode; > + CHAR16 *AlignedPathName; > + CHAR16 *PathName; > + EFI_FILE_PROTOCOL *NextFile; > + > + 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 specificati= on > + // 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 = out 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 th= e 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; Path= Name 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); > + > + ASSERT (EFI_ERROR (Status)); > + return Status; > +} > --=20 > 2.14.1.3.gb7cf6e02401b >=20 >=20