Home | History | Annotate | Line # | Download | only in arch
macppc.c revision 1.9
      1 /*	$NetBSD: macppc.c,v 1.9 2008/04/28 20:24:16 martin 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.9 2008/04/28 20:24:16 martin Exp $");
     39 #endif	/* !__lint */
     40 
     41 #include <sys/param.h>
     42 
     43 #include <assert.h>
     44 #include <err.h>
     45 #include <stdio.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 
     49 #include "installboot.h"
     50 
     51 static struct bbinfo_params bbparams = {
     52 	MACPPC_BBINFO_MAGIC,
     53 	MACPPC_BOOT_BLOCK_OFFSET,
     54 	MACPPC_BOOT_BLOCK_BLOCKSIZE,
     55 	MACPPC_BOOT_BLOCK_MAX_SIZE,
     56 	0,
     57 	BBINFO_BIG_ENDIAN,
     58 };
     59 
     60 static int writeapplepartmap(ib_params *, struct bbinfo_params *, uint8_t *);
     61 
     62 static int macppc_clearboot(ib_params *);
     63 static int macppc_setboot(ib_params *);
     64 
     65 struct ib_mach ib_mach_macppc =
     66 	{ "macppc", macppc_setboot, macppc_clearboot, no_editboot,
     67 		IB_STAGE2START };
     68 
     69 static int
     70 macppc_clearboot(ib_params *params)
     71 {
     72 
     73 	assert(params != NULL);
     74 
     75 		/* XXX: maybe clear the apple partition map too? */
     76 	return (shared_bbinfo_clearboot(params, &bbparams, NULL));
     77 }
     78 
     79 static int
     80 macppc_setboot(ib_params *params)
     81 {
     82 
     83 	assert(params != NULL);
     84 
     85 	return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap));
     86 }
     87 
     88 
     89 static int
     90 writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params,
     91     uint8_t *bb)
     92 {
     93 	struct apple_drvr_map dm;
     94 	struct apple_part_map_entry pme;
     95 
     96 	assert (params != NULL);
     97 	assert (bb_params != NULL);
     98 	assert (bb != NULL);
     99 
    100 	if (params->flags & IB_NOWRITE)
    101 		return (1);
    102 
    103 		/* block 0: driver map  */
    104 	if (pread(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
    105 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    106 		warn("Can't read sector 0 of `%s'", params->filesystem);
    107 		return (0);
    108 	}
    109 	dm.sbSig =		htobe16(APPLE_DRVR_MAP_MAGIC);
    110 	dm.sbBlockSize =	htobe16(512);
    111 	dm.sbBlkCount =		htobe32(0);
    112 	if (pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
    113 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    114 		warn("Can't write sector 0 of `%s'", params->filesystem);
    115 		return (0);
    116 	}
    117 
    118 		/* block 1: Apple Partition Map */
    119 	memset(&pme, 0, sizeof(pme));
    120 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
    121 	pme.pmMapBlkCnt =	htobe32(2);
    122 	pme.pmPyPartStart =	htobe32(1);
    123 	pme.pmPartBlkCnt =	htobe32(2);
    124 	pme.pmDataCnt =		htobe32(2);
    125 	strlcpy(pme.pmPartName, "Apple", sizeof(pme.pmPartName));
    126 	strlcpy(pme.pmPartType, "Apple_partition_map", sizeof(pme.pmPartType));
    127 	pme.pmPartStatus =	htobe32(0x37);
    128 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
    129 	    1 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    130 		warn("Can't write Apple Partition Map into sector 1 of `%s'",
    131 		    params->filesystem);
    132 		return (0);
    133 	}
    134 
    135 		/* block 2: NetBSD partition */
    136 	memset(&pme, 0, sizeof(pme));
    137 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
    138 	pme.pmMapBlkCnt =	htobe32(2);
    139 	pme.pmPyPartStart =	htobe32(4);
    140 	pme.pmPartBlkCnt =	htobe32(0x7fffffff);
    141 	pme.pmDataCnt =		htobe32(0x7fffffff);
    142 	strlcpy(pme.pmPartName, "NetBSD", sizeof(pme.pmPartName));
    143 	strlcpy(pme.pmPartType, "NetBSD/macppc", sizeof(pme.pmPartType));
    144 	pme.pmPartStatus =	htobe32(0x3b);
    145 	pme.pmBootSize =	htobe32(roundup(params->s1stat.st_size, 512));
    146 	pme.pmBootLoad =	htobe32(0x4000);
    147 	pme.pmBootEntry =	htobe32(0x4000);
    148 	strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
    149 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
    150 	    2 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    151 		warn("Can't write Apple Partition Map into sector 2 of `%s'",
    152 		    params->filesystem);
    153 		return (0);
    154 	}
    155 
    156 	return (1);
    157 }
    158