devopen.c revision 1.2 1 1.2 drochner /* $NetBSD: devopen.c,v 1.2 2004/03/24 16:54:18 drochner Exp $ */
2 1.1 dsl
3 1.1 dsl /*
4 1.1 dsl * Copyright (c) 1996, 1997
5 1.1 dsl * Matthias Drochner. All rights reserved.
6 1.1 dsl *
7 1.1 dsl * Redistribution and use in source and binary forms, with or without
8 1.1 dsl * modification, are permitted provided that the following conditions
9 1.1 dsl * are met:
10 1.1 dsl * 1. Redistributions of source code must retain the above copyright
11 1.1 dsl * notice, this list of conditions and the following disclaimer.
12 1.1 dsl * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 dsl * notice, this list of conditions and the following disclaimer in the
14 1.1 dsl * documentation and/or other materials provided with the distribution.
15 1.1 dsl *
16 1.1 dsl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 dsl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 dsl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 dsl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 dsl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 dsl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 dsl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 dsl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 dsl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 dsl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 dsl */
27 1.1 dsl
28 1.1 dsl
29 1.1 dsl #include <sys/types.h>
30 1.1 dsl
31 1.1 dsl #include <lib/libsa/stand.h>
32 1.1 dsl #include <lib/libkern/libkern.h>
33 1.1 dsl
34 1.1 dsl #include <libi386.h>
35 1.1 dsl #include <biosdisk.h>
36 1.1 dsl #include "devopen.h"
37 1.1 dsl #ifdef _STANDALONE
38 1.1 dsl #include <bootinfo.h>
39 1.1 dsl #endif
40 1.1 dsl #ifdef SUPPORT_PS2
41 1.1 dsl #include <biosmca.h>
42 1.1 dsl #endif
43 1.1 dsl
44 1.1 dsl static __inline int dev2bios __P((char *, unsigned int, int *));
45 1.1 dsl
46 1.1 dsl static __inline int
47 1.1 dsl dev2bios(devname, unit, biosdev)
48 1.1 dsl char *devname;
49 1.1 dsl unsigned int unit;
50 1.1 dsl int *biosdev;
51 1.1 dsl {
52 1.1 dsl if (strcmp(devname, "hd") == 0)
53 1.1 dsl *biosdev = 0x80 + unit;
54 1.1 dsl else if (strcmp(devname, "fd") == 0)
55 1.1 dsl *biosdev = 0x00 + unit;
56 1.1 dsl else
57 1.1 dsl return (ENXIO);
58 1.1 dsl
59 1.1 dsl return (0);
60 1.1 dsl }
61 1.1 dsl
62 1.1 dsl int
63 1.1 dsl bios2dev(int biosdev, char **devname, u_int *unit, u_int sector, u_int *ptnp)
64 1.1 dsl {
65 1.1 dsl if (biosdev & 0x80)
66 1.1 dsl *devname = "hd";
67 1.1 dsl else
68 1.1 dsl *devname = "fd";
69 1.1 dsl
70 1.1 dsl *unit = biosdev & 0x7f;
71 1.1 dsl *ptnp = biosdiskfindptn(biosdev, sector);
72 1.1 dsl
73 1.1 dsl return 0;
74 1.1 dsl }
75 1.1 dsl
76 1.1 dsl #ifdef _STANDALONE
77 1.1 dsl struct btinfo_bootpath bibp;
78 1.1 dsl #endif
79 1.1 dsl
80 1.1 dsl /*
81 1.1 dsl * Open the BIOS disk device
82 1.1 dsl */
83 1.1 dsl int
84 1.1 dsl devopen(f, fname, file)
85 1.1 dsl struct open_file *f;
86 1.1 dsl const char *fname;
87 1.1 dsl char **file;
88 1.1 dsl {
89 1.1 dsl char *fsname, *devname;
90 1.1 dsl unsigned int unit, partition;
91 1.1 dsl int biosdev;
92 1.1 dsl int error;
93 1.1 dsl struct devsw *dp;
94 1.1 dsl
95 1.1 dsl if ((error = parsebootfile(fname, &fsname, &devname,
96 1.1 dsl &unit, &partition, (const char **) file))
97 1.1 dsl || (error = dev2bios(devname, unit, &biosdev)))
98 1.1 dsl return (error);
99 1.1 dsl
100 1.1 dsl dp = &devsw[0]; /* must be biosdisk */
101 1.1 dsl f->f_dev = dp;
102 1.1 dsl
103 1.1 dsl #ifdef _STANDALONE
104 1.1 dsl strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
105 1.1 dsl BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
106 1.1 dsl #endif
107 1.1 dsl
108 1.1 dsl return (biosdiskopen(f, biosdev, partition));
109 1.1 dsl }
110