blob: 8bb4d72db1ae4a8a25855994ceb9be03cc72967c [file] [log] [blame]
Ying-Chun Liu (PaulLiu)cfbae482022-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
7#include <command.h>
8#include <common.h>
Michael Wallefe1489b2022-12-28 16:27:14 +01009#include <display_options.h>
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +080010#include <env.h>
11#include <image.h>
12#include <mapmem.h>
13#include <net.h>
14#include <net/tcp.h>
15#include <net/wget.h>
16
17static const char bootfile1[] = "GET ";
18static const char bootfile3[] = " HTTP/1.0\r\n\r\n";
19static const char http_eom[] = "\r\n\r\n";
20static const char http_ok[] = "200";
21static const char content_len[] = "Content-Length";
22static const char linefeed[] = "\r\n";
23static struct in_addr web_server_ip;
24static int our_port;
25static int wget_timeout_count;
26
27struct pkt_qd {
28 uchar *pkt;
29 unsigned int tcp_seq_num;
30 unsigned int len;
31};
32
33/*
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.
37 */
Richard Weinbergera8bd5ec2023-07-20 14:51:56 +020038#define PKTQ_SZ (PKTBUFSRX / 4)
39static struct pkt_qd pkt_q[PKTQ_SZ];
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +080040static int pkt_q_idx;
41static unsigned long content_length;
42static unsigned int packets;
43
44static unsigned int initial_data_seq_num;
45
46static enum wget_state current_wget_state;
47
48static char *image_url;
49static unsigned int wget_timeout = WGET_TIMEOUT;
50
51static enum net_loop_state wget_loop_state;
52
53/* Timeout retry parameters */
54static u8 retry_action; /* actions for TCP retry */
55static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/
56static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */
57static int retry_len; /* TCP retry length */
58
59/**
60 * store_block() - store block in memory
61 * @src: source of data
62 * @offset: offset
63 * @len: length
64 */
65static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
66{
67 ulong newsize = offset + len;
68 uchar *ptr;
69
70 ptr = map_sysmem(image_load_addr + offset, len);
71 memcpy(ptr, src, len);
72 unmap_sysmem(ptr);
73
74 if (net_boot_file_size < (offset + len))
75 net_boot_file_size = newsize;
76
77 return 0;
78}
79
80/**
81 * wget_send_stored() - wget response dispatcher
82 *
83 * WARNING, This, and only this, is the place in wget.c where
84 * SEQUENCE NUMBERS are swapped between incoming (RX)
85 * and outgoing (TX).
86 * Procedure wget_handler() is correct for RX traffic.
87 */
88static void wget_send_stored(void)
89{
90 u8 action = retry_action;
91 int len = retry_len;
Dmitrii Merkurev08fb8da2023-04-12 19:49:29 +010092 unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len);
93 unsigned int tcp_seq_num = retry_tcp_ack_num;
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +080094 uchar *ptr, *offset;
95
96 switch (current_wget_state) {
97 case WGET_CLOSED:
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);
102 packets = 0;
103 break;
104 case WGET_CONNECTING:
105 pkt_q_idx = 0;
106 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
107 tcp_seq_num, tcp_ack_num);
108
109 ptr = net_tx_packet + net_eth_hdr_size() +
110 IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
111 offset = ptr;
112
113 memcpy(offset, &bootfile1, strlen(bootfile1));
114 offset += strlen(bootfile1);
115
116 memcpy(offset, image_url, strlen(image_url));
117 offset += strlen(image_url);
118
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;
124 break;
125 case 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);
130 break;
131 }
132}
133
Dmitrii Merkurev08fb8da2023-04-12 19:49:29 +0100134static void wget_send(u8 action, unsigned int tcp_seq_num,
135 unsigned int tcp_ack_num, int len)
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800136{
137 retry_action = action;
138 retry_tcp_ack_num = tcp_ack_num;
139 retry_tcp_seq_num = tcp_seq_num;
140 retry_len = len;
141
142 wget_send_stored();
143}
144
145void wget_fail(char *error_message, unsigned int tcp_seq_num,
146 unsigned int tcp_ack_num, u8 action)
147{
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);
151}
152
153void wget_success(u8 action, unsigned int tcp_seq_num,
154 unsigned int tcp_ack_num, int len, int packets)
155{
156 printf("Packets received %d, Transfer Successful\n", packets);
157 wget_send(action, tcp_seq_num, tcp_ack_num, len);
158}
159
160/*
161 * Interfaces of U-BOOT
162 */
163static void wget_timeout_handler(void)
164{
165 if (++wget_timeout_count > WGET_RETRY_COUNT) {
166 puts("\nRetry count exceeded; starting again\n");
167 wget_send(TCP_RST, 0, 0, 0);
168 net_start_again();
169 } else {
170 puts("T ");
171 net_set_timeout_handler(wget_timeout +
172 WGET_TIMEOUT * wget_timeout_count,
173 wget_timeout_handler);
174 wget_send_stored();
175 }
176}
177
178#define PKT_QUEUE_OFFSET 0x20000
179#define PKT_QUEUE_PACKET_SIZE 0x800
180
181static void wget_connected(uchar *pkt, unsigned int tcp_seq_num,
Dmitrii Merkurev08fb8da2023-04-12 19:49:29 +0100182 u8 action, unsigned int tcp_ack_num, unsigned int len)
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800183{
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800184 uchar *pkt_in_q;
185 char *pos;
186 int hlen, i;
187 uchar *ptr1;
188
189 pkt[len] = '\0';
190 pos = strstr((char *)pkt, http_eom);
191
192 if (!pos) {
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);
197
198 ptr1 = map_sysmem((phys_addr_t)pkt_in_q, len);
199 memcpy(ptr1, pkt, len);
200 unmap_sysmem(ptr1);
201
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;
205 pkt_q_idx++;
Richard Weinbergera8bd5ec2023-07-20 14:51:56 +0200206
207 if (pkt_q_idx >= PKTQ_SZ) {
208 printf("wget: Fatal error, queue overrun!\n");
209 net_set_state(NETLOOP_FAIL);
210
211 return;
212 }
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800213 } else {
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);
218 if (pos > 0)
219 i = pos - (char *)pkt;
220 else
221 i = hlen;
222 printf("%.*s", i, pkt);
223
224 current_wget_state = WGET_TRANSFERRING;
225
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);
232 } else {
233 debug_cond(DEBUG_WGET,
234 "wget: Connctd pkt %p hlen %x\n",
235 pkt, hlen);
236 initial_data_seq_num = tcp_seq_num + hlen;
237
238 pos = strstr((char *)pkt, content_len);
239 if (!pos) {
240 content_length = -1;
241 } else {
242 pos += sizeof(content_len) + 2;
243 strict_strtoul(pos, 10, &content_length);
244 debug_cond(DEBUG_WGET,
245 "wget: Connected Len %lu\n",
246 content_length);
247 }
248
249 net_boot_file_size = 0;
250
251 if (len > hlen)
252 store_block(pkt + hlen, 0, len - hlen);
253
254 debug_cond(DEBUG_WGET,
255 "wget: Connected Pkt %p hlen %x\n",
256 pkt, hlen);
257
258 for (i = 0; i < pkt_q_idx; i++) {
259 ptr1 = map_sysmem(
260 (phys_addr_t)(pkt_q[i].pkt),
261 pkt_q[i].len);
262 store_block(ptr1,
263 pkt_q[i].tcp_seq_num -
264 initial_data_seq_num,
265 pkt_q[i].len);
266 unmap_sysmem(ptr1);
267 debug_cond(DEBUG_WGET,
268 "wget: Connctd pkt Q %p len %x\n",
269 pkt_q[i].pkt, pkt_q[i].len);
270 }
271 }
272 }
273 wget_send(action, tcp_seq_num, tcp_ack_num, len);
274}
275
276/**
Dmitrii Merkurev08fb8da2023-04-12 19:49:29 +0100277 * 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
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800286 *
287 * In the "application push" invocation, the TCP header with all
288 * its information is pointed to by the packet pointer.
289 */
Dmitrii Merkurev08fb8da2023-04-12 19:49:29 +0100290static 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)
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800294{
295 enum tcp_state wget_tcp_state = tcp_get_tcp_state();
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800296
297 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
298 packets++;
299
300 switch (current_wget_state) {
301 case WGET_CLOSED:
302 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
303 break;
304 case WGET_CONNECTING:
305 debug_cond(DEBUG_WGET,
Dmitrii Merkurev08fb8da2023-04-12 19:49:29 +0100306 "wget: Connecting In len=%x, Seq=%u, Ack=%u\n",
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800307 len, tcp_seq_num, tcp_ack_num);
308 if (!len) {
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,
313 len);
314 } else {
315 printf("%.*s", len, pkt);
316 wget_fail("wget: Handler Connected Fail\n",
317 tcp_seq_num, tcp_ack_num, action);
318 }
319 }
320 break;
321 case WGET_CONNECTED:
Dmitrii Merkurev08fb8da2023-04-12 19:49:29 +0100322 debug_cond(DEBUG_WGET, "wget: Connected seq=%u, len=%x\n",
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800323 tcp_seq_num, len);
324 if (!len) {
325 wget_fail("Image not found, no data returned\n",
326 tcp_seq_num, tcp_ack_num, action);
327 } else {
Dmitrii Merkurev08fb8da2023-04-12 19:49:29 +0100328 wget_connected(pkt, tcp_seq_num, action, tcp_ack_num, len);
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800329 }
330 break;
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);
335
336 if (tcp_seq_num >= initial_data_seq_num &&
337 store_block(pkt, tcp_seq_num - initial_data_seq_num,
338 len) != 0) {
339 wget_fail("wget: store error\n",
340 tcp_seq_num, tcp_ack_num, action);
341 return;
342 }
343
344 switch (wget_tcp_state) {
345 case TCP_FIN_WAIT_2:
346 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
347 fallthrough;
348 case TCP_SYN_SENT:
Dmitrii Merkurev08fb8da2023-04-12 19:49:29 +0100349 case TCP_SYN_RECEIVED:
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800350 case TCP_CLOSING:
351 case TCP_FIN_WAIT_1:
352 case TCP_CLOSED:
353 net_set_state(NETLOOP_FAIL);
354 break;
355 case TCP_ESTABLISHED:
356 wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
357 len);
358 wget_loop_state = NETLOOP_SUCCESS;
359 break;
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);
364 break;
365 }
366 break;
367 case WGET_TRANSFERRED:
368 printf("Packets received %d, Transfer Successful\n", packets);
369 net_set_state(wget_loop_state);
370 break;
371 }
372}
373
374#define RANDOM_PORT_START 1024
375#define RANDOM_PORT_RANGE 0x4000
376
377/**
378 * random_port() - make port a little random (1024-17407)
379 *
380 * Return: random port number from 1024 to 17407
381 *
382 * This keeps the math somewhat trivial to compute, and seems to work with
383 * all supported protocols/clients/servers
384 */
385static unsigned int random_port(void)
386{
387 return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
388}
389
390#define BLOCKSIZE 512
391
392void wget_start(void)
393{
394 image_url = strchr(net_boot_file_name, ':');
395 if (image_url > 0) {
396 web_server_ip = string_to_ip(net_boot_file_name);
397 ++image_url;
398 net_server_ip = web_server_ip;
399 } else {
400 web_server_ip = net_server_ip;
401 image_url = net_boot_file_name;
402 }
403
404 debug_cond(DEBUG_WGET,
405 "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
406 &web_server_ip, &net_ip);
407
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;
412
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",
418 &net_gateway);
419 }
420 debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
421
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,
426 "");
427 }
428 debug_cond(DEBUG_WGET,
429 "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
430
431 net_set_timeout_handler(wget_timeout, wget_timeout_handler);
432 tcp_set_tcp_handler(wget_handler);
433
434 wget_timeout_count = 0;
435 current_wget_state = WGET_CLOSED;
436
437 our_port = random_port();
438
439 /*
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.
443 */
444
445 memset(net_server_ethaddr, 0, 6);
446
447 wget_send(TCP_SYN, 0, 0, 0);
448}