xref: /illumos-gate/usr/src/boot/efi/libefi/handles.c (revision 22028508)
1*22028508SToomas Soome /*-
2*22028508SToomas Soome  * Copyright (c) 2006 Marcel Moolenaar
3*22028508SToomas Soome  * All rights reserved.
4*22028508SToomas Soome  *
5*22028508SToomas Soome  * Redistribution and use in source and binary forms, with or without
6*22028508SToomas Soome  * modification, are permitted provided that the following conditions
7*22028508SToomas Soome  * are met:
8*22028508SToomas Soome  *
9*22028508SToomas Soome  * 1. Redistributions of source code must retain the above copyright
10*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer.
11*22028508SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
12*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
13*22028508SToomas Soome  *    documentation and/or other materials provided with the distribution.
14*22028508SToomas Soome  *
15*22028508SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*22028508SToomas Soome  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*22028508SToomas Soome  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*22028508SToomas Soome  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*22028508SToomas Soome  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*22028508SToomas Soome  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*22028508SToomas Soome  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*22028508SToomas Soome  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*22028508SToomas Soome  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*22028508SToomas Soome  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*22028508SToomas Soome  */
26*22028508SToomas Soome 
27*22028508SToomas Soome #include <sys/cdefs.h>
28*22028508SToomas Soome __FBSDID("$FreeBSD$");
29*22028508SToomas Soome 
30*22028508SToomas Soome #include <efi.h>
31*22028508SToomas Soome #include <efilib.h>
32*22028508SToomas Soome 
33*22028508SToomas Soome struct entry {
34*22028508SToomas Soome 	EFI_HANDLE handle;
35*22028508SToomas Soome 	EFI_HANDLE alias;
36*22028508SToomas Soome 	struct devsw *dev;
37*22028508SToomas Soome 	int unit;
38*22028508SToomas Soome 	uint64_t extra;
39*22028508SToomas Soome };
40*22028508SToomas Soome 
41*22028508SToomas Soome struct entry *entry;
42*22028508SToomas Soome int nentries;
43*22028508SToomas Soome 
44*22028508SToomas Soome int
efi_register_handles(struct devsw * sw,EFI_HANDLE * handles,EFI_HANDLE * aliases,int count)45*22028508SToomas Soome efi_register_handles(struct devsw *sw, EFI_HANDLE *handles,
46*22028508SToomas Soome     EFI_HANDLE *aliases, int count)
47*22028508SToomas Soome {
48*22028508SToomas Soome 	size_t sz;
49*22028508SToomas Soome 	int idx, unit;
50*22028508SToomas Soome 
51*22028508SToomas Soome 	idx = nentries;
52*22028508SToomas Soome 	nentries += count;
53*22028508SToomas Soome 	sz = nentries * sizeof(struct entry);
54*22028508SToomas Soome 	entry = (entry == NULL) ? malloc(sz) : realloc(entry, sz);
55*22028508SToomas Soome 	for (unit = 0; idx < nentries; idx++, unit++) {
56*22028508SToomas Soome 		entry[idx].handle = handles[unit];
57*22028508SToomas Soome 		if (aliases != NULL)
58*22028508SToomas Soome 			entry[idx].alias = aliases[unit];
59*22028508SToomas Soome 		else
60*22028508SToomas Soome 			entry[idx].alias = NULL;
61*22028508SToomas Soome 		entry[idx].dev = sw;
62*22028508SToomas Soome 		entry[idx].unit = unit;
63*22028508SToomas Soome 	}
64*22028508SToomas Soome 	return (0);
65*22028508SToomas Soome }
66*22028508SToomas Soome 
67*22028508SToomas Soome EFI_HANDLE
efi_find_handle(struct devsw * dev,int unit)68*22028508SToomas Soome efi_find_handle(struct devsw *dev, int unit)
69*22028508SToomas Soome {
70*22028508SToomas Soome 	int idx;
71*22028508SToomas Soome 
72*22028508SToomas Soome 	for (idx = 0; idx < nentries; idx++) {
73*22028508SToomas Soome 		if (entry[idx].dev != dev)
74*22028508SToomas Soome 			continue;
75*22028508SToomas Soome 		if (entry[idx].unit != unit)
76*22028508SToomas Soome 			continue;
77*22028508SToomas Soome 		return (entry[idx].handle);
78*22028508SToomas Soome 	}
79*22028508SToomas Soome 	return (NULL);
80*22028508SToomas Soome }
81*22028508SToomas Soome 
82*22028508SToomas Soome int
efi_handle_lookup(EFI_HANDLE h,struct devsw ** dev,int * unit,uint64_t * extra)83*22028508SToomas Soome efi_handle_lookup(EFI_HANDLE h, struct devsw **dev, int *unit, uint64_t *extra)
84*22028508SToomas Soome {
85*22028508SToomas Soome 	int idx;
86*22028508SToomas Soome 
87*22028508SToomas Soome 	for (idx = 0; idx < nentries; idx++) {
88*22028508SToomas Soome 		if (entry[idx].handle != h && entry[idx].alias != h)
89*22028508SToomas Soome 			continue;
90*22028508SToomas Soome 		if (dev != NULL)
91*22028508SToomas Soome 			*dev = entry[idx].dev;
92*22028508SToomas Soome 		if (unit != NULL)
93*22028508SToomas Soome 			*unit = entry[idx].unit;
94*22028508SToomas Soome 		if (extra != NULL)
95*22028508SToomas Soome 			*extra = entry[idx].extra;
96*22028508SToomas Soome 		return (0);
97*22028508SToomas Soome 	}
98*22028508SToomas Soome 	return (ENOENT);
99*22028508SToomas Soome }
100*22028508SToomas Soome 
101*22028508SToomas Soome int
efi_handle_update_dev(EFI_HANDLE h,struct devsw * dev,int unit,uint64_t guid)102*22028508SToomas Soome efi_handle_update_dev(EFI_HANDLE h, struct devsw *dev, int unit,
103*22028508SToomas Soome     uint64_t guid)
104*22028508SToomas Soome {
105*22028508SToomas Soome 	int idx;
106*22028508SToomas Soome 
107*22028508SToomas Soome 	for (idx = 0; idx < nentries; idx++) {
108*22028508SToomas Soome 		if (entry[idx].handle != h)
109*22028508SToomas Soome 			continue;
110*22028508SToomas Soome 		entry[idx].dev = dev;
111*22028508SToomas Soome 		entry[idx].unit = unit;
112*22028508SToomas Soome 		entry[idx].alias = NULL;
113*22028508SToomas Soome 		entry[idx].extra = guid;
114*22028508SToomas Soome 		return (0);
115*22028508SToomas Soome 	}
116*22028508SToomas Soome 
117*22028508SToomas Soome 	return (ENOENT);
118*22028508SToomas Soome }
119