Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for HTTP Boot through Proxy Server #10777

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,11 @@ BmExpandLoadFiles (
UINTN HandleCount;
UINTN Index;
EFI_DEVICE_PATH_PROTOCOL *Node;
EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
EFI_DEVICE_PATH_PROTOCOL *HttpPath;
URI_DEVICE_PATH *NullUriPath;

NullUriPath = NULL;

//
// Get file buffer from load file instance.
Expand Down Expand Up @@ -1573,11 +1578,50 @@ BmExpandLoadFiles (

for (Index = 0; Index < HandleCount; Index++) {
if (BmMatchHttpBootDevicePath (DevicePathFromHandle (Handles[Index]), FilePath)) {
//
// Matches HTTP Boot Device Path described as
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv4(...)[/Dns(...)]/Uri(...)
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv6(...)[/Dns(...)]/Uri(...)
//
Handle = Handles[Index];
goto Done;
}
}

NullUriPath = (URI_DEVICE_PATH *)CreateDeviceNode (
MESSAGING_DEVICE_PATH,
MSG_URI_DP,
(UINT16)(sizeof (URI_DEVICE_PATH))
);
for (Index = 0; Index < HandleCount; Index++) {
if ((Handles == NULL) || (Handles[Index] == NULL)) {
continue;
}

NewDevicePath = DevicePathFromHandle (Handles[Index]);
if (NewDevicePath == NULL) {
continue;
}

HttpPath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)NullUriPath);
if (HttpPath == NULL) {
continue;
}

if (BmMatchHttpBootDevicePath (HttpPath, FilePath)) {
//
// Matches HTTP Boot Device Path described as
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv4(...)[/Dns(...)]/Uri(...)/Uri(...)
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv6(...)[/Dns(...)]/Uri(...)/Uri(...)
//
Handle = Handles[Index];
break;
}
}

FreePool (NullUriPath);

Done:
if (Handles != NULL) {
FreePool (Handles);
}
Expand Down
4 changes: 2 additions & 2 deletions MdeModulePkg/Library/UefiBootManagerLib/BmBootDescription.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,8 @@ BmGetNetworkDescription (
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv6(...)
//
// The HTTP device path is like:
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv4(...)[/Dns(...)]/Uri(...)
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv6(...)[/Dns(...)]/Uri(...)
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv4(...)[/Dns(...)][/Uri(...)]/Uri(...)
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv6(...)[/Dns(...)][/Uri(...)]/Uri(...)
//
while (!IsDevicePathEnd (DevicePath) &&
((DevicePathType (DevicePath) != MESSAGING_DEVICE_PATH) ||
Expand Down
17 changes: 16 additions & 1 deletion MdePkg/Include/Protocol/Http.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,26 @@ typedef struct {
/// The URI of a remote host. From the information in this field, the HTTP instance
/// will be able to determine whether to use HTTP or HTTPS and will also be able to
/// determine the port number to use. If no port number is specified, port 80 (HTTP)
/// is assumed. See RFC 3986 for more details on URI syntax.
/// or 443 (HTTPS) is assumed. See RFC 3986 for more details on URI syntax.
///
CHAR16 *Url;
} EFI_HTTP_REQUEST_DATA;

///
/// EFI_HTTP_CONNECT_REQUEST_DATA
///
typedef struct {
EFI_HTTP_REQUEST_DATA Base;
///
/// The URI of an Proxy Host. This field will be NULL if there is no Proxy Host
/// in the device path. From the information in this field, the HTTP instance will
/// be able to determine whether to use HTTP or HTTPS and will also be able to
/// determine the port number to use. If no port number is specified, port 80 (HTTP)
/// or 443 (HTTPS) is assumed. See RFC 3986 for more details on URI syntax.
///
CHAR16 *ProxyUrl;
} EFI_HTTP_CONNECT_REQUEST_DATA;

///
/// EFI_HTTP_RESPONSE_DATA
///
Expand Down
253 changes: 220 additions & 33 deletions NetworkPkg/HttpBootDxe/HttpBootClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,52 +664,63 @@ HttpBootFreeCache (
IN HTTP_BOOT_CACHE_CONTENT *Cache
)
{
UINTN Index;
LIST_ENTRY *Entry;
LIST_ENTRY *NextEntry;
HTTP_BOOT_ENTITY_DATA *EntityData;
UINTN Index;
LIST_ENTRY *Entry;
LIST_ENTRY *NextEntry;
HTTP_BOOT_ENTITY_DATA *EntityData;
EFI_HTTP_CONNECT_REQUEST_DATA *ConnRequestData;

if (Cache != NULL) {
//
// Free the request data
//
if (Cache->RequestData != NULL) {
if (Cache->RequestData->Url != NULL) {
FreePool (Cache->RequestData->Url);
}
if (Cache == NULL) {
return;
}

FreePool (Cache->RequestData);
//
// Free the request data
//
if (Cache->RequestData != NULL) {
if (Cache->RequestData->Url != NULL) {
FreePool (Cache->RequestData->Url);
}

//
// Free the response header
//
if (Cache->ResponseData != NULL) {
if (Cache->ResponseData->Headers != NULL) {
for (Index = 0; Index < Cache->ResponseData->HeaderCount; Index++) {
FreePool (Cache->ResponseData->Headers[Index].FieldName);
FreePool (Cache->ResponseData->Headers[Index].FieldValue);
}
if (Cache->RequestData->Method == HttpMethodConnect) {
ConnRequestData = (EFI_HTTP_CONNECT_REQUEST_DATA *)Cache->RequestData;

FreePool (Cache->ResponseData->Headers);
if (ConnRequestData->ProxyUrl != NULL) {
FreePool (ConnRequestData->ProxyUrl);
}
}

//
// Free the response body
//
NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Cache->EntityDataList) {
EntityData = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_ENTITY_DATA, Link);
if (EntityData->Block != NULL) {
FreePool (EntityData->Block);
FreePool (Cache->RequestData);
}

//
// Free the response header
//
if (Cache->ResponseData != NULL) {
if (Cache->ResponseData->Headers != NULL) {
for (Index = 0; Index < Cache->ResponseData->HeaderCount; Index++) {
FreePool (Cache->ResponseData->Headers[Index].FieldName);
FreePool (Cache->ResponseData->Headers[Index].FieldValue);
}

RemoveEntryList (&EntityData->Link);
FreePool (EntityData);
FreePool (Cache->ResponseData->Headers);
}
}

FreePool (Cache);
//
// Free the response body
//
NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Cache->EntityDataList) {
EntityData = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_ENTITY_DATA, Link);
if (EntityData->Block != NULL) {
FreePool (EntityData->Block);
}

RemoveEntryList (&EntityData->Link);
FreePool (EntityData);
}

FreePool (Cache);
}

/**
Expand Down Expand Up @@ -901,6 +912,182 @@ HttpBootGetBootFileCallback (
return EFI_SUCCESS;
}

/**
This function establishes a connection through a proxy server

@param[in] Private The pointer to the driver's private data.

@retval EFI_SUCCESS Connection successful.
@retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
@retval Others Unexpected error happened.

**/
EFI_STATUS
HttpBootConnectProxy (
IN HTTP_BOOT_PRIVATE_DATA *Private
)
{
EFI_STATUS Status;
EFI_HTTP_STATUS_CODE StatusCode;
CHAR8 *HostName;
EFI_HTTP_CONNECT_REQUEST_DATA *RequestData;
HTTP_IO_RESPONSE_DATA *ResponseData;
HTTP_IO *HttpIo;
HTTP_IO_HEADER *HttpIoHeader;
CHAR16 *Url;
CHAR16 *ProxyUrl;
UINTN UrlSize;

Url = NULL;
ProxyUrl = NULL;
RequestData = NULL;
ResponseData = NULL;
HttpIoHeader = NULL;

UrlSize = AsciiStrSize (Private->BootFileUri);
Url = AllocatePool (UrlSize * sizeof (CHAR16));
if (Url == NULL) {
return EFI_OUT_OF_RESOURCES;
}

AsciiStrToUnicodeStrS (Private->BootFileUri, Url, UrlSize);

UrlSize = AsciiStrSize (Private->ProxyUri);
ProxyUrl = AllocatePool (UrlSize * (sizeof (CHAR16)));
if (ProxyUrl == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}

AsciiStrToUnicodeStrS (Private->ProxyUri, ProxyUrl, UrlSize);

//
// Send HTTP request message.
//

//
// Build HTTP header for the request, 2 headers are needed to send a CONNECT method:
// Host
// User
//
HttpIoHeader = HttpIoCreateHeader (2);
if (HttpIoHeader == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}

//
// Add HTTP header field 1: Host (EndPoint URI)
//
HostName = NULL;
Status = HttpUrlGetHostName (
Private->BootFileUri,
Private->BootFileUriParser,
&HostName
);
if (EFI_ERROR (Status)) {
goto EXIT;
}

Status = HttpIoSetHeader (
HttpIoHeader,
HTTP_HEADER_HOST,
HostName
);
if (EFI_ERROR (Status)) {
goto EXIT;
}

//
// Add HTTP header field 2: User-Agent
//
Status = HttpIoSetHeader (
HttpIoHeader,
HTTP_HEADER_USER_AGENT,
HTTP_USER_AGENT_EFI_HTTP_BOOT
);
if (EFI_ERROR (Status)) {
goto EXIT;
}

//
// Build the rest of HTTP request info.
//
RequestData = AllocatePool (sizeof (EFI_HTTP_CONNECT_REQUEST_DATA));
if (RequestData == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}

RequestData->Base.Method = HttpMethodConnect;
RequestData->Base.Url = Url;
RequestData->ProxyUrl = ProxyUrl;

//
// Send out the request to HTTP server.
//
HttpIo = &Private->HttpIo;
Status = HttpIoSendRequest (
HttpIo,
&RequestData->Base,
HttpIoHeader->HeaderCount,
HttpIoHeader->Headers,
0,
NULL
);
if (EFI_ERROR (Status)) {
goto EXIT;
}

//
// Receive HTTP response message.
//

//
// Use zero BodyLength to only receive the response headers.
//
ResponseData = AllocateZeroPool (sizeof (HTTP_IO_RESPONSE_DATA));
if (ResponseData == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}

Status = HttpIoRecvResponse (
&Private->HttpIo,
TRUE,
ResponseData
);

if (EFI_ERROR (Status) || EFI_ERROR (ResponseData->Status)) {
if (EFI_ERROR (ResponseData->Status)) {
StatusCode = HttpIo->RspToken.Message->Data.Response->StatusCode;
HttpBootPrintErrorMessage (StatusCode);
Status = ResponseData->Status;
}
}

EXIT:
if (ResponseData != NULL) {
FreePool (ResponseData);
}

if (RequestData != NULL) {
FreePool (RequestData);
}

HttpIoFreeHeader (HttpIoHeader);

if (ProxyUrl != NULL) {
FreePool (ProxyUrl);
}

if (Url != NULL) {
FreePool (Url);
}

return Status;
}

/**
This function download the boot file by using UEFI HTTP protocol.

Expand Down
Loading
Loading