xref: /illumos-gate/usr/src/boot/efi/libefi/handles.c (revision 22028508)
1199767f8SToomas Soome /*-
2199767f8SToomas Soome  * Copyright (c) 2006 Marcel Moolenaar
3199767f8SToomas Soome  * All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome  * modification, are permitted provided that the following conditions
7199767f8SToomas Soome  * are met:
8199767f8SToomas Soome  *
9199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
10199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
11199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
12199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
13199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
14199767f8SToomas Soome  *
15199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16199767f8SToomas Soome  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17199767f8SToomas Soome  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18199767f8SToomas Soome  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19199767f8SToomas Soome  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20199767f8SToomas Soome  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21199767f8SToomas Soome  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22199767f8SToomas Soome  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23199767f8SToomas Soome  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24199767f8SToomas Soome  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25199767f8SToomas Soome  */
26199767f8SToomas Soome 
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome __FBSDID("$FreeBSD$");
29199767f8SToomas Soome 
30199767f8SToomas Soome #include <efi.h>
31199767f8SToomas Soome #include <efilib.h>
32199767f8SToomas Soome 
33199767f8SToomas Soome struct entry {
34199767f8SToomas Soome 	EFI_HANDLE handle;
35199767f8SToomas Soome 	EFI_HANDLE alias;
36199767f8SToomas Soome 	struct devsw *dev;
37199767f8SToomas Soome 	int unit;
38199767f8SToomas Soome 	uint64_t extra;
39199767f8SToomas Soome };
40199767f8SToomas Soome 
41199767f8SToomas Soome struct entry *entry;
42199767f8SToomas Soome int nentries;
43199767f8SToomas Soome 
44199767f8SToomas Soome int
efi_register_handles(struct devsw * sw,EFI_HANDLE * handles,EFI_HANDLE * aliases,int count)45199767f8SToomas Soome efi_register_handles(struct devsw *sw, EFI_HANDLE *handles,
46199767f8SToomas Soome     EFI_HANDLE *aliases, int count)
47199767f8SToomas Soome {
48199767f8SToomas Soome 	size_t sz;
49199767f8SToomas Soome 	int idx, unit;
50199767f8SToomas Soome 
51199767f8SToomas Soome 	idx = nentries;
52199767f8SToomas Soome 	nentries += count;
53199767f8SToomas Soome 	sz = nentries * sizeof(struct entry);
54199767f8SToomas Soome 	entry = (entry == NULL) ? malloc(sz) : realloc(entry, sz);
55199767f8SToomas Soome 	for (unit = 0; idx < nentries; idx++, unit++) {
56199767f8SToomas Soome 		entry[idx].handle = handles[unit];
57199767f8SToomas Soome 		if (aliases != NULL)
58199767f8SToomas Soome 			entry[idx].alias = aliases[unit];
59199767f8SToomas Soome 		else
60199767f8SToomas Soome 			entry[idx].alias = NULL;
61199767f8SToomas Soome 		entry[idx].dev = sw;
62199767f8SToomas Soome 		entry[idx].unit = unit;
63199767f8SToomas Soome 	}
64199767f8SToomas Soome 	return (0);
65199767f8SToomas Soome }
66199767f8SToomas Soome 
67199767f8SToomas Soome EFI_HANDLE
efi_find_handle(struct devsw * dev,int unit)68199767f8SToomas Soome efi_find_handle(struct devsw *dev, int unit)
69199767f8SToomas Soome {
70199767f8SToomas Soome 	int idx;
71199767f8SToomas Soome 
72199767f8SToomas Soome 	for (idx = 0; idx < nentries; idx++) {
73199767f8SToomas Soome 		if (entry[idx].dev != dev)
74199767f8SToomas Soome 			continue;
75199767f8SToomas Soome 		if (entry[idx].unit != unit)
76199767f8SToomas Soome 			continue;
77199767f8SToomas Soome 		return (entry[idx].handle);
78199767f8SToomas Soome 	}
79199767f8SToomas Soome 	return (NULL);
80199767f8SToomas Soome }
81199767f8SToomas Soome 
82199767f8SToomas Soome int
efi_handle_lookup(EFI_HANDLE h,struct devsw ** dev,int * unit,uint64_t * extra)83199767f8SToomas Soome efi_handle_lookup(EFI_HANDLE h, struct devsw **dev, int *unit, uint64_t *extra)
84199767f8SToomas Soome {
85199767f8SToomas Soome 	int idx;
86199767f8SToomas Soome 
87199767f8SToomas Soome 	for (idx = 0; idx < nentries; idx++) {
88199767f8SToomas Soome 		if (entry[idx].handle != h && entry[idx].alias != h)
89199767f8SToomas Soome 			continue;
90199767f8SToomas Soome 		if (dev != NULL)
91199767f8SToomas Soome 			*dev = entry[idx].dev;
92199767f8SToomas Soome 		if (unit != NULL)
93199767f8SToomas Soome 			*unit = entry[idx].unit;
94199767f8SToomas Soome 		if (extra != NULL)
95199767f8SToomas Soome 			*extra = entry[idx].extra;
96199767f8SToomas Soome 		return (0);
97199767f8SToomas Soome 	}
98199767f8SToomas Soome 	return (ENOENT);
99199767f8SToomas Soome }
100199767f8SToomas Soome 
101199767f8SToomas Soome int
efi_handle_update_dev(EFI_HANDLE h,struct devsw * dev,int unit,uint64_t guid)102199767f8SToomas Soome efi_handle_update_dev(EFI_HANDLE h, struct devsw *dev, int unit,
103199767f8SToomas Soome     uint64_t guid)
104199767f8SToomas Soome {
105199767f8SToomas Soome 	int idx;
106199767f8SToomas Soome 
107199767f8SToomas Soome 	for (idx = 0; idx < nentries; idx++) {
108199767f8SToomas Soome 		if (entry[idx].handle != h)
109199767f8SToomas Soome 			continue;
110199767f8SToomas Soome 		entry[idx].dev = dev;
111199767f8SToomas Soome 		entry[idx].unit = unit;
112199767f8SToomas Soome 		entry[idx].alias = NULL;
113199767f8SToomas Soome 		entry[idx].extra = guid;
114199767f8SToomas Soome 		return (0);
115199767f8SToomas Soome 	}
116199767f8SToomas Soome 
117199767f8SToomas Soome 	return (ENOENT);
118199767f8SToomas Soome }
119