Home | History | Annotate | Line # | Download | only in hpcarm
md.c revision 1.2.28.1
      1 /*	$NetBSD: md.c,v 1.2.28.1 2020/04/13 08:06:02 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 -- hpcarm machine specific routines */
     36 
     37 #include <stdio.h>
     38 #include <util.h>
     39 #include <sys/param.h>
     40 #include <machine/cpu.h>
     41 #include <sys/sysctl.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 #include "mbr.h"
     49 
     50 void
     51 md_init(void)
     52 {
     53 }
     54 
     55 void
     56 md_init_set_status(int flags)
     57 {
     58 	static const struct {
     59 		const char *name;
     60 		const int set;
     61 	} kern_sets[] = {
     62 		{ "IPAQ",	SET_KERNEL_IPAQ },
     63 		{ "JORNADA720",	SET_KERNEL_JORNADA720 },
     64 		{ "WZERO3",	SET_KERNEL_WZERO3 }
     65 	};
     66 	static const int mib[2] = {CTL_KERN, KERN_VERSION};
     67 	size_t len;
     68 	char *version;
     69 	u_int i;
     70 
     71 	/* check INSTALL kernel name to select an appropriate kernel set */
     72 	/* XXX: hw.cpu_model has a processor name on arm ports */
     73 	sysctl(mib, 2, NULL, &len, NULL, 0);
     74 	version = malloc(len);
     75 	if (version == NULL)
     76 		return;
     77 	sysctl(mib, 2, version, &len, NULL, 0);
     78 	for (i = 0; i < __arraycount(kern_sets); i++) {
     79 		if (strstr(version, kern_sets[i].name) != NULL) {
     80 			set_kernel_set(kern_sets[i].set);
     81 			break;
     82 		}
     83 	}
     84 	free(version);
     85 }
     86 
     87 bool
     88 md_get_info(struct install_partition_desc *install)
     89 {
     90 
     91 	if (pm->no_mbr || pm->no_part)
     92 		return true;
     93 
     94 	if (pm->parts == NULL) {
     95 
     96 		const struct disk_partitioning_scheme *ps =
     97 		    select_part_scheme(pm, NULL, true, NULL);
     98 
     99 		if (!ps)
    100 			return false;
    101 
    102 		struct disk_partitions *parts =
    103 		   (*ps->create_new_for_disk)(pm->diskdev,
    104 		   0, pm->dlsize, true, NULL);
    105 		if (!parts)
    106 			return false;
    107 
    108 		pm->parts = parts;
    109 		if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
    110 			pm->dlsize = ps->size_limit;
    111 	}
    112 
    113 	return set_bios_geom_with_mbr_guess(pm->parts);
    114 }
    115 
    116 /*
    117  * md back-end code for menu-driven BSD disklabel editor.
    118  */
    119 bool
    120 md_make_bsd_partitions(struct install_partition_desc *install)
    121 {
    122 	return make_bsd_partitions(install);
    123 }
    124 
    125 /*
    126  * any additional partition validation
    127  */
    128 bool
    129 md_check_partitions(struct install_partition_desc *install)
    130 {
    131 	return true;
    132 }
    133 
    134 /*
    135  * hook called before writing new disklabel.
    136  */
    137 bool
    138 md_pre_disklabel(struct install_partition_desc *install,
    139     struct disk_partitions *parts)
    140 {
    141 
    142 	if (parts->parent == NULL)
    143 		return true;	/* no outer partitions */
    144 
    145 	parts = parts->parent;
    146 
    147 	msg_display_subst(MSG_dofdisk, 3, parts->disk,
    148 	    msg_string(parts->pscheme->name),
    149 	    msg_string(parts->pscheme->short_name));
    150 
    151 	/* write edited "MBR" onto disk. */
    152 	if (!parts->pscheme->write_to_disk(parts)) {
    153 		msg_display(MSG_wmbrfail);
    154 		process_menu(MENU_ok, NULL);
    155 		return false;
    156 	}
    157 	return true;
    158 }
    159 
    160 /*
    161  * hook called after writing disklabel to new target disk.
    162  */
    163 bool
    164 md_post_disklabel(struct install_partition_desc *install,
    165     struct disk_partitions *parts)
    166 {
    167 #if 0
    168 	/* Sector forwarding / badblocks ... */
    169 	if (*pm->doessf) {
    170 		msg_display(MSG_dobad144);
    171 		return run_program(RUN_DISPLAY, "/usr/sbin/bad144 %s 0",
    172 		    pm->diskdev);
    173 	}
    174 #endif
    175 
    176 	return true;
    177 }
    178 
    179 /*
    180  * hook called after upgrade() or install() has finished setting
    181  * up the target disk but immediately before the user is given the
    182  * ``disks are now set up'' message.
    183  */
    184 int
    185 md_post_newfs(struct install_partition_desc *install)
    186 {
    187 	struct mbr_sector pbr;
    188 	char adevname[STRSIZE];
    189 	ssize_t sz;
    190 	int fd = -1;
    191 
    192 	snprintf(adevname, sizeof(adevname), "/dev/r%sa", pm->diskdev);
    193 	fd = open(adevname, O_RDWR);
    194 	if (fd < 0)
    195 		goto out;
    196 
    197 	/* Read partition boot record */
    198 	sz = pread(fd, &pbr, sizeof(pbr), 0);
    199 	if (sz != sizeof(pbr))
    200 		goto out;
    201 
    202 	/* Check magic number */
    203 	if (pbr.mbr_magic != le16toh(MBR_MAGIC))
    204 		goto out;
    205 
    206 #define	OSNAME	"NetBSD60"
    207 	/* Update oemname */
    208 	memcpy(&pbr.mbr_oemname, OSNAME, sizeof(OSNAME) - 1);
    209 
    210 	/* Clear BPB */
    211 	memset(&pbr.mbr_bpb, 0, sizeof(pbr.mbr_bpb));
    212 
    213 	/* write-backed new patition boot record */
    214 	(void)pwrite(fd, &pbr, sizeof(pbr), 0);
    215 
    216 out:
    217 	if (fd >= 0)
    218 		close(fd);
    219 	return 0;
    220 }
    221 
    222 int
    223 md_post_extract(struct install_partition_desc *install)
    224 {
    225 	return 0;
    226 }
    227 
    228 void
    229 md_cleanup_install(struct install_partition_desc *install)
    230 {
    231 #ifndef DEBUG
    232 	enable_rc_conf();
    233 #endif
    234 }
    235 
    236 int
    237 md_pre_update(struct install_partition_desc *install)
    238 {
    239 	return 1;
    240 }
    241 
    242 /* Upgrade support */
    243 int
    244 md_update(struct install_partition_desc *install)
    245 {
    246 	md_post_newfs(install);
    247 	return 1;
    248 }
    249 
    250 int
    251 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
    252 {
    253 	return 2;
    254 }
    255 
    256 bool
    257 md_parts_use_wholedisk(struct disk_partitions *parts)
    258 {
    259 	return parts_use_wholedisk(parts, 0, NULL);
    260 }
    261 
    262 int
    263 md_pre_mount(struct install_partition_desc *install, size_t ndx)
    264 {
    265 	return 0;
    266 }
    267 
    268 bool
    269 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
    270 {
    271 	return false;	/* no change, no need to write back */
    272 }
    273 
    274 #ifdef HAVE_GPT
    275 bool
    276 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
    277     bool root_is_new, part_id efi_id, bool efi_is_new)
    278 {
    279 	/* no GPT boot support, nothing needs to be done here */
    280 	return true;
    281 }
    282 #endif
    283 
    284