1164010Smarcel/*-
2164010Smarcel * Copyright (c) 2006 Marcel Moolenaar
3164010Smarcel * All rights reserved.
4164010Smarcel *
5164010Smarcel * Redistribution and use in source and binary forms, with or without
6164010Smarcel * modification, are permitted provided that the following conditions
7164010Smarcel * are met:
8164010Smarcel *
9164010Smarcel * 1. Redistributions of source code must retain the above copyright
10164010Smarcel *    notice, this list of conditions and the following disclaimer.
11164010Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12164010Smarcel *    notice, this list of conditions and the following disclaimer in the
13164010Smarcel *    documentation and/or other materials provided with the distribution.
14164010Smarcel *
15164010Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16164010Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17164010Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18164010Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19164010Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20164010Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21164010Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22164010Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23164010Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24164010Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25164010Smarcel */
26164010Smarcel
27164010Smarcel#include <sys/cdefs.h>
28164010Smarcel__FBSDID("$FreeBSD$");
29164010Smarcel
30164010Smarcel#include <efi.h>
31164010Smarcel#include <efilib.h>
32164010Smarcel
33164010Smarcelstruct entry {
34164010Smarcel	EFI_HANDLE handle;
35271135Semaste	EFI_HANDLE alias;
36164010Smarcel	struct devsw *dev;
37164010Smarcel	int unit;
38294999Ssmh	uint64_t extra;
39164010Smarcel};
40164010Smarcel
41164010Smarcelstruct entry *entry;
42164010Smarcelint nentries;
43164010Smarcel
44164010Smarcelint
45271135Semasteefi_register_handles(struct devsw *sw, EFI_HANDLE *handles,
46271135Semaste    EFI_HANDLE *aliases, int count)
47164010Smarcel{
48164010Smarcel	size_t sz;
49164010Smarcel	int idx, unit;
50164010Smarcel
51164010Smarcel	idx = nentries;
52164010Smarcel	nentries += count;
53164010Smarcel	sz = nentries * sizeof(struct entry);
54164010Smarcel	entry = (entry == NULL) ? malloc(sz) : realloc(entry, sz);
55164010Smarcel	for (unit = 0; idx < nentries; idx++, unit++) {
56164010Smarcel		entry[idx].handle = handles[unit];
57271135Semaste		if (aliases != NULL)
58271135Semaste			entry[idx].alias = aliases[unit];
59271135Semaste		else
60271135Semaste			entry[idx].alias = NULL;
61164010Smarcel		entry[idx].dev = sw;
62164010Smarcel		entry[idx].unit = unit;
63164010Smarcel	}
64164010Smarcel	return (0);
65164010Smarcel}
66164010Smarcel
67164010SmarcelEFI_HANDLE
68164010Smarcelefi_find_handle(struct devsw *dev, int unit)
69164010Smarcel{
70164010Smarcel	int idx;
71164010Smarcel
72164010Smarcel	for (idx = 0; idx < nentries; idx++) {
73164010Smarcel		if (entry[idx].dev != dev)
74164010Smarcel			continue;
75164010Smarcel		if (entry[idx].unit != unit)
76164010Smarcel			continue;
77164010Smarcel		return (entry[idx].handle);
78164010Smarcel	}
79164010Smarcel	return (NULL);
80164010Smarcel}
81164010Smarcel
82164010Smarcelint
83294999Ssmhefi_handle_lookup(EFI_HANDLE h, struct devsw **dev, int *unit, uint64_t *extra)
84164010Smarcel{
85164010Smarcel	int idx;
86164010Smarcel
87164010Smarcel	for (idx = 0; idx < nentries; idx++) {
88271135Semaste		if (entry[idx].handle != h && entry[idx].alias != h)
89164010Smarcel			continue;
90164010Smarcel		if (dev != NULL)
91164010Smarcel			*dev = entry[idx].dev;
92164010Smarcel		if (unit != NULL)
93164010Smarcel			*unit = entry[idx].unit;
94294999Ssmh		if (extra != NULL)
95294999Ssmh			*extra = entry[idx].extra;
96164010Smarcel		return (0);
97164010Smarcel	}
98164010Smarcel	return (ENOENT);
99164010Smarcel}
100294999Ssmh
101294999Ssmhint
102294999Ssmhefi_handle_update_dev(EFI_HANDLE h, struct devsw *dev, int unit,
103294999Ssmh    uint64_t guid)
104294999Ssmh{
105294999Ssmh	int idx;
106294999Ssmh
107294999Ssmh	for (idx = 0; idx < nentries; idx++) {
108294999Ssmh		if (entry[idx].handle != h)
109294999Ssmh			continue;
110294999Ssmh		entry[idx].dev = dev;
111294999Ssmh		entry[idx].unit = unit;
112294999Ssmh		entry[idx].alias = NULL;
113294999Ssmh		entry[idx].extra = guid;
114294999Ssmh		return (0);
115294999Ssmh	}
116294999Ssmh
117294999Ssmh	return (ENOENT);
118294999Ssmh}
119