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