blob: c22843ee10dbceab0d1146c502743224c0c788eb [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{
Adriano Cordova50a2a0b2024-11-26 13:19:21 -030043 if (wget_info->headers) {
44 if (hdr_len < MAX_HTTP_HEADERS_SIZE)
45 pbuf_copy_partial(hdr, (void *)wget_info->headers, hdr_len, 0);
46 else
47 hdr_len = 0;
48 wget_info->headers[hdr_len] = 0;
49 }
Adriano Cordova25e88412024-11-11 18:09:01 -030050 wget_info->hdr_cont_len = (u32)hdr_cont_len;
51}
52
53static void wget_lwip_set_file_size(u32_t rx_content_len)
54{
55 wget_info->file_size = (ulong)rx_content_len;
56}
57
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020058bool wget_validate_uri(char *uri);
59
60int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len,
61 size_t *olen)
62{
63 struct udevice *dev;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020064 int ret;
65
66 *olen = 0;
67
68 ret = uclass_get_device(UCLASS_RNG, 0, &dev);
69 if (ret) {
70 log_err("Failed to get an rng: %d\n", ret);
71 return ret;
72 }
Ilias Apalodimas4276a3c2024-11-14 16:29:15 +020073 ret = dm_rng_read(dev, output, len);
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020074 if (ret)
75 return ret;
76
Ilias Apalodimas4276a3c2024-11-14 16:29:15 +020077 *olen = len;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020078
79 return 0;
80}
81
82static int parse_url(char *url, char *host, u16 *port, char **path,
83 bool *is_https)
Jerome Forissier359d4ed2024-10-16 12:04:09 +020084{
85 char *p, *pp;
86 long lport;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020087 size_t prefix_len = 0;
Jerome Forissier359d4ed2024-10-16 12:04:09 +020088
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020089 if (!wget_validate_uri(url)) {
90 log_err("Invalid URL. Use http(s)://\n");
91 return -EINVAL;
92 }
93
94 *is_https = false;
95 *port = HTTP_PORT_DEFAULT;
96 prefix_len = strlen("http://");
Jerome Forissier359d4ed2024-10-16 12:04:09 +020097 p = strstr(url, "http://");
98 if (!p) {
Ilias Apalodimas99618ca2024-11-10 10:28:40 +020099 p = strstr(url, "https://");
100 prefix_len = strlen("https://");
101 *port = HTTPS_PORT_DEFAULT;
102 *is_https = true;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200103 }
104
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200105 p += prefix_len;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200106
107 /* Parse hostname */
108 pp = strchr(p, ':');
109 if (!pp)
110 pp = strchr(p, '/');
111 if (!pp)
112 return -EINVAL;
113
114 if (p + SERVER_NAME_SIZE <= pp)
115 return -EINVAL;
116
117 memcpy(host, p, pp - p);
118 host[pp - p] = '\0';
119
120 if (*pp == ':') {
121 /* Parse port number */
122 p = pp + 1;
123 lport = simple_strtol(p, &pp, 10);
124 if (pp && *pp != '/')
125 return -EINVAL;
126 if (lport > 65535)
127 return -EINVAL;
128 *port = (u16)lport;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200129 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200130
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200131 if (*pp != '/')
132 return -EINVAL;
133 *path = pp;
134
135 return 0;
136}
137
138/*
139 * Legacy syntax support
140 * Convert [<server_name_or_ip>:]filename into a URL if needed
141 */
142static int parse_legacy_arg(char *arg, char *nurl, size_t rem)
143{
144 char *p = nurl;
145 size_t n;
146 char *col = strchr(arg, ':');
147 char *env;
148 char *server;
149 char *path;
150
151 if (strstr(arg, "http") == arg) {
152 n = snprintf(nurl, rem, "%s", arg);
153 if (n < 0 || n > rem)
154 return -1;
155 return 0;
156 }
157
158 n = snprintf(p, rem, "%s", "http://");
159 if (n < 0 || n > rem)
160 return -1;
161 p += n;
162 rem -= n;
163
164 if (col) {
165 n = col - arg;
166 server = arg;
167 path = col + 1;
168 } else {
169 env = env_get("httpserverip");
170 if (!env)
171 env = env_get("serverip");
172 if (!env) {
173 log_err("error: httpserver/serverip has to be set\n");
174 return -1;
175 }
176 n = strlen(env);
177 server = env;
178 path = arg;
179 }
180
181 if (rem < n)
182 return -1;
Jerome Forissierc7157322024-11-26 15:45:06 +0100183 strncpy(p, server, n);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200184 p += n;
185 rem -= n;
186 if (rem < 1)
187 return -1;
188 *p = '/';
189 p++;
190 rem--;
191 n = strlen(path);
192 if (rem < n)
193 return -1;
Jerome Forissierc7157322024-11-26 15:45:06 +0100194 strncpy(p, path, n);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200195 p += n;
196 rem -= n;
197 if (rem < 1)
198 return -1;
199 *p = '\0';
200
201 return 0;
202}
203
204static err_t httpc_recv_cb(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf,
205 err_t err)
206{
207 struct wget_ctx *ctx = arg;
208 struct pbuf *buf;
209
210 if (!pbuf)
211 return ERR_BUF;
212
213 if (!ctx->start_time)
214 ctx->start_time = get_timer(0);
215
216 for (buf = pbuf; buf; buf = buf->next) {
217 memcpy((void *)ctx->daddr, buf->payload, buf->len);
218 ctx->daddr += buf->len;
219 ctx->size += buf->len;
220 if (ctx->size - ctx->prevsize > PROGRESS_PRINT_STEP_BYTES) {
221 printf("#");
222 ctx->prevsize = ctx->size;
223 }
224 }
225
226 altcp_recved(pcb, pbuf->tot_len);
227 pbuf_free(pbuf);
228 return ERR_OK;
229}
230
231static void httpc_result_cb(void *arg, httpc_result_t httpc_result,
232 u32_t rx_content_len, u32_t srv_res, err_t err)
233{
234 struct wget_ctx *ctx = arg;
235 ulong elapsed;
236
Adriano Cordova25e88412024-11-11 18:09:01 -0300237 wget_info->status_code = (u32)srv_res;
238
239 if (err == ERR_BUF) {
240 ctx->done = FAILURE;
241 return;
242 }
243
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200244 if (httpc_result != HTTPC_RESULT_OK) {
245 log_err("\nHTTP client error %d\n", httpc_result);
246 ctx->done = FAILURE;
247 return;
248 }
249 if (srv_res != 200) {
250 log_err("\nHTTP server error %d\n", srv_res);
251 ctx->done = FAILURE;
252 return;
253 }
254
255 elapsed = get_timer(ctx->start_time);
256 if (!elapsed)
257 elapsed = 1;
258 if (rx_content_len > PROGRESS_PRINT_STEP_BYTES)
259 printf("\n");
260 printf("%u bytes transferred in %lu ms (", rx_content_len, elapsed);
261 print_size(rx_content_len / elapsed * 1000, "/s)\n");
262 printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size);
Adriano Cordova93cba0f2024-12-04 00:05:23 -0300263 if (wget_info->set_bootdev)
264 efi_set_bootdev("Http", ctx->server_name, ctx->path, map_sysmem(ctx->saved_daddr, 0),
Adriano Cordova25e88412024-11-11 18:09:01 -0300265 rx_content_len);
Adriano Cordova25e88412024-11-11 18:09:01 -0300266 wget_lwip_set_file_size(rx_content_len);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200267 if (env_set_hex("filesize", rx_content_len) ||
268 env_set_hex("fileaddr", ctx->saved_daddr)) {
269 log_err("Could not set filesize or fileaddr\n");
270 ctx->done = FAILURE;
271 return;
272 }
273
274 ctx->done = SUCCESS;
275}
276
Adriano Cordova25e88412024-11-11 18:09:01 -0300277static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct pbuf *hdr,
278 u16_t hdr_len, u32_t content_len)
279{
280 wget_lwip_fill_info(hdr, hdr_len, content_len);
281
282 if (wget_info->check_buffer_size && (ulong)content_len > wget_info->buffer_size)
283 return ERR_BUF;
284
285 return ERR_OK;
286}
287
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100288#if CONFIG_IS_ENABLED(WGET_HTTPS)
289enum auth_mode {
290 AUTH_NONE,
291 AUTH_OPTIONAL,
292 AUTH_REQUIRED,
293};
294
295static char *cacert;
296static size_t cacert_size;
297static enum auth_mode cacert_auth_mode = AUTH_OPTIONAL;
298#endif
299
300#if CONFIG_IS_ENABLED(WGET_CACERT)
301static int set_auth(enum auth_mode auth)
302{
303 cacert_auth_mode = auth;
304
305 return CMD_RET_SUCCESS;
306}
307
308static int set_cacert(char * const saddr, char * const ssz)
309{
310 mbedtls_x509_crt crt;
311 ulong addr, sz;
312 int ret;
313
314 if (cacert)
315 free(cacert);
316
317 addr = hextoul(saddr, NULL);
318 sz = hextoul(ssz, NULL);
319
320 if (!addr) {
321 cacert = NULL;
322 cacert_size = 0;
323 return CMD_RET_SUCCESS;
324 }
325
326 cacert = malloc(sz);
327 if (!cacert)
328 return CMD_RET_FAILURE;
329 cacert_size = sz;
330
331 memcpy(cacert, (void *)addr, sz);
332
333 mbedtls_x509_crt_init(&crt);
334 ret = mbedtls_x509_crt_parse(&crt, cacert, cacert_size);
335 if (ret) {
336 printf("Could not parse certificates (%d)\n", ret);
337 free(cacert);
338 cacert = NULL;
339 cacert_size = 0;
340 return CMD_RET_FAILURE;
341 }
342
343 return CMD_RET_SUCCESS;
344}
345#endif
346
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200347static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
348{
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100349#if CONFIG_IS_ENABLED(WGET_HTTPS)
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200350 altcp_allocator_t tls_allocator;
351#endif
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200352 httpc_connection_t conn;
353 httpc_state_t *state;
354 struct netif *netif;
355 struct wget_ctx ctx;
356 char *path;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200357 bool is_https;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200358
359 ctx.daddr = dst_addr;
360 ctx.saved_daddr = dst_addr;
361 ctx.done = NOT_DONE;
362 ctx.size = 0;
363 ctx.prevsize = 0;
364 ctx.start_time = 0;
365
Adriano Cordova484ade32024-12-03 09:55:34 -0300366 if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200367 return CMD_RET_USAGE;
368
369 netif = net_lwip_new_netif(udev);
370 if (!netif)
371 return -1;
372
373 memset(&conn, 0, sizeof(conn));
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100374#if CONFIG_IS_ENABLED(WGET_HTTPS)
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200375 if (is_https) {
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100376 char *ca = cacert;
377 size_t ca_sz = cacert_size;
378
379 if (cacert_auth_mode == AUTH_REQUIRED) {
380 if (!ca || !ca_sz) {
381 printf("Error: cacert authentication mode is "
382 "'required' but no CA certificates "
383 "given\n");
384 return CMD_RET_FAILURE;
385 }
386 } else if (cacert_auth_mode == AUTH_NONE) {
387 ca = NULL;
388 ca_sz = 0;
389 } else if (cacert_auth_mode == AUTH_OPTIONAL) {
390 /*
391 * Nothing to do, this is the default behavior of
392 * altcp_tls to check server certificates against CA
393 * certificates when the latter are provided and proceed
394 * with no verification if not.
395 */
396 }
397
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200398 tls_allocator.alloc = &altcp_tls_alloc;
399 tls_allocator.arg =
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100400 altcp_tls_create_config_client(ca, ca_sz,
401 ctx.server_name);
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200402
403 if (!tls_allocator.arg) {
404 log_err("error: Cannot create a TLS connection\n");
405 net_lwip_remove_netif(netif);
406 return -1;
407 }
408
409 conn.altcp_allocator = &tls_allocator;
410 }
411#endif
412
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200413 conn.result_fn = httpc_result_cb;
Adriano Cordova25e88412024-11-11 18:09:01 -0300414 conn.headers_done_fn = httpc_headers_done_cb;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200415 ctx.path = path;
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -0300416 if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb,
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200417 &ctx, &state)) {
418 net_lwip_remove_netif(netif);
419 return CMD_RET_FAILURE;
420 }
421
422 while (!ctx.done) {
423 net_lwip_rx(udev, netif);
424 sys_check_timeouts();
425 if (ctrlc())
426 break;
427 }
428
429 net_lwip_remove_netif(netif);
430
431 if (ctx.done == SUCCESS)
432 return 0;
433
434 return -1;
435}
436
Adriano Cordovab479fc42024-12-04 00:05:16 -0300437int wget_do_request(ulong dst_addr, char *uri)
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200438{
Jerome Forissier37829282025-01-30 09:22:20 +0100439 net_lwip_set_current();
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200440
Adriano Cordova25e88412024-11-11 18:09:01 -0300441 if (!wget_info)
442 wget_info = &default_wget_info;
443
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200444 return wget_loop(eth_get_dev(), dst_addr, uri);
445}
446
447int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
448{
449 char *end;
450 char *url;
451 ulong dst_addr;
452 char nurl[1024];
453
Jerome Forissier8fa383d2025-03-05 15:26:42 +0100454#if CONFIG_IS_ENABLED(WGET_CACERT)
455 if (argc == 4 && !strncmp(argv[1], "cacert", strlen("cacert")))
456 return set_cacert(argv[2], argv[3]);
457 if (argc == 3 && !strncmp(argv[1], "cacert", strlen("cacert"))) {
458 if (!strncmp(argv[2], "none", strlen("none")))
459 return set_auth(AUTH_NONE);
460 if (!strncmp(argv[2], "optional", strlen("optional")))
461 return set_auth(AUTH_OPTIONAL);
462 if (!strncmp(argv[2], "required", strlen("required")))
463 return set_auth(AUTH_REQUIRED);
464 return CMD_RET_USAGE;
465 }
466#endif
467
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200468 if (argc < 2 || argc > 3)
469 return CMD_RET_USAGE;
470
471 dst_addr = hextoul(argv[1], &end);
Jerome Forissier97083502024-11-07 12:27:57 +0100472 if (end == (argv[1] + strlen(argv[1]))) {
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200473 if (argc < 3)
474 return CMD_RET_USAGE;
475 url = argv[2];
476 } else {
477 dst_addr = image_load_addr;
478 url = argv[1];
479 }
480
481 if (parse_legacy_arg(url, nurl, sizeof(nurl)))
Jerome Forissier97083502024-11-07 12:27:57 +0100482 return CMD_RET_FAILURE;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200483
Adriano Cordova25e88412024-11-11 18:09:01 -0300484 wget_info = &default_wget_info;
Adriano Cordovab479fc42024-12-04 00:05:16 -0300485 if (wget_do_request(dst_addr, nurl))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200486 return CMD_RET_FAILURE;
487
488 return CMD_RET_SUCCESS;
489}
490
491/**
492 * wget_validate_uri() - validate the uri for wget
493 *
494 * @uri: uri string
495 *
496 * This function follows the current U-Boot wget implementation.
497 * scheme: only "http:" is supported
498 * authority:
499 * - user information: not supported
500 * - host: supported
501 * - port: not supported(always use the default port)
502 *
503 * Uri is expected to be correctly percent encoded.
504 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
505 * and space character(0x20) are not allowed.
506 *
507 * TODO: stricter uri conformance check
508 *
509 * Return: true on success, false on failure
510 */
511bool wget_validate_uri(char *uri)
512{
513 char c;
514 bool ret = true;
515 char *str_copy, *s, *authority;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200516 size_t prefix_len = 0;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200517
518 for (c = 0x1; c < 0x21; c++) {
519 if (strchr(uri, c)) {
520 log_err("invalid character is used\n");
521 return false;
522 }
523 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200524
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200525 if (strchr(uri, 0x7f)) {
526 log_err("invalid character is used\n");
527 return false;
528 }
529
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200530 if (!strncmp(uri, "http://", strlen("http://"))) {
531 prefix_len = strlen("http://");
Jerome Forissier42129542025-02-04 17:00:49 +0100532 } else if (CONFIG_IS_ENABLED(WGET_HTTPS)) {
533 if (!strncmp(uri, "https://", strlen("https://"))) {
534 prefix_len = strlen("https://");
535 } else {
536 log_err("only http(s):// is supported\n");
537 return false;
538 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200539 } else {
Jerome Forissier42129542025-02-04 17:00:49 +0100540 log_err("only http:// is supported\n");
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200541 return false;
542 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200543
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200544 str_copy = strdup(uri);
545 if (!str_copy)
546 return false;
547
548 s = str_copy + strlen("http://");
549 authority = strsep(&s, "/");
550 if (!s) {
551 log_err("invalid uri, no file path\n");
552 ret = false;
553 goto out;
554 }
555 s = strchr(authority, '@');
556 if (s) {
557 log_err("user information is not supported\n");
558 ret = false;
559 goto out;
560 }
561
562out:
563 free(str_copy);
564
565 return ret;
566}