Home | History | Annotate | Line # | Download | only in arch
macppc.c revision 1.3
      1 /*	$NetBSD: macppc.c,v 1.3 2002/05/15 15:43:01 lukem 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 #include <sys/cdefs.h>
     40 #if defined(__RCSID) && !defined(__lint)
     41 __RCSID("$NetBSD: macppc.c,v 1.3 2002/05/15 15:43:01 lukem Exp $");
     42 #endif	/* !__lint */
     43 
     44 #if HAVE_CONFIG_H
     45 #include "config.h"
     46 #endif
     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 	0,
     65 };
     66 
     67 static int writeapplepartmap(ib_params *, struct bbinfo_params *, char *);
     68 
     69 
     70 int
     71 macppc_clearboot(ib_params *params)
     72 {
     73 
     74 	assert(params != NULL);
     75 
     76 	if (params->flags & IB_STAGE1START) {
     77 		warnx("`-b bno' is not supported for %s",
     78 		    params->machine->name);
     79 		return (0);
     80 	}
     81 		/* XXX: maybe clear the apple partition map too? */
     82 	return (shared_bbinfo_clearboot(params, &bbparams));
     83 }
     84 
     85 static int
     86 writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params, char *bb)
     87 {
     88 	struct apple_drvr_map dm;
     89 	struct apple_part_map_entry pme;
     90 
     91 	assert (params != NULL);
     92 	assert (bb_params != NULL);
     93 	assert (bb != NULL);
     94 
     95 	if (params->flags & IB_NOWRITE)
     96 		return (1);
     97 
     98 		/* block 0: driver map  */
     99 	if (pread(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
    100 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    101 		warn("Can't read sector 0 of `%s'", params->filesystem);
    102 		return (0);
    103 	}
    104 	dm.sbSig =		htobe16(APPLE_DRVR_MAP_MAGIC);
    105 	dm.sbBlockSize =	htobe16(512);
    106 	dm.sbBlkCount =		htobe32(0);
    107 	if (pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
    108 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    109 		warn("Can't write sector 0 of `%s'", params->filesystem);
    110 		return (0);
    111 	}
    112 
    113 		/* block 1: Apple Partition Map */
    114 	memset(&pme, 0, sizeof(pme));
    115 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
    116 	pme.pmMapBlkCnt =	htobe32(2);
    117 	pme.pmPyPartStart =	htobe32(1);
    118 	pme.pmPartBlkCnt =	htobe32(2);
    119 	pme.pmDataCnt =		htobe32(2);
    120 	strlcpy(pme.pmPartName, "Apple", sizeof(pme.pmPartName));
    121 	strlcpy(pme.pmPartType, "Apple_partition_map", sizeof(pme.pmPartType));
    122 	pme.pmPartStatus =	htobe32(0x37);
    123 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
    124 	    1 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    125 		warn("Can't write Apple Partition Map into sector 1 of `%s'",
    126 		    params->filesystem);
    127 		return (0);
    128 	}
    129 
    130 		/* block 2: NetBSD partition */
    131 	memset(&pme, 0, sizeof(pme));
    132 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
    133 	pme.pmMapBlkCnt =	htobe32(2);
    134 	pme.pmPyPartStart =	htobe32(4);
    135 	pme.pmPartBlkCnt =	htobe32(0x7fffffff);
    136 	pme.pmDataCnt =		htobe32(0x7fffffff);
    137 	strlcpy(pme.pmPartName, "NetBSD", sizeof(pme.pmPartName));
    138 	strlcpy(pme.pmPartType, "NetBSD/macppc", sizeof(pme.pmPartType));
    139 	pme.pmPartStatus =	htobe32(0x3b);
    140 	pme.pmBootSize =	htobe32(0x400);
    141 	pme.pmBootLoad =	htobe32(0x4000);
    142 	pme.pmBootEntry =	htobe32(0x4000);
    143 	strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
    144 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
    145 	    2 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
    146 		warn("Can't write Apple Partition Map into sector 2 of `%s'",
    147 		    params->filesystem);
    148 		return (0);
    149 	}
    150 
    151 	return (1);
    152 }
    153 
    154 int
    155 macppc_setboot(ib_params *params)
    156 {
    157 
    158 	assert(params != NULL);
    159 
    160 	if (params->flags & IB_STAGE1START) {
    161 		warnx("`-b bno' is not supported for %s",
    162 		    params->machine->name);
    163 		return (0);
    164 	}
    165 	return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap));
    166 }
    167