blob: 4a168641c653b4436374d52f8cd45b474c77c426 [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 max_size = lmb_get_free_size(image_load_addr);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090079 if (!max_size)
80 return -1;
81
82 wget_load_size = max_size;
83
84 return 0;
85}
86
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080087/**
88 * store_block() - store block in memory
89 * @src: source of data
90 * @offset: offset
91 * @len: length
92 */
93static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
94{
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090095 ulong store_addr = image_load_addr + offset;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080096 ulong newsize = offset + len;
97 uchar *ptr;
98
Sughosh Ganu0d733f02024-08-26 17:29:22 +053099 if (CONFIG_IS_ENABLED(LMB)) {
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900100 ulong end_addr = image_load_addr + wget_load_size;
101
102 if (!end_addr)
103 end_addr = ULONG_MAX;
104
105 if (store_addr < image_load_addr ||
106 store_addr + len > end_addr) {
107 printf("\nwget error: ");
108 printf("trying to overwrite reserved memory...\n");
109 return -1;
110 }
111 }
112
113 ptr = map_sysmem(store_addr, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800114 memcpy(ptr, src, len);
115 unmap_sysmem(ptr);
116
117 if (net_boot_file_size < (offset + len))
118 net_boot_file_size = newsize;
119
120 return 0;
121}
122
123/**
124 * wget_send_stored() - wget response dispatcher
125 *
126 * WARNING, This, and only this, is the place in wget.c where
127 * SEQUENCE NUMBERS are swapped between incoming (RX)
128 * and outgoing (TX).
129 * Procedure wget_handler() is correct for RX traffic.
130 */
131static void wget_send_stored(void)
132{
133 u8 action = retry_action;
134 int len = retry_len;
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100135 unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len);
136 unsigned int tcp_seq_num = retry_tcp_ack_num;
Marek Vasut22a95082023-12-13 22:11:13 +0100137 unsigned int server_port;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800138 uchar *ptr, *offset;
139
Marek Vasut22a95082023-12-13 22:11:13 +0100140 server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff;
141
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800142 switch (current_wget_state) {
143 case WGET_CLOSED:
144 debug_cond(DEBUG_WGET, "wget: send SYN\n");
145 current_wget_state = WGET_CONNECTING;
Marek Vasut22a95082023-12-13 22:11:13 +0100146 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800147 tcp_seq_num, tcp_ack_num);
148 packets = 0;
149 break;
150 case WGET_CONNECTING:
151 pkt_q_idx = 0;
Marek Vasut22a95082023-12-13 22:11:13 +0100152 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800153 tcp_seq_num, tcp_ack_num);
154
155 ptr = net_tx_packet + net_eth_hdr_size() +
156 IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
157 offset = ptr;
158
159 memcpy(offset, &bootfile1, strlen(bootfile1));
160 offset += strlen(bootfile1);
161
162 memcpy(offset, image_url, strlen(image_url));
163 offset += strlen(image_url);
164
165 memcpy(offset, &bootfile3, strlen(bootfile3));
166 offset += strlen(bootfile3);
Marek Vasut22a95082023-12-13 22:11:13 +0100167 net_send_tcp_packet((offset - ptr), server_port, our_port,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800168 TCP_PUSH, tcp_seq_num, tcp_ack_num);
169 current_wget_state = WGET_CONNECTED;
170 break;
171 case WGET_CONNECTED:
172 case WGET_TRANSFERRING:
173 case WGET_TRANSFERRED:
Marek Vasut22a95082023-12-13 22:11:13 +0100174 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800175 tcp_seq_num, tcp_ack_num);
176 break;
177 }
178}
179
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100180static void wget_send(u8 action, unsigned int tcp_seq_num,
181 unsigned int tcp_ack_num, int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800182{
183 retry_action = action;
184 retry_tcp_ack_num = tcp_ack_num;
185 retry_tcp_seq_num = tcp_seq_num;
186 retry_len = len;
187
188 wget_send_stored();
189}
190
191void wget_fail(char *error_message, unsigned int tcp_seq_num,
192 unsigned int tcp_ack_num, u8 action)
193{
194 printf("wget: Transfer Fail - %s\n", error_message);
195 net_set_timeout_handler(0, NULL);
196 wget_send(action, tcp_seq_num, tcp_ack_num, 0);
197}
198
199void wget_success(u8 action, unsigned int tcp_seq_num,
200 unsigned int tcp_ack_num, int len, int packets)
201{
202 printf("Packets received %d, Transfer Successful\n", packets);
203 wget_send(action, tcp_seq_num, tcp_ack_num, len);
204}
205
206/*
207 * Interfaces of U-BOOT
208 */
209static void wget_timeout_handler(void)
210{
211 if (++wget_timeout_count > WGET_RETRY_COUNT) {
212 puts("\nRetry count exceeded; starting again\n");
213 wget_send(TCP_RST, 0, 0, 0);
214 net_start_again();
215 } else {
216 puts("T ");
217 net_set_timeout_handler(wget_timeout +
218 WGET_TIMEOUT * wget_timeout_count,
219 wget_timeout_handler);
220 wget_send_stored();
221 }
222}
223
224#define PKT_QUEUE_OFFSET 0x20000
225#define PKT_QUEUE_PACKET_SIZE 0x800
226
227static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100228 u8 action, unsigned int tcp_ack_num, unsigned int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800229{
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800230 uchar *pkt_in_q;
231 char *pos;
232 int hlen, i;
233 uchar *ptr1;
234
235 pkt[len] = '\0';
236 pos = strstr((char *)pkt, http_eom);
237
238 if (!pos) {
239 debug_cond(DEBUG_WGET,
240 "wget: Connected, data before Header %p\n", pkt);
241 pkt_in_q = (void *)image_load_addr + PKT_QUEUE_OFFSET +
242 (pkt_q_idx * PKT_QUEUE_PACKET_SIZE);
243
Yasuharu Shibataafab2682024-08-14 21:41:06 +0900244 ptr1 = map_sysmem((ulong)pkt_in_q, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800245 memcpy(ptr1, pkt, len);
246 unmap_sysmem(ptr1);
247
248 pkt_q[pkt_q_idx].pkt = pkt_in_q;
249 pkt_q[pkt_q_idx].tcp_seq_num = tcp_seq_num;
250 pkt_q[pkt_q_idx].len = len;
251 pkt_q_idx++;
Richard Weinbergerbf44cf42023-07-20 14:51:56 +0200252
253 if (pkt_q_idx >= PKTQ_SZ) {
254 printf("wget: Fatal error, queue overrun!\n");
255 net_set_state(NETLOOP_FAIL);
256
257 return;
258 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800259 } else {
260 debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt);
261 /* sizeof(http_eom) - 1 is the string length of (http_eom) */
262 hlen = pos - (char *)pkt + sizeof(http_eom) - 1;
263 pos = strstr((char *)pkt, linefeed);
264 if (pos > 0)
265 i = pos - (char *)pkt;
266 else
267 i = hlen;
268 printf("%.*s", i, pkt);
269
270 current_wget_state = WGET_TRANSFERRING;
271
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +0900272 initial_data_seq_num = tcp_seq_num + hlen;
273 next_data_seq_num = tcp_seq_num + len;
274
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800275 if (strstr((char *)pkt, http_ok) == 0) {
276 debug_cond(DEBUG_WGET,
277 "wget: Connected Bad Xfer\n");
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800278 wget_loop_state = NETLOOP_FAIL;
279 wget_send(action, tcp_seq_num, tcp_ack_num, len);
280 } else {
281 debug_cond(DEBUG_WGET,
282 "wget: Connctd pkt %p hlen %x\n",
283 pkt, hlen);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800284
285 pos = strstr((char *)pkt, content_len);
286 if (!pos) {
287 content_length = -1;
288 } else {
289 pos += sizeof(content_len) + 2;
290 strict_strtoul(pos, 10, &content_length);
291 debug_cond(DEBUG_WGET,
292 "wget: Connected Len %lu\n",
293 content_length);
294 }
295
296 net_boot_file_size = 0;
297
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900298 if (len > hlen) {
299 if (store_block(pkt + hlen, 0, len - hlen) != 0) {
300 wget_loop_state = NETLOOP_FAIL;
301 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
302 net_set_state(NETLOOP_FAIL);
303 return;
304 }
305 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800306
307 debug_cond(DEBUG_WGET,
308 "wget: Connected Pkt %p hlen %x\n",
309 pkt, hlen);
310
311 for (i = 0; i < pkt_q_idx; i++) {
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900312 int err;
313
Yasuharu Shibataafab2682024-08-14 21:41:06 +0900314 ptr1 = map_sysmem((ulong)pkt_q[i].pkt,
315 pkt_q[i].len);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900316 err = store_block(ptr1,
317 pkt_q[i].tcp_seq_num -
318 initial_data_seq_num,
319 pkt_q[i].len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800320 unmap_sysmem(ptr1);
321 debug_cond(DEBUG_WGET,
322 "wget: Connctd pkt Q %p len %x\n",
323 pkt_q[i].pkt, pkt_q[i].len);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900324 if (err) {
325 wget_loop_state = NETLOOP_FAIL;
326 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
327 net_set_state(NETLOOP_FAIL);
328 return;
329 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800330 }
331 }
332 }
333 wget_send(action, tcp_seq_num, tcp_ack_num, len);
334}
335
336/**
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100337 * wget_handler() - TCP handler of wget
338 * @pkt: pointer to the application packet
339 * @dport: destination TCP port
340 * @sip: source IP address
341 * @sport: source TCP port
342 * @tcp_seq_num: TCP sequential number
343 * @tcp_ack_num: TCP acknowledgment number
344 * @action: TCP action (SYN, ACK, FIN, etc)
345 * @len: packet length
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800346 *
347 * In the "application push" invocation, the TCP header with all
348 * its information is pointed to by the packet pointer.
349 */
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100350static void wget_handler(uchar *pkt, u16 dport,
351 struct in_addr sip, u16 sport,
352 u32 tcp_seq_num, u32 tcp_ack_num,
353 u8 action, unsigned int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800354{
355 enum tcp_state wget_tcp_state = tcp_get_tcp_state();
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800356
357 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
358 packets++;
359
360 switch (current_wget_state) {
361 case WGET_CLOSED:
362 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
363 break;
364 case WGET_CONNECTING:
365 debug_cond(DEBUG_WGET,
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100366 "wget: Connecting In len=%x, Seq=%u, Ack=%u\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800367 len, tcp_seq_num, tcp_ack_num);
368 if (!len) {
369 if (wget_tcp_state == TCP_ESTABLISHED) {
370 debug_cond(DEBUG_WGET,
371 "wget: Cting, send, len=%x\n", len);
372 wget_send(action, tcp_seq_num, tcp_ack_num,
373 len);
374 } else {
375 printf("%.*s", len, pkt);
376 wget_fail("wget: Handler Connected Fail\n",
377 tcp_seq_num, tcp_ack_num, action);
378 }
379 }
380 break;
381 case WGET_CONNECTED:
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100382 debug_cond(DEBUG_WGET, "wget: Connected seq=%u, len=%x\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800383 tcp_seq_num, len);
384 if (!len) {
385 wget_fail("Image not found, no data returned\n",
386 tcp_seq_num, tcp_ack_num, action);
387 } else {
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100388 wget_connected(pkt, tcp_seq_num, action, tcp_ack_num, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800389 }
390 break;
391 case WGET_TRANSFERRING:
392 debug_cond(DEBUG_WGET,
393 "wget: Transferring, seq=%x, ack=%x,len=%x\n",
394 tcp_seq_num, tcp_ack_num, len);
395
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +0900396 if (next_data_seq_num != tcp_seq_num) {
397 debug_cond(DEBUG_WGET, "wget: seq=%x packet was lost\n", next_data_seq_num);
398 return;
399 }
400 next_data_seq_num = tcp_seq_num + len;
401
Yasuharu Shibata2c9b5af2024-04-16 09:26:24 +0900402 if (store_block(pkt, tcp_seq_num - initial_data_seq_num, len) != 0) {
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800403 wget_fail("wget: store error\n",
404 tcp_seq_num, tcp_ack_num, action);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900405 net_set_state(NETLOOP_FAIL);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800406 return;
407 }
408
409 switch (wget_tcp_state) {
410 case TCP_FIN_WAIT_2:
411 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
412 fallthrough;
413 case TCP_SYN_SENT:
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100414 case TCP_SYN_RECEIVED:
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800415 case TCP_CLOSING:
416 case TCP_FIN_WAIT_1:
417 case TCP_CLOSED:
418 net_set_state(NETLOOP_FAIL);
419 break;
420 case TCP_ESTABLISHED:
421 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
422 len);
423 wget_loop_state = NETLOOP_SUCCESS;
424 break;
425 case TCP_CLOSE_WAIT: /* End of transfer */
426 current_wget_state = WGET_TRANSFERRED;
427 wget_send(action | TCP_ACK | TCP_FIN,
428 tcp_seq_num, tcp_ack_num, len);
429 break;
430 }
431 break;
432 case WGET_TRANSFERRED:
433 printf("Packets received %d, Transfer Successful\n", packets);
434 net_set_state(wget_loop_state);
435 break;
436 }
437}
438
439#define RANDOM_PORT_START 1024
440#define RANDOM_PORT_RANGE 0x4000
441
442/**
443 * random_port() - make port a little random (1024-17407)
444 *
445 * Return: random port number from 1024 to 17407
446 *
447 * This keeps the math somewhat trivial to compute, and seems to work with
448 * all supported protocols/clients/servers
449 */
450static unsigned int random_port(void)
451{
452 return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
453}
454
455#define BLOCKSIZE 512
456
457void wget_start(void)
458{
459 image_url = strchr(net_boot_file_name, ':');
460 if (image_url > 0) {
461 web_server_ip = string_to_ip(net_boot_file_name);
462 ++image_url;
463 net_server_ip = web_server_ip;
464 } else {
465 web_server_ip = net_server_ip;
466 image_url = net_boot_file_name;
467 }
468
469 debug_cond(DEBUG_WGET,
470 "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
471 &web_server_ip, &net_ip);
472
473 /* Check if we need to send across this subnet */
474 if (net_gateway.s_addr && net_netmask.s_addr) {
475 struct in_addr our_net;
476 struct in_addr server_net;
477
478 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
479 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
480 if (our_net.s_addr != server_net.s_addr)
481 debug_cond(DEBUG_WGET,
482 "wget: sending through gateway %pI4",
483 &net_gateway);
484 }
485 debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
486
487 if (net_boot_file_expected_size_in_blocks) {
488 debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ",
489 net_boot_file_expected_size_in_blocks * BLOCKSIZE);
490 print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE,
491 "");
492 }
493 debug_cond(DEBUG_WGET,
494 "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
495
Sughosh Ganu0d733f02024-08-26 17:29:22 +0530496 if (CONFIG_IS_ENABLED(LMB)) {
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900497 if (wget_init_load_size()) {
498 printf("\nwget error: ");
499 printf("trying to overwrite reserved memory...\n");
500 net_set_state(NETLOOP_FAIL);
501 return;
502 }
503 }
504
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800505 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
506 tcp_set_tcp_handler(wget_handler);
507
508 wget_timeout_count = 0;
509 current_wget_state = WGET_CLOSED;
510
511 our_port = random_port();
512
513 /*
514 * Zero out server ether to force arp resolution in case
515 * the server ip for the previous u-boot command, for example dns
516 * is not the same as the web server ip.
517 */
518
519 memset(net_server_ethaddr, 0, 6);
520
521 wget_send(TCP_SYN, 0, 0, 0);
522}
Masahisa Kojima6721d182023-11-10 13:25:35 +0900523
524#if (IS_ENABLED(CONFIG_CMD_DNS))
525int wget_with_dns(ulong dst_addr, char *uri)
526{
527 int ret;
528 char *s, *host_name, *file_name, *str_copy;
529
530 /*
531 * Download file using wget.
532 *
533 * U-Boot wget takes the target uri in this format.
534 * "<http server ip>:<file path>" e.g.) 192.168.1.1:/sample/test.iso
535 * Need to resolve the http server ip address before starting wget.
536 */
537 str_copy = strdup(uri);
538 if (!str_copy)
539 return -ENOMEM;
540
541 s = str_copy + strlen("http://");
542 host_name = strsep(&s, "/");
543 if (!s) {
544 log_err("Error: invalied uri, no file path\n");
545 ret = -EINVAL;
546 goto out;
547 }
548 file_name = s;
549
550 /* TODO: If the given uri has ip address for the http server, skip dns */
551 net_dns_resolve = host_name;
552 net_dns_env_var = "httpserverip";
553 if (net_loop(DNS) < 0) {
554 log_err("Error: dns lookup of %s failed, check setup\n", net_dns_resolve);
555 ret = -EINVAL;
556 goto out;
557 }
558 s = env_get("httpserverip");
559 if (!s) {
560 ret = -EINVAL;
561 goto out;
562 }
563
564 strlcpy(net_boot_file_name, s, sizeof(net_boot_file_name));
565 strlcat(net_boot_file_name, ":/", sizeof(net_boot_file_name)); /* append '/' which is removed by strsep() */
566 strlcat(net_boot_file_name, file_name, sizeof(net_boot_file_name));
567 image_load_addr = dst_addr;
568 ret = net_loop(WGET);
569
570out:
571 free(str_copy);
572
573 return ret;
574}
575#endif
Masahisa Kojimae501ce12023-11-10 13:25:41 +0900576
577/**
578 * wget_validate_uri() - validate the uri for wget
579 *
580 * @uri: uri string
581 *
582 * This function follows the current U-Boot wget implementation.
583 * scheme: only "http:" is supported
584 * authority:
585 * - user information: not supported
586 * - host: supported
587 * - port: not supported(always use the default port)
588 *
589 * Uri is expected to be correctly percent encoded.
590 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
591 * and space character(0x20) are not allowed.
592 *
593 * TODO: stricter uri conformance check
594 *
595 * Return: true on success, false on failure
596 */
597bool wget_validate_uri(char *uri)
598{
599 char c;
600 bool ret = true;
601 char *str_copy, *s, *authority;
602
603 for (c = 0x1; c < 0x21; c++) {
604 if (strchr(uri, c)) {
605 log_err("invalid character is used\n");
606 return false;
607 }
608 }
609 if (strchr(uri, 0x7f)) {
610 log_err("invalid character is used\n");
611 return false;
612 }
613
614 if (strncmp(uri, "http://", 7)) {
615 log_err("only http:// is supported\n");
616 return false;
617 }
618 str_copy = strdup(uri);
619 if (!str_copy)
620 return false;
621
622 s = str_copy + strlen("http://");
623 authority = strsep(&s, "/");
624 if (!s) {
625 log_err("invalid uri, no file path\n");
626 ret = false;
627 goto out;
628 }
629 s = strchr(authority, '@');
630 if (s) {
631 log_err("user information is not supported\n");
632 ret = false;
633 goto out;
634 }
635 s = strchr(authority, ':');
636 if (s) {
637 log_err("user defined port is not supported\n");
638 ret = false;
639 goto out;
640 }
641
642out:
643 free(str_copy);
644
645 return ret;
646}