1*2e9df72eSandvar /* $NetBSD: md.c,v 1.12 2022/04/07 19:33:38 andvar Exp $ */
250dbef1aSdholland
350dbef1aSdholland /*
450dbef1aSdholland * Copyright 1997 Piermont Information Systems Inc.
550dbef1aSdholland * All rights reserved.
650dbef1aSdholland *
750dbef1aSdholland * Based on code written by Philip A. Nelson for Piermont Information
850dbef1aSdholland * Systems Inc.
950dbef1aSdholland *
1050dbef1aSdholland * Redistribution and use in source and binary forms, with or without
1150dbef1aSdholland * modification, are permitted provided that the following conditions
1250dbef1aSdholland * are met:
1350dbef1aSdholland * 1. Redistributions of source code must retain the above copyright
1450dbef1aSdholland * notice, this list of conditions and the following disclaimer.
1550dbef1aSdholland * 2. Redistributions in binary form must reproduce the above copyright
1650dbef1aSdholland * notice, this list of conditions and the following disclaimer in the
1750dbef1aSdholland * documentation and/or other materials provided with the distribution.
1850dbef1aSdholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
1950dbef1aSdholland * or promote products derived from this software without specific prior
2050dbef1aSdholland * written permission.
2150dbef1aSdholland *
2250dbef1aSdholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
2350dbef1aSdholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2450dbef1aSdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2550dbef1aSdholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2650dbef1aSdholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2750dbef1aSdholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2850dbef1aSdholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2950dbef1aSdholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3050dbef1aSdholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3150dbef1aSdholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3250dbef1aSdholland * THE POSSIBILITY OF SUCH DAMAGE.
3350dbef1aSdholland */
3450dbef1aSdholland
3550dbef1aSdholland /* md.c -- mac68k machine specific routines */
3650dbef1aSdholland
3750dbef1aSdholland #include <stdio.h>
3850dbef1aSdholland #include <util.h>
3950dbef1aSdholland #include <unistd.h>
4050dbef1aSdholland #include <fcntl.h>
4150dbef1aSdholland #include <sys/ioctl.h>
4250dbef1aSdholland #include <sys/utsname.h>
4350dbef1aSdholland #include <machine/int_fmtio.h>
4450dbef1aSdholland
4550dbef1aSdholland #include "defs.h"
4650dbef1aSdholland #include "md.h"
4750dbef1aSdholland #include "msg_defs.h"
4850dbef1aSdholland #include "menu_defs.h"
4950dbef1aSdholland
5050dbef1aSdholland static int stricmp(const char *c1, const char *c2);
5150dbef1aSdholland static void setpartition(struct apple_part_map_entry *, char *, int);
5250dbef1aSdholland static int getFreeLabelEntry(char *);
5350dbef1aSdholland static char *getFstype(struct apple_part_map_entry *, int, char *);
5450dbef1aSdholland static char *getUse(struct apple_part_map_entry *, int, char *);
5550dbef1aSdholland static char *getName(struct apple_part_map_entry *, int, char *);
5650dbef1aSdholland static int findStdType(int, char *, int, int *, int);
5750dbef1aSdholland static int check_for_errors(void);
5850dbef1aSdholland static int edit_diskmap(void);
5950dbef1aSdholland #ifdef MD_DEBUG_SORT_MERGE
6050dbef1aSdholland static int md_debug_dump(char *);
6150dbef1aSdholland #endif
6250dbef1aSdholland
6350dbef1aSdholland int blk_size;
6450dbef1aSdholland
6550dbef1aSdholland MAP_TYPE map_types[] = {
6650dbef1aSdholland {MAP_RESERVED, APPLE_PART_TYPE_DRIVER},
6750dbef1aSdholland {MAP_RESERVED, APPLE_PART_TYPE_DRIVER43},
6850dbef1aSdholland {MAP_RESERVED, APPLE_PART_TYPE_DRIVERATA},
6950dbef1aSdholland {MAP_RESERVED, APPLE_PART_TYPE_FWB_COMPONENT},
7050dbef1aSdholland {MAP_MACOS, APPLE_PART_TYPE_MAC},
7150dbef1aSdholland {MAP_NETBSD, APPLE_PART_TYPE_NETBSD},
7250dbef1aSdholland {MAP_RESERVED, APPLE_PART_TYPE_PARTMAP},
7350dbef1aSdholland {MAP_OTHER, APPLE_PART_TYPE_SCRATCH},
7450dbef1aSdholland {MAP_NETBSD, APPLE_PART_TYPE_UNIX},
7550dbef1aSdholland {MAP_EOL, NULL}
7650dbef1aSdholland };
7750dbef1aSdholland
7850dbef1aSdholland MAP map = {0, 0, 0, 0, 0, 0, 0, 0, {0}, NULL};
7950dbef1aSdholland
8050dbef1aSdholland struct apple_part_map_entry new_map[] =
8150dbef1aSdholland {
8250dbef1aSdholland { APPLE_PART_MAP_ENTRY_MAGIC, 0xa5a5, 6, 1, NEW_MAP_SIZE & 0x7e,
8350dbef1aSdholland "Apple", "Apple_Partition_Map", 0, NEW_MAP_SIZE, 0x37,
8450dbef1aSdholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
8550dbef1aSdholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 64, 32,
8650dbef1aSdholland "Macintosh", "Apple_Driver", 0, 0, 0x37,
8750dbef1aSdholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
8850dbef1aSdholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 96, 64,
8950dbef1aSdholland "Macintosh", "Apple_Driver43", 0, 0, 0x37,
9050dbef1aSdholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
9150dbef1aSdholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 160, 64,
9250dbef1aSdholland "Macintosh", "Apple_Driver_ATA", 0, 0, 0x37,
9350dbef1aSdholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
9450dbef1aSdholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6, 224, 4096,
9550dbef1aSdholland "untitled", "Apple_HFS", 0, 0, 0x37,
9650dbef1aSdholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}},
9750dbef1aSdholland { APPLE_PART_MAP_ENTRY_MAGIC, 0, 6,4320, 0,
9850dbef1aSdholland "untitled", "Apple_Free", 0, 0, 0x37,
9950dbef1aSdholland 0, 0, 0, 0, 0, 0, 0, {0}, {0}, {0}}
10050dbef1aSdholland };
10150dbef1aSdholland
10250dbef1aSdholland void
md_init(void)10350dbef1aSdholland md_init(void)
10450dbef1aSdholland {
10550dbef1aSdholland }
10650dbef1aSdholland
10750dbef1aSdholland void
md_init_set_status(int flags)10850dbef1aSdholland md_init_set_status(int flags)
10950dbef1aSdholland {
11050dbef1aSdholland struct utsname instsys;
11150dbef1aSdholland
11250dbef1aSdholland (void)flags;
11350dbef1aSdholland
11450dbef1aSdholland /*
11550dbef1aSdholland * Get the name of the Install Kernel we are running under and
11650dbef1aSdholland * enable the installation of the corresponding GENERIC kernel.
11750dbef1aSdholland *
11850dbef1aSdholland * Note: In md.h the two kernels are disabled. If they are
11950dbef1aSdholland * enabled there the logic here needs to be switched.
12050dbef1aSdholland */
12150dbef1aSdholland uname(&instsys);
12250dbef1aSdholland if (strstr(instsys.version, "(INSTALLSBC)"))
12350dbef1aSdholland /*
12450dbef1aSdholland * Running the SBC Installation Kernel, so enable GENERICSBC
12550dbef1aSdholland */
12650dbef1aSdholland set_kernel_set(SET_KERNEL_2);
12750dbef1aSdholland else
12850dbef1aSdholland /*
12950dbef1aSdholland * Running the GENERIC Installation Kernel, so enable GENERIC
13050dbef1aSdholland */
13150dbef1aSdholland set_kernel_set(SET_KERNEL_1);
13250dbef1aSdholland }
13350dbef1aSdholland
1344103857bSmartin bool
md_get_info(struct install_partition_desc * install)1354103857bSmartin md_get_info(struct install_partition_desc *install)
13650dbef1aSdholland {
13750dbef1aSdholland struct disklabel disklabel;
13850dbef1aSdholland int fd, i;
13950dbef1aSdholland char dev_name[100];
14050dbef1aSdholland struct apple_part_map_entry block;
14150dbef1aSdholland
14250dbef1aSdholland snprintf(dev_name, sizeof(dev_name), "/dev/r%s%c",
1434b2364d9Smartin pm->diskdev, 'a' + getrawpartition());
14450dbef1aSdholland
14550dbef1aSdholland /*
14650dbef1aSdholland * Open the disk as a raw device
14750dbef1aSdholland */
14850dbef1aSdholland fd = open(dev_name, O_RDONLY, 0);
14950dbef1aSdholland if (fd < 0) {
15050dbef1aSdholland endwin();
15150dbef1aSdholland fprintf (stderr, "Can't open %s\n", dev_name);
15250dbef1aSdholland exit(1);
15350dbef1aSdholland }
15450dbef1aSdholland /*
15550dbef1aSdholland * Try to get the default disklabel info for the device
15650dbef1aSdholland */
15750dbef1aSdholland if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
15850dbef1aSdholland endwin();
15950dbef1aSdholland fprintf (stderr, "Can't read disklabel on %s\n", dev_name);
16050dbef1aSdholland close(fd);
16150dbef1aSdholland exit(1);
16250dbef1aSdholland }
16350dbef1aSdholland /*
16450dbef1aSdholland * Get the disk parameters from the disk driver. It should have
16550dbef1aSdholland * obained them by querying the disk itself.
16650dbef1aSdholland */
16750dbef1aSdholland blk_size = disklabel.d_secsize;
1684b2364d9Smartin pm->dlcyl = disklabel.d_ncylinders;
1694b2364d9Smartin pm->dlhead = disklabel.d_ntracks;
1704b2364d9Smartin pm->dlsec = disklabel.d_nsectors;
17150dbef1aSdholland /*
17250dbef1aSdholland * Just in case, initialize the structures we'll need if we
17350dbef1aSdholland * need to completely initialize the disk.
17450dbef1aSdholland */
1754b2364d9Smartin pm->dlsize = disklabel.d_secperunit;
176f8610727Sdholland /*
177f8610727Sdholland * XXX this code is broken: it accesses off the end of new_map[],
178f8610727Sdholland * because NEW_MAP_SIZE is substantially larger than the number of
179f8610727Sdholland * entries in new_map[]. Based on the description of struct
180f8610727Sdholland * apple_part_map_entry in sys/bootblock.h, and the usage of it in
181f8610727Sdholland * new_map[], NEW_MAP_SIZE is expected to be a block count, not an
182f8610727Sdholland * entry count. As far I can tell the logic here is just wrong; it
183f8610727Sdholland * needs someone with platform knowledge to sort it out.
184f8610727Sdholland *
185f8610727Sdholland * Note that nothing uses the data this writes into new_map[] so
186f8610727Sdholland * disabling it should have no adverse consequences.
187f8610727Sdholland *
188f8610727Sdholland * - dholland 20160530
189f8610727Sdholland */
190f8610727Sdholland #if 0 /* XXX broken */
19150dbef1aSdholland for (i=0;i<NEW_MAP_SIZE;i++) {
19250dbef1aSdholland if (i > 0)
19350dbef1aSdholland new_map[i].pmPyPartStart = new_map[i-1].pmPyPartStart +
19450dbef1aSdholland new_map[i-1].pmPartBlkCnt;
19550dbef1aSdholland new_map[i].pmDataCnt = new_map[i].pmPartBlkCnt;
19650dbef1aSdholland if (new_map[i].pmPartBlkCnt == 0) {
1974b2364d9Smartin new_map[i].pmPartBlkCnt = pm->dlsize;
1984b2364d9Smartin new_map[i].pmDataCnt = pm->dlsize;
19950dbef1aSdholland break;
20050dbef1aSdholland }
2014b2364d9Smartin pm->dlsize -= new_map[i].pmPartBlkCnt;
20250dbef1aSdholland }
203f8610727Sdholland #endif /* 0 - broken */
2044b2364d9Smartin pm->dlsize = disklabel.d_secperunit;
20550dbef1aSdholland #if 0
20624ecf24eSchristos msg_fmt_display(MSG_dldebug, "%d%d%d%d%d",
20724ecf24eSchristos blk_size, pm->dlcyl, pm->dlhead, pm->dlsec, pm->dlsize);
20850dbef1aSdholland process_menu(MENU_ok, NULL);
20950dbef1aSdholland #endif
21050dbef1aSdholland map.size = 0;
21150dbef1aSdholland /*
21250dbef1aSdholland * Verify the disk has been initialized for MacOS use by checking
21350dbef1aSdholland * to see if the disk have a Boot Block
21450dbef1aSdholland */
21550dbef1aSdholland if (lseek(fd, (off_t)0 * blk_size, SEEK_SET) < 0 ||
21650dbef1aSdholland read(fd, &block, sizeof(block)) - sizeof(block) != 0 ||
21750dbef1aSdholland block.pmSig != 0x4552) {
21850dbef1aSdholland process_menu(MENU_nodiskmap, NULL);
21950dbef1aSdholland }
22050dbef1aSdholland else {
22150dbef1aSdholland /*
22250dbef1aSdholland * Scan for the Partition Map entry that describes the Partition
22350dbef1aSdholland * Map itself. We need to know the number of blocks allocated
22450dbef1aSdholland * to it and the number currently in use.
22550dbef1aSdholland */
22650dbef1aSdholland for (i=0;i<MAXMAXPARTITIONS;i++) {
22750dbef1aSdholland lseek(fd, (off_t)(i+1) * blk_size, SEEK_SET);
22850dbef1aSdholland read(fd, &block, sizeof(block));
22950dbef1aSdholland if (stricmp("Apple_partition_map", (char *)block.pmPartType) == 0) {
23050dbef1aSdholland map.size = block.pmPartBlkCnt;
23150dbef1aSdholland map.in_use_cnt = block.pmMapBlkCnt;
23250dbef1aSdholland map.blk = (struct apple_part_map_entry *)malloc(map.size * blk_size);
23350dbef1aSdholland break;
23450dbef1aSdholland }
23550dbef1aSdholland }
23650dbef1aSdholland lseek(fd, (off_t)1 * blk_size, SEEK_SET);
23750dbef1aSdholland read(fd, map.blk, map.size * blk_size);
23850dbef1aSdholland }
23950dbef1aSdholland close(fd);
24050dbef1aSdholland
24150dbef1aSdholland return edit_diskmap();
24250dbef1aSdholland }
24350dbef1aSdholland
24450dbef1aSdholland /*
24550dbef1aSdholland * md back-end code for menu-driven BSD disklabel editor.
24650dbef1aSdholland */
247957b5cd6Smartin int
md_make_bsd_partitions(struct install_partition_desc * install)2484103857bSmartin md_make_bsd_partitions(struct install_partition_desc *install)
24950dbef1aSdholland {
2500e36a5a9Smartin int i, j, rv;
25150dbef1aSdholland EBZB *bzb;
2520e36a5a9Smartin struct disk_part_info info;
2530e36a5a9Smartin uint fs_type;
2540e36a5a9Smartin const char *mountpoint;
2550e36a5a9Smartin part_id pid;
2560e36a5a9Smartin size_t ndx;
25750dbef1aSdholland
25850dbef1aSdholland /*
25950dbef1aSdholland * Scan for any problems and report them before continuing.
26050dbef1aSdholland * The user can abort installation and we'll take them back
26150dbef1aSdholland * to the main menu; continue ignoring the warnings, or
26250dbef1aSdholland * ask to reedit the Disk Partition Map.
26350dbef1aSdholland */
26450dbef1aSdholland while (1) {
26550dbef1aSdholland if (check_for_errors()) {
266cc33bc91Smartin process_menu (MENU_sanity, &rv);
267cc33bc91Smartin if (rv < 0)
268957b5cd6Smartin return 0;
269cc33bc91Smartin else if (rv)
27050dbef1aSdholland break;
27150dbef1aSdholland edit_diskmap();
27250dbef1aSdholland } else
27350dbef1aSdholland break;
27450dbef1aSdholland }
27550dbef1aSdholland
2760e36a5a9Smartin /* Start with empty fake disklabel partitions */
2770e36a5a9Smartin pm->parts->pscheme->delete_all_partitions(pm->parts);
27850dbef1aSdholland
27950dbef1aSdholland /*
28050dbef1aSdholland * Now, scan through the Disk Partition Map and transfer the
28150dbef1aSdholland * information into the incore disklabel.
28250dbef1aSdholland */
28350dbef1aSdholland for (i=0;i<map.usable_cnt;i++) {
28450dbef1aSdholland j = map.mblk[i];
28550dbef1aSdholland bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
28650dbef1aSdholland if (bzb->flags.part) {
2870e36a5a9Smartin mountpoint = NULL;
2880e36a5a9Smartin fs_type = FS_UNUSED;
28950dbef1aSdholland switch (whichType(&map.blk[j])) {
29050dbef1aSdholland case HFS_PART:
2910e36a5a9Smartin fs_type = FS_HFS;
2920e36a5a9Smartin mountpoint = (const char*)bzb->mount_point;
29350dbef1aSdholland break;
29450dbef1aSdholland case ROOT_PART:
29550dbef1aSdholland case UFS_PART:
2960e36a5a9Smartin fs_type = FS_BSDFFS;
2970e36a5a9Smartin mountpoint = (const char*)bzb->mount_point;
29850dbef1aSdholland break;
29950dbef1aSdholland case SWAP_PART:
3000e36a5a9Smartin fs_type = FS_SWAP;
30150dbef1aSdholland break;
30250dbef1aSdholland case SCRATCH_PART:
3030e36a5a9Smartin fs_type = FS_OTHER;
3040e36a5a9Smartin mountpoint = (const char*)bzb->mount_point;
30550dbef1aSdholland break;
3060e36a5a9Smartin default:
3070e36a5a9Smartin continue;
30850dbef1aSdholland }
3090e36a5a9Smartin if (fs_type != FS_UNUSED) {
3100e36a5a9Smartin memset(&info, 0, sizeof info);
3110e36a5a9Smartin info.start = map.blk[j].pmPyPartStart;
3120e36a5a9Smartin info.size = map.blk[j].pmPartBlkCnt;
3130e36a5a9Smartin info.fs_type = fs_type;
3140e36a5a9Smartin info.last_mounted = mountpoint;
3150e36a5a9Smartin info.nat_type = pm->parts->pscheme->get_fs_part_type(
3160e36a5a9Smartin PT_root, fs_type, 0);
3170e36a5a9Smartin pid = pm->parts->pscheme->add_outer_partition(pm->parts,
3180e36a5a9Smartin &info, NULL);
3190e36a5a9Smartin if (pid == NO_PART)
320957b5cd6Smartin return 0;
32150dbef1aSdholland }
32250dbef1aSdholland }
32350dbef1aSdholland }
32450dbef1aSdholland
32550dbef1aSdholland /* Disk name - don't bother asking, just use the physical name*/
3260e36a5a9Smartin pm->parts->pscheme->set_disk_pack_name(pm->parts, pm->diskdev);
32750dbef1aSdholland
3280e36a5a9Smartin /* Write the converted partitions */
3290e36a5a9Smartin if (!pm->parts->pscheme->write_to_disk(pm->parts))
330957b5cd6Smartin return 0;
33150dbef1aSdholland
3320e36a5a9Smartin /* now convert to install info */
3330e36a5a9Smartin if (!install_desc_from_parts(install, pm->parts))
334957b5cd6Smartin return 0;
3350e36a5a9Smartin
3360e36a5a9Smartin /* set newfs flag for all FFS partitions */
3370e36a5a9Smartin for (ndx = 0; ndx < install->num; ndx++) {
3380e36a5a9Smartin if (install->infos[ndx].fs_type == FS_BSDFFS &&
3390e36a5a9Smartin install->infos[ndx].size > 0 &&
3400e36a5a9Smartin (install->infos[ndx].instflags & PUIINST_MOUNT))
3410e36a5a9Smartin install->infos[ndx].instflags |= PUIINST_NEWFS;
3420e36a5a9Smartin }
3430e36a5a9Smartin
344957b5cd6Smartin return 1;
34550dbef1aSdholland }
34650dbef1aSdholland
34750dbef1aSdholland /*
34850dbef1aSdholland * any additional partition validation
34950dbef1aSdholland */
3504103857bSmartin bool
md_check_partitions(struct install_partition_desc * install)3514103857bSmartin md_check_partitions(struct install_partition_desc *install)
35250dbef1aSdholland {
3534103857bSmartin return true;
35450dbef1aSdholland }
35550dbef1aSdholland
35650dbef1aSdholland /*
35750dbef1aSdholland * hook called before writing new disklabel.
35850dbef1aSdholland */
3594103857bSmartin bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)3604103857bSmartin md_pre_disklabel(struct install_partition_desc *install,
3614103857bSmartin struct disk_partitions *parts)
36250dbef1aSdholland {
36350dbef1aSdholland int fd;
36450dbef1aSdholland char dev_name[100];
36550dbef1aSdholland struct disklabel lp;
36650dbef1aSdholland Block0 new_block0 = {APPLE_DRVR_MAP_MAGIC, 512,
36750dbef1aSdholland 0, 0, 0, 0, 0, 0, 0, 0, {0}};
36850dbef1aSdholland
36950dbef1aSdholland /*
37050dbef1aSdholland * Danger Will Robinson! We're about to turn that nice MacOS disk
37150dbef1aSdholland * into an expensive doorstop...
37250dbef1aSdholland */
37350dbef1aSdholland printf ("%s", msg_string (MSG_dodiskmap));
37450dbef1aSdholland
3754b2364d9Smartin snprintf (dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
37650dbef1aSdholland /*
37750dbef1aSdholland * Open the disk as a raw device
37850dbef1aSdholland */
37950dbef1aSdholland if ((fd = open(dev_name, O_WRONLY, 0)) < 0) {
38050dbef1aSdholland endwin();
38150dbef1aSdholland fprintf(stderr, "Can't open %s to rewrite the Disk Map\n", dev_name);
38250dbef1aSdholland exit (1);
38350dbef1aSdholland }
38450dbef1aSdholland /*
38550dbef1aSdholland * First check the pmSigPad field of the first block in the incore
38650dbef1aSdholland * Partition Map. It should be zero, but if it's 0xa5a5 that means
38750dbef1aSdholland * we need to write out Block0 too.
38850dbef1aSdholland */
38950dbef1aSdholland if (map.blk[0].pmSigPad == 0xa5a5) {
39050dbef1aSdholland if (lseek (fd, (off_t)0 * blk_size, SEEK_SET) < 0) {
39150dbef1aSdholland endwin();
39250dbef1aSdholland fprintf (stderr, "Can't position to write Block0\n");
39350dbef1aSdholland close (fd);
39450dbef1aSdholland exit (1);
39550dbef1aSdholland }
3964b2364d9Smartin new_block0.sbBlkCount = pm->dlsize; /* Set disk size */
39750dbef1aSdholland if (write (fd, &new_block0, blk_size) != blk_size) {
39850dbef1aSdholland endwin();
39950dbef1aSdholland fprintf (stderr, "I/O error writing Block0\n");
40050dbef1aSdholland close (fd);
40150dbef1aSdholland exit (1);
40250dbef1aSdholland }
40350dbef1aSdholland map.blk[0].pmSigPad = 0;
40450dbef1aSdholland }
40550dbef1aSdholland if (lseek (fd, (off_t)1 * blk_size, SEEK_SET) < 0) {
40650dbef1aSdholland endwin();
40750dbef1aSdholland fprintf (stderr, "Can't position disk to rewrite Disk Map\n");
40850dbef1aSdholland close (fd);
40950dbef1aSdholland exit (1);
41050dbef1aSdholland }
41150dbef1aSdholland if (write (fd, map.blk, map.size * blk_size) != (map.size * blk_size)) {
41250dbef1aSdholland endwin();
41350dbef1aSdholland fprintf(stderr, "I/O error writing Disk Map\n");
41450dbef1aSdholland close (fd);
41550dbef1aSdholland exit (1);
41650dbef1aSdholland }
41750dbef1aSdholland fsync(fd);
41850dbef1aSdholland /*
41950dbef1aSdholland * Well, if we get here the dirty deed has been done.
42050dbef1aSdholland *
42150dbef1aSdholland * Now we need to force the incore disk table to get updated. This
42250dbef1aSdholland * should be done by disklabel -- which is normally called right after
42350dbef1aSdholland * we return -- but may be commented out for the mac68k port. We'll
42450dbef1aSdholland * instead update the incore table by forcing a dummy write here. This
4254b2364d9Smartin * relies on a change in the mac68k-specific write_disklabel() routine.
42650dbef1aSdholland * If that change doesn't exist nothing bad happens here. If disklabel
42750dbef1aSdholland * properly updates the ondisk and incore labels everything still
42850dbef1aSdholland * works. Only if we fail here and if disklabel fails are we in
42950dbef1aSdholland * in a state where we've updated the disk but not the incore and
43050dbef1aSdholland * a reboot is necessary.
43150dbef1aSdholland *
43250dbef1aSdholland * First, we grab a copy of the incore label as it existed before
43350dbef1aSdholland * we did anything to it. Then we invoke the "write label" ioctl to
43450dbef1aSdholland * rewrite it to disk. As a result, the ondisk partition map is
43550dbef1aSdholland * re-read and the incore label is reconstructed from it. If
43650dbef1aSdholland * disklabel() is then called to update again, either that fails
43750dbef1aSdholland * because the mac68k port doesn't support native disklabels, or it
43850dbef1aSdholland * succeeds and writes out a new ondisk copy.
43950dbef1aSdholland */
44050dbef1aSdholland ioctl(fd, DIOCGDINFO, &lp); /* Get the current disk label */
44150dbef1aSdholland ioctl(fd, DIOCWDINFO, &lp); /* Write it out again */
44250dbef1aSdholland
44350dbef1aSdholland close (fd);
4444103857bSmartin return true;
44550dbef1aSdholland }
44650dbef1aSdholland
44750dbef1aSdholland /*
44850dbef1aSdholland * hook called after writing disklabel to new target disk.
44950dbef1aSdholland */
4504103857bSmartin bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)4514103857bSmartin md_post_disklabel(struct install_partition_desc *install,
4524103857bSmartin struct disk_partitions *parts)
45350dbef1aSdholland {
4544103857bSmartin #if 0 // XXX
45550dbef1aSdholland struct disklabel updated_label;
45650dbef1aSdholland int fd, i, no_match;
45750dbef1aSdholland char dev_name[100], buf[80];
45850dbef1aSdholland const char *fst[] = {"free", "swap", " v6 ", " v7 ", "sysv", "v71k",
45950dbef1aSdholland " v8 ", "ffs ", "dos ", "lfs ", "othr", "hpfs",
46050dbef1aSdholland "9660", "boot", "ados", "hfs ", "fcor", "ex2f",
46150dbef1aSdholland "ntfs", "raid", "ccd "};
46250dbef1aSdholland
4634b2364d9Smartin snprintf(dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
46450dbef1aSdholland /*
46550dbef1aSdholland * Open the disk as a raw device
46650dbef1aSdholland */
46750dbef1aSdholland if ((fd = open(dev_name, O_RDONLY, 0)) < 0)
4684103857bSmartin return false;
46950dbef1aSdholland /*
47050dbef1aSdholland * Get the "new" label to see if we were successful. If we aren't
47150dbef1aSdholland * we'll return an error to keep from destroying the user's disk.
47250dbef1aSdholland */
47350dbef1aSdholland ioctl(fd, DIOCGDINFO, &updated_label);
47450dbef1aSdholland close(fd);
47550dbef1aSdholland /*
47650dbef1aSdholland * Make sure the in-core label matches the on-disk one
47750dbef1aSdholland */
47850dbef1aSdholland no_match = 0;
47950dbef1aSdholland for (i=0;i<MAXPARTITIONS;i++) {
48050dbef1aSdholland if (i > updated_label.d_npartitions)
48150dbef1aSdholland break;
4824b2364d9Smartin if (pm->bsdlabel[i].pi_size != updated_label.d_partitions[i].p_size)
48350dbef1aSdholland no_match = 1;
4844b2364d9Smartin if (pm->bsdlabel[i].pi_size) {
4854b2364d9Smartin if (pm->bsdlabel[i].pi_offset != updated_label.d_partitions[i].p_offset)
48650dbef1aSdholland no_match = 1;
4874b2364d9Smartin if (pm->bsdlabel[i].pi_fstype != updated_label.d_partitions[i].p_fstype)
48850dbef1aSdholland no_match = 1;
48950dbef1aSdholland }
49050dbef1aSdholland if (no_match)
49150dbef1aSdholland break;
49250dbef1aSdholland }
49350dbef1aSdholland /*
49450dbef1aSdholland * If the labels don't match, tell the user why
49550dbef1aSdholland */
49650dbef1aSdholland if (no_match) {
49750dbef1aSdholland msg_clear();
49850dbef1aSdholland msg_display(MSG_label_error);
49924ecf24eSchristos msg_fmt_table_add(MSG_dump_line, "%s",
50050dbef1aSdholland " in-core: offset size type on-disk: offset size type");
50150dbef1aSdholland for (i=0;i<MAXPARTITIONS;i++) {
50224ecf24eSchristos snprintf(buf, sizeof(buf),
50324ecf24eSchristos " %c:%13.8x%10.8x%5s%16.8x%10.8x%5s", i+'a',
5044b2364d9Smartin pm->bsdlabel[i].pi_offset, pm->bsdlabel[i].pi_size,
5054b2364d9Smartin fst[pm->bsdlabel[i].pi_fstype],
50650dbef1aSdholland updated_label.d_partitions[i].p_offset,
50750dbef1aSdholland updated_label.d_partitions[i].p_size,
50850dbef1aSdholland fst[updated_label.d_partitions[i].p_fstype]);
50924ecf24eSchristos msg_fmt_table_add(MSG_dump_line, "%s", buf);
51050dbef1aSdholland }
51150dbef1aSdholland process_menu(MENU_ok2, NULL);
51250dbef1aSdholland }
5134103857bSmartin return no_match == 0;
5144103857bSmartin #else
5154103857bSmartin return true;
5164103857bSmartin #endif
51750dbef1aSdholland }
51850dbef1aSdholland
51950dbef1aSdholland /*
52050dbef1aSdholland * hook called after upgrade() or install() has finished setting
52150dbef1aSdholland * up the target disk but immediately before the user is given the
52250dbef1aSdholland * ``disks are now set up'' message.
52350dbef1aSdholland */
52450dbef1aSdholland int
md_post_newfs(struct install_partition_desc * install)5254103857bSmartin md_post_newfs(struct install_partition_desc *install)
52650dbef1aSdholland {
52750dbef1aSdholland return 0;
52850dbef1aSdholland }
52950dbef1aSdholland
53050dbef1aSdholland int
md_post_extract(struct install_partition_desc * install,bool upgrade)5314204f810Smartin md_post_extract(struct install_partition_desc *install, bool upgrade)
53250dbef1aSdholland {
53350dbef1aSdholland return 0;
53450dbef1aSdholland }
53550dbef1aSdholland
53650dbef1aSdholland void
md_cleanup_install(struct install_partition_desc * install)5374103857bSmartin md_cleanup_install(struct install_partition_desc *install)
53850dbef1aSdholland {
53950dbef1aSdholland #ifndef DEBUG
54050dbef1aSdholland enable_rc_conf();
54150dbef1aSdholland #endif
54250dbef1aSdholland }
54350dbef1aSdholland
54450dbef1aSdholland int
md_pre_update(struct install_partition_desc * install)5454103857bSmartin md_pre_update(struct install_partition_desc *install)
54650dbef1aSdholland {
54750dbef1aSdholland return 1;
54850dbef1aSdholland }
54950dbef1aSdholland
55050dbef1aSdholland /* Upgrade support */
55150dbef1aSdholland int
md_update(struct install_partition_desc * install)5524103857bSmartin md_update(struct install_partition_desc *install)
55350dbef1aSdholland {
5544103857bSmartin md_post_newfs(install);
55550dbef1aSdholland return 1;
55650dbef1aSdholland }
55750dbef1aSdholland
55850dbef1aSdholland /*
55950dbef1aSdholland * Compare lexigraphically two strings
56050dbef1aSdholland */
56150dbef1aSdholland static int
stricmp(s1,s2)56250dbef1aSdholland stricmp(s1, s2)
56350dbef1aSdholland const char *s1;
56450dbef1aSdholland const char *s2;
56550dbef1aSdholland {
56650dbef1aSdholland char c1, c2;
56750dbef1aSdholland
56850dbef1aSdholland while (1) {
56950dbef1aSdholland c1 = tolower((unsigned char)*s1++);
57050dbef1aSdholland c2 = tolower((unsigned char)*s2++);
57150dbef1aSdholland if (c1 < c2) return -1;
57250dbef1aSdholland if (c1 > c2) return 1;
57350dbef1aSdholland if (c1 == 0) return 0;
57450dbef1aSdholland }
57550dbef1aSdholland }
57650dbef1aSdholland
57750dbef1aSdholland static void
setpartition(part,in_use,slot)57850dbef1aSdholland setpartition(part, in_use, slot)
57950dbef1aSdholland struct apple_part_map_entry *part;
58050dbef1aSdholland char in_use[];
58150dbef1aSdholland int slot;
58250dbef1aSdholland {
58350dbef1aSdholland EBZB *bzb;
58450dbef1aSdholland
58550dbef1aSdholland bzb = (EBZB *)&part->pmBootArgs[0];
58650dbef1aSdholland in_use[slot] = 1;
58750dbef1aSdholland bzb->flags.used = 1;
58850dbef1aSdholland bzb->flags.part = 'a' + slot;
58950dbef1aSdholland }
59050dbef1aSdholland
59150dbef1aSdholland /*
59250dbef1aSdholland * Find an entry in a use array that is unused and return it or
59350dbef1aSdholland * -1 if no entry is available
59450dbef1aSdholland */
59550dbef1aSdholland static int
getFreeLabelEntry(slots)59650dbef1aSdholland getFreeLabelEntry(slots)
59750dbef1aSdholland char *slots;
59850dbef1aSdholland {
59950dbef1aSdholland int i;
60050dbef1aSdholland
60150dbef1aSdholland for ( i = 0; i < MAXPARTITIONS; i++) {
60250dbef1aSdholland if (i != RAW_PART && slots[i] == 0)
60350dbef1aSdholland return i;
60450dbef1aSdholland }
60550dbef1aSdholland return -1;
60650dbef1aSdholland }
60750dbef1aSdholland
60850dbef1aSdholland /*
60950dbef1aSdholland * Figure out what type type of the given partition is and return it.
61050dbef1aSdholland */
61150dbef1aSdholland int
whichType(part)61250dbef1aSdholland whichType(part)
61350dbef1aSdholland struct apple_part_map_entry *part;
61450dbef1aSdholland {
61550dbef1aSdholland MAP_TYPE *map_entry = (MAP_TYPE *)&map_types;
61650dbef1aSdholland EBZB *bzb;
61750dbef1aSdholland char partyp[32];
61850dbef1aSdholland int type, maxsiz, entry_type = MAP_OTHER;
61950dbef1aSdholland
62050dbef1aSdholland bzb = (EBZB *)&part->pmBootArgs[0];
62150dbef1aSdholland if (part->pmSig != APPLE_PART_MAP_ENTRY_MAGIC)
62250dbef1aSdholland return 0;
62350dbef1aSdholland maxsiz = sizeof(part->pmPartType);
62450dbef1aSdholland if (maxsiz > (int)sizeof(partyp))
62550dbef1aSdholland maxsiz = sizeof(partyp);
62650dbef1aSdholland strncpy(partyp, (char *)part->pmPartType, maxsiz);
62750dbef1aSdholland partyp[maxsiz-1] = '\0';
62850dbef1aSdholland
62950dbef1aSdholland /*
63050dbef1aSdholland * Find out how to treat the partition type under NetBSD
63150dbef1aSdholland */
63250dbef1aSdholland while (map_entry->type != MAP_EOL) {
63350dbef1aSdholland if (stricmp(map_entry->name, partyp) == 0) {
63450dbef1aSdholland entry_type = map_entry->type;
63550dbef1aSdholland break;
63650dbef1aSdholland }
63750dbef1aSdholland map_entry++;
63850dbef1aSdholland }
63950dbef1aSdholland
64050dbef1aSdholland /*
64150dbef1aSdholland * Now classify the use for NetBSD
64250dbef1aSdholland */
64350dbef1aSdholland if (entry_type == MAP_RESERVED)
64450dbef1aSdholland type = 0;
64550dbef1aSdholland else if (entry_type == MAP_NETBSD) {
64650dbef1aSdholland if (bzb->magic != APPLE_BZB_MAGIC)
64750dbef1aSdholland type = 0;
64850dbef1aSdholland else if (bzb->type == APPLE_BZB_TYPEFS) {
64950dbef1aSdholland if (bzb->flags.root)
65050dbef1aSdholland type = ROOT_PART;
65150dbef1aSdholland else if (bzb->flags.usr)
65250dbef1aSdholland type = UFS_PART;
65350dbef1aSdholland else
65450dbef1aSdholland type = SCRATCH_PART;
65550dbef1aSdholland } else if (bzb->type == APPLE_BZB_TYPESWAP)
65650dbef1aSdholland type = SWAP_PART;
65750dbef1aSdholland else
65850dbef1aSdholland type = SCRATCH_PART;
65950dbef1aSdholland } else if (entry_type == MAP_MACOS)
66050dbef1aSdholland type = HFS_PART;
66150dbef1aSdholland else
66250dbef1aSdholland type = SCRATCH_PART;
66350dbef1aSdholland return type;
66450dbef1aSdholland }
66550dbef1aSdholland
66650dbef1aSdholland static char *
getFstype(part,len_type,type)66750dbef1aSdholland getFstype(part, len_type, type)
66850dbef1aSdholland struct apple_part_map_entry *part;
66950dbef1aSdholland int len_type;
67050dbef1aSdholland char *type;
67150dbef1aSdholland {
67250dbef1aSdholland *type = '\0';
67350dbef1aSdholland switch(whichType(part)) {
67450dbef1aSdholland case ROOT_PART:
67550dbef1aSdholland case UFS_PART:
67650dbef1aSdholland strncpy(type, "4.2BSD", len_type);
67750dbef1aSdholland break;
67850dbef1aSdholland case SWAP_PART:
67950dbef1aSdholland strncpy(type, "swap", len_type);
68050dbef1aSdholland break;
68150dbef1aSdholland case HFS_PART:
68250dbef1aSdholland strncpy(type, "HFS", len_type);
68350dbef1aSdholland break;
68450dbef1aSdholland case SCRATCH_PART:
68550dbef1aSdholland default:
68650dbef1aSdholland break;
68750dbef1aSdholland }
68850dbef1aSdholland return (type);
68950dbef1aSdholland }
69050dbef1aSdholland
69150dbef1aSdholland static char *
getUse(part,len_use,use)69250dbef1aSdholland getUse(part, len_use, use)
69350dbef1aSdholland struct apple_part_map_entry *part;
69450dbef1aSdholland int len_use;
69550dbef1aSdholland char *use;
69650dbef1aSdholland {
69750dbef1aSdholland EBZB *bzb;
69850dbef1aSdholland char partyp[32];
69950dbef1aSdholland
70050dbef1aSdholland *use = '\0';
70150dbef1aSdholland bzb = (EBZB *)&part->pmBootArgs[0];
70250dbef1aSdholland switch(whichType(part)) {
70350dbef1aSdholland case ROOT_PART:
70450dbef1aSdholland if (bzb->flags.usr)
70550dbef1aSdholland strncpy(use, "Root&Usr", len_use);
70650dbef1aSdholland else
70750dbef1aSdholland strncpy(use, "Root", len_use);
70850dbef1aSdholland break;
70950dbef1aSdholland case UFS_PART:
71050dbef1aSdholland strncpy(use, "Usr", len_use);
71150dbef1aSdholland break;
71250dbef1aSdholland case SWAP_PART:
71350dbef1aSdholland break;
71450dbef1aSdholland case HFS_PART:
71550dbef1aSdholland strncpy(use, "MacOS", len_use);
71650dbef1aSdholland break;
71750dbef1aSdholland case SCRATCH_PART:
71850dbef1aSdholland strncpy(partyp, (char *)part->pmPartType, sizeof(partyp));
71950dbef1aSdholland partyp[sizeof(partyp)-1] = '\0';
72050dbef1aSdholland if (stricmp("Apple_Free", partyp) == 0)
72150dbef1aSdholland strncpy(use, "Free", len_use);
72250dbef1aSdholland else if (stricmp("Apple_Scratch", partyp) == 0)
72350dbef1aSdholland strncpy(use, "Scratch", len_use);
72450dbef1aSdholland else if (stricmp("Apple_MFS", partyp) == 0)
72550dbef1aSdholland strncpy(use, "MFS", len_use);
72650dbef1aSdholland else if (stricmp("Apple_PRODOS", partyp) == 0)
72750dbef1aSdholland strncpy(use, "PRODOS", len_use);
72850dbef1aSdholland else
72950dbef1aSdholland strncpy(use, "unknown", len_use);
73050dbef1aSdholland default:
73150dbef1aSdholland break;
73250dbef1aSdholland }
73350dbef1aSdholland return(use);
73450dbef1aSdholland }
73550dbef1aSdholland
73650dbef1aSdholland static char *
getName(part,len_name,name)73750dbef1aSdholland getName(part, len_name, name)
73850dbef1aSdholland struct apple_part_map_entry *part;
73950dbef1aSdholland int len_name;
74050dbef1aSdholland char *name;
74150dbef1aSdholland {
74250dbef1aSdholland EBZB *bzb;
74350dbef1aSdholland int fd;
74450dbef1aSdholland off_t seek;
74550dbef1aSdholland char dev_name[100], macosblk[512];
74650dbef1aSdholland
74750dbef1aSdholland *name = '\0';
74850dbef1aSdholland bzb = (EBZB *)&part->pmBootArgs[0];
74950dbef1aSdholland switch(whichType(part)) {
75050dbef1aSdholland case SCRATCH_PART:
75150dbef1aSdholland case ROOT_PART:
75250dbef1aSdholland case UFS_PART:
75350dbef1aSdholland strncpy(name, (char *)bzb->mount_point, len_name);
75450dbef1aSdholland break;
75550dbef1aSdholland case SWAP_PART:
75650dbef1aSdholland break;
75750dbef1aSdholland case HFS_PART:
75850dbef1aSdholland /*
75950dbef1aSdholland * OK, this is stupid but it's damn nice to know!
76050dbef1aSdholland */
7614b2364d9Smartin snprintf (dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
76250dbef1aSdholland /*
76350dbef1aSdholland * Open the disk as a raw device
76450dbef1aSdholland */
76550dbef1aSdholland if ((fd = open(dev_name, O_RDONLY, 0)) >= 0) {
76650dbef1aSdholland seek = (off_t)part->pmPyPartStart + (off_t)2;
76750dbef1aSdholland seek *= (off_t)blk_size;
76850dbef1aSdholland lseek(fd, seek, SEEK_SET);
76950dbef1aSdholland read(fd, &macosblk, sizeof(macosblk));
77050dbef1aSdholland macosblk[37+32] = '\0';
77150dbef1aSdholland strncpy(name, (char *)bzb->mount_point, len_name);
77250dbef1aSdholland strncat(name, " (", len_name-strlen(name));
77350dbef1aSdholland strncat(name, &macosblk[37], len_name-strlen(name));
77450dbef1aSdholland strncat(name, ")", len_name-strlen(name));
77550dbef1aSdholland close(fd);
77650dbef1aSdholland }
77750dbef1aSdholland break;
77850dbef1aSdholland default:
77950dbef1aSdholland break;
78050dbef1aSdholland }
78150dbef1aSdholland return(name);
78250dbef1aSdholland }
78350dbef1aSdholland
78450dbef1aSdholland /*
785*2e9df72eSandvar * Find the first occurrence of a Standard Type partition and
78650dbef1aSdholland * mark it for use along with the default mount slot.
78750dbef1aSdholland */
78850dbef1aSdholland static int
findStdType(num_parts,in_use,type,count,alt)78950dbef1aSdholland findStdType(num_parts, in_use, type, count, alt)
79050dbef1aSdholland int num_parts;
79150dbef1aSdholland char in_use[];
79250dbef1aSdholland int type;
79350dbef1aSdholland int *count;
79450dbef1aSdholland int alt;
79550dbef1aSdholland {
79650dbef1aSdholland EBZB *bzb;
79750dbef1aSdholland int i;
79850dbef1aSdholland
79950dbef1aSdholland for (i = 0; i < num_parts; i++) {
80050dbef1aSdholland bzb = (EBZB *)&map.blk[i].pmBootArgs[0];
80150dbef1aSdholland if (whichType(&map.blk[i]) != type || bzb->flags.used)
80250dbef1aSdholland continue;
80350dbef1aSdholland if (type == ROOT_PART) {
80450dbef1aSdholland if (alt >= 0 && alt != bzb->cluster)
80550dbef1aSdholland continue;
80650dbef1aSdholland setpartition(&map.blk[i], in_use, 0);
80750dbef1aSdholland strcpy ((char *)bzb->mount_point, "/");
80850dbef1aSdholland *count += 1;
80950dbef1aSdholland } else if (type == UFS_PART) {
81050dbef1aSdholland if (alt >= 0 && alt != bzb->cluster)
81150dbef1aSdholland continue;
81250dbef1aSdholland setpartition(&map.blk[i], in_use, 6);
81350dbef1aSdholland if (bzb->mount_point[0] == '\0')
81450dbef1aSdholland strcpy ((char *)bzb->mount_point, "/usr");
81550dbef1aSdholland *count += 1;
81650dbef1aSdholland } else if (type == SWAP_PART) {
81750dbef1aSdholland setpartition(&map.blk[i], in_use, 1);
81850dbef1aSdholland *count += 1;
81950dbef1aSdholland }
82050dbef1aSdholland return 0;
82150dbef1aSdholland }
82250dbef1aSdholland return -1;
82350dbef1aSdholland }
82450dbef1aSdholland
82550dbef1aSdholland /*
82650dbef1aSdholland * Reset the flags and reserved fields in the selected partition.
82750dbef1aSdholland * This functions isn't called to process any of the reserved partitions
82850dbef1aSdholland * where the boot code for MacOS is stored, so (hopefully) we won't
82950dbef1aSdholland * do more damage that we're trying to avoid. Eventually the NetBSD
83050dbef1aSdholland * Boot Code will need to go into a partition too, but that should go
83150dbef1aSdholland * into a reserved partition as well. I'd suggest using a partition
83250dbef1aSdholland * named something like "NetBSD_Boot" with a pmPartName of "Macintosh".
83350dbef1aSdholland * The Apple Start Manager (in ROM) will then recognize the partition
83450dbef1aSdholland * as the one containing the system bootstrip for the volume.
83550dbef1aSdholland */
83650dbef1aSdholland void
reset_part_flags(part)83750dbef1aSdholland reset_part_flags(part)
83850dbef1aSdholland struct apple_part_map_entry *part;
83950dbef1aSdholland {
84050dbef1aSdholland EBZB *bzb;
84150dbef1aSdholland
84250dbef1aSdholland /*
84350dbef1aSdholland * Clear out the MacOS fields that might be used for booting just
84450dbef1aSdholland * in case we've clobbered the boot code.
84550dbef1aSdholland */
84650dbef1aSdholland part->pmLgDataStart = 0;
84750dbef1aSdholland part->pmPartStatus = 0x77; /* make sure the partition shows up */
84850dbef1aSdholland part->pmLgBootStart = 0;
84950dbef1aSdholland part->pmBootSize = 0;
85050dbef1aSdholland part->pmBootLoad = 0;
85150dbef1aSdholland part->pmBootLoad2 = 0;
85250dbef1aSdholland part->pmBootEntry = 0;
85350dbef1aSdholland part->pmBootEntry2 = 0;
85450dbef1aSdholland part->pmBootCksum = 0;
85550dbef1aSdholland
85650dbef1aSdholland /*
85750dbef1aSdholland * Clear out all the NetBSD fields too. We only clear out the ones
85850dbef1aSdholland * that should get reset during our processing.
85950dbef1aSdholland */
86050dbef1aSdholland bzb = (EBZB *)&part->pmBootArgs[0];
86150dbef1aSdholland bzb->magic = 0;
86250dbef1aSdholland bzb->cluster = 0;
86350dbef1aSdholland bzb->inode = 0;
86450dbef1aSdholland bzb->type = 0;
86550dbef1aSdholland bzb->flags.root = 0;
86650dbef1aSdholland bzb->flags.usr = 0;
86750dbef1aSdholland bzb->flags.crit = 0;
86850dbef1aSdholland bzb->flags.slice = 0;
86950dbef1aSdholland bzb->flags.used = 0;
87050dbef1aSdholland return;
87150dbef1aSdholland }
87250dbef1aSdholland
87350dbef1aSdholland /*
87450dbef1aSdholland * sortmerge:
87550dbef1aSdholland * 1) Moves all the Partition Map entries to the front of the Map.
87650dbef1aSdholland * This is required because some disk formatters leave holes.
87750dbef1aSdholland * 2) Sorts all entries by ascending start block number.
87850dbef1aSdholland * Needed so the NetBSD algorithm for finding partitions works
87950dbef1aSdholland * consistently from a user perspective.
88050dbef1aSdholland * 3) Collapse multiple adjected "free" entries into a single entry.
88150dbef1aSdholland * 4) Identify the NetBSD mount_points.
88250dbef1aSdholland */
88350dbef1aSdholland void
sortmerge(void)88450dbef1aSdholland sortmerge(void)
88550dbef1aSdholland {
88650dbef1aSdholland struct apple_part_map_entry tmp_blk;
88750dbef1aSdholland char in_use[MAXPARTITIONS];
88850dbef1aSdholland int i, j;
88950dbef1aSdholland EBZB *bzb;
89050dbef1aSdholland
89150dbef1aSdholland /*
89250dbef1aSdholland * Step 1, squeeze out the holes that some disk formatters leave in
89350dbef1aSdholland * the Map. Also convert any "old" Map entries to the new entry
89450dbef1aSdholland * type. Also clear out our used flag which is used to indicte
89550dbef1aSdholland * we've mapped the partition.
89650dbef1aSdholland */
89750dbef1aSdholland map.in_use_cnt = 0;
89850dbef1aSdholland for (i=0;i<map.size-1;i++) {
89950dbef1aSdholland if (map.blk[i].pmSig == 0x5453)
90050dbef1aSdholland map.blk[i].pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
90150dbef1aSdholland if (map.blk[i].pmSig != APPLE_PART_MAP_ENTRY_MAGIC) {
90250dbef1aSdholland for (j=i+1;j<map.size;j++) {
90350dbef1aSdholland if (map.blk[j].pmSig == 0x5453)
90450dbef1aSdholland map.blk[j].pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
90550dbef1aSdholland if (map.blk[j].pmSig == APPLE_PART_MAP_ENTRY_MAGIC) {
90650dbef1aSdholland memcpy (&map.blk[i], &map.blk[j], sizeof(map.blk[i]));
90750dbef1aSdholland map.blk[j].pmSig = 0;
90850dbef1aSdholland break;
90950dbef1aSdholland }
91050dbef1aSdholland }
91150dbef1aSdholland } else {
91250dbef1aSdholland map.in_use_cnt += 1;
91350dbef1aSdholland bzb = (EBZB *)&map.blk[i].pmBootArgs[0];
91450dbef1aSdholland bzb->flags.used = 0;
91550dbef1aSdholland bzb->flags.part = 0;
91650dbef1aSdholland }
91750dbef1aSdholland }
91850dbef1aSdholland
91950dbef1aSdholland /*
92050dbef1aSdholland * Step 2, sort by ascending starting block number. Since
92150dbef1aSdholland * we've already removed the holes we only need to
92250dbef1aSdholland * deal with the in_use count of blocks.
92350dbef1aSdholland */
92450dbef1aSdholland for (i=0;i<map.in_use_cnt-1;i++) {
92550dbef1aSdholland for (j=i+1;j<map.in_use_cnt;j++) {
92650dbef1aSdholland if (map.blk[i].pmPyPartStart > map.blk[j].pmPyPartStart) {
92750dbef1aSdholland memcpy (&tmp_blk, &map.blk[i], sizeof(tmp_blk));
92850dbef1aSdholland memcpy (&map.blk[i], &map.blk[j], sizeof(map.blk[i]));
92950dbef1aSdholland memcpy (&map.blk[j], &tmp_blk, sizeof(map.blk[j]));
93050dbef1aSdholland }
93150dbef1aSdholland }
93250dbef1aSdholland }
93350dbef1aSdholland
93450dbef1aSdholland /*
93550dbef1aSdholland * Step 3, merge adjacent free space
93650dbef1aSdholland */
93750dbef1aSdholland for (i=0;i<map.in_use_cnt-1;i++) {
93850dbef1aSdholland if (stricmp("Apple_Free", (char *)map.blk[i].pmPartType) == 0 &&
93950dbef1aSdholland stricmp("Apple_Free", (char *)map.blk[i+1].pmPartType) == 0) {
94050dbef1aSdholland map.blk[i].pmPartBlkCnt += map.blk[i+1].pmPartBlkCnt;
94150dbef1aSdholland map.blk[i].pmDataCnt += map.blk[i+1].pmDataCnt;
94250dbef1aSdholland map.blk[i+1].pmSig = 0;
94350dbef1aSdholland for (j=i+1;j<map.in_use_cnt-1;j++) {
94450dbef1aSdholland memcpy (&map.blk[j], &map.blk[j+1], sizeof(map.blk[j]));
94550dbef1aSdholland map.blk[j+1].pmSig = 0;
94650dbef1aSdholland }
94750dbef1aSdholland map.in_use_cnt -= 1;
94850dbef1aSdholland }
94950dbef1aSdholland }
95050dbef1aSdholland
95150dbef1aSdholland /*
95250dbef1aSdholland * Step 4, try to identify the mount points for the partitions
95350dbef1aSdholland * and adjust the pmMapBlkCnt in each Map entry. Set
95450dbef1aSdholland * up the display array for the non-reserved partitions,
95550dbef1aSdholland * and count the number of NetBSD usable partitions
95650dbef1aSdholland */
95750dbef1aSdholland map.hfs_cnt = 0;
95850dbef1aSdholland map.root_cnt = 0;
95950dbef1aSdholland map.swap_cnt = 0;
96050dbef1aSdholland map.usr_cnt = 0;
96150dbef1aSdholland map.usable_cnt = 0;
96250dbef1aSdholland /*
96350dbef1aSdholland * Clear out record of partition slots already in use
96450dbef1aSdholland */
96550dbef1aSdholland memset(&in_use, 0, sizeof(in_use));
96650dbef1aSdholland for (i=0,j=0;i<map.in_use_cnt;i++) {
96750dbef1aSdholland map.blk[i].pmSig = APPLE_PART_MAP_ENTRY_MAGIC;
96850dbef1aSdholland map.blk[i].pmMapBlkCnt = map.in_use_cnt;
96950dbef1aSdholland /*
97050dbef1aSdholland * Since MAXPARTITIONS == 8 for mac68k, and we do not display
97150dbef1aSdholland * the c partition, we only need 7 partition slots on the screen.
97250dbef1aSdholland * If/when MAXPARTITIONS is changed, the "Edit Disk Partition Map"
97350dbef1aSdholland * needs to be a scrollable view of the partition table.
97450dbef1aSdholland */
97550dbef1aSdholland if (whichType(&map.blk[i]) && (j < MAXPARTITIONS - 1)) {
97650dbef1aSdholland map.mblk[j++] = i;
97750dbef1aSdholland map.usable_cnt += 1;
97850dbef1aSdholland }
97950dbef1aSdholland }
98050dbef1aSdholland /*
98150dbef1aSdholland * Fill in standard partitions. Look for a Cluster "0" first and use
98250dbef1aSdholland * it, otherwise take any Cluster value.
98350dbef1aSdholland */
98450dbef1aSdholland if (findStdType(map.in_use_cnt, in_use, ROOT_PART, &map.root_cnt, 0))
98550dbef1aSdholland findStdType(map.in_use_cnt, in_use, ROOT_PART, &map.root_cnt, -1);
98650dbef1aSdholland if (findStdType(map.in_use_cnt, in_use, UFS_PART, &map.usr_cnt, 0))
98750dbef1aSdholland findStdType(map.in_use_cnt, in_use, UFS_PART, &map.usr_cnt, -1);
98850dbef1aSdholland if (findStdType(map.in_use_cnt, in_use, SWAP_PART, &map.swap_cnt, 0))
98950dbef1aSdholland findStdType(map.in_use_cnt, in_use, SWAP_PART, &map.swap_cnt, -1);
99050dbef1aSdholland
99150dbef1aSdholland #ifdef MD_DEBUG_SORT_MERGE
99250dbef1aSdholland md_debug_dump("After marking Standard Types");
99350dbef1aSdholland #endif
99450dbef1aSdholland /*
99550dbef1aSdholland * Now fill in the remaining partitions counting them by type and
99650dbef1aSdholland * assigning them the slot the where the kernel should map them.
99750dbef1aSdholland * This will be where they are displayed in the Edit Map.
99850dbef1aSdholland */
99950dbef1aSdholland for (i=0; i < map.in_use_cnt; i++) {
100050dbef1aSdholland bzb = (EBZB *)&map.blk[i].pmBootArgs[0];
100150dbef1aSdholland if (bzb->flags.used == 0) {
100250dbef1aSdholland if ((j = getFreeLabelEntry(in_use)) < 0)
100350dbef1aSdholland break;
100450dbef1aSdholland switch (whichType(&map.blk[i])) {
100550dbef1aSdholland case ROOT_PART:
100650dbef1aSdholland map.root_cnt += 1;
100750dbef1aSdholland setpartition(&map.blk[i], in_use, j);
100850dbef1aSdholland break;
100950dbef1aSdholland case UFS_PART:
101050dbef1aSdholland map.usr_cnt += 1;
101150dbef1aSdholland setpartition(&map.blk[i], in_use, j);
101250dbef1aSdholland break;
101350dbef1aSdholland case SWAP_PART:
101450dbef1aSdholland map.swap_cnt += 1;
101550dbef1aSdholland setpartition(&map.blk[i], in_use, j);
101650dbef1aSdholland break;
101750dbef1aSdholland case HFS_PART:
101850dbef1aSdholland map.hfs_cnt += 1;
101950dbef1aSdholland setpartition(&map.blk[i], in_use, j);
102050dbef1aSdholland break;
102150dbef1aSdholland case SCRATCH_PART:
102250dbef1aSdholland setpartition(&map.blk[i], in_use, j);
102350dbef1aSdholland default:
102450dbef1aSdholland break;
102550dbef1aSdholland }
102650dbef1aSdholland }
102750dbef1aSdholland }
102850dbef1aSdholland #ifdef MD_DEBUG_SORT_MERGE
102950dbef1aSdholland md_debug_dump("After sort merge");
103050dbef1aSdholland #endif
103150dbef1aSdholland return;
103250dbef1aSdholland }
103350dbef1aSdholland
103450dbef1aSdholland void
disp_selected_part(sel)103550dbef1aSdholland disp_selected_part(sel)
103650dbef1aSdholland int sel;
103750dbef1aSdholland {
103850dbef1aSdholland int i,j;
103950dbef1aSdholland char fstyp[16], use[16], name[32];
104050dbef1aSdholland EBZB *bzb;
104150dbef1aSdholland
10424103857bSmartin msg_table_add(MSG_mac68k_part_header);
104350dbef1aSdholland for (i=0;i<map.usable_cnt;i++) {
104450dbef1aSdholland if (i == sel) msg_standout();
104550dbef1aSdholland j = map.mblk[i];
104650dbef1aSdholland getFstype(&map.blk[j], sizeof(fstyp), fstyp);
104750dbef1aSdholland getUse(&map.blk[j], sizeof(use), use);
104850dbef1aSdholland getName(&map.blk[j], sizeof(name), name);
104950dbef1aSdholland bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
105024ecf24eSchristos msg_fmt_table_add(MSG_part_row, "%s%c%d%d%s%s%s",
105124ecf24eSchristos pm->diskdev,
105250dbef1aSdholland bzb->flags.part, map.blk[j].pmPyPartStart,
105350dbef1aSdholland map.blk[j].pmPartBlkCnt, fstyp, use, name);
105450dbef1aSdholland if (i == sel) msg_standend();
105550dbef1aSdholland }
105650dbef1aSdholland return;
105750dbef1aSdholland }
105850dbef1aSdholland
105950dbef1aSdholland /*
106050dbef1aSdholland * check for any anomalies on the requested setup
106150dbef1aSdholland */
106250dbef1aSdholland static int
check_for_errors()106350dbef1aSdholland check_for_errors()
106450dbef1aSdholland {
106550dbef1aSdholland int i, j;
106650dbef1aSdholland int errs = 0;
106750dbef1aSdholland
106850dbef1aSdholland errs = (!map.root_cnt) || (map.root_cnt > 1) || (!map.swap_cnt) ||
106950dbef1aSdholland (map.swap_cnt > 1);
107050dbef1aSdholland
107150dbef1aSdholland for (i=0;i<map.usable_cnt;i++) {
107250dbef1aSdholland j = map.mblk[i];
10734b2364d9Smartin if (map.blk[j].pmPyPartStart > pm->dlsize)
107450dbef1aSdholland errs++;
10754b2364d9Smartin if ((map.blk[j].pmPyPartStart + map.blk[j].pmPartBlkCnt) > pm->dlsize + 1)
107650dbef1aSdholland errs++;
107750dbef1aSdholland }
107850dbef1aSdholland return(errs);
107950dbef1aSdholland }
108050dbef1aSdholland
108150dbef1aSdholland /*
108250dbef1aSdholland * check for and report anomalies on the requested setup
108350dbef1aSdholland */
108450dbef1aSdholland void
report_errors()108550dbef1aSdholland report_errors()
108650dbef1aSdholland {
108750dbef1aSdholland int i, j;
108850dbef1aSdholland int errs = 0;
108950dbef1aSdholland EBZB *bzb;
109050dbef1aSdholland
109150dbef1aSdholland if (!map.root_cnt) {
109250dbef1aSdholland msg_display_add(MSG_disksetup_no_root);
109350dbef1aSdholland errs++;
109450dbef1aSdholland }
109550dbef1aSdholland if (map.root_cnt > 1) {
109650dbef1aSdholland msg_display_add(MSG_disksetup_multiple_roots);
109750dbef1aSdholland errs++;
109850dbef1aSdholland }
109950dbef1aSdholland if (!map.swap_cnt) {
110050dbef1aSdholland msg_display_add(MSG_disksetup_no_swap);
110150dbef1aSdholland errs++;
110250dbef1aSdholland }
110350dbef1aSdholland if (map.swap_cnt > 1) {
110450dbef1aSdholland msg_display_add(MSG_disksetup_multiple_swaps);
110550dbef1aSdholland errs++;
110650dbef1aSdholland }
110750dbef1aSdholland for (i=0;i<map.usable_cnt;i++) {
110850dbef1aSdholland j = map.mblk[i];
11094b2364d9Smartin if (map.blk[j].pmPyPartStart > pm->dlsize) {
111050dbef1aSdholland bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
111124ecf24eSchristos msg_fmt_display_add(MSG_disksetup_part_beginning, "%s%c",
11124b2364d9Smartin pm->diskdev, bzb->flags.part);
111350dbef1aSdholland errs++;
111450dbef1aSdholland }
11154b2364d9Smartin if ((map.blk[j].pmPyPartStart + map.blk[j].pmPartBlkCnt) > pm->dlsize) {
111650dbef1aSdholland bzb = (EBZB *)&map.blk[j].pmBootArgs[0];
111724ecf24eSchristos msg_fmt_display_add(MSG_disksetup_part_size, "%s%c",
11184b2364d9Smartin pm->diskdev, bzb->flags.part);
111950dbef1aSdholland errs++;
112050dbef1aSdholland }
112150dbef1aSdholland }
112250dbef1aSdholland if (!errs)
112350dbef1aSdholland msg_display_add(MSG_disksetup_noerrors);
112450dbef1aSdholland return;
112550dbef1aSdholland }
112650dbef1aSdholland
112750dbef1aSdholland static int
edit_diskmap(void)112850dbef1aSdholland edit_diskmap(void)
112950dbef1aSdholland {
113050dbef1aSdholland int i;
113150dbef1aSdholland
113250dbef1aSdholland /* Ask full/part */
113324ecf24eSchristos msg_fmt_display (MSG_mac68k_fullpart, "%s", pm->diskdev);
113450dbef1aSdholland process_menu (MENU_fullpart, NULL);
113550dbef1aSdholland
113650dbef1aSdholland map.selected = 0;
113750dbef1aSdholland sortmerge();
113850dbef1aSdholland
113950dbef1aSdholland /* If blowing away the whole disk, let user know if there
114050dbef1aSdholland * are any active disk partitions */
114150dbef1aSdholland if (usefull) {
114250dbef1aSdholland if (map.usable_cnt > (map.root_cnt+map.swap_cnt+map.usr_cnt)) {
11434103857bSmartin msg_display (MSG_mac68k_ovrwrite);
1144e21052b4Smartin if (!ask_noyes(NULL)) {
114550dbef1aSdholland endwin();
114650dbef1aSdholland return 0;
114750dbef1aSdholland }
114850dbef1aSdholland }
114950dbef1aSdholland /*
115050dbef1aSdholland * mark all non-reserved partitions as "free"
115150dbef1aSdholland * then sort and merge the map into something sensible
115250dbef1aSdholland */
115350dbef1aSdholland for (i=0;i<map.size;i++)
115450dbef1aSdholland if (whichType(&map.blk[i]))
115550dbef1aSdholland strcpy ((char *)map.blk[i].pmPartType, "Apple_Free");
115650dbef1aSdholland sortmerge();
115750dbef1aSdholland }
115850dbef1aSdholland process_menu (MENU_editparttable, NULL);
115950dbef1aSdholland return 1;
116050dbef1aSdholland }
116150dbef1aSdholland
116250dbef1aSdholland #ifdef MD_DEBUG_SORT_MERGE
116350dbef1aSdholland static int
md_debug_dump(title)116450dbef1aSdholland md_debug_dump(title)
116550dbef1aSdholland char *title;
116650dbef1aSdholland {
116750dbef1aSdholland char buf[96], type;
116850dbef1aSdholland char fstyp[16], use[16], name[64];
1169cc33bc91Smartin int i, j, rv;
117050dbef1aSdholland EBZB *bzb;
117150dbef1aSdholland
117250dbef1aSdholland msg_clear();
117350dbef1aSdholland sprintf(buf, "Apple Disk Partition Map: %s", title);
117424ecf24eSchristos msg_fmt_table_add(MSG_dump_line, "%s", buf);
117524ecf24eSchristos msg_fmt_table_add(MSG_dump_line, "%s",
117650dbef1aSdholland "slot base fstype use name");
117750dbef1aSdholland for (i=0;i<map.in_use_cnt;i++) {
117850dbef1aSdholland j = whichType(&map.blk[i]);
117950dbef1aSdholland getFstype(&map.blk[i], sizeof(fstyp), fstyp);
118050dbef1aSdholland getUse(&map.blk[i], sizeof(use), use);
118150dbef1aSdholland getName(&map.blk[i], sizeof(name), name);
118250dbef1aSdholland bzb = (EBZB *) &map.blk[i].pmBootArgs[0];
118350dbef1aSdholland type = bzb->flags.part;
118450dbef1aSdholland if (type < 'a' || type > 'h') type = '?';
118550dbef1aSdholland if (j == 0) strcpy (name, "reserved for Apple");
118650dbef1aSdholland sprintf(buf, " %02d:%c %08x %8s %10s %s", i+1, type,
118750dbef1aSdholland map.blk[i].pmPyPartStart, fstyp, use, name);
118824ecf24eSchristos msg_fmt_table_add(MSG_dump_line, "%s", buf);
118950dbef1aSdholland }
1190cc33bc91Smartin process_menu(MENU_okabort, &rv);
119150dbef1aSdholland msg_clear();
1192cc33bc91Smartin return rv;
119350dbef1aSdholland }
119450dbef1aSdholland #endif /* MD_DEBUG_SORT_MERGE */
119550dbef1aSdholland
119650dbef1aSdholland int
md_pre_mount(struct install_partition_desc * install,size_t ndx)11974f30cbf3Smartin md_pre_mount(struct install_partition_desc *install, size_t ndx)
119850dbef1aSdholland {
119950dbef1aSdholland return 0;
120050dbef1aSdholland }
12014103857bSmartin
12024103857bSmartin bool
md_parts_use_wholedisk(struct disk_partitions * parts)12034103857bSmartin md_parts_use_wholedisk(struct disk_partitions *parts)
12044103857bSmartin {
12054103857bSmartin return parts_use_wholedisk(parts, 0, NULL);
12064103857bSmartin }
12074103857bSmartin
12084103857bSmartin #ifdef HAVE_GPT
12094103857bSmartin bool
md_gpt_post_write(struct disk_partitions * parts,part_id root_id,bool root_is_new,part_id efi_id,bool efi_is_new)12104103857bSmartin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
12114103857bSmartin bool root_is_new, part_id efi_id, bool efi_is_new)
12124103857bSmartin {
12134103857bSmartin /* no GPT boot support, nothing needs to be done here */
12144103857bSmartin return true;
12154103857bSmartin }
12164103857bSmartin #endif
12174103857bSmartin
1218