Merge tag 'u-boot-imx-20230203' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[platform/kernel/u-boot.git] / net / wget.c
1 // 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>
9 #include <display_options.h>
10 #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
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;
24 static int our_port;
25 static int wget_timeout_count;
26
27 struct 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  */
38 static struct pkt_qd pkt_q[PKTBUFSRX / 4];
39 static int pkt_q_idx;
40 static unsigned long content_length;
41 static unsigned int packets;
42
43 static unsigned int initial_data_seq_num;
44
45 static enum  wget_state current_wget_state;
46
47 static char *image_url;
48 static unsigned int wget_timeout = WGET_TIMEOUT;
49
50 static enum net_loop_state wget_loop_state;
51
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 */
57
58 /**
59  * store_block() - store block in memory
60  * @src: source of data
61  * @offset: offset
62  * @len: length
63  */
64 static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
65 {
66         ulong newsize = offset + len;
67         uchar *ptr;
68
69         ptr = map_sysmem(image_load_addr + offset, len);
70         memcpy(ptr, src, len);
71         unmap_sysmem(ptr);
72
73         if (net_boot_file_size < (offset + len))
74                 net_boot_file_size = newsize;
75
76         return 0;
77 }
78
79 /**
80  * wget_send_stored() - wget response dispatcher
81  *
82  * WARNING, This, and only this, is the place in wget.c where
83  * SEQUENCE NUMBERS are swapped between incoming (RX)
84  * and outgoing (TX).
85  * Procedure wget_handler() is correct for RX traffic.
86  */
87 static void wget_send_stored(void)
88 {
89         u8 action = retry_action;
90         int len = retry_len;
91         unsigned int tcp_ack_num = retry_tcp_ack_num + len;
92         unsigned int tcp_seq_num = retry_tcp_seq_num;
93         uchar *ptr, *offset;
94
95         switch (current_wget_state) {
96         case WGET_CLOSED:
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);
101                 packets = 0;
102                 break;
103         case WGET_CONNECTING:
104                 pkt_q_idx = 0;
105                 net_send_tcp_packet(0, SERVER_PORT, our_port, action,
106                                     tcp_seq_num, tcp_ack_num);
107
108                 ptr = net_tx_packet + net_eth_hdr_size() +
109                         IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2;
110                 offset = ptr;
111
112                 memcpy(offset, &bootfile1, strlen(bootfile1));
113                 offset += strlen(bootfile1);
114
115                 memcpy(offset, image_url, strlen(image_url));
116                 offset += strlen(image_url);
117
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;
123                 break;
124         case 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);
129                 break;
130         }
131 }
132
133 static void wget_send(u8 action, unsigned int tcp_ack_num,
134                       unsigned int tcp_seq_num, int len)
135 {
136         retry_action = action;
137         retry_tcp_ack_num = tcp_ack_num;
138         retry_tcp_seq_num = tcp_seq_num;
139         retry_len = len;
140
141         wget_send_stored();
142 }
143
144 void wget_fail(char *error_message, unsigned int tcp_seq_num,
145                unsigned int tcp_ack_num, u8 action)
146 {
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);
150 }
151
152 void wget_success(u8 action, unsigned int tcp_seq_num,
153                   unsigned int tcp_ack_num, int len, int packets)
154 {
155         printf("Packets received %d, Transfer Successful\n", packets);
156         wget_send(action, tcp_seq_num, tcp_ack_num, len);
157 }
158
159 /*
160  * Interfaces of U-BOOT
161  */
162 static void wget_timeout_handler(void)
163 {
164         if (++wget_timeout_count > WGET_RETRY_COUNT) {
165                 puts("\nRetry count exceeded; starting again\n");
166                 wget_send(TCP_RST, 0, 0, 0);
167                 net_start_again();
168         } else {
169                 puts("T ");
170                 net_set_timeout_handler(wget_timeout +
171                                         WGET_TIMEOUT * wget_timeout_count,
172                                         wget_timeout_handler);
173                 wget_send_stored();
174         }
175 }
176
177 #define PKT_QUEUE_OFFSET 0x20000
178 #define PKT_QUEUE_PACKET_SIZE 0x800
179
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)
183 {
184         u8 action = action_and_state.s_addr;
185         uchar *pkt_in_q;
186         char *pos;
187         int hlen, i;
188         uchar *ptr1;
189
190         pkt[len] = '\0';
191         pos = strstr((char *)pkt, http_eom);
192
193         if (!pos) {
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);
198
199                 ptr1 = map_sysmem((phys_addr_t)pkt_in_q, len);
200                 memcpy(ptr1, pkt, len);
201                 unmap_sysmem(ptr1);
202
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;
206                 pkt_q_idx++;
207         } else {
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);
212                 if (pos > 0)
213                         i = pos - (char *)pkt;
214                 else
215                         i = hlen;
216                 printf("%.*s", i,  pkt);
217
218                 current_wget_state = WGET_TRANSFERRING;
219
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);
226                 } else {
227                         debug_cond(DEBUG_WGET,
228                                    "wget: Connctd pkt %p  hlen %x\n",
229                                    pkt, hlen);
230                         initial_data_seq_num = tcp_seq_num + hlen;
231
232                         pos = strstr((char *)pkt, content_len);
233                         if (!pos) {
234                                 content_length = -1;
235                         } else {
236                                 pos += sizeof(content_len) + 2;
237                                 strict_strtoul(pos, 10, &content_length);
238                                 debug_cond(DEBUG_WGET,
239                                            "wget: Connected Len %lu\n",
240                                            content_length);
241                         }
242
243                         net_boot_file_size = 0;
244
245                         if (len > hlen)
246                                 store_block(pkt + hlen, 0, len - hlen);
247
248                         debug_cond(DEBUG_WGET,
249                                    "wget: Connected Pkt %p hlen %x\n",
250                                    pkt, hlen);
251
252                         for (i = 0; i < pkt_q_idx; i++) {
253                                 ptr1 = map_sysmem(
254                                         (phys_addr_t)(pkt_q[i].pkt),
255                                         pkt_q[i].len);
256                                 store_block(ptr1,
257                                             pkt_q[i].tcp_seq_num -
258                                             initial_data_seq_num,
259                                             pkt_q[i].len);
260                                 unmap_sysmem(ptr1);
261                                 debug_cond(DEBUG_WGET,
262                                            "wget: Connctd pkt Q %p len %x\n",
263                                            pkt_q[i].pkt, pkt_q[i].len);
264                         }
265                 }
266         }
267         wget_send(action, tcp_seq_num, tcp_ack_num, len);
268 }
269
270 /**
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
277  *
278  * In the "application push" invocation, the TCP header with all
279  * its information is pointed to by the packet pointer.
280  */
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)
284 {
285         enum tcp_state wget_tcp_state = tcp_get_tcp_state();
286         u8 action = action_and_state.s_addr;
287
288         net_set_timeout_handler(wget_timeout, wget_timeout_handler);
289         packets++;
290
291         switch (current_wget_state) {
292         case WGET_CLOSED:
293                 debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n");
294                 break;
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);
299                 if (!len) {
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,
304                                           len);
305                         } else {
306                                 printf("%.*s", len,  pkt);
307                                 wget_fail("wget: Handler Connected Fail\n",
308                                           tcp_seq_num, tcp_ack_num, action);
309                         }
310                 }
311                 break;
312         case WGET_CONNECTED:
313                 debug_cond(DEBUG_WGET, "wget: Connected seq=%x, len=%x\n",
314                            tcp_seq_num, len);
315                 if (!len) {
316                         wget_fail("Image not found, no data returned\n",
317                                   tcp_seq_num, tcp_ack_num, action);
318                 } else {
319                         wget_connected(pkt, tcp_seq_num, action_and_state,
320                                        tcp_ack_num, len);
321                 }
322                 break;
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);
327
328                 if (tcp_seq_num >= initial_data_seq_num &&
329                     store_block(pkt, tcp_seq_num - initial_data_seq_num,
330                                 len) != 0) {
331                         wget_fail("wget: store error\n",
332                                   tcp_seq_num, tcp_ack_num, action);
333                         return;
334                 }
335
336                 switch (wget_tcp_state) {
337                 case TCP_FIN_WAIT_2:
338                         wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len);
339                         fallthrough;
340                 case TCP_SYN_SENT:
341                 case TCP_CLOSING:
342                 case TCP_FIN_WAIT_1:
343                 case TCP_CLOSED:
344                         net_set_state(NETLOOP_FAIL);
345                         break;
346                 case TCP_ESTABLISHED:
347                         wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num,
348                                   len);
349                         wget_loop_state = NETLOOP_SUCCESS;
350                         break;
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);
355                         break;
356                 }
357                 break;
358         case WGET_TRANSFERRED:
359                 printf("Packets received %d, Transfer Successful\n", packets);
360                 net_set_state(wget_loop_state);
361                 break;
362         }
363 }
364
365 #define RANDOM_PORT_START 1024
366 #define RANDOM_PORT_RANGE 0x4000
367
368 /**
369  * random_port() - make port a little random (1024-17407)
370  *
371  * Return: random port number from 1024 to 17407
372  *
373  * This keeps the math somewhat trivial to compute, and seems to work with
374  * all supported protocols/clients/servers
375  */
376 static unsigned int random_port(void)
377 {
378         return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE);
379 }
380
381 #define BLOCKSIZE 512
382
383 void wget_start(void)
384 {
385         image_url = strchr(net_boot_file_name, ':');
386         if (image_url > 0) {
387                 web_server_ip = string_to_ip(net_boot_file_name);
388                 ++image_url;
389                 net_server_ip = web_server_ip;
390         } else {
391                 web_server_ip = net_server_ip;
392                 image_url = net_boot_file_name;
393         }
394
395         debug_cond(DEBUG_WGET,
396                    "wget: Transfer HTTP Server %pI4; our IP %pI4\n",
397                    &web_server_ip, &net_ip);
398
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;
403
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",
409                                    &net_gateway);
410         }
411         debug_cond(DEBUG_WGET, "URL '%s'\n", image_url);
412
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,
417                            "");
418         }
419         debug_cond(DEBUG_WGET,
420                    "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr);
421
422         net_set_timeout_handler(wget_timeout, wget_timeout_handler);
423         tcp_set_tcp_handler(wget_handler);
424
425         wget_timeout_count = 0;
426         current_wget_state = WGET_CLOSED;
427
428         our_port = random_port();
429
430         /*
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.
434          */
435
436         memset(net_server_ethaddr, 0, 6);
437
438         wget_send(TCP_SYN, 0, 0, 0);
439 }