devopen.c revision 1.6 1 /* $NetBSD: devopen.c,v 1.6 2019/08/18 02:18:24 manu 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
61 #include <biosdisk.h>
62 #include "devopen.h"
63 #include <bootinfo.h>
64 #include "efidisk.h"
65
66 static int
67 dev2bios(char *devname, int unit, int *biosdev)
68 {
69
70 if (strcmp(devname, "hd") == 0)
71 *biosdev = 0x80 + unit;
72 else if (strcmp(devname, "cd") == 0)
73 *biosdev = 0x80 + get_harddrives() + unit;
74 else
75 return ENXIO;
76
77 return 0;
78 }
79
80 void
81 bios2dev(int biosdev, daddr_t sector, char **devname, int *unit,
82 int *partition, const char **part_name)
83 {
84 static char savedevname[MAXDEVNAME+1];
85
86 *unit = biosdev & 0x7f;
87
88 if (efi_bootdp_type == BOOT_DEVICE_TYPE_NET) {
89 *devname = "net";
90 *unit = efi_net_get_booted_interface_unit();
91 if (*unit < 0)
92 *unit = 0;
93 *partition = 0;
94 return;
95 } else if (biosdev >= 0x80 + get_harddrives()) {
96 *devname = "cd";
97 *unit -= get_harddrives();
98 } else
99 *devname = "hd";
100
101 (void)biosdisk_findpartition(biosdev, sector, partition, part_name);
102 if (*part_name != NULL) {
103 snprintf(savedevname, MAXDEVNAME, "NAME=%s", *part_name);
104 *devname = savedevname;
105 }
106 }
107
108 struct btinfo_bootpath bibp;
109 extern bool kernel_loaded;
110
111 /*
112 * Open the EFI disk device
113 */
114 int
115 devopen(struct open_file *f, const char *fname, char **file)
116 {
117 #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
118 static const char *net_devnames[] = {
119 #if defined(SUPPORT_NFS)
120 "nfs",
121 #endif
122 #if defined(SUPPORT_TFTP)
123 "tftp",
124 #endif
125 };
126 #endif
127 struct devdesc desc;
128 struct devsw *dev;
129 char *fsname, *devname;
130 int unit, partition;
131 int biosdev;
132 int i, n, error;
133
134 error = parsebootfile(fname, &fsname, &devname, &unit, &partition,
135 (const char **) file);
136 if (error)
137 return error;
138
139 /* Search by GPT label or raidframe name */
140 if ((strstr(devname, "NAME=") == devname) ||
141 (strstr(devname, "raid") == devname)) {
142 f->f_dev = &devsw[0]; /* must be biosdisk */
143
144 if (!kernel_loaded) {
145 strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
146 BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
147 }
148
149 error = biosdisk_open_name(f, devname);
150 return error;
151 }
152
153 memcpy(file_system, file_system_disk, sizeof(*file_system) * nfsys);
154 nfsys = nfsys_disk;
155
156 #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
157 for (i = 0; i < __arraycount(net_devnames); i++) {
158 if (strcmp(devname, net_devnames[i]) == 0) {
159 fsname = devname;
160 devname = "net";
161 break;
162 }
163 }
164 #endif
165
166 for (i = 1; i < ndevs; i++) {
167 dev = &devsw[i];
168 if (strcmp(devname, DEV_NAME(dev)) == 0) {
169 if (strcmp(devname, "net") == 0) {
170 n = 0;
171 #if defined(SUPPORT_NFS)
172 if (strcmp(fsname, "nfs") == 0) {
173 memcpy(&file_system[n++], &file_system_nfs,
174 sizeof(file_system_nfs));
175 } else
176 #endif
177 #if defined(SUPPORT_TFTP)
178 if (strcmp(fsname, "tftp") == 0) {
179 memcpy(&file_system[n++], &file_system_tftp,
180 sizeof(file_system_tftp));
181 } else
182 #endif
183 {
184 #if defined(SUPPORT_NFS)
185 memcpy(&file_system[n++], &file_system_nfs,
186 sizeof(file_system_nfs));
187 #endif
188 #if defined(SUPPORT_TFTP)
189 memcpy(&file_system[n++], &file_system_tftp,
190 sizeof(file_system_tftp));
191 #endif
192 }
193 nfsys = n;
194
195 try_bootp = 1;
196 }
197
198 memset(&desc, 0, sizeof(desc));
199 strlcpy(desc.d_name, devname, sizeof(desc.d_name));
200 desc.d_unit = unit;
201
202 f->f_dev = dev;
203 if (!kernel_loaded) {
204 strncpy(bibp.bootpath, *file,
205 sizeof(bibp.bootpath));
206 BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
207 }
208 return DEV_OPEN(f->f_dev)(f, &desc);
209 }
210 }
211
212 /*
213 * biosdisk
214 */
215 if (strcmp(devname, "esp") == 0) {
216 bios2dev(boot_biosdev, boot_biossector, &devname, &unit,
217 &partition, NULL);
218 if (efidisk_get_efi_system_partition(boot_biosdev, &partition))
219 return ENXIO;
220 }
221
222 error = dev2bios(devname, unit, &biosdev);
223 if (error)
224 return error;
225
226 f->f_dev = &devsw[0]; /* must be biosdisk */
227
228 if (!kernel_loaded) {
229 strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
230 BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
231 }
232
233 return biosdisk_open(f, biosdev, partition);
234 }
235