Home | History | Annotate | Line # | Download | only in efi
      1  1.2  riastrad /* $NetBSD: getvars.c,v 1.2 2025/03/02 00:03:41 riastradh Exp $ */
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Redistribution and use in source and binary forms, with or without
      5  1.1  christos  * modification, are permitted provided that the following conditions
      6  1.1  christos  * are met:
      7  1.1  christos  * 1. Redistributions of source code must retain the above copyright
      8  1.1  christos  *    notice, this list of conditions and the following disclaimer.
      9  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     11  1.1  christos  *    documentation and/or other materials provided with the distribution.
     12  1.1  christos  *
     13  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     14  1.1  christos  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  1.1  christos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16  1.1  christos  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     17  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     18  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     19  1.1  christos  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     20  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     21  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     22  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     23  1.1  christos  * SUCH DAMAGE.
     24  1.1  christos  */
     25  1.1  christos 
     26  1.1  christos #include <sys/cdefs.h>
     27  1.1  christos #ifndef lint
     28  1.2  riastrad __RCSID("$NetBSD: getvars.c,v 1.2 2025/03/02 00:03:41 riastradh Exp $");
     29  1.1  christos #endif /* not lint */
     30  1.1  christos 
     31  1.1  christos #include <sys/efiio.h>
     32  1.1  christos #include <sys/queue.h>
     33  1.1  christos 
     34  1.1  christos #include <assert.h>
     35  1.1  christos #include <err.h>
     36  1.1  christos #include <regex.h>
     37  1.1  christos #include <stdio.h>
     38  1.1  christos #include <stdlib.h>
     39  1.1  christos #include <string.h>
     40  1.1  christos #include <util.h>
     41  1.1  christos 
     42  1.1  christos #include "defs.h"
     43  1.1  christos #include "efiio.h"
     44  1.1  christos #include "getvars.h"
     45  1.1  christos #include "utils.h"
     46  1.1  christos 
     47  1.1  christos typedef SLIST_HEAD(efi_var_head, efi_var_elm) efi_var_head_t;
     48  1.1  christos 
     49  1.1  christos typedef struct efi_var_elm {
     50  1.1  christos 	efi_var_t	v;
     51  1.1  christos 	SLIST_ENTRY(efi_var_elm) entry;
     52  1.1  christos } efi_var_elm_t;
     53  1.1  christos 
     54  1.1  christos typedef efi_var_head_t getvars_hdl_t;
     55  1.1  christos 
     56  1.1  christos struct fn_args {
     57  1.1  christos 	efi_var_head_t *list_head;
     58  1.1  christos 	regex_t preg;
     59  1.1  christos 	char *name;
     60  1.1  christos };
     61  1.1  christos 
     62  1.1  christos /****************************************/
     63  1.1  christos static void
     64  1.1  christos efi_var_cpy(efi_var_ioc_t *dst, efi_var_ioc_t *src)
     65  1.1  christos {
     66  1.1  christos 
     67  1.1  christos 	dst->name     = memdup(src->name, src->namesize);
     68  1.1  christos 	dst->namesize = src->namesize;
     69  1.1  christos 	dst->vendor   = src->vendor;
     70  1.1  christos 	dst->attrib   = src->attrib;
     71  1.1  christos 
     72  1.1  christos 	/* do not dup the data buffer */
     73  1.1  christos 	dst->data     = src->data;
     74  1.1  christos 	dst->datasize = src->datasize;
     75  1.1  christos 	src->data = NULL;
     76  1.1  christos 	src->datasize = 0;
     77  1.1  christos }
     78  1.1  christos 
     79  1.1  christos /****************************************/
     80  1.1  christos 
     81  1.1  christos static int
     82  1.1  christos var_name_cmp(const void *a, const void *b)
     83  1.1  christos {
     84  1.1  christos 	efi_var_t *x = *(efi_var_t * const *)a;
     85  1.1  christos 	efi_var_t *y = *(efi_var_t * const *)b;
     86  1.1  christos 	int rv;
     87  1.1  christos 
     88  1.1  christos 	rv = strcmp(x->name, y->name);
     89  1.1  christos 	if (rv != 0)
     90  1.1  christos 		return rv;
     91  1.1  christos 
     92  1.1  christos 	return memcmp(&y->ev.vendor, &x->ev.vendor, sizeof(x->ev.vendor));
     93  1.1  christos }
     94  1.1  christos 
     95  1.1  christos /****************************************/
     96  1.1  christos 
     97  1.1  christos static int
     98  1.1  christos save_variable(efi_var_ioc_t *ev, void *vp)
     99  1.1  christos {
    100  1.1  christos 	struct fn_args *args = vp;
    101  1.1  christos 	efi_var_head_t *head = args->list_head;
    102  1.1  christos 	efi_var_elm_t *elm;
    103  1.1  christos 
    104  1.1  christos 	assert(args->name != NULL);
    105  1.1  christos 
    106  1.1  christos 	elm = ecalloc(sizeof(*elm), 1);
    107  1.1  christos 	efi_var_cpy(&elm->v.ev, ev);
    108  1.1  christos 
    109  1.1  christos 	elm->v.name = args->name;
    110  1.1  christos 
    111  1.1  christos 	args->name = NULL;
    112  1.1  christos 
    113  1.1  christos 	SLIST_INSERT_HEAD(head, elm, entry);
    114  1.1  christos 
    115  1.1  christos 	return 0;
    116  1.1  christos }
    117  1.1  christos 
    118  1.1  christos static bool
    119  1.1  christos choose_variable(efi_var_ioc_t *ev, void *vp)
    120  1.1  christos {
    121  1.1  christos 	struct fn_args *args = vp;
    122  1.1  christos 	bool rv;
    123  1.1  christos 
    124  1.1  christos 	args->name = ucs2_to_utf8(ev->name, ev->namesize, NULL, NULL);
    125  1.1  christos 
    126  1.1  christos 	rv = !regexec(&args->preg, args->name, 0, NULL, 0);
    127  1.1  christos 	if (rv == false) {
    128  1.1  christos 		free(args->name);
    129  1.1  christos 		args->name = NULL;
    130  1.1  christos 	}
    131  1.1  christos 	return rv;
    132  1.1  christos }
    133  1.1  christos 
    134  1.1  christos PUBLIC void *
    135  1.1  christos get_variables(int fd, const char *regexp, efi_var_t ***array_ptr,
    136  1.1  christos     size_t *array_cnt)
    137  1.1  christos {
    138  1.1  christos 	static efi_var_head_t list_head;
    139  1.1  christos 	efi_var_elm_t *elm;
    140  1.1  christos 	efi_var_t **var_array;
    141  1.1  christos 	size_t var_cnt;
    142  1.1  christos 	int i;
    143  1.1  christos 	struct fn_args args;
    144  1.1  christos 
    145  1.1  christos 	assert(SLIST_EMPTY(&list_head));
    146  1.1  christos 
    147  1.1  christos 	SLIST_INIT(&list_head);
    148  1.1  christos 
    149  1.1  christos 	memset(&args, 0, sizeof(args));
    150  1.1  christos 	args.list_head = &list_head;
    151  1.1  christos 	if (regcomp(&args.preg, regexp, REG_EXTENDED) != 0)
    152  1.1  christos 		err(EXIT_FAILURE, "regcomp: %s", regexp);
    153  1.2  riastrad 
    154  1.1  christos 	var_cnt = get_variable_info(fd, choose_variable, save_variable, &args);
    155  1.1  christos 
    156  1.1  christos 	regfree(&args.preg);
    157  1.1  christos 
    158  1.1  christos 	var_array = emalloc(var_cnt * sizeof(*var_array));
    159  1.1  christos 	i = 0;
    160  1.1  christos 	SLIST_FOREACH(elm, &list_head, entry) {
    161  1.1  christos 		var_array[i++] = &elm->v;
    162  1.1  christos 	}
    163  1.1  christos 	qsort(var_array, var_cnt, sizeof(*var_array), var_name_cmp);
    164  1.2  riastrad 
    165  1.1  christos 	*array_ptr = var_array;
    166  1.1  christos 	*array_cnt = var_cnt;
    167  1.1  christos 
    168  1.1  christos 	return &list_head;
    169  1.1  christos }
    170  1.1  christos 
    171  1.1  christos static void
    172  1.1  christos free_efi_var_ioc(efi_var_ioc_t *ev)
    173  1.1  christos {
    174  1.2  riastrad 
    175  1.1  christos 	free(ev->name);
    176  1.1  christos 	free(ev->data);
    177  1.1  christos 	memset(ev, 0, sizeof(*ev));
    178  1.1  christos }
    179  1.1  christos 
    180  1.1  christos static void
    181  1.1  christos free_efi_var(efi_var_t *v)
    182  1.1  christos {
    183  1.2  riastrad 
    184  1.1  christos 	free(v->name);
    185  1.1  christos 	memset(v, 0, sizeof(*v));
    186  1.1  christos }
    187  1.1  christos 
    188  1.1  christos PUBLIC void
    189  1.1  christos free_variables(void *vp)
    190  1.1  christos {
    191  1.1  christos 	efi_var_head_t *list_head = vp;
    192  1.1  christos 	efi_var_elm_t *elm;
    193  1.2  riastrad 
    194  1.1  christos 	while (!SLIST_EMPTY(list_head)) {
    195  1.1  christos 		elm = SLIST_FIRST(list_head);
    196  1.1  christos 		SLIST_REMOVE_HEAD(list_head, entry);
    197  1.1  christos 		free_efi_var_ioc(&elm->v.ev);
    198  1.1  christos 		free_efi_var(&elm->v);
    199  1.1  christos 		free(elm);
    200  1.1  christos 	}
    201  1.1  christos }
    202