blob: 4add520045f7de0c7cf5892b3d3d0a3ec12a62a6 [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>
10#include <lwip/timeouts.h>
11#include <mapmem.h>
12#include <net.h>
13#include <time.h>
14
15#define SERVER_NAME_SIZE 200
16#define HTTP_PORT_DEFAULT 80
17#define PROGRESS_PRINT_STEP_BYTES (100 * 1024)
18
19enum done_state {
20 NOT_DONE = 0,
21 SUCCESS = 1,
22 FAILURE = 2
23};
24
25struct wget_ctx {
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -030026 char server_name[SERVER_NAME_SIZE];
27 u16 port;
Jerome Forissier359d4ed2024-10-16 12:04:09 +020028 char *path;
29 ulong daddr;
30 ulong saved_daddr;
31 ulong size;
32 ulong prevsize;
33 ulong start_time;
34 enum done_state done;
35};
36
37static int parse_url(char *url, char *host, u16 *port, char **path)
38{
39 char *p, *pp;
40 long lport;
41
42 p = strstr(url, "http://");
43 if (!p) {
44 log_err("only http:// is supported\n");
45 return -EINVAL;
46 }
47
48 p += strlen("http://");
49
50 /* Parse hostname */
51 pp = strchr(p, ':');
52 if (!pp)
53 pp = strchr(p, '/');
54 if (!pp)
55 return -EINVAL;
56
57 if (p + SERVER_NAME_SIZE <= pp)
58 return -EINVAL;
59
60 memcpy(host, p, pp - p);
61 host[pp - p] = '\0';
62
63 if (*pp == ':') {
64 /* Parse port number */
65 p = pp + 1;
66 lport = simple_strtol(p, &pp, 10);
67 if (pp && *pp != '/')
68 return -EINVAL;
69 if (lport > 65535)
70 return -EINVAL;
71 *port = (u16)lport;
72 } else {
73 *port = HTTP_PORT_DEFAULT;
74 }
75 if (*pp != '/')
76 return -EINVAL;
77 *path = pp;
78
79 return 0;
80}
81
82/*
83 * Legacy syntax support
84 * Convert [<server_name_or_ip>:]filename into a URL if needed
85 */
86static int parse_legacy_arg(char *arg, char *nurl, size_t rem)
87{
88 char *p = nurl;
89 size_t n;
90 char *col = strchr(arg, ':');
91 char *env;
92 char *server;
93 char *path;
94
95 if (strstr(arg, "http") == arg) {
96 n = snprintf(nurl, rem, "%s", arg);
97 if (n < 0 || n > rem)
98 return -1;
99 return 0;
100 }
101
102 n = snprintf(p, rem, "%s", "http://");
103 if (n < 0 || n > rem)
104 return -1;
105 p += n;
106 rem -= n;
107
108 if (col) {
109 n = col - arg;
110 server = arg;
111 path = col + 1;
112 } else {
113 env = env_get("httpserverip");
114 if (!env)
115 env = env_get("serverip");
116 if (!env) {
117 log_err("error: httpserver/serverip has to be set\n");
118 return -1;
119 }
120 n = strlen(env);
121 server = env;
122 path = arg;
123 }
124
125 if (rem < n)
126 return -1;
127 strncpy(p, server, n);
128 p += n;
129 rem -= n;
130 if (rem < 1)
131 return -1;
132 *p = '/';
133 p++;
134 rem--;
135 n = strlen(path);
136 if (rem < n)
137 return -1;
138 strncpy(p, path, n);
139 p += n;
140 rem -= n;
141 if (rem < 1)
142 return -1;
143 *p = '\0';
144
145 return 0;
146}
147
148static err_t httpc_recv_cb(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf,
149 err_t err)
150{
151 struct wget_ctx *ctx = arg;
152 struct pbuf *buf;
153
154 if (!pbuf)
155 return ERR_BUF;
156
157 if (!ctx->start_time)
158 ctx->start_time = get_timer(0);
159
160 for (buf = pbuf; buf; buf = buf->next) {
161 memcpy((void *)ctx->daddr, buf->payload, buf->len);
162 ctx->daddr += buf->len;
163 ctx->size += buf->len;
164 if (ctx->size - ctx->prevsize > PROGRESS_PRINT_STEP_BYTES) {
165 printf("#");
166 ctx->prevsize = ctx->size;
167 }
168 }
169
170 altcp_recved(pcb, pbuf->tot_len);
171 pbuf_free(pbuf);
172 return ERR_OK;
173}
174
175static void httpc_result_cb(void *arg, httpc_result_t httpc_result,
176 u32_t rx_content_len, u32_t srv_res, err_t err)
177{
178 struct wget_ctx *ctx = arg;
179 ulong elapsed;
180
181 if (httpc_result != HTTPC_RESULT_OK) {
182 log_err("\nHTTP client error %d\n", httpc_result);
183 ctx->done = FAILURE;
184 return;
185 }
186 if (srv_res != 200) {
187 log_err("\nHTTP server error %d\n", srv_res);
188 ctx->done = FAILURE;
189 return;
190 }
191
192 elapsed = get_timer(ctx->start_time);
193 if (!elapsed)
194 elapsed = 1;
195 if (rx_content_len > PROGRESS_PRINT_STEP_BYTES)
196 printf("\n");
197 printf("%u bytes transferred in %lu ms (", rx_content_len, elapsed);
198 print_size(rx_content_len / elapsed * 1000, "/s)\n");
199 printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size);
200 efi_set_bootdev("Net", "", ctx->path, map_sysmem(ctx->saved_daddr, 0),
201 rx_content_len);
202 if (env_set_hex("filesize", rx_content_len) ||
203 env_set_hex("fileaddr", ctx->saved_daddr)) {
204 log_err("Could not set filesize or fileaddr\n");
205 ctx->done = FAILURE;
206 return;
207 }
208
209 ctx->done = SUCCESS;
210}
211
212static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri)
213{
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200214 httpc_connection_t conn;
215 httpc_state_t *state;
216 struct netif *netif;
217 struct wget_ctx ctx;
218 char *path;
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200219
220 ctx.daddr = dst_addr;
221 ctx.saved_daddr = dst_addr;
222 ctx.done = NOT_DONE;
223 ctx.size = 0;
224 ctx.prevsize = 0;
225 ctx.start_time = 0;
226
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -0300227 if (parse_url(uri, ctx.server_name, &ctx.port, &path))
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200228 return CMD_RET_USAGE;
229
230 netif = net_lwip_new_netif(udev);
231 if (!netif)
232 return -1;
233
234 memset(&conn, 0, sizeof(conn));
235 conn.result_fn = httpc_result_cb;
236 ctx.path = path;
Adriano Cordovaa8a8d5c62024-11-11 18:09:00 -0300237 if (httpc_get_file_dns(ctx.server_name, ctx.port, path, &conn, httpc_recv_cb,
Jerome Forissier359d4ed2024-10-16 12:04:09 +0200238 &ctx, &state)) {
239 net_lwip_remove_netif(netif);
240 return CMD_RET_FAILURE;
241 }
242
243 while (!ctx.done) {
244 net_lwip_rx(udev, netif);
245 sys_check_timeouts();
246 if (ctrlc())
247 break;
248 }
249
250 net_lwip_remove_netif(netif);
251
252 if (ctx.done == SUCCESS)
253 return 0;
254
255 return -1;
256}
257
258int wget_with_dns(ulong dst_addr, char *uri)
259{
260 eth_set_current();
261
262 return wget_loop(eth_get_dev(), dst_addr, uri);
263}
264
265int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
266{
267 char *end;
268 char *url;
269 ulong dst_addr;
270 char nurl[1024];
271
272 if (argc < 2 || argc > 3)
273 return CMD_RET_USAGE;
274
275 dst_addr = hextoul(argv[1], &end);
276 if (end == (argv[1] + strlen(argv[1]))) {
277 if (argc < 3)
278 return CMD_RET_USAGE;
279 url = argv[2];
280 } else {
281 dst_addr = image_load_addr;
282 url = argv[1];
283 }
284
285 if (parse_legacy_arg(url, nurl, sizeof(nurl)))
286 return CMD_RET_FAILURE;
287
288 if (wget_with_dns(dst_addr, nurl))
289 return CMD_RET_FAILURE;
290
291 return CMD_RET_SUCCESS;
292}
293
294/**
295 * wget_validate_uri() - validate the uri for wget
296 *
297 * @uri: uri string
298 *
299 * This function follows the current U-Boot wget implementation.
300 * scheme: only "http:" is supported
301 * authority:
302 * - user information: not supported
303 * - host: supported
304 * - port: not supported(always use the default port)
305 *
306 * Uri is expected to be correctly percent encoded.
307 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
308 * and space character(0x20) are not allowed.
309 *
310 * TODO: stricter uri conformance check
311 *
312 * Return: true on success, false on failure
313 */
314bool wget_validate_uri(char *uri)
315{
316 char c;
317 bool ret = true;
318 char *str_copy, *s, *authority;
319
320 for (c = 0x1; c < 0x21; c++) {
321 if (strchr(uri, c)) {
322 log_err("invalid character is used\n");
323 return false;
324 }
325 }
326 if (strchr(uri, 0x7f)) {
327 log_err("invalid character is used\n");
328 return false;
329 }
330
331 if (strncmp(uri, "http://", 7)) {
332 log_err("only http:// is supported\n");
333 return false;
334 }
335 str_copy = strdup(uri);
336 if (!str_copy)
337 return false;
338
339 s = str_copy + strlen("http://");
340 authority = strsep(&s, "/");
341 if (!s) {
342 log_err("invalid uri, no file path\n");
343 ret = false;
344 goto out;
345 }
346 s = strchr(authority, '@');
347 if (s) {
348 log_err("user information is not supported\n");
349 ret = false;
350 goto out;
351 }
352
353out:
354 free(str_copy);
355
356 return ret;
357}