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