xref: /netbsd-src/sys/arch/i386/stand/efiboot/devopen.c (revision 3827b167b3365a652d4e133c0fee4b88d5527c03)
1 /*	$NetBSD: devopen.c,v 1.14 2023/06/20 07:46:03 rin Exp $	 */
2 
3 /*-
4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Bang Jun-Young.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1996, 1997
34  *	Matthias Drochner.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55  */
56 
57 #include "efiboot.h"
58 
59 #include <lib/libsa/dev_net.h>
60 #include <lib/libsa/net.h>
61 
62 #include <biosdisk.h>
63 #include "devopen.h"
64 #include <bootinfo.h>
65 #include "efidisk.h"
66 
67 static int
dev2bios(char * devname,int unit,int * biosdev)68 dev2bios(char *devname, int unit, int *biosdev)
69 {
70 
71 	if (strcmp(devname, "hd") == 0)
72 		*biosdev = 0x80 + unit;
73 	else if (strcmp(devname, "cd") == 0)
74 		*biosdev = 0x80 + get_harddrives() + unit;
75 	else
76 		return ENXIO;
77 
78 	return 0;
79 }
80 
81 void
bios2dev(int biosdev,daddr_t sector,char ** devname,int * unit,int * partition,const char ** part_name)82 bios2dev(int biosdev, daddr_t sector, char **devname, int *unit,
83 	 int *partition, const char **part_name)
84 {
85 	static char savedevname[MAXDEVNAME+1];
86 
87 	*unit = biosdev & 0x7f;
88 
89 	if (efi_bootdp_type == BOOT_DEVICE_TYPE_NET) {
90 		*devname = "net";
91 		*unit = efi_net_get_booted_interface_unit();
92 		if (*unit < 0)
93 			*unit = 0;
94 		*partition = 0;
95 		return;
96 	} else if (biosdev >= 0x80 + get_harddrives()) {
97 		*devname = "cd";
98 		*unit -= get_harddrives();
99 	} else
100 		*devname = "hd";
101 
102 	(void)biosdisk_findpartition(biosdev, sector, partition, part_name);
103 	if (part_name != NULL && *part_name != NULL) {
104 		snprintf(savedevname, sizeof(savedevname),
105 		    "NAME=%s", *part_name);
106 		*devname = savedevname;
107 	}
108 }
109 
110 #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
111 const struct netboot_fstab *
netboot_fstab_find(const char * name)112 netboot_fstab_find(const char *name)
113 {
114 	int i;
115 
116 	if (strcmp(name, "net") == 0)
117 		return &netboot_fstab[0];
118 
119 	for (i = 0; i < nnetboot_fstab; i++) {
120 		if (strcmp(name, netboot_fstab[i].name) == 0)
121 			return &netboot_fstab[i];
122 	}
123 
124 	return NULL;
125 }
126 
127 static const struct netboot_fstab *
netboot_fstab_findn(const char * name,size_t len)128 netboot_fstab_findn(const char *name, size_t len)
129 {
130 	int i;
131 
132 	if (strncmp(name, "net", len) == 0)
133 		return &netboot_fstab[0];
134 
135 	for (i = 0; i < nnetboot_fstab; i++) {
136 		if (strncmp(name, netboot_fstab[i].name, len) == 0)
137 			return &netboot_fstab[i];
138 	}
139 
140 	return NULL;
141 }
142 #endif
143 
144 struct btinfo_bootpath bibp;
145 extern bool kernel_loaded;
146 
147 /*
148  * Open the EFI disk device
149  */
150 int
devopen(struct open_file * f,const char * fname,char ** file)151 devopen(struct open_file *f, const char *fname, char **file)
152 {
153 	char *fsname, *devname;
154 	const char *xname = NULL;
155 	int unit, partition;
156 	int biosdev;
157 	int error;
158 #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
159 	struct devdesc desc;
160 	const struct netboot_fstab *nf;
161 	char *filename;
162 	size_t fsnamelen;
163 	int i, n;
164 #endif
165 
166 	error = parsebootfile(fname, &fsname, &devname, &unit, &partition,
167 	    (const char **) file);
168 	if (error)
169 		return error;
170 
171 	memcpy(file_system, file_system_disk,
172 	    sizeof(struct fs_ops) * nfsys_disk);
173 	nfsys = nfsys_disk;
174 
175 	/* Search by GPT label or raidframe name */
176 	if (strstr(devname, "NAME=") == devname)
177 		xname = devname;
178 	if (strstr(devname, "raid") == devname)
179 		xname = fname;
180 
181 	if (xname != NULL) {
182 		f->f_dev = &devsw[0];		/* must be biosdisk */
183 
184 		if (!kernel_loaded) {
185 			strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
186 			BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
187 		}
188 
189 		error = biosdisk_open_name(f, xname);
190 		return error;
191 	}
192 
193 	/*
194 	 * Network
195 	 */
196 #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
197 	nf = netboot_fstab_find(devname);
198 	if (nf != NULL) {
199 		n = 0;
200 		if (strcmp(devname, "net") == 0) {
201 			for (i = 0; i < nnetboot_fstab; i++) {
202 				memcpy(&file_system[n++], netboot_fstab[i].ops,
203 				    sizeof(struct fs_ops));
204 			}
205 		} else {
206 			memcpy(&file_system[n++], nf->ops,
207 			    sizeof(struct fs_ops));
208 		}
209 		nfsys = n;
210 
211 #ifdef SUPPORT_BOOTP
212 		try_bootp = 1;
213 #endif
214 
215 		/* If we got passed a filename, pass it to the BOOTP server. */
216 		if (fname) {
217 			filename = strchr(fname, ':');
218 			if (filename != NULL)
219 				filename++;
220 			else
221 				filename = (char *)fname;
222 			strlcpy(bootfile, filename, sizeof(bootfile));
223 		}
224 
225 		memset(&desc, 0, sizeof(desc));
226 		strlcpy(desc.d_name, "net", sizeof(desc.d_name));
227 		desc.d_unit = unit;
228 
229 		f->f_dev = &devsw[1];		/* must be net */
230 		if (!kernel_loaded) {
231 			strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
232 			BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
233 		}
234 		error = DEV_OPEN(f->f_dev)(f, &desc);
235 		if (error)
236 			return error;
237 
238 		/*
239 		 * If the DHCP server provided a file name:
240 		 * - If it contains a ":", assume it points to a NetBSD kernel.
241 		 * - If not, assume that the DHCP server was not able to pass
242 		 *   a separate filename for the kernel. (The name probably was
243 		 *   the same as used to load "efiboot".) Ignore it and use
244 		 *   the default in this case.
245 		 * So we cater to simple DHCP servers while being able to use
246 		 * the power of conditional behaviour in modern ones.
247 		 */
248 		filename = strchr(bootfile, ':');
249 		if (filename != NULL) {
250 			fname = bootfile;
251 
252 			fsnamelen = filename - fname;
253 			nf = netboot_fstab_findn(fname, fsnamelen);
254 			if (nf == NULL ||
255 			    strncmp(fname, "net", fsnamelen) == 0) {
256 				printf("Invalid file system type specified in "
257 				    "%s\n", fname);
258 				error = EINVAL;
259 				goto neterr;
260 			}
261 
262 			memcpy(file_system, nf->ops, sizeof(struct fs_ops));
263 			nfsys = 1;
264 		}
265 
266 		filename = fname ? strchr(fname, ':') : NULL;
267 		if (filename != NULL) {
268 			filename++;
269 			if (*filename == '\0') {
270 				printf("No file specified in %s\n", fname);
271 				error = EINVAL;
272 				goto neterr;
273 			}
274 		} else
275 			filename = (char *)fname;
276 
277 		*file = filename;
278 		return 0;
279 
280 neterr:
281 		DEV_CLOSE(f->f_dev)(f);
282 		f->f_dev = NULL;
283 		return error;
284 	}
285 #endif
286 
287 	/*
288 	 * biosdisk
289 	 */
290 	if (strcmp(devname, "esp") == 0) {
291 		bios2dev(boot_biosdev, boot_biossector, &devname, &unit,
292 		    &partition, NULL);
293 		if (efidisk_get_efi_system_partition(boot_biosdev, &partition))
294 			return ENXIO;
295 	}
296 
297 	error = dev2bios(devname, unit, &biosdev);
298 	if (error)
299 		return error;
300 
301 	f->f_dev = &devsw[0];		/* must be biosdisk */
302 
303 	if (!kernel_loaded) {
304 		strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
305 		BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
306 	}
307 
308 	return biosdisk_open(f, biosdev, partition);
309 }
310