xref: /illumos-gate/usr/src/cmd/format/partition.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  */
2123a1cceaSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
2448bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This file contains functions that operate on partition tables.
297c478bd9Sstevel@tonic-gate  */
3023a1cceaSRoger A. Faulkner #include <string.h>
3123a1cceaSRoger A. Faulkner #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include "global.h"
337c478bd9Sstevel@tonic-gate #include "partition.h"
347c478bd9Sstevel@tonic-gate #include "misc.h"
357c478bd9Sstevel@tonic-gate #include "menu_command.h"
367c478bd9Sstevel@tonic-gate #include "menu_partition.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Default vtoc information for non-SVr4 partitions
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate struct dk_map2	default_vtoc_map[NDKMAP] = {
437c478bd9Sstevel@tonic-gate 	{	V_ROOT,		0	},		/* a - 0 */
447c478bd9Sstevel@tonic-gate 	{	V_SWAP,		V_UNMNT	},		/* b - 1 */
457c478bd9Sstevel@tonic-gate 	{	V_BACKUP,	V_UNMNT	},		/* c - 2 */
467c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* d - 3 */
477c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* e - 4 */
487c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* f - 5 */
497c478bd9Sstevel@tonic-gate 	{	V_USR,		0	},		/* g - 6 */
507c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* h - 7 */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #if defined(i386)
557c478bd9Sstevel@tonic-gate 	{	V_BOOT,		V_UNMNT	},		/* i - 8 */
567c478bd9Sstevel@tonic-gate 	{	V_ALTSCTR,	0	},		/* j - 9 */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #else
597c478bd9Sstevel@tonic-gate #error No VTOC format defined.
607c478bd9Sstevel@tonic-gate #endif			/* defined(i386) */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* k - 10 */
637c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* l - 11 */
647c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* m - 12 */
657c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* n - 13 */
667c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* o - 14 */
677c478bd9Sstevel@tonic-gate 	{	V_UNASSIGNED,	0	},		/* p - 15 */
687c478bd9Sstevel@tonic-gate #endif			/* defined(_SUNOS_VTOC_16) */
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * This routine finds the last usable sector in the partition table.
737c478bd9Sstevel@tonic-gate  * It skips the BACKUP partition.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static uint64_t
maxofN(struct dk_gpt * map)767c478bd9Sstevel@tonic-gate maxofN(struct dk_gpt *map)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	uint64_t	max;
797c478bd9Sstevel@tonic-gate 	uint64_t	sec_no[2], start[2], size[2];
807c478bd9Sstevel@tonic-gate 	int		i;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	for (i = 0; i < map->efi_nparts - 1; i++) {
83edbad4feSToomas Soome 		start[0] = map->efi_parts[i].p_start;
84edbad4feSToomas Soome 		size[0] = map->efi_parts[i].p_size;
85edbad4feSToomas Soome 		sec_no[0] = start[0] + size[0];
86edbad4feSToomas Soome 
87edbad4feSToomas Soome 		start[1] = map->efi_parts[i + 1].p_start;
88edbad4feSToomas Soome 		size[1] = map->efi_parts[i + 1].p_size;
89edbad4feSToomas Soome 		sec_no[1] = start[1] + size[1];
90edbad4feSToomas Soome 
91edbad4feSToomas Soome 		if (map->efi_parts[i].p_tag == V_BACKUP) {
92edbad4feSToomas Soome 			sec_no[0] = 0;
93edbad4feSToomas Soome 		}
94edbad4feSToomas Soome 		if (map->efi_parts[i+1].p_tag == V_BACKUP) {
95edbad4feSToomas Soome 			sec_no[1] = 0;
96edbad4feSToomas Soome 		}
97edbad4feSToomas Soome 		if (i == 0) {
98edbad4feSToomas Soome 			max = sec_no[1];
99edbad4feSToomas Soome 		}
100edbad4feSToomas Soome 		if (sec_no[0] > max) {
101edbad4feSToomas Soome 			max = sec_no[0];
102edbad4feSToomas Soome 		} else {
103edbad4feSToomas Soome 			max = max;
104edbad4feSToomas Soome 		}
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	if (max == 0)
107edbad4feSToomas Soome 		max = map->efi_first_u_lba;
1087c478bd9Sstevel@tonic-gate 	return (max);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * This routine allows the user to change the boundaries of the given
1137c478bd9Sstevel@tonic-gate  * partition in the current partition map.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate void
change_partition(int num)1167c478bd9Sstevel@tonic-gate change_partition(int num)
1177c478bd9Sstevel@tonic-gate {
118342440ecSPrasad Singamsetty 	uint_t		i;
1197c478bd9Sstevel@tonic-gate 	uint64_t	i64, j64;
120342440ecSPrasad Singamsetty 	uint_t		j;
1217c478bd9Sstevel@tonic-gate 	int		deflt;
1227c478bd9Sstevel@tonic-gate 	part_deflt_t	p_deflt;
1237c478bd9Sstevel@tonic-gate 	u_ioparam_t	ioparam;
1247c478bd9Sstevel@tonic-gate 	int		tag;
1257c478bd9Sstevel@tonic-gate 	int		flag;
1267c478bd9Sstevel@tonic-gate 	char		msg[256];
127342440ecSPrasad Singamsetty 	blkaddr32_t	cyl_offset = 0;
1287c478bd9Sstevel@tonic-gate 	efi_deflt_t	efi_deflt;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * check if there exists a partition table for the disk.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	if (cur_parts == NULL) {
1347c478bd9Sstevel@tonic-gate 		err_print("Current Disk has no partition table.\n");
1357c478bd9Sstevel@tonic-gate 		return;
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (cur_label == L_TYPE_EFI) {
139edbad4feSToomas Soome 		if (num > cur_parts->etoc->efi_nparts - 1) {
140edbad4feSToomas Soome 			err_print("Invalid partition for EFI label\n");
141edbad4feSToomas Soome 			return;
142edbad4feSToomas Soome 		}
143edbad4feSToomas Soome 		print_efi_partition(cur_parts->etoc, num, 1);
144edbad4feSToomas Soome 		fmt_print("\n");
1457c478bd9Sstevel@tonic-gate 		/*
1467c478bd9Sstevel@tonic-gate 		 * Prompt for p_tag and p_flag values for this partition
1477c478bd9Sstevel@tonic-gate 		 */
148edbad4feSToomas Soome 		deflt = cur_parts->etoc->efi_parts[num].p_tag;
149edbad4feSToomas Soome 		if (deflt == V_UNASSIGNED) {
150edbad4feSToomas Soome 			deflt = V_USR;
151edbad4feSToomas Soome 		}
152edbad4feSToomas Soome 		(void) sprintf(msg, "Enter partition id tag");
153edbad4feSToomas Soome 		ioparam.io_slist = ptag_choices;
154edbad4feSToomas Soome 		tag = input(FIO_SLIST, msg, ':', &ioparam, &deflt, DATA_INPUT);
155edbad4feSToomas Soome 
156edbad4feSToomas Soome 		deflt = cur_parts->etoc->efi_parts[num].p_flag;
157edbad4feSToomas Soome 		(void) sprintf(msg, "Enter partition permission flags");
158edbad4feSToomas Soome 		ioparam.io_slist = pflag_choices;
159edbad4feSToomas Soome 		flag = input(FIO_SLIST, msg, ':', &ioparam, &deflt, DATA_INPUT);
160edbad4feSToomas Soome 
161edbad4feSToomas Soome 		ioparam.io_bounds.lower = cur_parts->etoc->efi_first_u_lba;
162edbad4feSToomas Soome 		ioparam.io_bounds.upper = cur_parts->etoc->efi_last_u_lba;
163edbad4feSToomas Soome 
164edbad4feSToomas Soome 		efi_deflt.start_sector = maxofN(cur_parts->etoc);
165edbad4feSToomas Soome 		if ((cur_parts->etoc->efi_parts[num].p_start != 0) &&
166edbad4feSToomas Soome 		    (cur_parts->etoc->efi_parts[num].p_size != 0)) {
167edbad4feSToomas Soome 			efi_deflt.start_sector =
168edbad4feSToomas Soome 			    cur_parts->etoc->efi_parts[num].p_start;
169edbad4feSToomas Soome 		}
170edbad4feSToomas Soome 		efi_deflt.end_sector = ioparam.io_bounds.upper -
171edbad4feSToomas Soome 		    efi_deflt.start_sector;
172edbad4feSToomas Soome 		i64 = input(FIO_INT64, "Enter new starting Sector", ':',
173edbad4feSToomas Soome 		    &ioparam, (int *)&efi_deflt, DATA_INPUT);
174edbad4feSToomas Soome 
175edbad4feSToomas Soome 		ioparam.io_bounds.lower = 0;
176edbad4feSToomas Soome 		ioparam.io_bounds.upper = cur_parts->etoc->efi_last_u_lba;
177edbad4feSToomas Soome 		efi_deflt.end_sector = cur_parts->etoc->efi_parts[num].p_size;
178edbad4feSToomas Soome 		efi_deflt.start_sector = i64;
179edbad4feSToomas Soome 		j64 = input(FIO_EFI, "Enter partition size", ':', &ioparam,
180edbad4feSToomas Soome 		    (int *)&efi_deflt, DATA_INPUT);
181edbad4feSToomas Soome 		if (j64 == 0) {
182edbad4feSToomas Soome 			tag = V_UNASSIGNED;
183edbad4feSToomas Soome 			i64 = 0;
184edbad4feSToomas Soome 		} else if ((j64 != 0) && (tag == V_UNASSIGNED)) {
185edbad4feSToomas Soome 			tag = V_USR;
186edbad4feSToomas Soome 		}
1877c478bd9Sstevel@tonic-gate 
188edbad4feSToomas Soome 		if (cur_parts->pinfo_name != NULL)
189edbad4feSToomas Soome 			make_partition();
1907c478bd9Sstevel@tonic-gate 
191edbad4feSToomas Soome 		cur_parts->etoc->efi_parts[num].p_tag = tag;
192edbad4feSToomas Soome 		cur_parts->etoc->efi_parts[num].p_flag = flag;
193edbad4feSToomas Soome 		cur_parts->etoc->efi_parts[num].p_start = i64;
194edbad4feSToomas Soome 		cur_parts->etoc->efi_parts[num].p_size = j64;
195edbad4feSToomas Soome 		/*
196edbad4feSToomas Soome 		 * We are now done with EFI part, so return now
197edbad4feSToomas Soome 		 */
198edbad4feSToomas Soome 		return;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	/*
20148bbca81SDaniel Hoffman 	 * Print out the given partition so the user knows what they're
2027c478bd9Sstevel@tonic-gate 	 * getting into.
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	print_partition(cur_parts, num, 1);
2057c478bd9Sstevel@tonic-gate 	fmt_print("\n");
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/*
2087c478bd9Sstevel@tonic-gate 	 * Prompt for p_tag and p_flag values for this partition.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	assert(cur_parts->vtoc.v_version == V_VERSION);
2117c478bd9Sstevel@tonic-gate 	deflt = cur_parts->vtoc.v_part[num].p_tag;
2127c478bd9Sstevel@tonic-gate 	(void) sprintf(msg, "Enter partition id tag");
2137c478bd9Sstevel@tonic-gate 	ioparam.io_slist = ptag_choices;
2147c478bd9Sstevel@tonic-gate 	tag = input(FIO_SLIST, msg, ':', &ioparam, &deflt, DATA_INPUT);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	deflt = cur_parts->vtoc.v_part[num].p_flag;
2177c478bd9Sstevel@tonic-gate 	(void) sprintf(msg, "Enter partition permission flags");
2187c478bd9Sstevel@tonic-gate 	ioparam.io_slist = pflag_choices;
2197c478bd9Sstevel@tonic-gate 	flag = input(FIO_SLIST, msg, ':', &ioparam, &deflt, DATA_INPUT);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/*
2227c478bd9Sstevel@tonic-gate 	 * Ask for the new values.  The old values are the defaults, and
2237c478bd9Sstevel@tonic-gate 	 * strict bounds checking is done on the values given.
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate #if defined(i386)
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (tag != V_UNASSIGNED && tag != V_BACKUP && tag != V_BOOT) {
2297c478bd9Sstevel@tonic-gate 		/*
2307c478bd9Sstevel@tonic-gate 		 * Determine cyl offset for boot and alternate partitions.
2317c478bd9Sstevel@tonic-gate 		 * Assuming that the alternate sectors partition (slice)
2327c478bd9Sstevel@tonic-gate 		 * physical location immediately follows the boot
2337c478bd9Sstevel@tonic-gate 		 * partition and partition sizes are expressed in multiples
2347c478bd9Sstevel@tonic-gate 		 * of cylinder size.
2357c478bd9Sstevel@tonic-gate 		 */
2367c478bd9Sstevel@tonic-gate 		cyl_offset = cur_parts->pinfo_map[I_PARTITION].dkl_cylno + 1;
2377c478bd9Sstevel@tonic-gate 		if (tag != V_ALTSCTR) {
2387c478bd9Sstevel@tonic-gate 			if (cur_parts->pinfo_map[J_PARTITION].dkl_nblk != 0) {
2397c478bd9Sstevel@tonic-gate 				cyl_offset =
240edbad4feSToomas Soome 				    cur_parts->
241edbad4feSToomas Soome 				    pinfo_map[J_PARTITION].dkl_cylno +
242edbad4feSToomas Soome 				    ((cur_parts->
243edbad4feSToomas Soome 				    pinfo_map[J_PARTITION].dkl_nblk +
244edbad4feSToomas Soome 				    (spc() - 1)) / spc());
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate #endif	/* defined(i386) */
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	ioparam.io_bounds.lower = 0;
2517c478bd9Sstevel@tonic-gate 	ioparam.io_bounds.upper = ncyl - 1;
252edbad4feSToomas Soome 	deflt = max(cur_parts->pinfo_map[num].dkl_cylno, cyl_offset);
253342440ecSPrasad Singamsetty 	i = (uint_t)input(FIO_INT, "Enter new starting cyl", ':', &ioparam,
2547c478bd9Sstevel@tonic-gate 	    &deflt, DATA_INPUT);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	ioparam.io_bounds.lower = 0;
2577c478bd9Sstevel@tonic-gate 	ioparam.io_bounds.upper = (ncyl - i) * spc();
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/* fill in defaults for the current partition */
2607c478bd9Sstevel@tonic-gate 	p_deflt.start_cyl = i;
261edbad4feSToomas Soome 	p_deflt.deflt_size = min(cur_parts->pinfo_map[num].dkl_nblk,
262edbad4feSToomas Soome 	    ioparam.io_bounds.upper);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/* call input, passing p_deflt's address, typecast to (int *) */
265342440ecSPrasad Singamsetty 	j = (uint_t)input(FIO_ECYL, "Enter partition size", ':', &ioparam,
2667c478bd9Sstevel@tonic-gate 	    (int *)&p_deflt, DATA_INPUT);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/*
2697c478bd9Sstevel@tonic-gate 	 * If the current partition has a size of zero change the
2707c478bd9Sstevel@tonic-gate 	 * tag to Unassigned and the starting cylinder to zero
2717c478bd9Sstevel@tonic-gate 	 */
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (j == 0) {
2747c478bd9Sstevel@tonic-gate 		tag = V_UNASSIGNED;
2757c478bd9Sstevel@tonic-gate 		i = 0;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate #if defined(i386)
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (i < cyl_offset && tag != V_UNASSIGNED && tag != V_BACKUP &&
2827c478bd9Sstevel@tonic-gate 	    tag != V_BOOT) {
2837c478bd9Sstevel@tonic-gate 		/*
2847c478bd9Sstevel@tonic-gate 		 * This slice overlaps boot and/or alternates slice
2857c478bd9Sstevel@tonic-gate 		 * Check if it's the boot or alternates slice and warn
2867c478bd9Sstevel@tonic-gate 		 * accordingly
2877c478bd9Sstevel@tonic-gate 		 */
2887c478bd9Sstevel@tonic-gate 		if (i < cur_parts->pinfo_map[I_PARTITION].dkl_cylno + 1) {
2897c478bd9Sstevel@tonic-gate 			fmt_print("\nWarning: Partition overlaps boot ");
2907c478bd9Sstevel@tonic-gate 			fmt_print("partition. Specify different start cyl.\n");
2917c478bd9Sstevel@tonic-gate 			return;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 		/*
2947c478bd9Sstevel@tonic-gate 		 * Cyl offset for alternates partition was calculated before
2957c478bd9Sstevel@tonic-gate 		 */
2967c478bd9Sstevel@tonic-gate 		if (i < cyl_offset) {
2977c478bd9Sstevel@tonic-gate 			fmt_print("\nWarning: Partition overlaps alternates ");
2987c478bd9Sstevel@tonic-gate 			fmt_print("partition. Specify different start cyl.\n");
2997c478bd9Sstevel@tonic-gate 			return;
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate #endif	/* defined(i386) */
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * If user has entered a V_BACKUP tag then the partition
3077c478bd9Sstevel@tonic-gate 	 * size should specify full disk capacity else
3087c478bd9Sstevel@tonic-gate 	 * return an Error.
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 	if (tag == V_BACKUP) {
311342440ecSPrasad Singamsetty 		uint_t fullsz;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		fullsz = ncyl * nhead * nsect;
3147c478bd9Sstevel@tonic-gate 		if (fullsz != j) {
3157c478bd9Sstevel@tonic-gate 		/*
3167c478bd9Sstevel@tonic-gate 		 * V_BACKUP Tag Partition != full disk capacity.
3177c478bd9Sstevel@tonic-gate 		 * print useful messages.
3187c478bd9Sstevel@tonic-gate 		 */
3197c478bd9Sstevel@tonic-gate 		fmt_print("\nWarning: Partition with V_BACKUP tag should ");
3207c478bd9Sstevel@tonic-gate 		fmt_print("specify full disk capacity. \n");
3217c478bd9Sstevel@tonic-gate 		return;
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * If the current partition is named, we can't change it.
3287c478bd9Sstevel@tonic-gate 	 * We create a new current partition map instead.
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	if (cur_parts->pinfo_name != NULL)
3317c478bd9Sstevel@tonic-gate 		make_partition();
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Change the values.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	cur_parts->pinfo_map[num].dkl_cylno = i;
3367c478bd9Sstevel@tonic-gate 	cur_parts->pinfo_map[num].dkl_nblk = j;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
3397c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[num].p_start = (daddr_t)(i * (nhead * nsect));
3407c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[num].p_size = (long)j;
3417c478bd9Sstevel@tonic-gate #endif	/* defined(_SUNOS_VTOC_16) */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * Install the p_tag and p_flag values for this partition
3457c478bd9Sstevel@tonic-gate 	 */
3467c478bd9Sstevel@tonic-gate 	assert(cur_parts->vtoc.v_version == V_VERSION);
3477c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[num].p_tag = (ushort_t)tag;
3487c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[num].p_flag = (ushort_t)flag;
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * This routine picks to closest partition table which matches the
3547c478bd9Sstevel@tonic-gate  * selected disk type.  It is called each time the disk type is
3557c478bd9Sstevel@tonic-gate  * changed.  If no match is found, it uses the first element
3567c478bd9Sstevel@tonic-gate  * of the partition table.  If no table exists, a dummy is
3577c478bd9Sstevel@tonic-gate  * created.
3587c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate int
get_partition(void)360*b12aaafbSToomas Soome get_partition(void)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	register struct partition_info *pptr;
3637c478bd9Sstevel@tonic-gate 	register struct partition_info *parts;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * If there are no pre-defined maps for this disk type, it's
3677c478bd9Sstevel@tonic-gate 	 * an error.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	parts = cur_dtype->dtype_plist;
3707c478bd9Sstevel@tonic-gate 	if (parts == NULL) {
3717c478bd9Sstevel@tonic-gate 		err_print("No defined partition tables.\n");
3727c478bd9Sstevel@tonic-gate 		make_partition();
3737c478bd9Sstevel@tonic-gate 		return (-1);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	/*
3767c478bd9Sstevel@tonic-gate 	 * Loop through the pre-defined maps searching for one which match
3777c478bd9Sstevel@tonic-gate 	 * disk type.  If found copy it into unmamed partition.
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	enter_critical();
3807c478bd9Sstevel@tonic-gate 	for (pptr = parts; pptr != NULL; pptr = pptr->pinfo_next) {
381edbad4feSToomas Soome 		if (cur_dtype->dtype_asciilabel) {
382edbad4feSToomas Soome 			if (pptr->pinfo_name != NULL && strcmp(pptr->pinfo_name,
383edbad4feSToomas Soome 			    cur_dtype->dtype_asciilabel) == 0) {
384edbad4feSToomas Soome 				/*
385edbad4feSToomas Soome 				 * Set current partition and name it.
386edbad4feSToomas Soome 				 */
387edbad4feSToomas Soome 				cur_disk->disk_parts = cur_parts = pptr;
388edbad4feSToomas Soome 				cur_parts->pinfo_name = pptr->pinfo_name;
389edbad4feSToomas Soome 				exit_critical();
390edbad4feSToomas Soome 				return (0);
391edbad4feSToomas Soome 			}
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * If we couldn't find a match, take the first one.
3967c478bd9Sstevel@tonic-gate 	 * Set current partition and name it.
3977c478bd9Sstevel@tonic-gate 	 */
3987c478bd9Sstevel@tonic-gate 	cur_disk->disk_parts = cur_parts = cur_dtype->dtype_plist;
3997c478bd9Sstevel@tonic-gate 	cur_parts->pinfo_name = parts->pinfo_name;
4007c478bd9Sstevel@tonic-gate 	exit_critical();
4017c478bd9Sstevel@tonic-gate 	return (0);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * This routine creates a new partition map and sets it current.  If there
4077c478bd9Sstevel@tonic-gate  * was a current map, the new map starts out identical to it.  Otherwise
4087c478bd9Sstevel@tonic-gate  * the new map starts out all zeroes.
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate void
make_partition(void)411*b12aaafbSToomas Soome make_partition(void)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	register struct partition_info *pptr, *parts;
4147c478bd9Sstevel@tonic-gate 	int	i;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * Lock out interrupts so the lists don't get mangled.
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	enter_critical();
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 * Get space for for the new map and link it into the list
4227c478bd9Sstevel@tonic-gate 	 * of maps for the current disk type.
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	pptr = (struct partition_info *)zalloc(sizeof (struct partition_info));
4257c478bd9Sstevel@tonic-gate 	parts = cur_dtype->dtype_plist;
4267c478bd9Sstevel@tonic-gate 	if (parts == NULL) {
4277c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_plist = pptr;
4287c478bd9Sstevel@tonic-gate 	} else {
4297c478bd9Sstevel@tonic-gate 		while (parts->pinfo_next != NULL) {
4307c478bd9Sstevel@tonic-gate 			parts = parts->pinfo_next;
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 		parts->pinfo_next = pptr;
4337c478bd9Sstevel@tonic-gate 		pptr->pinfo_next = NULL;
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 * If there was a current map, copy its values.
4377c478bd9Sstevel@tonic-gate 	 */
4387c478bd9Sstevel@tonic-gate 	if (cur_label == L_TYPE_EFI) {
439edbad4feSToomas Soome 		struct dk_gpt	*map;
440edbad4feSToomas Soome 		int		nparts;
441edbad4feSToomas Soome 		int		size;
442edbad4feSToomas Soome 
443edbad4feSToomas Soome 		nparts = cur_parts->etoc->efi_nparts;
444edbad4feSToomas Soome 		size = sizeof (struct dk_part) * nparts +
445edbad4feSToomas Soome 		    sizeof (struct dk_gpt);
446edbad4feSToomas Soome 		map = zalloc(size);
447edbad4feSToomas Soome 		(void) memcpy(map, cur_parts->etoc, size);
448edbad4feSToomas Soome 		pptr->etoc = map;
449edbad4feSToomas Soome 		cur_disk->disk_parts = cur_parts = pptr;
450edbad4feSToomas Soome 		exit_critical();
451edbad4feSToomas Soome 		return;
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 	if (cur_parts != NULL) {
4547c478bd9Sstevel@tonic-gate 		for (i = 0; i < NDKMAP; i++) {
4557c478bd9Sstevel@tonic-gate 			pptr->pinfo_map[i] = cur_parts->pinfo_map[i];
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 		pptr->vtoc = cur_parts->vtoc;
4587c478bd9Sstevel@tonic-gate 	} else {
4597c478bd9Sstevel@tonic-gate 		/*
4607c478bd9Sstevel@tonic-gate 		 * Otherwise set initial default vtoc values
4617c478bd9Sstevel@tonic-gate 		 */
4627c478bd9Sstevel@tonic-gate 		set_vtoc_defaults(pptr);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * Make the new one current.
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 	cur_disk->disk_parts = cur_parts = pptr;
4697c478bd9Sstevel@tonic-gate 	exit_critical();
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * This routine deletes a partition map from the list of maps for
4757c478bd9Sstevel@tonic-gate  * the given disk type.
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate void
delete_partition(struct partition_info * parts)4787c478bd9Sstevel@tonic-gate delete_partition(struct partition_info *parts)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	struct	partition_info *pptr;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	/*
4837c478bd9Sstevel@tonic-gate 	 * If there isn't a current map, it's an error.
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 	if (cur_dtype->dtype_plist == NULL) {
4867c478bd9Sstevel@tonic-gate 		err_print("Error: unexpected null partition list.\n");
4877c478bd9Sstevel@tonic-gate 		fullabort();
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * Remove the map from the list.
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	if (cur_dtype->dtype_plist == parts)
4937c478bd9Sstevel@tonic-gate 		cur_dtype->dtype_plist = parts->pinfo_next;
4947c478bd9Sstevel@tonic-gate 	else {
4957c478bd9Sstevel@tonic-gate 		for (pptr = cur_dtype->dtype_plist; pptr->pinfo_next != parts;
4967c478bd9Sstevel@tonic-gate 		    pptr = pptr->pinfo_next)
4977c478bd9Sstevel@tonic-gate 			;
4987c478bd9Sstevel@tonic-gate 		pptr->pinfo_next = parts->pinfo_next;
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 * Free the space it was using.
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	destroy_data((char *)parts);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate  * Set all partition vtoc fields to defaults
5097c478bd9Sstevel@tonic-gate  */
5107c478bd9Sstevel@tonic-gate void
set_vtoc_defaults(struct partition_info * part)5117c478bd9Sstevel@tonic-gate set_vtoc_defaults(struct partition_info *part)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	int	i;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&part->vtoc, sizeof (struct dk_vtoc));
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	part->vtoc.v_version = V_VERSION;
5187c478bd9Sstevel@tonic-gate 	part->vtoc.v_nparts = NDKMAP;
5197c478bd9Sstevel@tonic-gate 	part->vtoc.v_sanity = VTOC_SANE;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++) {
5227c478bd9Sstevel@tonic-gate 		part->vtoc.v_part[i].p_tag = default_vtoc_map[i].p_tag;
5237c478bd9Sstevel@tonic-gate 		part->vtoc.v_part[i].p_flag = default_vtoc_map[i].p_flag;
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate }
526