Home | History | Annotate | Line # | Download | only in prep
md.c revision 1.3
      1 /*	$NetBSD: md.c,v 1.3 2015/05/10 10:14:03 martin Exp $	*/
      2 
      3 /*
      4  * Copyright 1997 Piermont Information Systems Inc.
      5  * All rights reserved.
      6  *
      7  * Based on code written by Philip A. Nelson for Piermont Information
      8  * Systems Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     19  *    or promote products derived from this software without specific prior
     20  *    written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /* md.c -- prep machine specific routines */
     36 
     37 #include <sys/param.h>
     38 #include <sys/sysctl.h>
     39 #include <stdio.h>
     40 #include <util.h>
     41 #include <machine/cpu.h>
     42 
     43 #include "defs.h"
     44 #include "md.h"
     45 #include "msg_defs.h"
     46 #include "menu_defs.h"
     47 #include "endian.h"
     48 
     49 int prep_nobootfix = 0, prep_rawdevfix = 0, prep_bootpart = PART_BOOT;
     50 
     51 void
     52 md_init(void)
     53 {
     54 }
     55 
     56 void
     57 md_init_set_status(int flags)
     58 {
     59 	(void)flags;
     60 }
     61 
     62 int
     63 md_get_info(void)
     64 {
     65 	return set_bios_geom_with_mbr_guess();
     66 }
     67 
     68 /*
     69  * md back-end code for menu-driven BSD disklabel editor.
     70  */
     71 int
     72 md_make_bsd_partitions(void)
     73 {
     74 	return make_bsd_partitions();
     75 }
     76 
     77 /*
     78  * any additional partition validation
     79  */
     80 int
     81 md_check_partitions(void)
     82 {
     83 	int part;
     84 
     85 	/* we need to find a boot partition, otherwise we can't write our
     86 	 * "bootblock".  We make the assumption that the user hasn't done
     87 	 * something stupid, like move it away from the MBR partition.
     88 	 */
     89 	for (part = PART_A; part < MAXPARTITIONS; part++)
     90 		if (pm->bsdlabel[part].pi_fstype == FS_BOOT) {
     91 			prep_bootpart = part;
     92 			return 1;
     93 		}
     94 
     95 	msg_display(MSG_prepnobootpart);
     96 	process_menu(MENU_ok, NULL);
     97 	return 0;
     98 }
     99 
    100 /*
    101  * hook called before writing new disklabel.
    102  */
    103 int
    104 md_pre_disklabel(void)
    105 {
    106 	msg_display(MSG_dofdisk);
    107 
    108 	/* write edited MBR onto disk. */
    109 	if (write_mbr(pm->diskdev, &mbr, 1) != 0) {
    110 		msg_display(MSG_wmbrfail);
    111 		process_menu(MENU_ok, NULL);
    112 		return 1;
    113 	}
    114 	return 0;
    115 }
    116 
    117 /*
    118  * hook called after writing disklabel to new target disk.
    119  */
    120 int
    121 md_post_disklabel(void)
    122 {
    123 	return 0;
    124 }
    125 
    126 /*
    127  * hook called after upgrade() or install() has finished setting
    128  * up the target disk but immediately before the user is given the
    129  * ``disks are now set up'' message.
    130  */
    131 int
    132 md_post_newfs(void)
    133 {
    134 	return 0;
    135 }
    136 
    137 int
    138 md_post_extract(void)
    139 {
    140 	char rawdev[100], bootpart[100], bootloader[100];
    141 
    142 	/* if we can't make it bootable, just punt */
    143 	if (prep_nobootfix)
    144 		return 0;
    145 
    146 	process_menu(MENU_prepconsole, NULL);
    147 	if (yesno == 1)
    148 		snprintf(bootloader, 100, "/usr/mdec/boot_com0");
    149 	else
    150 		snprintf(bootloader, 100, "/usr/mdec/boot");
    151 
    152 	snprintf(rawdev, 100, "/dev/r%s%c", pm->diskdev, 'a' + getrawpartition());
    153 	snprintf(bootpart, 100, "/dev/r%s%c", pm->diskdev, 'a' + prep_bootpart);
    154 	if (prep_rawdevfix)
    155 		run_program(RUN_DISPLAY|RUN_CHROOT,
    156 		    "/usr/mdec/mkbootimage -b %s -k /netbsd "
    157 		    "-r %s /.bootimage", bootloader, rawdev);
    158 	else
    159 		run_program(RUN_DISPLAY|RUN_CHROOT,
    160 		    "/usr/mdec/mkbootimage -s -b %s -k /netbsd /.bootimage",
    161 		    bootloader);
    162 	run_program(RUN_DISPLAY|RUN_CHROOT, "/bin/dd if=/.bootimage of=%s "
    163 	    "bs=512 conv=sync", bootpart);
    164 
    165 	return 0;
    166 }
    167 
    168 void
    169 md_cleanup_install(void)
    170 {
    171 #ifndef DEBUG
    172 	enable_rc_conf();
    173 #endif
    174 	run_program(0, "rm -f %s", target_expand("/.bootimage"));
    175 }
    176 
    177 int
    178 md_pre_update(void)
    179 {
    180 	struct mbr_partition *part;
    181 	mbr_info_t *ext;
    182 	int i;
    183 
    184 	read_mbr(pm->diskdev, &mbr);
    185 	/* do a sanity check of the partition table */
    186 	for (ext = &mbr; ext; ext = ext->extended) {
    187 		part = ext->mbr.mbr_parts;
    188 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
    189 			if (part->mbrp_type != MBR_PTYPE_PREP)
    190 				continue;
    191 			if (part->mbrp_size < (MIN_PREP_BOOT/512)) {
    192 				msg_display(MSG_preptoosmall);
    193 				msg_display_add(MSG_prepnobootpart, 0);
    194 				if (!ask_yesno(NULL))
    195 					return 0;
    196 				prep_nobootfix = 1;
    197 			}
    198 			if (part->mbrp_start == 0)
    199 				prep_rawdevfix = 1;
    200 		}
    201 	}
    202 	if (md_check_partitions() == 0)
    203 		prep_nobootfix = 1;
    204 	return 1;
    205 }
    206 
    207 /* Upgrade support */
    208 int
    209 md_update(void)
    210 {
    211 	md_post_newfs();
    212 	return 1;
    213 }
    214 
    215 int
    216 md_check_mbr(mbr_info_t *mbri)
    217 {
    218 	mbr_info_t *ext;
    219 	struct mbr_partition *part;
    220 	int i;
    221 
    222 	for (ext = mbri; ext; ext = ext->extended) {
    223 		part = ext->mbr.mbr_parts;
    224 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
    225 			if (part->mbrp_type != MBR_PTYPE_PREP)
    226 				continue;
    227 			pm->bootstart = part->mbrp_start;
    228 			pm->bootsize = part->mbrp_size;
    229 			break;
    230 		}
    231 	}
    232 	if (pm->bootsize < (MIN_PREP_BOOT/512)) {
    233 		msg_display(MSG_preptoosmall);
    234 		msg_display_add(MSG_reeditpart, 0);
    235 		if (!ask_yesno(NULL))
    236 			return 0;
    237 		return 1;
    238 	}
    239 	if (pm->bootstart == 0 || pm->bootsize == 0) {
    240 		msg_display(MSG_nopreppart);
    241 		msg_display_add(MSG_reeditpart, 0);
    242 		if (!ask_yesno(NULL))
    243 			return 0;
    244 		return 1;
    245 	}
    246 	return 2;
    247 }
    248 
    249 int
    250 md_mbr_use_wholedisk(mbr_info_t *mbri)
    251 {
    252 	struct mbr_sector *mbrs = &mbri->mbr;
    253 	mbr_info_t *ext;
    254 	struct mbr_partition *part;
    255 
    256 	part = &mbrs->mbr_parts[0];
    257 	/* Set the partition information for full disk usage. */
    258 	while ((ext = mbri->extended)) {
    259 		mbri->extended = ext->extended;
    260 		free(ext);
    261 	}
    262 	memset(part, 0, MBR_PART_COUNT * sizeof *part);
    263 #ifdef BOOTSEL
    264 	memset(&mbri->mbrb, 0, sizeof mbri->mbrb);
    265 #endif
    266 	part[0].mbrp_type = MBR_PTYPE_PREP;
    267 	part[0].mbrp_size = PREP_BOOT_SIZE/512;
    268 	part[0].mbrp_start = bsec;
    269 	part[0].mbrp_flag = MBR_PFLAG_ACTIVE;
    270 
    271 	part[1].mbrp_type = MBR_PTYPE_NETBSD;
    272 	part[1].mbrp_size = pm->dlsize - (bsec + PREP_BOOT_SIZE/512);
    273 	part[1].mbrp_start = bsec + PREP_BOOT_SIZE/512;
    274 	part[1].mbrp_flag = 0;
    275 
    276 	pm->ptstart = part[1].mbrp_start;
    277 	pm->ptsize = part[1].mbrp_size;
    278 	pm->bootstart = part[0].mbrp_start;
    279 	pm->bootsize = part[0].mbrp_size;
    280 	return 1;
    281 }
    282 
    283 
    284 int
    285 md_pre_mount()
    286 {
    287 	return 0;
    288 }
    289