public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Adriano Cordova <adrianox@gmail.com>
To: u-boot@lists.denx.de
Cc: joe.hershberger@ni.com, rfried.dev@gmail.com,
	jerome.forissier@linaro.org, xypron.glpk@gmx.de,
	ilias.apalodimas@linaro.org, Adriano Cordova <adrianox@gmail.com>
Subject: [PATCH v2 10/15] efi_loader: net: add support to send http requests and parse http headers
Date: Fri,  8 Nov 2024 10:38:45 -0300	[thread overview]
Message-ID: <20241108133850.431075-11-adrianox@gmail.com> (raw)
In-Reply-To: <20241108133850.431075-1-adrianox@gmail.com>

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 the struct wget_http_info
'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@gmail.com>
---
 include/efi_loader.h     |  13 ++++
 lib/efi_loader/efi_net.c | 128 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 141 insertions(+)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index c37c1a4660..a19505d4cc 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, 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..43b57b63d5 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>
@@ -1117,3 +1118,130 @@ void efi_net_set_addr(struct efi_ipv4_address *ip,
 		memcpy((void *)&net_netmask, (void *)mask, 4);
 #endif
 }
+
+static efi_status_t efi_net_set_buffer(ulong *buffer, ulong size)
+{
+	efi_status_t ret = EFI_SUCCESS;
+
+	if (size < SZ_64K)
+		size = SZ_64K;
+
+	efi_free_pool((void *)*buffer);
+
+	*buffer = (ulong)efi_alloc(size);
+	if (!*buffer)
+		ret = EFI_OUT_OF_RESOURCES;
+
+	wget_info.buffer_size = size;
+	//debug("efi_net: set buffer of size %lu at %lu\n", size, *buffer);
+	return ret;
+}
+
+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 = 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_status_t efi_net_do_request(u8 *url, enum efi_http_method method,
+				ulong *buffer, ulong *file_size,
+				char *headers_buffer)
+{
+	efi_status_t ret = EFI_SUCCESS;
+	int wget_ret;
+	static bool last_head;
+	bool old_setbootdev, old_bufsize;
+
+	if (!buffer || !file_size)
+		return EFI_ABORTED;
+
+	wget_info.method = (enum wget_http_method)method;
+	wget_info.headers = headers_buffer;
+	// Do not set u-boot bootdevice when using wget from efi
+	old_setbootdev = wget_info.set_bootdev;
+	old_bufsize = wget_info.check_buffer_size;
+	wget_info.set_bootdev = false;
+	wget_info.check_buffer_size = true;
+
+	switch (method) {
+	case HTTP_METHOD_GET:
+		ret = efi_net_set_buffer(buffer, last_head ? wget_info.hdr_cont_len : 0);
+		if (ret != EFI_SUCCESS)
+			goto out;
+		wget_ret = wget_with_dns((ulong)*buffer, url);
+		if ((ulong)wget_info.hdr_cont_len > wget_info.buffer_size) {
+			// Try again with updated buffer size
+			ret = efi_net_set_buffer(buffer, (ulong)wget_info.hdr_cont_len);
+			if (ret != EFI_SUCCESS)
+				goto out;
+			if (wget_with_dns((ulong)*buffer, url)) {
+				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 = wget_info.file_size;
+		last_head = false;
+		break;
+	case HTTP_METHOD_HEAD:
+		ret = efi_net_set_buffer(buffer, 0);
+		if (ret != EFI_SUCCESS)
+			goto out;
+		wget_with_dns((ulong)*buffer, url);
+		*file_size = 0;
+		last_head = true;
+		break;
+	default:
+		ret = EFI_UNSUPPORTED;
+		break;
+	}
+
+out:
+	wget_info.set_bootdev = old_setbootdev;
+	wget_info.check_buffer_size = old_bufsize;
+	return ret;
+}
-- 
2.43.0


  parent reply	other threads:[~2024-11-08 15:57 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-08 13:38 [PATCH v2 00/15] efi_loader: efi http and ipconfig drivers Adriano Cordova
2024-11-08 13:38 ` [PATCH v2 01/15] net: net_utils: Move ip_to_string to lib/net_utils.c Adriano Cordova
2024-11-08 16:19   ` Heinrich Schuchardt
2024-11-10 11:37   ` Ilias Apalodimas
2024-11-08 13:38 ` [PATCH v2 02/15] net: wget: let wget_with_dns work with dns disabled Adriano Cordova
2024-11-08 13:38 ` [PATCH v2 03/15] efi_loader: device_path: add definition of DEVICE_PATH_SUB_TYPE_MSG_IPV4 Adriano Cordova
2024-11-10  9:01   ` Ilias Apalodimas
2024-11-08 13:38 ` [PATCH v2 04/15] efi_loader: device_path: add efi_dp_from_ipv4 Adriano Cordova
2024-11-10  9:13   ` Ilias Apalodimas
2024-11-08 13:38 ` [PATCH v2 05/15] efi_loader: add IPv4() to device path to text protocol Adriano Cordova
2024-11-08 13:38 ` [PATCH v2 06/15] efi_api: add definitions for HTTP and IP4_CONFIG2 protocols Adriano Cordova
2024-11-08 13:38 ` [PATCH v2 07/15] efi_loader: efi_net: add efi_net_set_addr, efi_net_get_addr Adriano Cordova
2024-11-08 13:38 ` [PATCH v2 08/15] efi_loader: device_path: add support for HTTP device path Adriano Cordova
2024-11-10  9:19   ` Ilias Apalodimas
2024-11-08 13:38 ` [PATCH v2 09/15] efi_loader: net: set EFI bootdevice device path to HTTP when loaded from wget Adriano Cordova
2024-11-08 13:38 ` Adriano Cordova [this message]
2024-11-08 13:38 ` [PATCH v2 11/15] efi_loader: efi_net: add EFI_IP4_CONFIG2_PROTOCOL Adriano Cordova
2024-11-08 13:38 ` [PATCH v2 12/15] efi_loader: efi_net: add EFI_HTTP_PROTOCOL Adriano Cordova
2024-11-08 13:38 ` [PATCH v2 13/15] lib: uuid: display HTTP and IPV4 Config II protocols Adriano Cordova
2024-11-10  8:41   ` Ilias Apalodimas
2024-11-10 12:03     ` Heinrich Schuchardt
2024-11-08 13:38 ` [PATCH v2 14/15] efi_selftest: add test for HTTP protocol Adriano Cordova
2024-11-08 13:38 ` [PATCH v2 15/15] efi_selftest: add test for IPv4 Config2 protocol Adriano Cordova

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241108133850.431075-11-adrianox@gmail.com \
    --to=adrianox@gmail.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jerome.forissier@linaro.org \
    --cc=joe.hershberger@ni.com \
    --cc=rfried.dev@gmail.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox