xref: /titanic_41/usr/src/cmd/format/disk_generic.c (revision 6fec3791b5a9a5621db93bfef3a6514bc0511b5d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains functions that implement the fdisk menu commands.
31  */
32 #include "global.h"
33 #include <sys/time.h>
34 #include <sys/resource.h>
35 #include <sys/wait.h>
36 #include <signal.h>
37 #include <string.h>
38 #include <sys/fcntl.h>
39 #include <sys/stat.h>
40 
41 #include <sys/dklabel.h>
42 #include <errno.h>
43 
44 
45 #include "main.h"
46 #include "analyze.h"
47 #include "menu.h"
48 #include "menu_command.h"
49 #include "menu_defect.h"
50 #include "menu_partition.h"
51 #if defined(_FIRMWARE_NEEDS_FDISK)
52 #include "menu_fdisk.h"
53 #endif	/* defined(_FIRMWARE_NEEDS_FDISK) */
54 #include "param.h"
55 #include "misc.h"
56 #include "label.h"
57 #include "startup.h"
58 #include "partition.h"
59 #include "prompts.h"
60 #include "checkdev.h"
61 #include "io.h"
62 #include "ctlr_scsi.h"
63 #include "auto_sense.h"
64 
65 #ifdef	__STDC__
66 /*
67  *	Local prototypes for ANSI C compilers
68  */
69 static int	generic_ck_format(void);
70 static int	generic_rdwr(int dir, int fd, daddr_t blkno, int secnt,
71 			caddr_t bufaddr, int flags, int *xfercntp);
72 
73 #else	/* __STDC__ */
74 
75 static int	generic_ck_format();
76 static int	generic_rdwr();
77 
78 #endif	/* __STDC__ */
79 
80 struct  ctlr_ops genericops = {
81 	generic_rdwr,
82 	generic_ck_format,
83 	0,
84 	0,
85 	0,
86 	0,
87 	0,
88 };
89 
90 
91 /*
92  * Check to see if the disk has been formatted.
93  * If we are able to read the first track, we conclude that
94  * the disk has been formatted.
95  */
96 static int
97 generic_ck_format()
98 {
99 	int	status;
100 
101 	/*
102 	 * Try to read the first four blocks.
103 	 */
104 	status = generic_rdwr(DIR_READ, cur_file, 0, 4, (caddr_t)cur_buf,
105 			F_SILENT, NULL);
106 	return (!status);
107 }
108 
109 /*
110  * Read or write the disk.
111  * Temporary interface until IOCTL interface finished.
112  */
113 /*ARGSUSED*/
114 static int
115 generic_rdwr(dir, fd, blkno, secnt, bufaddr, flags, xfercntp)
116 	int	dir;
117 	int	fd;
118 	daddr_t	blkno;
119 	int	secnt;
120 	caddr_t	bufaddr;
121 	int	flags;
122 	int	*xfercntp;
123 {
124 
125 	int	tmpsec, status, tmpblk;
126 
127 	tmpsec = secnt * UBSIZE;
128 	tmpblk = blkno * UBSIZE;
129 
130 	if (dir == DIR_READ) {
131 		status = lseek(fd, tmpblk, SEEK_SET);
132 		if (status != tmpblk)
133 			return (status);
134 
135 		status = read(fd, bufaddr, tmpsec);
136 		if (status != tmpsec)
137 			return (tmpsec);
138 		else
139 			return (0);
140 	} else {
141 		status = lseek(fd, tmpblk, SEEK_SET);
142 		if (status != tmpblk)
143 			return (status);
144 
145 		status = write(fd, bufaddr, tmpsec);
146 		if (status != tmpsec)
147 			return (tmpsec);
148 		else
149 			return (0);
150 	}
151 }
152