blob: 5a64f6887c9a29148a5072bef42b2bce4fe0aa05 [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{
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200286#if defined CONFIG_WGET_HTTPS
287 altcp_allocator_t tls_allocator;
288#endif
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200289 httpc_connection_t conn;
290 httpc_state_t *state;
291 struct netif *netif;
292 struct wget_ctx ctx;
293 char *path;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200294 bool is_https;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200295
296 ctx.daddr = dst_addr;
297 ctx.saved_daddr = dst_addr;
298 ctx.done = NOT_DONE;
299 ctx.size = 0;
300 ctx.prevsize = 0;
301 ctx.start_time = 0;
302
Adriano Cordova484ade32024-12-03 09:55:34 -0300303 if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200304 return CMD_RET_USAGE;
305
306 netif = net_lwip_new_netif(udev);
307 if (!netif)
308 return -1;
309
310 memset(&conn, 0, sizeof(conn));
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200311#if defined CONFIG_WGET_HTTPS
312 if (is_https) {
313 tls_allocator.alloc = &altcp_tls_alloc;
314 tls_allocator.arg =
Adriano Cordova484ade32024-12-03 09:55:34 -0300315 altcp_tls_create_config_client(NULL, 0, ctx.server_name);
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200316
317 if (!tls_allocator.arg) {
318 log_err("error: Cannot create a TLS connection\n");
319 net_lwip_remove_netif(netif);
320 return -1;
321 }
322
323 conn.altcp_allocator = &tls_allocator;
324 }
325#endif
326
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200327 conn.result_fn = httpc_result_cb;
Adriano Cordova25e88412024-11-11 18:09:01 -0300328 conn.headers_done_fn = httpc_headers_done_cb;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200329 ctx.path = path;
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -0300330 if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb,
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200331 &ctx, &state)) {
332 net_lwip_remove_netif(netif);
333 return CMD_RET_FAILURE;
334 }
335
336 while (!ctx.done) {
337 net_lwip_rx(udev, netif);
338 sys_check_timeouts();
339 if (ctrlc())
340 break;
341 }
342
343 net_lwip_remove_netif(netif);
344
345 if (ctx.done == SUCCESS)
346 return 0;
347
348 return -1;
349}
350
351int wget_with_dns(ulong dst_addr, char *uri)
352{
353 eth_set_current();
354
Adriano Cordova25e88412024-11-11 18:09:01 -0300355 if (!wget_info)
356 wget_info = &default_wget_info;
357
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200358 return wget_loop(eth_get_dev(), dst_addr, uri);
359}
360
361int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
362{
363 char *end;
364 char *url;
365 ulong dst_addr;
366 char nurl[1024];
367
368 if (argc < 2 || argc > 3)
369 return CMD_RET_USAGE;
370
371 dst_addr = hextoul(argv[1], &end);
Jerome Forissier97083502024-11-07 12:27:57 +0100372 if (end == (argv[1] + strlen(argv[1]))) {
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200373 if (argc < 3)
374 return CMD_RET_USAGE;
375 url = argv[2];
376 } else {
377 dst_addr = image_load_addr;
378 url = argv[1];
379 }
380
381 if (parse_legacy_arg(url, nurl, sizeof(nurl)))
Jerome Forissier97083502024-11-07 12:27:57 +0100382 return CMD_RET_FAILURE;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200383
Adriano Cordova25e88412024-11-11 18:09:01 -0300384 wget_info = &default_wget_info;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200385 if (wget_with_dns(dst_addr, nurl))
386 return CMD_RET_FAILURE;
387
388 return CMD_RET_SUCCESS;
389}
390
391/**
392 * wget_validate_uri() - validate the uri for wget
393 *
394 * @uri: uri string
395 *
396 * This function follows the current U-Boot wget implementation.
397 * scheme: only "http:" is supported
398 * authority:
399 * - user information: not supported
400 * - host: supported
401 * - port: not supported(always use the default port)
402 *
403 * Uri is expected to be correctly percent encoded.
404 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
405 * and space character(0x20) are not allowed.
406 *
407 * TODO: stricter uri conformance check
408 *
409 * Return: true on success, false on failure
410 */
411bool wget_validate_uri(char *uri)
412{
413 char c;
414 bool ret = true;
415 char *str_copy, *s, *authority;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200416 size_t prefix_len = 0;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200417
418 for (c = 0x1; c < 0x21; c++) {
419 if (strchr(uri, c)) {
420 log_err("invalid character is used\n");
421 return false;
422 }
423 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200424
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200425 if (strchr(uri, 0x7f)) {
426 log_err("invalid character is used\n");
427 return false;
428 }
429
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200430 if (!strncmp(uri, "http://", strlen("http://"))) {
431 prefix_len = strlen("http://");
432 } else if (!strncmp(uri, "https://", strlen("https://"))) {
433 prefix_len = strlen("https://");
434 } else {
435 log_err("only http(s):// is supported\n");
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200436 return false;
437 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200438
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200439 str_copy = strdup(uri);
440 if (!str_copy)
441 return false;
442
443 s = str_copy + strlen("http://");
444 authority = strsep(&s, "/");
445 if (!s) {
446 log_err("invalid uri, no file path\n");
447 ret = false;
448 goto out;
449 }
450 s = strchr(authority, '@');
451 if (s) {
452 log_err("user information is not supported\n");
453 ret = false;
454 goto out;
455 }
456
457out:
458 free(str_copy);
459
460 return ret;
461}