1*57106Sakito /*
2*57106Sakito  * Copyright (c) 1992 OMRON Corporation.
3*57106Sakito  * Copyright (c) 1992 The Regents of the University of California.
4*57106Sakito  * All rights reserved.
5*57106Sakito  *
6*57106Sakito  * This code is derived from software contributed to Berkeley by
7*57106Sakito  * OMRON Corporation.
8*57106Sakito  *
9*57106Sakito  * %sccs.include.redist.c%
10*57106Sakito  *
11*57106Sakito  *	@(#)ufs_disksubr.c	7.1 (Berkeley) 12/13/92
12*57106Sakito  */
13*57106Sakito 
14*57106Sakito /*
15*57106Sakito  * ufs_disksubr.c -- disk utility routines
16*57106Sakito  * by A.Fujita, FEB-26-1992
17*57106Sakito  */
18*57106Sakito 
19*57106Sakito #include <sys/param.h>
20*57106Sakito #include <sys/disklabel.h>
21*57106Sakito #include <luna68k/dev/scsireg.h>
22*57106Sakito 
23*57106Sakito extern u_char lbl_buff[];
24*57106Sakito 
25*57106Sakito /*
26*57106Sakito  * Attempt to read a disk label from a device
27*57106Sakito  * using the indicated stategy routine.
28*57106Sakito  * The label must be partly set up before this:
29*57106Sakito  * secpercyl and anything required in the strategy routine
30*57106Sakito  * (e.g., sector size) must be filled in before calling us.
31*57106Sakito  * Returns null on success and an error string on failure.
32*57106Sakito  */
33*57106Sakito char *
34*57106Sakito readdisklabel(dev, strat, lp)
35*57106Sakito 	int dev;
36*57106Sakito 	int (*strat)();
37*57106Sakito 	register struct disklabel *lp;
38*57106Sakito {
39*57106Sakito 	register u_char *bp = lbl_buff;
40*57106Sakito 	struct disklabel *dlp;
41*57106Sakito 	char *msg = NULL;
42*57106Sakito 	static struct scsi_fmt_cdb cdb = {
43*57106Sakito 		6,
44*57106Sakito 		CMD_READ, 0, 0, 0, 1, 0
45*57106Sakito 	};
46*57106Sakito 
47*57106Sakito 	if (lp->d_secperunit == 0)
48*57106Sakito 		lp->d_secperunit = 0x1fffffff;
49*57106Sakito 	lp->d_npartitions = 1;
50*57106Sakito 	if (lp->d_partitions[0].p_size == 0)
51*57106Sakito 		lp->d_partitions[0].p_size = 0x1fffffff;
52*57106Sakito 	lp->d_partitions[0].p_offset = 0;
53*57106Sakito 
54*57106Sakito 	if (scsi_immed_command(0, dev, 0, &cdb, bp, DEV_BSIZE) != 0) {
55*57106Sakito 		msg = "I/O error";
56*57106Sakito 	} else {
57*57106Sakito 		for (dlp = (struct disklabel *)bp;
58*57106Sakito 		     dlp <= (struct disklabel *)(bp + DEV_BSIZE - sizeof(*dlp));
59*57106Sakito 		     dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
60*57106Sakito 			if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
61*57106Sakito 				if (msg == NULL)
62*57106Sakito 					msg = "no disk label";
63*57106Sakito 			} else if (dlp->d_npartitions > MAXPARTITIONS ||
64*57106Sakito 				   dkcksum(dlp) != 0)
65*57106Sakito 				msg = "disk label corrupted";
66*57106Sakito 			else {
67*57106Sakito 				*lp = *dlp;
68*57106Sakito 				msg = NULL;
69*57106Sakito 				break;
70*57106Sakito 			}
71*57106Sakito 		}
72*57106Sakito 	}
73*57106Sakito 
74*57106Sakito 	return (msg);
75*57106Sakito }
76