/*-1* Copyright (c) 2006 Marcel Moolenaar2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#include <efi.h>27#include <efilib.h>2829struct entry {30EFI_HANDLE handle;31EFI_HANDLE alias;32struct devsw *dev;33int unit;34uint64_t extra;35};3637struct entry *entry;38int nentries;3940int41efi_register_handles(struct devsw *sw, EFI_HANDLE *handles,42EFI_HANDLE *aliases, int count)43{44size_t sz;45int idx, unit;4647idx = nentries;48nentries += count;49sz = nentries * sizeof(struct entry);50entry = (entry == NULL) ? malloc(sz) : realloc(entry, sz);51if (entry == NULL)52return (ENOMEM);53for (unit = 0; idx < nentries; idx++, unit++) {54entry[idx].handle = handles[unit];55if (aliases != NULL)56entry[idx].alias = aliases[unit];57else58entry[idx].alias = NULL;59entry[idx].dev = sw;60entry[idx].unit = unit;61}62return (0);63}6465EFI_HANDLE66efi_find_handle(struct devsw *dev, int unit)67{68int idx;6970for (idx = 0; idx < nentries; idx++) {71if (entry[idx].dev != dev)72continue;73if (entry[idx].unit != unit)74continue;75return (entry[idx].handle);76}77return (NULL);78}7980int81efi_handle_lookup(EFI_HANDLE h, struct devsw **dev, int *unit, uint64_t *extra)82{83int idx;8485for (idx = 0; idx < nentries; idx++) {86if (entry[idx].handle != h && entry[idx].alias != h)87continue;88if (dev != NULL)89*dev = entry[idx].dev;90if (unit != NULL)91*unit = entry[idx].unit;92if (extra != NULL)93*extra = entry[idx].extra;94return (0);95}96return (ENOENT);97}9899int100efi_handle_update_dev(EFI_HANDLE h, struct devsw *dev, int unit,101uint64_t guid)102{103int idx;104105for (idx = 0; idx < nentries; idx++) {106if (entry[idx].handle != h)107continue;108entry[idx].dev = dev;109entry[idx].unit = unit;110entry[idx].alias = NULL;111entry[idx].extra = guid;112return (0);113}114115return (ENOENT);116}117118119