1*49e9ba9aSchristos /* $NetBSD: biosdisk_user.c,v 1.7 2008/12/14 18:46:33 christos Exp $ */
241539711Sdrochner
341539711Sdrochner /*
441539711Sdrochner * Copyright (c) 1998
541539711Sdrochner * Matthias Drochner. All rights reserved.
641539711Sdrochner *
741539711Sdrochner * Redistribution and use in source and binary forms, with or without
841539711Sdrochner * modification, are permitted provided that the following conditions
941539711Sdrochner * are met:
1041539711Sdrochner * 1. Redistributions of source code must retain the above copyright
1141539711Sdrochner * notice, this list of conditions and the following disclaimer.
1241539711Sdrochner * 2. Redistributions in binary form must reproduce the above copyright
1341539711Sdrochner * notice, this list of conditions and the following disclaimer in the
1441539711Sdrochner * documentation and/or other materials provided with the distribution.
1541539711Sdrochner *
1641539711Sdrochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1741539711Sdrochner * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1841539711Sdrochner * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1941539711Sdrochner * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2041539711Sdrochner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2141539711Sdrochner * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2241539711Sdrochner * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2341539711Sdrochner * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2441539711Sdrochner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2541539711Sdrochner * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2641539711Sdrochner *
2741539711Sdrochner */
2841539711Sdrochner
2941539711Sdrochner #include "sanamespace.h"
3041539711Sdrochner
3141539711Sdrochner #include <stdio.h>
3241539711Sdrochner #include <unistd.h>
3341539711Sdrochner #include <fcntl.h>
3441539711Sdrochner #include <err.h>
3541539711Sdrochner
364feae58eSdrochner #include "biosdisk_ll.h"
3741539711Sdrochner #include "biosdisk_user.h"
3841539711Sdrochner
3941539711Sdrochner /*
4041539711Sdrochner * Replacement for i386/stand/lib/bios_disk.S.
4141539711Sdrochner * Allows to map BIOS-like device numbers to character
4241539711Sdrochner * device nodes or plain files.
4341539711Sdrochner * The actual mapping is defined in the external table
4441539711Sdrochner * "emuldisktab".
4541539711Sdrochner */
4641539711Sdrochner
4741539711Sdrochner static int currentdev, currentdte;
4841539711Sdrochner static int fd = -1;
4941539711Sdrochner
5099a84ef7Sdrochner int
get_diskinfo(int dev)51*49e9ba9aSchristos get_diskinfo(int dev)
5241539711Sdrochner {
5399a84ef7Sdrochner int i, retval;
5441539711Sdrochner
5541539711Sdrochner if (fd != -1) {
5641539711Sdrochner close(fd);
5741539711Sdrochner fd = -1;
5841539711Sdrochner }
5941539711Sdrochner
6041539711Sdrochner i = 0;
6141539711Sdrochner for (;;) {
6241539711Sdrochner if (emuldisktab[i].biosdev == -1)
6341539711Sdrochner break;
6499a84ef7Sdrochner if (emuldisktab[i].biosdev == dev)
6541539711Sdrochner goto ok;
6641539711Sdrochner i++;
6741539711Sdrochner }
6899a84ef7Sdrochner warnx("unknown device %x", dev);
69*49e9ba9aSchristos return 0; /* triggers error in set_geometry() */
7041539711Sdrochner
7141539711Sdrochner ok:
7241539711Sdrochner fd = open(emuldisktab[i].name, O_RDONLY, 0);
7341539711Sdrochner if (fd < 0) {
7441539711Sdrochner warn("open %s", emuldisktab[i].name);
75*49e9ba9aSchristos return 0;
7641539711Sdrochner }
7741539711Sdrochner
7899a84ef7Sdrochner currentdev = dev;
7941539711Sdrochner currentdte = i;
804feae58eSdrochner
8199a84ef7Sdrochner retval = ((emuldisktab[i].cyls - 1) & 0xff) << 16;
8299a84ef7Sdrochner retval |= ((emuldisktab[i].cyls - 1) & 0x300) << 6;
8399a84ef7Sdrochner retval |= emuldisktab[i].spt << 8;
8499a84ef7Sdrochner retval |= emuldisktab[i].heads - 1;
85*49e9ba9aSchristos return retval;
8641539711Sdrochner }
8741539711Sdrochner
8841539711Sdrochner int
biosread(int dev,int cyl,int head,int sec,int nsec,char * buf)89*49e9ba9aSchristos biosread(int dev, int cyl, int head, int sec, int nsec, char *buf)
9041539711Sdrochner {
91*49e9ba9aSchristos
9241539711Sdrochner if (dev != currentdev) {
9341539711Sdrochner warnx("biosread: unexpected device %x", dev);
94*49e9ba9aSchristos return -1;
9541539711Sdrochner }
9641539711Sdrochner
9741539711Sdrochner if (lseek(fd, ((cyl * emuldisktab[currentdte].heads + head)
9841539711Sdrochner * emuldisktab[currentdte].spt + sec) * 512,
9941539711Sdrochner SEEK_SET) == -1) {
10041539711Sdrochner warn("lseek");
101*49e9ba9aSchristos return -1;
10241539711Sdrochner }
10341539711Sdrochner if (read(fd, buf, nsec * 512) != nsec * 512) {
10441539711Sdrochner warn("read");
105*49e9ba9aSchristos return -1;
10641539711Sdrochner }
107*49e9ba9aSchristos return 0;
10841539711Sdrochner }
1091c0cc948Sdrochner
1101c0cc948Sdrochner int
int13_extension(int dev)111*49e9ba9aSchristos int13_extension(int dev)
1121c0cc948Sdrochner {
113*49e9ba9aSchristos
114*49e9ba9aSchristos return 0;
1151c0cc948Sdrochner }
1161c0cc948Sdrochner
1174feae58eSdrochner void
int13_getextinfo(int dev,struct biosdisk_ext13info * info)118*49e9ba9aSchristos int13_getextinfo(int dev, struct biosdisk_ext13info *info)
1194feae58eSdrochner {
1204feae58eSdrochner }
1214feae58eSdrochner
1221c0cc948Sdrochner struct ext {
1231c0cc948Sdrochner int8_t size;
1241c0cc948Sdrochner int8_t resvd;
1251c0cc948Sdrochner int16_t cnt;
1261c0cc948Sdrochner int16_t off;
1271c0cc948Sdrochner int16_t seg;
1281c0cc948Sdrochner int64_t sec;
1291c0cc948Sdrochner };
1301c0cc948Sdrochner
1311c0cc948Sdrochner int
biosextread(int dev,struct ext * ext)132*49e9ba9aSchristos biosextread(int dev, struct ext *ext)
1331c0cc948Sdrochner {
134*49e9ba9aSchristos return -1;
1351c0cc948Sdrochner }
136