xref: /netbsd-src/sys/stand/efiboot/efifile.c (revision 3fc11eecb7a487925583126e01a8180572a7e884)
1 /* $NetBSD: efifile.c,v 1.5 2020/07/15 00:51:40 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
efi_file_parse(const char * fname,UINTN * pvol,const char ** pfile)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
efi_file_system_probe(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
efi_file_open(struct open_file * f,...)68 efi_file_open(struct open_file *f, ...)
69 {
70 	EFI_DEVICE_PATH *file_dp, *dp;
71 	SIMPLE_READ_FILE *srf;
72 	EFI_HANDLE device;
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 	upath = NULL;
90 	rv = utf8_to_ucs2(path, &upath, &len);
91 	if (rv != 0)
92 		return rv;
93 
94 	file_dp = FileDevicePath(efi_vol[vol], upath);
95 	FreePool(upath);
96 	if (file_dp == NULL)
97 		return EINVAL;
98 
99 	srf = AllocatePool(sizeof(*srf));
100 	if (srf == NULL)
101 		return ENOMEM;
102 
103 	dp = file_dp;
104 	status = OpenSimpleReadFile(FALSE, NULL, 0, &dp, &device, srf);
105 	FreePool(file_dp);
106 	if (EFI_ERROR(status)) {
107 		FreePool(srf);
108 		return status == EFI_NOT_FOUND ? ENOENT : EIO;
109 	}
110 
111 	for (n = 0; n < ndevs; n++)
112 		if (strcmp(DEV_NAME(&devsw[n]), "efifile") == 0) {
113 			f->f_dev = &devsw[n];
114 			break;
115 		}
116 	if (n == ndevs) {
117 		FreePool(srf);
118 		return ENXIO;
119 	}
120 	f->f_devdata = f;
121 	f->f_fsdata = srf;
122 	f->f_flags = F_NODEV | F_READ;
123 
124 	return 0;
125 }
126 
127 int
efi_file_close(struct open_file * f)128 efi_file_close(struct open_file *f)
129 {
130 	SIMPLE_READ_FILE *srf = f->f_fsdata;
131 
132 	CloseSimpleReadFile(*srf);
133 	FreePool(srf);
134 
135 	return 0;
136 }
137 
138 int
efi_file_strategy(void * devdata,int rw,daddr_t dblk,size_t size,void * buf,size_t * rsize)139 efi_file_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
140 {
141 	struct open_file *f = devdata;
142 	SIMPLE_READ_FILE *srf = f->f_fsdata;
143 	EFI_STATUS status;
144 	UINTN len;
145 
146 	if (rw != F_READ)
147 		return EROFS;
148 
149 	len = size;
150 	status = ReadSimpleReadFile(*srf, f->f_offset, &len, buf);
151 	if (EFI_ERROR(status))
152 		return EIO;
153 	*rsize = len;
154 
155 	return 0;
156 }
157 
158 int
efi_file_path(EFI_DEVICE_PATH * dp,const char * fname,char * buf,size_t buflen)159 efi_file_path(EFI_DEVICE_PATH *dp, const char *fname, char *buf, size_t buflen)
160 {
161 	UINTN vol;
162 	int depth;
163 
164 	depth = efi_device_path_count(dp);
165 	if (depth == 0)
166 		return ENODEV;
167 
168 	for (vol = 0; vol < efi_nvol; vol++) {
169 		if (efi_device_path_ncmp(dp, DevicePathFromHandle(efi_vol[vol]), depth) == 0)
170 			break;
171 	}
172 	if (vol == efi_nvol)
173 		return ENOENT;
174 
175 	snprintf(buf, buflen, "fs%u:%s", (u_int)vol, fname);
176 	return 0;
177 }
178