devopen.c revision 1.10 1 1.10 manu /* $NetBSD: devopen.c,v 1.10 2019/12/10 02:02:47 manu Exp $ */
2 1.4 junyoung
3 1.4 junyoung /*-
4 1.4 junyoung * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 1.4 junyoung * All rights reserved.
6 1.4 junyoung *
7 1.4 junyoung * This code is derived from software contributed to The NetBSD Foundation
8 1.4 junyoung * by Bang Jun-Young.
9 1.4 junyoung *
10 1.4 junyoung * Redistribution and use in source and binary forms, with or without
11 1.4 junyoung * modification, are permitted provided that the following conditions
12 1.4 junyoung * are met:
13 1.4 junyoung * 1. Redistributions of source code must retain the above copyright
14 1.4 junyoung * notice, this list of conditions and the following disclaimer.
15 1.4 junyoung * 2. Redistributions in binary form must reproduce the above copyright
16 1.4 junyoung * notice, this list of conditions and the following disclaimer in the
17 1.4 junyoung * documentation and/or other materials provided with the distribution.
18 1.4 junyoung *
19 1.4 junyoung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.4 junyoung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.4 junyoung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.4 junyoung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.4 junyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.4 junyoung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.4 junyoung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.4 junyoung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.4 junyoung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.4 junyoung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.4 junyoung * POSSIBILITY OF SUCH DAMAGE.
30 1.4 junyoung */
31 1.1 dsl
32 1.1 dsl /*
33 1.1 dsl * Copyright (c) 1996, 1997
34 1.1 dsl * Matthias Drochner. All rights reserved.
35 1.1 dsl *
36 1.1 dsl * Redistribution and use in source and binary forms, with or without
37 1.1 dsl * modification, are permitted provided that the following conditions
38 1.1 dsl * are met:
39 1.1 dsl * 1. Redistributions of source code must retain the above copyright
40 1.1 dsl * notice, this list of conditions and the following disclaimer.
41 1.1 dsl * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 dsl * notice, this list of conditions and the following disclaimer in the
43 1.1 dsl * documentation and/or other materials provided with the distribution.
44 1.1 dsl *
45 1.1 dsl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 1.1 dsl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 1.1 dsl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 1.1 dsl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 1.1 dsl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 1.1 dsl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 1.1 dsl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 1.1 dsl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 1.1 dsl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 1.1 dsl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 1.1 dsl */
56 1.1 dsl
57 1.1 dsl
58 1.1 dsl #include <sys/types.h>
59 1.1 dsl
60 1.1 dsl #include <lib/libsa/stand.h>
61 1.1 dsl #include <lib/libkern/libkern.h>
62 1.1 dsl
63 1.1 dsl #include <libi386.h>
64 1.1 dsl #include <biosdisk.h>
65 1.1 dsl #include "devopen.h"
66 1.1 dsl #ifdef _STANDALONE
67 1.1 dsl #include <bootinfo.h>
68 1.1 dsl #endif
69 1.1 dsl #ifdef SUPPORT_PS2
70 1.1 dsl #include <biosmca.h>
71 1.1 dsl #endif
72 1.1 dsl
73 1.4 junyoung static int dev2bios(char *, int, int *);
74 1.1 dsl
75 1.3 junyoung static int
76 1.4 junyoung dev2bios(char *devname, int unit, int *biosdev)
77 1.1 dsl {
78 1.4 junyoung
79 1.1 dsl if (strcmp(devname, "hd") == 0)
80 1.1 dsl *biosdev = 0x80 + unit;
81 1.1 dsl else if (strcmp(devname, "fd") == 0)
82 1.1 dsl *biosdev = 0x00 + unit;
83 1.4 junyoung else if (strcmp(devname, "cd") == 0)
84 1.4 junyoung *biosdev = boot_biosdev;
85 1.1 dsl else
86 1.3 junyoung return ENXIO;
87 1.1 dsl
88 1.3 junyoung return 0;
89 1.1 dsl }
90 1.1 dsl
91 1.4 junyoung void
92 1.9 manu bios2dev(int biosdev, daddr_t sector, char **devname, int *unit,
93 1.9 manu int *partition, const char **part_name)
94 1.1 dsl {
95 1.1 dsl
96 1.4 junyoung /* set default */
97 1.1 dsl *unit = biosdev & 0x7f;
98 1.1 dsl
99 1.4 junyoung if (biosdev & 0x80) {
100 1.4 junyoung /*
101 1.4 junyoung * There seems to be no standard way of numbering BIOS
102 1.4 junyoung * CD-ROM drives. The following method is a little tricky
103 1.4 junyoung * but works nicely.
104 1.4 junyoung */
105 1.4 junyoung if (biosdev >= 0x80 + get_harddrives()) {
106 1.4 junyoung *devname = "cd";
107 1.4 junyoung *unit = 0; /* override default */
108 1.4 junyoung } else
109 1.4 junyoung *devname = "hd";
110 1.4 junyoung } else
111 1.4 junyoung *devname = "fd";
112 1.4 junyoung
113 1.9 manu (void)biosdisk_findpartition(biosdev, sector, partition, part_name);
114 1.1 dsl }
115 1.1 dsl
116 1.1 dsl #ifdef _STANDALONE
117 1.1 dsl struct btinfo_bootpath bibp;
118 1.7 ad extern bool kernel_loaded;
119 1.1 dsl #endif
120 1.1 dsl
121 1.1 dsl /*
122 1.1 dsl * Open the BIOS disk device
123 1.1 dsl */
124 1.1 dsl int
125 1.3 junyoung devopen(struct open_file *f, const char *fname, char **file)
126 1.1 dsl {
127 1.3 junyoung char *fsname, *devname;
128 1.4 junyoung int unit, partition;
129 1.3 junyoung int biosdev;
130 1.3 junyoung int error;
131 1.1 dsl
132 1.9 manu error = parsebootfile(fname, &fsname, &devname,
133 1.9 manu &unit, &partition, (const char **) file);
134 1.9 manu if (error)
135 1.3 junyoung return error;
136 1.1 dsl
137 1.3 junyoung f->f_dev = &devsw[0]; /* must be biosdisk */
138 1.1 dsl
139 1.1 dsl #ifdef _STANDALONE
140 1.7 ad if (!kernel_loaded) {
141 1.7 ad strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
142 1.7 ad BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
143 1.7 ad }
144 1.1 dsl #endif
145 1.1 dsl
146 1.9 manu #ifndef NO_GPT
147 1.9 manu /* Search by GPT label name */
148 1.9 manu if (strstr(devname, "NAME=") == devname) {
149 1.9 manu f->f_dev = &devsw[0]; /* must be biosdisk */
150 1.9 manu
151 1.9 manu return biosdisk_open_name(f, devname);
152 1.9 manu }
153 1.9 manu #endif
154 1.9 manu #ifndef NO_RAIDFRAME
155 1.9 manu /* Search by raidframe name */
156 1.9 manu if (strstr(devname, "raid") == devname) {
157 1.9 manu f->f_dev = &devsw[0]; /* must be biosdisk */
158 1.9 manu
159 1.10 manu return biosdisk_open_name(f, fname);
160 1.9 manu }
161 1.9 manu #endif
162 1.9 manu
163 1.9 manu error = dev2bios(devname, unit, &biosdev);
164 1.9 manu if (error)
165 1.9 manu return error;
166 1.9 manu
167 1.4 junyoung return biosdisk_open(f, biosdev, partition);
168 1.1 dsl }
169