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
56590Syl194034 * Common Development and Distribution License (the "License").
66590Syl194034 * 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*12289SBo.Zhou@Sun.COM * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * This file contains functions to implement the partition menu commands.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate #include "global.h"
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include "partition.h"
330Sstevel@tonic-gate #include "menu_partition.h"
340Sstevel@tonic-gate #include "menu_command.h"
350Sstevel@tonic-gate #include "misc.h"
360Sstevel@tonic-gate #include "param.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate #ifdef __STDC__
390Sstevel@tonic-gate
400Sstevel@tonic-gate /* Function prototypes for ANSI C Compilers */
410Sstevel@tonic-gate static void nspaces(int);
420Sstevel@tonic-gate static int ndigits(uint64_t);
430Sstevel@tonic-gate
440Sstevel@tonic-gate #else /* __STDC__ */
450Sstevel@tonic-gate
460Sstevel@tonic-gate /* Function prototypes for non-ANSI C Compilers */
470Sstevel@tonic-gate static void nspaces();
480Sstevel@tonic-gate static int ndigits();
490Sstevel@tonic-gate
500Sstevel@tonic-gate #endif /* __STDC__ */
510Sstevel@tonic-gate
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate * This routine implements the 'a' command. It changes the 'a' partition.
540Sstevel@tonic-gate */
550Sstevel@tonic-gate int
p_apart()560Sstevel@tonic-gate p_apart()
570Sstevel@tonic-gate {
580Sstevel@tonic-gate
590Sstevel@tonic-gate change_partition(0);
600Sstevel@tonic-gate return (0);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * This routine implements the 'b' command. It changes the 'b' partition.
650Sstevel@tonic-gate */
660Sstevel@tonic-gate int
p_bpart()670Sstevel@tonic-gate p_bpart()
680Sstevel@tonic-gate {
690Sstevel@tonic-gate
700Sstevel@tonic-gate change_partition(1);
710Sstevel@tonic-gate return (0);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * This routine implements the 'c' command. It changes the 'c' partition.
760Sstevel@tonic-gate */
770Sstevel@tonic-gate int
p_cpart()780Sstevel@tonic-gate p_cpart()
790Sstevel@tonic-gate {
800Sstevel@tonic-gate
810Sstevel@tonic-gate change_partition(2);
820Sstevel@tonic-gate return (0);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * This routine implements the 'd' command. It changes the 'd' partition.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate int
p_dpart()890Sstevel@tonic-gate p_dpart()
900Sstevel@tonic-gate {
910Sstevel@tonic-gate
920Sstevel@tonic-gate change_partition(3);
930Sstevel@tonic-gate return (0);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate * This routine implements the 'e' command. It changes the 'e' partition.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate int
p_epart()1000Sstevel@tonic-gate p_epart()
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate change_partition(4);
1040Sstevel@tonic-gate return (0);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate * This routine implements the 'f' command. It changes the 'f' partition.
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate int
p_fpart()1110Sstevel@tonic-gate p_fpart()
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate change_partition(5);
1150Sstevel@tonic-gate return (0);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate * This routine implements the 'g' command. It changes the 'g' partition.
1200Sstevel@tonic-gate */
1210Sstevel@tonic-gate int
p_gpart()1220Sstevel@tonic-gate p_gpart()
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate change_partition(6);
1260Sstevel@tonic-gate return (0);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate * This routine implements the 'h' command. It changes the 'h' partition.
1310Sstevel@tonic-gate */
1320Sstevel@tonic-gate int
p_hpart()1330Sstevel@tonic-gate p_hpart()
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate change_partition(7);
1370Sstevel@tonic-gate return (0);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate * This routine implements the 'i' command. It is valid only for EFI
1420Sstevel@tonic-gate * labeled disks. This can be used only in expert mode.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate int
p_ipart()1450Sstevel@tonic-gate p_ipart()
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate change_partition(8);
1480Sstevel@tonic-gate return (0);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate #if defined(i386)
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate * This routine implements the 'j' command. It changes the 'j' partition.
1540Sstevel@tonic-gate */
1550Sstevel@tonic-gate int
p_jpart()1560Sstevel@tonic-gate p_jpart()
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate change_partition(9);
1600Sstevel@tonic-gate return (0);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate #endif /* defined(i386) */
1630Sstevel@tonic-gate
1646590Syl194034 int
p_expand()1656590Syl194034 p_expand()
1666590Syl194034 {
1676590Syl194034 uint64_t delta;
1686590Syl194034 uint_t nparts;
1696590Syl194034 struct dk_gpt *efi_label = cur_parts->etoc;
1706590Syl194034
1716590Syl194034 if (cur_parts->etoc->efi_altern_lba == 1 ||
1726590Syl194034 (cur_parts->etoc->efi_altern_lba >=
1736590Syl194034 cur_parts->etoc->efi_last_lba)) {
1746590Syl194034 err_print("Warning: No expanded capacity is found.\n");
1756590Syl194034 return (0);
1766590Syl194034 }
1776590Syl194034
1786590Syl194034 delta = efi_label->efi_last_lba - efi_label->efi_altern_lba;
1796590Syl194034 nparts = efi_label->efi_nparts;
1806590Syl194034
1816590Syl194034 enter_critical();
1826590Syl194034 efi_label->efi_parts[nparts - 1].p_start += delta;
1836590Syl194034 efi_label->efi_last_u_lba += delta;
1846590Syl194034 efi_label->efi_altern_lba = cur_parts->etoc->efi_last_lba;
1856590Syl194034 exit_critical();
1866590Syl194034
1876590Syl194034 fmt_print("The expanded capacity is added to the unallocated space.\n");
1886590Syl194034 return (0);
1896590Syl194034 }
1906590Syl194034
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate * This routine implements the 'select' command. It allows the user
1930Sstevel@tonic-gate * to make a pre-defined partition map the current map.
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate int
p_select()1960Sstevel@tonic-gate p_select()
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate struct partition_info *pptr, *parts;
1990Sstevel@tonic-gate u_ioparam_t ioparam;
2000Sstevel@tonic-gate int i, index, deflt, *defltptr = NULL;
2017563SPrasad.Singamsetty@Sun.COM blkaddr_t b_cylno;
2020Sstevel@tonic-gate #if defined(i386)
2037563SPrasad.Singamsetty@Sun.COM blkaddr_t cyl_offset;
2040Sstevel@tonic-gate #endif
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate parts = cur_dtype->dtype_plist;
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * If there are no pre-defined maps for this disk type, it's
2090Sstevel@tonic-gate * an error.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate if (parts == NULL) {
2120Sstevel@tonic-gate err_print("No defined partition tables.\n");
2130Sstevel@tonic-gate return (-1);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate * Loop through the pre-defined maps and list them by name. If
2180Sstevel@tonic-gate * the current map is one of them, make it the default. If any
2190Sstevel@tonic-gate * the maps are unnamed, label them as such.
2200Sstevel@tonic-gate */
2210Sstevel@tonic-gate for (i = 0, pptr = parts; pptr != NULL; pptr = pptr->pinfo_next) {
2220Sstevel@tonic-gate if (cur_parts == pptr) {
2230Sstevel@tonic-gate deflt = i;
2240Sstevel@tonic-gate defltptr = &deflt;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate if (pptr->pinfo_name == NULL)
2270Sstevel@tonic-gate fmt_print(" %d. unnamed\n", i++);
2280Sstevel@tonic-gate else
2290Sstevel@tonic-gate fmt_print(" %d. %s\n", i++, pptr->pinfo_name);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate ioparam.io_bounds.lower = 0;
2320Sstevel@tonic-gate ioparam.io_bounds.upper = i - 1;
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate * Ask which map should be made current.
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate index = input(FIO_INT, "Specify table (enter its number)", ':',
2370Sstevel@tonic-gate &ioparam, defltptr, DATA_INPUT);
2380Sstevel@tonic-gate for (i = 0, pptr = parts; i < index; i++, pptr = pptr->pinfo_next)
2390Sstevel@tonic-gate ;
2400Sstevel@tonic-gate if (cur_label == L_TYPE_EFI) {
2410Sstevel@tonic-gate enter_critical();
2420Sstevel@tonic-gate cur_disk->disk_parts = cur_parts = pptr;
2430Sstevel@tonic-gate exit_critical();
2440Sstevel@tonic-gate fmt_print("\n");
2450Sstevel@tonic-gate return (0);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate #if defined(i386)
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * Adjust for the boot and alternate sectors partition - assuming that
2500Sstevel@tonic-gate * the alternate sectors partition physical location follows
2510Sstevel@tonic-gate * immediately the boot partition and partition sizes are
2520Sstevel@tonic-gate * expressed in multiple of cylinder size.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate cyl_offset = pptr->pinfo_map[I_PARTITION].dkl_cylno + 1;
2550Sstevel@tonic-gate if (pptr->pinfo_map[J_PARTITION].dkl_nblk != 0) {
2560Sstevel@tonic-gate cyl_offset = pptr->pinfo_map[J_PARTITION].dkl_cylno +
2570Sstevel@tonic-gate ((pptr->pinfo_map[J_PARTITION].dkl_nblk +
2580Sstevel@tonic-gate (spc() - 1)) / spc());
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate #else /* !defined(i386) */
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate b_cylno = 0;
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate #endif /* defined(i386) */
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate * Before we blow the current map away, do some limits checking.
2680Sstevel@tonic-gate */
2690Sstevel@tonic-gate for (i = 0; i < NDKMAP; i++) {
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate #if defined(i386)
2720Sstevel@tonic-gate if (i == I_PARTITION || i == J_PARTITION || i == C_PARTITION) {
2730Sstevel@tonic-gate b_cylno = 0;
2740Sstevel@tonic-gate } else if (pptr->pinfo_map[i].dkl_nblk == 0) {
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate * Always accept starting cyl 0 if the size is 0 also
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate b_cylno = 0;
2790Sstevel@tonic-gate } else {
2800Sstevel@tonic-gate b_cylno = cyl_offset;
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate #endif /* defined(i386) */
2830Sstevel@tonic-gate if (pptr->pinfo_map[i].dkl_cylno < b_cylno ||
2840Sstevel@tonic-gate pptr->pinfo_map[i].dkl_cylno > (ncyl-1)) {
2850Sstevel@tonic-gate err_print(
2860Sstevel@tonic-gate "partition %c: starting cylinder %d is out of range\n",
2870Sstevel@tonic-gate (PARTITION_BASE+i),
2880Sstevel@tonic-gate pptr->pinfo_map[i].dkl_cylno);
2890Sstevel@tonic-gate return (0);
2900Sstevel@tonic-gate }
2919402SLarry.Liu@Sun.COM if (pptr->pinfo_map[i].dkl_nblk > ((ncyl -
2929402SLarry.Liu@Sun.COM pptr->pinfo_map[i].dkl_cylno) * spc())) {
2930Sstevel@tonic-gate err_print(
2947563SPrasad.Singamsetty@Sun.COM "partition %c: specified # of blocks, %u, "
2957563SPrasad.Singamsetty@Sun.COM "is out of range\n",
2967563SPrasad.Singamsetty@Sun.COM (PARTITION_BASE+i),
2977563SPrasad.Singamsetty@Sun.COM pptr->pinfo_map[i].dkl_nblk);
2980Sstevel@tonic-gate return (0);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate * Lock out interrupts so the lists don't get mangled.
3030Sstevel@tonic-gate */
3040Sstevel@tonic-gate enter_critical();
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate * If the old current map is unnamed, delete it.
3070Sstevel@tonic-gate */
3080Sstevel@tonic-gate if (cur_parts != NULL && cur_parts != pptr &&
3090Sstevel@tonic-gate cur_parts->pinfo_name == NULL)
3100Sstevel@tonic-gate delete_partition(cur_parts);
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate * Make the selected map current.
3130Sstevel@tonic-gate */
3140Sstevel@tonic-gate cur_disk->disk_parts = cur_parts = pptr;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
3170Sstevel@tonic-gate for (i = 0; i < NDKMAP; i++) {
3180Sstevel@tonic-gate cur_parts->vtoc.v_part[i].p_start =
3197563SPrasad.Singamsetty@Sun.COM (blkaddr_t)(cur_parts->pinfo_map[i].dkl_cylno *
3200Sstevel@tonic-gate (nhead * nsect));
3210Sstevel@tonic-gate cur_parts->vtoc.v_part[i].p_size =
3227563SPrasad.Singamsetty@Sun.COM (blkaddr_t)cur_parts->pinfo_map[i].dkl_nblk;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate exit_critical();
3270Sstevel@tonic-gate fmt_print("\n");
3280Sstevel@tonic-gate return (0);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate * This routine implements the 'name' command. It allows the user
3330Sstevel@tonic-gate * to name the current partition map. If the map was already named,
3340Sstevel@tonic-gate * the name is changed. Once a map is named, the values of the partitions
3350Sstevel@tonic-gate * cannot be changed. Attempts to change them will cause another map
3360Sstevel@tonic-gate * to be created.
3370Sstevel@tonic-gate */
3380Sstevel@tonic-gate int
p_name()3390Sstevel@tonic-gate p_name()
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate char *name;
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate * check if there exists a partition table for the disk.
3450Sstevel@tonic-gate */
3460Sstevel@tonic-gate if (cur_parts == NULL) {
3470Sstevel@tonic-gate err_print("Current Disk has no partition table.\n");
3480Sstevel@tonic-gate return (-1);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate * Ask for the name. Note that the input routine will malloc
3540Sstevel@tonic-gate * space for the name since we are using the OSTR input type.
3550Sstevel@tonic-gate */
356362Sbg159949 name = (char *)(uintptr_t)input(FIO_OSTR,
357362Sbg159949 "Enter table name (remember quotes)",
3580Sstevel@tonic-gate ':', (u_ioparam_t *)NULL, (int *)NULL, DATA_INPUT);
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate * Lock out interrupts.
3610Sstevel@tonic-gate */
3620Sstevel@tonic-gate enter_critical();
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate * If it was already named, destroy the old name.
3650Sstevel@tonic-gate */
3660Sstevel@tonic-gate if (cur_parts->pinfo_name != NULL)
3670Sstevel@tonic-gate destroy_data(cur_parts->pinfo_name);
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate * Set the name.
3700Sstevel@tonic-gate */
3710Sstevel@tonic-gate cur_parts->pinfo_name = name;
3720Sstevel@tonic-gate exit_critical();
3730Sstevel@tonic-gate fmt_print("\n");
3740Sstevel@tonic-gate return (0);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate * This routine implements the 'print' command. It lists the values
3800Sstevel@tonic-gate * for all the partitions in the current partition map.
3810Sstevel@tonic-gate */
3820Sstevel@tonic-gate int
p_print()3830Sstevel@tonic-gate p_print()
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate * check if there exists a partition table for the disk.
3870Sstevel@tonic-gate */
3880Sstevel@tonic-gate if (cur_parts == NULL) {
3890Sstevel@tonic-gate err_print("Current Disk has no partition table.\n");
3900Sstevel@tonic-gate return (-1);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate * Print the volume name, if it appears to be set
3950Sstevel@tonic-gate */
3960Sstevel@tonic-gate if (chk_volname(cur_disk)) {
3970Sstevel@tonic-gate fmt_print("Volume: ");
3980Sstevel@tonic-gate print_volname(cur_disk);
3990Sstevel@tonic-gate fmt_print("\n");
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate * Print the name of the current map.
4030Sstevel@tonic-gate */
4040Sstevel@tonic-gate if ((cur_parts->pinfo_name != NULL) && (cur_label == L_TYPE_SOLARIS)) {
4050Sstevel@tonic-gate fmt_print("Current partition table (%s):\n",
4060Sstevel@tonic-gate cur_parts->pinfo_name);
407*12289SBo.Zhou@Sun.COM fmt_print("Total disk cylinders available: %d + %d "
408*12289SBo.Zhou@Sun.COM "(reserved cylinders)\n\n", ncyl, acyl);
4090Sstevel@tonic-gate } else if (cur_label == L_TYPE_SOLARIS) {
4100Sstevel@tonic-gate fmt_print("Current partition table (unnamed):\n");
411*12289SBo.Zhou@Sun.COM fmt_print("Total disk cylinders available: %d + %d "
412*12289SBo.Zhou@Sun.COM "(reserved cylinders)\n\n", ncyl, acyl);
4130Sstevel@tonic-gate } else if (cur_label == L_TYPE_EFI) {
414*12289SBo.Zhou@Sun.COM fmt_print("Current partition table (%s):\n",
415*12289SBo.Zhou@Sun.COM cur_parts->pinfo_name != NULL ?
416*12289SBo.Zhou@Sun.COM cur_parts->pinfo_name : "unnamed");
417*12289SBo.Zhou@Sun.COM fmt_print("Total disk sectors available: %llu + %d "
418*12289SBo.Zhou@Sun.COM "(reserved sectors)\n\n",
419*12289SBo.Zhou@Sun.COM cur_parts->etoc->efi_last_u_lba - EFI_MIN_RESV_SIZE -
420*12289SBo.Zhou@Sun.COM cur_parts->etoc->efi_first_u_lba + 1, EFI_MIN_RESV_SIZE);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate * Print the partition map itself
4260Sstevel@tonic-gate */
4270Sstevel@tonic-gate print_map(cur_parts);
4280Sstevel@tonic-gate return (0);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate * Print a partition map
4340Sstevel@tonic-gate */
4350Sstevel@tonic-gate void
print_map(struct partition_info * map)4360Sstevel@tonic-gate print_map(struct partition_info *map)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate int i;
4390Sstevel@tonic-gate int want_header;
4400Sstevel@tonic-gate struct dk_gpt *vtoc64;
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate if (cur_label == L_TYPE_EFI) {
4430Sstevel@tonic-gate vtoc64 = map->etoc;
4440Sstevel@tonic-gate want_header = 1;
4450Sstevel@tonic-gate for (i = 0; i < vtoc64->efi_nparts; i++) {
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate * we want to print partitions above 7 in expert mode only
4480Sstevel@tonic-gate * or if the partition is reserved
4490Sstevel@tonic-gate */
4500Sstevel@tonic-gate if (i >= 7 && !expert_mode &&
4510Sstevel@tonic-gate ((int)vtoc64->efi_parts[i].p_tag !=
4520Sstevel@tonic-gate V_RESERVED)) {
4530Sstevel@tonic-gate continue;
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate print_efi_partition(vtoc64, i, want_header);
4570Sstevel@tonic-gate want_header = 0;
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate fmt_print("\n");
4600Sstevel@tonic-gate return;
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate * Loop through each partition, printing the header
4640Sstevel@tonic-gate * the first time.
4650Sstevel@tonic-gate */
4660Sstevel@tonic-gate want_header = 1;
4670Sstevel@tonic-gate for (i = 0; i < NDKMAP; i++) {
4680Sstevel@tonic-gate if (i > 9) {
4690Sstevel@tonic-gate break;
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate print_partition(map, i, want_header);
4720Sstevel@tonic-gate want_header = 0;
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate fmt_print("\n");
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate * Print out one line of partition information,
4800Sstevel@tonic-gate * with optional header for EFI type disks.
4810Sstevel@tonic-gate */
4820Sstevel@tonic-gate /*ARGSUSED*/
4830Sstevel@tonic-gate void
print_efi_partition(struct dk_gpt * map,int partnum,int want_header)4840Sstevel@tonic-gate print_efi_partition(struct dk_gpt *map, int partnum, int want_header)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate int ncyl2_digits = 0;
4870Sstevel@tonic-gate float scaled;
4880Sstevel@tonic-gate char *s;
4890Sstevel@tonic-gate uint64_t secsize;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate ncyl2_digits = ndigits(map->efi_last_u_lba);
4920Sstevel@tonic-gate if (want_header) {
4930Sstevel@tonic-gate fmt_print("Part ");
4940Sstevel@tonic-gate fmt_print("Tag Flag ");
4950Sstevel@tonic-gate fmt_print("First Sector");
4960Sstevel@tonic-gate nspaces(ncyl2_digits);
4970Sstevel@tonic-gate fmt_print("Size");
4980Sstevel@tonic-gate nspaces(ncyl2_digits);
4990Sstevel@tonic-gate fmt_print("Last Sector\n");
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate fmt_print(" %d ", partnum);
5030Sstevel@tonic-gate s = find_string(ptag_choices,
5040Sstevel@tonic-gate (int)map->efi_parts[partnum].p_tag);
5050Sstevel@tonic-gate if (s == (char *)NULL)
5060Sstevel@tonic-gate s = "-";
5070Sstevel@tonic-gate nspaces(10 - (int)strlen(s));
5080Sstevel@tonic-gate fmt_print("%s", s);
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate s = find_string(pflag_choices,
5110Sstevel@tonic-gate (int)map->efi_parts[partnum].p_flag);
5120Sstevel@tonic-gate if (s == (char *)NULL)
5130Sstevel@tonic-gate s = "-";
5140Sstevel@tonic-gate nspaces(6 - (int)strlen(s));
5150Sstevel@tonic-gate fmt_print("%s", s);
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate nspaces(2);
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate secsize = map->efi_parts[partnum].p_size;
5200Sstevel@tonic-gate if (secsize == 0) {
5210Sstevel@tonic-gate fmt_print("%16llu", map->efi_parts[partnum].p_start);
5220Sstevel@tonic-gate nspaces(ncyl2_digits);
5230Sstevel@tonic-gate fmt_print(" 0 ");
5240Sstevel@tonic-gate } else {
5250Sstevel@tonic-gate fmt_print("%16llu", map->efi_parts[partnum].p_start);
5260Sstevel@tonic-gate scaled = bn2mb(secsize);
5270Sstevel@tonic-gate nspaces(ncyl2_digits - 5);
5280Sstevel@tonic-gate if (scaled >= (float)1024.0 * 1024) {
5290Sstevel@tonic-gate fmt_print("%8.2fTB", scaled/((float)1024.0 * 1024));
5300Sstevel@tonic-gate } else if (scaled >= (float)1024.0) {
5310Sstevel@tonic-gate fmt_print("%8.2fGB", scaled/(float)1024.0);
5320Sstevel@tonic-gate } else {
5330Sstevel@tonic-gate fmt_print("%8.2fMB", scaled);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate nspaces(ncyl2_digits);
5370Sstevel@tonic-gate if ((map->efi_parts[partnum].p_start+secsize - 1) ==
5380Sstevel@tonic-gate UINT_MAX64) {
5390Sstevel@tonic-gate fmt_print(" 0 \n");
5400Sstevel@tonic-gate } else {
5410Sstevel@tonic-gate fmt_print(" %llu \n",
5420Sstevel@tonic-gate map->efi_parts[partnum].p_start+secsize - 1);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate /*
5470Sstevel@tonic-gate * Print out one line of partition information,
5480Sstevel@tonic-gate * with optional header.
5490Sstevel@tonic-gate */
5500Sstevel@tonic-gate /*ARGSUSED*/
5510Sstevel@tonic-gate void
print_partition(struct partition_info * pinfo,int partnum,int want_header)5520Sstevel@tonic-gate print_partition(struct partition_info *pinfo, int partnum, int want_header)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate int i;
5557563SPrasad.Singamsetty@Sun.COM blkaddr_t nblks;
5560Sstevel@tonic-gate int cyl1;
5570Sstevel@tonic-gate int cyl2;
5580Sstevel@tonic-gate float scaled;
5590Sstevel@tonic-gate int maxcyl2;
5600Sstevel@tonic-gate int ncyl2_digits;
5610Sstevel@tonic-gate char *s;
5627563SPrasad.Singamsetty@Sun.COM blkaddr_t maxnblks = 0;
5637563SPrasad.Singamsetty@Sun.COM blkaddr_t len;
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate * To align things nicely, we need to know the maximum
5670Sstevel@tonic-gate * width of the number of cylinders field.
5680Sstevel@tonic-gate */
5690Sstevel@tonic-gate maxcyl2 = 0;
5700Sstevel@tonic-gate for (i = 0; i < NDKMAP; i++) {
5710Sstevel@tonic-gate nblks = (uint_t)pinfo->pinfo_map[i].dkl_nblk;
5720Sstevel@tonic-gate cyl1 = pinfo->pinfo_map[i].dkl_cylno;
5730Sstevel@tonic-gate cyl2 = cyl1 + (nblks / spc()) - 1;
5740Sstevel@tonic-gate if (nblks > 0) {
5750Sstevel@tonic-gate maxcyl2 = max(cyl2, maxcyl2);
5760Sstevel@tonic-gate maxnblks = max(nblks, maxnblks);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate /*
5800Sstevel@tonic-gate * Get the number of digits required
5810Sstevel@tonic-gate */
5820Sstevel@tonic-gate ncyl2_digits = ndigits(maxcyl2);
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate * Print the header, if necessary
5860Sstevel@tonic-gate */
5870Sstevel@tonic-gate if (want_header) {
5880Sstevel@tonic-gate fmt_print("Part ");
5890Sstevel@tonic-gate fmt_print("Tag Flag ");
5900Sstevel@tonic-gate fmt_print("Cylinders");
5910Sstevel@tonic-gate nspaces(ncyl2_digits);
5920Sstevel@tonic-gate fmt_print(" Size Blocks\n");
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate * Print the partition information
5970Sstevel@tonic-gate */
5980Sstevel@tonic-gate nblks = pinfo->pinfo_map[partnum].dkl_nblk;
5990Sstevel@tonic-gate cyl1 = pinfo->pinfo_map[partnum].dkl_cylno;
6000Sstevel@tonic-gate cyl2 = cyl1 + (nblks / spc()) - 1;
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate fmt_print(" %x ", partnum);
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate * Print the partition tag. If invalid, print -
6060Sstevel@tonic-gate */
6070Sstevel@tonic-gate s = find_string(ptag_choices,
6080Sstevel@tonic-gate (int)pinfo->vtoc.v_part[partnum].p_tag);
6090Sstevel@tonic-gate if (s == (char *)NULL)
6100Sstevel@tonic-gate s = "-";
6110Sstevel@tonic-gate nspaces(10 - (int)strlen(s));
6120Sstevel@tonic-gate fmt_print("%s", s);
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate * Print the partition flag. If invalid print -
6160Sstevel@tonic-gate */
6170Sstevel@tonic-gate s = find_string(pflag_choices,
6180Sstevel@tonic-gate (int)pinfo->vtoc.v_part[partnum].p_flag);
6190Sstevel@tonic-gate if (s == (char *)NULL)
6200Sstevel@tonic-gate s = "-";
6210Sstevel@tonic-gate nspaces(6 - (int)strlen(s));
6220Sstevel@tonic-gate fmt_print("%s", s);
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate nspaces(2);
6250Sstevel@tonic-gate
6260Sstevel@tonic-gate if (nblks == 0) {
6270Sstevel@tonic-gate fmt_print("%6d ", cyl1);
6280Sstevel@tonic-gate nspaces(ncyl2_digits);
6290Sstevel@tonic-gate fmt_print(" 0 ");
6300Sstevel@tonic-gate } else {
6310Sstevel@tonic-gate fmt_print("%6d - ", cyl1);
6320Sstevel@tonic-gate nspaces(ncyl2_digits - ndigits(cyl2));
6330Sstevel@tonic-gate fmt_print("%d ", cyl2);
6340Sstevel@tonic-gate scaled = bn2mb(nblks);
6350Sstevel@tonic-gate if (scaled > (float)1024.0 * 1024.0) {
6360Sstevel@tonic-gate fmt_print("%8.2fTB ",
6370Sstevel@tonic-gate scaled/((float)1024.0 * 1024.0));
6380Sstevel@tonic-gate } else if (scaled > (float)1024.0) {
6390Sstevel@tonic-gate fmt_print("%8.2fGB ", scaled/(float)1024.0);
6400Sstevel@tonic-gate } else {
6410Sstevel@tonic-gate fmt_print("%8.2fMB ", scaled);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate fmt_print("(");
6450Sstevel@tonic-gate pr_dblock(fmt_print, nblks);
6460Sstevel@tonic-gate fmt_print(")");
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate nspaces(ndigits(maxnblks/spc()) - ndigits(nblks/spc()));
6490Sstevel@tonic-gate /*
6500Sstevel@tonic-gate * Allocates size of the printf format string.
6510Sstevel@tonic-gate * ndigits(ndigits(maxblks)) gives the byte size of
6520Sstevel@tonic-gate * the printf width field for maxnblks.
6530Sstevel@tonic-gate */
6540Sstevel@tonic-gate len = strlen(" %") + ndigits(ndigits(maxnblks)) + strlen("d\n") + 1;
6550Sstevel@tonic-gate s = zalloc(len);
6567563SPrasad.Singamsetty@Sun.COM (void) snprintf(s, len, "%s%u%s", " %", ndigits(maxnblks), "u\n");
6570Sstevel@tonic-gate fmt_print(s, nblks);
6580Sstevel@tonic-gate (void) free(s);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate /*
6630Sstevel@tonic-gate * Return true if a disk has a volume name
6640Sstevel@tonic-gate */
6650Sstevel@tonic-gate int
chk_volname(disk)6660Sstevel@tonic-gate chk_volname(disk)
6670Sstevel@tonic-gate struct disk_info *disk;
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate return (disk->v_volume[0] != 0);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate * Print the volume name, if it appears to be set
6750Sstevel@tonic-gate */
6760Sstevel@tonic-gate void
print_volname(disk)6770Sstevel@tonic-gate print_volname(disk)
6780Sstevel@tonic-gate struct disk_info *disk;
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate int i;
6810Sstevel@tonic-gate char *p;
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate p = disk->v_volume;
6840Sstevel@tonic-gate for (i = 0; i < LEN_DKL_VVOL; i++, p++) {
6850Sstevel@tonic-gate if (*p == 0)
6860Sstevel@tonic-gate break;
6870Sstevel@tonic-gate fmt_print("%c", *p);
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate * Print a number of spaces
6940Sstevel@tonic-gate */
6950Sstevel@tonic-gate static void
nspaces(n)6960Sstevel@tonic-gate nspaces(n)
6970Sstevel@tonic-gate int n;
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate while (n-- > 0)
7000Sstevel@tonic-gate fmt_print(" ");
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate * Return the number of digits required to print a number
7050Sstevel@tonic-gate */
7060Sstevel@tonic-gate static int
ndigits(n)7070Sstevel@tonic-gate ndigits(n)
7080Sstevel@tonic-gate uint64_t n;
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate int i;
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate i = 0;
7130Sstevel@tonic-gate while (n > 0) {
7140Sstevel@tonic-gate n /= 10;
7150Sstevel@tonic-gate i++;
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate return (i == 0 ? 1 : i);
7190Sstevel@tonic-gate }
720