xref: /illumos-gate/usr/src/boot/efi/libefi/libefi.c (revision 69764de32bf29a19d2a8b8832adafc4bd4f934f4)
122028508SToomas Soome /*
222028508SToomas Soome  * Copyright (c) 2000 Doug Rabson
322028508SToomas Soome  * All rights reserved.
422028508SToomas Soome  *
522028508SToomas Soome  * Redistribution and use in source and binary forms, with or without
622028508SToomas Soome  * modification, are permitted provided that the following conditions
722028508SToomas Soome  * are met:
822028508SToomas Soome  * 1. Redistributions of source code must retain the above copyright
922028508SToomas Soome  *    notice, this list of conditions and the following disclaimer.
1022028508SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
1122028508SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
1222028508SToomas Soome  *    documentation and/or other materials provided with the distribution.
1322028508SToomas Soome  *
1422028508SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1522028508SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1622028508SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1722028508SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1822028508SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1922028508SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2022028508SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2122028508SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2222028508SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2322028508SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2422028508SToomas Soome  * SUCH DAMAGE.
2522028508SToomas Soome  */
2622028508SToomas Soome 
2722028508SToomas Soome #include <sys/cdefs.h>
2822028508SToomas Soome 
2922028508SToomas Soome #include <efi.h>
3022028508SToomas Soome #include <efilib.h>
3122028508SToomas Soome 
3222028508SToomas Soome EFI_HANDLE		IH;
3322028508SToomas Soome EFI_SYSTEM_TABLE	*ST;
3422028508SToomas Soome EFI_BOOT_SERVICES	*BS;
3522028508SToomas Soome EFI_RUNTIME_SERVICES	*RS;
3622028508SToomas Soome 
3722028508SToomas Soome void *
efi_get_table(EFI_GUID * tbl)3822028508SToomas Soome efi_get_table(EFI_GUID *tbl)
3922028508SToomas Soome {
4022028508SToomas Soome 	EFI_GUID *id;
4122028508SToomas Soome 	int i;
4222028508SToomas Soome 
4322028508SToomas Soome 	for (i = 0; i < ST->NumberOfTableEntries; i++) {
4422028508SToomas Soome 		id = &ST->ConfigurationTable[i].VendorGuid;
45*69764de3SToomas Soome 		if (memcmp(id, tbl, sizeof (EFI_GUID)) == 0)
4622028508SToomas Soome 			return (ST->ConfigurationTable[i].VendorTable);
4722028508SToomas Soome 	}
4822028508SToomas Soome 	return (NULL);
4922028508SToomas Soome }
5022028508SToomas Soome 
5122028508SToomas Soome EFI_STATUS
OpenProtocolByHandle(EFI_HANDLE handle,EFI_GUID * protocol,void ** interface)5222028508SToomas Soome OpenProtocolByHandle(EFI_HANDLE handle, EFI_GUID *protocol, void **interface)
5322028508SToomas Soome {
5422028508SToomas Soome 	return (BS->OpenProtocol(handle, protocol, interface, IH, NULL,
5522028508SToomas Soome 	    EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL));
5622028508SToomas Soome }
57*69764de3SToomas Soome 
58*69764de3SToomas Soome /*
59*69764de3SToomas Soome  * Allocate memory and query list of handles for indicated protocol.
60*69764de3SToomas Soome  * Returns EFI status, list of handles and number of handles in list.
61*69764de3SToomas Soome  * Caller needs to release the allocated memory.
62*69764de3SToomas Soome  */
63*69764de3SToomas Soome EFI_STATUS
efi_get_protocol_handles(EFI_GUID * protocolguid,uint_t * nhandles,EFI_HANDLE ** handlep)64*69764de3SToomas Soome efi_get_protocol_handles(EFI_GUID *protocolguid, uint_t *nhandles,
65*69764de3SToomas Soome     EFI_HANDLE **handlep)
66*69764de3SToomas Soome {
67*69764de3SToomas Soome 	UINTN bufsz = 0;
68*69764de3SToomas Soome 	EFI_STATUS status;
69*69764de3SToomas Soome 	EFI_HANDLE *handles;
70*69764de3SToomas Soome 
71*69764de3SToomas Soome 	/*
72*69764de3SToomas Soome 	 * get buffer size
73*69764de3SToomas Soome 	 */
74*69764de3SToomas Soome 	*nhandles = 0;
75*69764de3SToomas Soome 	handles = NULL;
76*69764de3SToomas Soome 	status = BS->LocateHandle(ByProtocol, protocolguid,
77*69764de3SToomas Soome 	    NULL, &bufsz, handles);
78*69764de3SToomas Soome 	if (status != EFI_BUFFER_TOO_SMALL)
79*69764de3SToomas Soome 		return (status);
80*69764de3SToomas Soome 
81*69764de3SToomas Soome 	handles = malloc(bufsz);
82*69764de3SToomas Soome 	if (handles == NULL)
83*69764de3SToomas Soome 		return (errno_to_efi_status(ENOMEM));
84*69764de3SToomas Soome 
85*69764de3SToomas Soome 	*nhandles = (uint_t)(bufsz / sizeof (EFI_HANDLE));
86*69764de3SToomas Soome 	/*
87*69764de3SToomas Soome 	 * get handle array
88*69764de3SToomas Soome 	 */
89*69764de3SToomas Soome 	status = BS->LocateHandle(ByProtocol, protocolguid,
90*69764de3SToomas Soome 	    NULL, &bufsz, handles);
91*69764de3SToomas Soome 	if (EFI_ERROR(status)) {
92*69764de3SToomas Soome 		free(handles);
93*69764de3SToomas Soome 		*nhandles = 0;
94*69764de3SToomas Soome 	} else {
95*69764de3SToomas Soome 		*handlep = handles;
96*69764de3SToomas Soome 	}
97*69764de3SToomas Soome 	return (status);
98*69764de3SToomas Soome }
99