1996-06-18 10:10:33 +04:00
|
|
|
/* $NetBSD: disk.c,v 1.14 1996/06/18 06:10:33 mycroft Exp $ */
|
1994-10-27 07:14:23 +03:00
|
|
|
|
1993-03-21 21:04:42 +03:00
|
|
|
/*
|
|
|
|
* Ported to boot 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
|
|
|
|
*
|
|
|
|
* Mach Operating System
|
|
|
|
* Copyright (c) 1992, 1991 Carnegie Mellon University
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify and distribute this software and its
|
|
|
|
* documentation is hereby granted, provided that both the copyright
|
|
|
|
* notice and this permission notice appear in all copies of the
|
|
|
|
* software, derivative works or modified versions, and any portions
|
|
|
|
* thereof, and that both notices appear in supporting documentation.
|
|
|
|
*
|
|
|
|
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
|
|
|
|
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
|
|
|
|
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* Carnegie Mellon requests users of this software to return to
|
|
|
|
*
|
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
|
|
|
*
|
|
|
|
* any improvements or extensions that they make and grant Carnegie Mellon
|
|
|
|
* the rights to redistribute these changes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "boot.h"
|
|
|
|
#ifdef DO_BAD144
|
|
|
|
#include <sys/dkbad.h>
|
|
|
|
#endif DO_BAD144
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
|
|
|
|
#define BIOS_DEV_FLOPPY 0x0
|
|
|
|
#define BIOS_DEV_WIN 0x80
|
|
|
|
|
|
|
|
#define BPS 512
|
|
|
|
#define SPT(di) ((di)&0xff)
|
|
|
|
#define HEADS(di) ((((di)>>8)&0xff)+1)
|
|
|
|
|
1996-06-18 10:10:33 +04:00
|
|
|
char *devs[] = {"wd", "hd", "fd", "", "sd", 0};
|
1993-03-21 21:04:42 +03:00
|
|
|
|
|
|
|
#ifdef DO_BAD144
|
|
|
|
struct dkbad dkb;
|
|
|
|
int do_bad144;
|
|
|
|
int bsize;
|
|
|
|
#endif DO_BAD144
|
|
|
|
|
|
|
|
char *iodest;
|
|
|
|
struct fs *fs;
|
|
|
|
struct inode inode;
|
1995-01-18 19:22:35 +03:00
|
|
|
int spt, spc;
|
1994-02-04 02:21:24 +03:00
|
|
|
int dosdev, unit, part, maj, boff, poff, bnum, cnt;
|
1993-03-21 21:04:42 +03:00
|
|
|
|
|
|
|
extern struct disklabel disklabel;
|
1995-01-18 19:22:35 +03:00
|
|
|
extern char iobuf[];
|
1993-03-21 21:04:42 +03:00
|
|
|
|
|
|
|
devopen()
|
|
|
|
{
|
|
|
|
struct dos_partition *dptr;
|
1995-01-18 19:22:35 +03:00
|
|
|
struct disklabel *lp;
|
1993-03-21 21:04:42 +03:00
|
|
|
int i, sector, di;
|
|
|
|
|
|
|
|
di = get_diskinfo(dosdev);
|
|
|
|
spc = (spt = SPT(di)) * HEADS(di);
|
1994-02-04 02:21:24 +03:00
|
|
|
if (dosdev == 2) {
|
1993-03-21 21:04:42 +03:00
|
|
|
boff = 0;
|
|
|
|
part = (spt == 15 ? 3 : 1);
|
1994-02-04 02:21:24 +03:00
|
|
|
} else {
|
1995-01-18 19:22:35 +03:00
|
|
|
Bread(0, iobuf);
|
|
|
|
dptr = (struct dos_partition *)&iobuf[DOSPARTOFF];
|
1994-10-21 01:44:37 +03:00
|
|
|
sector = LABELSECTOR;
|
1993-03-21 21:04:42 +03:00
|
|
|
for (i = 0; i < NDOSPART; i++, dptr++)
|
1994-10-21 01:44:37 +03:00
|
|
|
if (dptr->dp_typ == DOSPTYP_386BSD) {
|
|
|
|
sector = dptr->dp_start + LABELSECTOR;
|
1993-03-21 21:04:42 +03:00
|
|
|
break;
|
1994-10-21 01:44:37 +03:00
|
|
|
}
|
1995-01-18 19:22:35 +03:00
|
|
|
lp = &disklabel;
|
|
|
|
Bread(sector++, lp);
|
|
|
|
if (lp->d_magic != DISKMAGIC) {
|
1993-03-21 21:04:42 +03:00
|
|
|
printf("bad disklabel");
|
|
|
|
return 1;
|
|
|
|
}
|
1995-01-18 19:22:35 +03:00
|
|
|
if (maj == 4 || maj == 0 || maj == 1) {
|
|
|
|
if (lp->d_type == DTYPE_SCSI)
|
1993-03-21 21:04:42 +03:00
|
|
|
maj = 4; /* use scsi as boot dev */
|
|
|
|
else
|
|
|
|
maj = 0; /* must be ESDI/IDE */
|
|
|
|
}
|
1995-01-18 19:22:35 +03:00
|
|
|
boff = lp->d_partitions[part].p_offset;
|
1993-03-21 21:04:42 +03:00
|
|
|
#ifdef DO_BAD144
|
1995-01-18 19:22:35 +03:00
|
|
|
bsize = lp->d_partitions[part].p_size;
|
1993-03-21 21:04:42 +03:00
|
|
|
do_bad144 = 0;
|
1995-01-18 19:22:35 +03:00
|
|
|
if (lp->d_flags & D_BADSECT) {
|
1994-02-04 02:21:24 +03:00
|
|
|
/* this disk uses bad144 */
|
|
|
|
int i;
|
|
|
|
int dkbbnum;
|
|
|
|
struct dkbad *dkbptr;
|
|
|
|
|
|
|
|
/* find the first readable bad144 sector */
|
1995-01-18 19:22:35 +03:00
|
|
|
dkbbnum = lp->d_secperunit - lp->d_nsectors;
|
|
|
|
if (lp->d_secsize > DEV_BSIZE)
|
|
|
|
dkbbnum *= lp->d_secsize / DEV_BSIZE;
|
1994-02-04 02:21:24 +03:00
|
|
|
do_bad144 = 0;
|
1995-01-18 19:22:35 +03:00
|
|
|
for (i = 5; i; i--) {
|
1994-02-04 02:21:24 +03:00
|
|
|
/* XXX: what if the "DOS sector" < 512 bytes ??? */
|
1995-01-18 19:22:35 +03:00
|
|
|
Bread(dkbbnum, iobuf);
|
|
|
|
dkbptr = (struct dkbad *)&iobuf[0];
|
1993-03-21 21:04:42 +03:00
|
|
|
/* XXX why is this not in <sys/dkbad.h> ??? */
|
|
|
|
#define DKBAD_MAGIC 0x4321
|
1994-02-04 02:21:24 +03:00
|
|
|
if (dkbptr->bt_mbz == 0 &&
|
|
|
|
dkbptr->bt_flag == DKBAD_MAGIC) {
|
|
|
|
dkb = *dkbptr; /* structure copy */
|
|
|
|
do_bad144 = 1;
|
|
|
|
break;
|
|
|
|
}
|
1995-01-18 19:22:35 +03:00
|
|
|
dkbbnum += 2;
|
|
|
|
}
|
1994-02-04 02:21:24 +03:00
|
|
|
if (!do_bad144)
|
|
|
|
printf("Bad badsect table\n");
|
|
|
|
else
|
1995-01-18 19:22:35 +03:00
|
|
|
printf("Using bad144 bad sector at %d\n",
|
|
|
|
dkbbnum);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
#endif DO_BAD144
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
devread()
|
|
|
|
{
|
|
|
|
int offset, sector = bnum;
|
1995-01-18 19:22:35 +03:00
|
|
|
for (offset = 0; offset < cnt; offset += BPS)
|
|
|
|
Bread(badsect(sector++), iodest + offset);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
1993-07-11 16:02:17 +04:00
|
|
|
#define I_ADDR ((void *) 0) /* XXX where all reads go */
|
|
|
|
|
|
|
|
/* Read ahead buffer large enough for one track on a 1440K floppy. For
|
|
|
|
* reading from floppies, the bootstrap has to be loaded on a 64K boundary
|
|
|
|
* to ensure that this buffer doesn't cross a 64K DMA boundary.
|
|
|
|
*/
|
|
|
|
#define RA_SECTORS 18
|
|
|
|
static char ra_buf[RA_SECTORS * BPS];
|
1994-02-20 17:05:19 +03:00
|
|
|
static int ra_dev;
|
1993-07-11 16:02:17 +04:00
|
|
|
static int ra_end;
|
|
|
|
static int ra_first;
|
|
|
|
|
1995-01-18 19:22:35 +03:00
|
|
|
Bread(sector, addr)
|
1995-01-18 18:46:34 +03:00
|
|
|
int sector;
|
1995-01-18 19:22:35 +03:00
|
|
|
void *addr;
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1995-03-12 03:10:53 +03:00
|
|
|
|
1994-02-20 17:05:19 +03:00
|
|
|
if (dosdev != ra_dev || sector < ra_first || sector >= ra_end) {
|
1993-07-11 16:02:17 +04:00
|
|
|
int cyl, head, sec, nsec;
|
|
|
|
|
1995-01-18 05:54:23 +03:00
|
|
|
cyl = sector / spc;
|
1993-07-11 16:02:17 +04:00
|
|
|
head = (sector % spc) / spt;
|
|
|
|
sec = sector % spt;
|
|
|
|
nsec = spt - sec;
|
|
|
|
if (nsec > RA_SECTORS)
|
|
|
|
nsec = RA_SECTORS;
|
1993-06-18 06:28:58 +04:00
|
|
|
twiddle();
|
1995-03-12 15:09:18 +03:00
|
|
|
while (biosread(dosdev, cyl, head, sec, nsec, ra_buf)) {
|
|
|
|
printf("Error: C:%d H:%d S:%d\n", cyl, head, sec);
|
1994-02-04 02:21:24 +03:00
|
|
|
nsec = 1;
|
1993-07-11 16:02:17 +04:00
|
|
|
twiddle();
|
|
|
|
}
|
1994-02-20 17:05:19 +03:00
|
|
|
ra_dev = dosdev;
|
1993-07-11 16:02:17 +04:00
|
|
|
ra_first = sector;
|
|
|
|
ra_end = sector + nsec;
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
1995-01-18 19:22:35 +03:00
|
|
|
bcopy(ra_buf + (sector - ra_first) * BPS, addr, BPS);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
1995-01-18 18:46:34 +03:00
|
|
|
badsect(sector)
|
|
|
|
int sector;
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
#ifdef DO_BAD144
|
|
|
|
if (do_bad144) {
|
1995-01-18 05:54:23 +03:00
|
|
|
u_short cyl, head, sec;
|
1994-02-04 02:21:24 +03:00
|
|
|
int newsec;
|
|
|
|
struct disklabel *dl = &disklabel;
|
|
|
|
|
|
|
|
/* XXX */
|
|
|
|
/* from wd.c */
|
|
|
|
/* bt_cyl = cylinder number in sorted order */
|
|
|
|
/* bt_trksec is actually (head << 8) + sec */
|
|
|
|
|
|
|
|
/* only remap sectors in the partition */
|
|
|
|
if (sector < boff || sector >= boff + bsize)
|
|
|
|
goto no_remap;
|
|
|
|
|
|
|
|
cyl = sector / dl->d_secpercyl;
|
|
|
|
head = (sector % dl->d_secpercyl) / dl->d_nsectors;
|
|
|
|
sec = sector % dl->d_nsectors;
|
1995-01-18 05:54:23 +03:00
|
|
|
sec += head << 8;
|
1994-02-04 02:21:24 +03:00
|
|
|
|
|
|
|
/* now, look in the table for a possible bad sector */
|
|
|
|
for (i = 0; i < 126; i++) {
|
1995-01-18 05:54:23 +03:00
|
|
|
if (dkb.bt_bad[i].bt_cyl == cyl &&
|
|
|
|
dkb.bt_bad[i].bt_trksec == sec) {
|
|
|
|
/* found same sector */
|
|
|
|
goto remap;
|
1994-02-04 02:21:24 +03:00
|
|
|
} else if (dkb.bt_bad[i].bt_cyl > cyl) {
|
1995-01-18 05:54:23 +03:00
|
|
|
goto no_remap;
|
1994-02-04 02:21:24 +03:00
|
|
|
}
|
|
|
|
}
|
1995-01-18 05:54:23 +03:00
|
|
|
goto no_remap;
|
|
|
|
remap:
|
1994-02-04 02:21:24 +03:00
|
|
|
/* otherwise find replacement sector */
|
|
|
|
newsec = dl->d_secperunit - dl->d_nsectors - i -1;
|
|
|
|
return newsec;
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
#endif DO_BAD144
|
1994-02-04 02:21:24 +03:00
|
|
|
no_remap:
|
1993-03-21 21:04:42 +03:00
|
|
|
return sector;
|
|
|
|
}
|