blob: 5d70b7a82e006cfe1f6b14c06f237084b0c20564 [file] [log] [blame]
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * WGET/HTTP support driver based on U-BOOT's nfs.c
4 * Copyright Duncan Hare <dh@synoia.com> 2017
5 */
6
Masahisa Kojima77b0ae32023-11-10 13:25:34 +09007#include <asm/global_data.h>
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +08008#include <command.h>
Michael Wallecaad55b2022-12-28 16:27:14 +01009#include <display_options.h>
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080010#include <env.h>
Jerome Forissier612b2b12024-09-11 11:58:22 +020011#include <efi_loader.h>
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080012#include <image.h>
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090013#include <lmb.h>
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080014#include <mapmem.h>
15#include <net.h>
16#include <net/tcp.h>
17#include <net/wget.h>
Masahisa Kojima6721d182023-11-10 13:25:35 +090018#include <stdlib.h>
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080019
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090020DECLARE_GLOBAL_DATA_PTR;
21
Marek Vasut22a95082023-12-13 22:11:13 +010022/* The default, change with environment variable 'httpdstp' */
23#define SERVER_PORT 80
24
Adriano Cordova47f35e32024-11-11 18:08:58 -030025static const char bootfileGET[] = "GET ";
26static const char bootfileHEAD[] = "HEAD ";
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080027static const char bootfile3[] = " HTTP/1.0\r\n\r\n";
28static const char http_eom[] = "\r\n\r\n";
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080029static const char content_len[] = "Content-Length";
30static const char linefeed[] = "\r\n";
31static struct in_addr web_server_ip;
32static int our_port;
33static int wget_timeout_count;
34
35struct pkt_qd {
36 uchar *pkt;
37 unsigned int tcp_seq_num;
38 unsigned int len;
39};
40
41/*
42 * This is a control structure for out of order packets received.
43 * The actual packet bufers are in the kernel space, and are
44 * expected to be overwritten by the downloaded image.
45 */
Richard Weinbergerbf44cf42023-07-20 14:51:56 +020046#define PKTQ_SZ (PKTBUFSRX / 4)
47static struct pkt_qd pkt_q[PKTQ_SZ];
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080048static int pkt_q_idx;
49static unsigned long content_length;
50static unsigned int packets;
51
52static unsigned int initial_data_seq_num;
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +090053static unsigned int next_data_seq_num;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080054
55static enum wget_state current_wget_state;
56
57static char *image_url;
58static unsigned int wget_timeout = WGET_TIMEOUT;
59
60static enum net_loop_state wget_loop_state;
61
62/* Timeout retry parameters */
63static u8 retry_action; /* actions for TCP retry */
64static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/
65static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */
66static int retry_len; /* TCP retry length */
67
68/**
69 * store_block() - store block in memory
70 * @src: source of data
71 * @offset: offset
72 * @len: length
73 */
74static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
75{
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090076 ulong store_addr = image_load_addr + offset;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080077 ulong newsize = offset + len;
78 uchar *ptr;
79
Adriano Cordova47f35e32024-11-11 18:08:58 -030080 if (CONFIG_IS_ENABLED(LMB) && wget_info->set_bootdev) {
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090081 if (store_addr < image_load_addr ||
Sughosh Ganu77728092024-09-16 20:50:25 +053082 lmb_read_check(store_addr, len)) {
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090083 printf("\nwget error: ");
84 printf("trying to overwrite reserved memory...\n");
85 return -1;
86 }
87 }
88
89 ptr = map_sysmem(store_addr, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080090 memcpy(ptr, src, len);
91 unmap_sysmem(ptr);
92
93 if (net_boot_file_size < (offset + len))
94 net_boot_file_size = newsize;
95
96 return 0;
97}
98
99/**
100 * wget_send_stored() - wget response dispatcher
101 *
102 * WARNING, This, and only this, is the place in wget.c where
103 * SEQUENCE NUMBERS are swapped between incoming (RX)
104 * and outgoing (TX).
105 * Procedure wget_handler() is correct for RX traffic.
106 */
107static void wget_send_stored(void)
108{
109 u8 action = retry_action;
110 int len = retry_len;
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100111 unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len);
112 unsigned int tcp_seq_num = retry_tcp_ack_num;
Marek Vasut22a95082023-12-13 22:11:13 +0100113 unsigned int server_port;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800114 uchar *ptr, *offset;
115
Marek Vasut22a95082023-12-13 22:11:13 +0100116 server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff;
117
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800118 switch (current_wget_state) {
119 case WGET_CLOSED:
120 debug_cond(DEBUG_WGET, "wget: send SYN\n");
121 current_wget_state = WGET_CONNECTING;
Marek Vasut22a95082023-12-13 22:11:13 +0100122 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800123 tcp_seq_num, tcp_ack_num);
124 packets = 0;
125 break;
126 case WGET_CONNECTING:
127 pkt_q_idx = 0;
Marek Vasut22a95082023-12-13 22:11:13 +0100128 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800129 tcp_seq_num, tcp_ack_num);
130
131 ptr = net_tx_packet + net_eth_hdr_size() +
132 IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
133 offset = ptr;
134
Adriano Cordova47f35e32024-11-11 18:08:58 -0300135 switch (wget_info->method) {
136 case WGET_HTTP_METHOD_HEAD:
137 memcpy(offset, &bootfileHEAD, strlen(bootfileHEAD));
138 offset += strlen(bootfileHEAD);
139 break;
140 case WGET_HTTP_METHOD_GET:
141 default:
142 memcpy(offset, &bootfileGET, strlen(bootfileGET));
143 offset += strlen(bootfileGET);
144 break;
145 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800146
147 memcpy(offset, image_url, strlen(image_url));
148 offset += strlen(image_url);
149
150 memcpy(offset, &bootfile3, strlen(bootfile3));
151 offset += strlen(bootfile3);
Marek Vasut22a95082023-12-13 22:11:13 +0100152 net_send_tcp_packet((offset - ptr), server_port, our_port,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800153 TCP_PUSH, tcp_seq_num, tcp_ack_num);
154 current_wget_state = WGET_CONNECTED;
155 break;
156 case WGET_CONNECTED:
157 case WGET_TRANSFERRING:
158 case WGET_TRANSFERRED:
Marek Vasut22a95082023-12-13 22:11:13 +0100159 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800160 tcp_seq_num, tcp_ack_num);
161 break;
162 }
163}
164
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100165static void wget_send(u8 action, unsigned int tcp_seq_num,
166 unsigned int tcp_ack_num, int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800167{
168 retry_action = action;
169 retry_tcp_ack_num = tcp_ack_num;
170 retry_tcp_seq_num = tcp_seq_num;
171 retry_len = len;
172
173 wget_send_stored();
174}
175
176void wget_fail(char *error_message, unsigned int tcp_seq_num,
177 unsigned int tcp_ack_num, u8 action)
178{
179 printf("wget: Transfer Fail - %s\n", error_message);
180 net_set_timeout_handler(0, NULL);
181 wget_send(action, tcp_seq_num, tcp_ack_num, 0);
182}
183
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800184/*
185 * Interfaces of U-BOOT
186 */
187static void wget_timeout_handler(void)
188{
189 if (++wget_timeout_count > WGET_RETRY_COUNT) {
190 puts("\nRetry count exceeded; starting again\n");
191 wget_send(TCP_RST, 0, 0, 0);
192 net_start_again();
193 } else {
194 puts("T ");
195 net_set_timeout_handler(wget_timeout +
196 WGET_TIMEOUT * wget_timeout_count,
197 wget_timeout_handler);
198 wget_send_stored();
199 }
200}
201
202#define PKT_QUEUE_OFFSET 0x20000
203#define PKT_QUEUE_PACKET_SIZE 0x800
204
Adriano Cordova47f35e32024-11-11 18:08:58 -0300205static void wget_fill_info(const uchar *pkt, int hlen)
206{
207 const char *first_space;
208 const char *second_space;
209 char *pos, *end;
210
Heinrich Schuchardt89e40902024-11-26 13:19:20 -0300211 if (wget_info->headers) {
212 if (hlen < MAX_HTTP_HEADERS_SIZE)
213 strncpy(wget_info->headers, pkt, hlen);
214 else
215 hlen = 0;
216 wget_info->headers[hlen] = 0;
217 }
Adriano Cordova47f35e32024-11-11 18:08:58 -0300218
219 //Get status code
220 first_space = strchr(pkt, ' ');
221 if (!first_space) {
222 wget_info->status_code = -1;
223 return;
224 }
225
226 second_space = strchr(first_space + 1, ' ');
227 if (!second_space) {
228 wget_info->status_code = -1;
229 return;
230 }
231
232 wget_info->status_code = (u32)simple_strtoul(first_space + 1, &end, 10);
233
234 if (second_space != end)
235 wget_info->status_code = -1;
236
237 pos = strstr((char *)pkt, content_len);
238
239 if (pos) {
240 pos += sizeof(content_len) + 1;
241 while (*pos == ' ')
242 pos++;
243 content_length = simple_strtoul(pos, &end, 10);
244 debug_cond(DEBUG_WGET,
245 "wget: Connected Len %lu\n",
246 content_length);
247 wget_info->hdr_cont_len = content_length;
248 }
249}
250
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800251static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100252 u8 action, unsigned int tcp_ack_num, unsigned int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800253{
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800254 uchar *pkt_in_q;
255 char *pos;
256 int hlen, i;
257 uchar *ptr1;
258
259 pkt[len] = '\0';
260 pos = strstr((char *)pkt, http_eom);
261
262 if (!pos) {
263 debug_cond(DEBUG_WGET,
264 "wget: Connected, data before Header %p\n", pkt);
265 pkt_in_q = (void *)image_load_addr + PKT_QUEUE_OFFSET +
266 (pkt_q_idx * PKT_QUEUE_PACKET_SIZE);
267
Yasuharu Shibataafab2682024-08-14 21:41:06 +0900268 ptr1 = map_sysmem((ulong)pkt_in_q, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800269 memcpy(ptr1, pkt, len);
270 unmap_sysmem(ptr1);
271
272 pkt_q[pkt_q_idx].pkt = pkt_in_q;
273 pkt_q[pkt_q_idx].tcp_seq_num = tcp_seq_num;
274 pkt_q[pkt_q_idx].len = len;
275 pkt_q_idx++;
Richard Weinbergerbf44cf42023-07-20 14:51:56 +0200276
277 if (pkt_q_idx >= PKTQ_SZ) {
278 printf("wget: Fatal error, queue overrun!\n");
279 net_set_state(NETLOOP_FAIL);
280
281 return;
282 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800283 } else {
284 debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt);
285 /* sizeof(http_eom) - 1 is the string length of (http_eom) */
286 hlen = pos - (char *)pkt + sizeof(http_eom) - 1;
287 pos = strstr((char *)pkt, linefeed);
288 if (pos > 0)
289 i = pos - (char *)pkt;
290 else
291 i = hlen;
292 printf("%.*s", i, pkt);
293
294 current_wget_state = WGET_TRANSFERRING;
295
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +0900296 initial_data_seq_num = tcp_seq_num + hlen;
297 next_data_seq_num = tcp_seq_num + len;
298
Adriano Cordova47f35e32024-11-11 18:08:58 -0300299 wget_fill_info(pkt, hlen);
300 debug_cond(DEBUG_WGET,
301 "wget: HTTP Status Code %d\n", wget_info->status_code);
302
303 if (wget_info->status_code != 200) {
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800304 debug_cond(DEBUG_WGET,
305 "wget: Connected Bad Xfer\n");
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800306 wget_loop_state = NETLOOP_FAIL;
307 wget_send(action, tcp_seq_num, tcp_ack_num, len);
308 } else {
309 debug_cond(DEBUG_WGET,
Heinrich Schuchardt6b3cefc2024-10-09 01:02:05 +0200310 "wget: Connected Pkt %p hlen %x\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800311 pkt, hlen);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800312
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800313 net_boot_file_size = 0;
314
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900315 if (len > hlen) {
316 if (store_block(pkt + hlen, 0, len - hlen) != 0) {
317 wget_loop_state = NETLOOP_FAIL;
318 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
319 net_set_state(NETLOOP_FAIL);
320 return;
321 }
322 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800323
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800324 for (i = 0; i < pkt_q_idx; i++) {
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900325 int err;
326
Yasuharu Shibataafab2682024-08-14 21:41:06 +0900327 ptr1 = map_sysmem((ulong)pkt_q[i].pkt,
328 pkt_q[i].len);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900329 err = store_block(ptr1,
330 pkt_q[i].tcp_seq_num -
331 initial_data_seq_num,
332 pkt_q[i].len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800333 unmap_sysmem(ptr1);
334 debug_cond(DEBUG_WGET,
Heinrich Schuchardt6b3cefc2024-10-09 01:02:05 +0200335 "wget: Conncted pkt Q %p len %x\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800336 pkt_q[i].pkt, pkt_q[i].len);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900337 if (err) {
338 wget_loop_state = NETLOOP_FAIL;
339 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
340 net_set_state(NETLOOP_FAIL);
341 return;
342 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800343 }
344 }
345 }
346 wget_send(action, tcp_seq_num, tcp_ack_num, len);
347}
348
349/**
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100350 * wget_handler() - TCP handler of wget
351 * @pkt: pointer to the application packet
352 * @dport: destination TCP port
353 * @sip: source IP address
354 * @sport: source TCP port
355 * @tcp_seq_num: TCP sequential number
356 * @tcp_ack_num: TCP acknowledgment number
357 * @action: TCP action (SYN, ACK, FIN, etc)
358 * @len: packet length
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800359 *
360 * In the "application push" invocation, the TCP header with all
361 * its information is pointed to by the packet pointer.
362 */
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100363static void wget_handler(uchar *pkt, u16 dport,
364 struct in_addr sip, u16 sport,
365 u32 tcp_seq_num, u32 tcp_ack_num,
366 u8 action, unsigned int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800367{
368 enum tcp_state wget_tcp_state = tcp_get_tcp_state();
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800369
370 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
371 packets++;
372
373 switch (current_wget_state) {
374 case WGET_CLOSED:
375 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
376 break;
377 case WGET_CONNECTING:
378 debug_cond(DEBUG_WGET,
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100379 "wget: Connecting In len=%x, Seq=%u, Ack=%u\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800380 len, tcp_seq_num, tcp_ack_num);
381 if (!len) {
382 if (wget_tcp_state == TCP_ESTABLISHED) {
383 debug_cond(DEBUG_WGET,
384 "wget: Cting, send, len=%x\n", len);
385 wget_send(action, tcp_seq_num, tcp_ack_num,
386 len);
387 } else {
388 printf("%.*s", len, pkt);
389 wget_fail("wget: Handler Connected Fail\n",
390 tcp_seq_num, tcp_ack_num, action);
391 }
392 }
393 break;
394 case WGET_CONNECTED:
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100395 debug_cond(DEBUG_WGET, "wget: Connected seq=%u, len=%x\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800396 tcp_seq_num, len);
397 if (!len) {
398 wget_fail("Image not found, no data returned\n",
399 tcp_seq_num, tcp_ack_num, action);
400 } else {
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100401 wget_connected(pkt, tcp_seq_num, action, tcp_ack_num, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800402 }
403 break;
404 case WGET_TRANSFERRING:
405 debug_cond(DEBUG_WGET,
406 "wget: Transferring, seq=%x, ack=%x,len=%x\n",
407 tcp_seq_num, tcp_ack_num, len);
408
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +0900409 if (next_data_seq_num != tcp_seq_num) {
410 debug_cond(DEBUG_WGET, "wget: seq=%x packet was lost\n", next_data_seq_num);
411 return;
412 }
413 next_data_seq_num = tcp_seq_num + len;
414
Yasuharu Shibata2c9b5af2024-04-16 09:26:24 +0900415 if (store_block(pkt, tcp_seq_num - initial_data_seq_num, len) != 0) {
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800416 wget_fail("wget: store error\n",
417 tcp_seq_num, tcp_ack_num, action);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900418 net_set_state(NETLOOP_FAIL);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800419 return;
420 }
421
422 switch (wget_tcp_state) {
423 case TCP_FIN_WAIT_2:
424 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
425 fallthrough;
426 case TCP_SYN_SENT:
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100427 case TCP_SYN_RECEIVED:
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800428 case TCP_CLOSING:
429 case TCP_FIN_WAIT_1:
430 case TCP_CLOSED:
431 net_set_state(NETLOOP_FAIL);
432 break;
433 case TCP_ESTABLISHED:
434 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
435 len);
436 wget_loop_state = NETLOOP_SUCCESS;
437 break;
438 case TCP_CLOSE_WAIT: /* End of transfer */
439 current_wget_state = WGET_TRANSFERRED;
440 wget_send(action | TCP_ACK | TCP_FIN,
441 tcp_seq_num, tcp_ack_num, len);
442 break;
443 }
444 break;
445 case WGET_TRANSFERRED:
446 printf("Packets received %d, Transfer Successful\n", packets);
447 net_set_state(wget_loop_state);
Adriano Cordova47f35e32024-11-11 18:08:58 -0300448 wget_info->file_size = net_boot_file_size;
449 if (wget_info->method == WGET_HTTP_METHOD_GET && wget_info->set_bootdev) {
450 efi_set_bootdev("Net", "", image_url,
451 map_sysmem(image_load_addr, 0),
452 net_boot_file_size);
453 env_set_hex("filesize", net_boot_file_size);
454 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800455 break;
456 }
457}
458
459#define RANDOM_PORT_START 1024
460#define RANDOM_PORT_RANGE 0x4000
461
462/**
463 * random_port() - make port a little random (1024-17407)
464 *
465 * Return: random port number from 1024 to 17407
466 *
467 * This keeps the math somewhat trivial to compute, and seems to work with
468 * all supported protocols/clients/servers
469 */
470static unsigned int random_port(void)
471{
472 return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
473}
474
475#define BLOCKSIZE 512
476
477void wget_start(void)
478{
Adriano Cordova47f35e32024-11-11 18:08:58 -0300479 if (!wget_info)
480 wget_info = &default_wget_info;
481
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800482 image_url = strchr(net_boot_file_name, ':');
483 if (image_url > 0) {
484 web_server_ip = string_to_ip(net_boot_file_name);
485 ++image_url;
486 net_server_ip = web_server_ip;
487 } else {
488 web_server_ip = net_server_ip;
489 image_url = net_boot_file_name;
490 }
491
492 debug_cond(DEBUG_WGET,
493 "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
494 &web_server_ip, &net_ip);
495
496 /* Check if we need to send across this subnet */
497 if (net_gateway.s_addr && net_netmask.s_addr) {
498 struct in_addr our_net;
499 struct in_addr server_net;
500
501 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
502 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
503 if (our_net.s_addr != server_net.s_addr)
504 debug_cond(DEBUG_WGET,
505 "wget: sending through gateway %pI4",
506 &net_gateway);
507 }
508 debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
509
510 if (net_boot_file_expected_size_in_blocks) {
511 debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ",
512 net_boot_file_expected_size_in_blocks * BLOCKSIZE);
513 print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE,
514 "");
515 }
516 debug_cond(DEBUG_WGET,
517 "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
518
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800519 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
520 tcp_set_tcp_handler(wget_handler);
521
522 wget_timeout_count = 0;
523 current_wget_state = WGET_CLOSED;
524
525 our_port = random_port();
526
527 /*
528 * Zero out server ether to force arp resolution in case
529 * the server ip for the previous u-boot command, for example dns
530 * is not the same as the web server ip.
531 */
532
533 memset(net_server_ethaddr, 0, 6);
534
535 wget_send(TCP_SYN, 0, 0, 0);
536}
Masahisa Kojima6721d182023-11-10 13:25:35 +0900537
538#if (IS_ENABLED(CONFIG_CMD_DNS))
539int wget_with_dns(ulong dst_addr, char *uri)
540{
541 int ret;
542 char *s, *host_name, *file_name, *str_copy;
543
544 /*
545 * Download file using wget.
546 *
547 * U-Boot wget takes the target uri in this format.
548 * "<http server ip>:<file path>" e.g.) 192.168.1.1:/sample/test.iso
549 * Need to resolve the http server ip address before starting wget.
550 */
551 str_copy = strdup(uri);
552 if (!str_copy)
553 return -ENOMEM;
554
555 s = str_copy + strlen("http://");
556 host_name = strsep(&s, "/");
557 if (!s) {
558 log_err("Error: invalied uri, no file path\n");
559 ret = -EINVAL;
560 goto out;
561 }
562 file_name = s;
563
564 /* TODO: If the given uri has ip address for the http server, skip dns */
565 net_dns_resolve = host_name;
566 net_dns_env_var = "httpserverip";
567 if (net_loop(DNS) < 0) {
568 log_err("Error: dns lookup of %s failed, check setup\n", net_dns_resolve);
569 ret = -EINVAL;
570 goto out;
571 }
572 s = env_get("httpserverip");
573 if (!s) {
574 ret = -EINVAL;
575 goto out;
576 }
577
578 strlcpy(net_boot_file_name, s, sizeof(net_boot_file_name));
579 strlcat(net_boot_file_name, ":/", sizeof(net_boot_file_name)); /* append '/' which is removed by strsep() */
580 strlcat(net_boot_file_name, file_name, sizeof(net_boot_file_name));
581 image_load_addr = dst_addr;
582 ret = net_loop(WGET);
583
584out:
585 free(str_copy);
586
Adriano Cordova450b2342024-11-11 18:08:59 -0300587 return ret < 0 ? ret : 0;
Masahisa Kojima6721d182023-11-10 13:25:35 +0900588}
589#endif
Masahisa Kojimae501ce12023-11-10 13:25:41 +0900590
591/**
592 * wget_validate_uri() - validate the uri for wget
593 *
594 * @uri: uri string
595 *
596 * This function follows the current U-Boot wget implementation.
597 * scheme: only "http:" is supported
598 * authority:
599 * - user information: not supported
600 * - host: supported
601 * - port: not supported(always use the default port)
602 *
603 * Uri is expected to be correctly percent encoded.
604 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
605 * and space character(0x20) are not allowed.
606 *
607 * TODO: stricter uri conformance check
608 *
609 * Return: true on success, false on failure
610 */
611bool wget_validate_uri(char *uri)
612{
613 char c;
614 bool ret = true;
615 char *str_copy, *s, *authority;
616
617 for (c = 0x1; c < 0x21; c++) {
618 if (strchr(uri, c)) {
619 log_err("invalid character is used\n");
620 return false;
621 }
622 }
623 if (strchr(uri, 0x7f)) {
624 log_err("invalid character is used\n");
625 return false;
626 }
627
628 if (strncmp(uri, "http://", 7)) {
629 log_err("only http:// is supported\n");
630 return false;
631 }
632 str_copy = strdup(uri);
633 if (!str_copy)
634 return false;
635
636 s = str_copy + strlen("http://");
637 authority = strsep(&s, "/");
638 if (!s) {
639 log_err("invalid uri, no file path\n");
640 ret = false;
641 goto out;
642 }
643 s = strchr(authority, '@');
644 if (s) {
645 log_err("user information is not supported\n");
646 ret = false;
647 goto out;
648 }
649 s = strchr(authority, ':');
650 if (s) {
651 log_err("user defined port is not supported\n");
652 ret = false;
653 goto out;
654 }
655
656out:
657 free(str_copy);
658
659 return ret;
660}