Home | History | Annotate | Line # | Download | only in arch
macppc.c revision 1.8
      1 /*	$NetBSD: macppc.c,v 1.8 2006/02/18 10:08:07 dsl 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #if HAVE_NBTOOL_CONFIG_H
     40 #include "nbtool_config.h"
     41 #endif
     42 
     43 #include <sys/cdefs.h>
     44 #if !defined(__lint)
     45 __RCSID("$NetBSD: macppc.c,v 1.8 2006/02/18 10:08:07 dsl Exp $");
     46 #endif	/* !__lint */
     47 
     48 #include <sys/param.h>
     49 
     50 #include <assert.h>
     51 #include <err.h>
     52 #include <stdio.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #include "installboot.h"
     57 
     58 static struct bbinfo_params bbparams = {
     59 	MACPPC_BBINFO_MAGIC,
     60 	MACPPC_BOOT_BLOCK_OFFSET,
     61 	MACPPC_BOOT_BLOCK_BLOCKSIZE,
     62 	MACPPC_BOOT_BLOCK_MAX_SIZE,
     63 	0,
     64 	BBINFO_BIG_ENDIAN,
     65 };
     66 
     67 static int writeapplepartmap(ib_params *, struct bbinfo_params *, uint8_t *);
     68 
     69 static int macppc_clearboot(ib_params *);
     70 static int macppc_setboot(ib_params *);
     71 
     72 struct ib_mach ib_mach_macppc =
     73 	{ "macppc", macppc_setboot, macppc_clearboot, no_editboot,
     74 		IB_STAGE2START };
     75 
     76 static int
     77 macppc_clearboot(ib_params *params)
     78 {
     79 
     80 	assert(params != NULL);
     81 
     82 		/* XXX: maybe clear the apple partition map too? */
     83 	return (shared_bbinfo_clearboot(params, &bbparams, NULL));
     84 }
     85 
     86 static int
     87 macppc_setboot(ib_params *params)
     88 {
     89 
     90 	assert(params != NULL);
     91 
     92 	return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap));
     93 }
     94 
     95 
     96 static int
     97 writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params,
     98     uint8_t *bb)
     99 {
    100 	struct apple_drvr_map dm;
    101 	struct apple_part_map_entry pme;
    102 
    103 	assert (params != NULL);
    104 	assert (bb_params != NULL);
    105 	assert (bb != NULL);
    106 
    107 	if (params->flags & IB_NOWRITE)
    108 		return (1);
    109 
    110 		/* block 0: driver map  */
    111 	if (pread(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
    112 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    113 		warn("Can't read sector 0 of `%s'", params->filesystem);
    114 		return (0);
    115 	}
    116 	dm.sbSig =		htobe16(APPLE_DRVR_MAP_MAGIC);
    117 	dm.sbBlockSize =	htobe16(512);
    118 	dm.sbBlkCount =		htobe32(0);
    119 	if (pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
    120 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    121 		warn("Can't write sector 0 of `%s'", params->filesystem);
    122 		return (0);
    123 	}
    124 
    125 		/* block 1: Apple Partition Map */
    126 	memset(&pme, 0, sizeof(pme));
    127 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
    128 	pme.pmMapBlkCnt =	htobe32(2);
    129 	pme.pmPyPartStart =	htobe32(1);
    130 	pme.pmPartBlkCnt =	htobe32(2);
    131 	pme.pmDataCnt =		htobe32(2);
    132 	strlcpy(pme.pmPartName, "Apple", sizeof(pme.pmPartName));
    133 	strlcpy(pme.pmPartType, "Apple_partition_map", sizeof(pme.pmPartType));
    134 	pme.pmPartStatus =	htobe32(0x37);
    135 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
    136 	    1 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    137 		warn("Can't write Apple Partition Map into sector 1 of `%s'",
    138 		    params->filesystem);
    139 		return (0);
    140 	}
    141 
    142 		/* block 2: NetBSD partition */
    143 	memset(&pme, 0, sizeof(pme));
    144 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
    145 	pme.pmMapBlkCnt =	htobe32(2);
    146 	pme.pmPyPartStart =	htobe32(4);
    147 	pme.pmPartBlkCnt =	htobe32(0x7fffffff);
    148 	pme.pmDataCnt =		htobe32(0x7fffffff);
    149 	strlcpy(pme.pmPartName, "NetBSD", sizeof(pme.pmPartName));
    150 	strlcpy(pme.pmPartType, "NetBSD/macppc", sizeof(pme.pmPartType));
    151 	pme.pmPartStatus =	htobe32(0x3b);
    152 	pme.pmBootSize =	htobe32(roundup(params->s1stat.st_size, 512));
    153 	pme.pmBootLoad =	htobe32(0x4000);
    154 	pme.pmBootEntry =	htobe32(0x4000);
    155 	strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
    156 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
    157 	    2 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    158 		warn("Can't write Apple Partition Map into sector 2 of `%s'",
    159 		    params->filesystem);
    160 		return (0);
    161 	}
    162 
    163 	return (1);
    164 }
    165