[PATCH v3 10/15] efi_loader: net: add support to send http requests and parse http headers
Heinrich Schuchardt
xypron.glpk at gmx.de
Tue Nov 12 14:51:24 CET 2024
On 11.11.24 22:09, Adriano Cordova wrote:
> Add network-stack agnostic way to send an http request and
> parse http headers from efi drivers. This uses wget as a
> backend and communicates with it via efi_wget_info.
>
> The function efi_net_do_request allocates a buffer on behalf of an
> efi application using efi_alloc and passes it to wget to receive
> the data. If the method is GET and the buffer is too small, it
> re-allocates the buffer based on the last received Content-Length
> header and tries again. If the method is HEAD it just issues one
> request. So issuing a HEAD request (to update Content-Length) and
> then a GET request is preferred but not required.
>
> The function efi_net_parse_headers parses a raw buffer containing
> an http header into an array of EFI specific 'http_header' structs.
>
> Signed-off-by: Adriano Cordova <adrianox at gmail.com>
> ---
>
> Changes in v3:
> - Add a struct wget_http_info efi_wget_info for efi_net to pass to wget
> when issuing wget requests, instead of using the old wget_info (see v2
> of the series 'wget: Expose wget to applications').
> - Let a pointer to the http status code be passed to efi_net_do_request
> for it to fill it for the client after a request.
>
> include/efi_loader.h | 13 ++++
> lib/efi_loader/efi_net.c | 158 +++++++++++++++++++++++++++++++++++++++
> 2 files changed, 171 insertions(+)
>
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index f49f8e6be0..4d05c08441 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -16,6 +16,7 @@
> #include <image.h>
> #include <pe.h>
> #include <linux/list.h>
> +#include <linux/sizes.h>
> #include <linux/oid_registry.h>
>
> struct blk_desc;
> @@ -136,6 +137,18 @@ void efi_net_get_addr(struct efi_ipv4_address *ip,
> void efi_net_set_addr(struct efi_ipv4_address *ip,
> struct efi_ipv4_address *mask,
> struct efi_ipv4_address *gw);
> +efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, ulong *buffer,
> + u32 *status_code, ulong *file_size, char *headers_buffer);
> +#define MAX_HTTP_HEADERS_SIZE SZ_64K
> +#define MAX_HTTP_HEADERS 100
> +#define MAX_HTTP_HEADER_NAME 128
> +#define MAX_HTTP_HEADER_VALUE 512
> +struct http_header {
> + uchar name[MAX_HTTP_HEADER_NAME];
> + uchar value[MAX_HTTP_HEADER_VALUE];
> +};
> +
> +void efi_net_parse_headers(ulong *num_headers, struct http_header *headers);
> #else
> static inline void efi_net_set_dp(const char *dev, const char *server) { }
> static inline void efi_net_get_dp(void **dp) { }
> diff --git a/lib/efi_loader/efi_net.c b/lib/efi_loader/efi_net.c
> index d4570d4d7c..30234ae1c7 100644
> --- a/lib/efi_loader/efi_net.c
> +++ b/lib/efi_loader/efi_net.c
> @@ -17,6 +17,7 @@
>
> #include <efi_loader.h>
> #include <dm.h>
> +#include <linux/sizes.h>
> #include <malloc.h>
> #include <vsprintf.h>
> #include <net.h>
> @@ -34,6 +35,12 @@ static int rx_packet_num;
> static struct efi_net_obj *netobj;
> static struct efi_device_path *net_dp;
>
> +static struct wget_http_info efi_wget_info = {
> + .set_bootdev = false,
> + .check_buffer_size = true,
> +
> +};
> +
> /*
> * The notification function of this event is called in every timer cycle
> * to check if a new network packet has been received.
> @@ -1003,6 +1010,10 @@ out_of_resources:
> return EFI_OUT_OF_RESOURCES;
> }
>
> +/**
> + * efi_net_set_dp() - set device path of efi net device
> + *
> + */
> void efi_net_set_dp(const char *dev, const char *server)
> {
> if (!strcmp(dev, "Net"))
> @@ -1011,6 +1022,10 @@ void efi_net_set_dp(const char *dev, const char *server)
> net_dp = efi_dp_from_http(server);
> }
>
> +/**
> + * efi_net_set_dp() - get device path of efi net device
> + *
> + */
> void efi_net_get_dp(void **dp)
> {
> if (!net_dp)
> @@ -1019,6 +1034,10 @@ void efi_net_get_dp(void **dp)
> *dp = efi_dp_dup(net_dp);
> }
>
> +/**
> + * efi_net_get_addr() - get IP address information
> + *
> + */
> void efi_net_get_addr(struct efi_ipv4_address *ip,
> struct efi_ipv4_address *mask,
> struct efi_ipv4_address *gw)
> @@ -1068,6 +1087,10 @@ void efi_net_get_addr(struct efi_ipv4_address *ip,
> #endif
> }
>
> +/**
> + * efi_net_set_addr() - set IP address information
> + *
> + */
> void efi_net_set_addr(struct efi_ipv4_address *ip,
> struct efi_ipv4_address *mask,
> struct efi_ipv4_address *gw)
> @@ -1117,3 +1140,138 @@ void efi_net_set_addr(struct efi_ipv4_address *ip,
> memcpy((void *)&net_netmask, (void *)mask, 4);
> #endif
> }
> +
> +/**
> + * efi_net_set_buffer() - allocate a buffer of min 64K
Please, describe the parameters.
> + *
> + */
> +static efi_status_t efi_net_set_buffer(ulong *buffer, ulong size)
Please, use size_t for sizes.
All invocations use size = 0. So we should drop the parameter.
> +{
> + efi_status_t ret = EFI_SUCCESS;
> +
> + if (size < SZ_64K)
> + size = SZ_64K;
> +
> + efi_free_pool((void *)*buffer);
> +
> + *buffer = (ulong)efi_alloc(size);
efi_alloc() returns a pointer.
There is no good reason to store pointers in ulong. Please, use void *
for pointers.
Patch 13/15 shows the resulting confusion:
In efi_http_send_data() you assume http_load_addr is a sandbox virtual
address:
ptr = map_sysmem(inst->http_load_addr + inst->current_offset,
transfer_size);
And a few lines below you assume that it is a pointer:
efi_free_pool((void *)inst->http_load_addr);
We should keep sandbox specific addresses which need map_sysmem() and
map_to_sysmem() out of the EFI implementation as far as possible.
So let parameter buffer be of type **void here.
We need a test that we can run both on the sandbox and on Q
> + if (!*buffer)
> + ret = EFI_OUT_OF_RESOURCES;
> +
> + efi_wget_info.buffer_size = size;
> + //debug("efi_net: set buffer of size %lu at %lu\n", size, *buffer);
It would be fine to have a debug statement here but, please, remove
lines that only where used during your development process.
Nits:
As described in doc/develop/codingstyle.rst:224:
"* Put a blank line before the last ``return`` in a function unless it
is the only line:"
> + return ret;
> +}
> +
> +/**
> + * efi_net_parse_headers() - parse HTTP headers
> + *
> + * Parses the raw buffer efi_wget_info.headers into an array headers
> + * of efi structs http_headers. The array should be at least
> + * MAX_HTTP_HEADERS long.
Please, describe the parameters.
> + */
> +void efi_net_parse_headers(ulong *num_headers, struct http_header *headers)
> +{
> + if (!num_headers || !headers)
> + return;
> +
> + // Populate info with http headers.
> + *num_headers = 0;
> + const uchar *line_start = efi_wget_info.headers;
> + const uchar *line_end;
> + ulong count;
> + struct http_header *current_header;
> + const uchar *separator;
> + size_t name_length, value_length;
> +
> + // Skip the first line (request or status line)
> + line_end = strstr(line_start, "\r\n");
> +
> + if (line_end)
> + line_start = line_end + 2;
> +
> + while ((line_end = strstr(line_start, "\r\n")) != NULL) {
> + count = *num_headers;
> + if (line_start == line_end || count >= MAX_HTTP_HEADERS)
> + break;
> + current_header = headers + count;
> + separator = strchr(line_start, ':');
> + if (separator) {
> + name_length = separator - line_start;
> + ++separator;
> + while (*separator == ' ')
> + ++separator;
> + value_length = line_end - separator;
> + if (name_length < MAX_HTTP_HEADER_NAME &&
> + value_length < MAX_HTTP_HEADER_VALUE) {
> + strncpy(current_header->name, line_start, name_length);
> + current_header->name[name_length] = '\0';
> + strncpy(current_header->value, separator, value_length);
> + current_header->value[value_length] = '\0';
> + (*num_headers)++;
> + }
> + }
> + line_start = line_end + 2;
> + }
> +}
> +
> +/**
> + * efi_net_do_request() - issue an HTTP request using wget
Please, complete the function description.
> + *
> + */
> +efi_status_t efi_net_do_request(u8 *url, enum efi_http_method method, ulong *buffer,
Please, use void **buffer. Below you are assuming *buffer is a pointer
and not a sandbox virtual address.
> + u32 *status_code, ulong *file_size, char *headers_buffer)
> +{
> + efi_status_t ret = EFI_SUCCESS;
> + int wget_ret;
> + static bool last_head;
> +
> + if (!buffer || !file_size)
> + return EFI_ABORTED;
> +
> + efi_wget_info.method = (enum wget_http_method)method;
Parameter method has type 'enum wget_http_method'. Please, remove the
superfluous conversion.
Best regards
Heinrich
> + efi_wget_info.headers = headers_buffer;
> +
> + switch (method) {
> + case HTTP_METHOD_GET:
> + ret = efi_net_set_buffer(buffer, last_head ? efi_wget_info.hdr_cont_len : 0);
> + if (ret != EFI_SUCCESS)
> + goto out;
> + wget_ret = wget_request((ulong)*buffer, url, &efi_wget_info);
> + if ((ulong)efi_wget_info.hdr_cont_len > efi_wget_info.buffer_size) {
> + // Try again with updated buffer size
> + ret = efi_net_set_buffer(buffer, (ulong)efi_wget_info.hdr_cont_len);
> + if (ret != EFI_SUCCESS)
> + goto out;
> + if (wget_request((ulong)*buffer, url, &efi_wget_info)) {
> + efi_free_pool((void *)*buffer);
> + ret = EFI_DEVICE_ERROR;
> + goto out;
> + }
> + } else if (wget_ret) {
> + efi_free_pool((void *)*buffer);
> + ret = EFI_DEVICE_ERROR;
> + goto out;
> + }
> + // Pass the actual number of received bytes to the application
> + *file_size = efi_wget_info.file_size;
> + *status_code = efi_wget_info.status_code;
> + last_head = false;
> + break;
> + case HTTP_METHOD_HEAD:
> + ret = efi_net_set_buffer(buffer, 0);
> + if (ret != EFI_SUCCESS)
> + goto out;
> + wget_request((ulong)*buffer, url, &efi_wget_info);
> + *file_size = 0;
> + *status_code = efi_wget_info.status_code;
> + last_head = true;
> + break;
> + default:
> + ret = EFI_UNSUPPORTED;
> + break;
> + }
> +
> +out:
> + return ret;
> +}
More information about the U-Boot
mailing list