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