devopen.c revision 1.1 1 1.1 uwe /* $NetBSD: devopen.c,v 1.1 2006/09/01 21:26:18 uwe Exp $ */
2 1.1 uwe
3 1.1 uwe /*-
4 1.1 uwe * Copyright (c) 1993 John Brezak
5 1.1 uwe * All rights reserved.
6 1.1 uwe *
7 1.1 uwe * Redistribution and use in source and binary forms, with or without
8 1.1 uwe * modification, are permitted provided that the following conditions
9 1.1 uwe * are met:
10 1.1 uwe * 1. Redistributions of source code must retain the above copyright
11 1.1 uwe * notice, this list of conditions and the following disclaimer.
12 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uwe * notice, this list of conditions and the following disclaimer in the
14 1.1 uwe * documentation and/or other materials provided with the distribution.
15 1.1 uwe * 3. The name of the author may not be used to endorse or promote products
16 1.1 uwe * derived from this software without specific prior written permission.
17 1.1 uwe *
18 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 1.1 uwe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 uwe * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 uwe * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 1.1 uwe * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 1.1 uwe * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 uwe * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 uwe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 1.1 uwe * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 1.1 uwe * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 uwe * POSSIBILITY OF SUCH DAMAGE.
29 1.1 uwe */
30 1.1 uwe
31 1.1 uwe #include <lib/libsa/stand.h>
32 1.1 uwe #include <lib/libkern/libkern.h>
33 1.1 uwe
34 1.1 uwe #include "biosdisk.h"
35 1.1 uwe
36 1.1 uwe #include "boot.h"
37 1.1 uwe #include "bootinfo.h"
38 1.1 uwe
39 1.1 uwe static int dev2bios(char *devname, unsigned int unit, int *biosdev);
40 1.1 uwe static int devlookup(char *d);
41 1.1 uwe
42 1.1 uwe static int
43 1.1 uwe dev2bios(char *devname, unsigned int unit, int *biosdev)
44 1.1 uwe {
45 1.1 uwe
46 1.1 uwe if (strcmp(devname, "hd") == 0) {
47 1.1 uwe if (unit == 0 || unit == 1) {
48 1.1 uwe *biosdev = 0x40 + (unit << 4);
49 1.1 uwe return (0);
50 1.1 uwe }
51 1.1 uwe }
52 1.1 uwe return (ENXIO);
53 1.1 uwe }
54 1.1 uwe
55 1.1 uwe int
56 1.1 uwe bios2dev(int biosdev, char **devname, u_int *unit, u_int sector, u_int *ptnp)
57 1.1 uwe {
58 1.1 uwe
59 1.1 uwe *devname = "hd";
60 1.1 uwe *unit = (biosdev >> 4) & 1;
61 1.1 uwe *ptnp = biosdisk_findptn(biosdev, sector);
62 1.1 uwe return (0);
63 1.1 uwe }
64 1.1 uwe
65 1.1 uwe static int
66 1.1 uwe devlookup(char *d)
67 1.1 uwe {
68 1.1 uwe struct devsw *dp = devsw;
69 1.1 uwe int i;
70 1.1 uwe
71 1.1 uwe for (i = 0; i < ndevs; i++, dp++) {
72 1.1 uwe if ((dp->dv_name != NULL) && (strcmp(dp->dv_name, d) == 0)) {
73 1.1 uwe return (i);
74 1.1 uwe }
75 1.1 uwe }
76 1.1 uwe
77 1.1 uwe printf("No such device - Configured devices are:\n");
78 1.1 uwe for (dp = devsw, i = 0; i < ndevs; i++, dp++) {
79 1.1 uwe if (dp->dv_name != NULL) {
80 1.1 uwe printf(" %s", dp->dv_name);
81 1.1 uwe }
82 1.1 uwe }
83 1.1 uwe printf("\n");
84 1.1 uwe return (-1);
85 1.1 uwe }
86 1.1 uwe
87 1.1 uwe int
88 1.1 uwe devopen(struct open_file *f, const char *fname, char **file)
89 1.1 uwe {
90 1.1 uwe static struct btinfo_bootpath bibp;
91 1.1 uwe struct devsw *dp;
92 1.1 uwe char *devname;
93 1.1 uwe unsigned int dev, ctlr, unit, partition;
94 1.1 uwe int biosdev;
95 1.1 uwe int error;
96 1.1 uwe
97 1.1 uwe #if defined(DEBUG)
98 1.1 uwe printf("devopen: fname = %s\n", fname);
99 1.1 uwe #endif
100 1.1 uwe
101 1.1 uwe ctlr = 0;
102 1.1 uwe if ((error = parsebootfile(fname, &devname, &unit, &partition,
103 1.1 uwe (const char **)file)) != 0) {
104 1.1 uwe return (error);
105 1.1 uwe }
106 1.1 uwe
107 1.1 uwe #if defined(DEBUG)
108 1.1 uwe printf("devopen: devname = %s\n", devname);
109 1.1 uwe #endif
110 1.1 uwe dev = devlookup(devname);
111 1.1 uwe if (dev == -1) {
112 1.1 uwe #if defined(DEBUG)
113 1.1 uwe printf("devopen: devlookup failed\n");
114 1.1 uwe #endif
115 1.1 uwe return (ENXIO);
116 1.1 uwe }
117 1.1 uwe
118 1.1 uwe dp = &devsw[dev];
119 1.1 uwe if (dp->dv_open == NULL) {
120 1.1 uwe #if defined(DEBUG)
121 1.1 uwe printf("devopen: dev->dv_open() == NULL\n");
122 1.1 uwe #endif
123 1.1 uwe return (ENODEV);
124 1.1 uwe }
125 1.1 uwe f->f_dev = dp;
126 1.1 uwe
127 1.1 uwe strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
128 1.1 uwe BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
129 1.1 uwe
130 1.1 uwe if (dev2bios(devname, unit, &biosdev) == 0) {
131 1.1 uwe #if defined(DEBUG)
132 1.1 uwe printf("devopen: bios disk\n");
133 1.1 uwe #endif
134 1.1 uwe return (biosdisk_open(f, biosdev, partition));
135 1.1 uwe }
136 1.1 uwe
137 1.1 uwe #if defined(DEBUG)
138 1.1 uwe printf("devopen: dev->dv_open()\n");
139 1.1 uwe #endif
140 1.1 uwe if ((error = (*dp->dv_open)(f, ctlr, unit, partition)) == 0) {
141 1.1 uwe #if defined(DEBUG)
142 1.1 uwe printf("devopen: dev->dv_open() opened\n");
143 1.1 uwe #endif
144 1.1 uwe return (0);
145 1.1 uwe }
146 1.1 uwe
147 1.1 uwe printf("%s%d%c:%s : %s\n", dp->dv_name, unit, partition + 'a', *file,
148 1.1 uwe strerror(error));
149 1.1 uwe return (error);
150 1.1 uwe }
151