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