fwcfg.c revision 1.3 1 1.3 christos /* $NetBSD: fwcfg.c,v 1.3 2017/11/26 15:03:15 christos 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.3 christos __RCSID("$NetBSD: fwcfg.c,v 1.3 2017/11/26 15:03:15 christos Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/ioctl.h>
33 1.1 jmcneill
34 1.1 jmcneill #include <err.h>
35 1.1 jmcneill #include <errno.h>
36 1.1 jmcneill #include <fcntl.h>
37 1.1 jmcneill #include <fuse.h>
38 1.1 jmcneill #include <stdio.h>
39 1.1 jmcneill #include <stdlib.h>
40 1.1 jmcneill #include <string.h>
41 1.1 jmcneill #include <unistd.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/ic/qemufwcfgio.h>
44 1.1 jmcneill
45 1.1 jmcneill #include "virtdir.h"
46 1.1 jmcneill
47 1.1 jmcneill #define _PATH_FWCFG "/dev/qemufwcfg"
48 1.1 jmcneill
49 1.1 jmcneill struct fwcfg_file {
50 1.1 jmcneill uint32_t size;
51 1.1 jmcneill uint16_t select;
52 1.1 jmcneill uint16_t reserved;
53 1.1 jmcneill char name[56];
54 1.1 jmcneill };
55 1.1 jmcneill
56 1.1 jmcneill static int fwcfg_fd;
57 1.1 jmcneill static mode_t fwcfg_dir_mask = 0555;
58 1.1 jmcneill static mode_t fwcfg_file_mask = 0444;
59 1.1 jmcneill static uid_t fwcfg_uid;
60 1.1 jmcneill static gid_t fwcfg_gid;
61 1.1 jmcneill
62 1.1 jmcneill static virtdir_t fwcfg_virtdir;
63 1.1 jmcneill
64 1.1 jmcneill static void
65 1.1 jmcneill set_index(uint16_t index)
66 1.1 jmcneill {
67 1.1 jmcneill if (ioctl(fwcfg_fd, FWCFGIO_SET_INDEX, &index) != 0)
68 1.1 jmcneill err(EXIT_FAILURE, "failed to set index 0x%04x", index);
69 1.1 jmcneill }
70 1.1 jmcneill
71 1.1 jmcneill static void
72 1.1 jmcneill read_data(void *buf, size_t buflen)
73 1.1 jmcneill {
74 1.1 jmcneill if (read(fwcfg_fd, buf, buflen) != (ssize_t)buflen)
75 1.1 jmcneill err(EXIT_FAILURE, "failed to read data");
76 1.1 jmcneill }
77 1.1 jmcneill
78 1.1 jmcneill static int
79 1.1 jmcneill fwcfg_getattr(const char *path, struct stat *st)
80 1.1 jmcneill {
81 1.1 jmcneill virt_dirent_t *ep;
82 1.1 jmcneill
83 1.1 jmcneill if (strcmp(path, "/") == 0) {
84 1.1 jmcneill memset(st, 0, sizeof(*st));
85 1.1 jmcneill st->st_mode = S_IFDIR | fwcfg_dir_mask;
86 1.1 jmcneill st->st_nlink = 2;
87 1.1 jmcneill return 0;
88 1.1 jmcneill }
89 1.1 jmcneill
90 1.1 jmcneill if ((ep = virtdir_find(&fwcfg_virtdir, path, strlen(path))) == NULL)
91 1.1 jmcneill return -ENOENT;
92 1.1 jmcneill
93 1.1 jmcneill switch (ep->type) {
94 1.1 jmcneill case 'f':
95 1.1 jmcneill memcpy(st, &fwcfg_virtdir.file, sizeof(*st));
96 1.2 christos st->st_size = (off_t)ep->tgtlen;
97 1.1 jmcneill st->st_mode = S_IFREG | fwcfg_file_mask;
98 1.1 jmcneill break;
99 1.1 jmcneill case 'd':
100 1.1 jmcneill memcpy(st, &fwcfg_virtdir.dir, sizeof(*st));
101 1.1 jmcneill st->st_mode = S_IFDIR | fwcfg_dir_mask;
102 1.1 jmcneill break;
103 1.1 jmcneill }
104 1.2 christos st->st_ino = (ino_t)virtdir_offset(&fwcfg_virtdir, ep) + 10;
105 1.1 jmcneill
106 1.1 jmcneill return 0;
107 1.1 jmcneill }
108 1.1 jmcneill
109 1.1 jmcneill static int
110 1.1 jmcneill fwcfg_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
111 1.1 jmcneill off_t offset, struct fuse_file_info *fi)
112 1.1 jmcneill {
113 1.1 jmcneill static VIRTDIR *dirp;
114 1.1 jmcneill virt_dirent_t *dp;
115 1.1 jmcneill
116 1.1 jmcneill if (offset == 0) {
117 1.1 jmcneill if ((dirp = openvirtdir(&fwcfg_virtdir, path)) == NULL)
118 1.1 jmcneill return 0;
119 1.1 jmcneill filler(buf, ".", NULL, 0);
120 1.1 jmcneill filler(buf, "..", NULL, 0);
121 1.1 jmcneill }
122 1.1 jmcneill while ((dp = readvirtdir(dirp)) != NULL) {
123 1.1 jmcneill if (filler(buf, dp->d_name, NULL, 0) != 0)
124 1.1 jmcneill return 0;
125 1.1 jmcneill }
126 1.1 jmcneill closevirtdir(dirp);
127 1.1 jmcneill dirp = NULL;
128 1.1 jmcneill
129 1.1 jmcneill return 0;
130 1.1 jmcneill }
131 1.1 jmcneill
132 1.1 jmcneill static int
133 1.1 jmcneill fwcfg_open(const char *path, struct fuse_file_info *fi)
134 1.1 jmcneill {
135 1.1 jmcneill return 0;
136 1.1 jmcneill }
137 1.1 jmcneill
138 1.1 jmcneill static int
139 1.1 jmcneill fwcfg_read(const char *path, char *buf, size_t size, off_t offset,
140 1.1 jmcneill struct fuse_file_info *fi)
141 1.1 jmcneill {
142 1.1 jmcneill virt_dirent_t *ep;
143 1.1 jmcneill uint8_t tmp[64];
144 1.1 jmcneill
145 1.1 jmcneill if ((ep = virtdir_find(&fwcfg_virtdir, path, strlen(path))) == NULL)
146 1.1 jmcneill return -ENOENT;
147 1.1 jmcneill
148 1.1 jmcneill if (ep->select == 0)
149 1.1 jmcneill return -ENOENT;
150 1.1 jmcneill
151 1.1 jmcneill set_index(ep->select);
152 1.1 jmcneill
153 1.1 jmcneill /* Seek to correct offset */
154 1.1 jmcneill while (offset > 0) {
155 1.2 christos const size_t len = MIN(sizeof(tmp), (size_t)offset);
156 1.1 jmcneill read_data(tmp, len);
157 1.2 christos offset -= (off_t)len;
158 1.1 jmcneill }
159 1.1 jmcneill
160 1.1 jmcneill /* Read the data */
161 1.1 jmcneill read_data(buf, size);
162 1.1 jmcneill
163 1.2 christos return (int)size;
164 1.1 jmcneill }
165 1.1 jmcneill
166 1.1 jmcneill static int
167 1.1 jmcneill fwcfg_statfs(const char *path, struct statvfs *st)
168 1.1 jmcneill {
169 1.1 jmcneill uint32_t count;
170 1.1 jmcneill
171 1.1 jmcneill set_index(FW_CFG_FILE_DIR);
172 1.1 jmcneill read_data(&count, sizeof(count));
173 1.1 jmcneill
174 1.1 jmcneill memset(st, 0, sizeof(*st));
175 1.1 jmcneill st->f_files = be32toh(count);
176 1.1 jmcneill
177 1.1 jmcneill return 0;
178 1.1 jmcneill }
179 1.1 jmcneill
180 1.1 jmcneill static struct fuse_operations fwcfg_ops = {
181 1.1 jmcneill .getattr = fwcfg_getattr,
182 1.1 jmcneill .readdir = fwcfg_readdir,
183 1.1 jmcneill .open = fwcfg_open,
184 1.1 jmcneill .read = fwcfg_read,
185 1.1 jmcneill .statfs = fwcfg_statfs,
186 1.1 jmcneill };
187 1.1 jmcneill
188 1.1 jmcneill static void
189 1.1 jmcneill build_tree(virtdir_t *v)
190 1.1 jmcneill {
191 1.1 jmcneill char path[PATH_MAX];
192 1.1 jmcneill struct fwcfg_file f;
193 1.1 jmcneill uint32_t count, n;
194 1.1 jmcneill struct stat st;
195 1.1 jmcneill
196 1.1 jmcneill memset(&st, 0, sizeof(st));
197 1.1 jmcneill st.st_uid = fwcfg_uid;
198 1.1 jmcneill st.st_gid = fwcfg_gid;
199 1.1 jmcneill virtdir_init(v, NULL, &st, &st, &st);
200 1.1 jmcneill
201 1.1 jmcneill set_index(FW_CFG_FILE_DIR);
202 1.1 jmcneill read_data(&count, sizeof(count));
203 1.1 jmcneill for (n = 0; n < be32toh(count); n++) {
204 1.1 jmcneill read_data(&f, sizeof(f));
205 1.1 jmcneill snprintf(path, sizeof(path), "/%s", f.name);
206 1.1 jmcneill virtdir_add(v, path, strlen(path), 'f', NULL,
207 1.1 jmcneill be32toh(f.size), be16toh(f.select));
208 1.1 jmcneill }
209 1.1 jmcneill }
210 1.1 jmcneill
211 1.2 christos static __dead void
212 1.2 christos usage(void)
213 1.2 christos {
214 1.2 christos fprintf(stderr, "Usage: %s [-F <path>] [-g <gid>] [-m <file-mode>] "
215 1.2 christos "[-M <dir-mode>] [-u <uid>] [<fuse-options>]", getprogname());
216 1.2 christos exit(EXIT_FAILURE);
217 1.2 christos }
218 1.2 christos
219 1.1 jmcneill int
220 1.1 jmcneill main(int argc, char *argv[])
221 1.1 jmcneill {
222 1.1 jmcneill const char *path = _PATH_FWCFG;
223 1.2 christos int ch;
224 1.2 christos long m;
225 1.1 jmcneill char *ep;
226 1.1 jmcneill
227 1.1 jmcneill fwcfg_uid = geteuid();
228 1.1 jmcneill fwcfg_gid = getegid();
229 1.1 jmcneill
230 1.1 jmcneill while ((ch = getopt(argc, argv, "F:g:m:M:u:")) != -1) {
231 1.1 jmcneill switch (ch) {
232 1.1 jmcneill case 'F':
233 1.1 jmcneill path = optarg;
234 1.1 jmcneill break;
235 1.1 jmcneill case 'g':
236 1.2 christos fwcfg_gid = (gid_t)atoi(optarg);
237 1.1 jmcneill break;
238 1.1 jmcneill case 'm':
239 1.1 jmcneill m = strtol(optarg, &ep, 8);
240 1.1 jmcneill if (optarg == ep || *ep || m < 0)
241 1.2 christos errx(EXIT_FAILURE, "invalid file mode: %s",
242 1.2 christos optarg);
243 1.2 christos fwcfg_file_mask = (mode_t)m;
244 1.1 jmcneill break;
245 1.1 jmcneill case 'M':
246 1.1 jmcneill m = strtol(optarg, &ep, 8);
247 1.1 jmcneill if (optarg == ep || *ep || m < 0)
248 1.2 christos errx(EXIT_FAILURE, "invalid directory mode: %s",
249 1.2 christos optarg);
250 1.2 christos fwcfg_dir_mask = (mode_t)m;
251 1.1 jmcneill break;
252 1.1 jmcneill case 'u':
253 1.2 christos fwcfg_uid = (uid_t)atoi(optarg);
254 1.1 jmcneill break;
255 1.3 christos #ifdef notyet
256 1.2 christos default:
257 1.2 christos usage();
258 1.3 christos #endif
259 1.1 jmcneill }
260 1.1 jmcneill }
261 1.1 jmcneill
262 1.1 jmcneill fwcfg_fd = open(path, O_RDWR);
263 1.1 jmcneill if (fwcfg_fd == -1)
264 1.1 jmcneill err(EXIT_FAILURE, "failed to open %s", path);
265 1.1 jmcneill
266 1.1 jmcneill build_tree(&fwcfg_virtdir);
267 1.1 jmcneill
268 1.1 jmcneill return fuse_main(argc, argv, &fwcfg_ops, NULL);
269 1.1 jmcneill }
270