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