xref: /illumos-gate/usr/src/cmd/format/disk_generic.c (revision b12aaafb)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5342440ecSPrasad Singamsetty  * Common Development and Distribution License (the "License").
6342440ecSPrasad Singamsetty  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2265908c77Syu, larry liu - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
233e1bd7a2Ssjelinek  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This file contains functions that implement the fdisk menu commands.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #include "global.h"
307c478bd9Sstevel@tonic-gate #include <sys/time.h>
317c478bd9Sstevel@tonic-gate #include <sys/resource.h>
327c478bd9Sstevel@tonic-gate #include <sys/wait.h>
337c478bd9Sstevel@tonic-gate #include <signal.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/dklabel.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "main.h"
437c478bd9Sstevel@tonic-gate #include "analyze.h"
447c478bd9Sstevel@tonic-gate #include "menu.h"
457c478bd9Sstevel@tonic-gate #include "menu_command.h"
467c478bd9Sstevel@tonic-gate #include "menu_defect.h"
477c478bd9Sstevel@tonic-gate #include "menu_partition.h"
487c478bd9Sstevel@tonic-gate #if defined(_FIRMWARE_NEEDS_FDISK)
497c478bd9Sstevel@tonic-gate #include "menu_fdisk.h"
507c478bd9Sstevel@tonic-gate #endif	/* defined(_FIRMWARE_NEEDS_FDISK) */
517c478bd9Sstevel@tonic-gate #include "param.h"
527c478bd9Sstevel@tonic-gate #include "misc.h"
537c478bd9Sstevel@tonic-gate #include "label.h"
547c478bd9Sstevel@tonic-gate #include "startup.h"
557c478bd9Sstevel@tonic-gate #include "partition.h"
567c478bd9Sstevel@tonic-gate #include "prompts.h"
573e1bd7a2Ssjelinek #include "checkdev.h"
587c478bd9Sstevel@tonic-gate #include "io.h"
597c478bd9Sstevel@tonic-gate #include "ctlr_scsi.h"
607c478bd9Sstevel@tonic-gate #include "auto_sense.h"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static int	generic_ck_format(void);
63342440ecSPrasad Singamsetty static int	generic_rdwr(int dir, int fd, diskaddr_t blkno, int secnt,
647c478bd9Sstevel@tonic-gate 			caddr_t bufaddr, int flags, int *xfercntp);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate struct  ctlr_ops genericops = {
677c478bd9Sstevel@tonic-gate 	generic_rdwr,
687c478bd9Sstevel@tonic-gate 	generic_ck_format,
697c478bd9Sstevel@tonic-gate 	0,
707c478bd9Sstevel@tonic-gate 	0,
717c478bd9Sstevel@tonic-gate 	0,
727c478bd9Sstevel@tonic-gate 	0,
737c478bd9Sstevel@tonic-gate 	0,
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Check to see if the disk has been formatted.
797c478bd9Sstevel@tonic-gate  * If we are able to read the first track, we conclude that
807c478bd9Sstevel@tonic-gate  * the disk has been formatted.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate static int
generic_ck_format(void)83*b12aaafbSToomas Soome generic_ck_format(void)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	int	status;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * Try to read the first four blocks.
897c478bd9Sstevel@tonic-gate 	 */
907c478bd9Sstevel@tonic-gate 	status = generic_rdwr(DIR_READ, cur_file, 0, 4, (caddr_t)cur_buf,
91342440ecSPrasad Singamsetty 	    F_SILENT, NULL);
927c478bd9Sstevel@tonic-gate 	return (!status);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Read or write the disk.
977c478bd9Sstevel@tonic-gate  * Temporary interface until IOCTL interface finished.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1007c478bd9Sstevel@tonic-gate static int
generic_rdwr(int dir,int fd,diskaddr_t blkno,int secnt,caddr_t bufaddr,int flags,int * xfercntp)101*b12aaafbSToomas Soome generic_rdwr(int dir, int fd, diskaddr_t blkno, int secnt, caddr_t bufaddr,
102*b12aaafbSToomas Soome     int flags, int *xfercntp)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 
105342440ecSPrasad Singamsetty 	offset_t	tmpsec, status, tmpblk;
106342440ecSPrasad Singamsetty 	int		ret;
1077c478bd9Sstevel@tonic-gate 
10865908c77Syu, larry liu - Sun Microsystems - Beijing China 	tmpsec = (offset_t)secnt * cur_blksz;
10965908c77Syu, larry liu - Sun Microsystems - Beijing China 	tmpblk = (offset_t)blkno * cur_blksz;
1107c478bd9Sstevel@tonic-gate 
111342440ecSPrasad Singamsetty #if defined(_FIRMWARE_NEEDS_FDISK)
112342440ecSPrasad Singamsetty 	/* Use "p0" file to seek/read the data  */
113342440ecSPrasad Singamsetty 	(void) open_cur_file(FD_USE_P0_PATH);
114342440ecSPrasad Singamsetty #endif
1157c478bd9Sstevel@tonic-gate 	if (dir == DIR_READ) {
116342440ecSPrasad Singamsetty 		status = llseek(fd, tmpblk, SEEK_SET);
117342440ecSPrasad Singamsetty 		if (status != tmpblk) {
118342440ecSPrasad Singamsetty 			ret = (int)status;
119342440ecSPrasad Singamsetty 			goto out;
120342440ecSPrasad Singamsetty 		}
1217c478bd9Sstevel@tonic-gate 
122342440ecSPrasad Singamsetty 		status = read(fd, bufaddr, (size_t)tmpsec);
1237c478bd9Sstevel@tonic-gate 		if (status != tmpsec)
124342440ecSPrasad Singamsetty 			ret = (int)tmpsec;
1257c478bd9Sstevel@tonic-gate 		else
126342440ecSPrasad Singamsetty 			ret = 0;
1277c478bd9Sstevel@tonic-gate 	} else {
128342440ecSPrasad Singamsetty 		status = llseek(fd, tmpblk, SEEK_SET);
129342440ecSPrasad Singamsetty 		if (status != tmpblk) {
130342440ecSPrasad Singamsetty 			ret = (int)status;
131342440ecSPrasad Singamsetty 			goto out;
132342440ecSPrasad Singamsetty 		}
1337c478bd9Sstevel@tonic-gate 
134342440ecSPrasad Singamsetty 		status = write(fd, bufaddr, (size_t)tmpsec);
1357c478bd9Sstevel@tonic-gate 		if (status != tmpsec)
136342440ecSPrasad Singamsetty 			ret = (int)tmpsec;
1377c478bd9Sstevel@tonic-gate 		else
138342440ecSPrasad Singamsetty 			ret = 0;
1397c478bd9Sstevel@tonic-gate 	}
140342440ecSPrasad Singamsetty out:
141342440ecSPrasad Singamsetty #if defined(_FIRMWARE_NEEDS_FDISK)
142342440ecSPrasad Singamsetty 	/* Restore cur_file with cur_disk->disk_path */
143342440ecSPrasad Singamsetty 	(void) open_cur_file(FD_USE_CUR_DISK_PATH);
144342440ecSPrasad Singamsetty #endif
145342440ecSPrasad Singamsetty 	return (ret);
1467c478bd9Sstevel@tonic-gate }
147