xref: /freebsd-src/stand/efi/loader/main.c (revision 038457dde93c395cee7746f1f2a484056ab60703)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 2008-2010 Rui Paulo
3ca987d46SWarner Losh  * Copyright (c) 2006 Marcel Moolenaar
4ca987d46SWarner Losh  * All rights reserved.
5ca987d46SWarner Losh  *
6125e3b9dSWarner Losh  * Copyright (c) 2016-2019 Netflix, Inc. written by M. Warner Losh
752467047SWarner Losh  *
8ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
9ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
10ca987d46SWarner Losh  * are met:
11ca987d46SWarner Losh  *
12ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
13ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
14ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
15ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
16ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
17ca987d46SWarner Losh  *
18ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19ca987d46SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20ca987d46SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21ca987d46SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22ca987d46SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23ca987d46SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24ca987d46SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25ca987d46SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26ca987d46SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27ca987d46SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28ca987d46SWarner Losh  */
29ca987d46SWarner Losh 
30c96ac12eSWarner Losh #include <stand.h>
31c96ac12eSWarner Losh 
32ca987d46SWarner Losh #include <sys/disk.h>
33ca987d46SWarner Losh #include <sys/param.h>
34ca987d46SWarner Losh #include <sys/reboot.h>
35c96ac12eSWarner Losh #include <sys/boot.h>
361dc762d4SToomas Soome #ifdef EFI_ZFS_BOOT
37e307eb94SToomas Soome #include <sys/zfs_bootenv.h>
381dc762d4SToomas Soome #endif
39ec9abc18SWarner Losh #include <paths.h>
405b5438c6SMichal Meloun #include <netinet/in.h>
415b5438c6SMichal Meloun #include <netinet/in_systm.h>
42f38658e1SWarner Losh #include <stdint.h>
43ca987d46SWarner Losh #include <string.h>
44ca987d46SWarner Losh #include <setjmp.h>
45ca987d46SWarner Losh #include <disk.h>
465b5438c6SMichal Meloun #include <dev_net.h>
475b5438c6SMichal Meloun #include <net.h>
48780f2892SWarner Losh #include <machine/_inttypes.h>
49ca987d46SWarner Losh 
50ca987d46SWarner Losh #include <efi.h>
51ca987d46SWarner Losh #include <efilib.h>
52b43c6042SWarner Losh #include <efichar.h>
535c73b3e0SColin Percival #include <efirng.h>
54ca987d46SWarner Losh 
55ca987d46SWarner Losh #include <uuid.h>
56ca987d46SWarner Losh 
57ca987d46SWarner Losh #include <bootstrap.h>
58ca987d46SWarner Losh #include <smbios.h>
59ca987d46SWarner Losh 
60c8ebbd28SColin Percival #include <dev/random/fortuna.h>
61c8ebbd28SColin Percival #include <geom/eli/pkcs5v2.h>
62c8ebbd28SColin Percival 
63ca987d46SWarner Losh #include "efizfs.h"
64df065f69SWarner Losh #include "framebuffer.h"
65ca987d46SWarner Losh 
660b01d457SR. Christian McDonald #include "platform/acfreebsd.h"
670b01d457SR. Christian McDonald #include "acconfig.h"
680b01d457SR. Christian McDonald #define ACPI_SYSTEM_XFACE
690b01d457SR. Christian McDonald #include "actypes.h"
700b01d457SR. Christian McDonald #include "actbl.h"
710b01d457SR. Christian McDonald 
72ca987d46SWarner Losh #include "loader_efi.h"
73ca987d46SWarner Losh 
74ca987d46SWarner Losh struct arch_switch archsw;	/* MI/MD interface boundary */
75ca987d46SWarner Losh 
76ca987d46SWarner Losh EFI_GUID acpi = ACPI_TABLE_GUID;
77ca987d46SWarner Losh EFI_GUID acpi20 = ACPI_20_TABLE_GUID;
78ca987d46SWarner Losh EFI_GUID devid = DEVICE_PATH_PROTOCOL;
79ca987d46SWarner Losh EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
80ca987d46SWarner Losh EFI_GUID mps = MPS_TABLE_GUID;
81ca987d46SWarner Losh EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
82ca987d46SWarner Losh EFI_GUID smbios = SMBIOS_TABLE_GUID;
839b2ff7dbSEd Maste EFI_GUID smbios3 = SMBIOS3_TABLE_GUID;
84ca987d46SWarner Losh EFI_GUID dxe = DXE_SERVICES_TABLE_GUID;
85ca987d46SWarner Losh EFI_GUID hoblist = HOB_LIST_TABLE_GUID;
869b2ff7dbSEd Maste EFI_GUID lzmadecomp = LZMA_DECOMPRESSION_GUID;
879b2ff7dbSEd Maste EFI_GUID mpcore = ARM_MP_CORE_INFO_TABLE_GUID;
889b2ff7dbSEd Maste EFI_GUID esrt = ESRT_TABLE_GUID;
89ca987d46SWarner Losh EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID;
90ca987d46SWarner Losh EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID;
91ca987d46SWarner Losh EFI_GUID fdtdtb = FDT_TABLE_GUID;
92ca987d46SWarner Losh EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
93ca987d46SWarner Losh 
94fa9dc8d3SWarner Losh /*
95fa9dc8d3SWarner Losh  * Number of seconds to wait for a keystroke before exiting with failure
96fa9dc8d3SWarner Losh  * in the event no currdev is found. -2 means always break, -1 means
97fa9dc8d3SWarner Losh  * never break, 0 means poll once and then reboot, > 0 means wait for
98fa9dc8d3SWarner Losh  * that many seconds. "fail_timeout" can be set in the environment as
99fa9dc8d3SWarner Losh  * well.
100fa9dc8d3SWarner Losh  */
101fa9dc8d3SWarner Losh static int fail_timeout = 5;
102fa9dc8d3SWarner Losh 
103b43c6042SWarner Losh /*
104b43c6042SWarner Losh  * Current boot variable
105b43c6042SWarner Losh  */
106b43c6042SWarner Losh UINT16 boot_current;
107b43c6042SWarner Losh 
10859a05bdfSWarner Losh /*
10959a05bdfSWarner Losh  * Image that we booted from.
11059a05bdfSWarner Losh  */
11159a05bdfSWarner Losh EFI_LOADED_IMAGE *boot_img;
11259a05bdfSWarner Losh 
113c5f3a7f6SWarner Losh /*
114c5f3a7f6SWarner Losh  * RSDP base table.
115c5f3a7f6SWarner Losh  */
116c5f3a7f6SWarner Losh ACPI_TABLE_RSDP *rsdp;
117c5f3a7f6SWarner Losh 
118c0626257SWarner Losh static bool
119ca987d46SWarner Losh has_keyboard(void)
120ca987d46SWarner Losh {
121ca987d46SWarner Losh 	EFI_STATUS status;
122ca987d46SWarner Losh 	EFI_DEVICE_PATH *path;
123ca987d46SWarner Losh 	EFI_HANDLE *hin, *hin_end, *walker;
124ca987d46SWarner Losh 	UINTN sz;
125c0626257SWarner Losh 	bool retval = false;
126ca987d46SWarner Losh 
127ca987d46SWarner Losh 	/*
128ca987d46SWarner Losh 	 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
129ca987d46SWarner Losh 	 * do the typical dance to get the right sized buffer.
130ca987d46SWarner Losh 	 */
131ca987d46SWarner Losh 	sz = 0;
132ca987d46SWarner Losh 	hin = NULL;
133ca987d46SWarner Losh 	status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0);
134ca987d46SWarner Losh 	if (status == EFI_BUFFER_TOO_SMALL) {
135ca987d46SWarner Losh 		hin = (EFI_HANDLE *)malloc(sz);
136ca987d46SWarner Losh 		status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz,
137ca987d46SWarner Losh 		    hin);
138ca987d46SWarner Losh 		if (EFI_ERROR(status))
139ca987d46SWarner Losh 			free(hin);
140ca987d46SWarner Losh 	}
141ca987d46SWarner Losh 	if (EFI_ERROR(status))
142ca987d46SWarner Losh 		return retval;
143ca987d46SWarner Losh 
144ca987d46SWarner Losh 	/*
145ca987d46SWarner Losh 	 * Look at each of the handles. If it supports the device path protocol,
146ca987d46SWarner Losh 	 * use it to get the device path for this handle. Then see if that
147ca987d46SWarner Losh 	 * device path matches either the USB device path for keyboards or the
148ca987d46SWarner Losh 	 * legacy device path for keyboards.
149ca987d46SWarner Losh 	 */
150ca987d46SWarner Losh 	hin_end = &hin[sz / sizeof(*hin)];
151ca987d46SWarner Losh 	for (walker = hin; walker < hin_end; walker++) {
152110d56cbSToomas Soome 		status = OpenProtocolByHandle(*walker, &devid, (void **)&path);
153ca987d46SWarner Losh 		if (EFI_ERROR(status))
154ca987d46SWarner Losh 			continue;
155ca987d46SWarner Losh 
156ca987d46SWarner Losh 		while (!IsDevicePathEnd(path)) {
157ca987d46SWarner Losh 			/*
158ca987d46SWarner Losh 			 * Check for the ACPI keyboard node. All PNP3xx nodes
159ca987d46SWarner Losh 			 * are keyboards of different flavors. Note: It is
160ca987d46SWarner Losh 			 * unclear of there's always a keyboard node when
161ca987d46SWarner Losh 			 * there's a keyboard controller, or if there's only one
162ca987d46SWarner Losh 			 * when a keyboard is detected at boot.
163ca987d46SWarner Losh 			 */
164ca987d46SWarner Losh 			if (DevicePathType(path) == ACPI_DEVICE_PATH &&
165ca987d46SWarner Losh 			    (DevicePathSubType(path) == ACPI_DP ||
166ca987d46SWarner Losh 				DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
167ca987d46SWarner Losh 				ACPI_HID_DEVICE_PATH  *acpi;
168ca987d46SWarner Losh 
169ca987d46SWarner Losh 				acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
170ca987d46SWarner Losh 				if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 &&
171ca987d46SWarner Losh 				    (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
172c0626257SWarner Losh 					retval = true;
173ca987d46SWarner Losh 					goto out;
174ca987d46SWarner Losh 				}
175ca987d46SWarner Losh 			/*
176ca987d46SWarner Losh 			 * Check for USB keyboard node, if present. Unlike a
177ca987d46SWarner Losh 			 * PS/2 keyboard, these definitely only appear when
178ca987d46SWarner Losh 			 * connected to the system.
179ca987d46SWarner Losh 			 */
180ca987d46SWarner Losh 			} else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
181ca987d46SWarner Losh 			    DevicePathSubType(path) == MSG_USB_CLASS_DP) {
182ca987d46SWarner Losh 				USB_CLASS_DEVICE_PATH *usb;
183ca987d46SWarner Losh 
184ca987d46SWarner Losh 				usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
185ca987d46SWarner Losh 				if (usb->DeviceClass == 3 && /* HID */
186ca987d46SWarner Losh 				    usb->DeviceSubClass == 1 && /* Boot devices */
187ca987d46SWarner Losh 				    usb->DeviceProtocol == 1) { /* Boot keyboards */
188c0626257SWarner Losh 					retval = true;
189ca987d46SWarner Losh 					goto out;
190ca987d46SWarner Losh 				}
191ca987d46SWarner Losh 			}
192ca987d46SWarner Losh 			path = NextDevicePathNode(path);
193ca987d46SWarner Losh 		}
194ca987d46SWarner Losh 	}
195ca987d46SWarner Losh out:
196ca987d46SWarner Losh 	free(hin);
197ca987d46SWarner Losh 	return retval;
198ca987d46SWarner Losh }
199ca987d46SWarner Losh 
200ca987d46SWarner Losh static void
201fa9dc8d3SWarner Losh set_currdev_devdesc(struct devdesc *currdev)
202fa9dc8d3SWarner Losh {
203fa9dc8d3SWarner Losh 	const char *devname;
204fa9dc8d3SWarner Losh 
2051e7a2eb9SWarner Losh 	devname = devformat(currdev);
206fa9dc8d3SWarner Losh 	printf("Setting currdev to %s\n", devname);
207e39cc267SWarner Losh 	set_currdev(devname);
208fa9dc8d3SWarner Losh }
209fa9dc8d3SWarner Losh 
210fa9dc8d3SWarner Losh static void
211fa9dc8d3SWarner Losh set_currdev_devsw(struct devsw *dev, int unit)
212ca987d46SWarner Losh {
213ca987d46SWarner Losh 	struct devdesc currdev;
214ca987d46SWarner Losh 
215ca987d46SWarner Losh 	currdev.d_dev = dev;
216ca987d46SWarner Losh 	currdev.d_unit = unit;
217ca987d46SWarner Losh 
218fa9dc8d3SWarner Losh 	set_currdev_devdesc(&currdev);
219fa9dc8d3SWarner Losh }
220fa9dc8d3SWarner Losh 
221fa9dc8d3SWarner Losh static void
222fa9dc8d3SWarner Losh set_currdev_pdinfo(pdinfo_t *dp)
223fa9dc8d3SWarner Losh {
224fa9dc8d3SWarner Losh 
225fa9dc8d3SWarner Losh 	/*
226fa9dc8d3SWarner Losh 	 * Disks are special: they have partitions. if the parent
227fa9dc8d3SWarner Losh 	 * pointer is non-null, we're a partition not a full disk
228fa9dc8d3SWarner Losh 	 * and we need to adjust currdev appropriately.
229fa9dc8d3SWarner Losh 	 */
230fa9dc8d3SWarner Losh 	if (dp->pd_devsw->dv_type == DEVT_DISK) {
231fa9dc8d3SWarner Losh 		struct disk_devdesc currdev;
232fa9dc8d3SWarner Losh 
233fa9dc8d3SWarner Losh 		currdev.dd.d_dev = dp->pd_devsw;
234fa9dc8d3SWarner Losh 		if (dp->pd_parent == NULL) {
235fa9dc8d3SWarner Losh 			currdev.dd.d_unit = dp->pd_unit;
23614243f8dSIan Lepore 			currdev.d_slice = D_SLICENONE;
23714243f8dSIan Lepore 			currdev.d_partition = D_PARTNONE;
238fa9dc8d3SWarner Losh 		} else {
239fa9dc8d3SWarner Losh 			currdev.dd.d_unit = dp->pd_parent->pd_unit;
240fa9dc8d3SWarner Losh 			currdev.d_slice = dp->pd_unit;
24114243f8dSIan Lepore 			currdev.d_partition = D_PARTISGPT; /* XXX Assumes GPT */
242fa9dc8d3SWarner Losh 		}
243fa9dc8d3SWarner Losh 		set_currdev_devdesc((struct devdesc *)&currdev);
244fa9dc8d3SWarner Losh 	} else {
245fa9dc8d3SWarner Losh 		set_currdev_devsw(dp->pd_devsw, dp->pd_unit);
246fa9dc8d3SWarner Losh 	}
247fa9dc8d3SWarner Losh }
248fa9dc8d3SWarner Losh 
249fa9dc8d3SWarner Losh static bool
250fa9dc8d3SWarner Losh sanity_check_currdev(void)
251fa9dc8d3SWarner Losh {
252fa9dc8d3SWarner Losh 	struct stat st;
253fa9dc8d3SWarner Losh 
254ec9abc18SWarner Losh 	return (stat(PATH_DEFAULTS_LOADER_CONF, &st) == 0 ||
255c7e6f9dcSSimon J. Gerraty #ifdef PATH_BOOTABLE_TOKEN
256c7e6f9dcSSimon J. Gerraty 	    stat(PATH_BOOTABLE_TOKEN, &st) == 0 || /* non-standard layout */
257c7e6f9dcSSimon J. Gerraty #endif
258ec9abc18SWarner Losh 	    stat(PATH_KERNEL, &st) == 0);
259fa9dc8d3SWarner Losh }
260fa9dc8d3SWarner Losh 
261fa9dc8d3SWarner Losh #ifdef EFI_ZFS_BOOT
262fa9dc8d3SWarner Losh static bool
263fa9dc8d3SWarner Losh probe_zfs_currdev(uint64_t guid)
264fa9dc8d3SWarner Losh {
265e3e2681dSGleb Smirnoff 	char buf[VDEV_PAD_SIZE];
266fa9dc8d3SWarner Losh 	char *devname;
267fa9dc8d3SWarner Losh 	struct zfs_devdesc currdev;
268fa9dc8d3SWarner Losh 
269fa9dc8d3SWarner Losh 	currdev.dd.d_dev = &zfs_dev;
270fa9dc8d3SWarner Losh 	currdev.dd.d_unit = 0;
271fa9dc8d3SWarner Losh 	currdev.pool_guid = guid;
272fa9dc8d3SWarner Losh 	currdev.root_guid = 0;
2731e7a2eb9SWarner Losh 	devname = devformat(&currdev.dd);
2742425dbdfSWarner Losh 	set_currdev(devname);
2752425dbdfSWarner Losh 	printf("Setting currdev to %s\n", devname);
276277f38abSMariusz Zaborski 	init_zfs_boot_options(devname);
277fa9dc8d3SWarner Losh 
278d5babd0dSWarner Losh 	if (zfs_get_bootonce(&currdev, OS_BOOTONCE, buf, sizeof(buf)) == 0) {
279e307eb94SToomas Soome 		printf("zfs bootonce: %s\n", buf);
2803830659eSToomas Soome 		set_currdev(buf);
281e307eb94SToomas Soome 		setenv("zfs-bootonce", buf, 1);
2823830659eSToomas Soome 	}
283d13550f0SGleb Smirnoff 	(void)zfs_attach_nvstore(&currdev);
284d5babd0dSWarner Losh 
285e3e2681dSGleb Smirnoff 	return (sanity_check_currdev());
286fa9dc8d3SWarner Losh }
287fa9dc8d3SWarner Losh #endif
288fa9dc8d3SWarner Losh 
2895984246fSYongbo Yao #ifdef MD_IMAGE_SIZE
290b2822c40SWarner Losh extern struct devsw md_dev;
291b2822c40SWarner Losh 
2925984246fSYongbo Yao static bool
2935984246fSYongbo Yao probe_md_currdev(void)
2945984246fSYongbo Yao {
2955984246fSYongbo Yao 	bool rv;
2965984246fSYongbo Yao 
2975984246fSYongbo Yao 	set_currdev_devsw(&md_dev, 0);
2985984246fSYongbo Yao 	rv = sanity_check_currdev();
2995984246fSYongbo Yao 	if (!rv)
3005984246fSYongbo Yao 		printf("MD not present\n");
3015984246fSYongbo Yao 	return (rv);
3025984246fSYongbo Yao }
3035984246fSYongbo Yao #endif
3045984246fSYongbo Yao 
305fa9dc8d3SWarner Losh static bool
306fa9dc8d3SWarner Losh try_as_currdev(pdinfo_t *hd, pdinfo_t *pp)
307fa9dc8d3SWarner Losh {
308fa9dc8d3SWarner Losh 	uint64_t guid;
309fa9dc8d3SWarner Losh 
310fa9dc8d3SWarner Losh #ifdef EFI_ZFS_BOOT
311fa9dc8d3SWarner Losh 	/*
312fa9dc8d3SWarner Losh 	 * If there's a zpool on this device, try it as a ZFS
313fa9dc8d3SWarner Losh 	 * filesystem, which has somewhat different setup than all
314fa9dc8d3SWarner Losh 	 * other types of fs due to imperfect loader integration.
315fa9dc8d3SWarner Losh 	 * This all stems from ZFS being both a device (zpool) and
316fa9dc8d3SWarner Losh 	 * a filesystem, plus the boot env feature.
317fa9dc8d3SWarner Losh 	 */
318fa9dc8d3SWarner Losh 	if (efizfs_get_guid_by_handle(pp->pd_handle, &guid))
319fa9dc8d3SWarner Losh 		return (probe_zfs_currdev(guid));
320fa9dc8d3SWarner Losh #endif
321fa9dc8d3SWarner Losh 	/*
322fa9dc8d3SWarner Losh 	 * All other filesystems just need the pdinfo
323fa9dc8d3SWarner Losh 	 * initialized in the standard way.
324fa9dc8d3SWarner Losh 	 */
325fa9dc8d3SWarner Losh 	set_currdev_pdinfo(pp);
326fa9dc8d3SWarner Losh 	return (sanity_check_currdev());
327ca987d46SWarner Losh }
328ca987d46SWarner Losh 
329b43c6042SWarner Losh /*
330b43c6042SWarner Losh  * Sometimes we get filenames that are all upper case
331b43c6042SWarner Losh  * and/or have backslashes in them. Filter all this out
332b43c6042SWarner Losh  * if it looks like we need to do so.
333b43c6042SWarner Losh  */
334b43c6042SWarner Losh static void
335b43c6042SWarner Losh fix_dosisms(char *p)
336b43c6042SWarner Losh {
337b43c6042SWarner Losh 	while (*p) {
338b43c6042SWarner Losh 		if (isupper(*p))
339b43c6042SWarner Losh 			*p = tolower(*p);
340b43c6042SWarner Losh 		else if (*p == '\\')
341b43c6042SWarner Losh 			*p = '/';
342b43c6042SWarner Losh 		p++;
343b43c6042SWarner Losh 	}
344b43c6042SWarner Losh }
345b43c6042SWarner Losh 
34648f0136dSWarner Losh #define SIZE(dp, edp) (size_t)((intptr_t)(void *)edp - (intptr_t)(void *)dp)
34748f0136dSWarner Losh 
348b43c6042SWarner Losh enum { BOOT_INFO_OK = 0, BAD_CHOICE = 1, NOT_SPECIFIC = 2  };
349ca987d46SWarner Losh static int
35059a05bdfSWarner Losh match_boot_info(char *boot_info, size_t bisz)
351b43c6042SWarner Losh {
352b43c6042SWarner Losh 	uint32_t attr;
353b43c6042SWarner Losh 	uint16_t fplen;
354b43c6042SWarner Losh 	size_t len;
355b43c6042SWarner Losh 	char *walker, *ep;
356b43c6042SWarner Losh 	EFI_DEVICE_PATH *dp, *edp, *first_dp, *last_dp;
357b43c6042SWarner Losh 	pdinfo_t *pp;
358b43c6042SWarner Losh 	CHAR16 *descr;
359b43c6042SWarner Losh 	char *kernel = NULL;
360b43c6042SWarner Losh 	FILEPATH_DEVICE_PATH  *fp;
361b43c6042SWarner Losh 	struct stat st;
3628c18b82bSWarner Losh 	CHAR16 *text;
363b43c6042SWarner Losh 
364b43c6042SWarner Losh 	/*
365670d2ad8SEric van Gyzen 	 * FreeBSD encodes its boot loading path into the boot loader
366b43c6042SWarner Losh 	 * BootXXXX variable. We look for the last one in the path
367670d2ad8SEric van Gyzen 	 * and use that to load the kernel. However, if we only find
368b43c6042SWarner Losh 	 * one DEVICE_PATH, then there's nothing specific and we should
369b43c6042SWarner Losh 	 * fall back.
370b43c6042SWarner Losh 	 *
371b43c6042SWarner Losh 	 * In an ideal world, we'd look at the image handle we were
372b43c6042SWarner Losh 	 * passed, match up with the loader we are and then return the
373b43c6042SWarner Losh 	 * next one in the path. This would be most flexible and cover
374b43c6042SWarner Losh 	 * many chain booting scenarios where you need to use this
375b43c6042SWarner Losh 	 * boot loader to get to the next boot loader. However, that
376b43c6042SWarner Losh 	 * doesn't work. We rarely have the path to the image booted
377b43c6042SWarner Losh 	 * (just the device) so we can't count on that. So, we do the
378670d2ad8SEric van Gyzen 	 * next best thing: we look through the device path(s) passed
379670d2ad8SEric van Gyzen 	 * in the BootXXXX variable. If there's only one, we return
380b43c6042SWarner Losh 	 * NOT_SPECIFIC. Otherwise, we look at the last one and try to
381b43c6042SWarner Losh 	 * load that. If we can, we return BOOT_INFO_OK. Otherwise we
382b43c6042SWarner Losh 	 * return BAD_CHOICE for the caller to sort out.
383b43c6042SWarner Losh 	 */
384b43c6042SWarner Losh 	if (bisz < sizeof(attr) + sizeof(fplen) + sizeof(CHAR16))
385b43c6042SWarner Losh 		return NOT_SPECIFIC;
386b43c6042SWarner Losh 	walker = boot_info;
387b43c6042SWarner Losh 	ep = walker + bisz;
388b43c6042SWarner Losh 	memcpy(&attr, walker, sizeof(attr));
389b43c6042SWarner Losh 	walker += sizeof(attr);
390b43c6042SWarner Losh 	memcpy(&fplen, walker, sizeof(fplen));
391b43c6042SWarner Losh 	walker += sizeof(fplen);
392b43c6042SWarner Losh 	descr = (CHAR16 *)(intptr_t)walker;
393b43c6042SWarner Losh 	len = ucs2len(descr);
394b43c6042SWarner Losh 	walker += (len + 1) * sizeof(CHAR16);
395b43c6042SWarner Losh 	last_dp = first_dp = dp = (EFI_DEVICE_PATH *)walker;
396b43c6042SWarner Losh 	edp = (EFI_DEVICE_PATH *)(walker + fplen);
397b43c6042SWarner Losh 	if ((char *)edp > ep)
398b43c6042SWarner Losh 		return NOT_SPECIFIC;
39948f0136dSWarner Losh 	while (dp < edp && SIZE(dp, edp) > sizeof(EFI_DEVICE_PATH)) {
4008c18b82bSWarner Losh 		text = efi_devpath_name(dp);
4018c18b82bSWarner Losh 		if (text != NULL) {
4028c18b82bSWarner Losh 			printf("   BootInfo Path: %S\n", text);
4038c18b82bSWarner Losh 			efi_free_devpath_name(text);
4048c18b82bSWarner Losh 		}
405b43c6042SWarner Losh 		last_dp = dp;
406b43c6042SWarner Losh 		dp = (EFI_DEVICE_PATH *)((char *)dp + efi_devpath_length(dp));
407b43c6042SWarner Losh 	}
408b43c6042SWarner Losh 
409b43c6042SWarner Losh 	/*
410b43c6042SWarner Losh 	 * If there's only one item in the list, then nothing was
4111e354803SWarner Losh 	 * specified. Or if the last path doesn't have a media
4121e354803SWarner Losh 	 * path in it. Those show up as various VenHw() nodes
4131e354803SWarner Losh 	 * which are basically opaque to us. Don't count those
4141e354803SWarner Losh 	 * as something specifc.
415b43c6042SWarner Losh 	 */
4168c18b82bSWarner Losh 	if (last_dp == first_dp) {
41734b3f88fSWarner Losh 		printf("Ignoring Boot%04x: Only one DP found\n", boot_current);
418b43c6042SWarner Losh 		return NOT_SPECIFIC;
4198c18b82bSWarner Losh 	}
42034b3f88fSWarner Losh 	if (efi_devpath_to_media_path(last_dp) == NULL) {
42134b3f88fSWarner Losh 		printf("Ignoring Boot%04x: No Media Path\n", boot_current);
4221e354803SWarner Losh 		return NOT_SPECIFIC;
4238c18b82bSWarner Losh 	}
424b43c6042SWarner Losh 
425b43c6042SWarner Losh 	/*
426b43c6042SWarner Losh 	 * OK. At this point we either have a good path or a bad one.
427b43c6042SWarner Losh 	 * Let's check.
428b43c6042SWarner Losh 	 */
429b43c6042SWarner Losh 	pp = efiblk_get_pdinfo_by_device_path(last_dp);
4308c18b82bSWarner Losh 	if (pp == NULL) {
43134b3f88fSWarner Losh 		printf("Ignoring Boot%04x: Device Path not found\n", boot_current);
432b43c6042SWarner Losh 		return BAD_CHOICE;
4338c18b82bSWarner Losh 	}
434b43c6042SWarner Losh 	set_currdev_pdinfo(pp);
4358c18b82bSWarner Losh 	if (!sanity_check_currdev()) {
43634b3f88fSWarner Losh 		printf("Ignoring Boot%04x: sanity check failed\n", boot_current);
437b43c6042SWarner Losh 		return BAD_CHOICE;
4388c18b82bSWarner Losh 	}
439b43c6042SWarner Losh 
440b43c6042SWarner Losh 	/*
441b43c6042SWarner Losh 	 * OK. We've found a device that matches, next we need to check the last
442b43c6042SWarner Losh 	 * component of the path. If it's a file, then we set the default kernel
443b43c6042SWarner Losh 	 * to that. Otherwise, just use this as the default root.
444b43c6042SWarner Losh 	 *
445b43c6042SWarner Losh 	 * Reminder: we're running very early, before we've parsed the defaults
446b43c6042SWarner Losh 	 * file, so we may need to have a hack override.
447b43c6042SWarner Losh 	 */
448b43c6042SWarner Losh 	dp = efi_devpath_last_node(last_dp);
449b43c6042SWarner Losh 	if (DevicePathType(dp) !=  MEDIA_DEVICE_PATH ||
4508c18b82bSWarner Losh 	    DevicePathSubType(dp) != MEDIA_FILEPATH_DP) {
45134b3f88fSWarner Losh 		printf("Using Boot%04x for root partition\n", boot_current);
452b43c6042SWarner Losh 		return (BOOT_INFO_OK);		/* use currdir, default kernel */
4538c18b82bSWarner Losh 	}
454b43c6042SWarner Losh 	fp = (FILEPATH_DEVICE_PATH *)dp;
455b43c6042SWarner Losh 	ucs2_to_utf8(fp->PathName, &kernel);
4568c18b82bSWarner Losh 	if (kernel == NULL) {
45734b3f88fSWarner Losh 		printf("Not using Boot%04x: can't decode kernel\n", boot_current);
458b43c6042SWarner Losh 		return (BAD_CHOICE);
4598c18b82bSWarner Losh 	}
460b43c6042SWarner Losh 	if (*kernel == '\\' || isupper(*kernel))
461b43c6042SWarner Losh 		fix_dosisms(kernel);
462b43c6042SWarner Losh 	if (stat(kernel, &st) != 0) {
463b43c6042SWarner Losh 		free(kernel);
46434b3f88fSWarner Losh 		printf("Not using Boot%04x: can't find %s\n", boot_current,
46534b3f88fSWarner Losh 		    kernel);
466b43c6042SWarner Losh 		return (BAD_CHOICE);
467b43c6042SWarner Losh 	}
468b43c6042SWarner Losh 	setenv("kernel", kernel, 1);
469b43c6042SWarner Losh 	free(kernel);
4708c18b82bSWarner Losh 	text = efi_devpath_name(last_dp);
4718c18b82bSWarner Losh 	if (text) {
47208fa847aSWarner Losh 		printf("Using Boot%04x %S + %s\n", boot_current, text,
47334b3f88fSWarner Losh 		    kernel);
4748c18b82bSWarner Losh 		efi_free_devpath_name(text);
4758c18b82bSWarner Losh 	}
476b43c6042SWarner Losh 
477b43c6042SWarner Losh 	return (BOOT_INFO_OK);
478b43c6042SWarner Losh }
479b43c6042SWarner Losh 
480b43c6042SWarner Losh /*
481b43c6042SWarner Losh  * Look at the passed-in boot_info, if any. If we find it then we need
482b43c6042SWarner Losh  * to see if we can find ourselves in the boot chain. If we can, and
483b43c6042SWarner Losh  * there's another specified thing to boot next, assume that the file
484b43c6042SWarner Losh  * is loaded from / and use that for the root filesystem. If can't
485b43c6042SWarner Losh  * find the specified thing, we must fail the boot. If we're last on
486b43c6042SWarner Losh  * the list, then we fallback to looking for the first available /
487b43c6042SWarner Losh  * candidate (ZFS, if there's a bootable zpool, otherwise a UFS
488b43c6042SWarner Losh  * partition that has either /boot/defaults/loader.conf on it or
489b43c6042SWarner Losh  * /boot/kernel/kernel (the default kernel) that we can use.
490b43c6042SWarner Losh  *
491b43c6042SWarner Losh  * We always fail if we can't find the right thing. However, as
492b43c6042SWarner Losh  * a concession to buggy UEFI implementations, like u-boot, if
493b43c6042SWarner Losh  * we have determined that the host is violating the UEFI boot
494b43c6042SWarner Losh  * manager protocol, we'll signal the rest of the program that
495b43c6042SWarner Losh  * a drop to the OK boot loader prompt is possible.
496b43c6042SWarner Losh  */
497b43c6042SWarner Losh static int
49859a05bdfSWarner Losh find_currdev(bool do_bootmgr, bool is_last,
499b43c6042SWarner Losh     char *boot_info, size_t boot_info_sz)
500ca987d46SWarner Losh {
501ca987d46SWarner Losh 	pdinfo_t *dp, *pp;
502ca987d46SWarner Losh 	EFI_DEVICE_PATH *devpath, *copy;
503ca987d46SWarner Losh 	EFI_HANDLE h;
504fa9dc8d3SWarner Losh 	CHAR16 *text;
505ca987d46SWarner Losh 	struct devsw *dev;
506ca987d46SWarner Losh 	int unit;
507ca987d46SWarner Losh 	uint64_t extra;
508b43c6042SWarner Losh 	int rv;
509e39cc267SWarner Losh 	char *rootdev;
510e39cc267SWarner Losh 
511b43c6042SWarner Losh 	/*
512b43c6042SWarner Losh 	 * First choice: if rootdev is already set, use that, even if
513b43c6042SWarner Losh 	 * it's wrong.
514b43c6042SWarner Losh 	 */
515e39cc267SWarner Losh 	rootdev = getenv("rootdev");
516e39cc267SWarner Losh 	if (rootdev != NULL) {
517bc6053b3SWarner Losh 		printf("    Setting currdev to configured rootdev %s\n",
518bc6053b3SWarner Losh 		    rootdev);
519e39cc267SWarner Losh 		set_currdev(rootdev);
520e39cc267SWarner Losh 		return (0);
521e39cc267SWarner Losh 	}
522ca987d46SWarner Losh 
523b43c6042SWarner Losh 	/*
524bc6053b3SWarner Losh 	 * Second choice: If uefi_rootdev is set, translate that UEFI device
525bc6053b3SWarner Losh 	 * path to the loader's internal name and use that.
526bc6053b3SWarner Losh 	 */
527bc6053b3SWarner Losh 	do {
528bc6053b3SWarner Losh 		rootdev = getenv("uefi_rootdev");
529bc6053b3SWarner Losh 		if (rootdev == NULL)
530bc6053b3SWarner Losh 			break;
531bc6053b3SWarner Losh 		devpath = efi_name_to_devpath(rootdev);
532bc6053b3SWarner Losh 		if (devpath == NULL)
533bc6053b3SWarner Losh 			break;
534bc6053b3SWarner Losh 		dp = efiblk_get_pdinfo_by_device_path(devpath);
535bc6053b3SWarner Losh 		efi_devpath_free(devpath);
536bc6053b3SWarner Losh 		if (dp == NULL)
537bc6053b3SWarner Losh 			break;
538bc6053b3SWarner Losh 		printf("    Setting currdev to UEFI path %s\n",
539bc6053b3SWarner Losh 		    rootdev);
540bc6053b3SWarner Losh 		set_currdev_pdinfo(dp);
541bc6053b3SWarner Losh 		return (0);
542bc6053b3SWarner Losh 	} while (0);
543bc6053b3SWarner Losh 
544bc6053b3SWarner Losh 	/*
545bc6053b3SWarner Losh 	 * Third choice: If we can find out image boot_info, and there's
546b43c6042SWarner Losh 	 * a follow-on boot image in that boot_info, use that. In this
547b43c6042SWarner Losh 	 * case root will be the partition specified in that image and
548b43c6042SWarner Losh 	 * we'll load the kernel specified by the file path. Should there
549b43c6042SWarner Losh 	 * not be a filepath, we use the default. This filepath overrides
550b43c6042SWarner Losh 	 * loader.conf.
551b43c6042SWarner Losh 	 */
552b43c6042SWarner Losh 	if (do_bootmgr) {
55359a05bdfSWarner Losh 		rv = match_boot_info(boot_info, boot_info_sz);
554b43c6042SWarner Losh 		switch (rv) {
555b43c6042SWarner Losh 		case BOOT_INFO_OK:	/* We found it */
556b43c6042SWarner Losh 			return (0);
557b43c6042SWarner Losh 		case BAD_CHOICE:	/* specified file not found -> error */
558b43c6042SWarner Losh 			/* XXX do we want to have an escape hatch for last in boot order? */
559b43c6042SWarner Losh 			return (ENOENT);
560b43c6042SWarner Losh 		} /* Nothing specified, try normal match */
561b43c6042SWarner Losh 	}
562b43c6042SWarner Losh 
563ca987d46SWarner Losh #ifdef EFI_ZFS_BOOT
564fa9dc8d3SWarner Losh 	/*
565fa9dc8d3SWarner Losh 	 * Did efi_zfs_probe() detect the boot pool? If so, use the zpool
566fa9dc8d3SWarner Losh 	 * it found, if it's sane. ZFS is the only thing that looks for
567fa9dc8d3SWarner Losh 	 * disks and pools to boot. This may change in the future, however,
568fa9dc8d3SWarner Losh 	 * if we allow specifying which pool to boot from via UEFI variables
569fa9dc8d3SWarner Losh 	 * rather than the bootenv stuff that FreeBSD uses today.
570fa9dc8d3SWarner Losh 	 */
571ca987d46SWarner Losh 	if (pool_guid != 0) {
572fa9dc8d3SWarner Losh 		printf("Trying ZFS pool\n");
573fa9dc8d3SWarner Losh 		if (probe_zfs_currdev(pool_guid))
574ca987d46SWarner Losh 			return (0);
575ca987d46SWarner Losh 	}
576ca987d46SWarner Losh #endif /* EFI_ZFS_BOOT */
577ca987d46SWarner Losh 
5785984246fSYongbo Yao #ifdef MD_IMAGE_SIZE
5795984246fSYongbo Yao 	/*
5805984246fSYongbo Yao 	 * If there is an embedded MD, try to use that.
5815984246fSYongbo Yao 	 */
5825984246fSYongbo Yao 	printf("Trying MD\n");
5835984246fSYongbo Yao 	if (probe_md_currdev())
5845984246fSYongbo Yao 		return (0);
5855984246fSYongbo Yao #endif /* MD_IMAGE_SIZE */
5865984246fSYongbo Yao 
587fa9dc8d3SWarner Losh 	/*
588fa9dc8d3SWarner Losh 	 * Try to find the block device by its handle based on the
589fa9dc8d3SWarner Losh 	 * image we're booting. If we can't find a sane partition,
590fa9dc8d3SWarner Losh 	 * search all the other partitions of the disk. We do not
591fa9dc8d3SWarner Losh 	 * search other disks because it's a violation of the UEFI
592fa9dc8d3SWarner Losh 	 * boot protocol to do so. We fail and let UEFI go on to
593fa9dc8d3SWarner Losh 	 * the next candidate.
594fa9dc8d3SWarner Losh 	 */
59559a05bdfSWarner Losh 	dp = efiblk_get_pdinfo_by_handle(boot_img->DeviceHandle);
596fa9dc8d3SWarner Losh 	if (dp != NULL) {
597fa9dc8d3SWarner Losh 		text = efi_devpath_name(dp->pd_devpath);
598fa9dc8d3SWarner Losh 		if (text != NULL) {
599fa9dc8d3SWarner Losh 			printf("Trying ESP: %S\n", text);
600fa9dc8d3SWarner Losh 			efi_free_devpath_name(text);
601ca987d46SWarner Losh 		}
602fa9dc8d3SWarner Losh 		set_currdev_pdinfo(dp);
603fa9dc8d3SWarner Losh 		if (sanity_check_currdev())
604fa9dc8d3SWarner Losh 			return (0);
605fa9dc8d3SWarner Losh 		if (dp->pd_parent != NULL) {
6068c18b82bSWarner Losh 			pdinfo_t *espdp = dp;
607fa9dc8d3SWarner Losh 			dp = dp->pd_parent;
608ca987d46SWarner Losh 			STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
6098c18b82bSWarner Losh 				/* Already tried the ESP */
6108c18b82bSWarner Losh 				if (espdp == pp)
6118c18b82bSWarner Losh 					continue;
612fa9dc8d3SWarner Losh 				/*
613fa9dc8d3SWarner Losh 				 * Roll up the ZFS special case
614fa9dc8d3SWarner Losh 				 * for those partitions that have
615b43c6042SWarner Losh 				 * zpools on them.
616fa9dc8d3SWarner Losh 				 */
6178c18b82bSWarner Losh 				text = efi_devpath_name(pp->pd_devpath);
6188c18b82bSWarner Losh 				if (text != NULL) {
6198c18b82bSWarner Losh 					printf("Trying: %S\n", text);
6208c18b82bSWarner Losh 					efi_free_devpath_name(text);
6218c18b82bSWarner Losh 				}
622fa9dc8d3SWarner Losh 				if (try_as_currdev(dp, pp))
623ca987d46SWarner Losh 					return (0);
624ca987d46SWarner Losh 			}
625ca987d46SWarner Losh 		}
626ca987d46SWarner Losh 	}
627ca987d46SWarner Losh 
628ca987d46SWarner Losh 	/*
629ca987d46SWarner Losh 	 * Try the device handle from our loaded image first.  If that
630ca987d46SWarner Losh 	 * fails, use the device path from the loaded image and see if
631ca987d46SWarner Losh 	 * any of the nodes in that path match one of the enumerated
632fa9dc8d3SWarner Losh 	 * handles. Currently, this handle list is only for netboot.
633ca987d46SWarner Losh 	 */
63459a05bdfSWarner Losh 	if (efi_handle_lookup(boot_img->DeviceHandle, &dev, &unit, &extra) == 0) {
635fa9dc8d3SWarner Losh 		set_currdev_devsw(dev, unit);
636fa9dc8d3SWarner Losh 		if (sanity_check_currdev())
637ca987d46SWarner Losh 			return (0);
638ca987d46SWarner Losh 	}
639ca987d46SWarner Losh 
640ca987d46SWarner Losh 	copy = NULL;
641ca987d46SWarner Losh 	devpath = efi_lookup_image_devpath(IH);
642ca987d46SWarner Losh 	while (devpath != NULL) {
643ca987d46SWarner Losh 		h = efi_devpath_handle(devpath);
644ca987d46SWarner Losh 		if (h == NULL)
645ca987d46SWarner Losh 			break;
646ca987d46SWarner Losh 
647ca987d46SWarner Losh 		free(copy);
648ca987d46SWarner Losh 		copy = NULL;
649ca987d46SWarner Losh 
650ca987d46SWarner Losh 		if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
651fa9dc8d3SWarner Losh 			set_currdev_devsw(dev, unit);
652fa9dc8d3SWarner Losh 			if (sanity_check_currdev())
653ca987d46SWarner Losh 				return (0);
654ca987d46SWarner Losh 		}
655ca987d46SWarner Losh 
656ca987d46SWarner Losh 		devpath = efi_lookup_devpath(h);
657ca987d46SWarner Losh 		if (devpath != NULL) {
658ca987d46SWarner Losh 			copy = efi_devpath_trim(devpath);
659ca987d46SWarner Losh 			devpath = copy;
660ca987d46SWarner Losh 		}
661ca987d46SWarner Losh 	}
662ca987d46SWarner Losh 	free(copy);
663ca987d46SWarner Losh 
664ca987d46SWarner Losh 	return (ENOENT);
665ca987d46SWarner Losh }
666ca987d46SWarner Losh 
667fa9dc8d3SWarner Losh static bool
668fa9dc8d3SWarner Losh interactive_interrupt(const char *msg)
669fa9dc8d3SWarner Losh {
670fa9dc8d3SWarner Losh 	time_t now, then, last;
671fa9dc8d3SWarner Losh 
672fa9dc8d3SWarner Losh 	last = 0;
673fa9dc8d3SWarner Losh 	now = then = getsecs();
674fa9dc8d3SWarner Losh 	printf("%s\n", msg);
675fa9dc8d3SWarner Losh 	if (fail_timeout == -2)		/* Always break to OK */
676fa9dc8d3SWarner Losh 		return (true);
677fa9dc8d3SWarner Losh 	if (fail_timeout == -1)		/* Never break to OK */
678fa9dc8d3SWarner Losh 		return (false);
679fa9dc8d3SWarner Losh 	do {
680fa9dc8d3SWarner Losh 		if (last != now) {
681fa9dc8d3SWarner Losh 			printf("press any key to interrupt reboot in %d seconds\r",
682fa9dc8d3SWarner Losh 			    fail_timeout - (int)(now - then));
683fa9dc8d3SWarner Losh 			last = now;
684fa9dc8d3SWarner Losh 		}
685fa9dc8d3SWarner Losh 
686fa9dc8d3SWarner Losh 		/* XXX no pause or timeout wait for char */
687fa9dc8d3SWarner Losh 		if (ischar())
688fa9dc8d3SWarner Losh 			return (true);
689fa9dc8d3SWarner Losh 		now = getsecs();
690fa9dc8d3SWarner Losh 	} while (now - then < fail_timeout);
691fa9dc8d3SWarner Losh 	return (false);
692fa9dc8d3SWarner Losh }
693fa9dc8d3SWarner Losh 
6943fecfbebSWarner Losh static int
6953fecfbebSWarner Losh parse_args(int argc, CHAR16 *argv[])
696ca987d46SWarner Losh {
697667419d5SWarner Losh 	int i, howto;
698e8282eabSWarner Losh 	char var[128];
699ca987d46SWarner Losh 
700ca987d46SWarner Losh 	/*
701ca987d46SWarner Losh 	 * Parse the args to set the console settings, etc
702ca987d46SWarner Losh 	 * boot1.efi passes these in, if it can read /boot.config or /boot/config
703fa9dc8d3SWarner Losh 	 * or iPXE may be setup to pass these in. Or the optional argument in the
704fa9dc8d3SWarner Losh 	 * boot environment was used to pass these arguments in (in which case
705fa9dc8d3SWarner Losh 	 * neither /boot.config nor /boot/config are consulted).
706ca987d46SWarner Losh 	 *
707ca987d46SWarner Losh 	 * Loop through the args, and for each one that contains an '=' that is
708ca987d46SWarner Losh 	 * not the first character, add it to the environment.  This allows
709ca987d46SWarner Losh 	 * loader and kernel env vars to be passed on the command line.  Convert
710fa9dc8d3SWarner Losh 	 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied (though this
711fa9dc8d3SWarner Losh 	 * method is flawed for non-ASCII characters).
712ca987d46SWarner Losh 	 */
713ca987d46SWarner Losh 	howto = 0;
7142b3543dbSMichael Gmelin 	for (i = 0; i < argc; i++) {
7154569e913SWarner Losh 		cpy16to8(argv[i], var, sizeof(var));
7164569e913SWarner Losh 		howto |= boot_parse_arg(var);
7174569e913SWarner Losh 	}
718ca987d46SWarner Losh 
719e8282eabSWarner Losh 	return (howto);
720e8282eabSWarner Losh }
721e8282eabSWarner Losh 
722e78f6dd7SWarner Losh static void
723e78f6dd7SWarner Losh setenv_int(const char *key, int val)
724e78f6dd7SWarner Losh {
725e78f6dd7SWarner Losh 	char buf[20];
726e78f6dd7SWarner Losh 
727e78f6dd7SWarner Losh 	snprintf(buf, sizeof(buf), "%d", val);
728e78f6dd7SWarner Losh 	setenv(key, buf, 1);
729e78f6dd7SWarner Losh }
730e78f6dd7SWarner Losh 
73170253b53SWarner Losh static void *
73270253b53SWarner Losh acpi_map_sdt(vm_offset_t addr)
73370253b53SWarner Losh {
73470253b53SWarner Losh 	/* PA == VA */
73570253b53SWarner Losh 	return ((void *)addr);
73670253b53SWarner Losh }
73770253b53SWarner Losh 
73870253b53SWarner Losh static int
73970253b53SWarner Losh acpi_checksum(void *p, size_t length)
74070253b53SWarner Losh {
74170253b53SWarner Losh 	uint8_t *bp;
74270253b53SWarner Losh 	uint8_t sum;
74370253b53SWarner Losh 
74470253b53SWarner Losh 	bp = p;
74570253b53SWarner Losh 	sum = 0;
74670253b53SWarner Losh 	while (length--)
74770253b53SWarner Losh 		sum += *bp++;
74870253b53SWarner Losh 
74970253b53SWarner Losh 	return (sum);
75070253b53SWarner Losh }
75170253b53SWarner Losh 
75270253b53SWarner Losh static void *
75370253b53SWarner Losh acpi_find_table(uint8_t *sig)
75470253b53SWarner Losh {
75570253b53SWarner Losh 	int entries, i, addr_size;
75670253b53SWarner Losh 	ACPI_TABLE_HEADER *sdp;
75770253b53SWarner Losh 	ACPI_TABLE_RSDT *rsdt;
75870253b53SWarner Losh 	ACPI_TABLE_XSDT *xsdt;
75970253b53SWarner Losh 	vm_offset_t addr;
76070253b53SWarner Losh 
76170253b53SWarner Losh 	if (rsdp == NULL)
76270253b53SWarner Losh 		return (NULL);
76370253b53SWarner Losh 
76470253b53SWarner Losh 	rsdt = (ACPI_TABLE_RSDT *)(uintptr_t)rsdp->RsdtPhysicalAddress;
76570253b53SWarner Losh 	xsdt = (ACPI_TABLE_XSDT *)(uintptr_t)rsdp->XsdtPhysicalAddress;
76670253b53SWarner Losh 	if (rsdp->Revision < 2) {
76770253b53SWarner Losh 		sdp = (ACPI_TABLE_HEADER *)rsdt;
76870253b53SWarner Losh 		addr_size = sizeof(uint32_t);
76970253b53SWarner Losh 	} else {
77070253b53SWarner Losh 		sdp = (ACPI_TABLE_HEADER *)xsdt;
77170253b53SWarner Losh 		addr_size = sizeof(uint64_t);
77270253b53SWarner Losh 	}
77370253b53SWarner Losh 	entries = (sdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
77470253b53SWarner Losh 	for (i = 0; i < entries; i++) {
77570253b53SWarner Losh 		if (addr_size == 4)
77670253b53SWarner Losh 			addr = le32toh(rsdt->TableOffsetEntry[i]);
77770253b53SWarner Losh 		else
77870253b53SWarner Losh 			addr = le64toh(xsdt->TableOffsetEntry[i]);
77970253b53SWarner Losh 		if (addr == 0)
78070253b53SWarner Losh 			continue;
78170253b53SWarner Losh 		sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
78270253b53SWarner Losh 		if (acpi_checksum(sdp, sdp->Length)) {
78370253b53SWarner Losh 			printf("RSDT entry %d (sig %.4s) is corrupt", i,
78470253b53SWarner Losh 			    sdp->Signature);
78570253b53SWarner Losh 			continue;
78670253b53SWarner Losh 		}
78770253b53SWarner Losh 		if (memcmp(sig, sdp->Signature, 4) == 0)
78870253b53SWarner Losh 			return (sdp);
78970253b53SWarner Losh 	}
79070253b53SWarner Losh 	return (NULL);
79170253b53SWarner Losh }
79270253b53SWarner Losh 
7933fecfbebSWarner Losh /*
79470253b53SWarner Losh  * Convert the InterfaceType in the SPCR. These are encoded the same for DBG2
79570253b53SWarner Losh  * tables as well (though we don't parse those here).
79670253b53SWarner Losh  */
79770253b53SWarner Losh static const char *
79870253b53SWarner Losh acpi_uart_type(UINT8 t)
79970253b53SWarner Losh {
80070253b53SWarner Losh 	static const char *types[] = {
8017eb3273aSAndrew Turner 		[0x00] = "ns8250",	/* Full 16550 */
8027eb3273aSAndrew Turner 		[0x01] = "ns8250",	/* DBGP Rev 1 16550 subset */
8037eb3273aSAndrew Turner 		[0x03] = "pl011",	/* Arm PL011 */
8047eb3273aSAndrew Turner 		[0x05] = "ns8250",	/* Nvidia 16550 */
805*038457ddSAndrew Turner 		[0x0d] = "pl011",	/* Arm SBSA 32-bit width */
806*038457ddSAndrew Turner 		[0x0e] = "pl011",	/* Arm SBSA generic */
80770253b53SWarner Losh 		[0x12] = "ns8250",	/* 16550 defined in SerialPort */
80870253b53SWarner Losh 	};
80970253b53SWarner Losh 
81070253b53SWarner Losh 	if (t >= nitems(types))
81170253b53SWarner Losh 		return (NULL);
81270253b53SWarner Losh 	return (types[t]);
81370253b53SWarner Losh }
81470253b53SWarner Losh 
81570253b53SWarner Losh static int
81670253b53SWarner Losh acpi_uart_baud(UINT8 b)
81770253b53SWarner Losh {
81870253b53SWarner Losh 	static int baud[] = { 0, -1, -1, 9600, 19200, -1, 57600, 115200 };
81970253b53SWarner Losh 
82070253b53SWarner Losh 	if (b > 7)
82170253b53SWarner Losh 		return (-1);
82270253b53SWarner Losh 	return (baud[b]);
82370253b53SWarner Losh }
82470253b53SWarner Losh 
82570253b53SWarner Losh static int
82670253b53SWarner Losh acpi_uart_regionwidth(UINT8 rw)
82770253b53SWarner Losh {
82870253b53SWarner Losh 	if (rw == 0)
82970253b53SWarner Losh 		return (1);
83070253b53SWarner Losh 	if (rw > 4)
83170253b53SWarner Losh 		return (-1);
83270253b53SWarner Losh 	return (1 << (rw - 1));
83370253b53SWarner Losh }
83470253b53SWarner Losh 
83570253b53SWarner Losh static const char *
83670253b53SWarner Losh acpi_uart_parity(UINT8 p)
83770253b53SWarner Losh {
83870253b53SWarner Losh 	/* Some of these SPCR entires get this wrong, hard wire none */
83970253b53SWarner Losh 	return ("none");
84070253b53SWarner Losh }
84170253b53SWarner Losh 
84270253b53SWarner Losh /*
84370253b53SWarner Losh  * See if we can find a SPCR ACPI table in the static tables. If so, then it
84470253b53SWarner Losh  * describes the serial console that's been redirected to, so we know that at
84570253b53SWarner Losh  * least there's a serial console. this is most important for embedded systems
84670253b53SWarner Losh  * that don't have traidtional PC serial ports.
84770253b53SWarner Losh  *
84870253b53SWarner Losh  * All the two letter variables in this function correspond to their usage in
84970253b53SWarner Losh  * the uart(4) console string. We use io == -1 to select between I/O ports and
85070253b53SWarner Losh  * memory mapped addresses. Set both hw.uart.console and hw.uart.consol.extra
85170253b53SWarner Losh  * to communicate settings from SPCR to the kernel.
85270253b53SWarner Losh  */
85370253b53SWarner Losh static int
85470253b53SWarner Losh check_acpi_spcr(void)
85570253b53SWarner Losh {
85670253b53SWarner Losh 	ACPI_TABLE_SPCR *spcr;
85770253b53SWarner Losh 	int br, db, io, rs, rw, sb, xo, pv, pd;
85870253b53SWarner Losh 	uintmax_t mm;
85970253b53SWarner Losh 	const char *dt, *pa;
86070253b53SWarner Losh 	char *val = NULL;
86170253b53SWarner Losh 
86270253b53SWarner Losh 	spcr = acpi_find_table(ACPI_SIG_SPCR);
86370253b53SWarner Losh 	if (spcr == NULL)
86470253b53SWarner Losh 		return (0);
86570253b53SWarner Losh 	dt = acpi_uart_type(spcr->InterfaceType);
86670253b53SWarner Losh 	if (dt == NULL)	{ 	/* Kernel can't use unknown types */
86770253b53SWarner Losh 		printf("UART Type %d not known\n", spcr->InterfaceType);
86870253b53SWarner Losh 		return (0);
86970253b53SWarner Losh 	}
87070253b53SWarner Losh 
87170253b53SWarner Losh 	/* I/O vs Memory mapped vs PCI device */
87270253b53SWarner Losh 	io = -1;
87370253b53SWarner Losh 	pv = spcr->PciVendorId;
87470253b53SWarner Losh 	pd = spcr->PciDeviceId;
87570253b53SWarner Losh 	if (pv == 0xffff && pd == 0xffff) {
87670253b53SWarner Losh 		if (spcr->SerialPort.SpaceId == 1)
87770253b53SWarner Losh 			io = spcr->SerialPort.Address;
87870253b53SWarner Losh 		else {
87970253b53SWarner Losh 			mm = spcr->SerialPort.Address;
88070253b53SWarner Losh 			rs = ffs(spcr->SerialPort.BitWidth) - 4;
88170253b53SWarner Losh 			rw = acpi_uart_regionwidth(spcr->SerialPort.AccessWidth);
88270253b53SWarner Losh 		}
88370253b53SWarner Losh 	} else {
88470253b53SWarner Losh 		/* XXX todo: bus:device:function + flags and segment */
88570253b53SWarner Losh 	}
88670253b53SWarner Losh 
88770253b53SWarner Losh 	/* Uart settings */
88870253b53SWarner Losh 	pa = acpi_uart_parity(spcr->Parity);
88970253b53SWarner Losh 	sb = spcr->StopBits;
89070253b53SWarner Losh 	db = 8;
89170253b53SWarner Losh 
89270253b53SWarner Losh 	/*
893501983e6SWarner Losh 	 * UartClkFreq is 3 and newer. We always use it then (it's only valid if
894501983e6SWarner Losh 	 * it isn't 0, but if it is 0, we want to use 0 to have the kernel
895501983e6SWarner Losh 	 * guess).
89670253b53SWarner Losh 	 */
897501983e6SWarner Losh 	if (spcr->Header.Revision <= 2)
89870253b53SWarner Losh 		xo = 0;
899501983e6SWarner Losh 	else
900501983e6SWarner Losh 		xo = spcr->UartClkFreq;
901501983e6SWarner Losh 
902501983e6SWarner Losh 	/*
903501983e6SWarner Losh 	 * PreciseBaudrate, when non-zero, is to be preferred. It's only valid,
904501983e6SWarner Losh 	 * though, for rev 4 and newer. So when it's 0 or the version is too
905501983e6SWarner Losh 	 * old, we do the old-style table lookup. Otherwise we believe it.
906501983e6SWarner Losh 	 */
907501983e6SWarner Losh 	if (spcr->Header.Revision <= 3 || spcr->PreciseBaudrate == 0)
90870253b53SWarner Losh 		br = acpi_uart_baud(spcr->BaudRate);
909501983e6SWarner Losh 	else
910501983e6SWarner Losh 		br = spcr->PreciseBaudrate;
91170253b53SWarner Losh 
91270253b53SWarner Losh 	if (io != -1) {
91370253b53SWarner Losh 		asprintf(&val, "db:%d,dt:%s,io:%#x,pa:%s,br:%d,xo=%d",
91470253b53SWarner Losh 		    db, dt, io, pa, br, xo);
91570253b53SWarner Losh 	} else if (pv != 0xffff && pd != 0xffff) {
91670253b53SWarner Losh 		asprintf(&val, "db:%d,dt:%s,pv:%#x,pd:%#x,pa:%s,br:%d,xo=%d",
91770253b53SWarner Losh 		    db, dt, pv, pd, pa, br, xo);
91870253b53SWarner Losh 	} else {
91970253b53SWarner Losh 		asprintf(&val, "db:%d,dt:%s,mm:%#jx,rs:%d,rw:%d,pa:%s,br:%d,xo=%d",
92070253b53SWarner Losh 		    db, dt, mm, rs, rw, pa, br, xo);
92170253b53SWarner Losh 	}
92270253b53SWarner Losh 	env_setenv("hw.uart.console", EV_VOLATILE, val, NULL, NULL);
92370253b53SWarner Losh 	free(val);
92470253b53SWarner Losh 
92570253b53SWarner Losh 	return (RB_SERIAL);
92670253b53SWarner Losh }
92770253b53SWarner Losh 
92870253b53SWarner Losh 
92970253b53SWarner Losh /*
93070253b53SWarner Losh  * Parse ConOut (the list of consoles active) and see if we can find a serial
93170253b53SWarner Losh  * port and/or a video port. It would be nice to also walk the ACPI DSDT to map
93270253b53SWarner Losh  * the UID for the serial port to a port since there's no standard mapping. Also
93370253b53SWarner Losh  * check for ConIn as well. This will be enough to determine if we have serial,
93470253b53SWarner Losh  * and if we don't, we default to video. If there's a dual-console situation
93570253b53SWarner Losh  * with only ConIn defined, this will currently fail.
9363fecfbebSWarner Losh  */
937b9f745fdSToomas Soome int
9383fecfbebSWarner Losh parse_uefi_con_out(void)
9393fecfbebSWarner Losh {
9403fecfbebSWarner Losh 	int how, rv;
9413fecfbebSWarner Losh 	int vid_seen = 0, com_seen = 0, seen = 0;
9423fecfbebSWarner Losh 	size_t sz;
9433fecfbebSWarner Losh 	char buf[4096], *ep;
9443fecfbebSWarner Losh 	EFI_DEVICE_PATH *node;
9453fecfbebSWarner Losh 	ACPI_HID_DEVICE_PATH  *acpi;
9463fecfbebSWarner Losh 	UART_DEVICE_PATH  *uart;
9473fecfbebSWarner Losh 	bool pci_pending;
9483fecfbebSWarner Losh 
94970253b53SWarner Losh 	/*
95070253b53SWarner Losh 	 * A SPCR in the ACPI fixed tables documents a serial port used for the
95170253b53SWarner Losh 	 * console. It may mirror a video console, or may be stand alone. If it
95270253b53SWarner Losh 	 * is present, we return RB_SERIAL and will use it for the kernel.
95370253b53SWarner Losh 	 */
95470253b53SWarner Losh 	how = check_acpi_spcr();
9553fecfbebSWarner Losh 	sz = sizeof(buf);
9563fecfbebSWarner Losh 	rv = efi_global_getenv("ConOut", buf, &sz);
9572bd4ff2dSToomas Soome 	if (rv != EFI_SUCCESS)
9582bd4ff2dSToomas Soome 		rv = efi_global_getenv("ConOutDev", buf, &sz);
959064fa628SWarner Losh 	if (rv != EFI_SUCCESS)
960064fa628SWarner Losh 		rv = efi_global_getenv("ConIn", buf, &sz);
96114fb9485SEmmanuel Vadot 	if (rv != EFI_SUCCESS) {
962df065f69SWarner Losh 		/*
96370253b53SWarner Losh 		 * If we don't have any Con* variable use both. If we have GOP
96470253b53SWarner Losh 		 * make video primary, otherwise set serial primary. In either
96570253b53SWarner Losh 		 * case, try to use both the 'efi' console which will use the
96670253b53SWarner Losh 		 * GOP, if present and serial. If there's an EFI BIOS that omits
96770253b53SWarner Losh 		 * this, but has a serial port redirect, we'll unavioidably get
96870253b53SWarner Losh 		 * doubled characters, but we'll be right in all the other more
96970253b53SWarner Losh 		 * common cases.
970df065f69SWarner Losh 		 */
971df065f69SWarner Losh 		if (efi_has_gop())
97294164106SWarner Losh 			how |= RB_MULTIPLE;
973df065f69SWarner Losh 		else
97494164106SWarner Losh 			how |= RB_MULTIPLE | RB_SERIAL;
975df065f69SWarner Losh 		setenv("console", "efi,comconsole", 1);
9763fecfbebSWarner Losh 		goto out;
97714fb9485SEmmanuel Vadot 	}
9783fecfbebSWarner Losh 	ep = buf + sz;
9793fecfbebSWarner Losh 	node = (EFI_DEVICE_PATH *)buf;
980d59db438SWarner Losh 	while ((char *)node < ep) {
981d53ed735SWarner Losh 		if (IsDevicePathEndType(node)) {
982d53ed735SWarner Losh 			if (pci_pending && vid_seen == 0)
983d53ed735SWarner Losh 				vid_seen = ++seen;
984d53ed735SWarner Losh 		}
9853fecfbebSWarner Losh 		pci_pending = false;
9863fecfbebSWarner Losh 		if (DevicePathType(node) == ACPI_DEVICE_PATH &&
987ebe8cd79SToomas Soome 		    (DevicePathSubType(node) == ACPI_DP ||
988ebe8cd79SToomas Soome 		    DevicePathSubType(node) == ACPI_EXTENDED_DP)) {
9893fecfbebSWarner Losh 			/* Check for Serial node */
9903fecfbebSWarner Losh 			acpi = (void *)node;
991e78f6dd7SWarner Losh 			if (EISA_ID_TO_NUM(acpi->HID) == 0x501) {
992e78f6dd7SWarner Losh 				setenv_int("efi_8250_uid", acpi->UID);
9933fecfbebSWarner Losh 				com_seen = ++seen;
994e78f6dd7SWarner Losh 			}
9953fecfbebSWarner Losh 		} else if (DevicePathType(node) == MESSAGING_DEVICE_PATH &&
9963fecfbebSWarner Losh 		    DevicePathSubType(node) == MSG_UART_DP) {
997ebe8cd79SToomas Soome 			com_seen = ++seen;
9983fecfbebSWarner Losh 			uart = (void *)node;
999e78f6dd7SWarner Losh 			setenv_int("efi_com_speed", uart->BaudRate);
10003fecfbebSWarner Losh 		} else if (DevicePathType(node) == ACPI_DEVICE_PATH &&
10013fecfbebSWarner Losh 		    DevicePathSubType(node) == ACPI_ADR_DP) {
10023fecfbebSWarner Losh 			/* Check for AcpiAdr() Node for video */
10033fecfbebSWarner Losh 			vid_seen = ++seen;
10043fecfbebSWarner Losh 		} else if (DevicePathType(node) == HARDWARE_DEVICE_PATH &&
10053fecfbebSWarner Losh 		    DevicePathSubType(node) == HW_PCI_DP) {
10063fecfbebSWarner Losh 			/*
10073fecfbebSWarner Losh 			 * Note, vmware fusion has a funky console device
10083fecfbebSWarner Losh 			 *	PciRoot(0x0)/Pci(0xf,0x0)
10093fecfbebSWarner Losh 			 * which we can only detect at the end since we also
10103fecfbebSWarner Losh 			 * have to cope with:
10113fecfbebSWarner Losh 			 *	PciRoot(0x0)/Pci(0x1f,0x0)/Serial(0x1)
10123fecfbebSWarner Losh 			 * so only match it if it's last.
10133fecfbebSWarner Losh 			 */
10143fecfbebSWarner Losh 			pci_pending = true;
10153fecfbebSWarner Losh 		}
101679cb1bf2SWarner Losh 		node = NextDevicePathNode(node);
10173fecfbebSWarner Losh 	}
10183fecfbebSWarner Losh 
10193fecfbebSWarner Losh 	/*
10203fecfbebSWarner Losh 	 * Truth table for RB_MULTIPLE | RB_SERIAL
10213fecfbebSWarner Losh 	 * Value		Result
10223fecfbebSWarner Losh 	 * 0			Use only video console
10233fecfbebSWarner Losh 	 * RB_SERIAL		Use only serial console
10243fecfbebSWarner Losh 	 * RB_MULTIPLE		Use both video and serial console
10253fecfbebSWarner Losh 	 *			(but video is primary so gets rc messages)
10263fecfbebSWarner Losh 	 * both			Use both video and serial console
10273fecfbebSWarner Losh 	 *			(but serial is primary so gets rc messages)
10283fecfbebSWarner Losh 	 *
10293fecfbebSWarner Losh 	 * Try to honor this as best we can. If only one of serial / video
10303fecfbebSWarner Losh 	 * found, then use that. Otherwise, use the first one we found.
10313fecfbebSWarner Losh 	 * This also implies if we found nothing, default to video.
10323fecfbebSWarner Losh 	 */
10333fecfbebSWarner Losh 	how = 0;
10343fecfbebSWarner Losh 	if (vid_seen && com_seen) {
10353fecfbebSWarner Losh 		how |= RB_MULTIPLE;
10363fecfbebSWarner Losh 		if (com_seen < vid_seen)
10373fecfbebSWarner Losh 			how |= RB_SERIAL;
10383fecfbebSWarner Losh 	} else if (com_seen)
10393fecfbebSWarner Losh 		how |= RB_SERIAL;
10403fecfbebSWarner Losh out:
10413fecfbebSWarner Losh 	return (how);
10423fecfbebSWarner Losh }
1043e8282eabSWarner Losh 
10448ac2d6f5SWarner Losh void
10458ac2d6f5SWarner Losh parse_loader_efi_config(EFI_HANDLE h, const char *env_fn)
10468ac2d6f5SWarner Losh {
10478ac2d6f5SWarner Losh 	pdinfo_t *dp;
10488ac2d6f5SWarner Losh 	struct stat st;
10498ac2d6f5SWarner Losh 	int fd = -1;
10508ac2d6f5SWarner Losh 	char *env = NULL;
10518ac2d6f5SWarner Losh 
10528ac2d6f5SWarner Losh 	dp = efiblk_get_pdinfo_by_handle(h);
10538ac2d6f5SWarner Losh 	if (dp == NULL)
10548ac2d6f5SWarner Losh 		return;
10558ac2d6f5SWarner Losh 	set_currdev_pdinfo(dp);
10568ac2d6f5SWarner Losh 	if (stat(env_fn, &st) != 0)
10578ac2d6f5SWarner Losh 		return;
10588ac2d6f5SWarner Losh 	fd = open(env_fn, O_RDONLY);
10598ac2d6f5SWarner Losh 	if (fd == -1)
10608ac2d6f5SWarner Losh 		return;
10618ac2d6f5SWarner Losh 	env = malloc(st.st_size + 1);
10628ac2d6f5SWarner Losh 	if (env == NULL)
10638ac2d6f5SWarner Losh 		goto out;
10648ac2d6f5SWarner Losh 	if (read(fd, env, st.st_size) != st.st_size)
10658ac2d6f5SWarner Losh 		goto out;
10668ac2d6f5SWarner Losh 	env[st.st_size] = '\0';
10678ac2d6f5SWarner Losh 	boot_parse_cmdline(env);
10688ac2d6f5SWarner Losh out:
10698ac2d6f5SWarner Losh 	free(env);
10708ac2d6f5SWarner Losh 	close(fd);
10718ac2d6f5SWarner Losh }
10728ac2d6f5SWarner Losh 
10738ac2d6f5SWarner Losh static void
10748ac2d6f5SWarner Losh read_loader_env(const char *name, char *def_fn, bool once)
10758ac2d6f5SWarner Losh {
10768ac2d6f5SWarner Losh 	UINTN len;
10778ac2d6f5SWarner Losh 	char *fn, *freeme = NULL;
10788ac2d6f5SWarner Losh 
10798ac2d6f5SWarner Losh 	len = 0;
10808ac2d6f5SWarner Losh 	fn = def_fn;
10818ac2d6f5SWarner Losh 	if (efi_freebsd_getenv(name, NULL, &len) == EFI_BUFFER_TOO_SMALL) {
10828ac2d6f5SWarner Losh 		freeme = fn = malloc(len + 1);
10838ac2d6f5SWarner Losh 		if (fn != NULL) {
10848ac2d6f5SWarner Losh 			if (efi_freebsd_getenv(name, fn, &len) != EFI_SUCCESS) {
10858ac2d6f5SWarner Losh 				free(fn);
10868ac2d6f5SWarner Losh 				fn = NULL;
10878ac2d6f5SWarner Losh 				printf(
10888ac2d6f5SWarner Losh 			    "Can't fetch FreeBSD::%s we know is there\n", name);
10898ac2d6f5SWarner Losh 			} else {
10908ac2d6f5SWarner Losh 				/*
10918ac2d6f5SWarner Losh 				 * if tagged as 'once' delete the env variable so we
10928ac2d6f5SWarner Losh 				 * only use it once.
10938ac2d6f5SWarner Losh 				 */
10948ac2d6f5SWarner Losh 				if (once)
10958ac2d6f5SWarner Losh 					efi_freebsd_delenv(name);
10968ac2d6f5SWarner Losh 				/*
10978ac2d6f5SWarner Losh 				 * We malloced 1 more than len above, then redid the call.
10988ac2d6f5SWarner Losh 				 * so now we have room at the end of the string to NUL terminate
10998ac2d6f5SWarner Losh 				 * it here, even if the typical idium would have '- 1' here to
11008ac2d6f5SWarner Losh 				 * not overflow. len should be the same on return both times.
11018ac2d6f5SWarner Losh 				 */
11028ac2d6f5SWarner Losh 				fn[len] = '\0';
11038ac2d6f5SWarner Losh 			}
11048ac2d6f5SWarner Losh 		} else {
11058ac2d6f5SWarner Losh 			printf(
11068ac2d6f5SWarner Losh 		    "Can't allocate %d bytes to fetch FreeBSD::%s env var\n",
11078ac2d6f5SWarner Losh 			    len, name);
11088ac2d6f5SWarner Losh 		}
11098ac2d6f5SWarner Losh 	}
11108ac2d6f5SWarner Losh 	if (fn) {
11118ac2d6f5SWarner Losh 		printf("    Reading loader env vars from %s\n", fn);
11128ac2d6f5SWarner Losh 		parse_loader_efi_config(boot_img->DeviceHandle, fn);
11138ac2d6f5SWarner Losh 	}
11148ac2d6f5SWarner Losh }
11158ac2d6f5SWarner Losh 
1116ed2a6576SWarner Losh caddr_t
1117ed2a6576SWarner Losh ptov(uintptr_t x)
1118ed2a6576SWarner Losh {
1119ed2a6576SWarner Losh 	return ((caddr_t)x);
1120ed2a6576SWarner Losh }
11218ac2d6f5SWarner Losh 
11220b01d457SR. Christian McDonald static void
11230b01d457SR. Christian McDonald acpi_detect(void)
11240b01d457SR. Christian McDonald {
11250b01d457SR. Christian McDonald 	char buf[24];
11260b01d457SR. Christian McDonald 	int revision;
11270b01d457SR. Christian McDonald 
1128e183039fSKyle Evans 	feature_enable(FEATURE_EARLY_ACPI);
11290b01d457SR. Christian McDonald 	if ((rsdp = efi_get_table(&acpi20)) == NULL)
11300b01d457SR. Christian McDonald 		if ((rsdp = efi_get_table(&acpi)) == NULL)
11310b01d457SR. Christian McDonald 			return;
11320b01d457SR. Christian McDonald 
11336818ff77SAhmad Khalifa 	sprintf(buf, "0x%016"PRIxPTR, (uintptr_t)rsdp);
11340b01d457SR. Christian McDonald 	setenv("acpi.rsdp", buf, 1);
11350b01d457SR. Christian McDonald 	revision = rsdp->Revision;
11360b01d457SR. Christian McDonald 	if (revision == 0)
11370b01d457SR. Christian McDonald 		revision = 1;
11380b01d457SR. Christian McDonald 	sprintf(buf, "%d", revision);
11390b01d457SR. Christian McDonald 	setenv("acpi.revision", buf, 1);
11400b01d457SR. Christian McDonald 	strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
11410b01d457SR. Christian McDonald 	buf[sizeof(rsdp->OemId)] = '\0';
11420b01d457SR. Christian McDonald 	setenv("acpi.oem", buf, 1);
11430b01d457SR. Christian McDonald 	sprintf(buf, "0x%016x", rsdp->RsdtPhysicalAddress);
11440b01d457SR. Christian McDonald 	setenv("acpi.rsdt", buf, 1);
11450b01d457SR. Christian McDonald 	if (revision >= 2) {
11460b01d457SR. Christian McDonald 		/* XXX extended checksum? */
11470b01d457SR. Christian McDonald 		sprintf(buf, "0x%016llx",
11480b01d457SR. Christian McDonald 		    (unsigned long long)rsdp->XsdtPhysicalAddress);
11490b01d457SR. Christian McDonald 		setenv("acpi.xsdt", buf, 1);
11500b01d457SR. Christian McDonald 		sprintf(buf, "%d", rsdp->Length);
11510b01d457SR. Christian McDonald 		setenv("acpi.xsdt_length", buf, 1);
11520b01d457SR. Christian McDonald 	}
11530b01d457SR. Christian McDonald }
11540b01d457SR. Christian McDonald 
1155e8282eabSWarner Losh EFI_STATUS
1156e8282eabSWarner Losh main(int argc, CHAR16 *argv[])
1157e8282eabSWarner Losh {
1158e8282eabSWarner Losh 	EFI_GUID *guid;
11593fecfbebSWarner Losh 	int howto, i, uhowto;
1160e8282eabSWarner Losh 	UINTN k;
1161b43c6042SWarner Losh 	bool has_kbd, is_last;
1162e8282eabSWarner Losh 	char *s;
1163e8282eabSWarner Losh 	EFI_DEVICE_PATH *imgpath;
1164e8282eabSWarner Losh 	CHAR16 *text;
1165b43c6042SWarner Losh 	EFI_STATUS rv;
1166b43c6042SWarner Losh 	size_t sz, bosz = 0, bisz = 0;
1167e8282eabSWarner Losh 	UINT16 boot_order[100];
1168b43c6042SWarner Losh 	char boot_info[4096];
1169b43c6042SWarner Losh 	char buf[32];
1170b43c6042SWarner Losh 	bool uefi_boot_mgr;
1171e8282eabSWarner Losh 
1172e8282eabSWarner Losh 	archsw.arch_autoload = efi_autoload;
1173e8282eabSWarner Losh 	archsw.arch_getdev = efi_getdev;
1174e8282eabSWarner Losh 	archsw.arch_copyin = efi_copyin;
1175e8282eabSWarner Losh 	archsw.arch_copyout = efi_copyout;
1176f8ca5d45SAhmad Khalifa #if defined(__amd64__) || defined(__i386__)
117778cd72c9SSimon J. Gerraty 	archsw.arch_hypervisor = x86_hypervisor;
117821686d9eSSimon J. Gerraty #endif
1179e8282eabSWarner Losh 	archsw.arch_readin = efi_readin;
1180e8282eabSWarner Losh 	archsw.arch_zfs_probe = efi_zfs_probe;
1181e8282eabSWarner Losh 
11822efbc8e2SWarner Losh #if !defined(__arm__)
11832efbc8e2SWarner Losh 	for (k = 0; k < ST->NumberOfTableEntries; k++) {
11842efbc8e2SWarner Losh 		guid = &ST->ConfigurationTable[k].VendorGuid;
11852efbc8e2SWarner Losh 		if (!memcmp(guid, &smbios, sizeof(EFI_GUID)) ||
11862efbc8e2SWarner Losh 		    !memcmp(guid, &smbios3, sizeof(EFI_GUID))) {
11872efbc8e2SWarner Losh 			char buf[40];
11882efbc8e2SWarner Losh 
11892efbc8e2SWarner Losh 			snprintf(buf, sizeof(buf), "%p",
11902efbc8e2SWarner Losh 			    ST->ConfigurationTable[k].VendorTable);
11912efbc8e2SWarner Losh 			setenv("hint.smbios.0.mem", buf, 1);
11922efbc8e2SWarner Losh 			smbios_detect(ST->ConfigurationTable[k].VendorTable);
11932efbc8e2SWarner Losh 			break;
11942efbc8e2SWarner Losh 		}
11952efbc8e2SWarner Losh 	}
11962efbc8e2SWarner Losh #endif
11972efbc8e2SWarner Losh 
1198e8282eabSWarner Losh         /* Get our loaded image protocol interface structure. */
1199110d56cbSToomas Soome 	(void) OpenProtocolByHandle(IH, &imgid, (void **)&boot_img);
1200e8282eabSWarner Losh 
12010b01d457SR. Christian McDonald 	/* Report the RSDP early. */
12020b01d457SR. Christian McDonald 	acpi_detect();
12030b01d457SR. Christian McDonald 
120436d37160SWarner Losh 	/*
120536d37160SWarner Losh 	 * Chicken-and-egg problem; we want to have console output early, but
120636d37160SWarner Losh 	 * some console attributes may depend on reading from eg. the boot
120736d37160SWarner Losh 	 * device, which we can't do yet.  We can use printf() etc. once this is
120836d37160SWarner Losh 	 * done. So, we set it to the efi console, then call console init. This
120936d37160SWarner Losh 	 * gets us printf early, but also primes the pump for all future console
121036d37160SWarner Losh 	 * changes to take effect, regardless of where they come from.
121136d37160SWarner Losh 	 */
121236d37160SWarner Losh 	setenv("console", "efi", 1);
1213ebe8cd79SToomas Soome 	uhowto = parse_uefi_con_out();
1214123b5b87SEmmanuel Vadot #if defined(__riscv)
1215df065f69SWarner Losh 	/*
1216df065f69SWarner Losh 	 * This workaround likely is papering over a real issue
1217df065f69SWarner Losh 	 */
1218ebe8cd79SToomas Soome 	if ((uhowto & RB_SERIAL) != 0)
1219ebe8cd79SToomas Soome 		setenv("console", "comconsole", 1);
1220ebe8cd79SToomas Soome #endif
122136d37160SWarner Losh 	cons_probe();
122236d37160SWarner Losh 
1223b4cb3fe0SToomas Soome 	/* Set up currdev variable to have hooks in place. */
12241c1783d6SWarner Losh 	env_setenv("currdev", EV_VOLATILE, "", gen_setcurrdev, env_nounset);
1225b4cb3fe0SToomas Soome 
1226e8282eabSWarner Losh 	/* Init the time source */
1227e8282eabSWarner Losh 	efi_time_init();
1228e8282eabSWarner Losh 
1229e8282eabSWarner Losh 	/*
1230e8282eabSWarner Losh 	 * Initialise the block cache. Set the upper limit.
1231e8282eabSWarner Losh 	 */
1232e8282eabSWarner Losh 	bcache_init(32768, 512);
1233e8282eabSWarner Losh 
12348c914c57SWarner Losh 	/*
12358c914c57SWarner Losh 	 * Scan the BLOCK IO MEDIA handles then
12368c914c57SWarner Losh 	 * march through the device switch probing for things.
12378c914c57SWarner Losh 	 */
12388c914c57SWarner Losh 	i = efipart_inithandles();
12398c914c57SWarner Losh 	if (i != 0 && i != ENOENT) {
12408c914c57SWarner Losh 		printf("efipart_inithandles failed with ERRNO %d, expect "
12418c914c57SWarner Losh 		    "failures\n", i);
12428c914c57SWarner Losh 	}
12438c914c57SWarner Losh 
124466012c8fSWarner Losh 	devinit();
12458c914c57SWarner Losh 
12468c914c57SWarner Losh 	/*
12478c914c57SWarner Losh 	 * Detect console settings two different ways: one via the command
12488c914c57SWarner Losh 	 * args (eg -h) or via the UEFI ConOut variable.
12498c914c57SWarner Losh 	 */
12508c914c57SWarner Losh 	has_kbd = has_keyboard();
12513fecfbebSWarner Losh 	howto = parse_args(argc, argv);
12523fecfbebSWarner Losh 	if (!has_kbd && (howto & RB_PROBE))
12533fecfbebSWarner Losh 		howto |= RB_SERIAL | RB_MULTIPLE;
12543fecfbebSWarner Losh 	howto &= ~RB_PROBE;
1255fa9dc8d3SWarner Losh 
1256fa9dc8d3SWarner Losh 	/*
12578ac2d6f5SWarner Losh 	 * Read additional environment variables from the boot device's
12588ac2d6f5SWarner Losh 	 * "LoaderEnv" file. Any boot loader environment variable may be set
12598ac2d6f5SWarner Losh 	 * there, which are subtly different than loader.conf variables. Only
12608ac2d6f5SWarner Losh 	 * the 'simple' ones may be set so things like foo_load="YES" won't work
12618ac2d6f5SWarner Losh 	 * for two reasons.  First, the parser is simplistic and doesn't grok
12628ac2d6f5SWarner Losh 	 * quotes.  Second, because the variables that cause an action to happen
12638ac2d6f5SWarner Losh 	 * are parsed by the lua, 4th or whatever code that's not yet
12648ac2d6f5SWarner Losh 	 * loaded. This is relative to the root directory when loader.efi is
12658ac2d6f5SWarner Losh 	 * loaded off the UFS root drive (when chain booted), or from the ESP
12668ac2d6f5SWarner Losh 	 * when directly loaded by the BIOS.
12678ac2d6f5SWarner Losh 	 *
12688ac2d6f5SWarner Losh 	 * We also read in NextLoaderEnv if it was specified. This allows next boot
12698ac2d6f5SWarner Losh 	 * functionality to be implemented and to override anything in LoaderEnv.
12708ac2d6f5SWarner Losh 	 */
12718ac2d6f5SWarner Losh 	read_loader_env("LoaderEnv", "/efi/freebsd/loader.env", false);
12728ac2d6f5SWarner Losh 	read_loader_env("NextLoaderEnv", NULL, true);
12738ac2d6f5SWarner Losh 
12748ac2d6f5SWarner Losh 	/*
12753fecfbebSWarner Losh 	 * We now have two notions of console. howto should be viewed as
12762168b189SWarner Losh 	 * overrides. If console is already set, don't set it again.
1277fa9dc8d3SWarner Losh 	 */
12783fecfbebSWarner Losh #define	VIDEO_ONLY	0
12793fecfbebSWarner Losh #define	SERIAL_ONLY	RB_SERIAL
12803fecfbebSWarner Losh #define	VID_SER_BOTH	RB_MULTIPLE
12813fecfbebSWarner Losh #define	SER_VID_BOTH	(RB_SERIAL | RB_MULTIPLE)
12823fecfbebSWarner Losh #define	CON_MASK	(RB_SERIAL | RB_MULTIPLE)
1283afe7cf87SWarner Losh 	if (strcmp(getenv("console"), "efi") == 0) {
12843fecfbebSWarner Losh 		if ((howto & CON_MASK) == 0) {
12853fecfbebSWarner Losh 			/* No override, uhowto is controlling and efi cons is perfect */
12863fecfbebSWarner Losh 			howto = howto | (uhowto & CON_MASK);
12873fecfbebSWarner Losh 		} else if ((howto & CON_MASK) == (uhowto & CON_MASK)) {
12883fecfbebSWarner Losh 			/* override matches what UEFI told us, efi console is perfect */
12893fecfbebSWarner Losh 		} else if ((uhowto & (CON_MASK)) != 0) {
12903fecfbebSWarner Losh 			/*
12913fecfbebSWarner Losh 			 * We detected a serial console on ConOut. All possible
12923fecfbebSWarner Losh 			 * overrides include serial. We can't really override what efi
12933fecfbebSWarner Losh 			 * gives us, so we use it knowing it's the best choice.
12943fecfbebSWarner Losh 			 */
129536d37160SWarner Losh 			/* Do nothing */
12963fecfbebSWarner Losh 		} else {
12973fecfbebSWarner Losh 			/*
12983fecfbebSWarner Losh 			 * We detected some kind of serial in the override, but ConOut
12993fecfbebSWarner Losh 			 * has no serial, so we have to sort out which case it really is.
13003fecfbebSWarner Losh 			 */
13013fecfbebSWarner Losh 			switch (howto & CON_MASK) {
13023fecfbebSWarner Losh 			case SERIAL_ONLY:
13033fecfbebSWarner Losh 				setenv("console", "comconsole", 1);
13043fecfbebSWarner Losh 				break;
13053fecfbebSWarner Losh 			case VID_SER_BOTH:
13063fecfbebSWarner Losh 				setenv("console", "efi comconsole", 1);
13073fecfbebSWarner Losh 				break;
13083fecfbebSWarner Losh 			case SER_VID_BOTH:
13093fecfbebSWarner Losh 				setenv("console", "comconsole efi", 1);
13103fecfbebSWarner Losh 				break;
13113fecfbebSWarner Losh 				/* case VIDEO_ONLY can't happen -- it's the first if above */
13123fecfbebSWarner Losh 			}
13133fecfbebSWarner Losh 		}
13142168b189SWarner Losh 	}
13158c18b82bSWarner Losh 
13163fecfbebSWarner Losh 	/*
13173fecfbebSWarner Losh 	 * howto is set now how we want to export the flags to the kernel, so
13183fecfbebSWarner Losh 	 * set the env based on it.
13193fecfbebSWarner Losh 	 */
13203fecfbebSWarner Losh 	boot_howto_to_env(howto);
1321ca987d46SWarner Losh 
1322780332f1SVexedUXR 	if (efi_copy_init())
1323ca987d46SWarner Losh 		return (EFI_BUFFER_TOO_SMALL);
1324ca987d46SWarner Losh 
1325fa9dc8d3SWarner Losh 	if ((s = getenv("fail_timeout")) != NULL)
1326fa9dc8d3SWarner Losh 		fail_timeout = strtol(s, NULL, 10);
1327fa9dc8d3SWarner Losh 
1328d59db438SWarner Losh 	printf("%s\n", bootprog_info);
1329ca987d46SWarner Losh 	printf("   Command line arguments:");
1330ca987d46SWarner Losh 	for (i = 0; i < argc; i++)
1331ca987d46SWarner Losh 		printf(" %S", argv[i]);
1332ca987d46SWarner Losh 	printf("\n");
1333ca987d46SWarner Losh 
1334fed13eb3SToomas Soome 	printf("   Image base: 0x%lx\n", (unsigned long)boot_img->ImageBase);
1335ca987d46SWarner Losh 	printf("   EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
1336ca987d46SWarner Losh 	    ST->Hdr.Revision & 0xffff);
1337ca987d46SWarner Losh 	printf("   EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
1338ca987d46SWarner Losh 	    ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
1339fb4c4787SWarner Losh 	printf("   Console: %s (%#x)\n", getenv("console"), howto);
1340fb4c4787SWarner Losh 
1341fa9dc8d3SWarner Losh 	/* Determine the devpath of our image so we can prefer it. */
134259a05bdfSWarner Losh 	text = efi_devpath_name(boot_img->FilePath);
1343ac15bcdeSWarner Losh 	if (text != NULL) {
1344ac15bcdeSWarner Losh 		printf("   Load Path: %S\n", text);
1345ac15bcdeSWarner Losh 		efi_setenv_freebsd_wcs("LoaderPath", text);
1346ac15bcdeSWarner Losh 		efi_free_devpath_name(text);
1347ac15bcdeSWarner Losh 	}
1348ac15bcdeSWarner Losh 
1349110d56cbSToomas Soome 	rv = OpenProtocolByHandle(boot_img->DeviceHandle, &devid,
1350110d56cbSToomas Soome 	    (void **)&imgpath);
1351b43c6042SWarner Losh 	if (rv == EFI_SUCCESS) {
1352ac15bcdeSWarner Losh 		text = efi_devpath_name(imgpath);
1353ac15bcdeSWarner Losh 		if (text != NULL) {
1354ac15bcdeSWarner Losh 			printf("   Load Device: %S\n", text);
1355ac15bcdeSWarner Losh 			efi_setenv_freebsd_wcs("LoaderDev", text);
1356ac15bcdeSWarner Losh 			efi_free_devpath_name(text);
1357ac15bcdeSWarner Losh 		}
1358ac15bcdeSWarner Losh 	}
1359ac15bcdeSWarner Losh 
13604ecd512fSWarner Losh 	if (getenv("uefi_ignore_boot_mgr") != NULL) {
13614ecd512fSWarner Losh 		printf("    Ignoring UEFI boot manager\n");
13624ecd512fSWarner Losh 		uefi_boot_mgr = false;
13634ecd512fSWarner Losh 	} else {
1364b43c6042SWarner Losh 		uefi_boot_mgr = true;
1365ac15bcdeSWarner Losh 		boot_current = 0;
1366ac15bcdeSWarner Losh 		sz = sizeof(boot_current);
1367b43c6042SWarner Losh 		rv = efi_global_getenv("BootCurrent", &boot_current, &sz);
1368b43c6042SWarner Losh 		if (rv == EFI_SUCCESS)
1369ac15bcdeSWarner Losh 			printf("   BootCurrent: %04x\n", boot_current);
1370b43c6042SWarner Losh 		else {
1371b43c6042SWarner Losh 			boot_current = 0xffff;
1372b43c6042SWarner Losh 			uefi_boot_mgr = false;
1373b43c6042SWarner Losh 		}
1374ac15bcdeSWarner Losh 
1375ac15bcdeSWarner Losh 		sz = sizeof(boot_order);
1376b43c6042SWarner Losh 		rv = efi_global_getenv("BootOrder", &boot_order, &sz);
1377b43c6042SWarner Losh 		if (rv == EFI_SUCCESS) {
1378ac15bcdeSWarner Losh 			printf("   BootOrder:");
1379ac15bcdeSWarner Losh 			for (i = 0; i < sz / sizeof(boot_order[0]); i++)
1380ac15bcdeSWarner Losh 				printf(" %04x%s", boot_order[i],
1381ac15bcdeSWarner Losh 				    boot_order[i] == boot_current ? "[*]" : "");
1382ac15bcdeSWarner Losh 			printf("\n");
1383b43c6042SWarner Losh 			is_last = boot_order[(sz / sizeof(boot_order[0])) - 1] == boot_current;
1384b43c6042SWarner Losh 			bosz = sz;
1385b43c6042SWarner Losh 		} else if (uefi_boot_mgr) {
1386b43c6042SWarner Losh 			/*
1387b43c6042SWarner Losh 			 * u-boot doesn't set BootOrder, but otherwise participates in the
1388b43c6042SWarner Losh 			 * boot manager protocol. So we fake it here and don't consider it
1389b43c6042SWarner Losh 			 * a failure.
1390b43c6042SWarner Losh 			 */
1391b43c6042SWarner Losh 			bosz = sizeof(boot_order[0]);
1392b43c6042SWarner Losh 			boot_order[0] = boot_current;
1393b43c6042SWarner Losh 			is_last = true;
1394b43c6042SWarner Losh 		}
13954ecd512fSWarner Losh 	}
1396b43c6042SWarner Losh 
1397b43c6042SWarner Losh 	/*
1398b43c6042SWarner Losh 	 * Next, find the boot info structure the UEFI boot manager is
1399b43c6042SWarner Losh 	 * supposed to setup. We need this so we can walk through it to
1400b43c6042SWarner Losh 	 * find where we are in the booting process and what to try to
1401b43c6042SWarner Losh 	 * boot next.
1402b43c6042SWarner Losh 	 */
1403b43c6042SWarner Losh 	if (uefi_boot_mgr) {
1404b43c6042SWarner Losh 		snprintf(buf, sizeof(buf), "Boot%04X", boot_current);
1405b43c6042SWarner Losh 		sz = sizeof(boot_info);
1406b43c6042SWarner Losh 		rv = efi_global_getenv(buf, &boot_info, &sz);
1407b43c6042SWarner Losh 		if (rv == EFI_SUCCESS)
1408b43c6042SWarner Losh 			bisz = sz;
1409b43c6042SWarner Losh 		else
1410b43c6042SWarner Losh 			uefi_boot_mgr = false;
1411b43c6042SWarner Losh 	}
1412ac15bcdeSWarner Losh 
1413ca987d46SWarner Losh 	/*
1414ca987d46SWarner Losh 	 * Disable the watchdog timer. By default the boot manager sets
1415ca987d46SWarner Losh 	 * the timer to 5 minutes before invoking a boot option. If we
1416ca987d46SWarner Losh 	 * want to return to the boot manager, we have to disable the
1417ca987d46SWarner Losh 	 * watchdog timer and since we're an interactive program, we don't
1418ca987d46SWarner Losh 	 * want to wait until the user types "quit". The timer may have
1419ca987d46SWarner Losh 	 * fired by then. We don't care if this fails. It does not prevent
1420ca987d46SWarner Losh 	 * normal functioning in any way...
1421ca987d46SWarner Losh 	 */
1422ca987d46SWarner Losh 	BS->SetWatchdogTimer(0, 0, 0, NULL);
1423ca987d46SWarner Losh 
1424fa9dc8d3SWarner Losh 	/*
142513ea0450SMarcin Wojtas 	 * Initialize the trusted/forbidden certificates from UEFI.
142613ea0450SMarcin Wojtas 	 * They will be later used to verify the manifest(s),
142713ea0450SMarcin Wojtas 	 * which should contain hashes of verified files.
142813ea0450SMarcin Wojtas 	 * This needs to be initialized before any configuration files
142913ea0450SMarcin Wojtas 	 * are loaded.
143013ea0450SMarcin Wojtas 	 */
143113ea0450SMarcin Wojtas #ifdef EFI_SECUREBOOT
143213ea0450SMarcin Wojtas 	ve_efi_init();
143313ea0450SMarcin Wojtas #endif
143413ea0450SMarcin Wojtas 
143513ea0450SMarcin Wojtas 	/*
1436fa9dc8d3SWarner Losh 	 * Try and find a good currdev based on the image that was booted.
1437fa9dc8d3SWarner Losh 	 * It might be desirable here to have a short pause to allow falling
1438fa9dc8d3SWarner Losh 	 * through to the boot loader instead of returning instantly to follow
1439fa9dc8d3SWarner Losh 	 * the boot protocol and also allow an escape hatch for users wishing
1440fa9dc8d3SWarner Losh 	 * to try something different.
1441fa9dc8d3SWarner Losh 	 */
144259a05bdfSWarner Losh 	if (find_currdev(uefi_boot_mgr, is_last, boot_info, bisz) != 0)
14434ecd512fSWarner Losh 		if (uefi_boot_mgr &&
14444ecd512fSWarner Losh 		    !interactive_interrupt("Failed to find bootable partition"))
1445ca987d46SWarner Losh 			return (EFI_NOT_FOUND);
1446ca987d46SWarner Losh 
14473630506bSToomas Soome 	autoload_font(false);	/* Set up the font list for console. */
1448ca987d46SWarner Losh 	efi_init_environment();
1449ca987d46SWarner Losh 
14506bc86037SWarner Losh 	interact();			/* doesn't return */
1451ca987d46SWarner Losh 
1452ca987d46SWarner Losh 	return (EFI_SUCCESS);		/* keep compiler happy */
1453ca987d46SWarner Losh }
1454ca987d46SWarner Losh 
14555c73b3e0SColin Percival COMMAND_SET(efi_seed_entropy, "efi-seed-entropy", "try to get entropy from the EFI RNG", command_seed_entropy);
14565c73b3e0SColin Percival 
14575c73b3e0SColin Percival static int
14585c73b3e0SColin Percival command_seed_entropy(int argc, char *argv[])
14595c73b3e0SColin Percival {
14605c73b3e0SColin Percival 	EFI_STATUS status;
14615c73b3e0SColin Percival 	EFI_RNG_PROTOCOL *rng;
1462c8ebbd28SColin Percival 	unsigned int size_efi = RANDOM_FORTUNA_DEFPOOLSIZE * RANDOM_FORTUNA_NPOOLS;
1463c8ebbd28SColin Percival 	unsigned int size = RANDOM_FORTUNA_DEFPOOLSIZE * RANDOM_FORTUNA_NPOOLS;
1464c8ebbd28SColin Percival 	void *buf_efi;
14655c73b3e0SColin Percival 	void *buf;
14665c73b3e0SColin Percival 
14675c73b3e0SColin Percival 	if (argc > 1) {
1468c8ebbd28SColin Percival 		size_efi = strtol(argv[1], NULL, 0);
1469c8ebbd28SColin Percival 
1470c8ebbd28SColin Percival 		/* Don't *compress* the entropy we get from EFI. */
1471c8ebbd28SColin Percival 		if (size_efi > size)
1472c8ebbd28SColin Percival 			size = size_efi;
1473c8ebbd28SColin Percival 
1474c8ebbd28SColin Percival 		/*
1475c8ebbd28SColin Percival 		 * If the amount of entropy we get from EFI is less than the
1476c8ebbd28SColin Percival 		 * size of a single Fortuna pool -- i.e. not enough to ensure
1477c8ebbd28SColin Percival 		 * that Fortuna is safely seeded -- don't expand it since we
1478c8ebbd28SColin Percival 		 * don't want to trick Fortuna into thinking that it has been
1479c8ebbd28SColin Percival 		 * safely seeded when it has not.
1480c8ebbd28SColin Percival 		 */
1481c8ebbd28SColin Percival 		if (size_efi < RANDOM_FORTUNA_DEFPOOLSIZE)
1482c8ebbd28SColin Percival 			size = size_efi;
14835c73b3e0SColin Percival 	}
14845c73b3e0SColin Percival 
14855c73b3e0SColin Percival 	status = BS->LocateProtocol(&rng_guid, NULL, (VOID **)&rng);
14865c73b3e0SColin Percival 	if (status != EFI_SUCCESS) {
14875c73b3e0SColin Percival 		command_errmsg = "RNG protocol not found";
14885c73b3e0SColin Percival 		return (CMD_ERROR);
14895c73b3e0SColin Percival 	}
14905c73b3e0SColin Percival 
14915c73b3e0SColin Percival 	if ((buf = malloc(size)) == NULL) {
14925c73b3e0SColin Percival 		command_errmsg = "out of memory";
14935c73b3e0SColin Percival 		return (CMD_ERROR);
14945c73b3e0SColin Percival 	}
14955c73b3e0SColin Percival 
1496c8ebbd28SColin Percival 	if ((buf_efi = malloc(size_efi)) == NULL) {
1497c8ebbd28SColin Percival 		free(buf);
1498c8ebbd28SColin Percival 		command_errmsg = "out of memory";
1499c8ebbd28SColin Percival 		return (CMD_ERROR);
1500c8ebbd28SColin Percival 	}
1501c8ebbd28SColin Percival 
1502b58fe4ebSColin Percival 	TSENTER2("rng->GetRNG");
1503c8ebbd28SColin Percival 	status = rng->GetRNG(rng, NULL, size_efi, (UINT8 *)buf_efi);
1504b58fe4ebSColin Percival 	TSEXIT();
15055c73b3e0SColin Percival 	if (status != EFI_SUCCESS) {
1506c8ebbd28SColin Percival 		free(buf_efi);
15075c73b3e0SColin Percival 		free(buf);
15085c73b3e0SColin Percival 		command_errmsg = "GetRNG failed";
15095c73b3e0SColin Percival 		return (CMD_ERROR);
15105c73b3e0SColin Percival 	}
1511c8ebbd28SColin Percival 	if (size_efi < size)
1512c8ebbd28SColin Percival 		pkcs5v2_genkey_raw(buf, size, "", 0, buf_efi, size_efi, 1);
1513c8ebbd28SColin Percival 	else
1514c8ebbd28SColin Percival 		memcpy(buf, buf_efi, size);
15155c73b3e0SColin Percival 
15165c73b3e0SColin Percival 	if (file_addbuf("efi_rng_seed", "boot_entropy_platform", size, buf) != 0) {
1517c8ebbd28SColin Percival 		free(buf_efi);
15185c73b3e0SColin Percival 		free(buf);
15195c73b3e0SColin Percival 		return (CMD_ERROR);
15205c73b3e0SColin Percival 	}
15215c73b3e0SColin Percival 
1522c8ebbd28SColin Percival 	explicit_bzero(buf_efi, size_efi);
1523c8ebbd28SColin Percival 	free(buf_efi);
15245c73b3e0SColin Percival 	free(buf);
15255c73b3e0SColin Percival 	return (CMD_OK);
15265c73b3e0SColin Percival }
15275c73b3e0SColin Percival 
15280f31e1e6SToomas Soome COMMAND_SET(poweroff, "poweroff", "power off the system", command_poweroff);
15290f31e1e6SToomas Soome 
15300f31e1e6SToomas Soome static int
15310f31e1e6SToomas Soome command_poweroff(int argc __unused, char *argv[] __unused)
15320f31e1e6SToomas Soome {
15330f31e1e6SToomas Soome 	int i;
15340f31e1e6SToomas Soome 
15350f31e1e6SToomas Soome 	for (i = 0; devsw[i] != NULL; ++i)
15360f31e1e6SToomas Soome 		if (devsw[i]->dv_cleanup != NULL)
15370f31e1e6SToomas Soome 			(devsw[i]->dv_cleanup)();
15380f31e1e6SToomas Soome 
15390f31e1e6SToomas Soome 	RS->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
15400f31e1e6SToomas Soome 
15410f31e1e6SToomas Soome 	/* NOTREACHED */
15420f31e1e6SToomas Soome 	return (CMD_ERROR);
15430f31e1e6SToomas Soome }
15440f31e1e6SToomas Soome 
1545ca987d46SWarner Losh COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
1546ca987d46SWarner Losh 
1547ca987d46SWarner Losh static int
1548ca987d46SWarner Losh command_reboot(int argc, char *argv[])
1549ca987d46SWarner Losh {
1550ca987d46SWarner Losh 	int i;
1551ca987d46SWarner Losh 
1552ca987d46SWarner Losh 	for (i = 0; devsw[i] != NULL; ++i)
1553ca987d46SWarner Losh 		if (devsw[i]->dv_cleanup != NULL)
1554ca987d46SWarner Losh 			(devsw[i]->dv_cleanup)();
1555ca987d46SWarner Losh 
1556ca987d46SWarner Losh 	RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
1557ca987d46SWarner Losh 
1558ca987d46SWarner Losh 	/* NOTREACHED */
1559ca987d46SWarner Losh 	return (CMD_ERROR);
1560ca987d46SWarner Losh }
1561ca987d46SWarner Losh 
1562ca987d46SWarner Losh COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
1563ca987d46SWarner Losh 
1564ca987d46SWarner Losh static int
156534ada209SToomas Soome command_memmap(int argc __unused, char *argv[] __unused)
1566ca987d46SWarner Losh {
1567ca987d46SWarner Losh 	UINTN sz;
1568ca987d46SWarner Losh 	EFI_MEMORY_DESCRIPTOR *map, *p;
1569ca987d46SWarner Losh 	UINTN key, dsz;
1570ca987d46SWarner Losh 	UINT32 dver;
1571ca987d46SWarner Losh 	EFI_STATUS status;
1572ca987d46SWarner Losh 	int i, ndesc;
1573ca987d46SWarner Losh 	char line[80];
1574ca987d46SWarner Losh 
1575ca987d46SWarner Losh 	sz = 0;
1576ca987d46SWarner Losh 	status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
1577ca987d46SWarner Losh 	if (status != EFI_BUFFER_TOO_SMALL) {
1578ca987d46SWarner Losh 		printf("Can't determine memory map size\n");
1579ca987d46SWarner Losh 		return (CMD_ERROR);
1580ca987d46SWarner Losh 	}
1581ca987d46SWarner Losh 	map = malloc(sz);
1582ca987d46SWarner Losh 	status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
1583ca987d46SWarner Losh 	if (EFI_ERROR(status)) {
1584ca987d46SWarner Losh 		printf("Can't read memory map\n");
1585ca987d46SWarner Losh 		return (CMD_ERROR);
1586ca987d46SWarner Losh 	}
1587ca987d46SWarner Losh 
1588ca987d46SWarner Losh 	ndesc = sz / dsz;
1589ca987d46SWarner Losh 	snprintf(line, sizeof(line), "%23s %12s %12s %8s %4s\n",
1590ca987d46SWarner Losh 	    "Type", "Physical", "Virtual", "#Pages", "Attr");
1591ca987d46SWarner Losh 	pager_open();
1592ca987d46SWarner Losh 	if (pager_output(line)) {
1593ca987d46SWarner Losh 		pager_close();
1594ca987d46SWarner Losh 		return (CMD_OK);
1595ca987d46SWarner Losh 	}
1596ca987d46SWarner Losh 
1597ca987d46SWarner Losh 	for (i = 0, p = map; i < ndesc;
1598ca987d46SWarner Losh 	     i++, p = NextMemoryDescriptor(p, dsz)) {
159934ada209SToomas Soome 		snprintf(line, sizeof(line), "%23s %012jx %012jx %08jx ",
160034ada209SToomas Soome 		    efi_memory_type(p->Type), (uintmax_t)p->PhysicalStart,
160134ada209SToomas Soome 		    (uintmax_t)p->VirtualStart, (uintmax_t)p->NumberOfPages);
160234ada209SToomas Soome 		if (pager_output(line))
160334ada209SToomas Soome 			break;
160434ada209SToomas Soome 
1605ca987d46SWarner Losh 		if (p->Attribute & EFI_MEMORY_UC)
1606ca987d46SWarner Losh 			printf("UC ");
1607ca987d46SWarner Losh 		if (p->Attribute & EFI_MEMORY_WC)
1608ca987d46SWarner Losh 			printf("WC ");
1609ca987d46SWarner Losh 		if (p->Attribute & EFI_MEMORY_WT)
1610ca987d46SWarner Losh 			printf("WT ");
1611ca987d46SWarner Losh 		if (p->Attribute & EFI_MEMORY_WB)
1612ca987d46SWarner Losh 			printf("WB ");
1613ca987d46SWarner Losh 		if (p->Attribute & EFI_MEMORY_UCE)
1614ca987d46SWarner Losh 			printf("UCE ");
1615ca987d46SWarner Losh 		if (p->Attribute & EFI_MEMORY_WP)
1616ca987d46SWarner Losh 			printf("WP ");
1617ca987d46SWarner Losh 		if (p->Attribute & EFI_MEMORY_RP)
1618ca987d46SWarner Losh 			printf("RP ");
1619ca987d46SWarner Losh 		if (p->Attribute & EFI_MEMORY_XP)
1620ca987d46SWarner Losh 			printf("XP ");
162134ada209SToomas Soome 		if (p->Attribute & EFI_MEMORY_NV)
162234ada209SToomas Soome 			printf("NV ");
162334ada209SToomas Soome 		if (p->Attribute & EFI_MEMORY_MORE_RELIABLE)
162434ada209SToomas Soome 			printf("MR ");
162534ada209SToomas Soome 		if (p->Attribute & EFI_MEMORY_RO)
162634ada209SToomas Soome 			printf("RO ");
1627ca987d46SWarner Losh 		if (pager_output("\n"))
1628ca987d46SWarner Losh 			break;
1629ca987d46SWarner Losh 	}
1630ca987d46SWarner Losh 
1631ca987d46SWarner Losh 	pager_close();
1632ca987d46SWarner Losh 	return (CMD_OK);
1633ca987d46SWarner Losh }
1634ca987d46SWarner Losh 
1635ca987d46SWarner Losh COMMAND_SET(configuration, "configuration", "print configuration tables",
1636ca987d46SWarner Losh     command_configuration);
1637ca987d46SWarner Losh 
1638ca987d46SWarner Losh static int
1639ca987d46SWarner Losh command_configuration(int argc, char *argv[])
1640ca987d46SWarner Losh {
1641ca987d46SWarner Losh 	UINTN i;
164265641822SToomas Soome 	char *name;
1643ca987d46SWarner Losh 
164465641822SToomas Soome 	printf("NumberOfTableEntries=%lu\n",
1645ca987d46SWarner Losh 		(unsigned long)ST->NumberOfTableEntries);
1646ca987d46SWarner Losh 
1647ca987d46SWarner Losh 	for (i = 0; i < ST->NumberOfTableEntries; i++) {
1648ca987d46SWarner Losh 		EFI_GUID *guid;
1649ca987d46SWarner Losh 
1650ca987d46SWarner Losh 		printf("  ");
1651ca987d46SWarner Losh 		guid = &ST->ConfigurationTable[i].VendorGuid;
165265641822SToomas Soome 
165365641822SToomas Soome 		if (efi_guid_to_name(guid, &name) == true) {
165465641822SToomas Soome 			printf(name);
165565641822SToomas Soome 			free(name);
165665641822SToomas Soome 		} else {
165765641822SToomas Soome 			printf("Error while translating UUID to name");
165865641822SToomas Soome 		}
165965641822SToomas Soome 		printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
1660ca987d46SWarner Losh 	}
1661ca987d46SWarner Losh 
1662ca987d46SWarner Losh 	return (CMD_OK);
1663ca987d46SWarner Losh }
1664ca987d46SWarner Losh 
1665ca987d46SWarner Losh 
1666ca987d46SWarner Losh COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
1667ca987d46SWarner Losh 
1668ca987d46SWarner Losh static int
1669ca987d46SWarner Losh command_mode(int argc, char *argv[])
1670ca987d46SWarner Losh {
1671ca987d46SWarner Losh 	UINTN cols, rows;
1672ca987d46SWarner Losh 	unsigned int mode;
1673ca987d46SWarner Losh 	int i;
1674ca987d46SWarner Losh 	char *cp;
1675ca987d46SWarner Losh 	EFI_STATUS status;
1676ca987d46SWarner Losh 	SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
1677ca987d46SWarner Losh 
1678ca987d46SWarner Losh 	conout = ST->ConOut;
1679ca987d46SWarner Losh 
1680ca987d46SWarner Losh 	if (argc > 1) {
1681ca987d46SWarner Losh 		mode = strtol(argv[1], &cp, 0);
1682ca987d46SWarner Losh 		if (cp[0] != '\0') {
1683ca987d46SWarner Losh 			printf("Invalid mode\n");
1684ca987d46SWarner Losh 			return (CMD_ERROR);
1685ca987d46SWarner Losh 		}
1686ca987d46SWarner Losh 		status = conout->QueryMode(conout, mode, &cols, &rows);
1687ca987d46SWarner Losh 		if (EFI_ERROR(status)) {
1688ca987d46SWarner Losh 			printf("invalid mode %d\n", mode);
1689ca987d46SWarner Losh 			return (CMD_ERROR);
1690ca987d46SWarner Losh 		}
1691ca987d46SWarner Losh 		status = conout->SetMode(conout, mode);
1692ca987d46SWarner Losh 		if (EFI_ERROR(status)) {
1693ca987d46SWarner Losh 			printf("couldn't set mode %d\n", mode);
1694ca987d46SWarner Losh 			return (CMD_ERROR);
1695ca987d46SWarner Losh 		}
16963630506bSToomas Soome 		(void) cons_update_mode(true);
1697ca987d46SWarner Losh 		return (CMD_OK);
1698ca987d46SWarner Losh 	}
1699ca987d46SWarner Losh 
1700ca987d46SWarner Losh 	printf("Current mode: %d\n", conout->Mode->Mode);
1701ca987d46SWarner Losh 	for (i = 0; i <= conout->Mode->MaxMode; i++) {
1702ca987d46SWarner Losh 		status = conout->QueryMode(conout, i, &cols, &rows);
1703ca987d46SWarner Losh 		if (EFI_ERROR(status))
1704ca987d46SWarner Losh 			continue;
1705ca987d46SWarner Losh 		printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
1706ca987d46SWarner Losh 		    (unsigned)rows);
1707ca987d46SWarner Losh 	}
1708ca987d46SWarner Losh 
1709ca987d46SWarner Losh 	if (i != 0)
1710ca987d46SWarner Losh 		printf("Select a mode with the command \"mode <number>\"\n");
1711ca987d46SWarner Losh 
1712ca987d46SWarner Losh 	return (CMD_OK);
1713ca987d46SWarner Losh }
1714ca987d46SWarner Losh 
171565641822SToomas Soome COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
171665641822SToomas Soome 
17172e0d67c3SEmmanuel Vadot static void
17182e0d67c3SEmmanuel Vadot lsefi_print_handle_info(EFI_HANDLE handle)
17192e0d67c3SEmmanuel Vadot {
17202e0d67c3SEmmanuel Vadot 	EFI_DEVICE_PATH *devpath;
17212e0d67c3SEmmanuel Vadot 	EFI_DEVICE_PATH *imagepath;
17222e0d67c3SEmmanuel Vadot 	CHAR16 *dp_name;
17232e0d67c3SEmmanuel Vadot 
17242e0d67c3SEmmanuel Vadot 	imagepath = efi_lookup_image_devpath(handle);
17252e0d67c3SEmmanuel Vadot 	if (imagepath != NULL) {
17262e0d67c3SEmmanuel Vadot 		dp_name = efi_devpath_name(imagepath);
17272e0d67c3SEmmanuel Vadot 		printf("Handle for image %S", dp_name);
17282e0d67c3SEmmanuel Vadot 		efi_free_devpath_name(dp_name);
17292e0d67c3SEmmanuel Vadot 		return;
17302e0d67c3SEmmanuel Vadot 	}
17312e0d67c3SEmmanuel Vadot 	devpath = efi_lookup_devpath(handle);
17322e0d67c3SEmmanuel Vadot 	if (devpath != NULL) {
17332e0d67c3SEmmanuel Vadot 		dp_name = efi_devpath_name(devpath);
17342e0d67c3SEmmanuel Vadot 		printf("Handle for device %S", dp_name);
17352e0d67c3SEmmanuel Vadot 		efi_free_devpath_name(dp_name);
17362e0d67c3SEmmanuel Vadot 		return;
17372e0d67c3SEmmanuel Vadot 	}
17382e0d67c3SEmmanuel Vadot 	printf("Handle %p", handle);
17392e0d67c3SEmmanuel Vadot }
17402e0d67c3SEmmanuel Vadot 
174165641822SToomas Soome static int
174265641822SToomas Soome command_lsefi(int argc __unused, char *argv[] __unused)
174365641822SToomas Soome {
174465641822SToomas Soome 	char *name;
174565641822SToomas Soome 	EFI_HANDLE *buffer = NULL;
174665641822SToomas Soome 	EFI_HANDLE handle;
174765641822SToomas Soome 	UINTN bufsz = 0, i, j;
174865641822SToomas Soome 	EFI_STATUS status;
17490dc2db13SToomas Soome 	int ret = 0;
175065641822SToomas Soome 
175165641822SToomas Soome 	status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
175265641822SToomas Soome 	if (status != EFI_BUFFER_TOO_SMALL) {
175365641822SToomas Soome 		snprintf(command_errbuf, sizeof (command_errbuf),
175465641822SToomas Soome 		    "unexpected error: %lld", (long long)status);
175565641822SToomas Soome 		return (CMD_ERROR);
175665641822SToomas Soome 	}
175765641822SToomas Soome 	if ((buffer = malloc(bufsz)) == NULL) {
175865641822SToomas Soome 		sprintf(command_errbuf, "out of memory");
175965641822SToomas Soome 		return (CMD_ERROR);
176065641822SToomas Soome 	}
176165641822SToomas Soome 
176265641822SToomas Soome 	status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
176365641822SToomas Soome 	if (EFI_ERROR(status)) {
176465641822SToomas Soome 		free(buffer);
176565641822SToomas Soome 		snprintf(command_errbuf, sizeof (command_errbuf),
176665641822SToomas Soome 		    "LocateHandle() error: %lld", (long long)status);
176765641822SToomas Soome 		return (CMD_ERROR);
176865641822SToomas Soome 	}
176965641822SToomas Soome 
177065641822SToomas Soome 	pager_open();
177165641822SToomas Soome 	for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
177265641822SToomas Soome 		UINTN nproto = 0;
177365641822SToomas Soome 		EFI_GUID **protocols = NULL;
177465641822SToomas Soome 
177565641822SToomas Soome 		handle = buffer[i];
17762e0d67c3SEmmanuel Vadot 		lsefi_print_handle_info(handle);
177765641822SToomas Soome 		if (pager_output("\n"))
177865641822SToomas Soome 			break;
177965641822SToomas Soome 		/* device path */
178065641822SToomas Soome 
178165641822SToomas Soome 		status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
178265641822SToomas Soome 		if (EFI_ERROR(status)) {
178365641822SToomas Soome 			snprintf(command_errbuf, sizeof (command_errbuf),
178465641822SToomas Soome 			    "ProtocolsPerHandle() error: %lld",
178565641822SToomas Soome 			    (long long)status);
178665641822SToomas Soome 			continue;
178765641822SToomas Soome 		}
178865641822SToomas Soome 
178965641822SToomas Soome 		for (j = 0; j < nproto; j++) {
179065641822SToomas Soome 			if (efi_guid_to_name(protocols[j], &name) == true) {
179165641822SToomas Soome 				printf("  %s", name);
179265641822SToomas Soome 				free(name);
179365641822SToomas Soome 			} else {
179465641822SToomas Soome 				printf("Error while translating UUID to name");
179565641822SToomas Soome 			}
179665641822SToomas Soome 			if ((ret = pager_output("\n")) != 0)
179765641822SToomas Soome 				break;
179865641822SToomas Soome 		}
179965641822SToomas Soome 		BS->FreePool(protocols);
180065641822SToomas Soome 		if (ret != 0)
180165641822SToomas Soome 			break;
180265641822SToomas Soome 	}
180365641822SToomas Soome 	pager_close();
180465641822SToomas Soome 	free(buffer);
180565641822SToomas Soome 	return (CMD_OK);
180665641822SToomas Soome }
180765641822SToomas Soome 
1808ca987d46SWarner Losh #ifdef LOADER_FDT_SUPPORT
1809ca987d46SWarner Losh extern int command_fdt_internal(int argc, char *argv[]);
1810ca987d46SWarner Losh 
1811ca987d46SWarner Losh /*
1812ca987d46SWarner Losh  * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
1813ca987d46SWarner Losh  * and declaring it as extern is in contradiction with COMMAND_SET() macro
1814ca987d46SWarner Losh  * (which uses static pointer), we're defining wrapper function, which
1815ca987d46SWarner Losh  * calls the proper fdt handling routine.
1816ca987d46SWarner Losh  */
1817ca987d46SWarner Losh static int
1818ca987d46SWarner Losh command_fdt(int argc, char *argv[])
1819ca987d46SWarner Losh {
1820ca987d46SWarner Losh 
1821ca987d46SWarner Losh 	return (command_fdt_internal(argc, argv));
1822ca987d46SWarner Losh }
1823ca987d46SWarner Losh 
1824ca987d46SWarner Losh COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
1825ca987d46SWarner Losh #endif
1826ca987d46SWarner Losh 
1827ca987d46SWarner Losh /*
1828ca987d46SWarner Losh  * Chain load another efi loader.
1829ca987d46SWarner Losh  */
1830ca987d46SWarner Losh static int
1831ca987d46SWarner Losh command_chain(int argc, char *argv[])
1832ca987d46SWarner Losh {
1833ca987d46SWarner Losh 	EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
1834ca987d46SWarner Losh 	EFI_HANDLE loaderhandle;
1835ca987d46SWarner Losh 	EFI_LOADED_IMAGE *loaded_image;
1836ca987d46SWarner Losh 	EFI_STATUS status;
1837ca987d46SWarner Losh 	struct stat st;
1838ca987d46SWarner Losh 	struct devdesc *dev;
1839ca987d46SWarner Losh 	char *name, *path;
1840ca987d46SWarner Losh 	void *buf;
1841ca987d46SWarner Losh 	int fd;
1842ca987d46SWarner Losh 
1843ca987d46SWarner Losh 	if (argc < 2) {
1844ca987d46SWarner Losh 		command_errmsg = "wrong number of arguments";
1845ca987d46SWarner Losh 		return (CMD_ERROR);
1846ca987d46SWarner Losh 	}
1847ca987d46SWarner Losh 
1848ca987d46SWarner Losh 	name = argv[1];
1849ca987d46SWarner Losh 
1850ca987d46SWarner Losh 	if ((fd = open(name, O_RDONLY)) < 0) {
1851ca987d46SWarner Losh 		command_errmsg = "no such file";
1852ca987d46SWarner Losh 		return (CMD_ERROR);
1853ca987d46SWarner Losh 	}
1854ca987d46SWarner Losh 
1855435672e3SMarcin Wojtas #ifdef LOADER_VERIEXEC
1856afc571b1SSimon J. Gerraty 	if (verify_file(fd, name, 0, VE_MUST, __func__) < 0) {
1857435672e3SMarcin Wojtas 		sprintf(command_errbuf, "can't verify: %s", name);
1858435672e3SMarcin Wojtas 		close(fd);
1859435672e3SMarcin Wojtas 		return (CMD_ERROR);
1860435672e3SMarcin Wojtas 	}
1861435672e3SMarcin Wojtas #endif
1862435672e3SMarcin Wojtas 
1863ca987d46SWarner Losh 	if (fstat(fd, &st) < -1) {
1864ca987d46SWarner Losh 		command_errmsg = "stat failed";
1865ca987d46SWarner Losh 		close(fd);
1866ca987d46SWarner Losh 		return (CMD_ERROR);
1867ca987d46SWarner Losh 	}
1868ca987d46SWarner Losh 
1869ca987d46SWarner Losh 	status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
1870ca987d46SWarner Losh 	if (status != EFI_SUCCESS) {
1871ca987d46SWarner Losh 		command_errmsg = "failed to allocate buffer";
1872ca987d46SWarner Losh 		close(fd);
1873ca987d46SWarner Losh 		return (CMD_ERROR);
1874ca987d46SWarner Losh 	}
1875ca987d46SWarner Losh 	if (read(fd, buf, st.st_size) != st.st_size) {
1876ca987d46SWarner Losh 		command_errmsg = "error while reading the file";
1877ca987d46SWarner Losh 		(void)BS->FreePool(buf);
1878ca987d46SWarner Losh 		close(fd);
1879ca987d46SWarner Losh 		return (CMD_ERROR);
1880ca987d46SWarner Losh 	}
1881ca987d46SWarner Losh 	close(fd);
1882ca987d46SWarner Losh 	status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle);
1883ca987d46SWarner Losh 	(void)BS->FreePool(buf);
1884ca987d46SWarner Losh 	if (status != EFI_SUCCESS) {
1885ca987d46SWarner Losh 		command_errmsg = "LoadImage failed";
1886ca987d46SWarner Losh 		return (CMD_ERROR);
1887ca987d46SWarner Losh 	}
1888110d56cbSToomas Soome 	status = OpenProtocolByHandle(loaderhandle, &LoadedImageGUID,
1889ca987d46SWarner Losh 	    (void **)&loaded_image);
1890ca987d46SWarner Losh 
1891ca987d46SWarner Losh 	if (argc > 2) {
1892ca987d46SWarner Losh 		int i, len = 0;
1893ca987d46SWarner Losh 		CHAR16 *argp;
1894ca987d46SWarner Losh 
1895ca987d46SWarner Losh 		for (i = 2; i < argc; i++)
1896ca987d46SWarner Losh 			len += strlen(argv[i]) + 1;
1897ca987d46SWarner Losh 
1898ca987d46SWarner Losh 		len *= sizeof (*argp);
1899ca987d46SWarner Losh 		loaded_image->LoadOptions = argp = malloc (len);
1900ca987d46SWarner Losh 		loaded_image->LoadOptionsSize = len;
1901ca987d46SWarner Losh 		for (i = 2; i < argc; i++) {
1902ca987d46SWarner Losh 			char *ptr = argv[i];
1903ca987d46SWarner Losh 			while (*ptr)
1904ca987d46SWarner Losh 				*(argp++) = *(ptr++);
1905ca987d46SWarner Losh 			*(argp++) = ' ';
1906ca987d46SWarner Losh 		}
1907ca987d46SWarner Losh 		*(--argv) = 0;
1908ca987d46SWarner Losh 	}
1909ca987d46SWarner Losh 
1910ca987d46SWarner Losh 	if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
1911ca987d46SWarner Losh #ifdef EFI_ZFS_BOOT
1912ca987d46SWarner Losh 		struct zfs_devdesc *z_dev;
1913ca987d46SWarner Losh #endif
1914ca987d46SWarner Losh 		struct disk_devdesc *d_dev;
1915ca987d46SWarner Losh 		pdinfo_t *hd, *pd;
1916ca987d46SWarner Losh 
1917ad00892fSWarner Losh 		switch (dev->d_dev->dv_type) {
1918ca987d46SWarner Losh #ifdef EFI_ZFS_BOOT
1919ca987d46SWarner Losh 		case DEVT_ZFS:
1920ca987d46SWarner Losh 			z_dev = (struct zfs_devdesc *)dev;
1921ca987d46SWarner Losh 			loaded_image->DeviceHandle =
1922ca987d46SWarner Losh 			    efizfs_get_handle_by_guid(z_dev->pool_guid);
1923ca987d46SWarner Losh 			break;
1924ca987d46SWarner Losh #endif
1925ca987d46SWarner Losh 		case DEVT_NET:
1926ca987d46SWarner Losh 			loaded_image->DeviceHandle =
1927ca987d46SWarner Losh 			    efi_find_handle(dev->d_dev, dev->d_unit);
1928ca987d46SWarner Losh 			break;
1929ca987d46SWarner Losh 		default:
1930ca987d46SWarner Losh 			hd = efiblk_get_pdinfo(dev);
1931ca987d46SWarner Losh 			if (STAILQ_EMPTY(&hd->pd_part)) {
1932ca987d46SWarner Losh 				loaded_image->DeviceHandle = hd->pd_handle;
1933ca987d46SWarner Losh 				break;
1934ca987d46SWarner Losh 			}
1935ca987d46SWarner Losh 			d_dev = (struct disk_devdesc *)dev;
1936ca987d46SWarner Losh 			STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
1937ca987d46SWarner Losh 				/*
1938ca987d46SWarner Losh 				 * d_partition should be 255
1939ca987d46SWarner Losh 				 */
1940ca987d46SWarner Losh 				if (pd->pd_unit == (uint32_t)d_dev->d_slice) {
1941ca987d46SWarner Losh 					loaded_image->DeviceHandle =
1942ca987d46SWarner Losh 					    pd->pd_handle;
1943ca987d46SWarner Losh 					break;
1944ca987d46SWarner Losh 				}
1945ca987d46SWarner Losh 			}
1946ca987d46SWarner Losh 			break;
1947ca987d46SWarner Losh 		}
1948ca987d46SWarner Losh 	}
1949ca987d46SWarner Losh 
1950ca987d46SWarner Losh 	dev_cleanup();
1951ca987d46SWarner Losh 	status = BS->StartImage(loaderhandle, NULL, NULL);
1952ca987d46SWarner Losh 	if (status != EFI_SUCCESS) {
1953ca987d46SWarner Losh 		command_errmsg = "StartImage failed";
1954ca987d46SWarner Losh 		free(loaded_image->LoadOptions);
1955ca987d46SWarner Losh 		loaded_image->LoadOptions = NULL;
1956ca987d46SWarner Losh 		status = BS->UnloadImage(loaded_image);
1957ca987d46SWarner Losh 		return (CMD_ERROR);
1958ca987d46SWarner Losh 	}
1959ca987d46SWarner Losh 
1960ca987d46SWarner Losh 	return (CMD_ERROR);	/* not reached */
1961ca987d46SWarner Losh }
1962ca987d46SWarner Losh 
1963ca987d46SWarner Losh COMMAND_SET(chain, "chain", "chain load file", command_chain);
19645b5438c6SMichal Meloun 
196547ef2a13STatiana #if defined(LOADER_NET_SUPPORT)
19665b5438c6SMichal Meloun extern struct in_addr servip;
19675b5438c6SMichal Meloun static int
19685b5438c6SMichal Meloun command_netserver(int argc, char *argv[])
19695b5438c6SMichal Meloun {
19705b5438c6SMichal Meloun 	char *proto;
19715b5438c6SMichal Meloun 	n_long rootaddr;
19725b5438c6SMichal Meloun 
19735b5438c6SMichal Meloun 	if (argc > 2) {
19745b5438c6SMichal Meloun 		command_errmsg = "wrong number of arguments";
19755b5438c6SMichal Meloun 		return (CMD_ERROR);
19765b5438c6SMichal Meloun 	}
19775b5438c6SMichal Meloun 	if (argc < 2) {
19785b5438c6SMichal Meloun 		proto = netproto == NET_TFTP ? "tftp://" : "nfs://";
19795b5438c6SMichal Meloun 		printf("Netserver URI: %s%s%s\n", proto, intoa(rootip.s_addr),
19805b5438c6SMichal Meloun 		    rootpath);
19815b5438c6SMichal Meloun 		return (CMD_OK);
19825b5438c6SMichal Meloun 	}
19835b5438c6SMichal Meloun 	if (argc == 2) {
19845b5438c6SMichal Meloun 		strncpy(rootpath, argv[1], sizeof(rootpath));
19855b5438c6SMichal Meloun 		rootpath[sizeof(rootpath) -1] = '\0';
19865b5438c6SMichal Meloun 		if ((rootaddr = net_parse_rootpath()) != INADDR_NONE)
19875b5438c6SMichal Meloun 			servip.s_addr = rootip.s_addr = rootaddr;
19885b5438c6SMichal Meloun 		return (CMD_OK);
19895b5438c6SMichal Meloun 	}
19905b5438c6SMichal Meloun 	return (CMD_ERROR);	/* not reached */
19915b5438c6SMichal Meloun 
19925b5438c6SMichal Meloun }
19935b5438c6SMichal Meloun 
19945b5438c6SMichal Meloun COMMAND_SET(netserver, "netserver", "change or display netserver URI",
19955b5438c6SMichal Meloun     command_netserver);
199647ef2a13STatiana #endif
1997