1*58a2b000SEvgeniy Ivanov /* $NetBSD: biosdisk_user.c,v 1.7 2008/12/14 18:46:33 christos Exp $ */
2*58a2b000SEvgeniy Ivanov
3*58a2b000SEvgeniy Ivanov /*
4*58a2b000SEvgeniy Ivanov * Copyright (c) 1998
5*58a2b000SEvgeniy Ivanov * Matthias Drochner. All rights reserved.
6*58a2b000SEvgeniy Ivanov *
7*58a2b000SEvgeniy Ivanov * Redistribution and use in source and binary forms, with or without
8*58a2b000SEvgeniy Ivanov * modification, are permitted provided that the following conditions
9*58a2b000SEvgeniy Ivanov * are met:
10*58a2b000SEvgeniy Ivanov * 1. Redistributions of source code must retain the above copyright
11*58a2b000SEvgeniy Ivanov * notice, this list of conditions and the following disclaimer.
12*58a2b000SEvgeniy Ivanov * 2. Redistributions in binary form must reproduce the above copyright
13*58a2b000SEvgeniy Ivanov * notice, this list of conditions and the following disclaimer in the
14*58a2b000SEvgeniy Ivanov * documentation and/or other materials provided with the distribution.
15*58a2b000SEvgeniy Ivanov *
16*58a2b000SEvgeniy Ivanov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*58a2b000SEvgeniy Ivanov * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*58a2b000SEvgeniy Ivanov * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*58a2b000SEvgeniy Ivanov * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*58a2b000SEvgeniy Ivanov * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*58a2b000SEvgeniy Ivanov * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*58a2b000SEvgeniy Ivanov * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*58a2b000SEvgeniy Ivanov * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*58a2b000SEvgeniy Ivanov * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*58a2b000SEvgeniy Ivanov * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*58a2b000SEvgeniy Ivanov *
27*58a2b000SEvgeniy Ivanov */
28*58a2b000SEvgeniy Ivanov
29*58a2b000SEvgeniy Ivanov #include "sanamespace.h"
30*58a2b000SEvgeniy Ivanov
31*58a2b000SEvgeniy Ivanov #include <stdio.h>
32*58a2b000SEvgeniy Ivanov #include <unistd.h>
33*58a2b000SEvgeniy Ivanov #include <fcntl.h>
34*58a2b000SEvgeniy Ivanov #include <err.h>
35*58a2b000SEvgeniy Ivanov
36*58a2b000SEvgeniy Ivanov #include "biosdisk_ll.h"
37*58a2b000SEvgeniy Ivanov #include "biosdisk_user.h"
38*58a2b000SEvgeniy Ivanov
39*58a2b000SEvgeniy Ivanov /*
40*58a2b000SEvgeniy Ivanov * Replacement for i386/stand/lib/bios_disk.S.
41*58a2b000SEvgeniy Ivanov * Allows to map BIOS-like device numbers to character
42*58a2b000SEvgeniy Ivanov * device nodes or plain files.
43*58a2b000SEvgeniy Ivanov * The actual mapping is defined in the external table
44*58a2b000SEvgeniy Ivanov * "emuldisktab".
45*58a2b000SEvgeniy Ivanov */
46*58a2b000SEvgeniy Ivanov
47*58a2b000SEvgeniy Ivanov static int currentdev, currentdte;
48*58a2b000SEvgeniy Ivanov static int fd = -1;
49*58a2b000SEvgeniy Ivanov
50*58a2b000SEvgeniy Ivanov int
get_diskinfo(int dev)51*58a2b000SEvgeniy Ivanov get_diskinfo(int dev)
52*58a2b000SEvgeniy Ivanov {
53*58a2b000SEvgeniy Ivanov int i, retval;
54*58a2b000SEvgeniy Ivanov
55*58a2b000SEvgeniy Ivanov if (fd != -1) {
56*58a2b000SEvgeniy Ivanov close(fd);
57*58a2b000SEvgeniy Ivanov fd = -1;
58*58a2b000SEvgeniy Ivanov }
59*58a2b000SEvgeniy Ivanov
60*58a2b000SEvgeniy Ivanov i = 0;
61*58a2b000SEvgeniy Ivanov for (;;) {
62*58a2b000SEvgeniy Ivanov if (emuldisktab[i].biosdev == -1)
63*58a2b000SEvgeniy Ivanov break;
64*58a2b000SEvgeniy Ivanov if (emuldisktab[i].biosdev == dev)
65*58a2b000SEvgeniy Ivanov goto ok;
66*58a2b000SEvgeniy Ivanov i++;
67*58a2b000SEvgeniy Ivanov }
68*58a2b000SEvgeniy Ivanov warnx("unknown device %x", dev);
69*58a2b000SEvgeniy Ivanov return 0; /* triggers error in set_geometry() */
70*58a2b000SEvgeniy Ivanov
71*58a2b000SEvgeniy Ivanov ok:
72*58a2b000SEvgeniy Ivanov fd = open(emuldisktab[i].name, O_RDONLY, 0);
73*58a2b000SEvgeniy Ivanov if (fd < 0) {
74*58a2b000SEvgeniy Ivanov warn("open %s", emuldisktab[i].name);
75*58a2b000SEvgeniy Ivanov return 0;
76*58a2b000SEvgeniy Ivanov }
77*58a2b000SEvgeniy Ivanov
78*58a2b000SEvgeniy Ivanov currentdev = dev;
79*58a2b000SEvgeniy Ivanov currentdte = i;
80*58a2b000SEvgeniy Ivanov
81*58a2b000SEvgeniy Ivanov retval = ((emuldisktab[i].cyls - 1) & 0xff) << 16;
82*58a2b000SEvgeniy Ivanov retval |= ((emuldisktab[i].cyls - 1) & 0x300) << 6;
83*58a2b000SEvgeniy Ivanov retval |= emuldisktab[i].spt << 8;
84*58a2b000SEvgeniy Ivanov retval |= emuldisktab[i].heads - 1;
85*58a2b000SEvgeniy Ivanov return retval;
86*58a2b000SEvgeniy Ivanov }
87*58a2b000SEvgeniy Ivanov
88*58a2b000SEvgeniy Ivanov int
biosread(int dev,int cyl,int head,int sec,int nsec,char * buf)89*58a2b000SEvgeniy Ivanov biosread(int dev, int cyl, int head, int sec, int nsec, char *buf)
90*58a2b000SEvgeniy Ivanov {
91*58a2b000SEvgeniy Ivanov
92*58a2b000SEvgeniy Ivanov if (dev != currentdev) {
93*58a2b000SEvgeniy Ivanov warnx("biosread: unexpected device %x", dev);
94*58a2b000SEvgeniy Ivanov return -1;
95*58a2b000SEvgeniy Ivanov }
96*58a2b000SEvgeniy Ivanov
97*58a2b000SEvgeniy Ivanov if (lseek(fd, ((cyl * emuldisktab[currentdte].heads + head)
98*58a2b000SEvgeniy Ivanov * emuldisktab[currentdte].spt + sec) * 512,
99*58a2b000SEvgeniy Ivanov SEEK_SET) == -1) {
100*58a2b000SEvgeniy Ivanov warn("lseek");
101*58a2b000SEvgeniy Ivanov return -1;
102*58a2b000SEvgeniy Ivanov }
103*58a2b000SEvgeniy Ivanov if (read(fd, buf, nsec * 512) != nsec * 512) {
104*58a2b000SEvgeniy Ivanov warn("read");
105*58a2b000SEvgeniy Ivanov return -1;
106*58a2b000SEvgeniy Ivanov }
107*58a2b000SEvgeniy Ivanov return 0;
108*58a2b000SEvgeniy Ivanov }
109*58a2b000SEvgeniy Ivanov
110*58a2b000SEvgeniy Ivanov int
int13_extension(int dev)111*58a2b000SEvgeniy Ivanov int13_extension(int dev)
112*58a2b000SEvgeniy Ivanov {
113*58a2b000SEvgeniy Ivanov
114*58a2b000SEvgeniy Ivanov return 0;
115*58a2b000SEvgeniy Ivanov }
116*58a2b000SEvgeniy Ivanov
117*58a2b000SEvgeniy Ivanov void
int13_getextinfo(int dev,struct biosdisk_ext13info * info)118*58a2b000SEvgeniy Ivanov int13_getextinfo(int dev, struct biosdisk_ext13info *info)
119*58a2b000SEvgeniy Ivanov {
120*58a2b000SEvgeniy Ivanov }
121*58a2b000SEvgeniy Ivanov
122*58a2b000SEvgeniy Ivanov struct ext {
123*58a2b000SEvgeniy Ivanov int8_t size;
124*58a2b000SEvgeniy Ivanov int8_t resvd;
125*58a2b000SEvgeniy Ivanov int16_t cnt;
126*58a2b000SEvgeniy Ivanov int16_t off;
127*58a2b000SEvgeniy Ivanov int16_t seg;
128*58a2b000SEvgeniy Ivanov int64_t sec;
129*58a2b000SEvgeniy Ivanov };
130*58a2b000SEvgeniy Ivanov
131*58a2b000SEvgeniy Ivanov int
biosextread(int dev,struct ext * ext)132*58a2b000SEvgeniy Ivanov biosextread(int dev, struct ext *ext)
133*58a2b000SEvgeniy Ivanov {
134*58a2b000SEvgeniy Ivanov return -1;
135*58a2b000SEvgeniy Ivanov }
136