1 // SPDX-License-Identifier: GPL-2.0
3 * WGET/HTTP support driver based on U-BOOT's nfs.c
9 #include <display_options.h>
17 static const char bootfile1[] = "GET ";
18 static const char bootfile3[] = " HTTP/1.0\r\n\r\n";
19 static const char http_eom[] = "\r\n\r\n";
20 static const char http_ok[] = "200";
21 static const char content_len[] = "Content-Length";
22 static const char linefeed[] = "\r\n";
23 static struct in_addr web_server_ip;
25 static int wget_timeout_count;
29 unsigned int tcp_seq_num;
34 * This is a control structure for out of order packets received.
35 * The actual packet bufers are in the kernel space, and are
36 * expected to be overwritten by the downloaded image.
38 #define PKTQ_SZ (PKTBUFSRX / 4)
39 static struct pkt_qd pkt_q[PKTQ_SZ];
41 static unsigned long content_length;
42 static unsigned int packets;
44 static unsigned int initial_data_seq_num;
46 static enum wget_state current_wget_state;
48 static char *image_url;
49 static unsigned int wget_timeout = WGET_TIMEOUT;
51 static enum net_loop_state wget_loop_state;
53 /* Timeout retry parameters */
54 static u8 retry_action; /* actions for TCP retry */
55 static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/
56 static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */
57 static int retry_len; /* TCP retry length */
60 * store_block() - store block in memory
61 * @src: source of data
65 static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
67 ulong newsize = offset + len;
70 ptr = map_sysmem(image_load_addr + offset, len);
71 memcpy(ptr, src, len);
74 if (net_boot_file_size < (offset + len))
75 net_boot_file_size = newsize;
81 * wget_send_stored() - wget response dispatcher
83 * WARNING, This, and only this, is the place in wget.c where
84 * SEQUENCE NUMBERS are swapped between incoming (RX)
86 * Procedure wget_handler() is correct for RX traffic.
88 static void wget_send_stored(void)
90 u8 action = retry_action;
92 unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len);
93 unsigned int tcp_seq_num = retry_tcp_ack_num;
96 switch (current_wget_state) {
98 debug_cond(DEBUG_WGET, "wget: send SYN\n");
99 current_wget_state = WGET_CONNECTING;
100 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
101 tcp_seq_num, tcp_ack_num);
104 case WGET_CONNECTING:
106 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
107 tcp_seq_num, tcp_ack_num);
109 ptr = net_tx_packet + net_eth_hdr_size() +
110 IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
113 memcpy(offset, &bootfile1, strlen(bootfile1));
114 offset += strlen(bootfile1);
116 memcpy(offset, image_url, strlen(image_url));
117 offset += strlen(image_url);
119 memcpy(offset, &bootfile3, strlen(bootfile3));
120 offset += strlen(bootfile3);
121 net_send_tcp_packet((offset - ptr), SERVER_PORT, our_port,
122 TCP_PUSH, tcp_seq_num, tcp_ack_num);
123 current_wget_state = WGET_CONNECTED;
126 case WGET_TRANSFERRING:
127 case WGET_TRANSFERRED:
128 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
129 tcp_seq_num, tcp_ack_num);
134 static void wget_send(u8 action, unsigned int tcp_seq_num,
135 unsigned int tcp_ack_num, int len)
137 retry_action = action;
138 retry_tcp_ack_num = tcp_ack_num;
139 retry_tcp_seq_num = tcp_seq_num;
145 void wget_fail(char *error_message, unsigned int tcp_seq_num,
146 unsigned int tcp_ack_num, u8 action)
148 printf("wget: Transfer Fail - %s\n", error_message);
149 net_set_timeout_handler(0, NULL);
150 wget_send(action, tcp_seq_num, tcp_ack_num, 0);
153 void wget_success(u8 action, unsigned int tcp_seq_num,
154 unsigned int tcp_ack_num, int len, int packets)
156 printf("Packets received %d, Transfer Successful\n", packets);
157 wget_send(action, tcp_seq_num, tcp_ack_num, len);
161 * Interfaces of U-BOOT
163 static void wget_timeout_handler(void)
165 if (++wget_timeout_count > WGET_RETRY_COUNT) {
166 puts("\nRetry count exceeded; starting again\n");
167 wget_send(TCP_RST, 0, 0, 0);
171 net_set_timeout_handler(wget_timeout +
172 WGET_TIMEOUT * wget_timeout_count,
173 wget_timeout_handler);
178 #define PKT_QUEUE_OFFSET 0x20000
179 #define PKT_QUEUE_PACKET_SIZE 0x800
181 static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
182 u8 action, unsigned int tcp_ack_num, unsigned int len)
190 pos = strstr((char *)pkt, http_eom);
193 debug_cond(DEBUG_WGET,
194 "wget: Connected, data before Header %p\n", pkt);
195 pkt_in_q = (void *)image_load_addr + PKT_QUEUE_OFFSET +
196 (pkt_q_idx * PKT_QUEUE_PACKET_SIZE);
198 ptr1 = map_sysmem((phys_addr_t)pkt_in_q, len);
199 memcpy(ptr1, pkt, len);
202 pkt_q[pkt_q_idx].pkt = pkt_in_q;
203 pkt_q[pkt_q_idx].tcp_seq_num = tcp_seq_num;
204 pkt_q[pkt_q_idx].len = len;
207 if (pkt_q_idx >= PKTQ_SZ) {
208 printf("wget: Fatal error, queue overrun!\n");
209 net_set_state(NETLOOP_FAIL);
214 debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt);
215 /* sizeof(http_eom) - 1 is the string length of (http_eom) */
216 hlen = pos - (char *)pkt + sizeof(http_eom) - 1;
217 pos = strstr((char *)pkt, linefeed);
219 i = pos - (char *)pkt;
222 printf("%.*s", i, pkt);
224 current_wget_state = WGET_TRANSFERRING;
226 if (strstr((char *)pkt, http_ok) == 0) {
227 debug_cond(DEBUG_WGET,
228 "wget: Connected Bad Xfer\n");
229 initial_data_seq_num = tcp_seq_num + hlen;
230 wget_loop_state = NETLOOP_FAIL;
231 wget_send(action, tcp_seq_num, tcp_ack_num, len);
233 debug_cond(DEBUG_WGET,
234 "wget: Connctd pkt %p hlen %x\n",
236 initial_data_seq_num = tcp_seq_num + hlen;
238 pos = strstr((char *)pkt, content_len);
242 pos += sizeof(content_len) + 2;
243 strict_strtoul(pos, 10, &content_length);
244 debug_cond(DEBUG_WGET,
245 "wget: Connected Len %lu\n",
249 net_boot_file_size = 0;
252 store_block(pkt + hlen, 0, len - hlen);
254 debug_cond(DEBUG_WGET,
255 "wget: Connected Pkt %p hlen %x\n",
258 for (i = 0; i < pkt_q_idx; i++) {
260 (phys_addr_t)(pkt_q[i].pkt),
263 pkt_q[i].tcp_seq_num -
264 initial_data_seq_num,
267 debug_cond(DEBUG_WGET,
268 "wget: Connctd pkt Q %p len %x\n",
269 pkt_q[i].pkt, pkt_q[i].len);
273 wget_send(action, tcp_seq_num, tcp_ack_num, len);
277 * wget_handler() - TCP handler of wget
278 * @pkt: pointer to the application packet
279 * @dport: destination TCP port
280 * @sip: source IP address
281 * @sport: source TCP port
282 * @tcp_seq_num: TCP sequential number
283 * @tcp_ack_num: TCP acknowledgment number
284 * @action: TCP action (SYN, ACK, FIN, etc)
285 * @len: packet length
287 * In the "application push" invocation, the TCP header with all
288 * its information is pointed to by the packet pointer.
290 static void wget_handler(uchar *pkt, u16 dport,
291 struct in_addr sip, u16 sport,
292 u32 tcp_seq_num, u32 tcp_ack_num,
293 u8 action, unsigned int len)
295 enum tcp_state wget_tcp_state = tcp_get_tcp_state();
297 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
300 switch (current_wget_state) {
302 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
304 case WGET_CONNECTING:
305 debug_cond(DEBUG_WGET,
306 "wget: Connecting In len=%x, Seq=%u, Ack=%u\n",
307 len, tcp_seq_num, tcp_ack_num);
309 if (wget_tcp_state == TCP_ESTABLISHED) {
310 debug_cond(DEBUG_WGET,
311 "wget: Cting, send, len=%x\n", len);
312 wget_send(action, tcp_seq_num, tcp_ack_num,
315 printf("%.*s", len, pkt);
316 wget_fail("wget: Handler Connected Fail\n",
317 tcp_seq_num, tcp_ack_num, action);
322 debug_cond(DEBUG_WGET, "wget: Connected seq=%u, len=%x\n",
325 wget_fail("Image not found, no data returned\n",
326 tcp_seq_num, tcp_ack_num, action);
328 wget_connected(pkt, tcp_seq_num, action, tcp_ack_num, len);
331 case WGET_TRANSFERRING:
332 debug_cond(DEBUG_WGET,
333 "wget: Transferring, seq=%x, ack=%x,len=%x\n",
334 tcp_seq_num, tcp_ack_num, len);
336 if (tcp_seq_num >= initial_data_seq_num &&
337 store_block(pkt, tcp_seq_num - initial_data_seq_num,
339 wget_fail("wget: store error\n",
340 tcp_seq_num, tcp_ack_num, action);
344 switch (wget_tcp_state) {
346 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
349 case TCP_SYN_RECEIVED:
353 net_set_state(NETLOOP_FAIL);
355 case TCP_ESTABLISHED:
356 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
358 wget_loop_state = NETLOOP_SUCCESS;
360 case TCP_CLOSE_WAIT: /* End of transfer */
361 current_wget_state = WGET_TRANSFERRED;
362 wget_send(action | TCP_ACK | TCP_FIN,
363 tcp_seq_num, tcp_ack_num, len);
367 case WGET_TRANSFERRED:
368 printf("Packets received %d, Transfer Successful\n", packets);
369 net_set_state(wget_loop_state);
374 #define RANDOM_PORT_START 1024
375 #define RANDOM_PORT_RANGE 0x4000
378 * random_port() - make port a little random (1024-17407)
380 * Return: random port number from 1024 to 17407
382 * This keeps the math somewhat trivial to compute, and seems to work with
383 * all supported protocols/clients/servers
385 static unsigned int random_port(void)
387 return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
390 #define BLOCKSIZE 512
392 void wget_start(void)
394 image_url = strchr(net_boot_file_name, ':');
396 web_server_ip = string_to_ip(net_boot_file_name);
398 net_server_ip = web_server_ip;
400 web_server_ip = net_server_ip;
401 image_url = net_boot_file_name;
404 debug_cond(DEBUG_WGET,
405 "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
406 &web_server_ip, &net_ip);
408 /* Check if we need to send across this subnet */
409 if (net_gateway.s_addr && net_netmask.s_addr) {
410 struct in_addr our_net;
411 struct in_addr server_net;
413 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
414 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
415 if (our_net.s_addr != server_net.s_addr)
416 debug_cond(DEBUG_WGET,
417 "wget: sending through gateway %pI4",
420 debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
422 if (net_boot_file_expected_size_in_blocks) {
423 debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ",
424 net_boot_file_expected_size_in_blocks * BLOCKSIZE);
425 print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE,
428 debug_cond(DEBUG_WGET,
429 "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
431 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
432 tcp_set_tcp_handler(wget_handler);
434 wget_timeout_count = 0;
435 current_wget_state = WGET_CLOSED;
437 our_port = random_port();
440 * Zero out server ether to force arp resolution in case
441 * the server ip for the previous u-boot command, for example dns
442 * is not the same as the web server ip.
445 memset(net_server_ethaddr, 0, 6);
447 wget_send(TCP_SYN, 0, 0, 0);