199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2006 Marcel Moolenaar
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efi.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efilib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct entry {
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_HANDLE handle;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_HANDLE alias;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct devsw *dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t extra;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct entry *entry;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint nentries;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefi_register_handles(struct devsw *sw, EFI_HANDLE *handles,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_HANDLE *aliases, int count)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t sz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int idx, unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome idx = nentries;
199767f8919635c4928607450d9e0abb932109ceToomas Soome nentries += count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = nentries * sizeof(struct entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry = (entry == NULL) ? malloc(sz) : realloc(entry, sz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (unit = 0; idx < nentries; idx++, unit++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry[idx].handle = handles[unit];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (aliases != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry[idx].alias = aliases[unit];
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry[idx].alias = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry[idx].dev = sw;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry[idx].unit = unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeEFI_HANDLE
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefi_find_handle(struct devsw *dev, int unit)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int idx;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (idx = 0; idx < nentries; idx++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry[idx].dev != dev)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry[idx].unit != unit)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (entry[idx].handle);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefi_handle_lookup(EFI_HANDLE h, struct devsw **dev, int *unit, uint64_t *extra)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int idx;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (idx = 0; idx < nentries; idx++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry[idx].handle != h && entry[idx].alias != h)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dev != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *dev = entry[idx].dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (unit != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *unit = entry[idx].unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (extra != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *extra = entry[idx].extra;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefi_handle_update_dev(EFI_HANDLE h, struct devsw *dev, int unit,
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t guid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int idx;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (idx = 0; idx < nentries; idx++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (entry[idx].handle != h)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry[idx].dev = dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry[idx].unit = unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry[idx].alias = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome entry[idx].extra = guid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}