blob: 669eded0f38c05ab24fecaec291f937790cab191 [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 Forissier97083502024-11-07 12:27:57 +0100183 strlcpy(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 Forissier97083502024-11-07 12:27:57 +0100194 strlcpy(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 Cordova25e88412024-11-11 18:09:01 -0300263 if (wget_info->set_bootdev) {
264 efi_set_bootdev("Net", "", ctx->path, map_sysmem(ctx->saved_daddr, 0),
265 rx_content_len);
266 }
267 wget_lwip_set_file_size(rx_content_len);
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200268 if (env_set_hex("filesize", rx_content_len) ||
269 env_set_hex("fileaddr", ctx->saved_daddr)) {
270 log_err("Could not set filesize or fileaddr\n");
271 ctx->done = FAILURE;
272 return;
273 }
274
275 ctx->done = SUCCESS;
276}
277
Adriano Cordova25e88412024-11-11 18:09:01 -0300278static err_t httpc_headers_done_cb(httpc_state_t *connection, void *arg, struct pbuf *hdr,
279 u16_t hdr_len, u32_t content_len)
280{
281 wget_lwip_fill_info(hdr, hdr_len, content_len);
282
283 if (wget_info->check_buffer_size && (ulong)content_len > wget_info->buffer_size)
284 return ERR_BUF;
285
286 return ERR_OK;
287}
288
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200289static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
290{
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200291#if defined CONFIG_WGET_HTTPS
292 altcp_allocator_t tls_allocator;
293#endif
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200294 httpc_connection_t conn;
295 httpc_state_t *state;
296 struct netif *netif;
297 struct wget_ctx ctx;
298 char *path;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200299 bool is_https;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200300
301 ctx.daddr = dst_addr;
302 ctx.saved_daddr = dst_addr;
303 ctx.done = NOT_DONE;
304 ctx.size = 0;
305 ctx.prevsize = 0;
306 ctx.start_time = 0;
307
Adriano Cordova484ade32024-12-03 09:55:34 -0300308 if (parse_url(uri, ctx.server_name, &ctx.port, &path, &is_https))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200309 return CMD_RET_USAGE;
310
311 netif = net_lwip_new_netif(udev);
312 if (!netif)
313 return -1;
314
315 memset(&conn, 0, sizeof(conn));
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200316#if defined CONFIG_WGET_HTTPS
317 if (is_https) {
318 tls_allocator.alloc = &altcp_tls_alloc;
319 tls_allocator.arg =
Adriano Cordova484ade32024-12-03 09:55:34 -0300320 altcp_tls_create_config_client(NULL, 0, ctx.server_name);
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200321
322 if (!tls_allocator.arg) {
323 log_err("error: Cannot create a TLS connection\n");
324 net_lwip_remove_netif(netif);
325 return -1;
326 }
327
328 conn.altcp_allocator = &tls_allocator;
329 }
330#endif
331
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200332 conn.result_fn = httpc_result_cb;
Adriano Cordova25e88412024-11-11 18:09:01 -0300333 conn.headers_done_fn = httpc_headers_done_cb;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200334 ctx.path = path;
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -0300335 if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb,
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200336 &ctx, &state)) {
337 net_lwip_remove_netif(netif);
338 return CMD_RET_FAILURE;
339 }
340
341 while (!ctx.done) {
342 net_lwip_rx(udev, netif);
343 sys_check_timeouts();
344 if (ctrlc())
345 break;
346 }
347
348 net_lwip_remove_netif(netif);
349
350 if (ctx.done == SUCCESS)
351 return 0;
352
353 return -1;
354}
355
Adriano Cordovab479fc42024-12-04 00:05:16 -0300356int wget_do_request(ulong dst_addr, char *uri)
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200357{
358 eth_set_current();
359
Adriano Cordova25e88412024-11-11 18:09:01 -0300360 if (!wget_info)
361 wget_info = &default_wget_info;
362
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200363 return wget_loop(eth_get_dev(), dst_addr, uri);
364}
365
366int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
367{
368 char *end;
369 char *url;
370 ulong dst_addr;
371 char nurl[1024];
372
373 if (argc < 2 || argc > 3)
374 return CMD_RET_USAGE;
375
376 dst_addr = hextoul(argv[1], &end);
Jerome Forissier97083502024-11-07 12:27:57 +0100377 if (end == (argv[1] + strlen(argv[1]))) {
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200378 if (argc < 3)
379 return CMD_RET_USAGE;
380 url = argv[2];
381 } else {
382 dst_addr = image_load_addr;
383 url = argv[1];
384 }
385
386 if (parse_legacy_arg(url, nurl, sizeof(nurl)))
Jerome Forissier97083502024-11-07 12:27:57 +0100387 return CMD_RET_FAILURE;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200388
Adriano Cordova25e88412024-11-11 18:09:01 -0300389 wget_info = &default_wget_info;
Adriano Cordovab479fc42024-12-04 00:05:16 -0300390 if (wget_do_request(dst_addr, nurl))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200391 return CMD_RET_FAILURE;
392
393 return CMD_RET_SUCCESS;
394}
395
396/**
397 * wget_validate_uri() - validate the uri for wget
398 *
399 * @uri: uri string
400 *
401 * This function follows the current U-Boot wget implementation.
402 * scheme: only "http:" is supported
403 * authority:
404 * - user information: not supported
405 * - host: supported
406 * - port: not supported(always use the default port)
407 *
408 * Uri is expected to be correctly percent encoded.
409 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
410 * and space character(0x20) are not allowed.
411 *
412 * TODO: stricter uri conformance check
413 *
414 * Return: true on success, false on failure
415 */
416bool wget_validate_uri(char *uri)
417{
418 char c;
419 bool ret = true;
420 char *str_copy, *s, *authority;
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200421 size_t prefix_len = 0;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200422
423 for (c = 0x1; c < 0x21; c++) {
424 if (strchr(uri, c)) {
425 log_err("invalid character is used\n");
426 return false;
427 }
428 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200429
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200430 if (strchr(uri, 0x7f)) {
431 log_err("invalid character is used\n");
432 return false;
433 }
434
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200435 if (!strncmp(uri, "http://", strlen("http://"))) {
436 prefix_len = strlen("http://");
437 } else if (!strncmp(uri, "https://", strlen("https://"))) {
438 prefix_len = strlen("https://");
439 } else {
440 log_err("only http(s):// is supported\n");
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200441 return false;
442 }
Ilias Apalodimas99618ca2024-11-10 10:28:40 +0200443
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200444 str_copy = strdup(uri);
445 if (!str_copy)
446 return false;
447
448 s = str_copy + strlen("http://");
449 authority = strsep(&s, "/");
450 if (!s) {
451 log_err("invalid uri, no file path\n");
452 ret = false;
453 goto out;
454 }
455 s = strchr(authority, '@');
456 if (s) {
457 log_err("user information is not supported\n");
458 ret = false;
459 goto out;
460 }
461
462out:
463 free(str_copy);
464
465 return ret;
466}