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