blob: 46858cb5dd31b3be3a0083295aeebbebd222c013 [file] [log] [blame]
Jerome Forissier359d4ed2024-10-16 12:04:09 +02001// SPDX-License-Identifier: GPL-2.0+
2/* Copyright (C) 2024 Linaro Ltd. */
3
4#include <command.h>
5#include <console.h>
6#include <display_options.h>
7#include <efi_loader.h>
8#include <image.h>
9#include <lwip/apps/http_client.h>
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020010#include "lwip/altcp_tls.h"
Jerome Forissier359d4ed2024-10-16 12:04:09 +020011#include <lwip/timeouts.h>
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020012#include <rng.h>
Jerome Forissier359d4ed2024-10-16 12:04:09 +020013#include <mapmem.h>
14#include <net.h>
15#include <time.h>
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020016#include <dm/uclass.h>
Jerome Forissier359d4ed2024-10-16 12:04:09 +020017
Heinrich Schuchardtd064fc92024-11-08 18:45:26 +010018#define SERVER_NAME_SIZE 254
Jerome Forissier359d4ed2024-10-16 12:04:09 +020019#define HTTP_PORT_DEFAULT 80
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020020#define HTTPS_PORT_DEFAULT 443
Jerome Forissier359d4ed2024-10-16 12:04:09 +020021#define PROGRESS_PRINT_STEP_BYTES (100 * 1024)
22
23enum done_state {
Jerome Forissier97083502024-11-07 12:27:57 +010024 NOT_DONE = 0,
25 SUCCESS = 1,
26 FAILURE = 2
Jerome Forissier359d4ed2024-10-16 12:04:09 +020027};
28
29struct wget_ctx {
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -030030 char server_name[SERVER_NAME_SIZE];
31 u16 port;
Jerome Forissier359d4ed2024-10-16 12:04:09 +020032 char *path;
33 ulong daddr;
34 ulong saved_daddr;
35 ulong size;
36 ulong prevsize;
37 ulong start_time;
38 enum done_state done;
39};
40
Adriano Cordova25e88412024-11-11 18:09:01 -030041static void wget_lwip_fill_info(struct pbuf *hdr, u16_t hdr_len, u32_t hdr_cont_len)
42{
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;
46}
47
48static void wget_lwip_set_file_size(u32_t rx_content_len)
49{
50 wget_info->file_size = (ulong)rx_content_len;
51}
52
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020053bool wget_validate_uri(char *uri);
54
55int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len,
56 size_t *olen)
57{
58 struct udevice *dev;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020059 int ret;
60
61 *olen = 0;
62
63 ret = uclass_get_device(UCLASS_RNG, 0, &dev);
64 if (ret) {
65 log_err("Failed to get an rng: %d\n", ret);
66 return ret;
67 }
Ilias Apalodimas4276a3c2024-11-14 16:29:15 +020068 ret = dm_rng_read(dev, output, len);
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020069 if (ret)
70 return ret;
71
Ilias Apalodimas4276a3c2024-11-14 16:29:15 +020072 *olen = len;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020073
74 return 0;
75}
76
77static int parse_url(char *url, char *host, u16 *port, char **path,
78 bool *is_https)
Jerome Forissier359d4ed2024-10-16 12:04:09 +020079{
80 char *p, *pp;
81 long lport;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020082 size_t prefix_len = 0;
Jerome Forissier359d4ed2024-10-16 12:04:09 +020083
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020084 if (!wget_validate_uri(url)) {
85 log_err("Invalid URL. Use http(s)://\n");
86 return -EINVAL;
87 }
88
89 *is_https = false;
90 *port = HTTP_PORT_DEFAULT;
91 prefix_len = strlen("http://");
Jerome Forissier359d4ed2024-10-16 12:04:09 +020092 p = strstr(url, "http://");
93 if (!p) {
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020094 p = strstr(url, "https://");
95 prefix_len = strlen("https://");
96 *port = HTTPS_PORT_DEFAULT;
97 *is_https = true;
Jerome Forissier359d4ed2024-10-16 12:04:09 +020098 }
99
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200100 p += prefix_len;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200101
102 /* Parse hostname */
103 pp = strchr(p, ':');
104 if (!pp)
105 pp = strchr(p, '/');
106 if (!pp)
107 return -EINVAL;
108
109 if (p + SERVER_NAME_SIZE <= pp)
110 return -EINVAL;
111
112 memcpy(host, p, pp - p);
113 host[pp - p] = '\0';
114
115 if (*pp == ':') {
116 /* Parse port number */
117 p = pp + 1;
118 lport = simple_strtol(p, &pp, 10);
119 if (pp && *pp != '/')
120 return -EINVAL;
121 if (lport > 65535)
122 return -EINVAL;
123 *port = (u16)lport;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200124 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200125
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200126 if (*pp != '/')
127 return -EINVAL;
128 *path = pp;
129
130 return 0;
131}
132
133/*
134 * Legacy syntax support
135 * Convert [<server_name_or_ip>:]filename into a URL if needed
136 */
137static int parse_legacy_arg(char *arg, char *nurl, size_t rem)
138{
139 char *p = nurl;
140 size_t n;
141 char *col = strchr(arg, ':');
142 char *env;
143 char *server;
144 char *path;
145
146 if (strstr(arg, "http") == arg) {
147 n = snprintf(nurl, rem, "%s", arg);
148 if (n < 0 || n > rem)
149 return -1;
150 return 0;
151 }
152
153 n = snprintf(p, rem, "%s", "http://");
154 if (n < 0 || n > rem)
155 return -1;
156 p += n;
157 rem -= n;
158
159 if (col) {
160 n = col - arg;
161 server = arg;
162 path = col + 1;
163 } else {
164 env = env_get("httpserverip");
165 if (!env)
166 env = env_get("serverip");
167 if (!env) {
168 log_err("error: httpserver/serverip has to be set\n");
169 return -1;
170 }
171 n = strlen(env);
172 server = env;
173 path = arg;
174 }
175
176 if (rem < n)
177 return -1;
Jerome Forissier97083502024-11-07 12:27:57 +0100178 strlcpy(p, server, n);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200179 p += n;
180 rem -= n;
181 if (rem < 1)
182 return -1;
183 *p = '/';
184 p++;
185 rem--;
186 n = strlen(path);
187 if (rem < n)
188 return -1;
Jerome Forissier97083502024-11-07 12:27:57 +0100189 strlcpy(p, path, n);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200190 p += n;
191 rem -= n;
192 if (rem < 1)
193 return -1;
194 *p = '\0';
195
196 return 0;
197}
198
199static err_t httpc_recv_cb(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf,
200 err_t err)
201{
202 struct wget_ctx *ctx = arg;
203 struct pbuf *buf;
204
205 if (!pbuf)
206 return ERR_BUF;
207
208 if (!ctx->start_time)
209 ctx->start_time = get_timer(0);
210
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) {
216 printf("#");
217 ctx->prevsize = ctx->size;
218 }
219 }
220
221 altcp_recved(pcb, pbuf->tot_len);
222 pbuf_free(pbuf);
223 return ERR_OK;
224}
225
226static void httpc_result_cb(void *arg, httpc_result_t httpc_result,
227 u32_t rx_content_len, u32_t srv_res, err_t err)
228{
229 struct wget_ctx *ctx = arg;
230 ulong elapsed;
231
Adriano Cordova25e88412024-11-11 18:09:01 -0300232 wget_info->status_code = (u32)srv_res;
233
234 if (err == ERR_BUF) {
235 ctx->done = FAILURE;
236 return;
237 }
238
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200239 if (httpc_result != HTTPC_RESULT_OK) {
240 log_err("\nHTTP client error %d\n", httpc_result);
241 ctx->done = FAILURE;
242 return;
243 }
244 if (srv_res != 200) {
245 log_err("\nHTTP server error %d\n", srv_res);
246 ctx->done = FAILURE;
247 return;
248 }
249
250 elapsed = get_timer(ctx->start_time);
251 if (!elapsed)
252 elapsed = 1;
253 if (rx_content_len > PROGRESS_PRINT_STEP_BYTES)
254 printf("\n");
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);
Adriano Cordova25e88412024-11-11 18:09:01 -0300258 if (wget_info->set_bootdev) {
259 efi_set_bootdev("Net", "", ctx->path, map_sysmem(ctx->saved_daddr, 0),
260 rx_content_len);
261 }
262 wget_lwip_set_file_size(rx_content_len);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200263 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");
266 ctx->done = FAILURE;
267 return;
268 }
269
270 ctx->done = SUCCESS;
271}
272
Adriano Cordova25e88412024-11-11 18:09:01 -0300273static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct pbuf *hdr,
274 u16_t hdr_len, u32_t content_len)
275{
276 wget_lwip_fill_info(hdr, hdr_len, content_len);
277
278 if (wget_info->check_buffer_size && (ulong)content_len > wget_info->buffer_size)
279 return ERR_BUF;
280
281 return ERR_OK;
282}
283
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200284static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
285{
286 char server_name[SERVER_NAME_SIZE];
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200287#if defined CONFIG_WGET_HTTPS
288 altcp_allocator_t tls_allocator;
289#endif
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200290 httpc_connection_t conn;
291 httpc_state_t *state;
292 struct netif *netif;
293 struct wget_ctx ctx;
294 char *path;
295 u16 port;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200296 bool is_https;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200297
298 ctx.daddr = dst_addr;
299 ctx.saved_daddr = dst_addr;
300 ctx.done = NOT_DONE;
301 ctx.size = 0;
302 ctx.prevsize = 0;
303 ctx.start_time = 0;
304
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200305 if (parse_url(uri, server_name, &port, &path, &is_https))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200306 return CMD_RET_USAGE;
307
308 netif = net_lwip_new_netif(udev);
309 if (!netif)
310 return -1;
311
312 memset(&conn, 0, sizeof(conn));
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200313#if defined CONFIG_WGET_HTTPS
314 if (is_https) {
315 tls_allocator.alloc = &altcp_tls_alloc;
316 tls_allocator.arg =
317 altcp_tls_create_config_client(NULL, 0, server_name);
318
319 if (!tls_allocator.arg) {
320 log_err("error: Cannot create a TLS connection\n");
321 net_lwip_remove_netif(netif);
322 return -1;
323 }
324
325 conn.altcp_allocator = &tls_allocator;
326 }
327#endif
328
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200329 conn.result_fn = httpc_result_cb;
Adriano Cordova25e88412024-11-11 18:09:01 -0300330 conn.headers_done_fn = httpc_headers_done_cb;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200331 ctx.path = path;
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -0300332 if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb,
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200333 &ctx, &state)) {
334 net_lwip_remove_netif(netif);
335 return CMD_RET_FAILURE;
336 }
337
338 while (!ctx.done) {
339 net_lwip_rx(udev, netif);
340 sys_check_timeouts();
341 if (ctrlc())
342 break;
343 }
344
345 net_lwip_remove_netif(netif);
346
347 if (ctx.done == SUCCESS)
348 return 0;
349
350 return -1;
351}
352
353int wget_with_dns(ulong dst_addr, char *uri)
354{
355 eth_set_current();
356
Adriano Cordova25e88412024-11-11 18:09:01 -0300357 if (!wget_info)
358 wget_info = &default_wget_info;
359
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200360 return wget_loop(eth_get_dev(), dst_addr, uri);
361}
362
363int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
364{
365 char *end;
366 char *url;
367 ulong dst_addr;
368 char nurl[1024];
369
370 if (argc < 2 || argc > 3)
371 return CMD_RET_USAGE;
372
373 dst_addr = hextoul(argv[1], &end);
Jerome Forissier97083502024-11-07 12:27:57 +0100374 if (end == (argv[1] + strlen(argv[1]))) {
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200375 if (argc < 3)
376 return CMD_RET_USAGE;
377 url = argv[2];
378 } else {
379 dst_addr = image_load_addr;
380 url = argv[1];
381 }
382
383 if (parse_legacy_arg(url, nurl, sizeof(nurl)))
Jerome Forissier97083502024-11-07 12:27:57 +0100384 return CMD_RET_FAILURE;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200385
Adriano Cordova25e88412024-11-11 18:09:01 -0300386 wget_info = &default_wget_info;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200387 if (wget_with_dns(dst_addr, nurl))
388 return CMD_RET_FAILURE;
389
390 return CMD_RET_SUCCESS;
391}
392
393/**
394 * wget_validate_uri() - validate the uri for wget
395 *
396 * @uri: uri string
397 *
398 * This function follows the current U-Boot wget implementation.
399 * scheme: only "http:" is supported
400 * authority:
401 * - user information: not supported
402 * - host: supported
403 * - port: not supported(always use the default port)
404 *
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.
408 *
409 * TODO: stricter uri conformance check
410 *
411 * Return: true on success, false on failure
412 */
413bool wget_validate_uri(char *uri)
414{
415 char c;
416 bool ret = true;
417 char *str_copy, *s, *authority;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200418 size_t prefix_len = 0;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200419
420 for (c = 0x1; c < 0x21; c++) {
421 if (strchr(uri, c)) {
422 log_err("invalid character is used\n");
423 return false;
424 }
425 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200426
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200427 if (strchr(uri, 0x7f)) {
428 log_err("invalid character is used\n");
429 return false;
430 }
431
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200432 if (!strncmp(uri, "http://", strlen("http://"))) {
433 prefix_len = strlen("http://");
434 } else if (!strncmp(uri, "https://", strlen("https://"))) {
435 prefix_len = strlen("https://");
436 } else {
437 log_err("only http(s):// is supported\n");
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200438 return false;
439 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200440
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200441 str_copy = strdup(uri);
442 if (!str_copy)
443 return false;
444
445 s = str_copy + strlen("http://");
446 authority = strsep(&s, "/");
447 if (!s) {
448 log_err("invalid uri, no file path\n");
449 ret = false;
450 goto out;
451 }
452 s = strchr(authority, '@');
453 if (s) {
454 log_err("user information is not supported\n");
455 ret = false;
456 goto out;
457 }
458
459out:
460 free(str_copy);
461
462 return ret;
463}