xref: /netbsd-src/sys/arch/luna68k/stand/boot/ufs_disksubr.c (revision 197f8adfcbc819a18ce16fded0cb428b239c2591)
1*197f8adfStsutsui /*	$NetBSD: ufs_disksubr.c,v 1.7 2023/10/13 16:10:24 tsutsui Exp $	*/
2df6945b8Stsutsui 
3df6945b8Stsutsui /*
4df6945b8Stsutsui  * Copyright (c) 1992 OMRON Corporation.
5df6945b8Stsutsui  *
6df6945b8Stsutsui  * This code is derived from software contributed to Berkeley by
7df6945b8Stsutsui  * OMRON Corporation.
8df6945b8Stsutsui  *
9df6945b8Stsutsui  * Redistribution and use in source and binary forms, with or without
10df6945b8Stsutsui  * modification, are permitted provided that the following conditions
11df6945b8Stsutsui  * are met:
12df6945b8Stsutsui  * 1. Redistributions of source code must retain the above copyright
13df6945b8Stsutsui  *    notice, this list of conditions and the following disclaimer.
14df6945b8Stsutsui  * 2. Redistributions in binary form must reproduce the above copyright
15df6945b8Stsutsui  *    notice, this list of conditions and the following disclaimer in the
16df6945b8Stsutsui  *    documentation and/or other materials provided with the distribution.
17df6945b8Stsutsui  * 3. All advertising materials mentioning features or use of this software
18df6945b8Stsutsui  *    must display the following acknowledgement:
19df6945b8Stsutsui  *	This product includes software developed by the University of
20df6945b8Stsutsui  *	California, Berkeley and its contributors.
21df6945b8Stsutsui  * 4. Neither the name of the University nor the names of its contributors
22df6945b8Stsutsui  *    may be used to endorse or promote products derived from this software
23df6945b8Stsutsui  *    without specific prior written permission.
24df6945b8Stsutsui  *
25df6945b8Stsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df6945b8Stsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df6945b8Stsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df6945b8Stsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df6945b8Stsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df6945b8Stsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df6945b8Stsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df6945b8Stsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df6945b8Stsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df6945b8Stsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df6945b8Stsutsui  * SUCH DAMAGE.
36df6945b8Stsutsui  *
37df6945b8Stsutsui  *	@(#)ufs_disksubr.c	8.1 (Berkeley) 6/10/93
38df6945b8Stsutsui  */
39df6945b8Stsutsui /*
40df6945b8Stsutsui  * Copyright (c) 1992, 1993
41df6945b8Stsutsui  *	The Regents of the University of California.  All rights reserved.
42df6945b8Stsutsui  *
43df6945b8Stsutsui  * This code is derived from software contributed to Berkeley by
44df6945b8Stsutsui  * OMRON Corporation.
45df6945b8Stsutsui  *
46df6945b8Stsutsui  * Redistribution and use in source and binary forms, with or without
47df6945b8Stsutsui  * modification, are permitted provided that the following conditions
48df6945b8Stsutsui  * are met:
49df6945b8Stsutsui  * 1. Redistributions of source code must retain the above copyright
50df6945b8Stsutsui  *    notice, this list of conditions and the following disclaimer.
51df6945b8Stsutsui  * 2. Redistributions in binary form must reproduce the above copyright
52df6945b8Stsutsui  *    notice, this list of conditions and the following disclaimer in the
53df6945b8Stsutsui  *    documentation and/or other materials provided with the distribution.
54df6945b8Stsutsui  * 3. Neither the name of the University nor the names of its contributors
55df6945b8Stsutsui  *    may be used to endorse or promote products derived from this software
56df6945b8Stsutsui  *    without specific prior written permission.
57df6945b8Stsutsui  *
58df6945b8Stsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59df6945b8Stsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60df6945b8Stsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61df6945b8Stsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62df6945b8Stsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63df6945b8Stsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64df6945b8Stsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65df6945b8Stsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66df6945b8Stsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67df6945b8Stsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68df6945b8Stsutsui  * SUCH DAMAGE.
69df6945b8Stsutsui  *
70df6945b8Stsutsui  *	@(#)ufs_disksubr.c	8.1 (Berkeley) 6/10/93
71df6945b8Stsutsui  */
72df6945b8Stsutsui 
73df6945b8Stsutsui /*
74df6945b8Stsutsui  * ufs_disksubr.c -- disk utility routines
75df6945b8Stsutsui  * by A.Fujita, FEB-26-1992
76df6945b8Stsutsui  */
77df6945b8Stsutsui 
78df6945b8Stsutsui #include <sys/param.h>
79df6945b8Stsutsui #include <sys/disklabel.h>
803b806a0fSmrg 
813b806a0fSmrg #include <lib/libkern/libkern.h>
823b806a0fSmrg 
83df6945b8Stsutsui #include <luna68k/stand/boot/samachdep.h>
84df6945b8Stsutsui #include <luna68k/stand/boot/scsireg.h>
85df6945b8Stsutsui 
86df6945b8Stsutsui /*
87df6945b8Stsutsui  * Attempt to read a disk label from a device
88*197f8adfStsutsui  * using the indicated strategy routine.
89df6945b8Stsutsui  * The label must be partly set up before this:
90df6945b8Stsutsui  * secpercyl and anything required in the strategy routine
91df6945b8Stsutsui  * (e.g., sector size) must be filled in before calling us.
92df6945b8Stsutsui  * Returns null on success and an error string on failure.
93df6945b8Stsutsui  */
94df6945b8Stsutsui char *
readdisklabel(int ctlr,int id,struct disklabel * lp)954667f281Stsutsui readdisklabel(int ctlr, int id, struct disklabel *lp)
96df6945b8Stsutsui {
97bdeab44eStsutsui 	uint8_t *bp = lbl_buff;
98df6945b8Stsutsui 	struct disklabel *dlp;
99df6945b8Stsutsui 	char *msg = NULL;
100d28b1498Stsutsui 	static struct scsi_generic_cdb cdb = {
101df6945b8Stsutsui 		6,
102df6945b8Stsutsui 		{ CMD_READ, 0, 0, 0, 1, 0 }
103df6945b8Stsutsui 	};
104df6945b8Stsutsui 
105df6945b8Stsutsui 	if (lp->d_secperunit == 0)
106df6945b8Stsutsui 		lp->d_secperunit = 0x1fffffff;
107df6945b8Stsutsui 	lp->d_npartitions = 1;
108df6945b8Stsutsui 	if (lp->d_partitions[0].p_size == 0)
109df6945b8Stsutsui 		lp->d_partitions[0].p_size = 0x1fffffff;
110df6945b8Stsutsui 	lp->d_partitions[0].p_offset = 0;
111df6945b8Stsutsui 
1124667f281Stsutsui 	if (scsi_immed_command(ctlr, id, 0, &cdb, bp, DEV_BSIZE) != 0) {
113df6945b8Stsutsui 		msg = "I/O error";
114df6945b8Stsutsui 	} else {
115df6945b8Stsutsui 		for (dlp = (struct disklabel *)bp;
116df6945b8Stsutsui 		     dlp <= (struct disklabel *)(bp + DEV_BSIZE - sizeof(*dlp));
117df6945b8Stsutsui 		     dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
1180dfe4134Stsutsui 			if (dlp->d_magic != DISKMAGIC ||
1190dfe4134Stsutsui 			    dlp->d_magic2 != DISKMAGIC) {
120df6945b8Stsutsui 				if (msg == NULL)
121df6945b8Stsutsui 					msg = "no disk label";
122df6945b8Stsutsui 			} else if (dlp->d_npartitions > MAXPARTITIONS ||
123df6945b8Stsutsui 				   dkcksum(dlp) != 0)
124df6945b8Stsutsui 				msg = "disk label corrupted";
125df6945b8Stsutsui 			else {
126df6945b8Stsutsui 				*lp = *dlp;
127df6945b8Stsutsui 				msg = NULL;
128df6945b8Stsutsui 				break;
129df6945b8Stsutsui 			}
130df6945b8Stsutsui 		}
131df6945b8Stsutsui 	}
132df6945b8Stsutsui 
1330dfe4134Stsutsui 	return msg;
134df6945b8Stsutsui }
135