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