blob: c1cd8216bc3e8ff22650e2b86304f453290ecfe4 [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>
11#include <image.h>
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090012#include <lmb.h>
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080013#include <mapmem.h>
14#include <net.h>
15#include <net/tcp.h>
16#include <net/wget.h>
Masahisa Kojima6721d182023-11-10 13:25:35 +090017#include <stdlib.h>
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080018
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090019DECLARE_GLOBAL_DATA_PTR;
20
Marek Vasut22a95082023-12-13 22:11:13 +010021/* The default, change with environment variable 'httpdstp' */
22#define SERVER_PORT 80
23
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080024static const char bootfile1[] = "GET ";
25static const char bootfile3[] = " HTTP/1.0\r\n\r\n";
26static const char http_eom[] = "\r\n\r\n";
27static const char http_ok[] = "200";
28static const char content_len[] = "Content-Length";
29static const char linefeed[] = "\r\n";
30static struct in_addr web_server_ip;
31static int our_port;
32static int wget_timeout_count;
33
34struct pkt_qd {
35 uchar *pkt;
36 unsigned int tcp_seq_num;
37 unsigned int len;
38};
39
40/*
41 * This is a control structure for out of order packets received.
42 * The actual packet bufers are in the kernel space, and are
43 * expected to be overwritten by the downloaded image.
44 */
Richard Weinbergerbf44cf42023-07-20 14:51:56 +020045#define PKTQ_SZ (PKTBUFSRX / 4)
46static struct pkt_qd pkt_q[PKTQ_SZ];
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080047static int pkt_q_idx;
48static unsigned long content_length;
49static unsigned int packets;
50
51static unsigned int initial_data_seq_num;
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +090052static unsigned int next_data_seq_num;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080053
54static enum wget_state current_wget_state;
55
56static char *image_url;
57static unsigned int wget_timeout = WGET_TIMEOUT;
58
59static enum net_loop_state wget_loop_state;
60
61/* Timeout retry parameters */
62static u8 retry_action; /* actions for TCP retry */
63static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/
64static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */
65static int retry_len; /* TCP retry length */
66
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090067static ulong wget_load_size;
68
69/**
70 * wget_init_max_size() - initialize maximum load size
71 *
72 * Return: 0 if success, -1 if fails
73 */
74static int wget_init_load_size(void)
75{
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090076 phys_size_t max_size;
77
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053078 lmb_init_and_reserve(gd->bd, (void *)gd->fdt_blob);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090079
Sughosh Ganu291bf9c2024-08-26 17:29:18 +053080 max_size = lmb_get_free_size(image_load_addr);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090081 if (!max_size)
82 return -1;
83
84 wget_load_size = max_size;
85
86 return 0;
87}
88
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080089/**
90 * store_block() - store block in memory
91 * @src: source of data
92 * @offset: offset
93 * @len: length
94 */
95static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
96{
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090097 ulong store_addr = image_load_addr + offset;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080098 ulong newsize = offset + len;
99 uchar *ptr;
100
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900101 if (IS_ENABLED(CONFIG_LMB)) {
102 ulong end_addr = image_load_addr + wget_load_size;
103
104 if (!end_addr)
105 end_addr = ULONG_MAX;
106
107 if (store_addr < image_load_addr ||
108 store_addr + len > end_addr) {
109 printf("\nwget error: ");
110 printf("trying to overwrite reserved memory...\n");
111 return -1;
112 }
113 }
114
115 ptr = map_sysmem(store_addr, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800116 memcpy(ptr, src, len);
117 unmap_sysmem(ptr);
118
119 if (net_boot_file_size < (offset + len))
120 net_boot_file_size = newsize;
121
122 return 0;
123}
124
125/**
126 * wget_send_stored() - wget response dispatcher
127 *
128 * WARNING, This, and only this, is the place in wget.c where
129 * SEQUENCE NUMBERS are swapped between incoming (RX)
130 * and outgoing (TX).
131 * Procedure wget_handler() is correct for RX traffic.
132 */
133static void wget_send_stored(void)
134{
135 u8 action = retry_action;
136 int len = retry_len;
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100137 unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len);
138 unsigned int tcp_seq_num = retry_tcp_ack_num;
Marek Vasut22a95082023-12-13 22:11:13 +0100139 unsigned int server_port;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800140 uchar *ptr, *offset;
141
Marek Vasut22a95082023-12-13 22:11:13 +0100142 server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff;
143
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800144 switch (current_wget_state) {
145 case WGET_CLOSED:
146 debug_cond(DEBUG_WGET, "wget: send SYN\n");
147 current_wget_state = WGET_CONNECTING;
Marek Vasut22a95082023-12-13 22:11:13 +0100148 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800149 tcp_seq_num, tcp_ack_num);
150 packets = 0;
151 break;
152 case WGET_CONNECTING:
153 pkt_q_idx = 0;
Marek Vasut22a95082023-12-13 22:11:13 +0100154 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800155 tcp_seq_num, tcp_ack_num);
156
157 ptr = net_tx_packet + net_eth_hdr_size() +
158 IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
159 offset = ptr;
160
161 memcpy(offset, &bootfile1, strlen(bootfile1));
162 offset += strlen(bootfile1);
163
164 memcpy(offset, image_url, strlen(image_url));
165 offset += strlen(image_url);
166
167 memcpy(offset, &bootfile3, strlen(bootfile3));
168 offset += strlen(bootfile3);
Marek Vasut22a95082023-12-13 22:11:13 +0100169 net_send_tcp_packet((offset - ptr), server_port, our_port,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800170 TCP_PUSH, tcp_seq_num, tcp_ack_num);
171 current_wget_state = WGET_CONNECTED;
172 break;
173 case WGET_CONNECTED:
174 case WGET_TRANSFERRING:
175 case WGET_TRANSFERRED:
Marek Vasut22a95082023-12-13 22:11:13 +0100176 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800177 tcp_seq_num, tcp_ack_num);
178 break;
179 }
180}
181
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100182static void wget_send(u8 action, unsigned int tcp_seq_num,
183 unsigned int tcp_ack_num, int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800184{
185 retry_action = action;
186 retry_tcp_ack_num = tcp_ack_num;
187 retry_tcp_seq_num = tcp_seq_num;
188 retry_len = len;
189
190 wget_send_stored();
191}
192
193void wget_fail(char *error_message, unsigned int tcp_seq_num,
194 unsigned int tcp_ack_num, u8 action)
195{
196 printf("wget: Transfer Fail - %s\n", error_message);
197 net_set_timeout_handler(0, NULL);
198 wget_send(action, tcp_seq_num, tcp_ack_num, 0);
199}
200
201void wget_success(u8 action, unsigned int tcp_seq_num,
202 unsigned int tcp_ack_num, int len, int packets)
203{
204 printf("Packets received %d, Transfer Successful\n", packets);
205 wget_send(action, tcp_seq_num, tcp_ack_num, len);
206}
207
208/*
209 * Interfaces of U-BOOT
210 */
211static void wget_timeout_handler(void)
212{
213 if (++wget_timeout_count > WGET_RETRY_COUNT) {
214 puts("\nRetry count exceeded; starting again\n");
215 wget_send(TCP_RST, 0, 0, 0);
216 net_start_again();
217 } else {
218 puts("T ");
219 net_set_timeout_handler(wget_timeout +
220 WGET_TIMEOUT * wget_timeout_count,
221 wget_timeout_handler);
222 wget_send_stored();
223 }
224}
225
226#define PKT_QUEUE_OFFSET 0x20000
227#define PKT_QUEUE_PACKET_SIZE 0x800
228
229static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100230 u8 action, unsigned int tcp_ack_num, unsigned int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800231{
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800232 uchar *pkt_in_q;
233 char *pos;
234 int hlen, i;
235 uchar *ptr1;
236
237 pkt[len] = '\0';
238 pos = strstr((char *)pkt, http_eom);
239
240 if (!pos) {
241 debug_cond(DEBUG_WGET,
242 "wget: Connected, data before Header %p\n", pkt);
243 pkt_in_q = (void *)image_load_addr + PKT_QUEUE_OFFSET +
244 (pkt_q_idx * PKT_QUEUE_PACKET_SIZE);
245
Yasuharu Shibataafab2682024-08-14 21:41:06 +0900246 ptr1 = map_sysmem((ulong)pkt_in_q, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800247 memcpy(ptr1, pkt, len);
248 unmap_sysmem(ptr1);
249
250 pkt_q[pkt_q_idx].pkt = pkt_in_q;
251 pkt_q[pkt_q_idx].tcp_seq_num = tcp_seq_num;
252 pkt_q[pkt_q_idx].len = len;
253 pkt_q_idx++;
Richard Weinbergerbf44cf42023-07-20 14:51:56 +0200254
255 if (pkt_q_idx >= PKTQ_SZ) {
256 printf("wget: Fatal error, queue overrun!\n");
257 net_set_state(NETLOOP_FAIL);
258
259 return;
260 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800261 } else {
262 debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt);
263 /* sizeof(http_eom) - 1 is the string length of (http_eom) */
264 hlen = pos - (char *)pkt + sizeof(http_eom) - 1;
265 pos = strstr((char *)pkt, linefeed);
266 if (pos > 0)
267 i = pos - (char *)pkt;
268 else
269 i = hlen;
270 printf("%.*s", i, pkt);
271
272 current_wget_state = WGET_TRANSFERRING;
273
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +0900274 initial_data_seq_num = tcp_seq_num + hlen;
275 next_data_seq_num = tcp_seq_num + len;
276
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800277 if (strstr((char *)pkt, http_ok) == 0) {
278 debug_cond(DEBUG_WGET,
279 "wget: Connected Bad Xfer\n");
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800280 wget_loop_state = NETLOOP_FAIL;
281 wget_send(action, tcp_seq_num, tcp_ack_num, len);
282 } else {
283 debug_cond(DEBUG_WGET,
284 "wget: Connctd pkt %p hlen %x\n",
285 pkt, hlen);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800286
287 pos = strstr((char *)pkt, content_len);
288 if (!pos) {
289 content_length = -1;
290 } else {
291 pos += sizeof(content_len) + 2;
292 strict_strtoul(pos, 10, &content_length);
293 debug_cond(DEBUG_WGET,
294 "wget: Connected Len %lu\n",
295 content_length);
296 }
297
298 net_boot_file_size = 0;
299
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900300 if (len > hlen) {
301 if (store_block(pkt + hlen, 0, len - hlen) != 0) {
302 wget_loop_state = NETLOOP_FAIL;
303 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
304 net_set_state(NETLOOP_FAIL);
305 return;
306 }
307 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800308
309 debug_cond(DEBUG_WGET,
310 "wget: Connected Pkt %p hlen %x\n",
311 pkt, hlen);
312
313 for (i = 0; i < pkt_q_idx; i++) {
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900314 int err;
315
Yasuharu Shibataafab2682024-08-14 21:41:06 +0900316 ptr1 = map_sysmem((ulong)pkt_q[i].pkt,
317 pkt_q[i].len);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900318 err = store_block(ptr1,
319 pkt_q[i].tcp_seq_num -
320 initial_data_seq_num,
321 pkt_q[i].len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800322 unmap_sysmem(ptr1);
323 debug_cond(DEBUG_WGET,
324 "wget: Connctd pkt Q %p len %x\n",
325 pkt_q[i].pkt, pkt_q[i].len);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900326 if (err) {
327 wget_loop_state = NETLOOP_FAIL;
328 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
329 net_set_state(NETLOOP_FAIL);
330 return;
331 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800332 }
333 }
334 }
335 wget_send(action, tcp_seq_num, tcp_ack_num, len);
336}
337
338/**
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100339 * wget_handler() - TCP handler of wget
340 * @pkt: pointer to the application packet
341 * @dport: destination TCP port
342 * @sip: source IP address
343 * @sport: source TCP port
344 * @tcp_seq_num: TCP sequential number
345 * @tcp_ack_num: TCP acknowledgment number
346 * @action: TCP action (SYN, ACK, FIN, etc)
347 * @len: packet length
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800348 *
349 * In the "application push" invocation, the TCP header with all
350 * its information is pointed to by the packet pointer.
351 */
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100352static void wget_handler(uchar *pkt, u16 dport,
353 struct in_addr sip, u16 sport,
354 u32 tcp_seq_num, u32 tcp_ack_num,
355 u8 action, unsigned int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800356{
357 enum tcp_state wget_tcp_state = tcp_get_tcp_state();
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800358
359 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
360 packets++;
361
362 switch (current_wget_state) {
363 case WGET_CLOSED:
364 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
365 break;
366 case WGET_CONNECTING:
367 debug_cond(DEBUG_WGET,
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100368 "wget: Connecting In len=%x, Seq=%u, Ack=%u\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800369 len, tcp_seq_num, tcp_ack_num);
370 if (!len) {
371 if (wget_tcp_state == TCP_ESTABLISHED) {
372 debug_cond(DEBUG_WGET,
373 "wget: Cting, send, len=%x\n", len);
374 wget_send(action, tcp_seq_num, tcp_ack_num,
375 len);
376 } else {
377 printf("%.*s", len, pkt);
378 wget_fail("wget: Handler Connected Fail\n",
379 tcp_seq_num, tcp_ack_num, action);
380 }
381 }
382 break;
383 case WGET_CONNECTED:
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100384 debug_cond(DEBUG_WGET, "wget: Connected seq=%u, len=%x\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800385 tcp_seq_num, len);
386 if (!len) {
387 wget_fail("Image not found, no data returned\n",
388 tcp_seq_num, tcp_ack_num, action);
389 } else {
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100390 wget_connected(pkt, tcp_seq_num, action, tcp_ack_num, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800391 }
392 break;
393 case WGET_TRANSFERRING:
394 debug_cond(DEBUG_WGET,
395 "wget: Transferring, seq=%x, ack=%x,len=%x\n",
396 tcp_seq_num, tcp_ack_num, len);
397
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +0900398 if (next_data_seq_num != tcp_seq_num) {
399 debug_cond(DEBUG_WGET, "wget: seq=%x packet was lost\n", next_data_seq_num);
400 return;
401 }
402 next_data_seq_num = tcp_seq_num + len;
403
Yasuharu Shibata2c9b5af2024-04-16 09:26:24 +0900404 if (store_block(pkt, tcp_seq_num - initial_data_seq_num, len) != 0) {
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800405 wget_fail("wget: store error\n",
406 tcp_seq_num, tcp_ack_num, action);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900407 net_set_state(NETLOOP_FAIL);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800408 return;
409 }
410
411 switch (wget_tcp_state) {
412 case TCP_FIN_WAIT_2:
413 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
414 fallthrough;
415 case TCP_SYN_SENT:
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100416 case TCP_SYN_RECEIVED:
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800417 case TCP_CLOSING:
418 case TCP_FIN_WAIT_1:
419 case TCP_CLOSED:
420 net_set_state(NETLOOP_FAIL);
421 break;
422 case TCP_ESTABLISHED:
423 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
424 len);
425 wget_loop_state = NETLOOP_SUCCESS;
426 break;
427 case TCP_CLOSE_WAIT: /* End of transfer */
428 current_wget_state = WGET_TRANSFERRED;
429 wget_send(action | TCP_ACK | TCP_FIN,
430 tcp_seq_num, tcp_ack_num, len);
431 break;
432 }
433 break;
434 case WGET_TRANSFERRED:
435 printf("Packets received %d, Transfer Successful\n", packets);
436 net_set_state(wget_loop_state);
437 break;
438 }
439}
440
441#define RANDOM_PORT_START 1024
442#define RANDOM_PORT_RANGE 0x4000
443
444/**
445 * random_port() - make port a little random (1024-17407)
446 *
447 * Return: random port number from 1024 to 17407
448 *
449 * This keeps the math somewhat trivial to compute, and seems to work with
450 * all supported protocols/clients/servers
451 */
452static unsigned int random_port(void)
453{
454 return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
455}
456
457#define BLOCKSIZE 512
458
459void wget_start(void)
460{
461 image_url = strchr(net_boot_file_name, ':');
462 if (image_url > 0) {
463 web_server_ip = string_to_ip(net_boot_file_name);
464 ++image_url;
465 net_server_ip = web_server_ip;
466 } else {
467 web_server_ip = net_server_ip;
468 image_url = net_boot_file_name;
469 }
470
471 debug_cond(DEBUG_WGET,
472 "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
473 &web_server_ip, &net_ip);
474
475 /* Check if we need to send across this subnet */
476 if (net_gateway.s_addr && net_netmask.s_addr) {
477 struct in_addr our_net;
478 struct in_addr server_net;
479
480 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
481 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
482 if (our_net.s_addr != server_net.s_addr)
483 debug_cond(DEBUG_WGET,
484 "wget: sending through gateway %pI4",
485 &net_gateway);
486 }
487 debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
488
489 if (net_boot_file_expected_size_in_blocks) {
490 debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ",
491 net_boot_file_expected_size_in_blocks * BLOCKSIZE);
492 print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE,
493 "");
494 }
495 debug_cond(DEBUG_WGET,
496 "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
497
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900498 if (IS_ENABLED(CONFIG_LMB)) {
499 if (wget_init_load_size()) {
500 printf("\nwget error: ");
501 printf("trying to overwrite reserved memory...\n");
502 net_set_state(NETLOOP_FAIL);
503 return;
504 }
505 }
506
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800507 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
508 tcp_set_tcp_handler(wget_handler);
509
510 wget_timeout_count = 0;
511 current_wget_state = WGET_CLOSED;
512
513 our_port = random_port();
514
515 /*
516 * Zero out server ether to force arp resolution in case
517 * the server ip for the previous u-boot command, for example dns
518 * is not the same as the web server ip.
519 */
520
521 memset(net_server_ethaddr, 0, 6);
522
523 wget_send(TCP_SYN, 0, 0, 0);
524}
Masahisa Kojima6721d182023-11-10 13:25:35 +0900525
526#if (IS_ENABLED(CONFIG_CMD_DNS))
527int wget_with_dns(ulong dst_addr, char *uri)
528{
529 int ret;
530 char *s, *host_name, *file_name, *str_copy;
531
532 /*
533 * Download file using wget.
534 *
535 * U-Boot wget takes the target uri in this format.
536 * "<http server ip>:<file path>" e.g.) 192.168.1.1:/sample/test.iso
537 * Need to resolve the http server ip address before starting wget.
538 */
539 str_copy = strdup(uri);
540 if (!str_copy)
541 return -ENOMEM;
542
543 s = str_copy + strlen("http://");
544 host_name = strsep(&s, "/");
545 if (!s) {
546 log_err("Error: invalied uri, no file path\n");
547 ret = -EINVAL;
548 goto out;
549 }
550 file_name = s;
551
552 /* TODO: If the given uri has ip address for the http server, skip dns */
553 net_dns_resolve = host_name;
554 net_dns_env_var = "httpserverip";
555 if (net_loop(DNS) < 0) {
556 log_err("Error: dns lookup of %s failed, check setup\n", net_dns_resolve);
557 ret = -EINVAL;
558 goto out;
559 }
560 s = env_get("httpserverip");
561 if (!s) {
562 ret = -EINVAL;
563 goto out;
564 }
565
566 strlcpy(net_boot_file_name, s, sizeof(net_boot_file_name));
567 strlcat(net_boot_file_name, ":/", sizeof(net_boot_file_name)); /* append '/' which is removed by strsep() */
568 strlcat(net_boot_file_name, file_name, sizeof(net_boot_file_name));
569 image_load_addr = dst_addr;
570 ret = net_loop(WGET);
571
572out:
573 free(str_copy);
574
575 return ret;
576}
577#endif
Masahisa Kojimae501ce12023-11-10 13:25:41 +0900578
579/**
580 * wget_validate_uri() - validate the uri for wget
581 *
582 * @uri: uri string
583 *
584 * This function follows the current U-Boot wget implementation.
585 * scheme: only "http:" is supported
586 * authority:
587 * - user information: not supported
588 * - host: supported
589 * - port: not supported(always use the default port)
590 *
591 * Uri is expected to be correctly percent encoded.
592 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
593 * and space character(0x20) are not allowed.
594 *
595 * TODO: stricter uri conformance check
596 *
597 * Return: true on success, false on failure
598 */
599bool wget_validate_uri(char *uri)
600{
601 char c;
602 bool ret = true;
603 char *str_copy, *s, *authority;
604
605 for (c = 0x1; c < 0x21; c++) {
606 if (strchr(uri, c)) {
607 log_err("invalid character is used\n");
608 return false;
609 }
610 }
611 if (strchr(uri, 0x7f)) {
612 log_err("invalid character is used\n");
613 return false;
614 }
615
616 if (strncmp(uri, "http://", 7)) {
617 log_err("only http:// is supported\n");
618 return false;
619 }
620 str_copy = strdup(uri);
621 if (!str_copy)
622 return false;
623
624 s = str_copy + strlen("http://");
625 authority = strsep(&s, "/");
626 if (!s) {
627 log_err("invalid uri, no file path\n");
628 ret = false;
629 goto out;
630 }
631 s = strchr(authority, '@');
632 if (s) {
633 log_err("user information is not supported\n");
634 ret = false;
635 goto out;
636 }
637 s = strchr(authority, ':');
638 if (s) {
639 log_err("user defined port is not supported\n");
640 ret = false;
641 goto out;
642 }
643
644out:
645 free(str_copy);
646
647 return ret;
648}