From 6cc4d0492b2fb231c163e3a06918be3cde381506 Mon Sep 17 00:00:00 2001 From: Adriano Cordova Date: Mon, 11 Nov 2024 18:09:00 -0300 Subject: net/lwip: wget: put server_name and port into wget_ctx Currently server_name and port are local variables in wget_loop. This commit puts them inside ctx, so that they are accessible from the http callbacks. Signed-off-by: Adriano Cordova --- net/lwip/wget.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'net/lwip/wget.c') diff --git a/net/lwip/wget.c b/net/lwip/wget.c index b495ebd1aa9..4add520045f 100644 --- a/net/lwip/wget.c +++ b/net/lwip/wget.c @@ -23,6 +23,8 @@ enum done_state { }; struct wget_ctx { + char server_name[SERVER_NAME_SIZE]; + u16 port; char *path; ulong daddr; ulong saved_daddr; @@ -209,13 +211,11 @@ static void httpc_result_cb(void *arg, httpc_result_t httpc_result, static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) { - char server_name[SERVER_NAME_SIZE]; httpc_connection_t conn; httpc_state_t *state; struct netif *netif; struct wget_ctx ctx; char *path; - u16 port; ctx.daddr = dst_addr; ctx.saved_daddr = dst_addr; @@ -224,7 +224,7 @@ static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) ctx.prevsize = 0; ctx.start_time = 0; - if (parse_url(uri, server_name, &port, &path)) + if (parse_url(uri, ctx.server_name, &ctx.port, &path)) return CMD_RET_USAGE; netif = net_lwip_new_netif(udev); @@ -234,7 +234,7 @@ static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) memset(&conn, 0, sizeof(conn)); conn.result_fn = httpc_result_cb; ctx.path = path; - if (httpc_get_file_dns(server_name, port, path, &conn, httpc_recv_cb, + if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb, &ctx, &state)) { net_lwip_remove_netif(netif); return CMD_RET_FAILURE; -- cgit v1.2.3 From 1327c2a8d6514d4abfa3642ce338fed50b811b21 Mon Sep 17 00:00:00 2001 From: Adriano Cordova Date: Mon, 11 Nov 2024 18:09:01 -0300 Subject: net/lwip: wget: integrate struct wget_info into wget code Each wget request now fills the struct wget_info. Also, the efi bootdevice is now set conditionally to the set_bootdevice variable in wget_info and a buffer size check is performed if check_buffer_size is set. Signed-off-by: Adriano Cordova --- net/lwip/wget.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'net/lwip/wget.c') diff --git a/net/lwip/wget.c b/net/lwip/wget.c index 4add520045f..53c3b169e01 100644 --- a/net/lwip/wget.c +++ b/net/lwip/wget.c @@ -34,6 +34,18 @@ struct wget_ctx { enum done_state done; }; +static void wget_lwip_fill_info(struct pbuf *hdr, u16_t hdr_len, u32_t hdr_cont_len) +{ + if (wget_info->headers && hdr_len < MAX_HTTP_HEADERS_SIZE) + pbuf_copy_partial(hdr, (void *)wget_info->headers, hdr_len, 0); + wget_info->hdr_cont_len = (u32)hdr_cont_len; +} + +static void wget_lwip_set_file_size(u32_t rx_content_len) +{ + wget_info->file_size = (ulong)rx_content_len; +} + static int parse_url(char *url, char *host, u16 *port, char **path) { char *p, *pp; @@ -178,6 +190,13 @@ static void httpc_result_cb(void *arg, httpc_result_t httpc_result, struct wget_ctx *ctx = arg; ulong elapsed; + wget_info->status_code = (u32)srv_res; + + if (err == ERR_BUF) { + ctx->done = FAILURE; + return; + } + if (httpc_result != HTTPC_RESULT_OK) { log_err("\nHTTP client error %d\n", httpc_result); ctx->done = FAILURE; @@ -197,8 +216,11 @@ static void httpc_result_cb(void *arg, httpc_result_t httpc_result, printf("%u bytes transferred in %lu ms (", rx_content_len, elapsed); print_size(rx_content_len / elapsed * 1000, "/s)\n"); printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size); - efi_set_bootdev("Net", "", ctx->path, map_sysmem(ctx->saved_daddr, 0), - rx_content_len); + if (wget_info->set_bootdev) { + efi_set_bootdev("Net", "", ctx->path, map_sysmem(ctx->saved_daddr, 0), + rx_content_len); + } + wget_lwip_set_file_size(rx_content_len); if (env_set_hex("filesize", rx_content_len) || env_set_hex("fileaddr", ctx->saved_daddr)) { log_err("Could not set filesize or fileaddr\n"); @@ -209,6 +231,17 @@ static void httpc_result_cb(void *arg, httpc_result_t httpc_result, ctx->done = SUCCESS; } +static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct pbuf *hdr, + u16_t hdr_len, u32_t content_len) +{ + wget_lwip_fill_info(hdr, hdr_len, content_len); + + if (wget_info->check_buffer_size && (ulong)content_len > wget_info->buffer_size) + return ERR_BUF; + + return ERR_OK; +} + static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) { httpc_connection_t conn; @@ -233,6 +266,7 @@ static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) memset(&conn, 0, sizeof(conn)); conn.result_fn = httpc_result_cb; + conn.headers_done_fn = httpc_headers_done_cb; ctx.path = path; if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb, &ctx, &state)) { @@ -259,6 +293,9 @@ int wget_with_dns(ulong dst_addr, char *uri) { eth_set_current(); + if (!wget_info) + wget_info = &default_wget_info; + return wget_loop(eth_get_dev(), dst_addr, uri); } @@ -285,6 +322,7 @@ int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) if (parse_legacy_arg(url, nurl, sizeof(nurl))) return CMD_RET_FAILURE; + wget_info = &default_wget_info; if (wget_with_dns(dst_addr, nurl)) return CMD_RET_FAILURE; -- cgit v1.2.3 From ab49ede318751c8a1920e3fd5957f16e4df7f490 Mon Sep 17 00:00:00 2001 From: Adriano Cordova Date: Tue, 3 Dec 2024 09:55:34 -0300 Subject: lwip: wget: pass port and server_name via wget_ctx Commit 5907c81 ("net: lwip: Enable https:// support for wget") was not correctly rebased on top of the changes introduced by Commit 6cc4d04 ("net/lwip: wget: put server_name and port into wget_ctx") in next. This commit re-applies a couple of lines from 6cc4d04. Fixes: Commit 5907c81 ("net: lwip: Enable https:// support for wget") Signed-off-by: Adriano Cordova --- net/lwip/wget.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'net/lwip/wget.c') diff --git a/net/lwip/wget.c b/net/lwip/wget.c index 46858cb5dd3..5a64f6887c9 100644 --- a/net/lwip/wget.c +++ b/net/lwip/wget.c @@ -283,7 +283,6 @@ static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) { - char server_name[SERVER_NAME_SIZE]; #if defined CONFIG_WGET_HTTPS altcp_allocator_t tls_allocator; #endif @@ -292,7 +291,6 @@ static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) struct netif *netif; struct wget_ctx ctx; char *path; - u16 port; bool is_https; ctx.daddr = dst_addr; @@ -302,7 +300,7 @@ static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) ctx.prevsize = 0; ctx.start_time = 0; - if (parse_url(uri, server_name, &port, &path, &is_https)) + if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https)) return CMD_RET_USAGE; netif = net_lwip_new_netif(udev); @@ -314,7 +312,7 @@ static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) if (is_https) { tls_allocator.alloc = &altcp_tls_alloc; tls_allocator.arg = - altcp_tls_create_config_client(NULL, 0, server_name); + altcp_tls_create_config_client(NULL, 0, ctx.server_name); if (!tls_allocator.arg) { log_err("error: Cannot create a TLS connection\n"); -- cgit v1.2.3 From dc7c8a2532925fa526ca6bd35b0519d5e3e97aed Mon Sep 17 00:00:00 2001 From: Adriano Cordova Date: Tue, 26 Nov 2024 13:19:21 -0300 Subject: net-lwip: zero terminate string with headers in wget_lwip_fill_info() This patch comes as a companion to the same patch but for the legacy net stack. Commit 1327c2a8d6 ("net/lwip: wget: integrate struct wget_info into wget code") introduced function wget_fill_info() which retrieves the headers from the HTTP server response. As we want to parse the string in later patches we need to ensure that it is NUL terminated. We must further check that wget_info->headers in not NULL. Otherwise a crash occurs. Signed-off-by: Adriano Cordova --- net/lwip/wget.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'net/lwip/wget.c') diff --git a/net/lwip/wget.c b/net/lwip/wget.c index 5a64f6887c9..263d6dab26c 100644 --- a/net/lwip/wget.c +++ b/net/lwip/wget.c @@ -40,8 +40,13 @@ struct wget_ctx { static void wget_lwip_fill_info(struct pbuf *hdr, u16_t hdr_len, u32_t hdr_cont_len) { - if (wget_info->headers && hdr_len < MAX_HTTP_HEADERS_SIZE) - pbuf_copy_partial(hdr, (void *)wget_info->headers, hdr_len, 0); + if (wget_info->headers) { + if (hdr_len < MAX_HTTP_HEADERS_SIZE) + pbuf_copy_partial(hdr, (void *)wget_info->headers, hdr_len, 0); + else + hdr_len = 0; + wget_info->headers[hdr_len] = 0; + } wget_info->hdr_cont_len = (u32)hdr_cont_len; } -- cgit v1.2.3 From 9bab7d2a7c37b486ed6c368cfdfb42575ab112e4 Mon Sep 17 00:00:00 2001 From: Adriano Cordova Date: Wed, 4 Dec 2024 00:05:16 -0300 Subject: net: wget: let wget_with_dns work with dns disabled This was marked as TODO in the code: - Enable use of wget_with_dns even if CMD_DNS is disabled if the given uri has the ip address for the http server. - Move the check for CMD_DNS inside wget_with_dns. - Rename wget_with_dns to wget_do_request Signed-off-by: Adriano Cordova Reviewed-by: Heinrich Schuchardt Reviewed-by: Jerome Forissier --- net/lwip/wget.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'net/lwip/wget.c') diff --git a/net/lwip/wget.c b/net/lwip/wget.c index 263d6dab26c..669eded0f38 100644 --- a/net/lwip/wget.c +++ b/net/lwip/wget.c @@ -353,7 +353,7 @@ static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) return -1; } -int wget_with_dns(ulong dst_addr, char *uri) +int wget_do_request(ulong dst_addr, char *uri) { eth_set_current(); @@ -387,7 +387,7 @@ int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) return CMD_RET_FAILURE; wget_info = &default_wget_info; - if (wget_with_dns(dst_addr, nurl)) + if (wget_do_request(dst_addr, nurl)) return CMD_RET_FAILURE; return CMD_RET_SUCCESS; -- cgit v1.2.3 From e55a4acb54e807c6411c4f6ab914fa2b3f55784e Mon Sep 17 00:00:00 2001 From: Adriano Cordova Date: Wed, 4 Dec 2024 00:05:23 -0300 Subject: efi_loader: net: set EFI bootdevice device path to HTTP when loaded from wget Set the device path of the efi boot device to an HTTP device path (as formed by efi_dp_from_http) when the next boot stage is loaded using wget (i.e., when wget is used with wget_info.set_bootdev=1). When loaded from HTTP, the device path should account for it so that the next boot stage is aware (e.g. grub only loads its http stack if it itself was loaded from http, and it checks this from its device path). Signed-off-by: Adriano Cordova Reviewed-by: Heinrich Schuchardt --- net/lwip/wget.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'net/lwip/wget.c') diff --git a/net/lwip/wget.c b/net/lwip/wget.c index 669eded0f38..c23f0640ec6 100644 --- a/net/lwip/wget.c +++ b/net/lwip/wget.c @@ -260,10 +260,9 @@ static void httpc_result_cb(void *arg, httpc_result_t httpc_result, printf("%u bytes transferred in %lu ms (", rx_content_len, elapsed); print_size(rx_content_len / elapsed * 1000, "/s)\n"); printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size); - if (wget_info->set_bootdev) { - efi_set_bootdev("Net", "", ctx->path, map_sysmem(ctx->saved_daddr, 0), + if (wget_info->set_bootdev) + efi_set_bootdev("Http", ctx->server_name, ctx->path, map_sysmem(ctx->saved_daddr, 0), rx_content_len); - } wget_lwip_set_file_size(rx_content_len); if (env_set_hex("filesize", rx_content_len) || env_set_hex("fileaddr", ctx->saved_daddr)) { -- cgit v1.2.3