Jerome Forissier | 359d4ed | 2024-10-16 12:04:09 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* Copyright (C) 2024 Linaro Ltd. */ |
| 3 | |
| 4 | #include <command.h> |
| 5 | #include <console.h> |
| 6 | #include <display_options.h> |
| 7 | #include <efi_loader.h> |
| 8 | #include <image.h> |
| 9 | #include <lwip/apps/http_client.h> |
| 10 | #include <lwip/timeouts.h> |
| 11 | #include <mapmem.h> |
| 12 | #include <net.h> |
| 13 | #include <time.h> |
| 14 | |
| 15 | #define SERVER_NAME_SIZE 200 |
| 16 | #define HTTP_PORT_DEFAULT 80 |
| 17 | #define PROGRESS_PRINT_STEP_BYTES (100 * 1024) |
| 18 | |
| 19 | enum done_state { |
| 20 | NOT_DONE = 0, |
| 21 | SUCCESS = 1, |
| 22 | FAILURE = 2 |
| 23 | }; |
| 24 | |
| 25 | struct wget_ctx { |
| 26 | char *path; |
| 27 | ulong daddr; |
| 28 | ulong saved_daddr; |
| 29 | ulong size; |
| 30 | ulong prevsize; |
| 31 | ulong start_time; |
| 32 | enum done_state done; |
| 33 | }; |
| 34 | |
| 35 | static int parse_url(char *url, char *host, u16 *port, char **path) |
| 36 | { |
| 37 | char *p, *pp; |
| 38 | long lport; |
| 39 | |
| 40 | p = strstr(url, "http://"); |
| 41 | if (!p) { |
| 42 | log_err("only http:// is supported\n"); |
| 43 | return -EINVAL; |
| 44 | } |
| 45 | |
| 46 | p += strlen("http://"); |
| 47 | |
| 48 | /* Parse hostname */ |
| 49 | pp = strchr(p, ':'); |
| 50 | if (!pp) |
| 51 | pp = strchr(p, '/'); |
| 52 | if (!pp) |
| 53 | return -EINVAL; |
| 54 | |
| 55 | if (p + SERVER_NAME_SIZE <= pp) |
| 56 | return -EINVAL; |
| 57 | |
| 58 | memcpy(host, p, pp - p); |
| 59 | host[pp - p] = '\0'; |
| 60 | |
| 61 | if (*pp == ':') { |
| 62 | /* Parse port number */ |
| 63 | p = pp + 1; |
| 64 | lport = simple_strtol(p, &pp, 10); |
| 65 | if (pp && *pp != '/') |
| 66 | return -EINVAL; |
| 67 | if (lport > 65535) |
| 68 | return -EINVAL; |
| 69 | *port = (u16)lport; |
| 70 | } else { |
| 71 | *port = HTTP_PORT_DEFAULT; |
| 72 | } |
| 73 | if (*pp != '/') |
| 74 | return -EINVAL; |
| 75 | *path = pp; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Legacy syntax support |
| 82 | * Convert [<server_name_or_ip>:]filename into a URL if needed |
| 83 | */ |
| 84 | static int parse_legacy_arg(char *arg, char *nurl, size_t rem) |
| 85 | { |
| 86 | char *p = nurl; |
| 87 | size_t n; |
| 88 | char *col = strchr(arg, ':'); |
| 89 | char *env; |
| 90 | char *server; |
| 91 | char *path; |
| 92 | |
| 93 | if (strstr(arg, "http") == arg) { |
| 94 | n = snprintf(nurl, rem, "%s", arg); |
| 95 | if (n < 0 || n > rem) |
| 96 | return -1; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | n = snprintf(p, rem, "%s", "http://"); |
| 101 | if (n < 0 || n > rem) |
| 102 | return -1; |
| 103 | p += n; |
| 104 | rem -= n; |
| 105 | |
| 106 | if (col) { |
| 107 | n = col - arg; |
| 108 | server = arg; |
| 109 | path = col + 1; |
| 110 | } else { |
| 111 | env = env_get("httpserverip"); |
| 112 | if (!env) |
| 113 | env = env_get("serverip"); |
| 114 | if (!env) { |
| 115 | log_err("error: httpserver/serverip has to be set\n"); |
| 116 | return -1; |
| 117 | } |
| 118 | n = strlen(env); |
| 119 | server = env; |
| 120 | path = arg; |
| 121 | } |
| 122 | |
| 123 | if (rem < n) |
| 124 | return -1; |
| 125 | strncpy(p, server, n); |
| 126 | p += n; |
| 127 | rem -= n; |
| 128 | if (rem < 1) |
| 129 | return -1; |
| 130 | *p = '/'; |
| 131 | p++; |
| 132 | rem--; |
| 133 | n = strlen(path); |
| 134 | if (rem < n) |
| 135 | return -1; |
| 136 | strncpy(p, path, n); |
| 137 | p += n; |
| 138 | rem -= n; |
| 139 | if (rem < 1) |
| 140 | return -1; |
| 141 | *p = '\0'; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static err_t httpc_recv_cb(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf, |
| 147 | err_t err) |
| 148 | { |
| 149 | struct wget_ctx *ctx = arg; |
| 150 | struct pbuf *buf; |
| 151 | |
| 152 | if (!pbuf) |
| 153 | return ERR_BUF; |
| 154 | |
| 155 | if (!ctx->start_time) |
| 156 | ctx->start_time = get_timer(0); |
| 157 | |
| 158 | for (buf = pbuf; buf; buf = buf->next) { |
| 159 | memcpy((void *)ctx->daddr, buf->payload, buf->len); |
| 160 | ctx->daddr += buf->len; |
| 161 | ctx->size += buf->len; |
| 162 | if (ctx->size - ctx->prevsize > PROGRESS_PRINT_STEP_BYTES) { |
| 163 | printf("#"); |
| 164 | ctx->prevsize = ctx->size; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | altcp_recved(pcb, pbuf->tot_len); |
| 169 | pbuf_free(pbuf); |
| 170 | return ERR_OK; |
| 171 | } |
| 172 | |
| 173 | static void httpc_result_cb(void *arg, httpc_result_t httpc_result, |
| 174 | u32_t rx_content_len, u32_t srv_res, err_t err) |
| 175 | { |
| 176 | struct wget_ctx *ctx = arg; |
| 177 | ulong elapsed; |
| 178 | |
| 179 | if (httpc_result != HTTPC_RESULT_OK) { |
| 180 | log_err("\nHTTP client error %d\n", httpc_result); |
| 181 | ctx->done = FAILURE; |
| 182 | return; |
| 183 | } |
| 184 | if (srv_res != 200) { |
| 185 | log_err("\nHTTP server error %d\n", srv_res); |
| 186 | ctx->done = FAILURE; |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | elapsed = get_timer(ctx->start_time); |
| 191 | if (!elapsed) |
| 192 | elapsed = 1; |
| 193 | if (rx_content_len > PROGRESS_PRINT_STEP_BYTES) |
| 194 | printf("\n"); |
| 195 | printf("%u bytes transferred in %lu ms (", rx_content_len, elapsed); |
| 196 | print_size(rx_content_len / elapsed * 1000, "/s)\n"); |
| 197 | printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size); |
| 198 | efi_set_bootdev("Net", "", ctx->path, map_sysmem(ctx->saved_daddr, 0), |
| 199 | rx_content_len); |
| 200 | if (env_set_hex("filesize", rx_content_len) || |
| 201 | env_set_hex("fileaddr", ctx->saved_daddr)) { |
| 202 | log_err("Could not set filesize or fileaddr\n"); |
| 203 | ctx->done = FAILURE; |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | ctx->done = SUCCESS; |
| 208 | } |
| 209 | |
| 210 | static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) |
| 211 | { |
| 212 | char server_name[SERVER_NAME_SIZE]; |
| 213 | httpc_connection_t conn; |
| 214 | httpc_state_t *state; |
| 215 | struct netif *netif; |
| 216 | struct wget_ctx ctx; |
| 217 | char *path; |
| 218 | u16 port; |
| 219 | |
| 220 | ctx.daddr = dst_addr; |
| 221 | ctx.saved_daddr = dst_addr; |
| 222 | ctx.done = NOT_DONE; |
| 223 | ctx.size = 0; |
| 224 | ctx.prevsize = 0; |
| 225 | ctx.start_time = 0; |
| 226 | |
| 227 | if (parse_url(uri, server_name, &port, &path)) |
| 228 | return CMD_RET_USAGE; |
| 229 | |
| 230 | netif = net_lwip_new_netif(udev); |
| 231 | if (!netif) |
| 232 | return -1; |
| 233 | |
| 234 | memset(&conn, 0, sizeof(conn)); |
| 235 | conn.result_fn = httpc_result_cb; |
| 236 | ctx.path = path; |
| 237 | if (httpc_get_file_dns(server_name, port, path, &conn, httpc_recv_cb, |
| 238 | &ctx, &state)) { |
| 239 | net_lwip_remove_netif(netif); |
| 240 | return CMD_RET_FAILURE; |
| 241 | } |
| 242 | |
| 243 | while (!ctx.done) { |
| 244 | net_lwip_rx(udev, netif); |
| 245 | sys_check_timeouts(); |
| 246 | if (ctrlc()) |
| 247 | break; |
| 248 | } |
| 249 | |
| 250 | net_lwip_remove_netif(netif); |
| 251 | |
| 252 | if (ctx.done == SUCCESS) |
| 253 | return 0; |
| 254 | |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | int wget_with_dns(ulong dst_addr, char *uri) |
| 259 | { |
| 260 | eth_set_current(); |
| 261 | |
| 262 | return wget_loop(eth_get_dev(), dst_addr, uri); |
| 263 | } |
| 264 | |
| 265 | int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) |
| 266 | { |
| 267 | char *end; |
| 268 | char *url; |
| 269 | ulong dst_addr; |
| 270 | char nurl[1024]; |
| 271 | |
| 272 | if (argc < 2 || argc > 3) |
| 273 | return CMD_RET_USAGE; |
| 274 | |
| 275 | dst_addr = hextoul(argv[1], &end); |
| 276 | if (end == (argv[1] + strlen(argv[1]))) { |
| 277 | if (argc < 3) |
| 278 | return CMD_RET_USAGE; |
| 279 | url = argv[2]; |
| 280 | } else { |
| 281 | dst_addr = image_load_addr; |
| 282 | url = argv[1]; |
| 283 | } |
| 284 | |
| 285 | if (parse_legacy_arg(url, nurl, sizeof(nurl))) |
| 286 | return CMD_RET_FAILURE; |
| 287 | |
| 288 | if (wget_with_dns(dst_addr, nurl)) |
| 289 | return CMD_RET_FAILURE; |
| 290 | |
| 291 | return CMD_RET_SUCCESS; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * wget_validate_uri() - validate the uri for wget |
| 296 | * |
| 297 | * @uri: uri string |
| 298 | * |
| 299 | * This function follows the current U-Boot wget implementation. |
| 300 | * scheme: only "http:" is supported |
| 301 | * authority: |
| 302 | * - user information: not supported |
| 303 | * - host: supported |
| 304 | * - port: not supported(always use the default port) |
| 305 | * |
| 306 | * Uri is expected to be correctly percent encoded. |
| 307 | * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0') |
| 308 | * and space character(0x20) are not allowed. |
| 309 | * |
| 310 | * TODO: stricter uri conformance check |
| 311 | * |
| 312 | * Return: true on success, false on failure |
| 313 | */ |
| 314 | bool wget_validate_uri(char *uri) |
| 315 | { |
| 316 | char c; |
| 317 | bool ret = true; |
| 318 | char *str_copy, *s, *authority; |
| 319 | |
| 320 | for (c = 0x1; c < 0x21; c++) { |
| 321 | if (strchr(uri, c)) { |
| 322 | log_err("invalid character is used\n"); |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | if (strchr(uri, 0x7f)) { |
| 327 | log_err("invalid character is used\n"); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | if (strncmp(uri, "http://", 7)) { |
| 332 | log_err("only http:// is supported\n"); |
| 333 | return false; |
| 334 | } |
| 335 | str_copy = strdup(uri); |
| 336 | if (!str_copy) |
| 337 | return false; |
| 338 | |
| 339 | s = str_copy + strlen("http://"); |
| 340 | authority = strsep(&s, "/"); |
| 341 | if (!s) { |
| 342 | log_err("invalid uri, no file path\n"); |
| 343 | ret = false; |
| 344 | goto out; |
| 345 | } |
| 346 | s = strchr(authority, '@'); |
| 347 | if (s) { |
| 348 | log_err("user information is not supported\n"); |
| 349 | ret = false; |
| 350 | goto out; |
| 351 | } |
| 352 | |
| 353 | out: |
| 354 | free(str_copy); |
| 355 | |
| 356 | return ret; |
| 357 | } |