Home | History | Annotate | Line # | Download | only in arch
      1 /*	$NetBSD: macppc.c,v 1.12 2019/05/07 04:35:31 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Luke Mewburn.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 #if !defined(__lint)
     38 __RCSID("$NetBSD: macppc.c,v 1.12 2019/05/07 04:35:31 thorpej Exp $");
     39 #endif	/* !__lint */
     40 
     41 #include <sys/param.h>
     42 #ifndef HAVE_NBTOOL_CONFIG_H
     43 #include <sys/ioctl.h>
     44 #include <sys/dkio.h>
     45 #include <errno.h>
     46 #endif
     47 
     48 #include <assert.h>
     49 #include <err.h>
     50 #include <stdio.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 
     54 #include "installboot.h"
     55 
     56 static struct bbinfo_params bbparams = {
     57 	MACPPC_BBINFO_MAGIC,
     58 	MACPPC_BOOT_BLOCK_OFFSET,
     59 	MACPPC_BOOT_BLOCK_BLOCKSIZE,
     60 	MACPPC_BOOT_BLOCK_MAX_SIZE,
     61 	0,
     62 	BBINFO_BIG_ENDIAN,
     63 };
     64 
     65 static int writeapplepartmap(ib_params *, struct bbinfo_params *, uint8_t *);
     66 
     67 static int macppc_clearboot(ib_params *);
     68 static int macppc_setboot(ib_params *);
     69 
     70 struct ib_mach ib_mach_macppc = {
     71 	.name		=	"macppc",
     72 	.setboot	=	macppc_setboot,
     73 	.clearboot	=	macppc_clearboot,
     74 	.editboot	=	no_editboot,
     75 	.valid_flags	=	IB_STAGE2START,
     76 };
     77 
     78 static int
     79 macppc_clearboot(ib_params *params)
     80 {
     81 
     82 	assert(params != NULL);
     83 
     84 		/* XXX: maybe clear the apple partition map too? */
     85 	return (shared_bbinfo_clearboot(params, &bbparams, NULL));
     86 }
     87 
     88 static int
     89 macppc_setboot(ib_params *params)
     90 {
     91 
     92 	assert(params != NULL);
     93 
     94 	return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap));
     95 }
     96 
     97 
     98 static int
     99 writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params,
    100     uint8_t *bb)
    101 {
    102 	struct apple_drvr_map dm;
    103 	struct apple_part_map_entry pme;
    104 	int rv;
    105 
    106 	assert (params != NULL);
    107 	assert (bb_params != NULL);
    108 	assert (bb != NULL);
    109 
    110 	if (params->flags & IB_NOWRITE)
    111 		return (1);
    112 
    113 		/* block 0: driver map  */
    114 	if (pread(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
    115 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    116 		warn("Can't read sector 0 of `%s'", params->filesystem);
    117 		return (0);
    118 	}
    119 	dm.sbSig =		htobe16(APPLE_DRVR_MAP_MAGIC);
    120 	dm.sbBlockSize =	htobe16(512);
    121 	dm.sbBlkCount =		htobe32(0);
    122 
    123 	rv = pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0);
    124 #ifdef DIOCWLABEL
    125 	if (rv == -1 && errno == EROFS) {
    126 		/*
    127 		 * block 0 is LABELSECTOR which might be protected by
    128 		 * bounds_check_with_label(9).
    129 		 */
    130 		int enable;
    131 
    132 		enable = 1;
    133 		rv = ioctl(params->fsfd, DIOCWLABEL, &enable);
    134 		if (rv != 0) {
    135 			warn("Cannot enable writes to the label sector");
    136 			return 0;
    137 		}
    138 
    139 		rv = pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0);
    140 
    141 		/* Reset write-protect. */
    142 		enable = 0;
    143 		(void)ioctl(params->fsfd, DIOCWLABEL, &enable);
    144 	}
    145 #endif
    146 	if (rv != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    147 		warn("Can't write sector 0 of `%s'", params->filesystem);
    148 		return (0);
    149 	}
    150 
    151 		/* block 1: Apple Partition Map */
    152 	memset(&pme, 0, sizeof(pme));
    153 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
    154 	pme.pmMapBlkCnt =	htobe32(2);
    155 	pme.pmPyPartStart =	htobe32(1);
    156 	pme.pmPartBlkCnt =	htobe32(2);
    157 	pme.pmDataCnt =		htobe32(2);
    158 	strlcpy(pme.pmPartName, "Apple", sizeof(pme.pmPartName));
    159 	strlcpy(pme.pmPartType, "Apple_partition_map", sizeof(pme.pmPartType));
    160 	pme.pmPartStatus =	htobe32(0x37);
    161 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
    162 	    1 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    163 		warn("Can't write Apple Partition Map into sector 1 of `%s'",
    164 		    params->filesystem);
    165 		return (0);
    166 	}
    167 
    168 		/* block 2: NetBSD partition */
    169 	memset(&pme, 0, sizeof(pme));
    170 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
    171 	pme.pmMapBlkCnt =	htobe32(2);
    172 	pme.pmPyPartStart =	htobe32(4);
    173 	pme.pmPartBlkCnt =	htobe32(0x7fffffff);
    174 	pme.pmDataCnt =		htobe32(0x7fffffff);
    175 	strlcpy(pme.pmPartName, "NetBSD", sizeof(pme.pmPartName));
    176 	strlcpy(pme.pmPartType, "NetBSD/macppc", sizeof(pme.pmPartType));
    177 	pme.pmPartStatus =	htobe32(0x3b);
    178 	pme.pmBootSize =	htobe32(roundup(params->s1stat.st_size, 512));
    179 	pme.pmBootLoad =	htobe32(0x4000);
    180 	pme.pmBootEntry =	htobe32(0x4000);
    181 	strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
    182 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
    183 	    2 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    184 		warn("Can't write Apple Partition Map into sector 2 of `%s'",
    185 		    params->filesystem);
    186 		return (0);
    187 	}
    188 
    189 	return (1);
    190 }
    191