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