1 // SPDX-License-Identifier: GPL-2.0
3 * WGET/HTTP support driver based on U-BOOT's nfs.c
7 #include <asm/global_data.h>
9 #include <display_options.h>
11 #include <efi_loader.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 /* The default, change with environment variable 'httpdstp' */
23 #define SERVER_PORT 80
25 static const char bootfile1[] = "GET ";
26 static const char bootfile3[] = " HTTP/1.0\r\n\r\n";
27 static const char http_eom[] = "\r\n\r\n";
28 static const char http_ok[] = "200";
29 static const char content_len[] = "Content-Length";
30 static const char linefeed[] = "\r\n";
31 static struct in_addr web_server_ip;
33 static int wget_timeout_count;
37 unsigned int tcp_seq_num;
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.
46 #define PKTQ_SZ (PKTBUFSRX / 4)
47 static struct pkt_qd pkt_q[PKTQ_SZ];
49 static unsigned long content_length;
50 static unsigned int packets;
52 static unsigned int initial_data_seq_num;
53 static unsigned int next_data_seq_num;
55 static enum wget_state current_wget_state;
57 static char *image_url;
58 static unsigned int wget_timeout = WGET_TIMEOUT;
60 static enum net_loop_state wget_loop_state;
62 /* Timeout retry parameters */
63 static u8 retry_action; /* actions for TCP retry */
64 static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/
65 static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */
66 static int retry_len; /* TCP retry length */
69 * store_block() - store block in memory
70 * @src: source of data
74 static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
76 ulong store_addr = image_load_addr + offset;
77 ulong newsize = offset + len;
80 if (CONFIG_IS_ENABLED(LMB)) {
81 if (store_addr < image_load_addr ||
82 lmb_read_check(store_addr, len)) {
83 printf("\nwget error: ");
84 printf("trying to overwrite reserved memory...\n");
89 ptr = map_sysmem(store_addr, len);
90 memcpy(ptr, src, len);
93 if (net_boot_file_size < (offset + len))
94 net_boot_file_size = newsize;
100 * wget_send_stored() - wget response dispatcher
102 * WARNING, This, and only this, is the place in wget.c where
103 * SEQUENCE NUMBERS are swapped between incoming (RX)
105 * Procedure wget_handler() is correct for RX traffic.
107 static void wget_send_stored(void)
109 u8 action = retry_action;
111 unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len);
112 unsigned int tcp_seq_num = retry_tcp_ack_num;
113 unsigned int server_port;
116 server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff;
118 switch (current_wget_state) {
120 debug_cond(DEBUG_WGET, "wget: send SYN\n");
121 current_wget_state = WGET_CONNECTING;
122 net_send_tcp_packet(0, server_port, our_port, action,
123 tcp_seq_num, tcp_ack_num);
126 case WGET_CONNECTING:
128 net_send_tcp_packet(0, server_port, our_port, action,
129 tcp_seq_num, tcp_ack_num);
131 ptr = net_tx_packet + net_eth_hdr_size() +
132 IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
135 memcpy(offset, &bootfile1, strlen(bootfile1));
136 offset += strlen(bootfile1);
138 memcpy(offset, image_url, strlen(image_url));
139 offset += strlen(image_url);
141 memcpy(offset, &bootfile3, strlen(bootfile3));
142 offset += strlen(bootfile3);
143 net_send_tcp_packet((offset - ptr), server_port, our_port,
144 TCP_PUSH, tcp_seq_num, tcp_ack_num);
145 current_wget_state = WGET_CONNECTED;
148 case WGET_TRANSFERRING:
149 case WGET_TRANSFERRED:
150 net_send_tcp_packet(0, server_port, our_port, action,
151 tcp_seq_num, tcp_ack_num);
156 static void wget_send(u8 action, unsigned int tcp_seq_num,
157 unsigned int tcp_ack_num, int len)
159 retry_action = action;
160 retry_tcp_ack_num = tcp_ack_num;
161 retry_tcp_seq_num = tcp_seq_num;
167 void wget_fail(char *error_message, unsigned int tcp_seq_num,
168 unsigned int tcp_ack_num, u8 action)
170 printf("wget: Transfer Fail - %s\n", error_message);
171 net_set_timeout_handler(0, NULL);
172 wget_send(action, tcp_seq_num, tcp_ack_num, 0);
176 * Interfaces of U-BOOT
178 static void wget_timeout_handler(void)
180 if (++wget_timeout_count > WGET_RETRY_COUNT) {
181 puts("\nRetry count exceeded; starting again\n");
182 wget_send(TCP_RST, 0, 0, 0);
186 net_set_timeout_handler(wget_timeout +
187 WGET_TIMEOUT * wget_timeout_count,
188 wget_timeout_handler);
193 #define PKT_QUEUE_OFFSET 0x20000
194 #define PKT_QUEUE_PACKET_SIZE 0x800
196 static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
197 u8 action, unsigned int tcp_ack_num, unsigned int len)
205 pos = strstr((char *)pkt, http_eom);
208 debug_cond(DEBUG_WGET,
209 "wget: Connected, data before Header %p\n", pkt);
210 pkt_in_q = (void *)image_load_addr + PKT_QUEUE_OFFSET +
211 (pkt_q_idx * PKT_QUEUE_PACKET_SIZE);
213 ptr1 = map_sysmem((ulong)pkt_in_q, len);
214 memcpy(ptr1, pkt, len);
217 pkt_q[pkt_q_idx].pkt = pkt_in_q;
218 pkt_q[pkt_q_idx].tcp_seq_num = tcp_seq_num;
219 pkt_q[pkt_q_idx].len = len;
222 if (pkt_q_idx >= PKTQ_SZ) {
223 printf("wget: Fatal error, queue overrun!\n");
224 net_set_state(NETLOOP_FAIL);
229 debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt);
230 /* sizeof(http_eom) - 1 is the string length of (http_eom) */
231 hlen = pos - (char *)pkt + sizeof(http_eom) - 1;
232 pos = strstr((char *)pkt, linefeed);
234 i = pos - (char *)pkt;
237 printf("%.*s", i, pkt);
239 current_wget_state = WGET_TRANSFERRING;
241 initial_data_seq_num = tcp_seq_num + hlen;
242 next_data_seq_num = tcp_seq_num + len;
244 if (strstr((char *)pkt, http_ok) == 0) {
245 debug_cond(DEBUG_WGET,
246 "wget: Connected Bad Xfer\n");
247 wget_loop_state = NETLOOP_FAIL;
248 wget_send(action, tcp_seq_num, tcp_ack_num, len);
250 debug_cond(DEBUG_WGET,
251 "wget: Connected Pkt %p hlen %x\n",
254 pos = strstr((char *)pkt, content_len);
258 pos += sizeof(content_len) + 2;
259 strict_strtoul(pos, 10, &content_length);
260 debug_cond(DEBUG_WGET,
261 "wget: Connected Len %lu\n",
265 net_boot_file_size = 0;
268 if (store_block(pkt + hlen, 0, len - hlen) != 0) {
269 wget_loop_state = NETLOOP_FAIL;
270 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
271 net_set_state(NETLOOP_FAIL);
276 for (i = 0; i < pkt_q_idx; i++) {
279 ptr1 = map_sysmem((ulong)pkt_q[i].pkt,
281 err = store_block(ptr1,
282 pkt_q[i].tcp_seq_num -
283 initial_data_seq_num,
286 debug_cond(DEBUG_WGET,
287 "wget: Conncted pkt Q %p len %x\n",
288 pkt_q[i].pkt, pkt_q[i].len);
290 wget_loop_state = NETLOOP_FAIL;
291 wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action);
292 net_set_state(NETLOOP_FAIL);
298 wget_send(action, tcp_seq_num, tcp_ack_num, len);
302 * wget_handler() - TCP handler of wget
303 * @pkt: pointer to the application packet
304 * @dport: destination TCP port
305 * @sip: source IP address
306 * @sport: source TCP port
307 * @tcp_seq_num: TCP sequential number
308 * @tcp_ack_num: TCP acknowledgment number
309 * @action: TCP action (SYN, ACK, FIN, etc)
310 * @len: packet length
312 * In the "application push" invocation, the TCP header with all
313 * its information is pointed to by the packet pointer.
315 static void wget_handler(uchar *pkt, u16 dport,
316 struct in_addr sip, u16 sport,
317 u32 tcp_seq_num, u32 tcp_ack_num,
318 u8 action, unsigned int len)
320 enum tcp_state wget_tcp_state = tcp_get_tcp_state();
322 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
325 switch (current_wget_state) {
327 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
329 case WGET_CONNECTING:
330 debug_cond(DEBUG_WGET,
331 "wget: Connecting In len=%x, Seq=%u, Ack=%u\n",
332 len, tcp_seq_num, tcp_ack_num);
334 if (wget_tcp_state == TCP_ESTABLISHED) {
335 debug_cond(DEBUG_WGET,
336 "wget: Cting, send, len=%x\n", len);
337 wget_send(action, tcp_seq_num, tcp_ack_num,
340 printf("%.*s", len, pkt);
341 wget_fail("wget: Handler Connected Fail\n",
342 tcp_seq_num, tcp_ack_num, action);
347 debug_cond(DEBUG_WGET, "wget: Connected seq=%u, len=%x\n",
350 wget_fail("Image not found, no data returned\n",
351 tcp_seq_num, tcp_ack_num, action);
353 wget_connected(pkt, tcp_seq_num, action, tcp_ack_num, len);
356 case WGET_TRANSFERRING:
357 debug_cond(DEBUG_WGET,
358 "wget: Transferring, seq=%x, ack=%x,len=%x\n",
359 tcp_seq_num, tcp_ack_num, len);
361 if (next_data_seq_num != tcp_seq_num) {
362 debug_cond(DEBUG_WGET, "wget: seq=%x packet was lost\n", next_data_seq_num);
365 next_data_seq_num = tcp_seq_num + len;
367 if (store_block(pkt, tcp_seq_num - initial_data_seq_num, len) != 0) {
368 wget_fail("wget: store error\n",
369 tcp_seq_num, tcp_ack_num, action);
370 net_set_state(NETLOOP_FAIL);
374 switch (wget_tcp_state) {
376 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
379 case TCP_SYN_RECEIVED:
383 net_set_state(NETLOOP_FAIL);
385 case TCP_ESTABLISHED:
386 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
388 wget_loop_state = NETLOOP_SUCCESS;
390 case TCP_CLOSE_WAIT: /* End of transfer */
391 current_wget_state = WGET_TRANSFERRED;
392 wget_send(action | TCP_ACK | TCP_FIN,
393 tcp_seq_num, tcp_ack_num, len);
397 case WGET_TRANSFERRED:
398 printf("Packets received %d, Transfer Successful\n", packets);
399 net_set_state(wget_loop_state);
400 efi_set_bootdev("Net", "", image_url,
401 map_sysmem(image_load_addr, 0),
403 env_set_hex("filesize", net_boot_file_size);
408 #define RANDOM_PORT_START 1024
409 #define RANDOM_PORT_RANGE 0x4000
412 * random_port() - make port a little random (1024-17407)
414 * Return: random port number from 1024 to 17407
416 * This keeps the math somewhat trivial to compute, and seems to work with
417 * all supported protocols/clients/servers
419 static unsigned int random_port(void)
421 return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
424 #define BLOCKSIZE 512
426 void wget_start(void)
428 image_url = strchr(net_boot_file_name, ':');
430 web_server_ip = string_to_ip(net_boot_file_name);
432 net_server_ip = web_server_ip;
434 web_server_ip = net_server_ip;
435 image_url = net_boot_file_name;
438 debug_cond(DEBUG_WGET,
439 "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
440 &web_server_ip, &net_ip);
442 /* Check if we need to send across this subnet */
443 if (net_gateway.s_addr && net_netmask.s_addr) {
444 struct in_addr our_net;
445 struct in_addr server_net;
447 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
448 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
449 if (our_net.s_addr != server_net.s_addr)
450 debug_cond(DEBUG_WGET,
451 "wget: sending through gateway %pI4",
454 debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
456 if (net_boot_file_expected_size_in_blocks) {
457 debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ",
458 net_boot_file_expected_size_in_blocks * BLOCKSIZE);
459 print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE,
462 debug_cond(DEBUG_WGET,
463 "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
465 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
466 tcp_set_tcp_handler(wget_handler);
468 wget_timeout_count = 0;
469 current_wget_state = WGET_CLOSED;
471 our_port = random_port();
474 * Zero out server ether to force arp resolution in case
475 * the server ip for the previous u-boot command, for example dns
476 * is not the same as the web server ip.
479 memset(net_server_ethaddr, 0, 6);
481 wget_send(TCP_SYN, 0, 0, 0);
484 #if (IS_ENABLED(CONFIG_CMD_DNS))
485 int wget_with_dns(ulong dst_addr, char *uri)
488 char *s, *host_name, *file_name, *str_copy;
491 * Download file using wget.
493 * U-Boot wget takes the target uri in this format.
494 * "<http server ip>:<file path>" e.g.) 192.168.1.1:/sample/test.iso
495 * Need to resolve the http server ip address before starting wget.
497 str_copy = strdup(uri);
501 s = str_copy + strlen("http://");
502 host_name = strsep(&s, "/");
504 log_err("Error: invalied uri, no file path\n");
510 /* TODO: If the given uri has ip address for the http server, skip dns */
511 net_dns_resolve = host_name;
512 net_dns_env_var = "httpserverip";
513 if (net_loop(DNS) < 0) {
514 log_err("Error: dns lookup of %s failed, check setup\n", net_dns_resolve);
518 s = env_get("httpserverip");
524 strlcpy(net_boot_file_name, s, sizeof(net_boot_file_name));
525 strlcat(net_boot_file_name, ":/", sizeof(net_boot_file_name)); /* append '/' which is removed by strsep() */
526 strlcat(net_boot_file_name, file_name, sizeof(net_boot_file_name));
527 image_load_addr = dst_addr;
528 ret = net_loop(WGET);
538 * wget_validate_uri() - validate the uri for wget
542 * This function follows the current U-Boot wget implementation.
543 * scheme: only "http:" is supported
545 * - user information: not supported
547 * - port: not supported(always use the default port)
549 * Uri is expected to be correctly percent encoded.
550 * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0')
551 * and space character(0x20) are not allowed.
553 * TODO: stricter uri conformance check
555 * Return: true on success, false on failure
557 bool wget_validate_uri(char *uri)
561 char *str_copy, *s, *authority;
563 for (c = 0x1; c < 0x21; c++) {
564 if (strchr(uri, c)) {
565 log_err("invalid character is used\n");
569 if (strchr(uri, 0x7f)) {
570 log_err("invalid character is used\n");
574 if (strncmp(uri, "http://", 7)) {
575 log_err("only http:// is supported\n");
578 str_copy = strdup(uri);
582 s = str_copy + strlen("http://");
583 authority = strsep(&s, "/");
585 log_err("invalid uri, no file path\n");
589 s = strchr(authority, '@');
591 log_err("user information is not supported\n");
595 s = strchr(authority, ':');
597 log_err("user defined port is not supported\n");