fwcfg.c revision 1.6 1 1.6 pho /* $NetBSD: fwcfg.c,v 1.6 2021/12/04 06:42:39 pho Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.6 pho __RCSID("$NetBSD: fwcfg.c,v 1.6 2021/12/04 06:42:39 pho Exp $");
31 1.6 pho
32 1.6 pho #define FUSE_USE_VERSION FUSE_MAKE_VERSION(2, 6)
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/ioctl.h>
35 1.1 jmcneill
36 1.1 jmcneill #include <err.h>
37 1.1 jmcneill #include <errno.h>
38 1.1 jmcneill #include <fcntl.h>
39 1.1 jmcneill #include <fuse.h>
40 1.1 jmcneill #include <stdio.h>
41 1.1 jmcneill #include <stdlib.h>
42 1.1 jmcneill #include <string.h>
43 1.1 jmcneill #include <unistd.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/ic/qemufwcfgio.h>
46 1.1 jmcneill
47 1.1 jmcneill #include "virtdir.h"
48 1.1 jmcneill
49 1.1 jmcneill #define _PATH_FWCFG "/dev/qemufwcfg"
50 1.1 jmcneill
51 1.1 jmcneill struct fwcfg_file {
52 1.1 jmcneill uint32_t size;
53 1.1 jmcneill uint16_t select;
54 1.1 jmcneill uint16_t reserved;
55 1.1 jmcneill char name[56];
56 1.1 jmcneill };
57 1.1 jmcneill
58 1.1 jmcneill static int fwcfg_fd;
59 1.1 jmcneill static mode_t fwcfg_dir_mask = 0555;
60 1.1 jmcneill static mode_t fwcfg_file_mask = 0444;
61 1.1 jmcneill static uid_t fwcfg_uid;
62 1.1 jmcneill static gid_t fwcfg_gid;
63 1.1 jmcneill
64 1.1 jmcneill static virtdir_t fwcfg_virtdir;
65 1.1 jmcneill
66 1.1 jmcneill static void
67 1.1 jmcneill set_index(uint16_t index)
68 1.1 jmcneill {
69 1.1 jmcneill if (ioctl(fwcfg_fd, FWCFGIO_SET_INDEX, &index) != 0)
70 1.1 jmcneill err(EXIT_FAILURE, "failed to set index 0x%04x", index);
71 1.1 jmcneill }
72 1.1 jmcneill
73 1.1 jmcneill static void
74 1.1 jmcneill read_data(void *buf, size_t buflen)
75 1.1 jmcneill {
76 1.1 jmcneill if (read(fwcfg_fd, buf, buflen) != (ssize_t)buflen)
77 1.1 jmcneill err(EXIT_FAILURE, "failed to read data");
78 1.1 jmcneill }
79 1.1 jmcneill
80 1.1 jmcneill static int
81 1.1 jmcneill fwcfg_getattr(const char *path, struct stat *st)
82 1.1 jmcneill {
83 1.1 jmcneill virt_dirent_t *ep;
84 1.1 jmcneill
85 1.1 jmcneill if (strcmp(path, "/") == 0) {
86 1.1 jmcneill memset(st, 0, sizeof(*st));
87 1.1 jmcneill st->st_mode = S_IFDIR | fwcfg_dir_mask;
88 1.1 jmcneill st->st_nlink = 2;
89 1.1 jmcneill return 0;
90 1.1 jmcneill }
91 1.1 jmcneill
92 1.1 jmcneill if ((ep = virtdir_find(&fwcfg_virtdir, path, strlen(path))) == NULL)
93 1.1 jmcneill return -ENOENT;
94 1.1 jmcneill
95 1.1 jmcneill switch (ep->type) {
96 1.1 jmcneill case 'f':
97 1.1 jmcneill memcpy(st, &fwcfg_virtdir.file, sizeof(*st));
98 1.2 christos st->st_size = (off_t)ep->tgtlen;
99 1.1 jmcneill st->st_mode = S_IFREG | fwcfg_file_mask;
100 1.1 jmcneill break;
101 1.1 jmcneill case 'd':
102 1.1 jmcneill memcpy(st, &fwcfg_virtdir.dir, sizeof(*st));
103 1.1 jmcneill st->st_mode = S_IFDIR | fwcfg_dir_mask;
104 1.1 jmcneill break;
105 1.1 jmcneill }
106 1.2 christos st->st_ino = (ino_t)virtdir_offset(&fwcfg_virtdir, ep) + 10;
107 1.1 jmcneill
108 1.1 jmcneill return 0;
109 1.1 jmcneill }
110 1.1 jmcneill
111 1.1 jmcneill static int
112 1.1 jmcneill fwcfg_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
113 1.1 jmcneill off_t offset, struct fuse_file_info *fi)
114 1.1 jmcneill {
115 1.1 jmcneill static VIRTDIR *dirp;
116 1.1 jmcneill virt_dirent_t *dp;
117 1.1 jmcneill
118 1.1 jmcneill if (offset == 0) {
119 1.1 jmcneill if ((dirp = openvirtdir(&fwcfg_virtdir, path)) == NULL)
120 1.1 jmcneill return 0;
121 1.1 jmcneill filler(buf, ".", NULL, 0);
122 1.1 jmcneill filler(buf, "..", NULL, 0);
123 1.1 jmcneill }
124 1.1 jmcneill while ((dp = readvirtdir(dirp)) != NULL) {
125 1.1 jmcneill if (filler(buf, dp->d_name, NULL, 0) != 0)
126 1.1 jmcneill return 0;
127 1.1 jmcneill }
128 1.1 jmcneill closevirtdir(dirp);
129 1.1 jmcneill dirp = NULL;
130 1.1 jmcneill
131 1.1 jmcneill return 0;
132 1.1 jmcneill }
133 1.1 jmcneill
134 1.1 jmcneill static int
135 1.1 jmcneill fwcfg_open(const char *path, struct fuse_file_info *fi)
136 1.1 jmcneill {
137 1.1 jmcneill return 0;
138 1.1 jmcneill }
139 1.1 jmcneill
140 1.1 jmcneill static int
141 1.1 jmcneill fwcfg_read(const char *path, char *buf, size_t size, off_t offset,
142 1.1 jmcneill struct fuse_file_info *fi)
143 1.1 jmcneill {
144 1.1 jmcneill virt_dirent_t *ep;
145 1.1 jmcneill uint8_t tmp[64];
146 1.1 jmcneill
147 1.1 jmcneill if ((ep = virtdir_find(&fwcfg_virtdir, path, strlen(path))) == NULL)
148 1.1 jmcneill return -ENOENT;
149 1.1 jmcneill
150 1.1 jmcneill if (ep->select == 0)
151 1.1 jmcneill return -ENOENT;
152 1.1 jmcneill
153 1.1 jmcneill set_index(ep->select);
154 1.1 jmcneill
155 1.1 jmcneill /* Seek to correct offset */
156 1.1 jmcneill while (offset > 0) {
157 1.2 christos const size_t len = MIN(sizeof(tmp), (size_t)offset);
158 1.1 jmcneill read_data(tmp, len);
159 1.2 christos offset -= (off_t)len;
160 1.1 jmcneill }
161 1.1 jmcneill
162 1.1 jmcneill /* Read the data */
163 1.1 jmcneill read_data(buf, size);
164 1.1 jmcneill
165 1.2 christos return (int)size;
166 1.1 jmcneill }
167 1.1 jmcneill
168 1.1 jmcneill static int
169 1.1 jmcneill fwcfg_statfs(const char *path, struct statvfs *st)
170 1.1 jmcneill {
171 1.1 jmcneill uint32_t count;
172 1.1 jmcneill
173 1.1 jmcneill set_index(FW_CFG_FILE_DIR);
174 1.1 jmcneill read_data(&count, sizeof(count));
175 1.1 jmcneill
176 1.1 jmcneill memset(st, 0, sizeof(*st));
177 1.1 jmcneill st->f_files = be32toh(count);
178 1.1 jmcneill
179 1.1 jmcneill return 0;
180 1.1 jmcneill }
181 1.1 jmcneill
182 1.1 jmcneill static struct fuse_operations fwcfg_ops = {
183 1.1 jmcneill .getattr = fwcfg_getattr,
184 1.1 jmcneill .readdir = fwcfg_readdir,
185 1.1 jmcneill .open = fwcfg_open,
186 1.1 jmcneill .read = fwcfg_read,
187 1.1 jmcneill .statfs = fwcfg_statfs,
188 1.1 jmcneill };
189 1.1 jmcneill
190 1.1 jmcneill static void
191 1.1 jmcneill build_tree(virtdir_t *v)
192 1.1 jmcneill {
193 1.1 jmcneill char path[PATH_MAX];
194 1.1 jmcneill struct fwcfg_file f;
195 1.1 jmcneill uint32_t count, n;
196 1.1 jmcneill struct stat st;
197 1.1 jmcneill
198 1.1 jmcneill memset(&st, 0, sizeof(st));
199 1.1 jmcneill st.st_uid = fwcfg_uid;
200 1.1 jmcneill st.st_gid = fwcfg_gid;
201 1.1 jmcneill virtdir_init(v, NULL, &st, &st, &st);
202 1.1 jmcneill
203 1.1 jmcneill set_index(FW_CFG_FILE_DIR);
204 1.1 jmcneill read_data(&count, sizeof(count));
205 1.1 jmcneill for (n = 0; n < be32toh(count); n++) {
206 1.1 jmcneill read_data(&f, sizeof(f));
207 1.1 jmcneill snprintf(path, sizeof(path), "/%s", f.name);
208 1.1 jmcneill virtdir_add(v, path, strlen(path), 'f', NULL,
209 1.1 jmcneill be32toh(f.size), be16toh(f.select));
210 1.1 jmcneill }
211 1.1 jmcneill }
212 1.1 jmcneill
213 1.4 christos #if 0
214 1.2 christos static __dead void
215 1.2 christos usage(void)
216 1.2 christos {
217 1.5 wiz fprintf(stderr, "Usage: %s [-F path] [-g gid] [-M dir-mode] "
218 1.5 wiz "[-m file-mode] [-u uid] [fuse-options]", getprogname());
219 1.2 christos exit(EXIT_FAILURE);
220 1.2 christos }
221 1.4 christos #endif
222 1.2 christos
223 1.1 jmcneill int
224 1.1 jmcneill main(int argc, char *argv[])
225 1.1 jmcneill {
226 1.1 jmcneill const char *path = _PATH_FWCFG;
227 1.2 christos int ch;
228 1.2 christos long m;
229 1.1 jmcneill char *ep;
230 1.1 jmcneill
231 1.1 jmcneill fwcfg_uid = geteuid();
232 1.1 jmcneill fwcfg_gid = getegid();
233 1.1 jmcneill
234 1.1 jmcneill while ((ch = getopt(argc, argv, "F:g:m:M:u:")) != -1) {
235 1.1 jmcneill switch (ch) {
236 1.1 jmcneill case 'F':
237 1.1 jmcneill path = optarg;
238 1.1 jmcneill break;
239 1.1 jmcneill case 'g':
240 1.2 christos fwcfg_gid = (gid_t)atoi(optarg);
241 1.1 jmcneill break;
242 1.1 jmcneill case 'm':
243 1.1 jmcneill m = strtol(optarg, &ep, 8);
244 1.1 jmcneill if (optarg == ep || *ep || m < 0)
245 1.2 christos errx(EXIT_FAILURE, "invalid file mode: %s",
246 1.2 christos optarg);
247 1.2 christos fwcfg_file_mask = (mode_t)m;
248 1.1 jmcneill break;
249 1.1 jmcneill case 'M':
250 1.1 jmcneill m = strtol(optarg, &ep, 8);
251 1.1 jmcneill if (optarg == ep || *ep || m < 0)
252 1.2 christos errx(EXIT_FAILURE, "invalid directory mode: %s",
253 1.2 christos optarg);
254 1.2 christos fwcfg_dir_mask = (mode_t)m;
255 1.1 jmcneill break;
256 1.1 jmcneill case 'u':
257 1.2 christos fwcfg_uid = (uid_t)atoi(optarg);
258 1.1 jmcneill break;
259 1.3 christos #ifdef notyet
260 1.2 christos default:
261 1.2 christos usage();
262 1.3 christos #endif
263 1.1 jmcneill }
264 1.1 jmcneill }
265 1.1 jmcneill
266 1.1 jmcneill fwcfg_fd = open(path, O_RDWR);
267 1.1 jmcneill if (fwcfg_fd == -1)
268 1.1 jmcneill err(EXIT_FAILURE, "failed to open %s", path);
269 1.1 jmcneill
270 1.1 jmcneill build_tree(&fwcfg_virtdir);
271 1.1 jmcneill
272 1.1 jmcneill return fuse_main(argc, argv, &fwcfg_ops, NULL);
273 1.1 jmcneill }
274