2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Intel Corporation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
39 #include <bootstrap.h>
45 #include <Protocol/Http.h>
46 #include <Protocol/Ip4Config2.h>
47 #include <Protocol/ServiceBinding.h>
49 /* Poll timeout in milliseconds */
50 static const int EFIHTTP_POLL_TIMEOUT = 300000;
52 static EFI_GUID http_guid = EFI_HTTP_PROTOCOL_GUID;
53 static EFI_GUID httpsb_guid = EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID;
54 static EFI_GUID ip4config2_guid = EFI_IP4_CONFIG2_PROTOCOL_GUID;
56 static bool efihttp_init_done = false;
58 static int efihttp_dev_init(void);
59 static int efihttp_dev_strategy(void *devdata, int rw, daddr_t blk, size_t size,
60 char *buf, size_t *rsize);
61 static int efihttp_dev_open(struct open_file *f, ...);
62 static int efihttp_dev_close(struct open_file *f);
64 static int efihttp_fs_open(const char *path, struct open_file *f);
65 static int efihttp_fs_close(struct open_file *f);
66 static int efihttp_fs_read(struct open_file *f, void *buf, size_t size,
68 static int efihttp_fs_write(struct open_file *f, const void *buf, size_t size,
70 static off_t efihttp_fs_seek(struct open_file *f, off_t offset, int where);
71 static int efihttp_fs_stat(struct open_file *f, struct stat *sb);
72 static int efihttp_fs_readdir(struct open_file *f, struct dirent *d);
75 EFI_HTTP_PROTOCOL *http;
76 EFI_HANDLE http_handle;
77 EFI_HANDLE dev_handle;
88 struct devsw efihttp_dev = {
91 .dv_init = efihttp_dev_init,
92 .dv_strategy = efihttp_dev_strategy,
93 .dv_open = efihttp_dev_open,
94 .dv_close = efihttp_dev_close,
100 struct fs_ops efihttp_fsops = {
101 .fs_name = "efihttp",
102 .fo_open = efihttp_fs_open,
103 .fo_close = efihttp_fs_close,
104 .fo_read = efihttp_fs_read,
105 .fo_write = efihttp_fs_write,
106 .fo_seek = efihttp_fs_seek,
107 .fo_stat = efihttp_fs_stat,
108 .fo_readdir = efihttp_fs_readdir,
112 notify(EFI_EVENT event __unused, void *context)
121 setup_ipv4_config2(EFI_HANDLE handle, MAC_ADDR_DEVICE_PATH *mac,
122 IPv4_DEVICE_PATH *ipv4, DNS_DEVICE_PATH *dns)
124 EFI_IP4_CONFIG2_PROTOCOL *ip4config2;
127 status = BS->OpenProtocol(handle, &ip4config2_guid,
128 (void **)&ip4config2, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
129 if (EFI_ERROR(status))
130 return (efi_status_to_errno(status));
133 setenv("boot.netif.hwaddr",
134 ether_sprintf((u_char *)mac->MacAddress.Addr), 1);
136 setenv("boot.netif.ip",
137 inet_ntoa(*(struct in_addr *)ipv4->LocalIpAddress.Addr), 1);
138 setenv("boot.netif.netmask",
139 intoa(*(n_long *)ipv4->SubnetMask.Addr), 1);
140 setenv("boot.netif.gateway",
141 inet_ntoa(*(struct in_addr *)ipv4->GatewayIpAddress.Addr),
143 status = ip4config2->SetData(ip4config2,
144 Ip4Config2DataTypePolicy, sizeof(EFI_IP4_CONFIG2_POLICY),
145 &(EFI_IP4_CONFIG2_POLICY) { Ip4Config2PolicyStatic });
146 if (EFI_ERROR(status))
147 return (efi_status_to_errno(status));
149 status = ip4config2->SetData(ip4config2,
150 Ip4Config2DataTypeManualAddress,
151 sizeof(EFI_IP4_CONFIG2_MANUAL_ADDRESS),
152 &(EFI_IP4_CONFIG2_MANUAL_ADDRESS) {
153 .Address = ipv4->LocalIpAddress,
154 .SubnetMask = ipv4->SubnetMask });
155 if (EFI_ERROR(status))
156 return (efi_status_to_errno(status));
158 if (ipv4->GatewayIpAddress.Addr[0] != 0) {
159 status = ip4config2->SetData(ip4config2,
160 Ip4Config2DataTypeGateway, sizeof(EFI_IPv4_ADDRESS),
161 &ipv4->GatewayIpAddress);
162 if (EFI_ERROR(status))
163 return (efi_status_to_errno(status));
167 status = ip4config2->SetData(ip4config2,
168 Ip4Config2DataTypeDnsServer,
169 sizeof(EFI_IPv4_ADDRESS), &dns->DnsServerIp);
170 if (EFI_ERROR(status))
171 return (efi_status_to_errno(status));
174 status = ip4config2->SetData(ip4config2,
175 Ip4Config2DataTypePolicy, sizeof(EFI_IP4_CONFIG2_POLICY),
176 &(EFI_IP4_CONFIG2_POLICY) { Ip4Config2PolicyDhcp });
177 if (EFI_ERROR(status))
178 return (efi_status_to_errno(status));
185 efihttp_dev_init(void)
187 EFI_DEVICE_PATH *imgpath, *devpath;
188 URI_DEVICE_PATH *uri;
194 imgpath = efi_lookup_image_devpath(IH);
199 for (; !IsDevicePathEnd(devpath);
200 devpath = NextDevicePathNode(devpath)) {
201 if (DevicePathType(devpath) != MESSAGING_DEVICE_PATH ||
202 DevicePathSubType(devpath) != MSG_URI_DP)
204 uri = (URI_DEVICE_PATH *)devpath;
205 if (strncmp("http", (const char *)uri->Uri, 4) == 0)
211 status = BS->LocateDevicePath(&httpsb_guid, &imgpath, &handle);
212 if (EFI_ERROR(status))
213 return (efi_status_to_errno(status));
215 err = efi_register_handles(&efihttp_dev, &handle, NULL, 1);
217 efihttp_init_done = true;
223 efihttp_dev_strategy(void *devdata __unused, int rw __unused,
224 daddr_t blk __unused, size_t size __unused, char *buf __unused,
225 size_t *rsize __unused)
231 efihttp_dev_open(struct open_file *f, ...)
233 EFI_HTTP_CONFIG_DATA config;
234 EFI_HTTPv4_ACCESS_POINT config_access;
235 DNS_DEVICE_PATH *dns;
236 EFI_DEVICE_PATH *devpath, *imgpath;
237 EFI_SERVICE_BINDING_PROTOCOL *sb;
238 IPv4_DEVICE_PATH *ipv4;
239 MAC_ADDR_DEVICE_PATH *mac;
240 URI_DEVICE_PATH *uri;
242 struct open_efihttp *oh;
248 if (!efihttp_init_done)
251 imgpath = efi_lookup_image_devpath(IH);
255 status = BS->LocateDevicePath(&httpsb_guid, &devpath, &handle);
256 if (EFI_ERROR(status))
257 return (efi_status_to_errno(status));
262 for (; !IsDevicePathEnd(imgpath);
263 imgpath = NextDevicePathNode(imgpath)) {
264 if (DevicePathType(imgpath) != MESSAGING_DEVICE_PATH)
266 switch (DevicePathSubType(imgpath)) {
267 case MSG_MAC_ADDR_DP:
268 mac = (MAC_ADDR_DEVICE_PATH *)imgpath;
271 ipv4 = (IPv4_DEVICE_PATH *)imgpath;
274 dns = (DNS_DEVICE_PATH *)imgpath;
277 uri = (URI_DEVICE_PATH *)imgpath;
287 err = setup_ipv4_config2(handle, mac, ipv4, dns);
291 oh = calloc(1, sizeof(struct open_efihttp));
294 oh->dev_handle = handle;
295 dev = (struct devdesc *)f->f_devdata;
296 dev->d_opendata = oh;
298 status = BS->OpenProtocol(handle, &httpsb_guid, (void **)&sb, IH, NULL,
299 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
300 if (EFI_ERROR(status)) {
301 err = efi_status_to_errno(status);
305 status = sb->CreateChild(sb, &oh->http_handle);
306 if (EFI_ERROR(status)) {
307 err = efi_status_to_errno(status);
311 status = BS->OpenProtocol(oh->http_handle, &http_guid,
312 (void **)&oh->http, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
313 if (EFI_ERROR(status)) {
314 sb->DestroyChild(sb, oh->http_handle);
315 err = efi_status_to_errno(status);
319 config.HttpVersion = HttpVersion11;
320 config.TimeOutMillisec = 0;
321 config.LocalAddressIsIPv6 = FALSE;
322 config.AccessPoint.IPv4Node = &config_access;
323 config_access.UseDefaultAddress = TRUE;
324 config_access.LocalPort = 0;
325 status = oh->http->Configure(oh->http, &config);
326 if (EFI_ERROR(status)) {
327 sb->DestroyChild(sb, oh->http_handle);
328 err = efi_status_to_errno(status);
333 * Here we make attempt to construct a "base" URI by stripping
334 * the last two path components from the loaded URI under the
335 * assumption that it is something like:
337 * http://127.0.0.1/foo/boot/loader.efi
339 * hoping to arriving at:
341 * http://127.0.0.1/foo/
343 len = DevicePathNodeLength(&uri->Header) - sizeof(URI_DEVICE_PATH);
344 oh->uri_base = malloc(len + 1);
345 if (oh->uri_base == NULL) {
349 strncpy(oh->uri_base, (const char *)uri->Uri, len);
350 oh->uri_base[len] = '\0';
351 c = strrchr(oh->uri_base, '/');
354 c = strrchr(oh->uri_base, '/');
355 if (c != NULL && *(c + 1) != '\0')
361 free(dev->d_opendata);
362 dev->d_opendata = NULL;
368 efihttp_dev_close(struct open_file *f)
370 EFI_SERVICE_BINDING_PROTOCOL *sb;
372 struct open_efihttp *oh;
375 dev = (struct devdesc *)f->f_devdata;
376 oh = (struct open_efihttp *)dev->d_opendata;
377 status = BS->OpenProtocol(oh->dev_handle, &httpsb_guid, (void **)&sb,
378 IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
379 if (EFI_ERROR(status))
380 return (efi_status_to_errno(status));
381 sb->DestroyChild(sb, oh->http_handle);
384 dev->d_opendata = NULL;
389 _efihttp_fs_open(const char *path, struct open_file *f)
391 EFI_HTTP_CONFIG_DATA config;
392 EFI_HTTPv4_ACCESS_POINT config_access;
393 EFI_HTTP_TOKEN token;
394 EFI_HTTP_MESSAGE message;
395 EFI_HTTP_REQUEST_DATA request;
396 EFI_HTTP_RESPONSE_DATA response;
397 EFI_HTTP_HEADER headers[3];
401 struct open_efihttp *oh;
402 struct file_efihttp *fh;
408 dev = (struct devdesc *)f->f_devdata;
409 oh = (struct open_efihttp *)dev->d_opendata;
410 fh = calloc(1, sizeof(struct file_efihttp));
414 fh->path = strdup(path);
417 * Reset the HTTP state.
419 * EDK II's persistent HTTP connection handling is graceless,
420 * assuming that all connections are persistent regardless of
421 * any Connection: header or HTTP version reported by the
422 * server, and failing to send requests when a more sane
423 * implementation would seem to be just reestablishing the
426 * In the hopes of having some robustness, we indicate to the
427 * server that we will close the connection by using a
428 * Connection: close header. And then here we manually
429 * unconfigure and reconfigure the http instance to force the
432 memset(&config, 0, sizeof(config));
433 memset(&config_access, 0, sizeof(config_access));
434 config.AccessPoint.IPv4Node = &config_access;
435 status = oh->http->GetModeData(oh->http, &config);
436 if (EFI_ERROR(status))
437 return (efi_status_to_errno(status));
438 status = oh->http->Configure(oh->http, NULL);
439 if (EFI_ERROR(status))
440 return (efi_status_to_errno(status));
441 status = oh->http->Configure(oh->http, &config);
442 if (EFI_ERROR(status))
443 return (efi_status_to_errno(status));
445 /* Send the read request */
447 status = BS->CreateEvent(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, notify,
448 &done, &token.Event);
449 if (EFI_ERROR(status))
450 return (efi_status_to_errno(status));
452 /* extract the host portion of the URL */
453 host = strdup(oh->uri_base);
457 /* Remove the protocol scheme */
458 c = strchr(host, '/');
459 if (c != NULL && *(c + 1) == '/')
462 /* Remove any path information */
463 c = strchr(hostp, '/');
467 token.Status = EFI_NOT_READY;
468 token.Message = &message;
469 message.Data.Request = &request;
470 message.HeaderCount = 3;
471 message.Headers = headers;
472 message.BodyLength = 0;
474 request.Method = HttpMethodGet;
475 request.Url = calloc(strlen(oh->uri_base) + strlen(path) + 1, 2);
476 headers[0].FieldName = (CHAR8 *)"Host";
477 headers[0].FieldValue = (CHAR8 *)hostp;
478 headers[1].FieldName = (CHAR8 *)"Connection";
479 headers[1].FieldValue = (CHAR8 *)"close";
480 headers[2].FieldName = (CHAR8 *)"Accept";
481 headers[2].FieldValue = (CHAR8 *)"*/*";
482 cpy8to16(oh->uri_base, request.Url, strlen(oh->uri_base));
483 cpy8to16(path, request.Url + strlen(oh->uri_base), strlen(path));
484 status = oh->http->Request(oh->http, &token);
487 if (EFI_ERROR(status)) {
488 BS->CloseEvent(token.Event);
489 return (efi_status_to_errno(status));
493 while (!done && polltime < EFIHTTP_POLL_TIMEOUT) {
494 status = oh->http->Poll(oh->http);
495 if (EFI_ERROR(status))
503 BS->CloseEvent(token.Event);
504 if (EFI_ERROR(token.Status))
505 return (efi_status_to_errno(token.Status));
507 /* Wait for the read response */
509 status = BS->CreateEvent(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, notify,
510 &done, &token.Event);
511 if (EFI_ERROR(status))
512 return (efi_status_to_errno(status));
513 token.Status = EFI_NOT_READY;
514 token.Message = &message;
515 message.Data.Response = &response;
516 message.HeaderCount = 0;
517 message.Headers = NULL;
518 message.BodyLength = 0;
520 response.StatusCode = HTTP_STATUS_UNSUPPORTED_STATUS;
521 status = oh->http->Response(oh->http, &token);
522 if (EFI_ERROR(status)) {
523 BS->CloseEvent(token.Event);
524 return (efi_status_to_errno(status));
528 while (!done && polltime < EFIHTTP_POLL_TIMEOUT) {
529 status = oh->http->Poll(oh->http);
530 if (EFI_ERROR(status))
538 BS->CloseEvent(token.Event);
539 if (EFI_ERROR(token.Status)) {
540 BS->FreePool(message.Headers);
541 return (efi_status_to_errno(token.Status));
543 if (response.StatusCode != HTTP_STATUS_200_OK) {
544 BS->FreePool(message.Headers);
549 for (i = 0; i < message.HeaderCount; i++) {
550 if (strcasecmp((const char *)message.Headers[i].FieldName,
551 "Content-Length") == 0)
552 fh->size = strtoul((const char *)
553 message.Headers[i].FieldValue, NULL, 10);
554 else if (strcasecmp((const char *)message.Headers[i].FieldName,
555 "Content-type") == 0) {
556 if (strncmp((const char *)message.Headers[i].FieldValue,
557 "text/html", 9) == 0)
566 efihttp_fs_open(const char *path, struct open_file *f)
571 if (!efihttp_init_done)
574 * If any path fails to open, try with a trailing slash in
575 * case it's a directory.
577 err = _efihttp_fs_open(path, f);
579 path_slash = malloc(strlen(path) + 2);
580 if (path_slash == NULL)
582 strcpy(path_slash, path);
583 strcat(path_slash, "/");
584 err = _efihttp_fs_open(path_slash, f);
591 efihttp_fs_close(struct open_file *f __unused)
597 _efihttp_fs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
599 EFI_HTTP_TOKEN token;
600 EFI_HTTP_MESSAGE message;
603 struct open_efihttp *oh;
604 struct file_efihttp *fh;
608 fh = (struct file_efihttp *)f->f_fsdata;
610 if (fh->size > 0 && fh->offset >= fh->size) {
617 dev = (struct devdesc *)f->f_devdata;
618 oh = (struct open_efihttp *)dev->d_opendata;
620 status = BS->CreateEvent(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, notify,
621 &done, &token.Event);
622 if (EFI_ERROR(status)) {
623 return (efi_status_to_errno(status));
625 token.Status = EFI_NOT_READY;
626 token.Message = &message;
627 message.Data.Request = NULL;
628 message.HeaderCount = 0;
629 message.Headers = NULL;
630 message.BodyLength = size;
632 status = oh->http->Response(oh->http, &token);
633 if (status == EFI_CONNECTION_FIN) {
637 } else if (EFI_ERROR(status)) {
638 BS->CloseEvent(token.Event);
639 return (efi_status_to_errno(status));
642 while (!done && polltime < EFIHTTP_POLL_TIMEOUT) {
643 status = oh->http->Poll(oh->http);
644 if (EFI_ERROR(status))
652 BS->CloseEvent(token.Event);
653 if (token.Status == EFI_CONNECTION_FIN) {
657 } else if (EFI_ERROR(token.Status))
658 return (efi_status_to_errno(token.Status));
660 *resid = size - message.BodyLength;
661 fh->offset += message.BodyLength;
666 efihttp_fs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
672 err = _efihttp_fs_read(f, buf, size, &res);
673 if (err != 0 || res == size)
685 efihttp_fs_write(struct open_file *f __unused, const void *buf __unused,
686 size_t size __unused, size_t *resid __unused)
692 efihttp_fs_seek(struct open_file *f, off_t offset, int where)
694 struct file_efihttp *fh;
700 fh = (struct file_efihttp *)f->f_fsdata;
701 if (where == SEEK_SET && fh->offset == offset)
703 if (where == SEEK_SET && fh->offset < offset) {
707 res = offset - fh->offset;
709 err = _efihttp_fs_read(f, buf, min(1500, res), &res2);
714 res -= min(1500, res) - res2;
718 } else if (where == SEEK_SET) {
722 err = efihttp_fs_open(path, f);
726 return efihttp_fs_seek(f, offset, where);
732 efihttp_fs_stat(struct open_file *f, struct stat *sb)
734 struct file_efihttp *fh;
736 fh = (struct file_efihttp *)f->f_fsdata;
737 memset(sb, 0, sizeof(*sb));
739 sb->st_mode = 0777 | (fh->is_dir ? S_IFDIR : S_IFREG);
740 sb->st_size = fh->size;
745 efihttp_fs_readdir(struct open_file *f, struct dirent *d)
747 static char *dirbuf = NULL, *db2, *cursor;
748 static int dirbuf_len = 0;
750 struct file_efihttp *fh;
752 fh = (struct file_efihttp *)f->f_fsdata;
753 if (dirbuf_len < fh->size) {
754 db2 = realloc(dirbuf, fh->size);
761 dirbuf_len = fh->size;
764 if (fh->offset != fh->size) {
765 efihttp_fs_seek(f, 0, SEEK_SET);
766 efihttp_fs_read(f, dirbuf, dirbuf_len, NULL);
770 cursor = strstr(cursor, "<a href=\"");
774 end = strchr(cursor, '"');
775 if (*(end - 1) == '/') {
780 memcpy(d->d_name, cursor, end - cursor);
781 d->d_name[end - cursor] = '\0';