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;
35164010Smarcel	struct devsw *dev;
36164010Smarcel	int unit;
37164010Smarcel};
38164010Smarcel
39164010Smarcelstruct entry *entry;
40164010Smarcelint nentries;
41164010Smarcel
42164010Smarcelint
43164010Smarcelefi_register_handles(struct devsw *sw, EFI_HANDLE *handles, int count)
44164010Smarcel{
45164010Smarcel	size_t sz;
46164010Smarcel	int idx, unit;
47164010Smarcel
48164010Smarcel	idx = nentries;
49164010Smarcel	nentries += count;
50164010Smarcel	sz = nentries * sizeof(struct entry);
51164010Smarcel	entry = (entry == NULL) ? malloc(sz) : realloc(entry, sz);
52164010Smarcel	for (unit = 0; idx < nentries; idx++, unit++) {
53164010Smarcel		entry[idx].handle = handles[unit];
54164010Smarcel		entry[idx].dev = sw;
55164010Smarcel		entry[idx].unit = unit;
56164010Smarcel	}
57164010Smarcel	return (0);
58164010Smarcel}
59164010Smarcel
60164010SmarcelEFI_HANDLE
61164010Smarcelefi_find_handle(struct devsw *dev, int unit)
62164010Smarcel{
63164010Smarcel	int idx;
64164010Smarcel
65164010Smarcel	for (idx = 0; idx < nentries; idx++) {
66164010Smarcel		if (entry[idx].dev != dev)
67164010Smarcel			continue;
68164010Smarcel		if (entry[idx].unit != unit)
69164010Smarcel			continue;
70164010Smarcel		return (entry[idx].handle);
71164010Smarcel	}
72164010Smarcel	return (NULL);
73164010Smarcel}
74164010Smarcel
75164010Smarcelint
76164010Smarcelefi_handle_lookup(EFI_HANDLE h, struct devsw **dev, int *unit)
77164010Smarcel{
78164010Smarcel	int idx;
79164010Smarcel
80164010Smarcel	for (idx = 0; idx < nentries; idx++) {
81164010Smarcel		if (entry[idx].handle != h)
82164010Smarcel			continue;
83164010Smarcel		if (dev != NULL)
84164010Smarcel			*dev = entry[idx].dev;
85164010Smarcel		if (unit != NULL)
86164010Smarcel			*unit = entry[idx].unit;
87164010Smarcel		return (0);
88164010Smarcel	}
89164010Smarcel	return (ENOENT);
90164010Smarcel}
91