Home | History | Annotate | Line # | Download | only in mac68k
md.c revision 1.2.4.1
      1  1.2.4.1       snj /*	$NetBSD: md.c,v 1.2.4.1 2015/05/14 07:58:49 snj Exp $ */
      2      1.1  dholland 
      3      1.1  dholland /*
      4      1.1  dholland  * Copyright 1997 Piermont Information Systems Inc.
      5      1.1  dholland  * All rights reserved.
      6      1.1  dholland  *
      7      1.1  dholland  * Based on code written by Philip A. Nelson for Piermont Information
      8      1.1  dholland  * Systems Inc.
      9      1.1  dholland  *
     10      1.1  dholland  * Redistribution and use in source and binary forms, with or without
     11      1.1  dholland  * modification, are permitted provided that the following conditions
     12      1.1  dholland  * are met:
     13      1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     14      1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     15      1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  dholland  *    documentation and/or other materials provided with the distribution.
     18      1.1  dholland  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     19      1.1  dholland  *    or promote products derived from this software without specific prior
     20      1.1  dholland  *    written permission.
     21      1.1  dholland  *
     22      1.1  dholland  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     23      1.1  dholland  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24      1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25      1.1  dholland  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     26      1.1  dholland  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27      1.1  dholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28      1.1  dholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29      1.1  dholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30      1.1  dholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31      1.1  dholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32      1.1  dholland  * THE POSSIBILITY OF SUCH DAMAGE.
     33      1.1  dholland  */
     34      1.1  dholland 
     35      1.1  dholland /* md.c -- mac68k machine specific routines */
     36      1.1  dholland 
     37      1.1  dholland #include <stdio.h>
     38      1.1  dholland #include <util.h>
     39      1.1  dholland #include <unistd.h>
     40      1.1  dholland #include <fcntl.h>
     41      1.1  dholland #include <sys/ioctl.h>
     42      1.1  dholland #include <sys/utsname.h>
     43      1.1  dholland #include <machine/int_fmtio.h>
     44      1.1  dholland 
     45      1.1  dholland #include "defs.h"
     46      1.1  dholland #include "md.h"
     47      1.1  dholland #include "msg_defs.h"
     48      1.1  dholland #include "menu_defs.h"
     49      1.1  dholland 
     50      1.1  dholland static int	stricmp(const char *c1, const char *c2);
     51      1.1  dholland static void	setpartition(struct apple_part_map_entry *, char *, int);
     52      1.1  dholland static int	getFreeLabelEntry(char *);
     53      1.1  dholland static char	*getFstype(struct apple_part_map_entry *, int, char *);
     54      1.1  dholland static char	*getUse(struct apple_part_map_entry *, int, char *);
     55      1.1  dholland static char	*getName(struct apple_part_map_entry *, int, char *);
     56      1.1  dholland static int	findStdType(int, char *, int, int *, int);
     57      1.1  dholland static int	check_for_errors(void);
     58      1.1  dholland static int	edit_diskmap(void);
     59      1.1  dholland #ifdef MD_DEBUG_SORT_MERGE
     60      1.1  dholland static int	md_debug_dump(char *);
     61      1.1  dholland #endif
     62      1.1  dholland 
     63      1.1  dholland int blk_size;
     64      1.1  dholland 
     65      1.1  dholland MAP_TYPE map_types[] = {
     66      1.1  dholland 	{MAP_RESERVED, APPLE_PART_TYPE_DRIVER},
     67      1.1  dholland 	{MAP_RESERVED, APPLE_PART_TYPE_DRIVER43},
     68      1.1  dholland 	{MAP_RESERVED, APPLE_PART_TYPE_DRIVERATA},
     69      1.1  dholland 	{MAP_RESERVED, APPLE_PART_TYPE_FWB_COMPONENT},
     70      1.1  dholland 	{MAP_MACOS,    APPLE_PART_TYPE_MAC},
     71      1.1  dholland 	{MAP_NETBSD,   APPLE_PART_TYPE_NETBSD},
     72      1.1  dholland 	{MAP_RESERVED, APPLE_PART_TYPE_PARTMAP},
     73      1.1  dholland 	{MAP_OTHER,    APPLE_PART_TYPE_SCRATCH},
     74      1.1  dholland 	{MAP_NETBSD,   APPLE_PART_TYPE_UNIX},
     75      1.1  dholland 	{MAP_EOL,      NULL}
     76      1.1  dholland };
     77      1.1  dholland 
     78      1.1  dholland MAP map = {0, 0, 0, 0, 0, 0, 0, 0, {0}, NULL};
     79      1.1  dholland 
     80      1.1  dholland struct apple_part_map_entry new_map[] =
     81      1.1  dholland {
     82      1.1  dholland 	{ APPLE_PART_MAP_ENTRY_MAGIC, 0xa5a5, 6, 1, NEW_MAP_SIZE & 0x7e,
     83      1.1  dholland 	  "Apple", "Apple_Partition_Map", 0, NEW_MAP_SIZE, 0x37,
     84      1.1  dholland 	  0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
     85      1.1  dholland 	{ APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 64, 32,
     86      1.1  dholland 	  "Macintosh", "Apple_Driver", 0, 0, 0x37,
     87      1.1  dholland 	  0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
     88      1.1  dholland 	{ APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 96, 64,
     89      1.1  dholland 	  "Macintosh", "Apple_Driver43", 0, 0, 0x37,
     90      1.1  dholland 	  0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
     91      1.1  dholland 	{ APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 160, 64,
     92      1.1  dholland 	  "Macintosh", "Apple_Driver_ATA", 0, 0, 0x37,
     93      1.1  dholland 	  0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
     94      1.1  dholland 	{ APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 224, 4096,
     95      1.1  dholland 	  "untitled", "Apple_HFS", 0, 0, 0x37,
     96      1.1  dholland 	  0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
     97      1.1  dholland 	{ APPLE_PART_MAP_ENTRY_MAGIC, 0, 6,4320, 0,
     98      1.1  dholland 	  "untitled", "Apple_Free", 0, 0, 0x37,
     99      1.1  dholland 	  0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}}
    100      1.1  dholland };
    101      1.1  dholland 
    102      1.1  dholland void
    103      1.1  dholland md_init(void)
    104      1.1  dholland {
    105      1.1  dholland }
    106      1.1  dholland 
    107      1.1  dholland void
    108      1.1  dholland md_init_set_status(int flags)
    109      1.1  dholland {
    110      1.1  dholland        struct utsname instsys;
    111      1.1  dholland 
    112      1.1  dholland 	(void)flags;
    113      1.1  dholland 
    114      1.1  dholland 	/*
    115      1.1  dholland 	 * Get the name of the Install Kernel we are running under and
    116      1.1  dholland 	 * enable the installation of the corresponding GENERIC kernel.
    117      1.1  dholland 	 *
    118      1.1  dholland 	 * Note:  In md.h the two kernels are disabled.  If they are
    119      1.1  dholland 	 *        enabled there the logic here needs to be switched.
    120      1.1  dholland 	 */
    121      1.1  dholland         uname(&instsys);
    122      1.1  dholland         if (strstr(instsys.version, "(INSTALLSBC)"))
    123      1.1  dholland 		/*
    124      1.1  dholland 		 * Running the SBC Installation Kernel, so enable GENERICSBC
    125      1.1  dholland 		 */
    126      1.1  dholland 		set_kernel_set(SET_KERNEL_2);
    127      1.1  dholland         else
    128      1.1  dholland 		/*
    129      1.1  dholland 		 * Running the GENERIC Installation Kernel, so enable GENERIC
    130      1.1  dholland 		 */
    131      1.1  dholland 		set_kernel_set(SET_KERNEL_1);
    132      1.1  dholland }
    133      1.1  dholland 
    134      1.1  dholland int
    135      1.1  dholland md_get_info(void)
    136      1.1  dholland {
    137      1.1  dholland 	struct disklabel disklabel;
    138      1.1  dholland 	int fd, i;
    139      1.1  dholland 	char dev_name[100];
    140      1.1  dholland 	struct apple_part_map_entry block;
    141      1.1  dholland 
    142      1.1  dholland 	snprintf(dev_name, sizeof(dev_name), "/dev/r%s%c",
    143      1.2    martin 		pm->diskdev, 'a' + getrawpartition());
    144      1.1  dholland 
    145      1.1  dholland 	/*
    146      1.1  dholland 	 * Open the disk as a raw device
    147      1.1  dholland 	 */
    148      1.1  dholland 	fd = open(dev_name, O_RDONLY, 0);
    149      1.1  dholland 	if (fd < 0) {
    150      1.1  dholland 		endwin();
    151      1.1  dholland 		fprintf (stderr, "Can't open %s\n", dev_name);
    152      1.1  dholland 		exit(1);
    153      1.1  dholland 	}
    154      1.1  dholland 	/*
    155      1.1  dholland 	 * Try to get the default disklabel info for the device
    156      1.1  dholland 	 */
    157      1.1  dholland 	if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
    158      1.1  dholland 		endwin();
    159      1.1  dholland 		fprintf (stderr, "Can't read disklabel on %s\n", dev_name);
    160      1.1  dholland 		close(fd);
    161      1.1  dholland 		exit(1);
    162      1.1  dholland 	}
    163      1.1  dholland 	/*
    164      1.1  dholland 	 * Get the disk parameters from the disk driver.  It should have
    165      1.1  dholland 	 *  obained them by querying the disk itself.
    166      1.1  dholland 	 */
    167      1.1  dholland 	blk_size = disklabel.d_secsize;
    168      1.2    martin 	pm->dlcyl = disklabel.d_ncylinders;
    169      1.2    martin 	pm->dlhead = disklabel.d_ntracks;
    170      1.2    martin 	pm->dlsec = disklabel.d_nsectors;
    171      1.1  dholland 	/*
    172      1.1  dholland 	 * Just in case, initialize the structures we'll need if we
    173      1.1  dholland 	 *  need to completely initialize the disk.
    174      1.1  dholland 	 */
    175      1.2    martin 	pm->dlsize = disklabel.d_secperunit;
    176      1.1  dholland 	for (i=0;i<NEW_MAP_SIZE;i++) {
    177      1.1  dholland 	   if (i > 0)
    178      1.1  dholland 		new_map[i].pmPyPartStart = new_map[i-1].pmPyPartStart +
    179      1.1  dholland 			new_map[i-1].pmPartBlkCnt;
    180      1.1  dholland 	   new_map[i].pmDataCnt = new_map[i].pmPartBlkCnt;
    181      1.1  dholland 	   if (new_map[i].pmPartBlkCnt == 0) {
    182      1.2    martin 		new_map[i].pmPartBlkCnt = pm->dlsize;
    183      1.2    martin 		new_map[i].pmDataCnt = pm->dlsize;
    184      1.1  dholland 		break;
    185      1.1  dholland 	   }
    186      1.2    martin 	   pm->dlsize -= new_map[i].pmPartBlkCnt;
    187      1.1  dholland 	}
    188      1.2    martin 	pm->dlsize = disklabel.d_secperunit;
    189      1.1  dholland #if 0
    190      1.2    martin 	msg_display(MSG_dldebug, blk_size, pm->dlcyl, pm->dlhead, pm->dlsec, pm->dlsize);
    191      1.1  dholland 	process_menu(MENU_ok, NULL);
    192      1.1  dholland #endif
    193      1.1  dholland 	map.size = 0;
    194      1.1  dholland 	/*
    195      1.1  dholland 	 * Verify the disk has been initialized for MacOS use by checking
    196      1.1  dholland 	 *  to see if the disk have a Boot Block
    197      1.1  dholland 	 */
    198      1.1  dholland 	if (lseek(fd, (off_t)0 * blk_size, SEEK_SET) < 0 ||
    199      1.1  dholland 	    read(fd,  &block, sizeof(block)) - sizeof(block) != 0 ||
    200      1.1  dholland 	    block.pmSig != 0x4552) {
    201      1.1  dholland              process_menu(MENU_nodiskmap, NULL);
    202      1.1  dholland         }
    203      1.1  dholland 	else {
    204      1.1  dholland 	   /*
    205      1.1  dholland 	    * Scan for the Partition Map entry that describes the Partition
    206      1.1  dholland 	    *  Map itself.  We need to know the number of blocks allocated
    207      1.1  dholland 	    *  to it and the number currently in use.
    208      1.1  dholland 	    */
    209      1.1  dholland 	   for (i=0;i<MAXMAXPARTITIONS;i++) {
    210      1.1  dholland 		lseek(fd, (off_t)(i+1) * blk_size, SEEK_SET);
    211      1.1  dholland 		read(fd, &block, sizeof(block));
    212      1.1  dholland 		if (stricmp("Apple_partition_map", (char *)block.pmPartType) == 0) {
    213      1.1  dholland 		    map.size = block.pmPartBlkCnt;
    214      1.1  dholland 		    map.in_use_cnt = block.pmMapBlkCnt;
    215      1.1  dholland 		    map.blk = (struct apple_part_map_entry *)malloc(map.size * blk_size);
    216      1.1  dholland 		    break;
    217      1.1  dholland 	        }
    218      1.1  dholland             }
    219      1.1  dholland 	    lseek(fd, (off_t)1 * blk_size, SEEK_SET);
    220      1.1  dholland 	    read(fd, map.blk, map.size * blk_size);
    221      1.1  dholland 	}
    222      1.1  dholland 	close(fd);
    223      1.1  dholland 	/*
    224      1.1  dholland 	 * Setup the disktype so /etc/disktab gets proper info
    225      1.1  dholland 	 */
    226      1.2    martin 	if (strncmp(pm->diskdev, "sd", 2) == 0) {
    227      1.2    martin 		pm->disktype = "SCSI";
    228      1.2    martin 		pm->doessf = "sf:";
    229      1.1  dholland 	} else
    230      1.2    martin 		pm->disktype = "IDE";
    231      1.1  dholland 
    232      1.1  dholland 	return edit_diskmap();
    233      1.1  dholland }
    234      1.1  dholland 
    235      1.1  dholland /*
    236      1.1  dholland  * md back-end code for menu-driven BSD disklabel editor.
    237      1.1  dholland  */
    238      1.1  dholland int
    239      1.1  dholland md_make_bsd_partitions(void)
    240      1.1  dholland {
    241      1.1  dholland 	FILE *f;
    242  1.2.4.1       snj 	int i, j, pl, rv;
    243      1.1  dholland 	EBZB *bzb;
    244      1.1  dholland 
    245      1.1  dholland 	/*
    246      1.1  dholland 	 * Scan for any problems and report them before continuing.
    247      1.1  dholland 	 *  The user can abort installation and we'll take them back
    248      1.1  dholland 	 *  to the main menu; continue ignoring the warnings, or
    249      1.1  dholland 	 *  ask to reedit the Disk Partition Map.
    250      1.1  dholland 	 */
    251      1.1  dholland 	while (1) {
    252      1.1  dholland 	    if (check_for_errors()) {
    253  1.2.4.1       snj 	        process_menu (MENU_sanity, &rv);
    254  1.2.4.1       snj 	        if (rv < 0)
    255      1.1  dholland 		    return 0;
    256  1.2.4.1       snj 	        else if (rv)
    257      1.1  dholland 		    break;
    258      1.1  dholland 	        edit_diskmap();
    259      1.1  dholland 	    } else
    260      1.1  dholland 		break;
    261      1.1  dholland 	}
    262      1.1  dholland 
    263      1.1  dholland 	/* Build standard partitions */
    264      1.2    martin 	memset(&pm->bsdlabel, 0, sizeof pm->bsdlabel);
    265      1.1  dholland 
    266      1.1  dholland 	/*
    267      1.1  dholland 	 * The mac68k port has a predefined partition for "c" which
    268      1.1  dholland 	 *  is the size of the disk, everything else is unused.
    269      1.1  dholland 	 */
    270      1.2    martin 	pm->bsdlabel[RAW_PART].pi_size = pm->dlsize;
    271      1.1  dholland 	/*
    272      1.1  dholland 	 * Now, scan through the Disk Partition Map and transfer the
    273      1.1  dholland 	 *  information into the incore disklabel.
    274      1.1  dholland 	 */
    275      1.1  dholland 	for (i=0;i<map.usable_cnt;i++) {
    276      1.1  dholland 	    j = map.mblk[i];
    277      1.1  dholland 	    bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
    278      1.1  dholland 	    if (bzb->flags.part) {
    279      1.1  dholland 		pl = bzb->flags.part - 'a';
    280      1.1  dholland 		switch (whichType(&map.blk[j])) {
    281      1.1  dholland 		    case HFS_PART:
    282      1.2    martin 			pm->bsdlabel[pl].pi_fstype = FS_HFS;
    283      1.2    martin 			strcpy (pm->bsdlabel[pl].pi_mount, (char *)bzb->mount_point);
    284      1.1  dholland 			break;
    285      1.1  dholland 		    case ROOT_PART:
    286      1.1  dholland 		    case UFS_PART:
    287      1.2    martin 			pm->bsdlabel[pl].pi_fstype = FS_BSDFFS;
    288      1.2    martin 			strcpy (pm->bsdlabel[pl].pi_mount, (char *)bzb->mount_point);
    289      1.2    martin 			pm->bsdlabel[pl].pi_flags |= PIF_NEWFS | PIF_MOUNT;
    290      1.1  dholland 			break;
    291      1.1  dholland 		    case SWAP_PART:
    292      1.2    martin 			pm->bsdlabel[pl].pi_fstype = FS_SWAP;
    293      1.1  dholland 			break;
    294      1.1  dholland 		    case SCRATCH_PART:
    295      1.2    martin 			pm->bsdlabel[pl].pi_fstype = FS_OTHER;
    296      1.2    martin 			strcpy (pm->bsdlabel[pl].pi_mount, (char *)bzb->mount_point);
    297      1.1  dholland 		    default:
    298      1.1  dholland 			break;
    299      1.1  dholland 		}
    300      1.2    martin 	        if (pm->bsdlabel[pl].pi_fstype != FS_UNUSED) {
    301      1.2    martin 		    pm->bsdlabel[pl].pi_size = map.blk[j].pmPartBlkCnt;
    302      1.2    martin 		    pm->bsdlabel[pl].pi_offset = map.blk[j].pmPyPartStart;
    303      1.2    martin 		    if (pm->bsdlabel[pl].pi_fstype != FS_SWAP) {
    304      1.2    martin 		        pm->bsdlabel[pl].pi_frag = 8;
    305      1.2    martin 		        pm->bsdlabel[pl].pi_fsize = 1024;
    306      1.1  dholland 		    }
    307      1.1  dholland 		}
    308      1.1  dholland 	    }
    309      1.1  dholland 	}
    310      1.1  dholland 
    311      1.1  dholland 	/* Disk name  - don't bother asking, just use the physical name*/
    312      1.2    martin 	strcpy (pm->bsddiskname, pm->diskdev);
    313      1.1  dholland 
    314      1.1  dholland #ifdef DEBUG
    315      1.1  dholland 	f = fopen ("/tmp/disktab", "w");
    316      1.1  dholland #else
    317      1.1  dholland 	f = fopen ("/etc/disktab", "w");
    318      1.1  dholland #endif
    319      1.1  dholland 	if (f == NULL) {
    320      1.1  dholland 		endwin();
    321      1.1  dholland 		(void) fprintf (stderr, "Could not open /etc/disktab");
    322      1.1  dholland 		exit (1);
    323      1.1  dholland 	}
    324      1.2    martin 	(void)fprintf (f, "%s|NetBSD installation generated:\\\n", pm->bsddiskname);
    325      1.2    martin 	(void)fprintf (f, "\t:dt=%s:ty=winchester:\\\n", pm->disktype);
    326      1.2    martin 	(void)fprintf (f, "\t:nc#%d:nt#%d:ns#%d:\\\n", pm->dlcyl, pm->dlhead, pm->dlsec);
    327      1.2    martin 	(void)fprintf (f, "\t:sc#%d:su#%" PRIu32 ":\\\n", pm->dlhead*pm->dlsec, (uint32_t)pm->dlsize);
    328      1.2    martin 	(void)fprintf (f, "\t:se#%d:%s\\\n", blk_size, pm->doessf);
    329      1.1  dholland 	for (i=0; i<8; i++) {
    330      1.2    martin 		if (pm->bsdlabel[i].pi_fstype == FS_HFS)
    331      1.1  dholland 		    (void)fprintf (f, "\t:p%c#%d:o%c#%d:t%c=macos:",
    332      1.2    martin 			       'a'+i, pm->bsdlabel[i].pi_size,
    333      1.2    martin 			       'a'+i, pm->bsdlabel[i].pi_offset,
    334      1.1  dholland 			       'a'+i);
    335      1.1  dholland 		else
    336      1.1  dholland 		    (void)fprintf (f, "\t:p%c#%d:o%c#%d:t%c=%s:",
    337      1.2    martin 			       'a'+i, pm->bsdlabel[i].pi_size,
    338      1.2    martin 			       'a'+i, pm->bsdlabel[i].pi_offset,
    339      1.2    martin 			       'a'+i, getfslabelname(pm->bsdlabel[i].pi_fstype));
    340      1.2    martin 		if (pm->bsdlabel[i].pi_fstype == FS_BSDFFS)
    341      1.1  dholland 			(void)fprintf (f, "b%c#%d:f%c#%d",
    342      1.2    martin 			   'a'+i, pm->bsdlabel[i].pi_fsize * pm->bsdlabel[i].pi_frag,
    343      1.2    martin 			   'a'+i, pm->bsdlabel[i].pi_fsize);
    344      1.1  dholland 		if (i < 7)
    345      1.1  dholland 			(void)fprintf (f, "\\\n");
    346      1.1  dholland 		else
    347      1.1  dholland 			(void)fprintf (f, "\n");
    348      1.1  dholland 	}
    349      1.1  dholland 	fclose (f);
    350      1.1  dholland 
    351      1.1  dholland 	/* Everything looks OK. */
    352      1.1  dholland 	return 1;
    353      1.1  dholland }
    354      1.1  dholland 
    355      1.1  dholland /*
    356      1.1  dholland  * any additional partition validation
    357      1.1  dholland  */
    358      1.1  dholland int
    359      1.1  dholland md_check_partitions(void)
    360      1.1  dholland {
    361      1.1  dholland 	return 1;
    362      1.1  dholland }
    363      1.1  dholland 
    364      1.1  dholland /*
    365      1.1  dholland  * hook called before writing new disklabel.
    366      1.1  dholland  */
    367      1.1  dholland int
    368      1.1  dholland md_pre_disklabel(void)
    369      1.1  dholland {
    370      1.1  dholland     int fd;
    371      1.1  dholland     char dev_name[100];
    372      1.1  dholland     struct disklabel lp;
    373      1.1  dholland     Block0 new_block0 = {APPLE_DRVR_MAP_MAGIC, 512,
    374      1.1  dholland 	 		 0, 0, 0, 0, 0, 0, 0, 0, {0}};
    375      1.1  dholland 
    376      1.1  dholland     /*
    377      1.1  dholland      * Danger Will Robinson!  We're about to turn that nice MacOS disk
    378      1.1  dholland      *  into an expensive doorstop...
    379      1.1  dholland      */
    380      1.1  dholland     printf ("%s", msg_string (MSG_dodiskmap));
    381      1.1  dholland 
    382      1.2    martin     snprintf (dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
    383      1.1  dholland     /*
    384      1.1  dholland      * Open the disk as a raw device
    385      1.1  dholland      */
    386      1.1  dholland     if ((fd = open(dev_name, O_WRONLY, 0)) < 0) {
    387      1.1  dholland 	endwin();
    388      1.1  dholland 	fprintf(stderr, "Can't open %s to rewrite the Disk Map\n", dev_name);
    389      1.1  dholland 	exit (1);
    390      1.1  dholland     }
    391      1.1  dholland     /*
    392      1.1  dholland      *  First check the pmSigPad field of the first block in the incore
    393      1.1  dholland      *  Partition Map.  It should be zero, but if it's 0xa5a5 that means
    394      1.1  dholland      *  we need to write out Block0 too.
    395      1.1  dholland      */
    396      1.1  dholland     if (map.blk[0].pmSigPad == 0xa5a5) {
    397      1.1  dholland 	if (lseek (fd, (off_t)0 * blk_size, SEEK_SET) < 0) {
    398      1.1  dholland 	    endwin();
    399      1.1  dholland 	    fprintf (stderr, "Can't position to write Block0\n");
    400      1.1  dholland 	    close (fd);
    401      1.1  dholland 	    exit (1);
    402      1.1  dholland 	}
    403      1.2    martin 	new_block0.sbBlkCount = pm->dlsize;		/* Set disk size */
    404      1.1  dholland 	if (write (fd, &new_block0, blk_size) != blk_size) {
    405      1.1  dholland 	    endwin();
    406      1.1  dholland 	    fprintf (stderr, "I/O error writing Block0\n");
    407      1.1  dholland 	    close (fd);
    408      1.1  dholland 	    exit (1);
    409      1.1  dholland 	}
    410      1.1  dholland 	map.blk[0].pmSigPad = 0;
    411      1.1  dholland     }
    412      1.1  dholland     if (lseek (fd, (off_t)1 * blk_size, SEEK_SET) < 0) {
    413      1.1  dholland 	endwin();
    414      1.1  dholland 	fprintf (stderr, "Can't position disk to rewrite Disk Map\n");
    415      1.1  dholland 	close (fd);
    416      1.1  dholland 	exit (1);
    417      1.1  dholland     }
    418      1.1  dholland     if (write (fd, map.blk, map.size * blk_size) != (map.size * blk_size)) {
    419      1.1  dholland 	endwin();
    420      1.1  dholland 	fprintf(stderr, "I/O error writing Disk Map\n");
    421      1.1  dholland 	close (fd);
    422      1.1  dholland 	exit (1);
    423      1.1  dholland     }
    424      1.1  dholland     fsync(fd);
    425      1.1  dholland     /*
    426      1.1  dholland      * Well, if we get here the dirty deed has been done.
    427      1.1  dholland      *
    428      1.1  dholland      * Now we need to force the incore disk table to get updated. This
    429      1.1  dholland      * should be done by disklabel -- which is normally called right after
    430      1.1  dholland      * we return -- but may be commented out for the mac68k port. We'll
    431      1.1  dholland      * instead update the incore table by forcing a dummy write here. This
    432      1.2    martin      * relies on a change in the mac68k-specific write_disklabel() routine.
    433      1.1  dholland      * If that change doesn't exist nothing bad happens here. If disklabel
    434      1.1  dholland      * properly updates the ondisk and incore labels everything still
    435      1.1  dholland      * works. Only if we fail here and if disklabel fails are we in
    436      1.1  dholland      * in a state where we've updated the disk but not the incore and
    437      1.1  dholland      * a reboot is necessary.
    438      1.1  dholland      *
    439      1.1  dholland      * First, we grab a copy of the incore label as it existed before
    440      1.1  dholland      * we did anything to it. Then we invoke the "write label" ioctl to
    441      1.1  dholland      * rewrite it to disk. As a result, the ondisk partition map is
    442      1.1  dholland      * re-read and the incore label is reconstructed from it. If
    443      1.1  dholland      * disklabel() is then called to update again, either that fails
    444      1.1  dholland      * because the mac68k port doesn't support native disklabels, or it
    445      1.1  dholland      * succeeds and writes out a new ondisk copy.
    446      1.1  dholland      */
    447      1.1  dholland     ioctl(fd, DIOCGDINFO, &lp);    /* Get the current disk label */
    448      1.1  dholland     ioctl(fd, DIOCWDINFO, &lp);    /* Write it out again */
    449      1.1  dholland 
    450      1.1  dholland     close (fd);
    451      1.1  dholland     return 0;
    452      1.1  dholland }
    453      1.1  dholland 
    454      1.1  dholland /*
    455      1.1  dholland  * hook called after writing disklabel to new target disk.
    456      1.1  dholland  */
    457      1.1  dholland int
    458      1.1  dholland md_post_disklabel(void)
    459      1.1  dholland {
    460      1.1  dholland     struct disklabel updated_label;
    461      1.1  dholland     int fd, i, no_match;
    462      1.1  dholland     char dev_name[100], buf[80];
    463      1.1  dholland     const char *fst[] = {"free", "swap", " v6 ", " v7 ", "sysv", "v71k",
    464      1.1  dholland 			" v8 ", "ffs ", "dos ", "lfs ", "othr", "hpfs",
    465      1.1  dholland 			"9660", "boot", "ados", "hfs ", "fcor", "ex2f",
    466      1.1  dholland 			"ntfs", "raid", "ccd "};
    467      1.1  dholland 
    468      1.2    martin     snprintf(dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
    469      1.1  dholland     /*
    470      1.1  dholland      * Open the disk as a raw device
    471      1.1  dholland      */
    472      1.1  dholland     if ((fd = open(dev_name, O_RDONLY, 0)) < 0)
    473      1.1  dholland        return 0;
    474      1.1  dholland     /*
    475      1.1  dholland      * Get the "new" label to see if we were successful.  If we aren't
    476      1.1  dholland      *  we'll return an error to keep from destroying the user's disk.
    477      1.1  dholland      */
    478      1.1  dholland     ioctl(fd, DIOCGDINFO, &updated_label);
    479      1.1  dholland     close(fd);
    480      1.1  dholland     /*
    481      1.1  dholland      * Make sure the in-core label matches the on-disk one
    482      1.1  dholland      */
    483      1.1  dholland     no_match = 0;
    484      1.1  dholland     for (i=0;i<MAXPARTITIONS;i++) {
    485      1.1  dholland         if (i > updated_label.d_npartitions)
    486      1.1  dholland            break;
    487      1.2    martin         if (pm->bsdlabel[i].pi_size != updated_label.d_partitions[i].p_size)
    488      1.1  dholland            no_match = 1;
    489      1.2    martin         if (pm->bsdlabel[i].pi_size) {
    490      1.2    martin            if (pm->bsdlabel[i].pi_offset != updated_label.d_partitions[i].p_offset)
    491      1.1  dholland                no_match = 1;
    492      1.2    martin            if (pm->bsdlabel[i].pi_fstype != updated_label.d_partitions[i].p_fstype)
    493      1.1  dholland                no_match = 1;
    494      1.1  dholland         }
    495      1.1  dholland         if (no_match)
    496      1.1  dholland            break;
    497      1.1  dholland     }
    498      1.1  dholland     /*
    499      1.1  dholland      * If the labels don't match, tell the user why
    500      1.1  dholland      */
    501      1.1  dholland     if (no_match) {
    502      1.1  dholland        msg_clear();
    503      1.1  dholland        msg_display(MSG_label_error);
    504      1.1  dholland        msg_table_add(MSG_dump_line,
    505      1.1  dholland            " in-core: offset      size type on-disk: offset      size type");
    506      1.1  dholland        for (i=0;i<MAXPARTITIONS;i++) {
    507      1.1  dholland            sprintf(buf, " %c:%13.8x%10.8x%5s%16.8x%10.8x%5s", i+'a',
    508      1.2    martin               pm->bsdlabel[i].pi_offset, pm->bsdlabel[i].pi_size,
    509      1.2    martin               fst[pm->bsdlabel[i].pi_fstype],
    510      1.1  dholland               updated_label.d_partitions[i].p_offset,
    511      1.1  dholland               updated_label.d_partitions[i].p_size,
    512      1.1  dholland               fst[updated_label.d_partitions[i].p_fstype]);
    513      1.1  dholland            msg_table_add(MSG_dump_line, buf);
    514      1.1  dholland        }
    515      1.1  dholland        process_menu(MENU_ok2, NULL);
    516      1.1  dholland     }
    517      1.1  dholland     return no_match;
    518      1.1  dholland }
    519      1.1  dholland 
    520      1.1  dholland /*
    521      1.1  dholland  * hook called after upgrade() or install() has finished setting
    522      1.1  dholland  * up the target disk but immediately before the user is given the
    523      1.1  dholland  * ``disks are now set up'' message.
    524      1.1  dholland  */
    525      1.1  dholland int
    526      1.1  dholland md_post_newfs(void)
    527      1.1  dholland {
    528      1.1  dholland 	return 0;
    529      1.1  dholland }
    530      1.1  dholland 
    531      1.1  dholland int
    532      1.1  dholland md_post_extract(void)
    533      1.1  dholland {
    534      1.1  dholland 	return 0;
    535      1.1  dholland }
    536      1.1  dholland 
    537      1.1  dholland void
    538      1.1  dholland md_cleanup_install(void)
    539      1.1  dholland {
    540      1.1  dholland #ifndef DEBUG
    541      1.1  dholland 	enable_rc_conf();
    542      1.1  dholland #endif
    543      1.1  dholland }
    544      1.1  dholland 
    545      1.1  dholland int
    546      1.1  dholland md_pre_update(void)
    547      1.1  dholland {
    548      1.1  dholland 	return 1;
    549      1.1  dholland }
    550      1.1  dholland 
    551      1.1  dholland /* Upgrade support */
    552      1.1  dholland int
    553      1.1  dholland md_update(void)
    554      1.1  dholland {
    555      1.1  dholland 	md_post_newfs();
    556      1.1  dholland 	return 1;
    557      1.1  dholland }
    558      1.1  dholland 
    559      1.1  dholland /*
    560      1.1  dholland  * Compare lexigraphically two strings
    561      1.1  dholland  */
    562      1.1  dholland static int
    563      1.1  dholland stricmp(s1, s2)
    564      1.1  dholland 	const char *s1;
    565      1.1  dholland 	const char *s2;
    566      1.1  dholland {
    567      1.1  dholland 	char c1, c2;
    568      1.1  dholland 
    569      1.1  dholland 	while (1) {
    570      1.1  dholland 	    c1 = tolower((unsigned char)*s1++);
    571      1.1  dholland 	    c2 = tolower((unsigned char)*s2++);
    572      1.1  dholland 	    if (c1 < c2) return -1;
    573      1.1  dholland 	    if (c1 > c2) return 1;
    574      1.1  dholland 	    if (c1 == 0) return 0;
    575      1.1  dholland 	}
    576      1.1  dholland }
    577      1.1  dholland 
    578      1.1  dholland static void
    579      1.1  dholland setpartition(part, in_use, slot)
    580      1.1  dholland 	struct apple_part_map_entry *part;
    581      1.1  dholland 	char in_use[];
    582      1.1  dholland 	int slot;
    583      1.1  dholland {
    584      1.1  dholland 	EBZB *bzb;
    585      1.1  dholland 
    586      1.1  dholland 	bzb = (EBZB *)&part->pmBootArgs[0];
    587      1.1  dholland 	in_use[slot] = 1;
    588      1.1  dholland 	bzb->flags.used = 1;
    589      1.1  dholland 	bzb->flags.part = 'a' + slot;
    590      1.1  dholland }
    591      1.1  dholland 
    592      1.1  dholland /*
    593      1.1  dholland  * Find an entry in a use array that is unused and return it or
    594      1.1  dholland  *  -1 if no entry is available
    595      1.1  dholland  */
    596      1.1  dholland static int
    597      1.1  dholland getFreeLabelEntry(slots)
    598      1.1  dholland 	char *slots;
    599      1.1  dholland {
    600      1.1  dholland 	int i;
    601      1.1  dholland 
    602      1.1  dholland 	for ( i = 0; i < MAXPARTITIONS; i++) {
    603      1.1  dholland 		if (i != RAW_PART && slots[i] == 0)
    604      1.1  dholland 			return i;
    605      1.1  dholland 	}
    606      1.1  dholland 	return -1;
    607      1.1  dholland }
    608      1.1  dholland 
    609      1.1  dholland /*
    610      1.1  dholland  * Figure out what type type of the given partition is and return it.
    611      1.1  dholland  */
    612      1.1  dholland int
    613      1.1  dholland whichType(part)
    614      1.1  dholland 	struct apple_part_map_entry *part;
    615      1.1  dholland {
    616      1.1  dholland 	MAP_TYPE *map_entry = (MAP_TYPE *)&map_types;
    617      1.1  dholland 	EBZB *bzb;
    618      1.1  dholland 	char partyp[32];
    619      1.1  dholland 	int type, maxsiz, entry_type = MAP_OTHER;
    620      1.1  dholland 
    621      1.1  dholland 	bzb = (EBZB *)&part->pmBootArgs[0];
    622      1.1  dholland 	if (part->pmSig != APPLE_PART_MAP_ENTRY_MAGIC)
    623      1.1  dholland 	    return 0;
    624      1.1  dholland 	maxsiz = sizeof(part->pmPartType);
    625      1.1  dholland 	if (maxsiz > (int)sizeof(partyp))
    626      1.1  dholland 	    maxsiz = sizeof(partyp);
    627      1.1  dholland 	strncpy(partyp, (char *)part->pmPartType, maxsiz);
    628      1.1  dholland 	partyp[maxsiz-1] = '\0';
    629      1.1  dholland 
    630      1.1  dholland 	/*
    631      1.1  dholland 	 * Find out how to treat the partition type under NetBSD
    632      1.1  dholland 	 */
    633      1.1  dholland 	while (map_entry->type != MAP_EOL) {
    634      1.1  dholland 	    if (stricmp(map_entry->name, partyp) == 0) {
    635      1.1  dholland 		entry_type = map_entry->type;
    636      1.1  dholland 		break;
    637      1.1  dholland 	    }
    638      1.1  dholland 	    map_entry++;
    639      1.1  dholland 	}
    640      1.1  dholland 
    641      1.1  dholland 	/*
    642      1.1  dholland 	 * Now classify the use for NetBSD
    643      1.1  dholland 	 */
    644      1.1  dholland 	if (entry_type == MAP_RESERVED)
    645      1.1  dholland 		type = 0;
    646      1.1  dholland 	else if (entry_type == MAP_NETBSD) {
    647      1.1  dholland 	    if (bzb->magic != APPLE_BZB_MAGIC)
    648      1.1  dholland 		type = 0;
    649      1.1  dholland 	    else if (bzb->type == APPLE_BZB_TYPEFS) {
    650      1.1  dholland 		if (bzb->flags.root)
    651      1.1  dholland 		    type = ROOT_PART;
    652      1.1  dholland 		else if (bzb->flags.usr)
    653      1.1  dholland 		    type = UFS_PART;
    654      1.1  dholland 		else
    655      1.1  dholland 		    type = SCRATCH_PART;
    656      1.1  dholland 	    } else if (bzb->type == APPLE_BZB_TYPESWAP)
    657      1.1  dholland 		type = SWAP_PART;
    658      1.1  dholland 	    else
    659      1.1  dholland 		type = SCRATCH_PART;
    660      1.1  dholland 	} else if (entry_type == MAP_MACOS)
    661      1.1  dholland 	    type = HFS_PART;
    662      1.1  dholland 	else
    663      1.1  dholland 	    type = SCRATCH_PART;
    664      1.1  dholland 	return type;
    665      1.1  dholland }
    666      1.1  dholland 
    667      1.1  dholland static char *
    668      1.1  dholland getFstype(part, len_type, type)
    669      1.1  dholland 	struct apple_part_map_entry *part;
    670      1.1  dholland 	int len_type;
    671      1.1  dholland 	char *type;
    672      1.1  dholland {
    673      1.1  dholland 	*type = '\0';
    674      1.1  dholland 	switch(whichType(part)) {
    675      1.1  dholland 	    case ROOT_PART:
    676      1.1  dholland 	    case UFS_PART:
    677      1.1  dholland 		strncpy(type, "4.2BSD", len_type);
    678      1.1  dholland 		break;
    679      1.1  dholland 	    case SWAP_PART:
    680      1.1  dholland 		strncpy(type, "swap", len_type);
    681      1.1  dholland 		break;
    682      1.1  dholland 	    case HFS_PART:
    683      1.1  dholland 		strncpy(type, "HFS", len_type);
    684      1.1  dholland 		break;
    685      1.1  dholland 	    case SCRATCH_PART:
    686      1.1  dholland 	    default:
    687      1.1  dholland 		break;
    688      1.1  dholland 	}
    689      1.1  dholland 	return (type);
    690      1.1  dholland }
    691      1.1  dholland 
    692      1.1  dholland static char *
    693      1.1  dholland getUse(part, len_use, use)
    694      1.1  dholland 	struct apple_part_map_entry *part;
    695      1.1  dholland 	int len_use;
    696      1.1  dholland 	char *use;
    697      1.1  dholland {
    698      1.1  dholland 	EBZB *bzb;
    699      1.1  dholland 	char partyp[32];
    700      1.1  dholland 
    701      1.1  dholland 	*use = '\0';
    702      1.1  dholland 	bzb = (EBZB *)&part->pmBootArgs[0];
    703      1.1  dholland 	switch(whichType(part)) {
    704      1.1  dholland 	    case ROOT_PART:
    705      1.1  dholland 		if (bzb->flags.usr)
    706      1.1  dholland 		    strncpy(use, "Root&Usr", len_use);
    707      1.1  dholland 		else
    708      1.1  dholland 		    strncpy(use, "Root", len_use);
    709      1.1  dholland 		break;
    710      1.1  dholland 	    case UFS_PART:
    711      1.1  dholland 		strncpy(use, "Usr", len_use);
    712      1.1  dholland 		break;
    713      1.1  dholland 	    case SWAP_PART:
    714      1.1  dholland 		break;
    715      1.1  dholland 	    case HFS_PART:
    716      1.1  dholland 		strncpy(use, "MacOS", len_use);
    717      1.1  dholland 		break;
    718      1.1  dholland 	    case SCRATCH_PART:
    719      1.1  dholland 		strncpy(partyp, (char *)part->pmPartType, sizeof(partyp));
    720      1.1  dholland 		partyp[sizeof(partyp)-1] = '\0';
    721      1.1  dholland 		if (stricmp("Apple_Free", partyp) == 0)
    722      1.1  dholland 		    strncpy(use, "Free", len_use);
    723      1.1  dholland 		else if (stricmp("Apple_Scratch", partyp) == 0)
    724      1.1  dholland 		    strncpy(use, "Scratch", len_use);
    725      1.1  dholland 		else if (stricmp("Apple_MFS", partyp) == 0)
    726      1.1  dholland 		    strncpy(use, "MFS", len_use);
    727      1.1  dholland 		else if (stricmp("Apple_PRODOS", partyp) == 0)
    728      1.1  dholland 		    strncpy(use, "PRODOS", len_use);
    729      1.1  dholland 		else
    730      1.1  dholland 		    strncpy(use, "unknown", len_use);
    731      1.1  dholland 	    default:
    732      1.1  dholland 		break;
    733      1.1  dholland 	}
    734      1.1  dholland 	return(use);
    735      1.1  dholland }
    736      1.1  dholland 
    737      1.1  dholland static char *
    738      1.1  dholland getName(part, len_name, name)
    739      1.1  dholland 	struct apple_part_map_entry *part;
    740      1.1  dholland 	int len_name;
    741      1.1  dholland 	char *name;
    742      1.1  dholland {
    743      1.1  dholland 	EBZB *bzb;
    744      1.1  dholland 	int fd;
    745      1.1  dholland 	off_t seek;
    746      1.1  dholland 	char dev_name[100], macosblk[512];
    747      1.1  dholland 
    748      1.1  dholland 	*name = '\0';
    749      1.1  dholland 	bzb = (EBZB *)&part->pmBootArgs[0];
    750      1.1  dholland 	switch(whichType(part)) {
    751      1.1  dholland 	    case SCRATCH_PART:
    752      1.1  dholland 	    case ROOT_PART:
    753      1.1  dholland 	    case UFS_PART:
    754      1.1  dholland 		strncpy(name, (char *)bzb->mount_point, len_name);
    755      1.1  dholland 		break;
    756      1.1  dholland 	    case SWAP_PART:
    757      1.1  dholland 		break;
    758      1.1  dholland 	    case HFS_PART:
    759      1.1  dholland 		/*
    760      1.1  dholland 		 * OK, this is stupid but it's damn nice to know!
    761      1.1  dholland 		 */
    762      1.2    martin 		snprintf (dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
    763      1.1  dholland 		/*
    764      1.1  dholland 		 * Open the disk as a raw device
    765      1.1  dholland 		 */
    766      1.1  dholland 		if ((fd = open(dev_name, O_RDONLY, 0)) >= 0) {
    767      1.1  dholland 		    seek = (off_t)part->pmPyPartStart + (off_t)2;
    768      1.1  dholland 		    seek *= (off_t)blk_size;
    769      1.1  dholland 		    lseek(fd, seek, SEEK_SET);
    770      1.1  dholland 		    read(fd, &macosblk, sizeof(macosblk));
    771      1.1  dholland 		    macosblk[37+32] = '\0';
    772      1.1  dholland 		    strncpy(name, (char *)bzb->mount_point, len_name);
    773      1.1  dholland 		    strncat(name, " (", len_name-strlen(name));
    774      1.1  dholland 		    strncat(name, &macosblk[37], len_name-strlen(name));
    775      1.1  dholland 		    strncat(name, ")", len_name-strlen(name));
    776      1.1  dholland 		    close(fd);
    777      1.1  dholland 		}
    778      1.1  dholland 		break;
    779      1.1  dholland 	    default:
    780      1.1  dholland 		break;
    781      1.1  dholland 	}
    782      1.1  dholland 	return(name);
    783      1.1  dholland }
    784      1.1  dholland 
    785      1.1  dholland /*
    786      1.1  dholland  * Find the first occurance of a Standard Type partition and
    787      1.1  dholland  *  mark it for use along with the default mount slot.
    788      1.1  dholland  */
    789      1.1  dholland static int
    790      1.1  dholland findStdType(num_parts, in_use, type, count, alt)
    791      1.1  dholland 	int num_parts;
    792      1.1  dholland 	char in_use[];
    793      1.1  dholland 	int type;
    794      1.1  dholland 	int *count;
    795      1.1  dholland 	int alt;
    796      1.1  dholland {
    797      1.1  dholland 	EBZB *bzb;
    798      1.1  dholland 	int i;
    799      1.1  dholland 
    800      1.1  dholland 	for (i = 0; i < num_parts; i++) {
    801      1.1  dholland 		bzb = (EBZB *)&map.blk[i].pmBootArgs[0];
    802      1.1  dholland 		if (whichType(&map.blk[i]) != type || bzb->flags.used)
    803      1.1  dholland 			continue;
    804      1.1  dholland 		if (type == ROOT_PART) {
    805      1.1  dholland 			if (alt >= 0 && alt != bzb->cluster)
    806      1.1  dholland 				continue;
    807      1.1  dholland 			setpartition(&map.blk[i], in_use, 0);
    808      1.1  dholland 			strcpy ((char *)bzb->mount_point, "/");
    809      1.1  dholland 			*count += 1;
    810      1.1  dholland 		} else if (type == UFS_PART) {
    811      1.1  dholland 			if (alt >= 0 && alt != bzb->cluster)
    812      1.1  dholland 				continue;
    813      1.1  dholland 			setpartition(&map.blk[i], in_use, 6);
    814      1.1  dholland 			if (bzb->mount_point[0] == '\0')
    815      1.1  dholland 				strcpy ((char *)bzb->mount_point, "/usr");
    816      1.1  dholland 			*count += 1;
    817      1.1  dholland 		} else if (type == SWAP_PART) {
    818      1.1  dholland 			setpartition(&map.blk[i], in_use, 1);
    819      1.1  dholland 			*count += 1;
    820      1.1  dholland 		}
    821      1.1  dholland 		return 0;
    822      1.1  dholland 	}
    823      1.1  dholland 	return -1;
    824      1.1  dholland }
    825      1.1  dholland 
    826      1.1  dholland /*
    827      1.1  dholland  * Reset the flags and reserved fields in the selected partition.
    828      1.1  dholland  * This functions isn't called to process any of the reserved partitions
    829      1.1  dholland  * where the boot code for MacOS is stored, so (hopefully) we won't
    830      1.1  dholland  * do more damage that we're trying to avoid.  Eventually the NetBSD
    831      1.1  dholland  * Boot Code will need to go into a partition too, but that should go
    832      1.1  dholland  * into a reserved partition as well.  I'd suggest using a partition
    833      1.1  dholland  * named something like "NetBSD_Boot" with a pmPartName of "Macintosh".
    834      1.1  dholland  * The Apple Start Manager (in ROM) will then recognize the partition
    835      1.1  dholland  * as the one containing the system bootstrip for the volume.
    836      1.1  dholland  */
    837      1.1  dholland void
    838      1.1  dholland reset_part_flags(part)
    839      1.1  dholland 	struct apple_part_map_entry *part;
    840      1.1  dholland {
    841      1.1  dholland 	EBZB *bzb;
    842      1.1  dholland 
    843      1.1  dholland 	/*
    844      1.1  dholland 	 * Clear out the MacOS fields that might be used for booting just
    845      1.1  dholland 	 *  in case we've clobbered the boot code.
    846      1.1  dholland 	 */
    847      1.1  dholland 	part->pmLgDataStart = 0;
    848      1.1  dholland 	part->pmPartStatus = 0x77;  /* make sure the partition shows up */
    849      1.1  dholland 	part->pmLgBootStart = 0;
    850      1.1  dholland 	part->pmBootSize = 0;
    851      1.1  dholland 	part->pmBootLoad = 0;
    852      1.1  dholland 	part->pmBootLoad2 = 0;
    853      1.1  dholland 	part->pmBootEntry = 0;
    854      1.1  dholland 	part->pmBootEntry2 = 0;
    855      1.1  dholland 	part->pmBootCksum = 0;
    856      1.1  dholland 
    857      1.1  dholland 	/*
    858      1.1  dholland 	 * Clear out all the NetBSD fields too.  We only clear out the ones
    859      1.1  dholland 	 *  that should get reset during our processing.
    860      1.1  dholland 	 */
    861      1.1  dholland 	bzb = (EBZB *)&part->pmBootArgs[0];
    862      1.1  dholland 	bzb->magic = 0;
    863      1.1  dholland 	bzb->cluster = 0;
    864      1.1  dholland 	bzb->inode = 0;
    865      1.1  dholland 	bzb->type = 0;
    866      1.1  dholland 	bzb->flags.root = 0;
    867      1.1  dholland 	bzb->flags.usr = 0;
    868      1.1  dholland 	bzb->flags.crit = 0;
    869      1.1  dholland 	bzb->flags.slice = 0;
    870      1.1  dholland 	bzb->flags.used = 0;
    871      1.1  dholland 	return;
    872      1.1  dholland }
    873      1.1  dholland 
    874      1.1  dholland /*
    875      1.1  dholland  * sortmerge:
    876      1.1  dholland  *  1) Moves all the Partition Map entries to the front of the Map.
    877      1.1  dholland  *     This is required because some disk formatters leave holes.
    878      1.1  dholland  *  2) Sorts all entries by ascending start block number.
    879      1.1  dholland  *     Needed so the NetBSD algorithm for finding partitions works
    880      1.1  dholland  *     consistently from a user perspective.
    881      1.1  dholland  *  3) Collapse multiple adjected "free" entries into a single entry.
    882      1.1  dholland  *  4) Identify the NetBSD mount_points.
    883      1.1  dholland  */
    884      1.1  dholland void
    885      1.1  dholland sortmerge(void)
    886      1.1  dholland {
    887      1.1  dholland     struct apple_part_map_entry tmp_blk;
    888      1.1  dholland     char in_use[MAXPARTITIONS];
    889      1.1  dholland     int i, j;
    890      1.1  dholland     EBZB *bzb;
    891      1.1  dholland 
    892      1.1  dholland     /*
    893      1.1  dholland      * Step 1, squeeze out the holes that some disk formatters leave in
    894      1.1  dholland      *  the Map.  Also convert any "old" Map entries to the new entry
    895      1.1  dholland      *  type. Also clear out our used flag which is used to indicte
    896      1.1  dholland      *  we've mapped the partition.
    897      1.1  dholland      */
    898      1.1  dholland     map.in_use_cnt = 0;
    899      1.1  dholland     for (i=0;i<map.size-1;i++) {
    900      1.1  dholland 	if (map.blk[i].pmSig == 0x5453)
    901      1.1  dholland 	    map.blk[i].pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
    902      1.1  dholland 	if (map.blk[i].pmSig != APPLE_PART_MAP_ENTRY_MAGIC) {
    903      1.1  dholland 	    for (j=i+1;j<map.size;j++) {
    904      1.1  dholland 		if (map.blk[j].pmSig == 0x5453)
    905      1.1  dholland 		    map.blk[j].pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
    906      1.1  dholland 		if (map.blk[j].pmSig == APPLE_PART_MAP_ENTRY_MAGIC) {
    907      1.1  dholland 		    memcpy (&map.blk[i], &map.blk[j], sizeof(map.blk[i]));
    908      1.1  dholland 		    map.blk[j].pmSig = 0;
    909      1.1  dholland 		    break;
    910      1.1  dholland 		}
    911      1.1  dholland 	    }
    912      1.1  dholland 	} else {
    913      1.1  dholland 	    map.in_use_cnt += 1;
    914      1.1  dholland 	    bzb = (EBZB *)&map.blk[i].pmBootArgs[0];
    915      1.1  dholland 	    bzb->flags.used = 0;
    916      1.1  dholland 	    bzb->flags.part = 0;
    917      1.1  dholland 	}
    918      1.1  dholland     }
    919      1.1  dholland 
    920      1.1  dholland     /*
    921      1.1  dholland      * Step 2, sort by ascending starting block number.  Since
    922      1.1  dholland      *         we've already removed the holes we only need to
    923      1.1  dholland      *         deal with the in_use count of blocks.
    924      1.1  dholland      */
    925      1.1  dholland     for (i=0;i<map.in_use_cnt-1;i++) {
    926      1.1  dholland 	for (j=i+1;j<map.in_use_cnt;j++) {
    927      1.1  dholland 	    if (map.blk[i].pmPyPartStart > map.blk[j].pmPyPartStart) {
    928      1.1  dholland 		memcpy (&tmp_blk, &map.blk[i], sizeof(tmp_blk));
    929      1.1  dholland 		memcpy (&map.blk[i], &map.blk[j], sizeof(map.blk[i]));
    930      1.1  dholland 		memcpy (&map.blk[j], &tmp_blk, sizeof(map.blk[j]));
    931      1.1  dholland 	    }
    932      1.1  dholland 	}
    933      1.1  dholland     }
    934      1.1  dholland 
    935      1.1  dholland     /*
    936      1.1  dholland      * Step 3, merge adjacent free space
    937      1.1  dholland      */
    938      1.1  dholland     for (i=0;i<map.in_use_cnt-1;i++) {
    939      1.1  dholland         if (stricmp("Apple_Free", (char *)map.blk[i].pmPartType) == 0 &&
    940      1.1  dholland 	    stricmp("Apple_Free", (char *)map.blk[i+1].pmPartType) == 0) {
    941      1.1  dholland 	    map.blk[i].pmPartBlkCnt += map.blk[i+1].pmPartBlkCnt;
    942      1.1  dholland 	    map.blk[i].pmDataCnt += map.blk[i+1].pmDataCnt;
    943      1.1  dholland 	    map.blk[i+1].pmSig = 0;
    944      1.1  dholland 	    for (j=i+1;j<map.in_use_cnt-1;j++) {
    945      1.1  dholland 		memcpy (&map.blk[j], &map.blk[j+1], sizeof(map.blk[j]));
    946      1.1  dholland 		map.blk[j+1].pmSig = 0;
    947      1.1  dholland 	    }
    948      1.1  dholland 	    map.in_use_cnt -= 1;
    949      1.1  dholland 	}
    950      1.1  dholland     }
    951      1.1  dholland 
    952      1.1  dholland     /*
    953      1.1  dholland      * Step 4, try to identify the mount points for the partitions
    954      1.1  dholland      *         and adjust the pmMapBlkCnt in each Map entry.  Set
    955      1.1  dholland      *         up the display array for the non-reserved partitions,
    956      1.1  dholland      *         and count the number of NetBSD usable partitions
    957      1.1  dholland      */
    958      1.1  dholland     map.hfs_cnt = 0;
    959      1.1  dholland     map.root_cnt = 0;
    960      1.1  dholland     map.swap_cnt = 0;
    961      1.1  dholland     map.usr_cnt = 0;
    962      1.1  dholland     map.usable_cnt = 0;
    963      1.1  dholland     /*
    964      1.1  dholland      * Clear out record of partition slots already in use
    965      1.1  dholland      */
    966      1.1  dholland     memset(&in_use, 0, sizeof(in_use));
    967      1.1  dholland     for (i=0,j=0;i<map.in_use_cnt;i++) {
    968      1.1  dholland         map.blk[i].pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
    969      1.1  dholland         map.blk[i].pmMapBlkCnt = map.in_use_cnt;
    970      1.1  dholland 	/*
    971      1.1  dholland 	 * Since MAXPARTITIONS == 8 for mac68k, and we do not display
    972      1.1  dholland 	 * the c partition, we only need 7 partition slots on the screen.
    973      1.1  dholland 	 * If/when MAXPARTITIONS is changed, the "Edit Disk Partition Map"
    974      1.1  dholland 	 * needs to be a scrollable view of the partition table.
    975      1.1  dholland 	 */
    976      1.1  dholland         if (whichType(&map.blk[i]) && (j < MAXPARTITIONS - 1)) {
    977      1.1  dholland 		map.mblk[j++] = i;
    978      1.1  dholland 		map.usable_cnt += 1;
    979      1.1  dholland 	}
    980      1.1  dholland     }
    981      1.1  dholland     /*
    982      1.1  dholland      * Fill in standard partitions.  Look for a Cluster "0" first and use
    983      1.1  dholland      *  it, otherwise take any Cluster value.
    984      1.1  dholland      */
    985      1.1  dholland     if (findStdType(map.in_use_cnt, in_use, ROOT_PART, &map.root_cnt, 0))
    986      1.1  dholland 	findStdType(map.in_use_cnt, in_use, ROOT_PART, &map.root_cnt, -1);
    987      1.1  dholland     if (findStdType(map.in_use_cnt, in_use, UFS_PART, &map.usr_cnt, 0))
    988      1.1  dholland 	findStdType(map.in_use_cnt, in_use, UFS_PART, &map.usr_cnt, -1);
    989      1.1  dholland     if (findStdType(map.in_use_cnt, in_use, SWAP_PART, &map.swap_cnt, 0))
    990      1.1  dholland 	findStdType(map.in_use_cnt, in_use, SWAP_PART, &map.swap_cnt, -1);
    991      1.1  dholland 
    992      1.1  dholland #ifdef MD_DEBUG_SORT_MERGE
    993      1.1  dholland 	md_debug_dump("After marking Standard Types");
    994      1.1  dholland #endif
    995      1.1  dholland     /*
    996      1.1  dholland      * Now fill in the remaining partitions counting them by type and
    997      1.1  dholland      *  assigning them the slot the where the kernel should map them.
    998      1.1  dholland      * This will be where they are displayed in the Edit Map.
    999      1.1  dholland      */
   1000      1.1  dholland     for (i=0; i < map.in_use_cnt; i++) {
   1001      1.1  dholland 	bzb = (EBZB *)&map.blk[i].pmBootArgs[0];
   1002      1.1  dholland 	if (bzb->flags.used == 0) {
   1003      1.1  dholland 	    if ((j = getFreeLabelEntry(in_use)) < 0)
   1004      1.1  dholland 		break;
   1005      1.1  dholland 	    switch (whichType(&map.blk[i])) {
   1006      1.1  dholland 		case ROOT_PART:
   1007      1.1  dholland 		    map.root_cnt += 1;
   1008      1.1  dholland 		    setpartition(&map.blk[i], in_use, j);
   1009      1.1  dholland 		    break;
   1010      1.1  dholland 		case UFS_PART:
   1011      1.1  dholland 		    map.usr_cnt += 1;
   1012      1.1  dholland 		    setpartition(&map.blk[i], in_use, j);
   1013      1.1  dholland 		    break;
   1014      1.1  dholland 		case SWAP_PART:
   1015      1.1  dholland 		    map.swap_cnt += 1;
   1016      1.1  dholland 		    setpartition(&map.blk[i], in_use, j);
   1017      1.1  dholland 		    break;
   1018      1.1  dholland 		case HFS_PART:
   1019      1.1  dholland 		    map.hfs_cnt += 1;
   1020      1.1  dholland 		    setpartition(&map.blk[i], in_use, j);
   1021      1.1  dholland 		    break;
   1022      1.1  dholland 		case SCRATCH_PART:
   1023      1.1  dholland 		    setpartition(&map.blk[i], in_use, j);
   1024      1.1  dholland 		default:
   1025      1.1  dholland 		    break;
   1026      1.1  dholland 	    }
   1027      1.1  dholland 	}
   1028      1.1  dholland     }
   1029      1.1  dholland #ifdef MD_DEBUG_SORT_MERGE
   1030      1.1  dholland 	md_debug_dump("After sort merge");
   1031      1.1  dholland #endif
   1032      1.1  dholland     return;
   1033      1.1  dholland }
   1034      1.1  dholland 
   1035      1.1  dholland void
   1036      1.1  dholland disp_selected_part(sel)
   1037      1.1  dholland 	int sel;
   1038      1.1  dholland {
   1039      1.1  dholland 	int i,j;
   1040      1.1  dholland 	char fstyp[16], use[16], name[32];
   1041      1.1  dholland 	EBZB *bzb;
   1042      1.1  dholland 
   1043      1.1  dholland 	msg_table_add(MSG_part_header);
   1044      1.1  dholland 	for (i=0;i<map.usable_cnt;i++) {
   1045      1.1  dholland 	    if (i == sel) msg_standout();
   1046      1.1  dholland 	    j = map.mblk[i];
   1047      1.1  dholland 	    getFstype(&map.blk[j], sizeof(fstyp), fstyp);
   1048      1.1  dholland 	    getUse(&map.blk[j], sizeof(use), use);
   1049      1.1  dholland 	    getName(&map.blk[j], sizeof(name), name);
   1050      1.1  dholland 	    bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
   1051      1.2    martin 	    msg_table_add(MSG_part_row, pm->diskdev,
   1052      1.1  dholland 		bzb->flags.part, map.blk[j].pmPyPartStart,
   1053      1.1  dholland                  map.blk[j].pmPartBlkCnt, fstyp, use, name);
   1054      1.1  dholland 	    if (i == sel) msg_standend();
   1055      1.1  dholland 	}
   1056      1.1  dholland 	return;
   1057      1.1  dholland }
   1058      1.1  dholland 
   1059      1.1  dholland /*
   1060      1.1  dholland  * check for any anomalies on the requested setup
   1061      1.1  dholland  */
   1062      1.1  dholland static int
   1063      1.1  dholland check_for_errors()
   1064      1.1  dholland {
   1065      1.1  dholland     int i, j;
   1066      1.1  dholland     int errs = 0;
   1067      1.1  dholland 
   1068      1.1  dholland     errs = (!map.root_cnt) || (map.root_cnt > 1) || (!map.swap_cnt) ||
   1069      1.1  dholland 	   (map.swap_cnt > 1);
   1070      1.1  dholland 
   1071      1.1  dholland     for (i=0;i<map.usable_cnt;i++) {
   1072      1.1  dholland 	j = map.mblk[i];
   1073      1.2    martin 	if (map.blk[j].pmPyPartStart > pm->dlsize)
   1074      1.1  dholland 		errs++;
   1075      1.2    martin 	if ((map.blk[j].pmPyPartStart + map.blk[j].pmPartBlkCnt) > pm->dlsize + 1)
   1076      1.1  dholland 		errs++;
   1077      1.1  dholland     }
   1078      1.1  dholland     return(errs);
   1079      1.1  dholland }
   1080      1.1  dholland 
   1081      1.1  dholland /*
   1082      1.1  dholland  * check for and report anomalies on the requested setup
   1083      1.1  dholland  */
   1084      1.1  dholland void
   1085      1.1  dholland report_errors()
   1086      1.1  dholland {
   1087      1.1  dholland     int i, j;
   1088      1.1  dholland     int errs = 0;
   1089      1.1  dholland     EBZB *bzb;
   1090      1.1  dholland 
   1091      1.1  dholland     if (!map.root_cnt) {
   1092      1.1  dholland 	msg_display_add(MSG_disksetup_no_root);
   1093      1.1  dholland 	errs++;
   1094      1.1  dholland     }
   1095      1.1  dholland     if (map.root_cnt > 1) {
   1096      1.1  dholland 	msg_display_add(MSG_disksetup_multiple_roots);
   1097      1.1  dholland 	errs++;
   1098      1.1  dholland     }
   1099      1.1  dholland     if (!map.swap_cnt) {
   1100      1.1  dholland 	msg_display_add(MSG_disksetup_no_swap);
   1101      1.1  dholland 	errs++;
   1102      1.1  dholland     }
   1103      1.1  dholland     if (map.swap_cnt > 1) {
   1104      1.1  dholland 	msg_display_add(MSG_disksetup_multiple_swaps);
   1105      1.1  dholland 	errs++;
   1106      1.1  dholland     }
   1107      1.1  dholland     for (i=0;i<map.usable_cnt;i++) {
   1108      1.1  dholland 	j = map.mblk[i];
   1109      1.2    martin 	if (map.blk[j].pmPyPartStart > pm->dlsize) {
   1110      1.1  dholland 	    bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
   1111      1.1  dholland 	    msg_display_add(MSG_disksetup_part_beginning,
   1112      1.2    martin 		pm->diskdev, bzb->flags.part);
   1113      1.1  dholland 	    errs++;
   1114      1.1  dholland 	}
   1115      1.2    martin 	if ((map.blk[j].pmPyPartStart + map.blk[j].pmPartBlkCnt) > pm->dlsize) {
   1116      1.1  dholland 	    bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
   1117      1.1  dholland 	    msg_display_add(MSG_disksetup_part_size,
   1118      1.2    martin 		pm->diskdev, bzb->flags.part);
   1119      1.1  dholland 	    errs++;
   1120      1.1  dholland 	}
   1121      1.1  dholland     }
   1122      1.1  dholland     if (!errs)
   1123      1.1  dholland 	msg_display_add(MSG_disksetup_noerrors);
   1124      1.1  dholland     return;
   1125      1.1  dholland }
   1126      1.1  dholland 
   1127      1.1  dholland static int
   1128      1.1  dholland edit_diskmap(void)
   1129      1.1  dholland {
   1130      1.1  dholland     int i;
   1131      1.1  dholland 
   1132      1.1  dholland 	/* Ask full/part */
   1133      1.2    martin 	msg_display (MSG_fullpart, pm->diskdev);
   1134      1.1  dholland 	process_menu (MENU_fullpart, NULL);
   1135      1.1  dholland 
   1136      1.1  dholland 	map.selected = 0;
   1137      1.1  dholland 	sortmerge();
   1138      1.1  dholland 
   1139      1.1  dholland 	/* If blowing away the whole disk, let user know if there
   1140      1.1  dholland 	 *  are any active disk partitions */
   1141      1.1  dholland 	if (usefull) {
   1142      1.1  dholland 	    if (map.usable_cnt > (map.root_cnt+map.swap_cnt+map.usr_cnt)) {
   1143      1.1  dholland 		msg_display (MSG_ovrwrite);
   1144  1.2.4.1       snj 		if (!ask_noyes(NULL)) {
   1145      1.1  dholland 			endwin();
   1146      1.1  dholland 			return 0;
   1147      1.1  dholland 		}
   1148      1.1  dholland 	    }
   1149      1.1  dholland 	    /*
   1150      1.1  dholland 	     * mark all non-reserved partitions as "free"
   1151      1.1  dholland 	     *  then sort and merge the map into something sensible
   1152      1.1  dholland 	     */
   1153      1.1  dholland 	    for (i=0;i<map.size;i++)
   1154      1.1  dholland 		if (whichType(&map.blk[i]))
   1155      1.1  dholland 		    strcpy ((char *)map.blk[i].pmPartType, "Apple_Free");
   1156      1.1  dholland 	    sortmerge();
   1157      1.1  dholland 	}
   1158      1.1  dholland 	process_menu (MENU_editparttable, NULL);
   1159      1.1  dholland 	return 1;
   1160      1.1  dholland }
   1161      1.1  dholland 
   1162      1.1  dholland #ifdef MD_DEBUG_SORT_MERGE
   1163      1.1  dholland static int
   1164      1.1  dholland md_debug_dump(title)
   1165      1.1  dholland 	char *title;
   1166      1.1  dholland {
   1167      1.1  dholland 	char buf[96], type;
   1168      1.1  dholland 	char fstyp[16], use[16], name[64];
   1169  1.2.4.1       snj 	int i, j, rv;
   1170      1.1  dholland 	EBZB *bzb;
   1171      1.1  dholland 
   1172      1.1  dholland 	msg_clear();
   1173      1.1  dholland 	sprintf(buf, "Apple Disk Partition Map: %s", title);
   1174      1.1  dholland 	msg_table_add(MSG_dump_line, buf);
   1175      1.1  dholland 	msg_table_add(MSG_dump_line,
   1176      1.1  dholland            "slot      base   fstype        use name");
   1177      1.1  dholland 	for (i=0;i<map.in_use_cnt;i++) {
   1178      1.1  dholland 	   j = whichType(&map.blk[i]);
   1179      1.1  dholland 	   getFstype(&map.blk[i], sizeof(fstyp), fstyp);
   1180      1.1  dholland 	   getUse(&map.blk[i], sizeof(use), use);
   1181      1.1  dholland 	   getName(&map.blk[i], sizeof(name), name);
   1182      1.1  dholland 	   bzb = (EBZB *) &map.blk[i].pmBootArgs[0];
   1183      1.1  dholland 	   type = bzb->flags.part;
   1184      1.1  dholland 	   if (type < 'a' || type > 'h') type = '?';
   1185      1.1  dholland 	   if (j == 0) strcpy (name, "reserved for Apple");
   1186      1.1  dholland            sprintf(buf, " %02d:%c %08x %8s %10s %s", i+1,  type,
   1187      1.1  dholland 		map.blk[i].pmPyPartStart, fstyp, use, name);
   1188      1.1  dholland            msg_table_add(MSG_dump_line, buf);
   1189      1.1  dholland 	}
   1190  1.2.4.1       snj 	process_menu(MENU_okabort, &rv);
   1191      1.1  dholland 	msg_clear();
   1192  1.2.4.1       snj 	return rv;
   1193      1.1  dholland }
   1194      1.1  dholland #endif /* MD_DEBUG_SORT_MERGE */
   1195      1.1  dholland 
   1196      1.1  dholland int
   1197      1.1  dholland md_pre_mount()
   1198      1.1  dholland {
   1199      1.1  dholland 	return 0;
   1200      1.1  dholland }
   1201