1 /* $NetBSD: macppc.c,v 1.5 2002/05/20 16:05:27 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.5 2002/05/20 16:05:27 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 BBINFO_BIG_ENDIAN, 65 }; 66 67 static int writeapplepartmap(ib_params *, struct bbinfo_params *, uint8_t *); 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, NULL)); 83 } 84 85 int 86 macppc_setboot(ib_params *params) 87 { 88 89 assert(params != NULL); 90 91 if (params->flags & IB_STAGE1START) { 92 warnx("`-b bno' is not supported for %s", 93 params->machine->name); 94 return (0); 95 } 96 return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap)); 97 } 98 99 100 static int 101 writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params, 102 uint8_t *bb) 103 { 104 struct apple_drvr_map dm; 105 struct apple_part_map_entry pme; 106 107 assert (params != NULL); 108 assert (bb_params != NULL); 109 assert (bb != NULL); 110 111 if (params->flags & IB_NOWRITE) 112 return (1); 113 114 /* block 0: driver map */ 115 if (pread(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) != 116 MACPPC_BOOT_BLOCK_BLOCKSIZE) { 117 warn("Can't read sector 0 of `%s'", params->filesystem); 118 return (0); 119 } 120 dm.sbSig = htobe16(APPLE_DRVR_MAP_MAGIC); 121 dm.sbBlockSize = htobe16(512); 122 dm.sbBlkCount = htobe32(0); 123 if (pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) != 124 MACPPC_BOOT_BLOCK_BLOCKSIZE) { 125 warn("Can't write sector 0 of `%s'", params->filesystem); 126 return (0); 127 } 128 129 /* block 1: Apple Partition Map */ 130 memset(&pme, 0, sizeof(pme)); 131 pme.pmSig = htobe16(APPLE_PART_MAP_ENTRY_MAGIC); 132 pme.pmMapBlkCnt = htobe32(2); 133 pme.pmPyPartStart = htobe32(1); 134 pme.pmPartBlkCnt = htobe32(2); 135 pme.pmDataCnt = htobe32(2); 136 strlcpy(pme.pmPartName, "Apple", sizeof(pme.pmPartName)); 137 strlcpy(pme.pmPartType, "Apple_partition_map", sizeof(pme.pmPartType)); 138 pme.pmPartStatus = htobe32(0x37); 139 if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE, 140 1 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) { 141 warn("Can't write Apple Partition Map into sector 1 of `%s'", 142 params->filesystem); 143 return (0); 144 } 145 146 /* block 2: NetBSD partition */ 147 memset(&pme, 0, sizeof(pme)); 148 pme.pmSig = htobe16(APPLE_PART_MAP_ENTRY_MAGIC); 149 pme.pmMapBlkCnt = htobe32(2); 150 pme.pmPyPartStart = htobe32(4); 151 pme.pmPartBlkCnt = htobe32(0x7fffffff); 152 pme.pmDataCnt = htobe32(0x7fffffff); 153 strlcpy(pme.pmPartName, "NetBSD", sizeof(pme.pmPartName)); 154 strlcpy(pme.pmPartType, "NetBSD/macppc", sizeof(pme.pmPartType)); 155 pme.pmPartStatus = htobe32(0x3b); 156 pme.pmBootSize = htobe32(roundup(params->s1stat.st_size, 512)); 157 pme.pmBootLoad = htobe32(0x4000); 158 pme.pmBootEntry = htobe32(0x4000); 159 strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor)); 160 if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE, 161 2 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) { 162 warn("Can't write Apple Partition Map into sector 2 of `%s'", 163 params->filesystem); 164 return (0); 165 } 166 167 return (1); 168 } 169