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