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