Home | History | Annotate | Line # | Download | only in cobalt
md.c revision 1.2.4.2
      1  1.2.4.2       snj /*	$NetBSD: md.c,v 1.2.4.2 2015/05/14 07:58:49 snj Exp $ */
      2      1.1  dholland 
      3      1.1  dholland /*
      4      1.1  dholland  * Copyright 1997 Piermont Information Systems Inc.
      5      1.1  dholland  * All rights reserved.
      6      1.1  dholland  *
      7      1.1  dholland  * Based on code written by Philip A. Nelson for Piermont Information
      8      1.1  dholland  * Systems Inc.
      9      1.1  dholland  *
     10      1.1  dholland  * Redistribution and use in source and binary forms, with or without
     11      1.1  dholland  * modification, are permitted provided that the following conditions
     12      1.1  dholland  * are met:
     13      1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     14      1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     15      1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  dholland  *    documentation and/or other materials provided with the distribution.
     18      1.1  dholland  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     19      1.1  dholland  *    or promote products derived from this software without specific prior
     20      1.1  dholland  *    written permission.
     21      1.1  dholland  *
     22      1.1  dholland  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     23      1.1  dholland  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24      1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25      1.1  dholland  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     26      1.1  dholland  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27      1.1  dholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28      1.1  dholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29      1.1  dholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30      1.1  dholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31      1.1  dholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32      1.1  dholland  * THE POSSIBILITY OF SUCH DAMAGE.
     33      1.1  dholland  */
     34      1.1  dholland 
     35      1.1  dholland /* md.c -- cobalt 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 <stdio.h>
     40      1.1  dholland #include <util.h>
     41      1.1  dholland #include <machine/cpu.h>
     42      1.1  dholland 
     43      1.1  dholland #include "defs.h"
     44      1.1  dholland #include "md.h"
     45      1.1  dholland #include "msg_defs.h"
     46      1.1  dholland #include "menu_defs.h"
     47      1.1  dholland 
     48      1.1  dholland /*
     49      1.1  dholland  * Firmware reognizes only Linux Ext2 REV 0, so we have to have
     50      1.1  dholland  * a Linux Ext2 fs to store our native bootloader.
     51      1.1  dholland  */
     52      1.1  dholland static int nobootfs = 0;
     53      1.1  dholland static int bootpart_ext2fs = PART_BOOT_EXT2FS;
     54      1.1  dholland 
     55      1.1  dholland void
     56      1.1  dholland md_init(void)
     57      1.1  dholland {
     58      1.1  dholland }
     59      1.1  dholland 
     60      1.1  dholland void
     61      1.1  dholland md_init_set_status(int flags)
     62      1.1  dholland {
     63      1.1  dholland 	(void)flags;
     64      1.1  dholland }
     65      1.1  dholland 
     66      1.1  dholland int
     67      1.1  dholland md_get_info(void)
     68      1.1  dholland {
     69      1.1  dholland 	return set_bios_geom_with_mbr_guess();
     70      1.1  dholland }
     71      1.1  dholland 
     72      1.1  dholland /*
     73      1.1  dholland  * md back-end code for menu-driven BSD disklabel editor.
     74      1.1  dholland  */
     75      1.1  dholland int
     76      1.1  dholland md_make_bsd_partitions(void)
     77      1.1  dholland {
     78      1.1  dholland 	int i;
     79      1.1  dholland 	int part;
     80      1.1  dholland 	int maxpart = getmaxpartitions();
     81      1.1  dholland 	int partstart;
     82      1.1  dholland 	int part_raw, part_bsd;
     83      1.1  dholland 	int ptend;
     84      1.1  dholland 	int no_swap = 0;
     85      1.1  dholland 	partinfo *p;
     86      1.1  dholland 
     87      1.1  dholland 	/*
     88      1.1  dholland 	 * Initialize global variables that track space used on this disk.
     89      1.1  dholland 	 * Standard 4.4BSD 8-partition labels always cover whole disk.
     90      1.1  dholland 	 */
     91      1.2    martin 	if (pm->ptsize == 0)
     92      1.2    martin 		pm->ptsize = pm->dlsize - pm->ptstart;
     93      1.2    martin 	if (pm->dlsize == 0)
     94      1.2    martin 		pm->dlsize = pm->ptstart + pm->ptsize;
     95      1.1  dholland 
     96      1.2    martin 	partstart = pm->ptstart;
     97      1.2    martin 	ptend = pm->ptstart + pm->ptsize;
     98      1.1  dholland 
     99      1.1  dholland 	/* Ask for layout type -- standard or special */
    100      1.1  dholland 	msg_display(MSG_layout,
    101      1.2    martin 		    pm->ptsize / (MEG / pm->sectorsize),
    102      1.1  dholland 		    DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE,
    103      1.1  dholland 		    DEFROOTSIZE + DEFSWAPSIZE + DEFUSRSIZE + XNEEDMB);
    104      1.1  dholland 
    105      1.1  dholland 	process_menu(MENU_layout, NULL);
    106      1.1  dholland 
    107      1.1  dholland 	/* Set so we use the 'real' geometry for rounding, input in MB */
    108      1.2    martin 	pm->current_cylsize = pm->dlcylsize;
    109      1.1  dholland 	set_sizemultname_meg();
    110      1.1  dholland 
    111      1.1  dholland 	/* Build standard partitions */
    112      1.2    martin 	memset(&pm->bsdlabel, 0, sizeof pm->bsdlabel);
    113      1.1  dholland 
    114      1.1  dholland 	/* Set initial partition types to unused */
    115      1.1  dholland 	for (part = 0 ; part < maxpart ; ++part)
    116      1.2    martin 		pm->bsdlabel[part].pi_fstype = FS_UNUSED;
    117      1.1  dholland 
    118      1.1  dholland 	/* Whole disk partition */
    119      1.1  dholland 	part_raw = getrawpartition();
    120      1.1  dholland 	if (part_raw == -1)
    121      1.1  dholland 		part_raw = PART_C;	/* for sanity... */
    122      1.2    martin 	pm->bsdlabel[part_raw].pi_offset = 0;
    123      1.2    martin 	pm->bsdlabel[part_raw].pi_size = pm->dlsize;
    124      1.1  dholland 
    125      1.1  dholland 	if (part_raw == PART_D) {
    126      1.1  dholland 		/* Probably a system that expects an i386 style mbr */
    127      1.1  dholland 		part_bsd = PART_C;
    128      1.2    martin 		pm->bsdlabel[PART_C].pi_offset = pm->ptstart;
    129      1.2    martin 		pm->bsdlabel[PART_C].pi_size = pm->ptsize;
    130      1.1  dholland 	} else {
    131      1.1  dholland 		part_bsd = part_raw;
    132      1.1  dholland 	}
    133      1.1  dholland 
    134      1.2    martin 	if (pm->bootsize != 0) {
    135      1.2    martin 		pm->bsdlabel[PART_BOOT_EXT2FS].pi_fstype = FS_EX2FS;
    136      1.2    martin 		pm->bsdlabel[PART_BOOT_EXT2FS].pi_size = pm->bootsize;
    137      1.2    martin 		pm->bsdlabel[PART_BOOT_EXT2FS].pi_offset = pm->bootstart;
    138      1.2    martin 		pm->bsdlabel[PART_BOOT_EXT2FS].pi_flags |=
    139      1.1  dholland 		    PART_BOOT_EXT2FS_PI_FLAGS;
    140      1.2    martin 		strlcpy(pm->bsdlabel[PART_BOOT_EXT2FS].pi_mount,
    141      1.1  dholland 		    PART_BOOT_EXT2FS_PI_MOUNT,
    142      1.2    martin 		    sizeof pm->bsdlabel[PART_BOOT_EXT2FS].pi_mount);
    143      1.1  dholland 	}
    144      1.1  dholland 
    145      1.1  dholland #ifdef PART_REST
    146      1.2    martin 	pm->bsdlabel[PART_REST].pi_offset = 0;
    147      1.2    martin 	pm->bsdlabel[PART_REST].pi_size = pm->ptstart;
    148      1.1  dholland #endif
    149      1.1  dholland 
    150      1.1  dholland 	/*
    151      1.1  dholland 	 * Save any partitions that are outside the area we are
    152      1.1  dholland 	 * going to use.
    153      1.1  dholland 	 * In particular this saves details of the other MBR
    154      1.1  dholland 	 * partitions on a multiboot i386 system.
    155      1.1  dholland 	 */
    156      1.1  dholland 	 for (i = maxpart; i--;) {
    157      1.2    martin 		if (pm->bsdlabel[i].pi_size != 0)
    158      1.1  dholland 			/* Don't overwrite special partitions */
    159      1.1  dholland 			continue;
    160      1.2    martin 		p = &pm->oldlabel[i];
    161      1.1  dholland 		if (p->pi_fstype == FS_UNUSED || p->pi_size == 0)
    162      1.1  dholland 			continue;
    163      1.2    martin 		if (layoutkind == LY_USEEXIST) {
    164      1.1  dholland 			if (PI_ISBSDFS(p))
    165      1.1  dholland 				p->pi_flags |= PIF_MOUNT;
    166      1.1  dholland 		} else {
    167      1.2    martin 			if (p->pi_offset < pm->ptstart + pm->ptsize &&
    168      1.2    martin 			    p->pi_offset + p->pi_size > pm->ptstart)
    169      1.1  dholland 				/* Not outside area we are allocating */
    170      1.1  dholland 				continue;
    171      1.1  dholland 			if (p->pi_fstype == FS_SWAP)
    172      1.1  dholland 				no_swap = 1;
    173      1.1  dholland 		}
    174      1.2    martin 		pm->bsdlabel[i] = pm->oldlabel[i];
    175      1.1  dholland 	 }
    176      1.1  dholland 
    177      1.2    martin 	if (layoutkind == LY_USEEXIST) {
    178      1.1  dholland 		/* XXX Check we have a sensible layout */
    179      1.1  dholland 		;
    180      1.1  dholland 	} else
    181      1.1  dholland 		get_ptn_sizes(partstart, ptend - partstart, no_swap);
    182      1.1  dholland 
    183      1.1  dholland 	/*
    184      1.1  dholland 	 * OK, we have a partition table. Give the user the chance to
    185      1.1  dholland 	 * edit it and verify it's OK, or abort altogether.
    186      1.1  dholland 	 */
    187      1.1  dholland  edit_check:
    188      1.2    martin 	if (edit_and_check_label(pm->bsdlabel, maxpart, part_raw, part_bsd) == 0) {
    189      1.1  dholland 		msg_display(MSG_abort);
    190      1.1  dholland 		return 0;
    191      1.1  dholland 	}
    192      1.1  dholland 	if (md_check_partitions() == 0)
    193      1.1  dholland 		goto edit_check;
    194      1.1  dholland 
    195      1.1  dholland 	/* Disk name */
    196      1.2    martin 	msg_prompt(MSG_packname, pm->bsddiskname, pm->bsddiskname, sizeof pm->bsddiskname);
    197      1.1  dholland 
    198      1.1  dholland 	/* save label to disk for MI code to update. */
    199      1.2    martin 	(void)savenewlabel(pm->bsdlabel, maxpart);
    200      1.1  dholland 
    201      1.1  dholland 	/* Everything looks OK. */
    202      1.1  dholland 	return 1;
    203      1.1  dholland }
    204      1.1  dholland 
    205      1.1  dholland /*
    206      1.1  dholland  * any additional partition validation
    207      1.1  dholland  */
    208      1.1  dholland int
    209      1.1  dholland md_check_partitions(void)
    210      1.1  dholland {
    211      1.1  dholland 	int part;
    212      1.1  dholland 
    213      1.1  dholland 	/* we need to find a boot partition, otherwise we can't write our
    214      1.1  dholland 	 * bootloader.  We make the assumption that the user hasn't done
    215      1.1  dholland 	 * something stupid, like move it away from the MBR partition.
    216      1.1  dholland 	 */
    217      1.1  dholland 	for (part = PART_A; part < MAXPARTITIONS; part++)
    218      1.2    martin 		if (pm->bsdlabel[part].pi_fstype == FS_EX2FS) {
    219      1.1  dholland 			bootpart_ext2fs = part;
    220      1.1  dholland 			return 1;
    221      1.1  dholland 		}
    222      1.1  dholland 
    223      1.1  dholland 	msg_display(MSG_nobootpartdisklabel);
    224      1.1  dholland 	process_menu(MENU_ok, NULL);
    225      1.1  dholland 	return 0;
    226      1.1  dholland }
    227      1.1  dholland 
    228      1.1  dholland /*
    229      1.1  dholland  * hook called before writing new disklabel.
    230      1.1  dholland  */
    231      1.1  dholland int
    232      1.1  dholland md_pre_disklabel(void)
    233      1.1  dholland {
    234      1.1  dholland 	msg_display(MSG_dofdisk);
    235      1.1  dholland 
    236      1.1  dholland 	/* write edited MBR onto disk. */
    237      1.2    martin 	if (write_mbr(pm->diskdev, &mbr, 1) != 0) {
    238      1.1  dholland 		msg_display(MSG_wmbrfail);
    239      1.1  dholland 		process_menu(MENU_ok, NULL);
    240      1.1  dholland 		return 1;
    241      1.1  dholland 	}
    242      1.1  dholland 	return 0;
    243      1.1  dholland }
    244      1.1  dholland 
    245      1.1  dholland /*
    246      1.1  dholland  * hook called after writing disklabel to new target disk.
    247      1.1  dholland  */
    248      1.1  dholland int
    249      1.1  dholland md_post_disklabel(void)
    250      1.1  dholland {
    251      1.1  dholland 	return 0;
    252      1.1  dholland }
    253      1.1  dholland 
    254      1.1  dholland /*
    255      1.1  dholland  * hook called after upgrade() or install() has finished setting
    256      1.1  dholland  * up the target disk but immediately before the user is given the
    257      1.1  dholland  * ``disks are now set up'' message.
    258      1.1  dholland  */
    259      1.1  dholland int
    260      1.1  dholland md_post_newfs(void)
    261      1.1  dholland {
    262      1.1  dholland 	static const char *kernels[] = {
    263      1.1  dholland 		"vmlinux-nfsroot.gz",
    264      1.1  dholland 		"vmlinux.gz",
    265      1.1  dholland 		"vmlinux_RAQ.gz",
    266      1.1  dholland 		"vmlinux_raq-2800.gz"
    267      1.1  dholland 	};
    268      1.1  dholland 	static const char *bootfile = "boot.gz";
    269      1.1  dholland 	char bootdir[64];
    270      1.1  dholland 	unsigned int i;
    271      1.1  dholland 
    272      1.1  dholland 	if (!nobootfs) {
    273      1.2    martin 		msg_display(msg_string(MSG_copybootloader), pm->diskdev);
    274      1.1  dholland 
    275      1.1  dholland 		snprintf(bootdir, sizeof(bootdir), "%s/boot",
    276      1.1  dholland 		    target_expand(PART_BOOT_EXT2FS_PI_MOUNT));
    277      1.1  dholland 		run_program(0, "/bin/mkdir -p %s", bootdir);
    278      1.1  dholland 		run_program(0, "/bin/cp /usr/mdec/boot %s", bootdir);
    279      1.1  dholland 		run_program(0, "/bin/rm -f %s/%s", bootdir, bootfile);
    280      1.1  dholland 		run_program(0, "/usr/bin/gzip -9 %s/boot", bootdir);
    281      1.1  dholland 		for (i = 0; i < __arraycount(kernels); i++)
    282      1.1  dholland 			run_program(0, "/bin/ln -fs %s %s/%s",
    283      1.1  dholland 			    bootfile, bootdir, kernels[i]);
    284      1.1  dholland 	}
    285      1.1  dholland 
    286      1.1  dholland 	return 0;
    287      1.1  dholland }
    288      1.1  dholland 
    289      1.1  dholland int
    290      1.1  dholland md_post_extract(void)
    291      1.1  dholland {
    292      1.1  dholland 	return 0;
    293      1.1  dholland }
    294      1.1  dholland 
    295      1.1  dholland void
    296      1.1  dholland md_cleanup_install(void)
    297      1.1  dholland {
    298      1.1  dholland #ifndef DEBUG
    299      1.1  dholland 	enable_rc_conf();
    300      1.1  dholland #endif
    301      1.1  dholland }
    302      1.1  dholland 
    303      1.1  dholland int
    304      1.1  dholland md_pre_update(void)
    305      1.1  dholland {
    306      1.1  dholland 	struct mbr_partition *part;
    307      1.1  dholland 	mbr_info_t *ext;
    308      1.1  dholland 	int i;
    309      1.1  dholland 
    310      1.2    martin 	read_mbr(pm->diskdev, &mbr);
    311      1.1  dholland 	/* do a sanity check of the partition table */
    312      1.1  dholland 	for (ext = &mbr; ext; ext = ext->extended) {
    313      1.1  dholland 		part = ext->mbr.mbr_parts;
    314      1.1  dholland 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
    315      1.1  dholland 			if (part->mbrp_type != MBR_PTYPE_LNXEXT2)
    316      1.1  dholland 				continue;
    317      1.1  dholland 			if (part->mbrp_size < (MIN_EXT2FS_BOOT / 512)) {
    318      1.1  dholland 				msg_display(MSG_boottoosmall);
    319      1.1  dholland 				msg_display_add(MSG_nobootpart, 0);
    320  1.2.4.2       snj 				if (!ask_yesno(NULL))
    321      1.1  dholland 					return 0;
    322      1.1  dholland 				nobootfs = 1;
    323      1.1  dholland 			}
    324      1.1  dholland 		}
    325      1.1  dholland 	}
    326      1.1  dholland 	if (md_check_partitions() == 0)
    327      1.1  dholland 		nobootfs = 1;
    328      1.1  dholland 	return 1;
    329      1.1  dholland }
    330      1.1  dholland 
    331      1.1  dholland /* Upgrade support */
    332      1.1  dholland int
    333      1.1  dholland md_update(void)
    334      1.1  dholland {
    335      1.1  dholland 	md_post_newfs();
    336      1.1  dholland 	return 1;
    337      1.1  dholland }
    338      1.1  dholland 
    339      1.1  dholland int
    340      1.1  dholland md_check_mbr(mbr_info_t *mbri)
    341      1.1  dholland {
    342      1.1  dholland 	mbr_info_t *ext;
    343      1.1  dholland 	struct mbr_partition *part;
    344      1.1  dholland 	int i;
    345      1.1  dholland 
    346      1.1  dholland 	for (ext = mbri; ext; ext = ext->extended) {
    347      1.1  dholland 		part = ext->mbr.mbr_parts;
    348      1.1  dholland 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
    349      1.1  dholland 			if (part->mbrp_type == MBR_PTYPE_LNXEXT2) {
    350      1.2    martin 				pm->bootstart = part->mbrp_start;
    351      1.2    martin 				pm->bootsize = part->mbrp_size;
    352      1.1  dholland 				break;
    353      1.1  dholland 			}
    354      1.1  dholland 		}
    355      1.1  dholland 	}
    356      1.2    martin 	if (pm->bootsize < (MIN_EXT2FS_BOOT / 512)) {
    357      1.1  dholland 		msg_display(MSG_boottoosmall);
    358      1.1  dholland 		msg_display_add(MSG_reeditpart, 0);
    359  1.2.4.2       snj 		if (!ask_yesno(NULL))
    360      1.1  dholland 			return 0;
    361      1.1  dholland 		return 1;
    362      1.1  dholland 	}
    363      1.2    martin 	if (pm->bootstart == 0 || pm->bootsize == 0) {
    364      1.1  dholland 		msg_display(MSG_nobootpart);
    365      1.1  dholland 		msg_display_add(MSG_reeditpart, 0);
    366  1.2.4.2       snj 		if (!ask_yesno(NULL))
    367      1.1  dholland 			return 0;
    368      1.1  dholland 		return 1;
    369      1.1  dholland 	}
    370      1.1  dholland 	return 2;
    371      1.1  dholland }
    372      1.1  dholland 
    373      1.1  dholland int
    374      1.1  dholland md_mbr_use_wholedisk(mbr_info_t *mbri)
    375      1.1  dholland {
    376      1.1  dholland 	struct mbr_sector *mbrs = &mbri->mbr;
    377      1.1  dholland 	mbr_info_t *ext;
    378      1.1  dholland 	struct mbr_partition *part;
    379      1.1  dholland 
    380      1.1  dholland 	part = &mbrs->mbr_parts[0];
    381      1.1  dholland 	/* Set the partition information for full disk usage. */
    382      1.1  dholland 	while ((ext = mbri->extended)) {
    383      1.1  dholland 		mbri->extended = ext->extended;
    384      1.1  dholland 		free(ext);
    385      1.1  dholland 	}
    386      1.1  dholland 	memset(part, 0, MBR_PART_COUNT * sizeof *part);
    387      1.1  dholland #ifdef BOOTSEL
    388      1.1  dholland 	memset(&mbri->mbrb, 0, sizeof mbri->mbrb);
    389      1.1  dholland #endif
    390      1.1  dholland 	part[0].mbrp_type = MBR_PTYPE_LNXEXT2;
    391      1.1  dholland 	part[0].mbrp_size = EXT2FS_BOOT_SIZE / 512;
    392      1.1  dholland 	part[0].mbrp_start = bsec;
    393      1.1  dholland 	part[0].mbrp_flag = MBR_PFLAG_ACTIVE;
    394      1.1  dholland 
    395      1.1  dholland 	part[1].mbrp_type = MBR_PTYPE_NETBSD;
    396      1.2    martin 	part[1].mbrp_size = pm->dlsize - (bsec + EXT2FS_BOOT_SIZE / 512);
    397      1.1  dholland 	part[1].mbrp_start = bsec + EXT2FS_BOOT_SIZE / 512;
    398      1.1  dholland 	part[1].mbrp_flag = 0;
    399      1.1  dholland 
    400      1.2    martin 	pm->ptstart = part[1].mbrp_start;
    401      1.2    martin 	pm->ptsize = part[1].mbrp_size;
    402      1.2    martin 	pm->bootstart = part[0].mbrp_start;
    403      1.2    martin 	pm->bootsize = part[0].mbrp_size;
    404      1.1  dholland 	return 1;
    405      1.1  dholland }
    406      1.1  dholland 
    407      1.1  dholland int
    408      1.1  dholland md_pre_mount()
    409      1.1  dholland {
    410      1.1  dholland 	return 0;
    411      1.1  dholland }
    412