10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57563SPrasad.Singamsetty@Sun.COM * Common Development and Distribution License (the "License").
67563SPrasad.Singamsetty@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*9889SLarry.Liu@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23767Ssjelinek * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * This file contains functions that implement the fdisk menu commands.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate #include "global.h"
300Sstevel@tonic-gate #include <sys/time.h>
310Sstevel@tonic-gate #include <sys/resource.h>
320Sstevel@tonic-gate #include <sys/wait.h>
330Sstevel@tonic-gate #include <signal.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <sys/fcntl.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <sys/dklabel.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include "main.h"
430Sstevel@tonic-gate #include "analyze.h"
440Sstevel@tonic-gate #include "menu.h"
450Sstevel@tonic-gate #include "menu_command.h"
460Sstevel@tonic-gate #include "menu_defect.h"
470Sstevel@tonic-gate #include "menu_partition.h"
480Sstevel@tonic-gate #if defined(_FIRMWARE_NEEDS_FDISK)
490Sstevel@tonic-gate #include "menu_fdisk.h"
500Sstevel@tonic-gate #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
510Sstevel@tonic-gate #include "param.h"
520Sstevel@tonic-gate #include "misc.h"
530Sstevel@tonic-gate #include "label.h"
540Sstevel@tonic-gate #include "startup.h"
550Sstevel@tonic-gate #include "partition.h"
560Sstevel@tonic-gate #include "prompts.h"
57767Ssjelinek #include "checkdev.h"
580Sstevel@tonic-gate #include "io.h"
590Sstevel@tonic-gate #include "ctlr_scsi.h"
600Sstevel@tonic-gate #include "auto_sense.h"
610Sstevel@tonic-gate
620Sstevel@tonic-gate #ifdef __STDC__
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * Local prototypes for ANSI C compilers
650Sstevel@tonic-gate */
660Sstevel@tonic-gate static int generic_ck_format(void);
677563SPrasad.Singamsetty@Sun.COM static int generic_rdwr(int dir, int fd, diskaddr_t blkno, int secnt,
680Sstevel@tonic-gate caddr_t bufaddr, int flags, int *xfercntp);
690Sstevel@tonic-gate #else /* __STDC__ */
700Sstevel@tonic-gate
710Sstevel@tonic-gate static int generic_ck_format();
720Sstevel@tonic-gate static int generic_rdwr();
730Sstevel@tonic-gate
740Sstevel@tonic-gate #endif /* __STDC__ */
750Sstevel@tonic-gate
760Sstevel@tonic-gate struct ctlr_ops genericops = {
770Sstevel@tonic-gate generic_rdwr,
780Sstevel@tonic-gate generic_ck_format,
790Sstevel@tonic-gate 0,
800Sstevel@tonic-gate 0,
810Sstevel@tonic-gate 0,
820Sstevel@tonic-gate 0,
830Sstevel@tonic-gate 0,
840Sstevel@tonic-gate };
850Sstevel@tonic-gate
860Sstevel@tonic-gate
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate * Check to see if the disk has been formatted.
890Sstevel@tonic-gate * If we are able to read the first track, we conclude that
900Sstevel@tonic-gate * the disk has been formatted.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate static int
generic_ck_format()930Sstevel@tonic-gate generic_ck_format()
940Sstevel@tonic-gate {
950Sstevel@tonic-gate int status;
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * Try to read the first four blocks.
990Sstevel@tonic-gate */
1000Sstevel@tonic-gate status = generic_rdwr(DIR_READ, cur_file, 0, 4, (caddr_t)cur_buf,
1017563SPrasad.Singamsetty@Sun.COM F_SILENT, NULL);
1020Sstevel@tonic-gate return (!status);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate * Read or write the disk.
1070Sstevel@tonic-gate * Temporary interface until IOCTL interface finished.
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate /*ARGSUSED*/
1100Sstevel@tonic-gate static int
generic_rdwr(dir,fd,blkno,secnt,bufaddr,flags,xfercntp)1110Sstevel@tonic-gate generic_rdwr(dir, fd, blkno, secnt, bufaddr, flags, xfercntp)
1120Sstevel@tonic-gate int dir;
1130Sstevel@tonic-gate int fd;
1147563SPrasad.Singamsetty@Sun.COM diskaddr_t blkno;
1150Sstevel@tonic-gate int secnt;
1160Sstevel@tonic-gate caddr_t bufaddr;
1170Sstevel@tonic-gate int flags;
1180Sstevel@tonic-gate int *xfercntp;
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate
1217563SPrasad.Singamsetty@Sun.COM offset_t tmpsec, status, tmpblk;
1227563SPrasad.Singamsetty@Sun.COM int ret;
1230Sstevel@tonic-gate
124*9889SLarry.Liu@Sun.COM tmpsec = (offset_t)secnt * cur_blksz;
125*9889SLarry.Liu@Sun.COM tmpblk = (offset_t)blkno * cur_blksz;
1260Sstevel@tonic-gate
1277563SPrasad.Singamsetty@Sun.COM #if defined(_FIRMWARE_NEEDS_FDISK)
1287563SPrasad.Singamsetty@Sun.COM /* Use "p0" file to seek/read the data */
1297563SPrasad.Singamsetty@Sun.COM (void) open_cur_file(FD_USE_P0_PATH);
1307563SPrasad.Singamsetty@Sun.COM #endif
1310Sstevel@tonic-gate if (dir == DIR_READ) {
1327563SPrasad.Singamsetty@Sun.COM status = llseek(fd, tmpblk, SEEK_SET);
1337563SPrasad.Singamsetty@Sun.COM if (status != tmpblk) {
1347563SPrasad.Singamsetty@Sun.COM ret = (int)status;
1357563SPrasad.Singamsetty@Sun.COM goto out;
1367563SPrasad.Singamsetty@Sun.COM }
1370Sstevel@tonic-gate
1387563SPrasad.Singamsetty@Sun.COM status = read(fd, bufaddr, (size_t)tmpsec);
1390Sstevel@tonic-gate if (status != tmpsec)
1407563SPrasad.Singamsetty@Sun.COM ret = (int)tmpsec;
1410Sstevel@tonic-gate else
1427563SPrasad.Singamsetty@Sun.COM ret = 0;
1430Sstevel@tonic-gate } else {
1447563SPrasad.Singamsetty@Sun.COM status = llseek(fd, tmpblk, SEEK_SET);
1457563SPrasad.Singamsetty@Sun.COM if (status != tmpblk) {
1467563SPrasad.Singamsetty@Sun.COM ret = (int)status;
1477563SPrasad.Singamsetty@Sun.COM goto out;
1487563SPrasad.Singamsetty@Sun.COM }
1490Sstevel@tonic-gate
1507563SPrasad.Singamsetty@Sun.COM status = write(fd, bufaddr, (size_t)tmpsec);
1510Sstevel@tonic-gate if (status != tmpsec)
1527563SPrasad.Singamsetty@Sun.COM ret = (int)tmpsec;
1530Sstevel@tonic-gate else
1547563SPrasad.Singamsetty@Sun.COM ret = 0;
1550Sstevel@tonic-gate }
1567563SPrasad.Singamsetty@Sun.COM out:
1577563SPrasad.Singamsetty@Sun.COM #if defined(_FIRMWARE_NEEDS_FDISK)
1587563SPrasad.Singamsetty@Sun.COM /* Restore cur_file with cur_disk->disk_path */
1597563SPrasad.Singamsetty@Sun.COM (void) open_cur_file(FD_USE_CUR_DISK_PATH);
1607563SPrasad.Singamsetty@Sun.COM #endif
1617563SPrasad.Singamsetty@Sun.COM return (ret);
1620Sstevel@tonic-gate }
163