efiio.c revision 1.4 1 1.4 riastrad /* $NetBSD: efiio.c,v 1.4 2025/03/30 14:36:48 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.4 riastrad __RCSID("$NetBSD: efiio.c,v 1.4 2025/03/30 14:36:48 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/ioctl.h>
33 1.1 christos
34 1.1 christos #include <assert.h>
35 1.1 christos #include <err.h>
36 1.1 christos #include <errno.h>
37 1.1 christos #include <stdbool.h>
38 1.1 christos #include <stdio.h>
39 1.1 christos #include <stdlib.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 "utils.h"
45 1.1 christos
46 1.1 christos //******************************************************
47 1.1 christos // Variable Attributes
48 1.1 christos //******************************************************
49 1.1 christos #if 0 /* see sys/efiio.h */
50 1.1 christos struct efi_var_ioc {
51 1.1 christos uint16_t * name; /* vendor's variable name */
52 1.1 christos size_t namesize; /* size in bytes of the name buffer */
53 1.1 christos struct uuid vendor; /* unique identifier for vendor */
54 1.1 christos uint32_t attrib; /* variable attribute bitmask */
55 1.1 christos void * data; /* buffer containing variable data */
56 1.1 christos size_t datasize; /* size in bytes of the data buffer */
57 1.1 christos };
58 1.1 christos #endif
59 1.1 christos
60 1.1 christos static int
61 1.1 christos xioctl(int fd, unsigned long request, void *buf)
62 1.1 christos {
63 1.1 christos int rv;
64 1.1 christos
65 1.1 christos rv = ioctl(fd, request, buf);
66 1.1 christos if (rv == -1 && errno != ENOENT)
67 1.1 christos err(EXIT_FAILURE, "%s: ioctl", __func__);
68 1.1 christos return rv;
69 1.1 christos }
70 1.1 christos
71 1.1 christos PUBLIC int
72 1.1 christos set_variable(int fd, struct efi_var_ioc *ev)
73 1.1 christos {
74 1.1 christos
75 1.1 christos return ioctl(fd, EFIIOC_VAR_SET, ev);
76 1.1 christos }
77 1.1 christos
78 1.1 christos static inline int
79 1.1 christos get_variable_core(int fd, struct efi_var_ioc *ev)
80 1.1 christos {
81 1.1 christos
82 1.1 christos if (xioctl(fd, EFIIOC_VAR_GET, ev) == -1)
83 1.1 christos return -1;
84 1.1 christos
85 1.1 christos ev->data = emalloc(ev->datasize);
86 1.1 christos xioctl(fd, EFIIOC_VAR_GET, ev);
87 1.1 christos return 0;
88 1.1 christos }
89 1.1 christos
90 1.1 christos PUBLIC struct efi_var_ioc
91 1.1 christos get_variable(int fd, const char *name, struct uuid *vendor,
92 1.1 christos uint32_t attrib)
93 1.1 christos {
94 1.1 christos struct efi_var_ioc ev;
95 1.1 christos
96 1.1 christos efi_var_init(&ev, name, vendor, attrib);
97 1.1 christos get_variable_core(fd, &ev);
98 1.1 christos
99 1.1 christos return ev;
100 1.1 christos }
101 1.1 christos
102 1.1 christos PUBLIC struct efi_var_ioc *
103 1.1 christos get_next_variable(int fd, struct efi_var_ioc *ev)
104 1.1 christos {
105 1.1 christos int rv;
106 1.1 christos
107 1.1 christos rv = ioctl(fd, EFIIOC_VAR_NEXT, ev);
108 1.1 christos if (rv == -1 && errno != ENOENT)
109 1.1 christos err(EXIT_FAILURE, "%s: ioctl", __func__);
110 1.1 christos return ev;
111 1.1 christos }
112 1.1 christos
113 1.1 christos PUBLIC void *
114 1.1 christos get_table(int fd, struct uuid *uuid, size_t *buflen)
115 1.1 christos {
116 1.1 christos struct efi_get_table_ioc egt;
117 1.1 christos
118 1.1 christos memset(&egt, 0, sizeof(egt));
119 1.1 christos memcpy(&egt.uuid, uuid, sizeof(egt.uuid));
120 1.1 christos
121 1.1 christos xioctl(fd, EFIIOC_GET_TABLE, &egt);
122 1.1 christos egt.buf = ecalloc(egt.table_len, 1);
123 1.1 christos egt.buf_len = egt.table_len;
124 1.1 christos
125 1.1 christos xioctl(fd, EFIIOC_GET_TABLE, &egt);
126 1.1 christos *buflen = egt.table_len;
127 1.1 christos return egt.buf;
128 1.1 christos }
129 1.1 christos
130 1.1 christos PUBLIC size_t
131 1.1 christos get_variable_info(int fd, bool (*choose)(struct efi_var_ioc *, void *),
132 1.1 christos int (*fn)(struct efi_var_ioc *ev, void *), void *arg)
133 1.1 christos {
134 1.1 christos struct efi_var_ioc ev;
135 1.1 christos size_t cnt;
136 1.1 christos int rv;
137 1.1 christos
138 1.1 christos assert(fn != NULL);
139 1.1 christos assert(arg != NULL);
140 1.1 christos
141 1.1 christos memset(&ev, 0, sizeof(ev));
142 1.1 christos
143 1.4 riastrad ev.name = ecalloc(EFI_VARNAME_MAXBYTES, 1);
144 1.1 christos cnt = 0;
145 1.1 christos for (;;) {
146 1.4 riastrad ev.namesize = EFI_VARNAME_MAXBYTES;
147 1.1 christos if (ioctl(fd, EFIIOC_VAR_NEXT, &ev) == -1) {
148 1.1 christos char *buf;
149 1.1 christos
150 1.1 christos if (errno == ENOENT)
151 1.1 christos break;
152 1.2 riastrad
153 1.1 christos /* XXX: ev is likely to be zero */
154 1.1 christos buf = ucs2_to_utf8(ev.name, ev.namesize, NULL, NULL);
155 1.1 christos err(EXIT_FAILURE, "%s: '%s'", __func__, buf);
156 1.1 christos }
157 1.1 christos
158 1.1 christos if (choose != NULL && !choose(&ev, arg))
159 1.1 christos continue;
160 1.1 christos
161 1.1 christos rv = get_variable_core(fd, &ev);
162 1.1 christos assert(rv == 0);
163 1.1 christos if (rv == -1)
164 1.1 christos err(EXIT_FAILURE, "get_variable_core");
165 1.1 christos
166 1.1 christos cnt++;
167 1.1 christos fn(&ev, arg);
168 1.2 riastrad
169 1.1 christos free(ev.data);
170 1.1 christos }
171 1.1 christos free(ev.name);
172 1.1 christos return cnt;
173 1.1 christos }
174