devopen.c revision 1.8 1 /* $NetBSD: devopen.c,v 1.8 2019/09/26 12:21:03 nonaka 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
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
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) {
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 *
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 *
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
151 devopen(struct open_file *f, const char *fname, char **file)
152 {
153 char *fsname, *devname;
154 int unit, partition;
155 int biosdev;
156 int i, error;
157 #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
158 struct devdesc desc;
159 const struct netboot_fstab *nf;
160 char *filename;
161 size_t fsnamelen;
162 int n;
163 #endif
164
165 error = parsebootfile(fname, &fsname, &devname, &unit, &partition,
166 (const char **) file);
167 if (error)
168 return error;
169
170 memcpy(file_system, file_system_disk,
171 sizeof(struct fs_ops) * nfsys_disk);
172 nfsys = nfsys_disk;
173
174 /* Search by GPT label or raidframe name */
175 if ((strstr(devname, "NAME=") == devname) ||
176 (strstr(devname, "raid") == devname)) {
177 f->f_dev = &devsw[0]; /* must be biosdisk */
178
179 if (!kernel_loaded) {
180 strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
181 BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
182 }
183
184 error = biosdisk_open_name(f, devname);
185 return error;
186 }
187
188 /*
189 * Network
190 */
191 #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
192 nf = netboot_fstab_find(devname);
193 if (nf != NULL) {
194 n = 0;
195 if (strcmp(devname, "net") == 0) {
196 for (i = 0; i < nnetboot_fstab; i++) {
197 memcpy(&file_system[n++], netboot_fstab[i].ops,
198 sizeof(struct fs_ops));
199 }
200 } else {
201 memcpy(&file_system[n++], nf->ops,
202 sizeof(struct fs_ops));
203 }
204 nfsys = n;
205
206 #ifdef SUPPORT_BOOTP
207 try_bootp = 1;
208 #endif
209
210 /* If we got passed a filename, pass it to the BOOTP server. */
211 if (fname) {
212 filename = strchr(fname, ':');
213 if (filename != NULL)
214 filename++;
215 else
216 filename = (char *)fname;
217 strlcpy(bootfile, filename, sizeof(bootfile));
218 }
219
220 memset(&desc, 0, sizeof(desc));
221 strlcpy(desc.d_name, "net", sizeof(desc.d_name));
222 desc.d_unit = unit;
223
224 f->f_dev = &devsw[1]; /* must be net */
225 if (!kernel_loaded) {
226 strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
227 BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
228 }
229 error = DEV_OPEN(f->f_dev)(f, &desc);
230 if (error)
231 return error;
232
233 /*
234 * If the DHCP server provided a file name:
235 * - If it contains a ":", assume it points to a NetBSD kernel.
236 * - If not, assume that the DHCP server was not able to pass
237 * a separate filename for the kernel. (The name probably was
238 * the same as used to load "efiboot".) Ignore it and use
239 * the default in this case.
240 * So we cater to simple DHCP servers while being able to use
241 * the power of conditional behaviour in modern ones.
242 */
243 filename = strchr(bootfile, ':');
244 if (filename != NULL) {
245 fname = bootfile;
246
247 fsnamelen = filename - fname;
248 nf = netboot_fstab_findn(fname, fsnamelen);
249 if (nf == NULL ||
250 strncmp(fname, "net", fsnamelen) == 0) {
251 printf("Invalid file system type specified in "
252 "%s\n", fname);
253 error = EINVAL;
254 goto neterr;
255 }
256
257 memcpy(file_system, nf->ops, sizeof(struct fs_ops));
258 nfsys = 1;
259 }
260
261 filename = fname ? strchr(fname, ':') : NULL;
262 if (filename != NULL) {
263 filename++;
264 if (*filename == '\0') {
265 printf("No file specified in %s\n", fname);
266 error = EINVAL;
267 goto neterr;
268 }
269 } else
270 filename = (char *)fname;
271
272 *file = filename;
273 return 0;
274
275 neterr:
276 DEV_CLOSE(f->f_dev)(f);
277 f->f_dev = NULL;
278 return error;
279 }
280 #endif
281
282 /*
283 * biosdisk
284 */
285 if (strcmp(devname, "esp") == 0) {
286 bios2dev(boot_biosdev, boot_biossector, &devname, &unit,
287 &partition, NULL);
288 if (efidisk_get_efi_system_partition(boot_biosdev, &partition))
289 return ENXIO;
290 }
291
292 error = dev2bios(devname, unit, &biosdev);
293 if (error)
294 return error;
295
296 f->f_dev = &devsw[0]; /* must be biosdisk */
297
298 if (!kernel_loaded) {
299 strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
300 BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
301 }
302
303 return biosdisk_open(f, biosdev, partition);
304 }
305