Home | History | Annotate | Line # | Download | only in ofppc
md.c revision 1.3
      1  1.3    martin /*	$NetBSD: md.c,v 1.3 2015/05/10 10:14:03 martin 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 -- ofppc machine specific routines */
     36  1.1  dholland 
     37  1.1  dholland #include <sys/param.h>
     38  1.1  dholland #include <sys/sysctl.h>
     39  1.1  dholland #include <sys/disklabel_rdb.h>
     40  1.1  dholland #include <stdio.h>
     41  1.1  dholland #include <util.h>
     42  1.1  dholland #include <machine/cpu.h>
     43  1.1  dholland 
     44  1.1  dholland #include "defs.h"
     45  1.1  dholland #include "md.h"
     46  1.1  dholland #include "msg_defs.h"
     47  1.1  dholland #include "menu_defs.h"
     48  1.1  dholland #include "endian.h"
     49  1.1  dholland 
     50  1.1  dholland static int check_rdb(void);
     51  1.1  dholland static uint32_t rdbchksum(void *);
     52  1.1  dholland 
     53  1.1  dholland /* We use MBR_PTYPE_PREP like port-prep does. */
     54  1.1  dholland static int nonewfsmsdos = 0, nobootfix = 0, noprepfix=0;
     55  1.1  dholland static int bootpart_fat12 = PART_BOOT_FAT12;
     56  1.1  dholland static int bootpart_binfo = PART_BOOT_BINFO;
     57  1.1  dholland static int bootpart_prep = PART_BOOT_PREP;
     58  1.1  dholland static int bootinfo_mbr = 1;
     59  1.1  dholland static int rdb_found = 0;
     60  1.1  dholland 
     61  1.1  dholland /* bootstart/bootsize are for the fat */
     62  1.1  dholland int binfostart, binfosize, bprepstart, bprepsize;
     63  1.1  dholland 
     64  1.1  dholland void
     65  1.1  dholland md_init(void)
     66  1.1  dholland {
     67  1.1  dholland }
     68  1.1  dholland 
     69  1.1  dholland void
     70  1.1  dholland md_init_set_status(int flags)
     71  1.1  dholland {
     72  1.1  dholland 
     73  1.1  dholland 	(void)flags;
     74  1.1  dholland }
     75  1.1  dholland 
     76  1.1  dholland int
     77  1.1  dholland md_get_info(void)
     78  1.1  dholland {
     79  1.1  dholland 
     80  1.1  dholland 	if (check_rdb())
     81  1.1  dholland 		return 1;
     82  1.1  dholland 
     83  1.1  dholland 	return set_bios_geom_with_mbr_guess();
     84  1.1  dholland }
     85  1.1  dholland 
     86  1.1  dholland /*
     87  1.1  dholland  * md back-end code for menu-driven BSD disklabel editor.
     88  1.1  dholland  */
     89  1.1  dholland int
     90  1.1  dholland md_make_bsd_partitions(void)
     91  1.1  dholland {
     92  1.1  dholland 	int i;
     93  1.1  dholland 	int part;
     94  1.1  dholland 	int maxpart = getmaxpartitions();
     95  1.1  dholland 	int partstart;
     96  1.1  dholland 	int part_raw, part_bsd;
     97  1.1  dholland 	int ptend;
     98  1.1  dholland 	int no_swap = 0;
     99  1.1  dholland 	partinfo *p;
    100  1.1  dholland 
    101  1.1  dholland 	if (rdb_found) {
    102  1.1  dholland 		/*
    103  1.1  dholland 		 * We found RDB partitions on the disk, which cannot be
    104  1.1  dholland 		 * modified by rewriting the disklabel.
    105  1.1  dholland 		 * So just use what we have got.
    106  1.1  dholland 		 */
    107  1.1  dholland 		for (part = 0; part < maxpart; part++) {
    108  1.2    martin 			if (PI_ISBSDFS(&pm->bsdlabel[part])) {
    109  1.2    martin 				pm->bsdlabel[part].pi_flags |=
    110  1.1  dholland 				    PIF_NEWFS | PIF_MOUNT;
    111  1.1  dholland 
    112  1.1  dholland 				if (part == PART_A)
    113  1.2    martin 					strcpy(pm->bsdlabel[part].pi_mount, "/");
    114  1.1  dholland 			}
    115  1.1  dholland 		}
    116  1.1  dholland 
    117  1.1  dholland 		part_bsd = part_raw = getrawpartition();
    118  1.1  dholland 		if (part_raw == -1)
    119  1.1  dholland 			part_raw = PART_C;	/* for sanity... */
    120  1.2    martin 		pm->bsdlabel[part_raw].pi_offset = 0;
    121  1.2    martin 		pm->bsdlabel[part_raw].pi_size = pm->dlsize;
    122  1.1  dholland 
    123  1.1  dholland 		set_sizemultname_meg();
    124  1.1  dholland rdb_edit_check:
    125  1.2    martin 		if (edit_and_check_label(pm->bsdlabel, maxpart, part_raw,
    126  1.1  dholland 		    part_bsd) == 0) {
    127  1.1  dholland 			msg_display(MSG_abort);
    128  1.1  dholland 			return 0;
    129  1.1  dholland 		}
    130  1.1  dholland 		if (md_check_partitions() == 0)
    131  1.1  dholland 			goto rdb_edit_check;
    132  1.1  dholland 
    133  1.1  dholland 		return 1;
    134  1.1  dholland 	}
    135  1.1  dholland 
    136  1.1  dholland 	/*
    137  1.1  dholland 	 * Initialize global variables that track space used on this disk.
    138  1.1  dholland 	 * Standard 4.4BSD 8-partition labels always cover whole disk.
    139  1.1  dholland 	 */
    140  1.2    martin 	if (pm->ptsize == 0)
    141  1.2    martin 		pm->ptsize = pm->dlsize - pm->ptstart;
    142  1.2    martin 	if (pm->dlsize == 0)
    143  1.2    martin 		pm->dlsize = pm->ptstart + pm->ptsize;
    144  1.1  dholland 
    145  1.2    martin 	partstart = pm->ptstart;
    146  1.2    martin 	ptend = pm->ptstart + pm->ptsize;
    147  1.1  dholland 
    148  1.1  dholland 	/* Ask for layout type -- standard or special */
    149  1.1  dholland 	msg_display(MSG_layout,
    150  1.2    martin 		    pm->ptsize / (MEG / pm->sectorsize),
    151  1.1  dholland 		    DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE,
    152  1.1  dholland 		    DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE + XNEEDMB);
    153  1.1  dholland 
    154  1.1  dholland 	process_menu(MENU_layout, NULL);
    155  1.1  dholland 
    156  1.1  dholland 	/* Set so we use the 'real' geometry for rounding, input in MB */
    157  1.2    martin 	pm->current_cylsize = pm->dlcylsize;
    158  1.1  dholland 	set_sizemultname_meg();
    159  1.1  dholland 
    160  1.1  dholland 	/* Build standard partitions */
    161  1.2    martin 	memset(&pm->bsdlabel, 0, sizeof pm->bsdlabel);
    162  1.1  dholland 
    163  1.1  dholland 	/* Set initial partition types to unused */
    164  1.1  dholland 	for (part = 0 ; part < maxpart ; ++part)
    165  1.2    martin 		pm->bsdlabel[part].pi_fstype = FS_UNUSED;
    166  1.1  dholland 
    167  1.1  dholland 	/* Whole disk partition */
    168  1.1  dholland 	part_raw = getrawpartition();
    169  1.1  dholland 	if (part_raw == -1)
    170  1.1  dholland 		part_raw = PART_C;	/* for sanity... */
    171  1.2    martin 	pm->bsdlabel[part_raw].pi_offset = 0;
    172  1.2    martin 	pm->bsdlabel[part_raw].pi_size = pm->dlsize;
    173  1.1  dholland 
    174  1.1  dholland 	if (part_raw == PART_D) {
    175  1.1  dholland 		/* Probably a system that expects an i386 style mbr */
    176  1.1  dholland 		part_bsd = PART_C;
    177  1.2    martin 		pm->bsdlabel[PART_C].pi_offset = pm->ptstart;
    178  1.2    martin 		pm->bsdlabel[PART_C].pi_size = pm->ptsize;
    179  1.1  dholland 	} else {
    180  1.1  dholland 		part_bsd = part_raw;
    181  1.1  dholland 	}
    182  1.1  dholland 
    183  1.2    martin 	if (pm->bootsize != 0) {
    184  1.2    martin 		pm->bsdlabel[PART_BOOT_FAT12].pi_fstype = FS_MSDOS;
    185  1.2    martin 		pm->bsdlabel[PART_BOOT_FAT12].pi_size = pm->bootsize;
    186  1.2    martin 		pm->bsdlabel[PART_BOOT_FAT12].pi_offset = pm->bootstart;
    187  1.2    martin 		pm->bsdlabel[PART_BOOT_FAT12].pi_flags |= PART_BOOT_FAT12_PI_FLAGS;
    188  1.2    martin 		strlcpy(pm->bsdlabel[PART_BOOT_FAT12].pi_mount,
    189  1.1  dholland 		    PART_BOOT_FAT12_PI_MOUNT,
    190  1.2    martin 		    sizeof pm->bsdlabel[PART_BOOT_FAT12].pi_mount);
    191  1.1  dholland 	}
    192  1.1  dholland 	if (binfosize != 0) {
    193  1.2    martin 		pm->bsdlabel[PART_BOOT_BINFO].pi_fstype = FS_OTHER;
    194  1.2    martin 		pm->bsdlabel[PART_BOOT_BINFO].pi_size = binfosize;
    195  1.2    martin 		pm->bsdlabel[PART_BOOT_BINFO].pi_offset = binfostart;
    196  1.1  dholland 	}
    197  1.1  dholland 	if (bprepsize != 0) {
    198  1.2    martin 		pm->bsdlabel[PART_BOOT_PREP].pi_fstype = FS_BOOT;
    199  1.2    martin 		pm->bsdlabel[PART_BOOT_PREP].pi_size = bprepsize;
    200  1.2    martin 		pm->bsdlabel[PART_BOOT_PREP].pi_offset = bprepstart;
    201  1.1  dholland 	}
    202  1.1  dholland 
    203  1.1  dholland #ifdef PART_REST
    204  1.2    martin 	pm->bsdlabel[PART_REST].pi_offset = 0;
    205  1.2    martin 	pm->bsdlabel[PART_REST].pi_size = pm->ptstart;
    206  1.1  dholland #endif
    207  1.1  dholland 
    208  1.1  dholland 	/*
    209  1.1  dholland 	 * Save any partitions that are outside the area we are
    210  1.1  dholland 	 * going to use.
    211  1.1  dholland 	 * In particular this saves details of the other MBR
    212  1.1  dholland 	 * partitions on a multiboot i386 system.
    213  1.1  dholland 	 */
    214  1.1  dholland 	 for (i = maxpart; i--;) {
    215  1.2    martin 		if (pm->bsdlabel[i].pi_size != 0)
    216  1.1  dholland 			/* Don't overwrite special partitions */
    217  1.1  dholland 			continue;
    218  1.2    martin 		p = &pm->oldlabel[i];
    219  1.1  dholland 		if (p->pi_fstype == FS_UNUSED || p->pi_size == 0)
    220  1.1  dholland 			continue;
    221  1.2    martin 		if (layoutkind == LY_USEEXIST) {
    222  1.1  dholland 			if (PI_ISBSDFS(p))
    223  1.1  dholland 				p->pi_flags |= PIF_MOUNT;
    224  1.1  dholland 		} else {
    225  1.2    martin 			if (p->pi_offset < pm->ptstart + pm->ptsize &&
    226  1.2    martin 			    p->pi_offset + p->pi_size > pm->ptstart)
    227  1.1  dholland 				/* Not outside area we are allocating */
    228  1.1  dholland 				continue;
    229  1.1  dholland 			if (p->pi_fstype == FS_SWAP)
    230  1.1  dholland 				no_swap = 1;
    231  1.1  dholland 		}
    232  1.2    martin 		pm->bsdlabel[i] = pm->oldlabel[i];
    233  1.1  dholland 	 }
    234  1.1  dholland 
    235  1.2    martin 	if (layoutkind == LY_USEEXIST) {
    236  1.1  dholland 		/* XXX Check we have a sensible layout */
    237  1.1  dholland 		;
    238  1.1  dholland 	} else
    239  1.1  dholland 		get_ptn_sizes(partstart, ptend - partstart, no_swap);
    240  1.1  dholland 
    241  1.1  dholland 	/*
    242  1.1  dholland 	 * OK, we have a partition table. Give the user the chance to
    243  1.1  dholland 	 * edit it and verify it's OK, or abort altogether.
    244  1.1  dholland 	 */
    245  1.1  dholland  edit_check:
    246  1.2    martin 	if (edit_and_check_label(pm->bsdlabel, maxpart, part_raw, part_bsd) == 0) {
    247  1.1  dholland 		msg_display(MSG_abort);
    248  1.1  dholland 		return 0;
    249  1.1  dholland 	}
    250  1.1  dholland 	if (md_check_partitions() == 0)
    251  1.1  dholland 		goto edit_check;
    252  1.1  dholland 
    253  1.1  dholland 	/* Disk name */
    254  1.2    martin 	msg_prompt(MSG_packname, pm->bsddiskname, pm->bsddiskname, sizeof pm->bsddiskname);
    255  1.1  dholland 
    256  1.1  dholland 	/* save label to disk for MI code to update. */
    257  1.2    martin 	(void) savenewlabel(pm->bsdlabel, maxpart);
    258  1.1  dholland 
    259  1.1  dholland 	/* Everything looks OK. */
    260  1.1  dholland 	return 1;
    261  1.1  dholland }
    262  1.1  dholland 
    263  1.1  dholland /*
    264  1.1  dholland  * any additional partition validation
    265  1.1  dholland  */
    266  1.1  dholland int
    267  1.1  dholland md_check_partitions(void)
    268  1.1  dholland {
    269  1.1  dholland 	int part, fprep=0, ffat=0;
    270  1.1  dholland 
    271  1.1  dholland 	if (rdb_found)
    272  1.1  dholland 		return 1;
    273  1.1  dholland 
    274  1.1  dholland 	/* we need to find a boot partition, otherwise we can't create
    275  1.1  dholland 	 * our msdos fs boot partition.  We make the assumption that
    276  1.1  dholland 	 * the user hasn't done something stupid, like move it away
    277  1.1  dholland 	 * from the MBR partition.
    278  1.1  dholland 	 */
    279  1.1  dholland 	for (part = PART_A; part < MAXPARTITIONS; part++) {
    280  1.2    martin 		if (pm->bsdlabel[part].pi_fstype == FS_MSDOS) {
    281  1.1  dholland 			bootpart_fat12 = part;
    282  1.1  dholland 			ffat++;
    283  1.2    martin 		} else if (pm->bsdlabel[part].pi_fstype == FS_BOOT) {
    284  1.1  dholland 			bootpart_prep = part;
    285  1.1  dholland 			fprep++;
    286  1.2    martin 		} else if (pm->bsdlabel[part].pi_fstype == FS_OTHER) {
    287  1.1  dholland 			bootpart_binfo = part;
    288  1.1  dholland 			fprep++;
    289  1.1  dholland 		}
    290  1.1  dholland 	}
    291  1.1  dholland 	/* oh, the confusion */
    292  1.1  dholland 	if (ffat >= 1 && fprep < 2)
    293  1.1  dholland 		return 1;
    294  1.1  dholland 	if (ffat < 1 && fprep >= 2)
    295  1.1  dholland 		return 2;
    296  1.1  dholland 	if (ffat >=1 && fprep >= 2)
    297  1.1  dholland 		return 3;
    298  1.1  dholland 
    299  1.1  dholland 	msg_display(MSG_nobootpartdisklabel);
    300  1.1  dholland 	process_menu(MENU_ok, NULL);
    301  1.1  dholland 	return 0;
    302  1.1  dholland }
    303  1.1  dholland 
    304  1.1  dholland /*
    305  1.1  dholland  * hook called before writing new disklabel.
    306  1.1  dholland  */
    307  1.1  dholland int
    308  1.1  dholland md_pre_disklabel(void)
    309  1.1  dholland {
    310  1.1  dholland 
    311  1.1  dholland 	if (rdb_found)
    312  1.1  dholland 		return 0;
    313  1.1  dholland 
    314  1.1  dholland 	msg_display(MSG_dofdisk);
    315  1.1  dholland 
    316  1.1  dholland 	/* write edited MBR onto disk. */
    317  1.2    martin 	if (write_mbr(pm->diskdev, &mbr, 1) != 0) {
    318  1.1  dholland 		msg_display(MSG_wmbrfail);
    319  1.1  dholland 		process_menu(MENU_ok, NULL);
    320  1.1  dholland 		return 1;
    321  1.1  dholland 	}
    322  1.1  dholland 	return 0;
    323  1.1  dholland }
    324  1.1  dholland 
    325  1.1  dholland /*
    326  1.1  dholland  * hook called after writing disklabel to new target disk.
    327  1.1  dholland  */
    328  1.1  dholland int
    329  1.1  dholland md_post_disklabel(void)
    330  1.1  dholland {
    331  1.1  dholland 	char bootdev[100];
    332  1.1  dholland 
    333  1.2    martin 	if (pm->bootstart == 0 || pm->bootsize == 0 || rdb_found)
    334  1.1  dholland 		return 0;
    335  1.1  dholland 
    336  1.2    martin 	snprintf(bootdev, sizeof bootdev, "/dev/r%s%c", pm->diskdev,
    337  1.1  dholland 	    'a'+bootpart_fat12);
    338  1.1  dholland 	run_program(RUN_DISPLAY, "/sbin/newfs_msdos %s", bootdev);
    339  1.1  dholland 
    340  1.1  dholland 	return 0;
    341  1.1  dholland }
    342  1.1  dholland 
    343  1.1  dholland /*
    344  1.1  dholland  * hook called after upgrade() or install() has finished setting
    345  1.1  dholland  * up the target disk but immediately before the user is given the
    346  1.1  dholland  * ``disks are now set up'' message.
    347  1.1  dholland  */
    348  1.1  dholland int
    349  1.1  dholland md_post_newfs(void)
    350  1.1  dholland {
    351  1.1  dholland 
    352  1.1  dholland 	/* No bootblock. We use ofwboot from a partition visiable by OFW. */
    353  1.1  dholland 	return 0;
    354  1.1  dholland }
    355  1.1  dholland 
    356  1.1  dholland int
    357  1.1  dholland md_post_extract(void)
    358  1.1  dholland {
    359  1.1  dholland 	char bootdev[100], bootbdev[100], version[64];
    360  1.1  dholland 
    361  1.1  dholland 	/* if we can't make it bootable, just punt */
    362  1.1  dholland 	if ((nobootfix && noprepfix) || rdb_found)
    363  1.1  dholland 		return 0;
    364  1.1  dholland 
    365  1.1  dholland 	snprintf(version, sizeof version, "NetBSD/%s %s", MACH, REL);
    366  1.1  dholland 	run_program(RUN_DISPLAY, "/usr/mdec/mkbootinfo '%s' %d "
    367  1.1  dholland 	    "/tmp/bootinfo.txt", version, bootinfo_mbr);
    368  1.1  dholland 
    369  1.1  dholland 	if (!nobootfix) {
    370  1.1  dholland 		run_program(RUN_DISPLAY, "/bin/mkdir -p /%s/boot/ppc",
    371  1.1  dholland 		    target_prefix());
    372  1.1  dholland 		run_program(RUN_DISPLAY, "/bin/mkdir -p /%s/boot/netbsd",
    373  1.1  dholland 		    target_prefix());
    374  1.1  dholland 		run_program(RUN_DISPLAY, "/bin/cp /usr/mdec/ofwboot "
    375  1.1  dholland 		    "/%s/boot/netbsd", target_prefix());
    376  1.1  dholland 		run_program(RUN_DISPLAY, "/bin/cp /tmp/bootinfo.txt "
    377  1.1  dholland 		    "/%s/boot/ppc", target_prefix());
    378  1.1  dholland 		run_program(RUN_DISPLAY, "/bin/cp /usr/mdec/ofwboot "
    379  1.1  dholland 		    "/%s/boot/ofwboot", target_prefix());
    380  1.1  dholland 	}
    381  1.1  dholland 
    382  1.1  dholland 	if (!noprepfix) {
    383  1.2    martin 		snprintf(bootdev, sizeof bootdev, "/dev/r%s%c", pm->diskdev,
    384  1.1  dholland 		    'a'+bootpart_prep);
    385  1.2    martin 		snprintf(bootbdev, sizeof bootbdev, "/dev/%s%c", pm->diskdev,
    386  1.1  dholland 		    'a'+bootpart_prep);
    387  1.1  dholland 		run_program(RUN_DISPLAY, "/bin/dd if=/dev/zero of=%s bs=512",
    388  1.1  dholland 		    bootdev);
    389  1.1  dholland 		run_program(RUN_DISPLAY, "/bin/dd if=/usr/mdec/ofwboot "
    390  1.1  dholland 		    "of=%s bs=512", bootbdev);
    391  1.1  dholland 
    392  1.2    martin 		snprintf(bootdev, sizeof bootdev, "/dev/r%s%c", pm->diskdev,
    393  1.1  dholland 		    'a'+bootpart_binfo);
    394  1.2    martin 		snprintf(bootbdev, sizeof bootbdev, "/dev/%s%c", pm->diskdev,
    395  1.1  dholland 		    'a'+bootpart_binfo);
    396  1.1  dholland 		run_program(RUN_DISPLAY, "/bin/dd if=/dev/zero of=%s bs=512",
    397  1.1  dholland 		    bootdev);
    398  1.1  dholland 		run_program(RUN_DISPLAY, "/bin/dd if=/tmp/bootinfo.txt "
    399  1.1  dholland 		    "of=%s bs=512", bootbdev);
    400  1.1  dholland 	}
    401  1.1  dholland 
    402  1.1  dholland 	return 0;
    403  1.1  dholland }
    404  1.1  dholland 
    405  1.1  dholland void
    406  1.1  dholland md_cleanup_install(void)
    407  1.1  dholland {
    408  1.1  dholland 
    409  1.1  dholland #ifndef DEBUG
    410  1.1  dholland 	enable_rc_conf();
    411  1.1  dholland #endif
    412  1.1  dholland }
    413  1.1  dholland 
    414  1.1  dholland int
    415  1.1  dholland md_pre_update(void)
    416  1.1  dholland {
    417  1.1  dholland 	struct mbr_partition *part;
    418  1.1  dholland 	mbr_info_t *ext;
    419  1.1  dholland 	int i;
    420  1.1  dholland 
    421  1.1  dholland 	if (check_rdb())
    422  1.1  dholland 		return 1;
    423  1.1  dholland 
    424  1.2    martin 	read_mbr(pm->diskdev, &mbr);
    425  1.1  dholland 	/* do a sanity check of the partition table */
    426  1.1  dholland 	for (ext = &mbr; ext; ext = ext->extended) {
    427  1.1  dholland 		part = ext->mbr.mbr_parts;
    428  1.1  dholland 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
    429  1.1  dholland 			if (part->mbrp_type == MBR_PTYPE_PREP &&
    430  1.1  dholland 			    part->mbrp_size > 50)
    431  1.1  dholland 				bootinfo_mbr = i+1;
    432  1.1  dholland 			if (part->mbrp_type == MBR_PTYPE_RESERVED_x21 &&
    433  1.1  dholland 			    part->mbrp_size < (MIN_FAT12_BOOT/512)) {
    434  1.1  dholland 				msg_display(MSG_boottoosmall);
    435  1.1  dholland 				msg_display_add(MSG_nobootpartdisklabel, 0);
    436  1.3    martin 				if (!ask_yesno(NULL))
    437  1.1  dholland 					return 0;
    438  1.1  dholland 				nobootfix = 1;
    439  1.1  dholland 			}
    440  1.1  dholland 		}
    441  1.1  dholland 	}
    442  1.1  dholland 
    443  1.1  dholland 	i = md_check_partitions();
    444  1.1  dholland 	switch (i) {
    445  1.1  dholland 	case 0: nobootfix=1; noprepfix=1; break;
    446  1.1  dholland 	case 1: noprepfix=1; break;
    447  1.1  dholland 	case 2: nobootfix=1; break;
    448  1.1  dholland 	default: break;
    449  1.1  dholland 	}
    450  1.1  dholland 
    451  1.1  dholland 	return 1;
    452  1.1  dholland }
    453  1.1  dholland 
    454  1.1  dholland /* Upgrade support */
    455  1.1  dholland int
    456  1.1  dholland md_update(void)
    457  1.1  dholland {
    458  1.1  dholland 
    459  1.1  dholland 	nonewfsmsdos = 1;
    460  1.1  dholland 	md_post_newfs();
    461  1.1  dholland 	return 1;
    462  1.1  dholland }
    463  1.1  dholland 
    464  1.1  dholland 
    465  1.1  dholland int
    466  1.1  dholland md_check_mbr(mbr_info_t *mbri)
    467  1.1  dholland {
    468  1.1  dholland 	mbr_info_t *ext;
    469  1.1  dholland 	struct mbr_partition *part;
    470  1.1  dholland 	int i;
    471  1.1  dholland 
    472  1.1  dholland 	for (ext = mbri; ext; ext = ext->extended) {
    473  1.1  dholland 		part = ext->mbr.mbr_parts;
    474  1.1  dholland 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
    475  1.1  dholland 			if (part->mbrp_type == MBR_PTYPE_FAT12) {
    476  1.2    martin 				pm->bootstart = part->mbrp_start;
    477  1.2    martin 				pm->bootsize = part->mbrp_size;
    478  1.1  dholland 			} else if (part->mbrp_type == MBR_PTYPE_PREP &&
    479  1.1  dholland 			    part->mbrp_size < 50) {
    480  1.1  dholland 				/* this is the bootinfo partition */
    481  1.1  dholland 				binfostart = part->mbrp_start;
    482  1.1  dholland 				binfosize = part->mbrp_size;
    483  1.1  dholland 				bootinfo_mbr = i+1;
    484  1.1  dholland 			} else if (part->mbrp_type == MBR_PTYPE_PREP &&
    485  1.1  dholland 			    part->mbrp_size > 50) {
    486  1.1  dholland 				bprepstart = part->mbrp_start;
    487  1.1  dholland 				bprepsize = part->mbrp_size;
    488  1.1  dholland 			}
    489  1.1  dholland 			break;
    490  1.1  dholland 		}
    491  1.1  dholland 	}
    492  1.1  dholland 
    493  1.1  dholland 	/* we need to either have a pair of prep partitions, or a single
    494  1.1  dholland 	 * fat.  if neither, things are broken. */
    495  1.2    martin 	if (!(pm->bootsize >= (MIN_FAT12_BOOT/512) ||
    496  1.1  dholland 		(binfosize >= (MIN_BINFO_BOOT/512) &&
    497  1.1  dholland 		    bprepsize >= (MIN_PREP_BOOT/512)))) {
    498  1.1  dholland 		msg_display(MSG_bootnotright);
    499  1.1  dholland 		msg_display_add(MSG_reeditpart, 0);
    500  1.3    martin 		if (!ask_yesno(NULL))
    501  1.1  dholland 			return 0;
    502  1.1  dholland 		return 1;
    503  1.1  dholland 	}
    504  1.1  dholland 
    505  1.1  dholland 	/* check the prep partitions */
    506  1.1  dholland 	if ((binfosize > 0 || bprepsize > 0) &&
    507  1.1  dholland 	    (binfosize < (MIN_BINFO_BOOT/512) ||
    508  1.1  dholland 		bprepsize < (MIN_PREP_BOOT/512))) {
    509  1.1  dholland 		msg_display(MSG_preptoosmall);
    510  1.1  dholland 		msg_display_add(MSG_reeditpart, 0);
    511  1.3    martin 		if (!ask_yesno(NULL))
    512  1.1  dholland 			return 0;
    513  1.1  dholland 		return 1;
    514  1.1  dholland 	}
    515  1.1  dholland 
    516  1.1  dholland 	/* check the fat12 parititons */
    517  1.2    martin 	if (pm->bootsize > 0 && pm->bootsize < (MIN_FAT12_BOOT/512)) {
    518  1.1  dholland 		msg_display(MSG_boottoosmall);
    519  1.1  dholland 		msg_display_add(MSG_reeditpart, 0);
    520  1.3    martin 		if (!ask_yesno(NULL))
    521  1.1  dholland 			return 0;
    522  1.1  dholland 		return 1;
    523  1.1  dholland 	}
    524  1.1  dholland 
    525  1.1  dholland 	/* if both sets contain zero, thats bad */
    526  1.2    martin 	if ((pm->bootstart == 0 || pm->bootsize == 0) &&
    527  1.1  dholland 	    (binfosize == 0 || binfostart == 0 ||
    528  1.1  dholland 		bprepsize == 0 || bprepstart == 0)) {
    529  1.1  dholland 		msg_display(MSG_nobootpart);
    530  1.1  dholland 		msg_display_add(MSG_reeditpart, 0);
    531  1.3    martin 		if (!ask_yesno(NULL))
    532  1.1  dholland 			return 0;
    533  1.1  dholland 		return 1;
    534  1.1  dholland 	}
    535  1.1  dholland 	return 2;
    536  1.1  dholland }
    537  1.1  dholland 
    538  1.1  dholland /*
    539  1.1  dholland  * NOTE, we use a reserved partition type, because some RS/6000 machines hang
    540  1.1  dholland  * hard if they find a FAT12, and if we use type prep, that indicates that
    541  1.1  dholland  * it should be read raw.
    542  1.1  dholland  * One partition for FAT12 booting
    543  1.1  dholland  * One partition for NetBSD
    544  1.1  dholland  * One partition to hold the bootinfo.txt file
    545  1.1  dholland  * One partition to hold ofwboot
    546  1.1  dholland  */
    547  1.1  dholland 
    548  1.1  dholland int
    549  1.1  dholland md_mbr_use_wholedisk(mbr_info_t *mbri)
    550  1.1  dholland {
    551  1.1  dholland 	struct mbr_sector *mbrs = &mbri->mbr;
    552  1.1  dholland 	mbr_info_t *ext;
    553  1.1  dholland 	struct mbr_partition *part;
    554  1.1  dholland 
    555  1.1  dholland 	part = &mbrs->mbr_parts[0];
    556  1.1  dholland 	/* Set the partition information for full disk usage. */
    557  1.1  dholland 	while ((ext = mbri->extended)) {
    558  1.1  dholland 		mbri->extended = ext->extended;
    559  1.1  dholland 		free(ext);
    560  1.1  dholland 	}
    561  1.1  dholland 	memset(part, 0, MBR_PART_COUNT * sizeof *part);
    562  1.1  dholland 
    563  1.1  dholland 	part[0].mbrp_type = MBR_PTYPE_RESERVED_x21;
    564  1.1  dholland 	part[0].mbrp_size = FAT12_BOOT_SIZE/512;
    565  1.1  dholland 	part[0].mbrp_start = bsec;
    566  1.1  dholland 	part[0].mbrp_flag = 0;
    567  1.1  dholland 
    568  1.1  dholland 	part[1].mbrp_type = MBR_PTYPE_NETBSD;
    569  1.2    martin 	part[1].mbrp_size = pm->dlsize - (bsec + FAT12_BOOT_SIZE/512 +
    570  1.1  dholland 	    BINFO_BOOT_SIZE/512 + PREP_BOOT_SIZE/512);
    571  1.1  dholland 	part[1].mbrp_start = bsec + FAT12_BOOT_SIZE/512 + BINFO_BOOT_SIZE/512 +
    572  1.1  dholland 	    PREP_BOOT_SIZE/512;
    573  1.1  dholland 	part[1].mbrp_flag = MBR_PFLAG_ACTIVE;
    574  1.1  dholland 
    575  1.1  dholland 	part[2].mbrp_type = MBR_PTYPE_PREP;
    576  1.1  dholland 	part[2].mbrp_size = BINFO_BOOT_SIZE/512;
    577  1.1  dholland 	part[2].mbrp_start = bsec + FAT12_BOOT_SIZE/512;
    578  1.1  dholland 	part[2].mbrp_flag = 0;
    579  1.1  dholland 
    580  1.1  dholland 	part[3].mbrp_type = MBR_PTYPE_PREP;
    581  1.1  dholland 	part[3].mbrp_size = PREP_BOOT_SIZE/512;
    582  1.1  dholland 	part[3].mbrp_start = bsec + FAT12_BOOT_SIZE/512 + BINFO_BOOT_SIZE/512;
    583  1.1  dholland 	part[3].mbrp_flag = 0;
    584  1.1  dholland 
    585  1.2    martin 	pm->ptstart = part[1].mbrp_start;
    586  1.2    martin 	pm->ptsize = part[1].mbrp_size;
    587  1.2    martin 	pm->bootstart = part[0].mbrp_start;
    588  1.2    martin 	pm->bootsize = part[0].mbrp_size;
    589  1.1  dholland 	binfostart = part[2].mbrp_start;
    590  1.1  dholland 	binfosize= part[2].mbrp_size;
    591  1.1  dholland 	bprepstart = part[3].mbrp_start;
    592  1.1  dholland 	bprepsize = part[3].mbrp_size;
    593  1.1  dholland 	bootinfo_mbr = 4;
    594  1.1  dholland 
    595  1.1  dholland 	return 1;
    596  1.1  dholland }
    597  1.1  dholland 
    598  1.1  dholland const char *md_disklabel_cmd(void)
    599  1.1  dholland {
    600  1.1  dholland 
    601  1.1  dholland 	/* we cannot rewrite an RDB disklabel */
    602  1.1  dholland 	if (rdb_found)
    603  1.1  dholland 		return "sync No disklabel";
    604  1.1  dholland 
    605  1.1  dholland 	return "disklabel -w -r";
    606  1.1  dholland }
    607  1.1  dholland 
    608  1.1  dholland static int
    609  1.1  dholland check_rdb(void)
    610  1.1  dholland {
    611  1.1  dholland 	char buf[512], diskpath[MAXPATHLEN];
    612  1.1  dholland 	struct rdblock *rdb;
    613  1.1  dholland 	off_t blk;
    614  1.1  dholland 	int fd;
    615  1.1  dholland 
    616  1.1  dholland 	/* Find out if this disk has a valid RDB, before continuing. */
    617  1.1  dholland 	rdb = (struct rdblock *)buf;
    618  1.2    martin 	fd = opendisk(pm->diskdev, O_RDONLY, diskpath, sizeof(diskpath), 0);
    619  1.1  dholland 	if (fd < 0)
    620  1.1  dholland 		return 0;
    621  1.1  dholland 	for (blk = 0; blk < RDB_MAXBLOCKS; blk++) {
    622  1.1  dholland 		if (pread(fd, rdb, 512, blk * 512) != 512)
    623  1.1  dholland 			return 0;
    624  1.1  dholland 		if (rdb->id == RDBLOCK_ID && rdbchksum(rdb) == 0) {
    625  1.1  dholland 			rdb_found = 1;	/* do not repartition! */
    626  1.1  dholland 			return 1;
    627  1.1  dholland 		}
    628  1.1  dholland 	}
    629  1.1  dholland 	return 0;
    630  1.1  dholland }
    631  1.1  dholland 
    632  1.1  dholland static uint32_t
    633  1.1  dholland rdbchksum(void *bdata)
    634  1.1  dholland {
    635  1.1  dholland 	uint32_t *blp, cnt, val;
    636  1.1  dholland 
    637  1.1  dholland 	blp = bdata;
    638  1.1  dholland 	cnt = blp[1];
    639  1.1  dholland 	val = 0;
    640  1.1  dholland 	while (cnt--)
    641  1.1  dholland 		val += *blp++;
    642  1.1  dholland 	return val;
    643  1.1  dholland }
    644  1.1  dholland 
    645  1.1  dholland int
    646  1.1  dholland md_pre_mount()
    647  1.1  dholland {
    648  1.1  dholland 
    649  1.1  dholland 	return 0;
    650  1.1  dholland }
    651