From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by mx.groups.io with SMTP id smtpd.web11.529.1589220202927152473 for ; Mon, 11 May 2020 11:03:23 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@broadcom.com header.s=google header.b=dOreCWcJ; spf=pass (domain: broadcom.com, ip: 209.85.215.193, mailfrom: vladimir.olovyannikov@broadcom.com) Received: by mail-pg1-f193.google.com with SMTP id s11so1417874pgv.13 for ; Mon, 11 May 2020 11:03:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dHbbLb/YSAv6ecv8ZfVgLxHDJuHPIbCuk/iOCnU/Ym8=; b=dOreCWcJBbDOgVDwXOGHTipsql6Cvy2QNEZ7yN88p53vVK7TtWWQuk2xk2mg5q1kT0 po0JYtC8k4mz7yruamVavWZPMwkDGR3Dz0nNTSL+E7gFlZoqBOkgaS9s7A9EiJKJWqua s3AmXq0IMhJaWY6qXXveB9EDrZUfGpX6dQz+Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dHbbLb/YSAv6ecv8ZfVgLxHDJuHPIbCuk/iOCnU/Ym8=; b=uUnmqY94cChzAd4a8qpQV6nbvMt5lOX6M7P3f4RlNyKGQitZfvwWsQJ42eU/6Lm43H 1cN4IJoj5Hw9lZ4WBpxD8dKwn0D56sfIKGmwEgAbg2skCpkgxjlVW+Q4UZLtp+mIPVv3 Ik0p8jlqVKvmnQT1Tl28g8I1oNMdA10/7lYR5W33i06J3T34gAQxPCjnXPICSlsa8HpS q9ByiNPk3BKLYil/iBanj/ECSJKqQJa4hQ+UulufRrBfUBAcq6rtoZ8Sh1lF6bk+Mzbx VnTjWTxNmOoxMMcwPGnvMe5dmIscx+h59PaTrdcUfUnC3yYFwHsNSxOaONWuvUXjDshX +jGA== X-Gm-Message-State: AGi0PuY8uIvXKyphQUmeLCbvTYIc5SfzwOb0f5mw1sa6lngOsRVNgWOD uDzrfIibRf8DYrrc42xsC4i33BtKx9ehdKSA2Q1lTyTh3E8AtuCMMxxC3JvOrK5oiOfnBWrd//X D2YZjQ5lSRropICBdL0ZIVTQo4T/tznewWwztWwCWkC3B5tEGWCO5cW/GrWexfOtWwpKRgMG/RV knM2WWckT+0Qo= X-Google-Smtp-Source: APiQypKL8m3duLe5VgVrHczSVAT0AgTG9Z1mCIfwOknW7i5byxCQhNB4VEscqB90VSq/YCKNwltcRQ== X-Received: by 2002:a63:d24b:: with SMTP id t11mr15180019pgi.312.1589220201290; Mon, 11 May 2020 11:03:21 -0700 (PDT) Return-Path: Received: from LBRMN-LNXUB114.ric.broadcom.net ([192.19.228.250]) by smtp.gmail.com with ESMTPSA id f70sm9755447pfa.17.2020.05.11.11.03.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2020 11:03:20 -0700 (PDT) From: "Vladimir Olovyannikov" To: devel@edk2.groups.io Cc: Vladimir Olovyannikov , Samer El-Haj-Mahmoud , Zhichao Gao , Maciej Rabeda , Jiaxin Wu , Siyuan Fu , Ray Ni , Liming Gao , Nd Subject: [PATCH v2 1/1] ShellPkg/DynamicCommand: add HttpDynamicCommand Date: Mon, 11 May 2020 11:03:10 -0700 Message-Id: <20200511180310.30964-1-vladimir.olovyannikov@broadcom.com> X-Mailer: git-send-email 2.26.2.266.ge870325ee8 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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. Signed-off-by: Vladimir Olovyannikov Tested-By: Samer El-Haj-Mahmoud Cc: Zhichao Gao Cc: Maciej Rabeda Cc: Jiaxin Wu Cc: Siyuan Fu Cc: Ray Ni Cc: Liming Gao Cc: Nd --- .../DynamicCommand/HttpDynamicCommand/Http.c | 1701 +++++++++++++++++ .../DynamicCommand/HttpDynamicCommand/Http.h | 84 + .../HttpDynamicCommand/Http.uni | 113 ++ .../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 + 10 files changed, 2217 insertions(+) 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/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c b/ShellPkg/D= ynamicCommand/HttpDynamicCommand/Http.c new file mode 100644 index 000000000000..7238cc6a07cc --- /dev/null +++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c @@ -0,0 +1,1701 @@ +/** @file=0D + The implementation for the 'http' Shell command.=0D +=0D + Copyright (c) 2015, ARM Ltd. All rights reserved.
=0D + Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
= =0D + (C) Copyright 2015 Hewlett Packard Enterprise Development LP
=0D + Copyright (c) 2020, Broadcom. All rights reserved.
=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"-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 + CHAR16 *ProblemParam;=0D + UINTN ParamCount;=0D + CONST CHAR16 *UserNicName;=0D + BOOLEAN NicFound;=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 + UINTN HandleCount;=0D + UINTN NicNumber;=0D + UINTN InitialSize;=0D + UINTN ParamOffset;=0D + CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];= =0D + EFI_HANDLE ControllerHandle;=0D + EFI_HANDLE HttpChildHandle;=0D + DOWNLOAD_CONTEXT Context;=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 + UINTN StartSize;=0D + CHAR16 *Walker;=0D + CHAR16 *VStr;=0D +=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 (Walker =3D VStr; *Walker; Walker++) {=0D + if (*Walker =3D=3D L'/') {=0D + break;=0D + }=0D + }=0D +=0D + if (*Walker =3D=3D L'/') {=0D + ParamOffset =3D 1;=0D + RemoteFilePath =3D Walker;=0D + }=0D +=0D + Context.ServerAddrAndProto =3D StrnCatGrow (=0D + &Context.ServerAddrAndProto,=0D + &StartSize,=0D + ValueStr,=0D + StrLen (ValueStr) - StrLen (Walker)=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 + 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 + 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 + 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 redirect 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 + CHAR8 *Walker;=0D + CHAR16 *Temp;=0D + UINTN Idx;=0D + UINTN Step;=0D +=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_STATUS Status;=0D + VOID *MsgParser;=0D + BOOLEAN IsTrunked;=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->Respons= eToken);=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 + EFI_HTTP_HEADER *Header;=0D + BOOLEAN Redirection;=0D +=0D + Redirection =3D=0D + ((ResponseData.StatusCode >=3D HTTP_STATUS_300_MULTIPLE_CHOICES)= &&=0D + (ResponseData.StatusCode <=3D HTTP_STATUS_307_TEMPORARY_REDIREC= T)=0D + ) ||=0D + (ResponseData.StatusCode =3D=3D HTTP_STATUS_308_PERMANENT_REDIRE= CT);=0D +=0D + if (Redirection) {=0D + //=0D + // Need to repeat the request with new Location (server redirect= ed).=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, DownloadUr= l);=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 +=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, "chunke= d"));=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_REDIRE= CT)=0D + ) {=0D + //=0D + // Server reported an error via Response code.=0D + // Collect the body if any.=0D + //=0D + if (!gHttpError) {=0D + CONST CHAR16 *Desc;=0D +=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 +=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 + ShellCloseFile (&mFileHandle);=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 + 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..a142b96f6ad2 --- /dev/null +++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.h @@ -0,0 +1,84 @@ +/** @file=0D + Header file for 'http' command functions.=0D +=0D + Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
= =0D + Copyright (c) 2015, ARM Ltd. All rights reserved.
=0D + Copyright (c) 2020, Broadcom. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef _HTTP_H_=0D +#define _HTTP_H_=0D +=0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +=0D +#define HTTP_APP_NAME L"http"=0D +=0D +#define REQ_OK 0=0D +#define REQ_NEED_REPEAT 1=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 + 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..efe50e25819c --- /dev/null +++ b/ShellPkg/DynamicCommand/HttpDynamicCommand/Http.uni @@ -0,0 +1,113 @@ +// /**=0D +//=0D +// (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP
=0D +// Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.
= =0D +// Copyright (c) 2020, Broadcom. All rights reserved.
=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]\r\n"=0D +" [localfilepath]\r\n"=0D +".SH OPTIONS\r\n"=0D +" \r\n"=0D +" -i interface - Specifies an adapter name, i.e., eth0.\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 +" -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 :\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"=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 +" SHELL_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, it would be saved either as\r\= n=0D + localfilepath filename, 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."=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.
= =0D + Copyright (c) 2015, ARM Ltd. All rights reserved.
=0D + Copyright (c) 2020, Broadcom. All rights reserved.
=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.
= =0D +# Copyright (c) 2015, ARM Ltd. All rights reserved.
=0D +# Copyright (c) 2020, Broadcom. All rights reserved.
=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.
= =0D + Copyright (c) 2015, ARM Ltd. All rights reserved.
=0D + Copyright (c) 2020, Broadcom. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +#include =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.
= =0D +# Copyright (c) 2015, ARM Ltd. All rights reserved.
=0D +# Copyright (c) 2020, Broadcom. All rights reserved.
=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 c17e358d15ed..f80a2962dfd1 100644 --- a/ShellPkg/ShellPkg.dec +++ b/ShellPkg/ShellPkg.dec @@ -50,6 +50,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 b7ee856b3a16..58aa6e12bea3 100644 --- a/ShellPkg/ShellPkg.dsc +++ b/ShellPkg/ShellPkg.dsc @@ -138,6 +138,11 @@ [Components] gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE=0D }=0D ShellPkg/DynamicCommand/TftpDynamicCommand/TftpApp.inf=0D + ShellPkg/DynamicCommand/HttpDynamicCommand/HttpDynamicCommand.inf {=0D + =0D + gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE=0D + }=0D + ShellPkg/DynamicCommand/HttpDynamicCommand/HttpApp.inf=0D ShellPkg/DynamicCommand/DpDynamicCommand/DpDynamicCommand.inf {=0D =0D gEfiShellPkgTokenSpaceGuid.PcdShellLibAutoInitialize|FALSE=0D --=20 2.26.2.266.ge870325ee8