xref: /minix3/usr.sbin/installboot/arch/macppc.c (revision 9f8e6353e54579f3834bed052b7960946fc4bb01)
1*9f8e6353SEvgeniy Ivanov /*	$NetBSD: macppc.c,v 1.11 2008/05/24 19:15:21 tsutsui Exp $ */
2*9f8e6353SEvgeniy Ivanov 
3*9f8e6353SEvgeniy Ivanov /*-
4*9f8e6353SEvgeniy Ivanov  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5*9f8e6353SEvgeniy Ivanov  * All rights reserved.
6*9f8e6353SEvgeniy Ivanov  *
7*9f8e6353SEvgeniy Ivanov  * This code is derived from software contributed to The NetBSD Foundation
8*9f8e6353SEvgeniy Ivanov  * by Luke Mewburn.
9*9f8e6353SEvgeniy Ivanov  *
10*9f8e6353SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
11*9f8e6353SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
12*9f8e6353SEvgeniy Ivanov  * are met:
13*9f8e6353SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
14*9f8e6353SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
15*9f8e6353SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
16*9f8e6353SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
17*9f8e6353SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
18*9f8e6353SEvgeniy Ivanov  *
19*9f8e6353SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*9f8e6353SEvgeniy Ivanov  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*9f8e6353SEvgeniy Ivanov  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*9f8e6353SEvgeniy Ivanov  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*9f8e6353SEvgeniy Ivanov  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*9f8e6353SEvgeniy Ivanov  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*9f8e6353SEvgeniy Ivanov  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*9f8e6353SEvgeniy Ivanov  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*9f8e6353SEvgeniy Ivanov  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*9f8e6353SEvgeniy Ivanov  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*9f8e6353SEvgeniy Ivanov  * POSSIBILITY OF SUCH DAMAGE.
30*9f8e6353SEvgeniy Ivanov  */
31*9f8e6353SEvgeniy Ivanov 
32*9f8e6353SEvgeniy Ivanov #if HAVE_NBTOOL_CONFIG_H
33*9f8e6353SEvgeniy Ivanov #include "nbtool_config.h"
34*9f8e6353SEvgeniy Ivanov #endif
35*9f8e6353SEvgeniy Ivanov 
36*9f8e6353SEvgeniy Ivanov #include <sys/cdefs.h>
37*9f8e6353SEvgeniy Ivanov #if !defined(__lint)
38*9f8e6353SEvgeniy Ivanov __RCSID("$NetBSD: macppc.c,v 1.11 2008/05/24 19:15:21 tsutsui Exp $");
39*9f8e6353SEvgeniy Ivanov #endif	/* !__lint */
40*9f8e6353SEvgeniy Ivanov 
41*9f8e6353SEvgeniy Ivanov #include <sys/param.h>
42*9f8e6353SEvgeniy Ivanov #ifndef HAVE_NBTOOL_CONFIG_H
43*9f8e6353SEvgeniy Ivanov #include <sys/ioctl.h>
44*9f8e6353SEvgeniy Ivanov #include <sys/dkio.h>
45*9f8e6353SEvgeniy Ivanov #include <errno.h>
46*9f8e6353SEvgeniy Ivanov #endif
47*9f8e6353SEvgeniy Ivanov 
48*9f8e6353SEvgeniy Ivanov #include <assert.h>
49*9f8e6353SEvgeniy Ivanov #include <err.h>
50*9f8e6353SEvgeniy Ivanov #include <stdio.h>
51*9f8e6353SEvgeniy Ivanov #include <string.h>
52*9f8e6353SEvgeniy Ivanov #include <unistd.h>
53*9f8e6353SEvgeniy Ivanov 
54*9f8e6353SEvgeniy Ivanov #include "installboot.h"
55*9f8e6353SEvgeniy Ivanov 
56*9f8e6353SEvgeniy Ivanov static struct bbinfo_params bbparams = {
57*9f8e6353SEvgeniy Ivanov 	MACPPC_BBINFO_MAGIC,
58*9f8e6353SEvgeniy Ivanov 	MACPPC_BOOT_BLOCK_OFFSET,
59*9f8e6353SEvgeniy Ivanov 	MACPPC_BOOT_BLOCK_BLOCKSIZE,
60*9f8e6353SEvgeniy Ivanov 	MACPPC_BOOT_BLOCK_MAX_SIZE,
61*9f8e6353SEvgeniy Ivanov 	0,
62*9f8e6353SEvgeniy Ivanov 	BBINFO_BIG_ENDIAN,
63*9f8e6353SEvgeniy Ivanov };
64*9f8e6353SEvgeniy Ivanov 
65*9f8e6353SEvgeniy Ivanov static int writeapplepartmap(ib_params *, struct bbinfo_params *, uint8_t *);
66*9f8e6353SEvgeniy Ivanov 
67*9f8e6353SEvgeniy Ivanov static int macppc_clearboot(ib_params *);
68*9f8e6353SEvgeniy Ivanov static int macppc_setboot(ib_params *);
69*9f8e6353SEvgeniy Ivanov 
70*9f8e6353SEvgeniy Ivanov struct ib_mach ib_mach_macppc =
71*9f8e6353SEvgeniy Ivanov 	{ "macppc", macppc_setboot, macppc_clearboot, no_editboot,
72*9f8e6353SEvgeniy Ivanov 		IB_STAGE2START };
73*9f8e6353SEvgeniy Ivanov 
74*9f8e6353SEvgeniy Ivanov static int
macppc_clearboot(ib_params * params)75*9f8e6353SEvgeniy Ivanov macppc_clearboot(ib_params *params)
76*9f8e6353SEvgeniy Ivanov {
77*9f8e6353SEvgeniy Ivanov 
78*9f8e6353SEvgeniy Ivanov 	assert(params != NULL);
79*9f8e6353SEvgeniy Ivanov 
80*9f8e6353SEvgeniy Ivanov 		/* XXX: maybe clear the apple partition map too? */
81*9f8e6353SEvgeniy Ivanov 	return (shared_bbinfo_clearboot(params, &bbparams, NULL));
82*9f8e6353SEvgeniy Ivanov }
83*9f8e6353SEvgeniy Ivanov 
84*9f8e6353SEvgeniy Ivanov static int
macppc_setboot(ib_params * params)85*9f8e6353SEvgeniy Ivanov macppc_setboot(ib_params *params)
86*9f8e6353SEvgeniy Ivanov {
87*9f8e6353SEvgeniy Ivanov 
88*9f8e6353SEvgeniy Ivanov 	assert(params != NULL);
89*9f8e6353SEvgeniy Ivanov 
90*9f8e6353SEvgeniy Ivanov 	return (shared_bbinfo_setboot(params, &bbparams, writeapplepartmap));
91*9f8e6353SEvgeniy Ivanov }
92*9f8e6353SEvgeniy Ivanov 
93*9f8e6353SEvgeniy Ivanov 
94*9f8e6353SEvgeniy Ivanov static int
writeapplepartmap(ib_params * params,struct bbinfo_params * bb_params,uint8_t * bb)95*9f8e6353SEvgeniy Ivanov writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params,
96*9f8e6353SEvgeniy Ivanov     uint8_t *bb)
97*9f8e6353SEvgeniy Ivanov {
98*9f8e6353SEvgeniy Ivanov 	struct apple_drvr_map dm;
99*9f8e6353SEvgeniy Ivanov 	struct apple_part_map_entry pme;
100*9f8e6353SEvgeniy Ivanov 	int rv;
101*9f8e6353SEvgeniy Ivanov 
102*9f8e6353SEvgeniy Ivanov 	assert (params != NULL);
103*9f8e6353SEvgeniy Ivanov 	assert (bb_params != NULL);
104*9f8e6353SEvgeniy Ivanov 	assert (bb != NULL);
105*9f8e6353SEvgeniy Ivanov 
106*9f8e6353SEvgeniy Ivanov 	if (params->flags & IB_NOWRITE)
107*9f8e6353SEvgeniy Ivanov 		return (1);
108*9f8e6353SEvgeniy Ivanov 
109*9f8e6353SEvgeniy Ivanov 		/* block 0: driver map  */
110*9f8e6353SEvgeniy Ivanov 	if (pread(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0) !=
111*9f8e6353SEvgeniy Ivanov 	    MACPPC_BOOT_BLOCK_BLOCKSIZE) {
112*9f8e6353SEvgeniy Ivanov 		warn("Can't read sector 0 of `%s'", params->filesystem);
113*9f8e6353SEvgeniy Ivanov 		return (0);
114*9f8e6353SEvgeniy Ivanov 	}
115*9f8e6353SEvgeniy Ivanov 	dm.sbSig =		htobe16(APPLE_DRVR_MAP_MAGIC);
116*9f8e6353SEvgeniy Ivanov 	dm.sbBlockSize =	htobe16(512);
117*9f8e6353SEvgeniy Ivanov 	dm.sbBlkCount =		htobe32(0);
118*9f8e6353SEvgeniy Ivanov 
119*9f8e6353SEvgeniy Ivanov 	rv = pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0);
120*9f8e6353SEvgeniy Ivanov #ifdef DIOCWLABEL
121*9f8e6353SEvgeniy Ivanov 	if (rv == -1 && errno == EROFS) {
122*9f8e6353SEvgeniy Ivanov 		/*
123*9f8e6353SEvgeniy Ivanov 		 * block 0 is LABELSECTOR which might be protected by
124*9f8e6353SEvgeniy Ivanov 		 * bounds_check_with_label(9).
125*9f8e6353SEvgeniy Ivanov 		 */
126*9f8e6353SEvgeniy Ivanov 		int enable;
127*9f8e6353SEvgeniy Ivanov 
128*9f8e6353SEvgeniy Ivanov 		enable = 1;
129*9f8e6353SEvgeniy Ivanov 		rv = ioctl(params->fsfd, DIOCWLABEL, &enable);
130*9f8e6353SEvgeniy Ivanov 		if (rv != 0) {
131*9f8e6353SEvgeniy Ivanov 			warn("Cannot enable writes to the label sector");
132*9f8e6353SEvgeniy Ivanov 			return 0;
133*9f8e6353SEvgeniy Ivanov 		}
134*9f8e6353SEvgeniy Ivanov 
135*9f8e6353SEvgeniy Ivanov 		rv = pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0);
136*9f8e6353SEvgeniy Ivanov 
137*9f8e6353SEvgeniy Ivanov 		/* Reset write-protect. */
138*9f8e6353SEvgeniy Ivanov 		enable = 0;
139*9f8e6353SEvgeniy Ivanov 		(void)ioctl(params->fsfd, DIOCWLABEL, &enable);
140*9f8e6353SEvgeniy Ivanov 	}
141*9f8e6353SEvgeniy Ivanov #endif
142*9f8e6353SEvgeniy Ivanov 	if (rv != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
143*9f8e6353SEvgeniy Ivanov 		warn("Can't write sector 0 of `%s'", params->filesystem);
144*9f8e6353SEvgeniy Ivanov 		return (0);
145*9f8e6353SEvgeniy Ivanov 	}
146*9f8e6353SEvgeniy Ivanov 
147*9f8e6353SEvgeniy Ivanov 		/* block 1: Apple Partition Map */
148*9f8e6353SEvgeniy Ivanov 	memset(&pme, 0, sizeof(pme));
149*9f8e6353SEvgeniy Ivanov 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
150*9f8e6353SEvgeniy Ivanov 	pme.pmMapBlkCnt =	htobe32(2);
151*9f8e6353SEvgeniy Ivanov 	pme.pmPyPartStart =	htobe32(1);
152*9f8e6353SEvgeniy Ivanov 	pme.pmPartBlkCnt =	htobe32(2);
153*9f8e6353SEvgeniy Ivanov 	pme.pmDataCnt =		htobe32(2);
154*9f8e6353SEvgeniy Ivanov 	strlcpy(pme.pmPartName, "Apple", sizeof(pme.pmPartName));
155*9f8e6353SEvgeniy Ivanov 	strlcpy(pme.pmPartType, "Apple_partition_map", sizeof(pme.pmPartType));
156*9f8e6353SEvgeniy Ivanov 	pme.pmPartStatus =	htobe32(0x37);
157*9f8e6353SEvgeniy Ivanov 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
158*9f8e6353SEvgeniy Ivanov 	    1 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
159*9f8e6353SEvgeniy Ivanov 		warn("Can't write Apple Partition Map into sector 1 of `%s'",
160*9f8e6353SEvgeniy Ivanov 		    params->filesystem);
161*9f8e6353SEvgeniy Ivanov 		return (0);
162*9f8e6353SEvgeniy Ivanov 	}
163*9f8e6353SEvgeniy Ivanov 
164*9f8e6353SEvgeniy Ivanov 		/* block 2: NetBSD partition */
165*9f8e6353SEvgeniy Ivanov 	memset(&pme, 0, sizeof(pme));
166*9f8e6353SEvgeniy Ivanov 	pme.pmSig =		htobe16(APPLE_PART_MAP_ENTRY_MAGIC);
167*9f8e6353SEvgeniy Ivanov 	pme.pmMapBlkCnt =	htobe32(2);
168*9f8e6353SEvgeniy Ivanov 	pme.pmPyPartStart =	htobe32(4);
169*9f8e6353SEvgeniy Ivanov 	pme.pmPartBlkCnt =	htobe32(0x7fffffff);
170*9f8e6353SEvgeniy Ivanov 	pme.pmDataCnt =		htobe32(0x7fffffff);
171*9f8e6353SEvgeniy Ivanov 	strlcpy(pme.pmPartName, "NetBSD", sizeof(pme.pmPartName));
172*9f8e6353SEvgeniy Ivanov 	strlcpy(pme.pmPartType, "NetBSD/macppc", sizeof(pme.pmPartType));
173*9f8e6353SEvgeniy Ivanov 	pme.pmPartStatus =	htobe32(0x3b);
174*9f8e6353SEvgeniy Ivanov 	pme.pmBootSize =	htobe32(roundup(params->s1stat.st_size, 512));
175*9f8e6353SEvgeniy Ivanov 	pme.pmBootLoad =	htobe32(0x4000);
176*9f8e6353SEvgeniy Ivanov 	pme.pmBootEntry =	htobe32(0x4000);
177*9f8e6353SEvgeniy Ivanov 	strlcpy(pme.pmProcessor, "PowerPC", sizeof(pme.pmProcessor));
178*9f8e6353SEvgeniy Ivanov 	if (pwrite(params->fsfd, &pme, MACPPC_BOOT_BLOCK_BLOCKSIZE,
179*9f8e6353SEvgeniy Ivanov 	    2 * MACPPC_BOOT_BLOCK_BLOCKSIZE) != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
180*9f8e6353SEvgeniy Ivanov 		warn("Can't write Apple Partition Map into sector 2 of `%s'",
181*9f8e6353SEvgeniy Ivanov 		    params->filesystem);
182*9f8e6353SEvgeniy Ivanov 		return (0);
183*9f8e6353SEvgeniy Ivanov 	}
184*9f8e6353SEvgeniy Ivanov 
185*9f8e6353SEvgeniy Ivanov 	return (1);
186*9f8e6353SEvgeniy Ivanov }
187