blob: b76f6c0f1d9567a8b8d5c30f50de15258264380c [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 Forissier359d4ed2024-10-16 12:04:09 +0200288static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
289{
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200290#if defined CONFIG_WGET_HTTPS
291 altcp_allocator_t tls_allocator;
292#endif
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200293 httpc_connection_t conn;
294 httpc_state_t *state;
295 struct netif *netif;
296 struct wget_ctx ctx;
297 char *path;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200298 bool is_https;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200299
300 ctx.daddr = dst_addr;
301 ctx.saved_daddr = dst_addr;
302 ctx.done = NOT_DONE;
303 ctx.size = 0;
304 ctx.prevsize = 0;
305 ctx.start_time = 0;
306
Adriano Cordova484ade32024-12-03 09:55:34 -0300307 if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200308 return CMD_RET_USAGE;
309
310 netif = net_lwip_new_netif(udev);
311 if (!netif)
312 return -1;
313
314 memset(&conn, 0, sizeof(conn));
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200315#if defined CONFIG_WGET_HTTPS
316 if (is_https) {
317 tls_allocator.alloc = &altcp_tls_alloc;
318 tls_allocator.arg =
Adriano Cordova484ade32024-12-03 09:55:34 -0300319 altcp_tls_create_config_client(NULL, 0, ctx.server_name);
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200320
321 if (!tls_allocator.arg) {
322 log_err("error: Cannot create a TLS connection\n");
323 net_lwip_remove_netif(netif);
324 return -1;
325 }
326
327 conn.altcp_allocator = &tls_allocator;
328 }
329#endif
330
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200331 conn.result_fn = httpc_result_cb;
Adriano Cordova25e88412024-11-11 18:09:01 -0300332 conn.headers_done_fn = httpc_headers_done_cb;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200333 ctx.path = path;
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -0300334 if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb,
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200335 &ctx, &state)) {
336 net_lwip_remove_netif(netif);
337 return CMD_RET_FAILURE;
338 }
339
340 while (!ctx.done) {
341 net_lwip_rx(udev, netif);
342 sys_check_timeouts();
343 if (ctrlc())
344 break;
345 }
346
347 net_lwip_remove_netif(netif);
348
349 if (ctx.done == SUCCESS)
350 return 0;
351
352 return -1;
353}
354
Adriano Cordovab479fc42024-12-04 00:05:16 -0300355int wget_do_request(ulong dst_addr, char *uri)
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200356{
357 eth_set_current();
358
Adriano Cordova25e88412024-11-11 18:09:01 -0300359 if (!wget_info)
360 wget_info = &default_wget_info;
361
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200362 return wget_loop(eth_get_dev(), dst_addr, uri);
363}
364
365int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
366{
367 char *end;
368 char *url;
369 ulong dst_addr;
370 char nurl[1024];
371
372 if (argc < 2 || argc > 3)
373 return CMD_RET_USAGE;
374
375 dst_addr = hextoul(argv[1], &end);
Jerome Forissier97083502024-11-07 12:27:57 +0100376 if (end == (argv[1] + strlen(argv[1]))) {
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200377 if (argc < 3)
378 return CMD_RET_USAGE;
379 url = argv[2];
380 } else {
381 dst_addr = image_load_addr;
382 url = argv[1];
383 }
384
385 if (parse_legacy_arg(url, nurl, sizeof(nurl)))
Jerome Forissier97083502024-11-07 12:27:57 +0100386 return CMD_RET_FAILURE;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200387
Adriano Cordova25e88412024-11-11 18:09:01 -0300388 wget_info = &default_wget_info;
Adriano Cordovab479fc42024-12-04 00:05:16 -0300389 if (wget_do_request(dst_addr, nurl))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200390 return CMD_RET_FAILURE;
391
392 return CMD_RET_SUCCESS;
393}
394
395/**
396 * wget_validate_uri() - validate the uri for wget
397 *
398 * @uri: uri string
399 *
400 * This function follows the current U-Boot wget implementation.
401 * scheme: only "http:" is supported
402 * authority:
403 * - user information: not supported
404 * - host: supported
405 * - port: not supported(always use the default port)
406 *
407 * Uri is expected to be correctly percent encoded.
408 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
409 * and space character(0x20) are not allowed.
410 *
411 * TODO: stricter uri conformance check
412 *
413 * Return: true on success, false on failure
414 */
415bool wget_validate_uri(char *uri)
416{
417 char c;
418 bool ret = true;
419 char *str_copy, *s, *authority;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200420 size_t prefix_len = 0;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200421
422 for (c = 0x1; c < 0x21; c++) {
423 if (strchr(uri, c)) {
424 log_err("invalid character is used\n");
425 return false;
426 }
427 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200428
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200429 if (strchr(uri, 0x7f)) {
430 log_err("invalid character is used\n");
431 return false;
432 }
433
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200434 if (!strncmp(uri, "http://", strlen("http://"))) {
435 prefix_len = strlen("http://");
436 } else if (!strncmp(uri, "https://", strlen("https://"))) {
437 prefix_len = strlen("https://");
438 } else {
439 log_err("only http(s):// is supported\n");
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200440 return false;
441 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200442
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200443 str_copy = strdup(uri);
444 if (!str_copy)
445 return false;
446
447 s = str_copy + strlen("http://");
448 authority = strsep(&s, "/");
449 if (!s) {
450 log_err("invalid uri, no file path\n");
451 ret = false;
452 goto out;
453 }
454 s = strchr(authority, '@');
455 if (s) {
456 log_err("user information is not supported\n");
457 ret = false;
458 goto out;
459 }
460
461out:
462 free(str_copy);
463
464 return ret;
465}