Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 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 | |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 7 | #include <asm/global_data.h> |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 8 | #include <command.h> |
Michael Walle | caad55b | 2022-12-28 16:27:14 +0100 | [diff] [blame] | 9 | #include <display_options.h> |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 10 | #include <env.h> |
Jerome Forissier | 612b2b1 | 2024-09-11 11:58:22 +0200 | [diff] [blame] | 11 | #include <efi_loader.h> |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 12 | #include <image.h> |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 13 | #include <lmb.h> |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 14 | #include <mapmem.h> |
| 15 | #include <net.h> |
| 16 | #include <net/tcp.h> |
| 17 | #include <net/wget.h> |
Masahisa Kojima | 6721d18 | 2023-11-10 13:25:35 +0900 | [diff] [blame] | 18 | #include <stdlib.h> |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 19 | |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Marek Vasut | 22a9508 | 2023-12-13 22:11:13 +0100 | [diff] [blame] | 22 | /* The default, change with environment variable 'httpdstp' */ |
| 23 | #define SERVER_PORT 80 |
| 24 | |
Adriano Cordova | 47f35e3 | 2024-11-11 18:08:58 -0300 | [diff] [blame] | 25 | static const char bootfileGET[] = "GET "; |
| 26 | static const char bootfileHEAD[] = "HEAD "; |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 27 | static const char bootfile3[] = " HTTP/1.0\r\n\r\n"; |
| 28 | static const char http_eom[] = "\r\n\r\n"; |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 29 | static const char content_len[] = "Content-Length"; |
| 30 | static const char linefeed[] = "\r\n"; |
| 31 | static struct in_addr web_server_ip; |
| 32 | static int our_port; |
| 33 | static int wget_timeout_count; |
| 34 | |
| 35 | struct pkt_qd { |
| 36 | uchar *pkt; |
| 37 | unsigned int tcp_seq_num; |
| 38 | unsigned int len; |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * This is a control structure for out of order packets received. |
| 43 | * The actual packet bufers are in the kernel space, and are |
| 44 | * expected to be overwritten by the downloaded image. |
| 45 | */ |
Richard Weinberger | bf44cf4 | 2023-07-20 14:51:56 +0200 | [diff] [blame] | 46 | #define PKTQ_SZ (PKTBUFSRX / 4) |
| 47 | static struct pkt_qd pkt_q[PKTQ_SZ]; |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 48 | static int pkt_q_idx; |
| 49 | static unsigned long content_length; |
| 50 | static unsigned int packets; |
| 51 | |
| 52 | static unsigned int initial_data_seq_num; |
Yasuharu Shibata | 07a00ef | 2024-04-14 19:46:07 +0900 | [diff] [blame] | 53 | static unsigned int next_data_seq_num; |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 54 | |
| 55 | static enum wget_state current_wget_state; |
| 56 | |
| 57 | static char *image_url; |
| 58 | static unsigned int wget_timeout = WGET_TIMEOUT; |
| 59 | |
| 60 | static enum net_loop_state wget_loop_state; |
| 61 | |
| 62 | /* Timeout retry parameters */ |
| 63 | static u8 retry_action; /* actions for TCP retry */ |
| 64 | static unsigned int retry_tcp_ack_num; /* TCP retry acknowledge number*/ |
| 65 | static unsigned int retry_tcp_seq_num; /* TCP retry sequence number */ |
| 66 | static int retry_len; /* TCP retry length */ |
| 67 | |
| 68 | /** |
| 69 | * store_block() - store block in memory |
| 70 | * @src: source of data |
| 71 | * @offset: offset |
| 72 | * @len: length |
| 73 | */ |
| 74 | static inline int store_block(uchar *src, unsigned int offset, unsigned int len) |
| 75 | { |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 76 | ulong store_addr = image_load_addr + offset; |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 77 | ulong newsize = offset + len; |
| 78 | uchar *ptr; |
| 79 | |
Adriano Cordova | 47f35e3 | 2024-11-11 18:08:58 -0300 | [diff] [blame] | 80 | if (CONFIG_IS_ENABLED(LMB) && wget_info->set_bootdev) { |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 81 | if (store_addr < image_load_addr || |
Sughosh Ganu | 7772809 | 2024-09-16 20:50:25 +0530 | [diff] [blame] | 82 | lmb_read_check(store_addr, len)) { |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 83 | printf("\nwget error: "); |
| 84 | printf("trying to overwrite reserved memory...\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | ptr = map_sysmem(store_addr, len); |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 90 | memcpy(ptr, src, len); |
| 91 | unmap_sysmem(ptr); |
| 92 | |
| 93 | if (net_boot_file_size < (offset + len)) |
| 94 | net_boot_file_size = newsize; |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * wget_send_stored() - wget response dispatcher |
| 101 | * |
| 102 | * WARNING, This, and only this, is the place in wget.c where |
| 103 | * SEQUENCE NUMBERS are swapped between incoming (RX) |
| 104 | * and outgoing (TX). |
| 105 | * Procedure wget_handler() is correct for RX traffic. |
| 106 | */ |
| 107 | static void wget_send_stored(void) |
| 108 | { |
| 109 | u8 action = retry_action; |
| 110 | int len = retry_len; |
Dmitrii Merkurev | 3acd054 | 2023-04-12 19:49:29 +0100 | [diff] [blame] | 111 | unsigned int tcp_ack_num = retry_tcp_seq_num + (len == 0 ? 1 : len); |
| 112 | unsigned int tcp_seq_num = retry_tcp_ack_num; |
Marek Vasut | 22a9508 | 2023-12-13 22:11:13 +0100 | [diff] [blame] | 113 | unsigned int server_port; |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 114 | uchar *ptr, *offset; |
| 115 | |
Marek Vasut | 22a9508 | 2023-12-13 22:11:13 +0100 | [diff] [blame] | 116 | server_port = env_get_ulong("httpdstp", 10, SERVER_PORT) & 0xffff; |
| 117 | |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 118 | switch (current_wget_state) { |
| 119 | case WGET_CLOSED: |
| 120 | debug_cond(DEBUG_WGET, "wget: send SYN\n"); |
| 121 | current_wget_state = WGET_CONNECTING; |
Marek Vasut | 22a9508 | 2023-12-13 22:11:13 +0100 | [diff] [blame] | 122 | net_send_tcp_packet(0, server_port, our_port, action, |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 123 | tcp_seq_num, tcp_ack_num); |
| 124 | packets = 0; |
| 125 | break; |
| 126 | case WGET_CONNECTING: |
| 127 | pkt_q_idx = 0; |
Marek Vasut | 22a9508 | 2023-12-13 22:11:13 +0100 | [diff] [blame] | 128 | net_send_tcp_packet(0, server_port, our_port, action, |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 129 | tcp_seq_num, tcp_ack_num); |
| 130 | |
| 131 | ptr = net_tx_packet + net_eth_hdr_size() + |
| 132 | IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; |
| 133 | offset = ptr; |
| 134 | |
Adriano Cordova | 47f35e3 | 2024-11-11 18:08:58 -0300 | [diff] [blame] | 135 | switch (wget_info->method) { |
| 136 | case WGET_HTTP_METHOD_HEAD: |
| 137 | memcpy(offset, &bootfileHEAD, strlen(bootfileHEAD)); |
| 138 | offset += strlen(bootfileHEAD); |
| 139 | break; |
| 140 | case WGET_HTTP_METHOD_GET: |
| 141 | default: |
| 142 | memcpy(offset, &bootfileGET, strlen(bootfileGET)); |
| 143 | offset += strlen(bootfileGET); |
| 144 | break; |
| 145 | } |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 146 | |
| 147 | memcpy(offset, image_url, strlen(image_url)); |
| 148 | offset += strlen(image_url); |
| 149 | |
| 150 | memcpy(offset, &bootfile3, strlen(bootfile3)); |
| 151 | offset += strlen(bootfile3); |
Marek Vasut | 22a9508 | 2023-12-13 22:11:13 +0100 | [diff] [blame] | 152 | net_send_tcp_packet((offset - ptr), server_port, our_port, |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 153 | TCP_PUSH, tcp_seq_num, tcp_ack_num); |
| 154 | current_wget_state = WGET_CONNECTED; |
| 155 | break; |
| 156 | case WGET_CONNECTED: |
| 157 | case WGET_TRANSFERRING: |
| 158 | case WGET_TRANSFERRED: |
Marek Vasut | 22a9508 | 2023-12-13 22:11:13 +0100 | [diff] [blame] | 159 | net_send_tcp_packet(0, server_port, our_port, action, |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 160 | tcp_seq_num, tcp_ack_num); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
Dmitrii Merkurev | 3acd054 | 2023-04-12 19:49:29 +0100 | [diff] [blame] | 165 | static void wget_send(u8 action, unsigned int tcp_seq_num, |
| 166 | unsigned int tcp_ack_num, int len) |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 167 | { |
| 168 | retry_action = action; |
| 169 | retry_tcp_ack_num = tcp_ack_num; |
| 170 | retry_tcp_seq_num = tcp_seq_num; |
| 171 | retry_len = len; |
| 172 | |
| 173 | wget_send_stored(); |
| 174 | } |
| 175 | |
| 176 | void wget_fail(char *error_message, unsigned int tcp_seq_num, |
| 177 | unsigned int tcp_ack_num, u8 action) |
| 178 | { |
| 179 | printf("wget: Transfer Fail - %s\n", error_message); |
| 180 | net_set_timeout_handler(0, NULL); |
| 181 | wget_send(action, tcp_seq_num, tcp_ack_num, 0); |
| 182 | } |
| 183 | |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 184 | /* |
| 185 | * Interfaces of U-BOOT |
| 186 | */ |
| 187 | static void wget_timeout_handler(void) |
| 188 | { |
| 189 | if (++wget_timeout_count > WGET_RETRY_COUNT) { |
| 190 | puts("\nRetry count exceeded; starting again\n"); |
| 191 | wget_send(TCP_RST, 0, 0, 0); |
| 192 | net_start_again(); |
| 193 | } else { |
| 194 | puts("T "); |
| 195 | net_set_timeout_handler(wget_timeout + |
| 196 | WGET_TIMEOUT * wget_timeout_count, |
| 197 | wget_timeout_handler); |
| 198 | wget_send_stored(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | #define PKT_QUEUE_OFFSET 0x20000 |
| 203 | #define PKT_QUEUE_PACKET_SIZE 0x800 |
| 204 | |
Adriano Cordova | 47f35e3 | 2024-11-11 18:08:58 -0300 | [diff] [blame] | 205 | static void wget_fill_info(const uchar *pkt, int hlen) |
| 206 | { |
| 207 | const char *first_space; |
| 208 | const char *second_space; |
| 209 | char *pos, *end; |
| 210 | |
| 211 | if (wget_info->headers && hlen < MAX_HTTP_HEADERS_SIZE) |
| 212 | strncpy(wget_info->headers, pkt, hlen); |
| 213 | |
| 214 | //Get status code |
| 215 | first_space = strchr(pkt, ' '); |
| 216 | if (!first_space) { |
| 217 | wget_info->status_code = -1; |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | second_space = strchr(first_space + 1, ' '); |
| 222 | if (!second_space) { |
| 223 | wget_info->status_code = -1; |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | wget_info->status_code = (u32)simple_strtoul(first_space + 1, &end, 10); |
| 228 | |
| 229 | if (second_space != end) |
| 230 | wget_info->status_code = -1; |
| 231 | |
| 232 | pos = strstr((char *)pkt, content_len); |
| 233 | |
| 234 | if (pos) { |
| 235 | pos += sizeof(content_len) + 1; |
| 236 | while (*pos == ' ') |
| 237 | pos++; |
| 238 | content_length = simple_strtoul(pos, &end, 10); |
| 239 | debug_cond(DEBUG_WGET, |
| 240 | "wget: Connected Len %lu\n", |
| 241 | content_length); |
| 242 | wget_info->hdr_cont_len = content_length; |
| 243 | } |
| 244 | } |
| 245 | |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 246 | static void wget_connected(uchar *pkt, unsigned int tcp_seq_num, |
Dmitrii Merkurev | 3acd054 | 2023-04-12 19:49:29 +0100 | [diff] [blame] | 247 | u8 action, unsigned int tcp_ack_num, unsigned int len) |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 248 | { |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 249 | uchar *pkt_in_q; |
| 250 | char *pos; |
| 251 | int hlen, i; |
| 252 | uchar *ptr1; |
| 253 | |
| 254 | pkt[len] = '\0'; |
| 255 | pos = strstr((char *)pkt, http_eom); |
| 256 | |
| 257 | if (!pos) { |
| 258 | debug_cond(DEBUG_WGET, |
| 259 | "wget: Connected, data before Header %p\n", pkt); |
| 260 | pkt_in_q = (void *)image_load_addr + PKT_QUEUE_OFFSET + |
| 261 | (pkt_q_idx * PKT_QUEUE_PACKET_SIZE); |
| 262 | |
Yasuharu Shibata | afab268 | 2024-08-14 21:41:06 +0900 | [diff] [blame] | 263 | ptr1 = map_sysmem((ulong)pkt_in_q, len); |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 264 | memcpy(ptr1, pkt, len); |
| 265 | unmap_sysmem(ptr1); |
| 266 | |
| 267 | pkt_q[pkt_q_idx].pkt = pkt_in_q; |
| 268 | pkt_q[pkt_q_idx].tcp_seq_num = tcp_seq_num; |
| 269 | pkt_q[pkt_q_idx].len = len; |
| 270 | pkt_q_idx++; |
Richard Weinberger | bf44cf4 | 2023-07-20 14:51:56 +0200 | [diff] [blame] | 271 | |
| 272 | if (pkt_q_idx >= PKTQ_SZ) { |
| 273 | printf("wget: Fatal error, queue overrun!\n"); |
| 274 | net_set_state(NETLOOP_FAIL); |
| 275 | |
| 276 | return; |
| 277 | } |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 278 | } else { |
| 279 | debug_cond(DEBUG_WGET, "wget: Connected HTTP Header %p\n", pkt); |
| 280 | /* sizeof(http_eom) - 1 is the string length of (http_eom) */ |
| 281 | hlen = pos - (char *)pkt + sizeof(http_eom) - 1; |
| 282 | pos = strstr((char *)pkt, linefeed); |
| 283 | if (pos > 0) |
| 284 | i = pos - (char *)pkt; |
| 285 | else |
| 286 | i = hlen; |
| 287 | printf("%.*s", i, pkt); |
| 288 | |
| 289 | current_wget_state = WGET_TRANSFERRING; |
| 290 | |
Yasuharu Shibata | 07a00ef | 2024-04-14 19:46:07 +0900 | [diff] [blame] | 291 | initial_data_seq_num = tcp_seq_num + hlen; |
| 292 | next_data_seq_num = tcp_seq_num + len; |
| 293 | |
Adriano Cordova | 47f35e3 | 2024-11-11 18:08:58 -0300 | [diff] [blame] | 294 | wget_fill_info(pkt, hlen); |
| 295 | debug_cond(DEBUG_WGET, |
| 296 | "wget: HTTP Status Code %d\n", wget_info->status_code); |
| 297 | |
| 298 | if (wget_info->status_code != 200) { |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 299 | debug_cond(DEBUG_WGET, |
| 300 | "wget: Connected Bad Xfer\n"); |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 301 | wget_loop_state = NETLOOP_FAIL; |
| 302 | wget_send(action, tcp_seq_num, tcp_ack_num, len); |
| 303 | } else { |
| 304 | debug_cond(DEBUG_WGET, |
Heinrich Schuchardt | 6b3cefc | 2024-10-09 01:02:05 +0200 | [diff] [blame] | 305 | "wget: Connected Pkt %p hlen %x\n", |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 306 | pkt, hlen); |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 307 | |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 308 | net_boot_file_size = 0; |
| 309 | |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 310 | if (len > hlen) { |
| 311 | if (store_block(pkt + hlen, 0, len - hlen) != 0) { |
| 312 | wget_loop_state = NETLOOP_FAIL; |
| 313 | wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); |
| 314 | net_set_state(NETLOOP_FAIL); |
| 315 | return; |
| 316 | } |
| 317 | } |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 318 | |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 319 | for (i = 0; i < pkt_q_idx; i++) { |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 320 | int err; |
| 321 | |
Yasuharu Shibata | afab268 | 2024-08-14 21:41:06 +0900 | [diff] [blame] | 322 | ptr1 = map_sysmem((ulong)pkt_q[i].pkt, |
| 323 | pkt_q[i].len); |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 324 | err = store_block(ptr1, |
| 325 | pkt_q[i].tcp_seq_num - |
| 326 | initial_data_seq_num, |
| 327 | pkt_q[i].len); |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 328 | unmap_sysmem(ptr1); |
| 329 | debug_cond(DEBUG_WGET, |
Heinrich Schuchardt | 6b3cefc | 2024-10-09 01:02:05 +0200 | [diff] [blame] | 330 | "wget: Conncted pkt Q %p len %x\n", |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 331 | pkt_q[i].pkt, pkt_q[i].len); |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 332 | if (err) { |
| 333 | wget_loop_state = NETLOOP_FAIL; |
| 334 | wget_fail("wget: store error\n", tcp_seq_num, tcp_ack_num, action); |
| 335 | net_set_state(NETLOOP_FAIL); |
| 336 | return; |
| 337 | } |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |
| 341 | wget_send(action, tcp_seq_num, tcp_ack_num, len); |
| 342 | } |
| 343 | |
| 344 | /** |
Dmitrii Merkurev | 3acd054 | 2023-04-12 19:49:29 +0100 | [diff] [blame] | 345 | * wget_handler() - TCP handler of wget |
| 346 | * @pkt: pointer to the application packet |
| 347 | * @dport: destination TCP port |
| 348 | * @sip: source IP address |
| 349 | * @sport: source TCP port |
| 350 | * @tcp_seq_num: TCP sequential number |
| 351 | * @tcp_ack_num: TCP acknowledgment number |
| 352 | * @action: TCP action (SYN, ACK, FIN, etc) |
| 353 | * @len: packet length |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 354 | * |
| 355 | * In the "application push" invocation, the TCP header with all |
| 356 | * its information is pointed to by the packet pointer. |
| 357 | */ |
Dmitrii Merkurev | 3acd054 | 2023-04-12 19:49:29 +0100 | [diff] [blame] | 358 | static void wget_handler(uchar *pkt, u16 dport, |
| 359 | struct in_addr sip, u16 sport, |
| 360 | u32 tcp_seq_num, u32 tcp_ack_num, |
| 361 | u8 action, unsigned int len) |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 362 | { |
| 363 | enum tcp_state wget_tcp_state = tcp_get_tcp_state(); |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 364 | |
| 365 | net_set_timeout_handler(wget_timeout, wget_timeout_handler); |
| 366 | packets++; |
| 367 | |
| 368 | switch (current_wget_state) { |
| 369 | case WGET_CLOSED: |
| 370 | debug_cond(DEBUG_WGET, "wget: Handler: Error!, State wrong\n"); |
| 371 | break; |
| 372 | case WGET_CONNECTING: |
| 373 | debug_cond(DEBUG_WGET, |
Dmitrii Merkurev | 3acd054 | 2023-04-12 19:49:29 +0100 | [diff] [blame] | 374 | "wget: Connecting In len=%x, Seq=%u, Ack=%u\n", |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 375 | len, tcp_seq_num, tcp_ack_num); |
| 376 | if (!len) { |
| 377 | if (wget_tcp_state == TCP_ESTABLISHED) { |
| 378 | debug_cond(DEBUG_WGET, |
| 379 | "wget: Cting, send, len=%x\n", len); |
| 380 | wget_send(action, tcp_seq_num, tcp_ack_num, |
| 381 | len); |
| 382 | } else { |
| 383 | printf("%.*s", len, pkt); |
| 384 | wget_fail("wget: Handler Connected Fail\n", |
| 385 | tcp_seq_num, tcp_ack_num, action); |
| 386 | } |
| 387 | } |
| 388 | break; |
| 389 | case WGET_CONNECTED: |
Dmitrii Merkurev | 3acd054 | 2023-04-12 19:49:29 +0100 | [diff] [blame] | 390 | debug_cond(DEBUG_WGET, "wget: Connected seq=%u, len=%x\n", |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 391 | tcp_seq_num, len); |
| 392 | if (!len) { |
| 393 | wget_fail("Image not found, no data returned\n", |
| 394 | tcp_seq_num, tcp_ack_num, action); |
| 395 | } else { |
Dmitrii Merkurev | 3acd054 | 2023-04-12 19:49:29 +0100 | [diff] [blame] | 396 | wget_connected(pkt, tcp_seq_num, action, tcp_ack_num, len); |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 397 | } |
| 398 | break; |
| 399 | case WGET_TRANSFERRING: |
| 400 | debug_cond(DEBUG_WGET, |
| 401 | "wget: Transferring, seq=%x, ack=%x,len=%x\n", |
| 402 | tcp_seq_num, tcp_ack_num, len); |
| 403 | |
Yasuharu Shibata | 07a00ef | 2024-04-14 19:46:07 +0900 | [diff] [blame] | 404 | if (next_data_seq_num != tcp_seq_num) { |
| 405 | debug_cond(DEBUG_WGET, "wget: seq=%x packet was lost\n", next_data_seq_num); |
| 406 | return; |
| 407 | } |
| 408 | next_data_seq_num = tcp_seq_num + len; |
| 409 | |
Yasuharu Shibata | 2c9b5af | 2024-04-16 09:26:24 +0900 | [diff] [blame] | 410 | if (store_block(pkt, tcp_seq_num - initial_data_seq_num, len) != 0) { |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 411 | wget_fail("wget: store error\n", |
| 412 | tcp_seq_num, tcp_ack_num, action); |
Masahisa Kojima | 77b0ae3 | 2023-11-10 13:25:34 +0900 | [diff] [blame] | 413 | net_set_state(NETLOOP_FAIL); |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 414 | return; |
| 415 | } |
| 416 | |
| 417 | switch (wget_tcp_state) { |
| 418 | case TCP_FIN_WAIT_2: |
| 419 | wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, len); |
| 420 | fallthrough; |
| 421 | case TCP_SYN_SENT: |
Dmitrii Merkurev | 3acd054 | 2023-04-12 19:49:29 +0100 | [diff] [blame] | 422 | case TCP_SYN_RECEIVED: |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 423 | case TCP_CLOSING: |
| 424 | case TCP_FIN_WAIT_1: |
| 425 | case TCP_CLOSED: |
| 426 | net_set_state(NETLOOP_FAIL); |
| 427 | break; |
| 428 | case TCP_ESTABLISHED: |
| 429 | wget_send(TCP_ACK, tcp_seq_num, tcp_ack_num, |
| 430 | len); |
| 431 | wget_loop_state = NETLOOP_SUCCESS; |
| 432 | break; |
| 433 | case TCP_CLOSE_WAIT: /* End of transfer */ |
| 434 | current_wget_state = WGET_TRANSFERRED; |
| 435 | wget_send(action | TCP_ACK | TCP_FIN, |
| 436 | tcp_seq_num, tcp_ack_num, len); |
| 437 | break; |
| 438 | } |
| 439 | break; |
| 440 | case WGET_TRANSFERRED: |
| 441 | printf("Packets received %d, Transfer Successful\n", packets); |
| 442 | net_set_state(wget_loop_state); |
Adriano Cordova | 47f35e3 | 2024-11-11 18:08:58 -0300 | [diff] [blame] | 443 | wget_info->file_size = net_boot_file_size; |
| 444 | if (wget_info->method == WGET_HTTP_METHOD_GET && wget_info->set_bootdev) { |
| 445 | efi_set_bootdev("Net", "", image_url, |
| 446 | map_sysmem(image_load_addr, 0), |
| 447 | net_boot_file_size); |
| 448 | env_set_hex("filesize", net_boot_file_size); |
| 449 | } |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 450 | break; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | #define RANDOM_PORT_START 1024 |
| 455 | #define RANDOM_PORT_RANGE 0x4000 |
| 456 | |
| 457 | /** |
| 458 | * random_port() - make port a little random (1024-17407) |
| 459 | * |
| 460 | * Return: random port number from 1024 to 17407 |
| 461 | * |
| 462 | * This keeps the math somewhat trivial to compute, and seems to work with |
| 463 | * all supported protocols/clients/servers |
| 464 | */ |
| 465 | static unsigned int random_port(void) |
| 466 | { |
| 467 | return RANDOM_PORT_START + (get_timer(0) % RANDOM_PORT_RANGE); |
| 468 | } |
| 469 | |
| 470 | #define BLOCKSIZE 512 |
| 471 | |
| 472 | void wget_start(void) |
| 473 | { |
Adriano Cordova | 47f35e3 | 2024-11-11 18:08:58 -0300 | [diff] [blame] | 474 | if (!wget_info) |
| 475 | wget_info = &default_wget_info; |
| 476 | |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 477 | image_url = strchr(net_boot_file_name, ':'); |
| 478 | if (image_url > 0) { |
| 479 | web_server_ip = string_to_ip(net_boot_file_name); |
| 480 | ++image_url; |
| 481 | net_server_ip = web_server_ip; |
| 482 | } else { |
| 483 | web_server_ip = net_server_ip; |
| 484 | image_url = net_boot_file_name; |
| 485 | } |
| 486 | |
| 487 | debug_cond(DEBUG_WGET, |
| 488 | "wget: Transfer HTTP Server %pI4; our IP %pI4\n", |
| 489 | &web_server_ip, &net_ip); |
| 490 | |
| 491 | /* Check if we need to send across this subnet */ |
| 492 | if (net_gateway.s_addr && net_netmask.s_addr) { |
| 493 | struct in_addr our_net; |
| 494 | struct in_addr server_net; |
| 495 | |
| 496 | our_net.s_addr = net_ip.s_addr & net_netmask.s_addr; |
| 497 | server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr; |
| 498 | if (our_net.s_addr != server_net.s_addr) |
| 499 | debug_cond(DEBUG_WGET, |
| 500 | "wget: sending through gateway %pI4", |
| 501 | &net_gateway); |
| 502 | } |
| 503 | debug_cond(DEBUG_WGET, "URL '%s'\n", image_url); |
| 504 | |
| 505 | if (net_boot_file_expected_size_in_blocks) { |
| 506 | debug_cond(DEBUG_WGET, "wget: Size is 0x%x Bytes = ", |
| 507 | net_boot_file_expected_size_in_blocks * BLOCKSIZE); |
| 508 | print_size(net_boot_file_expected_size_in_blocks * BLOCKSIZE, |
| 509 | ""); |
| 510 | } |
| 511 | debug_cond(DEBUG_WGET, |
| 512 | "\nwget:Load address: 0x%lx\nLoading: *\b", image_load_addr); |
| 513 | |
Ying-Chun Liu (PaulLiu) | cc96a1d | 2022-11-08 14:17:29 +0800 | [diff] [blame] | 514 | net_set_timeout_handler(wget_timeout, wget_timeout_handler); |
| 515 | tcp_set_tcp_handler(wget_handler); |
| 516 | |
| 517 | wget_timeout_count = 0; |
| 518 | current_wget_state = WGET_CLOSED; |
| 519 | |
| 520 | our_port = random_port(); |
| 521 | |
| 522 | /* |
| 523 | * Zero out server ether to force arp resolution in case |
| 524 | * the server ip for the previous u-boot command, for example dns |
| 525 | * is not the same as the web server ip. |
| 526 | */ |
| 527 | |
| 528 | memset(net_server_ethaddr, 0, 6); |
| 529 | |
| 530 | wget_send(TCP_SYN, 0, 0, 0); |
| 531 | } |
Masahisa Kojima | 6721d18 | 2023-11-10 13:25:35 +0900 | [diff] [blame] | 532 | |
| 533 | #if (IS_ENABLED(CONFIG_CMD_DNS)) |
| 534 | int wget_with_dns(ulong dst_addr, char *uri) |
| 535 | { |
| 536 | int ret; |
| 537 | char *s, *host_name, *file_name, *str_copy; |
| 538 | |
| 539 | /* |
| 540 | * Download file using wget. |
| 541 | * |
| 542 | * U-Boot wget takes the target uri in this format. |
| 543 | * "<http server ip>:<file path>" e.g.) 192.168.1.1:/sample/test.iso |
| 544 | * Need to resolve the http server ip address before starting wget. |
| 545 | */ |
| 546 | str_copy = strdup(uri); |
| 547 | if (!str_copy) |
| 548 | return -ENOMEM; |
| 549 | |
| 550 | s = str_copy + strlen("http://"); |
| 551 | host_name = strsep(&s, "/"); |
| 552 | if (!s) { |
| 553 | log_err("Error: invalied uri, no file path\n"); |
| 554 | ret = -EINVAL; |
| 555 | goto out; |
| 556 | } |
| 557 | file_name = s; |
| 558 | |
| 559 | /* TODO: If the given uri has ip address for the http server, skip dns */ |
| 560 | net_dns_resolve = host_name; |
| 561 | net_dns_env_var = "httpserverip"; |
| 562 | if (net_loop(DNS) < 0) { |
| 563 | log_err("Error: dns lookup of %s failed, check setup\n", net_dns_resolve); |
| 564 | ret = -EINVAL; |
| 565 | goto out; |
| 566 | } |
| 567 | s = env_get("httpserverip"); |
| 568 | if (!s) { |
| 569 | ret = -EINVAL; |
| 570 | goto out; |
| 571 | } |
| 572 | |
| 573 | strlcpy(net_boot_file_name, s, sizeof(net_boot_file_name)); |
| 574 | strlcat(net_boot_file_name, ":/", sizeof(net_boot_file_name)); /* append '/' which is removed by strsep() */ |
| 575 | strlcat(net_boot_file_name, file_name, sizeof(net_boot_file_name)); |
| 576 | image_load_addr = dst_addr; |
| 577 | ret = net_loop(WGET); |
| 578 | |
| 579 | out: |
| 580 | free(str_copy); |
| 581 | |
Adriano Cordova | 450b234 | 2024-11-11 18:08:59 -0300 | [diff] [blame^] | 582 | return ret < 0 ? ret : 0; |
Masahisa Kojima | 6721d18 | 2023-11-10 13:25:35 +0900 | [diff] [blame] | 583 | } |
| 584 | #endif |
Masahisa Kojima | e501ce1 | 2023-11-10 13:25:41 +0900 | [diff] [blame] | 585 | |
| 586 | /** |
| 587 | * wget_validate_uri() - validate the uri for wget |
| 588 | * |
| 589 | * @uri: uri string |
| 590 | * |
| 591 | * This function follows the current U-Boot wget implementation. |
| 592 | * scheme: only "http:" is supported |
| 593 | * authority: |
| 594 | * - user information: not supported |
| 595 | * - host: supported |
| 596 | * - port: not supported(always use the default port) |
| 597 | * |
| 598 | * Uri is expected to be correctly percent encoded. |
| 599 | * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0') |
| 600 | * and space character(0x20) are not allowed. |
| 601 | * |
| 602 | * TODO: stricter uri conformance check |
| 603 | * |
| 604 | * Return: true on success, false on failure |
| 605 | */ |
| 606 | bool wget_validate_uri(char *uri) |
| 607 | { |
| 608 | char c; |
| 609 | bool ret = true; |
| 610 | char *str_copy, *s, *authority; |
| 611 | |
| 612 | for (c = 0x1; c < 0x21; c++) { |
| 613 | if (strchr(uri, c)) { |
| 614 | log_err("invalid character is used\n"); |
| 615 | return false; |
| 616 | } |
| 617 | } |
| 618 | if (strchr(uri, 0x7f)) { |
| 619 | log_err("invalid character is used\n"); |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | if (strncmp(uri, "http://", 7)) { |
| 624 | log_err("only http:// is supported\n"); |
| 625 | return false; |
| 626 | } |
| 627 | str_copy = strdup(uri); |
| 628 | if (!str_copy) |
| 629 | return false; |
| 630 | |
| 631 | s = str_copy + strlen("http://"); |
| 632 | authority = strsep(&s, "/"); |
| 633 | if (!s) { |
| 634 | log_err("invalid uri, no file path\n"); |
| 635 | ret = false; |
| 636 | goto out; |
| 637 | } |
| 638 | s = strchr(authority, '@'); |
| 639 | if (s) { |
| 640 | log_err("user information is not supported\n"); |
| 641 | ret = false; |
| 642 | goto out; |
| 643 | } |
| 644 | s = strchr(authority, ':'); |
| 645 | if (s) { |
| 646 | log_err("user defined port is not supported\n"); |
| 647 | ret = false; |
| 648 | goto out; |
| 649 | } |
| 650 | |
| 651 | out: |
| 652 | free(str_copy); |
| 653 | |
| 654 | return ret; |
| 655 | } |