Topics

[PATCH v5 1/1] ShellPkg/DynamicCommand: add HttpDynamicCommand


Vladimir Olovyannikov
 

Introduce an http client utilizing EDK2 HTTP protocol, to
allow fast image downloading from http/https servers.
HTTP download speed is usually faster than tftp.
The client is based on the same approach as tftp dynamic command, and
uses the same UEFI Shell command line parameters. This makes it easy
integrating http into existing UEFI Shell scripts.
Note that to enable HTTP download, feature Pcd
gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections must
be set to TRUE.
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2860

Signed-off-by: Vladimir Olovyannikov <vladimir.olovyannikov@...>
CC: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>
CC: Laszlo Ersek <lersek@...>
Cc: Zhichao Gao <zhichao.gao@...>
Cc: Maciej Rabeda <maciej.rabeda@...>
Cc: Jiaxin Wu <jiaxin.wu@...>
Cc: Siyuan Fu <@sfu5>
Cc: Ray Ni <ray.ni@...>
Cc: Liming Gao <liming.gao@...>
Cc: Nd <nd@...>
---
CryptoPkg/Library/OpensslLib/openssl | 2 +-
.../DynamicCommand/HttpDynamicCommand/Http.c | 1715 +++++++++++++++++
.../DynamicCommand/HttpDynamicCommand/Http.h | 89 +
.../HttpDynamicCommand/Http.uni | 117 ++
.../HttpDynamicCommand/HttpApp.c | 53 +
.../HttpDynamicCommand/HttpApp.inf | 58 +
.../HttpDynamicCommand/HttpDynamicCommand.c | 134 ++
.../HttpDynamicCommand/HttpDynamicCommand.inf | 63 +
ShellPkg/Include/Guid/ShellLibHiiGuid.h | 5 +
ShellPkg/ShellPkg.dec | 1 +
ShellPkg/ShellPkg.dsc | 5 +
11 files changed, 2241 insertions(+), 1 deletion(-)
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicC=
ommand.c
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicC=
ommand.inf

diff --git a/CryptoPkg/Library/OpensslLib/openssl b/CryptoPkg/Library/Opens=
slLib/openssl
index e2e09d9fba11..c3656cc594da 160000
--- a/CryptoPkg/Library/OpensslLib/openssl
+++ b/CryptoPkg/Library/OpensslLib/openssl
@@ -1 +1 @@
-Subproject commit e2e09d9fba1187f8d6aafaa34d4172f56f1ffb72
+Subproject commit c3656cc594daac8167721dde7220f0e59ae146fc
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c b/ShellPkg/D=
ynamicCommand/HttpDynamicCommand/Http.c
new file mode 100644
index 000000000000..0565b07c3570
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
@@ -0,0 +1,1715 @@
+/** @file=0D
+ The implementation for the 'http' Shell command.=0D
+=0D
+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>=0D
+ Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved. <BR>=
=0D
+ (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>=0D
+ Copyright (c) 2020, Broadcom. All rights reserved. <BR>=0D
+=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+**/=0D
+=0D
+#include "Http.h"=0D
+=0D
+#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32=0D
+EFI_HII_HANDLE mHttpHiiHandle;=0D
+=0D
+/*=0D
+ Constant strings and definitions related to the message=0D
+ indicating the amount of progress in the dowloading of a HTTP file.=0D
+*/=0D
+=0D
+// Number of steps in the progression slider=0D
+#define HTTP_PROGRESS_SLIDER_STEPS \=0D
+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 3)=0D
+=0D
+// Size in number of characters plus one (final zero) of the message to=0D
+// indicate the progress of an HTTP download. The format is "[(progress sl=
ider:=0D
+// 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". The=
re=0D
+// are thus the number of characters in HTTP_PROGR_FRAME[] plus 11 charact=
ers=0D
+// (2 // spaces, "Kb" and seven characters for the number of KBytes).=0D
+#define HTTP_PROGRESS_MESSAGE_SIZE \=0D
+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) + 12)=0D
+=0D
+//=0D
+// Buffer size. Note that larger buffer does not mean better speed!=0D
+//=0D
+#define DEFAULT_BUF_SIZE SIZE_32KB=0D
+#define MAX_BUF_SIZE SIZE_4MB=0D
+=0D
+#define MIN_PARAM_COUNT 2=0D
+#define MAX_PARAM_COUNT 4=0D
+=0D
+#define TIMER_MAX_TIMEOUT_S 10=0D
+=0D
+// File name to use when URI ends with "/"=0D
+#define DEFAULT_HTML_FILE L"index.html"=0D
+#define DEFAULT_HTTP_PROTO L"http"=0D
+=0D
+// String to delete the HTTP progress message to be able to update it :=0D
+// (HTTP_PROGRESS_MESSAGE_SIZE-1) '\b'=0D
+#define HTTP_PROGRESS_DEL \=0D
+ L"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b=
\=0D
+\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"=0D
+=0D
+#define HTTP_KB L"\b\b\b\b\b\b\b\b\b\b"=0D
+// Frame for the progression slider=0D
+#define HTTP_PROGR_FRAME L"[ ]"=
=0D
+=0D
+// String descriptions for server errors=0D
+STATIC CONST CHAR16 *ErrStatusDesc[] =3D=0D
+{=0D
+ L"400 Bad Request",=0D
+ L"401 Unauthorized",=0D
+ L"402 Payment required",=0D
+ L"403 Forbidden",=0D
+ L"404 Not Found",=0D
+ L"405 Method not allowed",=0D
+ L"406 Not acceptable",=0D
+ L"407 Proxy authentication required",=0D
+ L"408 Request time out",=0D
+ L"409 Conflict",=0D
+ L"410 Gone",=0D
+ L"411 Length required",=0D
+ L"412 Precondition failed",=0D
+ L"413 Request entity too large",=0D
+ L"414 Request URI to large",=0D
+ L"415 Unsupported media type",=0D
+ L"416 Requested range not satisfied",=0D
+ L"417 Expectation failed",=0D
+ L"500 Internal server error",=0D
+ L"501 Not implemented",=0D
+ L"502 Bad gateway",=0D
+ L"503 Service unavailable",=0D
+ L"504 Gateway timeout",=0D
+ L"505 HTTP version not supported"=0D
+};=0D
+=0D
+// Local File Handle=0D
+STATIC SHELL_FILE_HANDLE mFileHandle =3D NULL;=0D
+=0D
+// Path of the local file, Unicode encoded=0D
+STATIC CONST CHAR16 *mLocalFilePath;=0D
+=0D
+STATIC BOOLEAN gRequestCallbackComplete =3D FALSE;=0D
+STATIC BOOLEAN gResponseCallbackComplete =3D FALSE;=0D
+=0D
+STATIC BOOLEAN gHttpError;=0D
+=0D
+/**=0D
+ Cleans off leading and trailing spaces and tabs.=0D
+=0D
+ @param[in] String pointer to the string to trim them off.=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+TrimSpaces (=0D
+ IN CHAR16 **String=0D
+ )=0D
+{=0D
+ ASSERT(String !=3D NULL);=0D
+ ASSERT(*String!=3D NULL);=0D
+ //=0D
+ // Remove any spaces and tabs at the beginning of the (*String).=0D
+ //=0D
+ while (((*String)[0] =3D=3D L' ') || ((*String)[0] =3D=3D L'\t')) {=0D
+ CopyMem (=0D
+ (*String),=0D
+ (*String) + 1,=0D
+ StrSize ((*String)) - sizeof((*String)[0])=0D
+ );=0D
+ }=0D
+=0D
+ //=0D
+ // Remove any spaces and tabs at the end of the (*String).=0D
+ //=0D
+ while ((StrLen (*String) > 0) &&=0D
+ (((*String)[StrLen ((*String)) - 1] =3D=3D L' ') ||=0D
+ ((*String)[StrLen ((*String)) - 1] =3D=3D L'\t'))=0D
+ ) {=0D
+ (*String)[StrLen ((*String)) - 1] =3D CHAR_NULL;=0D
+ }=0D
+=0D
+ return (EFI_SUCCESS);=0D
+}=0D
+=0D
+=0D
+/*=0D
+ * Callbacks for request and response.=0D
+ * We just acknowledge that operation has completed here.=0D
+ */=0D
+STATIC=0D
+VOID=0D
+EFIAPI=0D
+RequestCallback (=0D
+ IN EFI_EVENT Event,=0D
+ IN VOID *Context=0D
+)=0D
+{=0D
+ gRequestCallbackComplete =3D TRUE;=0D
+}=0D
+=0D
+STATIC=0D
+VOID=0D
+EFIAPI=0D
+ResponseCallback (=0D
+ IN EFI_EVENT Event,=0D
+ IN VOID *Context=0D
+)=0D
+{=0D
+ gResponseCallbackComplete =3D TRUE;=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Check and convert the UINT16 option values of the 'http' command=0D
+=0D
+ @param[in] ValueStr Value as an Unicode encoded string=0D
+ @param[out] Value UINT16 value=0D
+=0D
+ @return TRUE The value was returned.=0D
+ @return FALSE A parsing error occured.=0D
+**/=0D
+STATIC=0D
+BOOLEAN=0D
+StringToUint16 (=0D
+ IN CONST CHAR16 *ValueStr,=0D
+ OUT UINT16 *Value=0D
+ );=0D
+=0D
+/**=0D
+ Get the name of the NIC.=0D
+=0D
+ @param[in] ControllerHandle The network physical device handle.=0D
+ @param[in] NicNumber The network physical device number.=0D
+ @param[out] NicName Address where to store the NIC name.=0D
+ The memory area has to be at least=0D
+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH=0D
+ double byte wide.=0D
+=0D
+ @return EFI_SUCCESS The name of the NIC was returned.=0D
+ @return Others The creation of the child for the Managed=0D
+ Network Service failed or the opening of=0D
+ the Managed Network Protocol failed or=0D
+ the operational parameters for the=0D
+ Managed Network Protocol could not be=0D
+ read.=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+GetNicName (=0D
+ IN EFI_HANDLE ControllerHandle,=0D
+ IN UINTN NicNumber,=0D
+ OUT CHAR16 *NicName=0D
+ );=0D
+=0D
+/**=0D
+ Create a child for the service identified by its service binding protoco=
l GUID=0D
+ and get from the child the interface of the protocol identified by its G=
UID.=0D
+=0D
+ @param[in] ControllerHandle Controller handle.=0D
+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID o=
f the=0D
+ service to be created.=0D
+ @param[in] ProtocolGuid GUID of the protocol to be open=
.=0D
+ @param[out] ChildHandle Address where the handler of th=
e=0D
+ created child is returned. NULL=
is=0D
+ returned in case of error.=0D
+ @param[out] Interface Address where a pointer to the=
=0D
+ protocol interface is returned =
in=0D
+ case of success.=0D
+=0D
+ @return EFI_SUCCESS The child was created and the protocol opened.=0D
+ @return Others Either the creation of the child or the opening=0D
+ of the protocol failed.=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+CreateServiceChildAndOpenProtocol (=0D
+ IN EFI_HANDLE ControllerHandle,=0D
+ IN EFI_GUID *ServiceBindingProtocolGuid,=0D
+ IN EFI_GUID *ProtocolGuid,=0D
+ OUT EFI_HANDLE *ChildHandle,=0D
+ OUT VOID **Interface=0D
+ );=0D
+=0D
+/**=0D
+ Close the protocol identified by its GUID on the child handle of the ser=
vice=0D
+ identified by its service binding protocol GUID, then destroy the child=
=0D
+ handle.=0D
+=0D
+ @param[in] ControllerHandle Controller handle.=0D
+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of=
the=0D
+ service to be destroyed.=0D
+ @param[in] ProtocolGuid GUID of the protocol to be close=
d.=0D
+ @param[in] ChildHandle Handle of the child to be destro=
yed.=0D
+=0D
+**/=0D
+STATIC=0D
+VOID=0D
+CloseProtocolAndDestroyServiceChild (=0D
+ IN EFI_HANDLE ControllerHandle,=0D
+ IN EFI_GUID *ServiceBindingProtocolGuid,=0D
+ IN EFI_GUID *ProtocolGuid,=0D
+ IN EFI_HANDLE ChildHandle=0D
+ );=0D
+=0D
+=0D
+/**=0D
+ Worker function that download the data of a file from an HTTP server giv=
en=0D
+ the path of the file and its size.=0D
+=0D
+ @param[in] Context A pointer to the download context.=0D
+=0D
+ @retval EFI_SUCCESS The file was downloaded.=0D
+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.=0D
+ @retval Others The downloading of the file=0D
+ from the server failed.=0D
+=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+DownloadFile (=0D
+ IN DOWNLOAD_CONTEXT *Context=0D
+ );=0D
+=0D
+STATIC CONST SHELL_PARAM_ITEM ParamList[] =3D {=0D
+ {L"-i", TypeValue},=0D
+ {L"-k", TypeFlag},=0D
+ {L"-l", TypeValue},=0D
+ {L"-s", TypeValue},=0D
+ {L"-t", TypeValue},=0D
+ {NULL , TypeMax}=0D
+ };=0D
+=0D
+/**=0D
+ Function for 'http' command.=0D
+=0D
+ @param[in] ImageHandle Handle to the Image (NULL if Internal).=0D
+ @param[in] SystemTable Pointer to the System Table (NULL if Internal).=
=0D
+=0D
+ @return SHELL_SUCCESS The 'http' command completed successfu=
lly.=0D
+ @return SHELL_ABORTED The Shell Library initialization faile=
d.=0D
+ @return SHELL_INVALID_PARAMETER At least one of the command's argument=
s is=0D
+ not valid.=0D
+ @return SHELL_OUT_OF_RESOURCES A memory allocation failed.=0D
+ @return SHELL_NOT_FOUND Network Interface Card not found.=0D
+ @return SHELL_UNSUPPORTED Command was valid, but the server retu=
rned=0D
+ a status code indicating some error.=0D
+ Examine the file requested for error b=
ody.=0D
+=0D
+**/=0D
+SHELL_STATUS=0D
+RunHttp (=0D
+ IN EFI_HANDLE ImageHandle,=0D
+ IN EFI_SYSTEM_TABLE *SystemTable=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ LIST_ENTRY *CheckPackage;=0D
+ UINTN ParamCount;=0D
+ UINTN HandleCount;=0D
+ UINTN NicNumber;=0D
+ UINTN InitialSize;=0D
+ UINTN ParamOffset;=0D
+ UINTN StartSize;=0D
+ CHAR16 *ProblemParam;=0D
+ CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];=
=0D
+ CHAR16 *Walker1;=0D
+ CHAR16 *VStr;=0D
+ CONST CHAR16 *UserNicName;=0D
+ CONST CHAR16 *ValueStr;=0D
+ CONST CHAR16 *RemoteFilePath;=0D
+ CONST CHAR16 *Walker;=0D
+ EFI_HTTP_CONFIG_DATA HttpConfigData;=0D
+ EFI_HTTPv4_ACCESS_POINT IPv4Node;=0D
+ EFI_HANDLE *Handles;=0D
+ EFI_HANDLE ControllerHandle;=0D
+ EFI_HANDLE HttpChildHandle;=0D
+ DOWNLOAD_CONTEXT Context;=0D
+ BOOLEAN NicFound;=0D
+=0D
+ ProblemParam =3D NULL;=0D
+ RemoteFilePath =3D NULL;=0D
+ NicFound =3D FALSE;=0D
+ Handles =3D NULL;=0D
+=0D
+ //=0D
+ // Initialize the Shell library (we must be in non-auto-init...)=0D
+ //=0D
+ ParamOffset =3D 0;=0D
+ gHttpError =3D FALSE;=0D
+=0D
+ Status =3D ShellInitialize ();=0D
+ if (EFI_ERROR (Status)) {=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ return SHELL_ABORTED;=0D
+ }=0D
+=0D
+ ZeroMem (&Context, sizeof (Context));=0D
+=0D
+ //=0D
+ // Parse the command line.=0D
+ //=0D
+ Status =3D ShellCommandLineParse (=0D
+ ParamList,=0D
+ &CheckPackage,=0D
+ &ProblemParam,=0D
+ TRUE);=0D
+ if (EFI_ERROR (Status)) {=0D
+ if ((Status =3D=3D EFI_VOLUME_CORRUPTED) &&=0D
+ (ProblemParam !=3D NULL) ) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mHttpHiiHandle,=0D
+ HTTP_APP_NAME, ProblemParam=0D
+ );=0D
+ SHELL_FREE_NON_NULL (ProblemParam);=0D
+ } else {=0D
+ ASSERT (FALSE);=0D
+ }=0D
+=0D
+ goto Error;=0D
+ }=0D
+=0D
+ //=0D
+ // Check the number of parameters=0D
+ //=0D
+ Status =3D EFI_INVALID_PARAMETER;=0D
+=0D
+ ParamCount =3D ShellCommandLineGetCount (CheckPackage);=0D
+ if (ParamCount > MAX_PARAM_COUNT) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY),=0D
+ mHttpHiiHandle, HTTP_APP_NAME=0D
+ );=0D
+ goto Error;=0D
+ }=0D
+=0D
+ if (ParamCount < MIN_PARAM_COUNT) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW),=0D
+ mHttpHiiHandle, HTTP_APP_NAME=0D
+ );=0D
+ goto Error;=0D
+ }=0D
+=0D
+ ZeroMem (&HttpConfigData, sizeof (HttpConfigData));=0D
+ ZeroMem (&IPv4Node, sizeof (IPv4Node));=0D
+ IPv4Node.UseDefaultAddress =3D TRUE;=0D
+=0D
+ HttpConfigData.HttpVersion =3D HttpVersion11;=0D
+ HttpConfigData.AccessPoint.IPv4Node =3D &IPv4Node;=0D
+=0D
+ //=0D
+ // Get the host address (not necessarily IPv4 format)=0D
+ //=0D
+ ValueStr =3D ShellCommandLineGetRawValue (CheckPackage, 1);=0D
+ if (!ValueStr) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), mHttpHiiHandle,=0D
+ HTTP_APP_NAME, ValueStr=0D
+ );=0D
+ goto Error;=0D
+ } else {=0D
+ StartSize =3D 0;=0D
+ if (!StrStr (ValueStr, L"://")) {=0D
+ Context.ServerAddrAndProto =3D StrnCatGrow (=0D
+ &Context.ServerAddrAndProto,=0D
+ &StartSize,=0D
+ DEFAULT_HTTP_PROTO,=0D
+ StrLen (DEFAULT_HTTP_PROTO)=0D
+ );=0D
+ Context.ServerAddrAndProto =3D StrnCatGrow (=0D
+ &Context.ServerAddrAndProto,=0D
+ &StartSize,=0D
+ L"://",=0D
+ StrLen (L"://")=0D
+ );=0D
+ VStr =3D (CHAR16 *)ValueStr;=0D
+ } else {=0D
+ VStr =3D StrStr (ValueStr, L"://") + StrLen (L"://");=0D
+ }=0D
+=0D
+ for (Walker1 =3D VStr; *Walker1; Walker1++) {=0D
+ if (*Walker1 =3D=3D L'/') {=0D
+ break;=0D
+ }=0D
+ }=0D
+=0D
+ if (*Walker1 =3D=3D L'/') {=0D
+ ParamOffset =3D 1;=0D
+ RemoteFilePath =3D Walker1;=0D
+ }=0D
+=0D
+ Context.ServerAddrAndProto =3D StrnCatGrow (=0D
+ &Context.ServerAddrAndProto,=0D
+ &StartSize,=0D
+ ValueStr,=0D
+ StrLen (ValueStr) - StrLen (Walker1)=0D
+ );=0D
+ if (!Context.ServerAddrAndProto) {=0D
+ Status =3D EFI_OUT_OF_RESOURCES;=0D
+ goto Error;=0D
+ }=0D
+ }=0D
+=0D
+ if (!RemoteFilePath) {=0D
+ RemoteFilePath =3D ShellCommandLineGetRawValue (CheckPackage, 2);=0D
+ if (!RemoteFilePath) {=0D
+ // If no path given, assume just "/"=0D
+ RemoteFilePath =3D L"/";=0D
+ }=0D
+ }=0D
+=0D
+ TrimSpaces ((CHAR16 **)&RemoteFilePath);=0D
+=0D
+ if (ParamCount =3D=3D MAX_PARAM_COUNT - ParamOffset) {=0D
+ mLocalFilePath =3D ShellCommandLineGetRawValue (=0D
+ CheckPackage,=0D
+ MAX_PARAM_COUNT - 1 - ParamOffset);=0D
+ } else {=0D
+ Walker =3D RemoteFilePath + StrLen (RemoteFilePath);=0D
+ while ((--Walker) >=3D RemoteFilePath) {=0D
+ if ((*Walker =3D=3D L'\\') ||=0D
+ (*Walker =3D=3D L'/' ) ) {=0D
+ break;=0D
+ }=0D
+ }=0D
+=0D
+ mLocalFilePath =3D Walker + 1;=0D
+ }=0D
+=0D
+ if (!StrLen (mLocalFilePath)) {=0D
+ mLocalFilePath =3D DEFAULT_HTML_FILE;=0D
+ }=0D
+=0D
+ InitialSize =3D 0;=0D
+ Context.URI =3D StrnCatGrow (=0D
+ &Context.URI,=0D
+ &InitialSize,=0D
+ RemoteFilePath,=0D
+ StrLen (RemoteFilePath)=0D
+ );=0D
+ if (!Context.URI) {=0D
+ Status =3D EFI_OUT_OF_RESOURCES;=0D
+ goto Error;=0D
+ }=0D
+=0D
+ //=0D
+ // Get the name of the Network Interface Card to be used if any.=0D
+ //=0D
+ UserNicName =3D ShellCommandLineGetValue (CheckPackage, L"-i");=0D
+=0D
+ ValueStr =3D ShellCommandLineGetValue (CheckPackage, L"-l");=0D
+ if (ValueStr !=3D NULL) {=0D
+ if (!StringToUint16 (=0D
+ ValueStr,=0D
+ &HttpConfigData.AccessPoint.IPv4Node->LocalPort=0D
+ )=0D
+ ) {=0D
+ goto Error;=0D
+ }=0D
+ }=0D
+=0D
+ Context.BufferSize =3D DEFAULT_BUF_SIZE;=0D
+=0D
+ ValueStr =3D ShellCommandLineGetValue (CheckPackage, L"-s");=0D
+ if (ValueStr !=3D NULL) {=0D
+ Context.BufferSize =3D ShellStrToUintn (ValueStr);=0D
+ if (!Context.BufferSize || Context.BufferSize > MAX_BUF_SIZE) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),=0D
+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr=0D
+ );=0D
+ goto Error;=0D
+ }=0D
+ }=0D
+=0D
+ ValueStr =3D ShellCommandLineGetValue (CheckPackage, L"-t");=0D
+ if (ValueStr !=3D NULL) {=0D
+ HttpConfigData.TimeOutMillisec =3D (UINT32)ShellStrToUintn (ValueStr);=
=0D
+ }=0D
+=0D
+ //=0D
+ // Locate all HTTP Service Binding protocols=0D
+ //=0D
+ Status =3D gBS->LocateHandleBuffer (=0D
+ ByProtocol,=0D
+ &gEfiManagedNetworkServiceBindingProtocolGuid,=0D
+ NULL,=0D
+ &HandleCount,=0D
+ &Handles=0D
+ );=0D
+ if (EFI_ERROR (Status) || (HandleCount =3D=3D 0)) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NO_NIC), mHttpHiiHandle=0D
+ );=0D
+ if (!EFI_ERROR (Status)) {=0D
+ Status =3D EFI_NOT_FOUND;=0D
+ }=0D
+=0D
+ goto Error;=0D
+ }=0D
+=0D
+ Status =3D EFI_NOT_FOUND;=0D
+=0D
+ for (NicNumber =3D 0;=0D
+ (NicNumber < HandleCount) && (Status !=3D EFI_SUCCESS);=0D
+ NicNumber++) {=0D
+ ControllerHandle =3D Handles[NicNumber];=0D
+=0D
+ Status =3D GetNicName (ControllerHandle, NicNumber, NicName);=0D
+ if (EFI_ERROR (Status)) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NAME),=0D
+ mHttpHiiHandle, NicNumber, Status=0D
+ );=0D
+ continue;=0D
+ }=0D
+=0D
+ if (UserNicName !=3D NULL) {=0D
+ if (StrCmp (NicName, UserNicName) !=3D 0) {=0D
+ Status =3D EFI_NOT_FOUND;=0D
+ continue;=0D
+ }=0D
+ NicFound =3D TRUE;=0D
+ }=0D
+=0D
+ Status =3D CreateServiceChildAndOpenProtocol (=0D
+ ControllerHandle,=0D
+ &gEfiHttpServiceBindingProtocolGuid,=0D
+ &gEfiHttpProtocolGuid,=0D
+ &HttpChildHandle,=0D
+ (VOID**)&Context.Http=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_OPEN_PROTOCOL),=0D
+ mHttpHiiHandle, NicName, Status=0D
+ );=0D
+ continue;=0D
+ }=0D
+=0D
+ Status =3D Context.Http->Configure (Context.Http, &HttpConfigData);=0D
+ if (EFI_ERROR (Status)) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_CONFIGURE),=0D
+ mHttpHiiHandle, NicName, Status=0D
+ );=0D
+ continue;=0D
+ }=0D
+=0D
+ Context.Flags =3D 0;=0D
+ if (ShellCommandLineGetFlag (CheckPackage, L"-m")) {=0D
+ Context.Flags |=3D DL_FLAG_TIME;=0D
+ }=0D
+=0D
+ if (ShellCommandLineGetFlag (CheckPackage, L"-k")) {=0D
+ Context.Flags |=3D DL_FLAG_KEEP_BAD;=0D
+ }=0D
+=0D
+ Status =3D DownloadFile (&Context);=0D
+=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle=0D
+ );=0D
+=0D
+ CloseProtocolAndDestroyServiceChild (=0D
+ ControllerHandle,=0D
+ &gEfiHttpServiceBindingProtocolGuid,=0D
+ &gEfiHttpProtocolGuid,=0D
+ HttpChildHandle=0D
+ );=0D
+=0D
+ if (EFI_ERROR (Status)) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_DOWNLOAD),=0D
+ mHttpHiiHandle, RemoteFilePath, NicName, Status=0D
+ );=0D
+ // If a user aborted the operation, do not try another controller.=0D
+ if (Status =3D=3D EFI_ABORTED) {=0D
+ goto Error;=0D
+ }=0D
+ }=0D
+=0D
+ if (gHttpError) {=0D
+ //=0D
+ // This is not related to connection, so no need to repeat with=0D
+ // another interface.=0D
+ //=0D
+ break;=0D
+ }=0D
+ }=0D
+=0D
+ if ((UserNicName !=3D NULL) && (!NicFound)) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NOT_FOUND),=0D
+ mHttpHiiHandle, UserNicName=0D
+ );=0D
+ }=0D
+=0D
+Error:=0D
+ ShellCommandLineFreeVarList (CheckPackage);=0D
+ SHELL_FREE_NON_NULL (Handles);=0D
+ SHELL_FREE_NON_NULL (Context.ServerAddrAndProto);=0D
+ SHELL_FREE_NON_NULL (Context.URI);=0D
+=0D
+ return Status & ~MAX_BIT;=0D
+}=0D
+=0D
+/**=0D
+ Check and convert the UINT16 option values of the 'http' command=0D
+=0D
+ @param[in] ValueStr Value as an Unicode encoded string=0D
+ @param[out] Value UINT16 value=0D
+=0D
+ @return TRUE The value was returned.=0D
+ @return FALSE A parsing error occured.=0D
+**/=0D
+STATIC=0D
+BOOLEAN=0D
+StringToUint16 (=0D
+ IN CONST CHAR16 *ValueStr,=0D
+ OUT UINT16 *Value=0D
+ )=0D
+{=0D
+ UINTN Val;=0D
+=0D
+ Val =3D ShellStrToUintn (ValueStr);=0D
+ if (Val > MAX_UINT16) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),=0D
+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr=0D
+ );=0D
+ return FALSE;=0D
+ }=0D
+=0D
+ *Value =3D (UINT16)Val;=0D
+ return TRUE;=0D
+}=0D
+=0D
+/**=0D
+ Get the name of the NIC.=0D
+=0D
+ @param[in] ControllerHandle The network physical device handle.=0D
+ @param[in] NicNumber The network physical device number.=0D
+ @param[out] NicName Address where to store the NIC name.=0D
+ The memory area has to be at least=0D
+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH=0D
+ double byte wide.=0D
+=0D
+ @return EFI_SUCCESS The name of the NIC was returned.=0D
+ @return Others The creation of the child for the Managed=0D
+ Network Service failed or the opening of=0D
+ the Managed Network Protocol failed or=0D
+ the operational parameters for the=0D
+ Managed Network Protocol could not be=0D
+ read.=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+GetNicName (=0D
+ IN EFI_HANDLE ControllerHandle,=0D
+ IN UINTN NicNumber,=0D
+ OUT CHAR16 *NicName=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_HANDLE MnpHandle;=0D
+ EFI_MANAGED_NETWORK_PROTOCOL *Mnp;=0D
+ EFI_SIMPLE_NETWORK_MODE SnpMode;=0D
+=0D
+ Status =3D CreateServiceChildAndOpenProtocol (=0D
+ ControllerHandle,=0D
+ &gEfiManagedNetworkServiceBindingProtocolGuid,=0D
+ &gEfiManagedNetworkProtocolGuid,=0D
+ &MnpHandle,=0D
+ (VOID**)&Mnp=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ goto Error;=0D
+ }=0D
+=0D
+ Status =3D Mnp->GetModeData (Mnp, NULL, &SnpMode);=0D
+ if (EFI_ERROR (Status) && (Status !=3D EFI_NOT_STARTED)) {=0D
+ goto Error;=0D
+ }=0D
+=0D
+ UnicodeSPrint (=0D
+ NicName,=0D
+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,=0D
+ SnpMode.IfType =3D=3D NET_IFTYPE_ETHERNET ?=0D
+ L"eth%d" :=0D
+ L"unk%d",=0D
+ NicNumber=0D
+ );=0D
+=0D
+ Status =3D EFI_SUCCESS;=0D
+=0D
+Error:=0D
+=0D
+ if (MnpHandle !=3D NULL) {=0D
+ CloseProtocolAndDestroyServiceChild (=0D
+ ControllerHandle,=0D
+ &gEfiManagedNetworkServiceBindingProtocolGuid,=0D
+ &gEfiManagedNetworkProtocolGuid,=0D
+ MnpHandle=0D
+ );=0D
+ }=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Create a child for the service identified by its service binding protoco=
l GUID=0D
+ and get from the child the interface of the protocol identified by its G=
UID.=0D
+=0D
+ @param[in] ControllerHandle Controller handle.=0D
+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID o=
f the=0D
+ service to be created.=0D
+ @param[in] ProtocolGuid GUID of the protocol to be open=
.=0D
+ @param[out] ChildHandle Address where the handler of th=
e=0D
+ created child is returned. NULL=
is=0D
+ returned in case of error.=0D
+ @param[out] Interface Address where a pointer to the=
=0D
+ protocol interface is returned =
in=0D
+ case of success.=0D
+=0D
+ @return EFI_SUCCESS The child was created and the protocol opened.=0D
+ @return Others Either the creation of the child or the opening=0D
+ of the protocol failed.=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+CreateServiceChildAndOpenProtocol (=0D
+ IN EFI_HANDLE ControllerHandle,=0D
+ IN EFI_GUID *ServiceBindingProtocolGuid,=0D
+ IN EFI_GUID *ProtocolGuid,=0D
+ OUT EFI_HANDLE *ChildHandle,=0D
+ OUT VOID **Interface=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+=0D
+ *ChildHandle =3D NULL;=0D
+ Status =3D NetLibCreateServiceChild (=0D
+ ControllerHandle,=0D
+ gImageHandle,=0D
+ ServiceBindingProtocolGuid,=0D
+ ChildHandle=0D
+ );=0D
+ if (!EFI_ERROR (Status)) {=0D
+ Status =3D gBS->OpenProtocol (=0D
+ *ChildHandle,=0D
+ ProtocolGuid,=0D
+ Interface,=0D
+ gImageHandle,=0D
+ ControllerHandle,=0D
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ NetLibDestroyServiceChild (=0D
+ ControllerHandle,=0D
+ gImageHandle,=0D
+ ServiceBindingProtocolGuid,=0D
+ *ChildHandle=0D
+ );=0D
+ *ChildHandle =3D NULL;=0D
+ }=0D
+ }=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Close the protocol identified by its GUID on the child handle of the ser=
vice=0D
+ identified by its service binding protocol GUID, then destroy the child=
=0D
+ handle.=0D
+=0D
+ @param[in] ControllerHandle Controller handle.=0D
+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of=
the=0D
+ service to be destroyed.=0D
+ @param[in] ProtocolGuid GUID of the protocol to be close=
d.=0D
+ @param[in] ChildHandle Handle of the child to be destro=
yed.=0D
+=0D
+**/=0D
+STATIC=0D
+VOID=0D
+CloseProtocolAndDestroyServiceChild (=0D
+ IN EFI_HANDLE ControllerHandle,=0D
+ IN EFI_GUID *ServiceBindingProtocolGuid,=0D
+ IN EFI_GUID *ProtocolGuid,=0D
+ IN EFI_HANDLE ChildHandle=0D
+ )=0D
+{=0D
+ gBS->CloseProtocol (=0D
+ ChildHandle,=0D
+ ProtocolGuid,=0D
+ gImageHandle,=0D
+ ControllerHandle=0D
+ );=0D
+=0D
+ NetLibDestroyServiceChild (=0D
+ ControllerHandle,=0D
+ gImageHandle,=0D
+ ServiceBindingProtocolGuid,=0D
+ ChildHandle=0D
+ );=0D
+}=0D
+=0D
+/**=0D
+ Wait until operation completes. Completion is indicated by=0D
+ setting of an appropriate variable.=0D
+=0D
+ @param[in] Context A pointer to the HTTP download context.=
=0D
+ @param[in] CallBackComplete A pointer to the callback completion=0D
+ variable set by the callback.=0D
+=0D
+ @return EFI_SUCCESS Callback signalled completion.=0D
+ @return EFI_TIMEOUT Timed out waiting for completion.=0D
+ @return Others Error waiting for completion.=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+WaitForCompletion (=0D
+ IN DOWNLOAD_CONTEXT *Context,=0D
+ IN OUT BOOLEAN *CallBackComplete=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_EVENT WaitEvt;=0D
+=0D
+ Status =3D EFI_SUCCESS;=0D
+=0D
+ // Use a timer to measure timeout. Cannot use Stall here!=0D
+ Status =3D gBS->CreateEvent (=0D
+ EVT_TIMER,=0D
+ TPL_CALLBACK,=0D
+ NULL,=0D
+ NULL,=0D
+ &WaitEvt=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ if (!EFI_ERROR (Status)) {=0D
+ Status =3D gBS->SetTimer(=0D
+ WaitEvt,=0D
+ TimerRelative,=0D
+ EFI_TIMER_PERIOD_SECONDS (TIMER_MAX_TIMEOUT_S)=0D
+ );=0D
+=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ }=0D
+=0D
+ while (! *CallBackComplete &&=0D
+ (!EFI_ERROR (Status)) &&=0D
+ EFI_ERROR (gBS->CheckEvent (WaitEvt))=0D
+ ) {=0D
+ Status =3D Context->Http->Poll (Context->Http);=0D
+ }=0D
+=0D
+ gBS->SetTimer (WaitEvt, TimerCancel, 0);=0D
+ gBS->CloseEvent (WaitEvt);=0D
+=0D
+ if (*CallBackComplete) {=0D
+ return EFI_SUCCESS;=0D
+ }=0D
+=0D
+ if (!EFI_ERROR (Status)) {=0D
+ Status =3D EFI_TIMEOUT;=0D
+ }=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Generate and send a request to the http server.=0D
+=0D
+ @param[in] Context HTTP download context.=0D
+ @param[in] DownloadUrl Fully qualified URL to be downloaded.=0D
+=0D
+ @return EFI_SUCCESS Request has been sent successfully.=0D
+ @return EFI_INVALID_PARAMETER Invalid URL.=0D
+ @return EFI_OUT_OF_RESOURCES Out of memory.=0D
+ @return EFI_DEVICE_ERROR If HTTPS is used, this probably=0D
+ means that TLS support either was not=0D
+ installed or not configured.=0D
+ @return Others Error sending the request.=0D
+**/=0D
+=0D
+STATIC=0D
+EFI_STATUS=0D
+SendRequest (=0D
+ IN DOWNLOAD_CONTEXT *Context,=0D
+ IN CHAR16 *DownloadUrl=0D
+ )=0D
+{=0D
+ EFI_HTTP_REQUEST_DATA RequestData;=0D
+ EFI_HTTP_HEADER RequestHeader;=0D
+ EFI_HTTP_MESSAGE RequestMessage;=0D
+ EFI_STATUS Status;=0D
+ CHAR16 *Host;=0D
+ UINTN StringSize;=0D
+=0D
+ ZeroMem (&RequestData, sizeof (RequestData));=0D
+ ZeroMem (&RequestHeader, sizeof (RequestHeader));=0D
+ ZeroMem (&RequestMessage, sizeof (RequestMessage));=0D
+ ZeroMem (&Context->RequestToken, sizeof (Context->RequestToken));=0D
+=0D
+ RequestHeader.FieldName =3D "Host";=0D
+=0D
+ Host =3D (CHAR16 *)Context->ServerAddrAndProto;=0D
+ while (*Host !=3D CHAR_NULL && *Host !=3D L'/') {=0D
+ Host++;=0D
+ }=0D
+=0D
+ if (*Host =3D=3D CHAR_NULL) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ //=0D
+ // Get the next slash=0D
+ //=0D
+ Host++;=0D
+ //=0D
+ // And now the host name=0D
+ //=0D
+ Host++;=0D
+=0D
+ StringSize =3D StrLen (Host) + 1;=0D
+ RequestHeader.FieldValue =3D AllocatePool (StringSize);=0D
+ if (!RequestHeader.FieldValue) {=0D
+ return EFI_OUT_OF_RESOURCES;=0D
+ }=0D
+=0D
+ UnicodeStrToAsciiStrS (Host, RequestHeader.FieldValue, StringSize);=0D
+=0D
+ RequestMessage.HeaderCount++;=0D
+=0D
+ RequestData.Method =3D HttpMethodGet;=0D
+ RequestData.Url =3D DownloadUrl;=0D
+=0D
+ RequestMessage.Data.Request =3D &RequestData;=0D
+ RequestMessage.Headers =3D &RequestHeader;=0D
+ RequestMessage.BodyLength =3D 0;=0D
+ RequestMessage.Body =3D NULL;=0D
+ Context->RequestToken.Event =3D NULL;=0D
+=0D
+ //=0D
+ // Completion callback event to be set when Request completes.=0D
+ //=0D
+ Status =3D gBS->CreateEvent (=0D
+ EVT_NOTIFY_SIGNAL,=0D
+ TPL_CALLBACK,=0D
+ RequestCallback,=0D
+ Context,=0D
+ &Context->RequestToken.Event=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+=0D
+ Context->RequestToken.Status =3D EFI_SUCCESS;=0D
+ Context->RequestToken.Message =3D &RequestMessage;=0D
+ gRequestCallbackComplete =3D FALSE;=0D
+ Status =3D Context->Http->Request (Context->Http, &Context->RequestTok=
en);=0D
+ if (EFI_ERROR (Status)) {=0D
+ goto Error;=0D
+ }=0D
+=0D
+ Status =3D WaitForCompletion (Context, &gRequestCallbackComplete);=0D
+ if (EFI_ERROR (Status)) {=0D
+ Context->Http->Cancel (Context->Http, &Context->RequestToken);=0D
+ }=0D
+Error:=0D
+ SHELL_FREE_NON_NULL (RequestHeader.FieldValue);=0D
+ if (Context->RequestToken.Event) {=0D
+ gBS->CloseEvent (Context->RequestToken.Event);=0D
+ ZeroMem (&Context->RequestToken, sizeof (Context->RequestToken));=0D
+ }=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Update the progress of a file download=0D
+ This procedure is called each time a new HTTP body portion is received.=
=0D
+=0D
+ @param[in] Context HTTP download context.=0D
+ @param[in] DownloadLen Portion size, in bytes.=0D
+ @param[in] Buffer The pointer to the parsed buffer.=0D
+=0D
+ @retval EFI_SUCCESS Portion saved.=0D
+ @retval Other Error saving the portion.=0D
+=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+EFIAPI=0D
+SavePortion (=0D
+ IN DOWNLOAD_CONTEXT *Context,=0D
+ IN UINTN DownloadLen,=0D
+ IN CHAR8 *Buffer=0D
+ )=0D
+{=0D
+ CHAR16 Progress[HTTP_PROGRESS_MESSAGE_SIZE];=0D
+ UINTN NbOfKb;=0D
+ UINTN Index;=0D
+ UINTN LastStep;=0D
+ UINTN Step;=0D
+ EFI_STATUS Status;=0D
+=0D
+ LastStep =3D 0;=0D
+ Step =3D 0;=0D
+ ShellSetFilePosition (mFileHandle, Context->LastReportedNbOfBytes);=0D
+ Status =3D ShellWriteFile (mFileHandle, &DownloadLen, Buffer);=0D
+ if (EFI_ERROR (Status)) {=0D
+ if (Context->ContentDownloaded > 0) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle=0D
+ );=0D
+ }=0D
+=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_WRITE),=0D
+ mHttpHiiHandle, mLocalFilePath, Status=0D
+ );=0D
+=0D
+ return Status;=0D
+ }=0D
+=0D
+ if (Context->ContentDownloaded =3D=3D 0) {=0D
+ ShellPrintEx (-1, -1, L"%s 0 Kb", HTTP_PROGR_FRAME);=0D
+ }=0D
+=0D
+ Context->ContentDownloaded +=3D DownloadLen;=0D
+ NbOfKb =3D Context->ContentDownloaded >> 10;=0D
+=0D
+ Progress[0] =3D L'\0';=0D
+ if (Context->ContentLength) {=0D
+ LastStep =3D (Context->LastReportedNbOfBytes * HTTP_PROGRESS_SLIDER_S=
TEPS) /=0D
+ Context->ContentLength;=0D
+ Step =3D (Context->ContentDownloaded * HTTP_PROGRESS_SLIDER_STEPS=
) /=0D
+ Context->ContentLength;=0D
+ }=0D
+=0D
+ Context->LastReportedNbOfBytes =3D Context->ContentDownloaded;=0D
+=0D
+ if (Step <=3D LastStep) {=0D
+ if (!Context->ContentLength) {=0D
+ //=0D
+ // Update downloaded size, there is no length info available.=0D
+ //=0D
+ ShellPrintEx (-1, -1, L"%s", HTTP_KB);=0D
+ ShellPrintEx (-1, -1, L"%7d Kb", NbOfKb);=0D
+ }=0D
+=0D
+ return EFI_SUCCESS;=0D
+ }=0D
+=0D
+ ShellPrintEx (-1, -1, L"%s", HTTP_PROGRESS_DEL);=0D
+=0D
+ Status =3D StrCpyS (Progress, HTTP_PROGRESS_MESSAGE_SIZE, HTTP_PROGR_FRA=
ME);=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ for (Index =3D 1; Index < Step; Index++) {=0D
+ Progress[Index] =3D L'=3D';=0D
+ }=0D
+=0D
+ if (Step) {=0D
+ Progress[Step] =3D L'>';=0D
+ }=0D
+=0D
+ UnicodeSPrint (=0D
+ Progress + (sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 1,=0D
+ sizeof (Progress) - sizeof (HTTP_PROGR_FRAME),=0D
+ L" %7d Kb",=0D
+ NbOfKb=0D
+ );=0D
+=0D
+=0D
+ ShellPrintEx (-1, -1, L"%s", Progress);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
+=0D
+/**=0D
+ Replace the original Host and URI with Host and URI returned by the=0D
+ HTTP server in 'Location' header (redirection).=0D
+=0D
+ @param[in] Location A pointer to the 'Location' string=0D
+ provided by HTTP server.=0D
+ @param[in] Context A pointer to HTTP download context.=0D
+ @param[in] DownloadUrl Fully qualified HTTP URL.=0D
+=0D
+ @return EFI_SUCCESS Host and URI were successfully set.=0D
+ @return EFI_OUT_OF_RESOURCES Error setting Host or URI.=0D
+**/=0D
+=0D
+STATIC=0D
+EFI_STATUS=0D
+SetHostURI (=0D
+ IN CHAR8 *Location,=0D
+ IN DOWNLOAD_CONTEXT *Context,=0D
+ IN CHAR16 *DownloadUrl=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ UINTN StringSize;=0D
+ UINTN FirstStep;=0D
+ UINTN Idx;=0D
+ UINTN Step;=0D
+ CHAR8 *Walker;=0D
+ CHAR16 *Temp;=0D
+ CHAR8 *Tmp;=0D
+ CHAR16 *Url;=0D
+ BOOLEAN IsAbEmptyUrl;=0D
+=0D
+ Tmp =3D NULL;=0D
+ Url =3D NULL;=0D
+ IsAbEmptyUrl =3D FALSE;=0D
+ FirstStep =3D 0;=0D
+=0D
+ StringSize =3D (AsciiStrSize (Location) * sizeof (CHAR16));=0D
+ Url =3D AllocateZeroPool (StringSize);=0D
+ if (!Url) {=0D
+ return EFI_OUT_OF_RESOURCES;=0D
+ }=0D
+=0D
+ Status =3D AsciiStrToUnicodeStrS (=0D
+ (CONST CHAR8 *)Location,=0D
+ Url,=0D
+ StringSize=0D
+ );=0D
+=0D
+ if (EFI_ERROR (Status)) {=0D
+ goto Error;=0D
+ }=0D
+=0D
+ //=0D
+ // If an HTTP server redirects to the same location more than once,=0D
+ // then stop attempts and tell it is not reachable.=0D
+ //=0D
+ if (!StrCmp (Url, DownloadUrl)) {=0D
+ Status =3D EFI_NO_MAPPING;=0D
+ goto Error;=0D
+ }=0D
+=0D
+ if (AsciiStrLen (Location) > 2) {=0D
+ // Some servers return 'Location: //server/resource'=0D
+ IsAbEmptyUrl =3D (Location[0] =3D=3D '/') && (Location[1] =3D=3D '/');=
=0D
+ if (IsAbEmptyUrl) {=0D
+ // Skip first "//"=0D
+ Location +=3D 2;=0D
+ FirstStep =3D 1;=0D
+ }=0D
+ }=0D
+=0D
+ if (AsciiStrStr (Location, "://") || IsAbEmptyUrl) {=0D
+ Idx =3D 0;=0D
+ Walker =3D Location;=0D
+=0D
+ for (Step =3D FirstStep; Step < 2; Step++) {=0D
+ for (; *Walker !=3D '/' && *Walker !=3D '\0'; Walker++) {=0D
+ Idx++;=0D
+ }=0D
+ if (!Step) {=0D
+ //=0D
+ // Skip "//"=0D
+ //=0D
+ Idx +=3D 2;=0D
+ Walker +=3D 2;=0D
+ }=0D
+ }=0D
+=0D
+ Tmp =3D AllocateZeroPool (Idx + 1);=0D
+ if (!Tmp) {=0D
+ Status =3D EFI_OUT_OF_RESOURCES;=0D
+ goto Error;=0D
+ }=0D
+=0D
+ CopyMem (Tmp, Location, Idx);=0D
+=0D
+ //=0D
+ // Location now points to URI=0D
+ //=0D
+ Location +=3D Idx;=0D
+ StringSize =3D (Idx + 1) * sizeof (CHAR16);=0D
+=0D
+ SHELL_FREE_NON_NULL (Context->ServerAddrAndProto);=0D
+=0D
+ Temp =3D AllocateZeroPool (StringSize);=0D
+ if (!Temp) {=0D
+ Status =3D EFI_OUT_OF_RESOURCES;=0D
+ goto Error;=0D
+ }=0D
+=0D
+ Status =3D AsciiStrToUnicodeStrS (=0D
+ (CONST CHAR8 *)Tmp,=0D
+ Temp,=0D
+ StringSize=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ SHELL_FREE_NON_NULL (Temp);=0D
+ goto Error;=0D
+ }=0D
+=0D
+ Idx =3D 0;=0D
+ if (IsAbEmptyUrl) {=0D
+ Context->ServerAddrAndProto =3D StrnCatGrow (=0D
+ &Context->ServerAddrAndProto,=0D
+ &Idx,=0D
+ L"http://",=0D
+ StrLen (L"http://")=0D
+ );=0D
+ }=0D
+=0D
+ Context->ServerAddrAndProto =3D StrnCatGrow (=0D
+ &Context->ServerAddrAndProto,=0D
+ &Idx,=0D
+ Temp,=0D
+ StrLen (Temp)=0D
+ );=0D
+ SHELL_FREE_NON_NULL (Temp);=0D
+ if (!Context->ServerAddrAndProto) {=0D
+ Status =3D EFI_OUT_OF_RESOURCES;=0D
+ goto Error;=0D
+ }=0D
+ }=0D
+=0D
+ SHELL_FREE_NON_NULL (Context->URI);=0D
+=0D
+ StringSize =3D AsciiStrSize (Location) * sizeof (CHAR16);=0D
+ Context->URI =3D AllocateZeroPool (StringSize);=0D
+ if (!Context->URI) {=0D
+ Status =3D EFI_OUT_OF_RESOURCES;=0D
+ goto Error;=0D
+ }=0D
+=0D
+ //=0D
+ // Now make changes to the URI part.=0D
+ //=0D
+ Status =3D AsciiStrToUnicodeStrS (=0D
+ (CONST CHAR8 *)Location,=0D
+ Context->URI,=0D
+ StringSize=0D
+ );=0D
+Error:=0D
+ SHELL_FREE_NON_NULL (Tmp);=0D
+ SHELL_FREE_NON_NULL (Url);=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Message parser callback.=0D
+ Save a portion of HTTP body.=0D
+=0D
+ @param[in] EventType Type of event. Can be either=0D
+ OnComplete or OnData.=0D
+ @param[in] Data A pointer to the buffer with data.=0D
+ @param[in] Length Data length of this portion.=0D
+ @param[in] Context A pointer to the HTTP download context.=0D
+=0D
+ @return EFI_SUCCESS The portion was processed successfully.=0D
+ @return Other Error returned by SavePortion.=0D
+**/=0D
+=0D
+STATIC=0D
+EFI_STATUS=0D
+EFIAPI=0D
+ParseMsg (=0D
+ IN HTTP_BODY_PARSE_EVENT EventType,=0D
+ IN CHAR8 *Data,=0D
+ IN UINTN Length,=0D
+ IN VOID *Context=0D
+ )=0D
+{=0D
+ if (!Data || (EventType =3D=3D BodyParseEventOnComplete) || !Context) {=
=0D
+ return EFI_SUCCESS;=0D
+ }=0D
+=0D
+ return SavePortion (Context, Length, Data);=0D
+}=0D
+=0D
+=0D
+/**=0D
+ Get HTTP server response and collect the whole body as a file.=0D
+ Set appropriate status in Context (REQ_OK, REQ_REPEAT, REQ_ERROR).=0D
+ Note that even if HTTP server returns an error code, it might send=0D
+ the body as well. This body will be collected in the resultant file.=0D
+=0D
+ @param[in] Context A pointer to the HTTP download context.=0D
+ @param[in] DownloadedUrl A pointer to the fully qualified URL to dow=
nload.=0D
+=0D
+ @return EFI_SUCCESS Valid file. Body successfully collected.=0D
+ @return EFI_HTTP_ERROR Response is a valid HTTP response, but the=
=0D
+ HTTP server=0D
+ indicated an error (HTTP code >=3D 400).=0D
+ Response body MAY contain full=0D
+ HTTP server response.=0D
+ @return Others Error getting the reponse from the HTTP ser=
ver.=0D
+ Response body is not collected.=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+GetResponse (=0D
+ IN DOWNLOAD_CONTEXT *Context,=0D
+ IN CHAR16 *DownloadUrl=0D
+ )=0D
+{=0D
+ EFI_HTTP_RESPONSE_DATA ResponseData;=0D
+ EFI_HTTP_MESSAGE ResponseMessage;=0D
+ EFI_HTTP_HEADER *Header;=0D
+ EFI_STATUS Status;=0D
+ VOID *MsgParser;=0D
+ CONST CHAR16 *Desc;=0D
+ BOOLEAN IsTrunked;=0D
+ BOOLEAN Redirection;=0D
+=0D
+ ZeroMem (&ResponseData, sizeof (ResponseData));=0D
+ ZeroMem (&ResponseMessage, sizeof (ResponseMessage));=0D
+ ZeroMem (&Context->ResponseToken, sizeof (Context->ResponseToken));=0D
+ IsTrunked =3D FALSE;=0D
+=0D
+ ResponseMessage.Body =3D Context->Buffer;=0D
+ Context->ResponseToken.Status =3D EFI_SUCCESS;=0D
+ Context->ResponseToken.Message =3D &ResponseMessage;=0D
+ Context->ContentLength =3D 0;=0D
+ Context->Status =3D REQ_OK;=0D
+ MsgParser =3D NULL;=0D
+ ResponseData.StatusCode =3D HTTP_STATUS_UNSUPPORTED_STATUS;=0D
+ ResponseMessage.Data.Response =3D &ResponseData;=0D
+ Context->ResponseToken.Event =3D NULL;=0D
+=0D
+ do {=0D
+ SHELL_FREE_NON_NULL (ResponseMessage.Headers);=0D
+ ResponseMessage.HeaderCount =3D 0;=0D
+ gResponseCallbackComplete =3D FALSE;=0D
+ ResponseMessage.BodyLength =3D Context->BufferSize;=0D
+=0D
+ if (ShellGetExecutionBreakFlag ()) {=0D
+ Status =3D EFI_ABORTED;=0D
+ break;=0D
+ }=0D
+=0D
+ if (!Context->ContentDownloaded && !Context->ResponseToken.Event) {=0D
+ Status =3D gBS->CreateEvent (=0D
+ EVT_NOTIFY_SIGNAL,=0D
+ TPL_CALLBACK,=0D
+ ResponseCallback,=0D
+ Context,=0D
+ &Context->ResponseToken.Event=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ } else {=0D
+ ResponseMessage.Data.Response =3D NULL;=0D
+ }=0D
+=0D
+ if (EFI_ERROR (Status)) {=0D
+ break;=0D
+ }=0D
+=0D
+ Status =3D Context->Http->Response (Context->Http, &Context->ResponseT=
oken);=0D
+ if (EFI_ERROR (Status)) {=0D
+ break;=0D
+ }=0D
+=0D
+ Status =3D WaitForCompletion (Context, &gResponseCallbackComplete);=0D
+ if (EFI_ERROR (Status) && ResponseMessage.HeaderCount) {=0D
+ Status =3D EFI_SUCCESS;=0D
+ }=0D
+=0D
+ if (EFI_ERROR (Status)) {=0D
+ Context->Http->Cancel (Context->Http, &Context->ResponseToken);=0D
+ break;=0D
+ }=0D
+=0D
+ if (!Context->ContentDownloaded) {=0D
+ Redirection =3D=0D
+ ((ResponseData.StatusCode >=3D HTTP_STATUS_300_MULTIPLE_CHOICES) &=
&=0D
+ (ResponseData.StatusCode <=3D HTTP_STATUS_307_TEMPORARY_REDIRECT)=
=0D
+ ) ||=0D
+ (ResponseData.StatusCode =3D=3D HTTP_STATUS_308_PERMANENT_REDIRECT=
);=0D
+=0D
+ if (Redirection) {=0D
+ //=0D
+ // Need to repeat the request with new Location (server redirected=
).=0D
+ //=0D
+ Context->Status =3D REQ_NEED_REPEAT;=0D
+=0D
+ Header =3D HttpFindHeader (=0D
+ ResponseMessage.HeaderCount,=0D
+ ResponseMessage.Headers,=0D
+ "Location"=0D
+ );=0D
+ if (Header) {=0D
+ Status =3D SetHostURI (Header->FieldValue, Context, DownloadUrl)=
;=0D
+ if (Status =3D=3D EFI_NO_MAPPING) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),=0D
+ mHttpHiiHandle, Context->ServerAddrAndProto,=0D
+ L"Recursive HTTP server relocation",=0D
+ Context->URI=0D
+ );=0D
+ }=0D
+ } else {=0D
+ //=0D
+ // Bad reply from the server. Server must specify the location.=
=0D
+ // Indicate that resource was not found, and no body collected.=
=0D
+ //=0D
+ Status =3D EFI_NOT_FOUND;=0D
+ }=0D
+=0D
+ Context->Http->Cancel (Context->Http, &Context->ResponseToken);=0D
+ break;=0D
+ }=0D
+=0D
+ //=0D
+ // Init message-body parser by header information.=0D
+ //=0D
+ if (!MsgParser) {=0D
+ Status =3D HttpInitMsgParser (=0D
+ ResponseMessage.Data.Request->Method,=0D
+ ResponseData.StatusCode,=0D
+ ResponseMessage.HeaderCount,=0D
+ ResponseMessage.Headers,=0D
+ ParseMsg,=0D
+ Context,=0D
+ &MsgParser=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ break;=0D
+ }=0D
+ }=0D
+=0D
+ //=0D
+ // If it is a trunked message, rely on the parser.=0D
+ //=0D
+ Header =3D HttpFindHeader (=0D
+ ResponseMessage.HeaderCount,=0D
+ ResponseMessage.Headers,=0D
+ "Transfer-Encoding"=0D
+ );=0D
+ IsTrunked =3D (Header && !AsciiStrCmp (Header->FieldValue, "chunked"=
));=0D
+=0D
+ HttpGetEntityLength (MsgParser, &Context->ContentLength);=0D
+=0D
+ if (ResponseData.StatusCode >=3D HTTP_STATUS_400_BAD_REQUEST &&=0D
+ (ResponseData.StatusCode !=3D HTTP_STATUS_308_PERMANENT_REDIRECT=
)=0D
+ ) {=0D
+ //=0D
+ // Server reported an error via Response code.=0D
+ // Collect the body if any.=0D
+ //=0D
+ if (!gHttpError) {=0D
+ gHttpError =3D TRUE;=0D
+=0D
+ Desc =3D ErrStatusDesc[ResponseData.StatusCode -=0D
+ HTTP_STATUS_400_BAD_REQUEST];=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),=0D
+ mHttpHiiHandle, Context->ServerAddrAndProto,=0D
+ Desc,=0D
+ Context->URI=0D
+ );=0D
+=0D
+ //=0D
+ // This gives an RFC HTTP error.=0D
+ //=0D
+ Context->Status =3D ShellStrToUintn (Desc);=0D
+ }=0D
+ }=0D
+ }=0D
+=0D
+ // Do NOT try to parse an empty body.=0D
+ if (ResponseMessage.BodyLength || IsTrunked) {=0D
+ Status =3D HttpParseMessageBody (=0D
+ MsgParser,=0D
+ ResponseMessage.BodyLength,=0D
+ ResponseMessage.Body=0D
+ );=0D
+ }=0D
+ } while (!HttpIsMessageComplete (MsgParser) &&=0D
+ !EFI_ERROR (Status) &&=0D
+ ResponseMessage.BodyLength=0D
+ );=0D
+=0D
+ SHELL_FREE_NON_NULL (MsgParser);=0D
+ if (Context->ResponseToken.Event) {=0D
+ gBS->CloseEvent (Context->ResponseToken.Event);=0D
+ ZeroMem (&Context->ResponseToken, sizeof (Context->ResponseToken));=0D
+ }=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Worker function that downloads the data of a file from an HTTP server gi=
ven=0D
+ the path of the file and its size.=0D
+=0D
+ @param[in] Context A pointer to the HTTP download context.=0D
+=0D
+ @retval EFI_SUCCESS The file was downloaded.=0D
+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.=0D
+ #retval EFI_HTTP_ERROR The server returned a valid HTTP error.=0D
+ Examine the mLocalFilePath file=0D
+ to get error body.=0D
+ @retval Others The downloading of the file from the serv=
er=0D
+ failed.=0D
+=0D
+**/=0D
+STATIC=0D
+EFI_STATUS=0D
+DownloadFile (=0D
+ IN DOWNLOAD_CONTEXT *Context=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ CHAR16 *DownloadUrl;=0D
+ UINTN UrlSize;=0D
+=0D
+ ASSERT (Context);=0D
+ if (!Context) {=0D
+ return EFI_INVALID_PARAMETER;=0D
+ }=0D
+=0D
+ DownloadUrl =3D NULL;=0D
+=0D
+ Context->Buffer =3D AllocatePool (Context->BufferSize);=0D
+ if (!Context->Buffer) {=0D
+ Status =3D EFI_OUT_OF_RESOURCES;=0D
+ goto ON_EXIT;=0D
+ }=0D
+=0D
+ //=0D
+ // OPEN FILE=0D
+ //=0D
+ if (!EFI_ERROR (ShellFileExists (mLocalFilePath))) {=0D
+ ShellDeleteFileByName (mLocalFilePath);=0D
+ }=0D
+=0D
+ Status =3D ShellOpenFileByName (=0D
+ mLocalFilePath,=0D
+ &mFileHandle,=0D
+ EFI_FILE_MODE_CREATE |=0D
+ EFI_FILE_MODE_WRITE |=0D
+ EFI_FILE_MODE_READ,=0D
+ 0);=0D
+ if (EFI_ERROR (Status)) {=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),=0D
+ mHttpHiiHandle, HTTP_APP_NAME, mLocalFilePath=0D
+ );=0D
+ goto ON_EXIT;=0D
+ }=0D
+=0D
+ do {=0D
+ SHELL_FREE_NON_NULL (DownloadUrl);=0D
+ UrlSize =3D 0;=0D
+ DownloadUrl =3D StrnCatGrow (=0D
+ &DownloadUrl,=0D
+ &UrlSize,=0D
+ Context->ServerAddrAndProto,=0D
+ StrLen (Context->ServerAddrAndProto)=0D
+ );=0D
+ if (Context->URI[0] !=3D L'/') {=0D
+ DownloadUrl =3D StrnCatGrow (=0D
+ &DownloadUrl,=0D
+ &UrlSize,=0D
+ L"/",=0D
+ StrLen (Context->ServerAddrAndProto)=0D
+ );=0D
+ }=0D
+=0D
+ DownloadUrl =3D StrnCatGrow (=0D
+ &DownloadUrl,=0D
+ &UrlSize,=0D
+ Context->URI,=0D
+ StrLen (Context->URI));=0D
+=0D
+ ShellPrintHiiEx (=0D
+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_DOWNLOADING),=0D
+ mHttpHiiHandle, DownloadUrl);=0D
+=0D
+ Status =3D SendRequest (Context, DownloadUrl);=0D
+ if (Status) {=0D
+ goto ON_EXIT;=0D
+ }=0D
+=0D
+ Status =3D GetResponse (Context, DownloadUrl);=0D
+=0D
+ if (Status) {=0D
+ goto ON_EXIT;=0D
+ }=0D
+=0D
+ } while (Context->Status =3D=3D REQ_NEED_REPEAT);=0D
+=0D
+ if (Context->Status) {=0D
+ Status =3D ENCODE_ERROR (Context->Status);=0D
+ }=0D
+=0D
+ON_EXIT:=0D
+ //=0D
+ // CLOSE FILE=0D
+ //=0D
+ if (mFileHandle) {=0D
+ if (EFI_ERROR (Status) && !(Context->Flags & DL_FLAG_KEEP_BAD)) {=0D
+ ShellDeleteFile (&mFileHandle);=0D
+ } else {=0D
+ ShellCloseFile (&mFileHandle);=0D
+ }=0D
+ }=0D
+=0D
+ SHELL_FREE_NON_NULL (DownloadUrl);=0D
+ SHELL_FREE_NON_NULL (Context->Buffer);=0D
+=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Retrive HII package list from ImageHandle and publish to HII database.=0D
+=0D
+ @param ImageHandle The image handle of the process.=0D
+=0D
+ @return HII handle.=0D
+**/=0D
+EFI_HII_HANDLE=0D
+InitializeHiiPackage (=0D
+ EFI_HANDLE ImageHandle=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ EFI_HII_PACKAGE_LIST_HEADER *PackageList;=0D
+ EFI_HII_HANDLE HiiHandle;=0D
+=0D
+ //=0D
+ // Retrieve HII package list from ImageHandle=0D
+ //=0D
+ Status =3D gBS->OpenProtocol (=0D
+ ImageHandle,=0D
+ &gEfiHiiPackageListProtocolGuid,=0D
+ (VOID **)&PackageList,=0D
+ ImageHandle,=0D
+ NULL,=0D
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ if (EFI_ERROR (Status)) {=0D
+ return NULL;=0D
+ }=0D
+=0D
+ //=0D
+ // Publish HII package list to HII Database.=0D
+ //=0D
+ Status =3D gHiiDatabase->NewPackageList (=0D
+ gHiiDatabase,=0D
+ PackageList,=0D
+ NULL,=0D
+ &HiiHandle=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ if (EFI_ERROR (Status)) {=0D
+ return NULL;=0D
+ }=0D
+=0D
+ return HiiHandle;=0D
+}=0D
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h b/ShellPkg/D=
ynamicCommand/HttpDynamicCommand/Http.h
new file mode 100644
index 000000000000..5c7b90e8cab4
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
@@ -0,0 +1,89 @@
+/** @file=0D
+ Header file for 'http' command functions.=0D
+=0D
+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved. <BR>=
=0D
+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>=0D
+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>=0D
+=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+=0D
+#ifndef _HTTP_H_=0D
+#define _HTTP_H_=0D
+=0D
+#include <Uefi.h>=0D
+=0D
+#include <Library/BaseLib.h>=0D
+#include <Library/BaseMemoryLib.h>=0D
+#include <Library/DebugLib.h>=0D
+#include <Library/HiiLib.h>=0D
+#include <Library/HttpLib.h>=0D
+#include <Library/MemoryAllocationLib.h>=0D
+#include <Library/NetLib.h>=0D
+#include <Library/PrintLib.h>=0D
+#include <Library/ShellLib.h>=0D
+#include <Library/UefiBootServicesTableLib.h>=0D
+#include <Library/UefiHiiServicesLib.h>=0D
+#include <Library/UefiLib.h>=0D
+#include <Library/UefiRuntimeServicesTableLib.h>=0D
+=0D
+#include <Protocol/HiiPackageList.h>=0D
+#include <Protocol/HttpUtilities.h>=0D
+#include <Protocol/ServiceBinding.h>=0D
+=0D
+#define HTTP_APP_NAME L"http"=0D
+=0D
+#define REQ_OK 0=0D
+#define REQ_NEED_REPEAT 1=0D
+=0D
+// Download Flags=0D
+#define DL_FLAG_TIME BIT0 // Show elapsed time.=0D
+#define DL_FLAG_KEEP_BAD BIT1 // Keep files even if download failed.=0D
+=0D
+extern EFI_HII_HANDLE mHttpHiiHandle;=0D
+=0D
+typedef struct {=0D
+ UINTN ContentDownloaded;=0D
+ UINTN ContentLength;=0D
+ UINTN LastReportedNbOfBytes;=0D
+ UINTN BufferSize;=0D
+ UINTN Status;=0D
+ UINTN Flags;=0D
+ UINT8 *Buffer;=0D
+ CHAR16 *ServerAddrAndProto;=0D
+ CHAR16 *URI;=0D
+ EFI_HTTP_TOKEN ResponseToken;=0D
+ EFI_HTTP_TOKEN RequestToken;=0D
+ EFI_HTTP_PROTOCOL *Http;=0D
+} DOWNLOAD_CONTEXT;=0D
+=0D
+/**=0D
+ Function for 'http' command.=0D
+=0D
+ @param[in] ImageHandle The image handle.=0D
+ @param[in] SystemTable The system table.=0D
+=0D
+ @retval SHELL_SUCCESS Command completed successfully.=0D
+ @retval SHELL_INVALID_PARAMETER Command usage error.=0D
+ @retval SHELL_ABORTED The user aborts the operation.=0D
+ @retval value Unknown error.=0D
+**/=0D
+SHELL_STATUS=0D
+RunHttp (=0D
+ IN EFI_HANDLE ImageHandle,=0D
+ IN EFI_SYSTEM_TABLE *SystemTable=0D
+ );=0D
+=0D
+/**=0D
+ Retrive HII package list from ImageHandle and publish to HII database.=0D
+=0D
+ @param ImageHandle The image handle of the process.=0D
+=0D
+ @return HII handle.=0D
+**/=0D
+EFI_HII_HANDLE=0D
+InitializeHiiPackage (=0D
+ EFI_HANDLE ImageHandle=0D
+ );=0D
+#endif // _HTTP_H_=0D
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni b/ShellPkg=
/DynamicCommand/HttpDynamicCommand/Http.uni
new file mode 100644
index 000000000000..00cf05deeb5c
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
@@ -0,0 +1,117 @@
+// /**=0D
+//=0D
+// (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>=0D
+// Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved. <BR>=
=0D
+// Copyright (c) 2020, Broadcom. All rights reserved.<BR>=0D
+// SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+//=0D
+// Module Name:=0D
+//=0D
+// Http.uni=0D
+//=0D
+// Abstract:=0D
+//=0D
+// String definitions for UEFI Shell HTTP command=0D
+//=0D
+//=0D
+// **/=0D
+=0D
+/=3D#=0D
+=0D
+#langdef en-US "english"=0D
+=0D
+#string STR_GEN_TOO_MANY #language en-US "%H%s%N: Too many argum=
ents. Try help http.\r\n"=0D
+#string STR_GEN_TOO_FEW #language en-US "%H%s%N: Too few argume=
nts. Try help http.\r\n"=0D
+#string STR_GEN_PARAM_INV #language en-US "%H%s%N: Invalid argume=
nt - '%H%s%N'. Try help http.\r\n"=0D
+#string STR_GEN_PROBLEM #language en-US "%H%s%N: Unknown flag -=
'%H%s%N'. Try help http.\r\n"=0D
+#string STR_GEN_FILE_OPEN_FAIL #language en-US "%H%s%N: Cannot open fi=
le - '%H%s%N'\r\n"=0D
+#string STR_GEN_CRLF #language en-US "\r\n"=0D
+=0D
+#string STR_HTTP_ERR_NO_NIC #language en-US "No network interface c=
ard found.\r\n"=0D
+#string STR_HTTP_ERR_NIC_NAME #language en-US "Failed to get the name=
of the network interface card number %d - %r\r\n"=0D
+#string STR_HTTP_ERR_OPEN_PROTOCOL #language en-US "Unable to open HTTP pr=
otocol on '%H%s%N' - %r\r\n"=0D
+#string STR_HTTP_ERR_CONFIGURE #language en-US "Unable to configure HT=
TP protocol on '%H%s%N' - %r\r\n"=0D
+#string STR_HTTP_ERR_DOWNLOAD #language en-US "Unable to download the=
file '%H%s%N' on '%H%s%N' - %r\r\n"=0D
+#string STR_HTTP_ERR_WRITE #language en-US "Unable to write into f=
ile '%H%s%N' - %r\r\n"=0D
+#string STR_HTTP_ERR_NIC_NOT_FOUND #language en-US "Network Interface Card=
'%H%s%N' not found.\r\n"=0D
+#string STR_HTTP_ERR_STATUSCODE #language en-US "\r'%H%s%N' reports '%s=
' for '%H%s%N' \r\n"=0D
+#string STR_HTTP_DOWNLOADING #language en-US "Downloading '%H%s%N'\r=
\n"=0D
+=0D
+#string STR_GET_HELP_HTTP #language en-US ""=0D
+".TH http 0 "Download a file from HTTP server."\r\n"=0D
+".SH NAME\r\n"=0D
+"Download a file from HTTP server.\r\n"=0D
+".SH SYNOPSIS\r\n"=0D
+" \r\n"=0D
+"HTTP [-i interface] [-l port] [-t timeout] [-s size] [-m] [-k]\r\n"=0D
+" <URL> [localfilepath]\r\n"=0D
+".SH OPTIONS\r\n"=0D
+" \r\n"=0D
+" -i interface - Specifies an adapter name, i.e., eth0.\r\n"=0D
+" -k Keep the downloaded file even if there was an error.=
\r\n"=0D
+" If this parameter is not used, the file will be dele=
ted.\r\n"=0D
+" -l port - Specifies the local port number. Default value is 0\=
r\n"=0D
+" and the port number is automatically assigned.\r\n"=
=0D
+" -m Measure and report download time (in seconds). \r\n"=
=0D
+" -s size The size of the download buffer for a chunk, in byte=
s.\r\n"=0D
+" Default is 32K. Note that larger buffer does not imp=
ly\r\n"=0D
+" better speed.\r\n"=0D
+" -t timeout - The number of seconds to wait for completion of\r\n"=
=0D
+" requests and responses. Default is 0 which is 'autom=
atic'.\r\n"=0D
+" %HURL%N\r\n"=0D
+" Two types of providing of URLs are supported:\r\n"=0D
+" 1. tftp-like, where host and http_uri are separate parameters\r\n"=0D
+" (example: host /host_uri), and\r\n\"=0D
+" 2. wget-like, where host and host_uri is one parameter.\r\n"=0D
+" (example: host/host_uri)\r\n"=0D
+"\r\n"=0D
+" host - Specifies HTTP Server address.\r\n=0D
+ Can be either IPv4 address or 'http (or https)://a=
ddr'\r\n=0D
+ Can use addresses resolvable by DNS as well. \r\n=
=0D
+ Port can be specified after ':' if needed. \r\n=0D
+ By default port 80 is used.\r\n"=0D
+" http_uri - HTTP server URI to download the file.\r\n"=0D
+"\r\n"=0D
+" localfilepath - Local destination file path.\r\n"=0D
+".SH DESCRIPTION\r\n"=0D
+" \r\n"=0D
+"NOTES:\r\n"=0D
+" 1. The HTTP command allows geting of the file specified by its 'http_ur=
i'\r\n"=0D
+" path from the HTTP server specified by its 'host' IPv4 address. If t=
he\r\n"=0D
+" optional 'localfilepath' parameter is provided, the downloaded file =
is\r\n"=0D
+" stored locally using the provided file path. If the local file path =
is\r\n"=0D
+" not specified, the file is stored in the current directory using the=
file\r\n"=0D
+" server's name.\r\n"=0D
+" 2. Before using the HTTP command, the network interface intended to be\=
r\n"=0D
+" used to retrieve the file must be configured. This configuration may=
be\r\n"=0D
+" done by means of the 'ifconfig' command.\r\n"=0D
+" 3. If a network interface is defined with the '-i' option then only thi=
s\r\n"=0D
+" interface will be used to retrieve the remote file. Otherwise, all n=
etwork\r\n"=0D
+" interfaces are tried in the order they have been discovered during t=
he\r\n"=0D
+" DXE phase.\r\n"=0D
+".SH EXAMPLES\r\n"=0D
+" \r\n"=0D
+"EXAMPLES:\r\n"=0D
+" * To get the file "dir1/file1.dat" from the HTTP server 192.168.1.1, po=
rt 8080, and\r\n"=0D
+" store it as file2.dat in the current directory (use tftp-like URL for=
mat) :\r\n"=0D
+" fs0:\> http 192.168.1.1:8080 dir1/file1.dat file2.dat\r\n"=0D
+" * To get the file /image.bin via HTTPS from server 192.168.1.1 at port =
443 \r\n"=0D
+" (default HTTPS port), and store it in the current directory: \r\n"=0D
+" fs0:\> http https://192.168.1.1 image.bin\r\n"=0D
+" To get an index file from http://google.com and place it into the \r\=
n"=0D
+" current directory:\r\n"=0D
+" fs0:\> http google.com index.html\r\n"=0D
+".SH RETURNVALUES\r\n"=0D
+" \r\n"=0D
+"RETURN VALUES:\r\n"=0D
+" SHELL_SUCCESS The action was completed as requested.\r\n"=0D
+" SHELL_INVALID_PARAMETER One of the passed-in parameters was incorrect=
ly\r\n"=0D
+" formatted or its value was out of bounds.\r\n=
"=0D
+" HTTP_ERROR No EFI errors, but the server reported a stat=
us code\r\n"=0D
+" which should be treated as an error. If an er=
ror body sent\r\n"=0D
+" by the server, and -k parameter is on command=
line,=0D
+" the file wil be saved either as localfilepath=
filename,\r\n"=0D
+" or as an URI name in the current directory.\r=
\n"=0D
+" If '/' is at the end of the URL, and no locaf=
ilepath filename\r\n"=0D
+" is given on the command line, the file will b=
e retrieved as\r\n"=0D
+" index.html.\r\n"=0D
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c b/ShellPk=
g/DynamicCommand/HttpDynamicCommand/HttpApp.c
new file mode 100644
index 000000000000..7bd5b46d3997
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
@@ -0,0 +1,53 @@
+/** @file=0D
+ Entrypoint of "http" shell standalone application.=0D
+=0D
+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved. <BR>=
=0D
+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>=0D
+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>=0D
+=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+#include "Http.h"=0D
+=0D
+/*=0D
+ * String token ID of help message text.=0D
+ * Shell supports to find help message in the resource section of an=0D
+ * application image if * .MAN file is not found.=0D
+ * This global variable is added to make build tool recognizes=0D
+ * that the help string is consumed by user and then build tool will=0D
+ * add the string into the resource section.=0D
+ * Thus the application can use '-?' option to show help message in Shell.=
=0D
+ */=0D
+GLOBAL_REMOVE_IF_UNREFERENCED=0D
+EFI_STRING_ID mStringHelpTokenId =3D STRING_TOKEN (STR_GET_HELP_HTTP);=0D
+=0D
+/**=0D
+ Entry point of Http standalone application.=0D
+=0D
+ @param ImageHandle The image handle of the process.=0D
+ @param SystemTable The EFI System Table pointer.=0D
+=0D
+ @retval EFI_SUCCESS Http command is executed sucessfully.=0D
+ @retval EFI_ABORTED HII package was failed to initialize.=0D
+ @retval others Other errors when executing http command.=
=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+HttpAppInitialize (=0D
+ IN EFI_HANDLE ImageHandle,=0D
+ IN EFI_SYSTEM_TABLE *SystemTable=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+ mHttpHiiHandle =3D InitializeHiiPackage (ImageHandle);=0D
+ if (mHttpHiiHandle =3D=3D NULL) {=0D
+ return EFI_ABORTED;=0D
+ }=0D
+=0D
+ Status =3D (EFI_STATUS)RunHttp (ImageHandle, SystemTable);=0D
+=0D
+ HiiRemovePackages (mHttpHiiHandle);=0D
+=0D
+ return Status;=0D
+}=0D
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf b/Shell=
Pkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
new file mode 100644
index 000000000000..d08d47fb37d5
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
@@ -0,0 +1,58 @@
+## @file=0D
+# Provides Shell 'http' standalone application.=0D
+#=0D
+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved. <BR>=
=0D
+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>=0D
+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>=0D
+#=0D
+# SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+#=0D
+#=0D
+##=0D
+=0D
+[Defines]=0D
+ INF_VERSION =3D 0x00010006=0D
+ BASE_NAME =3D http=0D
+ FILE_GUID =3D 56B00FB7-91D2-869B-CE5C-26CD1A89C73C=
=0D
+ MODULE_TYPE =3D UEFI_APPLICATION=0D
+ VERSION_STRING =3D 1.0=0D
+ ENTRY_POINT =3D HttpAppInitialize=0D
+#=0D
+# This flag specifies whether HII resource section is generated into PE i=
mage.=0D
+#=0D
+ UEFI_HII_RESOURCE_SECTION =3D TRUE=0D
+=0D
+[Sources.common]=0D
+ Http.c=0D
+ HttpApp.c=0D
+ Http.h=0D
+ Http.uni=0D
+=0D
+[Packages]=0D
+ EmbeddedPkg/EmbeddedPkg.dec=0D
+ MdeModulePkg/MdeModulePkg.dec=0D
+ MdePkg/MdePkg.dec=0D
+ NetworkPkg/NetworkPkg.dec=0D
+ ShellPkg/ShellPkg.dec=0D
+=0D
+[LibraryClasses]=0D
+ BaseLib=0D
+ BaseMemoryLib=0D
+ DebugLib=0D
+ FileHandleLib=0D
+ HiiLib=0D
+ HttpLib=0D
+ MemoryAllocationLib=0D
+ NetLib=0D
+ ShellLib=0D
+ UefiApplicationEntryPoint=0D
+ UefiBootServicesTableLib=0D
+ UefiHiiServicesLib=0D
+ UefiLib=0D
+ UefiRuntimeServicesTableLib=0D
+=0D
+[Protocols]=0D
+ gEfiHiiPackageListProtocolGuid ## CONSUMES=0D
+ gEfiHttpProtocolGuid ## CONSUMES=0D
+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES=0D
+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES=0D
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.=
c b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c
new file mode 100644
index 000000000000..ba654749a075
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c
@@ -0,0 +1,134 @@
+/** @file=0D
+ Produce "http" shell dynamic command.=0D
+=0D
+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved. <BR>=
=0D
+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>=0D
+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>=0D
+=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+**/=0D
+#include <Protocol/ShellDynamicCommand.h>=0D
+#include "Http.h"=0D
+=0D
+/**=0D
+ This is the shell command handler function pointer callback type. This=
=0D
+ function handles the command when it is invoked in the shell.=0D
+=0D
+ @param[in] This The instance of the=0D
+ EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.=0D
+ @param[in] SystemTable The pointer to the system table.=0D
+ @param[in] ShellParameters The parameters associated with the command=
.=0D
+ @param[in] Shell The instance of the shell protocol used in=
=0D
+ the context of processing this command.=0D
+=0D
+ @return EFI_SUCCESS the operation was sucessful=0D
+ @return other the operation failed.=0D
+**/=0D
+SHELL_STATUS=0D
+EFIAPI=0D
+HttpCommandHandler (=0D
+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,=0D
+ IN EFI_SYSTEM_TABLE *SystemTable,=0D
+ IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,=0D
+ IN EFI_SHELL_PROTOCOL *Shell=0D
+ )=0D
+{=0D
+ gEfiShellParametersProtocol =3D ShellParameters;=0D
+ gEfiShellProtocol =3D Shell;=0D
+=0D
+ return RunHttp (gImageHandle, SystemTable);=0D
+}=0D
+=0D
+/**=0D
+ This is the command help handler function pointer callback type. This=0D
+ function is responsible for displaying help information for the associat=
ed=0D
+ command.=0D
+=0D
+ @param[in] This The instance of the EFI_SHELL_DYNAMIC_COMMAND_PRO=
TOCOL.=0D
+ @param[in] Language The pointer to the language string to use.=0D
+=0D
+ @return string Pool allocated help string, must be freed by call=
er=0D
+**/=0D
+CHAR16 *=0D
+EFIAPI=0D
+HttpCommandGetHelp (=0D
+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,=0D
+ IN CONST CHAR8 *Language=0D
+ )=0D
+{=0D
+ return HiiGetString(mHttpHiiHandle,=0D
+ STRING_TOKEN (STR_GET_HELP_HTTP),=0D
+ Language);=0D
+}=0D
+=0D
+EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL mHttpDynamicCommand =3D {=0D
+ HTTP_APP_NAME,=0D
+ HttpCommandHandler,=0D
+ HttpCommandGetHelp=0D
+};=0D
+=0D
+/**=0D
+ Entry point of Http Dynamic Command.=0D
+=0D
+ Produce the DynamicCommand protocol to handle "http" command.=0D
+=0D
+ @param ImageHandle The image handle of the process.=0D
+ @param SystemTable The EFI System Table pointer.=0D
+=0D
+ @retval EFI_SUCCESS Http command is executed sucessfully.=0D
+ @retval EFI_ABORTED HII package was failed to initialize.=0D
+ @retval others Other errors when executing http command.=
=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+HttpCommandInitialize (=0D
+ IN EFI_HANDLE ImageHandle,=0D
+ IN EFI_SYSTEM_TABLE *SystemTable=0D
+ )=0D
+{=0D
+ EFI_STATUS Status;=0D
+=0D
+ mHttpHiiHandle =3D InitializeHiiPackage (ImageHandle);=0D
+ if (mHttpHiiHandle =3D=3D NULL) {=0D
+ return EFI_ABORTED;=0D
+ }=0D
+=0D
+ Status =3D gBS->InstallProtocolInterface (=0D
+ &ImageHandle,=0D
+ &gEfiShellDynamicCommandProtocolGuid,=0D
+ EFI_NATIVE_INTERFACE,=0D
+ &mHttpDynamicCommand=0D
+ );=0D
+ ASSERT_EFI_ERROR (Status);=0D
+ return Status;=0D
+}=0D
+=0D
+/**=0D
+ Http driver unload handler.=0D
+=0D
+ @param ImageHandle The image handle of the process.=0D
+=0D
+ @retval EFI_SUCCESS The image is unloaded.=0D
+ @retval Others Failed to unload the image.=0D
+**/=0D
+EFI_STATUS=0D
+EFIAPI=0D
+HttpUnload (=0D
+ IN EFI_HANDLE ImageHandle=0D
+)=0D
+{=0D
+ EFI_STATUS Status;=0D
+ Status =3D gBS->UninstallProtocolInterface (=0D
+ ImageHandle,=0D
+ &gEfiShellDynamicCommandProtocolGuid,=0D
+ &mHttpDynamicCommand=0D
+ );=0D
+ if (EFI_ERROR (Status)) {=0D
+ return Status;=0D
+ }=0D
+=0D
+ HiiRemovePackages (mHttpHiiHandle);=0D
+=0D
+ return EFI_SUCCESS;=0D
+}=0D
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.=
inf b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf
new file mode 100644
index 000000000000..5d46ee2384d5
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf
@@ -0,0 +1,63 @@
+## @file=0D
+# Provides Shell 'http' dynamic command.=0D
+#=0D
+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved. <BR>=
=0D
+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>=0D
+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>=0D
+#=0D
+# SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+#=0D
+#=0D
+##=0D
+=0D
+[Defines]=0D
+ INF_VERSION =3D 0x00010006=0D
+ BASE_NAME =3D httpDynamicCommand=0D
+ FILE_GUID =3D 19618BCE-55AE-09C6-37E9-4CE04084C7A1=
=0D
+ MODULE_TYPE =3D DXE_DRIVER=0D
+ VERSION_STRING =3D 1.0=0D
+ ENTRY_POINT =3D HttpCommandInitialize=0D
+ UNLOAD_IMAGE =3D HttpUnload=0D
+#=0D
+# This flag specifies whether HII resource section is generated into PE i=
mage.=0D
+#=0D
+ UEFI_HII_RESOURCE_SECTION =3D TRUE=0D
+=0D
+[Sources.common]=0D
+ Http.c=0D
+ HttpDynamicCommand.c=0D
+ Http.h=0D
+ Http.uni=0D
+=0D
+[Packages]=0D
+ EmbeddedPkg/EmbeddedPkg.dec=0D
+ MdePkg/MdePkg.dec=0D
+ MdeModulePkg/MdeModulePkg.dec=0D
+ NetworkPkg/NetworkPkg.dec=0D
+ ShellPkg/ShellPkg.dec=0D
+=0D
+[LibraryClasses]=0D
+ BaseLib=0D
+ BaseMemoryLib=0D
+ DebugLib=0D
+ FileHandleLib=0D
+ HiiLib=0D
+ HttpLib=0D
+ MemoryAllocationLib=0D
+ NetLib=0D
+ ShellLib=0D
+ UefiBootServicesTableLib=0D
+ UefiDriverEntryPoint=0D
+ UefiHiiServicesLib=0D
+ UefiLib=0D
+ UefiRuntimeServicesTableLib=0D
+=0D
+[Protocols]=0D
+ gEfiHiiPackageListProtocolGuid ## CONSUMES=0D
+ gEfiHttpProtocolGuid ## CONSUMES=0D
+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES=0D
+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES=0D
+ gEfiShellDynamicCommandProtocolGuid ## PRODUCES=0D
+=0D
+[DEPEX]=0D
+ TRUE=0D
diff --git a/ShellPkg/Include/Guid/ShellLibHiiGuid.h b/ShellPkg/Include/Gui=
d/ShellLibHiiGuid.h
index 5da9128333a4..6e328b460d8c 100644
--- a/ShellPkg/Include/Guid/ShellLibHiiGuid.h
+++ b/ShellPkg/Include/Guid/ShellLibHiiGuid.h
@@ -59,6 +59,10 @@
0x738a9314, 0x82c1, 0x4592, { 0x8f, 0xf7, 0xc1, 0xbd, 0xf1, 0xb2, 0x0e=
, 0xd4 } \=0D
}=0D
=0D
+#define SHELL_HTTP_HII_GUID \=0D
+ { \=0D
+ 0x390f84b3, 0x221c, 0x4d9e, { 0xb5, 0x06, 0x6d, 0xb9, 0x42, 0x3e, 0x0a=
, 0x7e } \=0D
+ }=0D
=0D
#define SHELL_BCFG_HII_GUID \=0D
{ \=0D
@@ -75,6 +79,7 @@ extern EFI_GUID gShellLevel3HiiGuid;
extern EFI_GUID gShellNetwork1HiiGuid;=0D
extern EFI_GUID gShellNetwork2HiiGuid;=0D
extern EFI_GUID gShellTftpHiiGuid;=0D
+extern EFI_GUID gShellHttpHiiGuid;=0D
extern EFI_GUID gShellBcfgHiiGuid;=0D
=0D
#endif=0D
diff --git a/ShellPkg/ShellPkg.dec b/ShellPkg/ShellPkg.dec
index d0843d338126..7b2d1230bd2c 100644
--- a/ShellPkg/ShellPkg.dec
+++ b/ShellPkg/ShellPkg.dec
@@ -53,6 +53,7 @@ [Guids]
gShellNetwork1HiiGuid =3D {0xf3d301bb, 0xf4a5, 0x45a8, {0xb0, =
0xb7, 0xfa, 0x99, 0x9c, 0x62, 0x37, 0xae}}=0D
gShellNetwork2HiiGuid =3D {0x174b2b5, 0xf505, 0x4b12, {0xaa, 0=
x60, 0x59, 0xdf, 0xf8, 0xd6, 0xea, 0x37}}=0D
gShellTftpHiiGuid =3D {0x738a9314, 0x82c1, 0x4592, {0x8f, =
0xf7, 0xc1, 0xbd, 0xf1, 0xb2, 0x0e, 0xd4}}=0D
+ gShellHttpHiiGuid =3D {0x390f84b3, 0x221c, 0x4d9e, {0xb5, =
0x06, 0x6d, 0xb9, 0x42, 0x3e, 0x0a, 0x7e}}=0D
gShellBcfgHiiGuid =3D {0x5f5f605d, 0x1583, 0x4a2d, {0xa6, =
0xb2, 0xeb, 0x12, 0xda, 0xb4, 0xa2, 0xb6}}=0D
gShellAcpiViewHiiGuid =3D {0xda8ccdf4, 0xed8f, 0x4ffc, {0xb5, =
0xef, 0x2e, 0xf5, 0x5e, 0x24, 0x93, 0x2a}}=0D
# FILE_GUID as defined in ShellPkg/Application/Shell/Shell.inf=0D
diff --git a/ShellPkg/ShellPkg.dsc b/ShellPkg/ShellPkg.dsc
index 86e9f1e0040d..c42bc9464a0f 100644
--- a/ShellPkg/ShellPkg.dsc
+++ b/ShellPkg/ShellPkg.dsc
@@ -139,6 +139,11 @@ [Components]
gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE=0D
}=0D
ShellPkg/DynamicCommand/TftpDynamicCommand/TftpApp.inf=0D
+ ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf {=0D
+ <PcdsFixedAtBuild>=0D
+ gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE=0D
+ }=0D
+ ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf=0D
ShellPkg/DynamicCommand/DpDynamicCommand/DpDynamicCommand.inf {=0D
<PcdsFixedAtBuild>=0D
gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE=0D
--=20
2.26.2.266.ge870325ee8


Laszlo Ersek
 

Just some quick remarks after a comparison with v3:

On 07/27/20 18:48, Vladimir Olovyannikov via groups.io wrote:
Introduce an http client utilizing EDK2 HTTP protocol, to
allow fast image downloading from http/https servers.
HTTP download speed is usually faster than tftp.
The client is based on the same approach as tftp dynamic command, and
uses the same UEFI Shell command line parameters. This makes it easy
integrating http into existing UEFI Shell scripts.
Note that to enable HTTP download, feature Pcd
gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections must
be set to TRUE.
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2860

Signed-off-by: Vladimir Olovyannikov <vladimir.olovyannikov@...>
CC: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>
CC: Laszlo Ersek <lersek@...>
(1) These "CC" lines are not formatted correctly -- they might do the
job as far as git-send-email is concerned, but they don't satisfy
"PatchCheck.py":

ShellPkg/DynamicCommand: add HttpDynamicCommand
The commit message format is not valid:
* 'CC' should be 'Cc'
* 'CC' should be 'Cc'
https://github.com/tianocore/tianocore.github.io/wiki/Commit-Message-Format
The exit status is 255, so this would break the CI run again.

Please run "PatchCheck.py" locally before posting, and/or submit a
personal CI build.

[...]

+ if (UserNicName != NULL) {
+ if (StrCmp (NicName, UserNicName) != 0) {
+ Status = EFI_NOT_FOUND;
Change is new since v4, but not documented in the v5 changelog.

(The code may be OK, but please help reviewers with the v(n) -> v(n+1)
changelogs.)

[...]

+ if (ShellCommandLineGetFlag (CheckPackage, L"-m")) {
+ Context.Flags |= DL_FLAG_TIME;
+ }
(2) The "-m" flag has not been removed from here

[...]

+// Download Flags
+#define DL_FLAG_TIME BIT0 // Show elapsed time.
(3) and here

+".SH SYNOPSIS\r\n"
+" \r\n"
+"HTTP [-i interface] [-l port] [-t timeout] [-s size] [-m] [-k]\r\n"
(4) and here

[...]

+" -m Measure and report download time (in seconds). \r\n"
(5) and here.

I suggest waiting for ShellPkg owner feedback before posting v6.

Thanks!
Laszlo


Liming Gao
 

Maciej, Jiaxin and Siyuan:
As you are network exporters, could you give the comments for this change?

Thanks
Liming

-----Original Message-----
From: Vladimir Olovyannikov <vladimir.olovyannikov@...>
Sent: 2020年7月28日 0:49
To: devel@edk2.groups.io
Cc: Laszlo Ersek <lersek@...>; Vladimir Olovyannikov <vladimir.olovyannikov@...>; Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Gao, Zhichao <zhichao.gao@...>; Maciej Rabeda <maciej.rabeda@...>; Wu, Jiaxin <jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray <ray.ni@...>; Gao, Liming <liming.gao@...>; Nd <nd@...>
Subject: [PATCH v5 1/1] ShellPkg/DynamicCommand: add HttpDynamicCommand

Introduce an http client utilizing EDK2 HTTP protocol, to
allow fast image downloading from http/https servers.
HTTP download speed is usually faster than tftp.
The client is based on the same approach as tftp dynamic command, and
uses the same UEFI Shell command line parameters. This makes it easy
integrating http into existing UEFI Shell scripts.
Note that to enable HTTP download, feature Pcd
gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections must
be set to TRUE.
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2860

Signed-off-by: Vladimir Olovyannikov <vladimir.olovyannikov@...>
CC: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>
CC: Laszlo Ersek <lersek@...>
Cc: Zhichao Gao <zhichao.gao@...>
Cc: Maciej Rabeda <maciej.rabeda@...>
Cc: Jiaxin Wu <jiaxin.wu@...>
Cc: Siyuan Fu <@sfu5>
Cc: Ray Ni <ray.ni@...>
Cc: Liming Gao <liming.gao@...>
Cc: Nd <nd@...>
---
CryptoPkg/Library/OpensslLib/openssl | 2 +-
.../DynamicCommand/HttpDynamicCommand/Http.c | 1715 +++++++++++++++++
.../DynamicCommand/HttpDynamicCommand/Http.h | 89 +
.../HttpDynamicCommand/Http.uni | 117 ++
.../HttpDynamicCommand/HttpApp.c | 53 +
.../HttpDynamicCommand/HttpApp.inf | 58 +
.../HttpDynamicCommand/HttpDynamicCommand.c | 134 ++
.../HttpDynamicCommand/HttpDynamicCommand.inf | 63 +
ShellPkg/Include/Guid/ShellLibHiiGuid.h | 5 +
ShellPkg/ShellPkg.dec | 1 +
ShellPkg/ShellPkg.dsc | 5 +
11 files changed, 2241 insertions(+), 1 deletion(-)
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf

diff --git a/CryptoPkg/Library/OpensslLib/openssl b/CryptoPkg/Library/OpensslLib/openssl
index e2e09d9fba11..c3656cc594da 160000
--- a/CryptoPkg/Library/OpensslLib/openssl
+++ b/CryptoPkg/Library/OpensslLib/openssl
@@ -1 +1 @@
-Subproject commit e2e09d9fba1187f8d6aafaa34d4172f56f1ffb72
+Subproject commit c3656cc594daac8167721dde7220f0e59ae146fc
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
new file mode 100644
index 000000000000..0565b07c3570
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
@@ -0,0 +1,1715 @@
+/** @file

+ The implementation for the 'http' Shell command.

+

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved. <BR>

+ (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved. <BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+**/

+

+#include "Http.h"

+

+#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32

+EFI_HII_HANDLE mHttpHiiHandle;

+

+/*

+ Constant strings and definitions related to the message

+ indicating the amount of progress in the dowloading of a HTTP file.

+*/

+

+// Number of steps in the progression slider

+#define HTTP_PROGRESS_SLIDER_STEPS \

+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 3)

+

+// Size in number of characters plus one (final zero) of the message to

+// indicate the progress of an HTTP download. The format is "[(progress slider:

+// 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There

+// are thus the number of characters in HTTP_PROGR_FRAME[] plus 11 characters

+// (2 // spaces, "Kb" and seven characters for the number of KBytes).

+#define HTTP_PROGRESS_MESSAGE_SIZE \

+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) + 12)

+

+//

+// Buffer size. Note that larger buffer does not mean better speed!

+//

+#define DEFAULT_BUF_SIZE SIZE_32KB

+#define MAX_BUF_SIZE SIZE_4MB

+

+#define MIN_PARAM_COUNT 2

+#define MAX_PARAM_COUNT 4

+

+#define TIMER_MAX_TIMEOUT_S 10

+

+// File name to use when URI ends with "/"

+#define DEFAULT_HTML_FILE L"index.html"

+#define DEFAULT_HTTP_PROTO L"http"

+

+// String to delete the HTTP progress message to be able to update it :

+// (HTTP_PROGRESS_MESSAGE_SIZE-1) '\b'

+#define HTTP_PROGRESS_DEL \

+ L"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\

+\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"

+

+#define HTTP_KB L"\b\b\b\b\b\b\b\b\b\b"

+// Frame for the progression slider

+#define HTTP_PROGR_FRAME L"[ ]"

+

+// String descriptions for server errors

+STATIC CONST CHAR16 *ErrStatusDesc[] =

+{

+ L"400 Bad Request",

+ L"401 Unauthorized",

+ L"402 Payment required",

+ L"403 Forbidden",

+ L"404 Not Found",

+ L"405 Method not allowed",

+ L"406 Not acceptable",

+ L"407 Proxy authentication required",

+ L"408 Request time out",

+ L"409 Conflict",

+ L"410 Gone",

+ L"411 Length required",

+ L"412 Precondition failed",

+ L"413 Request entity too large",

+ L"414 Request URI to large",

+ L"415 Unsupported media type",

+ L"416 Requested range not satisfied",

+ L"417 Expectation failed",

+ L"500 Internal server error",

+ L"501 Not implemented",

+ L"502 Bad gateway",

+ L"503 Service unavailable",

+ L"504 Gateway timeout",

+ L"505 HTTP version not supported"

+};

+

+// Local File Handle

+STATIC SHELL_FILE_HANDLE mFileHandle = NULL;

+

+// Path of the local file, Unicode encoded

+STATIC CONST CHAR16 *mLocalFilePath;

+

+STATIC BOOLEAN gRequestCallbackComplete = FALSE;

+STATIC BOOLEAN gResponseCallbackComplete = FALSE;

+

+STATIC BOOLEAN gHttpError;

+

+/**

+ Cleans off leading and trailing spaces and tabs.

+

+ @param[in] String pointer to the string to trim them off.

+**/

+STATIC

+EFI_STATUS

+TrimSpaces (

+ IN CHAR16 **String

+ )

+{

+ ASSERT(String != NULL);

+ ASSERT(*String!= NULL);

+ //

+ // Remove any spaces and tabs at the beginning of the (*String).

+ //

+ while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {

+ CopyMem (

+ (*String),

+ (*String) + 1,

+ StrSize ((*String)) - sizeof((*String)[0])

+ );

+ }

+

+ //

+ // Remove any spaces and tabs at the end of the (*String).

+ //

+ while ((StrLen (*String) > 0) &&

+ (((*String)[StrLen ((*String)) - 1] == L' ') ||

+ ((*String)[StrLen ((*String)) - 1] == L'\t'))

+ ) {

+ (*String)[StrLen ((*String)) - 1] = CHAR_NULL;

+ }

+

+ return (EFI_SUCCESS);

+}

+

+

+/*

+ * Callbacks for request and response.

+ * We just acknowledge that operation has completed here.

+ */

+STATIC

+VOID

+EFIAPI

+RequestCallback (

+ IN EFI_EVENT Event,

+ IN VOID *Context

+)

+{

+ gRequestCallbackComplete = TRUE;

+}

+

+STATIC

+VOID

+EFIAPI

+ResponseCallback (

+ IN EFI_EVENT Event,

+ IN VOID *Context

+)

+{

+ gResponseCallbackComplete = TRUE;

+}

+

+

+/**

+ Check and convert the UINT16 option values of the 'http' command

+

+ @param[in] ValueStr Value as an Unicode encoded string

+ @param[out] Value UINT16 value

+

+ @return TRUE The value was returned.

+ @return FALSE A parsing error occured.

+**/

+STATIC

+BOOLEAN

+StringToUint16 (

+ IN CONST CHAR16 *ValueStr,

+ OUT UINT16 *Value

+ );

+

+/**

+ Get the name of the NIC.

+

+ @param[in] ControllerHandle The network physical device handle.

+ @param[in] NicNumber The network physical device number.

+ @param[out] NicName Address where to store the NIC name.

+ The memory area has to be at least

+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH

+ double byte wide.

+

+ @return EFI_SUCCESS The name of the NIC was returned.

+ @return Others The creation of the child for the Managed

+ Network Service failed or the opening of

+ the Managed Network Protocol failed or

+ the operational parameters for the

+ Managed Network Protocol could not be

+ read.

+**/

+STATIC

+EFI_STATUS

+GetNicName (

+ IN EFI_HANDLE ControllerHandle,

+ IN UINTN NicNumber,

+ OUT CHAR16 *NicName

+ );

+

+/**

+ Create a child for the service identified by its service binding protocol GUID

+ and get from the child the interface of the protocol identified by its GUID.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the

+ service to be created.

+ @param[in] ProtocolGuid GUID of the protocol to be open.

+ @param[out] ChildHandle Address where the handler of the

+ created child is returned. NULL is

+ returned in case of error.

+ @param[out] Interface Address where a pointer to the

+ protocol interface is returned in

+ case of success.

+

+ @return EFI_SUCCESS The child was created and the protocol opened.

+ @return Others Either the creation of the child or the opening

+ of the protocol failed.

+**/

+STATIC

+EFI_STATUS

+CreateServiceChildAndOpenProtocol (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ OUT EFI_HANDLE *ChildHandle,

+ OUT VOID **Interface

+ );

+

+/**

+ Close the protocol identified by its GUID on the child handle of the service

+ identified by its service binding protocol GUID, then destroy the child

+ handle.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the

+ service to be destroyed.

+ @param[in] ProtocolGuid GUID of the protocol to be closed.

+ @param[in] ChildHandle Handle of the child to be destroyed.

+

+**/

+STATIC

+VOID

+CloseProtocolAndDestroyServiceChild (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ IN EFI_HANDLE ChildHandle

+ );

+

+

+/**

+ Worker function that download the data of a file from an HTTP server given

+ the path of the file and its size.

+

+ @param[in] Context A pointer to the download context.

+

+ @retval EFI_SUCCESS The file was downloaded.

+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.

+ @retval Others The downloading of the file

+ from the server failed.

+

+**/

+STATIC

+EFI_STATUS

+DownloadFile (

+ IN DOWNLOAD_CONTEXT *Context

+ );

+

+STATIC CONST SHELL_PARAM_ITEM ParamList[] = {

+ {L"-i", TypeValue},

+ {L"-k", TypeFlag},

+ {L"-l", TypeValue},

+ {L"-s", TypeValue},

+ {L"-t", TypeValue},

+ {NULL , TypeMax}

+ };

+

+/**

+ Function for 'http' command.

+

+ @param[in] ImageHandle Handle to the Image (NULL if Internal).

+ @param[in] SystemTable Pointer to the System Table (NULL if Internal).

+

+ @return SHELL_SUCCESS The 'http' command completed successfully.

+ @return SHELL_ABORTED The Shell Library initialization failed.

+ @return SHELL_INVALID_PARAMETER At least one of the command's arguments is

+ not valid.

+ @return SHELL_OUT_OF_RESOURCES A memory allocation failed.

+ @return SHELL_NOT_FOUND Network Interface Card not found.

+ @return SHELL_UNSUPPORTED Command was valid, but the server returned

+ a status code indicating some error.

+ Examine the file requested for error body.

+

+**/

+SHELL_STATUS

+RunHttp (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+ LIST_ENTRY *CheckPackage;

+ UINTN ParamCount;

+ UINTN HandleCount;

+ UINTN NicNumber;

+ UINTN InitialSize;

+ UINTN ParamOffset;

+ UINTN StartSize;

+ CHAR16 *ProblemParam;

+ CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];

+ CHAR16 *Walker1;

+ CHAR16 *VStr;

+ CONST CHAR16 *UserNicName;

+ CONST CHAR16 *ValueStr;

+ CONST CHAR16 *RemoteFilePath;

+ CONST CHAR16 *Walker;

+ EFI_HTTP_CONFIG_DATA HttpConfigData;

+ EFI_HTTPv4_ACCESS_POINT IPv4Node;

+ EFI_HANDLE *Handles;

+ EFI_HANDLE ControllerHandle;

+ EFI_HANDLE HttpChildHandle;

+ DOWNLOAD_CONTEXT Context;

+ BOOLEAN NicFound;

+

+ ProblemParam = NULL;

+ RemoteFilePath = NULL;

+ NicFound = FALSE;

+ Handles = NULL;

+

+ //

+ // Initialize the Shell library (we must be in non-auto-init...)

+ //

+ ParamOffset = 0;

+ gHttpError = FALSE;

+

+ Status = ShellInitialize ();

+ if (EFI_ERROR (Status)) {

+ ASSERT_EFI_ERROR (Status);

+ return SHELL_ABORTED;

+ }

+

+ ZeroMem (&Context, sizeof (Context));

+

+ //

+ // Parse the command line.

+ //

+ Status = ShellCommandLineParse (

+ ParamList,

+ &CheckPackage,

+ &ProblemParam,

+ TRUE);

+ if (EFI_ERROR (Status)) {

+ if ((Status == EFI_VOLUME_CORRUPTED) &&

+ (ProblemParam != NULL) ) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mHttpHiiHandle,

+ HTTP_APP_NAME, ProblemParam

+ );

+ SHELL_FREE_NON_NULL (ProblemParam);

+ } else {

+ ASSERT (FALSE);

+ }

+

+ goto Error;

+ }

+

+ //

+ // Check the number of parameters

+ //

+ Status = EFI_INVALID_PARAMETER;

+

+ ParamCount = ShellCommandLineGetCount (CheckPackage);

+ if (ParamCount > MAX_PARAM_COUNT) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY),

+ mHttpHiiHandle, HTTP_APP_NAME

+ );

+ goto Error;

+ }

+

+ if (ParamCount < MIN_PARAM_COUNT) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW),

+ mHttpHiiHandle, HTTP_APP_NAME

+ );

+ goto Error;

+ }

+

+ ZeroMem (&HttpConfigData, sizeof (HttpConfigData));

+ ZeroMem (&IPv4Node, sizeof (IPv4Node));

+ IPv4Node.UseDefaultAddress = TRUE;

+

+ HttpConfigData.HttpVersion = HttpVersion11;

+ HttpConfigData.AccessPoint.IPv4Node = &IPv4Node;

+

+ //

+ // Get the host address (not necessarily IPv4 format)

+ //

+ ValueStr = ShellCommandLineGetRawValue (CheckPackage, 1);

+ if (!ValueStr) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), mHttpHiiHandle,

+ HTTP_APP_NAME, ValueStr

+ );

+ goto Error;

+ } else {

+ StartSize = 0;

+ if (!StrStr (ValueStr, L"://")) {

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ DEFAULT_HTTP_PROTO,

+ StrLen (DEFAULT_HTTP_PROTO)

+ );

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ L"://",

+ StrLen (L"://")

+ );

+ VStr = (CHAR16 *)ValueStr;

+ } else {

+ VStr = StrStr (ValueStr, L"://") + StrLen (L"://");

+ }

+

+ for (Walker1 = VStr; *Walker1; Walker1++) {

+ if (*Walker1 == L'/') {

+ break;

+ }

+ }

+

+ if (*Walker1 == L'/') {

+ ParamOffset = 1;

+ RemoteFilePath = Walker1;

+ }

+

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ ValueStr,

+ StrLen (ValueStr) - StrLen (Walker1)

+ );

+ if (!Context.ServerAddrAndProto) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+ }

+

+ if (!RemoteFilePath) {

+ RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);

+ if (!RemoteFilePath) {

+ // If no path given, assume just "/"

+ RemoteFilePath = L"/";

+ }

+ }

+

+ TrimSpaces ((CHAR16 **)&RemoteFilePath);

+

+ if (ParamCount == MAX_PARAM_COUNT - ParamOffset) {

+ mLocalFilePath = ShellCommandLineGetRawValue (

+ CheckPackage,

+ MAX_PARAM_COUNT - 1 - ParamOffset);

+ } else {

+ Walker = RemoteFilePath + StrLen (RemoteFilePath);

+ while ((--Walker) >= RemoteFilePath) {

+ if ((*Walker == L'\\') ||

+ (*Walker == L'/' ) ) {

+ break;

+ }

+ }

+

+ mLocalFilePath = Walker + 1;

+ }

+

+ if (!StrLen (mLocalFilePath)) {

+ mLocalFilePath = DEFAULT_HTML_FILE;

+ }

+

+ InitialSize = 0;

+ Context.URI = StrnCatGrow (

+ &Context.URI,

+ &InitialSize,

+ RemoteFilePath,

+ StrLen (RemoteFilePath)

+ );

+ if (!Context.URI) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ //

+ // Get the name of the Network Interface Card to be used if any.

+ //

+ UserNicName = ShellCommandLineGetValue (CheckPackage, L"-i");

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-l");

+ if (ValueStr != NULL) {

+ if (!StringToUint16 (

+ ValueStr,

+ &HttpConfigData.AccessPoint.IPv4Node->LocalPort

+ )

+ ) {

+ goto Error;

+ }

+ }

+

+ Context.BufferSize = DEFAULT_BUF_SIZE;

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-s");

+ if (ValueStr != NULL) {

+ Context.BufferSize = ShellStrToUintn (ValueStr);

+ if (!Context.BufferSize || Context.BufferSize > MAX_BUF_SIZE) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),

+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr

+ );

+ goto Error;

+ }

+ }

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");

+ if (ValueStr != NULL) {

+ HttpConfigData.TimeOutMillisec = (UINT32)ShellStrToUintn (ValueStr);

+ }

+

+ //

+ // Locate all HTTP Service Binding protocols

+ //

+ Status = gBS->LocateHandleBuffer (

+ ByProtocol,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ NULL,

+ &HandleCount,

+ &Handles

+ );

+ if (EFI_ERROR (Status) || (HandleCount == 0)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NO_NIC), mHttpHiiHandle

+ );

+ if (!EFI_ERROR (Status)) {

+ Status = EFI_NOT_FOUND;

+ }

+

+ goto Error;

+ }

+

+ Status = EFI_NOT_FOUND;

+

+ for (NicNumber = 0;

+ (NicNumber < HandleCount) && (Status != EFI_SUCCESS);

+ NicNumber++) {

+ ControllerHandle = Handles[NicNumber];

+

+ Status = GetNicName (ControllerHandle, NicNumber, NicName);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NAME),

+ mHttpHiiHandle, NicNumber, Status

+ );

+ continue;

+ }

+

+ if (UserNicName != NULL) {

+ if (StrCmp (NicName, UserNicName) != 0) {

+ Status = EFI_NOT_FOUND;

+ continue;

+ }

+ NicFound = TRUE;

+ }

+

+ Status = CreateServiceChildAndOpenProtocol (

+ ControllerHandle,

+ &gEfiHttpServiceBindingProtocolGuid,

+ &gEfiHttpProtocolGuid,

+ &HttpChildHandle,

+ (VOID**)&Context.Http

+ );

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_OPEN_PROTOCOL),

+ mHttpHiiHandle, NicName, Status

+ );

+ continue;

+ }

+

+ Status = Context.Http->Configure (Context.Http, &HttpConfigData);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_CONFIGURE),

+ mHttpHiiHandle, NicName, Status

+ );

+ continue;

+ }

+

+ Context.Flags = 0;

+ if (ShellCommandLineGetFlag (CheckPackage, L"-m")) {

+ Context.Flags |= DL_FLAG_TIME;

+ }

+

+ if (ShellCommandLineGetFlag (CheckPackage, L"-k")) {

+ Context.Flags |= DL_FLAG_KEEP_BAD;

+ }

+

+ Status = DownloadFile (&Context);

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle

+ );

+

+ CloseProtocolAndDestroyServiceChild (

+ ControllerHandle,

+ &gEfiHttpServiceBindingProtocolGuid,

+ &gEfiHttpProtocolGuid,

+ HttpChildHandle

+ );

+

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_DOWNLOAD),

+ mHttpHiiHandle, RemoteFilePath, NicName, Status

+ );

+ // If a user aborted the operation, do not try another controller.

+ if (Status == EFI_ABORTED) {

+ goto Error;

+ }

+ }

+

+ if (gHttpError) {

+ //

+ // This is not related to connection, so no need to repeat with

+ // another interface.

+ //

+ break;

+ }

+ }

+

+ if ((UserNicName != NULL) && (!NicFound)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NOT_FOUND),

+ mHttpHiiHandle, UserNicName

+ );

+ }

+

+Error:

+ ShellCommandLineFreeVarList (CheckPackage);

+ SHELL_FREE_NON_NULL (Handles);

+ SHELL_FREE_NON_NULL (Context.ServerAddrAndProto);

+ SHELL_FREE_NON_NULL (Context.URI);

+

+ return Status & ~MAX_BIT;

+}

+

+/**

+ Check and convert the UINT16 option values of the 'http' command

+

+ @param[in] ValueStr Value as an Unicode encoded string

+ @param[out] Value UINT16 value

+

+ @return TRUE The value was returned.

+ @return FALSE A parsing error occured.

+**/

+STATIC

+BOOLEAN

+StringToUint16 (

+ IN CONST CHAR16 *ValueStr,

+ OUT UINT16 *Value

+ )

+{

+ UINTN Val;

+

+ Val = ShellStrToUintn (ValueStr);

+ if (Val > MAX_UINT16) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),

+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr

+ );

+ return FALSE;

+ }

+

+ *Value = (UINT16)Val;

+ return TRUE;

+}

+

+/**

+ Get the name of the NIC.

+

+ @param[in] ControllerHandle The network physical device handle.

+ @param[in] NicNumber The network physical device number.

+ @param[out] NicName Address where to store the NIC name.

+ The memory area has to be at least

+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH

+ double byte wide.

+

+ @return EFI_SUCCESS The name of the NIC was returned.

+ @return Others The creation of the child for the Managed

+ Network Service failed or the opening of

+ the Managed Network Protocol failed or

+ the operational parameters for the

+ Managed Network Protocol could not be

+ read.

+**/

+STATIC

+EFI_STATUS

+GetNicName (

+ IN EFI_HANDLE ControllerHandle,

+ IN UINTN NicNumber,

+ OUT CHAR16 *NicName

+ )

+{

+ EFI_STATUS Status;

+ EFI_HANDLE MnpHandle;

+ EFI_MANAGED_NETWORK_PROTOCOL *Mnp;

+ EFI_SIMPLE_NETWORK_MODE SnpMode;

+

+ Status = CreateServiceChildAndOpenProtocol (

+ ControllerHandle,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ &gEfiManagedNetworkProtocolGuid,

+ &MnpHandle,

+ (VOID**)&Mnp

+ );

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);

+ if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {

+ goto Error;

+ }

+

+ UnicodeSPrint (

+ NicName,

+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,

+ SnpMode.IfType == NET_IFTYPE_ETHERNET ?

+ L"eth%d" :

+ L"unk%d",

+ NicNumber

+ );

+

+ Status = EFI_SUCCESS;

+

+Error:

+

+ if (MnpHandle != NULL) {

+ CloseProtocolAndDestroyServiceChild (

+ ControllerHandle,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ &gEfiManagedNetworkProtocolGuid,

+ MnpHandle

+ );

+ }

+

+ return Status;

+}

+

+/**

+ Create a child for the service identified by its service binding protocol GUID

+ and get from the child the interface of the protocol identified by its GUID.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the

+ service to be created.

+ @param[in] ProtocolGuid GUID of the protocol to be open.

+ @param[out] ChildHandle Address where the handler of the

+ created child is returned. NULL is

+ returned in case of error.

+ @param[out] Interface Address where a pointer to the

+ protocol interface is returned in

+ case of success.

+

+ @return EFI_SUCCESS The child was created and the protocol opened.

+ @return Others Either the creation of the child or the opening

+ of the protocol failed.

+**/

+STATIC

+EFI_STATUS

+CreateServiceChildAndOpenProtocol (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ OUT EFI_HANDLE *ChildHandle,

+ OUT VOID **Interface

+ )

+{

+ EFI_STATUS Status;

+

+ *ChildHandle = NULL;

+ Status = NetLibCreateServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ ChildHandle

+ );

+ if (!EFI_ERROR (Status)) {

+ Status = gBS->OpenProtocol (

+ *ChildHandle,

+ ProtocolGuid,

+ Interface,

+ gImageHandle,

+ ControllerHandle,

+ EFI_OPEN_PROTOCOL_GET_PROTOCOL

+ );

+ if (EFI_ERROR (Status)) {

+ NetLibDestroyServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ *ChildHandle

+ );

+ *ChildHandle = NULL;

+ }

+ }

+

+ return Status;

+}

+

+/**

+ Close the protocol identified by its GUID on the child handle of the service

+ identified by its service binding protocol GUID, then destroy the child

+ handle.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the

+ service to be destroyed.

+ @param[in] ProtocolGuid GUID of the protocol to be closed.

+ @param[in] ChildHandle Handle of the child to be destroyed.

+

+**/

+STATIC

+VOID

+CloseProtocolAndDestroyServiceChild (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ IN EFI_HANDLE ChildHandle

+ )

+{

+ gBS->CloseProtocol (

+ ChildHandle,

+ ProtocolGuid,

+ gImageHandle,

+ ControllerHandle

+ );

+

+ NetLibDestroyServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ ChildHandle

+ );

+}

+

+/**

+ Wait until operation completes. Completion is indicated by

+ setting of an appropriate variable.

+

+ @param[in] Context A pointer to the HTTP download context.

+ @param[in] CallBackComplete A pointer to the callback completion

+ variable set by the callback.

+

+ @return EFI_SUCCESS Callback signalled completion.

+ @return EFI_TIMEOUT Timed out waiting for completion.

+ @return Others Error waiting for completion.

+**/

+STATIC

+EFI_STATUS

+WaitForCompletion (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN OUT BOOLEAN *CallBackComplete

+ )

+{

+ EFI_STATUS Status;

+ EFI_EVENT WaitEvt;

+

+ Status = EFI_SUCCESS;

+

+ // Use a timer to measure timeout. Cannot use Stall here!

+ Status = gBS->CreateEvent (

+ EVT_TIMER,

+ TPL_CALLBACK,

+ NULL,

+ NULL,

+ &WaitEvt

+ );

+ ASSERT_EFI_ERROR (Status);

+

+ if (!EFI_ERROR (Status)) {

+ Status = gBS->SetTimer(

+ WaitEvt,

+ TimerRelative,

+ EFI_TIMER_PERIOD_SECONDS (TIMER_MAX_TIMEOUT_S)

+ );

+

+ ASSERT_EFI_ERROR (Status);

+ }

+

+ while (! *CallBackComplete &&

+ (!EFI_ERROR (Status)) &&

+ EFI_ERROR (gBS->CheckEvent (WaitEvt))

+ ) {

+ Status = Context->Http->Poll (Context->Http);

+ }

+

+ gBS->SetTimer (WaitEvt, TimerCancel, 0);

+ gBS->CloseEvent (WaitEvt);

+

+ if (*CallBackComplete) {

+ return EFI_SUCCESS;

+ }

+

+ if (!EFI_ERROR (Status)) {

+ Status = EFI_TIMEOUT;

+ }

+

+ return Status;

+}

+

+/**

+ Generate and send a request to the http server.

+

+ @param[in] Context HTTP download context.

+ @param[in] DownloadUrl Fully qualified URL to be downloaded.

+

+ @return EFI_SUCCESS Request has been sent successfully.

+ @return EFI_INVALID_PARAMETER Invalid URL.

+ @return EFI_OUT_OF_RESOURCES Out of memory.

+ @return EFI_DEVICE_ERROR If HTTPS is used, this probably

+ means that TLS support either was not

+ installed or not configured.

+ @return Others Error sending the request.

+**/

+

+STATIC

+EFI_STATUS

+SendRequest (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_HTTP_REQUEST_DATA RequestData;

+ EFI_HTTP_HEADER RequestHeader;

+ EFI_HTTP_MESSAGE RequestMessage;

+ EFI_STATUS Status;

+ CHAR16 *Host;

+ UINTN StringSize;

+

+ ZeroMem (&RequestData, sizeof (RequestData));

+ ZeroMem (&RequestHeader, sizeof (RequestHeader));

+ ZeroMem (&RequestMessage, sizeof (RequestMessage));

+ ZeroMem (&Context->RequestToken, sizeof (Context->RequestToken));

+

+ RequestHeader.FieldName = "Host";

+

+ Host = (CHAR16 *)Context->ServerAddrAndProto;

+ while (*Host != CHAR_NULL && *Host != L'/') {

+ Host++;

+ }

+

+ if (*Host == CHAR_NULL) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ //

+ // Get the next slash

+ //

+ Host++;

+ //

+ // And now the host name

+ //

+ Host++;

+

+ StringSize = StrLen (Host) + 1;

+ RequestHeader.FieldValue = AllocatePool (StringSize);

+ if (!RequestHeader.FieldValue) {

+ return EFI_OUT_OF_RESOURCES;

+ }

+

+ UnicodeStrToAsciiStrS (Host, RequestHeader.FieldValue, StringSize);

+

+ RequestMessage.HeaderCount++;

+

+ RequestData.Method = HttpMethodGet;

+ RequestData.Url = DownloadUrl;

+

+ RequestMessage.Data.Request = &RequestData;

+ RequestMessage.Headers = &RequestHeader;

+ RequestMessage.BodyLength = 0;

+ RequestMessage.Body = NULL;

+ Context->RequestToken.Event = NULL;

+

+ //

+ // Completion callback event to be set when Request completes.

+ //

+ Status = gBS->CreateEvent (

+ EVT_NOTIFY_SIGNAL,

+ TPL_CALLBACK,

+ RequestCallback,

+ Context,

+ &Context->RequestToken.Event

+ );

+ ASSERT_EFI_ERROR (Status);

+

+ Context->RequestToken.Status = EFI_SUCCESS;

+ Context->RequestToken.Message = &RequestMessage;

+ gRequestCallbackComplete = FALSE;

+ Status = Context->Http->Request (Context->Http, &Context->RequestToken);

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ Status = WaitForCompletion (Context, &gRequestCallbackComplete);

+ if (EFI_ERROR (Status)) {

+ Context->Http->Cancel (Context->Http, &Context->RequestToken);

+ }

+Error:

+ SHELL_FREE_NON_NULL (RequestHeader.FieldValue);

+ if (Context->RequestToken.Event) {

+ gBS->CloseEvent (Context->RequestToken.Event);

+ ZeroMem (&Context->RequestToken, sizeof (Context->RequestToken));

+ }

+

+ return Status;

+}

+

+/**

+ Update the progress of a file download

+ This procedure is called each time a new HTTP body portion is received.

+

+ @param[in] Context HTTP download context.

+ @param[in] DownloadLen Portion size, in bytes.

+ @param[in] Buffer The pointer to the parsed buffer.

+

+ @retval EFI_SUCCESS Portion saved.

+ @retval Other Error saving the portion.

+

+**/

+STATIC

+EFI_STATUS

+EFIAPI

+SavePortion (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN UINTN DownloadLen,

+ IN CHAR8 *Buffer

+ )

+{

+ CHAR16 Progress[HTTP_PROGRESS_MESSAGE_SIZE];

+ UINTN NbOfKb;

+ UINTN Index;

+ UINTN LastStep;

+ UINTN Step;

+ EFI_STATUS Status;

+

+ LastStep = 0;

+ Step = 0;

+ ShellSetFilePosition (mFileHandle, Context->LastReportedNbOfBytes);

+ Status = ShellWriteFile (mFileHandle, &DownloadLen, Buffer);

+ if (EFI_ERROR (Status)) {

+ if (Context->ContentDownloaded > 0) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle

+ );

+ }

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_WRITE),

+ mHttpHiiHandle, mLocalFilePath, Status

+ );

+

+ return Status;

+ }

+

+ if (Context->ContentDownloaded == 0) {

+ ShellPrintEx (-1, -1, L"%s 0 Kb", HTTP_PROGR_FRAME);

+ }

+

+ Context->ContentDownloaded += DownloadLen;

+ NbOfKb = Context->ContentDownloaded >> 10;

+

+ Progress[0] = L'\0';

+ if (Context->ContentLength) {

+ LastStep = (Context->LastReportedNbOfBytes * HTTP_PROGRESS_SLIDER_STEPS) /

+ Context->ContentLength;

+ Step = (Context->ContentDownloaded * HTTP_PROGRESS_SLIDER_STEPS) /

+ Context->ContentLength;

+ }

+

+ Context->LastReportedNbOfBytes = Context->ContentDownloaded;

+

+ if (Step <= LastStep) {

+ if (!Context->ContentLength) {

+ //

+ // Update downloaded size, there is no length info available.

+ //

+ ShellPrintEx (-1, -1, L"%s", HTTP_KB);

+ ShellPrintEx (-1, -1, L"%7d Kb", NbOfKb);

+ }

+

+ return EFI_SUCCESS;

+ }

+

+ ShellPrintEx (-1, -1, L"%s", HTTP_PROGRESS_DEL);

+

+ Status = StrCpyS (Progress, HTTP_PROGRESS_MESSAGE_SIZE, HTTP_PROGR_FRAME);

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ for (Index = 1; Index < Step; Index++) {

+ Progress[Index] = L'=';

+ }

+

+ if (Step) {

+ Progress[Step] = L'>';

+ }

+

+ UnicodeSPrint (

+ Progress + (sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 1,

+ sizeof (Progress) - sizeof (HTTP_PROGR_FRAME),

+ L" %7d Kb",

+ NbOfKb

+ );

+

+

+ ShellPrintEx (-1, -1, L"%s", Progress);

+

+ return EFI_SUCCESS;

+}

+

+/**

+ Replace the original Host and URI with Host and URI returned by the

+ HTTP server in 'Location' header (redirection).

+

+ @param[in] Location A pointer to the 'Location' string

+ provided by HTTP server.

+ @param[in] Context A pointer to HTTP download context.

+ @param[in] DownloadUrl Fully qualified HTTP URL.

+

+ @return EFI_SUCCESS Host and URI were successfully set.

+ @return EFI_OUT_OF_RESOURCES Error setting Host or URI.

+**/

+

+STATIC

+EFI_STATUS

+SetHostURI (

+ IN CHAR8 *Location,

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_STATUS Status;

+ UINTN StringSize;

+ UINTN FirstStep;

+ UINTN Idx;

+ UINTN Step;

+ CHAR8 *Walker;

+ CHAR16 *Temp;

+ CHAR8 *Tmp;

+ CHAR16 *Url;

+ BOOLEAN IsAbEmptyUrl;

+

+ Tmp = NULL;

+ Url = NULL;

+ IsAbEmptyUrl = FALSE;

+ FirstStep = 0;

+

+ StringSize = (AsciiStrSize (Location) * sizeof (CHAR16));

+ Url = AllocateZeroPool (StringSize);

+ if (!Url) {

+ return EFI_OUT_OF_RESOURCES;

+ }

+

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Location,

+ Url,

+ StringSize

+ );

+

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ //

+ // If an HTTP server redirects to the same location more than once,

+ // then stop attempts and tell it is not reachable.

+ //

+ if (!StrCmp (Url, DownloadUrl)) {

+ Status = EFI_NO_MAPPING;

+ goto Error;

+ }

+

+ if (AsciiStrLen (Location) > 2) {

+ // Some servers return 'Location: //server/resource'

+ IsAbEmptyUrl = (Location[0] == '/') && (Location[1] == '/');

+ if (IsAbEmptyUrl) {

+ // Skip first "//"

+ Location += 2;

+ FirstStep = 1;

+ }

+ }

+

+ if (AsciiStrStr (Location, "://") || IsAbEmptyUrl) {

+ Idx = 0;

+ Walker = Location;

+

+ for (Step = FirstStep; Step < 2; Step++) {

+ for (; *Walker != '/' && *Walker != '\0'; Walker++) {

+ Idx++;

+ }

+ if (!Step) {

+ //

+ // Skip "//"

+ //

+ Idx += 2;

+ Walker += 2;

+ }

+ }

+

+ Tmp = AllocateZeroPool (Idx + 1);

+ if (!Tmp) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ CopyMem (Tmp, Location, Idx);

+

+ //

+ // Location now points to URI

+ //

+ Location += Idx;

+ StringSize = (Idx + 1) * sizeof (CHAR16);

+

+ SHELL_FREE_NON_NULL (Context->ServerAddrAndProto);

+

+ Temp = AllocateZeroPool (StringSize);

+ if (!Temp) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Tmp,

+ Temp,

+ StringSize

+ );

+ if (EFI_ERROR (Status)) {

+ SHELL_FREE_NON_NULL (Temp);

+ goto Error;

+ }

+

+ Idx = 0;

+ if (IsAbEmptyUrl) {

+ Context->ServerAddrAndProto = StrnCatGrow (

+ &Context->ServerAddrAndProto,

+ &Idx,

+ L"http://",

+ StrLen (L"http://")

+ );

+ }

+

+ Context->ServerAddrAndProto = StrnCatGrow (

+ &Context->ServerAddrAndProto,

+ &Idx,

+ Temp,

+ StrLen (Temp)

+ );

+ SHELL_FREE_NON_NULL (Temp);

+ if (!Context->ServerAddrAndProto) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+ }

+

+ SHELL_FREE_NON_NULL (Context->URI);

+

+ StringSize = AsciiStrSize (Location) * sizeof (CHAR16);

+ Context->URI = AllocateZeroPool (StringSize);

+ if (!Context->URI) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ //

+ // Now make changes to the URI part.

+ //

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Location,

+ Context->URI,

+ StringSize

+ );

+Error:

+ SHELL_FREE_NON_NULL (Tmp);

+ SHELL_FREE_NON_NULL (Url);

+ return Status;

+}

+

+/**

+ Message parser callback.

+ Save a portion of HTTP body.

+

+ @param[in] EventType Type of event. Can be either

+ OnComplete or OnData.

+ @param[in] Data A pointer to the buffer with data.

+ @param[in] Length Data length of this portion.

+ @param[in] Context A pointer to the HTTP download context.

+

+ @return EFI_SUCCESS The portion was processed successfully.

+ @return Other Error returned by SavePortion.

+**/

+

+STATIC

+EFI_STATUS

+EFIAPI

+ParseMsg (

+ IN HTTP_BODY_PARSE_EVENT EventType,

+ IN CHAR8 *Data,

+ IN UINTN Length,

+ IN VOID *Context

+ )

+{

+ if (!Data || (EventType == BodyParseEventOnComplete) || !Context) {

+ return EFI_SUCCESS;

+ }

+

+ return SavePortion (Context, Length, Data);

+}

+

+

+/**

+ Get HTTP server response and collect the whole body as a file.

+ Set appropriate status in Context (REQ_OK, REQ_REPEAT, REQ_ERROR).

+ Note that even if HTTP server returns an error code, it might send

+ the body as well. This body will be collected in the resultant file.

+

+ @param[in] Context A pointer to the HTTP download context.

+ @param[in] DownloadedUrl A pointer to the fully qualified URL to download.

+

+ @return EFI_SUCCESS Valid file. Body successfully collected.

+ @return EFI_HTTP_ERROR Response is a valid HTTP response, but the

+ HTTP server

+ indicated an error (HTTP code >= 400).

+ Response body MAY contain full

+ HTTP server response.

+ @return Others Error getting the reponse from the HTTP server.

+ Response body is not collected.

+**/

+STATIC

+EFI_STATUS

+GetResponse (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_HTTP_RESPONSE_DATA ResponseData;

+ EFI_HTTP_MESSAGE ResponseMessage;

+ EFI_HTTP_HEADER *Header;

+ EFI_STATUS Status;

+ VOID *MsgParser;

+ CONST CHAR16 *Desc;

+ BOOLEAN IsTrunked;

+ BOOLEAN Redirection;

+

+ ZeroMem (&ResponseData, sizeof (ResponseData));

+ ZeroMem (&ResponseMessage, sizeof (ResponseMessage));

+ ZeroMem (&Context->ResponseToken, sizeof (Context->ResponseToken));

+ IsTrunked = FALSE;

+

+ ResponseMessage.Body = Context->Buffer;

+ Context->ResponseToken.Status = EFI_SUCCESS;

+ Context->ResponseToken.Message = &ResponseMessage;

+ Context->ContentLength = 0;

+ Context->Status = REQ_OK;

+ MsgParser = NULL;

+ ResponseData.StatusCode = HTTP_STATUS_UNSUPPORTED_STATUS;

+ ResponseMessage.Data.Response = &ResponseData;

+ Context->ResponseToken.Event = NULL;

+

+ do {

+ SHELL_FREE_NON_NULL (ResponseMessage.Headers);

+ ResponseMessage.HeaderCount = 0;

+ gResponseCallbackComplete = FALSE;

+ ResponseMessage.BodyLength = Context->BufferSize;

+

+ if (ShellGetExecutionBreakFlag ()) {

+ Status = EFI_ABORTED;

+ break;

+ }

+

+ if (!Context->ContentDownloaded && !Context->ResponseToken.Event) {

+ Status = gBS->CreateEvent (

+ EVT_NOTIFY_SIGNAL,

+ TPL_CALLBACK,

+ ResponseCallback,

+ Context,

+ &Context->ResponseToken.Event

+ );

+ ASSERT_EFI_ERROR (Status);

+ } else {

+ ResponseMessage.Data.Response = NULL;

+ }

+

+ if (EFI_ERROR (Status)) {

+ break;

+ }

+

+ Status = Context->Http->Response (Context->Http, &Context->ResponseToken);

+ if (EFI_ERROR (Status)) {

+ break;

+ }

+

+ Status = WaitForCompletion (Context, &gResponseCallbackComplete);

+ if (EFI_ERROR (Status) && ResponseMessage.HeaderCount) {

+ Status = EFI_SUCCESS;

+ }

+

+ if (EFI_ERROR (Status)) {

+ Context->Http->Cancel (Context->Http, &Context->ResponseToken);

+ break;

+ }

+

+ if (!Context->ContentDownloaded) {

+ Redirection =

+ ((ResponseData.StatusCode >= HTTP_STATUS_300_MULTIPLE_CHOICES) &&

+ (ResponseData.StatusCode <= HTTP_STATUS_307_TEMPORARY_REDIRECT)

+ ) ||

+ (ResponseData.StatusCode == HTTP_STATUS_308_PERMANENT_REDIRECT);

+

+ if (Redirection) {

+ //

+ // Need to repeat the request with new Location (server redirected).

+ //

+ Context->Status = REQ_NEED_REPEAT;

+

+ Header = HttpFindHeader (

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ "Location"

+ );

+ if (Header) {

+ Status = SetHostURI (Header->FieldValue, Context, DownloadUrl);

+ if (Status == EFI_NO_MAPPING) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),

+ mHttpHiiHandle, Context->ServerAddrAndProto,

+ L"Recursive HTTP server relocation",

+ Context->URI

+ );

+ }

+ } else {

+ //

+ // Bad reply from the server. Server must specify the location.

+ // Indicate that resource was not found, and no body collected.

+ //

+ Status = EFI_NOT_FOUND;

+ }

+

+ Context->Http->Cancel (Context->Http, &Context->ResponseToken);

+ break;

+ }

+

+ //

+ // Init message-body parser by header information.

+ //

+ if (!MsgParser) {

+ Status = HttpInitMsgParser (

+ ResponseMessage.Data.Request->Method,

+ ResponseData.StatusCode,

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ ParseMsg,

+ Context,

+ &MsgParser

+ );

+ if (EFI_ERROR (Status)) {

+ break;

+ }

+ }

+

+ //

+ // If it is a trunked message, rely on the parser.

+ //

+ Header = HttpFindHeader (

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ "Transfer-Encoding"

+ );

+ IsTrunked = (Header && !AsciiStrCmp (Header->FieldValue, "chunked"));

+

+ HttpGetEntityLength (MsgParser, &Context->ContentLength);

+

+ if (ResponseData.StatusCode >= HTTP_STATUS_400_BAD_REQUEST &&

+ (ResponseData.StatusCode != HTTP_STATUS_308_PERMANENT_REDIRECT)

+ ) {

+ //

+ // Server reported an error via Response code.

+ // Collect the body if any.

+ //

+ if (!gHttpError) {

+ gHttpError = TRUE;

+

+ Desc = ErrStatusDesc[ResponseData.StatusCode -

+ HTTP_STATUS_400_BAD_REQUEST];

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),

+ mHttpHiiHandle, Context->ServerAddrAndProto,

+ Desc,

+ Context->URI

+ );

+

+ //

+ // This gives an RFC HTTP error.

+ //

+ Context->Status = ShellStrToUintn (Desc);

+ }

+ }

+ }

+

+ // Do NOT try to parse an empty body.

+ if (ResponseMessage.BodyLength || IsTrunked) {

+ Status = HttpParseMessageBody (

+ MsgParser,

+ ResponseMessage.BodyLength,

+ ResponseMessage.Body

+ );

+ }

+ } while (!HttpIsMessageComplete (MsgParser) &&

+ !EFI_ERROR (Status) &&

+ ResponseMessage.BodyLength

+ );

+

+ SHELL_FREE_NON_NULL (MsgParser);

+ if (Context->ResponseToken.Event) {

+ gBS->CloseEvent (Context->ResponseToken.Event);

+ ZeroMem (&Context->ResponseToken, sizeof (Context->ResponseToken));

+ }

+

+ return Status;

+}

+

+/**

+ Worker function that downloads the data of a file from an HTTP server given

+ the path of the file and its size.

+

+ @param[in] Context A pointer to the HTTP download context.

+

+ @retval EFI_SUCCESS The file was downloaded.

+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.

+ #retval EFI_HTTP_ERROR The server returned a valid HTTP error.

+ Examine the mLocalFilePath file

+ to get error body.

+ @retval Others The downloading of the file from the server

+ failed.

+

+**/

+STATIC

+EFI_STATUS

+DownloadFile (

+ IN DOWNLOAD_CONTEXT *Context

+ )

+{

+ EFI_STATUS Status;

+ CHAR16 *DownloadUrl;

+ UINTN UrlSize;

+

+ ASSERT (Context);

+ if (!Context) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ DownloadUrl = NULL;

+

+ Context->Buffer = AllocatePool (Context->BufferSize);

+ if (!Context->Buffer) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto ON_EXIT;

+ }

+

+ //

+ // OPEN FILE

+ //

+ if (!EFI_ERROR (ShellFileExists (mLocalFilePath))) {

+ ShellDeleteFileByName (mLocalFilePath);

+ }

+

+ Status = ShellOpenFileByName (

+ mLocalFilePath,

+ &mFileHandle,

+ EFI_FILE_MODE_CREATE |

+ EFI_FILE_MODE_WRITE |

+ EFI_FILE_MODE_READ,

+ 0);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),

+ mHttpHiiHandle, HTTP_APP_NAME, mLocalFilePath

+ );

+ goto ON_EXIT;

+ }

+

+ do {

+ SHELL_FREE_NON_NULL (DownloadUrl);

+ UrlSize = 0;

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ Context->ServerAddrAndProto,

+ StrLen (Context->ServerAddrAndProto)

+ );

+ if (Context->URI[0] != L'/') {

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ L"/",

+ StrLen (Context->ServerAddrAndProto)

+ );

+ }

+

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ Context->URI,

+ StrLen (Context->URI));

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_DOWNLOADING),

+ mHttpHiiHandle, DownloadUrl);

+

+ Status = SendRequest (Context, DownloadUrl);

+ if (Status) {

+ goto ON_EXIT;

+ }

+

+ Status = GetResponse (Context, DownloadUrl);

+

+ if (Status) {

+ goto ON_EXIT;

+ }

+

+ } while (Context->Status == REQ_NEED_REPEAT);

+

+ if (Context->Status) {

+ Status = ENCODE_ERROR (Context->Status);

+ }

+

+ON_EXIT:

+ //

+ // CLOSE FILE

+ //

+ if (mFileHandle) {

+ if (EFI_ERROR (Status) && !(Context->Flags & DL_FLAG_KEEP_BAD)) {

+ ShellDeleteFile (&mFileHandle);

+ } else {

+ ShellCloseFile (&mFileHandle);

+ }

+ }

+

+ SHELL_FREE_NON_NULL (DownloadUrl);

+ SHELL_FREE_NON_NULL (Context->Buffer);

+

+ return Status;

+}

+

+/**

+ Retrive HII package list from ImageHandle and publish to HII database.

+

+ @param ImageHandle The image handle of the process.

+

+ @return HII handle.

+**/

+EFI_HII_HANDLE

+InitializeHiiPackage (

+ EFI_HANDLE ImageHandle

+ )

+{

+ EFI_STATUS Status;

+ EFI_HII_PACKAGE_LIST_HEADER *PackageList;

+ EFI_HII_HANDLE HiiHandle;

+

+ //

+ // Retrieve HII package list from ImageHandle

+ //

+ Status = gBS->OpenProtocol (

+ ImageHandle,

+ &gEfiHiiPackageListProtocolGuid,

+ (VOID **)&PackageList,

+ ImageHandle,

+ NULL,

+ EFI_OPEN_PROTOCOL_GET_PROTOCOL

+ );

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return NULL;

+ }

+

+ //

+ // Publish HII package list to HII Database.

+ //

+ Status = gHiiDatabase->NewPackageList (

+ gHiiDatabase,

+ PackageList,

+ NULL,

+ &HiiHandle

+ );

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return NULL;

+ }

+

+ return HiiHandle;

+}

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
new file mode 100644
index 000000000000..5c7b90e8cab4
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
@@ -0,0 +1,89 @@
+/** @file

+ Header file for 'http' command functions.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved. <BR>

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#ifndef _HTTP_H_

+#define _HTTP_H_

+

+#include <Uefi.h>

+

+#include <Library/BaseLib.h>

+#include <Library/BaseMemoryLib.h>

+#include <Library/DebugLib.h>

+#include <Library/HiiLib.h>

+#include <Library/HttpLib.h>

+#include <Library/MemoryAllocationLib.h>

+#include <Library/NetLib.h>

+#include <Library/PrintLib.h>

+#include <Library/ShellLib.h>

+#include <Library/UefiBootServicesTableLib.h>

+#include <Library/UefiHiiServicesLib.h>

+#include <Library/UefiLib.h>

+#include <Library/UefiRuntimeServicesTableLib.h>

+

+#include <Protocol/HiiPackageList.h>

+#include <Protocol/HttpUtilities.h>

+#include <Protocol/ServiceBinding.h>

+

+#define HTTP_APP_NAME L"http"

+

+#define REQ_OK 0

+#define REQ_NEED_REPEAT 1

+

+// Download Flags

+#define DL_FLAG_TIME BIT0 // Show elapsed time.

+#define DL_FLAG_KEEP_BAD BIT1 // Keep files even if download failed.

+

+extern EFI_HII_HANDLE mHttpHiiHandle;

+

+typedef struct {

+ UINTN ContentDownloaded;

+ UINTN ContentLength;

+ UINTN LastReportedNbOfBytes;

+ UINTN BufferSize;

+ UINTN Status;

+ UINTN Flags;

+ UINT8 *Buffer;

+ CHAR16 *ServerAddrAndProto;

+ CHAR16 *URI;

+ EFI_HTTP_TOKEN ResponseToken;

+ EFI_HTTP_TOKEN RequestToken;

+ EFI_HTTP_PROTOCOL *Http;

+} DOWNLOAD_CONTEXT;

+

+/**

+ Function for 'http' command.

+

+ @param[in] ImageHandle The image handle.

+ @param[in] SystemTable The system table.

+

+ @retval SHELL_SUCCESS Command completed successfully.

+ @retval SHELL_INVALID_PARAMETER Command usage error.

+ @retval SHELL_ABORTED The user aborts the operation.

+ @retval value Unknown error.

+**/

+SHELL_STATUS

+RunHttp (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ );

+

+/**

+ Retrive HII package list from ImageHandle and publish to HII database.

+

+ @param ImageHandle The image handle of the process.

+

+ @return HII handle.

+**/

+EFI_HII_HANDLE

+InitializeHiiPackage (

+ EFI_HANDLE ImageHandle

+ );

+#endif // _HTTP_H_

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
new file mode 100644
index 000000000000..00cf05deeb5c
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
@@ -0,0 +1,117 @@
+// /**

+//

+// (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>

+// Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved. <BR>

+// Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+// SPDX-License-Identifier: BSD-2-Clause-Patent

+//

+// Module Name:

+//

+// Http.uni

+//

+// Abstract:

+//

+// String definitions for UEFI Shell HTTP command

+//

+//

+// **/

+

+/=#

+

+#langdef en-US "english"

+

+#string STR_GEN_TOO_MANY #language en-US "%H%s%N: Too many arguments. Try help http.\r\n"

+#string STR_GEN_TOO_FEW #language en-US "%H%s%N: Too few arguments. Try help http.\r\n"

+#string STR_GEN_PARAM_INV #language en-US "%H%s%N: Invalid argument - '%H%s%N'. Try help http.\r\n"

+#string STR_GEN_PROBLEM #language en-US "%H%s%N: Unknown flag - '%H%s%N'. Try help http.\r\n"

+#string STR_GEN_FILE_OPEN_FAIL #language en-US "%H%s%N: Cannot open file - '%H%s%N'\r\n"

+#string STR_GEN_CRLF #language en-US "\r\n"

+

+#string STR_HTTP_ERR_NO_NIC #language en-US "No network interface card found.\r\n"

+#string STR_HTTP_ERR_NIC_NAME #language en-US "Failed to get the name of the network interface card number %d - %r\r\n"

+#string STR_HTTP_ERR_OPEN_PROTOCOL #language en-US "Unable to open HTTP protocol on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_CONFIGURE #language en-US "Unable to configure HTTP protocol on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_DOWNLOAD #language en-US "Unable to download the file '%H%s%N' on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_WRITE #language en-US "Unable to write into file '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_NIC_NOT_FOUND #language en-US "Network Interface Card '%H%s%N' not found.\r\n"

+#string STR_HTTP_ERR_STATUSCODE #language en-US "\r'%H%s%N' reports '%s' for '%H%s%N' \r\n"

+#string STR_HTTP_DOWNLOADING #language en-US "Downloading '%H%s%N'\r\n"

+

+#string STR_GET_HELP_HTTP #language en-US ""

+".TH http 0 "Download a file from HTTP server."\r\n"

+".SH NAME\r\n"

+"Download a file from HTTP server.\r\n"

+".SH SYNOPSIS\r\n"

+" \r\n"

+"HTTP [-i interface] [-l port] [-t timeout] [-s size] [-m] [-k]\r\n"

+" <URL> [localfilepath]\r\n"

+".SH OPTIONS\r\n"

+" \r\n"

+" -i interface - Specifies an adapter name, i.e., eth0.\r\n"

+" -k Keep the downloaded file even if there was an error.\r\n"

+" If this parameter is not used, the file will be deleted.\r\n"

+" -l port - Specifies the local port number. Default value is 0\r\n"

+" and the port number is automatically assigned.\r\n"

+" -m Measure and report download time (in seconds). \r\n"

+" -s size The size of the download buffer for a chunk, in bytes.\r\n"

+" Default is 32K. Note that larger buffer does not imply\r\n"

+" better speed.\r\n"

+" -t timeout - The number of seconds to wait for completion of\r\n"

+" requests and responses. Default is 0 which is 'automatic'.\r\n"

+" %HURL%N\r\n"

+" Two types of providing of URLs are supported:\r\n"

+" 1. tftp-like, where host and http_uri are separate parameters\r\n"

+" (example: host /host_uri), and\r\n\"

+" 2. wget-like, where host and host_uri is one parameter.\r\n"

+" (example: host/host_uri)\r\n"

+"\r\n"

+" host - Specifies HTTP Server address.\r\n

+ Can be either IPv4 address or 'http (or https)://addr'\r\n

+ Can use addresses resolvable by DNS as well. \r\n

+ Port can be specified after ':' if needed. \r\n

+ By default port 80 is used.\r\n"

+" http_uri - HTTP server URI to download the file.\r\n"

+"\r\n"

+" localfilepath - Local destination file path.\r\n"

+".SH DESCRIPTION\r\n"

+" \r\n"

+"NOTES:\r\n"

+" 1. The HTTP command allows geting of the file specified by its 'http_uri'\r\n"

+" path from the HTTP server specified by its 'host' IPv4 address. If the\r\n"

+" optional 'localfilepath' parameter is provided, the downloaded file is\r\n"

+" stored locally using the provided file path. If the local file path is\r\n"

+" not specified, the file is stored in the current directory using the file\r\n"

+" server's name.\r\n"

+" 2. Before using the HTTP command, the network interface intended to be\r\n"

+" used to retrieve the file must be configured. This configuration may be\r\n"

+" done by means of the 'ifconfig' command.\r\n"

+" 3. If a network interface is defined with the '-i' option then only this\r\n"

+" interface will be used to retrieve the remote file. Otherwise, all network\r\n"

+" interfaces are tried in the order they have been discovered during the\r\n"

+" DXE phase.\r\n"

+".SH EXAMPLES\r\n"

+" \r\n"

+"EXAMPLES:\r\n"

+" * To get the file "dir1/file1.dat" from the HTTP server 192.168.1.1, port 8080, and\r\n"

+" store it as file2.dat in the current directory (use tftp-like URL format) :\r\n"

+" fs0:\> http 192.168.1.1:8080 dir1/file1.dat file2.dat\r\n"

+" * To get the file /image.bin via HTTPS from server 192.168.1.1 at port 443 \r\n"

+" (default HTTPS port), and store it in the current directory: \r\n"

+" fs0:\> http https://192.168.1.1 image.bin\r\n"

+" To get an index file from http://google.com and place it into the \r\n"

+" current directory:\r\n"

+" fs0:\> http google.com index.html\r\n"

+".SH RETURNVALUES\r\n"

+" \r\n"

+"RETURN VALUES:\r\n"

+" SHELL_SUCCESS The action was completed as requested.\r\n"

+" SHELL_INVALID_PARAMETER One of the passed-in parameters was incorrectly\r\n"

+" formatted or its value was out of bounds.\r\n"

+" HTTP_ERROR No EFI errors, but the server reported a status code\r\n"

+" which should be treated as an error. If an error body sent\r\n"

+" by the server, and -k parameter is on command line,

+" the file wil be saved either as localfilepath filename,\r\n"

+" or as an URI name in the current directory.\r\n"

+" If '/' is at the end of the URL, and no locafilepath filename\r\n"

+" is given on the command line, the file will be retrieved as\r\n"

+" index.html.\r\n"

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
new file mode 100644
index 000000000000..7bd5b46d3997
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
@@ -0,0 +1,53 @@
+/** @file

+ Entrypoint of "http" shell standalone application.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved. <BR>

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+#include "Http.h"

+

+/*

+ * String token ID of help message text.

+ * Shell supports to find help message in the resource section of an

+ * application image if * .MAN file is not found.

+ * This global variable is added to make build tool recognizes

+ * that the help string is consumed by user and then build tool will

+ * add the string into the resource section.

+ * Thus the application can use '-?' option to show help message in Shell.

+ */

+GLOBAL_REMOVE_IF_UNREFERENCED

+EFI_STRING_ID mStringHelpTokenId = STRING_TOKEN (STR_GET_HELP_HTTP);

+

+/**

+ Entry point of Http standalone application.

+

+ @param ImageHandle The image handle of the process.

+ @param SystemTable The EFI System Table pointer.

+

+ @retval EFI_SUCCESS Http command is executed sucessfully.

+ @retval EFI_ABORTED HII package was failed to initialize.

+ @retval others Other errors when executing http command.

+**/

+EFI_STATUS

+EFIAPI

+HttpAppInitialize (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+ mHttpHiiHandle = InitializeHiiPackage (ImageHandle);

+ if (mHttpHiiHandle == NULL) {

+ return EFI_ABORTED;

+ }

+

+ Status = (EFI_STATUS)RunHttp (ImageHandle, SystemTable);

+

+ HiiRemovePackages (mHttpHiiHandle);

+

+ return Status;

+}

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
new file mode 100644
index 000000000000..d08d47fb37d5
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
@@ -0,0 +1,58 @@
+## @file

+# Provides Shell 'http' standalone application.

+#

+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved. <BR>

+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010006

+ BASE_NAME = http

+ FILE_GUID = 56B00FB7-91D2-869B-CE5C-26CD1A89C73C

+ MODULE_TYPE = UEFI_APPLICATION

+ VERSION_STRING = 1.0

+ ENTRY_POINT = HttpAppInitialize

+#

+# This flag specifies whether HII resource section is generated into PE image.

+#

+ UEFI_HII_RESOURCE_SECTION = TRUE

+

+[Sources.common]

+ Http.c

+ HttpApp.c

+ Http.h

+ Http.uni

+

+[Packages]

+ EmbeddedPkg/EmbeddedPkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ MdePkg/MdePkg.dec

+ NetworkPkg/NetworkPkg.dec

+ ShellPkg/ShellPkg.dec

+

+[LibraryClasses]

+ BaseLib

+ BaseMemoryLib

+ DebugLib

+ FileHandleLib

+ HiiLib

+ HttpLib

+ MemoryAllocationLib

+ NetLib

+ ShellLib

+ UefiApplicationEntryPoint

+ UefiBootServicesTableLib

+ UefiHiiServicesLib

+ UefiLib

+ UefiRuntimeServicesTableLib

+

+[Protocols]

+ gEfiHiiPackageListProtocolGuid ## CONSUMES

+ gEfiHttpProtocolGuid ## CONSUMES

+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES

+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c
new file mode 100644
index 000000000000..ba654749a075
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c
@@ -0,0 +1,134 @@
+/** @file

+ Produce "http" shell dynamic command.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved. <BR>

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+#include <Protocol/ShellDynamicCommand.h>

+#include "Http.h"

+

+/**

+ This is the shell command handler function pointer callback type. This

+ function handles the command when it is invoked in the shell.

+

+ @param[in] This The instance of the

+ EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.

+ @param[in] SystemTable The pointer to the system table.

+ @param[in] ShellParameters The parameters associated with the command.

+ @param[in] Shell The instance of the shell protocol used in

+ the context of processing this command.

+

+ @return EFI_SUCCESS the operation was sucessful

+ @return other the operation failed.

+**/

+SHELL_STATUS

+EFIAPI

+HttpCommandHandler (

+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,

+ IN EFI_SYSTEM_TABLE *SystemTable,

+ IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,

+ IN EFI_SHELL_PROTOCOL *Shell

+ )

+{

+ gEfiShellParametersProtocol = ShellParameters;

+ gEfiShellProtocol = Shell;

+

+ return RunHttp (gImageHandle, SystemTable);

+}

+

+/**

+ This is the command help handler function pointer callback type. This

+ function is responsible for displaying help information for the associated

+ command.

+

+ @param[in] This The instance of the EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.

+ @param[in] Language The pointer to the language string to use.

+

+ @return string Pool allocated help string, must be freed by caller

+**/

+CHAR16 *

+EFIAPI

+HttpCommandGetHelp (

+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,

+ IN CONST CHAR8 *Language

+ )

+{

+ return HiiGetString(mHttpHiiHandle,

+ STRING_TOKEN (STR_GET_HELP_HTTP),

+ Language);

+}

+

+EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL mHttpDynamicCommand = {

+ HTTP_APP_NAME,

+ HttpCommandHandler,

+ HttpCommandGetHelp

+};

+

+/**

+ Entry point of Http Dynamic Command.

+

+ Produce the DynamicCommand protocol to handle "http" command.

+

+ @param ImageHandle The image handle of the process.

+ @param SystemTable The EFI System Table pointer.

+

+ @retval EFI_SUCCESS Http command is executed sucessfully.

+ @retval EFI_ABORTED HII package was failed to initialize.

+ @retval others Other errors when executing http command.

+**/

+EFI_STATUS

+EFIAPI

+HttpCommandInitialize (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+

+ mHttpHiiHandle = InitializeHiiPackage (ImageHandle);

+ if (mHttpHiiHandle == NULL) {

+ return EFI_ABORTED;

+ }

+

+ Status = gBS->InstallProtocolInterface (

+ &ImageHandle,

+ &gEfiShellDynamicCommandProtocolGuid,

+ EFI_NATIVE_INTERFACE,

+ &mHttpDynamicCommand

+ );

+ ASSERT_EFI_ERROR (Status);

+ return Status;

+}

+

+/**

+ Http driver unload handler.

+

+ @param ImageHandle The image handle of the process.

+

+ @retval EFI_SUCCESS The image is unloaded.

+ @retval Others Failed to unload the image.

+**/

+EFI_STATUS

+EFIAPI

+HttpUnload (

+ IN EFI_HANDLE ImageHandle

+)

+{

+ EFI_STATUS Status;

+ Status = gBS->UninstallProtocolInterface (

+ ImageHandle,

+ &gEfiShellDynamicCommandProtocolGuid,

+ &mHttpDynamicCommand

+ );

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ HiiRemovePackages (mHttpHiiHandle);

+

+ return EFI_SUCCESS;

+}

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf
new file mode 100644
index 000000000000..5d46ee2384d5
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf
@@ -0,0 +1,63 @@
+## @file

+# Provides Shell 'http' dynamic command.

+#

+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved. <BR>

+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010006

+ BASE_NAME = httpDynamicCommand

+ FILE_GUID = 19618BCE-55AE-09C6-37E9-4CE04084C7A1

+ MODULE_TYPE = DXE_DRIVER

+ VERSION_STRING = 1.0

+ ENTRY_POINT = HttpCommandInitialize

+ UNLOAD_IMAGE = HttpUnload

+#

+# This flag specifies whether HII resource section is generated into PE image.

+#

+ UEFI_HII_RESOURCE_SECTION = TRUE

+

+[Sources.common]

+ Http.c

+ HttpDynamicCommand.c

+ Http.h

+ Http.uni

+

+[Packages]

+ EmbeddedPkg/EmbeddedPkg.dec

+ MdePkg/MdePkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ NetworkPkg/NetworkPkg.dec

+ ShellPkg/ShellPkg.dec

+

+[LibraryClasses]

+ BaseLib

+ BaseMemoryLib

+ DebugLib

+ FileHandleLib

+ HiiLib

+ HttpLib

+ MemoryAllocationLib

+ NetLib

+ ShellLib

+ UefiBootServicesTableLib

+ UefiDriverEntryPoint

+ UefiHiiServicesLib

+ UefiLib

+ UefiRuntimeServicesTableLib

+

+[Protocols]

+ gEfiHiiPackageListProtocolGuid ## CONSUMES

+ gEfiHttpProtocolGuid ## CONSUMES

+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES

+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES

+ gEfiShellDynamicCommandProtocolGuid ## PRODUCES

+

+[DEPEX]

+ TRUE

diff --git a/ShellPkg/Include/Guid/ShellLibHiiGuid.h b/ShellPkg/Include/Guid/ShellLibHiiGuid.h
index 5da9128333a4..6e328b460d8c 100644
--- a/ShellPkg/Include/Guid/ShellLibHiiGuid.h
+++ b/ShellPkg/Include/Guid/ShellLibHiiGuid.h
@@ -59,6 +59,10 @@
0x738a9314, 0x82c1, 0x4592, { 0x8f, 0xf7, 0xc1, 0xbd, 0xf1, 0xb2, 0x0e, 0xd4 } \

}



+#define SHELL_HTTP_HII_GUID \

+ { \

+ 0x390f84b3, 0x221c, 0x4d9e, { 0xb5, 0x06, 0x6d, 0xb9, 0x42, 0x3e, 0x0a, 0x7e } \

+ }



#define SHELL_BCFG_HII_GUID \

{ \

@@ -75,6 +79,7 @@ extern EFI_GUID gShellLevel3HiiGuid;
extern EFI_GUID gShellNetwork1HiiGuid;

extern EFI_GUID gShellNetwork2HiiGuid;

extern EFI_GUID gShellTftpHiiGuid;

+extern EFI_GUID gShellHttpHiiGuid;

extern EFI_GUID gShellBcfgHiiGuid;



#endif

diff --git a/ShellPkg/ShellPkg.dec b/ShellPkg/ShellPkg.dec
index d0843d338126..7b2d1230bd2c 100644
--- a/ShellPkg/ShellPkg.dec
+++ b/ShellPkg/ShellPkg.dec
@@ -53,6 +53,7 @@ [Guids]
gShellNetwork1HiiGuid = {0xf3d301bb, 0xf4a5, 0x45a8, {0xb0, 0xb7, 0xfa, 0x99, 0x9c, 0x62, 0x37, 0xae}}

gShellNetwork2HiiGuid = {0x174b2b5, 0xf505, 0x4b12, {0xaa, 0x60, 0x59, 0xdf, 0xf8, 0xd6, 0xea, 0x37}}

gShellTftpHiiGuid = {0x738a9314, 0x82c1, 0x4592, {0x8f, 0xf7, 0xc1, 0xbd, 0xf1, 0xb2, 0x0e, 0xd4}}

+ gShellHttpHiiGuid = {0x390f84b3, 0x221c, 0x4d9e, {0xb5, 0x06, 0x6d, 0xb9, 0x42, 0x3e, 0x0a, 0x7e}}

gShellBcfgHiiGuid = {0x5f5f605d, 0x1583, 0x4a2d, {0xa6, 0xb2, 0xeb, 0x12, 0xda, 0xb4, 0xa2, 0xb6}}

gShellAcpiViewHiiGuid = {0xda8ccdf4, 0xed8f, 0x4ffc, {0xb5, 0xef, 0x2e, 0xf5, 0x5e, 0x24, 0x93, 0x2a}}

# FILE_GUID as defined in ShellPkg/Application/Shell/Shell.inf

diff --git a/ShellPkg/ShellPkg.dsc b/ShellPkg/ShellPkg.dsc
index 86e9f1e0040d..c42bc9464a0f 100644
--- a/ShellPkg/ShellPkg.dsc
+++ b/ShellPkg/ShellPkg.dsc
@@ -139,6 +139,11 @@ [Components]
gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

}

ShellPkg/DynamicCommand/TftpDynamicCommand/TftpApp.inf

+ ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf {

+ <PcdsFixedAtBuild>

+ gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

+ }

+ ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf

ShellPkg/DynamicCommand/DpDynamicCommand/DpDynamicCommand.inf {

<PcdsFixedAtBuild>

gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

--
2.26.2.266.ge870325ee8


Vladimir Olovyannikov
 

Hi Laszlo,

Thank you for the comments.
I agree with them, and as you suggest, I will address them along with
comments from maintainers.

Thank you,
Vladimir

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Laszlo
Ersek
Sent: Monday, July 27, 2020 3:08 PM
To: devel@edk2.groups.io; vladimir.olovyannikov@...
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Zhichao
Gao <zhichao.gao@...>; Maciej Rabeda
<maciej.rabeda@...>; Jiaxin Wu <jiaxin.wu@...>; Siyuan
Fu <@sfu5>; Ray Ni <ray.ni@...>; Liming Gao
<liming.gao@...>; Nd <nd@...>
Subject: Re: [edk2-devel] [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

Just some quick remarks after a comparison with v3:

On 07/27/20 18:48, Vladimir Olovyannikov via groups.io wrote:
Introduce an http client utilizing EDK2 HTTP protocol, to allow fast
image downloading from http/https servers.
HTTP download speed is usually faster than tftp.
The client is based on the same approach as tftp dynamic command, and
uses the same UEFI Shell command line parameters. This makes it easy
integrating http into existing UEFI Shell scripts.
Note that to enable HTTP download, feature Pcd
gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections must be set to
TRUE.
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2860

Signed-off-by: Vladimir Olovyannikov
<vladimir.olovyannikov@...>
CC: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>
CC: Laszlo Ersek <lersek@...>
(1) These "CC" lines are not formatted correctly -- they might do the job
as
far as git-send-email is concerned, but they don't satisfy
"PatchCheck.py":

ShellPkg/DynamicCommand: add HttpDynamicCommand The commit
message
format is not valid:
* 'CC' should be 'Cc'
* 'CC' should be 'Cc'
https://github.com/tianocore/tianocore.github.io/wiki/Commit-Message-F
ormat
The exit status is 255, so this would break the CI run again.

Please run "PatchCheck.py" locally before posting, and/or submit a
personal
CI build.

[...]

+ if (UserNicName != NULL) {
+ if (StrCmp (NicName, UserNicName) != 0) {
+ Status = EFI_NOT_FOUND;
Change is new since v4, but not documented in the v5 changelog.

(The code may be OK, but please help reviewers with the v(n) -> v(n+1)
changelogs.)

[...]

+ if (ShellCommandLineGetFlag (CheckPackage, L"-m")) {
+ Context.Flags |= DL_FLAG_TIME;
+ }
(2) The "-m" flag has not been removed from here

[...]

+// Download Flags
+#define DL_FLAG_TIME BIT0 // Show elapsed time.
(3) and here

+".SH SYNOPSIS\r\n"
+" \r\n"
+"HTTP [-i interface] [-l port] [-t timeout] [-s size] [-m] [-k]\r\n"
(4) and here

[...]

+" -m Measure and report download time (in seconds).
\r\n"
(5) and here.

I suggest waiting for ShellPkg owner feedback before posting v6.

Thanks!
Laszlo



Gao, Zhichao
 

Sorry for the delay reply.

See below:

-----Original Message-----
From: Vladimir Olovyannikov <vladimir.olovyannikov@...>
Sent: Tuesday, July 28, 2020 12:49 AM
To: devel@edk2.groups.io
Cc: Laszlo Ersek <lersek@...>; Vladimir Olovyannikov
<vladimir.olovyannikov@...>; Samer El-Haj-Mahmoud <Samer.El-
Haj-Mahmoud@...>; Gao, Zhichao <zhichao.gao@...>; Maciej
Rabeda <maciej.rabeda@...>; Wu, Jiaxin <jiaxin.wu@...>; Fu,
Siyuan <@sfu5>; Ni, Ray <ray.ni@...>; Gao, Liming
<liming.gao@...>; Nd <nd@...>
Subject: [PATCH v5 1/1] ShellPkg/DynamicCommand: add HttpDynamicCommand

Introduce an http client utilizing EDK2 HTTP protocol, to
allow fast image downloading from http/https servers.
HTTP download speed is usually faster than tftp.
The client is based on the same approach as tftp dynamic command, and
uses the same UEFI Shell command line parameters. This makes it easy
integrating http into existing UEFI Shell scripts.
Note that to enable HTTP download, feature Pcd
gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections must
be set to TRUE.
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2860

Signed-off-by: Vladimir Olovyannikov <vladimir.olovyannikov@...>
CC: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>
CC: Laszlo Ersek <lersek@...>
Cc: Zhichao Gao <zhichao.gao@...>
Cc: Maciej Rabeda <maciej.rabeda@...>
Cc: Jiaxin Wu <jiaxin.wu@...>
Cc: Siyuan Fu <@sfu5>
Cc: Ray Ni <ray.ni@...>
Cc: Liming Gao <liming.gao@...>
Cc: Nd <nd@...>
---
CryptoPkg/Library/OpensslLib/openssl | 2 +-
.../DynamicCommand/HttpDynamicCommand/Http.c | 1715
+++++++++++++++++
.../DynamicCommand/HttpDynamicCommand/Http.h | 89 +
.../HttpDynamicCommand/Http.uni | 117 ++
.../HttpDynamicCommand/HttpApp.c | 53 +
.../HttpDynamicCommand/HttpApp.inf | 58 +
.../HttpDynamicCommand/HttpDynamicCommand.c | 134 ++
.../HttpDynamicCommand/HttpDynamicCommand.inf | 63 +
ShellPkg/Include/Guid/ShellLibHiiGuid.h | 5 +
ShellPkg/ShellPkg.dec | 1 +
ShellPkg/ShellPkg.dsc | 5 +
11 files changed, 2241 insertions(+), 1 deletion(-)
create mode 100644 ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf

diff --git a/CryptoPkg/Library/OpensslLib/openssl
b/CryptoPkg/Library/OpensslLib/openssl
index e2e09d9fba11..c3656cc594da 160000
--- a/CryptoPkg/Library/OpensslLib/openssl
+++ b/CryptoPkg/Library/OpensslLib/openssl
@@ -1 +1 @@
-Subproject commit e2e09d9fba1187f8d6aafaa34d4172f56f1ffb72
+Subproject commit c3656cc594daac8167721dde7220f0e59ae146fc
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
new file mode 100644
index 000000000000..0565b07c3570
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
@@ -0,0 +1,1715 @@
+/** @file

+ The implementation for the 'http' Shell command.

+

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved. <BR>

+ (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved. <BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+**/

+

+#include "Http.h"

+

+#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32

+EFI_HII_HANDLE mHttpHiiHandle;

+

+/*

+ Constant strings and definitions related to the message

+ indicating the amount of progress in the dowloading of a HTTP file.

+*/

+

+// Number of steps in the progression slider

+#define HTTP_PROGRESS_SLIDER_STEPS \

+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 3)

+

+// Size in number of characters plus one (final zero) of the message to

+// indicate the progress of an HTTP download. The format is "[(progress slider:

+// 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There

+// are thus the number of characters in HTTP_PROGR_FRAME[] plus 11
characters

+// (2 // spaces, "Kb" and seven characters for the number of KBytes).

+#define HTTP_PROGRESS_MESSAGE_SIZE \

+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) + 12)

+

+//

+// Buffer size. Note that larger buffer does not mean better speed!

+//

+#define DEFAULT_BUF_SIZE SIZE_32KB

+#define MAX_BUF_SIZE SIZE_4MB

+

+#define MIN_PARAM_COUNT 2

+#define MAX_PARAM_COUNT 4

+

+#define TIMER_MAX_TIMEOUT_S 10

+

+// File name to use when URI ends with "/"

+#define DEFAULT_HTML_FILE L"index.html"

+#define DEFAULT_HTTP_PROTO L"http"

+

+// String to delete the HTTP progress message to be able to update it :

+// (HTTP_PROGRESS_MESSAGE_SIZE-1) '\b'

+#define HTTP_PROGRESS_DEL \

+ L"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\

+\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"

+

+#define HTTP_KB L"\b\b\b\b\b\b\b\b\b\b"

+// Frame for the progression slider

+#define HTTP_PROGR_FRAME L"[ ]"

+

+// String descriptions for server errors

+STATIC CONST CHAR16 *ErrStatusDesc[] =

+{

+ L"400 Bad Request",

+ L"401 Unauthorized",

+ L"402 Payment required",

+ L"403 Forbidden",

+ L"404 Not Found",

+ L"405 Method not allowed",

+ L"406 Not acceptable",

+ L"407 Proxy authentication required",

+ L"408 Request time out",

+ L"409 Conflict",

+ L"410 Gone",

+ L"411 Length required",

+ L"412 Precondition failed",

+ L"413 Request entity too large",

+ L"414 Request URI to large",

+ L"415 Unsupported media type",

+ L"416 Requested range not satisfied",

+ L"417 Expectation failed",

+ L"500 Internal server error",

+ L"501 Not implemented",

+ L"502 Bad gateway",

+ L"503 Service unavailable",

+ L"504 Gateway timeout",

+ L"505 HTTP version not supported"

+};

+

+// Local File Handle

+STATIC SHELL_FILE_HANDLE mFileHandle = NULL;

+

+// Path of the local file, Unicode encoded

+STATIC CONST CHAR16 *mLocalFilePath;

+

+STATIC BOOLEAN gRequestCallbackComplete = FALSE;

+STATIC BOOLEAN gResponseCallbackComplete = FALSE;

+

+STATIC BOOLEAN gHttpError;

+

+/**

+ Cleans off leading and trailing spaces and tabs.

+

+ @param[in] String pointer to the string to trim them off.

+**/

+STATIC

+EFI_STATUS

+TrimSpaces (

+ IN CHAR16 **String

+ )

+{

+ ASSERT(String != NULL);

+ ASSERT(*String!= NULL);

+ //

+ // Remove any spaces and tabs at the beginning of the (*String).

+ //

+ while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {

+ CopyMem (

+ (*String),

+ (*String) + 1,

+ StrSize ((*String)) - sizeof((*String)[0])

+ );

+ }

+

+ //

+ // Remove any spaces and tabs at the end of the (*String).

+ //

+ while ((StrLen (*String) > 0) &&

+ (((*String)[StrLen ((*String)) - 1] == L' ') ||

+ ((*String)[StrLen ((*String)) - 1] == L'\t'))

+ ) {

+ (*String)[StrLen ((*String)) - 1] = CHAR_NULL;

+ }

+

+ return (EFI_SUCCESS);

+}

+

+

+/*

+ * Callbacks for request and response.

+ * We just acknowledge that operation has completed here.

+ */

+STATIC

+VOID

+EFIAPI

+RequestCallback (

+ IN EFI_EVENT Event,

+ IN VOID *Context

+)

+{

+ gRequestCallbackComplete = TRUE;

+}

+

+STATIC

+VOID

+EFIAPI

+ResponseCallback (

+ IN EFI_EVENT Event,

+ IN VOID *Context

+)

+{

+ gResponseCallbackComplete = TRUE;

+}

+

+

+/**

+ Check and convert the UINT16 option values of the 'http' command

+

+ @param[in] ValueStr Value as an Unicode encoded string

+ @param[out] Value UINT16 value

+

+ @return TRUE The value was returned.

+ @return FALSE A parsing error occured.

+**/

+STATIC

+BOOLEAN

+StringToUint16 (

+ IN CONST CHAR16 *ValueStr,

+ OUT UINT16 *Value

+ );

+

+/**

+ Get the name of the NIC.

+

+ @param[in] ControllerHandle The network physical device handle.

+ @param[in] NicNumber The network physical device number.

+ @param[out] NicName Address where to store the NIC name.

+ The memory area has to be at least

+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH

+ double byte wide.

+

+ @return EFI_SUCCESS The name of the NIC was returned.

+ @return Others The creation of the child for the Managed

+ Network Service failed or the opening of

+ the Managed Network Protocol failed or

+ the operational parameters for the

+ Managed Network Protocol could not be

+ read.

+**/

+STATIC

+EFI_STATUS

+GetNicName (

+ IN EFI_HANDLE ControllerHandle,

+ IN UINTN NicNumber,

+ OUT CHAR16 *NicName

+ );

+

+/**

+ Create a child for the service identified by its service binding protocol GUID

+ and get from the child the interface of the protocol identified by its GUID.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the

+ service to be created.

+ @param[in] ProtocolGuid GUID of the protocol to be open.

+ @param[out] ChildHandle Address where the handler of the

+ created child is returned. NULL is

+ returned in case of error.

+ @param[out] Interface Address where a pointer to the

+ protocol interface is returned in

+ case of success.

+

+ @return EFI_SUCCESS The child was created and the protocol opened.

+ @return Others Either the creation of the child or the opening

+ of the protocol failed.

+**/

+STATIC

+EFI_STATUS

+CreateServiceChildAndOpenProtocol (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ OUT EFI_HANDLE *ChildHandle,

+ OUT VOID **Interface

+ );

+

+/**

+ Close the protocol identified by its GUID on the child handle of the service

+ identified by its service binding protocol GUID, then destroy the child

+ handle.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the

+ service to be destroyed.

+ @param[in] ProtocolGuid GUID of the protocol to be closed.

+ @param[in] ChildHandle Handle of the child to be destroyed.

+

+**/

+STATIC

+VOID

+CloseProtocolAndDestroyServiceChild (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ IN EFI_HANDLE ChildHandle

+ );

+

+

+/**

+ Worker function that download the data of a file from an HTTP server given

+ the path of the file and its size.

+

+ @param[in] Context A pointer to the download context.

+

+ @retval EFI_SUCCESS The file was downloaded.

+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.

+ @retval Others The downloading of the file

+ from the server failed.

+

+**/

+STATIC

+EFI_STATUS

+DownloadFile (

+ IN DOWNLOAD_CONTEXT *Context

+ );

+

+STATIC CONST SHELL_PARAM_ITEM ParamList[] = {

+ {L"-i", TypeValue},

+ {L"-k", TypeFlag},

+ {L"-l", TypeValue},

+ {L"-s", TypeValue},

+ {L"-t", TypeValue},

+ {NULL , TypeMax}

+ };

+

+/**

+ Function for 'http' command.

+

+ @param[in] ImageHandle Handle to the Image (NULL if Internal).

+ @param[in] SystemTable Pointer to the System Table (NULL if Internal).

+

+ @return SHELL_SUCCESS The 'http' command completed successfully.

+ @return SHELL_ABORTED The Shell Library initialization failed.

+ @return SHELL_INVALID_PARAMETER At least one of the command's
arguments is

+ not valid.

+ @return SHELL_OUT_OF_RESOURCES A memory allocation failed.

+ @return SHELL_NOT_FOUND Network Interface Card not found.

+ @return SHELL_UNSUPPORTED Command was valid, but the server
returned

+ a status code indicating some error.

+ Examine the file requested for error body.

+

+**/

+SHELL_STATUS

+RunHttp (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+ LIST_ENTRY *CheckPackage;

+ UINTN ParamCount;

+ UINTN HandleCount;

+ UINTN NicNumber;

+ UINTN InitialSize;

+ UINTN ParamOffset;

+ UINTN StartSize;

+ CHAR16 *ProblemParam;

+ CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];

+ CHAR16 *Walker1;

+ CHAR16 *VStr;

+ CONST CHAR16 *UserNicName;

+ CONST CHAR16 *ValueStr;

+ CONST CHAR16 *RemoteFilePath;

+ CONST CHAR16 *Walker;

+ EFI_HTTP_CONFIG_DATA HttpConfigData;

+ EFI_HTTPv4_ACCESS_POINT IPv4Node;

+ EFI_HANDLE *Handles;

+ EFI_HANDLE ControllerHandle;

+ EFI_HANDLE HttpChildHandle;

+ DOWNLOAD_CONTEXT Context;

+ BOOLEAN NicFound;

+

+ ProblemParam = NULL;

+ RemoteFilePath = NULL;

+ NicFound = FALSE;

+ Handles = NULL;

+

+ //

+ // Initialize the Shell library (we must be in non-auto-init...)

+ //

+ ParamOffset = 0;

+ gHttpError = FALSE;

+

+ Status = ShellInitialize ();

+ if (EFI_ERROR (Status)) {

+ ASSERT_EFI_ERROR (Status);

+ return SHELL_ABORTED;

+ }

+

+ ZeroMem (&Context, sizeof (Context));

+

+ //

+ // Parse the command line.

+ //

+ Status = ShellCommandLineParse (

+ ParamList,

+ &CheckPackage,

+ &ProblemParam,

+ TRUE);
I prefer to put ');' in next line like other functions.


+ if (EFI_ERROR (Status)) {

+ if ((Status == EFI_VOLUME_CORRUPTED) &&

+ (ProblemParam != NULL) ) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mHttpHiiHandle,

+ HTTP_APP_NAME, ProblemParam

+ );

+ SHELL_FREE_NON_NULL (ProblemParam);

+ } else {

+ ASSERT (FALSE);

+ }

+

+ goto Error;

+ }

+

+ //

+ // Check the number of parameters

+ //

+ Status = EFI_INVALID_PARAMETER;

+

+ ParamCount = ShellCommandLineGetCount (CheckPackage);

+ if (ParamCount > MAX_PARAM_COUNT) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY),

+ mHttpHiiHandle, HTTP_APP_NAME

+ );

+ goto Error;

+ }

+

+ if (ParamCount < MIN_PARAM_COUNT) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW),

+ mHttpHiiHandle, HTTP_APP_NAME

+ );

+ goto Error;

+ }

+

+ ZeroMem (&HttpConfigData, sizeof (HttpConfigData));

+ ZeroMem (&IPv4Node, sizeof (IPv4Node));

+ IPv4Node.UseDefaultAddress = TRUE;

+

+ HttpConfigData.HttpVersion = HttpVersion11;

+ HttpConfigData.AccessPoint.IPv4Node = &IPv4Node;

+

+ //

+ // Get the host address (not necessarily IPv4 format)

+ //

+ ValueStr = ShellCommandLineGetRawValue (CheckPackage, 1);

+ if (!ValueStr) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), mHttpHiiHandle,

+ HTTP_APP_NAME, ValueStr

+ );

+ goto Error;

+ } else {

+ StartSize = 0;

+ if (!StrStr (ValueStr, L"://")) {

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ DEFAULT_HTTP_PROTO,

+ StrLen (DEFAULT_HTTP_PROTO)

+ );

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ L"://",

+ StrLen (L"://")

+ );

+ VStr = (CHAR16 *)ValueStr;

+ } else {

+ VStr = StrStr (ValueStr, L"://") + StrLen (L"://");

+ }

+

+ for (Walker1 = VStr; *Walker1; Walker1++) {

+ if (*Walker1 == L'/') {

+ break;

+ }

+ }

+

+ if (*Walker1 == L'/') {

+ ParamOffset = 1;

+ RemoteFilePath = Walker1;

+ }

+

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ ValueStr,

+ StrLen (ValueStr) - StrLen (Walker1)

+ );

+ if (!Context.ServerAddrAndProto) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+ }

+

+ if (!RemoteFilePath) {

+ RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);

+ if (!RemoteFilePath) {

+ // If no path given, assume just "/"

+ RemoteFilePath = L"/";

+ }

+ }

+

+ TrimSpaces ((CHAR16 **)&RemoteFilePath);

+

+ if (ParamCount == MAX_PARAM_COUNT - ParamOffset) {

+ mLocalFilePath = ShellCommandLineGetRawValue (

+ CheckPackage,

+ MAX_PARAM_COUNT - 1 - ParamOffset);

+ } else {

+ Walker = RemoteFilePath + StrLen (RemoteFilePath);

+ while ((--Walker) >= RemoteFilePath) {

+ if ((*Walker == L'\\') ||

+ (*Walker == L'/' ) ) {
It is better to remove the useless space in the above line at the end of 'if' condition.


+ break;

+ }

+ }

+

+ mLocalFilePath = Walker + 1;

+ }

+

+ if (!StrLen (mLocalFilePath)) {

+ mLocalFilePath = DEFAULT_HTML_FILE;

+ }

+

+ InitialSize = 0;

+ Context.URI = StrnCatGrow (

+ &Context.URI,

+ &InitialSize,

+ RemoteFilePath,

+ StrLen (RemoteFilePath)

+ );

+ if (!Context.URI) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ //

+ // Get the name of the Network Interface Card to be used if any.

+ //

+ UserNicName = ShellCommandLineGetValue (CheckPackage, L"-i");

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-l");

+ if (ValueStr != NULL) {

+ if (!StringToUint16 (

+ ValueStr,

+ &HttpConfigData.AccessPoint.IPv4Node->LocalPort

+ )

+ ) {
I suggest to align the end ')' of StringToUint16 to align with the parameters. And the end ')' of 'if' to go right next to the end ')' of StringToUint16.


+ goto Error;

+ }

+ }

+

+ Context.BufferSize = DEFAULT_BUF_SIZE;

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-s");

+ if (ValueStr != NULL) {

+ Context.BufferSize = ShellStrToUintn (ValueStr);

+ if (!Context.BufferSize || Context.BufferSize > MAX_BUF_SIZE) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),

+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr

+ );

+ goto Error;

+ }

+ }

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");

+ if (ValueStr != NULL) {

+ HttpConfigData.TimeOutMillisec = (UINT32)ShellStrToUintn (ValueStr);
I am not sure if it is fine to transfer '-1' as time out value when the -t with invalid number string. It depends on the NetworkPkg maintainers' review.

I am OK with other part of the patch. But I still need NetworkPkg maintainers' help to review above issue and the implementation.
There is no need to resend another patch if NetworkPkg maintainers are OK with above issue and the implementation.

Thanks,
Zhichao


+ }

+

+ //

+ // Locate all HTTP Service Binding protocols

+ //

+ Status = gBS->LocateHandleBuffer (

+ ByProtocol,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ NULL,

+ &HandleCount,

+ &Handles

+ );

+ if (EFI_ERROR (Status) || (HandleCount == 0)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NO_NIC), mHttpHiiHandle

+ );

+ if (!EFI_ERROR (Status)) {

+ Status = EFI_NOT_FOUND;

+ }

+

+ goto Error;

+ }

+

+ Status = EFI_NOT_FOUND;

+

+ for (NicNumber = 0;

+ (NicNumber < HandleCount) && (Status != EFI_SUCCESS);

+ NicNumber++) {

+ ControllerHandle = Handles[NicNumber];

+

+ Status = GetNicName (ControllerHandle, NicNumber, NicName);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NAME),

+ mHttpHiiHandle, NicNumber, Status

+ );

+ continue;

+ }

+

+ if (UserNicName != NULL) {

+ if (StrCmp (NicName, UserNicName) != 0) {

+ Status = EFI_NOT_FOUND;

+ continue;

+ }

+ NicFound = TRUE;

+ }

+

+ Status = CreateServiceChildAndOpenProtocol (

+ ControllerHandle,

+ &gEfiHttpServiceBindingProtocolGuid,

+ &gEfiHttpProtocolGuid,

+ &HttpChildHandle,

+ (VOID**)&Context.Http

+ );

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_OPEN_PROTOCOL),

+ mHttpHiiHandle, NicName, Status

+ );

+ continue;

+ }

+

+ Status = Context.Http->Configure (Context.Http, &HttpConfigData);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_CONFIGURE),

+ mHttpHiiHandle, NicName, Status

+ );

+ continue;

+ }

+

+ Context.Flags = 0;

+ if (ShellCommandLineGetFlag (CheckPackage, L"-m")) {

+ Context.Flags |= DL_FLAG_TIME;

+ }

+

+ if (ShellCommandLineGetFlag (CheckPackage, L"-k")) {

+ Context.Flags |= DL_FLAG_KEEP_BAD;

+ }

+

+ Status = DownloadFile (&Context);

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle

+ );

+

+ CloseProtocolAndDestroyServiceChild (

+ ControllerHandle,

+ &gEfiHttpServiceBindingProtocolGuid,

+ &gEfiHttpProtocolGuid,

+ HttpChildHandle

+ );

+

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_DOWNLOAD),

+ mHttpHiiHandle, RemoteFilePath, NicName, Status

+ );

+ // If a user aborted the operation, do not try another controller.

+ if (Status == EFI_ABORTED) {

+ goto Error;

+ }

+ }

+

+ if (gHttpError) {

+ //

+ // This is not related to connection, so no need to repeat with

+ // another interface.

+ //

+ break;

+ }

+ }

+

+ if ((UserNicName != NULL) && (!NicFound)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NOT_FOUND),

+ mHttpHiiHandle, UserNicName

+ );

+ }

+

+Error:

+ ShellCommandLineFreeVarList (CheckPackage);

+ SHELL_FREE_NON_NULL (Handles);

+ SHELL_FREE_NON_NULL (Context.ServerAddrAndProto);

+ SHELL_FREE_NON_NULL (Context.URI);

+

+ return Status & ~MAX_BIT;

+}

+

+/**

+ Check and convert the UINT16 option values of the 'http' command

+

+ @param[in] ValueStr Value as an Unicode encoded string

+ @param[out] Value UINT16 value

+

+ @return TRUE The value was returned.

+ @return FALSE A parsing error occured.

+**/

+STATIC

+BOOLEAN

+StringToUint16 (

+ IN CONST CHAR16 *ValueStr,

+ OUT UINT16 *Value

+ )

+{

+ UINTN Val;

+

+ Val = ShellStrToUintn (ValueStr);

+ if (Val > MAX_UINT16) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),

+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr

+ );

+ return FALSE;

+ }

+

+ *Value = (UINT16)Val;

+ return TRUE;

+}

+

+/**

+ Get the name of the NIC.

+

+ @param[in] ControllerHandle The network physical device handle.

+ @param[in] NicNumber The network physical device number.

+ @param[out] NicName Address where to store the NIC name.

+ The memory area has to be at least

+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH

+ double byte wide.

+

+ @return EFI_SUCCESS The name of the NIC was returned.

+ @return Others The creation of the child for the Managed

+ Network Service failed or the opening of

+ the Managed Network Protocol failed or

+ the operational parameters for the

+ Managed Network Protocol could not be

+ read.

+**/

+STATIC

+EFI_STATUS

+GetNicName (

+ IN EFI_HANDLE ControllerHandle,

+ IN UINTN NicNumber,

+ OUT CHAR16 *NicName

+ )

+{

+ EFI_STATUS Status;

+ EFI_HANDLE MnpHandle;

+ EFI_MANAGED_NETWORK_PROTOCOL *Mnp;

+ EFI_SIMPLE_NETWORK_MODE SnpMode;

+

+ Status = CreateServiceChildAndOpenProtocol (

+ ControllerHandle,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ &gEfiManagedNetworkProtocolGuid,

+ &MnpHandle,

+ (VOID**)&Mnp

+ );

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);

+ if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {

+ goto Error;

+ }

+

+ UnicodeSPrint (

+ NicName,

+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,

+ SnpMode.IfType == NET_IFTYPE_ETHERNET ?

+ L"eth%d" :

+ L"unk%d",

+ NicNumber

+ );

+

+ Status = EFI_SUCCESS;

+

+Error:

+

+ if (MnpHandle != NULL) {

+ CloseProtocolAndDestroyServiceChild (

+ ControllerHandle,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ &gEfiManagedNetworkProtocolGuid,

+ MnpHandle

+ );

+ }

+

+ return Status;

+}

+

+/**

+ Create a child for the service identified by its service binding protocol GUID

+ and get from the child the interface of the protocol identified by its GUID.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the

+ service to be created.

+ @param[in] ProtocolGuid GUID of the protocol to be open.

+ @param[out] ChildHandle Address where the handler of the

+ created child is returned. NULL is

+ returned in case of error.

+ @param[out] Interface Address where a pointer to the

+ protocol interface is returned in

+ case of success.

+

+ @return EFI_SUCCESS The child was created and the protocol opened.

+ @return Others Either the creation of the child or the opening

+ of the protocol failed.

+**/

+STATIC

+EFI_STATUS

+CreateServiceChildAndOpenProtocol (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ OUT EFI_HANDLE *ChildHandle,

+ OUT VOID **Interface

+ )

+{

+ EFI_STATUS Status;

+

+ *ChildHandle = NULL;

+ Status = NetLibCreateServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ ChildHandle

+ );

+ if (!EFI_ERROR (Status)) {

+ Status = gBS->OpenProtocol (

+ *ChildHandle,

+ ProtocolGuid,

+ Interface,

+ gImageHandle,

+ ControllerHandle,

+ EFI_OPEN_PROTOCOL_GET_PROTOCOL

+ );

+ if (EFI_ERROR (Status)) {

+ NetLibDestroyServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ *ChildHandle

+ );

+ *ChildHandle = NULL;

+ }

+ }

+

+ return Status;

+}

+

+/**

+ Close the protocol identified by its GUID on the child handle of the service

+ identified by its service binding protocol GUID, then destroy the child

+ handle.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the

+ service to be destroyed.

+ @param[in] ProtocolGuid GUID of the protocol to be closed.

+ @param[in] ChildHandle Handle of the child to be destroyed.

+

+**/

+STATIC

+VOID

+CloseProtocolAndDestroyServiceChild (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ IN EFI_HANDLE ChildHandle

+ )

+{

+ gBS->CloseProtocol (

+ ChildHandle,

+ ProtocolGuid,

+ gImageHandle,

+ ControllerHandle

+ );

+

+ NetLibDestroyServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ ChildHandle

+ );

+}

+

+/**

+ Wait until operation completes. Completion is indicated by

+ setting of an appropriate variable.

+

+ @param[in] Context A pointer to the HTTP download context.

+ @param[in] CallBackComplete A pointer to the callback completion

+ variable set by the callback.

+

+ @return EFI_SUCCESS Callback signalled completion.

+ @return EFI_TIMEOUT Timed out waiting for completion.

+ @return Others Error waiting for completion.

+**/

+STATIC

+EFI_STATUS

+WaitForCompletion (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN OUT BOOLEAN *CallBackComplete

+ )

+{

+ EFI_STATUS Status;

+ EFI_EVENT WaitEvt;

+

+ Status = EFI_SUCCESS;

+

+ // Use a timer to measure timeout. Cannot use Stall here!

+ Status = gBS->CreateEvent (

+ EVT_TIMER,

+ TPL_CALLBACK,

+ NULL,

+ NULL,

+ &WaitEvt

+ );

+ ASSERT_EFI_ERROR (Status);

+

+ if (!EFI_ERROR (Status)) {

+ Status = gBS->SetTimer(

+ WaitEvt,

+ TimerRelative,

+ EFI_TIMER_PERIOD_SECONDS (TIMER_MAX_TIMEOUT_S)

+ );

+

+ ASSERT_EFI_ERROR (Status);

+ }

+

+ while (! *CallBackComplete &&

+ (!EFI_ERROR (Status)) &&

+ EFI_ERROR (gBS->CheckEvent (WaitEvt))

+ ) {

+ Status = Context->Http->Poll (Context->Http);

+ }

+

+ gBS->SetTimer (WaitEvt, TimerCancel, 0);

+ gBS->CloseEvent (WaitEvt);

+

+ if (*CallBackComplete) {

+ return EFI_SUCCESS;

+ }

+

+ if (!EFI_ERROR (Status)) {

+ Status = EFI_TIMEOUT;

+ }

+

+ return Status;

+}

+

+/**

+ Generate and send a request to the http server.

+

+ @param[in] Context HTTP download context.

+ @param[in] DownloadUrl Fully qualified URL to be downloaded.

+

+ @return EFI_SUCCESS Request has been sent successfully.

+ @return EFI_INVALID_PARAMETER Invalid URL.

+ @return EFI_OUT_OF_RESOURCES Out of memory.

+ @return EFI_DEVICE_ERROR If HTTPS is used, this probably

+ means that TLS support either was not

+ installed or not configured.

+ @return Others Error sending the request.

+**/

+

+STATIC

+EFI_STATUS

+SendRequest (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_HTTP_REQUEST_DATA RequestData;

+ EFI_HTTP_HEADER RequestHeader;

+ EFI_HTTP_MESSAGE RequestMessage;

+ EFI_STATUS Status;

+ CHAR16 *Host;

+ UINTN StringSize;

+

+ ZeroMem (&RequestData, sizeof (RequestData));

+ ZeroMem (&RequestHeader, sizeof (RequestHeader));

+ ZeroMem (&RequestMessage, sizeof (RequestMessage));

+ ZeroMem (&Context->RequestToken, sizeof (Context->RequestToken));

+

+ RequestHeader.FieldName = "Host";

+

+ Host = (CHAR16 *)Context->ServerAddrAndProto;

+ while (*Host != CHAR_NULL && *Host != L'/') {

+ Host++;

+ }

+

+ if (*Host == CHAR_NULL) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ //

+ // Get the next slash

+ //

+ Host++;

+ //

+ // And now the host name

+ //

+ Host++;

+

+ StringSize = StrLen (Host) + 1;

+ RequestHeader.FieldValue = AllocatePool (StringSize);

+ if (!RequestHeader.FieldValue) {

+ return EFI_OUT_OF_RESOURCES;

+ }

+

+ UnicodeStrToAsciiStrS (Host, RequestHeader.FieldValue, StringSize);

+

+ RequestMessage.HeaderCount++;

+

+ RequestData.Method = HttpMethodGet;

+ RequestData.Url = DownloadUrl;

+

+ RequestMessage.Data.Request = &RequestData;

+ RequestMessage.Headers = &RequestHeader;

+ RequestMessage.BodyLength = 0;

+ RequestMessage.Body = NULL;

+ Context->RequestToken.Event = NULL;

+

+ //

+ // Completion callback event to be set when Request completes.

+ //

+ Status = gBS->CreateEvent (

+ EVT_NOTIFY_SIGNAL,

+ TPL_CALLBACK,

+ RequestCallback,

+ Context,

+ &Context->RequestToken.Event

+ );

+ ASSERT_EFI_ERROR (Status);

+

+ Context->RequestToken.Status = EFI_SUCCESS;

+ Context->RequestToken.Message = &RequestMessage;

+ gRequestCallbackComplete = FALSE;

+ Status = Context->Http->Request (Context->Http, &Context->RequestToken);

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ Status = WaitForCompletion (Context, &gRequestCallbackComplete);

+ if (EFI_ERROR (Status)) {

+ Context->Http->Cancel (Context->Http, &Context->RequestToken);

+ }

+Error:

+ SHELL_FREE_NON_NULL (RequestHeader.FieldValue);

+ if (Context->RequestToken.Event) {

+ gBS->CloseEvent (Context->RequestToken.Event);

+ ZeroMem (&Context->RequestToken, sizeof (Context->RequestToken));

+ }

+

+ return Status;

+}

+

+/**

+ Update the progress of a file download

+ This procedure is called each time a new HTTP body portion is received.

+

+ @param[in] Context HTTP download context.

+ @param[in] DownloadLen Portion size, in bytes.

+ @param[in] Buffer The pointer to the parsed buffer.

+

+ @retval EFI_SUCCESS Portion saved.

+ @retval Other Error saving the portion.

+

+**/

+STATIC

+EFI_STATUS

+EFIAPI

+SavePortion (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN UINTN DownloadLen,

+ IN CHAR8 *Buffer

+ )

+{

+ CHAR16 Progress[HTTP_PROGRESS_MESSAGE_SIZE];

+ UINTN NbOfKb;

+ UINTN Index;

+ UINTN LastStep;

+ UINTN Step;

+ EFI_STATUS Status;

+

+ LastStep = 0;

+ Step = 0;

+ ShellSetFilePosition (mFileHandle, Context->LastReportedNbOfBytes);

+ Status = ShellWriteFile (mFileHandle, &DownloadLen, Buffer);

+ if (EFI_ERROR (Status)) {

+ if (Context->ContentDownloaded > 0) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle

+ );

+ }

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_WRITE),

+ mHttpHiiHandle, mLocalFilePath, Status

+ );

+

+ return Status;

+ }

+

+ if (Context->ContentDownloaded == 0) {

+ ShellPrintEx (-1, -1, L"%s 0 Kb", HTTP_PROGR_FRAME);

+ }

+

+ Context->ContentDownloaded += DownloadLen;

+ NbOfKb = Context->ContentDownloaded >> 10;

+

+ Progress[0] = L'\0';

+ if (Context->ContentLength) {

+ LastStep = (Context->LastReportedNbOfBytes *
HTTP_PROGRESS_SLIDER_STEPS) /

+ Context->ContentLength;

+ Step = (Context->ContentDownloaded * HTTP_PROGRESS_SLIDER_STEPS) /

+ Context->ContentLength;

+ }

+

+ Context->LastReportedNbOfBytes = Context->ContentDownloaded;

+

+ if (Step <= LastStep) {

+ if (!Context->ContentLength) {

+ //

+ // Update downloaded size, there is no length info available.

+ //

+ ShellPrintEx (-1, -1, L"%s", HTTP_KB);

+ ShellPrintEx (-1, -1, L"%7d Kb", NbOfKb);

+ }

+

+ return EFI_SUCCESS;

+ }

+

+ ShellPrintEx (-1, -1, L"%s", HTTP_PROGRESS_DEL);

+

+ Status = StrCpyS (Progress, HTTP_PROGRESS_MESSAGE_SIZE,
HTTP_PROGR_FRAME);

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ for (Index = 1; Index < Step; Index++) {

+ Progress[Index] = L'=';

+ }

+

+ if (Step) {

+ Progress[Step] = L'>';

+ }

+

+ UnicodeSPrint (

+ Progress + (sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 1,

+ sizeof (Progress) - sizeof (HTTP_PROGR_FRAME),

+ L" %7d Kb",

+ NbOfKb

+ );

+

+

+ ShellPrintEx (-1, -1, L"%s", Progress);

+

+ return EFI_SUCCESS;

+}

+

+/**

+ Replace the original Host and URI with Host and URI returned by the

+ HTTP server in 'Location' header (redirection).

+

+ @param[in] Location A pointer to the 'Location' string

+ provided by HTTP server.

+ @param[in] Context A pointer to HTTP download context.

+ @param[in] DownloadUrl Fully qualified HTTP URL.

+

+ @return EFI_SUCCESS Host and URI were successfully set.

+ @return EFI_OUT_OF_RESOURCES Error setting Host or URI.

+**/

+

+STATIC

+EFI_STATUS

+SetHostURI (

+ IN CHAR8 *Location,

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_STATUS Status;

+ UINTN StringSize;

+ UINTN FirstStep;

+ UINTN Idx;

+ UINTN Step;

+ CHAR8 *Walker;

+ CHAR16 *Temp;

+ CHAR8 *Tmp;

+ CHAR16 *Url;

+ BOOLEAN IsAbEmptyUrl;

+

+ Tmp = NULL;

+ Url = NULL;

+ IsAbEmptyUrl = FALSE;

+ FirstStep = 0;

+

+ StringSize = (AsciiStrSize (Location) * sizeof (CHAR16));

+ Url = AllocateZeroPool (StringSize);

+ if (!Url) {

+ return EFI_OUT_OF_RESOURCES;

+ }

+

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Location,

+ Url,

+ StringSize

+ );

+

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ //

+ // If an HTTP server redirects to the same location more than once,

+ // then stop attempts and tell it is not reachable.

+ //

+ if (!StrCmp (Url, DownloadUrl)) {

+ Status = EFI_NO_MAPPING;

+ goto Error;

+ }

+

+ if (AsciiStrLen (Location) > 2) {

+ // Some servers return 'Location: //server/resource'

+ IsAbEmptyUrl = (Location[0] == '/') && (Location[1] == '/');

+ if (IsAbEmptyUrl) {

+ // Skip first "//"

+ Location += 2;

+ FirstStep = 1;

+ }

+ }

+

+ if (AsciiStrStr (Location, "://") || IsAbEmptyUrl) {

+ Idx = 0;

+ Walker = Location;

+

+ for (Step = FirstStep; Step < 2; Step++) {

+ for (; *Walker != '/' && *Walker != '\0'; Walker++) {

+ Idx++;

+ }

+ if (!Step) {

+ //

+ // Skip "//"

+ //

+ Idx += 2;

+ Walker += 2;

+ }

+ }

+

+ Tmp = AllocateZeroPool (Idx + 1);

+ if (!Tmp) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ CopyMem (Tmp, Location, Idx);

+

+ //

+ // Location now points to URI

+ //

+ Location += Idx;

+ StringSize = (Idx + 1) * sizeof (CHAR16);

+

+ SHELL_FREE_NON_NULL (Context->ServerAddrAndProto);

+

+ Temp = AllocateZeroPool (StringSize);

+ if (!Temp) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Tmp,

+ Temp,

+ StringSize

+ );

+ if (EFI_ERROR (Status)) {

+ SHELL_FREE_NON_NULL (Temp);

+ goto Error;

+ }

+

+ Idx = 0;

+ if (IsAbEmptyUrl) {

+ Context->ServerAddrAndProto = StrnCatGrow (

+ &Context->ServerAddrAndProto,

+ &Idx,

+ L"http://",

+ StrLen (L"http://")

+ );

+ }

+

+ Context->ServerAddrAndProto = StrnCatGrow (

+ &Context->ServerAddrAndProto,

+ &Idx,

+ Temp,

+ StrLen (Temp)

+ );

+ SHELL_FREE_NON_NULL (Temp);

+ if (!Context->ServerAddrAndProto) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+ }

+

+ SHELL_FREE_NON_NULL (Context->URI);

+

+ StringSize = AsciiStrSize (Location) * sizeof (CHAR16);

+ Context->URI = AllocateZeroPool (StringSize);

+ if (!Context->URI) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ //

+ // Now make changes to the URI part.

+ //

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Location,

+ Context->URI,

+ StringSize

+ );

+Error:

+ SHELL_FREE_NON_NULL (Tmp);

+ SHELL_FREE_NON_NULL (Url);

+ return Status;

+}

+

+/**

+ Message parser callback.

+ Save a portion of HTTP body.

+

+ @param[in] EventType Type of event. Can be either

+ OnComplete or OnData.

+ @param[in] Data A pointer to the buffer with data.

+ @param[in] Length Data length of this portion.

+ @param[in] Context A pointer to the HTTP download context.

+

+ @return EFI_SUCCESS The portion was processed successfully.

+ @return Other Error returned by SavePortion.

+**/

+

+STATIC

+EFI_STATUS

+EFIAPI

+ParseMsg (

+ IN HTTP_BODY_PARSE_EVENT EventType,

+ IN CHAR8 *Data,

+ IN UINTN Length,

+ IN VOID *Context

+ )

+{

+ if (!Data || (EventType == BodyParseEventOnComplete) || !Context) {

+ return EFI_SUCCESS;

+ }

+

+ return SavePortion (Context, Length, Data);

+}

+

+

+/**

+ Get HTTP server response and collect the whole body as a file.

+ Set appropriate status in Context (REQ_OK, REQ_REPEAT, REQ_ERROR).

+ Note that even if HTTP server returns an error code, it might send

+ the body as well. This body will be collected in the resultant file.

+

+ @param[in] Context A pointer to the HTTP download context.

+ @param[in] DownloadedUrl A pointer to the fully qualified URL to download.

+

+ @return EFI_SUCCESS Valid file. Body successfully collected.

+ @return EFI_HTTP_ERROR Response is a valid HTTP response, but the

+ HTTP server

+ indicated an error (HTTP code >= 400).

+ Response body MAY contain full

+ HTTP server response.

+ @return Others Error getting the reponse from the HTTP server.

+ Response body is not collected.

+**/

+STATIC

+EFI_STATUS

+GetResponse (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_HTTP_RESPONSE_DATA ResponseData;

+ EFI_HTTP_MESSAGE ResponseMessage;

+ EFI_HTTP_HEADER *Header;

+ EFI_STATUS Status;

+ VOID *MsgParser;

+ CONST CHAR16 *Desc;

+ BOOLEAN IsTrunked;

+ BOOLEAN Redirection;

+

+ ZeroMem (&ResponseData, sizeof (ResponseData));

+ ZeroMem (&ResponseMessage, sizeof (ResponseMessage));

+ ZeroMem (&Context->ResponseToken, sizeof (Context->ResponseToken));

+ IsTrunked = FALSE;

+

+ ResponseMessage.Body = Context->Buffer;

+ Context->ResponseToken.Status = EFI_SUCCESS;

+ Context->ResponseToken.Message = &ResponseMessage;

+ Context->ContentLength = 0;

+ Context->Status = REQ_OK;

+ MsgParser = NULL;

+ ResponseData.StatusCode = HTTP_STATUS_UNSUPPORTED_STATUS;

+ ResponseMessage.Data.Response = &ResponseData;

+ Context->ResponseToken.Event = NULL;

+

+ do {

+ SHELL_FREE_NON_NULL (ResponseMessage.Headers);

+ ResponseMessage.HeaderCount = 0;

+ gResponseCallbackComplete = FALSE;

+ ResponseMessage.BodyLength = Context->BufferSize;

+

+ if (ShellGetExecutionBreakFlag ()) {

+ Status = EFI_ABORTED;

+ break;

+ }

+

+ if (!Context->ContentDownloaded && !Context->ResponseToken.Event) {

+ Status = gBS->CreateEvent (

+ EVT_NOTIFY_SIGNAL,

+ TPL_CALLBACK,

+ ResponseCallback,

+ Context,

+ &Context->ResponseToken.Event

+ );

+ ASSERT_EFI_ERROR (Status);

+ } else {

+ ResponseMessage.Data.Response = NULL;

+ }

+

+ if (EFI_ERROR (Status)) {

+ break;

+ }

+

+ Status = Context->Http->Response (Context->Http, &Context-
ResponseToken);
+ if (EFI_ERROR (Status)) {

+ break;

+ }

+

+ Status = WaitForCompletion (Context, &gResponseCallbackComplete);

+ if (EFI_ERROR (Status) && ResponseMessage.HeaderCount) {

+ Status = EFI_SUCCESS;

+ }

+

+ if (EFI_ERROR (Status)) {

+ Context->Http->Cancel (Context->Http, &Context->ResponseToken);

+ break;

+ }

+

+ if (!Context->ContentDownloaded) {

+ Redirection =

+ ((ResponseData.StatusCode >= HTTP_STATUS_300_MULTIPLE_CHOICES)
&&

+ (ResponseData.StatusCode <=
HTTP_STATUS_307_TEMPORARY_REDIRECT)

+ ) ||

+ (ResponseData.StatusCode ==
HTTP_STATUS_308_PERMANENT_REDIRECT);

+

+ if (Redirection) {

+ //

+ // Need to repeat the request with new Location (server redirected).

+ //

+ Context->Status = REQ_NEED_REPEAT;

+

+ Header = HttpFindHeader (

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ "Location"

+ );

+ if (Header) {

+ Status = SetHostURI (Header->FieldValue, Context, DownloadUrl);

+ if (Status == EFI_NO_MAPPING) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),

+ mHttpHiiHandle, Context->ServerAddrAndProto,

+ L"Recursive HTTP server relocation",

+ Context->URI

+ );

+ }

+ } else {

+ //

+ // Bad reply from the server. Server must specify the location.

+ // Indicate that resource was not found, and no body collected.

+ //

+ Status = EFI_NOT_FOUND;

+ }

+

+ Context->Http->Cancel (Context->Http, &Context->ResponseToken);

+ break;

+ }

+

+ //

+ // Init message-body parser by header information.

+ //

+ if (!MsgParser) {

+ Status = HttpInitMsgParser (

+ ResponseMessage.Data.Request->Method,

+ ResponseData.StatusCode,

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ ParseMsg,

+ Context,

+ &MsgParser

+ );

+ if (EFI_ERROR (Status)) {

+ break;

+ }

+ }

+

+ //

+ // If it is a trunked message, rely on the parser.

+ //

+ Header = HttpFindHeader (

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ "Transfer-Encoding"

+ );

+ IsTrunked = (Header && !AsciiStrCmp (Header->FieldValue, "chunked"));

+

+ HttpGetEntityLength (MsgParser, &Context->ContentLength);

+

+ if (ResponseData.StatusCode >= HTTP_STATUS_400_BAD_REQUEST &&

+ (ResponseData.StatusCode !=
HTTP_STATUS_308_PERMANENT_REDIRECT)

+ ) {

+ //

+ // Server reported an error via Response code.

+ // Collect the body if any.

+ //

+ if (!gHttpError) {

+ gHttpError = TRUE;

+

+ Desc = ErrStatusDesc[ResponseData.StatusCode -

+ HTTP_STATUS_400_BAD_REQUEST];

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),

+ mHttpHiiHandle, Context->ServerAddrAndProto,

+ Desc,

+ Context->URI

+ );

+

+ //

+ // This gives an RFC HTTP error.

+ //

+ Context->Status = ShellStrToUintn (Desc);

+ }

+ }

+ }

+

+ // Do NOT try to parse an empty body.

+ if (ResponseMessage.BodyLength || IsTrunked) {

+ Status = HttpParseMessageBody (

+ MsgParser,

+ ResponseMessage.BodyLength,

+ ResponseMessage.Body

+ );

+ }

+ } while (!HttpIsMessageComplete (MsgParser) &&

+ !EFI_ERROR (Status) &&

+ ResponseMessage.BodyLength

+ );

+

+ SHELL_FREE_NON_NULL (MsgParser);

+ if (Context->ResponseToken.Event) {

+ gBS->CloseEvent (Context->ResponseToken.Event);

+ ZeroMem (&Context->ResponseToken, sizeof (Context->ResponseToken));

+ }

+

+ return Status;

+}

+

+/**

+ Worker function that downloads the data of a file from an HTTP server given

+ the path of the file and its size.

+

+ @param[in] Context A pointer to the HTTP download context.

+

+ @retval EFI_SUCCESS The file was downloaded.

+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.

+ #retval EFI_HTTP_ERROR The server returned a valid HTTP error.

+ Examine the mLocalFilePath file

+ to get error body.

+ @retval Others The downloading of the file from the server

+ failed.

+

+**/

+STATIC

+EFI_STATUS

+DownloadFile (

+ IN DOWNLOAD_CONTEXT *Context

+ )

+{

+ EFI_STATUS Status;

+ CHAR16 *DownloadUrl;

+ UINTN UrlSize;

+

+ ASSERT (Context);

+ if (!Context) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ DownloadUrl = NULL;

+

+ Context->Buffer = AllocatePool (Context->BufferSize);

+ if (!Context->Buffer) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto ON_EXIT;

+ }

+

+ //

+ // OPEN FILE

+ //

+ if (!EFI_ERROR (ShellFileExists (mLocalFilePath))) {

+ ShellDeleteFileByName (mLocalFilePath);

+ }

+

+ Status = ShellOpenFileByName (

+ mLocalFilePath,

+ &mFileHandle,

+ EFI_FILE_MODE_CREATE |

+ EFI_FILE_MODE_WRITE |

+ EFI_FILE_MODE_READ,

+ 0);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),

+ mHttpHiiHandle, HTTP_APP_NAME, mLocalFilePath

+ );

+ goto ON_EXIT;

+ }

+

+ do {

+ SHELL_FREE_NON_NULL (DownloadUrl);

+ UrlSize = 0;

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ Context->ServerAddrAndProto,

+ StrLen (Context->ServerAddrAndProto)

+ );

+ if (Context->URI[0] != L'/') {

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ L"/",

+ StrLen (Context->ServerAddrAndProto)

+ );

+ }

+

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ Context->URI,

+ StrLen (Context->URI));

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_DOWNLOADING),

+ mHttpHiiHandle, DownloadUrl);

+

+ Status = SendRequest (Context, DownloadUrl);

+ if (Status) {

+ goto ON_EXIT;

+ }

+

+ Status = GetResponse (Context, DownloadUrl);

+

+ if (Status) {

+ goto ON_EXIT;

+ }

+

+ } while (Context->Status == REQ_NEED_REPEAT);

+

+ if (Context->Status) {

+ Status = ENCODE_ERROR (Context->Status);

+ }

+

+ON_EXIT:

+ //

+ // CLOSE FILE

+ //

+ if (mFileHandle) {

+ if (EFI_ERROR (Status) && !(Context->Flags & DL_FLAG_KEEP_BAD)) {

+ ShellDeleteFile (&mFileHandle);

+ } else {

+ ShellCloseFile (&mFileHandle);

+ }

+ }

+

+ SHELL_FREE_NON_NULL (DownloadUrl);

+ SHELL_FREE_NON_NULL (Context->Buffer);

+

+ return Status;

+}

+

+/**

+ Retrive HII package list from ImageHandle and publish to HII database.

+

+ @param ImageHandle The image handle of the process.

+

+ @return HII handle.

+**/

+EFI_HII_HANDLE

+InitializeHiiPackage (

+ EFI_HANDLE ImageHandle

+ )

+{

+ EFI_STATUS Status;

+ EFI_HII_PACKAGE_LIST_HEADER *PackageList;

+ EFI_HII_HANDLE HiiHandle;

+

+ //

+ // Retrieve HII package list from ImageHandle

+ //

+ Status = gBS->OpenProtocol (

+ ImageHandle,

+ &gEfiHiiPackageListProtocolGuid,

+ (VOID **)&PackageList,

+ ImageHandle,

+ NULL,

+ EFI_OPEN_PROTOCOL_GET_PROTOCOL

+ );

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return NULL;

+ }

+

+ //

+ // Publish HII package list to HII Database.

+ //

+ Status = gHiiDatabase->NewPackageList (

+ gHiiDatabase,

+ PackageList,

+ NULL,

+ &HiiHandle

+ );

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return NULL;

+ }

+

+ return HiiHandle;

+}

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
new file mode 100644
index 000000000000..5c7b90e8cab4
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
@@ -0,0 +1,89 @@
+/** @file

+ Header file for 'http' command functions.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved. <BR>

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#ifndef _HTTP_H_

+#define _HTTP_H_

+

+#include <Uefi.h>

+

+#include <Library/BaseLib.h>

+#include <Library/BaseMemoryLib.h>

+#include <Library/DebugLib.h>

+#include <Library/HiiLib.h>

+#include <Library/HttpLib.h>

+#include <Library/MemoryAllocationLib.h>

+#include <Library/NetLib.h>

+#include <Library/PrintLib.h>

+#include <Library/ShellLib.h>

+#include <Library/UefiBootServicesTableLib.h>

+#include <Library/UefiHiiServicesLib.h>

+#include <Library/UefiLib.h>

+#include <Library/UefiRuntimeServicesTableLib.h>

+

+#include <Protocol/HiiPackageList.h>

+#include <Protocol/HttpUtilities.h>

+#include <Protocol/ServiceBinding.h>

+

+#define HTTP_APP_NAME L"http"

+

+#define REQ_OK 0

+#define REQ_NEED_REPEAT 1

+

+// Download Flags

+#define DL_FLAG_TIME BIT0 // Show elapsed time.

+#define DL_FLAG_KEEP_BAD BIT1 // Keep files even if download failed.

+

+extern EFI_HII_HANDLE mHttpHiiHandle;

+

+typedef struct {

+ UINTN ContentDownloaded;

+ UINTN ContentLength;

+ UINTN LastReportedNbOfBytes;

+ UINTN BufferSize;

+ UINTN Status;

+ UINTN Flags;

+ UINT8 *Buffer;

+ CHAR16 *ServerAddrAndProto;

+ CHAR16 *URI;

+ EFI_HTTP_TOKEN ResponseToken;

+ EFI_HTTP_TOKEN RequestToken;

+ EFI_HTTP_PROTOCOL *Http;

+} DOWNLOAD_CONTEXT;

+

+/**

+ Function for 'http' command.

+

+ @param[in] ImageHandle The image handle.

+ @param[in] SystemTable The system table.

+

+ @retval SHELL_SUCCESS Command completed successfully.

+ @retval SHELL_INVALID_PARAMETER Command usage error.

+ @retval SHELL_ABORTED The user aborts the operation.

+ @retval value Unknown error.

+**/

+SHELL_STATUS

+RunHttp (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ );

+

+/**

+ Retrive HII package list from ImageHandle and publish to HII database.

+

+ @param ImageHandle The image handle of the process.

+

+ @return HII handle.

+**/

+EFI_HII_HANDLE

+InitializeHiiPackage (

+ EFI_HANDLE ImageHandle

+ );

+#endif // _HTTP_H_

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
new file mode 100644
index 000000000000..00cf05deeb5c
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
@@ -0,0 +1,117 @@
+// /**

+//

+// (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>

+// Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved. <BR>

+// Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+// SPDX-License-Identifier: BSD-2-Clause-Patent

+//

+// Module Name:

+//

+// Http.uni

+//

+// Abstract:

+//

+// String definitions for UEFI Shell HTTP command

+//

+//

+// **/

+

+/=#

+

+#langdef en-US "english"

+

+#string STR_GEN_TOO_MANY #language en-US "%H%s%N: Too many
arguments. Try help http.\r\n"

+#string STR_GEN_TOO_FEW #language en-US "%H%s%N: Too few
arguments. Try help http.\r\n"

+#string STR_GEN_PARAM_INV #language en-US "%H%s%N: Invalid
argument - '%H%s%N'. Try help http.\r\n"

+#string STR_GEN_PROBLEM #language en-US "%H%s%N: Unknown flag -
'%H%s%N'. Try help http.\r\n"

+#string STR_GEN_FILE_OPEN_FAIL #language en-US "%H%s%N: Cannot open
file - '%H%s%N'\r\n"

+#string STR_GEN_CRLF #language en-US "\r\n"

+

+#string STR_HTTP_ERR_NO_NIC #language en-US "No network interface
card found.\r\n"

+#string STR_HTTP_ERR_NIC_NAME #language en-US "Failed to get the name
of the network interface card number %d - %r\r\n"

+#string STR_HTTP_ERR_OPEN_PROTOCOL #language en-US "Unable to open
HTTP protocol on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_CONFIGURE #language en-US "Unable to configure
HTTP protocol on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_DOWNLOAD #language en-US "Unable to download
the file '%H%s%N' on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_WRITE #language en-US "Unable to write into file
'%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_NIC_NOT_FOUND #language en-US "Network Interface
Card '%H%s%N' not found.\r\n"

+#string STR_HTTP_ERR_STATUSCODE #language en-US "\r'%H%s%N' reports
'%s' for '%H%s%N' \r\n"

+#string STR_HTTP_DOWNLOADING #language en-US "Downloading
'%H%s%N'\r\n"

+

+#string STR_GET_HELP_HTTP #language en-US ""

+".TH http 0 "Download a file from HTTP server."\r\n"

+".SH NAME\r\n"

+"Download a file from HTTP server.\r\n"

+".SH SYNOPSIS\r\n"

+" \r\n"

+"HTTP [-i interface] [-l port] [-t timeout] [-s size] [-m] [-k]\r\n"

+" <URL> [localfilepath]\r\n"

+".SH OPTIONS\r\n"

+" \r\n"

+" -i interface - Specifies an adapter name, i.e., eth0.\r\n"

+" -k Keep the downloaded file even if there was an error.\r\n"

+" If this parameter is not used, the file will be deleted.\r\n"

+" -l port - Specifies the local port number. Default value is 0\r\n"

+" and the port number is automatically assigned.\r\n"

+" -m Measure and report download time (in seconds). \r\n"

+" -s size The size of the download buffer for a chunk, in bytes.\r\n"

+" Default is 32K. Note that larger buffer does not imply\r\n"

+" better speed.\r\n"

+" -t timeout - The number of seconds to wait for completion of\r\n"

+" requests and responses. Default is 0 which is 'automatic'.\r\n"

+" %HURL%N\r\n"

+" Two types of providing of URLs are supported:\r\n"

+" 1. tftp-like, where host and http_uri are separate parameters\r\n"

+" (example: host /host_uri), and\r\n\"

+" 2. wget-like, where host and host_uri is one parameter.\r\n"

+" (example: host/host_uri)\r\n"

+"\r\n"

+" host - Specifies HTTP Server address.\r\n

+ Can be either IPv4 address or 'http (or https)://addr'\r\n

+ Can use addresses resolvable by DNS as well. \r\n

+ Port can be specified after ':' if needed. \r\n

+ By default port 80 is used.\r\n"

+" http_uri - HTTP server URI to download the file.\r\n"

+"\r\n"

+" localfilepath - Local destination file path.\r\n"

+".SH DESCRIPTION\r\n"

+" \r\n"

+"NOTES:\r\n"

+" 1. The HTTP command allows geting of the file specified by its 'http_uri'\r\n"

+" path from the HTTP server specified by its 'host' IPv4 address. If the\r\n"

+" optional 'localfilepath' parameter is provided, the downloaded file is\r\n"

+" stored locally using the provided file path. If the local file path is\r\n"

+" not specified, the file is stored in the current directory using the file\r\n"

+" server's name.\r\n"

+" 2. Before using the HTTP command, the network interface intended to
be\r\n"

+" used to retrieve the file must be configured. This configuration may be\r\n"

+" done by means of the 'ifconfig' command.\r\n"

+" 3. If a network interface is defined with the '-i' option then only this\r\n"

+" interface will be used to retrieve the remote file. Otherwise, all
network\r\n"

+" interfaces are tried in the order they have been discovered during the\r\n"

+" DXE phase.\r\n"

+".SH EXAMPLES\r\n"

+" \r\n"

+"EXAMPLES:\r\n"

+" * To get the file "dir1/file1.dat" from the HTTP server 192.168.1.1, port 8080,
and\r\n"

+" store it as file2.dat in the current directory (use tftp-like URL format) :\r\n"

+" fs0:\> http 192.168.1.1:8080 dir1/file1.dat file2.dat\r\n"

+" * To get the file /image.bin via HTTPS from server 192.168.1.1 at port 443
\r\n"

+" (default HTTPS port), and store it in the current directory: \r\n"

+" fs0:\> http https://192.168.1.1 image.bin\r\n"

+" To get an index file from http://google.com and place it into the \r\n"

+" current directory:\r\n"

+" fs0:\> http google.com index.html\r\n"

+".SH RETURNVALUES\r\n"

+" \r\n"

+"RETURN VALUES:\r\n"

+" SHELL_SUCCESS The action was completed as requested.\r\n"

+" SHELL_INVALID_PARAMETER One of the passed-in parameters was
incorrectly\r\n"

+" formatted or its value was out of bounds.\r\n"

+" HTTP_ERROR No EFI errors, but the server reported a status
code\r\n"

+" which should be treated as an error. If an error body sent\r\n"

+" by the server, and -k parameter is on command line,

+" the file wil be saved either as localfilepath filename,\r\n"

+" or as an URI name in the current directory.\r\n"

+" If '/' is at the end of the URL, and no locafilepath filename\r\n"

+" is given on the command line, the file will be retrieved as\r\n"

+" index.html.\r\n"

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
new file mode 100644
index 000000000000..7bd5b46d3997
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
@@ -0,0 +1,53 @@
+/** @file

+ Entrypoint of "http" shell standalone application.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved. <BR>

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+#include "Http.h"

+

+/*

+ * String token ID of help message text.

+ * Shell supports to find help message in the resource section of an

+ * application image if * .MAN file is not found.

+ * This global variable is added to make build tool recognizes

+ * that the help string is consumed by user and then build tool will

+ * add the string into the resource section.

+ * Thus the application can use '-?' option to show help message in Shell.

+ */

+GLOBAL_REMOVE_IF_UNREFERENCED

+EFI_STRING_ID mStringHelpTokenId = STRING_TOKEN (STR_GET_HELP_HTTP);

+

+/**

+ Entry point of Http standalone application.

+

+ @param ImageHandle The image handle of the process.

+ @param SystemTable The EFI System Table pointer.

+

+ @retval EFI_SUCCESS Http command is executed sucessfully.

+ @retval EFI_ABORTED HII package was failed to initialize.

+ @retval others Other errors when executing http command.

+**/

+EFI_STATUS

+EFIAPI

+HttpAppInitialize (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+ mHttpHiiHandle = InitializeHiiPackage (ImageHandle);

+ if (mHttpHiiHandle == NULL) {

+ return EFI_ABORTED;

+ }

+

+ Status = (EFI_STATUS)RunHttp (ImageHandle, SystemTable);

+

+ HiiRemovePackages (mHttpHiiHandle);

+

+ return Status;

+}

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
new file mode 100644
index 000000000000..d08d47fb37d5
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
@@ -0,0 +1,58 @@
+## @file

+# Provides Shell 'http' standalone application.

+#

+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved. <BR>

+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010006

+ BASE_NAME = http

+ FILE_GUID = 56B00FB7-91D2-869B-CE5C-26CD1A89C73C

+ MODULE_TYPE = UEFI_APPLICATION

+ VERSION_STRING = 1.0

+ ENTRY_POINT = HttpAppInitialize

+#

+# This flag specifies whether HII resource section is generated into PE image.

+#

+ UEFI_HII_RESOURCE_SECTION = TRUE

+

+[Sources.common]

+ Http.c

+ HttpApp.c

+ Http.h

+ Http.uni

+

+[Packages]

+ EmbeddedPkg/EmbeddedPkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ MdePkg/MdePkg.dec

+ NetworkPkg/NetworkPkg.dec

+ ShellPkg/ShellPkg.dec

+

+[LibraryClasses]

+ BaseLib

+ BaseMemoryLib

+ DebugLib

+ FileHandleLib

+ HiiLib

+ HttpLib

+ MemoryAllocationLib

+ NetLib

+ ShellLib

+ UefiApplicationEntryPoint

+ UefiBootServicesTableLib

+ UefiHiiServicesLib

+ UefiLib

+ UefiRuntimeServicesTableLib

+

+[Protocols]

+ gEfiHiiPackageListProtocolGuid ## CONSUMES

+ gEfiHttpProtocolGuid ## CONSUMES

+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES

+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES

diff --git
a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c
new file mode 100644
index 000000000000..ba654749a075
--- /dev/null
+++
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.c
@@ -0,0 +1,134 @@
+/** @file

+ Produce "http" shell dynamic command.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved. <BR>

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+#include <Protocol/ShellDynamicCommand.h>

+#include "Http.h"

+

+/**

+ This is the shell command handler function pointer callback type. This

+ function handles the command when it is invoked in the shell.

+

+ @param[in] This The instance of the

+ EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.

+ @param[in] SystemTable The pointer to the system table.

+ @param[in] ShellParameters The parameters associated with the command.

+ @param[in] Shell The instance of the shell protocol used in

+ the context of processing this command.

+

+ @return EFI_SUCCESS the operation was sucessful

+ @return other the operation failed.

+**/

+SHELL_STATUS

+EFIAPI

+HttpCommandHandler (

+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,

+ IN EFI_SYSTEM_TABLE *SystemTable,

+ IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,

+ IN EFI_SHELL_PROTOCOL *Shell

+ )

+{

+ gEfiShellParametersProtocol = ShellParameters;

+ gEfiShellProtocol = Shell;

+

+ return RunHttp (gImageHandle, SystemTable);

+}

+

+/**

+ This is the command help handler function pointer callback type. This

+ function is responsible for displaying help information for the associated

+ command.

+

+ @param[in] This The instance of the
EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.

+ @param[in] Language The pointer to the language string to use.

+

+ @return string Pool allocated help string, must be freed by caller

+**/

+CHAR16 *

+EFIAPI

+HttpCommandGetHelp (

+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,

+ IN CONST CHAR8 *Language

+ )

+{

+ return HiiGetString(mHttpHiiHandle,

+ STRING_TOKEN (STR_GET_HELP_HTTP),

+ Language);

+}

+

+EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL mHttpDynamicCommand = {

+ HTTP_APP_NAME,

+ HttpCommandHandler,

+ HttpCommandGetHelp

+};

+

+/**

+ Entry point of Http Dynamic Command.

+

+ Produce the DynamicCommand protocol to handle "http" command.

+

+ @param ImageHandle The image handle of the process.

+ @param SystemTable The EFI System Table pointer.

+

+ @retval EFI_SUCCESS Http command is executed sucessfully.

+ @retval EFI_ABORTED HII package was failed to initialize.

+ @retval others Other errors when executing http command.

+**/

+EFI_STATUS

+EFIAPI

+HttpCommandInitialize (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+

+ mHttpHiiHandle = InitializeHiiPackage (ImageHandle);

+ if (mHttpHiiHandle == NULL) {

+ return EFI_ABORTED;

+ }

+

+ Status = gBS->InstallProtocolInterface (

+ &ImageHandle,

+ &gEfiShellDynamicCommandProtocolGuid,

+ EFI_NATIVE_INTERFACE,

+ &mHttpDynamicCommand

+ );

+ ASSERT_EFI_ERROR (Status);

+ return Status;

+}

+

+/**

+ Http driver unload handler.

+

+ @param ImageHandle The image handle of the process.

+

+ @retval EFI_SUCCESS The image is unloaded.

+ @retval Others Failed to unload the image.

+**/

+EFI_STATUS

+EFIAPI

+HttpUnload (

+ IN EFI_HANDLE ImageHandle

+)

+{

+ EFI_STATUS Status;

+ Status = gBS->UninstallProtocolInterface (

+ ImageHandle,

+ &gEfiShellDynamicCommandProtocolGuid,

+ &mHttpDynamicCommand

+ );

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ HiiRemovePackages (mHttpHiiHandle);

+

+ return EFI_SUCCESS;

+}

diff --git
a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.in
f
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.in
f
new file mode 100644
index 000000000000..5d46ee2384d5
--- /dev/null
+++
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.in
f
@@ -0,0 +1,63 @@
+## @file

+# Provides Shell 'http' dynamic command.

+#

+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved. <BR>

+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010006

+ BASE_NAME = httpDynamicCommand

+ FILE_GUID = 19618BCE-55AE-09C6-37E9-4CE04084C7A1

+ MODULE_TYPE = DXE_DRIVER

+ VERSION_STRING = 1.0

+ ENTRY_POINT = HttpCommandInitialize

+ UNLOAD_IMAGE = HttpUnload

+#

+# This flag specifies whether HII resource section is generated into PE image.

+#

+ UEFI_HII_RESOURCE_SECTION = TRUE

+

+[Sources.common]

+ Http.c

+ HttpDynamicCommand.c

+ Http.h

+ Http.uni

+

+[Packages]

+ EmbeddedPkg/EmbeddedPkg.dec

+ MdePkg/MdePkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ NetworkPkg/NetworkPkg.dec

+ ShellPkg/ShellPkg.dec

+

+[LibraryClasses]

+ BaseLib

+ BaseMemoryLib

+ DebugLib

+ FileHandleLib

+ HiiLib

+ HttpLib

+ MemoryAllocationLib

+ NetLib

+ ShellLib

+ UefiBootServicesTableLib

+ UefiDriverEntryPoint

+ UefiHiiServicesLib

+ UefiLib

+ UefiRuntimeServicesTableLib

+

+[Protocols]

+ gEfiHiiPackageListProtocolGuid ## CONSUMES

+ gEfiHttpProtocolGuid ## CONSUMES

+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES

+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES

+ gEfiShellDynamicCommandProtocolGuid ## PRODUCES

+

+[DEPEX]

+ TRUE

diff --git a/ShellPkg/Include/Guid/ShellLibHiiGuid.h
b/ShellPkg/Include/Guid/ShellLibHiiGuid.h
index 5da9128333a4..6e328b460d8c 100644
--- a/ShellPkg/Include/Guid/ShellLibHiiGuid.h
+++ b/ShellPkg/Include/Guid/ShellLibHiiGuid.h
@@ -59,6 +59,10 @@
0x738a9314, 0x82c1, 0x4592, { 0x8f, 0xf7, 0xc1, 0xbd, 0xf1, 0xb2, 0x0e, 0xd4 }
\

}



+#define SHELL_HTTP_HII_GUID \

+ { \

+ 0x390f84b3, 0x221c, 0x4d9e, { 0xb5, 0x06, 0x6d, 0xb9, 0x42, 0x3e, 0x0a,
0x7e } \

+ }



#define SHELL_BCFG_HII_GUID \

{ \

@@ -75,6 +79,7 @@ extern EFI_GUID gShellLevel3HiiGuid;
extern EFI_GUID gShellNetwork1HiiGuid;

extern EFI_GUID gShellNetwork2HiiGuid;

extern EFI_GUID gShellTftpHiiGuid;

+extern EFI_GUID gShellHttpHiiGuid;

extern EFI_GUID gShellBcfgHiiGuid;



#endif

diff --git a/ShellPkg/ShellPkg.dec b/ShellPkg/ShellPkg.dec
index d0843d338126..7b2d1230bd2c 100644
--- a/ShellPkg/ShellPkg.dec
+++ b/ShellPkg/ShellPkg.dec
@@ -53,6 +53,7 @@ [Guids]
gShellNetwork1HiiGuid = {0xf3d301bb, 0xf4a5, 0x45a8, {0xb0, 0xb7, 0xfa,
0x99, 0x9c, 0x62, 0x37, 0xae}}

gShellNetwork2HiiGuid = {0x174b2b5, 0xf505, 0x4b12, {0xaa, 0x60, 0x59,
0xdf, 0xf8, 0xd6, 0xea, 0x37}}

gShellTftpHiiGuid = {0x738a9314, 0x82c1, 0x4592, {0x8f, 0xf7, 0xc1,
0xbd, 0xf1, 0xb2, 0x0e, 0xd4}}

+ gShellHttpHiiGuid = {0x390f84b3, 0x221c, 0x4d9e, {0xb5, 0x06, 0x6d,
0xb9, 0x42, 0x3e, 0x0a, 0x7e}}

gShellBcfgHiiGuid = {0x5f5f605d, 0x1583, 0x4a2d, {0xa6, 0xb2, 0xeb,
0x12, 0xda, 0xb4, 0xa2, 0xb6}}

gShellAcpiViewHiiGuid = {0xda8ccdf4, 0xed8f, 0x4ffc, {0xb5, 0xef, 0x2e,
0xf5, 0x5e, 0x24, 0x93, 0x2a}}

# FILE_GUID as defined in ShellPkg/Application/Shell/Shell.inf

diff --git a/ShellPkg/ShellPkg.dsc b/ShellPkg/ShellPkg.dsc
index 86e9f1e0040d..c42bc9464a0f 100644
--- a/ShellPkg/ShellPkg.dsc
+++ b/ShellPkg/ShellPkg.dsc
@@ -139,6 +139,11 @@ [Components]
gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

}

ShellPkg/DynamicCommand/TftpDynamicCommand/TftpApp.inf

+
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf {

+ <PcdsFixedAtBuild>

+ gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

+ }

+ ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf

ShellPkg/DynamicCommand/DpDynamicCommand/DpDynamicCommand.inf {

<PcdsFixedAtBuild>

gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

--
2.26.2.266.ge870325ee8


Vladimir Olovyannikov
 

Hi Gao,

Thanks for reviewing.
As you suggest, I will postpone the next patchset submission until
NetworkPkg maintainer(s) review.

-----Original Message-----
From: Gao, Zhichao <zhichao.gao@...>
Sent: Sunday, August 16, 2020 6:48 PM
To: Vladimir Olovyannikov <vladimir.olovyannikov@...>;
devel@edk2.groups.io
Cc: Laszlo Ersek <lersek@...>; Samer El-Haj-Mahmoud <Samer.El-
Haj-Mahmoud@...>; Maciej Rabeda
<maciej.rabeda@...>; Wu, Jiaxin <jiaxin.wu@...>; Fu,
Siyuan <@sfu5>; Ni, Ray <ray.ni@...>; Gao, Liming
<liming.gao@...>; Nd <nd@...>
Subject: RE: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

Sorry for the delay reply.

See below:

-----Original Message-----
From: Vladimir Olovyannikov <vladimir.olovyannikov@...>
Sent: Tuesday, July 28, 2020 12:49 AM
To: devel@edk2.groups.io
Cc: Laszlo Ersek <lersek@...>; Vladimir Olovyannikov
<vladimir.olovyannikov@...>; Samer El-Haj-Mahmoud
<Samer.El-
Haj-Mahmoud@...>; Gao, Zhichao <zhichao.gao@...>; Maciej
Rabeda <maciej.rabeda@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu,
Siyuan <@sfu5>; Ni, Ray <ray.ni@...>; Gao, Liming
<liming.gao@...>; Nd <nd@...>
Subject: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

Introduce an http client utilizing EDK2 HTTP protocol, to
allow fast image downloading from http/https servers.
HTTP download speed is usually faster than tftp.
The client is based on the same approach as tftp dynamic command, and
uses the same UEFI Shell command line parameters. This makes it easy
integrating http into existing UEFI Shell scripts.
Note that to enable HTTP download, feature Pcd
gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections must
be set to TRUE.
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2860

Signed-off-by: Vladimir Olovyannikov
<vladimir.olovyannikov@...>
CC: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>
CC: Laszlo Ersek <lersek@...>
Cc: Zhichao Gao <zhichao.gao@...>
Cc: Maciej Rabeda <maciej.rabeda@...>
Cc: Jiaxin Wu <jiaxin.wu@...>
Cc: Siyuan Fu <@sfu5>
Cc: Ray Ni <ray.ni@...>
Cc: Liming Gao <liming.gao@...>
Cc: Nd <nd@...>
---
CryptoPkg/Library/OpensslLib/openssl | 2 +-
.../DynamicCommand/HttpDynamicCommand/Http.c | 1715
+++++++++++++++++
.../DynamicCommand/HttpDynamicCommand/Http.h | 89 +
.../HttpDynamicCommand/Http.uni | 117 ++
.../HttpDynamicCommand/HttpApp.c | 53 +
.../HttpDynamicCommand/HttpApp.inf | 58 +
.../HttpDynamicCommand/HttpDynamicCommand.c | 134 ++
.../HttpDynamicCommand/HttpDynamicCommand.inf | 63 +
ShellPkg/Include/Guid/ShellLibHiiGuid.h | 5 +
ShellPkg/ShellPkg.dec | 1 +
ShellPkg/ShellPkg.dsc | 5 +
11 files changed, 2241 insertions(+), 1 deletion(-)
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand
.c
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand
.inf

diff --git a/CryptoPkg/Library/OpensslLib/openssl
b/CryptoPkg/Library/OpensslLib/openssl
index e2e09d9fba11..c3656cc594da 160000
--- a/CryptoPkg/Library/OpensslLib/openssl
+++ b/CryptoPkg/Library/OpensslLib/openssl
@@ -1 +1 @@
-Subproject commit e2e09d9fba1187f8d6aafaa34d4172f56f1ffb72
+Subproject commit c3656cc594daac8167721dde7220f0e59ae146fc
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
new file mode 100644
index 000000000000..0565b07c3570
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
@@ -0,0 +1,1715 @@
+/** @file

+ The implementation for the 'http' Shell command.

+

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
<BR>

+ (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved. <BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+**/

+

+#include "Http.h"

+

+#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32

+EFI_HII_HANDLE mHttpHiiHandle;

+

+/*

+ Constant strings and definitions related to the message

+ indicating the amount of progress in the dowloading of a HTTP
file.

+*/

+

+// Number of steps in the progression slider

+#define HTTP_PROGRESS_SLIDER_STEPS \

+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 3)

+

+// Size in number of characters plus one (final zero) of the message
to

+// indicate the progress of an HTTP download. The format is
"[(progress
slider:

+// 40 characters)] (nb of KBytes downloaded so far: 7 characters)
Kb".
There

+// are thus the number of characters in HTTP_PROGR_FRAME[] plus 11
characters

+// (2 // spaces, "Kb" and seven characters for the number of KBytes).

+#define HTTP_PROGRESS_MESSAGE_SIZE \

+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) + 12)

+

+//

+// Buffer size. Note that larger buffer does not mean better speed!

+//

+#define DEFAULT_BUF_SIZE SIZE_32KB

+#define MAX_BUF_SIZE SIZE_4MB

+

+#define MIN_PARAM_COUNT 2

+#define MAX_PARAM_COUNT 4

+

+#define TIMER_MAX_TIMEOUT_S 10

+

+// File name to use when URI ends with "/"

+#define DEFAULT_HTML_FILE L"index.html"

+#define DEFAULT_HTTP_PROTO L"http"

+

+// String to delete the HTTP progress message to be able to update it
:

+// (HTTP_PROGRESS_MESSAGE_SIZE-1) '\b'

+#define HTTP_PROGRESS_DEL \

+
L"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\
b\b\b\

+\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"

+

+#define HTTP_KB L"\b\b\b\b\b\b\b\b\b\b"

+// Frame for the progression slider

+#define HTTP_PROGR_FRAME L"[
]"

+

+// String descriptions for server errors

+STATIC CONST CHAR16 *ErrStatusDesc[] =

+{

+ L"400 Bad Request",

+ L"401 Unauthorized",

+ L"402 Payment required",

+ L"403 Forbidden",

+ L"404 Not Found",

+ L"405 Method not allowed",

+ L"406 Not acceptable",

+ L"407 Proxy authentication required",

+ L"408 Request time out",

+ L"409 Conflict",

+ L"410 Gone",

+ L"411 Length required",

+ L"412 Precondition failed",

+ L"413 Request entity too large",

+ L"414 Request URI to large",

+ L"415 Unsupported media type",

+ L"416 Requested range not satisfied",

+ L"417 Expectation failed",

+ L"500 Internal server error",

+ L"501 Not implemented",

+ L"502 Bad gateway",

+ L"503 Service unavailable",

+ L"504 Gateway timeout",

+ L"505 HTTP version not supported"

+};

+

+// Local File Handle

+STATIC SHELL_FILE_HANDLE mFileHandle = NULL;

+

+// Path of the local file, Unicode encoded

+STATIC CONST CHAR16 *mLocalFilePath;

+

+STATIC BOOLEAN gRequestCallbackComplete = FALSE;

+STATIC BOOLEAN gResponseCallbackComplete = FALSE;

+

+STATIC BOOLEAN gHttpError;

+

+/**

+ Cleans off leading and trailing spaces and tabs.

+

+ @param[in] String pointer to the string to trim them off.

+**/

+STATIC

+EFI_STATUS

+TrimSpaces (

+ IN CHAR16 **String

+ )

+{

+ ASSERT(String != NULL);

+ ASSERT(*String!= NULL);

+ //

+ // Remove any spaces and tabs at the beginning of the (*String).

+ //

+ while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {

+ CopyMem (

+ (*String),

+ (*String) + 1,

+ StrSize ((*String)) - sizeof((*String)[0])

+ );

+ }

+

+ //

+ // Remove any spaces and tabs at the end of the (*String).

+ //

+ while ((StrLen (*String) > 0) &&

+ (((*String)[StrLen ((*String)) - 1] == L' ') ||

+ ((*String)[StrLen ((*String)) - 1] == L'\t'))

+ ) {

+ (*String)[StrLen ((*String)) - 1] = CHAR_NULL;

+ }

+

+ return (EFI_SUCCESS);

+}

+

+

+/*

+ * Callbacks for request and response.

+ * We just acknowledge that operation has completed here.

+ */

+STATIC

+VOID

+EFIAPI

+RequestCallback (

+ IN EFI_EVENT Event,

+ IN VOID *Context

+)

+{

+ gRequestCallbackComplete = TRUE;

+}

+

+STATIC

+VOID

+EFIAPI

+ResponseCallback (

+ IN EFI_EVENT Event,

+ IN VOID *Context

+)

+{

+ gResponseCallbackComplete = TRUE;

+}

+

+

+/**

+ Check and convert the UINT16 option values of the 'http' command

+

+ @param[in] ValueStr Value as an Unicode encoded string

+ @param[out] Value UINT16 value

+

+ @return TRUE The value was returned.

+ @return FALSE A parsing error occured.

+**/

+STATIC

+BOOLEAN

+StringToUint16 (

+ IN CONST CHAR16 *ValueStr,

+ OUT UINT16 *Value

+ );

+

+/**

+ Get the name of the NIC.

+

+ @param[in] ControllerHandle The network physical device handle.

+ @param[in] NicNumber The network physical device number.

+ @param[out] NicName Address where to store the NIC name.

+ The memory area has to be at least

+
IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH

+ double byte wide.

+

+ @return EFI_SUCCESS The name of the NIC was returned.

+ @return Others The creation of the child for the Managed

+ Network Service failed or the opening of

+ the Managed Network Protocol failed or

+ the operational parameters for the

+ Managed Network Protocol could not be

+ read.

+**/

+STATIC

+EFI_STATUS

+GetNicName (

+ IN EFI_HANDLE ControllerHandle,

+ IN UINTN NicNumber,

+ OUT CHAR16 *NicName

+ );

+

+/**

+ Create a child for the service identified by its service binding
protocol
GUID

+ and get from the child the interface of the protocol identified by
its
GUID.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol
GUID
of the

+ service to be created.

+ @param[in] ProtocolGuid GUID of the protocol to be
open.

+ @param[out] ChildHandle Address where the handler
of the

+ created child is returned.
NULL is

+ returned in case of error.

+ @param[out] Interface Address where a pointer to
the

+ protocol interface is
returned in

+ case of success.

+

+ @return EFI_SUCCESS The child was created and the protocol
opened.

+ @return Others Either the creation of the child or the
opening

+ of the protocol failed.

+**/

+STATIC

+EFI_STATUS

+CreateServiceChildAndOpenProtocol (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ OUT EFI_HANDLE *ChildHandle,

+ OUT VOID **Interface

+ );

+

+/**

+ Close the protocol identified by its GUID on the child handle of
the
service

+ identified by its service binding protocol GUID, then destroy the
child

+ handle.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol
GUID
of the

+ service to be destroyed.

+ @param[in] ProtocolGuid GUID of the protocol to be
closed.

+ @param[in] ChildHandle Handle of the child to be
destroyed.

+

+**/

+STATIC

+VOID

+CloseProtocolAndDestroyServiceChild (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ IN EFI_HANDLE ChildHandle

+ );

+

+

+/**

+ Worker function that download the data of a file from an HTTP
server
given

+ the path of the file and its size.

+

+ @param[in] Context A pointer to the download context.

+

+ @retval EFI_SUCCESS The file was downloaded.

+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.

+ @retval Others The downloading of the file

+ from the server failed.

+

+**/

+STATIC

+EFI_STATUS

+DownloadFile (

+ IN DOWNLOAD_CONTEXT *Context

+ );

+

+STATIC CONST SHELL_PARAM_ITEM ParamList[] = {

+ {L"-i", TypeValue},

+ {L"-k", TypeFlag},

+ {L"-l", TypeValue},

+ {L"-s", TypeValue},

+ {L"-t", TypeValue},

+ {NULL , TypeMax}

+ };

+

+/**

+ Function for 'http' command.

+

+ @param[in] ImageHandle Handle to the Image (NULL if Internal).

+ @param[in] SystemTable Pointer to the System Table (NULL if
Internal).

+

+ @return SHELL_SUCCESS The 'http' command completed
successfully.

+ @return SHELL_ABORTED The Shell Library initialization
failed.

+ @return SHELL_INVALID_PARAMETER At least one of the command's
arguments is

+ not valid.

+ @return SHELL_OUT_OF_RESOURCES A memory allocation failed.

+ @return SHELL_NOT_FOUND Network Interface Card not found.

+ @return SHELL_UNSUPPORTED Command was valid, but the server
returned

+ a status code indicating some
error.

+ Examine the file requested for
error body.

+

+**/

+SHELL_STATUS

+RunHttp (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+ LIST_ENTRY *CheckPackage;

+ UINTN ParamCount;

+ UINTN HandleCount;

+ UINTN NicNumber;

+ UINTN InitialSize;

+ UINTN ParamOffset;

+ UINTN StartSize;

+ CHAR16 *ProblemParam;

+ CHAR16
NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];

+ CHAR16 *Walker1;

+ CHAR16 *VStr;

+ CONST CHAR16 *UserNicName;

+ CONST CHAR16 *ValueStr;

+ CONST CHAR16 *RemoteFilePath;

+ CONST CHAR16 *Walker;

+ EFI_HTTP_CONFIG_DATA HttpConfigData;

+ EFI_HTTPv4_ACCESS_POINT IPv4Node;

+ EFI_HANDLE *Handles;

+ EFI_HANDLE ControllerHandle;

+ EFI_HANDLE HttpChildHandle;

+ DOWNLOAD_CONTEXT Context;

+ BOOLEAN NicFound;

+

+ ProblemParam = NULL;

+ RemoteFilePath = NULL;

+ NicFound = FALSE;

+ Handles = NULL;

+

+ //

+ // Initialize the Shell library (we must be in non-auto-init...)

+ //

+ ParamOffset = 0;

+ gHttpError = FALSE;

+

+ Status = ShellInitialize ();

+ if (EFI_ERROR (Status)) {

+ ASSERT_EFI_ERROR (Status);

+ return SHELL_ABORTED;

+ }

+

+ ZeroMem (&Context, sizeof (Context));

+

+ //

+ // Parse the command line.

+ //

+ Status = ShellCommandLineParse (

+ ParamList,

+ &CheckPackage,

+ &ProblemParam,

+ TRUE);
I prefer to put ');' in next line like other functions.


+ if (EFI_ERROR (Status)) {

+ if ((Status == EFI_VOLUME_CORRUPTED) &&

+ (ProblemParam != NULL) ) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mHttpHiiHandle,

+ HTTP_APP_NAME, ProblemParam

+ );

+ SHELL_FREE_NON_NULL (ProblemParam);

+ } else {

+ ASSERT (FALSE);

+ }

+

+ goto Error;

+ }

+

+ //

+ // Check the number of parameters

+ //

+ Status = EFI_INVALID_PARAMETER;

+

+ ParamCount = ShellCommandLineGetCount (CheckPackage);

+ if (ParamCount > MAX_PARAM_COUNT) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY),

+ mHttpHiiHandle, HTTP_APP_NAME

+ );

+ goto Error;

+ }

+

+ if (ParamCount < MIN_PARAM_COUNT) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW),

+ mHttpHiiHandle, HTTP_APP_NAME

+ );

+ goto Error;

+ }

+

+ ZeroMem (&HttpConfigData, sizeof (HttpConfigData));

+ ZeroMem (&IPv4Node, sizeof (IPv4Node));

+ IPv4Node.UseDefaultAddress = TRUE;

+

+ HttpConfigData.HttpVersion = HttpVersion11;

+ HttpConfigData.AccessPoint.IPv4Node = &IPv4Node;

+

+ //

+ // Get the host address (not necessarily IPv4 format)

+ //

+ ValueStr = ShellCommandLineGetRawValue (CheckPackage, 1);

+ if (!ValueStr) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),
mHttpHiiHandle,

+ HTTP_APP_NAME, ValueStr

+ );

+ goto Error;

+ } else {

+ StartSize = 0;

+ if (!StrStr (ValueStr, L"://")) {

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ DEFAULT_HTTP_PROTO,

+ StrLen (DEFAULT_HTTP_PROTO)

+ );

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ L"://",

+ StrLen (L"://")

+ );

+ VStr = (CHAR16 *)ValueStr;

+ } else {

+ VStr = StrStr (ValueStr, L"://") + StrLen (L"://");

+ }

+

+ for (Walker1 = VStr; *Walker1; Walker1++) {

+ if (*Walker1 == L'/') {

+ break;

+ }

+ }

+

+ if (*Walker1 == L'/') {

+ ParamOffset = 1;

+ RemoteFilePath = Walker1;

+ }

+

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ ValueStr,

+ StrLen (ValueStr) - StrLen
(Walker1)

+ );

+ if (!Context.ServerAddrAndProto) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+ }

+

+ if (!RemoteFilePath) {

+ RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);

+ if (!RemoteFilePath) {

+ // If no path given, assume just "/"

+ RemoteFilePath = L"/";

+ }

+ }

+

+ TrimSpaces ((CHAR16 **)&RemoteFilePath);

+

+ if (ParamCount == MAX_PARAM_COUNT - ParamOffset) {

+ mLocalFilePath = ShellCommandLineGetRawValue (

+ CheckPackage,

+ MAX_PARAM_COUNT - 1 - ParamOffset);

+ } else {

+ Walker = RemoteFilePath + StrLen (RemoteFilePath);

+ while ((--Walker) >= RemoteFilePath) {

+ if ((*Walker == L'\\') ||

+ (*Walker == L'/' ) ) {
It is better to remove the useless space in the above line at the end of
'if'
condition.


+ break;

+ }

+ }

+

+ mLocalFilePath = Walker + 1;

+ }

+

+ if (!StrLen (mLocalFilePath)) {

+ mLocalFilePath = DEFAULT_HTML_FILE;

+ }

+

+ InitialSize = 0;

+ Context.URI = StrnCatGrow (

+ &Context.URI,

+ &InitialSize,

+ RemoteFilePath,

+ StrLen (RemoteFilePath)

+ );

+ if (!Context.URI) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ //

+ // Get the name of the Network Interface Card to be used if any.

+ //

+ UserNicName = ShellCommandLineGetValue (CheckPackage, L"-i");

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-l");

+ if (ValueStr != NULL) {

+ if (!StringToUint16 (

+ ValueStr,

+ &HttpConfigData.AccessPoint.IPv4Node->LocalPort

+ )

+ ) {
I suggest to align the end ')' of StringToUint16 to align with the
parameters.
And the end ')' of 'if' to go right next to the end ')' of
StringToUint16.
OK, will do.


+ goto Error;

+ }

+ }

+

+ Context.BufferSize = DEFAULT_BUF_SIZE;

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-s");

+ if (ValueStr != NULL) {

+ Context.BufferSize = ShellStrToUintn (ValueStr);

+ if (!Context.BufferSize || Context.BufferSize > MAX_BUF_SIZE) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),

+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr

+ );

+ goto Error;

+ }

+ }

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");

+ if (ValueStr != NULL) {

+ HttpConfigData.TimeOutMillisec = (UINT32)ShellStrToUintn
(ValueStr);

I am not sure if it is fine to transfer '-1' as time out value when the
-t with
invalid number string. It depends on the NetworkPkg maintainers' review.

I am OK with other part of the patch. But I still need NetworkPkg
maintainers'
help to review above issue and the implementation.
There is no need to resend another patch if NetworkPkg maintainers are
OK
with above issue and the implementation.
OK, I will postpone submission of the patch until NetworkPkg
maintainter(s) review.

Thanks,
Zhichao
Thank you,
Vladimir

+ }

+

+ //

+ // Locate all HTTP Service Binding protocols

+ //

+ Status = gBS->LocateHandleBuffer (

+ ByProtocol,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ NULL,

+ &HandleCount,

+ &Handles

+ );

+ if (EFI_ERROR (Status) || (HandleCount == 0)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NO_NIC),
mHttpHiiHandle

+ );

+ if (!EFI_ERROR (Status)) {

+ Status = EFI_NOT_FOUND;

+ }

+

+ goto Error;

+ }

+

+ Status = EFI_NOT_FOUND;

+

+ for (NicNumber = 0;

+ (NicNumber < HandleCount) && (Status != EFI_SUCCESS);

+ NicNumber++) {

+ ControllerHandle = Handles[NicNumber];

+

+ Status = GetNicName (ControllerHandle, NicNumber, NicName);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NAME),

+ mHttpHiiHandle, NicNumber, Status

+ );

+ continue;

+ }

+

+ if (UserNicName != NULL) {

+ if (StrCmp (NicName, UserNicName) != 0) {

+ Status = EFI_NOT_FOUND;

+ continue;

+ }

+ NicFound = TRUE;

+ }

+

+ Status = CreateServiceChildAndOpenProtocol (

+ ControllerHandle,

+ &gEfiHttpServiceBindingProtocolGuid,

+ &gEfiHttpProtocolGuid,

+ &HttpChildHandle,

+ (VOID**)&Context.Http

+ );

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_OPEN_PROTOCOL),

+ mHttpHiiHandle, NicName, Status

+ );

+ continue;

+ }

+

+ Status = Context.Http->Configure (Context.Http, &HttpConfigData);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_CONFIGURE),

+ mHttpHiiHandle, NicName, Status

+ );

+ continue;

+ }

+

+ Context.Flags = 0;

+ if (ShellCommandLineGetFlag (CheckPackage, L"-m")) {

+ Context.Flags |= DL_FLAG_TIME;

+ }

+

+ if (ShellCommandLineGetFlag (CheckPackage, L"-k")) {

+ Context.Flags |= DL_FLAG_KEEP_BAD;

+ }

+

+ Status = DownloadFile (&Context);

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle

+ );

+

+ CloseProtocolAndDestroyServiceChild (

+ ControllerHandle,

+ &gEfiHttpServiceBindingProtocolGuid,

+ &gEfiHttpProtocolGuid,

+ HttpChildHandle

+ );

+

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_DOWNLOAD),

+ mHttpHiiHandle, RemoteFilePath, NicName, Status

+ );

+ // If a user aborted the operation, do not try another
controller.

+ if (Status == EFI_ABORTED) {

+ goto Error;

+ }

+ }

+

+ if (gHttpError) {

+ //

+ // This is not related to connection, so no need to repeat with

+ // another interface.

+ //

+ break;

+ }

+ }

+

+ if ((UserNicName != NULL) && (!NicFound)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NOT_FOUND),

+ mHttpHiiHandle, UserNicName

+ );

+ }

+

+Error:

+ ShellCommandLineFreeVarList (CheckPackage);

+ SHELL_FREE_NON_NULL (Handles);

+ SHELL_FREE_NON_NULL (Context.ServerAddrAndProto);

+ SHELL_FREE_NON_NULL (Context.URI);

+

+ return Status & ~MAX_BIT;

+}

+

+/**

+ Check and convert the UINT16 option values of the 'http' command

+

+ @param[in] ValueStr Value as an Unicode encoded string

+ @param[out] Value UINT16 value

+

+ @return TRUE The value was returned.

+ @return FALSE A parsing error occured.

+**/

+STATIC

+BOOLEAN

+StringToUint16 (

+ IN CONST CHAR16 *ValueStr,

+ OUT UINT16 *Value

+ )

+{

+ UINTN Val;

+

+ Val = ShellStrToUintn (ValueStr);

+ if (Val > MAX_UINT16) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),

+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr

+ );

+ return FALSE;

+ }

+

+ *Value = (UINT16)Val;

+ return TRUE;

+}

+

+/**

+ Get the name of the NIC.

+

+ @param[in] ControllerHandle The network physical device handle.

+ @param[in] NicNumber The network physical device number.

+ @param[out] NicName Address where to store the NIC name.

+ The memory area has to be at least

+
IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH

+ double byte wide.

+

+ @return EFI_SUCCESS The name of the NIC was returned.

+ @return Others The creation of the child for the Managed

+ Network Service failed or the opening of

+ the Managed Network Protocol failed or

+ the operational parameters for the

+ Managed Network Protocol could not be

+ read.

+**/

+STATIC

+EFI_STATUS

+GetNicName (

+ IN EFI_HANDLE ControllerHandle,

+ IN UINTN NicNumber,

+ OUT CHAR16 *NicName

+ )

+{

+ EFI_STATUS Status;

+ EFI_HANDLE MnpHandle;

+ EFI_MANAGED_NETWORK_PROTOCOL *Mnp;

+ EFI_SIMPLE_NETWORK_MODE SnpMode;

+

+ Status = CreateServiceChildAndOpenProtocol (

+ ControllerHandle,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ &gEfiManagedNetworkProtocolGuid,

+ &MnpHandle,

+ (VOID**)&Mnp

+ );

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);

+ if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {

+ goto Error;

+ }

+

+ UnicodeSPrint (

+ NicName,

+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,

+ SnpMode.IfType == NET_IFTYPE_ETHERNET ?

+ L"eth%d" :

+ L"unk%d",

+ NicNumber

+ );

+

+ Status = EFI_SUCCESS;

+

+Error:

+

+ if (MnpHandle != NULL) {

+ CloseProtocolAndDestroyServiceChild (

+ ControllerHandle,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ &gEfiManagedNetworkProtocolGuid,

+ MnpHandle

+ );

+ }

+

+ return Status;

+}

+

+/**

+ Create a child for the service identified by its service binding
protocol
GUID

+ and get from the child the interface of the protocol identified by
its
GUID.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol
GUID
of the

+ service to be created.

+ @param[in] ProtocolGuid GUID of the protocol to be
open.

+ @param[out] ChildHandle Address where the handler
of the

+ created child is returned.
NULL is

+ returned in case of error.

+ @param[out] Interface Address where a pointer to
the

+ protocol interface is
returned in

+ case of success.

+

+ @return EFI_SUCCESS The child was created and the protocol
opened.

+ @return Others Either the creation of the child or the
opening

+ of the protocol failed.

+**/

+STATIC

+EFI_STATUS

+CreateServiceChildAndOpenProtocol (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ OUT EFI_HANDLE *ChildHandle,

+ OUT VOID **Interface

+ )

+{

+ EFI_STATUS Status;

+

+ *ChildHandle = NULL;

+ Status = NetLibCreateServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ ChildHandle

+ );

+ if (!EFI_ERROR (Status)) {

+ Status = gBS->OpenProtocol (

+ *ChildHandle,

+ ProtocolGuid,

+ Interface,

+ gImageHandle,

+ ControllerHandle,

+ EFI_OPEN_PROTOCOL_GET_PROTOCOL

+ );

+ if (EFI_ERROR (Status)) {

+ NetLibDestroyServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ *ChildHandle

+ );

+ *ChildHandle = NULL;

+ }

+ }

+

+ return Status;

+}

+

+/**

+ Close the protocol identified by its GUID on the child handle of
the
service

+ identified by its service binding protocol GUID, then destroy the
child

+ handle.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol
GUID
of the

+ service to be destroyed.

+ @param[in] ProtocolGuid GUID of the protocol to be
closed.

+ @param[in] ChildHandle Handle of the child to be
destroyed.

+

+**/

+STATIC

+VOID

+CloseProtocolAndDestroyServiceChild (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ IN EFI_HANDLE ChildHandle

+ )

+{

+ gBS->CloseProtocol (

+ ChildHandle,

+ ProtocolGuid,

+ gImageHandle,

+ ControllerHandle

+ );

+

+ NetLibDestroyServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ ChildHandle

+ );

+}

+

+/**

+ Wait until operation completes. Completion is indicated by

+ setting of an appropriate variable.

+

+ @param[in] Context A pointer to the HTTP download
context.

+ @param[in] CallBackComplete A pointer to the callback
completion

+ variable set by the callback.

+

+ @return EFI_SUCCESS Callback signalled completion.

+ @return EFI_TIMEOUT Timed out waiting for completion.

+ @return Others Error waiting for completion.

+**/

+STATIC

+EFI_STATUS

+WaitForCompletion (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN OUT BOOLEAN *CallBackComplete

+ )

+{

+ EFI_STATUS Status;

+ EFI_EVENT WaitEvt;

+

+ Status = EFI_SUCCESS;

+

+ // Use a timer to measure timeout. Cannot use Stall here!

+ Status = gBS->CreateEvent (

+ EVT_TIMER,

+ TPL_CALLBACK,

+ NULL,

+ NULL,

+ &WaitEvt

+ );

+ ASSERT_EFI_ERROR (Status);

+

+ if (!EFI_ERROR (Status)) {

+ Status = gBS->SetTimer(

+ WaitEvt,

+ TimerRelative,

+ EFI_TIMER_PERIOD_SECONDS (TIMER_MAX_TIMEOUT_S)

+ );

+

+ ASSERT_EFI_ERROR (Status);

+ }

+

+ while (! *CallBackComplete &&

+ (!EFI_ERROR (Status)) &&

+ EFI_ERROR (gBS->CheckEvent (WaitEvt))

+ ) {

+ Status = Context->Http->Poll (Context->Http);

+ }

+

+ gBS->SetTimer (WaitEvt, TimerCancel, 0);

+ gBS->CloseEvent (WaitEvt);

+

+ if (*CallBackComplete) {

+ return EFI_SUCCESS;

+ }

+

+ if (!EFI_ERROR (Status)) {

+ Status = EFI_TIMEOUT;

+ }

+

+ return Status;

+}

+

+/**

+ Generate and send a request to the http server.

+

+ @param[in] Context HTTP download context.

+ @param[in] DownloadUrl Fully qualified URL to be
downloaded.

+

+ @return EFI_SUCCESS Request has been sent successfully.

+ @return EFI_INVALID_PARAMETER Invalid URL.

+ @return EFI_OUT_OF_RESOURCES Out of memory.

+ @return EFI_DEVICE_ERROR If HTTPS is used, this probably

+ means that TLS support either was
not

+ installed or not configured.

+ @return Others Error sending the request.

+**/

+

+STATIC

+EFI_STATUS

+SendRequest (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_HTTP_REQUEST_DATA RequestData;

+ EFI_HTTP_HEADER RequestHeader;

+ EFI_HTTP_MESSAGE RequestMessage;

+ EFI_STATUS Status;

+ CHAR16 *Host;

+ UINTN StringSize;

+

+ ZeroMem (&RequestData, sizeof (RequestData));

+ ZeroMem (&RequestHeader, sizeof (RequestHeader));

+ ZeroMem (&RequestMessage, sizeof (RequestMessage));

+ ZeroMem (&Context->RequestToken, sizeof (Context-
RequestToken));

+

+ RequestHeader.FieldName = "Host";

+

+ Host = (CHAR16 *)Context->ServerAddrAndProto;

+ while (*Host != CHAR_NULL && *Host != L'/') {

+ Host++;

+ }

+

+ if (*Host == CHAR_NULL) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ //

+ // Get the next slash

+ //

+ Host++;

+ //

+ // And now the host name

+ //

+ Host++;

+

+ StringSize = StrLen (Host) + 1;

+ RequestHeader.FieldValue = AllocatePool (StringSize);

+ if (!RequestHeader.FieldValue) {

+ return EFI_OUT_OF_RESOURCES;

+ }

+

+ UnicodeStrToAsciiStrS (Host, RequestHeader.FieldValue,
StringSize);

+

+ RequestMessage.HeaderCount++;

+

+ RequestData.Method = HttpMethodGet;

+ RequestData.Url = DownloadUrl;

+

+ RequestMessage.Data.Request = &RequestData;

+ RequestMessage.Headers = &RequestHeader;

+ RequestMessage.BodyLength = 0;

+ RequestMessage.Body = NULL;

+ Context->RequestToken.Event = NULL;

+

+ //

+ // Completion callback event to be set when Request completes.

+ //

+ Status = gBS->CreateEvent (

+ EVT_NOTIFY_SIGNAL,

+ TPL_CALLBACK,

+ RequestCallback,

+ Context,

+ &Context->RequestToken.Event

+ );

+ ASSERT_EFI_ERROR (Status);

+

+ Context->RequestToken.Status = EFI_SUCCESS;

+ Context->RequestToken.Message = &RequestMessage;

+ gRequestCallbackComplete = FALSE;

+ Status = Context->Http->Request (Context->Http, &Context-
RequestToken);

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ Status = WaitForCompletion (Context, &gRequestCallbackComplete);

+ if (EFI_ERROR (Status)) {

+ Context->Http->Cancel (Context->Http, &Context->RequestToken);

+ }

+Error:

+ SHELL_FREE_NON_NULL (RequestHeader.FieldValue);

+ if (Context->RequestToken.Event) {

+ gBS->CloseEvent (Context->RequestToken.Event);

+ ZeroMem (&Context->RequestToken, sizeof (Context-
RequestToken));

+ }

+

+ return Status;

+}

+

+/**

+ Update the progress of a file download

+ This procedure is called each time a new HTTP body portion is
received.

+

+ @param[in] Context HTTP download context.

+ @param[in] DownloadLen Portion size, in bytes.

+ @param[in] Buffer The pointer to the parsed buffer.

+

+ @retval EFI_SUCCESS Portion saved.

+ @retval Other Error saving the portion.

+

+**/

+STATIC

+EFI_STATUS

+EFIAPI

+SavePortion (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN UINTN DownloadLen,

+ IN CHAR8 *Buffer

+ )

+{

+ CHAR16 Progress[HTTP_PROGRESS_MESSAGE_SIZE];

+ UINTN NbOfKb;

+ UINTN Index;

+ UINTN LastStep;

+ UINTN Step;

+ EFI_STATUS Status;

+

+ LastStep = 0;

+ Step = 0;

+ ShellSetFilePosition (mFileHandle, Context->LastReportedNbOfBytes);

+ Status = ShellWriteFile (mFileHandle, &DownloadLen, Buffer);

+ if (EFI_ERROR (Status)) {

+ if (Context->ContentDownloaded > 0) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle

+ );

+ }

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_WRITE),

+ mHttpHiiHandle, mLocalFilePath, Status

+ );

+

+ return Status;

+ }

+

+ if (Context->ContentDownloaded == 0) {

+ ShellPrintEx (-1, -1, L"%s 0 Kb", HTTP_PROGR_FRAME);

+ }

+

+ Context->ContentDownloaded += DownloadLen;

+ NbOfKb = Context->ContentDownloaded >> 10;

+

+ Progress[0] = L'\0';

+ if (Context->ContentLength) {

+ LastStep = (Context->LastReportedNbOfBytes *
HTTP_PROGRESS_SLIDER_STEPS) /

+ Context->ContentLength;

+ Step = (Context->ContentDownloaded *
HTTP_PROGRESS_SLIDER_STEPS) /

+ Context->ContentLength;

+ }

+

+ Context->LastReportedNbOfBytes = Context->ContentDownloaded;

+

+ if (Step <= LastStep) {

+ if (!Context->ContentLength) {

+ //

+ // Update downloaded size, there is no length info available.

+ //

+ ShellPrintEx (-1, -1, L"%s", HTTP_KB);

+ ShellPrintEx (-1, -1, L"%7d Kb", NbOfKb);

+ }

+

+ return EFI_SUCCESS;

+ }

+

+ ShellPrintEx (-1, -1, L"%s", HTTP_PROGRESS_DEL);

+

+ Status = StrCpyS (Progress, HTTP_PROGRESS_MESSAGE_SIZE,
HTTP_PROGR_FRAME);

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ for (Index = 1; Index < Step; Index++) {

+ Progress[Index] = L'=';

+ }

+

+ if (Step) {

+ Progress[Step] = L'>';

+ }

+

+ UnicodeSPrint (

+ Progress + (sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 1,

+ sizeof (Progress) - sizeof (HTTP_PROGR_FRAME),

+ L" %7d Kb",

+ NbOfKb

+ );

+

+

+ ShellPrintEx (-1, -1, L"%s", Progress);

+

+ return EFI_SUCCESS;

+}

+

+/**

+ Replace the original Host and URI with Host and URI returned by the

+ HTTP server in 'Location' header (redirection).

+

+ @param[in] Location A pointer to the 'Location' string

+ provided by HTTP server.

+ @param[in] Context A pointer to HTTP download context.

+ @param[in] DownloadUrl Fully qualified HTTP URL.

+

+ @return EFI_SUCCESS Host and URI were successfully set.

+ @return EFI_OUT_OF_RESOURCES Error setting Host or URI.

+**/

+

+STATIC

+EFI_STATUS

+SetHostURI (

+ IN CHAR8 *Location,

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_STATUS Status;

+ UINTN StringSize;

+ UINTN FirstStep;

+ UINTN Idx;

+ UINTN Step;

+ CHAR8 *Walker;

+ CHAR16 *Temp;

+ CHAR8 *Tmp;

+ CHAR16 *Url;

+ BOOLEAN IsAbEmptyUrl;

+

+ Tmp = NULL;

+ Url = NULL;

+ IsAbEmptyUrl = FALSE;

+ FirstStep = 0;

+

+ StringSize = (AsciiStrSize (Location) * sizeof (CHAR16));

+ Url = AllocateZeroPool (StringSize);

+ if (!Url) {

+ return EFI_OUT_OF_RESOURCES;

+ }

+

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Location,

+ Url,

+ StringSize

+ );

+

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ //

+ // If an HTTP server redirects to the same location more than once,

+ // then stop attempts and tell it is not reachable.

+ //

+ if (!StrCmp (Url, DownloadUrl)) {

+ Status = EFI_NO_MAPPING;

+ goto Error;

+ }

+

+ if (AsciiStrLen (Location) > 2) {

+ // Some servers return 'Location: //server/resource'

+ IsAbEmptyUrl = (Location[0] == '/') && (Location[1] == '/');

+ if (IsAbEmptyUrl) {

+ // Skip first "//"

+ Location += 2;

+ FirstStep = 1;

+ }

+ }

+

+ if (AsciiStrStr (Location, "://") || IsAbEmptyUrl) {

+ Idx = 0;

+ Walker = Location;

+

+ for (Step = FirstStep; Step < 2; Step++) {

+ for (; *Walker != '/' && *Walker != '\0'; Walker++) {

+ Idx++;

+ }

+ if (!Step) {

+ //

+ // Skip "//"

+ //

+ Idx += 2;

+ Walker += 2;

+ }

+ }

+

+ Tmp = AllocateZeroPool (Idx + 1);

+ if (!Tmp) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ CopyMem (Tmp, Location, Idx);

+

+ //

+ // Location now points to URI

+ //

+ Location += Idx;

+ StringSize = (Idx + 1) * sizeof (CHAR16);

+

+ SHELL_FREE_NON_NULL (Context->ServerAddrAndProto);

+

+ Temp = AllocateZeroPool (StringSize);

+ if (!Temp) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Tmp,

+ Temp,

+ StringSize

+ );

+ if (EFI_ERROR (Status)) {

+ SHELL_FREE_NON_NULL (Temp);

+ goto Error;

+ }

+

+ Idx = 0;

+ if (IsAbEmptyUrl) {

+ Context->ServerAddrAndProto = StrnCatGrow (

+ &Context->ServerAddrAndProto,

+ &Idx,

+ L"http://",

+ StrLen (L"http://")

+ );

+ }

+

+ Context->ServerAddrAndProto = StrnCatGrow (

+ &Context->ServerAddrAndProto,

+ &Idx,

+ Temp,

+ StrLen (Temp)

+ );

+ SHELL_FREE_NON_NULL (Temp);

+ if (!Context->ServerAddrAndProto) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+ }

+

+ SHELL_FREE_NON_NULL (Context->URI);

+

+ StringSize = AsciiStrSize (Location) * sizeof (CHAR16);

+ Context->URI = AllocateZeroPool (StringSize);

+ if (!Context->URI) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ //

+ // Now make changes to the URI part.

+ //

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Location,

+ Context->URI,

+ StringSize

+ );

+Error:

+ SHELL_FREE_NON_NULL (Tmp);

+ SHELL_FREE_NON_NULL (Url);

+ return Status;

+}

+

+/**

+ Message parser callback.

+ Save a portion of HTTP body.

+

+ @param[in] EventType Type of event. Can be either

+ OnComplete or OnData.

+ @param[in] Data A pointer to the buffer with data.

+ @param[in] Length Data length of this portion.

+ @param[in] Context A pointer to the HTTP download
context.

+

+ @return EFI_SUCCESS The portion was processed successfully.

+ @return Other Error returned by SavePortion.

+**/

+

+STATIC

+EFI_STATUS

+EFIAPI

+ParseMsg (

+ IN HTTP_BODY_PARSE_EVENT EventType,

+ IN CHAR8 *Data,

+ IN UINTN Length,

+ IN VOID *Context

+ )

+{

+ if (!Data || (EventType == BodyParseEventOnComplete) || !Context) {

+ return EFI_SUCCESS;

+ }

+

+ return SavePortion (Context, Length, Data);

+}

+

+

+/**

+ Get HTTP server response and collect the whole body as a file.

+ Set appropriate status in Context (REQ_OK, REQ_REPEAT, REQ_ERROR).

+ Note that even if HTTP server returns an error code, it might send

+ the body as well. This body will be collected in the resultant
file.

+

+ @param[in] Context A pointer to the HTTP download
context.

+ @param[in] DownloadedUrl A pointer to the fully qualified URL
to
download.

+

+ @return EFI_SUCCESS Valid file. Body successfully
collected.

+ @return EFI_HTTP_ERROR Response is a valid HTTP response, but
the

+ HTTP server

+ indicated an error (HTTP code >= 400).

+ Response body MAY contain full

+ HTTP server response.

+ @return Others Error getting the reponse from the
HTTP server.

+ Response body is not collected.

+**/

+STATIC

+EFI_STATUS

+GetResponse (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_HTTP_RESPONSE_DATA ResponseData;

+ EFI_HTTP_MESSAGE ResponseMessage;

+ EFI_HTTP_HEADER *Header;

+ EFI_STATUS Status;

+ VOID *MsgParser;

+ CONST CHAR16 *Desc;

+ BOOLEAN IsTrunked;

+ BOOLEAN Redirection;

+

+ ZeroMem (&ResponseData, sizeof (ResponseData));

+ ZeroMem (&ResponseMessage, sizeof (ResponseMessage));

+ ZeroMem (&Context->ResponseToken, sizeof (Context-
ResponseToken));

+ IsTrunked = FALSE;

+

+ ResponseMessage.Body = Context->Buffer;

+ Context->ResponseToken.Status = EFI_SUCCESS;

+ Context->ResponseToken.Message = &ResponseMessage;

+ Context->ContentLength = 0;

+ Context->Status = REQ_OK;

+ MsgParser = NULL;

+ ResponseData.StatusCode = HTTP_STATUS_UNSUPPORTED_STATUS;

+ ResponseMessage.Data.Response = &ResponseData;

+ Context->ResponseToken.Event = NULL;

+

+ do {

+ SHELL_FREE_NON_NULL (ResponseMessage.Headers);

+ ResponseMessage.HeaderCount = 0;

+ gResponseCallbackComplete = FALSE;

+ ResponseMessage.BodyLength = Context->BufferSize;

+

+ if (ShellGetExecutionBreakFlag ()) {

+ Status = EFI_ABORTED;

+ break;

+ }

+

+ if (!Context->ContentDownloaded && !Context-
ResponseToken.Event) {

+ Status = gBS->CreateEvent (

+ EVT_NOTIFY_SIGNAL,

+ TPL_CALLBACK,

+ ResponseCallback,

+ Context,

+ &Context->ResponseToken.Event

+ );

+ ASSERT_EFI_ERROR (Status);

+ } else {

+ ResponseMessage.Data.Response = NULL;

+ }

+

+ if (EFI_ERROR (Status)) {

+ break;

+ }

+

+ Status = Context->Http->Response (Context->Http, &Context-
ResponseToken);
+ if (EFI_ERROR (Status)) {

+ break;

+ }

+

+ Status = WaitForCompletion (Context, &gResponseCallbackComplete);

+ if (EFI_ERROR (Status) && ResponseMessage.HeaderCount) {

+ Status = EFI_SUCCESS;

+ }

+

+ if (EFI_ERROR (Status)) {

+ Context->Http->Cancel (Context->Http, &Context->ResponseToken);

+ break;

+ }

+

+ if (!Context->ContentDownloaded) {

+ Redirection =

+ ((ResponseData.StatusCode >=
HTTP_STATUS_300_MULTIPLE_CHOICES)
&&

+ (ResponseData.StatusCode <=
HTTP_STATUS_307_TEMPORARY_REDIRECT)

+ ) ||

+ (ResponseData.StatusCode ==
HTTP_STATUS_308_PERMANENT_REDIRECT);

+

+ if (Redirection) {

+ //

+ // Need to repeat the request with new Location (server
redirected).

+ //

+ Context->Status = REQ_NEED_REPEAT;

+

+ Header = HttpFindHeader (

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ "Location"

+ );

+ if (Header) {

+ Status = SetHostURI (Header->FieldValue, Context,
DownloadUrl);

+ if (Status == EFI_NO_MAPPING) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),

+ mHttpHiiHandle, Context->ServerAddrAndProto,

+ L"Recursive HTTP server relocation",

+ Context->URI

+ );

+ }

+ } else {

+ //

+ // Bad reply from the server. Server must specify the
location.

+ // Indicate that resource was not found, and no body
collected.

+ //

+ Status = EFI_NOT_FOUND;

+ }

+

+ Context->Http->Cancel (Context->Http,
&Context->ResponseToken);

+ break;

+ }

+

+ //

+ // Init message-body parser by header information.

+ //

+ if (!MsgParser) {

+ Status = HttpInitMsgParser (

+ ResponseMessage.Data.Request->Method,

+ ResponseData.StatusCode,

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ ParseMsg,

+ Context,

+ &MsgParser

+ );

+ if (EFI_ERROR (Status)) {

+ break;

+ }

+ }

+

+ //

+ // If it is a trunked message, rely on the parser.

+ //

+ Header = HttpFindHeader (

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ "Transfer-Encoding"

+ );

+ IsTrunked = (Header && !AsciiStrCmp (Header->FieldValue,
"chunked"));

+

+ HttpGetEntityLength (MsgParser, &Context->ContentLength);

+

+ if (ResponseData.StatusCode >= HTTP_STATUS_400_BAD_REQUEST
&&

+ (ResponseData.StatusCode !=
HTTP_STATUS_308_PERMANENT_REDIRECT)

+ ) {

+ //

+ // Server reported an error via Response code.

+ // Collect the body if any.

+ //

+ if (!gHttpError) {

+ gHttpError = TRUE;

+

+ Desc = ErrStatusDesc[ResponseData.StatusCode -

+ HTTP_STATUS_400_BAD_REQUEST];

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),

+ mHttpHiiHandle, Context->ServerAddrAndProto,

+ Desc,

+ Context->URI

+ );

+

+ //

+ // This gives an RFC HTTP error.

+ //

+ Context->Status = ShellStrToUintn (Desc);

+ }

+ }

+ }

+

+ // Do NOT try to parse an empty body.

+ if (ResponseMessage.BodyLength || IsTrunked) {

+ Status = HttpParseMessageBody (

+ MsgParser,

+ ResponseMessage.BodyLength,

+ ResponseMessage.Body

+ );

+ }

+ } while (!HttpIsMessageComplete (MsgParser) &&

+ !EFI_ERROR (Status) &&

+ ResponseMessage.BodyLength

+ );

+

+ SHELL_FREE_NON_NULL (MsgParser);

+ if (Context->ResponseToken.Event) {

+ gBS->CloseEvent (Context->ResponseToken.Event);

+ ZeroMem (&Context->ResponseToken, sizeof (Context-
ResponseToken));

+ }

+

+ return Status;

+}

+

+/**

+ Worker function that downloads the data of a file from an HTTP
server
given

+ the path of the file and its size.

+

+ @param[in] Context A pointer to the HTTP download
context.

+

+ @retval EFI_SUCCESS The file was downloaded.

+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.

+ #retval EFI_HTTP_ERROR The server returned a valid HTTP
error.

+ Examine the mLocalFilePath file

+ to get error body.

+ @retval Others The downloading of the file from the
server

+ failed.

+

+**/

+STATIC

+EFI_STATUS

+DownloadFile (

+ IN DOWNLOAD_CONTEXT *Context

+ )

+{

+ EFI_STATUS Status;

+ CHAR16 *DownloadUrl;

+ UINTN UrlSize;

+

+ ASSERT (Context);

+ if (!Context) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ DownloadUrl = NULL;

+

+ Context->Buffer = AllocatePool (Context->BufferSize);

+ if (!Context->Buffer) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto ON_EXIT;

+ }

+

+ //

+ // OPEN FILE

+ //

+ if (!EFI_ERROR (ShellFileExists (mLocalFilePath))) {

+ ShellDeleteFileByName (mLocalFilePath);

+ }

+

+ Status = ShellOpenFileByName (

+ mLocalFilePath,

+ &mFileHandle,

+ EFI_FILE_MODE_CREATE |

+ EFI_FILE_MODE_WRITE |

+ EFI_FILE_MODE_READ,

+ 0);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),

+ mHttpHiiHandle, HTTP_APP_NAME, mLocalFilePath

+ );

+ goto ON_EXIT;

+ }

+

+ do {

+ SHELL_FREE_NON_NULL (DownloadUrl);

+ UrlSize = 0;

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ Context->ServerAddrAndProto,

+ StrLen (Context->ServerAddrAndProto)

+ );

+ if (Context->URI[0] != L'/') {

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ L"/",

+ StrLen (Context->ServerAddrAndProto)

+ );

+ }

+

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ Context->URI,

+ StrLen (Context->URI));

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_DOWNLOADING),

+ mHttpHiiHandle, DownloadUrl);

+

+ Status = SendRequest (Context, DownloadUrl);

+ if (Status) {

+ goto ON_EXIT;

+ }

+

+ Status = GetResponse (Context, DownloadUrl);

+

+ if (Status) {

+ goto ON_EXIT;

+ }

+

+ } while (Context->Status == REQ_NEED_REPEAT);

+

+ if (Context->Status) {

+ Status = ENCODE_ERROR (Context->Status);

+ }

+

+ON_EXIT:

+ //

+ // CLOSE FILE

+ //

+ if (mFileHandle) {

+ if (EFI_ERROR (Status) && !(Context->Flags & DL_FLAG_KEEP_BAD)) {

+ ShellDeleteFile (&mFileHandle);

+ } else {

+ ShellCloseFile (&mFileHandle);

+ }

+ }

+

+ SHELL_FREE_NON_NULL (DownloadUrl);

+ SHELL_FREE_NON_NULL (Context->Buffer);

+

+ return Status;

+}

+

+/**

+ Retrive HII package list from ImageHandle and publish to HII
database.

+

+ @param ImageHandle The image handle of the process.

+

+ @return HII handle.

+**/

+EFI_HII_HANDLE

+InitializeHiiPackage (

+ EFI_HANDLE ImageHandle

+ )

+{

+ EFI_STATUS Status;

+ EFI_HII_PACKAGE_LIST_HEADER *PackageList;

+ EFI_HII_HANDLE HiiHandle;

+

+ //

+ // Retrieve HII package list from ImageHandle

+ //

+ Status = gBS->OpenProtocol (

+ ImageHandle,

+ &gEfiHiiPackageListProtocolGuid,

+ (VOID **)&PackageList,

+ ImageHandle,

+ NULL,

+ EFI_OPEN_PROTOCOL_GET_PROTOCOL

+ );

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return NULL;

+ }

+

+ //

+ // Publish HII package list to HII Database.

+ //

+ Status = gHiiDatabase->NewPackageList (

+ gHiiDatabase,

+ PackageList,

+ NULL,

+ &HiiHandle

+ );

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return NULL;

+ }

+

+ return HiiHandle;

+}

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
new file mode 100644
index 000000000000..5c7b90e8cab4
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
@@ -0,0 +1,89 @@
+/** @file

+ Header file for 'http' command functions.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
<BR>

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#ifndef _HTTP_H_

+#define _HTTP_H_

+

+#include <Uefi.h>

+

+#include <Library/BaseLib.h>

+#include <Library/BaseMemoryLib.h>

+#include <Library/DebugLib.h>

+#include <Library/HiiLib.h>

+#include <Library/HttpLib.h>

+#include <Library/MemoryAllocationLib.h>

+#include <Library/NetLib.h>

+#include <Library/PrintLib.h>

+#include <Library/ShellLib.h>

+#include <Library/UefiBootServicesTableLib.h>

+#include <Library/UefiHiiServicesLib.h>

+#include <Library/UefiLib.h>

+#include <Library/UefiRuntimeServicesTableLib.h>

+

+#include <Protocol/HiiPackageList.h>

+#include <Protocol/HttpUtilities.h>

+#include <Protocol/ServiceBinding.h>

+

+#define HTTP_APP_NAME L"http"

+

+#define REQ_OK 0

+#define REQ_NEED_REPEAT 1

+

+// Download Flags

+#define DL_FLAG_TIME BIT0 // Show elapsed time.

+#define DL_FLAG_KEEP_BAD BIT1 // Keep files even if download failed.

+

+extern EFI_HII_HANDLE mHttpHiiHandle;

+

+typedef struct {

+ UINTN ContentDownloaded;

+ UINTN ContentLength;

+ UINTN LastReportedNbOfBytes;

+ UINTN BufferSize;

+ UINTN Status;

+ UINTN Flags;

+ UINT8 *Buffer;

+ CHAR16 *ServerAddrAndProto;

+ CHAR16 *URI;

+ EFI_HTTP_TOKEN ResponseToken;

+ EFI_HTTP_TOKEN RequestToken;

+ EFI_HTTP_PROTOCOL *Http;

+} DOWNLOAD_CONTEXT;

+

+/**

+ Function for 'http' command.

+

+ @param[in] ImageHandle The image handle.

+ @param[in] SystemTable The system table.

+

+ @retval SHELL_SUCCESS Command completed successfully.

+ @retval SHELL_INVALID_PARAMETER Command usage error.

+ @retval SHELL_ABORTED The user aborts the operation.

+ @retval value Unknown error.

+**/

+SHELL_STATUS

+RunHttp (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ );

+

+/**

+ Retrive HII package list from ImageHandle and publish to HII
database.

+

+ @param ImageHandle The image handle of the process.

+

+ @return HII handle.

+**/

+EFI_HII_HANDLE

+InitializeHiiPackage (

+ EFI_HANDLE ImageHandle

+ );

+#endif // _HTTP_H_

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
new file mode 100644
index 000000000000..00cf05deeb5c
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
@@ -0,0 +1,117 @@
+// /**

+//

+// (C) Copyright 2015-2016 Hewlett Packard Enterprise Development
LP<BR>

+// Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.
<BR>

+// Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+// SPDX-License-Identifier: BSD-2-Clause-Patent

+//

+// Module Name:

+//

+// Http.uni

+//

+// Abstract:

+//

+// String definitions for UEFI Shell HTTP command

+//

+//

+// **/

+

+/=#

+

+#langdef en-US "english"

+

+#string STR_GEN_TOO_MANY #language en-US "%H%s%N: Too
many
arguments. Try help http.\r\n"

+#string STR_GEN_TOO_FEW #language en-US "%H%s%N: Too few
arguments. Try help http.\r\n"

+#string STR_GEN_PARAM_INV #language en-US "%H%s%N: Invalid
argument - '%H%s%N'. Try help http.\r\n"

+#string STR_GEN_PROBLEM #language en-US "%H%s%N: Unknown
flag -
'%H%s%N'. Try help http.\r\n"

+#string STR_GEN_FILE_OPEN_FAIL #language en-US "%H%s%N: Cannot
open
file - '%H%s%N'\r\n"

+#string STR_GEN_CRLF #language en-US "\r\n"

+

+#string STR_HTTP_ERR_NO_NIC #language en-US "No network
interface
card found.\r\n"

+#string STR_HTTP_ERR_NIC_NAME #language en-US "Failed to get the
name
of the network interface card number %d - %r\r\n"

+#string STR_HTTP_ERR_OPEN_PROTOCOL #language en-US "Unable to
open
HTTP protocol on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_CONFIGURE #language en-US "Unable to
configure
HTTP protocol on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_DOWNLOAD #language en-US "Unable to
download
the file '%H%s%N' on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_WRITE #language en-US "Unable to write
into
file
'%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_NIC_NOT_FOUND #language en-US "Network
Interface
Card '%H%s%N' not found.\r\n"

+#string STR_HTTP_ERR_STATUSCODE #language en-US "\r'%H%s%N'
reports
'%s' for '%H%s%N' \r\n"

+#string STR_HTTP_DOWNLOADING #language en-US "Downloading
'%H%s%N'\r\n"

+

+#string STR_GET_HELP_HTTP #language en-US ""

+".TH http 0 "Download a file from HTTP server."\r\n"

+".SH NAME\r\n"

+"Download a file from HTTP server.\r\n"

+".SH SYNOPSIS\r\n"

+" \r\n"

+"HTTP [-i interface] [-l port] [-t timeout] [-s size] [-m] [-k]\r\n"

+" <URL> [localfilepath]\r\n"

+".SH OPTIONS\r\n"

+" \r\n"

+" -i interface - Specifies an adapter name, i.e., eth0.\r\n"

+" -k Keep the downloaded file even if there was an
error.\r\n"

+" If this parameter is not used, the file will be
deleted.\r\n"

+" -l port - Specifies the local port number. Default value
is 0\r\n"

+" and the port number is automatically
assigned.\r\n"

+" -m Measure and report download time (in seconds).
\r\n"

+" -s size The size of the download buffer for a chunk, in
bytes.\r\n"

+" Default is 32K. Note that larger buffer does
not imply\r\n"

+" better speed.\r\n"

+" -t timeout - The number of seconds to wait for completion
of\r\n"

+" requests and responses. Default is 0 which is
'automatic'.\r\n"

+" %HURL%N\r\n"

+" Two types of providing of URLs are supported:\r\n"

+" 1. tftp-like, where host and http_uri are separate
parameters\r\n"

+" (example: host /host_uri), and\r\n\"

+" 2. wget-like, where host and host_uri is one parameter.\r\n"

+" (example: host/host_uri)\r\n"

+"\r\n"

+" host - Specifies HTTP Server address.\r\n

+ Can be either IPv4 address or 'http (or
https)://addr'\r\n

+ Can use addresses resolvable by DNS as well.
\r\n

+ Port can be specified after ':' if needed.
\r\n

+ By default port 80 is used.\r\n"

+" http_uri - HTTP server URI to download the file.\r\n"

+"\r\n"

+" localfilepath - Local destination file path.\r\n"

+".SH DESCRIPTION\r\n"

+" \r\n"

+"NOTES:\r\n"

+" 1. The HTTP command allows geting of the file specified by its
'http_uri'\r\n"

+" path from the HTTP server specified by its 'host' IPv4 address.
If
the\r\n"

+" optional 'localfilepath' parameter is provided, the downloaded
file
is\r\n"

+" stored locally using the provided file path. If the local file
path is\r\n"

+" not specified, the file is stored in the current directory
using the
file\r\n"

+" server's name.\r\n"

+" 2. Before using the HTTP command, the network interface intended
to
be\r\n"

+" used to retrieve the file must be configured. This
configuration may
be\r\n"

+" done by means of the 'ifconfig' command.\r\n"

+" 3. If a network interface is defined with the '-i' option then
only
this\r\n"

+" interface will be used to retrieve the remote file. Otherwise,
all
network\r\n"

+" interfaces are tried in the order they have been discovered
during
the\r\n"

+" DXE phase.\r\n"

+".SH EXAMPLES\r\n"

+" \r\n"

+"EXAMPLES:\r\n"

+" * To get the file "dir1/file1.dat" from the HTTP server
192.168.1.1, port
8080,
and\r\n"

+" store it as file2.dat in the current directory (use tftp-like
URL format)
:\r\n"

+" fs0:\> http 192.168.1.1:8080 dir1/file1.dat file2.dat\r\n"

+" * To get the file /image.bin via HTTPS from server 192.168.1.1 at
port 443
\r\n"

+" (default HTTPS port), and store it in the current directory:
\r\n"

+" fs0:\> http https://192.168.1.1 image.bin\r\n"

+" To get an index file from http://google.com and place it into
the \r\n"

+" current directory:\r\n"

+" fs0:\> http google.com index.html\r\n"

+".SH RETURNVALUES\r\n"

+" \r\n"

+"RETURN VALUES:\r\n"

+" SHELL_SUCCESS The action was completed as
requested.\r\n"

+" SHELL_INVALID_PARAMETER One of the passed-in parameters was
incorrectly\r\n"

+" formatted or its value was out of
bounds.\r\n"

+" HTTP_ERROR No EFI errors, but the server reported a
status
code\r\n"

+" which should be treated as an error. If
an error body
sent\r\n"

+" by the server, and -k parameter is on
command line,

+" the file wil be saved either as
localfilepath filename,\r\n"

+" or as an URI name in the current
directory.\r\n"

+" If '/' is at the end of the URL, and no
locafilepath
filename\r\n"

+" is given on the command line, the file
will be retrieved
as\r\n"

+" index.html.\r\n"

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
new file mode 100644
index 000000000000..7bd5b46d3997
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
@@ -0,0 +1,53 @@
+/** @file

+ Entrypoint of "http" shell standalone application.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
<BR>

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+#include "Http.h"

+

+/*

+ * String token ID of help message text.

+ * Shell supports to find help message in the resource section of an

+ * application image if * .MAN file is not found.

+ * This global variable is added to make build tool recognizes

+ * that the help string is consumed by user and then build tool will

+ * add the string into the resource section.

+ * Thus the application can use '-?' option to show help message in
Shell.

+ */

+GLOBAL_REMOVE_IF_UNREFERENCED

+EFI_STRING_ID mStringHelpTokenId = STRING_TOKEN
(STR_GET_HELP_HTTP);

+

+/**

+ Entry point of Http standalone application.

+

+ @param ImageHandle The image handle of the process.

+ @param SystemTable The EFI System Table pointer.

+

+ @retval EFI_SUCCESS Http command is executed sucessfully.

+ @retval EFI_ABORTED HII package was failed to initialize.

+ @retval others Other errors when executing http
command.

+**/

+EFI_STATUS

+EFIAPI

+HttpAppInitialize (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+ mHttpHiiHandle = InitializeHiiPackage (ImageHandle);

+ if (mHttpHiiHandle == NULL) {

+ return EFI_ABORTED;

+ }

+

+ Status = (EFI_STATUS)RunHttp (ImageHandle, SystemTable);

+

+ HiiRemovePackages (mHttpHiiHandle);

+

+ return Status;

+}

diff --git
a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
new file mode 100644
index 000000000000..d08d47fb37d5
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
@@ -0,0 +1,58 @@
+## @file

+# Provides Shell 'http' standalone application.

+#

+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
<BR>

+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010006

+ BASE_NAME = http

+ FILE_GUID =
56B00FB7-91D2-869B-CE5C-26CD1A89C73C

+ MODULE_TYPE = UEFI_APPLICATION

+ VERSION_STRING = 1.0

+ ENTRY_POINT = HttpAppInitialize

+#

+# This flag specifies whether HII resource section is generated into
PE
image.

+#

+ UEFI_HII_RESOURCE_SECTION = TRUE

+

+[Sources.common]

+ Http.c

+ HttpApp.c

+ Http.h

+ Http.uni

+

+[Packages]

+ EmbeddedPkg/EmbeddedPkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ MdePkg/MdePkg.dec

+ NetworkPkg/NetworkPkg.dec

+ ShellPkg/ShellPkg.dec

+

+[LibraryClasses]

+ BaseLib

+ BaseMemoryLib

+ DebugLib

+ FileHandleLib

+ HiiLib

+ HttpLib

+ MemoryAllocationLib

+ NetLib

+ ShellLib

+ UefiApplicationEntryPoint

+ UefiBootServicesTableLib

+ UefiHiiServicesLib

+ UefiLib

+ UefiRuntimeServicesTableLib

+

+[Protocols]

+ gEfiHiiPackageListProtocolGuid ## CONSUMES

+ gEfiHttpProtocolGuid ## CONSUMES

+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES

+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES

diff --git
a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.c
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.c
new file mode 100644
index 000000000000..ba654749a075
--- /dev/null
+++
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.c
@@ -0,0 +1,134 @@
+/** @file

+ Produce "http" shell dynamic command.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
<BR>

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+#include <Protocol/ShellDynamicCommand.h>

+#include "Http.h"

+

+/**

+ This is the shell command handler function pointer callback type.
This

+ function handles the command when it is invoked in the shell.

+

+ @param[in] This The instance of the

+ EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.

+ @param[in] SystemTable The pointer to the system table.

+ @param[in] ShellParameters The parameters associated with the
command.

+ @param[in] Shell The instance of the shell protocol
used in

+ the context of processing this
command.

+

+ @return EFI_SUCCESS the operation was sucessful

+ @return other the operation failed.

+**/

+SHELL_STATUS

+EFIAPI

+HttpCommandHandler (

+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,

+ IN EFI_SYSTEM_TABLE *SystemTable,

+ IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,

+ IN EFI_SHELL_PROTOCOL *Shell

+ )

+{

+ gEfiShellParametersProtocol = ShellParameters;

+ gEfiShellProtocol = Shell;

+

+ return RunHttp (gImageHandle, SystemTable);

+}

+

+/**

+ This is the command help handler function pointer callback type.
This

+ function is responsible for displaying help information for the
associated

+ command.

+

+ @param[in] This The instance of the
EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.

+ @param[in] Language The pointer to the language string to use.

+

+ @return string Pool allocated help string, must be freed by
caller

+**/

+CHAR16 *

+EFIAPI

+HttpCommandGetHelp (

+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,

+ IN CONST CHAR8 *Language

+ )

+{

+ return HiiGetString(mHttpHiiHandle,

+ STRING_TOKEN (STR_GET_HELP_HTTP),

+ Language);

+}

+

+EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL mHttpDynamicCommand
= {

+ HTTP_APP_NAME,

+ HttpCommandHandler,

+ HttpCommandGetHelp

+};

+

+/**

+ Entry point of Http Dynamic Command.

+

+ Produce the DynamicCommand protocol to handle "http" command.

+

+ @param ImageHandle The image handle of the process.

+ @param SystemTable The EFI System Table pointer.

+

+ @retval EFI_SUCCESS Http command is executed sucessfully.

+ @retval EFI_ABORTED HII package was failed to initialize.

+ @retval others Other errors when executing http
command.

+**/

+EFI_STATUS

+EFIAPI

+HttpCommandInitialize (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+

+ mHttpHiiHandle = InitializeHiiPackage (ImageHandle);

+ if (mHttpHiiHandle == NULL) {

+ return EFI_ABORTED;

+ }

+

+ Status = gBS->InstallProtocolInterface (

+ &ImageHandle,

+ &gEfiShellDynamicCommandProtocolGuid,

+ EFI_NATIVE_INTERFACE,

+ &mHttpDynamicCommand

+ );

+ ASSERT_EFI_ERROR (Status);

+ return Status;

+}

+

+/**

+ Http driver unload handler.

+

+ @param ImageHandle The image handle of the process.

+

+ @retval EFI_SUCCESS The image is unloaded.

+ @retval Others Failed to unload the image.

+**/

+EFI_STATUS

+EFIAPI

+HttpUnload (

+ IN EFI_HANDLE ImageHandle

+)

+{

+ EFI_STATUS Status;

+ Status = gBS->UninstallProtocolInterface (

+ ImageHandle,

+ &gEfiShellDynamicCommandProtocolGuid,

+ &mHttpDynamicCommand

+ );

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ HiiRemovePackages (mHttpHiiHandle);

+

+ return EFI_SUCCESS;

+}

diff --git
a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.in
f
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.in
f
new file mode 100644
index 000000000000..5d46ee2384d5
--- /dev/null
+++
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.in
f
@@ -0,0 +1,63 @@
+## @file

+# Provides Shell 'http' dynamic command.

+#

+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
<BR>

+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010006

+ BASE_NAME = httpDynamicCommand

+ FILE_GUID =
19618BCE-55AE-09C6-37E9-4CE04084C7A1

+ MODULE_TYPE = DXE_DRIVER

+ VERSION_STRING = 1.0

+ ENTRY_POINT = HttpCommandInitialize

+ UNLOAD_IMAGE = HttpUnload

+#

+# This flag specifies whether HII resource section is generated into
PE
image.

+#

+ UEFI_HII_RESOURCE_SECTION = TRUE

+

+[Sources.common]

+ Http.c

+ HttpDynamicCommand.c

+ Http.h

+ Http.uni

+

+[Packages]

+ EmbeddedPkg/EmbeddedPkg.dec

+ MdePkg/MdePkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ NetworkPkg/NetworkPkg.dec

+ ShellPkg/ShellPkg.dec

+

+[LibraryClasses]

+ BaseLib

+ BaseMemoryLib

+ DebugLib

+ FileHandleLib

+ HiiLib

+ HttpLib

+ MemoryAllocationLib

+ NetLib

+ ShellLib

+ UefiBootServicesTableLib

+ UefiDriverEntryPoint

+ UefiHiiServicesLib

+ UefiLib

+ UefiRuntimeServicesTableLib

+

+[Protocols]

+ gEfiHiiPackageListProtocolGuid ## CONSUMES

+ gEfiHttpProtocolGuid ## CONSUMES

+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES

+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES

+ gEfiShellDynamicCommandProtocolGuid ## PRODUCES

+

+[DEPEX]

+ TRUE

diff --git a/ShellPkg/Include/Guid/ShellLibHiiGuid.h
b/ShellPkg/Include/Guid/ShellLibHiiGuid.h
index 5da9128333a4..6e328b460d8c 100644
--- a/ShellPkg/Include/Guid/ShellLibHiiGuid.h
+++ b/ShellPkg/Include/Guid/ShellLibHiiGuid.h
@@ -59,6 +59,10 @@
0x738a9314, 0x82c1, 0x4592, { 0x8f, 0xf7, 0xc1, 0xbd, 0xf1, 0xb2,
0x0e,
0xd4 }
\

}



+#define SHELL_HTTP_HII_GUID \

+ { \

+ 0x390f84b3, 0x221c, 0x4d9e, { 0xb5, 0x06, 0x6d, 0xb9, 0x42, 0x3e,
0x0a,
0x7e } \

+ }



#define SHELL_BCFG_HII_GUID \

{ \

@@ -75,6 +79,7 @@ extern EFI_GUID gShellLevel3HiiGuid;
extern EFI_GUID gShellNetwork1HiiGuid;

extern EFI_GUID gShellNetwork2HiiGuid;

extern EFI_GUID gShellTftpHiiGuid;

+extern EFI_GUID gShellHttpHiiGuid;

extern EFI_GUID gShellBcfgHiiGuid;



#endif

diff --git a/ShellPkg/ShellPkg.dec b/ShellPkg/ShellPkg.dec
index d0843d338126..7b2d1230bd2c 100644
--- a/ShellPkg/ShellPkg.dec
+++ b/ShellPkg/ShellPkg.dec
@@ -53,6 +53,7 @@ [Guids]
gShellNetwork1HiiGuid = {0xf3d301bb, 0xf4a5, 0x45a8,
{0xb0, 0xb7,
0xfa,
0x99, 0x9c, 0x62, 0x37, 0xae}}

gShellNetwork2HiiGuid = {0x174b2b5, 0xf505, 0x4b12,
{0xaa, 0x60,
0x59,
0xdf, 0xf8, 0xd6, 0xea, 0x37}}

gShellTftpHiiGuid = {0x738a9314, 0x82c1, 0x4592,
{0x8f, 0xf7, 0xc1,
0xbd, 0xf1, 0xb2, 0x0e, 0xd4}}

+ gShellHttpHiiGuid = {0x390f84b3, 0x221c, 0x4d9e,
{0xb5, 0x06,
0x6d,
0xb9, 0x42, 0x3e, 0x0a, 0x7e}}

gShellBcfgHiiGuid = {0x5f5f605d, 0x1583, 0x4a2d,
{0xa6, 0xb2, 0xeb,
0x12, 0xda, 0xb4, 0xa2, 0xb6}}

gShellAcpiViewHiiGuid = {0xda8ccdf4, 0xed8f, 0x4ffc,
{0xb5, 0xef,
0x2e,
0xf5, 0x5e, 0x24, 0x93, 0x2a}}

# FILE_GUID as defined in ShellPkg/Application/Shell/Shell.inf

diff --git a/ShellPkg/ShellPkg.dsc b/ShellPkg/ShellPkg.dsc
index 86e9f1e0040d..c42bc9464a0f 100644
--- a/ShellPkg/ShellPkg.dsc
+++ b/ShellPkg/ShellPkg.dsc
@@ -139,6 +139,11 @@ [Components]
gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

}

ShellPkg/DynamicCommand/TftpDynamicCommand/TftpApp.inf

+
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand
.inf {

+ <PcdsFixedAtBuild>

+ gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

+ }

+ ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf

ShellPkg/DynamicCommand/DpDynamicCommand/DpDynamicCommand.inf
{

<PcdsFixedAtBuild>

gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

--
2.26.2.266.ge870325ee8


Maciej Rabeda
 

Hi Vladimir,

I cannot apply the patch via 'git am'.
Is your git configured in a manner described here?
https://github.com/tianocore/tianocore.github.io/wiki/Laszlo's-unkempt-git-guide-for-edk2-contributors-and-maintainers

Laszlo,

Were you able to apply this patch from .eml file?

Thanks,
Maciej

On 17-Aug-20 17:46, Vladimir Olovyannikov wrote:
Hi Gao,

Thanks for reviewing.
As you suggest, I will postpone the next patchset submission until
NetworkPkg maintainer(s) review.

-----Original Message-----
From: Gao, Zhichao <zhichao.gao@...>
Sent: Sunday, August 16, 2020 6:48 PM
To: Vladimir Olovyannikov <vladimir.olovyannikov@...>;
devel@edk2.groups.io
Cc: Laszlo Ersek <lersek@...>; Samer El-Haj-Mahmoud <Samer.El-
Haj-Mahmoud@...>; Maciej Rabeda
<maciej.rabeda@...>; Wu, Jiaxin <jiaxin.wu@...>; Fu,
Siyuan <@sfu5>; Ni, Ray <ray.ni@...>; Gao, Liming
<liming.gao@...>; Nd <nd@...>
Subject: RE: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

Sorry for the delay reply.

See below:

-----Original Message-----
From: Vladimir Olovyannikov <vladimir.olovyannikov@...>
Sent: Tuesday, July 28, 2020 12:49 AM
To: devel@edk2.groups.io
Cc: Laszlo Ersek <lersek@...>; Vladimir Olovyannikov
<vladimir.olovyannikov@...>; Samer El-Haj-Mahmoud
<Samer.El-
Haj-Mahmoud@...>; Gao, Zhichao <zhichao.gao@...>; Maciej
Rabeda <maciej.rabeda@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu,
Siyuan <@sfu5>; Ni, Ray <ray.ni@...>; Gao, Liming
<liming.gao@...>; Nd <nd@...>
Subject: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand
Introduce an http client utilizing EDK2 HTTP protocol, to
allow fast image downloading from http/https servers.
HTTP download speed is usually faster than tftp.
The client is based on the same approach as tftp dynamic command, and
uses the same UEFI Shell command line parameters. This makes it easy
integrating http into existing UEFI Shell scripts.
Note that to enable HTTP download, feature Pcd
gEfiNetworkPkgTokenSpaceGuid.PcdAllowHttpConnections must
be set to TRUE.
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2860

Signed-off-by: Vladimir Olovyannikov
<vladimir.olovyannikov@...>
CC: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>
CC: Laszlo Ersek <lersek@...>
Cc: Zhichao Gao <zhichao.gao@...>
Cc: Maciej Rabeda <maciej.rabeda@...>
Cc: Jiaxin Wu <jiaxin.wu@...>
Cc: Siyuan Fu <@sfu5>
Cc: Ray Ni <ray.ni@...>
Cc: Liming Gao <liming.gao@...>
Cc: Nd <nd@...>
---
CryptoPkg/Library/OpensslLib/openssl | 2 +-
.../DynamicCommand/HttpDynamicCommand/Http.c | 1715
+++++++++++++++++
.../DynamicCommand/HttpDynamicCommand/Http.h | 89 +
.../HttpDynamicCommand/Http.uni | 117 ++
.../HttpDynamicCommand/HttpApp.c | 53 +
.../HttpDynamicCommand/HttpApp.inf | 58 +
.../HttpDynamicCommand/HttpDynamicCommand.c | 134 ++
.../HttpDynamicCommand/HttpDynamicCommand.inf | 63 +
ShellPkg/Include/Guid/ShellLibHiiGuid.h | 5 +
ShellPkg/ShellPkg.dec | 1 +
ShellPkg/ShellPkg.dsc | 5 +
11 files changed, 2241 insertions(+), 1 deletion(-)
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand
.c
create mode 100644
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand
.inf
diff --git a/CryptoPkg/Library/OpensslLib/openssl
b/CryptoPkg/Library/OpensslLib/openssl
index e2e09d9fba11..c3656cc594da 160000
--- a/CryptoPkg/Library/OpensslLib/openssl
+++ b/CryptoPkg/Library/OpensslLib/openssl
@@ -1 +1 @@
-Subproject commit e2e09d9fba1187f8d6aafaa34d4172f56f1ffb72
+Subproject commit c3656cc594daac8167721dde7220f0e59ae146fc
diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
new file mode 100644
index 000000000000..0565b07c3570
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
@@ -0,0 +1,1715 @@
+/** @file

+ The implementation for the 'http' Shell command.

+

+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
<BR>
+ (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved. <BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+**/

+

+#include "Http.h"

+

+#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32

+EFI_HII_HANDLE mHttpHiiHandle;

+

+/*

+ Constant strings and definitions related to the message

+ indicating the amount of progress in the dowloading of a HTTP
file.
+*/

+

+// Number of steps in the progression slider

+#define HTTP_PROGRESS_SLIDER_STEPS \

+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 3)

+

+// Size in number of characters plus one (final zero) of the message
to
+// indicate the progress of an HTTP download. The format is
"[(progress
slider:
+// 40 characters)] (nb of KBytes downloaded so far: 7 characters)
Kb".
There
+// are thus the number of characters in HTTP_PROGR_FRAME[] plus 11
characters

+// (2 // spaces, "Kb" and seven characters for the number of KBytes).

+#define HTTP_PROGRESS_MESSAGE_SIZE \

+ ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) + 12)

+

+//

+// Buffer size. Note that larger buffer does not mean better speed!

+//

+#define DEFAULT_BUF_SIZE SIZE_32KB

+#define MAX_BUF_SIZE SIZE_4MB

+

+#define MIN_PARAM_COUNT 2

+#define MAX_PARAM_COUNT 4

+

+#define TIMER_MAX_TIMEOUT_S 10

+

+// File name to use when URI ends with "/"

+#define DEFAULT_HTML_FILE L"index.html"

+#define DEFAULT_HTTP_PROTO L"http"

+

+// String to delete the HTTP progress message to be able to update it
:
+// (HTTP_PROGRESS_MESSAGE_SIZE-1) '\b'

+#define HTTP_PROGRESS_DEL \

+
L"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\
b\b\b\
+\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"

+

+#define HTTP_KB L"\b\b\b\b\b\b\b\b\b\b"

+// Frame for the progression slider

+#define HTTP_PROGR_FRAME L"[
]"
+

+// String descriptions for server errors

+STATIC CONST CHAR16 *ErrStatusDesc[] =

+{

+ L"400 Bad Request",

+ L"401 Unauthorized",

+ L"402 Payment required",

+ L"403 Forbidden",

+ L"404 Not Found",

+ L"405 Method not allowed",

+ L"406 Not acceptable",

+ L"407 Proxy authentication required",

+ L"408 Request time out",

+ L"409 Conflict",

+ L"410 Gone",

+ L"411 Length required",

+ L"412 Precondition failed",

+ L"413 Request entity too large",

+ L"414 Request URI to large",

+ L"415 Unsupported media type",

+ L"416 Requested range not satisfied",

+ L"417 Expectation failed",

+ L"500 Internal server error",

+ L"501 Not implemented",

+ L"502 Bad gateway",

+ L"503 Service unavailable",

+ L"504 Gateway timeout",

+ L"505 HTTP version not supported"

+};

+

+// Local File Handle

+STATIC SHELL_FILE_HANDLE mFileHandle = NULL;

+

+// Path of the local file, Unicode encoded

+STATIC CONST CHAR16 *mLocalFilePath;

+

+STATIC BOOLEAN gRequestCallbackComplete = FALSE;

+STATIC BOOLEAN gResponseCallbackComplete = FALSE;

+

+STATIC BOOLEAN gHttpError;

+

+/**

+ Cleans off leading and trailing spaces and tabs.

+

+ @param[in] String pointer to the string to trim them off.

+**/

+STATIC

+EFI_STATUS

+TrimSpaces (

+ IN CHAR16 **String

+ )

+{

+ ASSERT(String != NULL);

+ ASSERT(*String!= NULL);

+ //

+ // Remove any spaces and tabs at the beginning of the (*String).

+ //

+ while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {

+ CopyMem (

+ (*String),

+ (*String) + 1,

+ StrSize ((*String)) - sizeof((*String)[0])

+ );

+ }

+

+ //

+ // Remove any spaces and tabs at the end of the (*String).

+ //

+ while ((StrLen (*String) > 0) &&

+ (((*String)[StrLen ((*String)) - 1] == L' ') ||

+ ((*String)[StrLen ((*String)) - 1] == L'\t'))

+ ) {

+ (*String)[StrLen ((*String)) - 1] = CHAR_NULL;

+ }

+

+ return (EFI_SUCCESS);

+}

+

+

+/*

+ * Callbacks for request and response.

+ * We just acknowledge that operation has completed here.

+ */

+STATIC

+VOID

+EFIAPI

+RequestCallback (

+ IN EFI_EVENT Event,

+ IN VOID *Context

+)

+{

+ gRequestCallbackComplete = TRUE;

+}

+

+STATIC

+VOID

+EFIAPI

+ResponseCallback (

+ IN EFI_EVENT Event,

+ IN VOID *Context

+)

+{

+ gResponseCallbackComplete = TRUE;

+}

+

+

+/**

+ Check and convert the UINT16 option values of the 'http' command

+

+ @param[in] ValueStr Value as an Unicode encoded string

+ @param[out] Value UINT16 value

+

+ @return TRUE The value was returned.

+ @return FALSE A parsing error occured.

+**/

+STATIC

+BOOLEAN

+StringToUint16 (

+ IN CONST CHAR16 *ValueStr,

+ OUT UINT16 *Value

+ );

+

+/**

+ Get the name of the NIC.

+

+ @param[in] ControllerHandle The network physical device handle.

+ @param[in] NicNumber The network physical device number.

+ @param[out] NicName Address where to store the NIC name.

+ The memory area has to be at least

+
IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
+ double byte wide.

+

+ @return EFI_SUCCESS The name of the NIC was returned.

+ @return Others The creation of the child for the Managed

+ Network Service failed or the opening of

+ the Managed Network Protocol failed or

+ the operational parameters for the

+ Managed Network Protocol could not be

+ read.

+**/

+STATIC

+EFI_STATUS

+GetNicName (

+ IN EFI_HANDLE ControllerHandle,

+ IN UINTN NicNumber,

+ OUT CHAR16 *NicName

+ );

+

+/**

+ Create a child for the service identified by its service binding
protocol
GUID
+ and get from the child the interface of the protocol identified by
its
GUID.
+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol
GUID
of the
+ service to be created.

+ @param[in] ProtocolGuid GUID of the protocol to be
open.
+ @param[out] ChildHandle Address where the handler
of the
+ created child is returned.
NULL is
+ returned in case of error.

+ @param[out] Interface Address where a pointer to
the
+ protocol interface is
returned in
+ case of success.

+

+ @return EFI_SUCCESS The child was created and the protocol
opened.
+ @return Others Either the creation of the child or the
opening
+ of the protocol failed.

+**/

+STATIC

+EFI_STATUS

+CreateServiceChildAndOpenProtocol (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ OUT EFI_HANDLE *ChildHandle,

+ OUT VOID **Interface

+ );

+

+/**

+ Close the protocol identified by its GUID on the child handle of
the
service
+ identified by its service binding protocol GUID, then destroy the
child
+ handle.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol
GUID
of the
+ service to be destroyed.

+ @param[in] ProtocolGuid GUID of the protocol to be
closed.
+ @param[in] ChildHandle Handle of the child to be
destroyed.
+

+**/

+STATIC

+VOID

+CloseProtocolAndDestroyServiceChild (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ IN EFI_HANDLE ChildHandle

+ );

+

+

+/**

+ Worker function that download the data of a file from an HTTP
server
given
+ the path of the file and its size.

+

+ @param[in] Context A pointer to the download context.

+

+ @retval EFI_SUCCESS The file was downloaded.

+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.

+ @retval Others The downloading of the file

+ from the server failed.

+

+**/

+STATIC

+EFI_STATUS

+DownloadFile (

+ IN DOWNLOAD_CONTEXT *Context

+ );

+

+STATIC CONST SHELL_PARAM_ITEM ParamList[] = {

+ {L"-i", TypeValue},

+ {L"-k", TypeFlag},

+ {L"-l", TypeValue},

+ {L"-s", TypeValue},

+ {L"-t", TypeValue},

+ {NULL , TypeMax}

+ };

+

+/**

+ Function for 'http' command.

+

+ @param[in] ImageHandle Handle to the Image (NULL if Internal).

+ @param[in] SystemTable Pointer to the System Table (NULL if
Internal).
+

+ @return SHELL_SUCCESS The 'http' command completed
successfully.
+ @return SHELL_ABORTED The Shell Library initialization
failed.
+ @return SHELL_INVALID_PARAMETER At least one of the command's
arguments is

+ not valid.

+ @return SHELL_OUT_OF_RESOURCES A memory allocation failed.

+ @return SHELL_NOT_FOUND Network Interface Card not found.

+ @return SHELL_UNSUPPORTED Command was valid, but the server
returned

+ a status code indicating some
error.
+ Examine the file requested for
error body.
+

+**/

+SHELL_STATUS

+RunHttp (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+ LIST_ENTRY *CheckPackage;

+ UINTN ParamCount;

+ UINTN HandleCount;

+ UINTN NicNumber;

+ UINTN InitialSize;

+ UINTN ParamOffset;

+ UINTN StartSize;

+ CHAR16 *ProblemParam;

+ CHAR16
NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];
+ CHAR16 *Walker1;

+ CHAR16 *VStr;

+ CONST CHAR16 *UserNicName;

+ CONST CHAR16 *ValueStr;

+ CONST CHAR16 *RemoteFilePath;

+ CONST CHAR16 *Walker;

+ EFI_HTTP_CONFIG_DATA HttpConfigData;

+ EFI_HTTPv4_ACCESS_POINT IPv4Node;

+ EFI_HANDLE *Handles;

+ EFI_HANDLE ControllerHandle;

+ EFI_HANDLE HttpChildHandle;

+ DOWNLOAD_CONTEXT Context;

+ BOOLEAN NicFound;

+

+ ProblemParam = NULL;

+ RemoteFilePath = NULL;

+ NicFound = FALSE;

+ Handles = NULL;

+

+ //

+ // Initialize the Shell library (we must be in non-auto-init...)

+ //

+ ParamOffset = 0;

+ gHttpError = FALSE;

+

+ Status = ShellInitialize ();

+ if (EFI_ERROR (Status)) {

+ ASSERT_EFI_ERROR (Status);

+ return SHELL_ABORTED;

+ }

+

+ ZeroMem (&Context, sizeof (Context));

+

+ //

+ // Parse the command line.

+ //

+ Status = ShellCommandLineParse (

+ ParamList,

+ &CheckPackage,

+ &ProblemParam,

+ TRUE);
I prefer to put ');' in next line like other functions.

+ if (EFI_ERROR (Status)) {

+ if ((Status == EFI_VOLUME_CORRUPTED) &&

+ (ProblemParam != NULL) ) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mHttpHiiHandle,

+ HTTP_APP_NAME, ProblemParam

+ );

+ SHELL_FREE_NON_NULL (ProblemParam);

+ } else {

+ ASSERT (FALSE);

+ }

+

+ goto Error;

+ }

+

+ //

+ // Check the number of parameters

+ //

+ Status = EFI_INVALID_PARAMETER;

+

+ ParamCount = ShellCommandLineGetCount (CheckPackage);

+ if (ParamCount > MAX_PARAM_COUNT) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY),

+ mHttpHiiHandle, HTTP_APP_NAME

+ );

+ goto Error;

+ }

+

+ if (ParamCount < MIN_PARAM_COUNT) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW),

+ mHttpHiiHandle, HTTP_APP_NAME

+ );

+ goto Error;

+ }

+

+ ZeroMem (&HttpConfigData, sizeof (HttpConfigData));

+ ZeroMem (&IPv4Node, sizeof (IPv4Node));

+ IPv4Node.UseDefaultAddress = TRUE;

+

+ HttpConfigData.HttpVersion = HttpVersion11;

+ HttpConfigData.AccessPoint.IPv4Node = &IPv4Node;

+

+ //

+ // Get the host address (not necessarily IPv4 format)

+ //

+ ValueStr = ShellCommandLineGetRawValue (CheckPackage, 1);

+ if (!ValueStr) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),
mHttpHiiHandle,
+ HTTP_APP_NAME, ValueStr

+ );

+ goto Error;

+ } else {

+ StartSize = 0;

+ if (!StrStr (ValueStr, L"://")) {

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ DEFAULT_HTTP_PROTO,

+ StrLen (DEFAULT_HTTP_PROTO)

+ );

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ L"://",

+ StrLen (L"://")

+ );

+ VStr = (CHAR16 *)ValueStr;

+ } else {

+ VStr = StrStr (ValueStr, L"://") + StrLen (L"://");

+ }

+

+ for (Walker1 = VStr; *Walker1; Walker1++) {

+ if (*Walker1 == L'/') {

+ break;

+ }

+ }

+

+ if (*Walker1 == L'/') {

+ ParamOffset = 1;

+ RemoteFilePath = Walker1;

+ }

+

+ Context.ServerAddrAndProto = StrnCatGrow (

+ &Context.ServerAddrAndProto,

+ &StartSize,

+ ValueStr,

+ StrLen (ValueStr) - StrLen
(Walker1)
+ );

+ if (!Context.ServerAddrAndProto) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+ }

+

+ if (!RemoteFilePath) {

+ RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);

+ if (!RemoteFilePath) {

+ // If no path given, assume just "/"

+ RemoteFilePath = L"/";

+ }

+ }

+

+ TrimSpaces ((CHAR16 **)&RemoteFilePath);

+

+ if (ParamCount == MAX_PARAM_COUNT - ParamOffset) {

+ mLocalFilePath = ShellCommandLineGetRawValue (

+ CheckPackage,

+ MAX_PARAM_COUNT - 1 - ParamOffset);

+ } else {

+ Walker = RemoteFilePath + StrLen (RemoteFilePath);

+ while ((--Walker) >= RemoteFilePath) {

+ if ((*Walker == L'\\') ||

+ (*Walker == L'/' ) ) {
It is better to remove the useless space in the above line at the end of
'if'
condition.

+ break;

+ }

+ }

+

+ mLocalFilePath = Walker + 1;

+ }

+

+ if (!StrLen (mLocalFilePath)) {

+ mLocalFilePath = DEFAULT_HTML_FILE;

+ }

+

+ InitialSize = 0;

+ Context.URI = StrnCatGrow (

+ &Context.URI,

+ &InitialSize,

+ RemoteFilePath,

+ StrLen (RemoteFilePath)

+ );

+ if (!Context.URI) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ //

+ // Get the name of the Network Interface Card to be used if any.

+ //

+ UserNicName = ShellCommandLineGetValue (CheckPackage, L"-i");

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-l");

+ if (ValueStr != NULL) {

+ if (!StringToUint16 (

+ ValueStr,

+ &HttpConfigData.AccessPoint.IPv4Node->LocalPort

+ )

+ ) {
I suggest to align the end ')' of StringToUint16 to align with the
parameters.
And the end ')' of 'if' to go right next to the end ')' of
StringToUint16.
OK, will do.
+ goto Error;

+ }

+ }

+

+ Context.BufferSize = DEFAULT_BUF_SIZE;

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-s");

+ if (ValueStr != NULL) {

+ Context.BufferSize = ShellStrToUintn (ValueStr);

+ if (!Context.BufferSize || Context.BufferSize > MAX_BUF_SIZE) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),

+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr

+ );

+ goto Error;

+ }

+ }

+

+ ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");

+ if (ValueStr != NULL) {

+ HttpConfigData.TimeOutMillisec = (UINT32)ShellStrToUintn
(ValueStr);
I am not sure if it is fine to transfer '-1' as time out value when the
-t with
invalid number string. It depends on the NetworkPkg maintainers' review.

I am OK with other part of the patch. But I still need NetworkPkg
maintainers'
help to review above issue and the implementation.
There is no need to resend another patch if NetworkPkg maintainers are
OK
with above issue and the implementation.
OK, I will postpone submission of the patch until NetworkPkg
maintainter(s) review.
Thanks,
Zhichao
Thank you,
Vladimir
+ }

+

+ //

+ // Locate all HTTP Service Binding protocols

+ //

+ Status = gBS->LocateHandleBuffer (

+ ByProtocol,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ NULL,

+ &HandleCount,

+ &Handles

+ );

+ if (EFI_ERROR (Status) || (HandleCount == 0)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NO_NIC),
mHttpHiiHandle
+ );

+ if (!EFI_ERROR (Status)) {

+ Status = EFI_NOT_FOUND;

+ }

+

+ goto Error;

+ }

+

+ Status = EFI_NOT_FOUND;

+

+ for (NicNumber = 0;

+ (NicNumber < HandleCount) && (Status != EFI_SUCCESS);

+ NicNumber++) {

+ ControllerHandle = Handles[NicNumber];

+

+ Status = GetNicName (ControllerHandle, NicNumber, NicName);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NAME),

+ mHttpHiiHandle, NicNumber, Status

+ );

+ continue;

+ }

+

+ if (UserNicName != NULL) {

+ if (StrCmp (NicName, UserNicName) != 0) {

+ Status = EFI_NOT_FOUND;

+ continue;

+ }

+ NicFound = TRUE;

+ }

+

+ Status = CreateServiceChildAndOpenProtocol (

+ ControllerHandle,

+ &gEfiHttpServiceBindingProtocolGuid,

+ &gEfiHttpProtocolGuid,

+ &HttpChildHandle,

+ (VOID**)&Context.Http

+ );

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_OPEN_PROTOCOL),

+ mHttpHiiHandle, NicName, Status

+ );

+ continue;

+ }

+

+ Status = Context.Http->Configure (Context.Http, &HttpConfigData);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_CONFIGURE),

+ mHttpHiiHandle, NicName, Status

+ );

+ continue;

+ }

+

+ Context.Flags = 0;

+ if (ShellCommandLineGetFlag (CheckPackage, L"-m")) {

+ Context.Flags |= DL_FLAG_TIME;

+ }

+

+ if (ShellCommandLineGetFlag (CheckPackage, L"-k")) {

+ Context.Flags |= DL_FLAG_KEEP_BAD;

+ }

+

+ Status = DownloadFile (&Context);

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle

+ );

+

+ CloseProtocolAndDestroyServiceChild (

+ ControllerHandle,

+ &gEfiHttpServiceBindingProtocolGuid,

+ &gEfiHttpProtocolGuid,

+ HttpChildHandle

+ );

+

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_DOWNLOAD),

+ mHttpHiiHandle, RemoteFilePath, NicName, Status

+ );

+ // If a user aborted the operation, do not try another
controller.
+ if (Status == EFI_ABORTED) {

+ goto Error;

+ }

+ }

+

+ if (gHttpError) {

+ //

+ // This is not related to connection, so no need to repeat with

+ // another interface.

+ //

+ break;

+ }

+ }

+

+ if ((UserNicName != NULL) && (!NicFound)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_NIC_NOT_FOUND),

+ mHttpHiiHandle, UserNicName

+ );

+ }

+

+Error:

+ ShellCommandLineFreeVarList (CheckPackage);

+ SHELL_FREE_NON_NULL (Handles);

+ SHELL_FREE_NON_NULL (Context.ServerAddrAndProto);

+ SHELL_FREE_NON_NULL (Context.URI);

+

+ return Status & ~MAX_BIT;

+}

+

+/**

+ Check and convert the UINT16 option values of the 'http' command

+

+ @param[in] ValueStr Value as an Unicode encoded string

+ @param[out] Value UINT16 value

+

+ @return TRUE The value was returned.

+ @return FALSE A parsing error occured.

+**/

+STATIC

+BOOLEAN

+StringToUint16 (

+ IN CONST CHAR16 *ValueStr,

+ OUT UINT16 *Value

+ )

+{

+ UINTN Val;

+

+ Val = ShellStrToUintn (ValueStr);

+ if (Val > MAX_UINT16) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),

+ mHttpHiiHandle, HTTP_APP_NAME, ValueStr

+ );

+ return FALSE;

+ }

+

+ *Value = (UINT16)Val;

+ return TRUE;

+}

+

+/**

+ Get the name of the NIC.

+

+ @param[in] ControllerHandle The network physical device handle.

+ @param[in] NicNumber The network physical device number.

+ @param[out] NicName Address where to store the NIC name.

+ The memory area has to be at least

+
IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
+ double byte wide.

+

+ @return EFI_SUCCESS The name of the NIC was returned.

+ @return Others The creation of the child for the Managed

+ Network Service failed or the opening of

+ the Managed Network Protocol failed or

+ the operational parameters for the

+ Managed Network Protocol could not be

+ read.

+**/

+STATIC

+EFI_STATUS

+GetNicName (

+ IN EFI_HANDLE ControllerHandle,

+ IN UINTN NicNumber,

+ OUT CHAR16 *NicName

+ )

+{

+ EFI_STATUS Status;

+ EFI_HANDLE MnpHandle;

+ EFI_MANAGED_NETWORK_PROTOCOL *Mnp;

+ EFI_SIMPLE_NETWORK_MODE SnpMode;

+

+ Status = CreateServiceChildAndOpenProtocol (

+ ControllerHandle,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ &gEfiManagedNetworkProtocolGuid,

+ &MnpHandle,

+ (VOID**)&Mnp

+ );

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);

+ if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {

+ goto Error;

+ }

+

+ UnicodeSPrint (

+ NicName,

+ IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,

+ SnpMode.IfType == NET_IFTYPE_ETHERNET ?

+ L"eth%d" :

+ L"unk%d",

+ NicNumber

+ );

+

+ Status = EFI_SUCCESS;

+

+Error:

+

+ if (MnpHandle != NULL) {

+ CloseProtocolAndDestroyServiceChild (

+ ControllerHandle,

+ &gEfiManagedNetworkServiceBindingProtocolGuid,

+ &gEfiManagedNetworkProtocolGuid,

+ MnpHandle

+ );

+ }

+

+ return Status;

+}

+

+/**

+ Create a child for the service identified by its service binding
protocol
GUID
+ and get from the child the interface of the protocol identified by
its
GUID.
+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol
GUID
of the
+ service to be created.

+ @param[in] ProtocolGuid GUID of the protocol to be
open.
+ @param[out] ChildHandle Address where the handler
of the
+ created child is returned.
NULL is
+ returned in case of error.

+ @param[out] Interface Address where a pointer to
the
+ protocol interface is
returned in
+ case of success.

+

+ @return EFI_SUCCESS The child was created and the protocol
opened.
+ @return Others Either the creation of the child or the
opening
+ of the protocol failed.

+**/

+STATIC

+EFI_STATUS

+CreateServiceChildAndOpenProtocol (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ OUT EFI_HANDLE *ChildHandle,

+ OUT VOID **Interface

+ )

+{

+ EFI_STATUS Status;

+

+ *ChildHandle = NULL;

+ Status = NetLibCreateServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ ChildHandle

+ );

+ if (!EFI_ERROR (Status)) {

+ Status = gBS->OpenProtocol (

+ *ChildHandle,

+ ProtocolGuid,

+ Interface,

+ gImageHandle,

+ ControllerHandle,

+ EFI_OPEN_PROTOCOL_GET_PROTOCOL

+ );

+ if (EFI_ERROR (Status)) {

+ NetLibDestroyServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ *ChildHandle

+ );

+ *ChildHandle = NULL;

+ }

+ }

+

+ return Status;

+}

+

+/**

+ Close the protocol identified by its GUID on the child handle of
the
service
+ identified by its service binding protocol GUID, then destroy the
child
+ handle.

+

+ @param[in] ControllerHandle Controller handle.

+ @param[in] ServiceBindingProtocolGuid Service binding protocol
GUID
of the
+ service to be destroyed.

+ @param[in] ProtocolGuid GUID of the protocol to be
closed.
+ @param[in] ChildHandle Handle of the child to be
destroyed.
+

+**/

+STATIC

+VOID

+CloseProtocolAndDestroyServiceChild (

+ IN EFI_HANDLE ControllerHandle,

+ IN EFI_GUID *ServiceBindingProtocolGuid,

+ IN EFI_GUID *ProtocolGuid,

+ IN EFI_HANDLE ChildHandle

+ )

+{

+ gBS->CloseProtocol (

+ ChildHandle,

+ ProtocolGuid,

+ gImageHandle,

+ ControllerHandle

+ );

+

+ NetLibDestroyServiceChild (

+ ControllerHandle,

+ gImageHandle,

+ ServiceBindingProtocolGuid,

+ ChildHandle

+ );

+}

+

+/**

+ Wait until operation completes. Completion is indicated by

+ setting of an appropriate variable.

+

+ @param[in] Context A pointer to the HTTP download
context.
+ @param[in] CallBackComplete A pointer to the callback
completion
+ variable set by the callback.

+

+ @return EFI_SUCCESS Callback signalled completion.

+ @return EFI_TIMEOUT Timed out waiting for completion.

+ @return Others Error waiting for completion.

+**/

+STATIC

+EFI_STATUS

+WaitForCompletion (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN OUT BOOLEAN *CallBackComplete

+ )

+{

+ EFI_STATUS Status;

+ EFI_EVENT WaitEvt;

+

+ Status = EFI_SUCCESS;

+

+ // Use a timer to measure timeout. Cannot use Stall here!

+ Status = gBS->CreateEvent (

+ EVT_TIMER,

+ TPL_CALLBACK,

+ NULL,

+ NULL,

+ &WaitEvt

+ );

+ ASSERT_EFI_ERROR (Status);

+

+ if (!EFI_ERROR (Status)) {

+ Status = gBS->SetTimer(

+ WaitEvt,

+ TimerRelative,

+ EFI_TIMER_PERIOD_SECONDS (TIMER_MAX_TIMEOUT_S)

+ );

+

+ ASSERT_EFI_ERROR (Status);

+ }

+

+ while (! *CallBackComplete &&

+ (!EFI_ERROR (Status)) &&

+ EFI_ERROR (gBS->CheckEvent (WaitEvt))

+ ) {

+ Status = Context->Http->Poll (Context->Http);

+ }

+

+ gBS->SetTimer (WaitEvt, TimerCancel, 0);

+ gBS->CloseEvent (WaitEvt);

+

+ if (*CallBackComplete) {

+ return EFI_SUCCESS;

+ }

+

+ if (!EFI_ERROR (Status)) {

+ Status = EFI_TIMEOUT;

+ }

+

+ return Status;

+}

+

+/**

+ Generate and send a request to the http server.

+

+ @param[in] Context HTTP download context.

+ @param[in] DownloadUrl Fully qualified URL to be
downloaded.
+

+ @return EFI_SUCCESS Request has been sent successfully.

+ @return EFI_INVALID_PARAMETER Invalid URL.

+ @return EFI_OUT_OF_RESOURCES Out of memory.

+ @return EFI_DEVICE_ERROR If HTTPS is used, this probably

+ means that TLS support either was
not
+ installed or not configured.

+ @return Others Error sending the request.

+**/

+

+STATIC

+EFI_STATUS

+SendRequest (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_HTTP_REQUEST_DATA RequestData;

+ EFI_HTTP_HEADER RequestHeader;

+ EFI_HTTP_MESSAGE RequestMessage;

+ EFI_STATUS Status;

+ CHAR16 *Host;

+ UINTN StringSize;

+

+ ZeroMem (&RequestData, sizeof (RequestData));

+ ZeroMem (&RequestHeader, sizeof (RequestHeader));

+ ZeroMem (&RequestMessage, sizeof (RequestMessage));

+ ZeroMem (&Context->RequestToken, sizeof (Context-
RequestToken));

+

+ RequestHeader.FieldName = "Host";

+

+ Host = (CHAR16 *)Context->ServerAddrAndProto;

+ while (*Host != CHAR_NULL && *Host != L'/') {

+ Host++;

+ }

+

+ if (*Host == CHAR_NULL) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ //

+ // Get the next slash

+ //

+ Host++;

+ //

+ // And now the host name

+ //

+ Host++;

+

+ StringSize = StrLen (Host) + 1;

+ RequestHeader.FieldValue = AllocatePool (StringSize);

+ if (!RequestHeader.FieldValue) {

+ return EFI_OUT_OF_RESOURCES;

+ }

+

+ UnicodeStrToAsciiStrS (Host, RequestHeader.FieldValue,
StringSize);
+

+ RequestMessage.HeaderCount++;

+

+ RequestData.Method = HttpMethodGet;

+ RequestData.Url = DownloadUrl;

+

+ RequestMessage.Data.Request = &RequestData;

+ RequestMessage.Headers = &RequestHeader;

+ RequestMessage.BodyLength = 0;

+ RequestMessage.Body = NULL;

+ Context->RequestToken.Event = NULL;

+

+ //

+ // Completion callback event to be set when Request completes.

+ //

+ Status = gBS->CreateEvent (

+ EVT_NOTIFY_SIGNAL,

+ TPL_CALLBACK,

+ RequestCallback,

+ Context,

+ &Context->RequestToken.Event

+ );

+ ASSERT_EFI_ERROR (Status);

+

+ Context->RequestToken.Status = EFI_SUCCESS;

+ Context->RequestToken.Message = &RequestMessage;

+ gRequestCallbackComplete = FALSE;

+ Status = Context->Http->Request (Context->Http, &Context-
RequestToken);

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ Status = WaitForCompletion (Context, &gRequestCallbackComplete);

+ if (EFI_ERROR (Status)) {

+ Context->Http->Cancel (Context->Http, &Context->RequestToken);

+ }

+Error:

+ SHELL_FREE_NON_NULL (RequestHeader.FieldValue);

+ if (Context->RequestToken.Event) {

+ gBS->CloseEvent (Context->RequestToken.Event);

+ ZeroMem (&Context->RequestToken, sizeof (Context-
RequestToken));

+ }

+

+ return Status;

+}

+

+/**

+ Update the progress of a file download

+ This procedure is called each time a new HTTP body portion is
received.
+

+ @param[in] Context HTTP download context.

+ @param[in] DownloadLen Portion size, in bytes.

+ @param[in] Buffer The pointer to the parsed buffer.

+

+ @retval EFI_SUCCESS Portion saved.

+ @retval Other Error saving the portion.

+

+**/

+STATIC

+EFI_STATUS

+EFIAPI

+SavePortion (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN UINTN DownloadLen,

+ IN CHAR8 *Buffer

+ )

+{

+ CHAR16 Progress[HTTP_PROGRESS_MESSAGE_SIZE];

+ UINTN NbOfKb;

+ UINTN Index;

+ UINTN LastStep;

+ UINTN Step;

+ EFI_STATUS Status;

+

+ LastStep = 0;

+ Step = 0;

+ ShellSetFilePosition (mFileHandle, Context->LastReportedNbOfBytes);

+ Status = ShellWriteFile (mFileHandle, &DownloadLen, Buffer);

+ if (EFI_ERROR (Status)) {

+ if (Context->ContentDownloaded > 0) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), mHttpHiiHandle

+ );

+ }

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_WRITE),

+ mHttpHiiHandle, mLocalFilePath, Status

+ );

+

+ return Status;

+ }

+

+ if (Context->ContentDownloaded == 0) {

+ ShellPrintEx (-1, -1, L"%s 0 Kb", HTTP_PROGR_FRAME);

+ }

+

+ Context->ContentDownloaded += DownloadLen;

+ NbOfKb = Context->ContentDownloaded >> 10;

+

+ Progress[0] = L'\0';

+ if (Context->ContentLength) {

+ LastStep = (Context->LastReportedNbOfBytes *
HTTP_PROGRESS_SLIDER_STEPS) /

+ Context->ContentLength;

+ Step = (Context->ContentDownloaded *
HTTP_PROGRESS_SLIDER_STEPS) /
+ Context->ContentLength;

+ }

+

+ Context->LastReportedNbOfBytes = Context->ContentDownloaded;

+

+ if (Step <= LastStep) {

+ if (!Context->ContentLength) {

+ //

+ // Update downloaded size, there is no length info available.

+ //

+ ShellPrintEx (-1, -1, L"%s", HTTP_KB);

+ ShellPrintEx (-1, -1, L"%7d Kb", NbOfKb);

+ }

+

+ return EFI_SUCCESS;

+ }

+

+ ShellPrintEx (-1, -1, L"%s", HTTP_PROGRESS_DEL);

+

+ Status = StrCpyS (Progress, HTTP_PROGRESS_MESSAGE_SIZE,
HTTP_PROGR_FRAME);

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ for (Index = 1; Index < Step; Index++) {

+ Progress[Index] = L'=';

+ }

+

+ if (Step) {

+ Progress[Step] = L'>';

+ }

+

+ UnicodeSPrint (

+ Progress + (sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 1,

+ sizeof (Progress) - sizeof (HTTP_PROGR_FRAME),

+ L" %7d Kb",

+ NbOfKb

+ );

+

+

+ ShellPrintEx (-1, -1, L"%s", Progress);

+

+ return EFI_SUCCESS;

+}

+

+/**

+ Replace the original Host and URI with Host and URI returned by the

+ HTTP server in 'Location' header (redirection).

+

+ @param[in] Location A pointer to the 'Location' string

+ provided by HTTP server.

+ @param[in] Context A pointer to HTTP download context.

+ @param[in] DownloadUrl Fully qualified HTTP URL.

+

+ @return EFI_SUCCESS Host and URI were successfully set.

+ @return EFI_OUT_OF_RESOURCES Error setting Host or URI.

+**/

+

+STATIC

+EFI_STATUS

+SetHostURI (

+ IN CHAR8 *Location,

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_STATUS Status;

+ UINTN StringSize;

+ UINTN FirstStep;

+ UINTN Idx;

+ UINTN Step;

+ CHAR8 *Walker;

+ CHAR16 *Temp;

+ CHAR8 *Tmp;

+ CHAR16 *Url;

+ BOOLEAN IsAbEmptyUrl;

+

+ Tmp = NULL;

+ Url = NULL;

+ IsAbEmptyUrl = FALSE;

+ FirstStep = 0;

+

+ StringSize = (AsciiStrSize (Location) * sizeof (CHAR16));

+ Url = AllocateZeroPool (StringSize);

+ if (!Url) {

+ return EFI_OUT_OF_RESOURCES;

+ }

+

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Location,

+ Url,

+ StringSize

+ );

+

+ if (EFI_ERROR (Status)) {

+ goto Error;

+ }

+

+ //

+ // If an HTTP server redirects to the same location more than once,

+ // then stop attempts and tell it is not reachable.

+ //

+ if (!StrCmp (Url, DownloadUrl)) {

+ Status = EFI_NO_MAPPING;

+ goto Error;

+ }

+

+ if (AsciiStrLen (Location) > 2) {

+ // Some servers return 'Location: //server/resource'

+ IsAbEmptyUrl = (Location[0] == '/') && (Location[1] == '/');

+ if (IsAbEmptyUrl) {

+ // Skip first "//"

+ Location += 2;

+ FirstStep = 1;

+ }

+ }

+

+ if (AsciiStrStr (Location, "://") || IsAbEmptyUrl) {

+ Idx = 0;

+ Walker = Location;

+

+ for (Step = FirstStep; Step < 2; Step++) {

+ for (; *Walker != '/' && *Walker != '\0'; Walker++) {

+ Idx++;

+ }

+ if (!Step) {

+ //

+ // Skip "//"

+ //

+ Idx += 2;

+ Walker += 2;

+ }

+ }

+

+ Tmp = AllocateZeroPool (Idx + 1);

+ if (!Tmp) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ CopyMem (Tmp, Location, Idx);

+

+ //

+ // Location now points to URI

+ //

+ Location += Idx;

+ StringSize = (Idx + 1) * sizeof (CHAR16);

+

+ SHELL_FREE_NON_NULL (Context->ServerAddrAndProto);

+

+ Temp = AllocateZeroPool (StringSize);

+ if (!Temp) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Tmp,

+ Temp,

+ StringSize

+ );

+ if (EFI_ERROR (Status)) {

+ SHELL_FREE_NON_NULL (Temp);

+ goto Error;

+ }

+

+ Idx = 0;

+ if (IsAbEmptyUrl) {

+ Context->ServerAddrAndProto = StrnCatGrow (

+ &Context->ServerAddrAndProto,

+ &Idx,

+ L"http://",

+ StrLen (L"http://")

+ );

+ }

+

+ Context->ServerAddrAndProto = StrnCatGrow (

+ &Context->ServerAddrAndProto,

+ &Idx,

+ Temp,

+ StrLen (Temp)

+ );

+ SHELL_FREE_NON_NULL (Temp);

+ if (!Context->ServerAddrAndProto) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+ }

+

+ SHELL_FREE_NON_NULL (Context->URI);

+

+ StringSize = AsciiStrSize (Location) * sizeof (CHAR16);

+ Context->URI = AllocateZeroPool (StringSize);

+ if (!Context->URI) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto Error;

+ }

+

+ //

+ // Now make changes to the URI part.

+ //

+ Status = AsciiStrToUnicodeStrS (

+ (CONST CHAR8 *)Location,

+ Context->URI,

+ StringSize

+ );

+Error:

+ SHELL_FREE_NON_NULL (Tmp);

+ SHELL_FREE_NON_NULL (Url);

+ return Status;

+}

+

+/**

+ Message parser callback.

+ Save a portion of HTTP body.

+

+ @param[in] EventType Type of event. Can be either

+ OnComplete or OnData.

+ @param[in] Data A pointer to the buffer with data.

+ @param[in] Length Data length of this portion.

+ @param[in] Context A pointer to the HTTP download
context.
+

+ @return EFI_SUCCESS The portion was processed successfully.

+ @return Other Error returned by SavePortion.

+**/

+

+STATIC

+EFI_STATUS

+EFIAPI

+ParseMsg (

+ IN HTTP_BODY_PARSE_EVENT EventType,

+ IN CHAR8 *Data,

+ IN UINTN Length,

+ IN VOID *Context

+ )

+{

+ if (!Data || (EventType == BodyParseEventOnComplete) || !Context) {

+ return EFI_SUCCESS;

+ }

+

+ return SavePortion (Context, Length, Data);

+}

+

+

+/**

+ Get HTTP server response and collect the whole body as a file.

+ Set appropriate status in Context (REQ_OK, REQ_REPEAT, REQ_ERROR).

+ Note that even if HTTP server returns an error code, it might send

+ the body as well. This body will be collected in the resultant
file.
+

+ @param[in] Context A pointer to the HTTP download
context.
+ @param[in] DownloadedUrl A pointer to the fully qualified URL
to
download.
+

+ @return EFI_SUCCESS Valid file. Body successfully
collected.
+ @return EFI_HTTP_ERROR Response is a valid HTTP response, but
the
+ HTTP server

+ indicated an error (HTTP code >= 400).

+ Response body MAY contain full

+ HTTP server response.

+ @return Others Error getting the reponse from the
HTTP server.
+ Response body is not collected.

+**/

+STATIC

+EFI_STATUS

+GetResponse (

+ IN DOWNLOAD_CONTEXT *Context,

+ IN CHAR16 *DownloadUrl

+ )

+{

+ EFI_HTTP_RESPONSE_DATA ResponseData;

+ EFI_HTTP_MESSAGE ResponseMessage;

+ EFI_HTTP_HEADER *Header;

+ EFI_STATUS Status;

+ VOID *MsgParser;

+ CONST CHAR16 *Desc;

+ BOOLEAN IsTrunked;

+ BOOLEAN Redirection;

+

+ ZeroMem (&ResponseData, sizeof (ResponseData));

+ ZeroMem (&ResponseMessage, sizeof (ResponseMessage));

+ ZeroMem (&Context->ResponseToken, sizeof (Context-
ResponseToken));

+ IsTrunked = FALSE;

+

+ ResponseMessage.Body = Context->Buffer;

+ Context->ResponseToken.Status = EFI_SUCCESS;

+ Context->ResponseToken.Message = &ResponseMessage;

+ Context->ContentLength = 0;

+ Context->Status = REQ_OK;

+ MsgParser = NULL;

+ ResponseData.StatusCode = HTTP_STATUS_UNSUPPORTED_STATUS;

+ ResponseMessage.Data.Response = &ResponseData;

+ Context->ResponseToken.Event = NULL;

+

+ do {

+ SHELL_FREE_NON_NULL (ResponseMessage.Headers);

+ ResponseMessage.HeaderCount = 0;

+ gResponseCallbackComplete = FALSE;

+ ResponseMessage.BodyLength = Context->BufferSize;

+

+ if (ShellGetExecutionBreakFlag ()) {

+ Status = EFI_ABORTED;

+ break;

+ }

+

+ if (!Context->ContentDownloaded && !Context-
ResponseToken.Event) {

+ Status = gBS->CreateEvent (

+ EVT_NOTIFY_SIGNAL,

+ TPL_CALLBACK,

+ ResponseCallback,

+ Context,

+ &Context->ResponseToken.Event

+ );

+ ASSERT_EFI_ERROR (Status);

+ } else {

+ ResponseMessage.Data.Response = NULL;

+ }

+

+ if (EFI_ERROR (Status)) {

+ break;

+ }

+

+ Status = Context->Http->Response (Context->Http, &Context-
ResponseToken);
+ if (EFI_ERROR (Status)) {

+ break;

+ }

+

+ Status = WaitForCompletion (Context, &gResponseCallbackComplete);

+ if (EFI_ERROR (Status) && ResponseMessage.HeaderCount) {

+ Status = EFI_SUCCESS;

+ }

+

+ if (EFI_ERROR (Status)) {

+ Context->Http->Cancel (Context->Http, &Context->ResponseToken);

+ break;

+ }

+

+ if (!Context->ContentDownloaded) {

+ Redirection =

+ ((ResponseData.StatusCode >=
HTTP_STATUS_300_MULTIPLE_CHOICES)
&&

+ (ResponseData.StatusCode <=
HTTP_STATUS_307_TEMPORARY_REDIRECT)

+ ) ||

+ (ResponseData.StatusCode ==
HTTP_STATUS_308_PERMANENT_REDIRECT);

+

+ if (Redirection) {

+ //

+ // Need to repeat the request with new Location (server
redirected).
+ //

+ Context->Status = REQ_NEED_REPEAT;

+

+ Header = HttpFindHeader (

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ "Location"

+ );

+ if (Header) {

+ Status = SetHostURI (Header->FieldValue, Context,
DownloadUrl);
+ if (Status == EFI_NO_MAPPING) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),

+ mHttpHiiHandle, Context->ServerAddrAndProto,

+ L"Recursive HTTP server relocation",

+ Context->URI

+ );

+ }

+ } else {

+ //

+ // Bad reply from the server. Server must specify the
location.
+ // Indicate that resource was not found, and no body
collected.
+ //

+ Status = EFI_NOT_FOUND;

+ }

+

+ Context->Http->Cancel (Context->Http,
&Context->ResponseToken);
+ break;

+ }

+

+ //

+ // Init message-body parser by header information.

+ //

+ if (!MsgParser) {

+ Status = HttpInitMsgParser (

+ ResponseMessage.Data.Request->Method,

+ ResponseData.StatusCode,

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ ParseMsg,

+ Context,

+ &MsgParser

+ );

+ if (EFI_ERROR (Status)) {

+ break;

+ }

+ }

+

+ //

+ // If it is a trunked message, rely on the parser.

+ //

+ Header = HttpFindHeader (

+ ResponseMessage.HeaderCount,

+ ResponseMessage.Headers,

+ "Transfer-Encoding"

+ );

+ IsTrunked = (Header && !AsciiStrCmp (Header->FieldValue,
"chunked"));
+

+ HttpGetEntityLength (MsgParser, &Context->ContentLength);

+

+ if (ResponseData.StatusCode >= HTTP_STATUS_400_BAD_REQUEST
&&
+ (ResponseData.StatusCode !=
HTTP_STATUS_308_PERMANENT_REDIRECT)

+ ) {

+ //

+ // Server reported an error via Response code.

+ // Collect the body if any.

+ //

+ if (!gHttpError) {

+ gHttpError = TRUE;

+

+ Desc = ErrStatusDesc[ResponseData.StatusCode -

+ HTTP_STATUS_400_BAD_REQUEST];

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),

+ mHttpHiiHandle, Context->ServerAddrAndProto,

+ Desc,

+ Context->URI

+ );

+

+ //

+ // This gives an RFC HTTP error.

+ //

+ Context->Status = ShellStrToUintn (Desc);

+ }

+ }

+ }

+

+ // Do NOT try to parse an empty body.

+ if (ResponseMessage.BodyLength || IsTrunked) {

+ Status = HttpParseMessageBody (

+ MsgParser,

+ ResponseMessage.BodyLength,

+ ResponseMessage.Body

+ );

+ }

+ } while (!HttpIsMessageComplete (MsgParser) &&

+ !EFI_ERROR (Status) &&

+ ResponseMessage.BodyLength

+ );

+

+ SHELL_FREE_NON_NULL (MsgParser);

+ if (Context->ResponseToken.Event) {

+ gBS->CloseEvent (Context->ResponseToken.Event);

+ ZeroMem (&Context->ResponseToken, sizeof (Context-
ResponseToken));

+ }

+

+ return Status;

+}

+

+/**

+ Worker function that downloads the data of a file from an HTTP
server
given
+ the path of the file and its size.

+

+ @param[in] Context A pointer to the HTTP download
context.
+

+ @retval EFI_SUCCESS The file was downloaded.

+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.

+ #retval EFI_HTTP_ERROR The server returned a valid HTTP
error.
+ Examine the mLocalFilePath file

+ to get error body.

+ @retval Others The downloading of the file from the
server
+ failed.

+

+**/

+STATIC

+EFI_STATUS

+DownloadFile (

+ IN DOWNLOAD_CONTEXT *Context

+ )

+{

+ EFI_STATUS Status;

+ CHAR16 *DownloadUrl;

+ UINTN UrlSize;

+

+ ASSERT (Context);

+ if (!Context) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ DownloadUrl = NULL;

+

+ Context->Buffer = AllocatePool (Context->BufferSize);

+ if (!Context->Buffer) {

+ Status = EFI_OUT_OF_RESOURCES;

+ goto ON_EXIT;

+ }

+

+ //

+ // OPEN FILE

+ //

+ if (!EFI_ERROR (ShellFileExists (mLocalFilePath))) {

+ ShellDeleteFileByName (mLocalFilePath);

+ }

+

+ Status = ShellOpenFileByName (

+ mLocalFilePath,

+ &mFileHandle,

+ EFI_FILE_MODE_CREATE |

+ EFI_FILE_MODE_WRITE |

+ EFI_FILE_MODE_READ,

+ 0);

+ if (EFI_ERROR (Status)) {

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),

+ mHttpHiiHandle, HTTP_APP_NAME, mLocalFilePath

+ );

+ goto ON_EXIT;

+ }

+

+ do {

+ SHELL_FREE_NON_NULL (DownloadUrl);

+ UrlSize = 0;

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ Context->ServerAddrAndProto,

+ StrLen (Context->ServerAddrAndProto)

+ );

+ if (Context->URI[0] != L'/') {

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ L"/",

+ StrLen (Context->ServerAddrAndProto)

+ );

+ }

+

+ DownloadUrl = StrnCatGrow (

+ &DownloadUrl,

+ &UrlSize,

+ Context->URI,

+ StrLen (Context->URI));

+

+ ShellPrintHiiEx (

+ -1, -1, NULL, STRING_TOKEN (STR_HTTP_DOWNLOADING),

+ mHttpHiiHandle, DownloadUrl);

+

+ Status = SendRequest (Context, DownloadUrl);

+ if (Status) {

+ goto ON_EXIT;

+ }

+

+ Status = GetResponse (Context, DownloadUrl);

+

+ if (Status) {

+ goto ON_EXIT;

+ }

+

+ } while (Context->Status == REQ_NEED_REPEAT);

+

+ if (Context->Status) {

+ Status = ENCODE_ERROR (Context->Status);

+ }

+

+ON_EXIT:

+ //

+ // CLOSE FILE

+ //

+ if (mFileHandle) {

+ if (EFI_ERROR (Status) && !(Context->Flags & DL_FLAG_KEEP_BAD)) {

+ ShellDeleteFile (&mFileHandle);

+ } else {

+ ShellCloseFile (&mFileHandle);

+ }

+ }

+

+ SHELL_FREE_NON_NULL (DownloadUrl);

+ SHELL_FREE_NON_NULL (Context->Buffer);

+

+ return Status;

+}

+

+/**

+ Retrive HII package list from ImageHandle and publish to HII
database.
+

+ @param ImageHandle The image handle of the process.

+

+ @return HII handle.

+**/

+EFI_HII_HANDLE

+InitializeHiiPackage (

+ EFI_HANDLE ImageHandle

+ )

+{

+ EFI_STATUS Status;

+ EFI_HII_PACKAGE_LIST_HEADER *PackageList;

+ EFI_HII_HANDLE HiiHandle;

+

+ //

+ // Retrieve HII package list from ImageHandle

+ //

+ Status = gBS->OpenProtocol (

+ ImageHandle,

+ &gEfiHiiPackageListProtocolGuid,

+ (VOID **)&PackageList,

+ ImageHandle,

+ NULL,

+ EFI_OPEN_PROTOCOL_GET_PROTOCOL

+ );

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return NULL;

+ }

+

+ //

+ // Publish HII package list to HII Database.

+ //

+ Status = gHiiDatabase->NewPackageList (

+ gHiiDatabase,

+ PackageList,

+ NULL,

+ &HiiHandle

+ );

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return NULL;

+ }

+

+ return HiiHandle;

+}

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
new file mode 100644
index 000000000000..5c7b90e8cab4
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h
@@ -0,0 +1,89 @@
+/** @file

+ Header file for 'http' command functions.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
<BR>
+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#ifndef _HTTP_H_

+#define _HTTP_H_

+

+#include <Uefi.h>

+

+#include <Library/BaseLib.h>

+#include <Library/BaseMemoryLib.h>

+#include <Library/DebugLib.h>

+#include <Library/HiiLib.h>

+#include <Library/HttpLib.h>

+#include <Library/MemoryAllocationLib.h>

+#include <Library/NetLib.h>

+#include <Library/PrintLib.h>

+#include <Library/ShellLib.h>

+#include <Library/UefiBootServicesTableLib.h>

+#include <Library/UefiHiiServicesLib.h>

+#include <Library/UefiLib.h>

+#include <Library/UefiRuntimeServicesTableLib.h>

+

+#include <Protocol/HiiPackageList.h>

+#include <Protocol/HttpUtilities.h>

+#include <Protocol/ServiceBinding.h>

+

+#define HTTP_APP_NAME L"http"

+

+#define REQ_OK 0

+#define REQ_NEED_REPEAT 1

+

+// Download Flags

+#define DL_FLAG_TIME BIT0 // Show elapsed time.

+#define DL_FLAG_KEEP_BAD BIT1 // Keep files even if download failed.

+

+extern EFI_HII_HANDLE mHttpHiiHandle;

+

+typedef struct {

+ UINTN ContentDownloaded;

+ UINTN ContentLength;

+ UINTN LastReportedNbOfBytes;

+ UINTN BufferSize;

+ UINTN Status;

+ UINTN Flags;

+ UINT8 *Buffer;

+ CHAR16 *ServerAddrAndProto;

+ CHAR16 *URI;

+ EFI_HTTP_TOKEN ResponseToken;

+ EFI_HTTP_TOKEN RequestToken;

+ EFI_HTTP_PROTOCOL *Http;

+} DOWNLOAD_CONTEXT;

+

+/**

+ Function for 'http' command.

+

+ @param[in] ImageHandle The image handle.

+ @param[in] SystemTable The system table.

+

+ @retval SHELL_SUCCESS Command completed successfully.

+ @retval SHELL_INVALID_PARAMETER Command usage error.

+ @retval SHELL_ABORTED The user aborts the operation.

+ @retval value Unknown error.

+**/

+SHELL_STATUS

+RunHttp (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ );

+

+/**

+ Retrive HII package list from ImageHandle and publish to HII
database.
+

+ @param ImageHandle The image handle of the process.

+

+ @return HII handle.

+**/

+EFI_HII_HANDLE

+InitializeHiiPackage (

+ EFI_HANDLE ImageHandle

+ );

+#endif // _HTTP_H_

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
new file mode 100644
index 000000000000..00cf05deeb5c
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni
@@ -0,0 +1,117 @@
+// /**

+//

+// (C) Copyright 2015-2016 Hewlett Packard Enterprise Development
LP<BR>
+// Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.
<BR>
+// Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+// SPDX-License-Identifier: BSD-2-Clause-Patent

+//

+// Module Name:

+//

+// Http.uni

+//

+// Abstract:

+//

+// String definitions for UEFI Shell HTTP command

+//

+//

+// **/

+

+/=#

+

+#langdef en-US "english"

+

+#string STR_GEN_TOO_MANY #language en-US "%H%s%N: Too
many
arguments. Try help http.\r\n"

+#string STR_GEN_TOO_FEW #language en-US "%H%s%N: Too few
arguments. Try help http.\r\n"

+#string STR_GEN_PARAM_INV #language en-US "%H%s%N: Invalid
argument - '%H%s%N'. Try help http.\r\n"

+#string STR_GEN_PROBLEM #language en-US "%H%s%N: Unknown
flag -
'%H%s%N'. Try help http.\r\n"

+#string STR_GEN_FILE_OPEN_FAIL #language en-US "%H%s%N: Cannot
open
file - '%H%s%N'\r\n"

+#string STR_GEN_CRLF #language en-US "\r\n"

+

+#string STR_HTTP_ERR_NO_NIC #language en-US "No network
interface
card found.\r\n"

+#string STR_HTTP_ERR_NIC_NAME #language en-US "Failed to get the
name
of the network interface card number %d - %r\r\n"

+#string STR_HTTP_ERR_OPEN_PROTOCOL #language en-US "Unable to
open
HTTP protocol on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_CONFIGURE #language en-US "Unable to
configure
HTTP protocol on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_DOWNLOAD #language en-US "Unable to
download
the file '%H%s%N' on '%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_WRITE #language en-US "Unable to write
into
file
'%H%s%N' - %r\r\n"

+#string STR_HTTP_ERR_NIC_NOT_FOUND #language en-US "Network
Interface
Card '%H%s%N' not found.\r\n"

+#string STR_HTTP_ERR_STATUSCODE #language en-US "\r'%H%s%N'
reports
'%s' for '%H%s%N' \r\n"

+#string STR_HTTP_DOWNLOADING #language en-US "Downloading
'%H%s%N'\r\n"

+

+#string STR_GET_HELP_HTTP #language en-US ""

+".TH http 0 "Download a file from HTTP server."\r\n"

+".SH NAME\r\n"

+"Download a file from HTTP server.\r\n"

+".SH SYNOPSIS\r\n"

+" \r\n"

+"HTTP [-i interface] [-l port] [-t timeout] [-s size] [-m] [-k]\r\n"

+" <URL> [localfilepath]\r\n"

+".SH OPTIONS\r\n"

+" \r\n"

+" -i interface - Specifies an adapter name, i.e., eth0.\r\n"

+" -k Keep the downloaded file even if there was an
error.\r\n"
+" If this parameter is not used, the file will be
deleted.\r\n"
+" -l port - Specifies the local port number. Default value
is 0\r\n"
+" and the port number is automatically
assigned.\r\n"
+" -m Measure and report download time (in seconds).
\r\n"
+" -s size The size of the download buffer for a chunk, in
bytes.\r\n"
+" Default is 32K. Note that larger buffer does
not imply\r\n"
+" better speed.\r\n"

+" -t timeout - The number of seconds to wait for completion
of\r\n"
+" requests and responses. Default is 0 which is
'automatic'.\r\n"
+" %HURL%N\r\n"

+" Two types of providing of URLs are supported:\r\n"

+" 1. tftp-like, where host and http_uri are separate
parameters\r\n"
+" (example: host /host_uri), and\r\n\"

+" 2. wget-like, where host and host_uri is one parameter.\r\n"

+" (example: host/host_uri)\r\n"

+"\r\n"

+" host - Specifies HTTP Server address.\r\n

+ Can be either IPv4 address or 'http (or
https)://addr'\r\n
+ Can use addresses resolvable by DNS as well.
\r\n
+ Port can be specified after ':' if needed.
\r\n
+ By default port 80 is used.\r\n"

+" http_uri - HTTP server URI to download the file.\r\n"

+"\r\n"

+" localfilepath - Local destination file path.\r\n"

+".SH DESCRIPTION\r\n"

+" \r\n"

+"NOTES:\r\n"

+" 1. The HTTP command allows geting of the file specified by its
'http_uri'\r\n"
+" path from the HTTP server specified by its 'host' IPv4 address.
If
the\r\n"
+" optional 'localfilepath' parameter is provided, the downloaded
file
is\r\n"
+" stored locally using the provided file path. If the local file
path is\r\n"
+" not specified, the file is stored in the current directory
using the
file\r\n"
+" server's name.\r\n"

+" 2. Before using the HTTP command, the network interface intended
to
be\r\n"

+" used to retrieve the file must be configured. This
configuration may
be\r\n"
+" done by means of the 'ifconfig' command.\r\n"

+" 3. If a network interface is defined with the '-i' option then
only
this\r\n"
+" interface will be used to retrieve the remote file. Otherwise,
all
network\r\n"

+" interfaces are tried in the order they have been discovered
during
the\r\n"
+" DXE phase.\r\n"

+".SH EXAMPLES\r\n"

+" \r\n"

+"EXAMPLES:\r\n"

+" * To get the file "dir1/file1.dat" from the HTTP server
192.168.1.1, port
8080,
and\r\n"

+" store it as file2.dat in the current directory (use tftp-like
URL format)
:\r\n"
+" fs0:\> http 192.168.1.1:8080 dir1/file1.dat file2.dat\r\n"

+" * To get the file /image.bin via HTTPS from server 192.168.1.1 at
port 443
\r\n"

+" (default HTTPS port), and store it in the current directory:
\r\n"
+" fs0:\> http https://192.168.1.1 image.bin\r\n"

+" To get an index file from http://google.com and place it into
the \r\n"
+" current directory:\r\n"

+" fs0:\> http google.com index.html\r\n"

+".SH RETURNVALUES\r\n"

+" \r\n"

+"RETURN VALUES:\r\n"

+" SHELL_SUCCESS The action was completed as
requested.\r\n"
+" SHELL_INVALID_PARAMETER One of the passed-in parameters was
incorrectly\r\n"

+" formatted or its value was out of
bounds.\r\n"
+" HTTP_ERROR No EFI errors, but the server reported a
status
code\r\n"

+" which should be treated as an error. If
an error body
sent\r\n"
+" by the server, and -k parameter is on
command line,
+" the file wil be saved either as
localfilepath filename,\r\n"
+" or as an URI name in the current
directory.\r\n"
+" If '/' is at the end of the URL, and no
locafilepath
filename\r\n"
+" is given on the command line, the file
will be retrieved
as\r\n"
+" index.html.\r\n"

diff --git a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
new file mode 100644
index 000000000000..7bd5b46d3997
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.c
@@ -0,0 +1,53 @@
+/** @file

+ Entrypoint of "http" shell standalone application.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
<BR>
+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+#include "Http.h"

+

+/*

+ * String token ID of help message text.

+ * Shell supports to find help message in the resource section of an

+ * application image if * .MAN file is not found.

+ * This global variable is added to make build tool recognizes

+ * that the help string is consumed by user and then build tool will

+ * add the string into the resource section.

+ * Thus the application can use '-?' option to show help message in
Shell.
+ */

+GLOBAL_REMOVE_IF_UNREFERENCED

+EFI_STRING_ID mStringHelpTokenId = STRING_TOKEN
(STR_GET_HELP_HTTP);
+

+/**

+ Entry point of Http standalone application.

+

+ @param ImageHandle The image handle of the process.

+ @param SystemTable The EFI System Table pointer.

+

+ @retval EFI_SUCCESS Http command is executed sucessfully.

+ @retval EFI_ABORTED HII package was failed to initialize.

+ @retval others Other errors when executing http
command.
+**/

+EFI_STATUS

+EFIAPI

+HttpAppInitialize (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+ mHttpHiiHandle = InitializeHiiPackage (ImageHandle);

+ if (mHttpHiiHandle == NULL) {

+ return EFI_ABORTED;

+ }

+

+ Status = (EFI_STATUS)RunHttp (ImageHandle, SystemTable);

+

+ HiiRemovePackages (mHttpHiiHandle);

+

+ return Status;

+}

diff --git
a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
new file mode 100644
index 000000000000..d08d47fb37d5
--- /dev/null
+++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf
@@ -0,0 +1,58 @@
+## @file

+# Provides Shell 'http' standalone application.

+#

+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
<BR>
+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010006

+ BASE_NAME = http

+ FILE_GUID =
56B00FB7-91D2-869B-CE5C-26CD1A89C73C
+ MODULE_TYPE = UEFI_APPLICATION

+ VERSION_STRING = 1.0

+ ENTRY_POINT = HttpAppInitialize

+#

+# This flag specifies whether HII resource section is generated into
PE
image.
+#

+ UEFI_HII_RESOURCE_SECTION = TRUE

+

+[Sources.common]

+ Http.c

+ HttpApp.c

+ Http.h

+ Http.uni

+

+[Packages]

+ EmbeddedPkg/EmbeddedPkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ MdePkg/MdePkg.dec

+ NetworkPkg/NetworkPkg.dec

+ ShellPkg/ShellPkg.dec

+

+[LibraryClasses]

+ BaseLib

+ BaseMemoryLib

+ DebugLib

+ FileHandleLib

+ HiiLib

+ HttpLib

+ MemoryAllocationLib

+ NetLib

+ ShellLib

+ UefiApplicationEntryPoint

+ UefiBootServicesTableLib

+ UefiHiiServicesLib

+ UefiLib

+ UefiRuntimeServicesTableLib

+

+[Protocols]

+ gEfiHiiPackageListProtocolGuid ## CONSUMES

+ gEfiHttpProtocolGuid ## CONSUMES

+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES

+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES

diff --git
a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.c
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.c
new file mode 100644
index 000000000000..ba654749a075
--- /dev/null
+++
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.c
@@ -0,0 +1,134 @@
+/** @file

+ Produce "http" shell dynamic command.

+

+ Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
<BR>
+ Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+ Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+#include <Protocol/ShellDynamicCommand.h>

+#include "Http.h"

+

+/**

+ This is the shell command handler function pointer callback type.
This
+ function handles the command when it is invoked in the shell.

+

+ @param[in] This The instance of the

+ EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.

+ @param[in] SystemTable The pointer to the system table.

+ @param[in] ShellParameters The parameters associated with the
command.
+ @param[in] Shell The instance of the shell protocol
used in
+ the context of processing this
command.
+

+ @return EFI_SUCCESS the operation was sucessful

+ @return other the operation failed.

+**/

+SHELL_STATUS

+EFIAPI

+HttpCommandHandler (

+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,

+ IN EFI_SYSTEM_TABLE *SystemTable,

+ IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters,

+ IN EFI_SHELL_PROTOCOL *Shell

+ )

+{

+ gEfiShellParametersProtocol = ShellParameters;

+ gEfiShellProtocol = Shell;

+

+ return RunHttp (gImageHandle, SystemTable);

+}

+

+/**

+ This is the command help handler function pointer callback type.
This
+ function is responsible for displaying help information for the
associated
+ command.

+

+ @param[in] This The instance of the
EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL.

+ @param[in] Language The pointer to the language string to use.

+

+ @return string Pool allocated help string, must be freed by
caller
+**/

+CHAR16 *

+EFIAPI

+HttpCommandGetHelp (

+ IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This,

+ IN CONST CHAR8 *Language

+ )

+{

+ return HiiGetString(mHttpHiiHandle,

+ STRING_TOKEN (STR_GET_HELP_HTTP),

+ Language);

+}

+

+EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL mHttpDynamicCommand
= {
+ HTTP_APP_NAME,

+ HttpCommandHandler,

+ HttpCommandGetHelp

+};

+

+/**

+ Entry point of Http Dynamic Command.

+

+ Produce the DynamicCommand protocol to handle "http" command.

+

+ @param ImageHandle The image handle of the process.

+ @param SystemTable The EFI System Table pointer.

+

+ @retval EFI_SUCCESS Http command is executed sucessfully.

+ @retval EFI_ABORTED HII package was failed to initialize.

+ @retval others Other errors when executing http
command.
+**/

+EFI_STATUS

+EFIAPI

+HttpCommandInitialize (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ EFI_STATUS Status;

+

+ mHttpHiiHandle = InitializeHiiPackage (ImageHandle);

+ if (mHttpHiiHandle == NULL) {

+ return EFI_ABORTED;

+ }

+

+ Status = gBS->InstallProtocolInterface (

+ &ImageHandle,

+ &gEfiShellDynamicCommandProtocolGuid,

+ EFI_NATIVE_INTERFACE,

+ &mHttpDynamicCommand

+ );

+ ASSERT_EFI_ERROR (Status);

+ return Status;

+}

+

+/**

+ Http driver unload handler.

+

+ @param ImageHandle The image handle of the process.

+

+ @retval EFI_SUCCESS The image is unloaded.

+ @retval Others Failed to unload the image.

+**/

+EFI_STATUS

+EFIAPI

+HttpUnload (

+ IN EFI_HANDLE ImageHandle

+)

+{

+ EFI_STATUS Status;

+ Status = gBS->UninstallProtocolInterface (

+ ImageHandle,

+ &gEfiShellDynamicCommandProtocolGuid,

+ &mHttpDynamicCommand

+ );

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ HiiRemovePackages (mHttpHiiHandle);

+

+ return EFI_SUCCESS;

+}

diff --git
a/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.in
f
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.in
f
new file mode 100644
index 000000000000..5d46ee2384d5
--- /dev/null
+++
b/ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicComma
nd.in
f
@@ -0,0 +1,63 @@
+## @file

+# Provides Shell 'http' dynamic command.

+#

+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
<BR>
+# Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>

+# Copyright (c) 2020, Broadcom. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010006

+ BASE_NAME = httpDynamicCommand

+ FILE_GUID =
19618BCE-55AE-09C6-37E9-4CE04084C7A1
+ MODULE_TYPE = DXE_DRIVER

+ VERSION_STRING = 1.0

+ ENTRY_POINT = HttpCommandInitialize

+ UNLOAD_IMAGE = HttpUnload

+#

+# This flag specifies whether HII resource section is generated into
PE
image.
+#

+ UEFI_HII_RESOURCE_SECTION = TRUE

+

+[Sources.common]

+ Http.c

+ HttpDynamicCommand.c

+ Http.h

+ Http.uni

+

+[Packages]

+ EmbeddedPkg/EmbeddedPkg.dec

+ MdePkg/MdePkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ NetworkPkg/NetworkPkg.dec

+ ShellPkg/ShellPkg.dec

+

+[LibraryClasses]

+ BaseLib

+ BaseMemoryLib

+ DebugLib

+ FileHandleLib

+ HiiLib

+ HttpLib

+ MemoryAllocationLib

+ NetLib

+ ShellLib

+ UefiBootServicesTableLib

+ UefiDriverEntryPoint

+ UefiHiiServicesLib

+ UefiLib

+ UefiRuntimeServicesTableLib

+

+[Protocols]

+ gEfiHiiPackageListProtocolGuid ## CONSUMES

+ gEfiHttpProtocolGuid ## CONSUMES

+ gEfiHttpServiceBindingProtocolGuid ## CONSUMES

+ gEfiManagedNetworkServiceBindingProtocolGuid ## CONSUMES

+ gEfiShellDynamicCommandProtocolGuid ## PRODUCES

+

+[DEPEX]

+ TRUE

diff --git a/ShellPkg/Include/Guid/ShellLibHiiGuid.h
b/ShellPkg/Include/Guid/ShellLibHiiGuid.h
index 5da9128333a4..6e328b460d8c 100644
--- a/ShellPkg/Include/Guid/ShellLibHiiGuid.h
+++ b/ShellPkg/Include/Guid/ShellLibHiiGuid.h
@@ -59,6 +59,10 @@
0x738a9314, 0x82c1, 0x4592, { 0x8f, 0xf7, 0xc1, 0xbd, 0xf1, 0xb2,
0x0e,
0xd4 }
\

}



+#define SHELL_HTTP_HII_GUID \

+ { \

+ 0x390f84b3, 0x221c, 0x4d9e, { 0xb5, 0x06, 0x6d, 0xb9, 0x42, 0x3e,
0x0a,
0x7e } \

+ }



#define SHELL_BCFG_HII_GUID \

{ \

@@ -75,6 +79,7 @@ extern EFI_GUID gShellLevel3HiiGuid;
extern EFI_GUID gShellNetwork1HiiGuid;

extern EFI_GUID gShellNetwork2HiiGuid;

extern EFI_GUID gShellTftpHiiGuid;

+extern EFI_GUID gShellHttpHiiGuid;

extern EFI_GUID gShellBcfgHiiGuid;



#endif

diff --git a/ShellPkg/ShellPkg.dec b/ShellPkg/ShellPkg.dec
index d0843d338126..7b2d1230bd2c 100644
--- a/ShellPkg/ShellPkg.dec
+++ b/ShellPkg/ShellPkg.dec
@@ -53,6 +53,7 @@ [Guids]
gShellNetwork1HiiGuid = {0xf3d301bb, 0xf4a5, 0x45a8,
{0xb0, 0xb7,
0xfa,
0x99, 0x9c, 0x62, 0x37, 0xae}}

gShellNetwork2HiiGuid = {0x174b2b5, 0xf505, 0x4b12,
{0xaa, 0x60,
0x59,
0xdf, 0xf8, 0xd6, 0xea, 0x37}}

gShellTftpHiiGuid = {0x738a9314, 0x82c1, 0x4592,
{0x8f, 0xf7, 0xc1,
0xbd, 0xf1, 0xb2, 0x0e, 0xd4}}

+ gShellHttpHiiGuid = {0x390f84b3, 0x221c, 0x4d9e,
{0xb5, 0x06,
0x6d,
0xb9, 0x42, 0x3e, 0x0a, 0x7e}}

gShellBcfgHiiGuid = {0x5f5f605d, 0x1583, 0x4a2d,
{0xa6, 0xb2, 0xeb,
0x12, 0xda, 0xb4, 0xa2, 0xb6}}

gShellAcpiViewHiiGuid = {0xda8ccdf4, 0xed8f, 0x4ffc,
{0xb5, 0xef,
0x2e,
0xf5, 0x5e, 0x24, 0x93, 0x2a}}

# FILE_GUID as defined in ShellPkg/Application/Shell/Shell.inf

diff --git a/ShellPkg/ShellPkg.dsc b/ShellPkg/ShellPkg.dsc
index 86e9f1e0040d..c42bc9464a0f 100644
--- a/ShellPkg/ShellPkg.dsc
+++ b/ShellPkg/ShellPkg.dsc
@@ -139,6 +139,11 @@ [Components]
gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

}

ShellPkg/DynamicCommand/TftpDynamicCommand/TftpApp.inf

+
ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand
.inf {
+ <PcdsFixedAtBuild>

+ gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

+ }

+ ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf

ShellPkg/DynamicCommand/DpDynamicCommand/DpDynamicCommand.inf
{
<PcdsFixedAtBuild>

gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE

--
2.26.2.266.ge870325ee8


Laszlo Ersek
 

On 08/17/20 19:15, Rabeda, Maciej wrote:
Hi Vladimir,

I cannot apply the patch via 'git am'.
Is your git configured in a manner described here?
https://github.com/tianocore/tianocore.github.io/wiki/Laszlo's-unkempt-git-guide-for-edk2-contributors-and-maintainers


Laszlo,

Were you able to apply this patch from .eml file?
Yes, but I had to use some tricks (implemented by my colleague Paolo in
a python script) to undo the damage caused by the "quoted-printable"
content-transfer-encoding on the posting.

Our recommended Content-Transfer-Encoding (that is,
"sendemail.transferEncoding") is "8bit", or even "base64".
quoted-printable is practically impossible to get functional, with the
hard CRLF line endings in edk2.

"BaseTools/Scripts/SetupGit.py" does set "8bit".

Thanks
Laszlo


Vladimir Olovyannikov
 

-----Original Message-----
From: Laszlo Ersek <lersek@...>
Sent: Monday, August 17, 2020 11:01 AM
To: Rabeda, Maciej <maciej.rabeda@...>; Vladimir Olovyannikov
<vladimir.olovyannikov@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

On 08/17/20 19:15, Rabeda, Maciej wrote:
Hi Vladimir,

I cannot apply the patch via 'git am'.
Is your git configured in a manner described here?
https://github.com/tianocore/tianocore.github.io/wiki/Laszlo's-unkempt
-git-guide-for-edk2-contributors-and-maintainers
Hi Rabeda,
Yes, I followed the page. I did not run the SetupGit.py though.


Laszlo,

Were you able to apply this patch from .eml file?
Yes, but I had to use some tricks (implemented by my colleague Paolo in a
python script) to undo the damage caused by the "quoted-printable"
content-transfer-encoding on the posting.

Our recommended Content-Transfer-Encoding (that is,
"sendemail.transferEncoding") is "8bit", or even "base64".
quoted-printable is practically impossible to get functional, with the
hard
CRLF line endings in edk2.

"BaseTools/Scripts/SetupGit.py" does set "8bit".
Thank you for notice Laszlo,
So, should I run this script and re-send the patch as v6?

Thank you,
Vladimir

Thanks
Laszlo


Laszlo Ersek
 

On 08/17/20 20:29, Vladimir Olovyannikov wrote:
-----Original Message-----
From: Laszlo Ersek <lersek@...>
Sent: Monday, August 17, 2020 11:01 AM
To: Rabeda, Maciej <maciej.rabeda@...>; Vladimir Olovyannikov
<vladimir.olovyannikov@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

On 08/17/20 19:15, Rabeda, Maciej wrote:
Hi Vladimir,

I cannot apply the patch via 'git am'.
Is your git configured in a manner described here?
https://github.com/tianocore/tianocore.github.io/wiki/Laszlo's-unkempt
-git-guide-for-edk2-contributors-and-maintainers
Hi Rabeda,
Yes, I followed the page. I did not run the SetupGit.py though.


Laszlo,

Were you able to apply this patch from .eml file?
Yes, but I had to use some tricks (implemented by my colleague Paolo in a
python script) to undo the damage caused by the "quoted-printable"
content-transfer-encoding on the posting.

Our recommended Content-Transfer-Encoding (that is,
"sendemail.transferEncoding") is "8bit", or even "base64".
quoted-printable is practically impossible to get functional, with the
hard
CRLF line endings in edk2.

"BaseTools/Scripts/SetupGit.py" does set "8bit".
Thank you for notice Laszlo,
So, should I run this script and re-send the patch as v6?
I think that would be useful, yes -- and if you have made no changes
since v5, you can also post it as "v5 RESEND". If you've implemented
some changes meanwhile, then please post it as v6 indeed.

Thanks
Laszlo


Vladimir Olovyannikov
 

-----Original Message-----
From: Laszlo Ersek <lersek@...>
Sent: Monday, August 17, 2020 1:44 PM
To: Vladimir Olovyannikov <vladimir.olovyannikov@...>;
Rabeda, Maciej <maciej.rabeda@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

On 08/17/20 20:29, Vladimir Olovyannikov wrote:
-----Original Message-----
From: Laszlo Ersek <lersek@...>
Sent: Monday, August 17, 2020 11:01 AM
To: Rabeda, Maciej <maciej.rabeda@...>; Vladimir
Olovyannikov <vladimir.olovyannikov@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu,
Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

On 08/17/20 19:15, Rabeda, Maciej wrote:
Hi Vladimir,

I cannot apply the patch via 'git am'.
Is your git configured in a manner described here?
https://github.com/tianocore/tianocore.github.io/wiki/Laszlo's-unkem
pt -git-guide-for-edk2-contributors-and-maintainers
Hi Rabeda,
Yes, I followed the page. I did not run the SetupGit.py though.


Laszlo,

Were you able to apply this patch from .eml file?
Yes, but I had to use some tricks (implemented by my colleague Paolo
in a python script) to undo the damage caused by the "quoted-printable"
content-transfer-encoding on the posting.

Our recommended Content-Transfer-Encoding (that is,
"sendemail.transferEncoding") is "8bit", or even "base64".
quoted-printable is practically impossible to get functional, with
the hard CRLF line endings in edk2.

"BaseTools/Scripts/SetupGit.py" does set "8bit".
Thank you for notice Laszlo,
So, should I run this script and re-send the patch as v6?
I think that would be useful, yes -- and if you have made no changes since
v5,
you can also post it as "v5 RESEND". If you've implemented some changes
meanwhile, then please post it as v6 indeed.
Thanks Laszlo,
I will post v6.

Vladimir

Thanks
Laszlo


Maciej Rabeda
 

Hi Vladimir,

I am inprog of going through the patch. There are some coding standard violations.
For reference: https://edk2-docs.gitbook.io/edk-ii-c-coding-standards-specification/

Http.c:
Lines 110-111: Spacing before open bracket and != operator
Line 133: Unnecessary brackets around EFI_SUCCESS
Line 144+: Function argument alignment, closed bracket spacing
Line 357: End of function call ');' in a multiline call should be in the next line, aligned with argument list
Line 360: In a multiline if/while statement, '{' should be moved to a new line
Line 361, 380, 388, ... : ShellPrintHiiEx calls: I suggest defining a macro to improve readability. First 3 parameters and mHttpHiiHandle seem to be commonly used in your calls.
Line 740: Keep unary operation in a single line
Lines 891-901, 946-1028: Tab alignment
Lines 988-995: Assignments to a struct line by line. Could you align them in the following manner?
    SpecificStruct.Field1        =    10;
    SpecificStruct.Field2        =    20;

HttpApp.c:
Line 48: Space after type cast.

List above does not exhaust the things that can be found there.
May I ask you to read through the code again and catch what you and I might have missed?

Additionally:
Http.c, TrimSpaces:
1. It takes CHAR16** as an argument, yet it does not seem to modify the value of the pointer for other functions outside its scope. I think it would be better to just make it CHAR16*.
2. First while loop is highly inefficient. If I had a string like "    asdf.txt", it would call CopyMem for each space, each time moving the asdf.txt one char closer to the beginning.
It would be way better to achieve the same functionality by iterating from front and back of the string looking for a char that is not a space nor a tab.
Having their indices, you can easily call CopyMem once to move the string forward and ZeroMem the rest.

Thanks,
Maciej

On 18-Aug-20 00:52, Vladimir Olovyannikov wrote:
-----Original Message-----
From: Laszlo Ersek <lersek@...>
Sent: Monday, August 17, 2020 1:44 PM
To: Vladimir Olovyannikov <vladimir.olovyannikov@...>;
Rabeda, Maciej <maciej.rabeda@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

On 08/17/20 20:29, Vladimir Olovyannikov wrote:
-----Original Message-----
From: Laszlo Ersek <lersek@...>
Sent: Monday, August 17, 2020 11:01 AM
To: Rabeda, Maciej <maciej.rabeda@...>; Vladimir
Olovyannikov <vladimir.olovyannikov@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu,
Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

On 08/17/20 19:15, Rabeda, Maciej wrote:
Hi Vladimir,

I cannot apply the patch via 'git am'.
Is your git configured in a manner described here?
https://github.com/tianocore/tianocore.github.io/wiki/Laszlo's-unkem
pt -git-guide-for-edk2-contributors-and-maintainers
Hi Rabeda,
Yes, I followed the page. I did not run the SetupGit.py though.

Laszlo,

Were you able to apply this patch from .eml file?
Yes, but I had to use some tricks (implemented by my colleague Paolo
in a python script) to undo the damage caused by the "quoted-printable"
content-transfer-encoding on the posting.

Our recommended Content-Transfer-Encoding (that is,
"sendemail.transferEncoding") is "8bit", or even "base64".
quoted-printable is practically impossible to get functional, with
the hard CRLF line endings in edk2.

"BaseTools/Scripts/SetupGit.py" does set "8bit".
Thank you for notice Laszlo,
So, should I run this script and re-send the patch as v6?
I think that would be useful, yes -- and if you have made no changes since
v5,
you can also post it as "v5 RESEND". If you've implemented some changes
meanwhile, then please post it as v6 indeed.
Thanks Laszlo,
I will post v6.

Vladimir
Thanks
Laszlo


Vladimir Olovyannikov
 

Hi Rabeda,
Thank you for reviewing.

-----Original Message-----
From: Rabeda, Maciej <maciej.rabeda@...>
Sent: Tuesday, August 18, 2020 9:54 AM
To: Vladimir Olovyannikov <vladimir.olovyannikov@...>; Laszlo
Ersek <lersek@...>; Gao, Zhichao <zhichao.gao@...>;
devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

Hi Vladimir,

I am inprog of going through the patch. There are some coding standard
violations.
For reference:
https://edk2-docs.gitbook.io/edk-ii-c-coding-standards-specification/

Http.c:
Lines 110-111: Spacing before open bracket and != operator Line 133:
Unnecessary brackets around EFI_SUCCESS Line 144+: Function argument
alignment, closed bracket spacing Line 357: End of function call ');' in a
multiline call should be in the next line, aligned with argument list Line
360: In
a multiline if/while statement, '{' should be moved to a new line Line
361,
380, 388, ... : ShellPrintHiiEx calls: I suggest defining a macro to
improve
readability. First 3 parameters and mHttpHiiHandle seem to be commonly
used in your calls.
Line 740: Keep unary operation in a single line Lines 891-901, 946-1028:
Tab
alignment Lines 988-995: Assignments to a struct line by line. Could you
align
them in the following manner?
SpecificStruct.Field1 = 10;
SpecificStruct.Field2 = 20;
OK, I will look through all files again, probably something slipped from my
attention as I work with Linux code as well.

HttpApp.c:
Line 48: Space after type cast.
Sorry, I don't understand here. You mean, Something = (CAST)SomethingElse
should actually be "Something = (CAST) SomethingElse?
I don't see this say in the Tftp.c:
if (AsciiStrnCmp ((CHAR8 *)Option->OptionStr, "tsize", 5) == 0) ...
or in TftpApp.c
Status = (EFI_STATUS)RunTftp (ImageHandle, SystemTable);
Am I missing anything?

List above does not exhaust the things that can be found there.
May I ask you to read through the code again and catch what you and I
might
have missed?

Additionally:
Http.c, TrimSpaces:
1. It takes CHAR16** as an argument, yet it does not seem to modify the
value of the pointer for other functions outside its scope. I think it
would be
better to just make it CHAR16*.
2. First while loop is highly inefficient. If I had a string like "
asdf.txt", it would
call CopyMem for each space, each time moving the asdf.txt one char closer
to the beginning.
It would be way better to achieve the same functionality by iterating from
front and back of the string looking for a char that is not a space nor a
tab.
Having their indices, you can easily call CopyMem once to move the string
forward and ZeroMem the rest.
Definitely, thanks. This TrimSpaces() was copy/paste from some other edk2
code.
I should've looked into it.

Thanks,
Maciej
Thank you,
Vladimir

On 18-Aug-20 00:52, Vladimir Olovyannikov wrote:
-----Original Message-----
From: Laszlo Ersek <lersek@...>
Sent: Monday, August 17, 2020 1:44 PM
To: Vladimir Olovyannikov <vladimir.olovyannikov@...>;
Rabeda, Maciej <maciej.rabeda@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu,
Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

On 08/17/20 20:29, Vladimir Olovyannikov wrote:
-----Original Message-----
From: Laszlo Ersek <lersek@...>
Sent: Monday, August 17, 2020 11:01 AM
To: Rabeda, Maciej <maciej.rabeda@...>; Vladimir
Olovyannikov <vladimir.olovyannikov@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu,
Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

On 08/17/20 19:15, Rabeda, Maciej wrote:
Hi Vladimir,

I cannot apply the patch via 'git am'.
Is your git configured in a manner described here?
https://github.com/tianocore/tianocore.github.io/wiki/Laszlo's-unk
em pt -git-guide-for-edk2-contributors-and-maintainers
Hi Rabeda,
Yes, I followed the page. I did not run the SetupGit.py though.

Laszlo,

Were you able to apply this patch from .eml file?
Yes, but I had to use some tricks (implemented by my colleague
Paolo in a python script) to undo the damage caused by the "quoted-
printable"
content-transfer-encoding on the posting.

Our recommended Content-Transfer-Encoding (that is,
"sendemail.transferEncoding") is "8bit", or even "base64".
quoted-printable is practically impossible to get functional, with
the hard CRLF line endings in edk2.

"BaseTools/Scripts/SetupGit.py" does set "8bit".
Thank you for notice Laszlo,
So, should I run this script and re-send the patch as v6?
I think that would be useful, yes -- and if you have made no changes
since v5, you can also post it as "v5 RESEND". If you've implemented
some changes meanwhile, then please post it as v6 indeed.
Thanks Laszlo,
I will post v6.

Vladimir
Thanks
Laszlo


Laszlo Ersek
 

On 08/18/20 20:33, Vladimir Olovyannikov wrote:
-----Original Message-----
From: Rabeda, Maciej <maciej.rabeda@...>
Sent: Tuesday, August 18, 2020 9:54 AM
To: Vladimir Olovyannikov <vladimir.olovyannikov@...>; Laszlo
Ersek <lersek@...>; Gao, Zhichao <zhichao.gao@...>;
devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

Hi Vladimir,

I am inprog of going through the patch. There are some coding standard
violations.
For reference:
https://edk2-docs.gitbook.io/edk-ii-c-coding-standards-specification/
[...]

OK, I will look through all files again, probably something slipped from my
attention as I work with Linux code as well.
Now that we have ECC checking enabled in CI, submitting a personal pull
request could help. Additionally, the CI workload should be possible to
run locally, according to ".pytool/Readme.md".

(I'm planning to learn how to run that locally myself.)


HttpApp.c:
Line 48: Space after type cast.
Sorry, I don't understand here. You mean, Something = (CAST)SomethingElse
should actually be "Something = (CAST) SomethingElse?
I don't see this say in the Tftp.c:
if (AsciiStrnCmp ((CHAR8 *)Option->OptionStr, "tsize", 5) == 0) ...
or in TftpApp.c
Status = (EFI_STATUS)RunTftp (ImageHandle, SystemTable);
Am I missing anything?

In core edk2 packages, casts are frequently written like this:

(TYPE) Expression

(Importantly, Expression itself is not parenthesized.)

In my opinion, this is a bad practice. That's because the typecast has
one of the strongest operator bindings in the C language, and visually
distancing (TYPE) from "Expression" suggests the opposite -- it's
counter-intuitive.

I strongly prefer

(TYPE)Expression

but other maintainers may have a different preference.

Thanks
Laszlo


Maciej Rabeda
 

@Laszlo,

As for type casting, I don't have strong opinions.
Space after typecast (for me) always seemed to be visually more consistent with the rest of the code.
In my regular projects written in C-based languages, I would do it the way you are describing.

@Vladimir
I went through the rest of the code.
There are more coding standard problems, but in terms of HTTP usage the code seems to be OK.

Thanks,
Maciej

PS. Rabeda is my last name :)

On 19-Aug-20 11:47, Laszlo Ersek wrote:
On 08/18/20 20:33, Vladimir Olovyannikov wrote:
-----Original Message-----
From: Rabeda, Maciej <maciej.rabeda@...>
Sent: Tuesday, August 18, 2020 9:54 AM
To: Vladimir Olovyannikov <vladimir.olovyannikov@...>; Laszlo
Ersek <lersek@...>; Gao, Zhichao <zhichao.gao@...>;
devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

Hi Vladimir,

I am inprog of going through the patch. There are some coding standard
violations.
For reference:
https://edk2-docs.gitbook.io/edk-ii-c-coding-standards-specification/
[...]

OK, I will look through all files again, probably something slipped from my
attention as I work with Linux code as well.
Now that we have ECC checking enabled in CI, submitting a personal pull
request could help. Additionally, the CI workload should be possible to
run locally, according to ".pytool/Readme.md".

(I'm planning to learn how to run that locally myself.)

HttpApp.c:
Line 48: Space after type cast.
Sorry, I don't understand here. You mean, Something = (CAST)SomethingElse
should actually be "Something = (CAST) SomethingElse?
I don't see this say in the Tftp.c:
if (AsciiStrnCmp ((CHAR8 *)Option->OptionStr, "tsize", 5) == 0) ...
or in TftpApp.c
Status = (EFI_STATUS)RunTftp (ImageHandle, SystemTable);
Am I missing anything?
In core edk2 packages, casts are frequently written like this:

(TYPE) Expression

(Importantly, Expression itself is not parenthesized.)

In my opinion, this is a bad practice. That's because the typecast has
one of the strongest operator bindings in the C language, and visually
distancing (TYPE) from "Expression" suggests the opposite -- it's
counter-intuitive.

I strongly prefer

(TYPE)Expression

but other maintainers may have a different preference.

Thanks
Laszlo


Vladimir Olovyannikov
 

-----Original Message-----
From: Rabeda, Maciej <maciej.rabeda@...>
Sent: Wednesday, August 19, 2020 10:44 AM
To: Laszlo Ersek <lersek@...>; Vladimir Olovyannikov
<vladimir.olovyannikov@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu, Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

@Laszlo,

As for type casting, I don't have strong opinions.
Space after typecast (for me) always seemed to be visually more consistent
with the rest of the code.
In my regular projects written in C-based languages, I would do it the way
you are describing.

@Vladimir
I went through the rest of the code.
There are more coding standard problems, but in terms of HTTP usage the
code seems to be OK.
OK, great, thanks. I am reviewing it and will submit the patch shortly.
Thanks to Laszlo, I hope to be able to run CI locally and detect hidden
issues.
Indeed, I have to switch between Linux and UEFI code, so I missed several
code style issues.

Thanks,
Maciej

PS. Rabeda is my last name :)
Oops, thanks for letting me know, Maciej.
It is just a bit unusual to have the last name first :)

Thank you,
Vladimir

On 19-Aug-20 11:47, Laszlo Ersek wrote:
On 08/18/20 20:33, Vladimir Olovyannikov wrote:
-----Original Message-----
From: Rabeda, Maciej <maciej.rabeda@...>
Sent: Tuesday, August 18, 2020 9:54 AM
To: Vladimir Olovyannikov <vladimir.olovyannikov@...>;
Laszlo Ersek <lersek@...>; Gao, Zhichao
<zhichao.gao@...>; devel@edk2.groups.io
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@...>; Wu,
Jiaxin
<jiaxin.wu@...>; Fu, Siyuan <@sfu5>; Ni, Ray
<ray.ni@...>; Gao, Liming <liming.gao@...>; Nd
<nd@...>
Subject: Re: [PATCH v5 1/1] ShellPkg/DynamicCommand: add
HttpDynamicCommand

Hi Vladimir,

I am inprog of going through the patch. There are some coding
standard violations.
For reference:
https://edk2-docs.gitbook.io/edk-ii-c-coding-standards-specification
/
[...]

OK, I will look through all files again, probably something slipped
from my attention as I work with Linux code as well.
Now that we have ECC checking enabled in CI, submitting a personal
pull request could help. Additionally, the CI workload should be
possible to run locally, according to ".pytool/Readme.md".

(I'm planning to learn how to run that locally myself.)

HttpApp.c:
Line 48: Space after type cast.
Sorry, I don't understand here. You mean, Something =
(CAST)SomethingElse should actually be "Something = (CAST)
SomethingElse?
I don't see this say in the Tftp.c:
if (AsciiStrnCmp ((CHAR8 *)Option->OptionStr, "tsize", 5) == 0) ...
or in TftpApp.c
Status = (EFI_STATUS)RunTftp (ImageHandle, SystemTable); Am I missing
anything?
In core edk2 packages, casts are frequently written like this:

(TYPE) Expression

(Importantly, Expression itself is not parenthesized.)

In my opinion, this is a bad practice. That's because the typecast has
one of the strongest operator bindings in the C language, and visually
distancing (TYPE) from "Expression" suggests the opposite -- it's
counter-intuitive.

I strongly prefer

(TYPE)Expression

but other maintainers may have a different preference.

Thanks
Laszlo