1 // SPDX-License-Identifier: GPL-2.0
3 * WGET/HTTP support driver based on U-BOOT's nfs.c
4 * Copyright Duncan Hare <dh@synoia.com> 2017
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 static struct pkt_qd pkt_q[PKTBUFSRX / 4];
40 static unsigned long content_length;
41 static unsigned int packets;
43 static unsigned int initial_data_seq_num;
45 static enum wget_state current_wget_state;
47 static char *image_url;
48 static unsigned int wget_timeout = WGET_TIMEOUT;
50 static enum net_loop_state wget_loop_state;
52 /* Timeout retry parameters */
53 static u8 retry_action; /* actions for TCP retry */
54 static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/
55 static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */
56 static int retry_len; /* TCP retry length */
59 * store_block() - store block in memory
60 * @src: source of data
64 static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
66 ulong newsize = offset + len;
69 ptr = map_sysmem(image_load_addr + offset, len);
70 memcpy(ptr, src, len);
73 if (net_boot_file_size < (offset + len))
74 net_boot_file_size = newsize;
80 * wget_send_stored() - wget response dispatcher
82 * WARNING, This, and only this, is the place in wget.c where
83 * SEQUENCE NUMBERS are swapped between incoming (RX)
85 * Procedure wget_handler() is correct for RX traffic.
87 static void wget_send_stored(void)
89 u8 action = retry_action;
91 unsigned int tcp_ack_num = retry_tcp_ack_num + len;
92 unsigned int tcp_seq_num = retry_tcp_seq_num;
95 switch (current_wget_state) {
97 debug_cond(DEBUG_WGET, "wget: send SYN\n");
98 current_wget_state = WGET_CONNECTING;
99 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
100 tcp_seq_num, tcp_ack_num);
103 case WGET_CONNECTING:
105 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
106 tcp_seq_num, tcp_ack_num);
108 ptr = net_tx_packet + net_eth_hdr_size() +
109 IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
112 memcpy(offset, &bootfile1, strlen(bootfile1));
113 offset += strlen(bootfile1);
115 memcpy(offset, image_url, strlen(image_url));
116 offset += strlen(image_url);
118 memcpy(offset, &bootfile3, strlen(bootfile3));
119 offset += strlen(bootfile3);
120 net_send_tcp_packet((offset - ptr), SERVER_PORT, our_port,
121 TCP_PUSH, tcp_seq_num, tcp_ack_num);
122 current_wget_state = WGET_CONNECTED;
125 case WGET_TRANSFERRING:
126 case WGET_TRANSFERRED:
127 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
128 tcp_seq_num, tcp_ack_num);
133 static void wget_send(u8 action, unsigned int tcp_ack_num,
134 unsigned int tcp_seq_num, int len)
136 retry_action = action;
137 retry_tcp_ack_num = tcp_ack_num;
138 retry_tcp_seq_num = tcp_seq_num;
144 void wget_fail(char *error_message, unsigned int tcp_seq_num,
145 unsigned int tcp_ack_num, u8 action)
147 printf("wget: Transfer Fail - %s\n", error_message);
148 net_set_timeout_handler(0, NULL);
149 wget_send(action, tcp_seq_num, tcp_ack_num, 0);
152 void wget_success(u8 action, unsigned int tcp_seq_num,
153 unsigned int tcp_ack_num, int len, int packets)
155 printf("Packets received %d, Transfer Successful\n", packets);
156 wget_send(action, tcp_seq_num, tcp_ack_num, len);
160 * Interfaces of U-BOOT
162 static void wget_timeout_handler(void)
164 if (++wget_timeout_count > WGET_RETRY_COUNT) {
165 puts("\nRetry count exceeded; starting again\n");
166 wget_send(TCP_RST, 0, 0, 0);
170 net_set_timeout_handler(wget_timeout +
171 WGET_TIMEOUT * wget_timeout_count,
172 wget_timeout_handler);
177 #define PKT_QUEUE_OFFSET 0x20000
178 #define PKT_QUEUE_PACKET_SIZE 0x800
180 static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
181 struct in_addr action_and_state,
182 unsigned int tcp_ack_num, unsigned int len)
184 u8 action = action_and_state.s_addr;
191 pos = strstr((char *)pkt, http_eom);
194 debug_cond(DEBUG_WGET,
195 "wget: Connected, data before Header %p\n", pkt);
196 pkt_in_q = (void *)image_load_addr + PKT_QUEUE_OFFSET +
197 (pkt_q_idx * PKT_QUEUE_PACKET_SIZE);
199 ptr1 = map_sysmem((phys_addr_t)pkt_in_q, len);
200 memcpy(ptr1, pkt, len);
203 pkt_q[pkt_q_idx].pkt = pkt_in_q;
204 pkt_q[pkt_q_idx].tcp_seq_num = tcp_seq_num;
205 pkt_q[pkt_q_idx].len = len;
208 debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt);
209 /* sizeof(http_eom) - 1 is the string length of (http_eom) */
210 hlen = pos - (char *)pkt + sizeof(http_eom) - 1;
211 pos = strstr((char *)pkt, linefeed);
213 i = pos - (char *)pkt;
216 printf("%.*s", i, pkt);
218 current_wget_state = WGET_TRANSFERRING;
220 if (strstr((char *)pkt, http_ok) == 0) {
221 debug_cond(DEBUG_WGET,
222 "wget: Connected Bad Xfer\n");
223 initial_data_seq_num = tcp_seq_num + hlen;
224 wget_loop_state = NETLOOP_FAIL;
225 wget_send(action, tcp_seq_num, tcp_ack_num, len);
227 debug_cond(DEBUG_WGET,
228 "wget: Connctd pkt %p hlen %x\n",
230 initial_data_seq_num = tcp_seq_num + hlen;
232 pos = strstr((char *)pkt, content_len);
236 pos += sizeof(content_len) + 2;
237 strict_strtoul(pos, 10, &content_length);
238 debug_cond(DEBUG_WGET,
239 "wget: Connected Len %lu\n",
243 net_boot_file_size = 0;
246 store_block(pkt + hlen, 0, len - hlen);
248 debug_cond(DEBUG_WGET,
249 "wget: Connected Pkt %p hlen %x\n",
252 for (i = 0; i < pkt_q_idx; i++) {
254 (phys_addr_t)(pkt_q[i].pkt),
257 pkt_q[i].tcp_seq_num -
258 initial_data_seq_num,
261 debug_cond(DEBUG_WGET,
262 "wget: Connctd pkt Q %p len %x\n",
263 pkt_q[i].pkt, pkt_q[i].len);
267 wget_send(action, tcp_seq_num, tcp_ack_num, len);
271 * wget_handler() - handler of wget
272 * @pkt: the pointer to the payload
273 * @tcp_seq_num: tcp sequence number
274 * @action_and_state: TCP state
275 * @tcp_ack_num: tcp acknowledge number
276 * @len: length of the payload
278 * In the "application push" invocation, the TCP header with all
279 * its information is pointed to by the packet pointer.
281 static void wget_handler(uchar *pkt, unsigned int tcp_seq_num,
282 struct in_addr action_and_state,
283 unsigned int tcp_ack_num, unsigned int len)
285 enum tcp_state wget_tcp_state = tcp_get_tcp_state();
286 u8 action = action_and_state.s_addr;
288 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
291 switch (current_wget_state) {
293 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
295 case WGET_CONNECTING:
296 debug_cond(DEBUG_WGET,
297 "wget: Connecting In len=%x, Seq=%x, Ack=%x\n",
298 len, tcp_seq_num, tcp_ack_num);
300 if (wget_tcp_state == TCP_ESTABLISHED) {
301 debug_cond(DEBUG_WGET,
302 "wget: Cting, send, len=%x\n", len);
303 wget_send(action, tcp_seq_num, tcp_ack_num,
306 printf("%.*s", len, pkt);
307 wget_fail("wget: Handler Connected Fail\n",
308 tcp_seq_num, tcp_ack_num, action);
313 debug_cond(DEBUG_WGET, "wget: Connected seq=%x, len=%x\n",
316 wget_fail("Image not found, no data returned\n",
317 tcp_seq_num, tcp_ack_num, action);
319 wget_connected(pkt, tcp_seq_num, action_and_state,
323 case WGET_TRANSFERRING:
324 debug_cond(DEBUG_WGET,
325 "wget: Transferring, seq=%x, ack=%x,len=%x\n",
326 tcp_seq_num, tcp_ack_num, len);
328 if (tcp_seq_num >= initial_data_seq_num &&
329 store_block(pkt, tcp_seq_num - initial_data_seq_num,
331 wget_fail("wget: store error\n",
332 tcp_seq_num, tcp_ack_num, action);
336 switch (wget_tcp_state) {
338 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
344 net_set_state(NETLOOP_FAIL);
346 case TCP_ESTABLISHED:
347 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
349 wget_loop_state = NETLOOP_SUCCESS;
351 case TCP_CLOSE_WAIT: /* End of transfer */
352 current_wget_state = WGET_TRANSFERRED;
353 wget_send(action | TCP_ACK | TCP_FIN,
354 tcp_seq_num, tcp_ack_num, len);
358 case WGET_TRANSFERRED:
359 printf("Packets received %d, Transfer Successful\n", packets);
360 net_set_state(wget_loop_state);
365 #define RANDOM_PORT_START 1024
366 #define RANDOM_PORT_RANGE 0x4000
369 * random_port() - make port a little random (1024-17407)
371 * Return: random port number from 1024 to 17407
373 * This keeps the math somewhat trivial to compute, and seems to work with
374 * all supported protocols/clients/servers
376 static unsigned int random_port(void)
378 return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
381 #define BLOCKSIZE 512
383 void wget_start(void)
385 image_url = strchr(net_boot_file_name, ':');
387 web_server_ip = string_to_ip(net_boot_file_name);
389 net_server_ip = web_server_ip;
391 web_server_ip = net_server_ip;
392 image_url = net_boot_file_name;
395 debug_cond(DEBUG_WGET,
396 "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
397 &web_server_ip, &net_ip);
399 /* Check if we need to send across this subnet */
400 if (net_gateway.s_addr && net_netmask.s_addr) {
401 struct in_addr our_net;
402 struct in_addr server_net;
404 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
405 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
406 if (our_net.s_addr != server_net.s_addr)
407 debug_cond(DEBUG_WGET,
408 "wget: sending through gateway %pI4",
411 debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
413 if (net_boot_file_expected_size_in_blocks) {
414 debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ",
415 net_boot_file_expected_size_in_blocks * BLOCKSIZE);
416 print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE,
419 debug_cond(DEBUG_WGET,
420 "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
422 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
423 tcp_set_tcp_handler(wget_handler);
425 wget_timeout_count = 0;
426 current_wget_state = WGET_CLOSED;
428 our_port = random_port();
431 * Zero out server ether to force arp resolution in case
432 * the server ip for the previous u-boot command, for example dns
433 * is not the same as the web server ip.
436 memset(net_server_ethaddr, 0, 6);
438 wget_send(TCP_SYN, 0, 0, 0);