Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | // SPDX-License-Identifier: GPL-2.0-or-later /* * An HTTP driver * * HTTP_PROTOCOL * HTTP_SERVICE_BINDING_PROTOCOL * IP4_CONFIG2_PROTOCOL */ #define LOG_CATEGORY LOGC_EFI #include <charset.h> #include <efi_loader.h> #include <image.h> #include <malloc.h> #include <mapmem.h> #include <net.h> static const efi_guid_t efi_http_service_binding_guid = EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID; static const efi_guid_t efi_http_guid = EFI_HTTP_PROTOCOL_GUID; /** * struct efi_http_instance - EFI object representing an HTTP protocol instance * * @http: EFI_HTTP_PROTOCOL interface * @handle: handle to efi object * @configured: configuration status * @http_load_addr: data buffer * @file_size: size of data * @current_offset: offset in data buffer * @status_code: HTTP status code * @num_headers: number of received headers * @headers: array of headers * @headers_buffer: raw buffer with headers */ struct efi_http_instance { struct efi_http_protocol http; efi_handle_t handle; struct efi_service_binding_protocol *parent; bool configured; void *http_load_addr; ulong file_size; ulong current_offset; u32 status_code; ulong num_headers; struct http_header headers[MAX_HTTP_HEADERS]; char headers_buffer[MAX_HTTP_HEADERS_SIZE]; }; static int num_instances; /* * efi_u32_to_httpstatus() - convert u32 to status * */ enum efi_http_status_code efi_u32_to_httpstatus(u32 status); /* * efi_http_send_data() - sends data to client * * * @client_buffer: client buffer to send data to * @client_buffer_size: size of the client buffer * @inst: HTTP instance for which to send data * * Return: status code */ static efi_status_t efi_http_send_data(void *client_buffer, efi_uintn_t *client_buffer_size, struct efi_http_instance *inst) { efi_status_t ret = EFI_SUCCESS; ulong total_size, transfer_size; uchar *ptr; // Amount of data left; total_size = inst->file_size; transfer_size = total_size - inst->current_offset; debug("efi_http: sending data to client, total size %lu\n", total_size); // Amount of data the client is willing to receive if (transfer_size > *client_buffer_size) transfer_size = *client_buffer_size; else *client_buffer_size = transfer_size; debug("efi_http: transfer size %lu\n", transfer_size); if (!transfer_size) // Ok, only headers goto out; if (!client_buffer) { ret = EFI_INVALID_PARAMETER; goto out; } // Send data ptr = (uchar *)inst->http_load_addr + inst->current_offset; memcpy(client_buffer, ptr, transfer_size); inst->current_offset += transfer_size; // Whole file served, clean the buffer: if (inst->current_offset == inst->file_size) { efi_free_pool(inst->http_load_addr); inst->http_load_addr = NULL; inst->current_offset = 0; inst->file_size = 0; } out: return ret; } /* EFI_HTTP_PROTOCOL */ /* * efi_http_get_mode_data() - Gets the current operational status. * * This function implements EFI_HTTP_PROTOCOL.GetModeData(). * See the Unified Extensible Firmware Interface * (UEFI) specification for details. * * @this: pointer to the protocol instance * @data: pointer to the buffer for operational parameters * of this HTTP instance * Return: status code */ static efi_status_t EFIAPI efi_http_get_mode_data(struct efi_http_protocol *this, struct efi_http_config_data *data) { EFI_ENTRY("%p, %p", this, data); efi_status_t ret = EFI_UNSUPPORTED; return EFI_EXIT(ret); } /* * efi_http_configure() - Initializes operational status for this * EFI HTTP instance. * * This function implements EFI_HTTP_PROTOCOL.Configure(). * See the Unified Extensible Firmware Interface * (UEFI) specification for details. * * @this: pointer to the protocol instance * @data: pointer to the buffer for operational parameters of * this HTTP instance * Return: status code */ static efi_status_t EFIAPI efi_http_configure(struct efi_http_protocol *this, struct efi_http_config_data *data) { EFI_ENTRY("%p, %p", this, data); efi_status_t ret = EFI_SUCCESS; enum efi_http_version http_version; struct efi_httpv4_access_point *ipv4_node; struct efi_http_instance *http_instance; if (!this) { ret = EFI_INVALID_PARAMETER; goto out; } http_instance = (struct efi_http_instance *)this; if (!data) { efi_free_pool(http_instance->http_load_addr); http_instance->http_load_addr = NULL; http_instance->current_offset = 0; http_instance->configured = false; goto out; } if (http_instance->configured) { ret = EFI_ALREADY_STARTED; goto out; } http_version = data->http_version; ipv4_node = data->access_point.ipv4_node; if ((http_version != HTTPVERSION10 && http_version != HTTPVERSION11) || data->is_ipv6 || !ipv4_node) { /* Only support ipv4 */ ret = EFI_UNSUPPORTED; goto out; } if (!ipv4_node->use_default_address) { efi_net_set_addr((struct efi_ipv4_address *)&ipv4_node->local_address, (struct efi_ipv4_address *)&ipv4_node->local_subnet, NULL, NULL); } http_instance->current_offset = 0; http_instance->configured = true; out: return EFI_EXIT(ret); } /* * efi_http_request() - Queues an HTTP request to this HTTP instance * * This function implements EFI_HTTP_PROTOCOL.Request(). * See the Unified Extensible Firmware Interface * (UEFI) specification for details. * * @this: pointer to the protocol instance * @token: pointer to storage containing HTTP request token * Return: status code */ static efi_status_t EFIAPI efi_http_request(struct efi_http_protocol *this, struct efi_http_token *token) { EFI_ENTRY("%p, %p", this, token); efi_status_t ret = EFI_SUCCESS; u8 *tmp; u8 url_8[1024]; u16 *url_16; enum efi_http_method current_method; struct efi_http_instance *http_instance; if (!token || !this || !token->message || !token->message->data.request) { ret = EFI_INVALID_PARAMETER; goto out; } http_instance = (struct efi_http_instance *)this; if (!http_instance->configured) { ret = EFI_NOT_STARTED; goto out; } current_method = token->message->data.request->method; url_16 = token->message->data.request->url; /* Parse URL. It comes in UCS-2 encoding and follows RFC3986 */ tmp = url_8; utf16_utf8_strncpy((char **)&tmp, url_16, 1024); ret = efi_net_do_request(url_8, current_method, &http_instance->http_load_addr, &http_instance->status_code, &http_instance->file_size, http_instance->headers_buffer, http_instance->parent); if (ret != EFI_SUCCESS) goto out; // We have a successful request efi_net_parse_headers(&http_instance->num_headers, http_instance->headers); http_instance->current_offset = 0; token->status = EFI_SUCCESS; goto out_signal; out_signal: efi_signal_event(token->event); out: return EFI_EXIT(ret); } /* * efi_http_cancel() - Abort an asynchronous HTTP request or response token * * This function implements EFI_HTTP_PROTOCOL.Cancel(). * See the Unified Extensible Firmware Interface * (UEFI) specification for details. * * @this: pointer to the protocol instance * @token: pointer to storage containing HTTP request token * Return: status code */ static efi_status_t EFIAPI efi_http_cancel(struct efi_http_protocol *this, struct efi_http_token *token) { EFI_ENTRY("%p, %p", this, token); efi_status_t ret = EFI_UNSUPPORTED; return EFI_EXIT(ret); } /* * efi_http_response() - Queues an HTTP response to this HTTP instance * * This function implements EFI_HTTP_PROTOCOL.Response(). * See the Unified Extensible Firmware Interface * (UEFI) specification for details. * * @this: pointer to the protocol instance * @token: pointer to storage containing HTTP request token * Return: status code */ static efi_status_t EFIAPI efi_http_response(struct efi_http_protocol *this, struct efi_http_token *token) { EFI_ENTRY("%p, %p", this, token); efi_status_t ret = EFI_SUCCESS; struct efi_http_instance *http_instance; struct efi_http_header **client_headers; struct efi_http_response_data *response; if (!token || !this || !token->message) { ret = EFI_INVALID_PARAMETER; goto out; } http_instance = (struct efi_http_instance *)this; // Set HTTP status code if (token->message->data.response) { // TODO extra check, see spec. response = token->message->data.response; response->status_code = efi_u32_to_httpstatus(http_instance->status_code); } client_headers = &token->message->headers; ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, (http_instance->num_headers) * sizeof(struct efi_http_header), (void **)client_headers); // This is deallocated by the client. if (ret != EFI_SUCCESS) goto out_bad_signal; // Send headers token->message->header_count = http_instance->num_headers; for (int i = 0; i < http_instance->num_headers; i++) { (*client_headers)[i].field_name = http_instance->headers[i].name; (*client_headers)[i].field_value = http_instance->headers[i].value; } ret = efi_http_send_data(token->message->body, &token->message->body_length, http_instance); if (ret != EFI_SUCCESS) goto out_bad_signal; token->status = EFI_SUCCESS; goto out_signal; out_bad_signal: token->status = EFI_ABORTED; out_signal: efi_signal_event(token->event); out: return EFI_EXIT(ret); } /* * efi_http_poll() - Polls for incoming data packets and processes outgoing data packets * * This function implements EFI_HTTP_PROTOCOL.Poll(). * See the Unified Extensible Firmware Interface * (UEFI) specification for details. * * @this: pointer to the protocol instance * @token: pointer to storage containing HTTP request token * Return: status code */ static efi_status_t EFIAPI efi_http_poll(struct efi_http_protocol *this) { EFI_ENTRY("%p", this); efi_status_t ret = EFI_UNSUPPORTED; return EFI_EXIT(ret); } /* EFI_HTTP_SERVICE_BINDING_PROTOCOL */ /* * efi_http_service_binding_create_child() - Creates a child handle * and installs a protocol * * This function implements EFI_HTTP_SERVICE_BINDING.CreateChild(). * See the Unified Extensible Firmware Interface * (UEFI) specification for details. * * @this: pointer to the protocol instance * @child_handle: pointer to child handle * Return: status code */ static efi_status_t EFIAPI efi_http_service_binding_create_child( struct efi_service_binding_protocol *this, efi_handle_t *child_handle) { EFI_ENTRY("%p, %p", this, child_handle); efi_status_t ret = EFI_SUCCESS; struct efi_http_instance *new_instance; if (!child_handle) return EFI_EXIT(EFI_INVALID_PARAMETER); new_instance = calloc(1, sizeof(struct efi_http_instance)); if (!new_instance) { ret = EFI_OUT_OF_RESOURCES; goto failure_to_add_protocol; } if (*child_handle) { new_instance->handle = *child_handle; goto install; } new_instance->handle = calloc(1, sizeof(struct efi_object)); if (!new_instance->handle) { efi_free_pool((void *)new_instance); ret = EFI_OUT_OF_RESOURCES; goto failure_to_add_protocol; } new_instance->parent = this; efi_add_handle(new_instance->handle); *child_handle = new_instance->handle; install: ret = efi_add_protocol(new_instance->handle, &efi_http_guid, &new_instance->http); if (ret != EFI_SUCCESS) goto failure_to_add_protocol; new_instance->http.get_mode_data = efi_http_get_mode_data; new_instance->http.configure = efi_http_configure; new_instance->http.request = efi_http_request; new_instance->http.cancel = efi_http_cancel; new_instance->http.response = efi_http_response; new_instance->http.poll = efi_http_poll; ++num_instances; return EFI_EXIT(EFI_SUCCESS); failure_to_add_protocol: return EFI_EXIT(ret); } /* * efi_http_service_binding_destroy_child() - Destroys a child handle with * a protocol installed on it * * This function implements EFI_HTTP_SERVICE_BINDING.DestroyChild(). * See the Unified Extensible Firmware Interface * (UEFI) specification for details. * * @this: pointer to the protocol instance * @child_handle: child handle * Return: status code */ static efi_status_t EFIAPI efi_http_service_binding_destroy_child( struct efi_service_binding_protocol *this, efi_handle_t child_handle) { EFI_ENTRY("%p, %p", this, child_handle); efi_status_t ret = EFI_SUCCESS; struct efi_http_instance *http_instance; struct efi_handler *phandler; if (num_instances == 0) return EFI_EXIT(EFI_NOT_FOUND); if (!child_handle) return EFI_EXIT(EFI_INVALID_PARAMETER); ret = efi_search_protocol(child_handle, &efi_http_guid, &phandler); if (ret != EFI_SUCCESS) { if (ret != EFI_INVALID_PARAMETER) ret = EFI_UNSUPPORTED; goto out; } ret = efi_delete_handle(child_handle); if (ret != EFI_SUCCESS) goto out; http_instance = phandler->protocol_interface; efi_free_pool(http_instance->http_load_addr); http_instance->http_load_addr = NULL; free(phandler->protocol_interface); num_instances--; out: return EFI_EXIT(ret); } /** * efi_http_register() - register the http protocol * */ efi_status_t efi_http_register(const efi_handle_t handle, struct efi_service_binding_protocol *http_service_binding) { efi_status_t r = EFI_SUCCESS; r = efi_add_protocol(handle, &efi_http_service_binding_guid, http_service_binding); if (r != EFI_SUCCESS) goto failure_to_add_protocol; http_service_binding->create_child = efi_http_service_binding_create_child; http_service_binding->destroy_child = efi_http_service_binding_destroy_child; return EFI_SUCCESS; failure_to_add_protocol: return r; } enum efi_http_status_code efi_u32_to_httpstatus(u32 status) { switch (status) { case 100: return HTTP_STATUS_100_CONTINUE; case 101: return HTTP_STATUS_101_SWITCHING_PROTOCOLS; case 200: return HTTP_STATUS_200_OK; case 201: return HTTP_STATUS_201_CREATED; case 202: return HTTP_STATUS_202_ACCEPTED; case 203: return HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION; case 204: return HTTP_STATUS_204_NO_CONTENT; case 205: return HTTP_STATUS_205_RESET_CONTENT; case 206: return HTTP_STATUS_206_PARTIAL_CONTENT; case 300: return HTTP_STATUS_300_MULTIPLE_CHOICES; case 301: return HTTP_STATUS_301_MOVED_PERMANENTLY; case 302: return HTTP_STATUS_302_FOUND; case 303: return HTTP_STATUS_303_SEE_OTHER; case 304: return HTTP_STATUS_304_NOT_MODIFIED; case 305: return HTTP_STATUS_305_USE_PROXY; case 307: return HTTP_STATUS_307_TEMPORARY_REDIRECT; case 400: return HTTP_STATUS_400_BAD_REQUEST; case 401: return HTTP_STATUS_401_UNAUTHORIZED; case 402: return HTTP_STATUS_402_PAYMENT_REQUIRED; case 403: return HTTP_STATUS_403_FORBIDDEN; case 404: return HTTP_STATUS_404_NOT_FOUND; case 405: return HTTP_STATUS_405_METHOD_NOT_ALLOWED; case 406: return HTTP_STATUS_406_NOT_ACCEPTABLE; case 407: return HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED; case 408: return HTTP_STATUS_408_REQUEST_TIME_OUT; case 409: return HTTP_STATUS_409_CONFLICT; case 410: return HTTP_STATUS_410_GONE; case 411: return HTTP_STATUS_411_LENGTH_REQUIRED; case 412: return HTTP_STATUS_412_PRECONDITION_FAILED; case 413: return HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE; case 414: return HTTP_STATUS_414_REQUEST_URI_TOO_LARGE; case 415: return HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE; case 416: return HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED; case 417: return HTTP_STATUS_417_EXPECTATION_FAILED; case 500: return HTTP_STATUS_500_INTERNAL_SERVER_ERROR; case 501: return HTTP_STATUS_501_NOT_IMPLEMENTED; case 502: return HTTP_STATUS_502_BAD_GATEWAY; case 503: return HTTP_STATUS_503_SERVICE_UNAVAILABLE; case 504: return HTTP_STATUS_504_GATEWAY_TIME_OUT; case 505: return HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED; case 308: return HTTP_STATUS_308_PERMANENT_REDIRECT; default: return HTTP_STATUS_UNSUPPORTED_STATUS; } } |