157106Sakito /*
257106Sakito  * Copyright (c) 1992 OMRON Corporation.
3*63199Sbostic  * Copyright (c) 1992, 1993
4*63199Sbostic  *	The Regents of the University of California.  All rights reserved.
557106Sakito  *
657106Sakito  * This code is derived from software contributed to Berkeley by
757106Sakito  * OMRON Corporation.
857106Sakito  *
957106Sakito  * %sccs.include.redist.c%
1057106Sakito  *
11*63199Sbostic  *	@(#)ufs_disksubr.c	8.1 (Berkeley) 06/10/93
1257106Sakito  */
1357106Sakito 
1457106Sakito /*
1557106Sakito  * ufs_disksubr.c -- disk utility routines
1657106Sakito  * by A.Fujita, FEB-26-1992
1757106Sakito  */
1857106Sakito 
1957106Sakito #include <sys/param.h>
2057106Sakito #include <sys/disklabel.h>
2157106Sakito #include <luna68k/dev/scsireg.h>
2257106Sakito 
2357106Sakito extern u_char lbl_buff[];
2457106Sakito 
2557106Sakito /*
2657106Sakito  * Attempt to read a disk label from a device
2757106Sakito  * using the indicated stategy routine.
2857106Sakito  * The label must be partly set up before this:
2957106Sakito  * secpercyl and anything required in the strategy routine
3057106Sakito  * (e.g., sector size) must be filled in before calling us.
3157106Sakito  * Returns null on success and an error string on failure.
3257106Sakito  */
3357106Sakito char *
readdisklabel(dev,strat,lp)3457106Sakito readdisklabel(dev, strat, lp)
3557106Sakito 	int dev;
3657106Sakito 	int (*strat)();
3757106Sakito 	register struct disklabel *lp;
3857106Sakito {
3957106Sakito 	register u_char *bp = lbl_buff;
4057106Sakito 	struct disklabel *dlp;
4157106Sakito 	char *msg = NULL;
4257106Sakito 	static struct scsi_fmt_cdb cdb = {
4357106Sakito 		6,
4457106Sakito 		CMD_READ, 0, 0, 0, 1, 0
4557106Sakito 	};
4657106Sakito 
4757106Sakito 	if (lp->d_secperunit == 0)
4857106Sakito 		lp->d_secperunit = 0x1fffffff;
4957106Sakito 	lp->d_npartitions = 1;
5057106Sakito 	if (lp->d_partitions[0].p_size == 0)
5157106Sakito 		lp->d_partitions[0].p_size = 0x1fffffff;
5257106Sakito 	lp->d_partitions[0].p_offset = 0;
5357106Sakito 
5457106Sakito 	if (scsi_immed_command(0, dev, 0, &cdb, bp, DEV_BSIZE) != 0) {
5557106Sakito 		msg = "I/O error";
5657106Sakito 	} else {
5757106Sakito 		for (dlp = (struct disklabel *)bp;
5857106Sakito 		     dlp <= (struct disklabel *)(bp + DEV_BSIZE - sizeof(*dlp));
5957106Sakito 		     dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
6057106Sakito 			if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
6157106Sakito 				if (msg == NULL)
6257106Sakito 					msg = "no disk label";
6357106Sakito 			} else if (dlp->d_npartitions > MAXPARTITIONS ||
6457106Sakito 				   dkcksum(dlp) != 0)
6557106Sakito 				msg = "disk label corrupted";
6657106Sakito 			else {
6757106Sakito 				*lp = *dlp;
6857106Sakito 				msg = NULL;
6957106Sakito 				break;
7057106Sakito 			}
7157106Sakito 		}
7257106Sakito 	}
7357106Sakito 
7457106Sakito 	return (msg);
7557106Sakito }
76