xref: /netbsd-src/usr.sbin/installboot/arch/macppc.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: macppc.c,v 1.7 2003/10/27 00:12:44 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 #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.7 2003/10/27 00:12:44 lukem 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 
70 int
71 macppc_clearboot(ib_params *params)
72 {
73 
74 	assert(params != NULL);
75 
76 		/* XXX: maybe clear the apple partition map too? */
77 	return (shared_bbinfo_clearboot(params, &bbparams, NULL));
78 }
79 
80 int
81 macppc_setboot(ib_params *params)
82 {
83 
84 	assert(params != NULL);
85 
86 	return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap));
87 }
88 
89 
90 static int
91 writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params,
92     uint8_t *bb)
93 {
94 	struct apple_drvr_map dm;
95 	struct apple_part_map_entry pme;
96 
97 	assert (params != NULL);
98 	assert (bb_params != NULL);
99 	assert (bb != NULL);
100 
101 	if (params->flags & IB_NOWRITE)
102 		return (1);
103 
104 		/* block 0: driver map  */
105 	if (pread(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
106 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
107 		warn("Can't read sector 0 of `%s'", params->filesystem);
108 		return (0);
109 	}
110 	dm.sbSig =		htobe16(APPLE_DRVR_MAP_MAGIC);
111 	dm.sbBlockSize =	htobe16(512);
112 	dm.sbBlkCount =		htobe32(0);
113 	if (pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
114 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
115 		warn("Can't write sector 0 of `%s'", params->filesystem);
116 		return (0);
117 	}
118 
119 		/* block 1: Apple Partition Map */
120 	memset(&pme, 0, sizeof(pme));
121 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
122 	pme.pmMapBlkCnt =	htobe32(2);
123 	pme.pmPyPartStart =	htobe32(1);
124 	pme.pmPartBlkCnt =	htobe32(2);
125 	pme.pmDataCnt =		htobe32(2);
126 	strlcpy(pme.pmPartName, "Apple", sizeof(pme.pmPartName));
127 	strlcpy(pme.pmPartType, "Apple_partition_map", sizeof(pme.pmPartType));
128 	pme.pmPartStatus =	htobe32(0x37);
129 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
130 	    1 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
131 		warn("Can't write Apple Partition Map into sector 1 of `%s'",
132 		    params->filesystem);
133 		return (0);
134 	}
135 
136 		/* block 2: NetBSD partition */
137 	memset(&pme, 0, sizeof(pme));
138 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
139 	pme.pmMapBlkCnt =	htobe32(2);
140 	pme.pmPyPartStart =	htobe32(4);
141 	pme.pmPartBlkCnt =	htobe32(0x7fffffff);
142 	pme.pmDataCnt =		htobe32(0x7fffffff);
143 	strlcpy(pme.pmPartName, "NetBSD", sizeof(pme.pmPartName));
144 	strlcpy(pme.pmPartType, "NetBSD/macppc", sizeof(pme.pmPartType));
145 	pme.pmPartStatus =	htobe32(0x3b);
146 	pme.pmBootSize =	htobe32(roundup(params->s1stat.st_size, 512));
147 	pme.pmBootLoad =	htobe32(0x4000);
148 	pme.pmBootEntry =	htobe32(0x4000);
149 	strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
150 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
151 	    2 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
152 		warn("Can't write Apple Partition Map into sector 2 of `%s'",
153 		    params->filesystem);
154 		return (0);
155 	}
156 
157 	return (1);
158 }
159