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
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*362Sbg159949 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * This file contains the code to add new disk_type and partition
310Sstevel@tonic-gate * definitions to a format data file.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate #include "global.h"
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <fcntl.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <memory.h>
420Sstevel@tonic-gate #include <sys/fcntl.h>
430Sstevel@tonic-gate #include <sys/param.h>
440Sstevel@tonic-gate #include <time.h>
450Sstevel@tonic-gate #include <sys/time.h>
460Sstevel@tonic-gate #include <stdarg.h>
470Sstevel@tonic-gate
480Sstevel@tonic-gate #include "add_definition.h"
490Sstevel@tonic-gate #include "misc.h"
500Sstevel@tonic-gate #include "partition.h"
510Sstevel@tonic-gate #include "menu_command.h"
520Sstevel@tonic-gate #include "startup.h"
530Sstevel@tonic-gate
540Sstevel@tonic-gate extern struct ctlr_type ctlr_types[];
550Sstevel@tonic-gate extern int nctypes;
560Sstevel@tonic-gate
570Sstevel@tonic-gate extern int errno;
580Sstevel@tonic-gate
590Sstevel@tonic-gate /* Function prototypes */
600Sstevel@tonic-gate #ifdef __STDC__
610Sstevel@tonic-gate
620Sstevel@tonic-gate static void add_disktype(FILE *fd, struct disk_info *disk_info);
630Sstevel@tonic-gate static void add_partition(FILE *fd, struct disk_info *,
640Sstevel@tonic-gate struct partition_info *);
650Sstevel@tonic-gate static int add_entry(int col, FILE *fd, char *format, ...);
660Sstevel@tonic-gate
670Sstevel@tonic-gate #else /* __STDC__ */
680Sstevel@tonic-gate
690Sstevel@tonic-gate static void add_disktype();
700Sstevel@tonic-gate static void add_partition();
710Sstevel@tonic-gate static int add_entry();
720Sstevel@tonic-gate
730Sstevel@tonic-gate #endif /* __STDC__ */
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * Add new definitions for the current disk/partition to a format data file.
770Sstevel@tonic-gate */
780Sstevel@tonic-gate int
add_definition()790Sstevel@tonic-gate add_definition()
800Sstevel@tonic-gate {
810Sstevel@tonic-gate FILE *fd;
820Sstevel@tonic-gate char *filename;
830Sstevel@tonic-gate time_t clock;
840Sstevel@tonic-gate char *prompt;
850Sstevel@tonic-gate union {
860Sstevel@tonic-gate int xfoo;
870Sstevel@tonic-gate char deflt_str[MAXPATHLEN];
880Sstevel@tonic-gate } x;
890Sstevel@tonic-gate
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate * There must be a current disk and partition table
920Sstevel@tonic-gate */
930Sstevel@tonic-gate if (cur_disk == NULL) {
940Sstevel@tonic-gate err_print("No Current Disk.\n");
950Sstevel@tonic-gate return (0);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate if (cur_dtype == NULL) {
980Sstevel@tonic-gate err_print("Current disk type is not set.\n");
990Sstevel@tonic-gate return (-1);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate if (cur_parts == NULL) {
1020Sstevel@tonic-gate err_print("Current partition is not set.\n");
1030Sstevel@tonic-gate return (-1);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate * If neither the disk definition nor the partition
1070Sstevel@tonic-gate * information has been changed, there's nothing to save.
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate if (cur_dtype->dtype_filename != NULL &&
1100Sstevel@tonic-gate cur_parts->pinfo_filename != NULL) {
1110Sstevel@tonic-gate err_print("\
1120Sstevel@tonic-gate Neither the disk type nor the partitioning has been changed.\n");
1130Sstevel@tonic-gate return (-1);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate * If saving the partition, and it's unnamed, the user should name
1170Sstevel@tonic-gate * it first.
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate if (cur_parts->pinfo_name == NULL) {
1200Sstevel@tonic-gate assert(cur_parts->pinfo_filename == NULL);
1210Sstevel@tonic-gate err_print("Please name this partition type before saving it\n");
1220Sstevel@tonic-gate return (-1);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * Let the user know what we're doing
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate if (cur_dtype->dtype_filename == NULL &&
1280Sstevel@tonic-gate cur_parts->pinfo_filename == NULL) {
1290Sstevel@tonic-gate fmt_print("Saving new disk and partition definitions\n");
1300Sstevel@tonic-gate } else if (cur_dtype->dtype_filename == NULL) {
1310Sstevel@tonic-gate fmt_print("Saving new disk definition\n");
1320Sstevel@tonic-gate } else {
1330Sstevel@tonic-gate assert(cur_parts->pinfo_filename == NULL);
1340Sstevel@tonic-gate fmt_print("Saving new partition definition\n");
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * Ask for the file to which to append the new definitions
1380Sstevel@tonic-gate */
1390Sstevel@tonic-gate prompt = "Enter file name";
1400Sstevel@tonic-gate (void) strcpy(x.deflt_str, "./format.dat");
141*362Sbg159949 filename = (char *)(uintptr_t)input(FIO_OSTR, prompt,
1420Sstevel@tonic-gate ':', (u_ioparam_t *)NULL, &x.xfoo, DATA_INPUT);
1430Sstevel@tonic-gate assert(filename != NULL);
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * Open the file in append mode, or create it, if necessary
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate if ((fd = fopen(filename, "a")) == NULL) {
1480Sstevel@tonic-gate err_print("Cannot open `%s' - %s\n", filename,
1490Sstevel@tonic-gate strerror(errno));
1500Sstevel@tonic-gate destroy_data(filename);
1510Sstevel@tonic-gate return (-1);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Write a header for the new definitions
1550Sstevel@tonic-gate */
1560Sstevel@tonic-gate if ((cur_dtype->dtype_filename == NULL) &&
1570Sstevel@tonic-gate (cur_parts->pinfo_filename == NULL)) {
1580Sstevel@tonic-gate (void) fprintf(fd, "#\n# New disk/partition type ");
1590Sstevel@tonic-gate } else if (cur_dtype->dtype_filename == NULL) {
1600Sstevel@tonic-gate (void) fprintf(fd, "#\n# New disk type ");
1610Sstevel@tonic-gate } else {
1620Sstevel@tonic-gate (void) fprintf(fd, "#\n# New partition type ");
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate (void) time(&clock);
1650Sstevel@tonic-gate (void) fprintf(fd, " saved on %s#\n", ctime(&clock));
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate * Save the new definitions
1680Sstevel@tonic-gate */
1690Sstevel@tonic-gate if (cur_dtype->dtype_filename == NULL) {
1700Sstevel@tonic-gate add_disktype(fd, cur_disk);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate if (cur_parts->pinfo_filename == NULL) {
1730Sstevel@tonic-gate add_partition(fd, cur_disk, cur_parts);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate * We're finished. Clean up
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate (void) fclose(fd);
1790Sstevel@tonic-gate destroy_data(filename);
1800Sstevel@tonic-gate return (0);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate * Add a disk_type definition to the file fd
1850Sstevel@tonic-gate */
1860Sstevel@tonic-gate static void
add_disktype(fd,disk_info)1870Sstevel@tonic-gate add_disktype(fd, disk_info)
1880Sstevel@tonic-gate FILE *fd;
1890Sstevel@tonic-gate struct disk_info *disk_info;
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate int col;
1920Sstevel@tonic-gate struct disk_type *disk_type;
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate disk_type = disk_info->disk_type;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate (void) fprintf(fd, "disk_type = \"%s\" \\\n",
1970Sstevel@tonic-gate disk_type->dtype_asciilabel);
1980Sstevel@tonic-gate col = add_entry(0, fd, " : ctlr = %s",
1990Sstevel@tonic-gate ((disk_info->disk_ctlr)->ctlr_ctype)->ctype_name);
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate col = add_entry(col, fd, " : ncyl = %d", disk_type->dtype_ncyl);
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate col = add_entry(col, fd, " : acyl = %d", disk_type->dtype_acyl);
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate col = add_entry(col, fd, " : pcyl = %d", disk_type->dtype_pcyl);
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate col = add_entry(col, fd, " : nhead = %d", disk_type->dtype_nhead);
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate if (disk_type->dtype_options & SUP_PHEAD) {
2100Sstevel@tonic-gate col = add_entry(col, fd, " : phead = %d",
2110Sstevel@tonic-gate disk_type->dtype_phead);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate col = add_entry(col, fd, " : nsect = %d", disk_type->dtype_nsect);
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if (disk_type->dtype_options & SUP_PSECT) {
2170Sstevel@tonic-gate col = add_entry(col, fd, " : psect = %d",
2180Sstevel@tonic-gate disk_type->dtype_psect);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate if (disk_type->dtype_options & SUP_BPT) {
2220Sstevel@tonic-gate col = add_entry(col, fd, " : bpt = %d", disk_type->dtype_bpt);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate col = add_entry(col, fd, " : rpm = %d", disk_type->dtype_rpm);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (disk_type->dtype_options & SUP_FMTTIME) {
2280Sstevel@tonic-gate col = add_entry(col, fd, " : fmt_time = %d",
2290Sstevel@tonic-gate disk_type->dtype_fmt_time);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate if (disk_type->dtype_options & SUP_CYLSKEW) {
2330Sstevel@tonic-gate col = add_entry(col, fd, " : cyl_skew = %d",
2340Sstevel@tonic-gate disk_type->dtype_cyl_skew);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate if (disk_type->dtype_options & SUP_TRKSKEW) {
2380Sstevel@tonic-gate col = add_entry(col, fd, " : trk_skew = %d",
2390Sstevel@tonic-gate disk_type->dtype_trk_skew);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate if (disk_type->dtype_options & SUP_TRKS_ZONE) {
2430Sstevel@tonic-gate col = add_entry(col, fd, " : trks_zone = %d",
2440Sstevel@tonic-gate disk_type->dtype_trks_zone);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate if (disk_type->dtype_options & SUP_ATRKS) {
2480Sstevel@tonic-gate col = add_entry(col, fd, " : atrks = %d",
2490Sstevel@tonic-gate disk_type->dtype_atrks);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate if (disk_type->dtype_options & SUP_ASECT) {
2530Sstevel@tonic-gate col = add_entry(col, fd, " : asect = %d",
2540Sstevel@tonic-gate disk_type->dtype_asect);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if (disk_type->dtype_options & SUP_CACHE) {
2580Sstevel@tonic-gate col = add_entry(col, fd, " : cache = %d",
2590Sstevel@tonic-gate disk_type->dtype_cache);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate if (disk_type->dtype_options & SUP_PREFETCH) {
2630Sstevel@tonic-gate col = add_entry(col, fd, " : prefetch = %d",
2640Sstevel@tonic-gate disk_type->dtype_threshold);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate if (disk_type->dtype_options & SUP_CACHE_MIN) {
2680Sstevel@tonic-gate col = add_entry(col, fd, " : min_prefetch = %d",
2690Sstevel@tonic-gate disk_type->dtype_prefetch_min);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate if (disk_type->dtype_options & SUP_CACHE_MAX) {
2730Sstevel@tonic-gate col = add_entry(col, fd, " : max_prefetch = %d",
2740Sstevel@tonic-gate disk_type->dtype_prefetch_max);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate if (disk_type->dtype_options & SUP_BPS) {
2780Sstevel@tonic-gate col = add_entry(col, fd, " : bps = %d",
2790Sstevel@tonic-gate disk_type->dtype_bps);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate if (disk_type->dtype_options & SUP_DRTYPE) {
2830Sstevel@tonic-gate col = add_entry(col, fd, " : drive_type = %d",
2840Sstevel@tonic-gate disk_type->dtype_dr_type);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate * Terminate the last line, and print one blank line
2890Sstevel@tonic-gate */
2900Sstevel@tonic-gate (void) fprintf(fd, col == 0 ? "\n" : "\n\n");
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate * Once we exceed this length, wrap to a new line
2970Sstevel@tonic-gate */
2980Sstevel@tonic-gate #define MAX_COLUMNS 50
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate * Add a partition definition to the file fd
3020Sstevel@tonic-gate */
3030Sstevel@tonic-gate static void
add_partition(fd,disk_info,part)3040Sstevel@tonic-gate add_partition(fd, disk_info, part)
3050Sstevel@tonic-gate FILE *fd;
3060Sstevel@tonic-gate struct disk_info *disk_info;
3070Sstevel@tonic-gate struct partition_info *part;
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate int col;
3100Sstevel@tonic-gate int i;
3110Sstevel@tonic-gate struct disk_type *disk_type;
3120Sstevel@tonic-gate struct dk_map32 *pp;
3130Sstevel@tonic-gate char *s;
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate #if defined(_SUNOS_VTOC_8)
3160Sstevel@tonic-gate struct dk_map2 *pv;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_16)
3190Sstevel@tonic-gate struct dkl_partition *pv;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate #else
3220Sstevel@tonic-gate #error No VTOC format defined.
3230Sstevel@tonic-gate #endif /* defined (_SUNOS_VTOC_8) */
3240Sstevel@tonic-gate struct dk_map2 *dv;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate disk_type = disk_info->disk_type;
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate (void) fprintf(fd, "partition = \"%s\" \\\n", part->pinfo_name);
3290Sstevel@tonic-gate (void) fprintf(fd, "\t : disk = \"%s\" : ctlr = %s \\\n",
3300Sstevel@tonic-gate disk_type->dtype_asciilabel,
3310Sstevel@tonic-gate ((disk_info->disk_ctlr)->ctlr_ctype)->ctype_name);
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate * Print the specifications for each useful partition
3350Sstevel@tonic-gate */
3360Sstevel@tonic-gate col = 0;
3370Sstevel@tonic-gate pp = part->pinfo_map;
3380Sstevel@tonic-gate pv = part->vtoc.v_part;
3390Sstevel@tonic-gate dv = default_vtoc_map;
3400Sstevel@tonic-gate for (i = 0; i < NDKMAP; i++, pp++, pv++, dv++) {
3410Sstevel@tonic-gate if (pp->dkl_nblk != 0) {
3420Sstevel@tonic-gate col = add_entry(col, fd, " : %c = ",
3430Sstevel@tonic-gate i + PARTITION_BASE);
3440Sstevel@tonic-gate if (pv->p_tag != dv->p_tag ||
3450Sstevel@tonic-gate pv->p_flag != dv->p_flag) {
3460Sstevel@tonic-gate s = find_string(ptag_choices,
3470Sstevel@tonic-gate (int)pv->p_tag);
3480Sstevel@tonic-gate if (s != NULL) {
3490Sstevel@tonic-gate col = add_entry(col, fd, " %s,", s);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate s = find_string(pflag_choices,
3520Sstevel@tonic-gate (int)pv->p_flag);
3530Sstevel@tonic-gate if (s != NULL) {
3540Sstevel@tonic-gate col = add_entry(col, fd, " %s,", s);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate col = add_entry(col, fd, " %d, %d", pp->dkl_cylno,
3580Sstevel@tonic-gate pp->dkl_nblk);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate /*
3630Sstevel@tonic-gate * Terminate the last line, and print one blank line
3640Sstevel@tonic-gate */
3650Sstevel@tonic-gate (void) fprintf(fd, col == 0 ? "\n" : "\n\n");
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate * Add an entry to the file fd. col is the current starting column.
3700Sstevel@tonic-gate * Return the resulting new column position.
3710Sstevel@tonic-gate */
3720Sstevel@tonic-gate /*PRINTFLIKE3*/
3730Sstevel@tonic-gate static int
add_entry(int col,FILE * fd,char * format,...)3740Sstevel@tonic-gate add_entry(int col, FILE *fd, char *format, ...)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate va_list ap;
3770Sstevel@tonic-gate va_start(ap, format);
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate if (col > MAX_COLUMNS) {
3800Sstevel@tonic-gate (void) fprintf(fd, " \\\n");
3810Sstevel@tonic-gate col = 0;
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate if (col == 0) {
3840Sstevel@tonic-gate col += fprintf(fd, "\t");
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate col += vfprintf(fd, format, ap);
3870Sstevel@tonic-gate va_end(ap);
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate return (col);
3900Sstevel@tonic-gate }
391