xref: /illumos-gate/usr/src/cmd/format/disk_generic.c (revision b8052df9f609edb713f6828c9eecc3d7be19dfb3)
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 static int	generic_ck_format(void);
63 static int	generic_rdwr(int dir, int fd, diskaddr_t blkno, int secnt,
64 			caddr_t bufaddr, int flags, int *xfercntp);
65 
66 struct  ctlr_ops genericops = {
67 	generic_rdwr,
68 	generic_ck_format,
69 	0,
70 	0,
71 	0,
72 	0,
73 	0,
74 };
75 
76 
77 /*
78  * Check to see if the disk has been formatted.
79  * If we are able to read the first track, we conclude that
80  * the disk has been formatted.
81  */
82 static int
83 generic_ck_format(void)
84 {
85 	int	status;
86 
87 	/*
88 	 * Try to read the first four blocks.
89 	 */
90 	status = generic_rdwr(DIR_READ, cur_file, 0, 4, (caddr_t)cur_buf,
91 	    F_SILENT, NULL);
92 	return (!status);
93 }
94 
95 /*
96  * Read or write the disk.
97  * Temporary interface until IOCTL interface finished.
98  */
99 /*ARGSUSED*/
100 static int
101 generic_rdwr(int dir, int fd, diskaddr_t blkno, int secnt, caddr_t bufaddr,
102     int flags, int *xfercntp)
103 {
104 
105 	offset_t	tmpsec, status, tmpblk;
106 	int		ret;
107 
108 	tmpsec = (offset_t)secnt * cur_blksz;
109 	tmpblk = (offset_t)blkno * cur_blksz;
110 
111 #if defined(_FIRMWARE_NEEDS_FDISK)
112 	/* Use "p0" file to seek/read the data  */
113 	(void) open_cur_file(FD_USE_P0_PATH);
114 #endif
115 	if (dir == DIR_READ) {
116 		status = llseek(fd, tmpblk, SEEK_SET);
117 		if (status != tmpblk) {
118 			ret = (int)status;
119 			goto out;
120 		}
121 
122 		status = read(fd, bufaddr, (size_t)tmpsec);
123 		if (status != tmpsec)
124 			ret = (int)tmpsec;
125 		else
126 			ret = 0;
127 	} else {
128 		status = llseek(fd, tmpblk, SEEK_SET);
129 		if (status != tmpblk) {
130 			ret = (int)status;
131 			goto out;
132 		}
133 
134 		status = write(fd, bufaddr, (size_t)tmpsec);
135 		if (status != tmpsec)
136 			ret = (int)tmpsec;
137 		else
138 			ret = 0;
139 	}
140 out:
141 #if defined(_FIRMWARE_NEEDS_FDISK)
142 	/* Restore cur_file with cur_disk->disk_path */
143 	(void) open_cur_file(FD_USE_CUR_DISK_PATH);
144 #endif
145 	return (ret);
146 }
147