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
16 static const char bootfile1[] = "GET ";
17 static const char bootfile3[] = " HTTP/1.0\r\n\r\n";
18 static const char http_eom[] = "\r\n\r\n";
19 static const char http_ok[] = "200";
20 static const char content_len[] = "Content-Length";
21 static const char linefeed[] = "\r\n";
22 static struct in_addr web_server_ip;
24 static int wget_timeout_count;
28 unsigned int tcp_seq_num;
33 * This is a control structure for out of order packets received.
34 * The actual packet bufers are in the kernel space, and are
35 * expected to be overwritten by the downloaded image.
37 static struct pkt_qd pkt_q[PKTBUFSRX / 4];
39 static unsigned long content_length;
40 static unsigned int packets;
42 static unsigned int initial_data_seq_num;
44 static enum wget_state current_wget_state;
46 static char *image_url;
47 static unsigned int wget_timeout = WGET_TIMEOUT;
49 static enum net_loop_state wget_loop_state;
51 /* Timeout retry parameters */
52 static u8 retry_action; /* actions for TCP retry */
53 static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/
54 static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */
55 static int retry_len; /* TCP retry length */
58 * store_block() - store block in memory
59 * @src: source of data
63 static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
65 ulong newsize = offset + len;
68 ptr = map_sysmem(image_load_addr + offset, len);
69 memcpy(ptr, src, len);
72 if (net_boot_file_size < (offset + len))
73 net_boot_file_size = newsize;
79 * wget_send_stored() - wget response dispatcher
81 * WARNING, This, and only this, is the place in wget.c where
82 * SEQUENCE NUMBERS are swapped between incoming (RX)
84 * Procedure wget_handler() is correct for RX traffic.
86 static void wget_send_stored(void)
88 u8 action = retry_action;
90 unsigned int tcp_ack_num = retry_tcp_ack_num + len;
91 unsigned int tcp_seq_num = retry_tcp_seq_num;
94 switch (current_wget_state) {
96 debug_cond(DEBUG_WGET, "wget: send SYN\n");
97 current_wget_state = WGET_CONNECTING;
98 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
99 tcp_seq_num, tcp_ack_num);
102 case WGET_CONNECTING:
104 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
105 tcp_seq_num, tcp_ack_num);
107 ptr = net_tx_packet + net_eth_hdr_size() +
108 IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
111 memcpy(offset, &bootfile1, strlen(bootfile1));
112 offset += strlen(bootfile1);
114 memcpy(offset, image_url, strlen(image_url));
115 offset += strlen(image_url);
117 memcpy(offset, &bootfile3, strlen(bootfile3));
118 offset += strlen(bootfile3);
119 net_send_tcp_packet((offset - ptr), SERVER_PORT, our_port,
120 TCP_PUSH, tcp_seq_num, tcp_ack_num);
121 current_wget_state = WGET_CONNECTED;
124 case WGET_TRANSFERRING:
125 case WGET_TRANSFERRED:
126 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
127 tcp_seq_num, tcp_ack_num);
132 static void wget_send(u8 action, unsigned int tcp_ack_num,
133 unsigned int tcp_seq_num, int len)
135 retry_action = action;
136 retry_tcp_ack_num = tcp_ack_num;
137 retry_tcp_seq_num = tcp_seq_num;
143 void wget_fail(char *error_message, unsigned int tcp_seq_num,
144 unsigned int tcp_ack_num, u8 action)
146 printf("wget: Transfer Fail - %s\n", error_message);
147 net_set_timeout_handler(0, NULL);
148 wget_send(action, tcp_seq_num, tcp_ack_num, 0);
151 void wget_success(u8 action, unsigned int tcp_seq_num,
152 unsigned int tcp_ack_num, int len, int packets)
154 printf("Packets received %d, Transfer Successful\n", packets);
155 wget_send(action, tcp_seq_num, tcp_ack_num, len);
159 * Interfaces of U-BOOT
161 static void wget_timeout_handler(void)
163 if (++wget_timeout_count > WGET_RETRY_COUNT) {
164 puts("\nRetry count exceeded; starting again\n");
165 wget_send(TCP_RST, 0, 0, 0);
169 net_set_timeout_handler(wget_timeout +
170 WGET_TIMEOUT * wget_timeout_count,
171 wget_timeout_handler);
176 #define PKT_QUEUE_OFFSET 0x20000
177 #define PKT_QUEUE_PACKET_SIZE 0x800
179 static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
180 struct in_addr action_and_state,
181 unsigned int tcp_ack_num, unsigned int len)
183 u8 action = action_and_state.s_addr;
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 debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt);
208 /* sizeof(http_eom) - 1 is the string length of (http_eom) */
209 hlen = pos - (char *)pkt + sizeof(http_eom) - 1;
210 pos = strstr((char *)pkt, linefeed);
212 i = pos - (char *)pkt;
215 printf("%.*s", i, pkt);
217 current_wget_state = WGET_TRANSFERRING;
219 if (strstr((char *)pkt, http_ok) == 0) {
220 debug_cond(DEBUG_WGET,
221 "wget: Connected Bad Xfer\n");
222 initial_data_seq_num = tcp_seq_num + hlen;
223 wget_loop_state = NETLOOP_FAIL;
224 wget_send(action, tcp_seq_num, tcp_ack_num, len);
226 debug_cond(DEBUG_WGET,
227 "wget: Connctd pkt %p hlen %x\n",
229 initial_data_seq_num = tcp_seq_num + hlen;
231 pos = strstr((char *)pkt, content_len);
235 pos += sizeof(content_len) + 2;
236 strict_strtoul(pos, 10, &content_length);
237 debug_cond(DEBUG_WGET,
238 "wget: Connected Len %lu\n",
242 net_boot_file_size = 0;
245 store_block(pkt + hlen, 0, len - hlen);
247 debug_cond(DEBUG_WGET,
248 "wget: Connected Pkt %p hlen %x\n",
251 for (i = 0; i < pkt_q_idx; i++) {
253 (phys_addr_t)(pkt_q[i].pkt),
256 pkt_q[i].tcp_seq_num -
257 initial_data_seq_num,
260 debug_cond(DEBUG_WGET,
261 "wget: Connctd pkt Q %p len %x\n",
262 pkt_q[i].pkt, pkt_q[i].len);
266 wget_send(action, tcp_seq_num, tcp_ack_num, len);
270 * wget_handler() - handler of wget
271 * @pkt: the pointer to the payload
272 * @tcp_seq_num: tcp sequence number
273 * @action_and_state: TCP state
274 * @tcp_ack_num: tcp acknowledge number
275 * @len: length of the payload
277 * In the "application push" invocation, the TCP header with all
278 * its information is pointed to by the packet pointer.
280 static void wget_handler(uchar *pkt, unsigned int tcp_seq_num,
281 struct in_addr action_and_state,
282 unsigned int tcp_ack_num, unsigned int len)
284 enum tcp_state wget_tcp_state = tcp_get_tcp_state();
285 u8 action = action_and_state.s_addr;
287 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
290 switch (current_wget_state) {
292 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
294 case WGET_CONNECTING:
295 debug_cond(DEBUG_WGET,
296 "wget: Connecting In len=%x, Seq=%x, Ack=%x\n",
297 len, tcp_seq_num, tcp_ack_num);
299 if (wget_tcp_state == TCP_ESTABLISHED) {
300 debug_cond(DEBUG_WGET,
301 "wget: Cting, send, len=%x\n", len);
302 wget_send(action, tcp_seq_num, tcp_ack_num,
305 printf("%.*s", len, pkt);
306 wget_fail("wget: Handler Connected Fail\n",
307 tcp_seq_num, tcp_ack_num, action);
312 debug_cond(DEBUG_WGET, "wget: Connected seq=%x, len=%x\n",
315 wget_fail("Image not found, no data returned\n",
316 tcp_seq_num, tcp_ack_num, action);
318 wget_connected(pkt, tcp_seq_num, action_and_state,
322 case WGET_TRANSFERRING:
323 debug_cond(DEBUG_WGET,
324 "wget: Transferring, seq=%x, ack=%x,len=%x\n",
325 tcp_seq_num, tcp_ack_num, len);
327 if (tcp_seq_num >= initial_data_seq_num &&
328 store_block(pkt, tcp_seq_num - initial_data_seq_num,
330 wget_fail("wget: store error\n",
331 tcp_seq_num, tcp_ack_num, action);
335 switch (wget_tcp_state) {
337 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
343 net_set_state(NETLOOP_FAIL);
345 case TCP_ESTABLISHED:
346 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
348 wget_loop_state = NETLOOP_SUCCESS;
350 case TCP_CLOSE_WAIT: /* End of transfer */
351 current_wget_state = WGET_TRANSFERRED;
352 wget_send(action | TCP_ACK | TCP_FIN,
353 tcp_seq_num, tcp_ack_num, len);
357 case WGET_TRANSFERRED:
358 printf("Packets received %d, Transfer Successful\n", packets);
359 net_set_state(wget_loop_state);
364 #define RANDOM_PORT_START 1024
365 #define RANDOM_PORT_RANGE 0x4000
368 * random_port() - make port a little random (1024-17407)
370 * Return: random port number from 1024 to 17407
372 * This keeps the math somewhat trivial to compute, and seems to work with
373 * all supported protocols/clients/servers
375 static unsigned int random_port(void)
377 return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
380 #define BLOCKSIZE 512
382 void wget_start(void)
384 image_url = strchr(net_boot_file_name, ':');
386 web_server_ip = string_to_ip(net_boot_file_name);
388 net_server_ip = web_server_ip;
390 web_server_ip = net_server_ip;
391 image_url = net_boot_file_name;
394 debug_cond(DEBUG_WGET,
395 "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
396 &web_server_ip, &net_ip);
398 /* Check if we need to send across this subnet */
399 if (net_gateway.s_addr && net_netmask.s_addr) {
400 struct in_addr our_net;
401 struct in_addr server_net;
403 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
404 server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
405 if (our_net.s_addr != server_net.s_addr)
406 debug_cond(DEBUG_WGET,
407 "wget: sending through gateway %pI4",
410 debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
412 if (net_boot_file_expected_size_in_blocks) {
413 debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ",
414 net_boot_file_expected_size_in_blocks * BLOCKSIZE);
415 print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE,
418 debug_cond(DEBUG_WGET,
419 "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
421 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
422 tcp_set_tcp_handler(wget_handler);
424 wget_timeout_count = 0;
425 current_wget_state = WGET_CLOSED;
427 our_port = random_port();
430 * Zero out server ether to force arp resolution in case
431 * the server ip for the previous u-boot command, for example dns
432 * is not the same as the web server ip.
435 memset(net_server_ethaddr, 0, 6);
437 wget_send(TCP_SYN, 0, 0, 0);