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