1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2024 Linaro Ltd. */
6 #include <display_options.h>
7 #include <efi_loader.h>
9 #include <lwip/apps/http_client.h>
10 #include "lwip/altcp_tls.h"
11 #include <lwip/timeouts.h>
16 #include <dm/uclass.h>
18 #define SERVER_NAME_SIZE 254
19 #define HTTP_PORT_DEFAULT 80
20 #define HTTPS_PORT_DEFAULT 443
21 #define PROGRESS_PRINT_STEP_BYTES (100 * 1024)
30 char server_name[SERVER_NAME_SIZE];
41 static void wget_lwip_fill_info(struct pbuf *hdr, u16_t hdr_len, u32_t hdr_cont_len)
43 if (wget_info->headers && hdr_len < MAX_HTTP_HEADERS_SIZE)
44 pbuf_copy_partial(hdr, (void *)wget_info->headers, hdr_len, 0);
45 wget_info->hdr_cont_len = (u32)hdr_cont_len;
48 static void wget_lwip_set_file_size(u32_t rx_content_len)
50 wget_info->file_size = (ulong)rx_content_len;
53 bool wget_validate_uri(char *uri);
55 int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len,
63 ret = uclass_get_device(UCLASS_RNG, 0, &dev);
65 log_err("Failed to get an rng: %d\n", ret);
68 ret = dm_rng_read(dev, output, len);
77 static int parse_url(char *url, char *host, u16 *port, char **path,
82 size_t prefix_len = 0;
84 if (!wget_validate_uri(url)) {
85 log_err("Invalid URL. Use http(s)://\n");
90 *port = HTTP_PORT_DEFAULT;
91 prefix_len = strlen("http://");
92 p = strstr(url, "http://");
94 p = strstr(url, "https://");
95 prefix_len = strlen("https://");
96 *port = HTTPS_PORT_DEFAULT;
109 if (p + SERVER_NAME_SIZE <= pp)
112 memcpy(host, p, pp - p);
116 /* Parse port number */
118 lport = simple_strtol(p, &pp, 10);
119 if (pp && *pp != '/')
134 * Legacy syntax support
135 * Convert [<server_name_or_ip>:]filename into a URL if needed
137 static int parse_legacy_arg(char *arg, char *nurl, size_t rem)
141 char *col = strchr(arg, ':');
146 if (strstr(arg, "http") == arg) {
147 n = snprintf(nurl, rem, "%s", arg);
148 if (n < 0 || n > rem)
153 n = snprintf(p, rem, "%s", "http://");
154 if (n < 0 || n > rem)
164 env = env_get("httpserverip");
166 env = env_get("serverip");
168 log_err("error: httpserver/serverip has to be set\n");
178 strlcpy(p, server, n);
199 static err_t httpc_recv_cb(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf,
202 struct wget_ctx *ctx = arg;
208 if (!ctx->start_time)
209 ctx->start_time = get_timer(0);
211 for (buf = pbuf; buf; buf = buf->next) {
212 memcpy((void *)ctx->daddr, buf->payload, buf->len);
213 ctx->daddr += buf->len;
214 ctx->size += buf->len;
215 if (ctx->size - ctx->prevsize > PROGRESS_PRINT_STEP_BYTES) {
217 ctx->prevsize = ctx->size;
221 altcp_recved(pcb, pbuf->tot_len);
226 static void httpc_result_cb(void *arg, httpc_result_t httpc_result,
227 u32_t rx_content_len, u32_t srv_res, err_t err)
229 struct wget_ctx *ctx = arg;
232 wget_info->status_code = (u32)srv_res;
234 if (err == ERR_BUF) {
239 if (httpc_result != HTTPC_RESULT_OK) {
240 log_err("\nHTTP client error %d\n", httpc_result);
244 if (srv_res != 200) {
245 log_err("\nHTTP server error %d\n", srv_res);
250 elapsed = get_timer(ctx->start_time);
253 if (rx_content_len > PROGRESS_PRINT_STEP_BYTES)
255 printf("%u bytes transferred in %lu ms (", rx_content_len, elapsed);
256 print_size(rx_content_len / elapsed * 1000, "/s)\n");
257 printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size);
258 if (wget_info->set_bootdev) {
259 efi_set_bootdev("Net", "", ctx->path, map_sysmem(ctx->saved_daddr, 0),
262 wget_lwip_set_file_size(rx_content_len);
263 if (env_set_hex("filesize", rx_content_len) ||
264 env_set_hex("fileaddr", ctx->saved_daddr)) {
265 log_err("Could not set filesize or fileaddr\n");
273 static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct pbuf *hdr,
274 u16_t hdr_len, u32_t content_len)
276 wget_lwip_fill_info(hdr, hdr_len, content_len);
278 if (wget_info->check_buffer_size && (ulong)content_len > wget_info->buffer_size)
284 static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
286 char server_name[SERVER_NAME_SIZE];
287 #if defined CONFIG_WGET_HTTPS
288 altcp_allocator_t tls_allocator;
290 httpc_connection_t conn;
291 httpc_state_t *state;
298 ctx.daddr = dst_addr;
299 ctx.saved_daddr = dst_addr;
305 if (parse_url(uri, server_name, &port, &path, &is_https))
306 return CMD_RET_USAGE;
308 netif = net_lwip_new_netif(udev);
312 memset(&conn, 0, sizeof(conn));
313 #if defined CONFIG_WGET_HTTPS
315 tls_allocator.alloc = &altcp_tls_alloc;
317 altcp_tls_create_config_client(NULL, 0, server_name);
319 if (!tls_allocator.arg) {
320 log_err("error: Cannot create a TLS connection\n");
321 net_lwip_remove_netif(netif);
325 conn.altcp_allocator = &tls_allocator;
329 conn.result_fn = httpc_result_cb;
330 conn.headers_done_fn = httpc_headers_done_cb;
332 if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb,
334 net_lwip_remove_netif(netif);
335 return CMD_RET_FAILURE;
339 net_lwip_rx(udev, netif);
340 sys_check_timeouts();
345 net_lwip_remove_netif(netif);
347 if (ctx.done == SUCCESS)
353 int wget_with_dns(ulong dst_addr, char *uri)
358 wget_info = &default_wget_info;
360 return wget_loop(eth_get_dev(), dst_addr, uri);
363 int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
370 if (argc < 2 || argc > 3)
371 return CMD_RET_USAGE;
373 dst_addr = hextoul(argv[1], &end);
374 if (end == (argv[1] + strlen(argv[1]))) {
376 return CMD_RET_USAGE;
379 dst_addr = image_load_addr;
383 if (parse_legacy_arg(url, nurl, sizeof(nurl)))
384 return CMD_RET_FAILURE;
386 wget_info = &default_wget_info;
387 if (wget_with_dns(dst_addr, nurl))
388 return CMD_RET_FAILURE;
390 return CMD_RET_SUCCESS;
394 * wget_validate_uri() - validate the uri for wget
398 * This function follows the current U-Boot wget implementation.
399 * scheme: only "http:" is supported
401 * - user information: not supported
403 * - port: not supported(always use the default port)
405 * Uri is expected to be correctly percent encoded.
406 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
407 * and space character(0x20) are not allowed.
409 * TODO: stricter uri conformance check
411 * Return: true on success, false on failure
413 bool wget_validate_uri(char *uri)
417 char *str_copy, *s, *authority;
418 size_t prefix_len = 0;
420 for (c = 0x1; c < 0x21; c++) {
421 if (strchr(uri, c)) {
422 log_err("invalid character is used\n");
427 if (strchr(uri, 0x7f)) {
428 log_err("invalid character is used\n");
432 if (!strncmp(uri, "http://", strlen("http://"))) {
433 prefix_len = strlen("http://");
434 } else if (!strncmp(uri, "https://", strlen("https://"))) {
435 prefix_len = strlen("https://");
437 log_err("only http(s):// is supported\n");
441 str_copy = strdup(uri);
445 s = str_copy + strlen("http://");
446 authority = strsep(&s, "/");
448 log_err("invalid uri, no file path\n");
452 s = strchr(authority, '@');
454 log_err("user information is not supported\n");