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