Home | History | Annotate | Line # | Download | only in prep
md.c revision 1.5
      1 /*	$NetBSD: md.c,v 1.5 2019/06/12 06:20:22 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;
     50 size_t prep_bootpart = ~0U;
     51 
     52 void
     53 md_init(void)
     54 {
     55 }
     56 
     57 void
     58 md_init_set_status(int flags)
     59 {
     60 	(void)flags;
     61 }
     62 
     63 bool
     64 md_get_info(struct install_partition_desc *install)
     65 {
     66 	return set_bios_geom_with_mbr_guess(pm->parts);
     67 }
     68 
     69 /*
     70  * md back-end code for menu-driven BSD disklabel editor.
     71  */
     72 bool
     73 md_make_bsd_partitions(struct install_partition_desc *install)
     74 {
     75 	return make_bsd_partitions(install);
     76 }
     77 
     78 /*
     79  * any additional partition validation
     80  */
     81 bool
     82 md_check_partitions(struct install_partition_desc *install)
     83 {
     84 	size_t part;
     85 
     86 	/* we need to find a boot partition, otherwise we can't write our
     87 	 * "bootblock".  We make the assumption that the user hasn't done
     88 	 * something stupid, like move it away from the MBR partition.
     89 	 */
     90 	for (part = 0; part < install->num; part++)
     91 		if (install->infos[part].fs_type == FS_BOOT) {
     92 			prep_bootpart = part;
     93 			return true;
     94 		}
     95 
     96 	msg_display(MSG_prepnobootpart);
     97 	process_menu(MENU_ok, NULL);
     98 	return false;
     99 }
    100 
    101 /*
    102  * hook called before writing new disklabel.
    103  */
    104 bool
    105 md_pre_disklabel(struct install_partition_desc *install,
    106     struct disk_partitions *parts)
    107 {
    108 
    109 	if (parts->parent == NULL)
    110 		return true;	/* no outer partitions */
    111 
    112 	parts = parts->parent;
    113 
    114 	msg_display_subst(MSG_dofdisk, 3, parts->disk,
    115 	    msg_string(parts->pscheme->name),
    116 	    msg_string(parts->pscheme->short_name));
    117 
    118 	/* write edited "MBR" onto disk. */
    119 	if (!parts->pscheme->write_to_disk(parts)) {
    120 		msg_display(MSG_wmbrfail);
    121 		process_menu(MENU_ok, NULL);
    122 		return false;
    123 	}
    124 	return true;
    125 }
    126 
    127 /*
    128  * hook called after writing disklabel to new target disk.
    129  */
    130 bool
    131 md_post_disklabel(struct install_partition_desc *install,
    132     struct disk_partitions *parts)
    133 {
    134 	return true;
    135 }
    136 
    137 /*
    138  * hook called after upgrade() or install() has finished setting
    139  * up the target disk but immediately before the user is given the
    140  * ``disks are now set up'' message.
    141  */
    142 int
    143 md_post_newfs(struct install_partition_desc *install)
    144 {
    145 	return 0;
    146 }
    147 
    148 int
    149 md_post_extract(struct install_partition_desc *install)
    150 {
    151 	char rawdev[100], bootpart[100], bootloader[100];
    152 	int contype;
    153 
    154 	/* if we can't make it bootable, just punt */
    155 	if (prep_nobootfix)
    156 		return 0;
    157 
    158 	process_menu(MENU_prepconsole, &contype);
    159 	if (contype == 1)
    160 		snprintf(bootloader, 100, "/usr/mdec/boot_com0");
    161 	else
    162 		snprintf(bootloader, 100, "/usr/mdec/boot");
    163 
    164 	snprintf(rawdev, 100, "/dev/r%s%c", pm->diskdev, 'a' + getrawpartition());
    165 	snprintf(bootpart, 100, "/dev/r%s%c", pm->diskdev, 'a' + prep_bootpart);
    166 	if (prep_rawdevfix)
    167 		run_program(RUN_DISPLAY|RUN_CHROOT,
    168 		    "/usr/mdec/mkbootimage -b %s -k /netbsd "
    169 		    "-r %s /.bootimage", bootloader, rawdev);
    170 	else
    171 		run_program(RUN_DISPLAY|RUN_CHROOT,
    172 		    "/usr/mdec/mkbootimage -s -b %s -k /netbsd /.bootimage",
    173 		    bootloader);
    174 	run_program(RUN_DISPLAY|RUN_CHROOT, "/bin/dd if=/.bootimage of=%s "
    175 	    "bs=512 conv=sync", bootpart);
    176 
    177 	return 0;
    178 }
    179 
    180 void
    181 md_cleanup_install(struct install_partition_desc *install)
    182 {
    183 #ifndef DEBUG
    184 	enable_rc_conf();
    185 #endif
    186 	run_program(0, "rm -f %s", target_expand("/.bootimage"));
    187 }
    188 
    189 int
    190 md_pre_update(struct install_partition_desc *install)
    191 {
    192 	size_t i;
    193 
    194 	/* do a sanity check of the partition table */
    195 	for (i = 0; i < install->num; i++) {
    196 		if (install->infos[i].fs_type != PART_BOOT_TYPE)
    197 			continue;
    198 		if (install->infos[i].size < (MIN_PREP_BOOT/512)) {
    199 			msg_display(MSG_preptoosmall);
    200 			msg_display_add(MSG_prepnobootpart, 0);
    201 			if (!ask_yesno(NULL))
    202 				return 0;
    203 			prep_nobootfix = 1;
    204 		}
    205 		if (install->infos[i].cur_start == 0)
    206 			prep_rawdevfix = 1;
    207 	}
    208 	if (!md_check_partitions(install))
    209 		prep_nobootfix = 1;
    210 	return 1;
    211 }
    212 
    213 /* Upgrade support */
    214 int
    215 md_update(struct install_partition_desc *install)
    216 {
    217 	md_post_newfs(install);
    218 	return 1;
    219 }
    220 
    221 int
    222 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
    223 {
    224 	mbr_info_t *ext;
    225 	struct mbr_partition *part;
    226 	int i;
    227 
    228 	for (ext = mbri; ext; ext = ext->extended) {
    229 		part = ext->mbr.mbr_parts;
    230 		for (i = 0; i < MBR_PART_COUNT; part++, i++) {
    231 			if (part->mbrp_type != MBR_PTYPE_PREP)
    232 				continue;
    233 			pm->bootstart = part->mbrp_start;
    234 			pm->bootsize = part->mbrp_size;
    235 			break;
    236 		}
    237 	}
    238 	if (pm->bootsize < (MIN_PREP_BOOT/512)) {
    239 		msg_display(MSG_preptoosmall);
    240 		return ask_reedit(parts);
    241 	}
    242 	if (pm->bootstart == 0 || pm->bootsize == 0) {
    243 		if (quiet)
    244 			return 0;
    245 		msg_display(MSG_nopreppart);
    246 		return ask_reedit(parts);
    247 	}
    248 	return 2;
    249 }
    250 
    251 bool
    252 md_parts_use_wholedisk(struct disk_partitions *parts)
    253 {
    254 	struct disk_part_info boot_part = {
    255 		.size = PART_BOOT / 512,
    256 		.fs_type = PART_BOOT_TYPE,
    257 	};
    258 
    259 	boot_part.nat_type = parts->pscheme->get_fs_part_type(
    260 	    boot_part.fs_type, boot_part.fs_sub_type);
    261 
    262 	return parts_use_wholedisk(parts, 1, &boot_part);
    263 }
    264 
    265 int
    266 md_pre_mount(struct install_partition_desc *install)
    267 {
    268 	return 0;
    269 }
    270 
    271 bool
    272 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
    273 {
    274 	return false;	/* no change, no need to write back */
    275 }
    276 
    277 #ifdef HAVE_GPT
    278 bool
    279 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
    280     bool root_is_new, part_id efi_id, bool efi_is_new)
    281 {
    282 	/* no GPT boot support, nothing needs to be done here */
    283 	return true;
    284 }
    285 #endif
    286 
    287