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