diff --git a/stand/common/bootstrap.h b/stand/common/bootstrap.h index 2be6538efe4c..809859c276b9 100644 --- a/stand/common/bootstrap.h +++ b/stand/common/bootstrap.h @@ -1,354 +1,352 @@ /*- * Copyright (c) 1998 Michael Smith * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _BOOTSTRAP_H_ #define _BOOTSTRAP_H_ #include #include #include #include "readin.h" /* Commands and return values; nonzero return sets command_errmsg != NULL */ typedef int (bootblk_cmd_t)(int argc, char *argv[]); #define COMMAND_ERRBUFSZ (256) extern const char *command_errmsg; extern char command_errbuf[COMMAND_ERRBUFSZ]; #define CMD_OK 0 #define CMD_WARN 1 #define CMD_ERROR 2 #define CMD_CRIT 3 #define CMD_FATAL 4 /* interp.c */ void interact(void); void interp_emit_prompt(void); int interp_builtin_cmd(int argc, char *argv[]); /* Called by interp.c for interp_*.c embedded interpreters */ int interp_include(const char *filename); /* Execute commands from filename */ void interp_init(void); /* Initialize interpreater */ int interp_run(const char *line); /* Run a single command */ /* interp_backslash.c */ char *backslash(const char *str); /* interp_parse.c */ int parse(int *argc, char ***argv, const char *str); /* boot.c */ void autoboot_maybe(void); int getrootmount(char *rootdev); /* misc.c */ char *unargv(int argc, char *argv[]); void hexdump(caddr_t region, size_t len); size_t strlenout(vm_offset_t str); char *strdupout(vm_offset_t str); void kern_bzero(vm_offset_t dest, size_t len); int kern_pread(readin_handle_t fd, vm_offset_t dest, size_t len, off_t off); void *alloc_pread(readin_handle_t fd, off_t off, size_t len); /* bcache.c */ void bcache_init(size_t nblks, size_t bsize); void bcache_add_dev(int); void *bcache_allocate(void); void bcache_free(void *); int bcache_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize); /* * Disk block cache */ struct bcache_devdata { int (*dv_strategy)(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize); void *dv_devdata; void *dv_cache; }; /* * Modular console support. */ struct console { const char *c_name; const char *c_desc; int c_flags; #define C_PRESENTIN (1<<0) /* console can provide input */ #define C_PRESENTOUT (1<<1) /* console can provide output */ #define C_ACTIVEIN (1<<2) /* user wants input from console */ #define C_ACTIVEOUT (1<<3) /* user wants output to console */ #define C_WIDEOUT (1<<4) /* c_out routine groks wide chars */ void (* c_probe)(struct console *cp); /* set c_flags to match hardware */ int (* c_init)(int arg); /* reinit XXX may need more args */ void (* c_out)(int c); /* emit c */ int (* c_in)(void); /* wait for and return input */ int (* c_ready)(void); /* return nonzer if input waiting */ }; extern struct console *consoles[]; void cons_probe(void); /* * Plug-and-play enumerator/configurator interface. */ struct pnphandler { const char *pp_name; /* handler/bus name */ void (* pp_enumerate)(void); /* enumerate PnP devices, add to chain */ }; struct pnpident { char *id_ident; /* ASCII identifier, actual format varies with bus/handler */ STAILQ_ENTRY(pnpident) id_link; }; struct pnpinfo { char *pi_desc; /* ASCII description, optional */ int pi_revision; /* optional revision (or -1) if not supported */ char *pi_module; /* module/args nominated to handle device */ int pi_argc; /* module arguments */ char **pi_argv; struct pnphandler *pi_handler; /* handler which detected this device */ STAILQ_HEAD(,pnpident) pi_ident; /* list of identifiers */ STAILQ_ENTRY(pnpinfo) pi_link; }; STAILQ_HEAD(pnpinfo_stql, pnpinfo); extern struct pnphandler *pnphandlers[]; /* provided by MD code */ void pnp_addident(struct pnpinfo *pi, char *ident); struct pnpinfo *pnp_allocinfo(void); void pnp_freeinfo(struct pnpinfo *pi); void pnp_addinfo(struct pnpinfo *pi); char *pnp_eisaformat(uint8_t *data); /* * < 0 - No ISA in system * == 0 - Maybe ISA, search for read data port * > 0 - ISA in system, value is read data port address */ extern int isapnp_readport; /* * Version information */ extern char bootprog_info[]; /* * Interpreter information */ extern const char bootprog_interp[]; #define INTERP_DEFINE(interpstr) \ const char bootprog_interp[] = "$Interpreter:" interpstr /* * Preloaded file metadata header. * * Metadata are allocated on our heap, and copied into kernel space * before executing the kernel. */ struct file_metadata { size_t md_size; uint16_t md_type; struct file_metadata *md_next; char md_data[1]; /* data are immediately appended */ }; struct preloaded_file; struct mod_depend; struct kernel_module { char *m_name; /* module name */ int m_version; /* module version */ /* char *m_args;*/ /* arguments for the module */ struct preloaded_file *m_fp; struct kernel_module *m_next; }; /* * Preloaded file information. Depending on type, file can contain * additional units called 'modules'. * * At least one file (the kernel) must be loaded in order to boot. * The kernel is always loaded first. * * String fields (m_name, m_type) should be dynamically allocated. */ struct preloaded_file { char *f_name; /* file name */ char *f_type; /* verbose file type, eg 'ELF kernel', 'pnptable', etc. */ char *f_args; /* arguments for the file */ struct file_metadata *f_metadata; /* metadata that will be placed in the module directory */ int f_loader; /* index of the loader that read the file */ vm_offset_t f_addr; /* load address */ size_t f_size; /* file size */ struct kernel_module *f_modules; /* list of modules if any */ struct preloaded_file *f_next; /* next file */ }; struct file_format { /* Load function must return EFTYPE if it can't handle the module supplied */ int (* l_load)(char *filename, uint64_t dest, struct preloaded_file **result); /* Only a loader that will load a kernel (first module) should have an exec handler */ int (* l_exec)(struct preloaded_file *mp); }; extern struct file_format *file_formats[]; /* supplied by consumer */ extern struct preloaded_file *preloaded_files; int mod_load(char *name, struct mod_depend *verinfo, int argc, char *argv[]); int mod_loadkld(const char *name, int argc, char *argv[]); void unload(void); struct preloaded_file *file_alloc(void); struct preloaded_file *file_findfile(const char *name, const char *type); struct file_metadata *file_findmetadata(struct preloaded_file *fp, int type); struct preloaded_file *file_loadraw(const char *name, char *type, int insert); void file_discard(struct preloaded_file *fp); void file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p); int file_addmodule(struct preloaded_file *fp, char *modname, int version, struct kernel_module **newmp); void file_removemetadata(struct preloaded_file *fp); /* MI module loaders */ #ifdef __elfN /* Relocation types. */ #define ELF_RELOC_REL 1 #define ELF_RELOC_RELA 2 /* Relocation offset for some architectures */ extern uint64_t __elfN(relocation_offset); struct elf_file; typedef Elf_Addr (symaddr_fn)(struct elf_file *ef, Elf_Size symidx); int __elfN(loadfile)(char *filename, uint64_t dest, struct preloaded_file **result); int __elfN(obj_loadfile)(char *filename, uint64_t dest, struct preloaded_file **result); int __elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr, const void *reldata, int reltype, Elf_Addr relbase, Elf_Addr dataaddr, void *data, size_t len); int __elfN(loadfile_raw)(char *filename, uint64_t dest, struct preloaded_file **result, int multiboot); int __elfN(load_modmetadata)(struct preloaded_file *fp, uint64_t dest); #endif /* * Support for commands */ struct bootblk_command { const char *c_name; const char *c_desc; bootblk_cmd_t *c_fn; }; #define COMMAND_SET(tag, key, desc, func) \ static bootblk_cmd_t func; \ static struct bootblk_command _cmd_ ## tag = { key, desc, func }; \ DATA_SET(Xcommand_set, _cmd_ ## tag) SET_DECLARE(Xcommand_set, struct bootblk_command); /* * The intention of the architecture switch is to provide a convenient * encapsulation of the interface between the bootstrap MI and MD code. * MD code may selectively populate the switch at runtime based on the * actual configuration of the target system. */ struct arch_switch { /* Automatically load modules as required by detected hardware */ int (*arch_autoload)(void); /* Locate the device for (name), return pointer to tail in (*path) */ int (*arch_getdev)(void **dev, const char *name, const char **path); /* Copy from local address space to module address space, similar to bcopy() */ ssize_t (*arch_copyin)(const void *src, vm_offset_t dest, const size_t len); /* Copy to local address space from module address space, similar to bcopy() */ ssize_t (*arch_copyout)(const vm_offset_t src, void *dest, const size_t len); /* Read from file to module address space, same semantics as read() */ ssize_t (*arch_readin)(readin_handle_t fd, vm_offset_t dest, const size_t len); /* Perform ISA byte port I/O (only for systems with ISA) */ int (*arch_isainb)(int port); void (*arch_isaoutb)(int port, int value); /* * Interface to adjust the load address according to the "object" * being loaded. */ uint64_t (*arch_loadaddr)(u_int type, void *data, uint64_t addr); #define LOAD_ELF 1 /* data points to the ELF header. */ #define LOAD_RAW 2 /* data points to the file name. */ /* * Interface to inform MD code about a loaded (ELF) segment. This * can be used to flush caches and/or set up translations. */ #ifdef __elfN void (*arch_loadseg)(Elf_Ehdr *eh, Elf_Phdr *ph, uint64_t delta); #else void (*arch_loadseg)(void *eh, void *ph, uint64_t delta); #endif /* Probe ZFS pool(s), if needed. */ void (*arch_zfs_probe)(void); /* Return the hypervisor name/type or NULL if not virtualized. */ const char *(*arch_hypervisor)(void); /* For kexec-type loaders, get ksegment structure */ void (*arch_kexec_kseg_get)(int *nseg, void **kseg); }; extern struct arch_switch archsw; /* This must be provided by the MD code, but should it be in the archsw? */ void delay(int delay); void dev_cleanup(void); -time_t time(time_t *tloc); - #ifndef CTASSERT #define CTASSERT(x) _Static_assert(x, "compile-time assertion failed") #endif #endif /* !_BOOTSTRAP_H_ */ diff --git a/stand/efi/include/efilib.h b/stand/efi/include/efilib.h index a95fba64ff41..aace2351fcc6 100644 --- a/stand/efi/include/efilib.h +++ b/stand/efi/include/efilib.h @@ -1,150 +1,149 @@ /*- * Copyright (c) 2000 Doug Rabson * Copyright (c) 2006 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _LOADER_EFILIB_H #define _LOADER_EFILIB_H #include #include #include extern EFI_HANDLE IH; extern EFI_SYSTEM_TABLE *ST; extern EFI_BOOT_SERVICES *BS; extern EFI_RUNTIME_SERVICES *RS; extern struct devsw efipart_fddev; extern struct devsw efipart_cddev; extern struct devsw efipart_hddev; extern struct devsw efihttp_dev; extern struct devsw efinet_dev; extern struct netif_driver efinetif; /* EFI block device data, included here to help efi_zfs_probe() */ typedef STAILQ_HEAD(pdinfo_list, pdinfo) pdinfo_list_t; typedef struct pdinfo { STAILQ_ENTRY(pdinfo) pd_link; /* link in device list */ pdinfo_list_t pd_part; /* list of partitions */ EFI_HANDLE pd_handle; EFI_HANDLE pd_alias; EFI_DEVICE_PATH *pd_devpath; EFI_BLOCK_IO *pd_blkio; uint32_t pd_unit; /* unit number */ uint32_t pd_open; /* reference counter */ void *pd_bcache; /* buffer cache data */ struct pdinfo *pd_parent; /* Linked items (eg partitions) */ struct devsw *pd_devsw; /* Back pointer to devsw */ } pdinfo_t; pdinfo_list_t *efiblk_get_pdinfo_list(struct devsw *dev); pdinfo_t *efiblk_get_pdinfo(struct devdesc *dev); pdinfo_t *efiblk_get_pdinfo_by_handle(EFI_HANDLE h); pdinfo_t *efiblk_get_pdinfo_by_device_path(EFI_DEVICE_PATH *path); void *efi_get_table(EFI_GUID *tbl); EFI_STATUS OpenProtocolByHandle(EFI_HANDLE, EFI_GUID *, void **); int efi_getdev(void **vdev, const char *devspec, const char **path); char *efi_fmtdev(void *vdev); int efi_setcurrdev(struct env_var *ev, int flags, const void *value); int efi_register_handles(struct devsw *, EFI_HANDLE *, EFI_HANDLE *, int); EFI_HANDLE efi_find_handle(struct devsw *, int); int efi_handle_lookup(EFI_HANDLE, struct devsw **, int *, uint64_t *); int efi_handle_update_dev(EFI_HANDLE, struct devsw *, int, uint64_t); EFI_DEVICE_PATH *efi_lookup_image_devpath(EFI_HANDLE); EFI_DEVICE_PATH *efi_lookup_devpath(EFI_HANDLE); void efi_close_devpath(EFI_HANDLE); EFI_HANDLE efi_devpath_handle(EFI_DEVICE_PATH *); EFI_DEVICE_PATH *efi_devpath_last_node(EFI_DEVICE_PATH *); EFI_DEVICE_PATH *efi_devpath_trim(EFI_DEVICE_PATH *); bool efi_devpath_match(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *); bool efi_devpath_match_node(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *); bool efi_devpath_is_prefix(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *); CHAR16 *efi_devpath_name(EFI_DEVICE_PATH *); void efi_free_devpath_name(CHAR16 *); bool efi_devpath_same_disk(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *); EFI_DEVICE_PATH *efi_devpath_to_media_path(EFI_DEVICE_PATH *); UINTN efi_devpath_length(EFI_DEVICE_PATH *); EFI_HANDLE efi_devpath_to_handle(EFI_DEVICE_PATH *path, EFI_HANDLE *handles, unsigned nhandles); EFI_DEVICE_PATH *efi_name_to_devpath(const char *path); EFI_DEVICE_PATH *efi_name_to_devpath16(CHAR16 *path); void efi_devpath_free(EFI_DEVICE_PATH *dp); int efi_status_to_errno(EFI_STATUS); EFI_STATUS errno_to_efi_status(int errno); void efi_time_init(void); void efi_time_fini(void); EFI_STATUS efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE* Xsystab); EFI_STATUS main(int argc, CHAR16 *argv[]); void efi_exit(EFI_STATUS status) __dead2; -void delay(int usecs); /* EFI environment initialization. */ void efi_init_environment(void); /* EFI Memory type strings. */ const char *efi_memory_type(EFI_MEMORY_TYPE); /* CHAR16 utility functions. */ int wcscmp(CHAR16 *, CHAR16 *); void cpy8to16(const char *, CHAR16 *, size_t); void cpy16to8(const CHAR16 *, char *, size_t); /* * Routines for interacting with EFI's env vars in a more unix-like * way than the standard APIs. In addition, convenience routines for * the loader setting / getting FreeBSD specific variables. */ EFI_STATUS efi_delenv(EFI_GUID *guid, const char *varname); EFI_STATUS efi_freebsd_delenv(const char *varname); EFI_STATUS efi_freebsd_getenv(const char *v, void *data, __size_t *len); EFI_STATUS efi_getenv(EFI_GUID *g, const char *v, void *data, __size_t *len); EFI_STATUS efi_global_getenv(const char *v, void *data, __size_t *len); EFI_STATUS efi_setenv(EFI_GUID *guid, const char *varname, UINT32 attr, void *data, __size_t len); EFI_STATUS efi_setenv_freebsd_wcs(const char *varname, CHAR16 *valstr); /* guids and names */ bool efi_guid_to_str(const EFI_GUID *, char **); bool efi_str_to_guid(const char *, EFI_GUID *); bool efi_name_to_guid(const char *, EFI_GUID *); bool efi_guid_to_name(EFI_GUID *, char **); /* efipart.c */ int efipart_inithandles(void); #endif /* _LOADER_EFILIB_H */ diff --git a/stand/efi/libefi/efihttp.c b/stand/efi/libefi/efihttp.c index e637da23a088..12d445c55b67 100644 --- a/stand/efi/libefi/efihttp.c +++ b/stand/efi/libefi/efihttp.c @@ -1,781 +1,782 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2019 Intel Corporation * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include __FBSDID("$FreeBSD$"); #include #include #include #include +#include #include #include #include #include #include #include #include /* Poll timeout in milliseconds */ static const int EFIHTTP_POLL_TIMEOUT = 300000; static EFI_GUID http_guid = EFI_HTTP_PROTOCOL_GUID; static EFI_GUID httpsb_guid = EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID; static EFI_GUID ip4config2_guid = EFI_IP4_CONFIG2_PROTOCOL_GUID; static bool efihttp_init_done = false; static int efihttp_dev_init(void); static int efihttp_dev_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize); static int efihttp_dev_open(struct open_file *f, ...); static int efihttp_dev_close(struct open_file *f); static int efihttp_fs_open(const char *path, struct open_file *f); static int efihttp_fs_close(struct open_file *f); static int efihttp_fs_read(struct open_file *f, void *buf, size_t size, size_t *resid); static int efihttp_fs_write(struct open_file *f, const void *buf, size_t size, size_t *resid); static off_t efihttp_fs_seek(struct open_file *f, off_t offset, int where); static int efihttp_fs_stat(struct open_file *f, struct stat *sb); static int efihttp_fs_readdir(struct open_file *f, struct dirent *d); struct open_efihttp { EFI_HTTP_PROTOCOL *http; EFI_HANDLE http_handle; EFI_HANDLE dev_handle; char *uri_base; }; struct file_efihttp { ssize_t size; off_t offset; char *path; bool is_dir; }; struct devsw efihttp_dev = { .dv_name = "http", .dv_type = DEVT_NET, .dv_init = efihttp_dev_init, .dv_strategy = efihttp_dev_strategy, .dv_open = efihttp_dev_open, .dv_close = efihttp_dev_close, .dv_ioctl = noioctl, .dv_print = NULL, .dv_cleanup = NULL, }; struct fs_ops efihttp_fsops = { .fs_name = "efihttp", .fo_open = efihttp_fs_open, .fo_close = efihttp_fs_close, .fo_read = efihttp_fs_read, .fo_write = efihttp_fs_write, .fo_seek = efihttp_fs_seek, .fo_stat = efihttp_fs_stat, .fo_readdir = efihttp_fs_readdir, }; static void EFIAPI notify(EFI_EVENT event __unused, void *context) { bool *b; b = (bool *)context; *b = true; } static int setup_ipv4_config2(EFI_HANDLE handle, MAC_ADDR_DEVICE_PATH *mac, IPv4_DEVICE_PATH *ipv4, DNS_DEVICE_PATH *dns) { EFI_IP4_CONFIG2_PROTOCOL *ip4config2; EFI_STATUS status; status = BS->OpenProtocol(handle, &ip4config2_guid, (void **)&ip4config2, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); if (ipv4 != NULL) { if (mac != NULL) { setenv("boot.netif.hwaddr", ether_sprintf((u_char *)mac->MacAddress.Addr), 1); } setenv("boot.netif.ip", inet_ntoa(*(struct in_addr *)ipv4->LocalIpAddress.Addr), 1); setenv("boot.netif.netmask", intoa(*(n_long *)ipv4->SubnetMask.Addr), 1); setenv("boot.netif.gateway", inet_ntoa(*(struct in_addr *)ipv4->GatewayIpAddress.Addr), 1); status = ip4config2->SetData(ip4config2, Ip4Config2DataTypePolicy, sizeof(EFI_IP4_CONFIG2_POLICY), &(EFI_IP4_CONFIG2_POLICY) { Ip4Config2PolicyStatic }); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); status = ip4config2->SetData(ip4config2, Ip4Config2DataTypeManualAddress, sizeof(EFI_IP4_CONFIG2_MANUAL_ADDRESS), &(EFI_IP4_CONFIG2_MANUAL_ADDRESS) { .Address = ipv4->LocalIpAddress, .SubnetMask = ipv4->SubnetMask }); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); if (ipv4->GatewayIpAddress.Addr[0] != 0) { status = ip4config2->SetData(ip4config2, Ip4Config2DataTypeGateway, sizeof(EFI_IPv4_ADDRESS), &ipv4->GatewayIpAddress); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); } if (dns != NULL) { status = ip4config2->SetData(ip4config2, Ip4Config2DataTypeDnsServer, sizeof(EFI_IPv4_ADDRESS), &dns->DnsServerIp); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); } } else { status = ip4config2->SetData(ip4config2, Ip4Config2DataTypePolicy, sizeof(EFI_IP4_CONFIG2_POLICY), &(EFI_IP4_CONFIG2_POLICY) { Ip4Config2PolicyDhcp }); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); } return (0); } static int efihttp_dev_init(void) { EFI_DEVICE_PATH *imgpath, *devpath; URI_DEVICE_PATH *uri; EFI_HANDLE handle; EFI_STATUS status; int err; bool found_http; imgpath = efi_lookup_image_devpath(IH); if (imgpath == NULL) return (ENXIO); devpath = imgpath; found_http = false; for (; !IsDevicePathEnd(devpath); devpath = NextDevicePathNode(devpath)) { if (DevicePathType(devpath) != MESSAGING_DEVICE_PATH || DevicePathSubType(devpath) != MSG_URI_DP) continue; uri = (URI_DEVICE_PATH *)devpath; if (strncmp("http", (const char *)uri->Uri, 4) == 0) found_http = true; } if (!found_http) return (ENXIO); status = BS->LocateDevicePath(&httpsb_guid, &imgpath, &handle); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); err = efi_register_handles(&efihttp_dev, &handle, NULL, 1); if (!err) efihttp_init_done = true; return (err); } static int efihttp_dev_strategy(void *devdata __unused, int rw __unused, daddr_t blk __unused, size_t size __unused, char *buf __unused, size_t *rsize __unused) { return (EIO); } static int efihttp_dev_open(struct open_file *f, ...) { EFI_HTTP_CONFIG_DATA config; EFI_HTTPv4_ACCESS_POINT config_access; DNS_DEVICE_PATH *dns; EFI_DEVICE_PATH *devpath, *imgpath; EFI_SERVICE_BINDING_PROTOCOL *sb; IPv4_DEVICE_PATH *ipv4; MAC_ADDR_DEVICE_PATH *mac; URI_DEVICE_PATH *uri; struct devdesc *dev; struct open_efihttp *oh; char *c; EFI_HANDLE handle; EFI_STATUS status; int err, len; if (!efihttp_init_done) return (ENXIO); imgpath = efi_lookup_image_devpath(IH); if (imgpath == NULL) return (ENXIO); devpath = imgpath; status = BS->LocateDevicePath(&httpsb_guid, &devpath, &handle); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); mac = NULL; ipv4 = NULL; dns = NULL; uri = NULL; for (; !IsDevicePathEnd(imgpath); imgpath = NextDevicePathNode(imgpath)) { if (DevicePathType(imgpath) != MESSAGING_DEVICE_PATH) continue; switch (DevicePathSubType(imgpath)) { case MSG_MAC_ADDR_DP: mac = (MAC_ADDR_DEVICE_PATH *)imgpath; break; case MSG_IPv4_DP: ipv4 = (IPv4_DEVICE_PATH *)imgpath; break; case MSG_DNS_DP: dns = (DNS_DEVICE_PATH *)imgpath; break; case MSG_URI_DP: uri = (URI_DEVICE_PATH *)imgpath; break; default: break; } } if (uri == NULL) return (ENXIO); err = setup_ipv4_config2(handle, mac, ipv4, dns); if (err) return (err); oh = calloc(1, sizeof(struct open_efihttp)); if (!oh) return (ENOMEM); oh->dev_handle = handle; dev = (struct devdesc *)f->f_devdata; dev->d_opendata = oh; status = BS->OpenProtocol(handle, &httpsb_guid, (void **)&sb, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (EFI_ERROR(status)) { err = efi_status_to_errno(status); goto end; } status = sb->CreateChild(sb, &oh->http_handle); if (EFI_ERROR(status)) { err = efi_status_to_errno(status); goto end; } status = BS->OpenProtocol(oh->http_handle, &http_guid, (void **)&oh->http, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (EFI_ERROR(status)) { sb->DestroyChild(sb, oh->http_handle); err = efi_status_to_errno(status); goto end; } config.HttpVersion = HttpVersion11; config.TimeOutMillisec = 0; config.LocalAddressIsIPv6 = FALSE; config.AccessPoint.IPv4Node = &config_access; config_access.UseDefaultAddress = TRUE; config_access.LocalPort = 0; status = oh->http->Configure(oh->http, &config); if (EFI_ERROR(status)) { sb->DestroyChild(sb, oh->http_handle); err = efi_status_to_errno(status); goto end; } /* * Here we make attempt to construct a "base" URI by stripping * the last two path components from the loaded URI under the * assumption that it is something like: * * http://127.0.0.1/foo/boot/loader.efi * * hoping to arriving at: * * http://127.0.0.1/foo/ */ len = DevicePathNodeLength(&uri->Header) - sizeof(URI_DEVICE_PATH); oh->uri_base = malloc(len + 1); if (oh->uri_base == NULL) { err = ENOMEM; goto end; } strncpy(oh->uri_base, (const char *)uri->Uri, len); oh->uri_base[len] = '\0'; c = strrchr(oh->uri_base, '/'); if (c != NULL) *c = '\0'; c = strrchr(oh->uri_base, '/'); if (c != NULL && *(c + 1) != '\0') *(c + 1) = '\0'; err = 0; end: if (err != 0) { free(dev->d_opendata); dev->d_opendata = NULL; } return (err); } static int efihttp_dev_close(struct open_file *f) { EFI_SERVICE_BINDING_PROTOCOL *sb; struct devdesc *dev; struct open_efihttp *oh; EFI_STATUS status; dev = (struct devdesc *)f->f_devdata; oh = (struct open_efihttp *)dev->d_opendata; status = BS->OpenProtocol(oh->dev_handle, &httpsb_guid, (void **)&sb, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); sb->DestroyChild(sb, oh->http_handle); free(oh->uri_base); free(oh); dev->d_opendata = NULL; return (0); } static int _efihttp_fs_open(const char *path, struct open_file *f) { EFI_HTTP_CONFIG_DATA config; EFI_HTTPv4_ACCESS_POINT config_access; EFI_HTTP_TOKEN token; EFI_HTTP_MESSAGE message; EFI_HTTP_REQUEST_DATA request; EFI_HTTP_RESPONSE_DATA response; EFI_HTTP_HEADER headers[3]; char *host, *hostp; char *c; struct devdesc *dev; struct open_efihttp *oh; struct file_efihttp *fh; EFI_STATUS status; UINTN i; int polltime; bool done; dev = (struct devdesc *)f->f_devdata; oh = (struct open_efihttp *)dev->d_opendata; fh = calloc(1, sizeof(struct file_efihttp)); if (fh == NULL) return (ENOMEM); f->f_fsdata = fh; fh->path = strdup(path); /* * Reset the HTTP state. * * EDK II's persistent HTTP connection handling is graceless, * assuming that all connections are persistent regardless of * any Connection: header or HTTP version reported by the * server, and failing to send requests when a more sane * implementation would seem to be just reestablishing the * closed connection. * * In the hopes of having some robustness, we indicate to the * server that we will close the connection by using a * Connection: close header. And then here we manually * unconfigure and reconfigure the http instance to force the * connection closed. */ memset(&config, 0, sizeof(config)); memset(&config_access, 0, sizeof(config_access)); config.AccessPoint.IPv4Node = &config_access; status = oh->http->GetModeData(oh->http, &config); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); status = oh->http->Configure(oh->http, NULL); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); status = oh->http->Configure(oh->http, &config); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); /* Send the read request */ done = false; status = BS->CreateEvent(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, notify, &done, &token.Event); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); /* extract the host portion of the URL */ host = strdup(oh->uri_base); if (host == NULL) return (ENOMEM); hostp = host; /* Remove the protocol scheme */ c = strchr(host, '/'); if (c != NULL && *(c + 1) == '/') hostp = (c + 2); /* Remove any path information */ c = strchr(hostp, '/'); if (c != NULL) *c = '\0'; token.Status = EFI_NOT_READY; token.Message = &message; message.Data.Request = &request; message.HeaderCount = 3; message.Headers = headers; message.BodyLength = 0; message.Body = NULL; request.Method = HttpMethodGet; request.Url = calloc(strlen(oh->uri_base) + strlen(path) + 1, 2); headers[0].FieldName = (CHAR8 *)"Host"; headers[0].FieldValue = (CHAR8 *)hostp; headers[1].FieldName = (CHAR8 *)"Connection"; headers[1].FieldValue = (CHAR8 *)"close"; headers[2].FieldName = (CHAR8 *)"Accept"; headers[2].FieldValue = (CHAR8 *)"*/*"; cpy8to16(oh->uri_base, request.Url, strlen(oh->uri_base)); cpy8to16(path, request.Url + strlen(oh->uri_base), strlen(path)); status = oh->http->Request(oh->http, &token); free(request.Url); free(host); if (EFI_ERROR(status)) { BS->CloseEvent(token.Event); return (efi_status_to_errno(status)); } polltime = 0; while (!done && polltime < EFIHTTP_POLL_TIMEOUT) { status = oh->http->Poll(oh->http); if (EFI_ERROR(status)) break; if (!done) { delay(100 * 1000); polltime += 100; } } BS->CloseEvent(token.Event); if (EFI_ERROR(token.Status)) return (efi_status_to_errno(token.Status)); /* Wait for the read response */ done = false; status = BS->CreateEvent(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, notify, &done, &token.Event); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); token.Status = EFI_NOT_READY; token.Message = &message; message.Data.Response = &response; message.HeaderCount = 0; message.Headers = NULL; message.BodyLength = 0; message.Body = NULL; response.StatusCode = HTTP_STATUS_UNSUPPORTED_STATUS; status = oh->http->Response(oh->http, &token); if (EFI_ERROR(status)) { BS->CloseEvent(token.Event); return (efi_status_to_errno(status)); } polltime = 0; while (!done && polltime < EFIHTTP_POLL_TIMEOUT) { status = oh->http->Poll(oh->http); if (EFI_ERROR(status)) break; if (!done) { delay(100 * 1000); polltime += 100; } } BS->CloseEvent(token.Event); if (EFI_ERROR(token.Status)) { BS->FreePool(message.Headers); return (efi_status_to_errno(token.Status)); } if (response.StatusCode != HTTP_STATUS_200_OK) { BS->FreePool(message.Headers); return (EIO); } fh->size = 0; fh->is_dir = false; for (i = 0; i < message.HeaderCount; i++) { if (strcasecmp((const char *)message.Headers[i].FieldName, "Content-Length") == 0) fh->size = strtoul((const char *) message.Headers[i].FieldValue, NULL, 10); else if (strcasecmp((const char *)message.Headers[i].FieldName, "Content-type") == 0) { if (strncmp((const char *)message.Headers[i].FieldValue, "text/html", 9) == 0) fh->is_dir = true; } } return (0); } static int efihttp_fs_open(const char *path, struct open_file *f) { char *path_slash; int err; if (!efihttp_init_done) return (ENXIO); /* * If any path fails to open, try with a trailing slash in * case it's a directory. */ err = _efihttp_fs_open(path, f); if (err != 0) { path_slash = malloc(strlen(path) + 2); if (path_slash == NULL) return (ENOMEM); strcpy(path_slash, path); strcat(path_slash, "/"); err = _efihttp_fs_open(path_slash, f); free(path_slash); } return (err); } static int efihttp_fs_close(struct open_file *f __unused) { return (0); } static int _efihttp_fs_read(struct open_file *f, void *buf, size_t size, size_t *resid) { EFI_HTTP_TOKEN token; EFI_HTTP_MESSAGE message; EFI_STATUS status; struct devdesc *dev; struct open_efihttp *oh; struct file_efihttp *fh; bool done; int polltime; fh = (struct file_efihttp *)f->f_fsdata; if (fh->size > 0 && fh->offset >= fh->size) { if (resid != NULL) *resid = size; return 0; } dev = (struct devdesc *)f->f_devdata; oh = (struct open_efihttp *)dev->d_opendata; done = false; status = BS->CreateEvent(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, notify, &done, &token.Event); if (EFI_ERROR(status)) { return (efi_status_to_errno(status)); } token.Status = EFI_NOT_READY; token.Message = &message; message.Data.Request = NULL; message.HeaderCount = 0; message.Headers = NULL; message.BodyLength = size; message.Body = buf; status = oh->http->Response(oh->http, &token); if (status == EFI_CONNECTION_FIN) { if (resid) *resid = size; return (0); } else if (EFI_ERROR(status)) { BS->CloseEvent(token.Event); return (efi_status_to_errno(status)); } polltime = 0; while (!done && polltime < EFIHTTP_POLL_TIMEOUT) { status = oh->http->Poll(oh->http); if (EFI_ERROR(status)) break; if (!done) { delay(100 * 1000); polltime += 100; } } BS->CloseEvent(token.Event); if (token.Status == EFI_CONNECTION_FIN) { if (resid) *resid = size; return (0); } else if (EFI_ERROR(token.Status)) return (efi_status_to_errno(token.Status)); if (resid) *resid = size - message.BodyLength; fh->offset += message.BodyLength; return (0); } static int efihttp_fs_read(struct open_file *f, void *buf, size_t size, size_t *resid) { size_t res; int err = 0; while (size > 0) { err = _efihttp_fs_read(f, buf, size, &res); if (err != 0 || res == size) goto end; buf += (size - res); size = res; } end: if (resid) *resid = size; return (err); } static int efihttp_fs_write(struct open_file *f __unused, const void *buf __unused, size_t size __unused, size_t *resid __unused) { return (EIO); } static off_t efihttp_fs_seek(struct open_file *f, off_t offset, int where) { struct file_efihttp *fh; char *path; void *buf; size_t res, res2; int err; fh = (struct file_efihttp *)f->f_fsdata; if (where == SEEK_SET && fh->offset == offset) return (0); if (where == SEEK_SET && fh->offset < offset) { buf = malloc(1500); res = offset - fh->offset; while (res > 0) { err = _efihttp_fs_read(f, buf, min(1500, res), &res2); if (err != 0) { free(buf); return (err); } res -= min(1500, res) - res2; } free(buf); return (0); } else if (where == SEEK_SET) { path = fh->path; fh->path = NULL; efihttp_fs_close(f); err = efihttp_fs_open(path, f); free(path); if (err != 0) return (err); return efihttp_fs_seek(f, offset, where); } return (EIO); } static int efihttp_fs_stat(struct open_file *f, struct stat *sb) { struct file_efihttp *fh; fh = (struct file_efihttp *)f->f_fsdata; memset(sb, 0, sizeof(*sb)); sb->st_nlink = 1; sb->st_mode = 0777 | (fh->is_dir ? S_IFDIR : S_IFREG); sb->st_size = fh->size; return (0); } static int efihttp_fs_readdir(struct open_file *f, struct dirent *d) { static char *dirbuf = NULL, *db2, *cursor; static int dirbuf_len = 0; char *end; struct file_efihttp *fh; fh = (struct file_efihttp *)f->f_fsdata; if (dirbuf_len < fh->size) { db2 = realloc(dirbuf, fh->size); if (db2 == NULL) { free(dirbuf); return (ENOMEM); } else dirbuf = db2; dirbuf_len = fh->size; } if (fh->offset != fh->size) { efihttp_fs_seek(f, 0, SEEK_SET); efihttp_fs_read(f, dirbuf, dirbuf_len, NULL); cursor = dirbuf; } cursor = strstr(cursor, "d_type = DT_DIR; } else d->d_type = DT_REG; memcpy(d->d_name, cursor, end - cursor); d->d_name[end - cursor] = '\0'; return (0); }