blob: 945bfd266939e713940bb86f0c2ece3e0f964edb [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{
76 struct lmb lmb;
77 phys_size_t max_size;
78
79 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
80
81 max_size = lmb_get_free_size(&lmb, image_load_addr);
82 if (!max_size)
83 return -1;
84
85 wget_load_size = max_size;
86
87 return 0;
88}
89
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080090/**
91 * store_block() - store block in memory
92 * @src: source of data
93 * @offset: offset
94 * @len: length
95 */
96static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
97{
Masahisa Kojima77b0ae32023-11-10 13:25:34 +090098 ulong store_addr = image_load_addr + offset;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +080099 ulong newsize = offset + len;
100 uchar *ptr;
101
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900102 if (IS_ENABLED(CONFIG_LMB)) {
103 ulong end_addr = image_load_addr + wget_load_size;
104
105 if (!end_addr)
106 end_addr = ULONG_MAX;
107
108 if (store_addr < image_load_addr ||
109 store_addr + len > end_addr) {
110 printf("\nwget error: ");
111 printf("trying to overwrite reserved memory...\n");
112 return -1;
113 }
114 }
115
116 ptr = map_sysmem(store_addr, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800117 memcpy(ptr, src, len);
118 unmap_sysmem(ptr);
119
120 if (net_boot_file_size < (offset + len))
121 net_boot_file_size = newsize;
122
123 return 0;
124}
125
126/**
127 * wget_send_stored() - wget response dispatcher
128 *
129 * WARNING, This, and only this, is the place in wget.c where
130 * SEQUENCE NUMBERS are swapped between incoming (RX)
131 * and outgoing (TX).
132 * Procedure wget_handler() is correct for RX traffic.
133 */
134static void wget_send_stored(void)
135{
136 u8 action = retry_action;
137 int len = retry_len;
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100138 unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len);
139 unsigned int tcp_seq_num = retry_tcp_ack_num;
Marek Vasut22a95082023-12-13 22:11:13 +0100140 unsigned int server_port;
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800141 uchar *ptr, *offset;
142
Marek Vasut22a95082023-12-13 22:11:13 +0100143 server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff;
144
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800145 switch (current_wget_state) {
146 case WGET_CLOSED:
147 debug_cond(DEBUG_WGET, "wget: send SYN\n");
148 current_wget_state = WGET_CONNECTING;
Marek Vasut22a95082023-12-13 22:11:13 +0100149 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800150 tcp_seq_num, tcp_ack_num);
151 packets = 0;
152 break;
153 case WGET_CONNECTING:
154 pkt_q_idx = 0;
Marek Vasut22a95082023-12-13 22:11:13 +0100155 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800156 tcp_seq_num, tcp_ack_num);
157
158 ptr = net_tx_packet + net_eth_hdr_size() +
159 IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
160 offset = ptr;
161
162 memcpy(offset, &bootfile1, strlen(bootfile1));
163 offset += strlen(bootfile1);
164
165 memcpy(offset, image_url, strlen(image_url));
166 offset += strlen(image_url);
167
168 memcpy(offset, &bootfile3, strlen(bootfile3));
169 offset += strlen(bootfile3);
Marek Vasut22a95082023-12-13 22:11:13 +0100170 net_send_tcp_packet((offset - ptr), server_port, our_port,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800171 TCP_PUSH, tcp_seq_num, tcp_ack_num);
172 current_wget_state = WGET_CONNECTED;
173 break;
174 case WGET_CONNECTED:
175 case WGET_TRANSFERRING:
176 case WGET_TRANSFERRED:
Marek Vasut22a95082023-12-13 22:11:13 +0100177 net_send_tcp_packet(0, server_port, our_port, action,
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800178 tcp_seq_num, tcp_ack_num);
179 break;
180 }
181}
182
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100183static void wget_send(u8 action, unsigned int tcp_seq_num,
184 unsigned int tcp_ack_num, int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800185{
186 retry_action = action;
187 retry_tcp_ack_num = tcp_ack_num;
188 retry_tcp_seq_num = tcp_seq_num;
189 retry_len = len;
190
191 wget_send_stored();
192}
193
194void wget_fail(char *error_message, unsigned int tcp_seq_num,
195 unsigned int tcp_ack_num, u8 action)
196{
197 printf("wget: Transfer Fail - %s\n", error_message);
198 net_set_timeout_handler(0, NULL);
199 wget_send(action, tcp_seq_num, tcp_ack_num, 0);
200}
201
202void wget_success(u8 action, unsigned int tcp_seq_num,
203 unsigned int tcp_ack_num, int len, int packets)
204{
205 printf("Packets received %d, Transfer Successful\n", packets);
206 wget_send(action, tcp_seq_num, tcp_ack_num, len);
207}
208
209/*
210 * Interfaces of U-BOOT
211 */
212static void wget_timeout_handler(void)
213{
214 if (++wget_timeout_count > WGET_RETRY_COUNT) {
215 puts("\nRetry count exceeded; starting again\n");
216 wget_send(TCP_RST, 0, 0, 0);
217 net_start_again();
218 } else {
219 puts("T ");
220 net_set_timeout_handler(wget_timeout +
221 WGET_TIMEOUT * wget_timeout_count,
222 wget_timeout_handler);
223 wget_send_stored();
224 }
225}
226
227#define PKT_QUEUE_OFFSET 0x20000
228#define PKT_QUEUE_PACKET_SIZE 0x800
229
230static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100231 u8 action, unsigned int tcp_ack_num, unsigned int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800232{
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800233 uchar *pkt_in_q;
234 char *pos;
235 int hlen, i;
236 uchar *ptr1;
237
238 pkt[len] = '\0';
239 pos = strstr((char *)pkt, http_eom);
240
241 if (!pos) {
242 debug_cond(DEBUG_WGET,
243 "wget: Connected, data before Header %p\n", pkt);
244 pkt_in_q = (void *)image_load_addr + PKT_QUEUE_OFFSET +
245 (pkt_q_idx * PKT_QUEUE_PACKET_SIZE);
246
Yasuharu Shibataafab2682024-08-14 21:41:06 +0900247 ptr1 = map_sysmem((ulong)pkt_in_q, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800248 memcpy(ptr1, pkt, len);
249 unmap_sysmem(ptr1);
250
251 pkt_q[pkt_q_idx].pkt = pkt_in_q;
252 pkt_q[pkt_q_idx].tcp_seq_num = tcp_seq_num;
253 pkt_q[pkt_q_idx].len = len;
254 pkt_q_idx++;
Richard Weinbergerbf44cf42023-07-20 14:51:56 +0200255
256 if (pkt_q_idx >= PKTQ_SZ) {
257 printf("wget: Fatal error, queue overrun!\n");
258 net_set_state(NETLOOP_FAIL);
259
260 return;
261 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800262 } else {
263 debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt);
264 /* sizeof(http_eom) - 1 is the string length of (http_eom) */
265 hlen = pos - (char *)pkt + sizeof(http_eom) - 1;
266 pos = strstr((char *)pkt, linefeed);
267 if (pos > 0)
268 i = pos - (char *)pkt;
269 else
270 i = hlen;
271 printf("%.*s", i, pkt);
272
273 current_wget_state = WGET_TRANSFERRING;
274
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +0900275 initial_data_seq_num = tcp_seq_num + hlen;
276 next_data_seq_num = tcp_seq_num + len;
277
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800278 if (strstr((char *)pkt, http_ok) == 0) {
279 debug_cond(DEBUG_WGET,
280 "wget: Connected Bad Xfer\n");
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800281 wget_loop_state = NETLOOP_FAIL;
282 wget_send(action, tcp_seq_num, tcp_ack_num, len);
283 } else {
284 debug_cond(DEBUG_WGET,
285 "wget: Connctd pkt %p hlen %x\n",
286 pkt, hlen);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800287
288 pos = strstr((char *)pkt, content_len);
289 if (!pos) {
290 content_length = -1;
291 } else {
292 pos += sizeof(content_len) + 2;
293 strict_strtoul(pos, 10, &content_length);
294 debug_cond(DEBUG_WGET,
295 "wget: Connected Len %lu\n",
296 content_length);
297 }
298
299 net_boot_file_size = 0;
300
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900301 if (len > hlen) {
302 if (store_block(pkt + hlen, 0, len - hlen) != 0) {
303 wget_loop_state = NETLOOP_FAIL;
304 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
305 net_set_state(NETLOOP_FAIL);
306 return;
307 }
308 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800309
310 debug_cond(DEBUG_WGET,
311 "wget: Connected Pkt %p hlen %x\n",
312 pkt, hlen);
313
314 for (i = 0; i < pkt_q_idx; i++) {
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900315 int err;
316
Yasuharu Shibataafab2682024-08-14 21:41:06 +0900317 ptr1 = map_sysmem((ulong)pkt_q[i].pkt,
318 pkt_q[i].len);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900319 err = store_block(ptr1,
320 pkt_q[i].tcp_seq_num -
321 initial_data_seq_num,
322 pkt_q[i].len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800323 unmap_sysmem(ptr1);
324 debug_cond(DEBUG_WGET,
325 "wget: Connctd pkt Q %p len %x\n",
326 pkt_q[i].pkt, pkt_q[i].len);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900327 if (err) {
328 wget_loop_state = NETLOOP_FAIL;
329 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
330 net_set_state(NETLOOP_FAIL);
331 return;
332 }
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800333 }
334 }
335 }
336 wget_send(action, tcp_seq_num, tcp_ack_num, len);
337}
338
339/**
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100340 * wget_handler() - TCP handler of wget
341 * @pkt: pointer to the application packet
342 * @dport: destination TCP port
343 * @sip: source IP address
344 * @sport: source TCP port
345 * @tcp_seq_num: TCP sequential number
346 * @tcp_ack_num: TCP acknowledgment number
347 * @action: TCP action (SYN, ACK, FIN, etc)
348 * @len: packet length
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800349 *
350 * In the "application push" invocation, the TCP header with all
351 * its information is pointed to by the packet pointer.
352 */
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100353static void wget_handler(uchar *pkt, u16 dport,
354 struct in_addr sip, u16 sport,
355 u32 tcp_seq_num, u32 tcp_ack_num,
356 u8 action, unsigned int len)
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800357{
358 enum tcp_state wget_tcp_state = tcp_get_tcp_state();
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800359
360 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
361 packets++;
362
363 switch (current_wget_state) {
364 case WGET_CLOSED:
365 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
366 break;
367 case WGET_CONNECTING:
368 debug_cond(DEBUG_WGET,
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100369 "wget: Connecting In len=%x, Seq=%u, Ack=%u\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800370 len, tcp_seq_num, tcp_ack_num);
371 if (!len) {
372 if (wget_tcp_state == TCP_ESTABLISHED) {
373 debug_cond(DEBUG_WGET,
374 "wget: Cting, send, len=%x\n", len);
375 wget_send(action, tcp_seq_num, tcp_ack_num,
376 len);
377 } else {
378 printf("%.*s", len, pkt);
379 wget_fail("wget: Handler Connected Fail\n",
380 tcp_seq_num, tcp_ack_num, action);
381 }
382 }
383 break;
384 case WGET_CONNECTED:
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100385 debug_cond(DEBUG_WGET, "wget: Connected seq=%u, len=%x\n",
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800386 tcp_seq_num, len);
387 if (!len) {
388 wget_fail("Image not found, no data returned\n",
389 tcp_seq_num, tcp_ack_num, action);
390 } else {
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100391 wget_connected(pkt, tcp_seq_num, action, tcp_ack_num, len);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800392 }
393 break;
394 case WGET_TRANSFERRING:
395 debug_cond(DEBUG_WGET,
396 "wget: Transferring, seq=%x, ack=%x,len=%x\n",
397 tcp_seq_num, tcp_ack_num, len);
398
Yasuharu Shibata07a00ef2024-04-14 19:46:07 +0900399 if (next_data_seq_num != tcp_seq_num) {
400 debug_cond(DEBUG_WGET, "wget: seq=%x packet was lost\n", next_data_seq_num);
401 return;
402 }
403 next_data_seq_num = tcp_seq_num + len;
404
Yasuharu Shibata2c9b5af2024-04-16 09:26:24 +0900405 if (store_block(pkt, tcp_seq_num - initial_data_seq_num, len) != 0) {
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800406 wget_fail("wget: store error\n",
407 tcp_seq_num, tcp_ack_num, action);
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900408 net_set_state(NETLOOP_FAIL);
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800409 return;
410 }
411
412 switch (wget_tcp_state) {
413 case TCP_FIN_WAIT_2:
414 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
415 fallthrough;
416 case TCP_SYN_SENT:
Dmitrii Merkurev3acd0542023-04-12 19:49:29 +0100417 case TCP_SYN_RECEIVED:
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800418 case TCP_CLOSING:
419 case TCP_FIN_WAIT_1:
420 case TCP_CLOSED:
421 net_set_state(NETLOOP_FAIL);
422 break;
423 case TCP_ESTABLISHED:
424 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
425 len);
426 wget_loop_state = NETLOOP_SUCCESS;
427 break;
428 case TCP_CLOSE_WAIT: /* End of transfer */
429 current_wget_state = WGET_TRANSFERRED;
430 wget_send(action | TCP_ACK | TCP_FIN,
431 tcp_seq_num, tcp_ack_num, len);
432 break;
433 }
434 break;
435 case WGET_TRANSFERRED:
436 printf("Packets received %d, Transfer Successful\n", packets);
437 net_set_state(wget_loop_state);
438 break;
439 }
440}
441
442#define RANDOM_PORT_START 1024
443#define RANDOM_PORT_RANGE 0x4000
444
445/**
446 * random_port() - make port a little random (1024-17407)
447 *
448 * Return: random port number from 1024 to 17407
449 *
450 * This keeps the math somewhat trivial to compute, and seems to work with
451 * all supported protocols/clients/servers
452 */
453static unsigned int random_port(void)
454{
455 return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
456}
457
458#define BLOCKSIZE 512
459
460void wget_start(void)
461{
462 image_url = strchr(net_boot_file_name, ':');
463 if (image_url > 0) {
464 web_server_ip = string_to_ip(net_boot_file_name);
465 ++image_url;
466 net_server_ip = web_server_ip;
467 } else {
468 web_server_ip = net_server_ip;
469 image_url = net_boot_file_name;
470 }
471
472 debug_cond(DEBUG_WGET,
473 "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
474 &web_server_ip, &net_ip);
475
476 /* Check if we need to send across this subnet */
477 if (net_gateway.s_addr && net_netmask.s_addr) {
478 struct in_addr our_net;
479 struct in_addr server_net;
480
481 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
482 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
483 if (our_net.s_addr != server_net.s_addr)
484 debug_cond(DEBUG_WGET,
485 "wget: sending through gateway %pI4",
486 &net_gateway);
487 }
488 debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
489
490 if (net_boot_file_expected_size_in_blocks) {
491 debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ",
492 net_boot_file_expected_size_in_blocks * BLOCKSIZE);
493 print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE,
494 "");
495 }
496 debug_cond(DEBUG_WGET,
497 "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
498
Masahisa Kojima77b0ae32023-11-10 13:25:34 +0900499 if (IS_ENABLED(CONFIG_LMB)) {
500 if (wget_init_load_size()) {
501 printf("\nwget error: ");
502 printf("trying to overwrite reserved memory...\n");
503 net_set_state(NETLOOP_FAIL);
504 return;
505 }
506 }
507
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800508 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
509 tcp_set_tcp_handler(wget_handler);
510
511 wget_timeout_count = 0;
512 current_wget_state = WGET_CLOSED;
513
514 our_port = random_port();
515
516 /*
517 * Zero out server ether to force arp resolution in case
518 * the server ip for the previous u-boot command, for example dns
519 * is not the same as the web server ip.
520 */
521
522 memset(net_server_ethaddr, 0, 6);
523
524 wget_send(TCP_SYN, 0, 0, 0);
525}
Masahisa Kojima6721d182023-11-10 13:25:35 +0900526
527#if (IS_ENABLED(CONFIG_CMD_DNS))
528int wget_with_dns(ulong dst_addr, char *uri)
529{
530 int ret;
531 char *s, *host_name, *file_name, *str_copy;
532
533 /*
534 * Download file using wget.
535 *
536 * U-Boot wget takes the target uri in this format.
537 * "<http server ip>:<file path>" e.g.) 192.168.1.1:/sample/test.iso
538 * Need to resolve the http server ip address before starting wget.
539 */
540 str_copy = strdup(uri);
541 if (!str_copy)
542 return -ENOMEM;
543
544 s = str_copy + strlen("http://");
545 host_name = strsep(&s, "/");
546 if (!s) {
547 log_err("Error: invalied uri, no file path\n");
548 ret = -EINVAL;
549 goto out;
550 }
551 file_name = s;
552
553 /* TODO: If the given uri has ip address for the http server, skip dns */
554 net_dns_resolve = host_name;
555 net_dns_env_var = "httpserverip";
556 if (net_loop(DNS) < 0) {
557 log_err("Error: dns lookup of %s failed, check setup\n", net_dns_resolve);
558 ret = -EINVAL;
559 goto out;
560 }
561 s = env_get("httpserverip");
562 if (!s) {
563 ret = -EINVAL;
564 goto out;
565 }
566
567 strlcpy(net_boot_file_name, s, sizeof(net_boot_file_name));
568 strlcat(net_boot_file_name, ":/", sizeof(net_boot_file_name)); /* append '/' which is removed by strsep() */
569 strlcat(net_boot_file_name, file_name, sizeof(net_boot_file_name));
570 image_load_addr = dst_addr;
571 ret = net_loop(WGET);
572
573out:
574 free(str_copy);
575
576 return ret;
577}
578#endif
Masahisa Kojimae501ce12023-11-10 13:25:41 +0900579
580/**
581 * wget_validate_uri() - validate the uri for wget
582 *
583 * @uri: uri string
584 *
585 * This function follows the current U-Boot wget implementation.
586 * scheme: only "http:" is supported
587 * authority:
588 * - user information: not supported
589 * - host: supported
590 * - port: not supported(always use the default port)
591 *
592 * Uri is expected to be correctly percent encoded.
593 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
594 * and space character(0x20) are not allowed.
595 *
596 * TODO: stricter uri conformance check
597 *
598 * Return: true on success, false on failure
599 */
600bool wget_validate_uri(char *uri)
601{
602 char c;
603 bool ret = true;
604 char *str_copy, *s, *authority;
605
606 for (c = 0x1; c < 0x21; c++) {
607 if (strchr(uri, c)) {
608 log_err("invalid character is used\n");
609 return false;
610 }
611 }
612 if (strchr(uri, 0x7f)) {
613 log_err("invalid character is used\n");
614 return false;
615 }
616
617 if (strncmp(uri, "http://", 7)) {
618 log_err("only http:// is supported\n");
619 return false;
620 }
621 str_copy = strdup(uri);
622 if (!str_copy)
623 return false;
624
625 s = str_copy + strlen("http://");
626 authority = strsep(&s, "/");
627 if (!s) {
628 log_err("invalid uri, no file path\n");
629 ret = false;
630 goto out;
631 }
632 s = strchr(authority, '@');
633 if (s) {
634 log_err("user information is not supported\n");
635 ret = false;
636 goto out;
637 }
638 s = strchr(authority, ':');
639 if (s) {
640 log_err("user defined port is not supported\n");
641 ret = false;
642 goto out;
643 }
644
645out:
646 free(str_copy);
647
648 return ret;
649}