1 /* $NetBSD: efifile.c,v 1.3 2018/08/26 21:28:18 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 31 #include "efiboot.h" 32 #include "efifile.h" 33 34 static EFI_HANDLE *efi_vol; 35 static UINTN efi_nvol; 36 37 static int 38 efi_file_parse(const char *fname, UINTN *pvol, const char **pfile) 39 { 40 intmax_t vol; 41 char *ep = NULL; 42 43 if (strchr(fname, ':') != NULL) { 44 if (strncasecmp(fname, "fs", 2) != 0) 45 return EINVAL; 46 vol = strtoimax(fname + 2, &ep, 10); 47 if (vol < 0 || vol >= efi_nvol || *ep != ':') 48 return ENXIO; 49 *pvol = vol; 50 *pfile = ep + 1; 51 return 0; 52 } 53 54 return EINVAL; 55 } 56 57 void 58 efi_file_system_probe(void) 59 { 60 EFI_STATUS status; 61 62 status = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &efi_nvol, &efi_vol); 63 if (EFI_ERROR(status)) 64 return; 65 } 66 67 int 68 efi_file_open(struct open_file *f, ...) 69 { 70 EFI_DEVICE_PATH *dp; 71 SIMPLE_READ_FILE srf; 72 EFI_HANDLE device, file; 73 EFI_STATUS status; 74 UINTN vol; 75 const char *fname, *path; 76 CHAR16 *upath; 77 va_list ap; 78 size_t len; 79 int rv, n; 80 81 va_start(ap, f); 82 fname = va_arg(ap, const char *); 83 va_end(ap); 84 85 rv = efi_file_parse(fname, &vol, &path); 86 if (rv != 0) 87 return rv; 88 89 device = efi_vol[vol]; 90 91 upath = NULL; 92 rv = utf8_to_ucs2(path, &upath, &len); 93 if (rv != 0) 94 return rv; 95 96 dp = FileDevicePath(device, upath); 97 FreePool(upath); 98 if (dp == NULL) 99 return EINVAL; 100 101 status = OpenSimpleReadFile(TRUE, NULL, 0, &dp, &file, &srf); 102 FreePool(dp); 103 if (EFI_ERROR(status)) 104 return status == EFI_NOT_FOUND ? ENOENT : EIO; 105 106 for (n = 0; n < ndevs; n++) 107 if (strcmp(DEV_NAME(&devsw[n]), "efifile") == 0) { 108 f->f_dev = &devsw[n]; 109 break; 110 } 111 if (n == ndevs) 112 return ENXIO; 113 f->f_devdata = f; 114 f->f_fsdata = srf; 115 f->f_flags = F_NODEV | F_READ; 116 117 return 0; 118 } 119 120 int 121 efi_file_close(struct open_file *f) 122 { 123 SIMPLE_READ_FILE srf = f->f_fsdata; 124 125 CloseSimpleReadFile(srf); 126 127 return 0; 128 } 129 130 int 131 efi_file_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize) 132 { 133 struct open_file *f = devdata; 134 SIMPLE_READ_FILE srf = f->f_fsdata; 135 EFI_STATUS status; 136 UINTN len; 137 138 if (rw != F_READ) 139 return EROFS; 140 141 len = size; 142 status = ReadSimpleReadFile(srf, f->f_offset, &len, buf); 143 if (EFI_ERROR(status)) 144 return EIO; 145 *rsize = len; 146 147 return 0; 148 } 149